Debian Codegraph @

pdf/pdf_fmap.c

raw
1 /* Copyright (C) 2020-2025 Artifex Software, Inc.
2 All Rights Reserved.
3
4 This software is provided AS-IS with no warranty, either express or
5 implied.
6
7 This software is distributed under license and may not be copied,
8 modified or distributed except as expressly authorized under the terms
9 of the license contained in the file LICENSE in this distribution.
10
11 Refer to licensing information at http://www.artifex.com or contact
12 Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
13 CA 94129, USA, for further information.
14 */
15 /* Font re-mapping */
16
17 #include "strmio.h"
18 #include "stream.h"
19 #include "scanchar.h"
20 #include "gsstrl.h"
21
22 #include "pdf_int.h"
23 #include "pdf_types.h"
24 #include "pdf_font_types.h"
25 #include "pdf_array.h"
26 #include "pdf_dict.h"
27 #include "pdf_stack.h"
28 #include "pdf_file.h"
29 #include "pdf_misc.h"
30 #include "pdf_font.h"
31 #include "pdf_fontmt.h"
32 #include "pdf_fmap.h"
33
34 typedef struct
35 {
36 const char *keyname;
37 const char *mappedname;
38 } pdfi_custom_fmap_entry;
39
40 pdfi_custom_fmap_entry pdfi_custom_fmap_entries[] =
41 {
42 {"Helv", "Helvetica"},
43 {NULL, NULL}
44 };
45
46 static inline bool pdfi_fmap_file_exists(pdf_context *ctx, pdf_string *fname)
47 {
48 return pdfi_font_file_exists(ctx, (const char *)fname->data, fname->length);
49 }
50
51 static int
52 pdfi_fontmap_open_file(pdf_context *ctx, const char *mapfilename, byte **buf, int *buflen)
53 {
54 int code = 0;
55 stream *s;
56 char fname[gp_file_name_sizeof];
57 const char *path_pfx = "Init/";
58 const char *poststring = "\nendstream";
59 const int poststringlen = strlen(poststring);
60 fname[0] = '\0';
61
62 if (strlen(path_pfx) + strlen(mapfilename) + 1 > gp_file_name_sizeof)
63 return_error(gs_error_invalidfileaccess);
64
65 code = pdfi_open_resource_file(ctx, mapfilename, strlen(mapfilename), &s);
66 if (code < 0) {
67 strncat(fname, path_pfx, strlen(path_pfx));
68 strncat(fname, (char *)mapfilename, strlen(mapfilename));
69 code = pdfi_open_resource_file(ctx, fname, strlen(fname), &s);
70 }
71
72 if (code >= 0) {
73 int i;
74 int64_t file_size;
75
76 sfseek(s, 0, SEEK_END);
77 file_size = sftell(s);
78 sfseek(s, 0, SEEK_SET);
79 if (file_size < 0 || file_size > max_int - poststringlen) {
80 code = gs_note_error(gs_error_ioerror);
81 } else {
82 byte *dbuf;
83 *buflen = (int)file_size;
84
85 (*buf) = gs_alloc_bytes(ctx->memory, (size_t)*buflen + poststringlen, "pdf_cmap_open_file(buf)");
86 if (*buf != NULL) {
87 sfread((*buf), 1, *buflen, s);
88 memcpy((*buf) + *buflen, poststring, poststringlen);
89 *buflen += poststringlen;
90 /* This is naff, but works for now
91 When parsing Fontmap in PS, ";" is defined as "def"
92 Also some people use "cvn" to convert a string to a name.
93 We don't need either, because the dictionary is built from the stack.
94 */
95 for (i = 0, dbuf = *buf; i < *buflen - 1; i++, dbuf++) {
96 if (*dbuf == ';') {
97 *dbuf = ' ';
98 }
99 if (memcmp(dbuf, "cvn ", 4) == 0) {
100 dbuf[0] = dbuf[1] = dbuf[2] = 0x20;
101 }
102 }
103 }
104 else {
105 code = gs_note_error(gs_error_VMerror);
106 }
107 }
108 sfclose(s);
109 }
110 return code;
111 }
112
113 #ifdef UFST_BRIDGE
114 /* we know fco_path is null terminated */
115 static pdf_string *pdfi_make_fco_path_string(pdf_context *ctx, char *fco_path)
116 {
117 pdf_string *fp;
118 int code = pdfi_object_alloc(ctx, PDF_STRING, strlen(fco_path), (pdf_obj **)&fp);
119 if (code < 0)
120 return NULL;
121 pdfi_countup(fp);
122 memcpy(fp->data, fco_path, strlen(fco_path));
123
124 return fp;
125 }
126 #endif /* UFST_BRIDGE */
127
128 static inline int pdfi_populate_ufst_fontmap(pdf_context *ctx)
129 {
130 #ifdef UFST_BRIDGE
131 int status = 0;
132 int bSize, i;
133 char pthnm[gp_file_name_sizeof];
134 char *ufst_root_dir;
135 char *fco;
136 char *fco_start, *fco_lim;
137 size_t ufst_root_dir_len;
138
139 if (pdfi_fapi_ufst_available(ctx->memory) == false) {
140 return (0);
141 }
142
143 ufst_root_dir = (char *)pdfi_fapi_ufst_get_font_dir(ctx->memory);
144 ufst_root_dir_len = strlen(ufst_root_dir);
145 if (ufst_root_dir_len >= gp_file_name_sizeof) {
146 return gs_error_Fatal;
147 }
148 fco_start = fco = (char *)pdfi_fapi_ufst_get_fco_list(ctx->memory);
149 fco_lim = fco_start + strlen(fco_start) + 1;
150 while (fco < fco_lim && status == 0) {
151 pdf_string *fco_str;
152 status = 0;
153 /* build and open (get handle) for the k'th fco file name */
154 gs_strlcpy((char *)pthnm, ufst_root_dir, sizeof pthnm);
155
156 for (i = 2; fco[i] != gp_file_name_list_separator && (&fco[i]) < fco_lim - 1; i++)
157 ;
158
159 if (i + ufst_root_dir_len >= gp_file_name_sizeof) {
160 return gs_error_Fatal;
161 }
162 strncat(pthnm, fco, i);
163 fco += (i + 1);
164 fco_str = pdfi_make_fco_path_string(ctx, pthnm);
165 if (fco_str == NULL) {
166 return gs_error_Fatal;
167 }
168
169 /* enumerate the files in this fco */
170 for (i = 0; status == 0; i++) {
171 char *pname = NULL;
172 pdf_font_microtype *pdffont = NULL;
173 int font_number = 0;
174
175 status = pdfi_alloc_mt_font(ctx, fco_str, i, &pdffont);
176 if (status < 0)
177 break;
178 status = pdfi_fapi_passfont((pdf_font *)pdffont, i, (char *)"UFST", pthnm, NULL, 0);
179 if (status < 0){
180 #ifdef DEBUG
181 outprintf(ctx->memory, "CGIFfco_Access error %d\n", status);
182 #endif
183 pdfi_countdown(pdffont);
184 break;
185 }
186
187 /* For Microtype fonts, once we get here, these
188 * pl_fapi_get*() calls cannot fail, so we can
189 * safely ignore the return value
190 */
191 (void)pdfi_fapi_get_mtype_font_name((gs_font *)pdffont->pfont, NULL, &bSize);
192
193 pname = (char *)gs_alloc_bytes(ctx->memory, bSize, "pdfi: mt font name buffer");
194 if (!pname) {
195 pdfi_countdown(pdffont);
196 (void)pdfi_set_warning_stop(ctx, gs_note_error(gs_error_VMerror), NULL, W_PDF_VMERROR_BUILTIN_FONT, "pdfi_populate_ufst_fontmap", "");
197 outprintf(ctx->memory, "VM Error for built-in font %d", i);
198 continue;
199 }
200
201 (void)pdfi_fapi_get_mtype_font_name((gs_font *)pdffont->pfont, (byte *) pname, &bSize);
202 (void)pdfi_fapi_get_mtype_font_number((gs_font *)pdffont->pfont, &font_number);
203
204 if (bSize < gs_font_name_max) {
205 memcpy(pdffont->pfont->key_name.chars, pname, bSize);
206 pdffont->pfont->key_name.chars[bSize] = 0;
207 pdffont->pfont->key_name.size = bSize;
208 memcpy(pdffont->pfont->font_name.chars, pname, bSize);
209 pdffont->pfont->font_name.chars[bSize] = 0;
210 pdffont->pfont->font_name.size = bSize;
211 }
212 status = gs_definefont(ctx->font_dir, (gs_font *) pdffont->pfont);
213 if (status < 0) {
214 pdfi_countdown(pdffont);
215 status = 0;
216 continue;
217 }
218 gs_notify_release(&pdffont->pfont->notify_list);
219 status = pdfi_fapi_passfont((pdf_font *)pdffont, i, (char *)"UFST", pthnm, NULL, 0);
220 if (status < 0) {
221 pdfi_countdown(pdffont);
222 status = 0;
223 continue;
224 }
225 status = pdfi_dict_put(ctx, ctx->pdffontmap, pname, (pdf_obj *)pdffont);
226 pdfi_countdown(pdffont);
227 if (status < 0) {
228 status = 0;
229 continue;
230 }
231 gs_free_object(ctx->memory, pname, "pdfi_populate_ufst_fontmap");
232 }
233 pdfi_countdown(fco_str);
234 }
235 #endif /* UFST_BRIDGE */
236 return 0;
237 }
238
239 static int
240 pdf_make_fontmap(pdf_context *ctx, const char *default_fmapname, int cidfmap)
241 {
242 byte *fmapbuf = NULL;
243 int code, fmapbuflen;
244 pdf_c_stream *fmapstr = NULL;
245 pdf_stream fakedict = {0};
246 pdfi_custom_fmap_entry *pcfe = pdfi_custom_fmap_entries;
247 int i, j = 0;
248 char fmapname[gp_file_name_sizeof];
249 pdf_c_stream fakemainstream = {0};
250 int stacksize = pdfi_count_stack(ctx);
251
252 strncpy(fmapname, default_fmapname, strlen(default_fmapname) + 1);
253
254 code = pdfi_mark_stack(ctx, PDF_DICT_MARK);
255 if (code < 0)
256 goto done;
257
258 do {
259 if (j < ctx->num_fontmapfiles && cidfmap == false) {
260 memcpy(fmapname, ctx->fontmapfiles[j].data, ctx->fontmapfiles[j].size);
261 fmapname[ctx->fontmapfiles[j].size] = '\0';
262 }
263
264 code = pdfi_fontmap_open_file(ctx, (const char *)fmapname, &fmapbuf, &fmapbuflen);
265 if (code < 0) {
266 if (ctx->args.QUIET != true) {
267 (void)outwrite(ctx->memory, "Warning: ", 9);
268 if (cidfmap)
269 (void)outwrite(ctx->memory, "cidfmap file ", 13);
270 else
271 (void)outwrite(ctx->memory, "Fontmap file \"", 14);
272 (void)outwrite(ctx->memory, fmapname, strlen(fmapname));
273 (void)outwrite(ctx->memory, "\" not found.\n", 13);
274 code = 0;
275 }
276 }
277 else {
278 code = pdfi_open_memory_stream_from_memory(ctx, fmapbuflen, fmapbuf, &fmapstr, true);
279 if (code >= 0) {
280
281 if (ctx->main_stream == NULL) {
282 ctx->main_stream = &fakemainstream;
283 }
284
285 if (fmapbuflen > 0)
286 code = pdfi_interpret_content_stream(ctx, fmapstr, &fakedict, NULL);
287 else
288 code = 0;
289
290 if (ctx->main_stream == &fakemainstream) {
291 ctx->main_stream = NULL;
292 }
293 gs_free_object(ctx->memory, fmapbuf, "pdf_make_fontmap(fmapbuf)");
294 }
295 }
296 j++;
297 } while(j < ctx->num_fontmapfiles && cidfmap == false && code >= 0);
298
299 if (code >= 0) {
300 if (pdfi_count_stack(ctx) > stacksize) {
301 code = pdfi_dict_from_stack(ctx, 0, 0, true);
302 if (code < 0)
303 goto done;
304
305 if (cidfmap == true) {
306 ctx->pdfcidfmap = (pdf_dict *)ctx->stack_top[-1];
307 pdfi_countup(ctx->pdfcidfmap);
308 }
309 else {
310 ctx->pdffontmap = (pdf_dict *)ctx->stack_top[-1];
311 pdfi_countup(ctx->pdffontmap);
312 }
313 pdfi_pop(ctx, 1);
314 code = 0;
315
316 /* Add our internal aliases to the fontmap. */
317 if (cidfmap == false) {
318 for (i = 0; pcfe[i].keyname != NULL; i++) {
319 pdf_obj *value;
320 bool k;
321
322 /* We don't want to *replace* entries */
323 if (pdfi_dict_known(ctx, ctx->pdffontmap, pcfe[i].keyname, &k) >= 0
324 && k != true) {
325 code = pdfi_name_alloc(ctx, (byte *)pcfe[i].mappedname, strlen(pcfe[i].mappedname), &value);
326 if (code < 0)
327 continue;
328 pdfi_countup(value);
329 /* If dict_put throws an error, we just carry on - hence the (void) */
330 (void)pdfi_dict_put(ctx, ctx->pdffontmap, pcfe[i].keyname, value);
331 pdfi_countdown(value);
332 }
333 }
334 }
335 }
336 else {
337 code = gs_note_error(gs_error_syntaxerror);
338 }
339 }
340 done:
341 /* We always want to leave here with a valid map dictionary
342 even if it's empty
343 */
344 if (cidfmap == true) {
345 if (ctx->pdfcidfmap == NULL) {
346 code = pdfi_dict_alloc(ctx, 0, &ctx->pdfcidfmap);
347 if (code >= 0)
348 pdfi_countup(ctx->pdfcidfmap);
349 }
350 }
351 else {
352 if (ctx->pdffontmap == NULL) {
353 code = pdfi_dict_alloc(ctx, 0, &ctx->pdffontmap);
354 if (code >= 0)
355 pdfi_countup(ctx->pdffontmap);
356 }
357 }
358 if (code >= 0) {
359 code = pdfi_populate_ufst_fontmap(ctx);
360 }
361 #ifdef DUMP_FONTMAP
362 if (ctx->pdffontmap != NULL) {
363 uint64_t ind;
364 int find = -1;
365 pdf_name *key = NULL;
366 pdf_obj *v = NULL;
367 pdf_string *val = NULL;
368 (void)pdfi_dict_key_first(ctx, ctx->pdffontmap, (pdf_obj **) &key, &ind);
369 (void)pdfi_dict_get_by_key(ctx, ctx->pdffontmap, key, (pdf_obj **)&v);
370 for (j = 0; j < key->length; j++)
371 dprintf1("%c", key->data[j]);
372 if (pdfi_type_of(v) == PDF_DICT) {
373 pdf_num *n;
374 pdf_string *val2;
375 code = pdfi_dict_get(ctx, (pdf_dict *)v, "Index", (pdf_obj **)&n);
376 if (code >= 0 && pdfi_type_of(n) == PDF_INT)
377 find = n->value.i;
378 else
379 code = 0;
380 (void)pdfi_dict_get(ctx, (pdf_dict *)v, "Path", (pdf_obj **)&val2);
381 val = val2;
382 }
383 else {
384 val = (pdf_string *)v;
385 }
386 dprintf(" ");
387 for (j = 0; j < val->length; j++)
388 dprintf1("%c", val->data[j]);
389 if (find != -1) {
390 dprintf1(" Index = %d", find);
391 find = -1;
392 }
393
394 dprintf("\n");
395 pdfi_countdown(key);
396 pdfi_countdown(val);
397
398 while (pdfi_dict_key_next(ctx, ctx->pdffontmap, (pdf_obj **) &key, &ind) >= 0 && ind > 0) {
399 (void)pdfi_dict_get_by_key(ctx, ctx->pdffontmap, key, (pdf_obj **)&v);
400 for (j = 0; j < key->length; j++)
401 dprintf1("%c", key->data[j]);
402 if (pdfi_type_of(v) == PDF_DICT) {
403 pdf_num *n;
404 pdf_string *val2;
405 code = pdfi_dict_get(ctx, (pdf_dict *)v, "Index", (pdf_obj **)&n);
406 if (code >= 0 && pdfi_type_of(n) == PDF_INT)
407 find = n->value.i;
408 else
409 code = 0;
410 (void)pdfi_dict_get(ctx, (pdf_dict *)v, "Path", (pdf_obj **)&val2);
411 val = val2;
412 }
413 else {
414 val = (pdf_string *)v;
415 }
416 dprintf(" ");
417 for (j = 0; j < val->length; j++)
418 dprintf1("%c", val->data[j]);
419 if (find != -1) {
420 dprintf1(" Index = %d", find);
421 find = -1;
422 }
423 pdfi_countdown(key);
424 pdfi_countdown(val);
425 dprintf("\n");
426 }
427 }
428 #endif
429 pdfi_clearstack(ctx);
430 return code;
431 }
432
433 enum {
434 no_type_font = -1,
435 type0_font = 0,
436 type1_font = 1,
437 cff_font = 2,
438 type3_font = 3,
439 tt_font = 42
440 };
441
442 /* For font file scanning we want to treat OTTO as TTF (rather than, for "normal"
443 font loading, treat OTTO as CFF, hence we need a different type picker.
444 */
445 static int pdfi_font_scan_type_picker(byte *buf, int64_t buflen)
446 {
447 #define MAKEMAGIC(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
448
449 if (buflen >= 4) {
450 if (MAKEMAGIC(buf[0], buf[1], buf[2], buf[3]) == MAKEMAGIC(0, 1, 0, 0)
451 || MAKEMAGIC(buf[0], buf[1], buf[2], buf[3]) == MAKEMAGIC('t', 'r', 'u', 'e')
452 || MAKEMAGIC(buf[0], buf[1], buf[2], buf[3]) == MAKEMAGIC('t', 't', 'c', 'f')) {
453 return tt_font;
454 }
455 else if (MAKEMAGIC(buf[0], buf[1], buf[2], buf[3]) == MAKEMAGIC('O', 'T', 'T', 'O')) {
456 return tt_font;
457 }
458 else if (MAKEMAGIC(buf[0], buf[1], buf[2], 0) == MAKEMAGIC('%', '!', 'P', 0)) {
459 return type1_font; /* pfa */
460 }
461 else if (MAKEMAGIC(buf[0], buf[1], buf[2], 0) == MAKEMAGIC(1, 0, 4, 0)) {
462 return cff_font; /* 1C/CFF */
463 }
464 else if (MAKEMAGIC(buf[0], buf[1], 0, 0) == MAKEMAGIC(128, 1, 0, 0)) {
465 return type1_font; /* pfb */
466 }
467 }
468 return no_type_font;
469 #undef MAKEMAGIC
470 }
471
472 static int pdfi_add__to_native_fontmap(pdf_context *ctx, const char *fontname, const char *filepath, const int index)
473 {
474 int code;
475 pdf_string *fpstr;
476
477 if (ctx->pdfnativefontmap == NULL) {
478 /* 32 is just an arbitrary starting point */
479 code = pdfi_dict_alloc(ctx, 32, &ctx->pdfnativefontmap);
480 if (code < 0)
481 return code;
482 pdfi_countup(ctx->pdfnativefontmap);
483 }
484 /* index == -1 is a file with a single font in it */
485 if (index == -1) {
486 code = pdfi_object_alloc(ctx, PDF_STRING, strlen(filepath), (pdf_obj **)&fpstr);
487 if (code < 0)
488 return code;
489 pdfi_countup(fpstr);
490 memcpy(fpstr->data, filepath, fpstr->length);
491 code = pdfi_dict_put(ctx, ctx->pdfnativefontmap, fontname, (pdf_obj *)fpstr);
492 pdfi_countdown(fpstr);
493 }
494 else {
495 pdf_dict *recdict;
496 pdf_num *ind;
497
498 code = pdfi_object_alloc(ctx, PDF_DICT, 2, (pdf_obj **)&recdict);
499 if (code < 0)
500 return code;
501 pdfi_countup(recdict);
502
503 code = pdfi_object_alloc(ctx, PDF_STRING, strlen(filepath), (pdf_obj **)&fpstr);
504 if (code < 0) {
505 pdfi_countdown(recdict);
506 return code;
507 }
508 pdfi_countup(fpstr);
509 memcpy(fpstr->data, filepath, fpstr->length);
510 code = pdfi_dict_put(ctx, recdict, "Path", (pdf_obj *)fpstr);
511 pdfi_countdown(fpstr);
512 if (code < 0) {
513 pdfi_countdown(recdict);
514 return code;
515 }
516
517 code = pdfi_object_alloc(ctx, PDF_INT, 0, (pdf_obj **)&ind);
518 if (code < 0) {
519 pdfi_countdown(recdict);
520 return code;
521 }
522 ind->value.i = index;
523 pdfi_countup(ind);
524 code = pdfi_dict_put(ctx, recdict, "Index", (pdf_obj *)ind);
525 pdfi_countdown(ind);
526 if (code < 0) {
527 pdfi_countdown(recdict);
528 return code;
529 }
530 code = pdfi_dict_put(ctx, ctx->pdfnativefontmap, fontname, (pdf_obj *)recdict);
531 pdfi_countdown(recdict);
532 }
533
534 return code;
535 }
536
537 static inline int
538 pdfi_end_ps_token(int c)
539 {
540 return (c == 0x20) || (c == 0x9) || (c == 0xD) || (c == 0xA) || (c == '/') || (c == '\0');
541 }
542
543 /* Naive way to find a Type 1 /FontName key */
544 static int pdfi_type1_add_to_native_map(pdf_context *ctx, stream *f, char *fname, char *pname, int pname_size)
545 {
546 gs_string buf;
547 uint count = 0;
548 int code = gs_error_undefined;
549 char *namestr = NULL, *enamestr;
550 char *typestr;
551 bool pin_eol = false; /* initialised just to placate coverity */
552 int type = -1;
553 int lines = 0;
554 buf.data = (byte *)pname;
555 buf.size = pname_size;
556
557 /* It's not an absolute requirement, but it is a de facto standard that
558 /FontType and /FontName keys start in column 0 of their lines
559 */
560 while ((code = sreadline(f, NULL, NULL, NULL, &buf, NULL, &count, &pin_eol, NULL)) >= 0) {
561 lines++;
562 if (lines > 100 || (buf.size >= 17 && memcmp(buf.data, "currentfile eexec", 17) == 0))
563 break;
564 else if (buf.size > 9 && memcmp(buf.data, "/FontName", 9) == 0) {
565 namestr = (char *)buf.data + 9;
566 while (pdfi_end_ps_token(*namestr))
567 namestr++;
568 if (*namestr != '\0') {
569 while(*namestr == 0x20 || *namestr == 0x9)
570 namestr++;
571 if (*namestr == '/')
572 namestr++;
573 enamestr = namestr;
574 while(!pdfi_end_ps_token((int)*enamestr))
575 enamestr++;
576 count = enamestr - namestr > pname_size - 1 ? pname_size - 1 : enamestr - namestr;
577 memmove(pname, namestr, count);
578 pname[count] = '\0';
579 buf.data += count + 1;
580 buf.size -= count + 1;
581 if (type == 1)
582 break;
583 }
584 }
585 else if (buf.size > 9 && memcmp(buf.data, "/FontType", 9) == 0) {
586 typestr = (char *)buf.data + 9;
587 while (pdfi_end_ps_token(*typestr))
588 typestr++;
589 if (*typestr != '\0') {
590 while(*typestr == 0x20 || *typestr == 0x9)
591 namestr++;
592 if (*typestr == '/')
593 typestr++;
594 enamestr = typestr;
595 while(!pdfi_end_ps_token((int)*enamestr))
596 enamestr++;
597 count = enamestr - typestr > pname_size - 1 ? pname_size - 1 : enamestr - typestr;
598 if (count == 1 && *typestr == '1') {
599 type = 1;
600 if (namestr != NULL)
601 break;
602 }
603 }
604 }
605 count = 0;
606 }
607 if (type == 1 && namestr != NULL) {
608 code = pdfi_add__to_native_fontmap(ctx, (const char *)pname, (const char *)fname, -1);
609 }
610 return code < 0 ? code : gs_error_handled;
611 }
612
613 static inline int
614 u16(const byte *p)
615 {
616 return (p[0] << 8) | p[1];
617 }
618
619 static inline int
620 sru16(stream *s)
621 {
622 byte p[2];
623 int code = sfread(p, 1, 2, s);
624
625 if (code < 0)
626 return 0;
627
628 return u16(p);
629 }
630
631 static inline int
632 u32(const byte *p)
633 {
634 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
635 }
636
637 static inline int
638 sru32(stream *s)
639 {
640 byte p[4];
641 int code = sfread(p, 1, 4, s);
642 if (code < 0)
643 return 0;
644
645 return u32(p);
646 }
647
648 static int pdfi_ttf_add_to_native_map(pdf_context *ctx, stream *f, byte magic[4], char *fname, char *pname, int pname_size)
649 {
650 int ntables, i, j, k, code2, code = gs_error_undefined;
651 char table[4];
652 int nte, storageOffset;
653 bool include_index = false;
654 uint32_t nfonts = 1, tableoffs;
655 int findex;
656 gs_offset_t l;
657
658 code2 = sfseek(f, 0, SEEK_END);
659 if (code2 < 0)
660 return code;
661 l = stell(f);
662 /* 4 to skip the magic number */
663 code2 = sfseek(f, 4, SEEK_SET);
664 if (code2 < 0)
665 return code;
666
667 if (memcmp(magic, "ttcf", 4) == 0) {
668 uint32_t ver;
669 include_index = true;
670 ver = sru32(f);
671 if (ver != 0x00010000 && ver !=0x00020000) {
672 code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_invalidaccess), NULL, E_PDF_BAD_TTC_VERSION, "pdfi_ttf_add_to_native_map", NULL);
673 outprintf(ctx->memory, "Unknown TTC header version %08X.\n", ver);
674 return code;
675 }
676 nfonts = sru32(f);
677 /* There isn't a specific limit on the number of fonts,
678 freetype limits to 255, so we'll do the same here
679 */
680 if (nfonts > 255)
681 return_error(gs_error_invalidfont);
682 }
683
684 for (findex = 0; findex < nfonts; findex++) {
685 if (include_index == true ) {
686 if (findex * 4 > l)
687 break;
688 code2 = sfseek(f, findex * 4, SEEK_CUR);
689 if (code2 < 0)
690 return code2;
691
692 tableoffs = sru32(f);
693 if (tableoffs + 4 > l)
694 break;
695 code2 = sfseek(f, tableoffs + 4, SEEK_SET);
696 if (code2 < 0)
697 return code2;
698 }
699
700 ntables = sru16(f);
701 /* Similar to above - no spec limit, but we do the same
702 as freetype
703 */
704 if (ntables > 255)
705 continue;
706
707 /* Skip the remainder of the invariant header bytes */
708 (void)sru16(f);
709 (void)sru32(f);
710
711 for (i = 0, code2 = 0; i < ntables && code2 >= 0; i++) {
712 if (stell(f) + 4 > l) {
713 code2 = gs_error_ioerror;
714 continue;
715 }
716 code2 = sfread(table, 1, 4, f);
717 if (code2 < 0)
718 return code2;
719 if (!memcmp(table, "name", 4)) {
720 uint table_pos, table_len;
721 byte *namet;
722 (void)sru32(f); /* skip checksum */
723
724 table_pos = sru32(f);
725 table_len = sru32(f);
726 if (table_pos + table_len > l) {
727 code2 = gs_error_ioerror;
728 continue;
729 }
730
731 code2 = sfseek(f, table_pos, SEEK_SET);
732 if (code2 < 0)
733 continue;
734
735 namet = (byte *)gs_alloc_bytes(ctx->memory, table_len, "pdfi_ttf_add_to_native_map");
736 if (namet == NULL)
737 return_error(gs_error_VMerror);
738 code2 = sfread(namet, 1, table_len, f);
739 if (code2 < 0) {
740 gs_free_object(ctx->memory, namet,"pdfi_ttf_add_to_native_map");
741 continue;
742 }
743 nte = u16(namet + 2);
744 /* Again, arbitrary limit... */
745 if (nte > 255) {
746 gs_free_object(ctx->memory, namet,"pdfi_ttf_add_to_native_map");
747 continue;
748 }
749 storageOffset = u16(namet + 4);
750
751 for (j = 0; j < nte; j++) {
752 byte *rec = namet + 6 + j * 12;
753 if (u16(rec + 6) == 6) {
754 int pid = u16(rec);
755 int eid = u16(rec + 2);
756 int nl = u16(rec + 8);
757 int noffs = u16(rec + 10);
758
759 if (nl + noffs + storageOffset > table_len || nl >= pname_size) {
760 break;
761 }
762 memcpy(pname, namet + storageOffset + noffs, nl);
763 pname[nl] = '\0';
764 if ((pid == 0 || (pid == 2 && eid == 1) || (pid == 3 && eid == 1)) && (nl % 2) == 0) {
765 for (k = 0; k < nl; k += 2) {
766 if (pname[k] != '\0')
767 break;
768 }
769 if (k == nl) {
770 int k1, k2;
771 for (k1 = 0, k2 = 1; k2 < nl; k1++, k2 += 2) {
772 pname[k1] = pname[k2];
773 }
774 nl = nl >> 1;
775 pname[nl] = '\0';
776 }
777 }
778
779 for (k = 0; k < nl; k++)
780 if (pname[k] < 32 || pname[k] > 126) /* is it a valid PS name? */
781 break;
782 if (k == nl) {
783 code = 0;
784 break;
785 }
786 }
787 }
788 if (code == gs_error_undefined) {
789 for (j = 0; j < nte; j++) {
790 byte *rec = namet + 6 + j * 12;
791 if (u16(rec + 6) == 4) {
792 int pid = u16(rec);
793 int eid = u16(rec + 2);
794 int nl = u16(rec + 8);
795 int noffs = u16(rec + 10);
796
797 if (nl + noffs + storageOffset > table_len || nl >= pname_size) {
798 break;
799 }
800 memcpy(pname, namet + storageOffset + noffs, nl);
801 pname[nl] = '\0';
802 if ((pid == 0 || (pid == 2 && eid == 1) || (pid == 3 && eid == 1)) && (nl % 2) == 0) {
803 for (k = 0; k < nl; k += 2) {
804 if (pname[k] != '\0')
805 break;
806 }
807 if (k == nl) {
808 int k1, k2;
809 for (k1 = 0, k2 = 1; k2 < nl; k1++, k2 += 2) {
810 pname[k1] = pname[k2];
811 }
812 nl = nl >> 1;
813 pname[nl] = '\0';
814 }
815 }
816 for (k = 0; k < nl; k++)
817 if (pname[k] < 32 || pname[k] > 126) /* is it a valid PS name? */
818 break;
819 if (k == nl)
820 code = 0;
821 break;
822 }
823 }
824 }
825 gs_free_object(ctx->memory, namet, "pdfi_ttf_add_to_native_map");
826 break;
827 }
828 else {
829 sfseek(f, 12, SEEK_CUR);
830 }
831 }
832 if (code >= 0)
833 code = pdfi_add__to_native_fontmap(ctx, (const char *)pname, (const char *)fname, (include_index == true ? findex : -1));
834 }
835 return code;
836 }
837
838 static const char *font_scan_skip_list[] = {
839 ".afm",
840 ".bat",
841 ".c",
842 ".cmd",
843 ".com",
844 ".dir",
845 ".dll",
846 ".doc",
847 ".drv",
848 ".exe",
849 ".fon",
850 ".fot",
851 ".h",
852 ".o",
853 ".obj",
854 ".pfm",
855 ".pss",
856 ".txt",
857 ".gz",
858 ".pcf"
859 };
860
861 static bool font_scan_skip_file(const char *fname)
862 {
863 size_t l2, l = strlen(fname);
864 bool skip = false;
865 int i;
866
867 for (i = 0; i < (sizeof(font_scan_skip_list)/sizeof(*font_scan_skip_list)); i++) {
868 l2 = strlen(font_scan_skip_list[i]);
869 if (memcmp(font_scan_skip_list[i], fname + l - l2, l2) == 0) {
870 skip = true;
871 break;
872 }
873 }
874 return skip;
875 }
876
877 static int pdfi_add_font_to_native_map(pdf_context *ctx, const char *fp, char *working)
878 {
879 stream *sf;
880 int code = 0;
881 uint nread;
882 byte magic[4]; /* We only (currently) use up to 4 bytes for type guessing */
883 int type;
884
885 if (font_scan_skip_file(fp))
886 return 0;
887
888 sf = sfopen(fp, "r", ctx->memory);
889 if (sf == NULL)
890 return 0;
891 code = sgets(sf, magic, 4, &nread);
892 if (code < 0 || nread < 4) {
893 code = 0;
894 sfclose(sf);
895 return 0;
896 }
897
898 code = sfseek(sf, 0, SEEK_SET);
899 if (code < 0) {
900 code = 0;
901 sfclose(sf);
902 return 0;
903 }
904 /* Slightly naff: in this one case, we want to treat OTTO fonts
905 as Truetype, so we lookup the TTF 'name' table - it's more efficient
906 than decoding the CFF, and probably will give more expected results
907 */
908 if (memcmp(magic, "OTTO", 4) == 0 || memcmp(magic, "ttcf", 4) == 0) {
909 type = tt_font;
910 }
911 else {
912 type = pdfi_font_scan_type_picker((byte *)magic, 4);
913 }
914 switch(type) {
915 case tt_font:
916 code = pdfi_ttf_add_to_native_map(ctx, sf, magic, (char *)fp, working, gp_file_name_sizeof);
917 break;
918 case cff_font:
919 code = gs_error_undefined;
920 break;
921 case type1_font:
922 code = pdfi_type1_add_to_native_map(ctx, sf, (char *)fp, working, gp_file_name_sizeof);
923 break;
924 default:
925 break;
926 }
927 sfclose(sf);
928 /* We ignore most errors, on the basis it probably means it wasn't a valid font file */
929 if (code != gs_error_VMerror)
930 code = 0;
931
932 return code;
933 }
934
935 /* still writes into pdfnativefontmap dictionary */
936 static int pdfi_generate_platform_fontmap(pdf_context *ctx)
937 {
938 void *enum_state = NULL;
939 int code = 0;
940 char *fontname, *path;
941 char *working = NULL;
942
943 working = (char *)gs_alloc_bytes(ctx->memory, gp_file_name_sizeof, "pdfi_generate_platform_fontmap");
944 if (working == NULL) {
945 code = gs_note_error(gs_error_VMerror);
946 goto done;
947 }
948
949 enum_state = gp_enumerate_fonts_init(ctx->memory);
950 if (enum_state == NULL) {
951 code = gs_note_error(gs_error_VMerror);
952 goto done;
953 }
954
955 while((code = gp_enumerate_fonts_next(enum_state, &fontname, &path )) > 0) {
956 if (fontname == NULL || path == NULL) {
957 continue;
958 }
959 code = pdfi_add_font_to_native_map(ctx, path, working);
960
961 /* We ignore most errors, on the basis it probably means it wasn't a valid font file */
962 if (code == gs_error_VMerror)
963 break;
964 code = 0;
965 }
966 done:
967 gp_enumerate_fonts_free(enum_state);
968 gs_free_object(ctx->memory, working, "pdfi_generate_platform_fontmap");
969 return code;
970 }
971
972 static int pdfi_generate_native_fontmap(pdf_context *ctx)
973 {
974 file_enum *fe;
975 int i;
976 char *patrn= NULL;
977 char *result = NULL;
978 char *working = NULL;
979 int code = 0, l;
980
981 if (ctx->pdfnativefontmap != NULL) /* Only run this once */
982 return 0;
983 if (ctx->args.nonativefontmap == true) {
984 /* Basically create an empty dictionary */
985 code = pdfi_dict_alloc(ctx, 1, &ctx->pdfnativefontmap);
986 if (code < 0)
987 return code;
988 pdfi_countup(ctx->pdfnativefontmap);
989 return 0;
990 }
991
992 (void)pdfi_generate_platform_fontmap(ctx);
993
994 patrn = (char *)gs_alloc_bytes(ctx->memory, gp_file_name_sizeof, "pdfi_generate_native_fontmap");
995 result = (char *)gs_alloc_bytes(ctx->memory, gp_file_name_sizeof, "pdfi_generate_native_fontmap");
996 working = (char *)gs_alloc_bytes(ctx->memory, gp_file_name_sizeof, "pdfi_generate_native_fontmap");
997 if (patrn == NULL || result == NULL || working == NULL) {
998 gs_free_object(ctx->memory, patrn, "pdfi_generate_native_fontmap");
999 gs_free_object(ctx->memory, result, "pdfi_generate_native_fontmap");
1000 gs_free_object(ctx->memory, working, "pdfi_generate_native_fontmap");
1001 return_error(gs_error_VMerror);
1002 }
1003
1004 for (i = 0; i < ctx->search_paths.num_font_paths; i++) {
1005
1006 memcpy(patrn, ctx->search_paths.font_paths[i].data, ctx->search_paths.font_paths[i].size);
1007 memcpy(patrn + ctx->search_paths.font_paths[i].size, "/*", 2);
1008 patrn[ctx->search_paths.font_paths[i].size + 2] = '\0';
1009
1010 fe = gp_enumerate_files_init(ctx->memory, (const char *)patrn, strlen(patrn));
1011 while ((l = gp_enumerate_files_next(ctx->memory, fe, result, gp_file_name_sizeof - 1)) != ~(uint) 0) {
1012 result[l] = '\0';
1013
1014 code = pdfi_add_font_to_native_map(ctx, result, working);
1015
1016 /* We ignore most errors, on the basis it probably means it wasn't a valid font file */
1017 if (code == gs_error_VMerror)
1018 break;
1019 code = 0;
1020 }
1021 /* We only need to explicitly destroy the enumerator if we exit before enumeration is complete */
1022 if (code < 0)
1023 gp_enumerate_files_close(ctx->memory, fe);
1024 }
1025
1026 #ifdef DUMP_NATIVE_FONTMAP
1027 if (ctx->pdfnativefontmap != NULL) {
1028 uint64_t ind;
1029 int find = -1;
1030 pdf_name *key = NULL;
1031 pdf_obj *v = NULL;
1032 pdf_string *val = NULL;
1033 (void)pdfi_dict_key_first(ctx, ctx->pdfnativefontmap, (pdf_obj **) &key, &ind);
1034 (void)pdfi_dict_get_by_key(ctx, ctx->pdfnativefontmap, key, (pdf_obj **)&v);
1035 for (j = 0; j < key->length; j++)
1036 dprintf1("%c", key->data[j]);
1037 if (pdfi_type_of(v) == PDF_DICT) {
1038 pdf_num *n;
1039 pdf_string *val2;
1040 code = pdfi_dict_get(ctx, (pdf_dict *)v, "Index", (pdf_obj **)&n);
1041 if (code >= 0 && pdfi_type_of(n) == PDF_INT)
1042 find = n->value.i;
1043 else
1044 code = 0;
1045 (void)pdfi_dict_get(ctx, (pdf_dict *)v, "Path", (pdf_obj **)&val2);
1046 val = val2;
1047 }
1048 else {
1049 val = (pdf_string *)v;
1050 }
1051 dprintf(" ");
1052 for (j = 0; j < val->length; j++)
1053 dprintf1("%c", val->data[j]);
1054 if (find != -1) {
1055 dprintf1(" Index = %d", find);
1056 find = -1;
1057 }
1058
1059 dprintf("\n");
1060 pdfi_countdown(key);
1061 pdfi_countdown(val);
1062
1063 while (pdfi_dict_key_next(ctx, ctx->pdfnativefontmap, (pdf_obj **) &key, &ind) >= 0 && ind > 0) {
1064 (void)pdfi_dict_get_by_key(ctx, ctx->pdfnativefontmap, key, (pdf_obj **)&v);
1065 for (j = 0; j < key->length; j++)
1066 dprintf1("%c", key->data[j]);
1067 if (pdfi_type_of(v) == PDF_DICT) {
1068 pdf_num *n;
1069 pdf_string *val2;
1070 code = pdfi_dict_get(ctx, (pdf_dict *)v, "Index", (pdf_obj **)&n);
1071 if (code >= 0 && pdfi_type_of(n) == PDF_INT)
1072 find = n->value.i;
1073 else
1074 code = 0;
1075 (void)pdfi_dict_get(ctx, (pdf_dict *)v, "Path", (pdf_obj **)&val2);
1076 val = val2;
1077 }
1078 else {
1079 val = (pdf_string *)v;
1080 }
1081 dprintf(" ");
1082 for (j = 0; j < val->length; j++)
1083 dprintf1("%c", val->data[j]);
1084 if (find != -1) {
1085 dprintf1(" Index = %d", find);
1086 find = -1;
1087 }
1088 pdfi_countdown(key);
1089 pdfi_countdown(val);
1090 dprintf("\n");
1091 }
1092 }
1093 #endif
1094
1095 gs_free_object(ctx->memory, patrn, "pdfi_generate_native_fontmap");
1096 gs_free_object(ctx->memory, result, "pdfi_generate_native_fontmap");
1097 gs_free_object(ctx->memory, working, "pdfi_generate_native_fontmap");
1098 return 0;
1099 }
1100
1101 int
1102 pdfi_fontmap_lookup_font(pdf_context *ctx, pdf_dict *font_dict, pdf_name *fname, pdf_obj **mapname, int *findex)
1103 {
1104 int code;
1105 pdf_obj *mname;
1106
1107 *findex = 0;
1108
1109 if (ctx->pdffontmap == NULL) {
1110 const char *fmap_default = "Fontmap.GS";
1111 char *fmapname = (char *)fmap_default;
1112 code = pdf_make_fontmap(ctx, fmapname, false);
1113 if (code < 0) {
1114 return code;
1115 }
1116 }
1117 if (ctx->pdfnativefontmap == NULL) {
1118 code = pdfi_generate_native_fontmap(ctx);
1119 if (code < 0)
1120 return code;
1121 }
1122 code = pdfi_dict_get_by_key(ctx, ctx->pdffontmap, fname, &mname);
1123 if (code >= 0) {
1124 /* Fontmap can map in multiple "jump" i.e.
1125 name -> substitute name
1126 subsitute name -> file name
1127 So we want to loop until we no more hits.
1128 With "predefined fonts" in the fontmap, as in with UFST, we can
1129 have a an actual font, as well as a name or string (file name).
1130 */
1131 while(pdfi_type_of(mname) != PDF_FONT) {
1132 pdf_obj *mname2;
1133 code = pdfi_dict_get_by_key(ctx, ctx->pdffontmap, (pdf_name *)mname, &mname2);
1134 if (code < 0) {
1135 code = 0;
1136 break;
1137 }
1138 pdfi_countdown(mname);
1139 mname = mname2;
1140 }
1141 }
1142
1143 if (code < 0 && ctx->pdfnativefontmap != NULL) {
1144 pdf_obj *record;
1145 code = pdfi_dict_get_by_key(ctx, ctx->pdfnativefontmap, fname, &record);
1146 if (code >= 0) {
1147 if (pdfi_type_of(record) == PDF_STRING) {
1148 mname = record;
1149 }
1150 else if (pdfi_type_of(record) == PDF_DICT) {
1151 int64_t i64;
1152 code = pdfi_dict_get(ctx, (pdf_dict *)record, "Path", &mname);
1153 if (code >= 0)
1154 code = pdfi_dict_get_int(ctx, (pdf_dict *)record, "Index", &i64);
1155 if (code < 0) {
1156 pdfi_countdown(record);
1157 return code;
1158 }
1159 *findex = (int)i64; /* Rangecheck? */
1160 }
1161 else {
1162 pdfi_countdown(record);
1163 code = gs_error_undefined;
1164 }
1165 }
1166 }
1167 else {
1168 code = gs_error_undefined;
1169 }
1170
1171 if (mname != NULL && pdfi_type_of(mname) == PDF_STRING && pdfi_fmap_file_exists(ctx, (pdf_string *)mname)) {
1172 *mapname = mname;
1173 (void)pdfi_dict_put(ctx, font_dict, ".Path", mname);
1174 code = 0;
1175 }
1176 else if (mname != NULL && (pdfi_type_of(mname) == PDF_NAME || pdfi_type_of(mname) == PDF_FONT)) { /* If we map to a name, we assume (for now) we have the font as a "built-in" */
1177 *mapname = mname;
1178 code = 0;
1179 }
1180 else {
1181 pdfi_countdown(mname);
1182 code = gs_note_error(gs_error_undefined);
1183 }
1184 return code;
1185 }
1186
1187 int
1188 pdfi_fontmap_lookup_cidfont(pdf_context *ctx, pdf_dict *font_dict, pdf_name *name, pdf_obj **mapname, int *findex)
1189 {
1190 int code = 0;
1191 pdf_obj *cidname = NULL;
1192 pdf_obj *mname;
1193
1194 *findex = 0;
1195
1196 if (ctx->pdfcidfmap == NULL) {
1197 const char *cidfmap_default = "cidfmap";
1198 char *cidfmapname = (char *)cidfmap_default;
1199 code = pdf_make_fontmap(ctx, cidfmapname, true);
1200 if (code < 0) {
1201 return code;
1202 }
1203 }
1204 if (name == NULL || pdfi_type_of(name) != PDF_NAME) {
1205 code = pdfi_dict_get(ctx, font_dict, "BaseFont", &cidname);
1206 if (code < 0 || pdfi_type_of(cidname) != PDF_NAME) {
1207 pdfi_countdown(cidname);
1208 return_error(gs_error_undefined);
1209 }
1210
1211 }
1212 else {
1213 cidname = (pdf_obj *)name;
1214 pdfi_countup(cidname);
1215 }
1216
1217 code = pdfi_dict_get_by_key(ctx, ctx->pdfcidfmap, (pdf_name *)cidname, &mname);
1218 pdfi_countdown(cidname);
1219 if (code < 0)
1220 return code;
1221 /* As above cidfmap can map in multiple "jump" i.e.
1222 name -> substitute name
1223 subsitute name -> "record"
1224 So we want to loop until we no more hits.
1225 */
1226 while(1) {
1227 pdf_obj *mname2;
1228 code = pdfi_dict_get_by_key(ctx, ctx->pdfcidfmap, (pdf_name *)mname, &mname2);
1229 if (code < 0) break;
1230 pdfi_countdown(mname);
1231 mname = mname2;
1232 }
1233 if (pdfi_type_of(mname) == PDF_DICT) {
1234 pdf_dict *rec = (pdf_dict *)mname;
1235 pdf_name *filetype;
1236 pdf_name *path = NULL;
1237 int64_t i64;
1238
1239 code = pdfi_dict_get(ctx, rec, "FileType", (pdf_obj **)&filetype);
1240 /* We only handle TTF files, just now */
1241 if (code < 0 || pdfi_type_of(filetype) != PDF_NAME || filetype->length != 8 || memcmp(filetype->data, "TrueType", 8) != 0) {
1242 pdfi_countdown(filetype);
1243 pdfi_countdown(rec);
1244 return_error(gs_error_undefined);
1245 }
1246 pdfi_countdown(filetype);
1247
1248 code = pdfi_dict_get(ctx, rec, "Path", (pdf_obj **)&path);
1249 if (code < 0 || pdfi_type_of(path) != PDF_STRING || !pdfi_fmap_file_exists(ctx, (pdf_string *)path)) {
1250 pdfi_countdown(rec);
1251 return_error(gs_error_undefined);
1252 }
1253
1254 *mapname = (pdf_obj *)path;
1255 (void)pdfi_dict_put(ctx, font_dict, ".Path", (pdf_obj *)path);
1256 code = pdfi_dict_get_int(ctx, rec, "SubfontID", &i64);
1257 *findex = (code < 0) ? 0 : (int)i64; /* rangecheck? */
1258 code = 0;
1259
1260 }
1261 else {
1262 *mapname = (pdf_obj *)mname;
1263 code = 0;
1264 }
1265
1266 return code;
1267 }