Debian Codegraph @

texk/dvipdfm-x/fontmap.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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "system.h"
28 #include "mem.h"
29 #include "error.h"
30
31 #include "dpxconf.h"
32 #include "dpxfile.h"
33 #include "dpxutil.h"
34
35 #include "subfont.h"
36
37 #include "fontmap.h"
38
39 /* CIDFont */
40 static char *strip_options (const char *map_name, fontmap_opt *opt);
41
42 void
43 pdf_init_fontmap_record (fontmap_rec *mrec)
44 {
45 ASSERT(mrec);
46
47 mrec->map_name = NULL;
48
49 /* SFD char mapping */
50 mrec->charmap.sfd_name = NULL;
51 mrec->charmap.subfont_id = NULL;
52 /* for OFM */
53 mrec->opt.mapc = -1; /* compatibility */
54
55 mrec->font_name = NULL;
56 mrec->enc_name = NULL;
57
58 mrec->opt.slant = 0.0;
59 mrec->opt.extend = 1.0;
60 mrec->opt.bold = 0.0;
61
62 mrec->opt.flags = 0;
63
64 mrec->opt.design_size = -1.0;
65
66 mrec->opt.tounicode = NULL;
67 mrec->opt.otl_tags = NULL;
68 mrec->opt.index = 0;
69 mrec->opt.charcoll = NULL;
70 mrec->opt.style = FONTMAP_STYLE_NONE;
71 mrec->opt.stemv = -1; /* not given explicitly by an option */
72 mrec->opt.use_glyph_encoding = 0;
73 }
74
75 void
76 pdf_clear_fontmap_record (fontmap_rec *mrec)
77 {
78 ASSERT(mrec);
79
80 if (mrec->map_name)
81 RELEASE(mrec->map_name);
82 if (mrec->charmap.sfd_name)
83 RELEASE(mrec->charmap.sfd_name);
84 if (mrec->charmap.subfont_id)
85 RELEASE(mrec->charmap.subfont_id);
86 if (mrec->enc_name)
87 RELEASE(mrec->enc_name);
88 if (mrec->font_name)
89 RELEASE(mrec->font_name);
90
91 if (mrec->opt.tounicode)
92 RELEASE(mrec->opt.tounicode);
93 if (mrec->opt.otl_tags)
94 RELEASE(mrec->opt.otl_tags);
95 if (mrec->opt.charcoll)
96 RELEASE(mrec->opt.charcoll);
97 pdf_init_fontmap_record(mrec);
98 }
99
100 /* strdup: just returns NULL for NULL */
101 static char *
102 mstrdup (const char *s)
103 {
104 char *r;
105 if (!s)
106 return NULL;
107 r = NEW(strlen(s) + 1, char);
108 strcpy(r, s);
109 return r;
110 }
111
112 static void
113 pdf_copy_fontmap_record (fontmap_rec *dst, const fontmap_rec *src)
114 {
115 ASSERT( dst && src );
116
117 dst->map_name = mstrdup(src->map_name);
118
119 dst->charmap.sfd_name = mstrdup(src->charmap.sfd_name);
120 dst->charmap.subfont_id = mstrdup(src->charmap.subfont_id);
121
122 dst->font_name = mstrdup(src->font_name);
123 dst->enc_name = mstrdup(src->enc_name);
124
125 dst->opt.slant = src->opt.slant;
126 dst->opt.extend = src->opt.extend;
127 dst->opt.bold = src->opt.bold;
128
129 dst->opt.flags = src->opt.flags;
130 dst->opt.mapc = src->opt.mapc;
131
132 dst->opt.tounicode = mstrdup(src->opt.tounicode);
133 dst->opt.otl_tags = mstrdup(src->opt.otl_tags);
134 dst->opt.index = src->opt.index;
135 dst->opt.charcoll = mstrdup(src->opt.charcoll);
136 dst->opt.style = src->opt.style;
137 dst->opt.stemv = src->opt.stemv;
138 dst->opt.use_glyph_encoding = src->opt.use_glyph_encoding;
139 }
140
141
142 static void
143 hval_free (void *vp)
144 {
145 fontmap_rec *mrec = (fontmap_rec *) vp;
146 pdf_clear_fontmap_record(mrec);
147 RELEASE(mrec);
148 }
149
150
151 static void
152 fill_in_defaults (fontmap_rec *mrec, const char *tex_name)
153 {
154 if (mrec->enc_name &&
155 (!strcmp(mrec->enc_name, "default") ||
156 !strcmp(mrec->enc_name, "none"))) {
157 RELEASE(mrec->enc_name);
158 mrec->enc_name = NULL;
159 }
160 if (mrec->font_name &&
161 (!strcmp(mrec->font_name, "default") ||
162 !strcmp(mrec->font_name, "none"))) {
163 RELEASE(mrec->font_name);
164 mrec->font_name = NULL;
165 }
166 /* We *must* fill font_name either explicitly or by default */
167 if (!mrec->font_name) {
168 mrec->font_name = NEW(strlen(tex_name)+1, char);
169 strcpy(mrec->font_name, tex_name);
170 }
171
172 mrec->map_name = NEW(strlen(tex_name)+1, char);
173 strcpy(mrec->map_name, tex_name);
174
175 #ifndef WITHOUT_COMPAT
176 /* Use "UCS" character collection for Unicode SFD
177 * and Identity CMap combination. For backward
178 * compatibility.
179 */
180 if (mrec->charmap.sfd_name && mrec->enc_name &&
181 !mrec->opt.charcoll) {
182 if ((!strcmp(mrec->enc_name, "Identity-H") ||
183 !strcmp(mrec->enc_name, "Identity-V"))
184 &&
185 (strstr(mrec->charmap.sfd_name, "Uni") ||
186 strstr(mrec->charmap.sfd_name, "UBig") ||
187 strstr(mrec->charmap.sfd_name, "UBg") ||
188 strstr(mrec->charmap.sfd_name, "UGB") ||
189 strstr(mrec->charmap.sfd_name, "UKS") ||
190 strstr(mrec->charmap.sfd_name, "UJIS"))) {
191 mrec->opt.charcoll = NEW(strlen("UCS")+1, char);
192 strcpy(mrec->opt.charcoll, "UCS");
193 }
194 }
195 #endif /* WITHOUT_COMPAT */
196
197 return;
198 }
199
200 static char *
201 readline (char *buf, int buf_len, FILE *fp)
202 {
203 char *p, *q;
204 ASSERT( buf && buf_len > 0 && fp );
205 p = mfgets(buf, buf_len, fp);
206 if (!p)
207 return NULL;
208 q = strchr(p, '%'); /* we don't have quoted string */
209 if (q)
210 *q = '\0';
211 return p;
212 }
213
214 #ifndef ISBLANK
215 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
216 #endif
217 static void
218 skip_blank (const char **pp, const char *endptr)
219 {
220 const char *p = *pp;
221 if (!p || p >= endptr)
222 return;
223 for ( ; p < endptr && ISBLANK(*p); p++);
224 *pp = p;
225 }
226
227 static char *
228 parse_string_value (const char **pp, const char *endptr)
229 {
230 char *q = NULL;
231 const char *p = *pp;
232 int n;
233
234 if (!p || p >= endptr)
235 return NULL;
236 if (*p == '"')
237 q = parse_c_string(&p, endptr);
238 else {
239 for (n = 0; p < endptr && !isspace((unsigned char)*p); p++, n++);
240 if (n == 0)
241 return NULL;
242 q = NEW(n + 1, char);
243 memcpy(q, *pp, n); q[n] = '\0';
244 }
245
246 *pp = p;
247 return q;
248 }
249
250 /* no preceeding spaces allowed */
251 static char *
252 parse_integer_value (const char **pp, const char *endptr, int base)
253 {
254 char *q;
255 const char *p = *pp;
256 int has_sign = 0, has_prefix = 0, n;
257
258 ASSERT( base == 0 || (base >= 2 && base <= 36) );
259
260 if (!p || p >= endptr)
261 return NULL;
262
263 if (*p == '-' || *p == '+') {
264 p++; has_sign = 1;
265 }
266 if ((base == 0 || base == 16) &&
267 p + 2 <= endptr &&
268 p[0] == '0' && p[1] == 'x') {
269 p += 2; has_prefix = 1;
270 }
271 if (base == 0) {
272 if (has_prefix)
273 base = 16;
274 else if (p < endptr && *p == '0')
275 base = 8;
276 else {
277 base = 10;
278 }
279 }
280 #define ISDIGIT_WB(c,b) ( \
281 ((b) <= 10 && (c) >= '0' && (c) < '0' + (b)) || \
282 ((b) > 10 && ( \
283 ((c) >= '0' && (c) <= '9') || \
284 ((c) >= 'a' && (c) < 'a' + ((b) - 10)) || \
285 ((c) >= 'A' && (c) < 'A' + ((b) - 10)) \
286 ) \
287 ) \
288 )
289 for (n = 0; p < endptr && ISDIGIT_WB(*p, base); p++, n++);
290 if (n == 0)
291 return NULL;
292 if (has_sign)
293 n += 1;
294 if (has_prefix)
295 n += 2;
296
297 q = NEW(n + 1, char);
298 memcpy(q, *pp, n); q[n] = '\0';
299
300 *pp = p;
301 return q;
302 }
303
304 static int
305 fontmap_parse_mapdef_dpm (fontmap_rec *mrec,
306 const char *mapdef, const char *endptr)
307 {
308 const char *p = mapdef;
309
310 /*
311 * Parse record line in map file. First two fields (after TeX font
312 * name) are position specific. Arguments start at the first token
313 * beginning with a '-'.
314 *
315 * NOTE:
316 * Dvipdfm basically uses parse_ident() for parsing enc_name,
317 * font_name, and other string values which assumes PostScript-like
318 * syntax.
319 * skip_white() skips '\r' and '\n' but they should terminate
320 * fontmap line here.
321 */
322
323 skip_blank(&p, endptr);
324 /* encoding field */
325 if (p < endptr && *p != '-') { /* May be NULL */
326 mrec->enc_name = parse_string_value(&p, endptr);
327 skip_blank(&p, endptr);
328 }
329
330 /* fontname or font filename field */
331 if (p < endptr && *p != '-') { /* May be NULL */
332 mrec->font_name = parse_string_value(&p, endptr);
333 skip_blank(&p, endptr);
334 }
335 if (mrec->font_name) {
336 char *tmp;
337 /* Several options are encoded in font_name for
338 * compatibility with dvipdfm.
339 */
340 tmp = strip_options(mrec->font_name, &mrec->opt);
341 if (tmp) {
342 RELEASE(mrec->font_name);
343 mrec->font_name = tmp;
344 }
345 }
346
347 skip_blank(&p, endptr);
348 /* Parse any remaining arguments */
349 while (p + 1 < endptr &&
350 *p != '\r' && *p != '\n' && *p == '-') {
351 char *q, mopt = p[1];
352 int v;
353
354 p += 2; skip_blank(&p, endptr);
355 switch (mopt) {
356
357 case 's': /* Slant option */
358 q = parse_float_decimal(&p, endptr);
359 if (!q) {
360 WARN("Missing a number value for 's' option.");
361 return -1;
362 }
363 mrec->opt.slant = atof(q);
364 RELEASE(q);
365 break;
366
367 case 'e': /* Extend option */
368 q = parse_float_decimal(&p, endptr);
369 if (!q) {
370 WARN("Missing a number value for 'e' option.");
371 return -1;
372 }
373 mrec->opt.extend = atof(q);
374 if (mrec->opt.extend <= 0.0) {
375 WARN("Invalid value for 'e' option: %s", q);
376 return -1;
377 }
378 RELEASE(q);
379 break;
380
381 case 'b': /* Fake-bold option */
382 q = parse_float_decimal(&p, endptr);
383 if (!q) {
384 WARN("Missing a number value for 'b' option.");
385 return -1;
386 }
387 mrec->opt.bold = atof(q);
388 if (mrec->opt.bold <= 0.0) {
389 WARN("Invalid value for 'b' option: %s", q);
390 return -1;
391 }
392 RELEASE(q);
393 break;
394
395 case 'r': /* Remap option; obsolete; just ignore */
396 break;
397
398 case 'i': /* TTC index */
399 q = parse_integer_value(&p, endptr, 10);
400 if (!q) {
401 WARN("Missing TTC index number...");
402 return -1;
403 }
404 mrec->opt.index = strtoul(q, NULL, 10);
405 RELEASE(q);
406 break;
407
408 case 'p': /* UCS plane: just for testing */
409 q = parse_integer_value(&p, endptr, 0);
410 if (!q) {
411 WARN("Missing a number for 'p' option.");
412 return -1;
413 }
414 v = strtol(q, NULL, 0);
415 if (v < 0 || v > 16)
416 WARN("Invalid value for option 'p': %s", q);
417 else {
418 mrec->opt.mapc = v << 16;
419 }
420 RELEASE(q);
421 break;
422
423 case 'u': /* ToUnicode */
424 q = parse_string_value(&p, endptr);
425 if (q)
426 mrec->opt.tounicode = q;
427 else {
428 WARN("Missing string value for option 'u'.");
429 return -1;
430 }
431 break;
432
433 case 'v': /* StemV */
434 q = parse_integer_value(&p, endptr, 10);
435 if (!q) {
436 WARN("Missing a number for 'v' option.");
437 return -1;
438 }
439 mrec->opt.stemv = strtol(q, NULL, 0);
440 RELEASE(q);
441 break;
442
443 /* 2017.4.15 back again */
444 case 'l':
445 q = parse_string_value(&p, endptr);
446 if (q)
447 mrec->opt.otl_tags = q;
448 else {
449 WARN("Missing string value for option 'l'.");
450 return -1;
451 }
452 break;
453
454 /* Omega uses both single-byte and double-byte set_char command
455 * even for double-byte OFMs. This confuses CMap decoder.
456 */
457 case 'm':
458 /* Map single bytes char 0xab to double byte char 0xcdab */
459 if (p + 4 <= endptr &&
460 p[0] == '<' && p[3] == '>') {
461 p++;
462 q = parse_integer_value(&p, endptr, 16);
463 if (!q) {
464 WARN("Invalid value for option 'm'.");
465 return -1;
466 } else if (p < endptr && *p != '>') {
467 WARN("Invalid value for option 'm': %s", q);
468 RELEASE(q);
469 return -1;
470 }
471 v = strtol(q, NULL, 16);
472 mrec->opt.mapc = ((v << 8) & 0x0000ff00L);
473 RELEASE(q); p++;
474 } else if (p + 4 <= endptr &&
475 !memcmp(p, "sfd:", strlen("sfd:"))) {
476 char *r;
477 const char *rr;
478 /* SFD mapping: sfd:Big5,00 */
479 p += 4; skip_blank(&p, endptr);
480 q = parse_string_value(&p, endptr);
481 if (!q) {
482 WARN("Missing value for option 'm'.");
483 return -1;
484 }
485 r = strchr(q, ',');
486 if (!r) {
487 WARN("Invalid value for option 'm': %s", q);
488 RELEASE(q);
489 return -1;
490 }
491 *r = 0; rr = ++r; skip_blank(&rr, r + strlen(r));
492 if (*rr == '\0') {
493 WARN("Invalid value for option 'm': %s,", q);
494 RELEASE(q);
495 return -1;
496 }
497 mrec->charmap.sfd_name = mstrdup(q);
498 mrec->charmap.subfont_id = mstrdup(rr);
499 RELEASE(q);
500 } else if (p + 4 < endptr &&
501 !memcmp(p, "pad:", strlen("pad:"))) {
502 p += 4; skip_blank(&p, endptr);
503 q = parse_integer_value(&p, endptr, 16);
504 if (!q) {
505 WARN("Invalid value for option 'm'.");
506 return -1;
507 } else if (p < endptr && !isspace((unsigned char)*p)) {
508 WARN("Invalid value for option 'm': %s", q);
509 RELEASE(q);
510 return -1;
511 }
512 v = strtol(q, NULL, 16);
513 mrec->opt.mapc = ((v << 8) & 0x0000ff00L);
514 RELEASE(q);
515 } else {
516 WARN("Invalid value for option 'm'.");
517 return -1;
518 }
519 break;
520
521 case 'w': /* Writing mode (for unicode encoding) */
522 if (!mrec->enc_name ||
523 strcmp(mrec->enc_name, "unicode")) {
524 WARN("Fontmap option 'w' meaningless for encoding other than \"unicode\".");
525 return -1;
526 }
527 q = parse_integer_value(&p, endptr, 10);
528 if (!q) {
529 WARN("Missing wmode value...");
530 return -1;
531 }
532 if (atoi(q) == 1)
533 mrec->opt.flags |= FONTMAP_OPT_VERT;
534 else if (atoi(q) == 0)
535 mrec->opt.flags &= ~FONTMAP_OPT_VERT;
536 else {
537 WARN("Invalid value for option 'w': %s", q);
538 }
539 RELEASE(q);
540 break;
541
542 default:
543 WARN("Unrecognized font map option: '%c'", mopt);
544 return -1;
545 }
546 skip_blank(&p, endptr);
547 }
548
549 if (p < endptr && *p != '\r' && *p != '\n') {
550 WARN("Invalid char in fontmap line: %c", *p);
551 return -1;
552 }
553
554 return 0;
555 }
556
557
558 /* Parse record line in map file of DVIPS/pdfTeX format. */
559 static int
560 fontmap_parse_mapdef_dps (fontmap_rec *mrec,
561 const char *mapdef, const char *endptr)
562 {
563 const char *p = mapdef;
564 char *q;
565
566 skip_blank(&p, endptr);
567
568 /* The first field (after TFM name) must be PostScript name. */
569 /* However, pdftex.map allows a line without PostScript name. */
570
571 if (*p != '"' && *p != '<') {
572 if (p < endptr) {
573 q = parse_string_value(&p, endptr);
574 if (q) RELEASE(q);
575 skip_blank(&p, endptr);
576 } else {
577 WARN("Missing a PostScript font name.");
578 return -1;
579 }
580 }
581
582 if (p >= endptr) return 0;
583
584 /* Parse any remaining arguments */
585 while (p < endptr && *p != '\r' && *p != '\n' && (*p == '<' || *p == '"')) {
586 switch (*p) {
587 case '<': /* encoding or fontfile field */
588 /* If we see <[ or <<, just ignore the second char instead
589 of doing as directed (define encoding file, fully embed); sorry. */
590 if (++p < endptr && (*p == '[' || *p == '<')) p++; /*skip */
591 skip_blank(&p, endptr);
592 if ((q = parse_string_value(&p, endptr))) {
593 int n = strlen(q);
594 if (n > 4 && strncmp(q+n-4, ".enc", 4) == 0)
595 mrec->enc_name = q;
596 else
597 mrec->font_name = q;
598 }
599 skip_blank(&p, endptr);
600 break;
601
602 case '"': /* Options */
603 if ((q = parse_string_value(&p, endptr))) {
604 const char *r = q, *e = q+strlen(q);
605 char *s, *t;
606 skip_blank(&r, e);
607 while (r < e) {
608 if ((s = parse_float_decimal(&r, e))) {
609 skip_blank(&r, e);
610 if ((t = parse_string_value(&r, e))) {
611 if (strcmp(t, "SlantFont") == 0)
612 mrec->opt.slant = atof(s);
613 else if (strcmp(t, "ExtendFont") == 0)
614 mrec->opt.extend = atof(s);
615 RELEASE(t);
616 }
617 RELEASE(s);
618 } else if ((s = parse_string_value(&r, e))) { /* skip */
619 RELEASE(s);
620 }
621 skip_blank(&r, e);
622 }
623 RELEASE(q);
624 }
625 skip_blank(&p, endptr);
626 break;
627
628 default:
629 WARN("Found an invalid entry: %s", p);
630 return -1;
631 }
632 skip_blank(&p, endptr);
633 }
634
635 if (p < endptr && *p != '\r' && *p != '\n') {
636 WARN("Invalid char in fontmap line: %c", *p);
637 return -1;
638 }
639
640 return 0;
641 }
642
643
644 static struct ht_table *fontmap = NULL;
645
646 #define fontmap_invalid(m) (!(m) || !(m)->map_name || !(m)->font_name)
647 static char *
648 chop_sfd_name (const char *tex_name, char **sfd_name)
649 {
650 char *fontname;
651 char *p, *q;
652 int m, n, len;
653
654 *sfd_name = NULL;
655
656 p = strchr(tex_name, '@');
657 if (!p ||
658 p[1] == '\0' || p == tex_name) {
659 return NULL;
660 }
661 m = (int) (p - tex_name);
662 p++;
663 q = strchr(p, '@');
664 if (!q || q == p) {
665 return NULL;
666 }
667 n = (int) (q - p);
668 q++;
669
670 len = strlen(tex_name) - n;
671 fontname = NEW(len+1, char);
672 memcpy(fontname, tex_name, m);
673 fontname[m] = '\0';
674 if (*q)
675 strcat(fontname, q);
676
677 *sfd_name = NEW(n+1, char);
678 memcpy(*sfd_name, p, n);
679 (*sfd_name)[n] = '\0';
680
681 return fontname;
682 }
683
684 static char *
685 make_subfont_name (const char *map_name, const char *sfd_name, const char *sub_id)
686 {
687 char *tfm_name;
688 int n, m;
689 char *p, *q;
690
691 p = strchr(map_name, '@');
692 if (!p || p == map_name)
693 return NULL;
694 m = (int) (p - map_name);
695 q = strchr(p + 1, '@');
696 if (!q || q == p + 1)
697 return NULL;
698 n = (int) (q - p) + 1; /* including two '@' */
699 if (strlen(sfd_name) != n - 2 ||
700 memcmp(p + 1, sfd_name, n - 2))
701 return NULL;
702 tfm_name = NEW(strlen(map_name) - n + strlen(sub_id) + 1, char);
703 memcpy(tfm_name, map_name, m);
704 tfm_name[m] = '\0';
705 strcat(tfm_name, sub_id);
706 if (q[1]) /* not ending with '@' */
707 strcat(tfm_name, q + 1);
708
709 return tfm_name;
710 }
711
712 /* "foo@A@ ..." is expanded to
713 * fooab ... -m sfd:A,ab
714 * ...
715 * fooyz ... -m sfd:A,yz
716 * where 'ab' ... 'yz' is subfont IDs in SFD 'A'.
717 */
718 int
719 pdf_append_fontmap_record (const char *kp, const fontmap_rec *vp)
720 {
721 fontmap_rec *mrec;
722 char *fnt_name, *sfd_name = NULL;
723
724 if (!kp || fontmap_invalid(vp)) {
725 WARN("Invalid fontmap record...");
726 return -1;
727 }
728
729 if (dpx_conf.verbose_level > 3)
730 MESG("fontmap>> append key=\"%s\"...", kp);
731
732 fnt_name = chop_sfd_name(kp, &sfd_name);
733 if (fnt_name && sfd_name) {
734 char *tfm_name;
735 char **subfont_ids;
736 int n = 0;
737 subfont_ids = sfd_get_subfont_ids(sfd_name, &n);
738 if (!subfont_ids)
739 return -1;
740 while (n-- > 0) {
741 tfm_name = make_subfont_name(kp, sfd_name, subfont_ids[n]);
742 if (!tfm_name)
743 continue;
744 mrec = ht_lookup_table(fontmap, tfm_name, strlen(tfm_name));
745 if (!mrec) {
746 mrec = NEW(1, fontmap_rec);
747 pdf_init_fontmap_record(mrec);
748 mrec->map_name = mstrdup(kp); /* link */
749 mrec->charmap.sfd_name = mstrdup(sfd_name);
750 mrec->charmap.subfont_id = mstrdup(subfont_ids[n]);
751 ht_insert_table(fontmap, tfm_name, strlen(tfm_name), mrec);
752 }
753 RELEASE(tfm_name);
754 }
755 RELEASE(fnt_name);
756 RELEASE(sfd_name);
757 }
758
759 mrec = ht_lookup_table(fontmap, kp, strlen(kp));
760 if (!mrec) {
761 mrec = NEW(1, fontmap_rec);
762 pdf_copy_fontmap_record(mrec, vp);
763 if (mrec->map_name && !strcmp(kp, mrec->map_name)) {
764 RELEASE(mrec->map_name);
765 mrec->map_name = NULL;
766 }
767 ht_insert_table(fontmap, kp, strlen(kp), mrec);
768 }
769 if (dpx_conf.verbose_level > 3)
770 MESG("\n");
771
772 return 0;
773 }
774
775 int
776 pdf_remove_fontmap_record (const char *kp)
777 {
778 char *fnt_name, *sfd_name = NULL;
779
780 if (!kp)
781 return -1;
782
783 if (dpx_conf.verbose_level > 3)
784 MESG("fontmap>> remove key=\"%s\"...", kp);
785
786 fnt_name = chop_sfd_name(kp, &sfd_name);
787 if (fnt_name && sfd_name) {
788 char *tfm_name;
789 char **subfont_ids;
790 int n = 0;
791 subfont_ids = sfd_get_subfont_ids(sfd_name, &n);
792 if (!subfont_ids)
793 return -1;
794 if (dpx_conf.verbose_level > 3)
795 MESG("\nfontmap>> Expand @%s@:", sfd_name);
796 while (n-- > 0) {
797 tfm_name = make_subfont_name(kp, sfd_name, subfont_ids[n]);
798 if (!tfm_name)
799 continue;
800 if (dpx_conf.verbose_level > 3)
801 MESG(" %s", tfm_name);
802 ht_remove_table(fontmap, tfm_name, strlen(tfm_name));
803 RELEASE(tfm_name);
804 }
805 RELEASE(fnt_name);
806 RELEASE(sfd_name);
807 }
808
809 ht_remove_table(fontmap, kp, strlen(kp));
810
811 if (dpx_conf.verbose_level > 3)
812 MESG("\n");
813
814 return 0;
815 }
816
817 fontmap_rec *
818 pdf_insert_fontmap_record (const char *kp, const fontmap_rec *vp)
819 {
820 fontmap_rec *mrec;
821 char *fnt_name, *sfd_name;
822
823 if (!kp || fontmap_invalid(vp)) {
824 WARN("Invalid fontmap record...");
825 return NULL;
826 }
827
828 if (dpx_conf.verbose_level > 3)
829 MESG("fontmap>> insert key=\"%s\"...", kp);
830
831 fnt_name = chop_sfd_name(kp, &sfd_name);
832 if (fnt_name && sfd_name) {
833 char *tfm_name;
834 char **subfont_ids;
835 int n = 0;
836 subfont_ids = sfd_get_subfont_ids(sfd_name, &n);
837 if (!subfont_ids) {
838 RELEASE(fnt_name);
839 RELEASE(sfd_name);
840 WARN("Could not open SFD file: %s", sfd_name);
841 return NULL;
842 }
843 if (dpx_conf.verbose_level > 3)
844 MESG("\nfontmap>> Expand @%s@:", sfd_name);
845 while (n-- > 0) {
846 tfm_name = make_subfont_name(kp, sfd_name, subfont_ids[n]);
847 if (!tfm_name)
848 continue;
849 if (dpx_conf.verbose_level > 3)
850 MESG(" %s", tfm_name);
851 mrec = NEW(1, fontmap_rec);
852 pdf_init_fontmap_record(mrec);
853 mrec->map_name = mstrdup(kp); /* link to this entry */
854 mrec->charmap.sfd_name = mstrdup(sfd_name);
855 mrec->charmap.subfont_id = mstrdup(subfont_ids[n]);
856 ht_insert_table(fontmap, tfm_name, strlen(tfm_name), mrec);
857 RELEASE(tfm_name);
858 }
859 RELEASE(fnt_name);
860 RELEASE(sfd_name);
861 }
862
863 mrec = NEW(1, fontmap_rec);
864 pdf_copy_fontmap_record(mrec, vp);
865 if (mrec->map_name && !strcmp(kp, mrec->map_name)) {
866 RELEASE(mrec->map_name);
867 mrec->map_name = NULL;
868 }
869 ht_insert_table(fontmap, kp, strlen(kp), mrec);
870
871 if (dpx_conf.verbose_level > 3)
872 MESG("\n");
873
874 return mrec;
875 }
876
877
878 int
879 pdf_read_fontmap_line (fontmap_rec *mrec, const char *mline, int mline_len, int format)
880 {
881 int error;
882 char *q;
883 const char *p, *endptr;
884
885 ASSERT(mrec);
886
887 p = mline;
888 endptr = p + mline_len;
889
890 skip_blank(&p, endptr);
891 if (p >= endptr)
892 return -1;
893
894 q = parse_string_value(&p, endptr);
895 if (!q)
896 return -1;
897
898 if (format > 0) /* DVIPDFM format */
899 error = fontmap_parse_mapdef_dpm(mrec, p, endptr);
900 else /* DVIPS/pdfTeX format */
901 error = fontmap_parse_mapdef_dps(mrec, p, endptr);
902 if (!error) {
903 char *fnt_name, *sfd_name = NULL;
904 fnt_name = chop_sfd_name(q, &sfd_name);
905 if (fnt_name && sfd_name) {
906 if (!mrec->font_name) {
907 /* In the case of subfonts, the base name (before the character '@')
908 * will be used as a font_name by default.
909 * Otherwise tex_name will be used as a font_name by default.
910 */
911 mrec->font_name = fnt_name;
912 } else {
913 RELEASE(fnt_name);
914 }
915 if (mrec->charmap.sfd_name)
916 RELEASE(mrec->charmap.sfd_name);
917 mrec->charmap.sfd_name = sfd_name ;
918 }
919 fill_in_defaults(mrec, q);
920 }
921 RELEASE(q);
922
923 return error;
924 }
925
926 /* DVIPS/pdfTeX fontmap line if one of the following three cases found:
927 *
928 * (1) any line including the character '"'
929 * (2) any line including the character '<'
930 * (3) if the line consists of two entries (tfmname and psname)
931 *
932 * DVIPDFM fontmap line otherwise.
933 */
934 int
935 is_pdfm_mapline (const char *mline) /* NULL terminated. */
936 {
937 int n = 0;
938 const char *p, *endptr;
939
940 if (strchr(mline, '"') || strchr(mline, '<'))
941 return -1; /* DVIPS/pdfTeX format */
942
943 p = mline;
944 endptr = p + strlen(mline);
945
946 skip_blank(&p, endptr);
947
948 while (p < endptr) {
949 /* Break if '-' preceeded by blanks is found. (DVIPDFM format) */
950 if (*p == '-') return 1;
951 for (n++; p < endptr && !ISBLANK(*p); p++);
952 skip_blank(&p, endptr);
953 }
954
955 /* Two entries: TFM_NAME PS_NAME only (DVIPS format)
956 * Otherwise (DVIPDFM format) */
957 return (n == 2 ? 0 : 1);
958 }
959
960 int
961 pdf_load_fontmap_file (const char *filename, int mode)
962 {
963 fontmap_rec *mrec;
964 FILE *fp;
965 const char *p = NULL, *endptr;
966 int llen, lpos = 0;
967 int error = 0, format = 0;
968
969 ASSERT(filename);
970 ASSERT(fontmap) ;
971
972 if (dpx_conf.verbose_level > 0)
973 MESG("<FONTMAP:");
974 fp = DPXFOPEN(filename, DPX_RES_TYPE_FONTMAP); /* outputs path if verbose */
975 if (!fp) {
976 WARN("Couldn't open font map file \"%s\".", filename);
977 return -1;
978 }
979
980 while (!error &&
981 (p = readline(work_buffer, WORK_BUFFER_SIZE, fp)) != NULL) {
982 int m;
983
984 lpos++;
985 llen = strlen(work_buffer);
986 endptr = p + llen;
987
988 skip_blank(&p, endptr);
989 if (p == endptr)
990 continue;
991
992 m = is_pdfm_mapline(p);
993
994 if (format * m < 0) { /* mismatch */
995 WARN("Found a mismatched fontmap line %d from %s.", lpos, filename);
996 WARN("-- Ignore the current input buffer: %s", p);
997 continue;
998 } else
999 format += m;
1000
1001 mrec = NEW(1, fontmap_rec);
1002 pdf_init_fontmap_record(mrec);
1003
1004 /* format > 0: DVIPDFM, format <= 0: DVIPS/pdfTeX */
1005 error = pdf_read_fontmap_line(mrec, p, llen, format);
1006 if (error) {
1007 WARN("Invalid map record in fontmap line %d from %s.", lpos, filename);
1008 WARN("-- Ignore the current input buffer: %s", p);
1009 pdf_clear_fontmap_record(mrec);
1010 RELEASE(mrec);
1011 continue;
1012 } else {
1013 switch (mode) {
1014 case FONTMAP_RMODE_REPLACE:
1015 pdf_insert_fontmap_record(mrec->map_name, mrec);
1016 break;
1017 case FONTMAP_RMODE_APPEND:
1018 pdf_append_fontmap_record(mrec->map_name, mrec);
1019 break;
1020 case FONTMAP_RMODE_REMOVE:
1021 pdf_remove_fontmap_record(mrec->map_name);
1022 break;
1023 }
1024 }
1025 pdf_clear_fontmap_record(mrec);
1026 RELEASE(mrec);
1027 }
1028 DPXFCLOSE(fp);
1029
1030 if (dpx_conf.verbose_level > 0)
1031 MESG(">");
1032
1033 return error;
1034 }
1035
1036 fontmap_rec *
1037 pdf_insert_native_fontmap_record (const char *path, uint32_t index,
1038 int layout_dir, int extend, int slant, int embolden)
1039 {
1040 char *fontmap_key;
1041 fontmap_rec *mrec;
1042 fontmap_rec *ret;
1043
1044 ASSERT(path);
1045
1046 fontmap_key = malloc(strlen(path) + 40); // CHECK
1047 sprintf(fontmap_key, "%s/%d/%c/%d/%d/%d", path, index, layout_dir == 0 ? 'H' : 'V', extend, slant, embolden);
1048
1049 if (dpx_conf.verbose_level > 0)
1050 MESG("<NATIVE-FONTMAP:%s", fontmap_key);
1051
1052 mrec = NEW(1, fontmap_rec);
1053 pdf_init_fontmap_record(mrec);
1054
1055 mrec->map_name = fontmap_key;
1056 mrec->enc_name = mstrdup(layout_dir == 0 ? "Identity-H" : "Identity-V");
1057 mrec->font_name = mstrdup(path);
1058 mrec->opt.index = index;
1059 if (layout_dir != 0)
1060 mrec->opt.flags |= FONTMAP_OPT_VERT;
1061
1062 fill_in_defaults(mrec, fontmap_key);
1063
1064 mrec->opt.extend = extend / 65536.0;
1065 mrec->opt.slant = slant / 65536.0;
1066 mrec->opt.bold = embolden / 65536.0;
1067 mrec->opt.use_glyph_encoding = 1;
1068
1069 ret = pdf_insert_fontmap_record(mrec->map_name, mrec);
1070 pdf_clear_fontmap_record(mrec);
1071 RELEASE(mrec);
1072
1073 if (dpx_conf.verbose_level > 0)
1074 MESG(">");
1075
1076 return ret;
1077 }
1078
1079 #if 0
1080 /* tfm_name="dmjhira10", map_name="dmj@DNP@10", sfd_name="DNP"
1081 * --> sub_id="hira"
1082 * Test if tfm_name can be really considered as subfont.
1083 */
1084 static int
1085 test_subfont (const char *tfm_name, const char *map_name, const char *sfd_name)
1086 {
1087 int r = 0;
1088 char **ids;
1089 int n, m;
1090 char *p = (char *) map_name;
1091 char *q = (char *) tfm_name;
1092
1093 ASSERT( tfm_name && map_name && sfd_name );
1094
1095 /* until first occurence of '@' */
1096 for ( ; *p && *q && *p == *q && *p != '@'; p++, q++);
1097 if (*p != '@')
1098 return 0;
1099 p++;
1100 /* compare sfd_name (should be always true here) */
1101 if (strlen(p) <= strlen(sfd_name) ||
1102 memcmp(p, sfd_name, strlen(sfd_name)) ||
1103 p[strlen(sfd_name)] != '@')
1104 return 0;
1105 /* check tfm_name follows second '@' */
1106 p += strlen(sfd_name) + 1;
1107 if (*p) {
1108 char *r = (char *) tfm_name;
1109 r += strlen(tfm_name) - strlen(p);
1110 if (strcmp(r, p))
1111 return 0;
1112 }
1113 /* Now 'p' is located at next to SFD name terminator
1114 * (second '@') in map_name and 'q' is at first char
1115 * of subfont_id substring in tfm_name.
1116 */
1117 n = strlen(q) - strlen(p); /* length of subfont_id string */
1118 if (n <= 0)
1119 return 0;
1120 /* check if n-length substring 'q' is valid as subfont ID */
1121 ids = sfd_get_subfont_ids(sfd_name, &m);
1122 if (!ids)
1123 return 0;
1124 while (!r && m-- > 0) {
1125 if (strlen(ids[m]) == n &&
1126 !memcmp(q, ids[m], n)) {
1127 r = 1;
1128 }
1129 }
1130
1131 return r;
1132 }
1133 #endif /* 0 */
1134
1135
1136 fontmap_rec *
1137 pdf_lookup_fontmap_record (const char *tfm_name)
1138 {
1139 fontmap_rec *mrec = NULL;
1140
1141 if (fontmap && tfm_name)
1142 mrec = ht_lookup_table(fontmap, tfm_name, strlen(tfm_name));
1143
1144 return mrec;
1145 }
1146
1147
1148 void
1149 pdf_init_fontmaps (void)
1150 {
1151 fontmap = NEW(1, struct ht_table);
1152 ht_init_table(fontmap, hval_free);
1153 }
1154
1155 void
1156 pdf_close_fontmaps (void)
1157 {
1158 if (fontmap) {
1159 ht_clear_table(fontmap);
1160 RELEASE(fontmap);
1161 }
1162 fontmap = NULL;
1163
1164 release_sfd_record();
1165 }
1166
1167 #if 0
1168 void
1169 pdf_clear_fontmaps (void)
1170 {
1171 pdf_close_fontmaps();
1172 pdf_init_fontmaps();
1173 }
1174 #endif
1175
1176 /* CIDFont options
1177 *
1178 * FORMAT:
1179 *
1180 * (:int:)?!?string(/string)?(,string)?
1181 */
1182
1183 static char *
1184 substr (const char **str, char stop)
1185 {
1186 char *sstr;
1187 const char *endptr;
1188
1189 endptr = strchr(*str, stop);
1190 if (!endptr || endptr == *str)
1191 return NULL;
1192 sstr = NEW(endptr-(*str)+1, char);
1193 memcpy(sstr, *str, endptr-(*str));
1194 sstr[endptr-(*str)] = '\0';
1195
1196 *str = endptr+1;
1197 return sstr;
1198 }
1199
1200 #include <ctype.h>
1201 #define CID_MAPREC_CSI_DELIM '/'
1202
1203 static char *
1204 strip_options (const char *map_name, fontmap_opt *opt)
1205 {
1206 char *font_name;
1207 const char *p;
1208 char *next = NULL;
1209 int have_csi = 0, have_style = 0;
1210
1211 ASSERT(opt);
1212
1213 p = map_name;
1214 font_name = NULL;
1215 opt->charcoll = NULL;
1216 opt->index = 0;
1217 opt->style = FONTMAP_STYLE_NONE;
1218 opt->flags = 0;
1219
1220 if (*p == ':' && isdigit((unsigned char)*(p+1))) {
1221 opt->index = strtoul(p+1, &next, 10);
1222 if (*next == ':')
1223 p = next + 1;
1224 else {
1225 opt->index = 0;
1226 }
1227 }
1228 if (*p == '!') { /* no-embedding */
1229 if (*(++p) == '\0')
1230 ERROR("Invalid map record: %s (--> %s)", map_name, p);
1231 opt->flags |= FONTMAP_OPT_NOEMBED;
1232 }
1233
1234 if ((next = strchr(p, CID_MAPREC_CSI_DELIM)) != NULL) {
1235 if (next == p)
1236 ERROR("Invalid map record: %s (--> %s)", map_name, p);
1237 font_name = substr(&p, CID_MAPREC_CSI_DELIM);
1238 have_csi = 1;
1239 } else if ((next = strchr(p, ',')) != NULL) {
1240 if (next == p)
1241 ERROR("Invalid map record: %s (--> %s)", map_name, p);
1242 font_name = substr(&p, ',');
1243 have_style = 1;
1244 } else {
1245 font_name = NEW(strlen(p)+1, char);
1246 strcpy(font_name, p);
1247 }
1248
1249 if (have_csi) {
1250 if ((next = strchr(p, ',')) != NULL) {
1251 opt->charcoll = substr(&p, ',');
1252 have_style = 1;
1253 } else if (p[0] == '\0') {
1254 ERROR("Invalid map record: %s.", map_name);
1255 } else {
1256 opt->charcoll = NEW(strlen(p)+1, char);
1257 strcpy(opt->charcoll, p);
1258 }
1259 }
1260
1261 if (have_style) {
1262 if (!strncmp(p, "BoldItalic", 10)) {
1263 if (*(p+10))
1264 ERROR("Invalid map record: %s (--> %s)", map_name, p);
1265 opt->style = FONTMAP_STYLE_BOLDITALIC;
1266 } else if (!strncmp(p, "Bold", 4)) {
1267 if (*(p+4))
1268 ERROR("Invalid map record: %s (--> %s)", map_name, p);
1269 opt->style = FONTMAP_STYLE_BOLD;
1270 } else if (!strncmp(p, "Italic", 6)) {
1271 if (*(p+6))
1272 ERROR("Invalid map record: %s (--> %s)", map_name, p);
1273 opt->style = FONTMAP_STYLE_ITALIC;
1274 }
1275 }
1276
1277 return font_name;
1278 }
1279
1280 #if DPXTEST
1281 static void
1282 dump_fontmap_rec (const char *key, const fontmap_rec *mrec)
1283 {
1284 fontmap_opt *opt = (fontmap_opt *) &mrec->opt;
1285
1286 if (mrec->map_name)
1287 fprintf(stdout, " <!-- subfont");
1288 else
1289 fprintf(stdout, " <insert");
1290 fprintf(stdout, " id=\"%s\"", key);
1291 if (mrec->map_name)
1292 fprintf(stdout, " map-name=\"%s\"", mrec->map_name);
1293 if (mrec->enc_name)
1294 fprintf(stdout, " enc-name=\"%s\"", mrec->enc_name);
1295 if (mrec->font_name)
1296 fprintf(stdout, " font-name=\"%s\"", mrec->font_name);
1297 if (mrec->charmap.sfd_name && mrec->charmap.subfont_id) {
1298 fprintf(stdout, " charmap=\"sfd:%s,%s\"",
1299 mrec->charmap.sfd_name, mrec->charmap.subfont_id);
1300 }
1301 if (opt->slant != 0.0)
1302 fprintf(stdout, " font-slant=\"%g\"", opt->slant);
1303 if (opt->extend != 1.0)
1304 fprintf(stdout, " font-extend=\"%g\"", opt->extend);
1305 if (opt->charcoll)
1306 fprintf(stdout, " glyph-order=\"%s\"", opt->charcoll);
1307 if (opt->tounicode)
1308 fprintf(stdout, " tounicode=\"%s\"", opt->tounicode);
1309 if (opt->index != 0)
1310 fprintf(stdout, " ttc-index=\"%d\"", opt->index);
1311 if (opt->flags & FONTMAP_OPT_NOEMBED)
1312 fprintf(stdout, " embedding=\"no\"");
1313 if (opt->mapc >= 0) {
1314 fprintf(stdout, " charmap=\"pad:");
1315 if (opt->mapc > 0xffff)
1316 fprintf(stdout, "%02x %02x", (opt->mapc >> 16) & 0xff, (opt->mapc >> 8) & 0xff);
1317 else
1318 fprintf(stdout, "%02x", (opt->mapc >> 8) & 0xff);
1319 fprintf(stdout, "\"");
1320 }
1321 if (opt->flags & FONTMAP_OPT_VERT)
1322 fprintf(stdout, " writing-mode=\"vertical\"");
1323 if (opt->style != FONTMAP_STYLE_NONE) {
1324 fprintf(stdout, " font-style=\"");
1325 switch (opt->style) {
1326 case FONTMAP_STYLE_BOLD:
1327 fprintf(stdout, "bold");
1328 break;
1329 case FONTMAP_STYLE_ITALIC:
1330 fprintf(stdout, "italic");
1331 break;
1332 case FONTMAP_STYLE_BOLDITALIC:
1333 fprintf(stdout, "bolditalic");
1334 break;
1335 }
1336 fprintf(stdout, "\"");
1337 }
1338 if (mrec->map_name)
1339 fprintf(stdout, " / -->\n");
1340 else
1341 fprintf(stdout, " />\n");
1342 }
1343
1344 void
1345 dump_fontmaps (void)
1346 {
1347 struct ht_iter iter;
1348 fontmap_rec *mrec;
1349 char key[128], *kp;
1350 int kl;
1351
1352 if (!fontmap)
1353 return;
1354
1355 fprintf(stdout, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1356 fprintf(stdout, "<!DOCTYPE fontmap SYSTEM \"fontmap.dtd\">\n");
1357 fprintf(stdout, "<fontmap id=\"%s\">\n", "foo");
1358 if (ht_set_iter(fontmap, &iter) == 0) {
1359 do {
1360 kp = ht_iter_getkey(&iter, &kl);
1361 mrec = ht_iter_getval(&iter);
1362 if (kl > 127)
1363 continue;
1364 memcpy(key, kp, kl); key[kl] = 0;
1365 dump_fontmap_rec(key, mrec);
1366 } while (!ht_iter_next(&iter));
1367 }
1368 ht_clear_iter(&iter);
1369 fprintf(stdout, "</fontmap>\n");
1370
1371 return;
1372 }
1373
1374 void
1375 test_fontmap_help (void)
1376 {
1377 fprintf(stdout, "usage: fontmap [options] [mapfile...]\n");
1378 fprintf(stdout, "-l, --lookup string\n");
1379 fprintf(stdout, " Lookup fontmap entry for 'string' after loading mapfile(s).\n");
1380 }
1381
1382 int
1383 test_fontmap_main (int argc, char *argv[])
1384 {
1385 int i;
1386 char *key = NULL;
1387
1388 for (;;) {
1389 int c, optidx = 0;
1390 static struct option long_options[] = {
1391 {"lookup", 1, 0, 'l'},
1392 {"help", 0, 0, 'h'},
1393 {0, 0, 0, 0}
1394 };
1395 c = getopt_long(argc, argv, "l:h", long_options, &optidx);
1396 if (c == -1)
1397 break;
1398
1399 switch (c) {
1400 case 'l':
1401 key = optarg;
1402 break;
1403 case 'h':
1404 test_fontmap_help();
1405 return 0;
1406 break;
1407 default:
1408 test_fontmap_help();
1409 return -1;
1410 break;
1411 }
1412 }
1413
1414 pdf_init_fontmaps();
1415 for (i = optind; i < argc; i++)
1416 pdf_load_fontmap_file(argv[i], FONTMAP_RMODE_REPLACE);
1417
1418 if (key == NULL)
1419 dump_fontmaps();
1420 else {
1421 fontmap_rec *mrec;
1422 mrec = pdf_lookup_fontmap_record(key);
1423 if (mrec)
1424 dump_fontmap_rec(key, mrec);
1425 else {
1426 WARN("Fontmap entry \"%s\" not found.", key);
1427 }
1428 }
1429 pdf_close_fontmaps();
1430
1431 return 0;
1432 }
1433 #endif /* DPXTEST */