author | Claus Gittinger <cg@exept.de> |
Wed, 24 Oct 2012 18:36:16 +0200 | |
changeset 14426 | 6ac120dc7add |
parent 12483 | e5ad283c4f14 |
permissions | -rw-r--r-- |
8148 | 1 |
" |
2 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
8077 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
14 |
"{ NameSpace: CharacterEncoderImplementations }" |
|
15 |
||
8177 | 16 |
TwoByteEncoder subclass:#JIS0208_to_SJIS |
8077 | 17 |
instanceVariableNames:'' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Text-Encodings' |
|
21 |
! |
|
22 |
||
8177 | 23 |
!JIS0208_to_SJIS class methodsFor:'documentation'! |
8148 | 24 |
|
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! ! |
|
8077 | 38 |
|
8177 | 39 |
!JIS0208_to_SJIS class methodsFor:'mapping'! |
8077 | 40 |
|
41 |
mapFileURL1_relativePathName |
|
42 |
^ 'OBSOLETE/EASTASIA/JIS/SHIFTJIS.TXT' |
|
43 |
! ! |
|
44 |
||
8177 | 45 |
!JIS0208_to_SJIS class methodsFor:'queries'! |
46 |
||
47 |
nameOfDecodedCode |
|
48 |
"I encode sjis into jis" |
|
49 |
||
50 |
^ #'jis0208' |
|
51 |
! ! |
|
52 |
||
53 |
!JIS0208_to_SJIS methodsFor:'encoding & decoding'! |
|
54 |
||
55 |
decodeString:aString |
|
11298 | 56 |
"return a new JIS-Encoded-String containing the characters from aString, |
57 |
which are interpreted as Shift-JIS encoded singleByte chars. |
|
58 |
Shift-JIS is a leadyByte code, with a variable-length encoding." |
|
8177 | 59 |
|
60 |
|newString char1 char2 |
|
61 |
sz "{ Class: SmallInteger }" |
|
62 |
dstIdx "{ Class: SmallInteger }" |
|
63 |
srcIdx "{ Class: SmallInteger }" |
|
64 |
b1 "{ Class: SmallInteger }" |
|
65 |
b2 "{ Class: SmallInteger }" |
|
66 |
val "{ Class: SmallInteger }" |
|
8224
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
67 |
any16bit romans| |
8177 | 68 |
|
69 |
sz := aString size. |
|
70 |
sz == 0 ifTrue:[^ aString]. |
|
71 |
||
72 |
newString := TwoByteString new:sz. |
|
73 |
any16bit := false. |
|
74 |
dstIdx := 1. |
|
75 |
srcIdx := 1. |
|
8224
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
76 |
|
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
77 |
romans := CharacterEncoderImplementations::JIS0208 romanTable. |
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
78 |
|
8177 | 79 |
%{ |
12483
e5ad283c4f14
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11482
diff
changeset
|
80 |
if (__isStringLike(aString) |
8177 | 81 |
&& (__Class(newString) == @global(TwoByteString))) { |
8911 | 82 |
INT _dstIdx = 0, _srcIdx = 0; |
8177 | 83 |
int _sz = __intVal(sz); |
84 |
unsigned char *_cp = __stringVal(aString); |
|
85 |
unsigned char _c1, _c2; |
|
86 |
unsigned short *_jcp = (unsigned short *)__stringVal(newString); |
|
87 |
||
8224
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
88 |
while (_srcIdx < _sz) { |
8177 | 89 |
int _val; |
90 |
||
8224
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
91 |
_c1 = _cp[_srcIdx]; |
8177 | 92 |
_srcIdx++; |
93 |
||
8224
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
94 |
if ((_srcIdx < _sz) |
8177 | 95 |
&& (((_c1 >= 129) && (_c1 <= 159)) |
96 |
|| ((_c1 >= 224) && (_c1 <= 239)))) { |
|
8224
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
97 |
_c2 = _cp[_srcIdx]; |
8177 | 98 |
_srcIdx++; |
99 |
if ((_c2 >= 64) && (_c2 <= 252)) { |
|
100 |
int _adjust, _rowOffs, _cellOffs; |
|
101 |
int _b1, _b2; |
|
102 |
||
103 |
_adjust = (_c2 < 159) ? 1 : 0; |
|
104 |
_rowOffs = (_c1 < 160) ? 112 : 176; |
|
105 |
if (_adjust) { |
|
106 |
_cellOffs = 31 + ((_c2 > 127) ? 1 : 0); |
|
107 |
} else { |
|
108 |
_cellOffs = 126; |
|
109 |
} |
|
110 |
_b1 = ((_c1 - _rowOffs) << 1) - _adjust; |
|
111 |
_b2 = (_c2 - _cellOffs); |
|
112 |
_val = (_b1<<8) + _b2; |
|
113 |
if (_val <= 0) { |
|
114 |
/* decoder error - let smalltalk handle that */ |
|
115 |
_srcIdx -= 2; |
|
116 |
goto getOutOfHere; |
|
117 |
} |
|
118 |
if (_val > 0xFF) any16bit = true; |
|
8224
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
119 |
_jcp[_dstIdx] = _val; |
8177 | 120 |
} else { |
121 |
/* mhmh - append untranslated */ |
|
122 |
||
8224
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
123 |
_jcp[_dstIdx] = _c1; |
8177 | 124 |
_dstIdx++; |
8224
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
125 |
_jcp[_dstIdx] = _c2; |
8177 | 126 |
} |
127 |
} else { |
|
128 |
if ((_c1 >= 0xA1 /* 161 */) && (_c1 <= 0xDF /* 223 */)) { |
|
129 |
/* HALFWIDTH KATAKANA |
|
130 |
* map half-width katakana to 8E:xx |
|
131 |
*/ |
|
132 |
_val = _c1 - 128; |
|
133 |
_val = _val + 0x8E00; |
|
134 |
any16bit = true; |
|
8224
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
135 |
_jcp[_dstIdx] = _val; |
8177 | 136 |
} else { |
8224
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
137 |
/* roman characters are translated as per romanTable */ |
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
138 |
_jcp[_dstIdx] = _c1; |
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
139 |
if ((romans != nil) |
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
140 |
&& (__isArray(romans)) |
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
141 |
&& ((_c1 - 0x20) < __arraySize(romans))) { |
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
142 |
any16bit = true; |
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
143 |
_jcp[_dstIdx] = __intVal(__ArrayInstPtr(romans)->a_element[(_c1 - 0x20)]); |
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
144 |
} |
8177 | 145 |
} |
146 |
} |
|
147 |
_dstIdx++; |
|
148 |
} |
|
149 |
getOutOfHere: ; |
|
8913 | 150 |
dstIdx = __mkSmallInteger(_dstIdx+1); |
151 |
srcIdx = __mkSmallInteger(_srcIdx+1); |
|
8177 | 152 |
} |
153 |
%}. |
|
154 |
||
155 |
[srcIdx <= sz] whileTrue:[ |
|
156 |
"/ |
|
157 |
"/ scan for next character in 129..159 or 224..239 |
|
158 |
"/ |
|
159 |
char1 := aString at:srcIdx. |
|
160 |
srcIdx := srcIdx + 1. |
|
11482
8b354b82016e
asciiValue obsoleted by codePoint
Claus Gittinger <cg@exept.de>
parents:
11298
diff
changeset
|
161 |
b1 := char1 codePoint. |
8177 | 162 |
|
163 |
((srcIdx <= sz) |
|
8194 | 164 |
and:[(b1 >= 16r81"129" and:[b1 <= 16r9F"159"]) "/ SJIS1 81 .. 9F |
165 |
or:[b1 >= 16rE0"224" and:[b1 <= 16rEF"239"]]]) ifTrue:[ "/ E0 .. EF |
|
8177 | 166 |
char2 := aString at:srcIdx. |
167 |
srcIdx := srcIdx + 1. |
|
11482
8b354b82016e
asciiValue obsoleted by codePoint
Claus Gittinger <cg@exept.de>
parents:
11298
diff
changeset
|
168 |
b2 := char2 codePoint. |
8194 | 169 |
(b2 >= 16r40"64" and:[b2 <= 16rFC"252"]) ifTrue:[ "/ SJIS2 40 .. FC |
8177 | 170 |
|adjust rowOffs cellOffs| |
171 |
||
8194 | 172 |
adjust := (b2 < 16r9F"159") ifTrue:[1] ifFalse:[0]. |
173 |
rowOffs := b1 < 16rA0"160" ifTrue:[112] ifFalse:[176]. |
|
8177 | 174 |
adjust == 1 ifTrue:[ |
175 |
cellOffs := 31 + (b2 > 127 ifTrue:[1] ifFalse:[0]). |
|
176 |
] ifFalse:[ |
|
177 |
cellOffs := 126. |
|
178 |
]. |
|
179 |
b1 := ((b1 - rowOffs) bitShift:1) - adjust. |
|
180 |
b2 := (b2 - cellOffs). |
|
181 |
val := (b1 bitShift:8) + b2. |
|
182 |
val <= 0 ifTrue:[ |
|
183 |
DecodingError |
|
184 |
raiseWith:aString |
|
185 |
errorString:'SJIS decoding failed (not SJIS encoded ?)'. |
|
186 |
newString at:dstIdx put:char1. |
|
187 |
dstIdx := dstIdx + 1. |
|
188 |
newString at:dstIdx put:char2. |
|
189 |
] ifFalse:[ |
|
8911 | 190 |
val > 16rFF ifTrue:[any16bit := true]. |
8177 | 191 |
newString at:dstIdx put:(Character value:val). |
192 |
] |
|
193 |
] ifFalse:[ |
|
194 |
"/ mhmh - append untranslated |
|
195 |
||
196 |
newString at:dstIdx put:char1. |
|
197 |
dstIdx := dstIdx + 1. |
|
198 |
newString at:dstIdx put:char2. |
|
199 |
] |
|
200 |
] ifFalse:[ |
|
201 |
(b1 >= 16rA1 "161" and:[b1 <= 16rDF "223"]) ifTrue:[ "/ HALFWIDTH KATAKANA |
|
202 |
"/ map half-width katakan to 8E:xx |
|
203 |
val := b1 - 128. |
|
8194 | 204 |
val := val + (16r8E"142" bitShift:8). |
8911 | 205 |
any16bit := true. |
8177 | 206 |
newString at:dstIdx put:(Character value:val). |
207 |
] ifFalse:[ |
|
8224
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
208 |
"/ roman characters translated as per romanTable |
8177 | 209 |
newString at:dstIdx put:char1 |
8224
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
210 |
romans isArray ifTrue:[ |
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
211 |
char1 codePoint < romans size ifTrue:[ |
8911 | 212 |
any16bit := true. |
8225 | 213 |
newString at:dstIdx put:(Character value:(romans at:char1 codePoint-32+1)). |
8224
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
214 |
] |
4306c18b63c5
roman character translation fixed
Claus Gittinger <cg@exept.de>
parents:
8194
diff
changeset
|
215 |
] |
8177 | 216 |
] |
217 |
]. |
|
218 |
dstIdx := dstIdx + 1. |
|
219 |
]. |
|
220 |
any16bit ifFalse:[ |
|
221 |
newString := String fromString:newString |
|
222 |
]. |
|
223 |
||
224 |
(dstIdx-1) ~~ sz ifTrue:[ |
|
225 |
newString := newString copyTo:dstIdx - 1. |
|
226 |
]. |
|
227 |
||
228 |
^ newString |
|
229 |
||
230 |
"simple: |
|
231 |
||
11298 | 232 |
CharacterEncoderImplementations::JIS0208_to_SJIS decodeString:'hello' |
8177 | 233 |
(CharacterEncoder encoderFor:#sjis) decodeString:'hello' |
234 |
||
11298 | 235 |
CharacterEncoderImplementations::JIS0208_to_SJIS decodeString:('../../doc/online/japanese/TOP.html' asFilename contents asString) |
8177 | 236 |
|
237 |
'../../doc/online/japanese/TOP.html' asFilename contents asString |
|
238 |
decodeFrom:#sjis |
|
239 |
" |
|
8194 | 240 |
! |
241 |
||
242 |
encodeString:aJISString |
|
243 |
"return a new string with aJISStrings characters as SJIS encoded 8bit string. |
|
244 |
The resulting string is only useful to be stored on some external file, |
|
245 |
not for being displayed in an ST/X view." |
|
246 |
||
247 |
|sz "{ Class: SmallInteger }" |
|
248 |
rval "{ Class: SmallInteger }" |
|
249 |
val "{ Class: SmallInteger }" |
|
250 |
romans c out isSJIS| |
|
251 |
||
252 |
romans := JIS0208 romanTable. |
|
253 |
||
254 |
sz := aJISString size. |
|
255 |
sz == 0 ifTrue:[^ '']. |
|
256 |
||
257 |
out := WriteStream on:(String new:(sz * 2)). |
|
258 |
||
259 |
1 to:sz do:[:srcIndex | |
|
11482
8b354b82016e
asciiValue obsoleted by codePoint
Claus Gittinger <cg@exept.de>
parents:
11298
diff
changeset
|
260 |
val := (c := aJISString at:srcIndex) codePoint. |
8194 | 261 |
(val <= 128) ifTrue:[ |
262 |
"/ a control or ascii character |
|
263 |
out nextPut:c. |
|
264 |
] ifFalse:[ |
|
265 |
(val == 16rFFFF "invalid-char") ifTrue:[ |
|
266 |
out nextPut:Character space. |
|
267 |
] ifFalse:[ |
|
268 |
(val > 150 and:[val < 224]) ifTrue:[ |
|
269 |
"/ ascii subset |
|
270 |
out nextPut:c. |
|
271 |
] ifFalse:[ |
|
272 |
"/ should not happen ... |
|
273 |
val <= 255 ifTrue:[ |
|
274 |
out nextPut:c. |
|
275 |
] ifFalse:[ |
|
276 |
isSJIS := true. |
|
277 |
||
278 |
"/ check for HALFWIDTH KATAKANA |
|
279 |
"/ 8E:xx |
|
280 |
"/ NO: halfwidth katakana no longer generated |
|
281 |
"/ remains there as full-width katakana |
|
282 |
||
283 |
"/ (val bitAnd:16rFF00) == 16r8E00 ifTrue:[ |
|
284 |
"/ |b| |
|
285 |
"/ |
|
286 |
"/ b := (val bitAnd:16rFF) + 128. |
|
287 |
"/ (b >= 16rA1 "161" and:[b <= 16rDF "223"]) ifTrue:[ |
|
288 |
"/ out nextPut:(Character value:b). |
|
289 |
"/ isSJIS := false. |
|
290 |
"/ ]. |
|
291 |
"/ ]. |
|
292 |
||
293 |
isSJIS ifTrue:[ |
|
294 |
"/ check for a roman character |
|
295 |
(val between:"romanTable min" 16r2121 and:"romanTable max" 16r2573) ifTrue:[ |
|
296 |
rval := romans indexOf:val. |
|
297 |
rval ~~ 0 ifTrue:[ |
|
298 |
rval := rval - 1 + 32. |
|
299 |
rval <= 16r7F ifTrue:[ "/ do not translate halfwidth katakana |
|
300 |
out nextPut:(Character value:rval). |
|
301 |
isSJIS := false. |
|
302 |
] |
|
303 |
]. |
|
304 |
]. |
|
305 |
]. |
|
306 |
||
307 |
isSJIS ifTrue:[ |
|
308 |
|b1 b2 rowOffset cellOffset| |
|
309 |
||
310 |
b1 := (val bitShift:-8). |
|
311 |
b2 := (val bitAnd:16rFF). |
|
312 |
rowOffset := (b1 < 95) ifTrue:[112] ifFalse:[176]. |
|
313 |
cellOffset := b1 odd ifTrue:[(b2 > 95) ifTrue:[32] ifFalse:[31]] |
|
314 |
ifFalse:[126]. |
|
315 |
||
316 |
out nextPut:(Character value:(((b1 + 1) bitShift:-1) + rowOffset)). |
|
317 |
out nextPut:(Character value:b2 + cellOffset). |
|
318 |
] |
|
319 |
] |
|
320 |
] |
|
321 |
] |
|
322 |
]. |
|
323 |
]. |
|
324 |
^ out contents |
|
8177 | 325 |
! ! |
326 |
||
327 |
!JIS0208_to_SJIS methodsFor:'private'! |
|
8077 | 328 |
|
329 |
newString:size |
|
330 |
^ JISEncodedString new:size |
|
331 |
! ! |
|
332 |
||
8177 | 333 |
!JIS0208_to_SJIS methodsFor:'queries'! |
334 |
||
335 |
nameOfEncoding |
|
336 |
^ #'sjis' |
|
337 |
! ! |
|
338 |
||
339 |
!JIS0208_to_SJIS class methodsFor:'documentation'! |
|
8077 | 340 |
|
341 |
version |
|
12483
e5ad283c4f14
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11482
diff
changeset
|
342 |
^ '$Header: /cvs/stx/stx/libbasic/CharacterEncoderImplementations__SJIS.st,v 1.12 2009-11-05 16:26:27 stefan Exp $' |
e5ad283c4f14
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11482
diff
changeset
|
343 |
! |
e5ad283c4f14
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11482
diff
changeset
|
344 |
|
e5ad283c4f14
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11482
diff
changeset
|
345 |
version_CVS |
e5ad283c4f14
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11482
diff
changeset
|
346 |
^ '$Header: /cvs/stx/stx/libbasic/CharacterEncoderImplementations__SJIS.st,v 1.12 2009-11-05 16:26:27 stefan Exp $' |
8077 | 347 |
! ! |