Debian Codegraph @

texk/web2c/pdftexdir/writefont.c

raw
1 /* writefont.c: font descriptors and writing Type 1 fonts.
2 Copyright 1996-2021 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 /**********************************************************************/
23
24 struct avl_table *fo_tree = NULL; /* tree of font dictionaries */
25 struct avl_table *fd_tree = NULL; /* tree of font descriptor objects */
26
27 static int comp_fo_entry(const void *pa, const void *pb, void *p)
28 {
29 return strcmp(((const fo_entry *) pa)->fm->tfm_name,
30 ((const fo_entry *) pb)->fm->tfm_name);
31 }
32
33 static int comp_fd_entry(const void *pa, const void *pb, void *p)
34 {
35 int i;
36 const fd_entry *p1 = (const fd_entry *) pa, *p2 = (const fd_entry *) pb;
37 assert(p1->fm != NULL && is_fontfile(p1->fm) &&
38 p2->fm != NULL && is_fontfile(p2->fm));
39 if ((i = strcmp(p1->fm->ff_name, p2->fm->ff_name)) != 0)
40 return i;
41 cmp_return(p1->fm->slant, p2->fm->slant);
42 cmp_return(p1->fm->extend, p2->fm->extend);
43 return 0;
44 }
45
46 /**********************************************************************/
47 /* initialize data structure for /Type /Font */
48
49 static fo_entry *new_fo_entry(void)
50 {
51 fo_entry *fo;
52 fo = xtalloc(1, fo_entry);
53 fo->fo_objnum = 0;
54 fo->tex_font = 0;
55 fo->fm = NULL;
56 fo->fd = NULL;
57 fo->fe = NULL;
58 fo->cw = NULL;
59 fo->first_char = 1;
60 fo->last_char = 0;
61 fo->tounicode_objnum = 0;
62 return fo;
63 }
64
65 /* initialize data structure for /Type /FontDescriptor */
66
67 fd_entry *new_fd_entry(void)
68 {
69 fd_entry *fd;
70 int i;
71 fd = xtalloc(1, fd_entry);
72 fd->fd_objnum = 0;
73 fd->fontname = NULL;
74 fd->subset_tag = NULL;
75 fd->ff_found = false;
76 fd->ff_objnum = 0;
77 fd->fn_objnum = 0;
78 fd->all_glyphs = false;
79 fd->write_ttf_glyph_names = false;
80 for (i = 0; i < FONT_KEYS_NUM; i++) {
81 fd->font_dim[i].val = 0;
82 fd->font_dim[i].set = false;
83 }
84 fd->fe = NULL;
85 fd->builtin_glyph_names = NULL;
86 fd->fm = NULL;
87 fd->tx_tree = NULL;
88 fd->gl_tree = NULL;
89 return fd;
90 }
91
92 /* initialize data structure for /Widths array object */
93
94 static cw_entry *new_cw_entry(void)
95 {
96 cw_entry *cw;
97 cw = xtalloc(1, cw_entry);
98 cw->cw_objnum = 0;
99 cw->width = NULL;
100 return cw;
101 }
102
103 /**********************************************************************/
104 /*
105 * Only fallback values of font metrics are taken from the TFM info
106 * of f by preset_fontmetrics(). During reading of the font file,
107 * these values are replaced by metrics from the font, if available.
108 */
109
110 static void preset_fontmetrics(fd_entry * fd, internalfontnumber f)
111 {
112 int i;
113 fd->font_dim[ITALIC_ANGLE_CODE].val =
114 dividescaled(-atan(getslant(f) / 65536.0) * (180 / M_PI),
115 pdffontsize[f], 3);
116 fd->font_dim[ASCENT_CODE].val =
117 dividescaled(getcharheight(f, 'h'), pdffontsize[f], 3);
118 fd->font_dim[CAPHEIGHT_CODE].val =
119 dividescaled(getcharheight(f, 'H'), pdffontsize[f], 3);
120 i = -dividescaled(getchardepth(f, 'y'), pdffontsize[f], 3);
121 fd->font_dim[DESCENT_CODE].val = i < 0 ? i : 0;
122 fd->font_dim[STEMV_CODE].val =
123 dividescaled(getcharwidth(f, '.') / 3, pdffontsize[f], 3);
124 fd->font_dim[XHEIGHT_CODE].val =
125 dividescaled(getxheight(f), pdffontsize[f], 3);
126 fd->font_dim[FONTBBOX1_CODE].val = 0;
127 fd->font_dim[FONTBBOX2_CODE].val = fd->font_dim[DESCENT_CODE].val;
128 fd->font_dim[FONTBBOX3_CODE].val =
129 dividescaled(getquad(f), pdffontsize[f], 3);
130 fd->font_dim[FONTBBOX4_CODE].val =
131 fd->font_dim[CAPHEIGHT_CODE].val > fd->font_dim[ASCENT_CODE].val ?
132 fd->font_dim[CAPHEIGHT_CODE].val : fd->font_dim[ASCENT_CODE].val;
133 for (i = 0; i < INT_KEYS_NUM; i++)
134 fd->font_dim[i].set = true;
135 }
136
137 static void fix_fontmetrics(fd_entry * fd)
138 {
139 intparm *p = (intparm *) fd->font_dim;
140 if (!p[FONTBBOX1_CODE].set || !p[FONTBBOX2_CODE].set ||
141 !p[FONTBBOX3_CODE].set || !p[FONTBBOX4_CODE].set) {
142 pdftex_warn("font `%s' doesn't have a BoundingBox", fd->fm->ff_name);
143 return;
144 }
145 if (!p[ASCENT_CODE].set) {
146 p[ASCENT_CODE].val = p[FONTBBOX4_CODE].val;
147 p[ASCENT_CODE].set = true;
148 }
149 if (!p[DESCENT_CODE].set) {
150 p[DESCENT_CODE].val = p[FONTBBOX2_CODE].val;
151 p[DESCENT_CODE].set = true;
152 }
153 if (!p[CAPHEIGHT_CODE].set) {
154 p[CAPHEIGHT_CODE].val = p[FONTBBOX4_CODE].val;
155 p[CAPHEIGHT_CODE].set = true;
156 }
157 }
158
159 static void write_fontmetrics(fd_entry * fd)
160 {
161 int i;
162 fix_fontmetrics(fd);
163 if (fd->font_dim[FONTBBOX1_CODE].set && fd->font_dim[FONTBBOX2_CODE].set
164 && fd->font_dim[FONTBBOX3_CODE].set && fd->font_dim[FONTBBOX4_CODE].set)
165 pdf_printf("/%s [%i %i %i %i]\n", font_key[FONTBBOX1_CODE].pdfname,
166 (int) fd->font_dim[FONTBBOX1_CODE].val,
167 (int) fd->font_dim[FONTBBOX2_CODE].val,
168 (int) fd->font_dim[FONTBBOX3_CODE].val,
169 (int) fd->font_dim[FONTBBOX4_CODE].val);
170 for (i = 0; i < GEN_KEY_NUM; i++)
171 if (fd->font_dim[i].set)
172 pdf_printf("/%s %i\n", font_key[i].pdfname, fd->font_dim[i].val);
173 }
174
175 /**********************************************************************/
176
177 static void preset_fontname(fo_entry * fo)
178 {
179 if (fo->fm->ps_name != NULL)
180 fo->fd->fontname = xstrdup(fo->fm->ps_name); /* just fallback */
181 else
182 fo->fd->fontname = xstrdup(fo->fm->tfm_name);
183 }
184
185 static void write_fontname(fd_entry * fd, const char *key)
186 {
187 assert(fd->fontname != NULL);
188 pdf_puts("/");
189 if (key != NULL)
190 pdf_printf("%s /", key);
191 if (fd->subset_tag != NULL)
192 pdf_printf("%s+", fd->subset_tag);
193 pdf_printf("%s\n", fd->fontname);
194 }
195
196 static void write_fontname_object(fd_entry * fd)
197 {
198 assert(fd->fn_objnum != 0);
199 pdfbeginobj(fd->fn_objnum, 1);
200 write_fontname(fd, NULL);
201 pdfendobj();
202 }
203
204 /**********************************************************************/
205
206 fd_entry *lookup_fd_entry(char *s, integer slant, integer extend)
207 {
208 fd_entry fd;
209 fm_entry fm;
210 assert(s != NULL);
211 fm.ff_name = s;
212 fm.slant = slant;
213 fm.extend = extend;
214 fd.fm = &fm;
215 if (fd_tree == NULL) {
216 fd_tree = avl_create(comp_fd_entry, NULL, &avl_xallocator);
217 assert(fd_tree != NULL);
218 }
219 return (fd_entry *) avl_find(fd_tree, &fd);
220 }
221
222 static fd_entry *lookup_fontdescriptor(fo_entry * fo)
223 {
224 assert(fo != NULL);
225 assert(fo->fm != NULL);
226 assert(is_fontfile(fo->fm));
227 return lookup_fd_entry(fo->fm->ff_name, fo->fm->slant, fo->fm->extend);
228 }
229
230 void register_fd_entry(fd_entry * fd)
231 {
232 void **aa;
233 if (fd_tree == NULL) {
234 fd_tree = avl_create(comp_fd_entry, NULL, &avl_xallocator);
235 assert(fd_tree != NULL);
236 }
237 assert(fd != NULL && fd->fm != NULL && is_fontfile(fd->fm));
238 assert(lookup_fd_entry(fd->fm->ff_name, fd->fm->slant, fd->fm->extend) == NULL); /* font descriptor not yet registered */
239 aa = avl_probe(fd_tree, fd);
240 assert(aa != NULL);
241 }
242
243 static void create_fontdescriptor(fo_entry * fo, internalfontnumber f)
244 {
245 assert(fo != NULL);
246 assert(fo->fm != NULL);
247 assert(fo->fd == NULL);
248 fo->fd = new_fd_entry();
249 preset_fontname(fo);
250 preset_fontmetrics(fo->fd, f);
251 fo->fd->fe = fo->fe; /* encoding needed by TrueType writing */
252 fo->fd->fm = fo->fm; /* map entry needed by TrueType writing */
253 fo->fd->gl_tree = avl_create(comp_string_entry, NULL, &avl_xallocator);
254 assert(fo->fd->gl_tree != NULL);
255 }
256
257 /**********************************************************************/
258 /*
259 * For all used characters of TeX font f, get corresponding glyph names
260 * from external reencoding (.enc) file and collect these in the glyph
261 * tree gl_tree of font descriptor fd referenced by font dictionary fo.
262 */
263
264 static void mark_reenc_glyphs(fo_entry * fo, internalfontnumber f)
265 {
266 int i;
267 char **g;
268 void **aa;
269 assert(fo->fe != NULL);
270 if (is_subsetted(fo->fm)) {
271 assert(is_included(fo->fm));
272 /* mark glyphs from TeX (externally reencoded characters) */
273 g = fo->fe->glyph_names;
274 for (i = fo->first_char; i <= fo->last_char; i++) {
275 if (pdfcharmarked(f, i) && g[i] != notdef
276 && (char *) avl_find(fo->fd->gl_tree, g[i]) == NULL) {
277 aa = avl_probe(fo->fd->gl_tree, xstrdup(g[i]));
278 assert(aa != NULL);
279 }
280 }
281 }
282 }
283
284 /*
285 * Function mark_chars() has 2 uses:
286 * 1. Mark characters as chars on TeX level.
287 * 2. Mark encoding pairs used by TeX to optimize encoding vector.
288 */
289
290 static struct avl_table *mark_chars(fo_entry * fo, struct avl_table *tx_tree,
291 internalfontnumber f)
292 {
293 int i, *j;
294 void **aa;
295 if (tx_tree == NULL) {
296 tx_tree = avl_create(comp_int_entry, NULL, &avl_xallocator);
297 assert(tx_tree != NULL);
298 }
299 for (i = fo->first_char; i <= fo->last_char; i++) {
300 if (pdfcharmarked(f, i) && (int *) avl_find(tx_tree, &i) == NULL) {
301 j = xtalloc(1, int);
302 *j = i;
303 aa = avl_probe(tx_tree, j);
304 assert(aa != NULL);
305 }
306 }
307 return tx_tree;
308 }
309
310 /**********************************************************************/
311
312 static void get_char_range(fo_entry * fo, internalfontnumber f)
313 {
314 int i;
315 assert(fo != NULL);
316 for (i = fontbc[f]; i <= fontec[f]; i++) /* search for first_char and last_char */
317 if (pdfcharmarked(f, i))
318 break;
319 fo->first_char = i;
320 for (i = fontec[f]; i >= fontbc[f]; i--)
321 if (pdfcharmarked(f, i))
322 break;
323 fo->last_char = i;
324 if ((fo->first_char > fo->last_char)
325 || !pdfcharmarked(f, fo->first_char)) { /* no character used from this font */
326 fo->last_char = 0;
327 fo->first_char = fo->last_char + 1;
328 }
329 }
330
331 static void create_charwidth_array(fo_entry * fo, internalfontnumber f)
332 {
333 int i;
334 assert(fo != NULL);
335 assert(fo->cw == NULL);
336 fo->cw = new_cw_entry();
337 fo->cw->width = xtalloc(256, integer);
338 for (i = 0; i < fo->first_char; i++)
339 fo->cw->width[i] = 0;
340 for (i = fo->first_char; i <= fo->last_char; i++)
341 fo->cw->width[i] = dividescaled(getcharwidth(f, i), pdffontsize[f], 4);
342 for (i = fo->last_char + 1; i < 256; i++)
343 fo->cw->width[i] = 0;
344 }
345
346 static void write_charwidth_array(fo_entry * fo)
347 {
348 int i, j;
349 assert(fo->cw != NULL);
350 assert(fo->cw->cw_objnum == 0);
351 fo->cw->cw_objnum = pdfnewobjnum();
352 pdfbeginobj(fo->cw->cw_objnum, 1);
353 pdf_puts("[");
354 for (i = fo->first_char; i <= fo->last_char; i++) {
355 pdf_printf("%i", (int) fo->cw->width[i] / 10); /* see adv_char_width() in pdftex.web */
356 if ((j = fo->cw->width[i] % 10) != 0)
357 pdf_printf(".%i", j);
358 if (i != fo->last_char)
359 pdf_puts(" ");
360 }
361 pdf_puts("]\n");
362 pdfendobj();
363 }
364
365 /**********************************************************************/
366 /*
367 * Remark: Font objects from embedded PDF files are never registered
368 * into fo_tree; they are individually written out.
369 */
370
371 static fo_entry *lookup_fo_entry(char *s)
372 {
373 fo_entry fo;
374 fm_entry fm;
375 assert(s != NULL);
376 fm.tfm_name = s;
377 fo.fm = &fm;
378 if (fo_tree == NULL) {
379 fo_tree = avl_create(comp_fo_entry, NULL, &avl_xallocator);
380 assert(fo_tree != NULL);
381 }
382 return (fo_entry *) avl_find(fo_tree, &fo);
383 }
384
385 static void register_fo_entry(fo_entry * fo)
386 {
387 void **aa;
388 if (fo_tree == NULL) {
389 fo_tree = avl_create(comp_fo_entry, NULL, &avl_xallocator);
390 assert(fo_tree != NULL);
391 }
392 assert(fo != NULL);
393 assert(fo->fm != NULL);
394 assert(fo->fm->tfm_name != NULL);
395 assert(lookup_fo_entry(fo->fm->tfm_name) == NULL);
396 aa = avl_probe(fo_tree, fo);
397 assert(aa != NULL);
398 }
399
400 /**********************************************************************/
401
402 static void write_fontfile(fd_entry * fd)
403 {
404 assert(is_included(fd->fm));
405 if (is_type1(fd->fm))
406 writet1(fd);
407 else if (is_truetype(fd->fm))
408 writettf(fd);
409 else if (is_opentype(fd->fm))
410 writeotf(fd);
411 else
412 assert(0);
413 if (!fd->ff_found)
414 return;
415 assert(fd->ff_objnum == 0);
416 fd->ff_objnum = pdfnewobjnum();
417 pdfbegindict(fd->ff_objnum, 0); /* font file stream */
418 if (is_type1(fd->fm))
419 pdf_printf("/Length1 %i\n/Length2 %i\n/Length3 %i\n",
420 (int) t1_length1, (int) t1_length2, (int) t1_length3);
421 else if (is_truetype(fd->fm))
422 pdf_printf("/Length1 %i\n", (int) ttf_length);
423 else if (is_opentype(fd->fm))
424 pdf_puts("/Subtype /Type1C\n");
425 else
426 assert(0);
427 pdfbeginstream();
428 fb_flush();
429 pdfendstream();
430 }
431
432 /**********************************************************************/
433
434 static void write_fontdescriptor(fd_entry * fd)
435 {
436 static const int std_flags[] = {
437 /* indices for << start with 0, but bits start with 1, so the numbers
438 * for << are 1 lower than the bits in table 5.20 */
439 /* *INDENT-OFF* */
440 1 + 2 + (1 << 5), /* Courier */
441 1 + 2 + (1 << 5) + (1 << 18),/* Courier-Bold */
442 1 + 2 + (1 << 5) + (1 << 6), /* Courier-Oblique */
443 1 + 2 + (1 << 5) + (1 << 6) + (1 << 18),/* Courier-BoldOblique */
444 (1 << 5), /* Helvetica */
445 (1 << 5) + (1 << 18),/* Helvetica-Bold */
446 (1 << 5) + (1 << 6), /* Helvetica-Oblique */
447 (1 << 5) + (1 << 6) + (1 << 18),/* Helvetica-BoldOblique */
448 4, /* Symbol */
449 2 + (1 << 5), /* Times-Roman */
450 2 + (1 << 5) + (1 << 18),/* Times-Bold */
451 2 + (1 << 5) + (1 << 6), /* Times-Italic */
452 2 + (1 << 5) + (1 << 6) + (1 << 18),/* Times-BoldItalic */
453 4 /* ZapfDingbats */
454 /* *INDENT-ON* */
455 };
456 char *glyph;
457 struct avl_traverser t;
458 int fd_flags;
459 assert(fd != NULL && fd->fm != NULL);
460
461 if (is_fontfile(fd->fm))
462 write_fontfile(fd); /* this will set fd->ff_found if font file is found */
463 if (fd->fn_objnum != 0)
464 write_fontname_object(fd);
465 if (fd->fd_objnum == 0)
466 fd->fd_objnum = pdfnewobjnum();
467 pdfbegindict(fd->fd_objnum, 1);
468 pdf_puts("/Type /FontDescriptor\n");
469 write_fontname(fd, "FontName");
470 if (fd->fm->fd_flags != FD_FLAGS_NOT_SET_IN_MAPLINE)
471 fd_flags = (int) fd->fm->fd_flags;
472 else if (fd->ff_found)
473 fd_flags = FD_FLAGS_DEFAULT_EMBED;
474 else {
475 fd_flags = is_std_t1font(fd->fm)
476 ? std_flags[check_std_t1font(fd->fm->ps_name)]
477 : FD_FLAGS_DEFAULT_NON_EMBED;
478 pdftex_warn
479 ("No flags specified for non-embedded font `%s' (%s) (I'm using %i): "
480 "fix your map entry.",
481 fd->fm->ps_name != NULL ? fd->fm->ps_name : "No name given",
482 fd->fm->tfm_name, fd_flags);
483 }
484 pdf_printf("/Flags %i\n", fd_flags);
485 write_fontmetrics(fd);
486 if (fd->ff_found) {
487 if (getpdfomitcharset() == 0 && is_subsetted(fd->fm) && is_type1(fd->fm)) {
488 /* We don't get CharSet right. For some PDF standards,
489 CharSet is optional, but if it appears, it must be
490 correct. Unfortunately, there seems to be no practical
491 way we can guarantee correctness with precomposed accent
492 characters in our usual fonts (EC, TX, etc.):
493 https://mailman.ntg.nl/pipermail/ntg-pdftex/2018-June/004251.html
494
495 But for PDF/A-1, apparently it is required, regardless:
496 https://mailman.ntg.nl/pipermail/ntg-pdftex/2019-January/004264.html
497
498 Whereas for PDF/A-2 and PDF/A-3, it can be omitted:
499 https://github.com/veraPDF/veraPDF-validation-profiles/wiki/PDFA-Parts-2-and-3-rules#rule-62114-3
500
501 Therefore, whether it is output can be controlled by the
502 user at runtime via \pdfincludecharset. */
503 assert(fd->gl_tree != NULL);
504 avl_t_init(&t, fd->gl_tree);
505 /* Names may appear in any order. */
506 pdf_puts("/CharSet (");
507 for (glyph = (char *) avl_t_first(&t, fd->gl_tree); glyph != NULL;
508 glyph = (char *) avl_t_next(&t))
509 pdf_printf("/%s", glyph);
510 pdf_puts(")\n");
511 }
512
513 if (is_type1(fd->fm))
514 pdf_printf("/FontFile %i 0 R\n", (int) fd->ff_objnum);
515 else if (is_truetype(fd->fm))
516 pdf_printf("/FontFile2 %i 0 R\n", (int) fd->ff_objnum);
517 else if (is_opentype(fd->fm))
518 pdf_printf("/FontFile3 %i 0 R\n", (int) fd->ff_objnum);
519 else
520 assert(0);
521 }
522 pdfenddict();
523 }
524
525 static void write_fontdescriptors(void)
526 {
527 fd_entry *fd;
528 struct avl_traverser t;
529 if (fd_tree == NULL)
530 return;
531 avl_t_init(&t, fd_tree);
532 for (fd = (fd_entry *) avl_t_first(&t, fd_tree); fd != NULL;
533 fd = (fd_entry *) avl_t_next(&t))
534 write_fontdescriptor(fd);
535 }
536
537 /**********************************************************************/
538
539 static void write_fontdictionary(fo_entry * fo)
540 {
541 assert(fo != NULL);
542 assert(fo->fm != NULL);
543 assert(fo->fo_objnum != 0); /* reserved as pdf_font_num[f] in pdftex.web */
544 assert(fo->tex_font != 0);
545
546 /* write ToUnicode entry if needed */
547 if ((fixedgentounicode > 0 && fo->fd != NULL
548 && !pdffontnobuiltintounicode[fo->tex_font])
549 || (fo->fd != NULL && fo->fm->tfm_name != NULL
550 && strcmp(fo->fm->tfm_name, "dummy-space")) == 0) {
551 if (fo->fe != NULL) {
552 fo->tounicode_objnum =
553 write_tounicode(fo->fe->glyph_names, fo->fm->tfm_name, fo->fe->name);
554 } else if (is_type1(fo->fm)) {
555 if (fo->fd->builtin_glyph_names==NULL) {
556 pdftex_fail("builtin glyph names is empty");
557 }
558 fo->tounicode_objnum =
559 write_tounicode(fo->fd->builtin_glyph_names, fo->fm->tfm_name, NULL);
560 }
561 }
562
563 pdfbegindict(fo->fo_objnum, 1);
564 pdf_puts("/Type /Font\n");
565 pdf_puts("/Subtype /");
566 if (is_type1(fo->fm))
567 pdf_printf("%s\n", "Type1");
568 else if (is_truetype(fo->fm))
569 pdf_printf("%s\n", "TrueType");
570 else if (is_opentype(fo->fm))
571 pdf_printf("%s\n", "Type1");
572 else
573 assert(0);
574 assert(fo->fd != NULL && fo->fd->fd_objnum != 0);
575 write_fontname(fo->fd, "BaseFont");
576 pdf_printf("/FontDescriptor %i 0 R\n", (int) fo->fd->fd_objnum);
577 assert(fo->cw != NULL);
578 pdf_printf("/FirstChar %i\n/LastChar %i\n/Widths %i 0 R\n",
579 (int) fo->first_char, (int) fo->last_char,
580 (int) fo->cw->cw_objnum);
581 if ((is_type1(fo->fm) || is_opentype(fo->fm)) && fo->fe != NULL
582 && fo->fe->fe_objnum != 0)
583 pdf_printf("/Encoding %i 0 R\n", (int) fo->fe->fe_objnum);
584 if (fo->tounicode_objnum != 0)
585 pdf_printf("/ToUnicode %i 0 R\n", (int) fo->tounicode_objnum);
586 if (pdffontattr[fo->tex_font] != getnullstr()) {
587 pdfprint(pdffontattr[fo->tex_font]);
588 pdf_puts("\n");
589 }
590 pdfenddict();
591 }
592
593 static void write_fontdictionaries(void)
594 {
595 fo_entry *fo;
596 struct avl_traverser t;
597 if (fo_tree == NULL)
598 return;
599 avl_t_init(&t, fo_tree);
600 for (fo = (fo_entry *) avl_t_first(&t, fo_tree); fo != NULL;
601 fo = (fo_entry *) avl_t_next(&t))
602 write_fontdictionary(fo);
603 }
604
605 /**********************************************************************/
606 /*
607 * Final flush of all font related stuff by call from
608 * @<Output fonts definition@>= in pdftex.web.
609 */
610
611 void writefontstuff(void)
612 {
613 write_fontdescriptors();
614 write_fontencodings(); /* see writeenc.c */
615 write_fontdictionaries();
616 }
617
618 /**********************************************************************/
619
620 static void create_fontdictionary(fm_entry * fm, integer font_objnum,
621 internalfontnumber f)
622 {
623 fo_entry *fo = new_fo_entry();
624 get_char_range(fo, f); /* set fo->first_char and fo->last_char from f */
625 assert(fo->last_char >= fo->first_char);
626 fo->fm = fm;
627 fo->fo_objnum = font_objnum;
628 fo->tex_font = f;
629 if (is_reencoded(fo->fm)) { /* at least the map entry tells so */
630 fo->fe = get_fe_entry(fo->fm->encname); /* returns NULL if .enc file couldn't be opened */
631 if (fo->fe != NULL && (is_type1(fo->fm) || is_opentype(fo->fm))) {
632 if (fo->fe->fe_objnum == 0)
633 fo->fe->fe_objnum = pdfnewobjnum(); /* then it will be written out */
634 /* mark encoding pairs used by TeX to optimize encoding vector */
635 fo->fe->tx_tree = mark_chars(fo, fo->fe->tx_tree, f);
636 }
637 }
638 if (!is_builtin(fo->fm)) {
639 if (is_type1(fo->fm)) {
640 if ((fo->fd = lookup_fontdescriptor(fo)) == NULL) {
641 create_fontdescriptor(fo, f);
642 register_fd_entry(fo->fd);
643 }
644 } else
645 create_fontdescriptor(fo, f);
646 create_charwidth_array(fo, f);
647 write_charwidth_array(fo);
648 if (fo->fe != NULL) {
649 mark_reenc_glyphs(fo, f);
650 if (!is_type1(fo->fm)) {
651 /* mark reencoded characters as chars on TeX level */
652 assert(fo->fd->tx_tree == NULL);
653 fo->fd->tx_tree = mark_chars(fo, fo->fd->tx_tree, f);
654 if (is_truetype(fo->fm))
655 fo->fd->write_ttf_glyph_names = true;
656 }
657 } else
658 /* mark non-reencoded characters as chars on TeX level */
659 fo->fd->tx_tree = mark_chars(fo, fo->fd->tx_tree, f);
660 if (!is_type1(fo->fm))
661 write_fontdescriptor(fo->fd);
662 } else {
663 /* builtin fonts still need the /Widths array and /FontDescriptor
664 * (to avoid error 'font FOO contains bad /BBox')
665 */
666 create_charwidth_array(fo, f);
667 write_charwidth_array(fo);
668 create_fontdescriptor(fo, f);
669 write_fontdescriptor(fo->fd);
670 if (!is_std_t1font(fo->fm))
671 pdftex_warn("font `%s' is not a standard font; "
672 "I suppose it is available to your PDF viewer then",
673 fo->fm->ps_name);
674 }
675 if (is_type1(fo->fm))
676 register_fo_entry(fo);
677 else
678 write_fontdictionary(fo);
679 }
680
681
682
683 /* This is called font_has_subset in luatex, but it returns 1 if any
684 characters from the font are used, and 0 if not (using fontbc and
685 fontec as the endpoints to check), i.e., whether any characters are
686 actually used from the font. */
687
688 static int
689 font_is_used(internalfontnumber f)
690 {
691 int i, s;
692 /* search for |first_char| and |last_char| */
693 for (i = fontbc[f]; i <= fontec[f]; i++)
694 if (pdfcharmarked(f, i))
695 break;
696 s = i;
697 for (i = fontec[f]; i >= fontbc[f]; i--)
698 if (pdfcharmarked(f, i))
699 break;
700 if (s > i)
701 return 0;
702 else
703 return 1;
704 }
705
706 void
707 dopdffont(integer font_objnum, internalfontnumber f)
708 {
709 fm_entry *fm;
710 if (!font_is_used(f))
711 return; /* avoid failed assertion in create_fontdictionary,
712 mailman.ntg.nl/pipermail/ntg-pdftex/2018-January/004209.html */
713
714
715 fm = hasfmentry(f) ? (fm_entry *) pdffontmap[f] : NULL;
716 if (fm == NULL || is_pk(fm))
717 writet3(fm, font_objnum, f);
718 else
719 create_fontdictionary(fm, font_objnum, f);
720 }
721
722 // vim: ts=4