Debian Codegraph @

pdf/pdf_font.c

raw
1 /* Copyright (C) 2018-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 /* Font operations for the PDF interpreter */
17
18 #include "pdf_int.h"
19 #include "pdf_font_types.h"
20 #include "pdf_gstate.h"
21 #include "pdf_file.h"
22 #include "pdf_dict.h"
23 #include "pdf_loop_detect.h"
24 #include "pdf_array.h"
25 #include "pdf_font.h"
26 #include "pdf_stack.h"
27 #include "pdf_misc.h"
28 #include "pdf_doc.h"
29 #include "pdf_font0.h"
30 #include "pdf_font1.h"
31 #include "pdf_font1C.h"
32 #include "pdf_font3.h"
33 #include "pdf_fontTT.h"
34 #include "pdf_fontmt.h"
35 #include "pdf_font0.h"
36 #include "pdf_fmap.h"
37 #include "gscencs.h" /* For gs_c_known_encode and gs_c_glyph_name */
38 #include "gsagl.h"
39
40 #include "strmio.h"
41 #include "stream.h"
42 #include "gsstate.h" /* For gs_setPDFfontsize() */
43
44 extern single_glyph_list_t SingleGlyphList[];
45
46 static int pdfi_gs_setfont(pdf_context *ctx, gs_font *pfont)
47 {
48 int code = 0;
49 pdfi_int_gstate *igs = (pdfi_int_gstate *)ctx->pgs->client_data;
50 pdf_font *old_font = igs->current_font;
51
52 code = gs_setfont(ctx->pgs, pfont);
53 if (code >= 0) {
54 igs->current_font = (pdf_font *)pfont->client_data;
55 pdfi_countup(igs->current_font);
56 pdfi_countdown(old_font);
57 }
58 return code;
59 }
60
61 static void pdfi_cache_resource_font(pdf_context *ctx, pdf_obj *fontdesc, pdf_obj *ppdffont)
62 {
63 resource_font_cache_t *entry = NULL;
64 int i;
65
66 if (ctx->resource_font_cache == NULL) {
67 ctx->resource_font_cache = (resource_font_cache_t *)gs_alloc_bytes(ctx->memory, RESOURCE_FONT_CACHE_BLOCK_SIZE * sizeof(pdfi_name_entry_t), "pdfi_cache_resource_font");
68 if (ctx->resource_font_cache == NULL)
69 return;
70 ctx->resource_font_cache_size = RESOURCE_FONT_CACHE_BLOCK_SIZE;
71 memset(ctx->resource_font_cache, 0x00, RESOURCE_FONT_CACHE_BLOCK_SIZE * sizeof(pdfi_name_entry_t));
72 entry = &ctx->resource_font_cache[0];
73 }
74
75 for (i = 0; entry == NULL && i < ctx->resource_font_cache_size; i++) {
76 if (ctx->resource_font_cache[i].pdffont == NULL) {
77 entry = &ctx->resource_font_cache[i];
78 }
79 else if (i == ctx->resource_font_cache_size - 1) {
80 entry = (resource_font_cache_t *)gs_resize_object(ctx->memory, ctx->resource_font_cache, sizeof(pdfi_name_entry_t) * (ctx->resource_font_cache_size + RESOURCE_FONT_CACHE_BLOCK_SIZE), "pdfi_cache_resource_font");
81 if (entry == NULL)
82 break;
83 memset(entry + ctx->resource_font_cache_size, 0x00, RESOURCE_FONT_CACHE_BLOCK_SIZE * sizeof(pdfi_name_entry_t));
84 ctx->resource_font_cache = entry;
85 entry = &ctx->resource_font_cache[ctx->resource_font_cache_size];
86 ctx->resource_font_cache_size += RESOURCE_FONT_CACHE_BLOCK_SIZE;
87 }
88 }
89 if (entry != NULL) {
90 entry->desc_obj_num = fontdesc->object_num;
91 entry->pdffont = ppdffont;
92 pdfi_countup(ppdffont);
93 }
94 }
95
96 void pdfi_purge_cache_resource_font(pdf_context *ctx)
97 {
98 int i;
99 for (i = 0; i < ctx->resource_font_cache_size; i++) {
100 pdfi_countdown(ctx->resource_font_cache[i].pdffont);
101 }
102 gs_free_object(ctx->memory, ctx->resource_font_cache, "pdfi_purge_cache_resource_font");
103 ctx->resource_font_cache = NULL;
104 ctx->resource_font_cache_size = 0;
105 }
106
107 static int pdfi_find_cache_resource_font(pdf_context *ctx, pdf_obj *fontdesc, pdf_obj **ppdffont)
108 {
109 int i, code = gs_error_undefined;
110 for (i = 0; i < ctx->resource_font_cache_size; i++) {
111 if (ctx->resource_font_cache[i].desc_obj_num == fontdesc->object_num) {
112 *ppdffont = ctx->resource_font_cache[i].pdffont;
113 pdfi_countup(*ppdffont);
114 code = 0;
115 break;
116 }
117 }
118 return code;
119 }
120
121 /* These are fonts for which we have to ignore "named" encodings */
122 typedef struct known_symbolic_font_name_s
123 {
124 const char *name;
125 const int namelen;
126 } known_symbolic_font_name_t;
127
128 #define DEFINE_NAME_LEN(s) #s, sizeof(#s) - 1
129 static const known_symbolic_font_name_t known_symbolic_font_names[] =
130 {
131 {DEFINE_NAME_LEN(Symbol)},
132 {DEFINE_NAME_LEN(Wingdings2)},
133 {DEFINE_NAME_LEN(Wingdings)},
134 {DEFINE_NAME_LEN(ZapfDingbats)},
135 {NULL , 0}
136 };
137 #undef DEFINE_NAME_LEN
138
139 bool pdfi_font_known_symbolic(pdf_obj *basefont)
140 {
141 bool ignore = false;
142 int i;
143 pdf_name *nm = (pdf_name *)basefont;
144
145 if (basefont != NULL && pdfi_type_of(basefont) == PDF_NAME) {
146 for (i = 0; known_symbolic_font_names[i].name != NULL; i++) {
147 if (nm->length == known_symbolic_font_names[i].namelen
148 && !strncmp((char *)nm->data, known_symbolic_font_names[i].name, nm->length)) {
149 ignore = true;
150 break;
151 }
152 }
153 }
154 return ignore;
155 }
156
157 static int
158 pdfi_font_match_glyph_widths(pdf_font *pdfont)
159 {
160 int code = 0;
161 int i;
162 int sindex, lindex;
163 gs_font_base *pbfont = pdfont->pfont;
164 double fw = 0.0, ww = 0.0;
165
166 if (pdfont->LastChar < pdfont->FirstChar || pdfont->Widths == NULL)
167 return 0; /* Technically invalid - carry on, hope for the best */
168
169 /* For "best" results, restrict to what we *hope* are A-Z,a-z */
170 sindex = pdfont->FirstChar < 96 ? 96 : pdfont->FirstChar;
171 lindex = pdfont->LastChar > 122 ? 123 : pdfont->LastChar + 1;
172
173 for (i = sindex; i < lindex; i++) {
174 gs_glyph_info_t ginfo = {0};
175 gs_glyph g;
176 g = pbfont->procs.encode_char((gs_font *)pbfont, i, GLYPH_SPACE_NAME);
177
178 /* We're only interested in non-zero Widths entries for glyphs that actually exist in the font */
179 if (g != GS_NO_GLYPH && pdfont->Widths[i - pdfont->FirstChar] != 0.0
180 && (*pbfont->procs.glyph_info)((gs_font *)pbfont, g, NULL, GLYPH_INFO_WIDTH0, &ginfo) >= 0) {
181 fw += hypot(ginfo.width[0].x, ginfo.width[0].y);
182 ww += pdfont->Widths[i - pdfont->FirstChar];
183 }
184 }
185 /* Only reduce font width, don't expand */
186 if (ww != 0.0 && fw != 0.0 && ww / fw < 1.0) {
187 gs_matrix nmat, smat = {1.0, 0.0, 0.0, 1.0, 0.0, 0.0};
188 double wscale;
189 smat.xx = smat.yy = ww/fw;
190 wscale = 1.0 / smat.xx;
191
192 gs_matrix_multiply(&pbfont->FontMatrix, &smat, &nmat);
193 memcpy(&pbfont->FontMatrix, &nmat, sizeof(pbfont->FontMatrix));
194
195 for (i = pdfont->FirstChar; i <= pdfont->LastChar; i++) {
196 pdfont->Widths[i - pdfont->FirstChar] *= wscale;
197 }
198
199 /* Purging a font can be expensive, but in this case, we know
200 we have no scaled instances (pdfi doesn't work that way)
201 and we know we have no fm pairs, nor glyphs to purge (we
202 *just* created the font!).
203 So "purging" the font is really just removing it from the
204 doubly linked list of font objects in the font directory
205 */
206 code = gs_purge_font((gs_font *)pbfont);
207 if (code >= 0)
208 code = gs_definefont(pbfont->dir, (gs_font *)pbfont);
209 if (code >= 0)
210 code = pdfi_fapi_passfont((pdf_font *)pdfont, 0, NULL, NULL, NULL, 0);
211 }
212
213 return code;
214 }
215
216 /* Print a name object to stdout */
217 static void pdfi_print_font_name(pdf_context *ctx, pdf_name *n)
218 {
219 if (ctx->args.QUIET != true)
220 (void)outwrite(ctx->memory, (const char *)n->data, n->length);
221 }
222
223 static void pdfi_print_font_string(pdf_context *ctx, pdf_string *s)
224 {
225 if (ctx->args.QUIET != true)
226 (void)outwrite(ctx->memory, (const char *)s->data, s->length);
227 }
228
229 /* Print a null terminated string to stdout */
230 static void pdfi_print_cstring(pdf_context *ctx, const char *str)
231 {
232 if (ctx->args.QUIET != true)
233 (void)outwrite(ctx->memory, str, strlen(str));
234 }
235
236 /* Call with a CIDFont name to try to find the CIDFont on disk
237 call if with ffname NULL to load the default fallback CIDFont
238 substitue
239 Currently only loads substitute - DroidSansFallback
240 */
241 static int
242 pdfi_open_CIDFont_substitute_file(pdf_context *ctx, pdf_dict *font_dict, pdf_dict *fontdesc, bool fallback, byte ** buf, int64_t * buflen, int *findex)
243 {
244 int code = 0;
245 char fontfname[gp_file_name_sizeof];
246 stream *s;
247 pdf_name *cidname = NULL;
248 gs_const_string fname;
249
250 (void)pdfi_dict_get(ctx, font_dict, "BaseFont", (pdf_obj **)&cidname);
251
252 if (fallback == true) {
253 pdf_string *mname = NULL;
254 pdf_dict *csi = NULL;
255 pdf_name *fbname;
256 const char *cidfbstr = "CIDFallBack";
257
258 code = pdfi_fontmap_lookup_cidfont(ctx, font_dict, NULL, (pdf_obj **)&mname, findex);
259
260 if (mname == NULL || pdfi_type_of(mname) != PDF_STRING) {
261 pdfi_countdown(mname);
262
263 code = pdfi_object_alloc(ctx, PDF_NAME, strlen(cidfbstr), (pdf_obj **)&fbname);
264 if (code >= 0) {
265 pdfi_countup(fbname);
266 memcpy(fbname->data, cidfbstr, strlen(cidfbstr));
267 code = pdfi_fontmap_lookup_cidfont(ctx, font_dict, fbname, (pdf_obj **)&mname, findex);
268 pdfi_countdown(fbname);
269 }
270
271 if (code < 0 || pdfi_type_of(mname) != PDF_STRING) {
272 pdfi_countdown(mname);
273 mname = NULL;
274 code = pdfi_dict_get(ctx, font_dict, "CIDSystemInfo", (pdf_obj **)&csi);
275 if (code >= 0 && pdfi_type_of(csi) == PDF_DICT) {
276 pdf_string *csi_reg = NULL, *csi_ord = NULL;
277
278 if (pdfi_dict_get(ctx, csi, "Registry", (pdf_obj **)&csi_reg) >= 0
279 && pdfi_dict_get(ctx, csi, "Ordering", (pdf_obj **)&csi_ord) >= 0
280 && pdfi_type_of(csi_reg) == PDF_STRING && pdfi_type_of(csi_ord) == PDF_STRING
281 && csi_reg->length + csi_ord->length + 1 < gp_file_name_sizeof - 1) {
282 pdf_name *reg_ord;
283 memcpy(fontfname, csi_reg->data, csi_reg->length);
284 memcpy(fontfname + csi_reg->length, "-", 1);
285 memcpy(fontfname + csi_reg->length + 1, csi_ord->data, csi_ord->length);
286 fontfname[csi_reg->length + csi_ord->length + 1] = '\0';
287
288 code = pdfi_name_alloc(ctx, (byte *)fontfname, strlen(fontfname), (pdf_obj **) &reg_ord);
289 if (code >= 0) {
290 pdfi_countup(reg_ord);
291 code = pdfi_fontmap_lookup_cidfont(ctx, font_dict, reg_ord, (pdf_obj **)&mname, findex);
292 pdfi_countdown(reg_ord);
293 }
294 }
295 pdfi_countdown(csi_reg);
296 pdfi_countdown(csi_ord);
297 }
298 pdfi_countdown(csi);
299 }
300
301 if (mname == NULL || pdfi_type_of(mname) != PDF_STRING) {
302 pdfi_countdown(mname);
303 mname = NULL;
304 code = pdfi_fontmap_lookup_cidfont(ctx, font_dict, NULL, (pdf_obj **)&mname, findex);
305 }
306 }
307
308 do {
309 if (code < 0 || pdfi_type_of(mname) != PDF_STRING) {
310 const char *fsprefix = "CIDFSubst/";
311 int fsprefixlen = strlen(fsprefix);
312 const char *defcidfallack = "DroidSansFallback.ttf";
313 int defcidfallacklen = strlen(defcidfallack);
314
315 pdfi_countdown(mname);
316
317 if (ctx->args.nocidfallback == true) {
318 if (ctx->args.pdfstoponerror == true)
319 code = gs_note_error(gs_error_Fatal);
320 else
321 code = gs_note_error(gs_error_invalidfont);
322 }
323 else {
324 if (ctx->args.cidfsubstpath.data == NULL) {
325 memcpy(fontfname, fsprefix, fsprefixlen);
326 }
327 else {
328 if (ctx->args.cidfsubstpath.size > gp_file_name_sizeof - 1) {
329 code = gs_note_error(gs_error_rangecheck);
330 if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_CONFIG, "pdfi_open_CIDFont_substitute_file", "CIDFSubstPath parameter too long")) < 0)
331 goto exit;
332 memcpy(fontfname, fsprefix, fsprefixlen);
333 }
334 else {
335 memcpy(fontfname, ctx->args.cidfsubstpath.data, ctx->args.cidfsubstpath.size);
336 fsprefixlen = ctx->args.cidfsubstpath.size;
337 }
338 }
339
340 if (ctx->args.cidfsubstfont.data == NULL) {
341 int len = 0;
342 if (gp_getenv("CIDFSUBSTFONT", (char *)0, &len) < 0) {
343 if (len + fsprefixlen + 1 > gp_file_name_sizeof) {
344 code = gs_note_error(gs_error_rangecheck);
345 if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_CONFIG, "pdfi_open_CIDFont_substitute_file", "CIDFSUBSTFONT environment variable too long")) < 0)
346 goto exit;
347
348 if (defcidfallacklen + fsprefixlen > gp_file_name_sizeof - 1) {
349 pdfi_set_warning(ctx, code, NULL, W_PDF_BAD_CONFIG, "pdfi_open_CIDFont_substitute_file", "CIDFSubstPath parameter too long");
350 memcpy(fontfname, defcidfallack, defcidfallacklen);
351 fsprefixlen = 0;
352 } else
353 memcpy(fontfname + fsprefixlen, defcidfallack, defcidfallacklen);
354 }
355 else {
356 (void)gp_getenv("CIDFSUBSTFONT", (char *)(fontfname + fsprefixlen), &defcidfallacklen);
357 }
358 }
359 else {
360 if (fsprefixlen + defcidfallacklen > gp_file_name_sizeof - 1) {
361 code = gs_note_error(gs_error_rangecheck);
362 if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_CONFIG, "pdfi_open_CIDFont_substitute_file", "CIDFSUBSTFONT environment variable too long")) < 0)
363 goto exit;
364
365 memcpy(fontfname, defcidfallack, defcidfallacklen);
366 /* cidfsubstfont should either be a font name we find in the search path(s)
367 or an absolute path.
368 */
369 fsprefixlen = 0;
370 }
371 else {
372 memcpy(fontfname + fsprefixlen, defcidfallack, defcidfallacklen);
373 }
374 }
375 }
376 else {
377 if (ctx->args.cidfsubstfont.size + fsprefixlen > gp_file_name_sizeof - 1) {
378 code = gs_note_error(gs_error_rangecheck);
379 if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_CONFIG, "pdfi_open_CIDFont_substitute_file", "CIDFSubstFont parameter too long")) < 0)
380 goto exit;
381
382 if (fsprefixlen + defcidfallacklen > gp_file_name_sizeof - 1) {
383 pdfi_set_warning(ctx, code, NULL, W_PDF_BAD_CONFIG, "pdfi_open_CIDFont_substitute_file", "CIDFSubstPath parameter too long");
384 memcpy(fontfname, defcidfallack, defcidfallacklen);
385 fsprefixlen = 0;
386 }
387 else
388 memcpy(fontfname + fsprefixlen, defcidfallack, defcidfallacklen);
389 }
390 else {
391 if (ctx->args.cidfsubstfont.size > gp_file_name_sizeof - 1) {
392 code = gs_note_error(gs_error_rangecheck);
393 if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_BAD_CONFIG, "pdfi_open_CIDFont_substitute_file", "CIDFSubstFont parameter too long")) < 0)
394 goto exit;
395
396 memcpy(fontfname, defcidfallack, defcidfallacklen);
397 defcidfallacklen = ctx->args.cidfsubstfont.size;
398 /* cidfsubstfont should either be a font name we find in the search path(s)
399 or an absolute path.
400 */
401 fsprefixlen = 0;
402 }
403 else {
404 memcpy(fontfname, ctx->args.cidfsubstfont.data, ctx->args.cidfsubstfont.size);
405 defcidfallacklen = ctx->args.cidfsubstfont.size;
406 /* cidfsubstfont should either be a font name we find in the search path(s)
407 or an absolute path.
408 */
409 fsprefixlen = 0;
410 }
411 }
412 }
413 fontfname[fsprefixlen + defcidfallacklen] = '\0';
414
415 code = pdfi_open_resource_file(ctx, fontfname, strlen(fontfname), &s);
416 if (code < 0) {
417 pdf_name *fn;
418 code = pdfi_object_alloc(ctx, PDF_NAME, strlen(fontfname), (pdf_obj **)&fn);
419 if (code < 0) {
420 code = gs_note_error(gs_error_invalidfont);
421 }
422 else {
423 pdfi_countup(fn);
424 memcpy(fn->data, fontfname, strlen(fontfname));
425 pdfi_countdown(mname);
426 mname = NULL;
427 code = pdfi_fontmap_lookup_cidfont(ctx, font_dict, fn, (pdf_obj **)&mname, findex);
428 pdfi_countdown(fn);
429 if (code < 0)
430 code = gs_note_error(gs_error_invalidfont);
431 else
432 continue;
433 }
434 }
435 else {
436 if (cidname) {
437 pdfi_print_cstring(ctx, "Loading CIDFont ");
438 pdfi_print_font_name(ctx, (pdf_name *)cidname);
439 pdfi_print_cstring(ctx, " substitute from ");
440 }
441 else {
442 pdfi_print_cstring(ctx, "Loading nameless CIDFont from ");
443 }
444 sfilename(s, &fname);
445 if (fname.size < gp_file_name_sizeof) {
446 memcpy(fontfname, fname.data, fname.size);
447 fontfname[fname.size] = '\0';
448 }
449 else {
450 strcpy(fontfname, "unnamed file");
451 }
452 pdfi_print_cstring(ctx, fontfname);
453 pdfi_print_cstring(ctx, "\n");
454
455
456 sfseek(s, 0, SEEK_END);
457 *buflen = sftell(s);
458 sfseek(s, 0, SEEK_SET);
459 *buf = gs_alloc_bytes(ctx->memory, *buflen, "pdfi_open_CIDFont_file(buf)");
460 if (*buf != NULL) {
461 sfread(*buf, 1, *buflen, s);
462 }
463 else {
464 code = gs_note_error(gs_error_VMerror);
465 }
466 sfclose(s);
467 break;
468 }
469 }
470 }
471 else {
472 if (mname->length + 1 > gp_file_name_sizeof) {
473 pdfi_countdown(mname);
474 return_error(gs_error_invalidfont);
475 }
476 memcpy(fontfname, mname->data, mname->length);
477 fontfname[mname->length] = '\0';
478 code = pdfi_open_resource_file(ctx, (const char *)fontfname, mname->length, &s);
479 pdfi_countdown(mname);
480 if (code < 0) {
481 code = gs_note_error(gs_error_invalidfont);
482 }
483 else {
484 if (cidname) {
485 pdfi_print_cstring(ctx, "Loading CIDFont ");
486 pdfi_print_font_name(ctx, (pdf_name *)cidname);
487 pdfi_print_cstring(ctx, " (or substitute) from ");
488 }
489 else {
490 pdfi_print_cstring(ctx, "Loading nameless CIDFont from ");
491 }
492 sfilename(s, &fname);
493 if (fname.size < gp_file_name_sizeof) {
494 memcpy(fontfname, fname.data, fname.size);
495 fontfname[fname.size] = '\0';
496 }
497 else {
498 strcpy(fontfname, "unnamed file");
499 }
500 pdfi_print_cstring(ctx, fontfname);
501 pdfi_print_cstring(ctx, "\n");
502 sfseek(s, 0, SEEK_END);
503 *buflen = sftell(s);
504 sfseek(s, 0, SEEK_SET);
505 *buf = gs_alloc_bytes(ctx->memory, *buflen, "pdfi_open_CIDFont_file(buf)");
506 if (*buf != NULL) {
507 sfread(*buf, 1, *buflen, s);
508 }
509 else {
510 code = gs_note_error(gs_error_VMerror);
511 }
512 sfclose(s);
513 break;
514 }
515 }
516 } while (code >= 0);
517 }
518 else {
519 const char *fsprefix = "CIDFont/";
520 const int fsprefixlen = strlen(fsprefix);
521
522 if (cidname == NULL || pdfi_type_of(cidname) != PDF_NAME
523 || fsprefixlen + cidname->length >= gp_file_name_sizeof) {
524 code = gs_note_error(gs_error_invalidfont);
525 goto exit;
526 }
527
528 memcpy(fontfname, fsprefix, fsprefixlen);
529 memcpy(fontfname + fsprefixlen, cidname->data, cidname->length);
530 fontfname[fsprefixlen + cidname->length] = '\0';
531
532 code = pdfi_open_resource_file(ctx, fontfname, strlen(fontfname), &s);
533 if (code < 0) {
534 code = gs_note_error(gs_error_invalidfont);
535 }
536 else {
537 sfseek(s, 0, SEEK_END);
538 *buflen = sftell(s);
539 sfseek(s, 0, SEEK_SET);
540 *buf = gs_alloc_bytes(ctx->memory, *buflen, "pdfi_open_CIDFont_file(buf)");
541 if (*buf != NULL) {
542 sfread(*buf, 1, *buflen, s);
543 }
544 else {
545 code = gs_note_error(gs_error_invalidfont);
546 }
547 sfclose(s);
548 }
549 }
550
551 exit:
552 if (cidname != NULL)
553 pdfi_countdown(cidname);
554
555 return code;
556 }
557
558 enum
559 {
560 pdfi_font_flag_none = 0x00000,
561 pdfi_font_flag_fixed = 0x00001,
562 pdfi_font_flag_serif = 0x00002,
563 pdfi_font_flag_symbolic = 0x00004,
564 pdfi_font_flag_script = 0x00008,
565 pdfi_font_flag_nonsymbolic = 0x00020,
566 pdfi_font_flag_italic = 0x00040,
567 pdfi_font_flag_allcap = 0x10000,
568 pdfi_font_flag_smallcap = 0x20000,
569 pdfi_font_flag_forcebold = 0x40000
570 };
571
572 /* Barefaced theft from mupdf! */
573 static const char *pdfi_base_font_names[][10] =
574 {
575 { "Courier", "CourierNew", "CourierNewPSMT", "CourierStd", NULL },
576 { "Courier-Bold", "CourierNew,Bold", "Courier,Bold", "CourierNewPS-BoldMT", "CourierNew-Bold", NULL },
577 { "Courier-Oblique", "CourierNew,Italic", "Courier,Italic", "CourierNewPS-ItalicMT", "CourierNew-Italic", NULL },
578 { "Courier-BoldOblique", "CourierNew,BoldItalic", "Courier,BoldItalic", "CourierNewPS-BoldItalicMT", "CourierNew-BoldItalic", NULL },
579 { "Helvetica", "ArialMT", "Arial", NULL },
580 { "Helvetica-Bold", "Arial-BoldMT", "Arial,Bold", "Arial-Bold", "Helvetica,Bold", NULL },
581 { "Helvetica-Oblique", "Arial-ItalicMT", "Arial,Italic", "Arial-Italic", "Helvetica,Italic", "Helvetica-Italic", NULL },
582 { "Helvetica-BoldOblique", "Arial-BoldItalicMT", "Arial,BoldItalic", "Arial-BoldItalic", "Helvetica,BoldItalic", "Helvetica-BoldItalic", NULL },
583 { "Times-Roman", "TimesNewRomanPSMT", "TimesNewRoman", "TimesNewRomanPS", NULL },
584 { "Times-Bold", "TimesNewRomanPS-BoldMT", "TimesNewRoman,Bold", "TimesNewRomanPS-Bold", "TimesNewRoman-Bold", NULL },
585 { "Times-Italic", "TimesNewRomanPS-ItalicMT", "TimesNewRoman,Italic", "TimesNewRomanPS-Italic", "TimesNewRoman-Italic", NULL },
586 { "Times-BoldItalic", "TimesNewRomanPS-BoldItalicMT", "TimesNewRoman,BoldItalic", "TimesNewRomanPS-BoldItalic", "TimesNewRoman-BoldItalic", NULL },
587 { "Symbol", "Symbol,Italic", "Symbol,Bold", "Symbol,BoldItalic", "SymbolMT", "SymbolMT,Italic", "SymbolMT,Bold", "SymbolMT,BoldItalic", NULL },
588 { "ZapfDingbats", NULL }
589 };
590
591 static int strncmp_ignore_space(const char *a, const char *b)
592 {
593 while (1)
594 {
595 while (*a == ' ')
596 a++;
597 while (*b == ' ')
598 b++;
599 if (*a != *b)
600 return 1;
601 if (*a == 0)
602 return *a != *b;
603 if (*b == 0)
604 return *a != *b;
605 a++;
606 b++;
607 }
608 return 0; /* Shouldn't happen */
609 }
610
611 static const char *pdfi_clean_font_name(const char *fontname)
612 {
613 int i, k;
614 for (i = 0; i < (sizeof(pdfi_base_font_names)/sizeof(pdfi_base_font_names[0])); i++) {
615 for (k = 0; pdfi_base_font_names[i][k]; k++) {
616 if (!strncmp_ignore_space(pdfi_base_font_names[i][k], (const char *)fontname))
617 return pdfi_base_font_names[i][0];
618 }
619 }
620 return NULL;
621 }
622
623 static int pdfi_font_substitute_by_flags(pdf_context *ctx, unsigned int flags, char **name, int *namelen)
624 {
625 bool fixed = ((flags & pdfi_font_flag_fixed) != 0);
626 bool serif = ((flags & pdfi_font_flag_serif) != 0);
627 bool italic = ((flags & pdfi_font_flag_italic) != 0);
628 bool bold = ((flags & pdfi_font_flag_forcebold) != 0);
629 int code = 0;
630
631 if (ctx->args.defaultfont_is_name == true && ctx->args.defaultfont.size == 4
632 && !memcmp(ctx->args.defaultfont.data, "None", 4)) {
633 *name = NULL;
634 *namelen = 0;
635 code = gs_error_invalidfont;
636 }
637 else if (ctx->args.defaultfont.data != NULL && ctx->args.defaultfont.size > 0) {
638 *name = (char *)ctx->args.defaultfont.data;
639 *namelen = ctx->args.defaultfont.size;
640 }
641 else if (fixed) {
642 if (bold) {
643 if (italic) {
644 *name = (char *)pdfi_base_font_names[3][0];
645 *namelen = strlen(*name);
646 }
647 else {
648 *name = (char *)pdfi_base_font_names[1][0];
649 *namelen = strlen(*name);
650 }
651 }
652 else {
653 if (italic) {
654 *name = (char *)pdfi_base_font_names[2][0];
655 *namelen = strlen(*name);
656 }
657 else {
658 *name = (char *)pdfi_base_font_names[0][0];
659 *namelen = strlen(*name);
660 }
661 }
662 }
663 else if (serif) {
664 if (bold) {
665 if (italic) {
666 *name = (char *)pdfi_base_font_names[11][0];
667 *namelen = strlen(*name);
668 }
669 else {
670 *name = (char *)pdfi_base_font_names[9][0];
671 *namelen = strlen(*name);
672 }
673 }
674 else {
675 if (italic) {
676 *name = (char *)pdfi_base_font_names[10][0];
677 *namelen = strlen(*name);
678 }
679 else {
680 *name = (char *)pdfi_base_font_names[8][0];
681 *namelen = strlen(*name);
682 }
683 }
684 } else {
685 if (bold) {
686 if (italic) {
687 *name = (char *)pdfi_base_font_names[7][0];
688 *namelen = strlen(*name);
689 }
690 else {
691 *name = (char *)pdfi_base_font_names[5][0];
692 *namelen = strlen(*name);
693 }
694 }
695 else {
696 if (italic) {
697 *name = (char *)pdfi_base_font_names[6][0];
698 *namelen = strlen(*name);
699 }
700 else {
701 *name = (char *)pdfi_base_font_names[4][0];
702 *namelen = strlen(*name);
703 }
704 }
705 }
706 return code;
707 }
708
709 enum {
710 no_type_font = -1,
711 type0_font = 0,
712 type1_font = 1,
713 cff_font = 2,
714 type3_font = 3,
715 tt_font = 42
716 };
717
718 static int pdfi_fonttype_picker(byte *buf, int64_t buflen)
719 {
720 #define MAKEMAGIC(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
721
722 if (buflen >= 4) {
723 if (MAKEMAGIC(buf[0], buf[1], buf[2], buf[3]) == MAKEMAGIC(0, 1, 0, 0)
724 || MAKEMAGIC(buf[0], buf[1], buf[2], buf[3]) == MAKEMAGIC('t', 'r', 'u', 'e')
725 || MAKEMAGIC(buf[0], buf[1], buf[2], buf[3]) == MAKEMAGIC('t', 't', 'c', 'f')) {
726 return tt_font;
727 }
728 else if (MAKEMAGIC(buf[0], buf[1], buf[2], buf[3]) == MAKEMAGIC('O', 'T', 'T', 'O')) {
729 return cff_font; /* OTTO will end up as CFF */
730 }
731 else if (MAKEMAGIC(buf[0], buf[1], buf[2], 0) == MAKEMAGIC('%', '!', 'P', 0)) {
732 return type1_font; /* pfa */
733 }
734 else if (MAKEMAGIC(buf[0], buf[1], buf[2], 0) == MAKEMAGIC(1, 0, 4, 0)) {
735 return cff_font; /* 1C/CFF */
736 }
737 else if (MAKEMAGIC(buf[0], buf[1], 0, 0) == MAKEMAGIC(128, 1, 0, 0)) {
738 return type1_font; /* pfb */
739 }
740 }
741 return no_type_font;
742 #undef MAKEMAGIC
743 }
744
745 static int pdfi_copy_font(pdf_context *ctx, pdf_font *spdffont, pdf_dict *font_dict, pdf_font **tpdffont)
746 {
747 int code;
748 if (pdfi_type_of(spdffont) != PDF_FONT)
749 return_error(gs_error_typecheck);
750
751 switch(spdffont->pdfi_font_type) {
752 case e_pdf_font_type1:
753 code = pdfi_copy_type1_font(ctx, spdffont, font_dict, tpdffont);
754 break;
755 case e_pdf_font_cff:
756 code = pdfi_copy_cff_font(ctx, spdffont, font_dict, tpdffont);
757 break;
758 case e_pdf_font_truetype:
759 code = pdfi_copy_truetype_font(ctx, spdffont, font_dict, tpdffont);
760 break;
761 case e_pdf_font_microtype:
762 code = pdfi_copy_microtype_font(ctx, spdffont, font_dict, tpdffont);
763 break;
764 default:
765 return_error(gs_error_invalidfont);
766 }
767 return code;
768 }
769
770 enum {
771 font_embedded = 0,
772 font_from_file = 1,
773 font_substitute = 2
774 };
775
776 static int pdfi_load_font_buffer(pdf_context *ctx, byte *fbuf, int64_t fbuflen, int fftype, pdf_name *Subtype, int findex, pdf_dict *stream_dict, pdf_dict *page_dict, pdf_dict *font_dict, pdf_font **ppdffont, bool cidfont)
777 {
778 int code = gs_error_invalidfont;
779 if (fbuf != NULL) {
780 /* First, see if we can glean the type from the magic number */
781 int sftype = pdfi_fonttype_picker(fbuf, fbuflen);
782 if (sftype == no_type_font) {
783 if (fftype != no_type_font)
784 sftype = fftype;
785 else {
786 /* If we don't have a Subtype, can't work it out, try Type 1 */
787 if (Subtype == NULL || pdfi_name_is(Subtype, "Type1") || pdfi_name_is(Subtype, "MMType1"))
788 sftype = type1_font;
789 else if (pdfi_name_is(Subtype, "Type1C"))
790 sftype = cff_font;
791 else if (pdfi_name_is(Subtype, "TrueType"))
792 sftype = tt_font;
793 }
794 }
795 /* fbuf ownership passes to the font loader */
796 switch (sftype) {
797 case type1_font:
798 code = pdfi_read_type1_font(ctx, (pdf_dict *)font_dict, stream_dict, page_dict, fbuf, fbuflen, ppdffont);
799 fbuf = NULL;
800 break;
801 case cff_font:
802 code = pdfi_read_cff_font(ctx, (pdf_dict *)font_dict, stream_dict, page_dict, fbuf, fbuflen, cidfont, ppdffont);
803 fbuf = NULL;
804 break;
805 case tt_font:
806 {
807 if (cidfont)
808 code = pdfi_read_cidtype2_font(ctx, font_dict, stream_dict, page_dict, fbuf, fbuflen, findex, ppdffont);
809 else
810 code = pdfi_read_truetype_font(ctx, font_dict, stream_dict, page_dict, fbuf, fbuflen, findex, ppdffont);
811 fbuf = NULL;
812 }
813 break;
814 default:
815 gs_free_object(ctx->memory, fbuf, "pdfi_load_font_buffer(fbuf)");
816 code = gs_note_error(gs_error_invalidfont);
817 }
818 }
819 return code;
820 }
821
822 static int pdfi_load_font_file(pdf_context *ctx, int fftype, pdf_name *Subtype, pdf_dict *stream_dict, pdf_dict *page_dict, pdf_dict *font_dict, pdf_dict *fontdesc, bool substitute, pdf_font **ppdffont)
823 {
824 int code;
825 char fontfname[gp_file_name_sizeof];
826 pdf_obj *basefont = NULL, *mapname = NULL;
827 pdf_obj *fontname = NULL;
828 stream *s = NULL;
829 const char *fn;
830 int findex = 0;
831 byte *buf;
832 int buflen;
833 pdf_font *pdffont = NULL;
834 pdf_font *substpdffont = NULL;
835 bool f_retry = true;
836
837 code = pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, &basefont);
838 if (substitute == false && (code < 0 || basefont == NULL || ((pdf_name *)basefont)->length == 0)) {
839 pdfi_countdown(basefont);
840 return_error(gs_error_invalidfont);
841 }
842
843 if (substitute == true) {
844 char *fbname;
845 int fbnamelen;
846 int64_t flags = 0;
847 if (fontdesc != NULL) {
848 (void)pdfi_dict_get_int(ctx, fontdesc, "Flags", &flags);
849 }
850 code = pdfi_font_substitute_by_flags(ctx, (int)flags, &fbname, &fbnamelen);
851 if (code < 0)
852 return code;
853
854 code = pdfi_name_alloc(ctx, (byte *)fbname, strlen(fbname), (pdf_obj **) &fontname);
855 if (code < 0)
856 return code;
857 pdfi_countup(fontname);
858 }
859 else {
860 fontname = basefont;
861 pdfi_countup(fontname);
862 }
863
864 if (((pdf_name *)fontname)->length < gp_file_name_sizeof) {
865 memcpy(fontfname, ((pdf_name *)fontname)->data, ((pdf_name *)fontname)->length);
866 fontfname[((pdf_name *)fontname)->length] = '\0';
867 pdfi_countdown(fontname);
868
869 code = pdfi_name_alloc(ctx, (byte *)fontfname, strlen(fontfname), (pdf_obj **) &fontname);
870 if (code < 0)
871 return code;
872 pdfi_countup(fontname);
873 }
874 else {
875 /* Just to ensure that fontfname is a valid, though empty, string */
876 fontfname[0] = '\0';
877 }
878
879 do {
880 if (f_retry == true && pdfi_font_file_exists(ctx, (const char *)((pdf_name *)fontname)->data, ((pdf_name *)fontname)->length) == true) {
881 code = 0;
882 }
883 else {
884 code = pdfi_fontmap_lookup_font(ctx, font_dict, (pdf_name *) fontname, &mapname, &findex);
885 if (code < 0) {
886 if (((pdf_name *)fontname)->length < gp_file_name_sizeof) {
887 memcpy(fontfname, ((pdf_name *)fontname)->data, ((pdf_name *)fontname)->length);
888 fontfname[((pdf_name *)fontname)->length] = '\0';
889 fn = pdfi_clean_font_name(fontfname);
890 if (fn != NULL) {
891 pdfi_countdown(fontname);
892
893 code = pdfi_name_alloc(ctx, (byte *)fn, strlen(fn), (pdf_obj **) &fontname);
894 if (code < 0)
895 return code;
896 pdfi_countup(fontname);
897 }
898 }
899 code = pdfi_fontmap_lookup_font(ctx, font_dict, (pdf_name *) fontname, &mapname, &findex);
900 if (code < 0) {
901 mapname = fontname;
902 pdfi_countup(mapname);
903 code = 0;
904 }
905 }
906 if (pdfi_type_of(mapname) == PDF_FONT) {
907 pdffont = (pdf_font *)mapname;
908 pdfi_countup(pdffont);
909 break;
910 }
911 if (pdfi_type_of(mapname) == PDF_NAME || pdfi_type_of(mapname) == PDF_STRING) {
912 pdf_name *mname = (pdf_name *) mapname;
913 if (mname->length + 1 < gp_file_name_sizeof) {
914 memcpy(fontfname, mname->data, mname->length);
915 fontfname[mname->length] = '\0';
916 }
917 else {
918 pdfi_countdown(mapname);
919 pdfi_countdown(fontname);
920 return_error(gs_error_invalidfileaccess);
921 }
922 }
923 else {
924 pdfi_countdown(mapname);
925 pdfi_countdown(fontname);
926 return_error(gs_error_invalidfileaccess);
927 }
928
929 if (ctx->pdf_substitute_fonts != NULL) {
930 code = pdfi_dict_knownget_type(ctx, ctx->pdf_substitute_fonts, fontfname, PDF_FONT, (pdf_obj **)&pdffont);
931 if (code == 1 && pdffont->filename == NULL) {
932 pdfi_countdown(pdffont);
933 pdffont = NULL;
934 code = 0;
935 }
936 }
937 else
938 code = 0;
939 }
940 if (code != 1) {
941 code = pdfi_open_font_file(ctx, fontfname, strlen(fontfname), &s);
942 if (code < 0 && f_retry && pdfi_type_of(mapname) == PDF_NAME) {
943 pdfi_countdown(fontname);
944 fontname = mapname;
945 mapname = NULL;
946 f_retry = false;
947 continue;
948 }
949 if (code >= 0) {
950 gs_const_string fname;
951
952 sfilename(s, &fname);
953 if (fname.size < gp_file_name_sizeof) {
954 memcpy(fontfname, fname.data, fname.size);
955 fontfname[fname.size] = '\0';
956 }
957 else {
958 strcpy(fontfname, "unnamed file");
959 }
960 sfseek(s, 0, SEEK_END);
961 buflen = sftell(s);
962 sfseek(s, 0, SEEK_SET);
963 buf = gs_alloc_bytes(ctx->memory, buflen, "pdfi_open_t1_font_file(buf)");
964 if (buf != NULL) {
965 sfread(buf, 1, buflen, s);
966 }
967 else {
968 code = gs_note_error(gs_error_VMerror);
969 }
970 sfclose(s);
971 /* Buffer owership moves to the font object */
972 code = pdfi_load_font_buffer(ctx, buf, buflen, no_type_font, NULL, findex, stream_dict, page_dict, NULL, &pdffont, false);
973 if (code >= 0) {
974 pdffont->filename = NULL;
975 code = pdfi_object_alloc(ctx, PDF_STRING, strlen(fontfname) , (pdf_obj **)&pdffont->filename);
976 if (code >= 0) {
977 pdfi_countup(pdffont->filename);
978 memcpy(pdffont->filename->data, fontfname, strlen(fontfname));
979 pdffont->filename->length = strlen(fontfname);
980 }
981
982 if (ctx->pdf_substitute_fonts == NULL) {
983 code = pdfi_dict_alloc(ctx, 16, &ctx->pdf_substitute_fonts);
984 if (code >= 0)
985 pdfi_countup(ctx->pdf_substitute_fonts);
986 }
987 if (ctx->pdf_substitute_fonts != NULL) {
988 if (pdfi_type_of(mapname) == PDF_STRING) {
989 pdf_name *n = NULL;
990 pdf_string *mn = (pdf_string *)mapname;
991
992 code = pdfi_name_alloc(ctx, mn->data, mn->length, (pdf_obj **)&n);
993 if (code >= 0) {
994 pdfi_countdown(mapname);
995 mapname = (pdf_obj *)n;
996 pdfi_countup(mapname);
997 code = 0;
998 }
999 }
1000 else
1001 code = 0;
1002
1003 if (code == 0)
1004 (void)pdfi_dict_put_obj(ctx, ctx->pdf_substitute_fonts, mapname, (pdf_obj *)pdffont, true);
1005 code = 0;
1006 }
1007 }
1008 }
1009 }
1010 break;
1011 } while (1);
1012
1013 if (code >= 0) {
1014 if (basefont) {
1015 pdfi_print_cstring(ctx, "Loading font ");
1016 pdfi_print_font_name(ctx, (pdf_name *)basefont);
1017 pdfi_print_cstring(ctx, " (or substitute) from ");
1018 }
1019 else {
1020 pdfi_print_cstring(ctx, "Loading nameless font from ");
1021 }
1022 pdfi_print_font_string(ctx, pdffont->filename);
1023 pdfi_print_cstring(ctx, "\n");
1024
1025 code = pdfi_copy_font(ctx, pdffont, font_dict, &substpdffont);
1026 pdfi_countdown(pdffont);
1027 }
1028
1029 *ppdffont = substpdffont;
1030
1031 pdfi_countdown(basefont);
1032 pdfi_countdown(mapname);
1033 pdfi_countdown(fontname);
1034 return code;
1035 }
1036
1037 int pdfi_load_font(pdf_context *ctx, pdf_dict *stream_dict, pdf_dict *page_dict, pdf_dict *font_dict, gs_font **ppfont, bool cidfont)
1038 {
1039 int code;
1040 pdf_font *ppdffont = NULL;
1041 pdf_font *ppdfdescfont = NULL;
1042 pdf_name *Type = NULL;
1043 pdf_name *Subtype = NULL;
1044 pdf_dict *fontdesc = NULL;
1045 pdf_stream *fontfile = NULL;
1046 pdf_name *ffsubtype = NULL;
1047 int fftype = no_type_font;
1048 byte *fbuf = NULL;
1049 int64_t fbuflen = 0;
1050 int substitute = font_embedded;
1051 int findex = 0;
1052
1053 code = pdfi_dict_get_type(ctx, font_dict, "Type", PDF_NAME, (pdf_obj **)&Type);
1054 if (code < 0) {
1055 if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_MISSINGTYPE, "pdfi_load_font", NULL)) < 0)
1056 goto exit;
1057 }
1058 else {
1059 if (!pdfi_name_is(Type, "Font")){
1060 code = gs_note_error(gs_error_typecheck);
1061 goto exit;
1062 }
1063 }
1064 code = pdfi_dict_get_type(ctx, font_dict, "Subtype", PDF_NAME, (pdf_obj **)&Subtype);
1065 if (code < 0) {
1066 if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_NO_SUBTYPE, "pdfi_load_font", NULL)) < 0)
1067 goto exit;
1068 }
1069
1070 /* Beyond Type 0 and Type 3, there is no point trusting the Subtype key */
1071 if (Subtype != NULL && pdfi_name_is(Subtype, "Type0")) {
1072 if (cidfont == true) {
1073 code = gs_note_error(gs_error_invalidfont);
1074 }
1075 else {
1076 code = pdfi_read_type0_font(ctx, (pdf_dict *)font_dict, stream_dict, page_dict, &ppdffont);
1077 }
1078 }
1079 else if (Subtype != NULL && pdfi_name_is(Subtype, "Type3")) {
1080 code = pdfi_read_type3_font(ctx, (pdf_dict *)font_dict, stream_dict, page_dict, &ppdffont);
1081 if (code < 0)
1082 goto exit;
1083 }
1084 else {
1085 if (Subtype != NULL && !pdfi_name_is(Subtype, "Type1") && !pdfi_name_is(Subtype, "TrueType") && !pdfi_name_is(Subtype, "CIDFont")
1086 && !pdfi_name_is(Subtype, "CIDFontType2") && !pdfi_name_is(Subtype, "CIDFontType0") && !pdfi_name_is(Subtype, "MMType1")) {
1087
1088 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_typecheck), NULL, E_PDF_BAD_SUBTYPE, "pdfi_load_font", NULL)) < 0) {
1089 goto exit;
1090 }
1091 }
1092 /* We should always have a font descriptor here, but we have to carry on
1093 even if we don't
1094 */
1095 code = pdfi_dict_get_type(ctx, font_dict, "FontDescriptor", PDF_DICT, (pdf_obj **)&fontdesc);
1096 if (fontdesc != NULL && pdfi_type_of(fontdesc) == PDF_DICT) {
1097 pdf_obj *Name = NULL;
1098
1099 code = pdfi_dict_get_type(ctx, (pdf_dict *) fontdesc, "FontName", PDF_NAME, (pdf_obj **)&Name);
1100 if (code < 0)
1101 pdfi_set_warning(ctx, 0, NULL, W_PDF_FDESC_BAD_FONTNAME, "pdfi_load_font", "");
1102 pdfi_countdown(Name);
1103 Name = NULL;
1104
1105 if (cidfont == true) {
1106 code = -1;
1107 }
1108 else {
1109 code = pdfi_find_cache_resource_font(ctx, (pdf_obj *)fontdesc, (pdf_obj **)&ppdfdescfont);
1110 if (code >= 0) {
1111 code = pdfi_copy_font(ctx, ppdfdescfont, font_dict, &ppdffont);
1112 }
1113 }
1114
1115 if (code < 0) {
1116 code = pdfi_dict_get_type(ctx, (pdf_dict *) fontdesc, "FontFile", PDF_STREAM, (pdf_obj **)&fontfile);
1117 if (code >= 0)
1118 fftype = type1_font;
1119 else {
1120 code = pdfi_dict_get_type(ctx, (pdf_dict *) fontdesc, "FontFile2", PDF_STREAM, (pdf_obj **)&fontfile);
1121 fftype = tt_font;
1122 }
1123 if (code < 0) {
1124 code = pdfi_dict_get_type(ctx, (pdf_dict *) fontdesc, "FontFile3", PDF_STREAM, (pdf_obj **)&fontfile);
1125 if (code >= 0 && fontfile != NULL) {
1126 code = pdfi_dict_get_type(ctx, fontfile->stream_dict, "Subtype", PDF_NAME, (pdf_obj **)&ffsubtype);
1127 if (code >= 0) {
1128 if (pdfi_name_is(ffsubtype, "Type1"))
1129 fftype = type1_font;
1130 else if (pdfi_name_is(ffsubtype, "Type1C"))
1131 fftype = cff_font;
1132 else if (pdfi_name_is(ffsubtype, "OpenType"))
1133 fftype = cff_font;
1134 else if (pdfi_name_is(ffsubtype, "CIDFontType0C"))
1135 fftype = cff_font;
1136 else if (pdfi_name_is(ffsubtype, "TrueType"))
1137 fftype = tt_font;
1138 else
1139 fftype = no_type_font;
1140 }
1141 }
1142 }
1143 }
1144 }
1145
1146 if (ppdffont == NULL) {
1147 if (fontfile != NULL) {
1148 code = pdfi_stream_to_buffer(ctx, (pdf_stream *) fontfile, &fbuf, &fbuflen);
1149 pdfi_countdown(fontfile);
1150 if (code < 0 || fbuflen == 0) {
1151 char obj[129];
1152 pdfi_print_cstring(ctx, "**** Warning: cannot process embedded stream for font object ");
1153 gs_snprintf(obj, 128, "%d %d\n", (int)font_dict->object_num, (int)font_dict->generation_num);
1154 pdfi_print_cstring(ctx, obj);
1155 pdfi_print_cstring(ctx, "**** Attempting to load a substitute font.\n");
1156 gs_free_object(ctx->memory, fbuf, "pdfi_load_font(fbuf)");
1157 fbuf = NULL;
1158 code = gs_note_error(gs_error_invalidfont);
1159 }
1160 }
1161
1162 while (1) {
1163 if (fbuf != NULL) {
1164 /* fbuf overship passes to pdfi_load_font_buffer() */
1165 code = pdfi_load_font_buffer(ctx, fbuf, fbuflen, fftype, Subtype, findex, stream_dict, page_dict, font_dict, &ppdffont, cidfont);
1166
1167 if (code < 0 && substitute == font_embedded) {
1168 char msg[129];
1169
1170 gs_snprintf(msg, 128, "Cannot process embedded stream for font object %d %d. Attempting to load a substitute font.\n", (int)font_dict->object_num, (int)font_dict->generation_num);
1171
1172 if ((code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_invalidfont), NULL, E_PDF_BAD_EMBEDDED_FONT, "pdfi_load_font", msg)) < 0) {
1173 goto exit;
1174 }
1175 else {
1176 code = gs_error_invalidfont;
1177 }
1178 }
1179 }
1180 else {
1181 code = gs_error_invalidfont;
1182 }
1183
1184 if (code < 0 && code != gs_error_VMerror && substitute == font_embedded) {
1185 substitute = font_from_file;
1186
1187 if (cidfont == true) {
1188 code = pdfi_open_CIDFont_substitute_file(ctx, font_dict, fontdesc, false, &fbuf, &fbuflen, &findex);
1189 if (code < 0) {
1190 code = pdfi_open_CIDFont_substitute_file(ctx, font_dict, fontdesc, true, &fbuf, &fbuflen, &findex);
1191 substitute |= font_substitute;
1192 }
1193
1194 if (code < 0)
1195 goto exit;
1196 }
1197 else {
1198 code = pdfi_load_font_file(ctx, no_type_font, Subtype, stream_dict, page_dict, font_dict, fontdesc, false, &ppdffont);
1199 if (code < 0) {
1200 code = pdfi_load_font_file(ctx, no_type_font, Subtype, stream_dict, page_dict, font_dict, fontdesc, true, &ppdffont);
1201 substitute |= font_substitute;
1202 }
1203 break;
1204 }
1205 continue;
1206 }
1207 break;
1208 }
1209 }
1210 }
1211 if (ppdffont == NULL || code < 0) {
1212 *ppfont = NULL;
1213 if (code != gs_error_Fatal)
1214 code = gs_note_error(gs_error_invalidfont);
1215 }
1216 else {
1217 ppdffont->substitute = (substitute != font_embedded);
1218
1219 if ((substitute & font_substitute) == font_substitute)
1220 code = pdfi_font_match_glyph_widths(ppdffont);
1221 else if (ppdffont->substitute != true && fontdesc != NULL && ppdfdescfont == NULL
1222 && (ppdffont->pdfi_font_type == e_pdf_font_type1 || ppdffont->pdfi_font_type == e_pdf_font_cff
1223 || ppdffont->pdfi_font_type == e_pdf_font_truetype)) {
1224 pdfi_cache_resource_font(ctx, (pdf_obj *)fontdesc, (pdf_obj *)ppdffont);
1225 }
1226 *ppfont = (gs_font *)ppdffont->pfont;
1227 }
1228
1229 exit:
1230 pdfi_countdown(ppdfdescfont);
1231 pdfi_countdown(fontdesc);
1232 pdfi_countdown(Type);
1233 pdfi_countdown(Subtype);
1234 pdfi_countdown(ffsubtype);
1235 return code;
1236 }
1237
1238 int pdfi_load_dict_font(pdf_context *ctx, pdf_dict *stream_dict, pdf_dict *page_dict, pdf_dict *font_dict, double point_size)
1239 {
1240 int code;
1241 gs_font *pfont;
1242 pdf_font *pdfif;
1243
1244 switch (pdfi_type_of(font_dict)) {
1245 case PDF_FONT:
1246 pdfi_countup(font_dict);
1247 pfont = (gs_font *)((pdf_font *)font_dict)->pfont;
1248 code = 0;
1249 break;
1250 case PDF_DICT:
1251 code = pdfi_load_font(ctx, stream_dict, page_dict, font_dict, &pfont, false);
1252 break;
1253 default:
1254 code = gs_note_error(gs_error_typecheck);
1255 goto exit;
1256 }
1257 if (code < 0)
1258 goto exit;
1259
1260 /* Everything looks good, set the font, unless it's the current font */
1261 if (pfont != ctx->pgs->font) {
1262 code = pdfi_gs_setfont(ctx, pfont);
1263 }
1264 pdfif = (pdf_font *)pfont->client_data;
1265 pdfi_countdown(pdfif);
1266
1267 if (code < 0)
1268 goto exit;
1269
1270 code = gs_setPDFfontsize(ctx->pgs, point_size);
1271 exit:
1272 return code;
1273 }
1274
1275 static int pdfi_load_resource_font(pdf_context *ctx, pdf_dict *stream_dict, pdf_dict *page_dict, pdf_name *fontname, double point_size)
1276 {
1277 int code;
1278 pdf_dict *font_dict = NULL;
1279
1280 if (pdfi_type_of(fontname) != PDF_NAME) {
1281 /* Passing empty string here should fall back to a default font */
1282 return pdfi_font_set_internal_string(ctx, "", point_size);
1283 }
1284
1285 /* Look fontname up in the resources */
1286 code = pdfi_loop_detector_mark(ctx);
1287 if (code < 0)
1288 goto exit;
1289 code = pdfi_find_resource(ctx, (unsigned char *)"Font", fontname, stream_dict, page_dict, (pdf_obj **)&font_dict);
1290 (void)pdfi_loop_detector_cleartomark(ctx);
1291 if (code < 0)
1292 goto exit;
1293 code = pdfi_load_dict_font(ctx, stream_dict, page_dict, font_dict, point_size);
1294
1295 exit:
1296 pdfi_countdown(font_dict);
1297 return code;
1298 }
1299
1300 int pdfi_get_cidfont_glyph_metrics(gs_font *pfont, gs_glyph cid, double *widths, bool vertical)
1301 {
1302 pdf_font *pdffont = (pdf_font *)pfont->client_data;
1303 int i, code = 0;
1304 pdf_num *c = NULL, *c2 = NULL;
1305 pdf_obj *o = NULL;
1306 pdf_array *W = NULL, *W2 = NULL, *DW2 = NULL;
1307 double DW;
1308
1309 if (pdffont->pdfi_font_type == e_pdf_cidfont_type0) {
1310 pdf_cidfont_type0 *cidfont = (pdf_cidfont_type0 *)pdffont;
1311 DW = cidfont->DW;
1312 DW2 = cidfont->DW2;
1313 W = cidfont->W;
1314 W2 = cidfont->W2;
1315 }
1316 else if (pdffont->pdfi_font_type == e_pdf_cidfont_type2) {
1317 pdf_cidfont_type2 *cidfont = (pdf_cidfont_type2 *)pdffont;
1318 DW = cidfont->DW;
1319 DW2 = cidfont->DW2;
1320 W = cidfont->W;
1321 W2 = cidfont->W2;
1322 }
1323 else {
1324 return_error(gs_error_invalidfont);
1325 }
1326
1327 widths[GLYPH_W0_WIDTH_INDEX] = DW;
1328 widths[GLYPH_W0_HEIGHT_INDEX] = 0;
1329 if (W != NULL) {
1330 i = 0;
1331
1332 while(1) {
1333 pdf_obj_type type;
1334
1335 if (i + 1>= W->size) break;
1336 code = pdfi_array_get_type(pdffont->ctx, W, i, PDF_INT, (pdf_obj **)&c);
1337 if (code < 0) goto cleanup;
1338
1339 code = pdfi_array_get(pdffont->ctx, W, i + 1, &o);
1340 if (code < 0) goto cleanup;
1341
1342 type = pdfi_type_of(o);
1343 if (type == PDF_INT) {
1344 double d;
1345 c2 = (pdf_num *)o;
1346 o = NULL;
1347 if (i + 2 >= W->size){
1348 /* We countdown and NULL c, c2 and o after exit from the loop
1349 * in order to avoid doing so in the break statements
1350 */
1351 break;
1352 }
1353
1354 code = pdfi_array_get_number(pdffont->ctx, W, i + 2, &d);
1355 if (code < 0) goto cleanup;
1356 if (cid >= c->value.i && cid <= c2->value.i) {
1357 widths[GLYPH_W0_WIDTH_INDEX] = d;
1358 widths[GLYPH_W0_HEIGHT_INDEX] = 0.0;
1359 /* We countdown and NULL c, c2 and o after exit from the loop
1360 * in order to avoid doing so in the break statements
1361 */
1362 break;
1363 }
1364 else {
1365 i += 3;
1366 pdfi_countdown(c2);
1367 pdfi_countdown(c);
1368 c = c2 = NULL;
1369 continue;
1370 }
1371 }
1372 else if (type == PDF_ARRAY) {
1373 pdf_array *a = (pdf_array *)o;
1374 o = NULL;
1375 if (cid >= c->value.i && cid < c->value.i + a->size) {
1376 code = pdfi_array_get_number(pdffont->ctx, a, cid - c->value.i, &widths[GLYPH_W0_WIDTH_INDEX]);
1377 if (code >= 0) {
1378 pdfi_countdown(a);
1379 widths[GLYPH_W0_HEIGHT_INDEX] = 0.0;
1380 /* We countdown and NULL c, c2 and o on exit from the loop
1381 * in order to avoid doing so in the break statements
1382 */
1383 break;
1384 }
1385 }
1386 pdfi_countdown(a);
1387 pdfi_countdown(c);
1388 c = NULL;
1389 i += 2;
1390 continue;
1391 }
1392 else {
1393 code = gs_note_error(gs_error_typecheck);
1394 goto cleanup;
1395 }
1396 }
1397 pdfi_countdown(c2);
1398 pdfi_countdown(c);
1399 pdfi_countdown(o);
1400 c = c2 = NULL;
1401 o = NULL;
1402 }
1403
1404 if (vertical) {
1405 /* Default default <sigh>! */
1406 widths[GLYPH_W1_WIDTH_INDEX] = 0;
1407 widths[GLYPH_W1_HEIGHT_INDEX] = -1000.0;
1408 widths[GLYPH_W1_V_X_INDEX] = (widths[GLYPH_W0_WIDTH_INDEX] / 2.0);
1409 widths[GLYPH_W1_V_Y_INDEX] = 880.0;
1410
1411 if (DW2 != NULL && pdfi_type_of(DW2) == PDF_ARRAY
1412 && DW2->size >= 2) {
1413 code = pdfi_array_get_number(pdffont->ctx, (pdf_array *)DW2, 0, &widths[GLYPH_W1_V_Y_INDEX]);
1414 if (code >= 0)
1415 code = pdfi_array_get_number(pdffont->ctx, (pdf_array *)DW2, 1, &widths[GLYPH_W1_HEIGHT_INDEX]);
1416 if (code >= 0) {
1417 widths[GLYPH_W1_V_X_INDEX] = widths[GLYPH_W0_WIDTH_INDEX] / 2.0;
1418 widths[GLYPH_W1_WIDTH_INDEX] = 0.0;
1419 }
1420 }
1421 if (W2 != NULL && pdfi_type_of(W2) == PDF_ARRAY) {
1422 i = 0;
1423 while(1) {
1424 pdf_obj_type type;
1425 if (i + 1 >= W2->size) break;
1426 (void)pdfi_array_get(pdffont->ctx, W2, i, (pdf_obj **)&c);
1427 if (pdfi_type_of(c) != PDF_INT) {
1428 code = gs_note_error(gs_error_typecheck);
1429 goto cleanup;
1430 }
1431 code = pdfi_array_get(pdffont->ctx, W2, i + 1, (pdf_obj **)&o);
1432 if (code < 0) goto cleanup;
1433 type = pdfi_type_of(o);
1434 if (type == PDF_INT) {
1435 if (cid >= c->value.i && cid <= ((pdf_num *)o)->value.i) {
1436 if (i + 4 >= W2->size) {
1437 /* We countdown and NULL c, and o on exit from the function
1438 * so we don't need to do so in the break statements
1439 */
1440 break;
1441 }
1442 code = pdfi_array_get_number(pdffont->ctx, W2, i + 2, &widths[GLYPH_W1_HEIGHT_INDEX]);
1443 if (code < 0) goto cleanup;
1444 code = pdfi_array_get_number(pdffont->ctx, W2, i + 3, &widths[GLYPH_W1_V_X_INDEX]);
1445 if (code < 0) goto cleanup;
1446 code = pdfi_array_get_number(pdffont->ctx, W2, i + 4, &widths[GLYPH_W1_V_Y_INDEX]);
1447 if (code < 0) goto cleanup;
1448 /* We countdown and NULL c, and o on exit from the function
1449 * so we don't need to do so in the break statements
1450 */
1451 break;
1452 }
1453 i += 5;
1454 }
1455 else if (type == PDF_ARRAY) {
1456 pdf_array *a = (pdf_array *)o;
1457 int l = a->size - (a->size % 3);
1458 o = NULL;
1459 if (cid >= c->value.i && cid < c->value.i + (l / 3)) {
1460 int index = (cid - c->value.i) * 3;
1461 code = pdfi_array_get_number(pdffont->ctx, a, index, &widths[GLYPH_W1_HEIGHT_INDEX]);
1462 if (code < 0) {
1463 pdfi_countdown(a);
1464 goto cleanup;
1465 }
1466 code = pdfi_array_get_number(pdffont->ctx, a, index + 1, &widths[GLYPH_W1_V_X_INDEX]);
1467 if (code < 0) {
1468 pdfi_countdown(a);
1469 goto cleanup;
1470 }
1471 code = pdfi_array_get_number(pdffont->ctx, a, index + 2, &widths[GLYPH_W1_V_Y_INDEX]);
1472 pdfi_countdown(a);
1473 if (code < 0) goto cleanup;
1474
1475 /* We countdown and NULL c, and o on exit from the function
1476 * so we don't need to do so in the break statements
1477 */
1478 break;
1479 } else
1480 pdfi_countdown(a);
1481 i += 2;
1482 }
1483 else {
1484 code = gs_note_error(gs_error_typecheck);
1485 goto cleanup;
1486 }
1487 pdfi_countdown(o);
1488 pdfi_countdown(c);
1489 o = NULL;
1490 c = NULL;
1491 }
1492 }
1493 }
1494
1495 cleanup:
1496 pdfi_countdown(c2);
1497 pdfi_countdown(c);
1498 pdfi_countdown(o);
1499
1500 return code;
1501 }
1502
1503 int pdfi_d0(pdf_context *ctx)
1504 {
1505 int code = 0, gsave_level = 0;
1506 double width[2];
1507
1508 if (ctx->text.inside_CharProc == false)
1509 pdfi_set_warning(ctx, 0, NULL, W_PDF_NOTINCHARPROC, "pdfi_d0", NULL);
1510
1511 ctx->text.CharProc_d_type = pdf_type3_d0;
1512
1513 if (pdfi_count_stack(ctx) < 2) {
1514 code = gs_note_error(gs_error_stackunderflow);
1515 goto d0_error;
1516 }
1517
1518 if (pdfi_type_of(ctx->stack_top[-1]) != PDF_INT && pdfi_type_of(ctx->stack_top[-1]) != PDF_REAL) {
1519 code = gs_note_error(gs_error_typecheck);
1520 goto d0_error;
1521 }
1522 if (pdfi_type_of(ctx->stack_top[-2]) != PDF_INT && pdfi_type_of(ctx->stack_top[-2]) != PDF_REAL) {
1523 code = gs_note_error(gs_error_typecheck);
1524 goto d0_error;
1525 }
1526 if(ctx->text.current_enum == NULL) {
1527 code = gs_note_error(gs_error_undefined);
1528 goto d0_error;
1529 }
1530
1531 if (pdfi_type_of(ctx->stack_top[-2]) == PDF_INT)
1532 width[0] = (double)((pdf_num *)ctx->stack_top[-2])->value.i;
1533 else
1534 width[0] = ((pdf_num *)ctx->stack_top[-2])->value.d;
1535 if (pdfi_type_of(ctx->stack_top[-1]) == PDF_INT)
1536 width[1] = (double)((pdf_num *)ctx->stack_top[-1])->value.i;
1537 else
1538 width[1] = ((pdf_num *)ctx->stack_top[-1])->value.d;
1539
1540 gsave_level = ctx->pgs->level;
1541
1542 /*
1543 * We don't intend to retain this, instead we will use (effectively) xyshow to apply
1544 * width overrides at the text level.
1545 if (font && font->Widths && ctx->current_chr >= font->FirstChar && ctx->current_chr <= font->LastChar)
1546 width[0] = font->Widths[font->ctx->current_chr - font->FirstChar];
1547 */
1548
1549 if (ctx->text.current_enum == NULL) {
1550 code = gs_note_error(gs_error_unknownerror);
1551 goto d0_error;
1552 }
1553
1554 code = gs_text_setcharwidth(ctx->text.current_enum, width);
1555
1556 /* Nasty hackery. setcachedevice potentially pushes a new device into the graphics state
1557 * and there's no way to remove that device again without grestore'ing back to a point
1558 * before the device was loaded. To facilitate this, setcachedevice will do a gs_gsave()
1559 * before changing the device. Note, the grestore for this is done back in show_update()
1560 * which is not reached until after the CharProc has been executed.
1561 *
1562 * This is a problem for us when running a PDF content stream, because after running the
1563 * stream we check the gsave level and, if its not the same as it was when we started
1564 * the stream, we pdfi_grestore() back until it is. This mismatch of the gsave levels
1565 * causes all sorts of trouble with the font and we can end up counting the pdf_font
1566 * object down and discarding the font we're tryign to use.
1567 *
1568 * The solution (ugly though it is) is to patch up the saved gsave_level in the
1569 * context to expect that we have one more gsave level on exit. That wasy we won't
1570 * try and pdf_grestore() back to an earlier point.
1571 */
1572 if (ctx->pgs->level > gsave_level)
1573 ctx->current_stream_save.gsave_level += ctx->pgs->level - gsave_level;
1574
1575 if (code < 0)
1576 goto d0_error;
1577 pdfi_pop(ctx, 2);
1578 return 0;
1579
1580 d0_error:
1581 pdfi_clearstack(ctx);
1582 return code;
1583 }
1584
1585 int pdfi_d1(pdf_context *ctx)
1586 {
1587 int code = 0, gsave_level;
1588 double wbox[6];
1589
1590 if (ctx->text.inside_CharProc == false)
1591 pdfi_set_warning(ctx, 0, NULL, W_PDF_NOTINCHARPROC, "pdfi_d1", NULL);
1592
1593 ctx->text.CharProc_d_type = pdf_type3_d1;
1594
1595 code = pdfi_destack_reals(ctx, wbox, 6);
1596 if (code < 0)
1597 goto d1_error;
1598
1599 /*
1600 * We don't intend to retain this, instead we will use (effectively) xyshow to apply
1601 * width overrides at the text level.
1602 if (font && font->Widths && ctx->current_chr >= font->FirstChar && ctx->current_chr <= font->LastChar)
1603 wbox[0] = font->Widths[font->ctx->current_chr - font->FirstChar];
1604 */
1605
1606 gsave_level = ctx->pgs->level;
1607
1608 if (ctx->text.current_enum == NULL) {
1609 code = gs_note_error(gs_error_unknownerror);
1610 goto d1_error;
1611 }
1612
1613 #if 0
1614 /* This code stops us caching the bitmap and filling it. This is required if the 'pdf-differences'
1615 * test Type3WordSpacing/Type3Test.pdf is to be rendered with a red stroke. Currently the cache
1616 * results in a bitmap which is filled with the current fill colour, stroke colours are not
1617 * implemented. I believe that, since the spec references the PLRM implementation of 'd1' that
1618 * this is correct. I further suspect that the authors of the test used viewers which do not implement
1619 * a cache at all, and redraw the glyph every time it is used. This has implications for other parts
1620 * of the graphics state, such as dash patterns, line joins and caps etc.
1621 *
1622 * The change in rendering results in many small differences, particularly at low resolution and PDF generated
1623 * by pdfwrite from PCL input.
1624 *
1625 * We need to clip the glyph description to the bounding box, because that's how the cache works with
1626 * a bitmap, if we don't do this then some glyphs are fully rendered which should only be partially
1627 * rendered. However, adding this causes two test files to render incorrectly (PostScript files
1628 * converted to PDF via pdfwrite). This is what decided me to revert this code.
1629 */
1630 code = gs_rectclip(ctx->pgs, (const gs_rect *)&wbox[2], 1);
1631 if (code < 0)
1632 goto d1_error;
1633
1634 code = gs_text_setcharwidth(ctx->text.current_enum, wbox);
1635 #else
1636 code = gs_text_setcachedevice(ctx->text.current_enum, wbox);
1637 #endif
1638
1639 /* See the comment immediately after gs_text_setcachedvice() in pdfi_d0 above */
1640 if (ctx->pgs->level > gsave_level)
1641 ctx->current_stream_save.gsave_level += ctx->pgs->level - gsave_level;
1642
1643 if (code < 0)
1644 goto d1_error;
1645 return 0;
1646
1647 d1_error:
1648 pdfi_clearstack(ctx);
1649 return code;
1650 }
1651
1652 int pdfi_Tf(pdf_context *ctx, pdf_dict *stream_dict, pdf_dict *page_dict)
1653 {
1654 double point_size = 0;
1655 pdf_obj *point_arg = NULL;
1656 int code = 0;
1657 pdf_name *fontname = NULL;
1658
1659 if (pdfi_count_stack(ctx) < 2) {
1660 pdfi_clearstack(ctx);
1661 return_error(gs_error_stackunderflow);
1662 }
1663
1664 /* Get refs to the args and pop them */
1665 point_arg = ctx->stack_top[-1];
1666 pdfi_countup(point_arg);
1667 fontname = (pdf_name *)ctx->stack_top[-2];
1668 pdfi_countup(fontname);
1669 pdfi_pop(ctx, 2);
1670
1671 /* Get the point_size */
1672 code = pdfi_obj_to_real(ctx, point_arg, &point_size);
1673 if (code < 0)
1674 goto exit0;
1675
1676 code = pdfi_load_resource_font(ctx, stream_dict, page_dict, fontname, point_size);
1677
1678 /* If we failed to load font, try to load an internal one */
1679 if (code != gs_error_Fatal && code < 0)
1680 code = pdfi_font_set_internal_name(ctx, fontname, point_size);
1681 exit0:
1682 pdfi_countdown(fontname);
1683 pdfi_countdown(point_arg);
1684 return code;
1685 }
1686
1687 int pdfi_free_font(pdf_obj *font)
1688 {
1689 pdf_font *f = (pdf_font *)font;
1690
1691 switch (f->pdfi_font_type) {
1692 case e_pdf_font_type0:
1693 return pdfi_free_font_type0((pdf_obj *)font);
1694 break;
1695 case e_pdf_font_type1:
1696 return pdfi_free_font_type1((pdf_obj *)font);
1697 break;
1698 case e_pdf_font_cff:
1699 return pdfi_free_font_cff((pdf_obj *)font);
1700 case e_pdf_font_type3:
1701 return pdfi_free_font_type3((pdf_obj *)font);
1702 break;
1703 case e_pdf_font_truetype:
1704 return pdfi_free_font_truetype((pdf_obj *)font);
1705 break;
1706 case e_pdf_font_microtype:
1707 return pdfi_free_font_microtype((pdf_obj *)font);
1708 break;
1709 case e_pdf_cidfont_type2:
1710 return pdfi_free_font_cidtype2((pdf_obj *)font);
1711 break;
1712 case e_pdf_cidfont_type0:
1713 return pdfi_free_font_cidtype0((pdf_obj *)font);
1714 break;
1715 case e_pdf_cidfont_type1:
1716 case e_pdf_cidfont_type4:
1717 default:
1718 return gs_note_error(gs_error_typecheck);
1719 break;
1720 }
1721 return 0;
1722 }
1723
1724 /* Assumes Encoding is an array, and parameters *ind and *near_ind set to ENCODING_INDEX_UNKNOWN */
1725 static void
1726 pdfi_gs_simple_font_encoding_indices(pdf_context *ctx, pdf_array *Encoding, gs_encoding_index_t *ind, gs_encoding_index_t *near_ind)
1727 {
1728 uint esize = Encoding->size;
1729 uint best = esize / 3; /* must match at least this many */
1730 int i, index, near_index = ENCODING_INDEX_UNKNOWN, code;
1731
1732 for (index = 0; index < NUM_KNOWN_REAL_ENCODINGS; ++index) {
1733 uint match = esize;
1734
1735 for (i = esize; --i >= 0;) {
1736 gs_const_string rstr;
1737 pdf_name *ename;
1738
1739 code = pdfi_array_get_type(ctx, Encoding, (uint64_t)i, PDF_NAME, (pdf_obj **)&ename);
1740 if (code < 0) {
1741 return;
1742 }
1743
1744 gs_c_glyph_name(gs_c_known_encode((gs_char)i, index), &rstr);
1745 if (rstr.size == ename->length &&
1746 !memcmp(rstr.data, ename->data, rstr.size)) {
1747 pdfi_countdown(ename);
1748 continue;
1749 }
1750 pdfi_countdown(ename);
1751 if (--match <= best) {
1752 break;
1753 }
1754 }
1755 if (match > best) {
1756 best = match;
1757 near_index = index;
1758 /* If we have a perfect match, stop now. */
1759 if (best == esize)
1760 break;
1761 }
1762 }
1763 if (best == esize) *ind = index;
1764 *near_ind = near_index;
1765 }
1766
1767 static inline int pdfi_encoding_name_to_index(pdf_name *name)
1768 {
1769 int ind = gs_error_undefined;
1770 if (pdfi_type_of(name) == PDF_NAME) {
1771 if (pdfi_name_is(name, "StandardEncoding")) {
1772 ind = ENCODING_INDEX_STANDARD;
1773 } else if (pdfi_name_is(name, "WinAnsiEncoding")) {
1774 ind = ENCODING_INDEX_WINANSI;
1775 }
1776 else if (pdfi_name_is(name, "MacRomanEncoding")) {
1777 ind = ENCODING_INDEX_MACROMAN;
1778 }
1779 else if (pdfi_name_is(name, "MacExpertEncoding")) {
1780 ind = ENCODING_INDEX_MACEXPERT;
1781 }
1782 else if (pdfi_name_is(name, "SymbolEncoding")) {
1783 ind = ENCODING_INDEX_SYMBOL;
1784 }
1785 else if (pdfi_name_is(name, "DingbatsEncoding")) {
1786 ind = ENCODING_INDEX_DINGBATS;
1787 }
1788 }
1789 return ind;
1790 }
1791
1792 /*
1793 * Routine to fill in an array with each of the glyph names from a given
1794 * 'standard' Encoding.
1795 */
1796 static int pdfi_build_Encoding(pdf_context *ctx, pdf_name *name, pdf_array *Encoding, gs_encoding_index_t *ind)
1797 {
1798 int i, code = 0;
1799 unsigned char gs_encoding;
1800 gs_glyph temp;
1801 gs_const_string str;
1802 pdf_name *n = NULL;
1803
1804 if (pdfi_array_size(Encoding) < 256)
1805 return gs_note_error(gs_error_rangecheck);
1806
1807 code = pdfi_encoding_name_to_index(name);
1808 if (code < 0)
1809 return code;
1810 if (ind != NULL) *ind = (gs_encoding_index_t)code;
1811 gs_encoding = (unsigned char)code;
1812 code = 0;
1813
1814 for (i = 0;i<256;i++) {
1815 temp = gs_c_known_encode(i, gs_encoding);
1816 gs_c_glyph_name(temp, &str);
1817 code = pdfi_name_alloc(ctx, (byte *)str.data, str.size, (pdf_obj **)&n);
1818 if (code < 0)
1819 return code;
1820 pdfi_countup(n);
1821 code = pdfi_array_put(ctx, Encoding, (uint64_t)i, (pdf_obj *)n);
1822 pdfi_countdown(n);
1823 if (code < 0)
1824 return code;
1825 }
1826 return 0;
1827 }
1828
1829 /*
1830 * Create and fill in a pdf_array with an Encoding for a font. pdf_Encoding must be either
1831 * a name (eg StandardEncoding) or a dictionary. If its a name we use that to create the
1832 * entries, if its a dictionary we start by getting the BaseEncoding and using that to
1833 * create an array of glyph names as above, *or* for a symbolic font, we use the "predef_Encoding"
1834 * which is the encoding from the font description itself (i.e. the /Encoding array
1835 * from a Type 1 font. We then get the Differences array from the dictionary and use that to
1836 * refine the Encoding.
1837 */
1838 int pdfi_create_Encoding(pdf_context *ctx, pdf_font *ppdffont, pdf_obj *pdf_Encoding, pdf_obj *font_Encoding, pdf_obj **Encoding)
1839 {
1840 int code = 0, i;
1841 gs_encoding_index_t encoding_index = ENCODING_INDEX_UNKNOWN, nearest_encoding_index = ENCODING_INDEX_UNKNOWN;
1842
1843 code = pdfi_array_alloc(ctx, 256, (pdf_array **)Encoding);
1844 if (code < 0)
1845 return code;
1846 pdfi_countup(*Encoding);
1847
1848 switch (pdfi_type_of(pdf_Encoding)) {
1849 case PDF_NAME:
1850 code = pdfi_build_Encoding(ctx, (pdf_name *)pdf_Encoding, (pdf_array *)*Encoding, &encoding_index);
1851 if (code < 0) {
1852 pdfi_countdown(*Encoding);
1853 *Encoding = NULL;
1854 return code;
1855 }
1856 nearest_encoding_index = encoding_index;
1857 break;
1858 case PDF_DICT:
1859 {
1860 pdf_name *n = NULL;
1861 pdf_array *a = NULL;
1862 pdf_obj *o = NULL;
1863 int offset = 0;
1864 bool b_e_known;
1865
1866 if (pdfi_type_of(pdf_Encoding) == PDF_DICT) {
1867 code = pdfi_dict_known(ctx, (pdf_dict *)pdf_Encoding, "BaseEncoding", &b_e_known);
1868 if (code < 0)
1869 b_e_known = false;
1870 }
1871 else {
1872 b_e_known = false;
1873 }
1874
1875 if (b_e_known == false && font_Encoding != NULL && pdfi_type_of(font_Encoding) == PDF_ARRAY) {
1876 pdf_array *fenc = (pdf_array *)font_Encoding;
1877 for (i = 0; i < pdfi_array_size(fenc) && code >= 0; i++) {
1878 code = pdfi_array_get(ctx, fenc, (uint64_t)i, &o);
1879 if (code >= 0)
1880 code = pdfi_array_put(ctx, (pdf_array *)*Encoding, (uint64_t)i, o);
1881 pdfi_countdown(o);
1882 }
1883 if (code < 0) {
1884 pdfi_countdown(*Encoding);
1885 *Encoding = NULL;
1886 return code;
1887 }
1888 }
1889 else {
1890 code = pdfi_dict_get(ctx, (pdf_dict *)pdf_Encoding, "BaseEncoding", (pdf_obj **)&n);
1891 if (code >= 0) {
1892 if (pdfi_encoding_name_to_index(n) < 0) {
1893 pdfi_set_warning(ctx, 0, NULL, W_PDF_INVALID_FONT_BASEENC, "pdfi_create_Encoding", NULL);
1894 pdfi_countdown(n);
1895 n = NULL;
1896 code = gs_error_undefined;
1897 }
1898 else if (pdfi_name_is(n, "StandardEncoding") == true) {
1899 pdfi_set_warning(ctx, 0, NULL, W_PDF_INVALID_FONT_BASEENC, "pdfi_create_Encoding", NULL);
1900 }
1901 }
1902
1903 if (code < 0) {
1904 code = pdfi_name_alloc(ctx, (byte *)"StandardEncoding", 16, (pdf_obj **)&n);
1905 if (code < 0) {
1906 pdfi_countdown(*Encoding);
1907 *Encoding = NULL;
1908 return code;
1909 }
1910 pdfi_countup(n);
1911 }
1912
1913 code = pdfi_build_Encoding(ctx, n, (pdf_array *)*Encoding, NULL);
1914 if (code < 0) {
1915 pdfi_countdown(*Encoding);
1916 *Encoding = NULL;
1917 pdfi_countdown(n);
1918 return code;
1919 }
1920 pdfi_countdown(n);
1921 }
1922 code = pdfi_dict_knownget_type(ctx, (pdf_dict *)pdf_Encoding, "Differences", PDF_ARRAY, (pdf_obj **)&a);
1923 if (code <= 0) {
1924 if (code < 0) {
1925 pdfi_countdown(*Encoding);
1926 *Encoding = NULL;
1927 }
1928 return code;
1929 }
1930
1931 for (i=0;i < pdfi_array_size(a);i++) {
1932 pdf_obj_type type;
1933 code = pdfi_array_get(ctx, a, (uint64_t)i, &o);
1934 if (code < 0)
1935 break;
1936 type = pdfi_type_of(o);
1937 if (type == PDF_NAME) {
1938 if (offset < pdfi_array_size((pdf_array *)*Encoding))
1939 code = pdfi_array_put(ctx, (pdf_array *)*Encoding, (uint64_t)offset, o);
1940 pdfi_countdown(o);
1941 offset++;
1942 if (code < 0)
1943 break;
1944 } else if (type == PDF_INT) {
1945 offset = ((pdf_num *)o)->value.i;
1946 pdfi_countdown(o);
1947 } else {
1948 code = gs_note_error(gs_error_typecheck);
1949 pdfi_countdown(o);
1950 break;
1951 }
1952 }
1953 pdfi_countdown(a);
1954 if (code < 0) {
1955 pdfi_countdown(*Encoding);
1956 *Encoding = NULL;
1957 return code;
1958 }
1959 if (ppdffont != NULL) /* No sense doing this if we can't record the result */
1960 pdfi_gs_simple_font_encoding_indices(ctx, (pdf_array *)(*Encoding), &encoding_index, &nearest_encoding_index);
1961 break;
1962 }
1963 default:
1964 pdfi_countdown(*Encoding);
1965 *Encoding = NULL;
1966 return gs_note_error(gs_error_typecheck);
1967 }
1968 if (ppdffont != NULL) {
1969 ppdffont->pfont->encoding_index = encoding_index;
1970 ppdffont->pfont->nearest_encoding_index = nearest_encoding_index;
1971 }
1972 return 0;
1973 }
1974
1975 gs_glyph pdfi_encode_char(gs_font * pfont, gs_char chr, gs_glyph_space_t not_used)
1976 {
1977 int code;
1978 unsigned int nindex = 0;
1979 gs_glyph g = GS_NO_GLYPH;
1980
1981 if (pfont->FontType == ft_encrypted || pfont->FontType == ft_encrypted2
1982 || pfont->FontType == ft_user_defined || pfont->FontType == ft_TrueType
1983 || pfont->FontType == ft_MicroType
1984 || pfont->FontType == ft_PDF_user_defined) {
1985 pdf_font *font = (pdf_font *)pfont->client_data;
1986 pdf_context *ctx = (pdf_context *)font->ctx;
1987
1988 if (font->Encoding != NULL) { /* safety */
1989 pdf_name *GlyphName = NULL;
1990 code = pdfi_array_get(ctx, font->Encoding, (uint64_t)chr, (pdf_obj **)&GlyphName);
1991 if (code >= 0) {
1992 if (pdfi_type_of(GlyphName) != PDF_NAME)
1993 /* Can't signal an error, just return the 'not found' case */
1994 return g;
1995
1996 code = (*ctx->get_glyph_index)(pfont, (byte *)GlyphName->data, GlyphName->length, &nindex);
1997 pdfi_countdown(GlyphName);
1998 if (code >= 0)
1999 g = (gs_glyph)nindex;
2000 }
2001 }
2002 }
2003
2004 return g;
2005 }
2006
2007 extern const pdfi_cid_decoding_t *pdfi_cid_decoding_list[];
2008 extern const pdfi_cid_subst_nwp_table_t *pdfi_cid_substnwp_list[];
2009
2010 void pdfi_cidfont_cid_subst_tables(const char *reg, const int reglen, const char *ord,
2011 const int ordlen, pdfi_cid_decoding_t **decoding, pdfi_cid_subst_nwp_table_t **substnwp)
2012 {
2013 int i;
2014 *decoding = NULL;
2015 *substnwp = NULL;
2016 /* This only makes sense for Adobe orderings */
2017 if (reglen == 5 && !memcmp(reg, "Adobe", 5)) {
2018 for (i = 0; pdfi_cid_decoding_list[i] != NULL; i++) {
2019 if (strlen(pdfi_cid_decoding_list[i]->s_order) == ordlen &&
2020 !memcmp(pdfi_cid_decoding_list[i]->s_order, ord, ordlen)) {
2021 *decoding = (pdfi_cid_decoding_t *)pdfi_cid_decoding_list[i];
2022 break;
2023 }
2024 }
2025 /* For now, also only for Adobe orderings */
2026 for (i = 0; pdfi_cid_substnwp_list[i] != NULL; i++) {
2027 if (strlen(pdfi_cid_substnwp_list[i]->ordering) == ordlen &&
2028 !memcmp(pdfi_cid_substnwp_list[i]->ordering, ord, ordlen)) {
2029 *substnwp = (pdfi_cid_subst_nwp_table_t *)pdfi_cid_substnwp_list[i];
2030 break;
2031 }
2032 }
2033 }
2034 }
2035
2036 int pdfi_tounicode_char_to_unicode(pdf_context *ctx, pdf_cmap *tounicode, gs_glyph glyph, int ch, ushort *unicode_return, unsigned int length)
2037 {
2038 int i, l = 0;
2039 int code = gs_error_undefined;
2040 unsigned char *ucode = (unsigned char *)unicode_return;
2041
2042 if (tounicode != NULL) {
2043 gs_cmap_lookups_enum_t lenum;
2044 gs_cmap_lookups_enum_init((const gs_cmap_t *)tounicode->gscmap, 0, &lenum);
2045 while (l == 0 && gs_cmap_enum_next_lookup(ctx->memory, &lenum) == 0) {
2046 gs_cmap_lookups_enum_t counter = lenum;
2047 while (l == 0 && gs_cmap_enum_next_entry(&counter) == 0) {
2048 if (counter.entry.value_type == CODE_VALUE_CID) {
2049 if (counter.entry.key_is_range) {
2050 unsigned int v0 = 0, v1 = 0;
2051 for (i = 0; i < counter.entry.key_size; i++) {
2052 v0 |= (counter.entry.key[0][counter.entry.key_size - i - 1]) << (i * 8);
2053 }
2054 for (i = 0; i < counter.entry.key_size; i++) {
2055 v1 |= (counter.entry.key[1][counter.entry.key_size - i - 1]) << (i * 8);
2056 }
2057
2058 if (ch >= v0 && ch <= v1) {
2059 unsigned int offs = ch - v0;
2060
2061 if (counter.entry.value.size == 1) {
2062 l = 2;
2063 if (ucode != NULL && length >= l) {
2064 ucode[0] = 0x00;
2065 ucode[1] = counter.entry.value.data[0] + offs;
2066 }
2067 }
2068 else if (counter.entry.value.size == 2) {
2069 l = 2;
2070 if (ucode != NULL && length >= l) {
2071 ucode[0] = counter.entry.value.data[0] + ((offs >> 8) & 0xff);
2072 ucode[1] = counter.entry.value.data[1] + (offs & 0xff);
2073 }
2074 }
2075 else if (counter.entry.value.size == 3) {
2076 l = 4;
2077 if (ucode != NULL && length >= l) {
2078 ucode[0] = 0;
2079 ucode[1] = counter.entry.value.data[0] + ((offs >> 16) & 0xff);
2080 ucode[2] = counter.entry.value.data[1] + ((offs >> 8) & 0xff);
2081 ucode[3] = counter.entry.value.data[2] + (offs & 0xff);
2082 }
2083 }
2084 else {
2085 l = 4;
2086 if (ucode != NULL && length >= l) {
2087 ucode[0] = counter.entry.value.data[0] + ((offs >> 24) & 0xff);
2088 ucode[1] = counter.entry.value.data[1] + ((offs >> 16) & 0xff);
2089 ucode[2] = counter.entry.value.data[2] + ((offs >> 8) & 0xff);
2090 ucode[3] = counter.entry.value.data[3] + (offs & 0xff);
2091 }
2092 }
2093 }
2094 }
2095 else {
2096 unsigned int v = 0;
2097 for (i = 0; i < counter.entry.key_size; i++) {
2098 v |= (counter.entry.key[0][counter.entry.key_size - i - 1]) << (i * 8);
2099 }
2100 if (ch == v) {
2101 if (counter.entry.value.size == 1) {
2102 l = 2;
2103 if (ucode != NULL && length >= l) {
2104 ucode[0] = 0x00;
2105 ucode[1] = counter.entry.value.data[0];
2106 }
2107 }
2108 else if (counter.entry.value.size == 2) {
2109 l = 2;
2110 if (ucode != NULL && length >= l) {
2111 ucode[0] = counter.entry.value.data[0];
2112 ucode[1] = counter.entry.value.data[1];
2113 }
2114 }
2115 else if (counter.entry.value.size == 3) {
2116 l = 4;
2117 if (ucode != NULL && length >= l) {
2118 ucode[0] = 0;
2119 ucode[1] = counter.entry.value.data[0];
2120 ucode[2] = counter.entry.value.data[1];
2121 ucode[3] = counter.entry.value.data[2];
2122 }
2123 }
2124 else {
2125 l = 4;
2126 if (ucode != NULL && length >= l) {
2127 ucode[0] = counter.entry.value.data[0];
2128 ucode[1] = counter.entry.value.data[1];
2129 ucode[2] = counter.entry.value.data[2];
2130 ucode[3] = counter.entry.value.data[3];
2131 }
2132 }
2133 }
2134 }
2135 }
2136 }
2137 }
2138 if (l > 0)
2139 code = l;
2140 }
2141
2142 return code;
2143 }
2144
2145 int
2146 pdfi_cidfont_decode_glyph(gs_font *font, gs_glyph glyph, int ch, ushort *u, unsigned int length)
2147 {
2148 gs_glyph cc = glyph < GS_MIN_CID_GLYPH ? glyph : glyph - GS_MIN_CID_GLYPH;
2149 pdf_cidfont_t *pcidfont = (pdf_cidfont_t *)font->client_data;
2150 int code = gs_error_undefined, i;
2151 uchar *unicode_return = (uchar *)u;
2152 pdfi_cid_subst_nwp_table_t *substnwp = pcidfont->substnwp;
2153
2154 code = gs_error_undefined;
2155 while (1) { /* Loop to make retrying with a substitute CID easier */
2156 /* Favour the ToUnicode if one exists */
2157 code = pdfi_tounicode_char_to_unicode(pcidfont->ctx, (pdf_cmap *)pcidfont->ToUnicode, glyph, ch, u, length);
2158
2159 if (code == gs_error_undefined && pcidfont->decoding) {
2160 const int *n;
2161
2162 if (cc / 256 < pcidfont->decoding->nranges) {
2163 n = (const int *)pcidfont->decoding->ranges[cc / 256][cc % 256];
2164 for (i = 0; i < pcidfont->decoding->val_sizes; i++) {
2165 unsigned int cmapcc;
2166 if (n[i] == -1)
2167 break;
2168 cc = n[i];
2169 cmapcc = (unsigned int)cc;
2170 if (pcidfont->pdfi_font_type == e_pdf_cidfont_type2)
2171 code = pdfi_fapi_check_cmap_for_GID((gs_font *)pcidfont->pfont, (unsigned int)cc, &cmapcc);
2172 else
2173 code = 0;
2174 if (code >= 0 && cmapcc != 0){
2175 code = 0;
2176 break;
2177 }
2178 }
2179 /* If it's a TTF derived CIDFont, we prefer a code point supported by the cmap table
2180 but if not, use the first available one
2181 */
2182 if (code < 0 && n[0] != -1) {
2183 cc = n[0];
2184 code = 0;
2185 }
2186 }
2187 if (code >= 0) {
2188 if (cc > 65535) {
2189 code = 4;
2190 if (unicode_return != NULL && length >= code) {
2191 unicode_return[0] = (cc & 0xFF000000)>> 24;
2192 unicode_return[1] = (cc & 0x00FF0000) >> 16;
2193 unicode_return[2] = (cc & 0x0000FF00) >> 8;
2194 unicode_return[3] = (cc & 0x000000FF);
2195 }
2196 }
2197 else {
2198 code = 2;
2199 if (unicode_return != NULL && length >= code) {
2200 unicode_return[0] = (cc & 0x0000FF00) >> 8;
2201 unicode_return[1] = (cc & 0x000000FF);
2202 }
2203 }
2204 }
2205 }
2206 /* If we get here, and still don't have a usable code point, check for a
2207 pre-defined CID substitution, and if there's one, jump back to the start
2208 and try again.
2209 */
2210 if (code == gs_error_undefined && substnwp) {
2211 for (i = 0; substnwp->subst[i].s_type != 0; i++ ) {
2212 if (cc >= substnwp->subst[i].s_scid && cc <= substnwp->subst[i].e_scid) {
2213 cc = substnwp->subst[i].s_dcid + (cc - substnwp->subst[i].s_scid);
2214 substnwp = NULL;
2215 break;
2216 }
2217 if (cc >= substnwp->subst[i].s_dcid
2218 && cc <= substnwp->subst[i].s_dcid + (substnwp->subst[i].e_scid - substnwp->subst[i].s_scid)) {
2219 cc = substnwp->subst[i].s_scid + (cc - substnwp->subst[i].s_dcid);
2220 substnwp = NULL;
2221 break;
2222 }
2223 }
2224 if (substnwp == NULL)
2225 continue;
2226 }
2227 break;
2228 }
2229 return (code < 0 ? 0 : code);
2230 }
2231
2232 /* Get the unicode valude for a glyph FIXME - not written yet
2233 */
2234 int pdfi_decode_glyph(gs_font * font, gs_glyph glyph, int ch, ushort *unicode_return, unsigned int length)
2235 {
2236 pdf_font *pdffont = (pdf_font *)font->client_data;
2237 int code = 0;
2238
2239 if (pdffont->pdfi_font_type != e_pdf_cidfont_type0 && pdffont->pdfi_font_type != e_pdf_cidfont_type1
2240 && pdffont->pdfi_font_type != e_pdf_cidfont_type2 && pdffont->pdfi_font_type != e_pdf_cidfont_type4) {
2241 code = pdfi_tounicode_char_to_unicode(pdffont->ctx, (pdf_cmap *)pdffont->ToUnicode, glyph, ch, unicode_return, length);
2242 }
2243 if (code < 0) code = 0;
2244
2245 return code;
2246 }
2247
2248 int pdfi_glyph_index(gs_font *pfont, byte *str, uint size, uint *glyph)
2249 {
2250 int code = 0;
2251 pdf_font *font = (pdf_font *)pfont->client_data;
2252
2253 code = pdfi_get_name_index(font->ctx, (char *)str, size, glyph);
2254
2255 return code;
2256 }
2257
2258 int pdfi_glyph_name(gs_font * pfont, gs_glyph glyph, gs_const_string * pstr)
2259 {
2260 int code = gs_error_invalidfont;
2261
2262 if (pfont->FontType == ft_encrypted || pfont->FontType == ft_encrypted2
2263 || pfont->FontType == ft_user_defined || pfont->FontType == ft_TrueType
2264 || pfont->FontType == ft_PDF_user_defined || pfont->FontType == ft_MicroType) {
2265 pdf_font *font = (pdf_font *)pfont->client_data;
2266
2267 code = pdfi_name_from_index(font->ctx, glyph, (unsigned char **)&pstr->data, &pstr->size);
2268 }
2269
2270 return code;
2271 }
2272
2273
2274 static int pdfi_global_glyph_code(const gs_font *pfont, gs_const_string *gstr, gs_glyph *pglyph)
2275 {
2276 int code = 0;
2277 if (pfont->FontType == ft_encrypted) {
2278 code = pdfi_t1_global_glyph_code(pfont, gstr, pglyph);
2279 }
2280 else if (pfont->FontType == ft_encrypted2) {
2281 code = pdfi_cff_global_glyph_code(pfont, gstr, pglyph);
2282 }
2283 else {
2284 code = gs_note_error(gs_error_invalidaccess);
2285 }
2286 return code;
2287 }
2288
2289 int pdfi_map_glyph_name_via_agl(pdf_dict *cstrings, pdf_name *gname, pdf_string **cstring)
2290 {
2291 single_glyph_list_t *sgl = (single_glyph_list_t *)&(SingleGlyphList);
2292 int i, code, ucode = gs_error_undefined;
2293 *cstring = NULL;
2294
2295 if (gname->length == 7 && strncmp((char *)gname->data, "uni", 3) == 0) {
2296 char u[5] = {0};
2297 memcpy(u, gname->data + 3, 4);
2298 code = sscanf(u, "%x", &ucode);
2299 if (code <= 0)
2300 ucode = gs_error_undefined;
2301 }
2302
2303 if (ucode == gs_error_undefined) {
2304 for (i = 0; sgl[i].Glyph != 0x00; i++) {
2305 if (sgl[i].Glyph[0] == gname->data[0]
2306 && strlen(sgl[i].Glyph) == gname->length
2307 && !strncmp((char *)sgl[i].Glyph, (char *)gname->data, gname->length)) {
2308 ucode = (int)sgl[i].Unicode;
2309 break;
2310 }
2311 }
2312 }
2313 if (ucode > 0) {
2314 for (i = 0; sgl[i].Glyph != 0x00; i++) {
2315 if (sgl[i].Unicode == (unsigned short)ucode) {
2316 pdf_string *s;
2317 code = pdfi_dict_get((pdf_context *)cstrings->ctx, cstrings, (char *)sgl[i].Glyph, (pdf_obj **)&s);
2318 if (code >= 0) {
2319 *cstring = s;
2320 break;
2321 }
2322 }
2323 }
2324 if (*cstring == NULL) {
2325 char u[16] = {0};
2326 code = gs_snprintf(u, 16, "uni%04x", ucode);
2327 if (code > 0) {
2328 pdf_string *s;
2329 code = pdfi_dict_get((pdf_context *)cstrings->ctx, cstrings, u, (pdf_obj **)&s);
2330 if (code >= 0) {
2331 *cstring = s;
2332 }
2333 }
2334 }
2335 }
2336
2337 if (*cstring == NULL)
2338 code = gs_note_error(gs_error_undefined);
2339 else
2340 code = 0;
2341
2342 return code;
2343 }
2344
2345
2346 int pdfi_init_font_directory(pdf_context *ctx)
2347 {
2348 gs_font_dir *pfdir = ctx->memory->gs_lib_ctx->font_dir;
2349 if (pfdir) {
2350 ctx->font_dir = gs_font_dir_alloc2_limits(ctx->memory, ctx->memory,
2351 pfdir->smax, pfdir->ccache.bmax, pfdir->fmcache.mmax,
2352 pfdir->ccache.cmax, pfdir->ccache.upper);
2353 if (ctx->font_dir == NULL) {
2354 return_error(gs_error_VMerror);
2355 }
2356 ctx->font_dir->align_to_pixels = pfdir->align_to_pixels;
2357 ctx->font_dir->grid_fit_tt = pfdir->grid_fit_tt;
2358 }
2359 else {
2360 ctx->font_dir = gs_font_dir_alloc2(ctx->memory, ctx->memory);
2361 if (ctx->font_dir == NULL) {
2362 return_error(gs_error_VMerror);
2363 }
2364 }
2365 ctx->font_dir->global_glyph_code = pdfi_global_glyph_code;
2366 return 0;
2367 }
2368
2369 /* Loads a (should be!) non-embedded font by name
2370 Only currently works for Type 1 fonts set.
2371 */
2372 int pdfi_load_font_by_name_string(pdf_context *ctx, const byte *fontname, size_t length,
2373 pdf_obj **ppdffont)
2374 {
2375 pdf_obj *fname = NULL;
2376 pdf_obj *fontobjtype = NULL;
2377 pdf_dict *fdict = NULL;
2378 int code;
2379 gs_font *pgsfont = NULL;
2380 const char *fs = "Font";
2381 pdf_name *Type1Name = NULL;
2382
2383 code = pdfi_name_alloc(ctx, (byte *)fontname, length, &fname);
2384 if (code < 0)
2385 return code;
2386 pdfi_countup(fname);
2387
2388 code = pdfi_name_alloc(ctx, (byte *)fs, strlen(fs), &fontobjtype);
2389 if (code < 0)
2390 goto exit;
2391 pdfi_countup(fontobjtype);
2392
2393 code = pdfi_dict_alloc(ctx, 1, &fdict);
2394 if (code < 0)
2395 goto exit;
2396 pdfi_countup(fdict);
2397
2398 code = pdfi_dict_put(ctx, fdict, "BaseFont", fname);
2399 if (code < 0)
2400 goto exit;
2401
2402 code = pdfi_dict_put(ctx, fdict, "Type", fontobjtype);
2403 if (code < 0)
2404 goto exit;
2405
2406 code = pdfi_obj_charstr_to_name(ctx, "Type1", &Type1Name);
2407 if (code < 0)
2408 goto exit;
2409
2410 code = pdfi_dict_put(ctx, fdict, "Subtype", (pdf_obj *)Type1Name);
2411 if (code < 0)
2412 goto exit;
2413
2414 code = pdfi_load_font(ctx, NULL, NULL, fdict, &pgsfont, false);
2415 if (code < 0)
2416 goto exit;
2417
2418 *ppdffont = (pdf_obj *)pgsfont->client_data;
2419
2420 exit:
2421 pdfi_countdown(Type1Name);
2422 pdfi_countdown(fontobjtype);
2423 pdfi_countdown(fname);
2424 pdfi_countdown(fdict);
2425 return code;
2426 }
2427
2428
2429 int pdfi_font_create_widths(pdf_context *ctx, pdf_dict *fontdict, pdf_font *font, double scale)
2430 {
2431 int code = 0;
2432 pdf_obj *obj = NULL;
2433 int i;
2434
2435 font->Widths = NULL;
2436
2437 if (font->FontDescriptor != NULL) {
2438 code = pdfi_dict_knownget(ctx, font->FontDescriptor, "MissingWidth", &obj);
2439 if (code > 0) {
2440 if (pdfi_type_of(obj) == PDF_INT) {
2441 font->MissingWidth = ((pdf_num *) obj)->value.i * scale;
2442 }
2443 else if (pdfi_type_of(obj) == PDF_REAL) {
2444 font->MissingWidth = ((pdf_num *) obj)->value.d * scale;
2445 }
2446 else {
2447 font->MissingWidth = 0;
2448 }
2449 pdfi_countdown(obj);
2450 obj = NULL;
2451 }
2452 else {
2453 font->MissingWidth = 0;
2454 }
2455 }
2456 else {
2457 font->MissingWidth = 0;
2458 }
2459
2460 code = pdfi_dict_knownget_type(ctx, fontdict, "Widths", PDF_ARRAY, (pdf_obj **)&obj);
2461 if (code > 0) {
2462 if (pdfi_array_size((pdf_array *)obj) < font->LastChar - font->FirstChar + 1) {
2463 code = gs_note_error(gs_error_rangecheck);
2464 goto error;
2465 }
2466
2467 font->Widths = (double *)gs_alloc_bytes(OBJ_MEMORY(font), (size_t)sizeof(double) * (font->LastChar - font->FirstChar + 1), "pdfi_font_create_widths(Widths)");
2468 if (font->Widths == NULL) {
2469 code = gs_note_error(gs_error_VMerror);
2470 goto error;
2471 }
2472 memset(font->Widths, 0x00, sizeof(double) * (font->LastChar - font->FirstChar + 1));
2473 for (i = 0; i < (font->LastChar - font->FirstChar + 1); i++) {
2474 code = pdfi_array_get_number(ctx, (pdf_array *)obj, (uint64_t)i, &font->Widths[i]);
2475 if (code < 0)
2476 goto error;
2477 font->Widths[i] *= scale;
2478 }
2479 }
2480 error:
2481 pdfi_countdown(obj);
2482 if (code < 0) {
2483 gs_free_object(OBJ_MEMORY(font), font->Widths, "pdfi_font_create_widths(Widths)");
2484 font->Widths = NULL;
2485 }
2486 return code;
2487 }
2488
2489 void pdfi_font_set_first_last_char(pdf_context *ctx, pdf_dict *fontdict, pdf_font *font)
2490 {
2491 double f, l;
2492 int code;
2493
2494 if (fontdict == NULL) {
2495 f = (double)0;
2496 l = (double)255;
2497 }
2498 else {
2499 code = pdfi_dict_get_number(ctx, fontdict, "FirstChar", &f);
2500 if (code < 0 || f < 0 || f > 255)
2501 f = (double)0;
2502
2503 code = pdfi_dict_get_number(ctx, fontdict, "LastChar", &l);
2504 if (code < 0 || l < 0 || l > 255)
2505 l = (double)255;
2506 }
2507 if (f <= l) {
2508 font->FirstChar = (int)f;
2509 font->LastChar = (int)l;
2510 }
2511 else {
2512 font->FirstChar = 0;
2513 font->LastChar = 255;
2514 }
2515 }
2516
2517 void pdfi_font_set_orig_fonttype(pdf_context *ctx, pdf_font *font)
2518 {
2519 pdf_name *ftype;
2520 pdf_dict *fontdict = font->PDF_font;
2521 int code;
2522
2523 code = pdfi_dict_get_type(ctx, fontdict, "Subtype", PDF_NAME, (pdf_obj**)&ftype);
2524 if (code < 0) {
2525 font->orig_FontType = ft_undefined;
2526 }
2527 else {
2528 if (pdfi_name_is(ftype, "Type1") || pdfi_name_is(ftype, "MMType1"))
2529 font->orig_FontType = ft_encrypted;
2530 else if (pdfi_name_is(ftype, "Type1C"))
2531 font->orig_FontType = ft_encrypted2;
2532 else if (pdfi_name_is(ftype, "TrueType"))
2533 font->orig_FontType = ft_TrueType;
2534 else if (pdfi_name_is(ftype, "Type3"))
2535 font->orig_FontType = ft_user_defined;
2536 else if (pdfi_name_is(ftype, "CIDFontType0"))
2537 font->orig_FontType = ft_CID_encrypted;
2538 else if (pdfi_name_is(ftype, "CIDFontType2"))
2539 font->orig_FontType = ft_CID_TrueType;
2540 else
2541 font->orig_FontType = ft_undefined;
2542 }
2543 pdfi_countdown(ftype);
2544 }
2545
2546 /* Patch or create a new XUID based on the existing UID/XUID, a simple hash
2547 of the input file name and the font dictionary object number.
2548 This allows improved glyph cache efficiency, also ensures pdfwrite understands
2549 which fonts are repetitions, and which are different.
2550 Currently cannot return an error - if we can't allocate the new XUID values array,
2551 we just skip it, and assume the font is compliant.
2552 */
2553 int pdfi_font_generate_pseudo_XUID(pdf_context *ctx, pdf_dict *fontdict, gs_font_base *pfont)
2554 {
2555 gs_const_string fn;
2556 int i;
2557 uint32_t hash = 0;
2558 long *xvalues;
2559 int xuidlen = 3;
2560
2561 sfilename(ctx->main_stream->s, &fn);
2562 if (fontdict!= NULL && fontdict->object_num != 0) {
2563 const byte *sb;
2564 size_t l;
2565 if (fn.size > 0) {
2566 sb = fn.data;
2567 l = fn.size;
2568 }
2569 else {
2570 s_process_read_buf(ctx->main_stream->s);
2571 sb = sbufptr(ctx->main_stream->s);
2572 l = sbufavailable(ctx->main_stream->s) > 128 ? 128: sbufavailable(ctx->main_stream->s);
2573 }
2574
2575 for (i = 0; i < l; i++) {
2576 hash = ((((hash & 0xf8000000) >> 27) ^ (hash << 5)) & 0x7ffffffff) ^ sb[i];
2577 }
2578 hash = ((((hash & 0xf8000000) >> 27) ^ (hash << 5)) & 0x7ffffffff) ^ fontdict->object_num;
2579
2580 if (uid_is_XUID(&pfont->UID) && uid_XUID_size(&pfont->UID) > 2 && uid_XUID_values(&pfont->UID)[0] == 1000000) {
2581 /* This is already a pseudo XUID, probably because we are copying an existing font object
2582 * So just update the hash and the object number
2583 */
2584 uid_XUID_values(&pfont->UID)[1] = hash;
2585 uid_XUID_values(&pfont->UID)[2] = ctx->device_state.HighLevelDevice ? fontdict->object_num : 0;
2586 }
2587 else {
2588 if (uid_is_XUID(&pfont->UID))
2589 xuidlen += uid_XUID_size(&pfont->UID);
2590 else if (uid_is_valid(&pfont->UID))
2591 xuidlen++;
2592
2593 xvalues = (long *)gs_alloc_bytes(pfont->memory, (size_t)xuidlen * sizeof(long), "pdfi_font_generate_pseudo_XUID");
2594 if (xvalues == NULL) {
2595 return 0;
2596 }
2597 xvalues[0] = 1000000; /* "Private" value */
2598 xvalues[1] = hash;
2599
2600 xvalues[2] = ctx->device_state.HighLevelDevice ? fontdict->object_num : 0;
2601
2602 if (uid_is_XUID(&pfont->UID)) {
2603 for (i = 0; i < uid_XUID_size(&pfont->UID); i++) {
2604 xvalues[i + 3] = uid_XUID_values(&pfont->UID)[i];
2605 }
2606 uid_free(&pfont->UID, pfont->memory, "pdfi_font_generate_pseudo_XUID");
2607 }
2608 else if (uid_is_valid(&pfont->UID))
2609 xvalues[3] = pfont->UID.id;
2610
2611 uid_set_XUID(&pfont->UID, xvalues, xuidlen);
2612 }
2613 }
2614 return 0;
2615 }
2616
2617 int pdfi_default_font_info(gs_font *font, const gs_point *pscale, int members, gs_font_info_t *info)
2618 {
2619 pdf_font *pdff = (pdf_font *)font->client_data;
2620 int code;
2621
2622 /* We *must* call this first as it sets info->members = 0; */
2623 code = pdff->default_font_info(font, pscale, members, info);
2624 if (code < 0)
2625 return code;
2626 if ((members & FONT_INFO_EMBEDDED) != 0) {
2627 info->orig_FontType = pdff->orig_FontType;
2628 if (pdff->pdfi_font_type == e_pdf_font_type3) {
2629 info->FontEmbedded = (int)(true);
2630 info->members |= FONT_INFO_EMBEDDED;
2631 }
2632 else {
2633 info->FontEmbedded = (int)(pdff->substitute == font_embedded);
2634 info->members |= FONT_INFO_EMBEDDED;
2635 }
2636 }
2637 if (pdff->pdfi_font_type != e_pdf_font_truetype && pdff->pdfi_font_type != e_pdf_cidfont_type2) {
2638 if (((members & FONT_INFO_COPYRIGHT) != 0) && pdff->copyright != NULL) {
2639 info->Copyright.data = pdff->copyright->data;
2640 info->Copyright.size = pdff->copyright->length;
2641 info->members |= FONT_INFO_COPYRIGHT;
2642 }
2643 if (((members & FONT_INFO_NOTICE) != 0) && pdff->notice != NULL) {
2644 info->Notice.data = pdff->notice->data;
2645 info->Notice.size = pdff->notice->length;
2646 info->members |= FONT_INFO_NOTICE;
2647 }
2648 if (((members & FONT_INFO_FAMILY_NAME) != 0) && pdff->familyname != NULL) {
2649 info->FamilyName.data = pdff->familyname->data;
2650 info->FamilyName.size = pdff->familyname->length;
2651 info->members |= FONT_INFO_FAMILY_NAME;
2652 }
2653 if (((members & FONT_INFO_FULL_NAME) != 0) && pdff->fullname != NULL) {
2654 info->FullName.data = pdff->fullname->data;
2655 info->FullName.size = pdff->fullname->length;
2656 info->members |= FONT_INFO_FULL_NAME;
2657 }
2658 }
2659 return 0;
2660 }
2661
2662 /* Convenience function for using fonts created by
2663 pdfi_load_font_by_name_string
2664 */
2665 int pdfi_set_font_internal(pdf_context *ctx, pdf_obj *fontobj, double point_size)
2666 {
2667 int code;
2668 pdf_font *pdffont = (pdf_font *)fontobj;
2669
2670 if (pdfi_type_of(pdffont) != PDF_FONT || pdffont->pfont == NULL)
2671 return_error(gs_error_invalidfont);
2672
2673 code = gs_setPDFfontsize(ctx->pgs, point_size);
2674 if (code < 0)
2675 return code;
2676
2677 return pdfi_gs_setfont(ctx, (gs_font *)pdffont->pfont);
2678 }
2679
2680 /* Convenience function for setting font by name
2681 * Keeps one ref to the font, which will be in the graphics state font ->client_data
2682 */
2683 static int pdfi_font_set_internal_inner(pdf_context *ctx, const byte *fontname, size_t length,
2684 double point_size)
2685 {
2686 int code = 0;
2687 pdf_obj *font = NULL;
2688
2689
2690 code = pdfi_load_font_by_name_string(ctx, fontname, length, &font);
2691 if (code < 0) goto exit;
2692
2693 code = pdfi_set_font_internal(ctx, font, point_size);
2694
2695 exit:
2696 pdfi_countdown(font);
2697 return code;
2698 }
2699
2700 int pdfi_font_set_internal_string(pdf_context *ctx, const char *fontname, double point_size)
2701 {
2702 return pdfi_font_set_internal_inner(ctx, (const byte *)fontname, strlen(fontname), point_size);
2703 }
2704
2705 int pdfi_font_set_internal_name(pdf_context *ctx, pdf_name *fontname, double point_size)
2706 {
2707 if (pdfi_type_of(fontname) != PDF_NAME)
2708 return_error(gs_error_typecheck);
2709 else
2710 return pdfi_font_set_internal_inner(ctx, fontname->data, fontname->length, point_size);
2711 }