Debian Codegraph @

texk/web2c/pdftexdir/mapfile.c

raw
1 /* mapfile.c: handling of map files/lines
2
3 Copyright 1996-2023 Han The Thanh, <[email protected]>
4
5 This file is part of pdfTeX.
6
7 pdfTeX is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 pdfTeX is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "ptexlib.h"
22 #include <kpathsea/c-auto.h>
23 #include <kpathsea/c-memstr.h>
24 #include <math.h>
25 #include <string.h>
26
27 #define FM_BUF_SIZE 1024
28
29 static FILE *fm_file;
30
31 #define fm_open() \
32 open_input (&fm_file, kpse_fontmap_format, FOPEN_RBIN_MODE)
33 #define fm_close() xfclose(fm_file, cur_file_name)
34 #define fm_getchar() xgetc(fm_file)
35 #define fm_eof() feof(fm_file)
36
37 typedef enum { FM_DUPIGNORE, FM_REPLACE, FM_DELETE } updatemode;
38 typedef enum { MAPFILE, MAPLINE } maptype;
39
40 typedef struct mitem {
41 updatemode mode; /* FM_DUPIGNORE or FM_REPLACE or FM_DELETE */
42 maptype type; /* map file or map line */
43 char *line; /* pointer to map file name or map line */
44 int lineno; /* line number in map file */
45 } mapitem;
46 mapitem *mitem = NULL;
47
48 static const char nontfm[] = "<nontfm>";
49
50 #define read_field(r, q, buf, buf_size) do { \
51 q = buf; \
52 while (*r != ' ' && *r != '<' && *r != '"' && *r != '\0') { \
53 check_buf(q - (buf) + 1, (buf_size)); \
54 *q++ = *r++; \
55 } \
56 *q = '\0'; \
57 skip (r, ' '); \
58 } while (0)
59
60 #define set_field(F) do { \
61 if (q > buf) \
62 fm->F = xstrdup(buf); \
63 if (*r == '\0') \
64 goto done; \
65 } while (0)
66
67 fm_entry *new_fm_entry(void)
68 {
69 fm_entry *fm;
70 fm = xtalloc(1, fm_entry);
71 fm->tfm_name = NULL;
72 fm->sfd_name = NULL;
73 fm->ps_name = NULL;
74 fm->fd_flags = FD_FLAGS_NOT_SET_IN_MAPLINE;
75 fm->ff_name = NULL;
76 fm->encname = NULL;
77 fm->type = 0;
78 fm->slant = 0;
79 fm->extend = 0;
80 fm->links = 0;
81 fm->pid = -1;
82 fm->eid = -1;
83 fm->subfont = NULL;
84 fm->in_use = false;
85 return fm;
86 }
87
88 void delete_fm_entry(fm_entry * fm)
89 {
90 xfree(fm->tfm_name);
91 xfree(fm->sfd_name);
92 xfree(fm->ps_name);
93 xfree(fm->ff_name);
94 xfree(fm);
95 }
96
97 static ff_entry *new_ff_entry(void)
98 {
99 ff_entry *ff;
100 ff = xtalloc(1, ff_entry);
101 ff->ff_name = NULL;
102 ff->ff_path = NULL;
103 return ff;
104 }
105
106 static void delete_ff_entry(ff_entry * ff)
107 {
108 xfree(ff->ff_name);
109 xfree(ff->ff_path);
110 xfree(ff);
111 }
112
113 static fm_entry *dummy_fm_entry(void)
114 {
115 static fm_entry const_fm_entry;
116 return & const_fm_entry;
117 }
118
119 /**********************************************************************/
120
121 struct avl_table *tfm_tree = NULL;
122 struct avl_table *ps_tree = NULL;
123 struct avl_table *ff_tree = NULL;
124 struct avl_table *encname_tree = NULL;
125
126 /* AVL sort fm_entry into tfm_tree by tfm_name */
127
128 static int comp_fm_entry_tfm(const void *pa, const void *pb, void *p)
129 {
130 return strcmp(((const fm_entry *) pa)->tfm_name,
131 ((const fm_entry *) pb)->tfm_name);
132 }
133
134 /* AVL sort fm_entry into ps_tree by ps_name, slant, and extend */
135
136 static int comp_fm_entry_ps(const void *pa, const void *pb, void *p)
137 {
138 int i;
139 const fm_entry *p1 = (const fm_entry *) pa, *p2 = (const fm_entry *) pb;
140 assert(p1->ps_name != NULL && p2->ps_name != NULL);
141 if ((i = strcmp(p1->ps_name, p2->ps_name)) != 0)
142 return i;
143 cmp_return(p1->slant, p2->slant);
144 cmp_return(p1->extend, p2->extend);
145 return 0;
146 }
147
148 /* AVL sort ff_entry into ff_tree by ff_name */
149
150 static int comp_ff_entry(const void *pa, const void *pb, void *p)
151 {
152 return strcmp(((const ff_entry *) pa)->ff_name,
153 ((const ff_entry *) pb)->ff_name);
154 }
155
156 static void create_avl_trees(void)
157 {
158 assert(tfm_tree == NULL);
159 tfm_tree = avl_create(comp_fm_entry_tfm, NULL, &avl_xallocator);
160 assert(tfm_tree != NULL);
161 assert(ps_tree == NULL);
162 ps_tree = avl_create(comp_fm_entry_ps, NULL, &avl_xallocator);
163 assert(ps_tree != NULL);
164 assert(ff_tree == NULL);
165 ff_tree = avl_create(comp_ff_entry, NULL, &avl_xallocator);
166 assert(ff_tree != NULL);
167 assert(encname_tree == NULL);
168 encname_tree = avl_create(comp_string_entry, NULL, &avl_xallocator);
169 assert(encname_tree != NULL);
170 }
171
172 /**********************************************************************/
173 /*
174 The function avl_do_entry() is not completely symmetrical with regards
175 to tfm_name and ps_name handling, e. g. a duplicate tfm_name gives a
176 "goto exit", and no ps_name link is tried. This is to keep it compatible
177 with the original version.
178 */
179
180 int avl_do_entry(fm_entry * fm, int mode)
181 {
182 fm_entry *p;
183 void *a;
184 void **aa;
185 boolean suppress_warn = (getpdfsuppresswarningdupmap() > 0);
186
187 /* handle tfm_name link */
188
189 if (strcmp(fm->tfm_name, nontfm) != 0) {
190 p = (fm_entry *) avl_find(tfm_tree, fm);
191 if (p != NULL) {
192 switch (mode) {
193 case FM_DUPIGNORE:
194 if (!suppress_warn)
195 pdftex_warn
196 ("fontmap entry for `%s' already exists, duplicates ignored",
197 fm->tfm_name);
198 goto exit;
199 break;
200 case FM_REPLACE:
201 case FM_DELETE:
202 if (p->in_use) {
203 pdftex_warn
204 ("fontmap entry for `%s' has been used, replace/delete not allowed",
205 fm->tfm_name);
206 goto exit;
207 }
208 a = avl_delete(tfm_tree, p);
209 assert(a != NULL);
210 unset_tfmlink(p);
211 if (!has_pslink(p))
212 delete_fm_entry(p);
213 break;
214 default:
215 assert(0);
216 }
217 }
218 if (mode != FM_DELETE) {
219 aa = avl_probe(tfm_tree, fm);
220 assert(aa != NULL);
221 set_tfmlink(fm);
222 }
223 }
224
225 /* handle ps_name link */
226
227 if (fm->ps_name != NULL) {
228 p = (fm_entry *) avl_find(ps_tree, fm);
229 if (p != NULL) {
230 switch (mode) {
231 case FM_DUPIGNORE:
232 goto exit;
233 break;
234 case FM_REPLACE:
235 case FM_DELETE:
236 if (p->in_use)
237 goto exit;
238 a = avl_delete(ps_tree, p);
239 assert(a != NULL);
240 unset_pslink(p);
241 if (!has_tfmlink(p))
242 delete_fm_entry(p);
243 break;
244 default:
245 assert(0);
246 }
247 }
248 if (mode != FM_DELETE && is_t1fontfile(fm) && is_included(fm)) {
249 aa = avl_probe(ps_tree, fm);
250 assert(aa != NULL);
251 set_pslink(fm);
252 }
253 }
254 exit:
255 if (!has_tfmlink(fm) && !has_pslink(fm)) /* e. g. after FM_DELETE */
256 return 1; /* deallocation of fm_entry structure required */
257 else
258 return 0;
259 }
260
261 /* add the encoding name to an AVL tree. this has nothing to do with writeenc.c */
262
263 static char *add_encname(char *s)
264 {
265 char *p;
266 void **aa;
267 assert(s != NULL);
268 assert(encname_tree != NULL);
269 if ((p = (char *) avl_find(encname_tree, s)) == NULL) { /* encoding name not yet registered */
270 p = xstrdup(s);
271 aa = avl_probe(encname_tree, p);
272 assert(aa != NULL);
273 }
274 return p;
275 }
276
277
278 /**********************************************************************/
279 /* consistency check for map entry, with warn flag; return zero if ok,
280 nonzero bits of warnings otherwise. */
281
282 static int check_fm_entry(fm_entry * fm, boolean warn)
283 {
284 int a = 0;
285 assert(fm != NULL);
286 assert(fm->tfm_name);
287
288 if (is_fontfile(fm) && !is_included(fm)) {
289 if (warn)
290 pdftex_warn
291 ("ambiguous entry for `%s': font file present but not included, "
292 "will be treated as font file not present", fm->tfm_name);
293 xfree(fm->ff_name);
294 /* do not set variable |a| as this entry will still be accepted */
295 }
296
297 /* if no tfm name, nothing to do here; a bare tfm is ok */
298 if (*(fm->tfm_name) == '\0') {
299 if (warn)
300 pdftex_warn ("invalid map entry: tfm missing");
301 a += 1;
302 }
303
304 /* TrueType fonts cannot be reencoded without subsetting */
305 if (is_truetype(fm) && is_reencoded(fm) && !is_subsetted(fm)) {
306 if (warn)
307 pdftex_warn
308 ("invalid entry for `%s': only subsetted TrueType fonts can be reencoded",
309 fm->tfm_name);
310 a += 2;
311 }
312
313 /* SlantFont and ExtendFont can be used only with Type1 fonts */
314 if ((fm->slant != 0 || fm->extend != 0)
315 && (strlen(fm->tfm_name) == 0
316 || !(is_t1fontfile(fm) && is_included(fm)))) {
317 if (warn)
318 pdftex_warn
319 ("invalid entry for `%s': SlantFont/ExtendFont can be used only with embedded Type1 fonts",
320 fm->tfm_name);
321 a += 4;
322 }
323
324 /* the value of SlantFont and ExtendFont must be reasonable */
325 if (abs(fm->slant) > 1000) {
326 if (warn)
327 pdftex_warn
328 ("invalid entry for `%s': SlantFont value too big: %g",
329 fm->tfm_name, fm->slant / 1000.0);
330 a += 8;
331 }
332 if (abs(fm->extend) > 2000) {
333 if (warn)
334 pdftex_warn
335 ("invalid entry for `%s': ExtendFont value too big: %g",
336 fm->tfm_name, fm->extend / 1000.0);
337 a += 16;
338 }
339
340 /* subfonts must be used with subsetted non-reencoded TrueType fonts */
341 if (fm->pid != -1 &&
342 !(is_truetype(fm) && is_subsetted(fm) && !is_reencoded(fm))) {
343 if (warn)
344 pdftex_warn
345 ("invalid entry for `%s': PidEid can be used only with subsetted non-reencoded TrueType fonts",
346 fm->tfm_name);
347 a += 32;
348 }
349
350 /* font file for bitmap PK fonts is determinated by kpse and depends
351 on current font size; writet3.c ignores font file for bitmap PK fonts */
352 if (is_fontfile(fm) && is_pk(fm)) {
353 if (warn)
354 pdftex_warn
355 ("invalid entry for `%s': "
356 "FontFile cannot be specified for bitmap PK font: %s",
357 fm->tfm_name, fm_fontfile(fm));
358 a += 64;
359 }
360
361 /* ps name cannot be stored into PDF file for PDF Type3 fonts */
362 if (fm->ps_name != NULL && is_pk(fm)) {
363 if (warn)
364 pdftex_warn
365 ("invalid entry for `%s': "
366 "PsName cannot be specified for bitmap PK font: %s",
367 fm->tfm_name, fm->ps_name);
368 a += 128;
369 }
370
371 return a;
372 }
373
374 /**********************************************************************/
375 /* returns the font number if s is one of the 14 std. font names, -1 otherwise; speed-trimmed. */
376
377 int check_std_t1font(char *s)
378 {
379 static const char *std_t1font_names[] = {
380 "Courier", /* 0:7 */
381 "Courier-Bold", /* 1:12 */
382 "Courier-Oblique", /* 2:15 */
383 "Courier-BoldOblique", /* 3:19 */
384 "Helvetica", /* 4:9 */
385 "Helvetica-Bold", /* 5:14 */
386 "Helvetica-Oblique", /* 6:17 */
387 "Helvetica-BoldOblique", /* 7:21 */
388 "Symbol", /* 8:6 */
389 "Times-Roman", /* 9:11 */
390 "Times-Bold", /* 10:10 */
391 "Times-Italic", /* 11:12 */
392 "Times-BoldItalic", /* 12:16 */
393 "ZapfDingbats" /* 13:12 */
394 };
395 static const int index[] =
396 { -1, -1, -1, -1, -1, -1, 8, 0, -1, 4, 10, 9, -1, -1, 5, 2, 12, 6, -1,
397 3, -1, 7
398 };
399 size_t n;
400 int k = -1;
401 assert(s != NULL);
402 n = strlen(s);
403 if (n > 21)
404 return -1;
405 if (n == 12) { /* three names have length 12 */
406 switch (*s) {
407 case 'C':
408 k = 1; /* Courier-Bold */
409 break;
410 case 'T':
411 k = 11; /* Times-Italic */
412 break;
413 case 'Z':
414 k = 13; /* ZapfDingbats */
415 break;
416 default:
417 return -1;
418 }
419 } else
420 k = index[n];
421 if (k > -1 && strcmp(std_t1font_names[k], s) == 0)
422 return k;
423 return -1;
424 };
425
426 /**********************************************************************/
427
428 static void fm_scan_line(void)
429 {
430 int a, b, c, j, u = 0, v = 0;
431 float d;
432 fm_entry *fm;
433 char fm_line[FM_BUF_SIZE], buf[FM_BUF_SIZE];
434 char *p, *q, *r, *s;
435 switch (mitem->type) {
436 case MAPFILE:
437 p = fm_line;
438 do {
439 c = fm_getchar();
440 append_char_to_buf(c, p, fm_line, FM_BUF_SIZE);
441 }
442 while (c != 10);
443 *(--p) = '\0';
444 r = fm_line;
445 break;
446 case MAPLINE:
447 r = mitem->line; /* work on string from makecstring() */
448 break;
449 default:
450 assert(0);
451 }
452 if (*r == '\0' || is_cfg_comment(*r))
453 return;
454 fm = new_fm_entry();
455 read_field(r, q, buf, FM_BUF_SIZE);
456 set_field(tfm_name);
457 if (fm->tfm_name == NULL) /* used in messages, so don't let be null */
458 fm->tfm_name = xstrdup ("");
459 if (!isdigit((unsigned char)*r)) { /* 2nd field ps_name may not start with a digit */
460 read_field(r, q, buf, FM_BUF_SIZE);
461 set_field(ps_name);
462 }
463 if (isdigit((unsigned char)*r)) { /* font descriptor /Flags given? */
464 for (s = r; isdigit((unsigned char)*s); s++);
465 if (*s == ' ' || *s == '"' || *s == '<' || *s == '\0') { /* not e. g. 8r.enc */
466 fm->fd_flags = atoi(r);
467 while (isdigit((unsigned char)*r))
468 r++;
469 }
470 }
471 while (1) { /* loop through "specials", encoding, font file */
472 skip(r, ' ');
473 switch (*r) {
474 case '\0':
475 goto done;
476 case '"': /* opening quote */
477 r++;
478 u = v = 0;
479 do {
480 skip(r, ' ');
481 if (sscanf(r, "%f %n", &d, &j) > 0) {
482 s = r + j; /* jump behind number, eat also blanks, if any */
483 if (*(s - 1) == 'E' || *(s - 1) == 'e')
484 s--; /* e. g. 0.5ExtendFont: %f = 0.5E */
485 if (str_prefix(s, "SlantFont")) {
486 d *= 1000.0; /* correct rounding also for neg. numbers */
487 fm->slant = (integer) (d > 0 ? d + 0.5 : d - 0.5);
488 r = s + strlen("SlantFont");
489 } else if (str_prefix(s, "ExtendFont")) {
490 d *= 1000.0;
491 fm->extend = (integer) (d > 0 ? d + 0.5 : d - 0.5);
492 if (fm->extend == 1000)
493 fm->extend = 0;
494 r = s + strlen("ExtendFont");
495 } else { /* unknown name */
496 for (r = s; *r != ' ' && *r != '"' && *r != '\0'; r++); /* jump over name */
497 c = *r; /* remember char for temporary end of string */
498 *r = '\0';
499 pdftex_warn
500 ("invalid entry for `%s': unknown name `%s' ignored",
501 fm->tfm_name, s);
502 *r = c;
503 }
504 } else
505 for (; *r != ' ' && *r != '"' && *r != '\0'; r++);
506 }
507 while (*r == ' ');
508 if (*r == '"') /* closing quote */
509 r++;
510 else {
511 pdftex_warn
512 ("invalid entry for `%s': closing quote missing",
513 fm->tfm_name);
514 goto bad_line;
515 }
516 break;
517 case 'P': /* handle cases for subfonts like 'PidEid=3,1' */
518 if (sscanf(r, "PidEid=%i, %i %n", &a, &b, &c) >= 2) {
519 fm->pid = a;
520 fm->eid = b;
521 r += c;
522 break;
523 }
524 default: /* encoding or font file specification */
525 a = b = 0;
526 if (*r == '<') {
527 a = *r++;
528 if (*r == '<' || *r == '[')
529 b = *r++;
530 }
531 read_field(r, q, buf, FM_BUF_SIZE);
532 /* encoding, formats: '8r.enc' or '<8r.enc' or '<[8r.enc' */
533 if (strlen(buf) > 4 && strcasecmp(strend(buf) - 4, ".enc") == 0) {
534 fm->encname = add_encname(buf);
535 u = v = 0; /* u, v used if intervening blank: "<< foo" */
536 } else if (strlen(buf) > 0) { /* file name given */
537 /* font file, formats:
538 * subsetting: '<cmr10.pfa'
539 * no subsetting: '<<cmr10.pfa'
540 * no embedding: 'cmr10.pfa'
541 */
542 if (a == '<' || u == '<') {
543 set_included(fm);
544 if ((a == '<' && b == 0) || (a == 0 && v == 0))
545 set_subsetted(fm);
546 /* otherwise b == '<' (or '[') => no subsetting */
547 }
548 set_field(ff_name);
549 u = v = 0;
550 } else {
551 u = a;
552 v = b;
553 }
554 }
555 }
556 done:
557 if (fm->ps_name != NULL && (check_std_t1font(fm->ps_name) >= 0))
558 set_std_t1font(fm);
559 if (is_fontfile(fm) && strlen(fm_fontfile(fm)) > 3) {
560 if (strcasecmp(strend(fm_fontfile(fm)) - 4, ".ttf") == 0)
561 set_truetype(fm);
562 else if (strcasecmp(strend(fm_fontfile(fm)) - 4, ".ttc") == 0)
563 set_truetype(fm);
564 else if (strcasecmp(strend(fm_fontfile(fm)) - 4, ".otf") == 0)
565 set_opentype(fm);
566 else
567 set_type1(fm);
568 } else if (fm->ps_name == NULL) {
569 set_pk(fm); /* font without ps_name and without fontfile,
570 it can be only bitmap PK font */
571 } else
572 set_type1(fm); /* assume a builtin font is Type1 */
573
574 if (check_fm_entry(fm, true) != 0)
575 goto bad_line;
576 /*
577 If we get here, the map line has been completely scanned without errors;
578 fm points to a valid, freshly filled-out fm_entry structure.
579 Now follows the actual work of registering/deleting.
580 */
581 if (handle_subfont_fm(fm, mitem->mode)) /* is this a subfont? */
582 return;
583 if (avl_do_entry(fm, mitem->mode) == 0) /* if success */
584 return;
585 bad_line:
586 delete_fm_entry(fm);
587 }
588
589 /**********************************************************************/
590
591 void fm_read_info(void)
592 {
593 if (tfm_tree == NULL)
594 create_avl_trees();
595 if (mitem->line == NULL) /* nothing to do */
596 return;
597 mitem->lineno = 1;
598 switch (mitem->type) {
599 case MAPFILE:
600 set_cur_file_name(mitem->line);
601 if (!fm_open()) {
602 pdftex_warn("cannot open font map file");
603 } else {
604 cur_file_name = (char *) nameoffile + 1;
605 tex_printf("{%s", cur_file_name);
606 while (!fm_eof()) {
607 fm_scan_line();
608 mitem->lineno++;
609 }
610 fm_close();
611 tex_printf("}");
612 fm_file = NULL;
613 }
614 break;
615 case MAPLINE:
616 cur_file_name = NULL; /* makes pdftex_warn() shorter */
617 fm_scan_line();
618 break;
619 default:
620 assert(0);
621 }
622 mitem->line = NULL; /* done with this line */
623 cur_file_name = NULL;
624 return;
625 }
626
627 /**********************************************************************/
628
629 static fmentryptr fmlookup(internalfontnumber f)
630 {
631 char *tfm;
632 fm_entry *fm;
633 fm_entry tmp;
634 if (tfm_tree == NULL)
635 fm_read_info(); /* only to read default map file */
636 tfm = makecstring(fontname[f]);
637 assert(strcmp(tfm, nontfm) != 0);
638
639 /* Look up for full <tfmname>[+-]<expand> */
640 tmp.tfm_name = tfm;
641 fm = (fm_entry *) avl_find(tfm_tree, &tmp);
642 if (fm != NULL) {
643 fm->in_use = true;
644 return (fmentryptr) fm;
645 }
646 return (fmentryptr) dummy_fm_entry();
647 }
648
649 boolean hasfmentry(internalfontnumber f)
650 {
651 if (pdffontmap[f] == NULL)
652 pdffontmap[f] = fmlookup(f);
653 assert(pdffontmap[f] != NULL);
654 return pdffontmap[f] != (fmentryptr) dummy_fm_entry();
655 }
656
657 boolean isscalable(internalfontnumber f)
658 {
659 return hasfmentry(f) && (!is_pk((fm_entry *)pdffontmap[f]));
660 }
661
662 boolean hasspacechar(internalfontnumber f)
663 {
664 fm_entry *fm;
665 fe_entry *fe;
666 if (!isscalable(f))
667 return false;
668
669 fm = (fm_entry *)pdffontmap[f];
670
671 /* if a font is not re-encoded via its map entry, we assume it has no space char */
672 if (is_reencoded(fm) && (fe = get_fe_entry(fm->encname)) != NULL) {
673 char *s = fe->glyph_names[32];
674 assert(s != NULL);
675 if (strcmp(s, "space") == 0)
676 return true;
677 }
678 return false;
679 }
680
681 /* check whether a map entry is valid for font replacement */
682
683 static boolean fm_valid_for_font_replacement(fm_entry * fm)
684 {
685 ff_entry *ff;
686
687 assert(fm != NULL);
688 assert(is_fontfile(fm)); /* ps_tree should contain only entries with font file */
689 assert(is_type1(fm)); /* ps_tree should contain only Type1 entries */
690
691 ff = check_ff_exist(fm->ff_name, false);
692 assert(ff != NULL);
693 if (ff->ff_path == NULL) /* ...there is no font file available */
694 return false;
695 return true; /* all tests passed */
696 }
697
698 /**********************************************************************/
699 /*
700 * lookup fontmap by ps_name;
701 * used for Type1 font replacement when embedding of PDF files
702 */
703
704 fm_entry *lookup_fontmap(char *ps_name)
705 {
706 fm_entry *fm, *fm2, tmp;
707 char *a, *b, *c, *d, *e, *s;
708 int i, sl, ex;
709 struct avl_traverser t, t2;
710 if (tfm_tree == NULL)
711 fm_read_info(); /* only to read default map file */
712 assert(ps_name != NULL);
713 s = ps_name;
714 if (strlen(ps_name) > 7) { /* check for subsetted name tag */
715 for (i = 0; i < 6; i++, s++)
716 if (*s < 'A' || *s > 'Z')
717 break;
718 if (i == 6 && *s == '+')
719 s++; /* if name tag found, skip behind it */
720 else
721 s = ps_name;
722 }
723
724 /*
725 * Scan -Slant_<slant> and -Extend_<extend> font name extensions;
726 * three valid formats:
727 * <fontname>-Slant_<slant>
728 * <fontname>-Slant_<slant>-Extend_<extend>
729 * <fontname>-Extend_<extend>
730 * Slant entry must come _before_ Extend entry
731 */
732
733 tmp.slant = 0;
734 tmp.extend = 0;
735 if ((a = strstr(s, "-Slant_")) != NULL) {
736 b = a + strlen("-Slant_");
737 sl = (int) strtol(b, &e, 10);
738 if ((e != b) && (e == strend(b))) {
739 tmp.slant = sl;
740 *a = '\0'; /* ps_name string ends before "-Slant_" */
741 } else {
742 if (e != b) { /* only if <slant> is valid number */
743 if ((c = strstr(e, "-Extend_")) != NULL) {
744 d = c + strlen("-Extend_");
745 ex = (int) strtol(d, &e, 10);
746 if ((e != d) && (e == strend(d))) {
747 tmp.slant = sl;
748 tmp.extend = ex;
749 *a = '\0'; /* ps_name string ends before "-Slant_" */
750 }
751 }
752 }
753 }
754 } else {
755 if ((a = strstr(s, "-Extend_")) != NULL) {
756 b = a + strlen("-Extend_");
757 ex = (int) strtol(b, &e, 10);
758 if ((e != b) && (e == strend(b))) {
759 tmp.extend = ex;
760 *a = '\0'; /* ps_name string ends before "-Extend_" */
761 }
762 }
763 }
764 tmp.ps_name = s;
765
766 fm = (fm_entry *) avl_t_find(&t, ps_tree, &tmp);
767 if (fm == NULL)
768 return NULL; /* no entry found */
769
770 /* at this point we know there is at least one fm_entry with given ps_name;
771 * we test all such entries and return the first one that is valid for font
772 * replacement */
773
774 t2 = t;
775 fm2 = (fm_entry *) avl_t_prev(&t2);
776
777 /* search forward */
778 do {
779 if (fm_valid_for_font_replacement(fm))
780 return fm;
781 fm = (fm_entry *) avl_t_next(&t);
782 } while (fm != NULL && comp_fm_entry_ps(fm, &tmp, NULL) == 0);
783
784 /* search backward */
785 while (fm2 != NULL && comp_fm_entry_ps(fm2, &tmp, NULL) == 0) {
786 if (fm_valid_for_font_replacement(fm2))
787 return fm2;
788 fm2 = (fm_entry *) avl_t_prev(&t2);
789 }
790
791 return NULL;
792 }
793
794 /**********************************************************************/
795 /*
796 * Process map file given by its name or map line contents. Items not
797 * beginning with [+-=] flush default map file, if it has not yet been
798 * read. Leading blanks and blanks immediately following [+-=] are
799 * ignored.
800 */
801
802 static void process_map_item(char *s, int type)
803 {
804 char *p;
805 int mode;
806 if (*s == ' ')
807 s++; /* ignore leading blank */
808 switch (*s) {
809 case '+': /* +mapfile.map, +mapline */
810 mode = FM_DUPIGNORE; /* insert entry, if it is not duplicate */
811 s++;
812 break;
813 case '=': /* =mapfile.map, =mapline */
814 mode = FM_REPLACE; /* try to replace earlier entry */
815 s++;
816 break;
817 case '-': /* -mapfile.map, -mapline */
818 mode = FM_DELETE; /* try to delete entry */
819 s++;
820 break;
821 default:
822 mode = FM_DUPIGNORE; /* like +, but also: */
823 mitem->line = NULL; /* flush default map file name */
824 }
825 if (*s == ' ')
826 s++; /* ignore blank after [+-=] */
827 p = s; /* map item starts here */
828 switch (type) {
829 case MAPFILE: /* remove blank at end */
830 while (*p != '\0' && *p != ' ')
831 p++;
832 *p = '\0';
833 break;
834 case MAPLINE: /* blank at end allowed */
835 break;
836 default:
837 assert(0);
838 }
839 if (mitem->line != NULL) /* read default map file first */
840 fm_read_info();
841 if (*s != '\0') { /* only if real item to process */
842 mitem->mode = mode;
843 mitem->type = type;
844 mitem->line = s;
845 fm_read_info();
846 }
847 }
848
849 void pdfmapfile(integer t)
850 {
851 process_map_item(makecstring(tokenstostring(t)), MAPFILE);
852 flushstr(lasttokensstring);
853 }
854
855 void pdfmapline(integer t)
856 {
857 process_map_item(makecstring(tokenstostring(t)), MAPLINE);
858 flushstr(lasttokensstring);
859 }
860
861 void pdfmaplinesp(void)
862 {
863 process_map_item("=pdftexspace PdfTeX-Space <pdftexspace.pfb", MAPLINE);
864 }
865
866 void pdfinitmapfile(const_string map_name)
867 {
868 assert(mitem == NULL);
869 mitem = xtalloc(1, mapitem);
870 mitem->mode = FM_DUPIGNORE;
871 mitem->type = MAPFILE;
872 mitem->line = xstrdup(map_name);
873 }
874
875 /**********************************************************************/
876 /*
877 * Early check whether a font file exists. Search tree ff_tree is used
878 * in 1st instance, as it may be faster than the kpse_find_file(), and
879 * kpse_find_file() is called only once per font file name + expansion
880 * parameter. This might help keeping speed, if many PDF pages with
881 * same fonts are to be embedded.
882 *
883 * The ff_tree contains only font files, which are actually needed,
884 * so this tree typically is much smaller than the tfm_tree or ps_tree.
885 */
886
887 ff_entry *check_ff_exist(char *ff_name, boolean is_tt)
888 {
889 ff_entry *ff;
890 ff_entry tmp;
891 void **aa;
892
893 assert(ff_name != NULL);
894 tmp.ff_name = ff_name;
895 ff = (ff_entry *) avl_find(ff_tree, &tmp);
896 if (ff == NULL) { /* not yet in database */
897 ff = new_ff_entry();
898 ff->ff_name = xstrdup(ff_name);
899 if (is_tt)
900 ff->ff_path = kpse_find_file(ff_name, kpse_truetype_format, 0);
901 else
902 ff->ff_path = kpse_find_file(ff_name, kpse_type1_format, 0);
903 aa = avl_probe(ff_tree, ff);
904 assert(aa != NULL);
905 }
906 return ff;
907 }
908
909 /**********************************************************************/
910 /* cleaning up... */
911
912 static void destroy_fm_entry_tfm(void *pa, void *pb)
913 {
914 fm_entry *fm;
915 fm = (fm_entry *) pa;
916 if (!has_pslink(fm))
917 delete_fm_entry(fm);
918 else
919 unset_tfmlink(fm);
920 }
921
922 static void destroy_fm_entry_ps(void *pa, void *pb)
923 {
924 fm_entry *fm;
925 fm = (fm_entry *) pa;
926 if (!has_tfmlink(fm))
927 delete_fm_entry(fm);
928 else
929 unset_pslink(fm);
930 }
931
932 static void destroy_ff_entry(void *pa, void *pb)
933 {
934 ff_entry *ff;
935 ff = (ff_entry *) pa;
936 delete_ff_entry(ff);
937 }
938
939 void fm_free(void)
940 {
941 if (tfm_tree != NULL) {
942 avl_destroy(tfm_tree, destroy_fm_entry_tfm);
943 tfm_tree = NULL;
944 }
945 if (ps_tree != NULL) {
946 avl_destroy(ps_tree, destroy_fm_entry_ps);
947 ps_tree = NULL;
948 }
949 if (ff_tree != NULL) {
950 avl_destroy(ff_tree, destroy_ff_entry);
951 ff_tree = NULL;
952 }
953 }
954
955 /**********************************************************************/
956 /* end of mapfile.c */
957 // vim: ts=4