DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 1 | /* "$Id: $" |
| 2 | * |
| 3 | * Author: Jean-Marc Lienher ( http://oksid.ch ) |
| 4 | * Copyright 2000-2003 by O'ksi'D. |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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. |
| 20 | * |
| 21 | * Please report all bugs and problems on the following page: |
| 22 | * |
| 23 | * http://www.fltk.org/str.php |
| 24 | */ |
| 25 | |
| 26 | #if !defined(WIN32) && !defined(__APPLE__) |
| 27 | |
| 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> |
| 35 | |
| 36 | #if HAVE_LIBC_ICONV |
| 37 | #include <iconv.h> |
| 38 | #endif |
| 39 | /* |
| 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 |
| 43 | */ |
| 44 | |
| 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; |
| 54 | |
| 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" |
| 63 | |
| 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; |
| 75 | |
| 76 | if (len < 1) return 0; |
| 77 | b = buf = (char*) malloc((unsigned)len); |
| 78 | memcpy(buf, buffer_return, (unsigned) len); |
| 79 | |
| 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++; |
| 111 | } |
| 112 | } else { |
| 113 | ucs = '?'; i++; |
| 114 | } |
| 115 | } else { |
| 116 | ucs = '?'; |
| 117 | i++; |
| 118 | } |
| 119 | l += XConvertUcsToUtf8(ucs, buffer_return + l); |
| 120 | } |
| 121 | #endif |
| 122 | free(buf); |
| 123 | return l; |
| 124 | } |
| 125 | |
| 126 | int |
| 127 | XConvertEucKrToUtf8(char* buffer_return, int len) { |
| 128 | int i = 0, l = 0; |
| 129 | char *buf; |
| 130 | |
| 131 | if (len < 1) return 0; |
| 132 | |
| 133 | buf = (char*) malloc((unsigned)len); |
| 134 | memcpy(buf, buffer_return, (unsigned)len); |
| 135 | |
| 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 = '?'; |
| 151 | } |
| 152 | } else { |
| 153 | ucs = '?'; |
| 154 | } |
| 155 | i += 2; |
| 156 | } else { |
| 157 | ucs = '?'; |
| 158 | i++; |
| 159 | } |
| 160 | l += XConvertUcsToUtf8(ucs, buffer_return + l); |
| 161 | } |
| 162 | free(buf); |
| 163 | return l; |
| 164 | } |
| 165 | |
| 166 | int |
| 167 | XConvertBig5ToUtf8(char* buffer_return, int len) { |
| 168 | int i = 0, l = 0; |
| 169 | char *buf; |
| 170 | |
| 171 | if (len < 1) return 0; |
| 172 | buf = (char*) malloc((unsigned)len); |
| 173 | memcpy(buf, buffer_return, (unsigned)len); |
| 174 | |
| 175 | if (len == 1) { |
| 176 | l += XConvertUcsToUtf8((unsigned int)buf[i], buffer_return + l); |
| 177 | } |
| 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++; |
| 188 | } |
| 189 | l += XConvertUcsToUtf8(ucs, buffer_return + l); |
| 190 | } |
| 191 | free(buf); |
| 192 | return l; |
| 193 | } |
| 194 | |
| 195 | int |
| 196 | XConvertCp936extToUtf8(char* buffer_return, int len) |
| 197 | { |
| 198 | int i = 0, l = 0; |
| 199 | char *buf; |
| 200 | |
| 201 | if (len < 1) return 0; |
| 202 | buf = (char*) malloc((unsigned)len); |
| 203 | memcpy(buf, buffer_return, (unsigned)len); |
| 204 | |
| 205 | if (len == 1) { |
| 206 | l += XConvertUcsToUtf8((unsigned int)buf[i], buffer_return + l); |
| 207 | } |
| 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 = '?'; |
| 220 | } |
| 221 | i++; |
| 222 | } |
| 223 | l += XConvertUcsToUtf8(ucs, buffer_return + l); |
| 224 | } |
| 225 | if(i + 1 == len) { |
| 226 | l += XConvertUcsToUtf8((unsigned int)buf[i], buffer_return + l); |
| 227 | } |
| 228 | free(buf); |
| 229 | return l; |
| 230 | } |
| 231 | |
| 232 | int |
| 233 | XConvertGb2312ToUtf8(char* buffer_return, int len) { |
| 234 | int i = 0, l = 0; |
| 235 | char *buf; |
| 236 | |
| 237 | if (len < 1) return 0; |
| 238 | buf = (char*) malloc((unsigned)len); |
| 239 | memcpy(buf, buffer_return, (unsigned)len); |
| 240 | |
| 241 | if (len == 1) { |
| 242 | l += XConvertUcsToUtf8((unsigned int)buf[i], buffer_return + l); |
| 243 | } |
| 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++; |
| 257 | } |
| 258 | l += XConvertUcsToUtf8(ucs, buffer_return + l); |
| 259 | } |
| 260 | if (i + 1 == len) { |
| 261 | l += XConvertUcsToUtf8((unsigned int)buf[i], buffer_return + l); |
| 262 | } |
| 263 | free(buf); |
| 264 | return l; |
| 265 | } |
| 266 | |
| 267 | int |
| 268 | XConvertEucCnToUtf8(char* buffer_return, int len) { |
| 269 | int i = 0, l = 0; |
| 270 | char *buf; |
| 271 | |
| 272 | if (len < 1) return 0; |
| 273 | buf = (char*) malloc((unsigned)len); |
| 274 | memcpy(buf, buffer_return, (unsigned)len); |
| 275 | |
| 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 = '?'; |
| 291 | } |
| 292 | } else { |
| 293 | ucs = '?'; |
| 294 | } |
| 295 | i += 2; |
| 296 | } else { |
| 297 | ucs = '?'; |
| 298 | i++; |
| 299 | } |
| 300 | l += XConvertUcsToUtf8(ucs, buffer_return + l); |
| 301 | } |
| 302 | free(buf); |
| 303 | return l; |
| 304 | } |
| 305 | |
| 306 | int |
| 307 | XConvertEucJpToUtf8(char* buffer_return, int len) { |
| 308 | int i = 0, l = 0; |
| 309 | char *buf; |
| 310 | |
| 311 | if (len < 1) return 0; |
| 312 | buf = (char*) malloc((unsigned)len); |
| 313 | memcpy(buf, buffer_return, (unsigned)len); |
| 314 | |
| 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 = '?'; |
| 330 | } |
| 331 | } else if (c1 >= 0xA1 && c1 < 0xFF) { |
| 332 | ucs = 0xE000 + 94 * (c - 0xF5) + (c1 - 0xA1); |
| 333 | } else { |
| 334 | ucs = '?'; |
| 335 | } |
| 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 = '?'; |
| 342 | } |
| 343 | } else { |
| 344 | ucs = '?'; |
| 345 | } |
| 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 = '?'; |
| 357 | } |
| 358 | } else { |
| 359 | ucs = '?'; |
| 360 | } |
| 361 | } else { |
| 362 | if (c1 >= 0xa1 && c1 < 0xff) { |
| 363 | ucs = 0xe3ac + 94 * (c - 0xF5) + (c1 - 0xA1); |
| 364 | } else { |
| 365 | ucs = '?'; |
| 366 | } |
| 367 | } |
| 368 | i += 3; |
| 369 | } else { |
| 370 | ucs = '?'; |
| 371 | i++; |
| 372 | } |
| 373 | l += XConvertUcsToUtf8(ucs, buffer_return + l); |
| 374 | } |
| 375 | free(buf); |
| 376 | return l; |
| 377 | } |
| 378 | |
| 379 | int |
| 380 | XConvertEucToUtf8(const char* locale, |
| 381 | char* buffer_return, |
| 382 | int len, |
| 383 | int bytes_buffer) { |
| 384 | |
| 385 | /* if (!locale) { */ |
| 386 | /* if (!locale || strstr(locale, "UTF") || strstr(locale, "utf")) { */ |
| 387 | if (!locale || strstr(locale, "UTF") || strstr(locale, "utf")) { |
| 388 | return len; |
| 389 | } |
| 390 | |
| 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); |
| 401 | } |
| 402 | return XConvertBig5ToUtf8(buffer_return, len); |
| 403 | } |
| 404 | if (strstr(locale, "EUC") || strstr(locale, "euc")) { |
| 405 | return XConvertEucCnToUtf8(buffer_return, len); |
| 406 | } |
| 407 | return XConvertGb2312ToUtf8(buffer_return, len); |
| 408 | } else if (strstr(locale, "ko")) { |
| 409 | return XConvertEucKrToUtf8(buffer_return, len); |
| 410 | } |
| 411 | return len; |
| 412 | } |
| 413 | |
| 414 | int |
| 415 | XUtf8LookupString(XIC ic, |
| 416 | XKeyPressedEvent* event, |
| 417 | char* buffer_return, |
| 418 | int bytes_buffer, |
| 419 | KeySym* keysym, |
| 420 | Status* status_return) { |
| 421 | |
| 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; |
| 428 | } |
| 429 | if (*keysym > 0 && *keysym < 0x100 && len == 1) { |
| 430 | if (*keysym < 0x80) { |
| 431 | ucs = (unsigned char)buffer_return[0]; |
| 432 | } else { |
| 433 | ucs = *keysym; |
| 434 | } |
| 435 | } else if (((*keysym >= 0x100 && *keysym <= 0xf000) || |
| 436 | (*keysym & 0xff000000U) == 0x01000000)) |
| 437 | { |
| 438 | ucs = XKeysymToUcs(*keysym); |
| 439 | } else { |
| 440 | ucs = -2; |
| 441 | } |
| 442 | |
| 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); |
| 451 | } |
| 452 | return len; |
| 453 | } |
| 454 | |
| 455 | #endif /* X11 only */ |
| 456 | |
| 457 | /* |
| 458 | * End of "$Id$". |
| 459 | */ |