Debian Codegraph @

pdf/pdf_fontTT.c

raw
1 /* Copyright (C) 2019-2026 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
16 /* code for TrueType font handling */
17
18 #include "pdf_int.h"
19 #include "pdf_font.h"
20 #include "pdf_fontTT.h"
21 #include "pdf_font1C.h" /* OTTO support */
22 #include "pdf_font_types.h"
23 #include "pdf_stack.h"
24 #include "pdf_file.h"
25 #include "pdf_dict.h"
26 #include "pdf_array.h"
27 #include "pdf_deref.h"
28 #include "gxfont42.h"
29 #include "gscencs.h"
30 #include "gsagl.h"
31 #include "gsutil.h" /* For gs_next_ids() */
32
33 enum {
34 CMAP_TABLE_NONE = 0,
35 CMAP_TABLE_10_PRESENT = 1,
36 CMAP_TABLE_30_PRESENT = 2,
37 CMAP_TABLE_31_PRESENT = 4,
38 CMAP_TABLE_310_PRESENT = 8
39 };
40
41 static int
42 pdfi_ttf_string_proc(gs_font_type42 * pfont, ulong offset, uint length, const byte ** pdata)
43 {
44 pdf_font_truetype *ttfont = (pdf_font_truetype *)pfont->client_data;
45 int code = 0;
46
47 if ((uint64_t)offset + length > ttfont->sfnt->length) {
48 *pdata = NULL;
49 code = gs_note_error(gs_error_invalidfont);
50 }
51 else {
52 *pdata = ttfont->sfnt->data + offset;
53 }
54 return code;
55 }
56
57 static gs_glyph pdfi_ttf_encode_char(gs_font *pfont, gs_char chr, gs_glyph_space_t sp)
58 {
59 pdf_font_truetype *ttfont = (pdf_font_truetype *)pfont->client_data;
60 gs_glyph g = GS_NO_GLYPH;
61 uint ID;
62 int code;
63
64 if ((ttfont->descflags & 4) != 0 || sp == GLYPH_SPACE_INDEX) {
65 int code = pdfi_fapi_check_cmap_for_GID(pfont, (uint)chr, &ID);
66 if (code < 0 || ID == 0)
67 code = pdfi_fapi_check_cmap_for_GID(pfont, (uint)(chr | 0xf0 << 8), &ID);
68 g = (gs_glyph)ID;
69 }
70 else {
71 pdf_context *ctx = (pdf_context *)ttfont->ctx;
72
73 if (ttfont->Encoding != NULL) { /* safety */
74 pdf_name *GlyphName = NULL;
75 code = pdfi_array_get(ctx, ttfont->Encoding, (uint64_t)chr, (pdf_obj **)&GlyphName);
76 if (code >= 0) {
77 code = (*ctx->get_glyph_index)(pfont, (byte *)GlyphName->data, GlyphName->length, &ID);
78 pdfi_countdown(GlyphName);
79 if (code >= 0)
80 g = (gs_glyph)ID;
81 }
82 }
83 }
84
85 return g;
86 }
87
88 int pdfi_find_post_entry(gs_font_type42 *pfont, gs_const_string *gname, uint *cc)
89 {
90 pdf_font_truetype *ttfont = (pdf_font_truetype *)pfont->client_data;
91 pdf_context *ctx = (pdf_context *)ttfont->ctx;
92 int code = 0;
93 if (ttfont->post != NULL) {
94 pdf_name *name = NULL;
95 pdf_num *n = NULL;
96
97 code = pdfi_name_alloc(ctx, (byte *)gname->data, gname->size, (pdf_obj **)&name);
98 if (code >= 0) {
99 pdfi_countup(name);
100 code = pdfi_dict_get_by_key(ctx, ttfont->post, name, (pdf_obj **)&n);
101 if (code >= 0 && pdfi_type_of(n) == PDF_INT) {
102 *cc = (uint)n->value.i;
103 }
104 else
105 *cc = 0;
106 pdfi_countdown(name);
107 pdfi_countdown(n);
108 }
109 }
110 else
111 code = gs_error_VMerror;
112
113 if (code == gs_error_VMerror){
114 uint i;
115 gs_string postname = {0};
116
117 code = gs_error_undefined;
118 if (pfont->data.numGlyphs > 0) { /* protect from corrupt font */
119 for (i = 0; i < pfont->data.numGlyphs; i++) {
120 code = gs_type42_find_post_name(pfont, (gs_glyph)i, &postname);
121 if (code >= 0) {
122 if (gname->data[0] == postname.data[0]
123 && gname->size == postname.size
124 && !strncmp((char *)gname->data, (char *)postname.data, postname.size))
125 {
126 *cc = i;
127 code = 0;
128 break;
129 }
130 }
131 }
132 }
133 }
134 return code;
135 }
136
137 extern single_glyph_list_t SingleGlyphList[];
138
139 static uint pdfi_type42_get_glyph_index(gs_font_type42 *pfont, gs_glyph glyph)
140 {
141 pdf_font_truetype *ttfont = (pdf_font_truetype *)pfont->client_data;
142 uint gind = 0;
143 uint cc = 0;
144 int i, code = 0;
145
146 if ((ttfont->descflags & 4) != 0 || glyph >= GS_MIN_GLYPH_INDEX) {
147 gind = (uint)glyph < GS_MIN_GLYPH_INDEX ? glyph : glyph - GS_MIN_GLYPH_INDEX;
148 }
149 else {
150 pdf_context *ctx = (pdf_context *)ttfont->ctx;
151 gs_const_string gname;
152
153 code = (*ctx->get_glyph_name)((gs_font *)pfont, glyph, &gname);
154 if (code < 0 || gname.data == NULL) {
155 return (uint)glyph;
156 }
157 if (gname.size == 7 && gname.data[0] == '.' && strncmp((char *)gname.data, ".notdef", 7) == 0) {
158 return 0; /* .notdef is GID 0, so short cut the rest of the function */
159 }
160
161 if (ttfont->cmap == pdfi_truetype_cmap_10) {
162 gs_glyph g;
163
164 g = gs_c_name_glyph((const byte *)gname.data, gname.size);
165 if (g != GS_NO_GLYPH) {
166 g = (gs_glyph)gs_c_decode(g, ENCODING_INDEX_MACROMAN);
167 }
168 else {
169 g = GS_NO_CHAR;
170 }
171
172 if (g != GS_NO_CHAR) {
173 code = pdfi_fapi_check_cmap_for_GID((gs_font *)pfont, (uint)g, &cc);
174 }
175
176 if (code < 0 || cc == 0) {
177 code = pdfi_find_post_entry(pfont, &gname, &cc);
178 if (code < 0) {
179 cc = 0;
180 code = 0;
181 }
182 }
183 }
184 else {
185 /* In theory, this should be 3,1 cmap, but we have examples that use 0,1 or other
186 Unicode "platform" cmap tables, so "hail mary" just try it
187 */
188 single_glyph_list_t *sgl = (single_glyph_list_t *)&(SingleGlyphList);
189 /* Not to spec, but... if we get a "uni..." formatted name, use
190 the hex value from that.
191 */
192 if (gname.size == 7 && !strncmp((char *)gname.data, "uni", 3)) {
193 char gnbuf[64];
194 int l = (gname.size - 3) > 63 ? 63 : gname.size - 3;
195
196 memcpy(gnbuf, gname.data + 3, l);
197 gnbuf[l] = '\0';
198 l = sscanf(gnbuf, "%x", &gind);
199 if (l > 0)
200 (void)pdfi_fapi_check_cmap_for_GID((gs_font *)pfont, (uint)gind, &cc);
201 else
202 cc = 0;
203 }
204 else {
205 /* Slow linear search */
206 for (i = 0; sgl->Glyph != 0x00; i++) {
207 if (sgl->Glyph[0] == gname.data[0]
208 && strlen(sgl->Glyph) == gname.size
209 && !strncmp((char *)sgl->Glyph, (char *)gname.data, gname.size))
210 break;
211 sgl++;
212 }
213 if (sgl->Glyph != NULL) {
214 code = pdfi_fapi_check_cmap_for_GID((gs_font *)pfont, (uint)sgl->Unicode, &cc);
215 if (code < 0 || cc == 0)
216 cc = 0;
217 }
218 else
219 cc = 0;
220
221 if (cc == 0) {
222 code = pdfi_find_post_entry(pfont, &gname, &cc);
223 if (code < 0) {
224 cc = (uint)glyph;
225 code = 0;
226 }
227 }
228 }
229 }
230 gind = cc;
231 }
232 return gind;
233 }
234
235 static int
236 pdfi_ttf_enumerate_glyph(gs_font *font, int *pindex, gs_glyph_space_t glyph_space, gs_glyph *pglyph)
237 {
238 if (glyph_space == GLYPH_SPACE_INDEX) {
239 return gs_type42_enumerate_glyph(font, pindex, glyph_space, pglyph);
240 }
241 else if (glyph_space == GLYPH_SPACE_NAME) {
242 pdf_font_truetype *ttfont = (pdf_font_truetype *)font->client_data;
243
244 if ((ttfont->descflags & 4) == 0) {
245 if (*pindex <= 0) {
246 *pindex = 0;
247 }
248 *pglyph = (*font->procs.encode_char)(font, (gs_char)*pindex, glyph_space);
249 if (*pglyph == GS_NO_GLYPH)
250 *pindex = 0;
251 else
252 (*pindex)++;
253 }
254 }
255 else
256 *pindex = 0;
257 return 0;
258 }
259
260 static int pdfi_ttf_glyph_name(gs_font *pfont, gs_glyph glyph, gs_const_string * pstr)
261 {
262 pdf_font_truetype *ttfont = (pdf_font_truetype *)pfont->client_data;
263 pdf_context *ctx = (pdf_context *)ttfont->ctx;
264 uint ID = 0;
265 int l, code = -1;
266
267 if (glyph >= GS_MIN_CID_GLYPH) { /* Fabricate a numeric name. */
268 char cid_name[sizeof(gs_glyph) * 3 + 1];
269
270 l = gs_snprintf(cid_name, sizeof(cid_name), "%lu", (ulong) glyph);
271 /* We need to create the entry in the name table... */
272 code = (*ctx->get_glyph_index)(pfont, (byte *)cid_name, l, &ID);
273 if (code < 0)
274 return -1;
275
276 code = (*ctx->get_glyph_name)(pfont, (gs_glyph)ID, pstr);
277 if (code < 0)
278 return -1; /* No name, trigger pdfwrite Type 3 fallback */
279 } else {
280 if ((ttfont->descflags & 4) != 0) {
281 code = gs_type42_find_post_name((gs_font_type42 *)pfont, glyph, (gs_string *)pstr);
282 if (code < 0) {
283 char buf[64];
284 l = gs_snprintf(buf, sizeof(buf), "~gs~gName~%04x", (uint)glyph);
285 code = (*ctx->get_glyph_index)(pfont, (byte *)buf, l, &ID);
286 }
287 else {
288 code = (*ctx->get_glyph_index)(pfont, (byte *)pstr->data, pstr->size, &ID);
289 }
290 if (code < 0)
291 return -1; /* No name, trigger pdfwrite Type 3 fallback */
292
293 code = (*ctx->get_glyph_name)(pfont, (gs_glyph)ID, pstr);
294 if (code < 0)
295 return -1; /* No name, trigger pdfwrite Type 3 fallback */
296 }
297 else {
298 code = (*ctx->get_glyph_name)(pfont, glyph, pstr);
299 if (code < 0)
300 return -1; /* No name, trigger pdfwrite Type 3 fallback */
301 }
302 }
303 return code;
304
305 }
306
307 static void pdfi_make_post_dict(gs_font_type42 *pfont)
308 {
309 pdf_font_truetype *ttfont = (pdf_font_truetype *)pfont->client_data;
310 pdf_context *ctx = (pdf_context *)ttfont->ctx;
311 int i, code = 0;
312 if (ttfont->post == NULL && pfont->data.numGlyphs > 0) {
313 code = pdfi_dict_alloc(ctx, pfont->data.numGlyphs, &ttfont->post);
314 if (code < 0)
315 return;
316
317 pdfi_countup(ttfont->post);
318
319 for (i = 0; i < pfont->data.numGlyphs; i++) {
320 gs_string postname = {0};
321 pdf_name *key;
322 pdf_num *ind;
323
324 code = gs_type42_find_post_name(pfont, (gs_glyph)i, &postname);
325 if (code < 0) {
326 continue;
327 }
328 code = pdfi_name_alloc(ctx, postname.data, postname.size, (pdf_obj **)&key);
329 if (code < 0) {
330 continue;
331 }
332 pdfi_countup(key);
333 code = pdfi_object_alloc(ctx, PDF_INT, 0, (pdf_obj **)&ind);
334 if (code < 0) {
335 pdfi_countdown(key);
336 continue;
337 }
338 pdfi_countup(ind);
339 ind->value.i = i;
340 (void)pdfi_dict_put_obj(ctx, ttfont->post, (pdf_obj *)key, (pdf_obj *)ind, false);
341 pdfi_countdown(key);
342 pdfi_countdown(ind);
343 }
344 }
345 }
346
347
348 static int
349 pdfi_alloc_tt_font(pdf_context *ctx, pdf_font_truetype **font, bool is_cid)
350 {
351 pdf_font_truetype *ttfont = NULL;
352 gs_font_type42 *pfont = NULL;
353
354 ttfont = (pdf_font_truetype *)gs_alloc_bytes(ctx->memory, sizeof(pdf_font_truetype), "pdfi (truetype pdf_font)");
355 if (ttfont == NULL)
356 return_error(gs_error_VMerror);
357
358 memset(ttfont, 0x00, sizeof(pdf_font_truetype));
359 ttfont->type = PDF_FONT;
360 ttfont->ctx = ctx;
361 ttfont->pdfi_font_type = e_pdf_font_truetype;
362
363 #if REFCNT_DEBUG
364 ttfont->UID = ctx->UID++;
365 outprintf(ctx->memory, "Allocated object of type %c with UID %"PRIi64"\n", ttfont->type, ttfont->UID);
366 #endif
367
368 pdfi_countup(ttfont);
369
370 pfont = (gs_font_type42 *)gs_alloc_struct(ctx->memory, gs_font_type42, &st_gs_font_type42,
371 "pdfi (truetype pfont)");
372 if (pfont == NULL) {
373 pdfi_countdown(ttfont);
374 return_error(gs_error_VMerror);
375 }
376 memset(pfont, 0x00, sizeof(gs_font_type42));
377
378 ttfont->pfont = (gs_font_base *)pfont;
379
380 gs_make_identity(&pfont->orig_FontMatrix);
381 gs_make_identity(&pfont->FontMatrix);
382 pfont->next = pfont->prev = 0;
383 pfont->memory = ctx->memory;
384 pfont->dir = ctx->font_dir;
385 pfont->is_resource = false;
386 gs_notify_init(&pfont->notify_list, ctx->memory);
387 pfont->base = (gs_font *) ttfont->pfont;
388 pfont->client_data = ttfont;
389 pfont->WMode = 0;
390 pfont->PaintType = 0;
391 pfont->StrokeWidth = 0;
392 pfont->is_cached = 0;
393 pfont->FAPI = NULL;
394 pfont->FAPI_font_data = NULL;
395 pfont->procs.init_fstack = gs_default_init_fstack;
396 pfont->procs.next_char_glyph = gs_default_next_char_glyph;
397 pfont->FontType = ft_TrueType;
398 pfont->ExactSize = fbit_use_outlines;
399 pfont->InBetweenSize = fbit_use_outlines;
400 pfont->TransformedChar = fbit_use_outlines;
401 /* We may want to do something clever with an XUID here */
402 pfont->id = gs_next_ids(ctx->memory, 1);
403 uid_set_UniqueID(&pfont->UID, pfont->id);
404 /* The buildchar proc will be filled in by FAPI -
405 we won't worry about working without FAPI */
406 pfont->procs.encode_char = pdfi_ttf_encode_char;
407 pfont->data.string_proc = pdfi_ttf_string_proc;
408 pfont->procs.glyph_name = pdfi_ttf_glyph_name;
409 pfont->procs.decode_glyph = pdfi_decode_glyph;
410 pfont->procs.define_font = gs_no_define_font;
411 pfont->procs.make_font = gs_no_make_font;
412
413 ttfont->default_font_info = gs_default_font_info;
414 pfont->procs.font_info = pdfi_default_font_info;
415
416 pfont->procs.glyph_info = gs_default_glyph_info;
417 pfont->procs.glyph_outline = gs_no_glyph_outline;
418 pfont->procs.build_char = NULL;
419 pfont->procs.same_font = gs_default_same_font;
420 pfont->procs.enumerate_glyph = gs_no_enumerate_glyph;
421
422 pfont->encoding_index = ENCODING_INDEX_UNKNOWN;
423 pfont->nearest_encoding_index = ENCODING_INDEX_UNKNOWN;
424
425 pfont->client_data = (void *)ttfont;
426
427 *font = ttfont;
428 return 0;
429 }
430
431 static void pdfi_set_type42_custom_procs(pdf_font_truetype *pdfttfont)
432 {
433 gs_font_type42 *pfont = (gs_font_type42 *)pdfttfont->pfont;
434
435 pdfttfont->default_font_info = pfont->procs.font_info;
436 pfont->procs.font_info = pdfi_default_font_info;
437 pfont->data.get_glyph_index = pdfi_type42_get_glyph_index;
438 pfont->procs.enumerate_glyph = pdfi_ttf_enumerate_glyph;
439 }
440
441 int pdfi_read_truetype_font(pdf_context *ctx, pdf_dict *font_dict, pdf_dict *stream_dict, pdf_dict *page_dict, byte *buf, int64_t buflen, int findex, pdf_font **ppdffont)
442 {
443 pdf_font_truetype *font = NULL;
444 int code = 0, i;
445 pdf_obj *fontdesc = NULL;
446 pdf_obj *obj = NULL;
447 pdf_obj *basefont = NULL;
448 int64_t descflags;
449 bool encoding_known = false;
450 bool forced_symbolic = false;
451 pdf_obj *tounicode = NULL;
452
453 if (ppdffont == NULL)
454 return_error(gs_error_invalidaccess);
455
456 *ppdffont = NULL;
457
458 if (font_dict != NULL)
459 (void)pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, &fontdesc);
460
461 if ((code = pdfi_alloc_tt_font(ctx, &font, false)) < 0) {
462 code = gs_note_error(gs_error_invalidfont);
463 goto error;
464 }
465 if (font_dict != NULL) {
466 font->object_num = font_dict->object_num;
467 font->generation_num = font_dict->generation_num;
468 font->indirect_num = font_dict->indirect_num;
469 font->indirect_gen = font_dict->indirect_gen;
470 }
471
472 font->FontDescriptor = (pdf_dict *)fontdesc;
473 fontdesc = NULL;
474
475 pdfi_font_set_first_last_char(ctx, font_dict, (pdf_font *)font);
476
477 code = pdfi_object_alloc(ctx, PDF_BUFFER, 0, (pdf_obj **)&font->sfnt);
478 if (code < 0) {
479 goto error;
480 }
481 pdfi_countup(font->sfnt);
482 code = pdfi_buffer_set_data((pdf_obj *)font->sfnt, buf, buflen);
483 if (code < 0) {
484 goto error;
485 }
486 buf = NULL;
487
488 /* Strictly speaking BaseFont is required, but we can continue without one */
489 if (font_dict != NULL) {
490 code = pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, (pdf_obj **)&basefont);
491 if (code > 0) {
492 pdf_name *nobj = (pdf_name *)basefont;
493 int nlen = nobj->length > gs_font_name_max ? gs_font_name_max : nobj->length;
494
495 memcpy(font->pfont->key_name.chars, nobj->data, nlen);
496 font->pfont->key_name.chars[nlen] = 0;
497 font->pfont->key_name.size = nlen;
498 memcpy(font->pfont->font_name.chars, nobj->data, nlen);
499 font->pfont->font_name.chars[nlen] = 0;
500 font->pfont->font_name.size = nlen;
501 pdfi_countdown(obj);
502 obj = NULL;
503 }
504 }
505 font->BaseFont = basefont;
506 basefont = NULL;
507 font->PDF_font = font_dict;
508 pdfi_countup(font_dict);
509
510 /* ignore errors with widths... for now */
511 if (font_dict != NULL)
512 (void)pdfi_font_create_widths(ctx, font_dict, (pdf_font*)font, 0.001);
513
514 if (ctx->args.ignoretounicode != true && font_dict != NULL) {
515 code = pdfi_dict_get(ctx, font_dict, "ToUnicode", (pdf_obj **)&tounicode);
516 if (code >= 0 && pdfi_type_of(tounicode) == PDF_STREAM) {
517 pdf_cmap *tu = NULL;
518 code = pdfi_read_cmap(ctx, tounicode, &tu);
519 pdfi_countdown(tounicode);
520 tounicode = (pdf_obj *)tu;
521 }
522 if (code < 0 || (tounicode != NULL && pdfi_type_of(tounicode) != PDF_CMAP)) {
523 pdfi_countdown(tounicode);
524 tounicode = NULL;
525 code = 0;
526 }
527 }
528 else {
529 tounicode = NULL;
530 }
531 font->ToUnicode = tounicode;
532 tounicode = NULL;
533
534 if (font->FontDescriptor != NULL) {
535 code = pdfi_dict_get_int(ctx, font->FontDescriptor, "Flags", &descflags);
536 if (code < 0)
537 descflags = 0;
538 }
539 else {
540 descflags = 0;
541 }
542
543 if (font_dict != NULL)
544 code = pdfi_dict_get(ctx, font_dict, "Encoding", &obj);
545 else
546 code = gs_error_undefined;
547
548 if (code < 0) {
549 static const char encstr[] = "WinAnsiEncoding";
550 code = pdfi_name_alloc(ctx, (byte *)encstr, strlen(encstr), (pdf_obj **)&obj);
551 if (code >= 0)
552 pdfi_countup(obj);
553 else
554 goto error;
555 }
556 else {
557 encoding_known = true;
558 /* If we have and encoding, and both the symbolic and non-symbolic flag are set,
559 believe that latter.
560 */
561 if ((descflags & 32) != 0)
562 descflags = (descflags & ~4);
563 }
564
565 if ((forced_symbolic = pdfi_font_known_symbolic(font->BaseFont)) == true) {
566 descflags |= 4;
567 }
568
569 code = pdfi_create_Encoding(ctx, (pdf_font *)font, obj, NULL, (pdf_obj **)&font->Encoding);
570 /* If we get an error, and the font is non-symbolic, return the error */
571 if (code < 0 && (descflags & 4) == 0)
572 goto error;
573 /* If we get an error, and the font is symbolic, pretend we never saw an /Encoding */
574 if (code < 0)
575 encoding_known = false;
576 pdfi_countdown(obj);
577 obj = NULL;
578
579 code = gs_type42_font_init((gs_font_type42 *)font->pfont, 0);
580 if (code < 0) {
581 goto error;
582 }
583
584 pdfi_set_type42_custom_procs(font);
585
586 /* We're probably dead in the water without cmap tables, but make sure, for safety */
587 /* This is a horrendous morass of guesses at what Acrobat does with files that contravene
588 what the spec says about symbolic fonts, cmap tables and encodings.
589 */
590 if (forced_symbolic != true && (descflags & 4) != 0 && ((gs_font_type42 *)font->pfont)->data.cmap != 0) {
591 gs_font_type42 *t42f = (gs_font_type42 *)font->pfont;
592 int numcmaps;
593 int cmaps_available = CMAP_TABLE_NONE;
594 const byte *d;
595 code = (*t42f->data.string_proc)(t42f, t42f->data.cmap + 2, 2, &d);
596 if (code < 0)
597 goto error;
598 numcmaps = d[1] | d[0] << 8;
599 for (i = 0; i < numcmaps; i++) {
600 code = (*t42f->data.string_proc)(t42f, t42f->data.cmap + 4 + i * 8, 4, &d);
601 if (code < 0)
602 goto error;
603 #define CMAP_PLAT_ENC_ID(a,b,c,d) (a << 24 | b << 16 | c << 8 | d)
604 switch(CMAP_PLAT_ENC_ID(d[0], d[1], d[2], d[3])) {
605 case CMAP_PLAT_ENC_ID(0, 1, 0, 0):
606 cmaps_available |= CMAP_TABLE_10_PRESENT;
607 break;
608 case CMAP_PLAT_ENC_ID(0, 3, 0, 0):
609 cmaps_available |= CMAP_TABLE_30_PRESENT;
610 break;
611 case CMAP_PLAT_ENC_ID(0, 3, 0, 1):
612 cmaps_available |= CMAP_TABLE_31_PRESENT;
613 break;
614 case CMAP_PLAT_ENC_ID(0, 3, 1, 0):
615 cmaps_available |= CMAP_TABLE_310_PRESENT;
616 break;
617 default: /* Not one we're interested in */
618 break;
619 }
620 }
621 #undef CMAP_PLAT_ENC_ID
622 if ((cmaps_available & CMAP_TABLE_30_PRESENT) == CMAP_TABLE_30_PRESENT) {
623 font->descflags = descflags;
624 }
625 else if (encoding_known == true) {
626 static const char mrencstr[] = "MacRomanEncoding";
627 static const char waencstr[] = "WinAnsiEncoding";
628 const char *encstr = ((cmaps_available & CMAP_TABLE_31_PRESENT) == CMAP_TABLE_31_PRESENT) ? waencstr : mrencstr;
629 font->descflags = descflags & ~4;
630 code = pdfi_name_alloc(ctx, (byte *)encstr, strlen(encstr), (pdf_obj **)&obj);
631 if (code >= 0)
632 pdfi_countup(obj);
633 else
634 goto error;
635 pdfi_countdown(font->Encoding);
636 code = pdfi_create_Encoding(ctx, (pdf_font *)font, obj, NULL, (pdf_obj **)&font->Encoding);
637 if (code < 0)
638 goto error;
639 pdfi_countdown(obj);
640 obj = NULL;
641 }
642 else
643 font->descflags = descflags;
644 }
645 else {
646 font->descflags = descflags;
647 }
648
649 if (uid_is_XUID(&font->pfont->UID))
650 uid_free(&font->pfont->UID, font->pfont->memory, "pdfi_read_type1_font");
651 uid_set_invalid(&font->pfont->UID);
652
653 code = pdfi_font_generate_pseudo_XUID(ctx, font_dict, font->pfont);
654 if (code < 0) {
655 goto error;
656 }
657
658 if ((font->descflags & 4) == 0) {
659 /* Horrid hacky solution */
660 /* We don't want to draw the TTF notdef */
661 gs_font_type42 *gst42 = ((gs_font_type42 *)font->pfont);
662 if (gst42->data.len_glyphs != NULL && gst42->data.len_glyphs[0] > 10) {
663 gst42->data.len_glyphs[0] = 0;
664 }
665 }
666
667 pdfi_make_post_dict((gs_font_type42 *)font->pfont);
668
669 pdfi_font_set_orig_fonttype(ctx, (pdf_font *)font);
670 code = gs_definefont(ctx->font_dir, (gs_font *)font->pfont);
671 if (code < 0) {
672 goto error;
673 }
674
675 code = pdfi_fapi_passfont((pdf_font *)font, 0, NULL, NULL, font->sfnt->data, font->sfnt->length);
676 if (code < 0) {
677 goto error;
678 }
679
680 /* object_num can be zero if the dictionary was defined inline */
681 if (font->object_num != 0) {
682 (void)replace_cache_entry(ctx, (pdf_obj *)font);
683 }
684
685 *ppdffont = (pdf_font *)font;
686 return code;
687 error:
688 pdfi_countdown(obj);
689 obj = NULL;
690 if (font_dict != NULL) {
691 if (pdfi_dict_get(ctx, font_dict, ".Path", &obj) >= 0)
692 {
693 char fname[gp_file_name_sizeof + 1];
694 pdf_string *fobj = (pdf_string *)obj;
695
696 memcpy(fname, fobj->data, fobj->length > gp_file_name_sizeof ? gp_file_name_sizeof : fobj->length);
697 fname[fobj->length > gp_file_name_sizeof ? gp_file_name_sizeof : fobj->length] = '\0';
698
699 (void)pdfi_set_error_var(ctx, code, NULL, E_PDF_BADSTREAM, "pdfi_read_truetype_font", "Error reading TrueType font file %s\n", fname);
700 }
701 else {
702 (void)pdfi_set_error_var(ctx, code, NULL, E_PDF_BADSTREAM, "pdfi_read_truetype_font", "Error reading embedded TrueType font object %u\n", font_dict->object_num);
703 }
704 }
705 else {
706 pdfi_set_error(ctx, code, NULL, E_PDF_BADSTREAM, "pdfi_read_truetype_font", "Error reading font\n");
707 }
708 if (buf != NULL)
709 gs_free_object(ctx->memory, buf, "pdfi_read_truetype_font(buf)");
710 pdfi_countdown(fontdesc);
711 pdfi_countdown(basefont);
712 pdfi_countdown(font);
713 return code;
714 }
715
716 int
717 pdfi_copy_truetype_font(pdf_context *ctx, pdf_font *spdffont, pdf_dict *font_dict, pdf_font **tpdffont)
718 {
719 int code = 0;
720 pdf_font_truetype *font = NULL;
721 gs_font_type42 *spfont1 = (gs_font_type42 *) spdffont->pfont;
722 gs_font_type42 *dpfont42;
723 gs_id t_id;
724 pdf_obj *tmp;
725
726 if (font_dict == NULL)
727 return_error(gs_error_invalidfont);
728
729 code = pdfi_alloc_tt_font(ctx, &font, font_dict->object_num);
730 if (code < 0)
731 return code;
732 dpfont42 = (gs_font_type42 *) font->pfont;
733
734 t_id = dpfont42->id;
735 memcpy(dpfont42, spfont1, sizeof(gs_font_type42));
736 dpfont42->id = t_id;
737 dpfont42->FAPI = NULL;
738 dpfont42->FAPI_font_data = NULL;
739 dpfont42->notify_list.memory = NULL;
740 dpfont42->notify_list.first = NULL;
741 gs_notify_init(&dpfont42->notify_list, dpfont42->memory);
742
743 memcpy(font, spdffont, sizeof(pdf_font_truetype));
744 font->refcnt = 1;
745 font->pfont = (gs_font_base *)dpfont42;
746 dpfont42->client_data = (void *)font;
747 font->filename = NULL;
748
749 pdfi_countup(font->post);
750 pdfi_countup(font->sfnt);
751 pdfi_countup(font->copyright);
752 pdfi_countup(font->notice);
753 pdfi_countup(font->fullname);
754 pdfi_countup(font->familyname);
755 font->Encoding = NULL;
756 font->ToUnicode = NULL;
757 font->Widths = NULL;
758
759 if (dpfont42->data.len_glyphs != NULL) {
760 dpfont42->data.len_glyphs = (uint *)gs_alloc_byte_array(dpfont42->memory, dpfont42->data.numGlyphs + 1, sizeof(uint), "pdfi_copy_truetype_font");
761 if (dpfont42->data.len_glyphs == NULL) {
762 pdfi_countdown(font);
763 return_error(gs_error_VMerror);
764 }
765 code = gs_font_notify_register((gs_font *)dpfont42, gs_len_glyphs_release, (void *)dpfont42);
766 if (code < 0) {
767 gs_free_object(dpfont42->memory, dpfont42->data.len_glyphs, "gs_len_glyphs_release");
768 dpfont42->data.len_glyphs = NULL;
769 pdfi_countdown(font);
770 return code;
771 }
772 memcpy(dpfont42->data.len_glyphs, spfont1->data.len_glyphs, (dpfont42->data.numGlyphs + 1) * sizeof(uint));
773 }
774 if (dpfont42->data.gsub != NULL) {
775 dpfont42->data.gsub_size = spfont1->data.gsub_size;
776
777 dpfont42->data.gsub = gs_alloc_byte_array(dpfont42->memory, dpfont42->data.gsub_size, 1, "pdfi_copy_truetype_font");
778 if (dpfont42->data.gsub == 0) {
779 pdfi_countdown(font);
780 return_error(gs_error_VMerror);
781 }
782
783 code = gs_font_notify_register((gs_font *)dpfont42, gs_gsub_release, (void *)dpfont42);
784 if (code < 0) {
785 gs_free_object(dpfont42->memory, dpfont42->data.gsub, "gs_len_glyphs_release");
786 dpfont42->data.gsub = NULL;
787 pdfi_countdown(font);
788 return code;
789 }
790 memcpy(dpfont42->data.gsub, spfont1->data.gsub, dpfont42->data.gsub_size);
791 }
792
793 font->PDF_font = font_dict;
794 font->object_num = font_dict->object_num;
795 font->generation_num = font_dict->generation_num;
796 pdfi_countup(font->PDF_font);
797
798 /* We want basefont and descriptor, but we can live without them */
799 font->BaseFont = NULL;
800 (void)pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, &font->BaseFont);
801 font->FontDescriptor = NULL;
802 (void)pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, (pdf_obj **)&font->FontDescriptor);
803
804 if (font->BaseFont != NULL && ((pdf_name *)font->BaseFont)->length <= gs_font_name_max) {
805 memcpy(dpfont42->key_name.chars, ((pdf_name *)font->BaseFont)->data, ((pdf_name *)font->BaseFont)->length);
806 dpfont42->key_name.size = ((pdf_name *)font->BaseFont)->length;
807 memcpy(dpfont42->font_name.chars, ((pdf_name *)font->BaseFont)->data, ((pdf_name *)font->BaseFont)->length);
808 dpfont42->font_name.size = ((pdf_name *)font->BaseFont)->length;
809 }
810
811 pdfi_font_set_first_last_char(ctx, font_dict, (pdf_font *)font);
812 (void)pdfi_font_create_widths(ctx, font_dict, (pdf_font*)font, (double)0.001);
813
814 font->descflags = 0;
815 if (font->FontDescriptor != NULL) {
816 code = pdfi_dict_get_int(ctx, font->FontDescriptor, "Flags", &font->descflags);
817 if (code >= 0) {
818 /* If both the symbolic and non-symbolic flag are set,
819 believe that latter.
820 */
821 if ((font->descflags & 32) != 0)
822 font->descflags = (font->descflags & ~4);
823 }
824 }
825
826 tmp = NULL;
827 code = pdfi_dict_knownget(ctx, font_dict, "Encoding", &tmp);
828 if (code == 1) {
829 if ((pdfi_type_of(tmp) == PDF_NAME || pdfi_type_of(tmp) == PDF_DICT) && (font->descflags & 4) == 0) {
830 code = pdfi_create_Encoding(ctx, (pdf_font *)font, tmp, NULL, (pdf_obj **) & font->Encoding);
831 }
832 else if (pdfi_type_of(tmp) == PDF_DICT && (font->descflags & 4) != 0) {
833 code = pdfi_create_Encoding(ctx, (pdf_font *)font, tmp, (pdf_obj *)spdffont->Encoding, (pdf_obj **) &font->Encoding);
834 }
835 else {
836 code = gs_error_undefined;
837 }
838 pdfi_countdown(tmp);
839 tmp = NULL;
840 }
841 else {
842 pdfi_countdown(tmp);
843 tmp = NULL;
844 code = gs_error_undefined;
845 }
846
847 if (code < 0) {
848 font->Encoding = spdffont->Encoding;
849 pdfi_countup(font->Encoding);
850 }
851
852 code = uid_copy(&font->pfont->UID, font->pfont->memory, "pdfi_copy_cff_font");
853 if (code < 0) {
854 uid_set_invalid(&font->pfont->UID);
855 }
856 if (spdffont->filename == NULL) {
857 code = pdfi_font_generate_pseudo_XUID(ctx, font_dict, font->pfont);
858 if (code < 0) {
859 goto error;
860 }
861 }
862 if (ctx->args.ignoretounicode != true) {
863 code = pdfi_dict_get(ctx, font_dict, "ToUnicode", (pdf_obj **)&tmp);
864 if (code >= 0 && pdfi_type_of(tmp) == PDF_STREAM) {
865 pdf_cmap *tu = NULL;
866 code = pdfi_read_cmap(ctx, tmp, &tu);
867 pdfi_countdown(tmp);
868 tmp = (pdf_obj *)tu;
869 }
870 if (code < 0 || (tmp != NULL && pdfi_type_of(tmp) != PDF_CMAP)) {
871 pdfi_countdown(tmp);
872 tmp = NULL;
873 code = 0;
874 }
875 }
876 else {
877 tmp = NULL;
878 }
879 font->ToUnicode = tmp;
880 pdfi_font_set_orig_fonttype(ctx, (pdf_font *)font);
881
882 code = gs_definefont(ctx->font_dir, (gs_font *) font->pfont);
883 if (code < 0) {
884 goto error;
885 }
886
887 code = pdfi_fapi_passfont((pdf_font *) font, 0, NULL, NULL, NULL, 0);
888 if (code < 0) {
889 goto error;
890 }
891 /* object_num can be zero if the dictionary was defined inline */
892 if (font->object_num != 0) {
893 (void)replace_cache_entry(ctx, (pdf_obj *) font);
894 }
895
896 *tpdffont = (pdf_font *)font;
897
898 error:
899 if (code < 0)
900 pdfi_countdown(font);
901 return code;
902 }
903
904 int pdfi_free_font_truetype(pdf_obj *font)
905 {
906 pdf_font_truetype *ttfont = (pdf_font_truetype *)font;
907
908 if (ttfont->pfont)
909 gs_free_object(OBJ_MEMORY(ttfont), ttfont->pfont, "Free TrueType gs_font");
910
911 if (ttfont->Widths)
912 gs_free_object(OBJ_MEMORY(ttfont), ttfont->Widths, "Free TrueType font Widths array");
913
914 pdfi_countdown(ttfont->sfnt);
915 pdfi_countdown(ttfont->FontDescriptor);
916 pdfi_countdown(ttfont->Encoding);
917 pdfi_countdown(ttfont->BaseFont);
918 pdfi_countdown(ttfont->PDF_font);
919 pdfi_countdown(ttfont->ToUnicode);
920 pdfi_countdown(ttfont->filename);
921 pdfi_countdown(ttfont->post);
922 pdfi_countdown(ttfont->copyright);
923 pdfi_countdown(ttfont->notice);
924 pdfi_countdown(ttfont->fullname);
925 pdfi_countdown(ttfont->familyname);
926
927 gs_free_object(OBJ_MEMORY(ttfont), ttfont, "Free TrueType font");
928
929 return 0;
930 }