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