CharacterEncoderImplementations__JIS0208_to_JIS7.st
changeset 8103 794d8e3f11d8
parent 8081 b468050174a9
child 8114 05274a80fcc4
equal deleted inserted replaced
8102:e0537422e2d3 8103:794d8e3f11d8
     1 "{ Encoding: utf8 }"
       
     2 
       
     3 "{ Package: 'stx:libbasic' }"
     1 "{ Package: 'stx:libbasic' }"
     4 
     2 
     5 "{ NameSpace: CharacterEncoderImplementations }"
     3 "{ NameSpace: CharacterEncoderImplementations }"
     6 
     4 
     7 TwoByteEncoder subclass:#JIS0208_to_JIS7
     5 TwoByteEncoder subclass:#JIS0208_to_JIS7
   122     ].
   120     ].
   123     ^ Jis7KanjiEscapeSequence.
   121     ^ Jis7KanjiEscapeSequence.
   124 
   122 
   125     "Created: 26.2.1996 / 17:38:08 / cg"
   123     "Created: 26.2.1996 / 17:38:08 / cg"
   126     "Modified: 30.6.1997 / 16:03:16 / cg"
   124     "Modified: 30.6.1997 / 16:03:16 / cg"
   127 !
       
   128 
       
   129 jis7KanjiOldEscapeSequence
       
   130     "return the escape sequence used to switch to kanji in some old jis7 encoded strings."
       
   131 
       
   132     Jis7KanjiOldEscapeSequence isNil ifTrue:[
       
   133         Jis7KanjiOldEscapeSequence := Character esc asString , '$@'..
       
   134     ].
       
   135     ^ Jis7KanjiOldEscapeSequence.
       
   136 !
   125 !
   137 
   126 
   138 jis7RomanEscapeSequence
   127 jis7RomanEscapeSequence
   139     "return the escape sequence used to switch to roman in jis7 encoded strings"
   128     "return the escape sequence used to switch to roman in jis7 encoded strings"
   140 
   129 
   230                     newString at:dstIdx put:c.
   219                     newString at:dstIdx put:c.
   231                     dstIdx := dstIdx + 1.
   220                     dstIdx := dstIdx + 1.
   232                 ].
   221                 ].
   233             ] ifFalse:[
   222             ] ifFalse:[
   234                 start to:(stop - 2) by:2 do:[:i |
   223                 start to:(stop - 2) by:2 do:[:i |
   235                     b1 := (aString at:i) asciiValue.
   224                     b1 := (aString at:i) codePoint.
   236                     b2 := (aString at:i+1) asciiValue.
   225                     b2 := (aString at:i+1) codePoint.
   237                     val := (b1 bitShift:8) bitOr:b2.
   226                     val := (b1 bitShift:8) bitOr:b2.
   238                     newString at:dstIdx put:(Character value:val).
   227                     newString at:dstIdx put:(Character value:val).
   239                     dstIdx := dstIdx + 1.
   228                     dstIdx := dstIdx + 1.
   240                 ]
   229                 ]
   241             ].
   230             ].
   326     sz == 0 ifTrue:[^ ''].
   315     sz == 0 ifTrue:[^ ''].
   327 
   316 
   328     out := WriteStream on:(String new:(sz * 2)).
   317     out := WriteStream on:(String new:(sz * 2)).
   329 
   318 
   330     1 to:sz do:[:srcIndex |
   319     1 to:sz do:[:srcIndex |
   331 	c := aJISString at:srcIndex.
   320         c := aJISString at:srcIndex.
   332 	b1 := c asciiValue.
   321         b1 := c codePoint.
   333 	b1 < 33 ifTrue:[
   322         b1 < 33 ifTrue:[
   334 	    "/ a control character
   323             "/ a control character
   335 	    inSingleByteMode ifFalse:[
   324             inSingleByteMode ifFalse:[
   336 		out nextPutAll:roman.
   325                 out nextPutAll:roman.
   337 		inSingleByteMode := true
   326                 inSingleByteMode := true
   338 	    ].
   327             ].
   339 	    out nextPut:c.
   328             out nextPut:c.
   340 	] ifFalse:[
   329         ] ifFalse:[
   341 	    "/ check for a roman character
   330             "/ check for a roman character
   342 	    "/ the two numbers below are romanTable min and romanTable max
   331             "/ the two numbers below are romanTable min and romanTable max
   343 	    (b1 between:16r2121 and:16r2573) ifTrue:[
   332             (b1 between:16r2121 and:16r2573) ifTrue:[
   344 		val := romans indexOf:b1.
   333                 val := romans indexOf:b1.
   345 		val2 := val - 1 + 32.
   334                 val2 := val - 1 + 32.
   346 		(val ~~ 0 and:[val2 <= 16r7F]) ifTrue:[
   335                 (val ~~ 0 and:[val2 <= 16r7F]) ifTrue:[
   347 		    inSingleByteMode ifFalse:[
   336                     inSingleByteMode ifFalse:[
   348 			out nextPutAll:roman.
   337                         out nextPutAll:roman.
   349 			inSingleByteMode := true
   338                         inSingleByteMode := true
   350 		    ].
   339                     ].
   351 		    out nextPut:(Character value:val2)
   340                     out nextPut:(Character value:val2)
   352 		] ifFalse:[
   341                 ] ifFalse:[
   353 		    inSingleByteMode ifTrue:[
   342                     inSingleByteMode ifTrue:[
   354 			out nextPutAll:kanji.
   343                         out nextPutAll:kanji.
   355 			inSingleByteMode := false
   344                         inSingleByteMode := false
   356 		    ].
   345                     ].
   357 		    out nextPut:(Character value:(b1 bitShift:-8)).
   346                     out nextPut:(Character value:(b1 bitShift:-8)).
   358 		    out nextPut:(Character value:(b1 bitAnd:16rFF)).
   347                     out nextPut:(Character value:(b1 bitAnd:16rFF)).
   359 		].
   348                 ].
   360 	    ] ifFalse:[
   349             ] ifFalse:[
   361 		b1 <= 255 ifTrue:[
   350                 b1 <= 255 ifTrue:[
   362 		    "/ mhmh - unrepresentable roman (national chars)
   351                     "/ mhmh - unrepresentable roman (national chars)
   363 "/                    b1 >= 160 ifTrue:[
   352 "/                    b1 >= 160 ifTrue:[
   364 "/                        ('no rep for ' , b1 printString) printNL.
   353 "/                        ('no rep for ' , b1 printString) printNL.
   365 "/                    ].
   354 "/                    ].
   366 		    "/ there are non-japanese characters in there...
   355                     "/ there are non-japanese characters in there...
   367 		    "/ assume that is OK (leave as is) ...
   356                     "/ assume that is OK (leave as is) ...
   368 "/                    EncodingFailedError
   357 "/                    EncodingFailedError
   369 "/                        raiseWith:aJISString
   358 "/                        raiseWith:aJISString
   370 "/                        errorString:'JIS7 encoding failed (contains 8-bit characters ?)'.
   359 "/                        errorString:'JIS7 encoding failed (contains 8-bit characters ?)'.
   371 
   360 
   372 		    inSingleByteMode ifFalse:[
   361                     inSingleByteMode ifFalse:[
   373 			out nextPutAll:roman.
   362                         out nextPutAll:roman.
   374 			inSingleByteMode := true
   363                         inSingleByteMode := true
   375 		    ].
   364                     ].
   376 		    out nextPut:c
   365                     out nextPut:c
   377 		] ifFalse:[
   366                 ] ifFalse:[
   378 		    inSingleByteMode ifTrue:[
   367                     inSingleByteMode ifTrue:[
   379 			out nextPutAll:kanji.
   368                         out nextPutAll:kanji.
   380 			inSingleByteMode := false
   369                         inSingleByteMode := false
   381 		    ].
   370                     ].
   382 		    out nextPut:(Character value:(b1 bitShift:-8)).
   371                     out nextPut:(Character value:(b1 bitShift:-8)).
   383 		    out nextPut:(Character value:(b1 bitAnd:16rFF)).
   372                     out nextPut:(Character value:(b1 bitAnd:16rFF)).
   384 		]
   373                 ]
   385 	    ]
   374             ]
   386 	].
   375         ].
   387     ].
   376     ].
   388     inSingleByteMode ifFalse:[
   377     inSingleByteMode ifFalse:[
   389 	out nextPutAll:roman.
   378         out nextPutAll:roman.
   390     ].
   379     ].
   391     ^ out contents
   380     ^ out contents
   392 
   381 
   393     "simple:
   382     "simple:
   394 
   383 
   397 ! !
   386 ! !
   398 
   387 
   399 !JIS0208_to_JIS7 class methodsFor:'documentation'!
   388 !JIS0208_to_JIS7 class methodsFor:'documentation'!
   400 
   389 
   401 version
   390 version
   402     ^ '$Header: /cvs/stx/stx/libbasic/CharacterEncoderImplementations__JIS0208_to_JIS7.st,v 1.1 2004-03-05 17:27:22 cg Exp $'
   391     ^ '$Header: /cvs/stx/stx/libbasic/CharacterEncoderImplementations__JIS0208_to_JIS7.st,v 1.2 2004-03-05 21:57:38 stefan Exp $'
   403 ! !
   392 ! !