Debian Codegraph @

texk/web2c/pdftexdir/tounicode.c

raw
1 /*
2 Copyright 2006-2023 Han The Thanh, <[email protected]>
3
4 This file is part of pdfTeX.
5
6 pdfTeX is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 pdfTeX 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
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "ptexlib.h"
21
22 #define isXdigit(c) (isdigit(c) || ('A' <= (c) && (c) <= 'F'))
23 #define UNI_UNDEF -1
24 #define UNI_STRING -2 /* string allocated by deftounicode() */
25 #define UNI_EXTRA_STRING -3 /* string allocated by set_glyph_unicode() */
26
27 static struct avl_table *glyph_unicode_tree = NULL;
28
29 static int comp_glyph_unicode_entry(const void *pa, const void *pb, void *p)
30 {
31 return strcmp(((const glyph_unicode_entry *) pa)->name,
32 ((const glyph_unicode_entry *) pb)->name);
33 }
34
35 static glyph_unicode_entry *new_glyph_unicode_entry(void)
36 {
37 glyph_unicode_entry *e;
38 e = xtalloc(1, glyph_unicode_entry);
39 e->name = NULL;
40 e->code = UNI_UNDEF;
41 e->unicode_seq = NULL;
42 return e;
43 }
44
45 static void destroy_glyph_unicode_entry(void *pa, void *pb)
46 {
47 glyph_unicode_entry *e = (glyph_unicode_entry *) pa;
48 xfree(e->name);
49 if (e->code == UNI_STRING) {
50 assert(e->unicode_seq != NULL);
51 xfree(e->unicode_seq);
52 }
53 }
54
55 void glyph_unicode_free(void)
56 {
57 if (glyph_unicode_tree != NULL)
58 avl_destroy(glyph_unicode_tree, destroy_glyph_unicode_entry);
59 }
60
61 void deftounicode(strnumber glyph, strnumber unistr)
62 {
63 char buf[SMALL_BUF_SIZE], *p;
64 char buf2[SMALL_BUF_SIZE], *q;
65 int valid_unistr; /* 0: invalid; 1: unicode value; 2: string */
66 int i, l;
67 glyph_unicode_entry *gu, t;
68 void **aa;
69 unsigned long sscan_result;
70
71 p = makecstring(glyph);
72 assert(strlen(p) < SMALL_BUF_SIZE);
73 strcpy(buf, p); /* copy the result to buf before next call of makecstring() */
74 p = makecstring(unistr);
75 while (*p == ' ')
76 p++; /* ignore leading spaces */
77 l = strlen(p);
78 while (l > 0 && p[l - 1] == ' ')
79 l--; /* ignore trailing spaces */
80 valid_unistr = 1; /* a unicode value is the most common case */
81 for (i = 0; i < l; i++) {
82 if (p[i] == ' ')
83 valid_unistr = 2; /* if a space occurs we treat this entry as a string */
84 else if (!isXdigit((unsigned char)p[i])) {
85 valid_unistr = 0;
86 break;
87 }
88 }
89 if (l == 0 || valid_unistr == 0 || strlen(buf) == 0
90 || strcmp(buf, notdef) == 0) {
91 pdftex_warn("ToUnicode: invalid parameter(s): `%s' => `%s'", buf, p);
92 return;
93 }
94 if (glyph_unicode_tree == NULL) {
95 glyph_unicode_tree =
96 avl_create(comp_glyph_unicode_entry, NULL, &avl_xallocator);
97 assert(glyph_unicode_tree != NULL);
98 }
99 t.name = buf;
100 /* allow overriding existing entries */
101 if ((gu = (glyph_unicode_entry *) avl_find(glyph_unicode_tree, &t)) != NULL) {
102 if (gu->code == UNI_STRING) {
103 assert(gu->unicode_seq != NULL);
104 xfree(gu->unicode_seq);
105 }
106 } else { /* make new entry */
107 gu = new_glyph_unicode_entry();
108 gu->name = xstrdup(buf);
109 }
110 if (valid_unistr == 2) { /* a string with space(s) */
111 /* copy p to buf2, ignoring spaces */
112 for (q = buf2; *p != 0; p++)
113 if (*p != ' ')
114 *q++ = *p;
115 *q = 0;
116 gu->code = UNI_STRING;
117 gu->unicode_seq = xstrdup(buf2);
118 } else {
119 i = sscanf(p, "%lX", &sscan_result);
120 assert(i == 1);
121 if (sscan_result > 0x10FFFF) {
122 pdftex_warn("ToUnicode: value out of range [0,10FFFF]: %lX",
123 sscan_result);
124 gu->code = UNI_UNDEF;
125 }
126 else
127 gu->code = sscan_result;
128 }
129 aa = avl_probe(glyph_unicode_tree, gu);
130 assert(aa != NULL);
131 }
132
133
134 static long check_unicode_value(const char *s, boolean multiple_value)
135 {
136 int l = strlen(s);
137 int i;
138 long code;
139
140 if (l == 0)
141 return UNI_UNDEF;
142 if (multiple_value && l % 4 != 0)
143 return UNI_UNDEF;
144 if (!multiple_value && !(4 <= l && l <= 6))
145 return UNI_UNDEF;
146
147 for (i = 0; i < l; i++) {
148 if (!isXdigit((unsigned char)s[i]))
149 return UNI_UNDEF;
150 if (multiple_value) {
151 if (i % 4 == 3) {
152 if (sscanf(s + i - 3, "%4lX", &code) != 1)
153 return UNI_UNDEF;
154 if (!((0x0000 <= code && code <= 0xD7FF) ||
155 (0xE000 <= code && code <= 0xFFFF)))
156 return UNI_UNDEF;
157 }
158 } else { /* single value */
159 if (i == l - 1) {
160 if (sscanf(s, "%lX", &code) != 1)
161 return UNI_UNDEF;
162 if (!((0x0000 <= code && code <= 0xD7FF) ||
163 (0xE000 <= code && code <= 0x10FFFF)))
164 return UNI_UNDEF;
165 }
166 }
167 }
168 return code;
169 }
170
171 static char *utf16be_str(long code)
172 {
173 static char buf[SMALL_BUF_SIZE];
174 long v;
175 unsigned vh, vl;
176
177 assert(code >= 0);
178
179 if (code <= 0xFFFF)
180 sprintf(buf, "%04lX", code);
181 else {
182 v = code - 0x10000;
183 vh = v / 0x400 + 0xD800;
184 vl = v % 0x400 + 0xDC00;
185 sprintf(buf, "%04X%04X", vh, vl);
186 }
187 return buf;
188 }
189
190
191 /* this function writes /ToUnicode data to *gp based on glyph name s and
192 * taking into account tfmname; in case it returns
193 * gp->code == UNI_EXTRA_STRING then the caller is responsible for freeing
194 * gp->unicode_seq too */
195 static void set_glyph_unicode(const char *s, const char* tfmname,
196 glyph_unicode_entry *gp)
197 {
198 char buf[SMALL_BUF_SIZE], buf2[SMALL_BUF_SIZE], *p;
199 const char *p2; /* p2 points in s; p above points in writable copies */
200 long code;
201 boolean last_component;
202 glyph_unicode_entry tmp, *ptmp;
203
204 /* skip dummy entries */
205 if (s == NULL || s == notdef)
206 return;
207
208 /* strip everything after the first dot */
209 p = strchr(s, '.');
210 if (p != NULL) {
211 *buf = 0;
212 strncat(buf, s, p - s);
213 s = buf;
214 }
215
216 if (strlen(s) == 0)
217 return;
218
219 /* check for case of multiple components separated by '_' */
220 p = strchr(s, '_');
221 if (p != NULL) {
222 assert(strlen(s) < sizeof(buf));
223 if (s != buf) {
224 strcpy(buf, s);
225 p = strchr(buf, '_');
226 s = buf;
227 }
228 *buf2 = 0;
229 last_component = false;
230 for (;;) {
231 *p = 0;
232 tmp.code = UNI_UNDEF;
233 set_glyph_unicode(s, tfmname, &tmp);
234 switch (tmp.code) {
235 case UNI_UNDEF: /* not found, do nothing */
236 break;
237 case UNI_STRING: /* s matched an entry with string value in the database */
238 assert(tmp.unicode_seq != NULL);
239 assert(strlen(buf2) + strlen(tmp.unicode_seq) < sizeof(buf2));
240 strcat(buf2, tmp.unicode_seq);
241 break;
242 case UNI_EXTRA_STRING: /* s is a multiple value of form "uniXXXX" */
243 assert(strlen(buf2) + strlen(tmp.unicode_seq) < sizeof(buf2));
244 strcat(buf2, tmp.unicode_seq);
245 xfree(tmp.unicode_seq);
246 break;
247 default: /* s matched an entry with numeric value in the
248 database, or a value derived from "uXXXX" */
249 assert(tmp.code >= 0);
250 strcat(buf2, utf16be_str(tmp.code));
251 }
252 if (last_component)
253 break;
254 s = p + 1;
255 p = strchr(s, '_');
256 if (p == NULL) {
257 p = strend(s);
258 last_component = true;
259 }
260 }
261 gp->code = UNI_EXTRA_STRING;
262 gp->unicode_seq = xstrdup(buf2);
263 return;
264 }
265
266 /* Glyph name search strategy: first look up the glyph name in the
267 tfm's namespace, failing that look it up in the main database. */
268 /* Note: buf may alias s in the code below, but s and buf2 are
269 guaranteed to be distinct because the code changing buf2 above
270 always returns before reaching the code below. */
271
272 /* lookup for glyph name in the tfm's namespace */
273 snprintf(buf2, SMALL_BUF_SIZE, "tfm:%s/%s", tfmname, s);
274 tmp.name = buf2;
275 tmp.code = UNI_UNDEF;
276 ptmp = (glyph_unicode_entry *) avl_find(glyph_unicode_tree, &tmp);
277 if (ptmp != NULL) {
278 gp->code = ptmp->code;
279 gp->unicode_seq = ptmp->unicode_seq;
280 return;
281 }
282
283 /* lookup for glyph name in the main database */
284 snprintf(buf2, SMALL_BUF_SIZE, "%s", s);
285 tmp.name = buf2;
286 tmp.code = UNI_UNDEF;
287 ptmp = (glyph_unicode_entry *) avl_find(glyph_unicode_tree, &tmp);
288 if (ptmp != NULL) {
289 gp->code = ptmp->code;
290 gp->unicode_seq = ptmp->unicode_seq;
291 return;
292 }
293
294 /* check for case of "uniXXXX" (multiple 4-hex-digit values allowed) */
295 if (str_prefix(s, "uni")) {
296 p2 = s + strlen("uni");
297 code = check_unicode_value(p2, true);
298 if (code != UNI_UNDEF) {
299 if (strlen(p2) == 4) /* single value */
300 gp->code = code;
301 else { /* multiple value */
302 gp->code = UNI_EXTRA_STRING;
303 gp->unicode_seq = xstrdup(p2);
304 }
305 }
306 return; /* since the last case cannot happen */
307 }
308
309 /* check for case of "uXXXX" (single value up to 6 hex digits) */
310 if (str_prefix(s, "u")) {
311 p2 = s + strlen("u");
312 code = check_unicode_value(p2, false);
313 if (code != UNI_UNDEF) {
314 assert(code >= 0);
315 gp->code = code;
316 }
317 }
318 }
319
320 static boolean is_last_byte_valid(int srcCode1, int srcCode2, long code)
321 {
322 /*
323 When defining ranges of this type, the value of the last byte in the
324 string shall be less than or equal to 255 − (srcCode2 − srcCode1). This
325 ensures that the last byte of the string shall not be incremented past
326 255; otherwise, the result of mapping is undefined.
327 */
328 char *s = strend(utf16be_str(code)) - 2;
329 long l = strtol(s, NULL, 16);
330 return l < 255 - (srcCode2 - srcCode1);
331 }
332
333
334 /* tfmname is without .tfm extension, but encname ends in .enc; */
335 integer write_tounicode(char **glyph_names, const char *tfmname,
336 const char* encname)
337 {
338 char buf[SMALL_BUF_SIZE], *p;
339 static char builtin_suffix[] = "builtin";
340 short range_size[257];
341 glyph_unicode_entry gtab[257];
342 integer objnum;
343 int i, j;
344 int bfchar_count, bfrange_count, subrange_count;
345
346 if (glyph_unicode_tree == NULL) {
347 pdftex_warn("no GlyphToUnicode entry has been inserted yet!");
348 fixedgentounicode = 0;
349 return 0;
350 }
351
352 /* build the name for this CMap as <tfmname>-<encname>; if encname is NULL
353 * then this is a builtin encoding so use instead builtin_suffix */
354 strcpy(buf, tfmname);
355 strcat(buf, "-");
356 if (encname) {
357 assert(strlen(tfmname) + strlen(encname) + 1 < SMALL_BUF_SIZE);
358 strcat(buf, encname);
359 if ((p = strrchr(buf, '.')) != NULL && strcmp(p, ".enc") == 0)
360 *p = 0; /* strip ".enc" from encoding name */
361 else /* some silly encoding file name not ending in enc; use as-is */
362 pdftex_warn("Dubious encoding file name: `%s'", encname);
363 } else { /* this is a builtin encoding, so name is e.g. "cmr10-builtin" */
364 assert(strlen(tfmname) + strlen(builtin_suffix) + 1 < SMALL_BUF_SIZE);
365 strcat(buf, builtin_suffix);
366 }
367
368 objnum = pdfnewobjnum();
369 pdfbegindict(objnum, 0);
370 pdfbeginstream();
371 pdf_printf("%%!PS-Adobe-3.0 Resource-CMap\n"
372 "%%%%DocumentNeededResources: ProcSet (CIDInit)\n"
373 "%%%%IncludeResource: ProcSet (CIDInit)\n"
374 "%%%%BeginResource: CMap (TeX-%s-0)\n"
375 "%%%%Title: (TeX-%s-0 TeX %s 0)\n"
376 "%%%%Version: 1.000\n"
377 "%%%%EndComments\n"
378 "/CIDInit /ProcSet findresource begin\n"
379 "12 dict begin\n"
380 "begincmap\n"
381 "/CIDSystemInfo\n"
382 "<< /Registry (TeX)\n"
383 "/Ordering (%s)\n"
384 "/Supplement 0\n"
385 ">> def\n"
386 "/CMapName /TeX-%s-0 def\n"
387 "/CMapType 2 def\n"
388 "1 begincodespacerange\n"
389 "<00> <FF>\n" "endcodespacerange\n", buf, buf, buf, buf, buf);
390
391 /* set gtab */
392 for (i = 0; i < 256; ++i) {
393 gtab[i].code = UNI_UNDEF;
394 set_glyph_unicode(glyph_names[i], tfmname, &gtab[i]);
395 }
396 gtab[256].code = UNI_UNDEF;
397
398 /* set range_size */
399 for (i = 0; i < 256;) {
400 if (gtab[i].code == UNI_STRING || gtab[i].code == UNI_EXTRA_STRING) {
401 range_size[i] = 1; /* single entry */
402 i++;
403 } else if (gtab[i].code == UNI_UNDEF) {
404 range_size[i] = 0; /* no entry */
405 i++;
406 } else { /* gtab[i].code >= 0 */
407 j = i;
408 while (i < 256 && gtab[i + 1].code >= 0
409 && gtab[i].code + 1 == gtab[i + 1].code
410 && is_last_byte_valid(j, i, gtab[i].code)
411 )
412 i++;
413 /* at this point i is the last entry of the subrange */
414 i++; /* move i to the next entry */
415 range_size[j] = i - j;
416 }
417 }
418
419 /* calculate bfrange_count and bfchar_count */
420 bfrange_count = 0;
421 bfchar_count = 0;
422 for (i = 0; i < 256;) {
423 if (range_size[i] == 1) {
424 bfchar_count++;
425 i++;
426 } else if (range_size[i] > 1) {
427 bfrange_count++;
428 i += range_size[i];
429 } else
430 i++;
431 }
432
433 /* write out bfrange */
434 i = 0;
435 write_bfrange:
436 if (bfrange_count > 100)
437 subrange_count = 100;
438 else
439 subrange_count = bfrange_count;
440 bfrange_count -= subrange_count;
441 pdf_printf("%i beginbfrange\n", subrange_count);
442 for (j = 0; j < subrange_count; j++) {
443 while (range_size[i] <= 1 && i < 256)
444 i++;
445 assert(i < 256);
446 pdf_printf("<%02X> <%02X> <%s>\n", i, i + range_size[i] - 1,
447 utf16be_str(gtab[i].code));
448 i += range_size[i];
449 }
450 pdf_printf("endbfrange\n");
451 if (bfrange_count > 0)
452 goto write_bfrange;
453
454 /* write out bfchar */
455 i = 0;
456 write_bfchar:
457 if (bfchar_count > 100)
458 subrange_count = 100;
459 else
460 subrange_count = bfchar_count;
461 bfchar_count -= subrange_count;
462 pdf_printf("%i beginbfchar\n", subrange_count);
463 for (j = 0; j < subrange_count; j++) {
464 while (i < 256) {
465 if (range_size[i] > 1)
466 i += range_size[i];
467 else if (range_size[i] == 0)
468 i++;
469 else /* range_size[i] == 1 */
470 break;
471 }
472 assert(i < 256 && gtab[i].code != UNI_UNDEF);
473 if (gtab[i].code == UNI_STRING || gtab[i].code == UNI_EXTRA_STRING) {
474 assert(gtab[i].unicode_seq != NULL);
475 pdf_printf("<%02X> <%s>\n", i, gtab[i].unicode_seq);
476 } else
477 pdf_printf("<%02X> <%s>\n", i, utf16be_str(gtab[i].code));
478 i++;
479 }
480 pdf_printf("endbfchar\n");
481 if (bfchar_count > 0)
482 goto write_bfchar;
483
484 /* free strings allocated by set_glyph_unicode() */
485 for (i = 0; i < 256; ++i) {
486 if (gtab[i].code == UNI_EXTRA_STRING)
487 xfree(gtab[i].unicode_seq);
488 }
489
490 pdf_printf("endcmap\n"
491 "CMapName currentdict /CMap defineresource pop\n"
492 "end\n" "end\n" "%%%%EndResource\n" "%%%%EOF\n");
493 pdfendstream();
494 return objnum;
495 }
496
497 void dumptounicode(void)
498 {
499 struct avl_traverser traverse;
500 integer count;
501 glyph_unicode_entry *gu;
502
503 if (glyph_unicode_tree == NULL) {
504 count = 0;
505 generic_dump(count);
506 return;
507 }
508
509 count = avl_count(glyph_unicode_tree);
510 generic_dump(count);
511
512 avl_t_init(&traverse, glyph_unicode_tree);
513 while (gu = avl_t_next(&traverse)) {
514 dumpcharptr(gu->name);
515 generic_dump(gu->code);
516
517 if (gu->code == UNI_STRING)
518 dumpcharptr(gu->unicode_seq);
519 }
520 }
521
522 void undumptounicode(void)
523 {
524 glyph_unicode_entry *tmp;
525 integer remaining;
526
527 generic_undump(remaining);
528
529 if (remaining == 0)
530 return;
531
532 assert(glyph_unicode_tree == NULL);
533 glyph_unicode_tree =
534 avl_create(comp_glyph_unicode_entry, NULL, &avl_xallocator);
535 assert(glyph_unicode_tree != NULL);
536
537 while (remaining--) {
538 void **result;
539 glyph_unicode_entry *gu = new_glyph_unicode_entry();
540 undumpcharptr(gu->name);
541 if (gu->name == NULL) {
542 pdftex_fail("undumpcharptr(gu->name) got NULL");
543 }
544 generic_undump(gu->code);
545
546 if (gu->code == UNI_STRING) {
547 undumpcharptr(gu->unicode_seq);
548 if (gu->unicode_seq == NULL) {
549 pdftex_fail("undumpcharptr(gu->unicode_seq) got NULL");
550 }
551 }
552
553 result = avl_probe(glyph_unicode_tree, gu);
554 assert(*result == gu);
555 }
556 }