Debian Codegraph @

texk/dvipdfm-x/agl.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata,
4 the dvipdfmx project team.
5
6 Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 */
22
23 /*
24 * References:
25 *
26 * Unicode and Glyph Names, ver. 2.3., Adobe Solution Network
27 * http://partners.adobe.com/asn/tech/type/unicodegn.jsp
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #include <string.h>
35 #include <ctype.h>
36
37 #include "system.h"
38 #include "mem.h"
39 #include "error.h"
40
41 #include "mfileio.h"
42 #include "pdfparse.h"
43 #include "dpxconf.h"
44 /* Hash */
45 #include "dpxutil.h"
46 #include "dpxfile.h"
47
48 #include "unicode.h"
49
50 #include "agl.h"
51
52 static int agl_load_listfile (const char *filename, int format);
53
54 static agl_name *
55 agl_new_name (void)
56 {
57 agl_name *agln;
58
59 agln = NEW(1, agl_name);
60 agln->name = NULL;
61 agln->suffix = NULL;
62 agln->n_components = 0;
63 agln->alternate = NULL;
64 agln->is_predef = 0;
65
66 return agln;
67 }
68
69 static void
70 agl_release_name (agl_name *agln)
71 {
72 agl_name *next;
73
74 while (agln) {
75 next = agln->alternate;
76 if (agln->name)
77 RELEASE(agln->name);
78 if (agln->suffix)
79 RELEASE(agln->suffix);
80 agln->name = NULL;
81 RELEASE(agln);
82 agln = next;
83 }
84 }
85
86 char *
87 agl_chop_suffix (const char *glyphname, char **suffix)
88 {
89 char *name, *p;
90 int len;
91
92 ASSERT(glyphname && suffix);
93
94 p = strchr(glyphname, '.');
95 if (p) {
96 len = strlen(glyphname) - strlen(p);
97 if (len < 1) {
98 name = NULL;
99 *suffix = NEW(strlen(glyphname), char);
100 strcpy(*suffix, glyphname+1);
101 } else {
102 p++;
103 name = NEW(len + 1, char);
104 strncpy(name, glyphname, len);
105 name[len] = '\0';
106 if (p[0] == '\0') {
107 *suffix = NULL;
108 } else {
109 *suffix = NEW(strlen(p) + 1, char);
110 strcpy(*suffix, p);
111 }
112 }
113 } else {
114 name = NEW(strlen(glyphname) + 1, char);
115 strcpy(name, glyphname);
116 *suffix = NULL;
117 }
118
119 return name;
120 }
121
122 static const char * const modifiers[] = {
123 "acute", "breve", "caron", "cedilla", "circumflex",
124 "dieresis", "dotaccent", "grave", "hungarumlaut",
125 "macron", "ogonek", "ring", "tilde", "commaaccent",
126 "slash",
127
128 /* The following entries are not accent nor something
129 * but PS font may have those "small" version...
130 */
131 "ampersand", "exclam", "exclamdown",
132 "question","questiondown",
133 NULL
134 };
135
136 static int
137 skip_capital (const char **p, const char *endptr)
138 {
139 int slen = 0, len;
140
141 len = (int) (endptr - (*p));
142
143 if (len >= 2 &&
144 ((**p == 'A' && *(*p+1) == 'E') ||
145 (**p == 'O' && *(*p+1) == 'E'))) {
146 *p += 2;
147 slen = 2;
148 } else if (len >= 3 &&
149 **p == 'E' &&
150 *(*p+1) == 't' &&
151 *(*p+2) == 'h') {
152 *p += 3;
153 slen = 3;
154 } else if (len >= 5 &&
155 **p == 'T' &&
156 *(*p+1) == 'h' &&
157 *(*p+2) == 'o' &&
158 *(*p+3) == 'r' &&
159 *(*p+4) == 'n') {
160 *p += 5;
161 slen = 5;
162 } else if (len >= 1 &&
163 **p >= 'A' && **p <= 'Z') {
164 *p += 1;
165 slen = 1;
166 }
167
168 return slen;
169 }
170
171 static int
172 skip_modifier (const char **p, const char *endptr)
173 {
174 int slen = 0, len;
175 int i;
176
177 len = (int) (endptr - (*p));
178
179 for (i = 0; modifiers[i] != NULL; i++) {
180 if ((len >= strlen(modifiers[i]) &&
181 !memcmp(*p, modifiers[i], len))) {
182 slen = strlen(modifiers[i]);
183 *p += slen;
184 break;
185 }
186 }
187
188 return slen;
189 }
190
191 static int
192 is_smallcap (const char *glyphname)
193 {
194 int len, slen;
195 const char *p, *endptr;
196
197 if (!glyphname)
198 return 0;
199
200 p = glyphname;
201 len = strlen(glyphname);
202 if (len < 6 ||
203 strcmp(p + len - 5, "small"))
204 return 0;
205
206 endptr = p + len - 5;
207
208 len -= 5;
209 slen = skip_modifier(&p, endptr);
210 if (slen == len)
211 return 1; /* Acutesmall, Gravesmall, etc */
212 else if (slen > 0) { /* ??? */
213 return 0;
214 }
215
216 len -= skip_capital(&p, endptr);
217 if (len == 0) {
218 return 1; /* Asmall, AEsmall, etc */
219 }
220
221 while (len > 0) { /* allow multiple accent */
222 slen = skip_modifier(&p, endptr);
223 if (slen == 0)
224 return 0;
225 len -= slen;
226 }
227
228 return 1;
229 }
230
231 #define SUFFIX_LIST_MAX 16
232 #define AGL_VAR_SMCP_IDX 0
233 static struct {
234 const char *key;
235 const char *otl_tag;
236 const char *suffixes[SUFFIX_LIST_MAX];
237 } var_list[] = {
238 {"small" , "smcp", {"sc", NULL}},
239 {"swash" , "swsh", {NULL}},
240 {"superior" , "sups", {NULL}},
241 {"inferior" , "sinf", {NULL}},
242 {"numerator" , "numr", {NULL}},
243 {"denominator" , "dnom", {NULL}},
244 {"oldstyle" , "onum", {NULL}},
245
246 /* The following only used by TeX, there are no
247 * corresponding OTL feat. tag.
248 */
249 {"display" , NULL, {NULL}},
250 {"text" , NULL, {NULL}},
251 {"big" , NULL, {NULL}},
252 {"bigg" , NULL, {NULL}},
253 {"Big" , NULL, {NULL}},
254 {"Bigg" , NULL, {NULL}},
255 {NULL, NULL, {NULL}}
256 };
257
258 const char *
259 agl_suffix_to_otltag (const char *suffix)
260 {
261 int i, j;
262
263 for (i = 0; var_list[i].key; i++) {
264 for (j = 0; var_list[i].suffixes[j]; j++) {
265 if (!strcmp(suffix, var_list[i].suffixes[j]))
266 return var_list[i].otl_tag;
267 }
268 if (!strcmp(suffix, var_list[i].key))
269 return var_list[i].otl_tag;
270 if (var_list[i].otl_tag &&
271 !strcmp(suffix, var_list[i].otl_tag))
272 return var_list[i].otl_tag;
273 }
274
275 return NULL;
276 }
277
278 static int
279 agl_guess_name (const char *glyphname)
280 {
281 int i, len;
282
283 if (is_smallcap(glyphname))
284 return AGL_VAR_SMCP_IDX;
285
286 len = strlen(glyphname);
287 for (i = 1; var_list[i].key != NULL; i++) {
288 if (len > strlen(var_list[i].key) &&
289 !strcmp(glyphname+len-strlen(var_list[i].key), var_list[i].key)
290 ) {
291 return i;
292 }
293 }
294
295 return -1;
296 }
297
298 static agl_name *
299 agl_normalized_name (char *glyphname)
300 {
301 agl_name *agln;
302 char *suffix;
303 int i, n;
304
305 if (!glyphname)
306 return NULL;
307
308 agln = agl_new_name();
309 suffix = strchr(glyphname, '.');
310 if (suffix) {
311 n = strlen(glyphname) - strlen(suffix);
312 if (suffix[1] != '\0') {
313 agln->suffix = NEW(strlen(suffix), char);
314 strcpy(agln->suffix, suffix+1);
315 }
316 agln->name = NEW(n+1, char);
317 memcpy(agln->name, glyphname, n);
318 agln->name[n] = '\0';
319 } else if (is_smallcap(glyphname)) {
320 n = strlen(glyphname) - 5;
321 agln->suffix = NEW(3, char);
322 strcpy(agln->suffix, "sc");
323 agln->name = NEW(n+1, char);
324 for (i = 0; i < n; i++) {
325 agln->name[i] = isupper((unsigned char)glyphname[i]) ?
326 (glyphname[i] + 32) : glyphname[i];
327 }
328 agln->name[n] = '\0';
329 } else {
330 int var_idx;
331
332 #define SET_STRING(p,s) do {\
333 (p) = NEW(strlen((s))+1, char);\
334 strcpy((p),(s));\
335 } while (0)
336 var_idx = agl_guess_name(glyphname);
337 if (var_idx < 0 ||
338 !var_list[var_idx].key) {
339 n = strlen(glyphname);
340 } else {
341 n = strlen(glyphname) - strlen(var_list[var_idx].key);
342 if (var_list[var_idx].suffixes[0])
343 SET_STRING(agln->suffix, var_list[var_idx].suffixes[0]);
344 else {
345 SET_STRING(agln->suffix, var_list[var_idx].key);
346 }
347 }
348 agln->name = NEW(n+1, char);
349 memcpy(agln->name, glyphname, n);
350 agln->name[n] = '\0';
351 }
352
353 return agln;
354 }
355
356 static struct ht_table aglmap;
357
358 static inline void
359 hval_free (void *hval)
360 {
361 agl_release_name((struct agl_name *) hval);
362 }
363
364 void
365 agl_init_map (void)
366 {
367 ht_init_table(&aglmap, hval_free);
368 agl_load_listfile(AGL_EXTRA_LISTFILE, 0);
369 if (agl_load_listfile(AGL_PREDEF_LISTFILE, 1) < 0) {
370 WARN("Failed to load AGL file \"%s\"...", AGL_PREDEF_LISTFILE);
371 }
372 if (agl_load_listfile(AGL_DEFAULT_LISTFILE, 0) < 0) {
373 WARN("Failed to load AGL file \"%s\"...", AGL_DEFAULT_LISTFILE);
374 }
375 }
376
377 void
378 agl_close_map (void)
379 {
380 ht_clear_table(&aglmap);
381 }
382
383 #define WBUF_SIZE 1024
384
385 static int
386 agl_load_listfile (const char *filename, int is_predef)
387 {
388 int count = 0;
389 const char *p, *endptr;
390 char *nextptr;
391 char wbuf[WBUF_SIZE];
392 FILE *fp;
393
394 if (!filename)
395 return -1;
396
397 fp = DPXFOPEN(filename, DPX_RES_TYPE_AGL);
398 if (!fp) {
399 return -1;
400 }
401
402 if (dpx_conf.verbose_level > 0)
403 MESG("<AGL:%s", filename);
404
405 while ((p = mfgets(wbuf, WBUF_SIZE, fp)) != NULL) {
406 agl_name *agln, *duplicate;
407 char *name;
408 int n_unicodes, i;
409 int32_t unicodes[AGL_MAX_UNICODES];
410
411 endptr = p + strlen(p);
412 skip_white(&p, endptr);
413
414 /* Need table version check. */
415 if (!p || p[0] == '#' || p >= endptr)
416 continue;
417 nextptr = strchr(p, ';');
418 if (!nextptr || nextptr == p)
419 continue;
420
421 name = parse_ident(&p, nextptr);
422
423 skip_white(&p, endptr);
424 if (!name || p[0] != ';') {
425 WARN("Invalid AGL entry: %s", wbuf);
426 if (name)
427 RELEASE(name);
428 continue;
429 }
430
431 p++;
432 skip_white(&p, endptr);
433
434 n_unicodes = 0;
435 while (p < endptr &&
436 ((p[0] >= '0' && p[0] <= '9') ||
437 (p[0] >= 'A' && p[0] <= 'F'))
438 ) {
439
440 if (n_unicodes >= AGL_MAX_UNICODES) {
441 WARN("Too many Unicode values");
442 break;
443 }
444 unicodes[n_unicodes++] = strtol(p, &nextptr, 16);
445
446 p = nextptr;
447 skip_white(&p, endptr);
448 }
449
450 if (n_unicodes == 0) {
451 WARN("AGL entry ignored (no mapping): %s", wbuf);
452 RELEASE(name);
453 continue;
454 }
455
456 agln = agl_normalized_name(name);
457 agln->is_predef = is_predef;
458 agln->n_components = n_unicodes;
459 for (i = 0; i < n_unicodes; i++) {
460 agln->unicodes[i] = unicodes[i];
461 }
462
463 duplicate = ht_lookup_table(&aglmap, name, strlen(name));
464 if (!duplicate)
465 ht_append_table(&aglmap, name, strlen(name), agln);
466 else {
467 while (duplicate->alternate)
468 duplicate = duplicate->alternate;
469 duplicate->alternate = agln;
470 }
471
472 if (dpx_conf.verbose_level > 5) {
473 if (agln->suffix)
474 MESG("agl: %s [%s.%s] -->", name, agln->name, agln->suffix);
475 else
476 MESG("agl: %s [%s] -->", name, agln->name);
477 for (i = 0; i < agln->n_components; i++) {
478 if (agln->unicodes[i] > 0xffff) {
479 MESG(" U+%06X", agln->unicodes[i]);
480 } else {
481 MESG(" U+%04X", agln->unicodes[i]);
482 }
483 }
484 MESG("\n");
485 }
486
487 RELEASE(name);
488 count++;
489 }
490 DPXFCLOSE(fp);
491
492 if (dpx_conf.verbose_level > 0)
493 MESG(">");
494
495 return count;
496 }
497
498 agl_name *
499 agl_lookup_list (const char *glyphname)
500 {
501 agl_name *agln;
502
503 if (!glyphname)
504 return NULL;
505
506 agln = ht_lookup_table(&aglmap, glyphname, strlen(glyphname));
507
508 return agln;
509 }
510
511 int
512 agl_name_is_unicode (const char *glyphname)
513 {
514 char c, *suffix;
515 int i, len;
516
517 if (!glyphname)
518 return 0;
519
520 suffix = strchr(glyphname, '.');
521 len = (int) (suffix ? suffix - glyphname : strlen(glyphname));
522 /*
523 * uni02ac is invalid glyph name and mapped to th empty string.
524 */
525 if (len >= 7 && (len - 3) % 4 == 0 &&
526 !strncmp(glyphname, "uni", 3)) {
527 c = glyphname[3];
528 /*
529 * Check if the 4th character is uppercase hexadecimal digit.
530 * "union" should not be treated as Unicode glyph name.
531 */
532 if (isdigit((unsigned char)c) || (c >= 'A' && c <= 'F'))
533 return 1;
534 else
535 return 0;
536 } else if (len <= 7 && len >= 5 &&
537 glyphname[0] == 'u') {
538 for (i = 1; i < len - 1; i++) {
539 c = glyphname[i];
540 if (!isdigit((unsigned char)c) && (c < 'A' || c > 'F'))
541 return 0;
542 }
543 return 1;
544 }
545
546 return 0;
547 }
548
549 int32_t
550 agl_name_convert_unicode (const char *glyphname)
551 {
552 int32_t ucv = -1;
553 const char *p;
554
555 if (!agl_name_is_unicode(glyphname))
556 return -1;
557
558 if (strlen(glyphname) > 7 && *(glyphname+7) != '.') {
559 WARN("Mapping to multiple Unicode characters not supported.");
560 return -1;
561 }
562
563 if (glyphname[1] == 'n')
564 p = glyphname + 3;
565 else
566 p = glyphname + 1;
567 ucv = 0;
568 while (*p != '\0' && *p != '.') {
569 if (!isdigit((unsigned char)*p) && (*p < 'A' || *p > 'F')) {
570 WARN("Invalid char %c in Unicode glyph name %s.", *p, glyphname);
571 return -1;
572 }
573 ucv <<= 4;
574 ucv += isdigit((unsigned char)*p) ? *p - '0' : *p - 'A' + 10;
575 p++;
576 }
577
578 if (!UC_is_valid(ucv)) {
579 if (ucv < 0x10000) {
580 WARN("Invalid Unicode code value U+%04X.", ucv);
581 } else {
582 WARN("Invalid Unicode code value U+%06X.", ucv);
583 }
584 ucv = -1;
585 }
586
587 return ucv;
588 }
589
590
591
592 static int
593 xtol (const char *start, int len)
594 {
595 int v = 0;
596
597 while (len-- > 0) {
598 v <<= 4;
599 if (isdigit((unsigned char)*start)) {
600 v += *start - '0';
601 } else if (*start >= 'A' && *start <= 'F') {
602 v += *start - 'A' + 10;
603 } else {
604 return -1;
605 }
606 start++;
607 }
608
609 return v;
610 }
611
612 #define IS_PUA(u) (((u) >= 0x00E000L && (u) <= 0x00F8FFL) || \
613 ((u) >= 0x0F0000L && (u) <= 0x0FFFFDL) || \
614 ((u) >= 0x100000L && (u) <= 0x10FFFDL) \
615 )
616
617 static int32_t
618 put_unicode_glyph (const char *name,
619 unsigned char **dstpp, unsigned char *limptr)
620 {
621 const char *p;
622 int32_t len = 0, ucv;
623
624 p = name;
625 ucv = 0;
626
627 if (p[1] != 'n') {
628 p += 1;
629 ucv = xtol(p, strlen(p));
630 len += UC_UTF16BE_encode_char(ucv, dstpp, limptr);
631 } else {
632 p += 3;
633 while (*p != '\0') {
634 ucv = xtol(p, 4);
635 len += UC_UTF16BE_encode_char(ucv, dstpp, limptr);
636 p += 4;
637 }
638 }
639
640 return len;
641 }
642
643 int32_t
644 agl_sput_UTF16BE (const char *glyphstr,
645 unsigned char **dstpp, unsigned char *limptr,
646 int *fail_count)
647 {
648 int32_t len = 0;
649 int count = 0;
650 const char *p, *endptr;
651
652 ASSERT(glyphstr && dstpp);
653
654 p = glyphstr;
655 endptr = strchr(p, '.');
656 if (!endptr)
657 endptr = p + strlen(p);
658
659 while (p < endptr) {
660 char *name;
661 const char *delim;
662 int32_t sub_len;
663 int i;
664 agl_name *agln0, *agln1 = NULL;
665
666 delim = strchr(p, '_');
667 if (delim == p) {
668 /*
669 * Glyph names starting with a underscore or two subsequent
670 * underscore in glyph name not allowed?
671 */
672 WARN("Invalid glyph name component in \"%s\".", glyphstr);
673 count++;
674 if (fail_count)
675 *fail_count = count;
676 return len; /* Cannot continue */
677 } else if (!delim || delim > endptr) {
678 delim = endptr;
679 }
680 sub_len = (int32_t) (delim - p);
681
682 name = NEW(sub_len+1, char);
683 memcpy(name, p, sub_len);
684 name[sub_len] = '\0';
685
686 if (agl_name_is_unicode(name)) {
687 sub_len = put_unicode_glyph(name, dstpp, limptr);
688 if (sub_len > 0)
689 len += sub_len;
690 else {
691 count++;
692 }
693 } else {
694 agln1 = agl_lookup_list(name);
695 if (!agln1 || (agln1->n_components == 1 &&
696 IS_PUA(agln1->unicodes[0]))) {
697 agln0 = agl_normalized_name(name);
698 if (agln0) {
699 if (dpx_conf.verbose_level > 1 && agln0->suffix) {
700 WARN("agl: fix %s --> %s.%s",
701 name, agln0->name, agln0->suffix);
702 }
703 agln1 = agl_lookup_list(agln0->name);
704 agl_release_name(agln0);
705 }
706 }
707 if (agln1) {
708 for (i = 0; i < agln1->n_components; i++) {
709 len += UC_UTF16BE_encode_char(agln1->unicodes[i], dstpp, limptr);
710 }
711 } else {
712 if (dpx_conf.verbose_level > 0) {
713 WARN("No Unicode mapping for glyph name \"%s\" found.", name);
714 }
715 count++;
716 }
717 }
718 RELEASE(name);
719 p = delim + 1;
720 }
721
722 if (fail_count)
723 *fail_count = count;
724 return len;
725 }
726
727 int
728 agl_get_unicodes (const char *glyphstr,
729 int32_t *unicodes, int max_unicodes)
730 {
731 int count = 0;
732 const char *p, *endptr;
733
734 p = glyphstr;
735 endptr = strchr(p, '.');
736 if (!endptr)
737 endptr = p + strlen(p);
738
739 while (p < endptr) {
740 char *name;
741 const char *delim;
742 int32_t sub_len;
743 int i;
744 agl_name *agln0, *agln1 = NULL;
745
746 delim = strchr(p, '_');
747 if (delim == p) {
748 /*
749 * Glyph names starting with a underscore or two subsequent
750 * underscore in glyph name not allowed?
751 */
752 WARN("Invalid glyph name component in \"%s\".", glyphstr);
753 return -1; /* Cannot continue */
754 } else if (!delim || delim > endptr) {
755 delim = endptr;
756 }
757 sub_len = (int32_t) (delim - p);
758
759 name = NEW(sub_len+1, char);
760 memcpy(name, p, sub_len);
761 name[sub_len] = '\0';
762
763 if (agl_name_is_unicode(name)) {
764 p = name;
765 if (p[1] != 'n') { /* uXXXXXXXX */
766 if (count >= max_unicodes) {
767 RELEASE(name);
768 return -1;
769 }
770 p++;
771 unicodes[count++] = xtol(p, strlen(p));
772 } else {
773 p += 3;
774 while (*p != '\0') {
775 if (count >= max_unicodes) {
776 RELEASE(name);
777 return -1;
778 }
779 unicodes[count++] = xtol(p, 4);
780 p += 4;
781 }
782 }
783 } else {
784 agln1 = agl_lookup_list(name);
785 if (!agln1 || (agln1->n_components == 1 &&
786 IS_PUA(agln1->unicodes[0]))) {
787 agln0 = agl_normalized_name(name);
788 if (agln0) {
789 if (dpx_conf.verbose_level > 1 && agln0->suffix) {
790 WARN("agl: fix %s --> %s.%s",
791 name, agln0->name, agln0->suffix);
792 }
793 agln1 = agl_lookup_list(agln0->name);
794 agl_release_name(agln0);
795 }
796 }
797 if (agln1) {
798 if (count + agln1->n_components > max_unicodes) {
799 RELEASE(name);
800 return -1;
801 }
802 for (i = 0; i < agln1->n_components; i++) {
803 unicodes[count++] = agln1->unicodes[i];
804 }
805 } else {
806 if (dpx_conf.verbose_level > 1)
807 WARN("No Unicode mapping for glyph name \"%s\" found.", name);
808 RELEASE(name);
809 return -1;
810 }
811 }
812 RELEASE(name);
813 p = delim + 1;
814 }
815
816 return count;
817 }