Tweak themes for more color consistency.
[ntk.git] / src / xutf8 / utf8Input.c
blob2fb415b513b64f622a54b3fd60c9e36c08eb57a2
1 /* "$Id: $"
3 * Author: Jean-Marc Lienher ( http://oksid.ch )
4 * Copyright 2000-2003 by O'ksi'D.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 * USA.
21 * Please report all bugs and problems on the following page:
23 * http://www.fltk.org/str.php
26 #if !defined(WIN32) && !defined(__APPLE__)
28 #include <config.h>
29 #include "../../FL/Xutf8.h"
30 #include <X11/X.h>
31 #include <X11/Xlib.h>
32 #include <X11/Xutil.h>
33 #include <string.h>
34 #include <stdlib.h>
36 #if HAVE_LIBC_ICONV
37 #include <iconv.h>
38 #endif
40 I haven't found much doc on the web about EUC encodings, so I've used
41 GNU libiconv source code as a reference.
42 http://clisp.cons.org/~haible/packages-libiconv.html
45 #define RET_ILSEQ -1
46 #define RET_TOOFEW(x) (-10 - x)
47 #define RET_TOOSMALL -2
48 #define conv_t void*
49 #define ucs4_t unsigned int
50 typedef struct {
51 unsigned short indx;
52 unsigned short used;
53 } Summary16;
55 #define NEED_TOWC /* indicates what part of these include files is needed here (avoid compilation warnings) */
56 #include "lcUniConv/big5.h"
57 #include "lcUniConv/gb2312.h"
58 #include "lcUniConv/cp936ext.h"
59 #include "lcUniConv/jisx0201.h"
60 #include "lcUniConv/jisx0208.h"
61 #include "lcUniConv/jisx0212.h"
62 #include "lcUniConv/ksc5601.h"
64 int
65 XConvertEucTwToUtf8(char* buffer_return, int len) {
66 /* FIXME */
67 #if HAVE_LIBC_ICONV
68 iconv_t cd;
69 int cdl;
70 #else
71 int i = 0;
72 #endif
73 int l = 0;
74 char *buf, *b;
76 if (len < 1) return 0;
77 b = buf = (char*) malloc((unsigned)len);
78 memcpy(buf, buffer_return, (unsigned) len);
80 #if HAVE_LIBC_ICONV
81 l = cdl = len;
82 cd = iconv_open("EUC-TW", "UTF-8");
83 iconv(cd, &b, &len, &buffer_return, &cdl);
84 iconv_close(cd);
85 l -= cdl;
86 #else
87 while (i < len) {
88 unsigned int ucs;
89 unsigned char c;
90 c = (unsigned char) buf[i];
91 if (c < 0x80) {
92 ucs = c;
93 i++;
94 } else if (c >= 0xa1 && c < 0xff && len - i > 1 ) {
95 unsigned char b[2];
96 b[0] = (unsigned char) c - 0x80;
97 b[1] = (unsigned char) buf[i + 1] - 0x80;
98 ucs = ' '; i += 2;
99 } else if (c == 0x8e && len - i > 3) {
100 unsigned char b[2];
101 unsigned char c1 = buf[i + 1];
102 unsigned char c2 = buf[i + 2];
103 unsigned char c3 = buf[i + 3];
104 b[0] = (unsigned char) buf[i + 2] - 0x80;
105 b[1] = (unsigned char) buf[i + 3] - 0x80;
106 if (c1 >= 0xa1 && c1 <= 0xb0) {
107 if (c2 >= 0xa1 && c2 < 0xff && c3 >= 0xa1 && c3 < 0xff) {
108 ucs = ' '; i += 4;
109 } else {
110 ucs = '?'; i++;
112 } else {
113 ucs = '?'; i++;
115 } else {
116 ucs = '?';
117 i++;
119 l += XConvertUcsToUtf8(ucs, buffer_return + l);
121 #endif
122 free(buf);
123 return l;
126 int
127 XConvertEucKrToUtf8(char* buffer_return, int len) {
128 int i = 0, l = 0;
129 char *buf;
131 if (len < 1) return 0;
133 buf = (char*) malloc((unsigned)len);
134 memcpy(buf, buffer_return, (unsigned)len);
136 while (i < len) {
137 unsigned int ucs;
138 unsigned char c, c1;
139 c = (unsigned char) buf[i];
140 if (c < 0x80) {
141 ucs = c;
142 i++;
143 } else if (c >= 0xA1 && c < 0xFF && len - i > 1) {
144 c1 = (unsigned char) buf[i + 1];
145 if (c1 >= 0xa1 && c1 < 0xff) {
146 unsigned char b[2];
147 b[0] = c - 0x80;
148 b[1] = c1 - 0x80;
149 if (ksc5601_mbtowc(NULL, &ucs, b, 2) < 1) {
150 ucs = '?';
152 } else {
153 ucs = '?';
155 i += 2;
156 } else {
157 ucs = '?';
158 i++;
160 l += XConvertUcsToUtf8(ucs, buffer_return + l);
162 free(buf);
163 return l;
166 int
167 XConvertBig5ToUtf8(char* buffer_return, int len) {
168 int i = 0, l = 0;
169 char *buf;
171 if (len < 1) return 0;
172 buf = (char*) malloc((unsigned)len);
173 memcpy(buf, buffer_return, (unsigned)len);
175 if (len == 1) {
176 l += XConvertUcsToUtf8((unsigned int)buf[i], buffer_return + l);
178 while (i + 1 < len) {
179 unsigned int ucs;
180 unsigned char b[2];
181 b[0] = (unsigned char) buf[i];
182 b[1] = (unsigned char) buf[i + 1];
183 if (big5_mbtowc(NULL, &ucs, b, 2) == 2) {
184 i += 2;
185 } else {
186 ucs = '?';
187 i++;
189 l += XConvertUcsToUtf8(ucs, buffer_return + l);
191 free(buf);
192 return l;
195 int
196 XConvertCp936extToUtf8(char* buffer_return, int len)
198 int i = 0, l = 0;
199 char *buf;
201 if (len < 1) return 0;
202 buf = (char*) malloc((unsigned)len);
203 memcpy(buf, buffer_return, (unsigned)len);
205 if (len == 1) {
206 l += XConvertUcsToUtf8((unsigned int)buf[i], buffer_return + l);
208 while (i + 1 < len) {
209 unsigned int ucs;
210 unsigned char b[2];
211 b[0] = (unsigned char) buf[i];
212 b[1] = (unsigned char) buf[i + 1];
213 if (cp936ext_mbtowc(NULL, &ucs, b, 2) == 2) {
214 i += 2;
215 } else {
216 if ( b[0] < 0x80) {
217 ucs = b[0];
218 }else{
219 ucs = '?';
221 i++;
223 l += XConvertUcsToUtf8(ucs, buffer_return + l);
225 if(i + 1 == len) {
226 l += XConvertUcsToUtf8((unsigned int)buf[i], buffer_return + l);
228 free(buf);
229 return l;
232 int
233 XConvertGb2312ToUtf8(char* buffer_return, int len) {
234 int i = 0, l = 0;
235 char *buf;
237 if (len < 1) return 0;
238 buf = (char*) malloc((unsigned)len);
239 memcpy(buf, buffer_return, (unsigned)len);
241 if (len == 1) {
242 l += XConvertUcsToUtf8((unsigned int)buf[i], buffer_return + l);
244 while (i + 1 < len) {
245 unsigned int ucs;
246 unsigned char b[2];
247 b[0] = (unsigned char) buf[i];
248 b[1] = (unsigned char) buf[i + 1];
249 if ( b[0] < 0x80 ) {
250 ucs = b[0];
251 i++;
252 } else if (gb2312_mbtowc(NULL, &ucs, b, 2) == 2) {
253 i += 2;
254 } else {
255 ucs = '?';
256 i++;
258 l += XConvertUcsToUtf8(ucs, buffer_return + l);
260 if (i + 1 == len) {
261 l += XConvertUcsToUtf8((unsigned int)buf[i], buffer_return + l);
263 free(buf);
264 return l;
267 int
268 XConvertEucCnToUtf8(char* buffer_return, int len) {
269 int i = 0, l = 0;
270 char *buf;
272 if (len < 1) return 0;
273 buf = (char*) malloc((unsigned)len);
274 memcpy(buf, buffer_return, (unsigned)len);
276 while (i < len) {
277 unsigned int ucs;
278 unsigned char c, c1;
279 c = (unsigned char) buf[i];
280 if (c < 0x80) {
281 ucs = c;
282 i++;
283 } else if (c >= 0xA1 && c < 0xFF && len - i > 1) {
284 c1 = (unsigned char) buf[i + 1];
285 if (c1 >= 0xa1 && c1 < 0xff) {
286 unsigned char b[2];
287 b[0] = (unsigned char) c;
288 b[1] = (unsigned char) c1;
289 if (gb2312_mbtowc(NULL, &ucs, b, 2) < 1) {
290 ucs = '?';
292 } else {
293 ucs = '?';
295 i += 2;
296 } else {
297 ucs = '?';
298 i++;
300 l += XConvertUcsToUtf8(ucs, buffer_return + l);
302 free(buf);
303 return l;
306 int
307 XConvertEucJpToUtf8(char* buffer_return, int len) {
308 int i = 0, l = 0;
309 char *buf;
311 if (len < 1) return 0;
312 buf = (char*) malloc((unsigned)len);
313 memcpy(buf, buffer_return, (unsigned)len);
315 while (i < len) {
316 unsigned int ucs;
317 unsigned char c, c1;
318 c = (unsigned char) buf[i];
319 if (c < 0x80) {
320 ucs = c;
321 i++;
322 } else if (c >= 0xA1 && c < 0xFF && len - i > 1) {
323 c1 = (unsigned char) buf[i + 1];
324 if (c < 0xF5 && c1 >= 0xa1) {
325 unsigned char b[2];
326 b[0] = c - 0x80;
327 b[1] = c1 - 0x80;
328 if (jisx0208_mbtowc(NULL, &ucs, b, 2) < 1) {
329 ucs = '?';
331 } else if (c1 >= 0xA1 && c1 < 0xFF) {
332 ucs = 0xE000 + 94 * (c - 0xF5) + (c1 - 0xA1);
333 } else {
334 ucs = '?';
336 i += 2;
337 } else if (c == 0x8E && len - i > 1) {
338 c1 = (unsigned char) buf[i + 1];
339 if (c1 >= 0xa1 && c1 <= 0xe0) {
340 if (jisx0201_mbtowc(NULL, &ucs, &c1, 1) != 1) {
341 ucs = '?';
343 } else {
344 ucs = '?';
346 i += 2;
347 } else if (c == 0x8F && len - i > 2) {
348 c = (unsigned char) buf[i + 1];
349 c1 = (unsigned char) buf[i + 2];
350 if (c >= 0xa1 && c < 0xff) {
351 if (c < 0xf5 && c1 >= 0xa1 && c1 < 0xff) {
352 unsigned char b[2];
353 b[0] = c - 0x80;
354 b[1] = c1 - 0x80;
355 if (jisx0212_mbtowc(NULL, &ucs, b, 2) < 1) {
356 ucs = '?';
358 } else {
359 ucs = '?';
361 } else {
362 if (c1 >= 0xa1 && c1 < 0xff) {
363 ucs = 0xe3ac + 94 * (c - 0xF5) + (c1 - 0xA1);
364 } else {
365 ucs = '?';
368 i += 3;
369 } else {
370 ucs = '?';
371 i++;
373 l += XConvertUcsToUtf8(ucs, buffer_return + l);
375 free(buf);
376 return l;
380 XConvertEucToUtf8(const char* locale,
381 char* buffer_return,
382 int len,
383 int bytes_buffer) {
385 /* if (!locale) { */
386 /* if (!locale || strstr(locale, "UTF") || strstr(locale, "utf")) { */
387 if (!locale || strstr(locale, "UTF") || strstr(locale, "utf")) {
388 return len;
391 if (strstr(locale, "ja")) {
392 return XConvertEucJpToUtf8(buffer_return, len);
393 } else if (strstr(locale, "Big5") || strstr(locale, "big5")) { /* BIG5 */
394 return XConvertBig5ToUtf8(buffer_return, len);
395 } else if (strstr(locale, "GBK") || strstr(locale, "gbk")) {
396 return XConvertCp936extToUtf8(buffer_return, len);
397 } else if (strstr(locale, "zh") || strstr(locale, "chinese-")) {
398 if (strstr(locale, "TW") || strstr(locale, "chinese-t")) {
399 if (strstr(locale, "EUC") || strstr(locale, "euc") || strstr(locale, "chinese-t")) {
400 return XConvertEucTwToUtf8(buffer_return, len);
402 return XConvertBig5ToUtf8(buffer_return, len);
404 if (strstr(locale, "EUC") || strstr(locale, "euc")) {
405 return XConvertEucCnToUtf8(buffer_return, len);
407 return XConvertGb2312ToUtf8(buffer_return, len);
408 } else if (strstr(locale, "ko")) {
409 return XConvertEucKrToUtf8(buffer_return, len);
411 return len;
415 XUtf8LookupString(XIC ic,
416 XKeyPressedEvent* event,
417 char* buffer_return,
418 int bytes_buffer,
419 KeySym* keysym,
420 Status* status_return) {
422 long ucs = -1;
423 int len;
424 len = XmbLookupString(ic, event, buffer_return, bytes_buffer / 5,
425 keysym, status_return);
426 if (*status_return == XBufferOverflow) {
427 return len * 5;
429 if (*keysym > 0 && *keysym < 0x100 && len == 1) {
430 if (*keysym < 0x80) {
431 ucs = (unsigned char)buffer_return[0];
432 } else {
433 ucs = *keysym;
435 } else if (((*keysym >= 0x100 && *keysym <= 0xf000) ||
436 (*keysym & 0xff000000U) == 0x01000000))
438 ucs = XKeysymToUcs(*keysym);
439 } else {
440 ucs = -2;
443 if (ucs > 0) {
444 len = XConvertUcsToUtf8((unsigned)ucs, (char *)buffer_return);
445 } else if (len > 0) {
446 XIM im;
447 if (!ic) return 0;
448 im = XIMOfIC(ic);
449 if (!im) return 0;
450 len = XConvertEucToUtf8(XLocaleOfIM(im), buffer_return, len, bytes_buffer);
452 return len;
455 #endif /* X11 only */
458 * End of "$Id$".