17711
|
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 |
"{ Package: 'stx:libbasic' }"
|
|
13 |
|
|
14 |
"{ NameSpace: CharacterEncoderImplementations }"
|
|
15 |
|
|
16 |
TwoByteEncoder subclass:#JIS0208_to_SJIS
|
|
17 |
instanceVariableNames:''
|
|
18 |
classVariableNames:''
|
|
19 |
poolDictionaries:''
|
|
20 |
category:'Collections-Text-Encodings'
|
|
21 |
!
|
|
22 |
|
|
23 |
!JIS0208_to_SJIS class methodsFor:'documentation'!
|
|
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 |
! !
|
|
38 |
|
|
39 |
!JIS0208_to_SJIS class methodsFor:'mapping'!
|
|
40 |
|
|
41 |
mapFileURL1_relativePathName
|
|
42 |
^ 'OBSOLETE/EASTASIA/JIS/SHIFTJIS.TXT'
|
|
43 |
! !
|
|
44 |
|
|
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
|
|
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."
|
|
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 }"
|
|
67 |
any16bit romans|
|
|
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.
|
|
76 |
|
|
77 |
romans := CharacterEncoderImplementations::JIS0208 romanTable.
|
|
78 |
|
|
79 |
%{
|
17735
|
80 |
if (__isStringLike(aString)
|
17711
|
81 |
&& (__Class(newString) == @global(TwoByteString))) {
|
|
82 |
INT _dstIdx = 0, _srcIdx = 0;
|
|
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 |
|
|
88 |
while (_srcIdx < _sz) {
|
|
89 |
int _val;
|
|
90 |
|
|
91 |
_c1 = _cp[_srcIdx];
|
|
92 |
_srcIdx++;
|
|
93 |
|
|
94 |
if ((_srcIdx < _sz)
|
|
95 |
&& (((_c1 >= 129) && (_c1 <= 159))
|
|
96 |
|| ((_c1 >= 224) && (_c1 <= 239)))) {
|
|
97 |
_c2 = _cp[_srcIdx];
|
|
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;
|
|
119 |
_jcp[_dstIdx] = _val;
|
|
120 |
} else {
|
|
121 |
/* mhmh - append untranslated */
|
|
122 |
|
|
123 |
_jcp[_dstIdx] = _c1;
|
|
124 |
_dstIdx++;
|
|
125 |
_jcp[_dstIdx] = _c2;
|
|
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;
|
|
135 |
_jcp[_dstIdx] = _val;
|
|
136 |
} else {
|
|
137 |
/* roman characters are translated as per romanTable */
|
|
138 |
_jcp[_dstIdx] = _c1;
|
|
139 |
if ((romans != nil)
|
|
140 |
&& (__isArray(romans))
|
|
141 |
&& ((_c1 - 0x20) < __arraySize(romans))) {
|
|
142 |
any16bit = true;
|
|
143 |
_jcp[_dstIdx] = __intVal(__ArrayInstPtr(romans)->a_element[(_c1 - 0x20)]);
|
|
144 |
}
|
|
145 |
}
|
|
146 |
}
|
|
147 |
_dstIdx++;
|
|
148 |
}
|
|
149 |
getOutOfHere: ;
|
|
150 |
dstIdx = __mkSmallInteger(_dstIdx+1);
|
|
151 |
srcIdx = __mkSmallInteger(_srcIdx+1);
|
|
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.
|
|
161 |
b1 := char1 codePoint.
|
|
162 |
|
|
163 |
((srcIdx <= sz)
|
|
164 |
and:[(b1 >= 16r81"129" and:[b1 <= 16r9F"159"]) "/ SJIS1 81 .. 9F
|
|
165 |
or:[b1 >= 16rE0"224" and:[b1 <= 16rEF"239"]]]) ifTrue:[ "/ E0 .. EF
|
|
166 |
char2 := aString at:srcIdx.
|
|
167 |
srcIdx := srcIdx + 1.
|
|
168 |
b2 := char2 codePoint.
|
|
169 |
(b2 >= 16r40"64" and:[b2 <= 16rFC"252"]) ifTrue:[ "/ SJIS2 40 .. FC
|
|
170 |
|adjust rowOffs cellOffs|
|
|
171 |
|
|
172 |
adjust := (b2 < 16r9F"159") ifTrue:[1] ifFalse:[0].
|
|
173 |
rowOffs := b1 < 16rA0"160" ifTrue:[112] ifFalse:[176].
|
|
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:[
|
|
190 |
val > 16rFF ifTrue:[any16bit := true].
|
|
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.
|
|
204 |
val := val + (16r8E"142" bitShift:8).
|
|
205 |
any16bit := true.
|
|
206 |
newString at:dstIdx put:(Character value:val).
|
|
207 |
] ifFalse:[
|
|
208 |
"/ roman characters translated as per romanTable
|
|
209 |
newString at:dstIdx put:char1
|
|
210 |
romans isArray ifTrue:[
|
|
211 |
char1 codePoint < romans size ifTrue:[
|
|
212 |
any16bit := true.
|
|
213 |
newString at:dstIdx put:(Character value:(romans at:char1 codePoint-32+1)).
|
|
214 |
]
|
|
215 |
]
|
|
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 |
|
|
232 |
CharacterEncoderImplementations::JIS0208_to_SJIS decodeString:'hello'
|
|
233 |
(CharacterEncoder encoderFor:#sjis) decodeString:'hello'
|
|
234 |
|
|
235 |
CharacterEncoderImplementations::JIS0208_to_SJIS decodeString:('../../doc/online/japanese/TOP.html' asFilename contents asString)
|
|
236 |
|
|
237 |
'../../doc/online/japanese/TOP.html' asFilename contents asString
|
|
238 |
decodeFrom:#sjis
|
|
239 |
"
|
|
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 |
|
|
260 |
val := (c := aJISString at:srcIndex) codePoint.
|
|
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
|
|
325 |
! !
|
|
326 |
|
|
327 |
!JIS0208_to_SJIS methodsFor:'private'!
|
|
328 |
|
|
329 |
newString:size
|
|
330 |
^ JISEncodedString new:size
|
|
331 |
! !
|
|
332 |
|
|
333 |
!JIS0208_to_SJIS methodsFor:'queries'!
|
|
334 |
|
|
335 |
nameOfEncoding
|
|
336 |
^ #'sjis'
|
|
337 |
! !
|
|
338 |
|
|
339 |
!JIS0208_to_SJIS class methodsFor:'documentation'!
|
|
340 |
|
|
341 |
version
|
17845
|
342 |
^ '$Id: CharacterEncoderImplementations__JIS0208_to_SJIS.st 10656 2011-07-07 22:08:07Z vranyj1 $'
|
17735
|
343 |
!
|
|
344 |
|
|
345 |
version_CVS
|
17845
|
346 |
^ '§Header: /cvs/stx/stx/libbasic/Encoder_SJIS.st,v 1.12 2009/11/05 16:26:27 stefan Exp §'
|
|
347 |
! ! |