Debian Codegraph @

texk/ptexenc/unicode.c

raw
1 /*
2 unicode.c -- utilities for Unicode
3 written by N. Tsuchimura
4 */
5
6 #include <ptexenc/c-auto.h>
7 #include <ptexenc/unicode.h>
8 #include <ptexenc/kanjicnv.h>
9 #include <tex8benc.h>
10
11 #include <stdio.h> /* for fprintf() */
12
13 /* determine if UTF-8 character or not */
14 boolean isUTF8(int length, int nth, int c)
15 {
16 c &= 0xff;
17 switch (length * 8 + nth) {
18 case 011: return (0x00 <= c && c < 0x80);
19 case 021: return (0xc2 <= c && c < 0xe0);
20 case 031: return (0xe0 <= c && c < 0xf0);
21 case 041: return (0xf0 <= c && c < 0xf5);
22 case 022:
23 case 032: case 033:
24 case 042: case 043: case 044:
25 return (0x80 <= c && c < 0xc0);
26 default:
27 fprintf(stderr, "isUTF8: unexpected param length=%d, nth=%d\n",
28 length, nth);
29 }
30 return false;
31 }
32
33
34 int UTF8length(int first_byte)
35 {
36 first_byte &= 0xff;
37 if (first_byte < 0x80) return 1;
38 if (first_byte < 0xc2) return -2; /* illegal */
39 if (first_byte < 0xe0) return 2;
40 if (first_byte < 0xf0) return 3;
41 if (first_byte < 0xf5) return 4;
42 return -1; /* reserved/undefined */
43 }
44
45
46 /* with strict range check */
47 #define DEFINE_UTF8SLENGTH(SUFF,TYPE) \
48 int UTF8Slength ## SUFF(TYPE *buff, int buff_len) \
49 { \
50 int i, len; \
51 len = UTF8length(buff[0]); \
52 if (len < 0) return -2; /* illegal */ \
53 if (len > buff_len) return -3; /* overflow */ \
54 for (i=0; i<len; i++) { \
55 if (!isUTF8(len, 1+i, buff[i])) return -1; /* not UTF-8 */ \
56 } \
57 return len; \
58 }
59
60 DEFINE_UTF8SLENGTH(, unsigned char)
61 DEFINE_UTF8SLENGTH(short, unsigned short)
62
63
64 /* WITHOUT strict range check */
65 long UTF8StoUCS(unsigned char *s)
66 {
67 switch (UTF8length(s[0])) {
68 case 1: return s[0];
69 case 2: return UTF8BtoUCS(s[0], s[1]);
70 case 3: return UTF8CtoUCS(s[0], s[1], s[2]);
71 case 4: return UTF8DtoUCS(s[0], s[1], s[2], s[3]);
72 default: return 0; /* error */
73 }
74 }
75
76 /* used only for debug */
77 #ifdef DEBUG
78 int UCStoUTF8S(long ucs, unsigned char *s)
79 {
80 if (ucs < 0x80) {
81 *s = ucs;
82 return 1;
83 } else if (ucs < 0x800) {
84 *s++ = UCStoUTF8B1(ucs);
85 *s++ = UCStoUTF8B2(ucs);
86 return 2;
87 } else if (ucs < 0xFFFF) {
88 *s++ = UCStoUTF8C1(ucs);
89 *s++ = UCStoUTF8C2(ucs);
90 *s++ = UCStoUTF8C3(ucs);
91 return 3;
92 } else if (ucs < 0x10FFFF) {
93 *s++ = UCStoUTF8D1(ucs);
94 *s++ = UCStoUTF8D2(ucs);
95 *s++ = UCStoUTF8D3(ucs);
96 *s++ = UCStoUTF8D4(ucs);
97 return 4;
98 }
99 return 0; /* unsupported */
100 }
101 #endif /* DEBUG */
102
103
104 long UCStoUTF8(long ucs)
105 {
106 if (ucs < 0x80) return ucs;
107 if (ucs < 0x800) return LONG(0,0,
108 UCStoUTF8B1(ucs),
109 UCStoUTF8B2(ucs));
110 if (ucs < 0xFFFF) return LONG(0,
111 UCStoUTF8C1(ucs),
112 UCStoUTF8C2(ucs),
113 UCStoUTF8C3(ucs));
114 if (ucs < 0x10FFFF) return LONG(UCStoUTF8D1(ucs),
115 UCStoUTF8D2(ucs),
116 UCStoUTF8D3(ucs),
117 UCStoUTF8D4(ucs));
118 return 0; /* unsupported */
119 }
120
121
122 long UCStoUPTEX (long ucs)
123 {
124 return ucs;
125 }
126
127 /* using over U+10.FFFF Area */
128 long UPTEXtoUCS (long uptex)
129 {
130 long vs[2];
131 if ((uptex>=0x220000 && uptex<=0x2FFFFF) || /* for 2-codepoint sequence */
132 uptex>=0x400000) { /* for SVS, IVS */
133 if (UVS_divide_code(uptex, vs) == 0) return 0;
134 return uptex;
135 }
136 return uptex;
137 }
138
139 int
140 UVS_get_codepoint_length(long ucv)
141 {
142 int len = 0;
143
144 if (ucv < 0x220000) {
145 len = 1;
146 }
147 else if (ucv >= 0x25E6E6 && ucv <= 0x25FFFF) { /* RGI Emoji Flag Sequence */
148 if ((ucv & 0xFF) < 0xE6) return 0; /* illegal value */
149 len = 2;
150 }
151 else if (ucv >= 0x800000 && ucv <= 0x80007F) { /* Emoji Keycap Sequence */
152 ucv &= 0x7F;
153 if ( ucv==0x23 || ucv==0x2A ||
154 (ucv>=0x30 && ucv<=0x39) ) len = 3;
155 else return 0; /* illegal value */
156 }
157 else if (ucv < 0x300000) {
158 len = 2;
159 }
160 else if (ucv >= 0x400000 && ucv <= 0x43FFFFF) {
161 len = 2;
162 }
163
164 return len;
165 }
166
167 long
168 UVS_get_codepoint_in_sequence(long ucv, int n)
169 {
170 long uvs, cp[3], len;
171
172 cp[1] = cp[2] = 0;
173
174 if (ucv < 0x220000) {
175 cp[0] = ucv % 0x110000;
176 len = 1;
177 }
178 else if (ucv >= 0x25E6E6 && ucv <= 0x25FFFF) { /* RGI Emoji Flag Sequence */
179 if ((ucv & 0xFF) < 0xE6) return 0; /* illegal value */
180 cp[0] = ((ucv >> 8) & 0xFF) + 0x1F100;
181 cp[1] = (ucv & 0xFF) + 0x1F100;
182 len = 2;
183 }
184 else if (ucv >= 0x800000 && ucv <= 0x80007F) { /* Emoji Keycap Sequence */
185 cp[0] = ucv & 0x7F;
186 cp[1] = 0xFE0F;
187 cp[2] = 0x20E3;
188 len = 3;
189 }
190 else if (ucv < 0x300000) {
191 len = 2;
192 uvs = ucv >> 16;
193 if (uvs >= 0x22 && uvs <= 0x25) { /* (Semi-)Voiced Sound Mark */
194 cp[0] = ucv & 0x1FFFF;
195 cp[1] = ((uvs - 0x22) >> 1) + 0x3099;
196 }
197 if (uvs >= 0x26 && uvs <= 0x2F) { /* Emoji Modifier Fitzpatrick */
198 cp[0] = ucv & 0x1FFFF;
199 cp[1] = ((uvs - 0x26) >> 1) + 0x1F3FB;
200 }
201 }
202 else if (ucv >= 0x400000) {
203 len = 2;
204 uvs = ucv >> 16;
205 if (uvs >= 0x40 && uvs <= 0x7F) { /* SVS, VS1 .. VS16 */
206 cp[0] = ucv & 0x3FFFF;
207 cp[1] = ((uvs - 0x40) >> 2) + 0xFE00;
208 }
209 if (uvs >= 0x80 && uvs <= 0x43F) { /* IVS, VS17 .. VS256 */
210 cp[0] = ucv & 0x3FFFF;
211 cp[1] = ((uvs - 0x80) >> 2) + 0xE0100;
212 }
213 }
214
215 if (n<0)
216 return len;
217 if (n>=1 && n<=3) {
218 return cp[n-1];
219 }
220
221 /* Unsupported */
222 return 0;
223 }
224
225 long
226 UVS_combine_code(long ucv, long uvs)
227 {
228 if ( uvs == 0x20E3 &&
229 ( ucv == 0x7C0023 || ucv == 0x7C002A || /* U+00xx U+FE0F U+20E3 */
230 (ucv >= 0x7C0030 && ucv <= 0x7C0039))) { /* Emoji Keycap Sequence */
231 return 0x40000 + ucv;
232 }
233
234 if (ucv > 0x3FFFF)
235 return 0;
236
237 if (((ucv >= 0x03000 && ucv <= 0x031FF) ||
238 (ucv >= 0x1AFF0 && ucv <= 0x1B16F)) &&
239 uvs >= 0x3099 && uvs <= 0x309A) { /* Kana (Semi-)Voiced Sound Mark */
240 return ((uvs - 0x3099) << 17) + 0x220000 + ucv;
241 }
242 if (((ucv >= 0x02600 && ucv <= 0x027BF) ||
243 (ucv >= 0x1F300 && ucv <= 0x1F9FF)) &&
244 uvs >= 0x1F3FB && uvs <= 0x1F3FF) { /* Emoji Modifier Fitzpatrick */
245 return ((uvs - 0x1F3FB) << 17) + 0x260000 + ucv;
246 }
247 if ( (ucv >=0x1F1E6 && ucv <= 0x1F1FF) &&
248 (uvs >=0x1F1E6 && uvs <= 0x1F1FF) ) { /* RGI Emoji Flag Sequence */
249 return ((ucv & 0xFF) << 8) + (uvs & 0xFF) + 0x250000;
250 }
251 if (ucv <= 0x2FFFF && uvs >= 0xFE00 && uvs <= 0xFE0F) { /* SVS, VS1 .. VS16 */
252 return ((uvs - 0xFE00) << 18) + 0x400000 + ucv;
253 }
254 if (uvs >= 0xE0100) { /* IVS */
255 if ( ucv <= 0x033FF ||
256 (ucv >= 0x04DC0 && ucv <= 0x04DFF) ||
257 (ucv >= 0x0A000 && ucv <= 0x0F8FF) ||
258 (ucv >= 0x0FB00 && ucv <= 0x1FFFF) ) return 0;
259 if (ucv <= 0x0FFFF && uvs <=0xE01EF) { /* BMP, VS17 .. VS256 */
260 return ((uvs - 0xE0100) << 18) + 0x800000 + ucv;
261 }
262 if (ucv <= 0x2FFFF && uvs <=0xE010F) { /* SIP, VS17 .. VS32 */
263 return ((uvs - 0xE0100) << 18) + 0x800000 + ucv;
264 }
265 if ( uvs <=0xE010F) { /* TIP, VS17 .. VS32 */
266 return ((uvs - 0xE0100) << 18) + 0x800000 + ucv;
267 }
268 }
269 /* Unsupported Combination */
270 return 0;
271 }
272
273 long
274 UVS_divide_code(long code, long* uvs)
275 {
276 long u, v, p;
277
278 u = code & 0x1FFFF; /* upto U+1FFFF */
279 v = code >> 16;
280 /* for Combining Katakana-Hiragana (Semi-)Voiced Sound Mark */
281 if (((u>=0x03000 && u<=0x031FF) ||
282 (u>=0x1AFF0 && u<=0x1B16F)) &&
283 v>=0x22 && v<=0x25) {
284 p = (v - 0x22) >> 1; /* Voiced or Semi-Voiced */
285 if (uvs) *uvs = 0x3099 + p;
286 return u;
287 }
288 /* for Emoji Modifier Fitzpatrick */
289 if (((u>=0x02600 && u<=0x027BF) ||
290 (u>=0x1F300 && u<=0x1F9FF)) &&
291 v>=0x26 && v<=0x2F) {
292 p = (v - 0x26) >> 1; /* Emoji Modifier Fitzpatrick Type-1..6 */
293 if (uvs) *uvs = 0x1F3FB + p;
294 return u;
295 }
296 /* for RGI Emoji Flag Sequence */
297 if ( u>=0x1E6E6 && v==0x25 ) {
298 if ((u & 0xFF) < 0xE6) goto Undefined; /* illegal value */
299 u = ((u >> 8) & 0xFF) + 0x1F100;
300 v = ( u & 0xFF) + 0x1F100; /* Regional Indicator Symbol Letter */
301 if (!uvs) goto Undefined;
302 *uvs = v;
303 return u;
304 }
305
306 if (code<0x400000 || code>=0x4400000) {
307 /* Undefined */
308 goto Undefined;
309 }
310
311 /* for Variation Selector */
312 u = code & 0x3FFFF; /* upto U+3FFFF */
313 v = code >> 18;
314 p = u >> 16;
315 if (v < 0x20) { /* SVS VS1 .. VS16 */
316 if (p==3) goto Undefined;
317 if (uvs) *uvs = v - 0x10 + 0xFE00;
318 return u;
319 } else
320 if (v == 0x20 && u <= 0x7F ) { /* for Emoji Keycap Sequence, need space for U+20E3 */
321 if (uvs) { *uvs = 0xFE0F; *(uvs+1) = 0x20E3; }
322 return u;
323 } else /* IVS */
324 if (v < 0x40) { /* VS17 .. VS32 */
325 if (p==1) goto Undefined;
326 if (uvs) *uvs = v - 0x20 + 0xE0100;
327 return u;
328 } else if (v <= 0x1FF) { /* VS33 .. VS256 */
329 if (p>0) goto Undefined;
330 if (uvs) *uvs = v - 0x20 + 0xE0100;
331 return u;
332 }
333
334 Undefined:
335 /* Undefined */
336 if (uvs) *uvs = 0;
337 return 0;
338 }
339
340 long
341 ptenc_ucs_to_8bit_code(short enc, long uch)
342 {
343 unsigned short *UCSto_enc, uch0;
344 int mid, left, right, size;
345
346 if (enc<0x80 || uch<0x80) return uch; /* no conversion */
347
348 switch (enc) {
349 case 0x80: UCSto_enc = UCStoT1enc; size = sizeof(UCStoT1enc);
350 break;
351 case 0x81: UCSto_enc = UCStoTS1enc; size = sizeof(UCStoTS1enc);
352 break;
353 case 0x82: UCSto_enc = UCStoLY1enc; size = sizeof(UCStoLY1enc);
354 break;
355 case 0x83: UCSto_enc = UCStoT5enc; size = sizeof(UCStoT5enc);
356 break;
357 case 0x84: UCSto_enc = UCStoL7Xenc; size = sizeof(UCStoL7Xenc);
358 break;
359 case 0x90: UCSto_enc = UCStoT2Aenc; size = sizeof(UCStoT2Aenc);
360 break;
361 case 0x91: UCSto_enc = UCStoT2Benc; size = sizeof(UCStoT2Benc);
362 break;
363 case 0x92: UCSto_enc = UCStoT2Cenc; size = sizeof(UCStoT2Cenc);
364 break;
365 case 0xA0: UCSto_enc = UCStoLGRenc; size = sizeof(UCStoLGRenc);
366 break;
367 default:
368 return 256; /* not supported yet */
369 }
370
371 left = 0;
372 right = size/(sizeof(unsigned short)*2);
373 while (left < right) {
374 mid = (left + right) / 2;
375 uch0 = UCSto_enc[mid*2];
376 if ( uch0 == uch ) return (long)UCSto_enc[mid*2+1];
377 if ( uch0 < uch ) left = mid + 1;
378 else right = mid;
379 }
380 return 256;
381 }
382
383 long
384 ptenc_8bit_code_to_ucs(short enc, long ech)
385 {
386 unsigned short *UCSto_enc;
387 int ii, size;
388
389 if (enc<0x80) return ech; /* no conversion */
390 if (ech>0xFF) return 0xFFFD; /* illegal */
391
392 switch (enc) {
393 case 0x80: UCSto_enc = UCStoT1enc; size = sizeof(UCStoT1enc);
394 break;
395 case 0x81: UCSto_enc = UCStoTS1enc; size = sizeof(UCStoTS1enc);
396 break;
397 case 0x82: UCSto_enc = UCStoLY1enc; size = sizeof(UCStoLY1enc);
398 break;
399 case 0x83: UCSto_enc = UCStoT5enc; size = sizeof(UCStoT5enc);
400 break;
401 case 0x84: UCSto_enc = UCStoL7Xenc; size = sizeof(UCStoL7Xenc);
402 break;
403 case 0x90: UCSto_enc = UCStoT2Aenc; size = sizeof(UCStoT2Aenc);
404 break;
405 case 0x91: UCSto_enc = UCStoT2Benc; size = sizeof(UCStoT2Benc);
406 break;
407 case 0x92: UCSto_enc = UCStoT2Cenc; size = sizeof(UCStoT2Cenc);
408 break;
409 case 0xA0: UCSto_enc = UCStoLGRenc; size = sizeof(UCStoLGRenc);
410 break;
411 default:
412 return 0xFFFD; /* not supported yet */
413 }
414
415 size = size/(sizeof(unsigned short)*2);
416 for (ii=0; ii<size; ii++) {
417 if ( UCSto_enc[ii*2+1] == ech ) return (long)UCSto_enc[ii*2];
418 }
419 return ech; /* no conversion */
420 }