other.c 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
Copyright (c) 2002 by Tomohiro KUBOTA

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
22 23 24 25

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "other.h"

#ifndef NULL
#define NULL 0
#endif

#define EURO_10646 0x20AC

int
init_gbk(OtherStatePtr s)
{
    s->gbk.mapping =
43
	FontEncMapFind("gbk-0", FONT_ENCODING_UNICODE, -1, -1, NULL);
44 45
    if (!s->gbk.mapping)
	return 0;
46 47

    s->gbk.reverse = FontMapReverse(s->gbk.mapping);
48 49
    if (!s->gbk.reverse)
	return 0;
50 51 52 53 54 55 56 57 58

    s->gbk.buf = -1;
    return 1;
}

unsigned int
mapping_gbk(unsigned int n, OtherStatePtr s)
{
    unsigned int r;
59 60 61 62
    if (n < 128)
	return n;
    if (n == 128)
	return EURO_10646;
63 64 65 66 67 68 69
    r = FontEncRecode(n, s->gbk.mapping);
    return r;
}

unsigned int
reverse_gbk(unsigned int n, OtherStatePtr s)
{
70 71 72 73
    if (n < 128)
	return n;
    if (n == EURO_10646)
	return 128;
74 75 76 77
    return s->gbk.reverse->reverse(n, s->gbk.reverse->data);
}

int
78
stack_gbk(unsigned c, OtherStatePtr s)
79
{
80
    if (s->gbk.buf < 0) {
81 82 83
	if (c < 129)
	    return (int) c;
	s->gbk.buf = (int) c;
84 85
	return -1;
    } else {
86 87 88
	int b;
	if (c < 0x40 || c == 0x7F) {
	    s->gbk.buf = -1;
89
	    return (int) c;
90 91
	}
	if (s->gbk.buf < 0xFF && c < 0xFF)
92
	    b = (int) ((unsigned) (s->gbk.buf << 8) + c);
93 94 95 96
	else
	    b = -1;
	s->gbk.buf = -1;
	return b;
97 98 99 100 101 102 103 104 105 106 107
    }
}

int
init_utf8(OtherStatePtr s)
{
    s->utf8.buf_ptr = 0;
    return 1;
}

unsigned int
108
mapping_utf8(unsigned int n, OtherStatePtr s GCC_UNUSED)
109 110 111 112 113
{
    return n;
}

unsigned int
114
reverse_utf8(unsigned int n, OtherStatePtr s GCC_UNUSED)
115
{
116 117 118 119 120 121 122 123
    if (n < 0x80)
	return n;
    if (n < 0x800)
	return 0xC080 + ((n & 0x7C0) << 2) + (n & 0x3F);
    if (n < 0x10000)
	return 0xE08080 + ((n & 0xF000) << 4) + ((n & 0xFC0) << 2) + (n & 0x3F);
    return 0xF0808080 + ((n & 0x1C0000) << 6) + ((n & 0x3F000) << 4) +
	((n & 0xFC0) << 2) + (n & 0x3F);
124 125 126
}

int
127
stack_utf8(unsigned c, OtherStatePtr s)
128 129 130
{
    int u;

131 132
    if (c < 0x80) {
	s->utf8.buf_ptr = 0;
133
	return (int) c;
134
    }
135
    if (s->utf8.buf_ptr == 0) {
136 137 138 139 140 141 142 143 144 145 146
	if ((c & 0x40) == 0)
	    return -1;
	s->utf8.buf[s->utf8.buf_ptr++] = UChar(c);
	if ((c & 0x60) == 0x40)
	    s->utf8.len = 2;
	else if ((c & 0x70) == 0x60)
	    s->utf8.len = 3;
	else if ((c & 0x78) == 0x70)
	    s->utf8.len = 4;
	else
	    s->utf8.buf_ptr = 0;
147
	return -1;
148
    }
149 150 151
    if ((c & 0x40) != 0) {
	s->utf8.buf_ptr = 0;
	return -1;
152
    }
153 154 155
    s->utf8.buf[s->utf8.buf_ptr++] = UChar(c);
    if (s->utf8.buf_ptr < s->utf8.len)
	return -1;
156
    switch (s->utf8.len) {
157
    case 2:
158 159
	u = ((s->utf8.buf[0] & 0x1F) << 6) | (s->utf8.buf[1] & 0x3F);
	s->utf8.buf_ptr = 0;
160 161 162 163
	if (u < 0x80)
	    return -1;
	else
	    return u;
164
    case 3:
165 166 167 168
	u = ((s->utf8.buf[0] & 0x0F) << 12)
	    | ((s->utf8.buf[1] & 0x3F) << 6)
	    | (s->utf8.buf[2] & 0x3F);
	s->utf8.buf_ptr = 0;
169 170 171 172
	if (u < 0x800)
	    return -1;
	else
	    return u;
173
    case 4:
174 175 176 177 178
	u = ((s->utf8.buf[0] & 0x03) << 18)
	    | ((s->utf8.buf[1] & 0x3F) << 12)
	    | ((s->utf8.buf[2] & 0x3F) << 6)
	    | ((s->utf8.buf[3] & 0x3F));
	s->utf8.buf_ptr = 0;
179 180 181 182
	if (u < 0x10000)
	    return -1;
	else
	    return u;
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    }
    s->utf8.buf_ptr = 0;
    return -1;
}

#define HALFWIDTH_10646 0xFF61
#define YEN_SJIS 0x5C
#define YEN_10646 0x00A5
#define OVERLINE_SJIS 0x7E
#define OVERLINE_10646 0x203E

int
init_sjis(OtherStatePtr s)
{
    s->sjis.x0208mapping =
198
	FontEncMapFind("jisx0208.1990-0", FONT_ENCODING_UNICODE, -1, -1, NULL);
199 200
    if (!s->sjis.x0208mapping)
	return 0;
201 202

    s->sjis.x0208reverse = FontMapReverse(s->sjis.x0208mapping);
203 204
    if (!s->sjis.x0208reverse)
	return 0;
205 206

    s->sjis.x0201mapping =
207
	FontEncMapFind("jisx0201.1976-0", FONT_ENCODING_UNICODE, -1, -1, NULL);
208 209
    if (!s->sjis.x0201mapping)
	return 0;
210 211

    s->sjis.x0201reverse = FontMapReverse(s->sjis.x0201mapping);
212 213
    if (!s->sjis.x0201reverse)
	return 0;
214 215 216 217 218 219 220 221 222

    s->sjis.buf = -1;
    return 1;
}

unsigned int
mapping_sjis(unsigned int n, OtherStatePtr s)
{
    unsigned int j1, j2, s1, s2;
223 224 225 226 227 228 229 230
    if (n == YEN_SJIS)
	return YEN_10646;
    if (n == OVERLINE_SJIS)
	return OVERLINE_10646;
    if (n < 0x80)
	return n;
    if (n >= 0xA0 && n <= 0xDF)
	return FontEncRecode(n, s->sjis.x0201mapping);
231 232
    s1 = ((n >> 8) & 0xFF);
    s2 = (n & 0xFF);
233 234 235 236 237 238 239
    j1 = (s1 << 1)
	- (unsigned) (s1 <= 0x9F ? 0xE0 : 0x160)
	- (unsigned) (s2 < 0x9F ? 1 : 0);
    j2 = s2
	- 0x1F
	- (unsigned) (s2 >= 0x7F ? 1 : 0)
	- (unsigned) (s2 >= 0x9F ? 0x5E : 0);
240
    return FontEncRecode((j1 << 8) + j2, s->sjis.x0208mapping);
241 242 243 244 245 246
}

unsigned int
reverse_sjis(unsigned int n, OtherStatePtr s)
{
    unsigned int j, j1, j2, s1, s2;
247 248 249 250 251 252
    if (n == YEN_10646)
	return YEN_SJIS;
    if (n == OVERLINE_10646)
	return OVERLINE_SJIS;
    if (n < 0x80)
	return n;
253 254
    if (n >= HALFWIDTH_10646)
	return s->sjis.x0201reverse->reverse(n, s->sjis.x0201reverse->data);
255
    j = s->sjis.x0208reverse->reverse(n, s->sjis.x0208reverse->data);
256 257
    j1 = ((j >> 8) & 0xFF);
    j2 = (j & 0xFF);
258 259 260 261
    s1 = ((j1 - 1) >> 1)
	+ (unsigned) ((j1 <= 0x5E) ? 0x71 : 0xB1);
    s2 = j2
	+ (unsigned) ((j1 & 1) ? ((j2 < 0x60) ? 0x1F : 0x20) : 0x7E);
262
    return (s1 << 8) + s2;
263 264 265
}

int
266
stack_sjis(unsigned c, OtherStatePtr s)
267
{
268
    if (s->sjis.buf < 0) {
269 270 271
	if (c < 128 || (c >= 0xA0 && c <= 0xDF))
	    return (int) c;
	s->sjis.buf = (int) c;
272 273
	return -1;
    } else {
274 275 276
	int b;
	if (c < 0x40 || c == 0x7F) {
	    s->sjis.buf = -1;
277
	    return (int) c;
278 279
	}
	if (s->sjis.buf < 0xFF && c < 0xFF)
280
	    b = (int) ((unsigned) (s->sjis.buf << 8) + c);
281 282 283 284
	else
	    b = -1;
	s->sjis.buf = -1;
	return b;
285 286 287
    }
}

288 289 290 291
int
init_hkscs(OtherStatePtr s)
{
    s->hkscs.mapping =
292
	FontEncMapFind("big5hkscs-0", FONT_ENCODING_UNICODE, -1, -1, NULL);
293 294
    if (!s->hkscs.mapping)
	return 0;
295 296

    s->hkscs.reverse = FontMapReverse(s->hkscs.mapping);
297 298
    if (!s->hkscs.reverse)
	return 0;
299 300 301 302 303 304 305 306 307

    s->hkscs.buf = -1;
    return 1;
}

unsigned int
mapping_hkscs(unsigned int n, OtherStatePtr s)
{
    unsigned int r;
308 309 310 311
    if (n < 128)
	return n;
    if (n == 128)
	return EURO_10646;
312 313 314 315 316 317 318
    r = FontEncRecode(n, s->hkscs.mapping);
    return r;
}

unsigned int
reverse_hkscs(unsigned int n, OtherStatePtr s)
{
319 320 321 322
    if (n < 128)
	return n;
    if (n == EURO_10646)
	return 128;
323 324 325 326
    return s->hkscs.reverse->reverse(n, s->hkscs.reverse->data);
}

int
327
stack_hkscs(unsigned c, OtherStatePtr s)
328
{
329
    if (s->hkscs.buf < 0) {
330 331 332
	if (c < 129)
	    return (int) c;
	s->hkscs.buf = (int) c;
333 334
	return -1;
    } else {
335 336 337
	int b;
	if (c < 0x40 || c == 0x7F) {
	    s->hkscs.buf = -1;
338
	    return (int) c;
339 340
	}
	if (s->hkscs.buf < 0xFF && c < 0xFF)
341
	    b = (int) ((unsigned) (s->hkscs.buf << 8) + c);
342 343 344 345
	else
	    b = -1;
	s->hkscs.buf = -1;
	return b;
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    }
}

/*
 *  Because of the 1 ~ 4 multi-bytes nature of GB18030.
 *  CharSet encoding is split to 2 subset (besides latin)
 *  The 2Bytes MB char is defined in gb18030.2000-0
 *  The 4Bytes MB char is defined in gb18030.2000-1
 *  Please note that the mapping in 2000-1 is not a 4Bytes seq => 2Bytes value
 *  mapping.
 *  To use the 2000-1 we need to 'linear' the 4Bytes sequence and 'lookup' the 
 *  unicode value after that.
 *
 *  For more info on GB18030 standard pls check:
 *    http://oss.software.ibm.com/icu/docs/papers/gb18030.html
 *
 *  For more info on GB18030 implementation issues in XFree86 pls check:
 *    http://www.ibm.com/developerWorks/cn/linux/i18n/gb18030/xfree86/part1
 */
int
init_gb18030(OtherStatePtr s)
{
    s->gb18030.cs0_mapping =
369
	FontEncMapFind("gb18030.2000-0", FONT_ENCODING_UNICODE, -1, -1, NULL);
370 371
    if (!s->gb18030.cs0_mapping)
	return 0;
372 373

    s->gb18030.cs0_reverse = FontMapReverse(s->gb18030.cs0_mapping);
374 375
    if (!s->gb18030.cs0_reverse)
	return 0;
376 377

    s->gb18030.cs1_mapping =
378
	FontEncMapFind("gb18030.2000-1", FONT_ENCODING_UNICODE, -1, -1, NULL);
379 380
    if (!s->gb18030.cs1_mapping)
	return 0;
381 382

    s->gb18030.cs1_reverse = FontMapReverse(s->gb18030.cs1_mapping);
383 384
    if (!s->gb18030.cs1_reverse)
	return 0;
385

386
    s->gb18030.linear = 0;
387 388 389 390 391 392 393
    s->gb18030.buf_ptr = 0;
    return 1;
}

unsigned int
mapping_gb18030(unsigned int n, OtherStatePtr s)
{
394 395 396 397
    if (n <= 0x80)
	return n;		/* 0x80 is valid but unassigned codepoint */
    if (n >= 0xFFFF)
	return '?';
398

399
    return FontEncRecode(n,
400
			 (s->gb18030.linear) ? s->gb18030.cs1_mapping : s->gb18030.cs0_mapping);
401 402 403 404 405 406 407 408
}

unsigned int
reverse_gb18030(unsigned int n, OtherStatePtr s)
{
    /* when lookup in 2000-0 failed. */
    /* lookup in 2000-1 and then try to unlinear'd */
    unsigned int r;
409 410
    if (n <= 0x80)
	return n;
411 412 413

    r = s->gb18030.cs0_reverse->reverse(n, s->gb18030.cs0_reverse->data);
    if (r != 0)
414
	return r;
415 416 417

    r = s->gb18030.cs1_reverse->reverse(n, s->gb18030.cs1_reverse->data);
    if (r != 0) {
418
	unsigned char bytes[4];
419

420 421 422 423 424 425 426
	bytes[3] = UChar(0x30 + r % 10);
	r /= 10;
	bytes[2] = UChar(0x81 + r % 126);
	r /= 126;
	bytes[1] = UChar(0x30 + r % 10);
	r /= 10;
	bytes[0] = UChar(0x81 + r);
427

428 429 430 431
	r = (unsigned int) bytes[0] << 24;
	r |= (unsigned int) bytes[1] << 16;
	r |= (unsigned int) bytes[2] << 8;
	r |= (unsigned int) bytes[3];
432 433 434 435 436
    }
    return r;
}

int
437
stack_gb18030(unsigned c, OtherStatePtr s)
438 439
{
    /* if set gb18030.linear => True. the return value is "linear'd" */
440
    if (s->gb18030.buf_ptr == 0) {
441 442 443 444
	if (c <= 0x80)
	    return (int) c;
	if (c == 0xFF)
	    return -1;
445
	s->gb18030.linear = 0;
446
	s->gb18030.buf[s->gb18030.buf_ptr++] = (int) c;
447
	return -1;
448
    } else if (s->gb18030.buf_ptr == 1) {
449 450 451 452 453
	if (c >= 0x40) {
	    s->gb18030.buf_ptr = 0;
	    if ((c == 0x80) || (c == 0xFF))
		return -1;
	    else
454
		return (int) ((unsigned) (s->gb18030.buf[0] << 8) + c);
455
	} else if (c >= 30) {	/* 2Byte is (0x30 -> 0x39) */
456
	    s->gb18030.buf[s->gb18030.buf_ptr++] = (int) c;
457 458 459
	    return -1;
	} else {
	    s->gb18030.buf_ptr = 0;
460
	    return (int) c;
461
	}
462
    } else if (s->gb18030.buf_ptr == 2) {
463
	if ((c >= 0x81) && (c <= 0xFE)) {
464
	    s->gb18030.buf[s->gb18030.buf_ptr++] = (int) c;
465 466 467
	    return -1;
	} else {
	    s->gb18030.buf_ptr = 0;
468
	    return (int) c;
469
	}
470
    } else {
471 472 473 474 475 476 477
	int r = 0;
	s->gb18030.buf_ptr = 0;
	if ((c >= 0x30) && (c <= 0x39)) {
	    s->gb18030.linear = 1;
	    r = (((s->gb18030.buf[0] - 0x81) * 10
		  + (s->gb18030.buf[1] - 0x30)) * 126
		 + (s->gb18030.buf[2] - 0x81)) * 10
478
		+ ((int) c - 0x30);
479 480 481
	    return r;
	}
	return -1;
482 483
    }
}