1 /*----------------------------------------------------------------------------
2                kanji.cc (several routines related to kanji character)
3                        This file is a part of topaz systems
4                   Copyright: Hisao Kawaura, All rights reserved
5                                    1997 - 98
6 
7 
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 
22 ----------------------------------------------------------------------------*/
23 
24 #include "kanji.h"
25 
maykanji(char c)26 bool maykanji(char c)
27 {
28 #if defined(TOPAZ_SJIS)
29     if (((unsigned char)c >= 0x81 && (unsigned char)c <= 0x9f) ||
30         ((unsigned char)c >= 0xe0 && (unsigned char)c <= 0xfc))
31       return true;
32     else
33       return false;
34 #else
35     if ((unsigned char)c >= 0xa1)
36       return true;
37     else
38       return false;
39 #endif
40 
41 }
42 
charlength(char c)43 int charlength(char c)
44 {
45   if (maykanji(c))
46     return 2;
47   else
48     return 1;
49 }
50 
charlengthback(const char * s,int pos)51 int charlengthback(const char *s, int pos)
52 {
53 #if defined(TOPAZ_SJIS)
54   int p = 0;
55   int last = 1;
56 
57   while(p < pos)
58     {
59       if (maykanji(s[p]))
60         {
61           last = 2;
62           p += 2;
63         }
64       else
65         {
66           last = 1;
67           p++;
68         }
69     }
70   return last;
71 #else
72   if (maykanji(s[pos-1]))
73     return 2;
74   else
75     return 1;
76 #endif
77 }
78 
iskanji1(char * pos)79 bool iskanji1(char *pos)
80 {
81   char *p;
82   p = pos;
83 
84   unsigned char u, u1;
85   u = *pos;
86   u1 = *(pos + 1);
87 
88 #if defined(TOPAZ_SJIS)
89     if (
90          ((u >= 0x81 && u <= 0x9f) || (u >= 0xe0 && u <= 0xfc))
91          &&
92          (u1 >= 0x40 && u1 <= 0xfc && u1 != 0x7f)
93        )
94       return true;
95     else
96       return false;
97 
98 #else
99 
100   int count = 0;
101   if (maykanji(*p))
102     {
103       while (*p != 0)
104 	  {
105 	    if (maykanji(*p))
106 	      count++;
107 	    p++;
108 	  }
109       return ((count % 2 + 1) % 2);
110     }
111   else
112     return false;
113 #endif
114 }
115 
tojis(unsigned int * s)116 void tojis(unsigned int *s)
117 {
118 
119   unsigned int h, l;
120 
121   l = (*s >> 8) & 0xff;
122   h = *s & 0xff;
123 
124   *s = ((h << 8) | l) - 0x8080 ;
125 
126 }
127 
tosjis(unsigned int * s)128 void tosjis(unsigned int *s)
129 {
130   unsigned int h, l;
131 
132   l = (*s >> 8) & 0xff;
133   h = *s & 0xff;
134 
135 
136   if (h & 1)
137     l += 0x1f;
138   else
139     l += 0x7d;
140 
141   if (l >= 0x7f)
142     l++;
143 
144   h = ((h - 0x21) >> 1) + 0x81;
145 
146   if (h > 0x9f)
147     h += 0x40;
148 
149   *s = (h << 8) | l;
150   return;
151 }
152 
sjistojis(unsigned int * s)153 void sjistojis(unsigned int *s)
154 {
155   unsigned int h, l;
156 
157   l = (*s >> 8) & 0xff;
158   h = *s & 0xff;
159 
160   if (h <= 0x9f)
161     h -= 0x71;
162   else
163     h -= 0xb1;
164 
165   h = (h << 1) + 1;
166 
167 
168   if (l >= 0x7f)
169     l -= 0x01;
170 
171   if (l >= 0x9e)
172   {
173     l -= 0x7d;
174     h++;
175   }
176   else
177   l -= 0x1f;
178 
179   *s = (h << 8) | l;
180   return;
181 }
182