1 /* Copyright (C) 1999  Free Software Foundation
2 
3    This file is part of libgcj.
4 
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8 
9 #include <config.h>
10 #include <gcj/cni.h>
11 #include <gnu/gcj/convert/Output_SJIS.h>
12 
13 extern unsigned short Unicode_to_JIS[];
14 
15 extern int trie_lookup (unsigned short *trie, unsigned short key);
16 
17 static jint
convert_TO_SJIS(gnu::gcj::convert::Output_SJIS * encoder,jchar * ptr,jint inlength)18 convert_TO_SJIS (gnu::gcj::convert::Output_SJIS *encoder,
19 			  jchar *ptr, jint inlength)
20 {
21   int orig_inlength = inlength;
22   jint outbuf_length = encoder->buf->length;
23   for (;;)
24     {
25       if (encoder->count >= outbuf_length)
26 	break;
27       if (encoder->pending >= 0)
28 	{
29 	  elements(encoder->buf)[encoder->count++] = encoder->pending;
30 	  encoder->pending = -1;
31 	  continue;
32 	}
33       if (inlength == 0)
34 	break;
35       jchar ch = *ptr++;
36       inlength--;
37       unsigned short val = trie_lookup(Unicode_to_JIS, ch);
38       if (val < 0xFF)
39 	{
40 	  if (val == 0xffff)
41 	    val = '?';
42 	}
43       else
44 	{
45 	  int b1 = val >> 8;
46 	  int b2 = val & 0xff;
47 	  // From Lunde: "CJKV Informatio Processing", O'Reilly, 1999:
48 	  int rowOffset = b1 < 95 ? 112 : 176;
49 	  int cellOffset = (b1 & 1) != 0 ? (b2 > 95 ? 32 : 31) : 126;
50 	  b1 = ((b1 + 1) >> 1) + rowOffset;
51 	  b2 += cellOffset;
52 	  val = b1;
53 	  encoder->pending = b2;
54 	}
55       elements(encoder->buf)[encoder->count++] = val;
56     }
57   return orig_inlength - inlength;
58 }
59 
60 jint
write(jcharArray inbuffer,jint inpos,jint inlength)61 gnu::gcj::convert::Output_SJIS::write (jcharArray inbuffer,
62 					 jint inpos, jint inlength)
63 {
64   return convert_TO_SJIS(this, &elements(inbuffer)[inpos], inlength);
65 }
66 
67 jint
write(jstring str,jint inpos,jint inlength,jcharArray)68 gnu::gcj::convert::Output_SJIS::write (jstring str, jint inpos,
69 					 jint inlength, jcharArray)
70 {
71   return convert_TO_SJIS(this, _Jv_GetStringChars(str)+inpos, inlength);
72 }
73