Debian Codegraph @

pdf/pdf_font1C.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 CFF (type 1C) font handling */
17
18 #include "pdf_int.h"
19
20 #include "gscedata.h"
21 #include "gscencs.h"
22 #include "gxfont0.h"
23 #include "gxfcid.h"
24 #include "assert_.h"
25
26 #include "pdf_types.h"
27 #include "pdf_font_types.h"
28 #include "pdf_font.h"
29 #include "pdf_font1C.h"
30 #include "pdf_fontps.h"
31 #include "pdf_dict.h"
32 #include "pdf_deref.h"
33 #include "pdf_file.h"
34 #include "pdf_array.h"
35
36 #include "gxtype1.h" /* for gs_type1_state_s */
37 #include "gsutil.h" /* For gs_next_ids() */
38
39 static byte *
40 pdfi_find_cff_index(byte *p, byte *e, int idx, byte **pp, byte **ep);
41
42 /* This is a super set of the contents of a pdfi Type 1C font/CIDFont.
43 Meaning we can store everying as we interpret, and not worry
44 about the actual font type until the end
45 */
46 typedef struct pdfi_cff_font_priv_s {
47 pdf_font_common;
48 pdf_array *Subrs;
49 int NumSubrs;
50 pdf_array *GlobalSubrs;
51 int NumGlobalSubrs;
52 pdf_dict *CharStrings;
53 byte *cffdata;
54 byte *cffend;
55 byte *gsubrs;
56 byte *subrs;
57 byte *charstrings;
58 int ncharstrings;
59 pdf_dict *CIDSystemInfo;
60 int64_t DW;
61 pdf_array *W;
62 pdf_array *DW2;
63 pdf_array *W2;
64 pdf_buffer *cidtogidmap;
65 pdf_array *FDArray;
66 /* The registry and ordering strings in gs_font_cid0_data are just references to
67 strings assumed to be managed be managed by the interpreter - so we have to stash
68 them in the pdfi font, too.
69 */
70 pdf_string *registry;
71 pdf_string *ordering;
72 int supplement;
73 int cidcount;
74 int uidbase;
75 font_proc_glyph_info((*orig_glyph_info));
76 } pdfi_cff_font_priv;
77
78 /* Same thing for the Ghostscript font
79 */
80 typedef struct pdfi_gs_cff_font_priv_s {
81 gs_font_base_common;
82 gs_type1_data type1data;
83 gs_font_cid0_data cidata;
84 bool forcecid;
85 pdfi_cff_font_priv pdfcffpriv;
86 } pdfi_gs_cff_font_priv;
87
88 typedef struct pdfi_gs_cff_font_common_priv_s {
89 gs_font_base_common;
90 } pdfi_gs_cff_font_common_priv;
91
92 typedef struct cff_font_offsets_s
93 {
94 unsigned int fdarray_off;
95 unsigned int fdselect_off;
96 unsigned int charset_off;
97 unsigned int encoding_off;
98 unsigned int strings_off;
99 unsigned int strings_size;
100 unsigned int private_off;
101 unsigned int private_size;
102 bool have_ros;
103 bool have_matrix;
104 } cff_font_offsets;
105
106 static int
107 pdfi_make_string_from_sid(pdf_context *ctx, pdf_obj **str,
108 pdfi_cff_font_priv *font, cff_font_offsets *offsets, unsigned int sid);
109
110 static void
111 pdfi_init_cff_font_priv(pdf_context *ctx, pdfi_gs_cff_font_priv *cffpriv,
112 byte *buf, int buflen, bool for_fdarray);
113
114 static int
115 pdfi_alloc_cff_font(pdf_context *ctx, pdf_font_cff ** font, uint32_t obj_num, bool for_fdarray);
116
117 /* CALLBACKS */
118 static int
119 pdfi_cff_glyph_data(gs_font_type1 *pfont, gs_glyph glyph, gs_glyph_data_t *pgd)
120 {
121 int code = 0;
122 pdf_font_cff *cfffont = (pdf_font_cff *) pfont->client_data;
123 pdf_context *ctx = (pdf_context *) cfffont->ctx;
124 pdf_name *glyphname = NULL;
125 pdf_string *charstring = NULL;
126
127 /* Getting here with Encoding == NULL means it's a subfont from an FDArray
128 so we index directly by gid
129 */
130 if (cfffont->Encoding == NULL) {
131 char indstring[33];
132 int l = gs_snprintf(indstring, sizeof(indstring), "%u", (unsigned int)glyph);
133
134 code = pdfi_name_alloc(ctx, (byte *) indstring, l, (pdf_obj **) &glyphname);
135 if (code >= 0)
136 pdfi_countup(glyphname);
137 }
138 else {
139 gs_const_string gname;
140 code = (*ctx->get_glyph_name)((gs_font *)pfont, glyph, &gname);
141 if (code >= 0) {
142 code = pdfi_name_alloc(ctx, (byte *) gname.data, gname.size, (pdf_obj **) &glyphname);
143 if (code >= 0)
144 pdfi_countup(glyphname);
145 }
146 }
147 if (code >= 0) {
148 code = pdfi_dict_get_by_key(ctx, cfffont->CharStrings, glyphname, (pdf_obj **) &charstring);
149 if (code < 0) {
150 code = pdfi_map_glyph_name_via_agl(cfffont->CharStrings, glyphname, &charstring);
151 }
152 if (code >= 0)
153 gs_glyph_data_from_bytes(pgd, charstring->data, 0, charstring->length, NULL);
154 }
155
156 pdfi_countdown(glyphname);
157 pdfi_countdown(charstring);
158
159 return code;
160 }
161
162 static int
163 pdfi_cff_subr_data(gs_font_type1 *pfont, int index, bool global, gs_glyph_data_t *pgd)
164 {
165 int code = 0;
166 pdf_font_cff *cfffont = (pdf_font_cff *) pfont->client_data;
167
168 if ((global &&index >= cfffont->NumGlobalSubrs)||(!global &&index >= cfffont->NumSubrs)) {
169 code = gs_note_error(gs_error_rangecheck);
170 }
171 else {
172 pdf_string *subrstring;
173 pdf_array *s = global ? cfffont->GlobalSubrs : cfffont->Subrs;
174
175 code = pdfi_array_get(cfffont->ctx, s, (uint64_t) index, (pdf_obj **) &subrstring);
176 if (code >= 0) {
177 gs_glyph_data_from_bytes(pgd, subrstring->data, 0, subrstring->length, NULL);
178 pdfi_countdown(subrstring);
179 }
180 }
181 return code;
182 }
183
184 static int
185 pdfi_cff_seac_data(gs_font_type1 *pfont, int ccode, gs_glyph *pglyph, gs_const_string *gstr, gs_glyph_data_t *pgd)
186 {
187 int code = 0;
188 pdf_font_cff *cfffont = (pdf_font_cff *) pfont->client_data;
189 pdf_context *ctx = (pdf_context *) cfffont->ctx;
190 gs_glyph glyph = gs_c_known_encode((gs_char)ccode, ENCODING_INDEX_STANDARD);
191
192 if (glyph == GS_NO_GLYPH)
193 return_error(gs_error_rangecheck);
194
195 code = gs_c_glyph_name(glyph, gstr);
196
197 if (code >= 0) {
198 unsigned int nindex;
199 code = (*ctx->get_glyph_index)((gs_font *)pfont, (byte *)gstr->data, gstr->size, &nindex);
200 if (pglyph != NULL)
201 *pglyph = (gs_glyph)nindex;
202 }
203
204 if (code >= 0) {
205 pdf_name *glyphname = NULL;
206 pdf_string *charstring = NULL;
207 code = pdfi_name_alloc(ctx, (byte *) gstr->data, gstr->size, (pdf_obj **) &glyphname);
208 if (code >= 0) {
209 pdfi_countup(glyphname);
210 code = pdfi_dict_get_by_key(ctx, cfffont->CharStrings, glyphname, (pdf_obj **)&charstring);
211 pdfi_countdown(glyphname);
212 if (code >= 0) {
213 if (pgd != NULL) {
214 gs_glyph_data_from_bytes(pgd, charstring->data, 0, charstring->length, NULL);
215 }
216 pdfi_countdown(charstring);
217 }
218 }
219 }
220
221 return code;
222 }
223
224 /* push/pop are null ops here */
225 static int
226 pdfi_cff_push(void *callback_data, const fixed *pf, int count)
227 {
228 (void)callback_data;
229 (void)pf;
230 (void)count;
231 return 0;
232 }
233 static int
234 pdfi_cff_pop(void *callback_data, fixed *pf)
235 {
236 (void)callback_data;
237 (void)pf;
238 return 0;
239 }
240
241 static int
242 pdfi_cff_enumerate_glyph(gs_font *pfont, int *pindex,
243 gs_glyph_space_t glyph_space, gs_glyph *pglyph)
244 {
245 int code, j;
246 pdf_name *key = NULL;
247 uint64_t i = (uint64_t) *pindex;
248 pdf_dict *cstrings;
249 pdf_font *pdffont = (pdf_font *) pfont->client_data;
250 pdf_context *ctx = (pdf_context *) pdffont->ctx;
251
252 (void)glyph_space;
253
254 /* Slightly naff: if build_char is NULL, this is an FDArray subfont */
255 if (pfont->procs.build_char == NULL) {
256 *pindex = 0;
257 *pglyph = GS_NO_GLYPH;
258 return 0;
259 }
260 else if (pdffont->pdfi_font_type == e_pdf_cidfont_type0) {
261 pdf_cidfont_type0 *cffcidfont = (pdf_cidfont_type0 *) pdffont;
262 cstrings = cffcidfont->CharStrings;
263 }
264 else {
265 pdf_font_cff *cfffont = (pdf_font_cff *) pdffont;
266
267 cstrings = cfffont->CharStrings;
268 }
269 if (*pindex <= 0)
270 code = pdfi_dict_key_first(pdffont->ctx, cstrings, (pdf_obj **) &key, &i);
271 else
272 code = pdfi_dict_key_next(pdffont->ctx, cstrings, (pdf_obj **) &key, &i);
273 if (code < 0) {
274 i = 0;
275 code = gs_note_error(gs_error_undefined);
276 }
277 /* If Encoding == NULL, it's an FDArray subfont */
278 else if (pdffont->pdfi_font_type != e_pdf_cidfont_type0 && pdffont->Encoding != NULL) {
279 unsigned int nindex;
280 code = (*ctx->get_glyph_index)(pfont, key->data, key->length, &nindex);
281 if (code < 0) {
282 code = (*ctx->get_glyph_index)(pfont, (byte *)".notdef", 7, &nindex);
283 if (code < 0)
284 *pglyph = GS_NO_GLYPH;
285 else
286 *pglyph = (gs_glyph)nindex;
287 }
288 else
289 *pglyph = (gs_glyph)nindex;
290 }
291 else {
292 char kbuf[32];
293 int l;
294 unsigned int val;
295 /* If this font started life as a CFF font that we've force to
296 act like a CIDFont, we can end up with a ".notdef" glyph name
297 */
298 if (key->length == 7 && memcmp(key->data, ".notdef", 7) == 0) {
299 val = 0;
300 l = 1;
301 }
302 else {
303 memcpy(kbuf, key->data, key->length);
304 kbuf[key->length] = 0;
305
306 l = sscanf(kbuf, "%ud", &val);
307 }
308 if (l > 0) {
309 pdf_cidfont_type0 *cffcidfont = (pdf_cidfont_type0 *) pdffont;
310 if (cffcidfont->cidtogidmap != NULL && cffcidfont->cidtogidmap->length > 0) {
311 for (j = (cffcidfont->cidtogidmap->length >> 1) - 1; j >= 0; j--) {
312 if (val == (cffcidfont->cidtogidmap->data[j << 1] << 8 | cffcidfont->cidtogidmap->data[(j << 1) + 1])) {
313 val = j;
314 break;
315 }
316 }
317 }
318 *pglyph = (gs_glyph) (val) + GS_MIN_CID_GLYPH;
319 }
320 }
321 *pindex = (int)i;
322 pdfi_countdown(key);
323 return code;
324 }
325
326 /* This *should* only get called for SEAC lookups, which have to come from StandardEncoding
327 so just try to lookup the string in the standard encodings
328 */
329 int
330 pdfi_cff_global_glyph_code(const gs_font *pfont, gs_const_string *gstr, gs_glyph *pglyph)
331 {
332 *pglyph = gs_c_name_glyph(gstr->data, gstr->size);
333 return 0;
334 }
335
336 static int
337 pdfi_cff_glyph_outline(gs_font *pfont, int WMode, gs_glyph glyph,
338 const gs_matrix *pmat, gx_path *ppath, double sbw[4])
339 {
340 gs_glyph_data_t gd;
341 gs_glyph_data_t *pgd = &gd;
342 gs_font_type1 *pfont1;
343 int code;
344
345 if (pfont->FontType == ft_CID_encrypted) {
346 gs_font_cid0 *pfcid0 = (gs_font_cid0 *) pfont;
347 int fididx = 0;
348
349 code = (*pfcid0->cidata.glyph_data) ((gs_font_base *) pfont, glyph, pgd, &fididx);
350 if (fididx < pfcid0->cidata.FDArray_size)
351 pfont1 = pfcid0->cidata.FDArray[fididx];
352 else
353 code = gs_note_error(gs_error_invalidaccess);
354 }
355 else {
356 pfont1 = (gs_font_type1 *) pfont;
357 code = (*pfont1->data.procs.glyph_data) ((gs_font_type1 *) pfont, glyph, pgd);
358 }
359
360 if (code >= 0) {
361 gs_type1_state cis = { 0 };
362 gs_type1_state *pcis = &cis;
363 gs_gstate gs;
364 int value;
365
366 if (pmat)
367 gs_matrix_fixed_from_matrix(&gs.ctm, pmat);
368 else {
369 gs_matrix imat;
370
371 gs_make_identity(&imat);
372 gs_matrix_fixed_from_matrix(&gs.ctm, &imat);
373 }
374 gs.flatness = 0;
375 code = gs_type1_interp_init(pcis, &gs, ppath, NULL, NULL, true, 0, pfont1);
376 if (code < 0)
377 return code;
378
379 pcis->no_grid_fitting = true;
380 gs_type1_set_callback_data(pcis, NULL);
381 /* Continue interpreting. */
382 icont:
383 code = pfont1->data.interpret(pcis, pgd, &value);
384 switch (code) {
385 case 0: /* all done */
386 /* falls through */
387 default: /* code < 0, error */
388 return code;
389 case type1_result_callothersubr: /* unknown OtherSubr */
390 return_error(gs_error_rangecheck); /* can't handle it */
391 case type1_result_sbw: /* [h]sbw, just continue */
392 type1_cis_get_metrics(pcis, sbw);
393 pgd = 0;
394 goto icont;
395 }
396 }
397 return code;
398 }
399 static int
400 pdfi_cff_glyph_info(gs_font *font, gs_glyph glyph, const gs_matrix *pmat, int members, gs_glyph_info_t *info)
401 {
402 if ((members & GLYPH_INFO_OUTLINE_WIDTHS) == 0)
403 return gs_type1_glyph_info(font, glyph, pmat, members, info);
404
405 return gs_default_glyph_info(font, glyph, pmat, members, info);
406 }
407
408 static int
409 pdfi_cff_fdarray_glyph_data(gs_font_type1 *pfont, gs_glyph glyph, gs_glyph_data_t *pgd)
410 {
411 return_error(gs_error_invalidfont);
412 }
413
414 static int
415 pdfi_cff_fdarray_seac_data(gs_font_type1 *pfont, int ccode,
416 gs_glyph *pglyph, gs_const_string *gstr, gs_glyph_data_t *pgd)
417 {
418 return_error(gs_error_invalidfont);
419 }
420
421 /* Note that pgd may be NULL - so only retrieve the fidx */
422 static int
423 pdfi_cff_cid_glyph_data(gs_font_base *pbfont, gs_glyph glyph, gs_glyph_data_t *pgd, int *pfidx)
424 {
425 int code = 0;
426 pdf_cidfont_type0 *pdffont9 = (pdf_cidfont_type0 *) pbfont->client_data;
427 gs_font_cid0 *gscidfont = (gs_font_cid0 *) pbfont;
428 pdf_name *glyphname = NULL;
429 pdf_string *charstring = NULL;
430 char nbuf[64];
431 uint32_t l;
432 gs_glyph gid;
433
434 *pfidx = 0;
435
436 if (glyph < GS_MIN_CID_GLYPH)
437 gid = glyph;
438 else
439 gid = glyph - GS_MIN_CID_GLYPH;
440
441 if (pdffont9->cidtogidmap != NULL && pdffont9->cidtogidmap->length > (gid << 1) + 1) {
442 gid = pdffont9->cidtogidmap->data[gid << 1] << 8 | pdffont9->cidtogidmap->data[(gid << 1) + 1];
443 }
444
445 l = gs_snprintf(nbuf, sizeof(nbuf), "%" PRId64, gid);
446
447 code = pdfi_name_alloc(pdffont9->ctx, (byte *) nbuf, l, (pdf_obj **) &glyphname);
448 if (code >= 0) {
449 pdfi_countup(glyphname);
450 code = pdfi_dict_get_by_key(pdffont9->ctx, pdffont9->CharStrings, glyphname, (pdf_obj **) &charstring);
451 if (code >= 0 && charstring->length >= gscidfont->cidata.FDBytes) {
452 if (gscidfont->cidata.FDBytes != 0) {
453 if ((int)charstring->data[0] >= gscidfont->cidata.FDArray_size)
454 code = gs_note_error(gs_error_invalidfont);
455 else
456 *pfidx = (int)charstring->data[0];
457 }
458
459 if (code >= 0 && pgd && ((int64_t)charstring->length - (int64_t)gscidfont->cidata.FDBytes) >= 0)
460 gs_glyph_data_from_bytes(pgd, charstring->data + gscidfont->cidata.FDBytes, 0, charstring->length - gscidfont->cidata.FDBytes, NULL);
461 }
462 }
463 pdfi_countdown(charstring);
464 pdfi_countdown(glyphname);
465
466 return code;
467 }
468
469
470 static int
471 pdfi_cff_cidfont_glyph_info(gs_font *font, gs_glyph glyph, const gs_matrix *pmat,
472 int members, gs_glyph_info_t *info)
473 {
474 int code;
475 gs_font_cid0 *pcidfont = (gs_font_cid0 *)font;
476 pdf_cidfont_type0 *pdffont9 = (pdf_cidfont_type0 *)font->client_data;
477 code = (*pdffont9->orig_glyph_info)(font, glyph, pmat, members, info);
478 if (code < 0)
479 return code;
480
481 if ((members & GLYPH_INFO_WIDTHS) != 0
482 && glyph > GS_MIN_CID_GLYPH
483 && glyph < GS_MIN_GLYPH_INDEX) {
484 double widths[6] = {0};
485 int fidx;
486 gs_matrix imat;
487 gs_matrix mat1 = font->FontMatrix;
488 gs_glyph g = glyph - GS_MIN_CID_GLYPH;
489
490 code = (*pcidfont->cidata.glyph_data) ((gs_font_base *)font, g + GS_MIN_CID_GLYPH, NULL, &fidx);
491 if (code < 0)
492 return code;
493 if (fidx < pcidfont->cidata.FDArray_size) {
494 gs_font_type1 *pfdfont = pcidfont->cidata.FDArray[fidx];
495 /* The following cannot fail - if the matrix multiplication didn't work
496 we'd have errored out at a higher level
497 */
498 (void)gs_matrix_multiply(&font->FontMatrix, &pfdfont->FontMatrix, &mat1);
499 }
500 code = gs_matrix_invert(&mat1, &imat);
501 if (code < 0)
502 return code; /* By this stage, this should be impossible */
503 if (pmat) {
504 gs_matrix_multiply(&imat, pmat, &mat1);
505 }
506 else {
507 mat1 = imat;
508 }
509
510 code = pdfi_get_cidfont_glyph_metrics(font, g, widths, true);
511 if (code >= 0) {
512 code = gs_point_transform(widths[GLYPH_W0_WIDTH_INDEX] / 1000.0, widths[GLYPH_W0_HEIGHT_INDEX] / 1000.0, &mat1, &info->width[0]);
513 if (code < 0)
514 return code;
515 info->members |= GLYPH_INFO_WIDTH0;
516
517 if ((members & GLYPH_INFO_WIDTH1) != 0 && (widths[GLYPH_W1_WIDTH_INDEX] != 0 || widths[GLYPH_W1_HEIGHT_INDEX] != 0)) {
518 code = gs_point_transform(widths[GLYPH_W1_WIDTH_INDEX] / 1000.0, widths[GLYPH_W1_HEIGHT_INDEX] / 1000.0, &mat1, &info->width[1]);
519 info->members |= GLYPH_INFO_WIDTH1;
520 }
521 if ((members & GLYPH_INFO_VVECTOR1) != 0) {
522 code = gs_point_transform(widths[GLYPH_W1_V_X_INDEX] / 1000.0, widths[GLYPH_W1_V_Y_INDEX] / 1000.0, &mat1, &info->v);
523 info->members |= GLYPH_INFO_VVECTOR1;
524 }
525 }
526 }
527 return code;
528 }
529
530 /* END CALLBACKS */
531
532 #if 0 /* not currently used */
533 static inline int
534 s16(const byte *p)
535 {
536 return (signed short)((p[0] << 8) | p[1]);
537 }
538 #endif /* not currently used */
539
540 static inline int
541 u16(const byte *p, const byte *e, int *ret)
542 {
543 if (p + 1 > e) {
544 *ret = 0;
545 return_error(gs_error_invalidfont);
546 }
547 *ret = (p[0] << 8) | p[1];
548 return 0;
549 }
550
551 static inline int
552 u24(const byte *p, const byte *e, int *ret)
553 {
554 if (p + 2 > e) {
555 *ret = 0;
556 return_error(gs_error_invalidfont);
557 }
558 *ret = (p[0] << 16) | (p[1] << 8) | p[2];
559 return 0;
560 }
561
562 static inline int
563 u32(const byte *p, const byte *e, int *ret)
564 {
565 if (p + 3 > e) {
566 *ret = 0;
567 return_error(gs_error_invalidfont);
568 }
569 *ret = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
570 return 0;
571 }
572
573
574 static int
575 subrbias(int count)
576 {
577 return count < 1240 ? 107 : count < 33900 ? 1131 : 32768;
578 }
579
580 static int
581 uofs(const byte *p, const byte *e, int offsize, int *ret)
582 {
583 if (p > e) {
584 *ret = 0;
585 return_error(gs_error_invalidfont);
586 }
587 if (offsize == 1) {
588 *ret = p[0];
589 return 0;
590 }
591 if (offsize == 2)
592 return u16(p, e, ret);
593 if (offsize == 3)
594 return u24(p, e, ret);
595 if (offsize == 4)
596 return u32(p, e, ret);
597
598 return_error(gs_error_invalidfont);
599 }
600
601 static int
602 iso_adobe_charset_proc(const byte *p, const byte *pe, unsigned i)
603 {
604 if (i < 228)
605 return i + 1;
606 else
607 return_error(gs_error_rangecheck);
608 }
609
610 static int
611 expert_charset_proc(const byte *p, const byte *pe, unsigned i)
612 {
613 if (i < gs_c_known_encoding_lengths[6])
614 return gs_c_known_encodings[6][i];
615
616 return_error(gs_error_rangecheck);
617 }
618
619 static int
620 expert_subset_charset_proc(const byte *p, const byte *pe, unsigned int i)
621 {
622 #if 0
623 if (i < sizeof(expert_subset_charset) / sizeof(*expert_subset_charset))
624 return expert_subset_charset[i];
625 #endif
626 return_error(gs_error_rangecheck);
627 }
628
629 static int
630 format0_charset_proc(const byte *p, const byte *pe, unsigned int i)
631 {
632 int code, ret;
633 if (p + 2 * i > pe)
634 return gs_error_rangecheck;
635
636 if ((code = u16(p + 2 * i, pe, &ret)) < 0) {
637 return code;
638 }
639 return ret;
640 }
641
642 static int
643 format1_charset_proc(const byte *p, const byte *pe, unsigned int i)
644 {
645 int code = gs_error_rangecheck;
646 unsigned int cid = 0;
647
648 while (p < pe - 3) {
649 unsigned int first, count;
650
651 code = (unsigned int)u16(p, pe, (int *)&first);
652 if (code < 0)
653 break;
654 count = (unsigned int)p[2] + 1;
655
656 if (i < cid + count) {
657 code = first + i - cid;
658 break;
659 }
660 p += 3;
661 cid += count;
662 }
663 return code;
664 }
665
666 static int
667 format2_charset_proc(const byte *p, const byte *pe, unsigned int i)
668 {
669 int code = gs_error_rangecheck;
670 unsigned int cid = 0;
671
672 while (p < pe - 4) {
673 unsigned int first, count;
674
675 code = u16(p, pe, (int *)&first);
676 if (code >= 0)
677 code = u16(p + 2, pe, (int *)&count);
678 if (code < 0)
679 break;
680
681 count += 1;
682
683 if (i < cid + count) {
684 code = first + i - cid;
685 break;
686 }
687 p += 4;
688 cid += count;
689 }
690 return code;
691 }
692
693 static int
694 format0_fdselect_proc(const byte *p, const byte *pe, unsigned int i)
695 {
696 if (p + i + 4 > pe)
697 return_error(gs_error_rangecheck);
698 return (int)(*(p + i));
699 }
700
701 static int
702 format3_fdselect_proc(const byte *p, const byte *pe, unsigned int i)
703 {
704 unsigned int n_ranges;
705 int code;
706
707 if ((code = u16(p, pe, (int *)&n_ranges)) < 0)
708 return code;
709
710 p += 2;
711
712 while (n_ranges-- && p + 5 <= pe) {
713 unsigned int first, last;
714
715 code = u16(p, pe, (int *)&first);
716 if (code >= 0)
717 code = u16(p + 3, pe, (int *)&last);
718
719 if (code < 0)
720 break;
721
722 if (i >= first && i < last) {
723 return (int)(*(p + 2));
724 }
725 p += 3;
726 }
727 return_error(gs_error_rangecheck);
728 }
729
730
731 static byte *
732 pdfi_read_cff_real(byte *p, byte *e, float *val)
733 {
734 char buf[65];
735 char *txt = buf;
736
737 /* b0 was 30 */
738
739 while (txt < buf + (sizeof buf) - 5 && p < e) {
740 int b, n;
741
742 b = *p++;
743
744 n = (b >> 4) &0xf;
745 if (n < 0xA) {
746 *txt++ = n + '0';
747 }
748 else if (n == 0xA) {
749 *txt++ = '.';
750 }
751 else if (n == 0xB) {
752 *txt++ = 'E';
753 }
754 else if (n == 0xC) {
755 *txt++ = 'E';
756 *txt++ = '-';
757 }
758 else if (n == 0xE) {
759 *txt++ = '-';
760 }
761 else if (n == 0xF) {
762 break;
763 }
764
765 n = b &0xf;
766 if (n < 0xA) {
767 *txt++ = n + '0';
768 }
769 else if (n == 0xA) {
770 *txt++ = '.';
771 }
772 else if (n == 0xB) {
773 *txt++ = 'E';
774 }
775 else if (n == 0xC) {
776 *txt++ = 'E';
777 *txt++ = '-';
778 }
779 else if (n == 0xE) {
780 *txt++ = '-';
781 }
782 else if (n == 0xF) {
783 break;
784 }
785 }
786
787 *txt = 0;
788
789 *val = atof(buf);
790
791 return p;
792 }
793
794 static byte *
795 pdfi_read_cff_integer(byte *p, byte *e, int b0, int *val)
796 {
797 int b1, b2, b3, b4;
798
799 if (b0 == 28) {
800 if (p + 2 > e) {
801 gs_throw(-1, "corrupt dictionary (integer)");
802 return 0;
803 }
804 b1 = *p++;
805 b2 = *p++;
806 *val = (b1 << 8) | b2;
807 }
808
809 else if (b0 == 29) {
810 if (p + 4 > e) {
811 gs_throw(-1, "corrupt dictionary (integer)");
812 return 0;
813 }
814 b1 = *p++;
815 b2 = *p++;
816 b3 = *p++;
817 b4 = *p++;
818 *val = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
819 }
820
821 else if (b0 < 247) {
822 *val = b0 - 139;
823 }
824
825 else if (b0 < 251) {
826 if (p + 1 > e) {
827 gs_throw(-1, "corrupt dictionary (integer)");
828 return 0;
829 }
830 b1 = *p++;
831 *val = (b0 - 247) * 256 + b1 + 108;
832 }
833
834 else {
835 if (p + 1 > e) {
836 gs_throw(-1, "corrupt dictionary (integer)");
837 return 0;
838 }
839 b1 = *p++;
840 *val = -(b0 - 251) * 256 - b1 - 108;
841 }
842
843 return p;
844 }
845
846 static inline void
847 pdfi_cff_font_priv_defaults(pdfi_gs_cff_font_priv *ptpriv)
848 {
849 ptpriv->type1data.BlueScale = 0.039625f;
850 ptpriv->type1data.BlueShift = 7;
851 ptpriv->type1data.BlueFuzz = 1;
852 ptpriv->type1data.ExpansionFactor = 0.06f;
853 }
854
855 #define PDFI_CFF_STACK_SIZE 48
856
857 static int
858 pdfi_read_cff_dict(byte *p, byte *e, pdfi_gs_cff_font_priv *ptpriv, cff_font_offsets *offsets, bool topdict)
859 {
860 pdfi_cff_font_priv *font = &ptpriv->pdfcffpriv;
861 struct
862 {
863 int ival;
864 float fval;
865 } args[PDFI_CFF_STACK_SIZE];
866 int offset;
867 int b0, n;
868 double f;
869 int i;
870 int code = 0;
871 bool do_priv = false;
872
873 memset(args, 0x00, sizeof(args));
874
875 offset = p - font->cffdata;
876
877 n = 0;
878 while (p < e && code >= 0) {
879 b0 = *p;
880 p++;
881
882 switch (b0) {
883 case 22:
884 case 23:
885 case 24:
886 case 25:
887 case 26:
888 case 27:
889 case 31:
890 case 255:
891 continue;
892 default:
893 break;
894 }
895
896 if (b0 < 22) {
897 if (b0 == 12) {
898 if (p + 1 > e) {
899 return gs_throw(-1, "corrupt dictionary (operator)");
900 }
901 b0 = 0x100 | *p++;
902 }
903 switch (b0) {
904 case 1:
905 {
906 code = pdfi_make_string_from_sid(font->ctx, (pdf_obj **) &font->notice, font, offsets, args[0].ival);
907 break;
908 }
909 case 2:
910 {
911 code = pdfi_make_string_from_sid(font->ctx, (pdf_obj **) &font->fullname, font, offsets, args[0].ival);
912 break;
913 }
914 case 3:
915 {
916 code = pdfi_make_string_from_sid(font->ctx, (pdf_obj **) &font->familyname, font, offsets, args[0].ival);
917 break;
918 }
919 case 13: /* UniqueID */
920 /* UID may not be less than 0, that makes it an XUID */
921 if (args[0].ival >= 0)
922 uid_set_UniqueID(&ptpriv->UID, args[0].ival);
923 break;
924
925 case 14: /* XUID */
926 {
927 long *xvalues = NULL;
928
929 if (n > 0) {
930 xvalues = (long *)gs_alloc_byte_array(font->pfont->memory, n, sizeof(long), "pdfi_read_cff_dict");
931 if (xvalues == NULL) {
932 uid_set_invalid(&ptpriv->UID);
933 }
934 else {
935 for (i = 1; i <= n; i++) {
936 xvalues[n - i] = args[i - 1].ival;
937 }
938 if (uid_is_XUID(&ptpriv->UID))
939 uid_free(&ptpriv->UID, font->pfont->memory, "pdfi_read_cff_dict");
940 uid_set_XUID(&ptpriv->UID, xvalues, n);
941 }
942 } else
943 uid_set_invalid(&ptpriv->UID);
944 break;
945 }
946
947 /* some CFF file offsets */
948 case 15:
949 {
950 if (args[0].ival < 0) {
951 code = gs_note_error(gs_error_invalidfont);
952 break;
953 }
954 offsets->charset_off = args[0].ival;
955 break;
956 }
957 case 16:
958 {
959 if (args[0].ival < 0) {
960 code = gs_note_error(gs_error_invalidfont);
961 break;
962 }
963 offsets->encoding_off = args[0].ival;
964 break;
965 }
966 case 17:
967 {
968 if (args[0].ival < 0) {
969 code = gs_note_error(gs_error_invalidfont);
970 break;
971 }
972 font->charstrings = font->cffdata + args[0].ival;
973 break;
974 }
975
976 case 18:
977 {
978 offsets->private_size = args[0].ival;
979 if (args[1].ival < 0) {
980 code = gs_note_error(gs_error_invalidfont);
981 break;
982 }
983 offsets->private_off = args[1].ival;
984 /* Catch a broken font with a self referencing Private dict */
985 if (topdict == true)
986 do_priv = offsets->private_size > 0 ? true : false;
987 else {
988 do_priv = false;
989 code = gs_error_invalidfont;
990 break;
991 }
992 break;
993 }
994
995 case 19:
996 {
997 if (args[0].ival < 0) {
998 code = gs_note_error(gs_error_invalidfont);
999 break;
1000 }
1001 font->subrs = font->cffdata + offset + args[0].ival;
1002 break;
1003 }
1004
1005 case 256 | 30:
1006 {
1007 code = pdfi_make_string_from_sid(font->ctx, (pdf_obj **) &font->registry, font, offsets, args[0].ival);
1008 if (code < 0)
1009 break;
1010 code = pdfi_make_string_from_sid(font->ctx, (pdf_obj **) &font->ordering, font, offsets, args[1].ival);
1011 if (code < 0)
1012 break;
1013 font->supplement = args[2].ival;
1014 offsets->have_ros = true;
1015 ptpriv->FontType = ft_CID_encrypted;
1016 break;
1017 }
1018
1019 case 256 | 34:
1020 {
1021 font->cidcount = args[0].ival;
1022 break;
1023 }
1024
1025 case 256 | 35:
1026 {
1027 font->uidbase = args[0].ival;
1028 break;
1029 }
1030
1031 case 256 | 36:
1032 {
1033 offsets->fdarray_off = args[0].ival;
1034 break;
1035 }
1036
1037 case 256 | 37:
1038 {
1039 offsets->fdselect_off = args[0].ival;
1040 break;
1041 }
1042
1043 case 256 | 38:
1044 {
1045 pdf_string *fnamestr = NULL;
1046
1047 code = pdfi_make_string_from_sid(font->ctx, (pdf_obj **) &fnamestr, font, offsets, args[0].ival);
1048 if (code >= 0) {
1049 int nlen = fnamestr->length > gs_font_name_max ? gs_font_name_max : fnamestr->length;
1050 memcpy(ptpriv->font_name.chars, fnamestr->data, nlen);
1051 memcpy(ptpriv->key_name.chars, fnamestr->data, nlen);
1052 ptpriv->font_name.size = ptpriv->key_name.size = nlen;
1053 pdfi_countdown(fnamestr);
1054 }
1055 break;
1056 }
1057
1058 /* Type1 stuff that need to be set for the ptpriv struct */
1059
1060 case 256 | 6:
1061 {
1062 if (args[0].ival == 1) {
1063 ptpriv->type1data.interpret = gs_type1_interpret;
1064 ptpriv->type1data.lenIV = -1; /* FIXME */
1065 }
1066 break;
1067 }
1068
1069 case 256 | 7:
1070 {
1071 ptpriv->FontMatrix.xx = args[0].fval;
1072 ptpriv->FontMatrix.xy = args[1].fval;
1073 ptpriv->FontMatrix.yx = args[2].fval;
1074 ptpriv->FontMatrix.yy = args[3].fval;
1075 ptpriv->FontMatrix.tx = args[4].fval;
1076 ptpriv->FontMatrix.ty = args[5].fval;
1077 offsets->have_matrix = true;
1078 break;
1079 }
1080 case 5:
1081 {
1082 ptpriv->FontBBox.p.x = args[0].fval;
1083 ptpriv->FontBBox.p.y = args[1].fval;
1084 ptpriv->FontBBox.q.x = args[2].fval;
1085 ptpriv->FontBBox.q.y = args[3].fval;
1086 break;
1087 }
1088
1089 case 20:
1090 {
1091 ptpriv->type1data.defaultWidthX = float2fixed(args[0].fval);
1092 break;
1093 }
1094
1095 case 21:
1096 {
1097 ptpriv->type1data.nominalWidthX = float2fixed(args[0].fval);
1098 break;
1099 }
1100
1101 case 256 | 19:
1102 {
1103 ptpriv->type1data.initialRandomSeed = args[0].ival;
1104 break;
1105 }
1106
1107 case 6:
1108 {
1109 if (n > max_BlueValues * 2) n = max_BlueValues * 2;
1110 ptpriv->type1data.BlueValues.count = n;
1111 ptpriv->type1data.BlueValues.values[0] = args[0].fval;
1112 for (i = 1; i < n; i++) {
1113 ptpriv->type1data.BlueValues.values[i] = ptpriv->type1data.BlueValues.values[i - 1] + args[i].fval;
1114 }
1115 break;
1116 }
1117
1118 case 7:
1119 {
1120 if (n > max_OtherBlues * 2) n = max_OtherBlues * 2;
1121 ptpriv->type1data.OtherBlues.count = n;
1122 ptpriv->type1data.OtherBlues.values[0] = args[0].fval;
1123 for (i = 1; i < n; i++) {
1124 ptpriv->type1data.OtherBlues.values[i] = ptpriv->type1data.OtherBlues.values[i - 1] + args[i].fval;
1125 }
1126 break;
1127 }
1128
1129 case 8:
1130 {
1131 if (n > max_FamilyBlues * 2) n = max_FamilyBlues * 2;
1132 ptpriv->type1data.FamilyBlues.count = n;
1133 ptpriv->type1data.FamilyBlues.values[0] = args[0].fval;
1134 for (i = 1; i < n; i++) {
1135 ptpriv->type1data.FamilyBlues.values[i] = ptpriv->type1data.FamilyBlues.values[i - 1] + args[i].fval;
1136 }
1137 break;
1138 }
1139
1140 case 9:
1141 {
1142 if (n > max_FamilyOtherBlues * 2) n = max_FamilyOtherBlues * 2;
1143 ptpriv->type1data.FamilyOtherBlues.count = n;
1144 ptpriv->type1data.FamilyOtherBlues.values[0] = args[0].fval;
1145 for (i = 1; i < n; i++) {
1146 ptpriv->type1data.FamilyOtherBlues.values[i] = ptpriv->type1data.FamilyOtherBlues.values[i - 1] + args[i].fval;
1147 }
1148 break;
1149 }
1150
1151 case 10:
1152 {
1153 ptpriv->type1data.StdHW.count = 1;
1154 ptpriv->type1data.StdHW.values[0] = args[0].fval;
1155 break;
1156 }
1157
1158 case 11:
1159 {
1160 ptpriv->type1data.StdVW.count = 1;
1161 ptpriv->type1data.StdVW.values[0] = args[0].fval;
1162 break;
1163 }
1164
1165 case 256:
1166 {
1167 code = pdfi_make_string_from_sid(font->ctx, (pdf_obj **) &font->copyright, font, offsets, args[0].ival);
1168 break;
1169 }
1170
1171 case 256 | 9:
1172 {
1173 ptpriv->type1data.BlueScale = args[0].fval;
1174 break;
1175 }
1176
1177 case 256 | 10:
1178 {
1179 ptpriv->type1data.BlueShift = args[0].fval;
1180 break;
1181 }
1182
1183 case 256 | 11:
1184 {
1185 ptpriv->type1data.BlueFuzz = (int)args[0].fval;
1186 break;
1187 }
1188
1189 case 256 | 12:
1190 {
1191 if (n > max_StemSnap) n = max_StemSnap;
1192 ptpriv->type1data.StemSnapH.count = n;
1193 for (f = 0, i = 0; i < n; f += args[i].fval, i++)
1194 ptpriv->type1data.StemSnapH.values[i] = f;
1195 break;
1196 }
1197
1198 case 256 | 13:
1199 {
1200 if (n > max_StemSnap) n = max_StemSnap;
1201 ptpriv->type1data.StemSnapV.count = n;
1202 for (f = 0, i = 0; i < n; f += args[i].fval, i++)
1203 ptpriv->type1data.StemSnapV.values[i] = f;
1204 break;
1205 }
1206
1207 case 256 | 14:
1208 {
1209 ptpriv->type1data.ForceBold = args[0].ival;
1210 break;
1211 }
1212
1213 case 256 | 17:
1214 {
1215 ptpriv->type1data.LanguageGroup = args[0].ival;
1216 break;
1217 }
1218
1219 case 256 | 18:
1220 {
1221 ptpriv->type1data.ExpansionFactor = args[0].fval;
1222 break;
1223 }
1224 default:
1225 break;
1226 }
1227 n = 0;
1228 }
1229 else {
1230 if (b0 == 30) {
1231 p = pdfi_read_cff_real(p, e, &args[n].fval);
1232 if (!p) {
1233 dbgprintf("\nCFF: corrupt dictionary operand\n");
1234 break;
1235 }
1236 args[n].ival = (int)args[n].fval;
1237 n++;
1238 }
1239 else if (b0 == 28 || b0 == 29 || (b0 >= 32 && b0 <= 254)) {
1240 /* If we run out of data reading an integer at the very end of the stream, don't throw an error
1241 just return.
1242 */
1243 bool near_end = ((e - p) <= 4);
1244 p = pdfi_read_cff_integer(p, e, b0, &args[n].ival);
1245 if (!p) {
1246 if (!near_end)
1247 code = gs_note_error(gs_error_invalidfont);
1248 dbgprintf("\nCFF: corrupt dictionary operand\n");
1249 break;
1250 }
1251 args[n].fval = (float)args[n].ival;
1252 n++;
1253 }
1254 else {
1255 dbgprintf1("CFF: corrupt dictionary operand (b0 = %d)", b0);
1256 }
1257 }
1258 if (n >= PDFI_CFF_STACK_SIZE) {
1259 code = gs_error_invalidfont;
1260 break;
1261 }
1262 }
1263
1264 /* recurse for the private dictionary */
1265 if (do_priv && code >= 0) {
1266 byte *dend = font->cffdata + offsets->private_off + offsets->private_size;
1267
1268 if (dend > font->cffend)
1269 dend = font->cffend;
1270
1271 if (p == NULL)
1272 code = gs_error_invalidfont;
1273 else
1274 code = pdfi_read_cff_dict(font->cffdata + offsets->private_off, dend, ptpriv, offsets, false);
1275
1276 if (code < 0)
1277 dbgprintf("CFF: cannot read private dictionary");
1278 }
1279
1280 return code;
1281 }
1282
1283 /*
1284 * Get the number of items in an INDEX, and return
1285 * a pointer to the end of the INDEX or NULL on
1286 * failure.
1287 */
1288 static byte *
1289 pdfi_count_cff_index(byte *p, byte *e, int *countp)
1290 {
1291 int count, offsize, last;
1292 int code;
1293
1294 if (p + 3 > e) {
1295 gs_throw(-1, "not enough data for index header");
1296 return 0;
1297 }
1298
1299 if ((code = u16(p, e, &count)) < 0)
1300 return NULL;
1301
1302 p += 2;
1303 *countp = count;
1304
1305 if (count == 0)
1306 return p;
1307
1308 offsize = *p++;
1309
1310 if (offsize < 1 || offsize > 4) {
1311 gs_throw(-1, "corrupt index header");
1312 return 0;
1313 }
1314
1315 if (p + count * offsize > e) {
1316 gs_throw(-1, "not enough data for index offset table");
1317 return 0;
1318 }
1319
1320 p += count * offsize;
1321 code = uofs(p, e, offsize, &last);
1322 p += offsize;
1323 p--; /* stupid offsets */
1324
1325 if (last < 0 || code < 0) {
1326 gs_throw(-1, "corrupt index");
1327 return 0;
1328 }
1329
1330 if (p + last - 1 > e) {
1331 gs_throw(-1, "not enough data for index data");
1332 return 0;
1333 }
1334
1335 p += last;
1336
1337 return p;
1338 }
1339
1340 /*
1341 * Locate and store pointers to the data of an
1342 * item in the index that starts at 'p'.
1343 * Return pointer to the end of the index,
1344 * or NULL on failure.
1345 */
1346 static byte *
1347 pdfi_find_cff_index(byte *p, byte *e, int idx, byte ** pp, byte ** ep)
1348 {
1349 int code, count, offsize, sofs, eofs, last;
1350
1351 if (p == NULL)
1352 return 0;
1353
1354 if (p + 3 > e) {
1355 gs_throw(-1, "not enough data for index header");
1356 return 0;
1357 }
1358
1359 if (u16(p, e, &count) < 0)
1360 return NULL;
1361
1362 p += 2;
1363 if (count == 0)
1364 return 0;
1365
1366 offsize = *p++;
1367
1368 if (offsize < 1 || offsize > 4) {
1369 gs_throw(-1, "corrupt index header");
1370 return 0;
1371 }
1372
1373 if (p + count * offsize > e) {
1374 gs_throw(-1, "not enough data for index offset table");
1375 return 0;
1376 }
1377
1378 if (idx < 0 || idx >= count) {
1379 gs_throw(-1, "tried to access non-existing index item");
1380 return 0;
1381 }
1382
1383 code = uofs(p + idx * offsize, e, offsize, &sofs);
1384 if (code >= 0)
1385 code = uofs(p + (idx + 1) * offsize, e, offsize, &eofs);
1386 if (code >= 0)
1387 code = uofs(p + count * offsize, e, offsize, &last);
1388
1389 if (code < 0) {
1390 gs_throw(-1, "not enough data for index data");
1391 return 0;
1392 }
1393
1394 p += count * offsize;
1395 p += offsize;
1396 p--; /* stupid offsets */
1397
1398 if (p + last - 1 > e) {
1399 gs_throw(-1, "not enough data for index data");
1400 return 0;
1401 }
1402
1403 if (sofs < 0 || eofs < 0 || sofs > eofs || eofs > last) {
1404 gs_throw(-1, "corrupt index offset table");
1405 return 0;
1406 }
1407
1408 *pp = p + sofs;
1409 *ep = p + eofs;
1410
1411 return p + last;
1412 }
1413
1414 static int
1415 pdfi_make_name_from_sid(pdf_context *ctx, pdf_obj ** nm, pdfi_cff_font_priv *font, cff_font_offsets *offsets, unsigned int sid)
1416 {
1417 gs_string str;
1418 byte *p;
1419
1420 if (sid < gs_c_known_encoding_lengths[10]) {
1421 gs_glyph gl = gs_c_known_encode(sid, 10);
1422
1423 (void)gs_c_glyph_name(gl, (gs_const_string *) &str);
1424 }
1425 else {
1426 byte *strp, *stre;
1427
1428 p = pdfi_find_cff_index(font->cffdata + offsets->strings_off, font->cffend, sid - gs_c_known_encoding_lengths[10], &strp, &stre);
1429 if (p == NULL)
1430 return_error(gs_error_rangecheck);
1431 str.data = strp;
1432 str.size = stre - strp;
1433 }
1434 return pdfi_name_alloc(ctx, str.data, str.size, nm);
1435 }
1436
1437 static int
1438 pdfi_make_string_from_sid(pdf_context *ctx, pdf_obj ** s0, pdfi_cff_font_priv *font, cff_font_offsets *offsets, unsigned int sid)
1439 {
1440 byte *p;
1441 int code;
1442 gs_string str;
1443 pdf_string *s = NULL;
1444
1445 if (sid < gs_c_known_encoding_lengths[10]) {
1446 gs_glyph gl = gs_c_known_encode(sid, 10);
1447
1448 (void)gs_c_glyph_name(gl, (gs_const_string *) &str);
1449 }
1450 else {
1451 byte *strp, *stre;
1452
1453 p = pdfi_find_cff_index(font->cffdata + offsets->strings_off, font->cffend,
1454 sid - gs_c_known_encoding_lengths[10], &strp, &stre);
1455 if (p == NULL)
1456 return_error(gs_error_rangecheck);
1457 str.data = strp;
1458 str.size = stre - strp;
1459 }
1460 code = pdfi_object_alloc(ctx, PDF_STRING, str.size, (pdf_obj **) &s);
1461 if (code < 0)
1462 return code;
1463 pdfi_countup(s);
1464 memcpy(s->data, str.data, str.size);
1465 s->length = str.size;
1466
1467 *s0 = (pdf_obj *) s;
1468 return 0;
1469 }
1470
1471 static int
1472 pdfi_cff_build_encoding(pdf_context *ctx, pdfi_gs_cff_font_priv *ptpriv, cff_font_offsets *offsets,
1473 int (*charset_proc)(const byte *p, const byte *pe, unsigned int i))
1474 {
1475 pdfi_cff_font_priv *font = &ptpriv->pdfcffpriv;
1476 int code = 0;
1477 byte *s, *e, *lp;
1478 pdf_string *pstr;
1479 unsigned int i, gid, enc_format = 0;
1480 int sid;
1481 pdf_name *ndname = NULL;
1482 unsigned char gid2char[256];
1483 unsigned supp_enc_offset = 0;
1484
1485 if (offsets->encoding_off <= 1) {
1486 /* Either standard or expert encoding */
1487 pdf_name *enm = NULL;
1488 const char *const stdenc = "StandardEncoding";
1489 const char *const expenc = "MacExpertEncoding";
1490 char const *enctouse;
1491
1492 if (offsets->encoding_off < 1) {
1493 enctouse = stdenc;
1494 }
1495 else {
1496 enctouse = expenc;
1497 }
1498 code = pdfi_name_alloc(ctx, (byte *) enctouse, strlen(enctouse), (pdf_obj **) &enm);
1499 if (code >= 0) {
1500 pdfi_countup(enm);
1501 code = pdfi_create_Encoding(ctx, (pdf_font *)font, (pdf_obj *) enm, NULL, (pdf_obj **) &font->Encoding);
1502 pdfi_countdown(enm);
1503 }
1504 }
1505 else {
1506 if (font->cffdata + offsets->encoding_off > font->cffend) {
1507 code = gs_note_error(gs_error_invalidfont);
1508 }
1509 else {
1510 code = pdfi_object_alloc(ctx, PDF_ARRAY, 256, (pdf_obj **) &font->Encoding);
1511 if (code < 0)
1512 return code;
1513
1514 code = pdfi_name_alloc(ctx, (byte *) ".notdef", 7, (pdf_obj **) &ndname);
1515 if (code < 0)
1516 return code;
1517
1518
1519 pdfi_countup(font->Encoding);
1520 pdfi_countup(ndname);
1521 code = 0;
1522 /* Prepopulate with notdefs */
1523 for (i = 0; i < 256 && code >= 0; i++) {
1524 code = pdfi_array_put(ctx, font->Encoding, (uint64_t) i, (pdf_obj *) ndname);
1525 }
1526
1527 if (code >= 0) {
1528 byte *p = font->cffdata + offsets->encoding_off;
1529
1530 enc_format = p[0];
1531
1532 lp = pdfi_find_cff_index(font->charstrings, font->cffend, 0, &s, &e);
1533 if (lp == NULL) {
1534 code = gs_note_error(gs_error_rangecheck);
1535 goto done;
1536 }
1537 code = pdfi_object_alloc(ctx, PDF_STRING, e - s, (pdf_obj **) &pstr);
1538 if (code < 0)
1539 goto done;
1540 memcpy(pstr->data, s, e - s);
1541 pdfi_countup(pstr);
1542 code =
1543 pdfi_dict_put_obj(ctx, font->CharStrings, (pdf_obj *) ndname, (pdf_obj *) pstr, true);
1544 pdfi_countdown(pstr);
1545 if (code < 0) {
1546 goto done;
1547 }
1548 pdfi_countdown(ndname);
1549 ndname = NULL; /* just to avoid bad things! */
1550
1551 if ((enc_format &0x7f) == 0) {
1552 unsigned int n_codes = p[1];
1553
1554 if (p + 2 + n_codes > font->cffend) {
1555 return_error(gs_error_invalidfont);
1556 }
1557 gid2char[0] = 0;
1558 for (i = 0; i < n_codes; i++) {
1559 gid2char[i + 1] = p[2 + i];
1560 }
1561 memset(gid2char + n_codes + 1, 0, sizeof(gid2char) - n_codes - 1);
1562 supp_enc_offset = 2 + n_codes;
1563 }
1564 else if ((enc_format &0x7f) == 1) {
1565 unsigned int n_ranges = p[1];
1566 unsigned int first, left, j, k = 1;
1567
1568 if (p + 2 + 2 * n_ranges > font->cffend) {
1569 return_error(gs_error_invalidfont);
1570 }
1571 gid2char[0] = 0;
1572 for (i = 0; i < n_ranges; i++) {
1573 first = p[2 + 2 * i];
1574 left = p[3 + 2 * i];
1575 for (j = 0; j <= left && k < 256; j++)
1576 gid2char[k++] = first + j;
1577 }
1578 memset(gid2char + k, 0, sizeof(gid2char) - k);
1579 supp_enc_offset = 2 * n_ranges + 2;
1580 }
1581 else {
1582 return_error(gs_error_rangecheck);
1583 }
1584 }
1585 }
1586 }
1587 if (code >= 0) {
1588 pdf_obj *gname;
1589
1590 code = 0;
1591
1592 lp = pdfi_find_cff_index(font->charstrings, font->cffend, 0, &s, &e);
1593 if (lp == NULL) {
1594 code = gs_note_error(gs_error_rangecheck);
1595 goto done;
1596 }
1597 code = pdfi_object_alloc(ctx, PDF_STRING, e - s, (pdf_obj **) &pstr);
1598 if (code < 0)
1599 goto done;
1600 memcpy(pstr->data, s, e - s);
1601 pdfi_countup(pstr);
1602 if (ptpriv->forcecid) {
1603 char buf[40];
1604 int len = gs_snprintf(buf, sizeof(buf), "%d", 0);
1605
1606 code = pdfi_name_alloc(ctx, (byte *) buf, len, &gname);
1607 if (code < 0) {
1608 pdfi_countdown(pstr);
1609 return code;
1610 }
1611 pdfi_countup(gname);
1612 }
1613 else {
1614 code = pdfi_name_alloc(ctx, (byte *) ".notdef", 7, &gname);
1615 if (code < 0) {
1616 pdfi_countdown(pstr);
1617 goto done;
1618 }
1619 pdfi_countup(gname);
1620 }
1621 code = pdfi_dict_put_obj(ctx, font->CharStrings, gname, (pdf_obj *) pstr, true);
1622 pdfi_countdown(pstr);
1623 pdfi_countdown(gname);
1624 if (code < 0)
1625 goto done;
1626
1627 for (gid = 1; gid < font->ncharstrings && code >= 0; gid++) {
1628
1629 lp = pdfi_find_cff_index(font->charstrings, font->cffend, gid, &s, &e);
1630 if (lp == NULL) {
1631 code = gs_note_error(gs_error_rangecheck);
1632 continue;
1633 }
1634 code = pdfi_object_alloc(ctx, PDF_STRING, e - s, (pdf_obj **) &pstr);
1635 if (code < 0)
1636 return code;
1637 memcpy(pstr->data, s, e - s);
1638 pdfi_countup(pstr);
1639
1640 if (ptpriv->forcecid) {
1641 char buf[40];
1642 int len = gs_snprintf(buf, sizeof(buf), "%d", gid);
1643
1644 code = pdfi_name_alloc(ctx, (byte *) buf, len, &gname);
1645 if (code < 0) {
1646 pdfi_countdown(pstr);
1647 return code;
1648 }
1649 }
1650 else {
1651 sid = (*charset_proc) (font->cffdata + offsets->charset_off + 1, font->cffend, gid - 1);
1652 if (sid < 0) {
1653 pdfi_countdown(pstr);
1654 return sid;
1655 }
1656 if ((code = pdfi_make_name_from_sid(ctx, &gname, font, offsets, sid)) < 0) {
1657 char buf[40];
1658 int len = gs_snprintf(buf, sizeof(buf), "sid-%d", sid);
1659
1660 code = pdfi_name_alloc(ctx, (byte *) buf, len, &gname);
1661 if (code < 0) {
1662 pdfi_countdown(pstr);
1663 return code;
1664 }
1665 }
1666 }
1667 pdfi_countup(gname);
1668 code = pdfi_dict_put_obj(ctx, font->CharStrings, gname, (pdf_obj *) pstr, true);
1669 pdfi_countdown(pstr);
1670 if (code < 0) {
1671 pdfi_countdown(gname);
1672 return code;
1673 }
1674 if (offsets->encoding_off > 1 && gid < 256) {
1675 code = pdfi_array_put(ctx, font->Encoding, (int64_t) gid2char[gid], gname);
1676 }
1677 pdfi_countdown(gname);
1678 }
1679
1680 if (offsets->encoding_off > 1 && (enc_format & 0x80)) {
1681 unsigned int n_supp, charcode, sid;
1682 byte *p = font->cffdata + offsets->encoding_off + supp_enc_offset;
1683 pdf_obj *gname;
1684
1685 n_supp = p[0];
1686
1687 for (i = 0; i < n_supp && code >= 0; i++) {
1688 charcode = p[1 + 3 * i];
1689 code = u16(p + 2 + 3 * i, e, (int *)&sid);
1690 if (code < 0) continue;
1691
1692 if ((code = pdfi_make_name_from_sid(ctx, &gname, font, offsets, sid)) < 0) {
1693 char buf[40];
1694 int len = gs_snprintf(buf, sizeof(buf), "sid-%d", sid);
1695
1696 if (len > 0)
1697 code = pdfi_name_alloc(ctx, (byte *) buf, len, &gname);
1698 else
1699 code = 0;
1700
1701 if (code < 0)
1702 continue;
1703 }
1704 pdfi_countup(gname);
1705 code = pdfi_array_put(ctx, font->Encoding, (int64_t) charcode, gname);
1706 pdfi_countdown(gname);
1707 }
1708 }
1709 }
1710 done:
1711 if (code < 0) {
1712 pdfi_countdown(ndname);
1713 }
1714 return code;
1715 }
1716
1717
1718 /*
1719 * Scan the CFF file structure and extract important data.
1720 */
1721
1722 static int
1723 pdfi_read_cff(pdf_context *ctx, pdfi_gs_cff_font_priv *ptpriv)
1724 {
1725 pdfi_cff_font_priv *font = &ptpriv->pdfcffpriv;
1726 byte *pstore, *p = font->cffdata;
1727 byte *e = font->cffend - 1;
1728 byte *dictp, *dicte;
1729 byte *strp, *stre;
1730 byte *nms, *nmp, *nme;
1731 int count;
1732 int i, code = 0;
1733 cff_font_offsets offsets = { 0 };
1734 int (*charset_proc)(const byte *p, const byte *pe, unsigned int i);
1735 int major, minor, hdrsize;
1736
1737 /* CFF header */
1738 if (p + 4 > e)
1739 return gs_throw(gs_error_invalidfont, "not enough data for header");
1740
1741 major = *p;
1742 minor = *(p + 1);
1743 hdrsize = *(p + 2);
1744
1745 if (major != 1 || minor != 0)
1746 return gs_throw(gs_error_invalidfont, "not a CFF 1.0 file");
1747
1748 if (p + hdrsize > e)
1749 return gs_throw(gs_error_invalidfont, "not enough data for extended header");
1750 p += hdrsize;
1751
1752 /* Name INDEX */
1753 nms = p;
1754 p = pdfi_count_cff_index(p, e, &count);
1755 if (p == NULL)
1756 return gs_throw(gs_error_invalidfont, "cannot read name index");
1757 if (count != 1)
1758 return gs_throw(gs_error_invalidfont, "file did not contain exactly one font");
1759
1760 nms = pdfi_find_cff_index(nms, e, 0, &nmp, &nme);
1761 if (!nms)
1762 return gs_throw(gs_error_invalidfont, "cannot read names index");
1763 else {
1764 int len = nme - nmp < sizeof(ptpriv->key_name.chars) ? nme - nmp : sizeof(ptpriv->key_name.chars);
1765 memcpy(ptpriv->key_name.chars, nmp, len);
1766 memcpy(ptpriv->font_name.chars, nmp, len);
1767 ptpriv->key_name.size = ptpriv->font_name.size = len;
1768 }
1769
1770 /* Top Dict INDEX */
1771 p = pdfi_find_cff_index(p, e, 0, &dictp, &dicte);
1772 if (p == NULL)
1773 return gs_throw(gs_error_invalidfont, "cannot read top dict index");
1774
1775 /* String index */
1776 pstore = p;
1777 p = pdfi_find_cff_index(p, e, 0, &strp, &stre);
1778
1779 offsets.strings_off = pstore - font->cffdata;
1780
1781 p = pdfi_count_cff_index(pstore, e, &count);
1782 if (p == NULL)
1783 return_error(gs_error_invalidfont);
1784
1785 offsets.strings_size = (unsigned int)count;
1786
1787 /* Global Subr INDEX */
1788 font->gsubrs = p;
1789 p = pdfi_count_cff_index(p, e, &font->NumGlobalSubrs);
1790 if (p == NULL) {
1791 font->GlobalSubrs = NULL;
1792 font->NumGlobalSubrs = 0;
1793 }
1794 /* Read the top and private dictionaries */
1795 pdfi_cff_font_priv_defaults(ptpriv);
1796 code = pdfi_read_cff_dict(dictp, dicte, ptpriv, &offsets, true);
1797 if (code < 0)
1798 return gs_rethrow(code, "cannot read top dictionary");
1799
1800 /* Check the subrs index */
1801 font->NumSubrs = 0;
1802 if (font->subrs) {
1803 p = pdfi_count_cff_index(font->subrs, e, &font->NumSubrs);
1804 if (p == NULL || font->NumSubrs > 65536) {
1805 font->Subrs = NULL;
1806 font->NumSubrs = 0;
1807 }
1808 else {
1809 ptpriv->type1data.subroutineNumberBias = subrbias(font->NumSubrs);
1810 }
1811 }
1812
1813
1814 font->GlobalSubrs = NULL;
1815 if (font->NumGlobalSubrs > 0 && font->NumGlobalSubrs <= 65536) {
1816 ptpriv->type1data.gsubrNumberBias = subrbias(font->NumGlobalSubrs);
1817 code = pdfi_object_alloc(ctx, PDF_ARRAY, font->NumGlobalSubrs, (pdf_obj **) &font->GlobalSubrs);
1818 if (code >= 0) {
1819 font->GlobalSubrs->refcnt = 1;
1820 for (i = 0; i < font->NumGlobalSubrs; i++) {
1821 pdf_string *gsubrstr;
1822
1823 p = pdfi_find_cff_index(font->gsubrs, font->cffend, i, &strp, &stre);
1824 if (p) {
1825 code = pdfi_object_alloc(ctx, PDF_STRING, stre - strp, (pdf_obj **) &gsubrstr);
1826 if (code >= 0) {
1827 memcpy(gsubrstr->data, strp, gsubrstr->length);
1828 code =
1829 pdfi_array_put(ctx, font->GlobalSubrs, (uint64_t) i,
1830 (pdf_obj *) gsubrstr);
1831 if (code < 0) {
1832 gsubrstr->refcnt = 1;
1833 pdfi_countdown(gsubrstr);
1834 }
1835 }
1836 }
1837 else {
1838 code = pdfi_array_put(ctx, font->GlobalSubrs, (uint64_t) i, PDF_NULL_OBJ);
1839 if (code < 0) {
1840 pdfi_countdown(font->GlobalSubrs);
1841 font->GlobalSubrs = NULL;
1842 }
1843 }
1844 }
1845 }
1846 }
1847
1848 font->Subrs = NULL;
1849 if (font->NumSubrs > 0) {
1850 code = pdfi_object_alloc(ctx, PDF_ARRAY, font->NumSubrs, (pdf_obj **) &font->Subrs);
1851 if (code >= 0 && font->Subrs != NULL) {
1852 font->Subrs->refcnt = 1;
1853 for (i = 0; i < font->NumSubrs; i++) {
1854 pdf_string *subrstr;
1855
1856 p = pdfi_find_cff_index(font->subrs, font->cffend, i, &strp, &stre);
1857 if (p) {
1858 code = pdfi_object_alloc(ctx, PDF_STRING, stre - strp, (pdf_obj **) &subrstr);
1859 if (code >= 0) {
1860 memcpy(subrstr->data, strp, subrstr->length);
1861 code = pdfi_array_put(ctx, font->Subrs, (uint64_t) i, (pdf_obj *) subrstr);
1862 if (code < 0) {
1863 subrstr->refcnt = 1;
1864 pdfi_countdown(subrstr);
1865 }
1866 }
1867 }
1868 else {
1869 code = pdfi_array_put(ctx, font->Subrs, (uint64_t) i, PDF_NULL_OBJ);
1870 if (code < 0) {
1871 pdfi_countdown(font->Subrs);
1872 font->Subrs = NULL;
1873 font->NumSubrs = 0;
1874 break;
1875 }
1876 }
1877 }
1878 }
1879 }
1880
1881 /* Check the charstrings index */
1882 if (font->charstrings) {
1883 p = pdfi_count_cff_index(font->charstrings, e, &font->ncharstrings);
1884 if (!p || font->ncharstrings > 65535)
1885 return gs_rethrow(-1, "cannot read charstrings index");
1886 }
1887 code = pdfi_object_alloc(ctx, PDF_DICT, font->ncharstrings, (pdf_obj **) &font->CharStrings);
1888 if (code < 0)
1889 return code;
1890 pdfi_countup(font->CharStrings);
1891
1892 switch (offsets.charset_off) {
1893 case 0:
1894 charset_proc = iso_adobe_charset_proc;
1895 break;
1896 case 1:
1897 charset_proc = expert_charset_proc;
1898 break;
1899 case 2:
1900 charset_proc = expert_subset_charset_proc;
1901 break;
1902 default:{
1903 if (font->cffdata + offsets.charset_off >= font->cffend)
1904 return_error(gs_error_rangecheck);
1905
1906 switch ((int)font->cffdata[offsets.charset_off]) {
1907 case 0:
1908 charset_proc = format0_charset_proc;
1909 break;
1910 case 1:
1911 charset_proc = format1_charset_proc;
1912 break;
1913 case 2:
1914 charset_proc = format2_charset_proc;
1915 break;
1916 default:
1917 return_error(gs_error_rangecheck);
1918 }
1919 }
1920 }
1921
1922 if (offsets.have_ros) { /* CIDFont */
1923 int fdarray_size;
1924 bool topdict_matrix = offsets.have_matrix;
1925 int (*fdselect_proc)(const byte *p, const byte *pe, unsigned int i);
1926
1927 p = pdfi_count_cff_index(font->cffdata + offsets.fdarray_off, e, &fdarray_size);
1928 if (!p || fdarray_size < 1 || fdarray_size > 64) /* 64 is arbitrary, but seems a reasonable upper limit */
1929 return gs_rethrow(-1, "cannot read charstrings index");
1930
1931 ptpriv->cidata.FDBytes = 1; /* Basically, always 1 just now */
1932
1933 ptpriv->cidata.FDArray = (gs_font_type1 **) gs_alloc_bytes(ctx->memory, (size_t)fdarray_size * sizeof(gs_font_type1 *), "pdfi_read_cff(fdarray)");
1934 if (!ptpriv->cidata.FDArray)
1935 return_error(gs_error_VMerror);
1936 ptpriv->cidata.FDArray_size = fdarray_size;
1937
1938 code = pdfi_object_alloc(ctx, PDF_ARRAY, fdarray_size, (pdf_obj **) &font->FDArray);
1939 if (code < 0) {
1940 gs_free_object(ctx->memory, ptpriv->cidata.FDArray, "pdfi_read_cff(fdarray)");
1941 ptpriv->cidata.FDArray = NULL;
1942 }
1943 else {
1944 pdfi_countup(font->FDArray);
1945 code = 0;
1946 for (i = 0; i < fdarray_size && code == 0; i++) {
1947 byte *fddictp, *fddicte;
1948 pdfi_gs_cff_font_priv fdptpriv = { 0 };
1949 pdf_font_cff *pdffont = NULL;
1950 gs_font_type1 *pt1font;
1951
1952 pdfi_init_cff_font_priv(ctx, &fdptpriv, font->cffdata, (font->cffend - font->cffdata), true);
1953
1954 offsets.private_off = 0;
1955
1956 p = pdfi_find_cff_index(font->cffdata + offsets.fdarray_off, e, i, &fddictp, &fddicte);
1957 if (!p) {
1958 ptpriv->cidata.FDArray[i] = NULL;
1959 code = gs_note_error(gs_error_invalidfont);
1960 continue;
1961 }
1962 if (fddicte > font->cffend)
1963 fddicte = font->cffend;
1964
1965 code = pdfi_read_cff_dict(fddictp, fddicte, &fdptpriv, &offsets, true);
1966 if (code < 0) {
1967 ptpriv->cidata.FDArray[i] = NULL;
1968 code = gs_note_error(gs_error_invalidfont);
1969 continue;
1970 }
1971 code = pdfi_alloc_cff_font(ctx, &pdffont, 0, true);
1972 if (code < 0) {
1973 ptpriv->cidata.FDArray[i] = NULL;
1974 code = gs_note_error(gs_error_invalidfont);
1975 continue;
1976 }
1977 pt1font = (gs_font_type1 *) pdffont->pfont;
1978 memcpy(pt1font, &fdptpriv, sizeof(pdfi_gs_cff_font_common_priv));
1979 memcpy(&pt1font->data, &fdptpriv.type1data, sizeof(fdptpriv.type1data));
1980 pt1font->base = (gs_font *) pdffont->pfont;
1981
1982 if (!topdict_matrix && offsets.have_matrix) {
1983 gs_matrix newfmat, onekmat = { 1000, 0, 0, 1000, 0, 0 };
1984 code = gs_matrix_multiply(&onekmat, &pt1font->FontMatrix, &newfmat);
1985 memcpy(&pt1font->FontMatrix, &newfmat, sizeof(newfmat));
1986 }
1987
1988 pt1font->FAPI = NULL;
1989 pt1font->client_data = pdffont;
1990
1991 /* Check the subrs index */
1992 pdffont->Subrs = NULL;
1993 if (fdptpriv.pdfcffpriv.subrs) {
1994 p = pdfi_count_cff_index(fdptpriv.pdfcffpriv.subrs, e, &pdffont->NumSubrs);
1995 if (!p) {
1996 pdffont->Subrs = NULL;
1997 pdffont->NumSubrs = 0;
1998 }
1999 }
2000
2001 if (pdffont->NumSubrs > 0) {
2002 code = pdfi_object_alloc(ctx, PDF_ARRAY, pdffont->NumSubrs, (pdf_obj **) &pdffont->Subrs);
2003 if (code >= 0) {
2004 int j;
2005
2006 pdffont->Subrs->refcnt = 1;
2007 for (j = 0; j < pdffont->NumSubrs; j++) {
2008 pdf_string *subrstr;
2009
2010 p = pdfi_find_cff_index(fdptpriv.pdfcffpriv.subrs, e, j, &strp, &stre);
2011 if (p) {
2012 code = pdfi_object_alloc(ctx, PDF_STRING, stre - strp, (pdf_obj **) &subrstr);
2013 if (code >= 0) {
2014 memcpy(subrstr->data, strp, subrstr->length);
2015 code = pdfi_array_put(ctx, pdffont->Subrs, (uint64_t) j, (pdf_obj *) subrstr);
2016 if (code < 0) {
2017 subrstr->refcnt = 1;
2018 pdfi_countdown(subrstr);
2019 }
2020 }
2021 }
2022 }
2023 }
2024 }
2025
2026 pdffont->GlobalSubrs = font->GlobalSubrs;
2027 pdffont->NumGlobalSubrs = font->NumGlobalSubrs;
2028 pdfi_countup(pdffont->GlobalSubrs);
2029 pdffont->CharStrings = font->CharStrings;
2030 pdfi_countup(pdffont->CharStrings);
2031 pt1font->data.subroutineNumberBias = subrbias(pdffont->NumSubrs);
2032 pt1font->data.gsubrNumberBias = subrbias(pdffont->NumGlobalSubrs);
2033
2034 ptpriv->cidata.FDArray[i] = pt1font;
2035 (void)pdfi_array_put(ctx, font->FDArray, i, (pdf_obj *) pdffont);
2036 pdfi_countdown(pdffont);
2037 }
2038 if (code < 0) {
2039 pdfi_countdown(font->FDArray);
2040 font->FDArray = NULL;
2041 for (i = 0; i < ptpriv->cidata.FDArray_size; i++) {
2042 ptpriv->cidata.FDArray[i] = NULL;
2043 }
2044 }
2045 else {
2046 if (font->cffdata + offsets.fdselect_off > font->cffend)
2047 return_error(gs_error_rangecheck);
2048
2049 switch ((int)font->cffdata[offsets.fdselect_off]) {
2050 case 0:
2051 fdselect_proc = format0_fdselect_proc;
2052 break;
2053 case 3:
2054 fdselect_proc = format3_fdselect_proc;
2055 break;
2056 default:
2057 return_error(gs_error_rangecheck);
2058 }
2059
2060 if (font->ncharstrings > 0) {
2061 int maxcid = 0;
2062 for (i = 0; i < font->ncharstrings; i++) {
2063 int fd, g;
2064 char gkey[64];
2065 pdf_string *charstr;
2066
2067 fd = fdarray_size <= 1 ? 0 : (*fdselect_proc) (font->cffdata + offsets.fdselect_off + 1, font->cffend, i);
2068
2069 p = pdfi_find_cff_index(font->charstrings, font->cffend, i, &strp, &stre);
2070 if (!p)
2071 continue;
2072
2073 code = pdfi_object_alloc(ctx, PDF_STRING, (stre - strp) + 1, (pdf_obj **) &charstr);
2074 if (code < 0)
2075 continue;
2076 charstr->data[0] = (byte) fd;
2077 memcpy(charstr->data + 1, strp, charstr->length - 1);
2078
2079 if (i == 0) {
2080 g = 0;
2081 }
2082 else {
2083 g = (*charset_proc) (font->cffdata + offsets.charset_off + 1, font->cffend, i - 1);
2084 }
2085
2086 if (g > maxcid) maxcid = g;
2087 gs_snprintf(gkey, sizeof(gkey), "%d", g);
2088 code = pdfi_dict_put_unchecked(ctx, font->CharStrings, gkey, (pdf_obj *) charstr);
2089 }
2090 if (maxcid > ptpriv->pdfcffpriv.cidcount - 1)
2091 ptpriv->pdfcffpriv.cidcount = maxcid + 1;
2092 }
2093 }
2094 }
2095 }
2096 else {
2097 code = pdfi_cff_build_encoding(ctx, ptpriv, &offsets, charset_proc);
2098 }
2099 return code;
2100 }
2101
2102 static int
2103 pdfi_alloc_cff_cidfont(pdf_context *ctx, pdf_cidfont_type0 ** font, uint32_t obj_num)
2104 {
2105 pdf_cidfont_type0 *cffcidfont = NULL;
2106 gs_font_cid0 *pfont = NULL;
2107 gs_matrix defmat = { 0.001f, 0.0f, 0.0f, 0.001f, 0.0f, 0.0f };
2108
2109 cffcidfont = (pdf_cidfont_type0 *) gs_alloc_bytes(ctx->memory, sizeof(pdf_cidfont_type0), "pdfi (cff pdf_cidfont_type0)");
2110 if (cffcidfont == NULL)
2111 return_error(gs_error_VMerror);
2112
2113 memset(cffcidfont, 0x00, sizeof(pdf_cidfont_type0));
2114 cffcidfont->ctx = ctx;
2115 cffcidfont->type = PDF_FONT;
2116 cffcidfont->pdfi_font_type = e_pdf_cidfont_type0;
2117
2118 #if REFCNT_DEBUG
2119 cffcidfont->UID = ctx->UID++;
2120 outprintf(ctx->memory, "Allocated object of type %c with UID %" PRIi64 "\n", cffcidfont->type,
2121 cffcidfont->UID);
2122 #endif
2123
2124 pdfi_countup(cffcidfont);
2125
2126 pfont = (gs_font_cid0 *) gs_alloc_struct(ctx->memory, gs_font_cid0, &st_gs_font_cid0, "pdfi (cff cid pfont)");
2127 if (pfont == NULL) {
2128 pdfi_countdown(cffcidfont);
2129 return_error(gs_error_VMerror);
2130 }
2131 memset(pfont, 0x00, sizeof(gs_font_cid0));
2132
2133 cffcidfont->pfont = (gs_font_base *) pfont;
2134 memcpy(&pfont->orig_FontMatrix, &defmat, sizeof(defmat));
2135 memcpy(&pfont->FontMatrix, &defmat, sizeof(defmat));
2136 pfont->next = pfont->prev = 0;
2137 pfont->memory = ctx->memory;
2138 pfont->dir = ctx->font_dir;
2139 pfont->is_resource = false;
2140 gs_notify_init(&pfont->notify_list, ctx->memory);
2141 pfont->base = (gs_font *) cffcidfont->pfont;
2142 pfont->client_data = cffcidfont;
2143 pfont->WMode = 0;
2144 pfont->PaintType = 0;
2145 pfont->StrokeWidth = 0;
2146 pfont->is_cached = 0;
2147 pfont->FAPI = NULL;
2148 pfont->FAPI_font_data = NULL;
2149 pfont->procs.init_fstack = gs_type0_init_fstack;
2150 pfont->procs.next_char_glyph = gs_default_next_char_glyph;
2151 pfont->FontType = ft_CID_encrypted;
2152 pfont->ExactSize = fbit_use_outlines;
2153 pfont->InBetweenSize = fbit_use_outlines;
2154 pfont->TransformedChar = fbit_use_outlines;
2155 /* We may want to do something clever with an XUID here */
2156 pfont->id = gs_next_ids(ctx->memory, 1);
2157 uid_set_invalid(&pfont->UID);
2158
2159 /* The buildchar proc will be filled in by FAPI -
2160 we won't worry about working without FAPI */
2161 pfont->procs.encode_char = pdfi_encode_char;
2162 pfont->procs.glyph_name = ctx->get_glyph_name;
2163 pfont->procs.decode_glyph = pdfi_cidfont_decode_glyph;
2164 pfont->procs.define_font = gs_no_define_font;
2165 pfont->procs.make_font = gs_no_make_font;
2166
2167 cffcidfont->default_font_info = gs_default_font_info;
2168 pfont->procs.font_info = pdfi_default_font_info;
2169
2170 pfont->procs.glyph_info = gs_default_glyph_info;
2171 pfont->procs.glyph_outline = pdfi_cff_glyph_outline;
2172 pfont->procs.build_char = NULL;
2173 pfont->procs.same_font = gs_default_same_font;
2174 pfont->procs.enumerate_glyph = pdfi_cff_enumerate_glyph;
2175
2176 pfont->cidata.glyph_data = pdfi_cff_cid_glyph_data;
2177
2178 pfont->encoding_index = ENCODING_INDEX_UNKNOWN;
2179 pfont->nearest_encoding_index = ENCODING_INDEX_UNKNOWN;
2180
2181 pfont->client_data = (void *)cffcidfont;
2182
2183 *font = cffcidfont;
2184 return 0;
2185 }
2186
2187 static int
2188 pdfi_alloc_cff_font(pdf_context *ctx, pdf_font_cff ** font, uint32_t obj_num, bool for_fdarray)
2189 {
2190 pdf_font_cff *cfffont = NULL;
2191 gs_font_type1 *pfont = NULL;
2192 gs_matrix defmat_font = { 0.001f, 0.0f, 0.0f, 0.001f, 0.0f, 0.0f };
2193 gs_matrix defmat_fd = { 1.00f, 0.0f, 0.0f, 1.000f, 0.0f, 0.0f };
2194 gs_matrix *defmat = (for_fdarray ? &defmat_fd : &defmat_font);
2195
2196 cfffont = (pdf_font_cff *) gs_alloc_bytes(ctx->memory, sizeof(pdf_font_cff), "pdfi (cff pdf_font)");
2197 if (cfffont == NULL)
2198 return_error(gs_error_VMerror);
2199
2200 memset(cfffont, 0x00, sizeof(pdf_font_cff));
2201 cfffont->ctx = ctx;
2202 cfffont->type = PDF_FONT;
2203 cfffont->pdfi_font_type = e_pdf_font_cff;
2204
2205 #if REFCNT_DEBUG
2206 cfffont->UID = ctx->UID++;
2207 outprintf(ctx->memory, "Allocated object of type %c with UID %" PRIi64 "\n", cfffont->type,
2208 cfffont->UID);
2209 #endif
2210
2211 pdfi_countup(cfffont);
2212
2213 pfont = (gs_font_type1 *) gs_alloc_struct(ctx->memory, gs_font_type1, &st_gs_font_type1, "pdfi (truetype pfont)");
2214 if (pfont == NULL) {
2215 pdfi_countdown(cfffont);
2216 return_error(gs_error_VMerror);
2217 }
2218 memset(pfont, 0x00, sizeof(gs_font_type1));
2219
2220 cfffont->pfont = (gs_font_base *) pfont;
2221 memcpy(&pfont->orig_FontMatrix, defmat, sizeof(*defmat));
2222 memcpy(&pfont->FontMatrix, defmat, sizeof(*defmat));
2223 pfont->next = pfont->prev = 0;
2224 pfont->memory = ctx->memory;
2225 pfont->dir = ctx->font_dir;
2226 pfont->is_resource = false;
2227 gs_notify_init(&pfont->notify_list, ctx->memory);
2228 pfont->base = (gs_font *) cfffont->pfont;
2229 pfont->client_data = cfffont;
2230 pfont->WMode = 0;
2231 pfont->PaintType = 0;
2232 pfont->StrokeWidth = 0;
2233 pfont->is_cached = 0;
2234 pfont->FAPI = NULL;
2235 pfont->FAPI_font_data = NULL;
2236 pfont->procs.init_fstack = gs_default_init_fstack;
2237 pfont->procs.next_char_glyph = gs_default_next_char_glyph;
2238 pfont->FontType = ft_encrypted2;
2239 pfont->ExactSize = fbit_use_outlines;
2240 pfont->InBetweenSize = fbit_use_outlines;
2241 pfont->TransformedChar = fbit_use_outlines;
2242 /* We may want to do something clever with an XUID here */
2243 pfont->id = gs_next_ids(ctx->memory, 1);
2244 uid_set_invalid(&pfont->UID);
2245
2246 /* The buildchar proc will be filled in by FAPI -
2247 we won't worry about working without FAPI */
2248 pfont->procs.encode_char = pdfi_encode_char;
2249 pfont->procs.glyph_name = ctx->get_glyph_name;
2250 pfont->procs.decode_glyph = pdfi_decode_glyph;
2251 pfont->procs.define_font = gs_no_define_font;
2252 pfont->procs.make_font = gs_no_make_font;
2253
2254 cfffont->default_font_info = gs_default_font_info;
2255 pfont->procs.font_info = pdfi_default_font_info;
2256
2257 pfont->procs.glyph_info = gs_default_glyph_info;
2258 pfont->procs.glyph_outline = pdfi_cff_glyph_outline;
2259 pfont->procs.build_char = NULL;
2260 pfont->procs.same_font = gs_default_same_font;
2261 pfont->procs.enumerate_glyph = pdfi_cff_enumerate_glyph;
2262
2263 pfont->data.procs.glyph_data = for_fdarray ? pdfi_cff_fdarray_glyph_data : pdfi_cff_glyph_data;
2264 pfont->data.procs.subr_data = pdfi_cff_subr_data;
2265 pfont->data.procs.seac_data = for_fdarray ? pdfi_cff_fdarray_seac_data : pdfi_cff_seac_data;
2266 pfont->data.procs.push_values = pdfi_cff_push;
2267 pfont->data.procs.pop_value = pdfi_cff_pop;
2268 pfont->data.interpret = gs_type2_interpret;
2269 pfont->data.lenIV = -1;
2270
2271 pfont->encoding_index = ENCODING_INDEX_UNKNOWN;
2272 pfont->nearest_encoding_index = ENCODING_INDEX_UNKNOWN;
2273
2274 pfont->client_data = (void *)cfffont;
2275
2276 *font = cfffont;
2277 return 0;
2278 }
2279
2280 static void
2281 pdfi_init_cff_font_priv(pdf_context *ctx, pdfi_gs_cff_font_priv *cffpriv,
2282 byte *buf, int buflen, bool for_fdarray)
2283 {
2284 gs_matrix defmat_font = { 0.001f, 0.0f, 0.0f, 0.001f, 0.0f, 0.0f };
2285 gs_matrix defmat_fd = { 1.00f, 0.0f, 0.0f, 1.000f, 0.0f, 0.0f };
2286 gs_matrix *defmat = (for_fdarray ? &defmat_fd : &defmat_font);
2287
2288 memset(cffpriv, 0x00, sizeof(pdfi_gs_cff_font_priv));
2289
2290 cffpriv->pdfcffpriv.ctx = ctx;
2291 cffpriv->pdfcffpriv.type = PDF_FONT;
2292 cffpriv->pdfcffpriv.pdfi_font_type = e_pdf_font_cff;
2293 cffpriv->client_data = (void *)(&cffpriv->pdfcffpriv);
2294 cffpriv->pdfcffpriv.pfont = (gs_font_base *)cffpriv;
2295 /* Dummy value for dummy object */
2296 cffpriv->pdfcffpriv.refcnt = 0xf0f0f0f0;
2297 cffpriv->pdfcffpriv.cffdata = buf;
2298 cffpriv->pdfcffpriv.cffend = buf + buflen;
2299 cffpriv->pdfcffpriv.cidcount = 8720;
2300
2301 memcpy(&cffpriv->orig_FontMatrix, defmat, sizeof(*defmat));
2302 memcpy(&cffpriv->FontMatrix, defmat, sizeof(*defmat));
2303 cffpriv->next = cffpriv->prev = 0;
2304 cffpriv->memory = ctx->memory;
2305 cffpriv->dir = ctx->font_dir;
2306 cffpriv->is_resource = false;
2307 gs_notify_init(&cffpriv->notify_list, ctx->memory);
2308 cffpriv->WMode = 0;
2309 cffpriv->PaintType = 0;
2310 cffpriv->StrokeWidth = 0;
2311 cffpriv->is_cached = 0;
2312 cffpriv->FAPI = NULL;
2313 cffpriv->FAPI_font_data = NULL;
2314 cffpriv->procs.init_fstack = gs_default_init_fstack;
2315 cffpriv->procs.next_char_glyph = gs_default_next_char_glyph;
2316 cffpriv->FontType = ft_encrypted2;
2317 cffpriv->ExactSize = fbit_use_outlines;
2318 cffpriv->InBetweenSize = fbit_use_outlines;
2319 cffpriv->TransformedChar = fbit_use_outlines;
2320 /* We may want to do something clever with an XUID here */
2321 cffpriv->id = gs_next_ids(ctx->memory, 1);
2322 uid_set_invalid(&cffpriv->UID);
2323
2324
2325 /* The buildchar proc will be filled in by FAPI -
2326 we won't worry about working without FAPI */
2327 cffpriv->procs.encode_char = pdfi_encode_char;
2328 cffpriv->procs.glyph_name = ctx->get_glyph_name;
2329 cffpriv->procs.decode_glyph = pdfi_decode_glyph;
2330 cffpriv->procs.define_font = gs_no_define_font;
2331
2332 cffpriv->pdfcffpriv.default_font_info = gs_default_font_info;
2333 cffpriv->procs.font_info = pdfi_default_font_info;
2334
2335 cffpriv->procs.glyph_info = gs_default_glyph_info;
2336 cffpriv->procs.glyph_outline = pdfi_cff_glyph_outline;
2337 cffpriv->procs.build_char = NULL;
2338 cffpriv->procs.same_font = gs_default_same_font;
2339 cffpriv->procs.enumerate_glyph = pdfi_cff_enumerate_glyph;
2340
2341 cffpriv->type1data.procs.glyph_data = pdfi_cff_glyph_data;
2342 cffpriv->type1data.procs.subr_data = pdfi_cff_subr_data;
2343 cffpriv->type1data.procs.seac_data = pdfi_cff_seac_data;
2344 cffpriv->type1data.procs.push_values = pdfi_cff_push;
2345 cffpriv->type1data.procs.pop_value = pdfi_cff_pop;
2346 cffpriv->type1data.interpret = gs_type2_interpret;
2347 cffpriv->type1data.lenIV = -1;
2348
2349 cffpriv->encoding_index = ENCODING_INDEX_UNKNOWN;
2350 cffpriv->nearest_encoding_index = ENCODING_INDEX_UNKNOWN;
2351 }
2352
2353 int
2354 pdfi_read_cff_font(pdf_context *ctx, pdf_dict *font_dict, pdf_dict *stream_dict, pdf_dict *page_dict, byte *pfbuf, int64_t fbuflen, bool forcecid, pdf_font **ppdffont)
2355 {
2356 int code = 0;
2357
2358 pdf_font *ppdfont = NULL;
2359 pdf_obj *basefont = NULL;
2360 pdf_obj *tmp = NULL;
2361 pdf_obj *fontdesc = NULL;
2362 pdf_string *registry = NULL;
2363 pdf_string *ordering = NULL;
2364 byte *fbuf = pfbuf;
2365
2366 if (fbuflen < 4) {
2367 gs_free_object(ctx->memory, pfbuf, "pdfi_read_cff_font(fbuf)");
2368 return_error(gs_error_invalidfont);
2369 }
2370
2371 if (!memcmp(fbuf, "OTTO", 4)) {
2372 int i, ntables;
2373 byte *p;
2374 uint32_t toffs = 0, tlen = 0;
2375
2376 code = u16(fbuf + 4, fbuf + fbuflen, &ntables);
2377
2378 if (code < 0 || ntables > 64) {
2379 gs_free_object(ctx->memory, pfbuf, "pdfi_read_cff_font(fbuf)");
2380 return_error(gs_error_invalidfont);
2381 }
2382
2383 for (i = 0; i < ntables; i++) {
2384 p = fbuf + 12 + i * 16;
2385 if (p + 4 >= fbuf + fbuflen)
2386 break;
2387
2388 if (!memcmp(p, "CFF ", 4)) {
2389 code = u32(p + 8, fbuf + fbuflen, (int *)&toffs);
2390 if (code >= 0)
2391 code = u32(p + 12, fbuf + fbuflen, (int *)&tlen);
2392 if (code < 0) {
2393 toffs = tlen = 0;
2394 }
2395 break;
2396 }
2397 }
2398 /* Sanity check the offset and size of the CFF table and make sure the declared
2399 * size and position fits inside the data we have. Promote the 32-bit variables to
2400 * 64-bit to avoid overflow calculating the end of the table.
2401 */
2402 if (toffs == 0 || tlen == 0 || (uint64_t)toffs + (uint64_t)tlen > fbuflen) {
2403 gs_free_object(ctx->memory, pfbuf, "pdfi_read_cff_font(fbuf)");
2404 return_error(gs_error_invalidfont);
2405 }
2406 fbuf += toffs;
2407 fbuflen = tlen;
2408 }
2409
2410 if (font_dict != NULL) {
2411 code = pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, &fontdesc);
2412 if (code < 0) {
2413 fontdesc = NULL;
2414 }
2415 }
2416 else {
2417 fontdesc = NULL;
2418 }
2419
2420 /* Vestigial magic number check - we can't check the third byte, as we have out of
2421 spec fonts that have a head size > 4
2422 */
2423 if (fbuf[0] == 1 && fbuf[1] == 0 && code >= 0) {
2424 pdfi_gs_cff_font_priv cffpriv;
2425
2426 pdfi_init_cff_font_priv(ctx, &cffpriv, fbuf, fbuflen, false);
2427 cffpriv.forcecid = forcecid;
2428 code = pdfi_read_cff(ctx, &cffpriv);
2429
2430 if (code >= 0) {
2431 if (cffpriv.FontType == ft_CID_encrypted) {
2432 pdf_obj *obj = NULL;
2433 pdf_cidfont_type0 *cffcid = NULL;
2434 gs_font_cid0 *pfont = NULL;
2435
2436 code = pdfi_alloc_cff_cidfont(ctx, &cffcid, font_dict->object_num);
2437 if (code < 0)
2438 goto error;
2439
2440 pfont = (gs_font_cid0 *) cffcid->pfont;
2441 ppdfont = (pdf_font *) cffcid;
2442
2443 memcpy(pfont, &cffpriv, sizeof(pdfi_gs_cff_font_common_priv));
2444 memcpy(&pfont->cidata, &cffpriv.cidata, sizeof(pfont->cidata));
2445
2446 pfont->procs.glyph_outline = pdfi_cff_glyph_outline;
2447 pfont->cidata.glyph_data = pdfi_cff_cid_glyph_data;
2448
2449 cffcid->orig_glyph_info = pfont->procs.glyph_info;
2450 pfont->procs.glyph_info = pdfi_cff_cidfont_glyph_info;
2451
2452 pfont->cidata.proc_data = NULL;
2453 pfont->FAPI = NULL;
2454 pfont->base = (gs_font *) cffcid->pfont;
2455
2456 code = pdfi_dict_knownget_type(ctx, font_dict, "CIDSystemInfo", PDF_DICT, (pdf_obj **)&obj);
2457 if (code <= 0) {
2458 cffcid->registry = cffpriv.pdfcffpriv.registry;
2459 cffcid->ordering = cffpriv.pdfcffpriv.ordering;
2460 cffcid->supplement = cffpriv.pdfcffpriv.supplement;
2461 }
2462 else {
2463 pdf_num *suppl = NULL;
2464
2465 code = pdfi_dict_knownget_type(ctx, (pdf_dict *)obj, "Registry", PDF_STRING, (pdf_obj **)&cffcid->registry);
2466 if (code <= 0) {
2467 cffcid->registry = cffpriv.pdfcffpriv.registry;
2468 }
2469 else {
2470 pdfi_countdown(cffpriv.pdfcffpriv.registry);
2471 cffpriv.pdfcffpriv.registry = NULL;
2472 }
2473
2474 code = pdfi_dict_knownget_type(ctx, (pdf_dict *)obj, "Ordering", PDF_STRING, (pdf_obj **)&cffcid->ordering);
2475 if (code <= 0) {
2476 cffcid->ordering = cffpriv.pdfcffpriv.ordering;
2477 }
2478 else {
2479 pdfi_countdown(cffpriv.pdfcffpriv.ordering);
2480 cffpriv.pdfcffpriv.ordering = NULL;
2481 }
2482 code = pdfi_dict_knownget_type(ctx, (pdf_dict *)obj, "Supplement", PDF_INT, (pdf_obj **)&suppl);
2483 if (code <= 0 || pdfi_type_of(suppl) != PDF_INT) {
2484 cffcid->supplement = cffpriv.pdfcffpriv.supplement;
2485 }
2486 else {
2487 cffcid->supplement = suppl->value.i;
2488 }
2489 pdfi_countdown(suppl);
2490 }
2491 pdfi_countdown(obj);
2492 obj = NULL;
2493
2494 pfont->cidata.common.CIDSystemInfo.Registry.data = cffcid->registry->data;
2495 pfont->cidata.common.CIDSystemInfo.Registry.size = cffcid->registry->length;
2496 pfont->cidata.common.CIDSystemInfo.Ordering.data = cffcid->ordering->data;
2497 pfont->cidata.common.CIDSystemInfo.Ordering.size = cffcid->ordering->length;
2498 pfont->cidata.common.CIDSystemInfo.Supplement = cffcid->supplement;
2499
2500 /* We don't need to bounds check these strings because they were checked when parsing
2501 * from the CFF stream.
2502 */
2503 memcpy(pfont->font_name.chars, cffpriv.font_name.chars, cffpriv.font_name.size);
2504 pfont->font_name.size = cffpriv.font_name.size;
2505 memcpy(pfont->key_name.chars, cffpriv.key_name.chars, cffpriv.key_name.size);
2506 pfont->key_name.size = cffpriv.key_name.size;
2507
2508 cffcid->FontDescriptor = (pdf_dict *) fontdesc;
2509 fontdesc = NULL;
2510
2511 cffcid->PDF_font = font_dict;
2512 pdfi_countup(font_dict);
2513
2514 pfont->client_data = cffcid;
2515
2516 cffcid->object_num = font_dict->object_num;
2517 cffcid->generation_num = font_dict->generation_num;
2518 cffcid->indirect_num = font_dict->indirect_num;
2519 cffcid->indirect_gen = font_dict->indirect_gen;
2520
2521 cffcid->CharStrings = cffpriv.pdfcffpriv.CharStrings;
2522 cffpriv.pdfcffpriv.CharStrings = NULL;
2523
2524 cffcid->Subrs = cffpriv.pdfcffpriv.Subrs;
2525 cffcid->NumSubrs = cffpriv.pdfcffpriv.NumSubrs;
2526 cffpriv.pdfcffpriv.Subrs = NULL;
2527
2528 cffcid->GlobalSubrs = cffpriv.pdfcffpriv.GlobalSubrs;
2529 cffcid->NumGlobalSubrs = cffpriv.pdfcffpriv.NumGlobalSubrs;
2530 cffpriv.pdfcffpriv.GlobalSubrs = NULL;
2531
2532 cffcid->FDArray = cffpriv.pdfcffpriv.FDArray;
2533 cffpriv.pdfcffpriv.FDArray = NULL;
2534
2535 cffcid->copyright = cffpriv.pdfcffpriv.copyright;
2536 cffcid->notice = cffpriv.pdfcffpriv.notice;
2537 cffcid->fullname = cffpriv.pdfcffpriv.fullname;
2538 cffcid->familyname = cffpriv.pdfcffpriv.familyname;
2539 cffpriv.pdfcffpriv.copyright = cffpriv.pdfcffpriv.notice \
2540 = cffpriv.pdfcffpriv.fullname = cffpriv.pdfcffpriv.familyname = NULL;
2541
2542 pfont->cidata.common.CIDCount = cffpriv.pdfcffpriv.cidcount;
2543
2544 cffcid->cidtogidmap = NULL;
2545 code = pdfi_dict_knownget(ctx, font_dict, "CIDToGIDMap", (pdf_obj **) &obj);
2546 if (code > 0) {
2547 /* CIDToGIDMap can only be a stream or a name, and if it's a name
2548 it's only permitted to be "/Identity", so ignore it
2549 */
2550 if (pdfi_type_of(obj) == PDF_STREAM) {
2551 byte *d;
2552 int64_t sz = 0;
2553
2554 code = pdfi_object_alloc(ctx, PDF_BUFFER, 0, (pdf_obj **)&cffcid->cidtogidmap);
2555 if (code < 0) {
2556 pdfi_countdown(obj);
2557 goto error;
2558 }
2559 pdfi_countup(cffcid->cidtogidmap);
2560 code = pdfi_stream_to_buffer(ctx, (pdf_stream *)obj, &d, &sz);
2561 if (code < 0) {
2562 pdfi_countdown(obj);
2563 goto error;
2564 }
2565 code = pdfi_buffer_set_data((pdf_obj *)cffcid->cidtogidmap, d, (int32_t)sz);
2566 if (code < 0) {
2567 pdfi_countdown(obj);
2568 goto error;
2569 }
2570 }
2571 pdfi_countdown(obj);
2572 obj = NULL;
2573
2574 if (cffcid->cidtogidmap != NULL && cffcid->cidtogidmap->length > 0) {
2575 pfont->cidata.common.CIDCount = cffcid->cidtogidmap->length >> 1;
2576 }
2577 }
2578 pfont->cidata.common.MaxCID = pfont->cidata.common.CIDCount - 1;
2579
2580 code = pdfi_dict_knownget_number(ctx, font_dict, "DW", &cffcid->DW);
2581 if (code <= 0) {
2582 cffcid->DW = 1000;
2583 }
2584
2585 code = pdfi_dict_knownget_type(ctx, font_dict, "DW2", PDF_ARRAY, (pdf_obj **) &obj);
2586 if (code > 0) {
2587 cffcid->DW2 = (pdf_array *) obj;
2588 obj = NULL;
2589 }
2590 else {
2591 cffcid->DW2 = NULL;
2592 }
2593 code = pdfi_dict_knownget_type(ctx, font_dict, "W", PDF_ARRAY, (pdf_obj **) &obj);
2594 if (code > 0) {
2595 cffcid->W = (pdf_array *) obj;
2596 obj = NULL;
2597 }
2598 else {
2599 cffcid->W = NULL;
2600 }
2601 code = pdfi_dict_knownget_type(ctx, font_dict, "W2", PDF_ARRAY, (pdf_obj **) &obj);
2602 if (code > 0) {
2603 cffcid->W2 = (pdf_array *) obj;
2604 obj = NULL;
2605 }
2606 else {
2607 cffcid->W2 = NULL;
2608 }
2609 cffcid->pfont->id = gs_next_ids(ctx->memory, 1);
2610 }
2611 else if (forcecid) {
2612 pdf_obj *obj;
2613 pdf_cidfont_type0 *cffcid;
2614 gs_font_cid0 *pfont;
2615 pdf_font_cff *fdcfffont;
2616 gs_font_type1 *pfdfont = NULL;
2617 static const char *const reg = "Adobe";
2618 static const char *const ord = "Identity";
2619
2620 code = pdfi_object_alloc(ctx, PDF_STRING, strlen(reg), (pdf_obj **) &registry);
2621 if (code < 0)
2622 goto error;
2623 pdfi_countup(registry);
2624
2625 code = pdfi_object_alloc(ctx, PDF_STRING, strlen(ord), (pdf_obj **) &ordering);
2626 if (code < 0) {
2627 goto error;
2628 }
2629 pdfi_countup(ordering);
2630
2631 memcpy(registry->data, reg, strlen(reg));
2632 registry->length = strlen(reg);
2633 memcpy(ordering->data, ord, strlen(ord));
2634 ordering->length = strlen(ord);
2635
2636 code = pdfi_alloc_cff_font(ctx, &fdcfffont, 0, true);
2637 if (code < 0)
2638 goto error;
2639
2640 pfdfont = (gs_font_type1 *) fdcfffont->pfont;
2641
2642 code = pdfi_alloc_cff_cidfont(ctx, &cffcid, 0);
2643 if (code < 0) {
2644 gs_free_object(ctx->memory, fdcfffont, "pdfi_read_cff_font");
2645 gs_free_object(ctx->memory, pfdfont, "pdfi_read_cff_font");
2646 goto error;
2647 }
2648 ppdfont = (pdf_font *) cffcid;
2649
2650 code = pdfi_object_alloc(ctx, PDF_ARRAY, 1, (pdf_obj **) &cffcid->FDArray);
2651 if (code < 0)
2652 goto error;
2653 pdfi_countup(cffcid->FDArray);
2654
2655 pfont = (gs_font_cid0 *) cffcid->pfont;
2656 pfont->cidata.FDArray = (gs_font_type1 **) gs_alloc_bytes(ctx->memory, sizeof(gs_font_type1 *), "pdfi_read_cff_font");
2657 pfont->base = (gs_font *)pfont;
2658 if (!pfont->cidata.FDArray) {
2659 pdfi_countdown(cffcid->FDArray);
2660 gs_free_object(ctx->memory, fdcfffont, "pdfi_read_cff_font");
2661 gs_free_object(ctx->memory, pfdfont, "pdfi_read_cff_font");
2662 gs_free_object(ctx->memory, cffcid, "pdfi_read_cff_font");
2663 gs_free_object(ctx->memory, pfont, "pdfi_read_cff_font");
2664 goto error;
2665 }
2666
2667 memcpy(pfdfont, &cffpriv, sizeof(pdfi_gs_cff_font_common_priv));
2668 memcpy(&pfdfont->data, &cffpriv.type1data, sizeof(pfdfont->data));
2669
2670
2671 pfont->procs.glyph_outline = pdfi_cff_glyph_outline;
2672 pfont->cidata.glyph_data = pdfi_cff_cid_glyph_data;
2673 pfont->cidata.common.CIDCount = cffpriv.pdfcffpriv.CharStrings->entries;
2674 pfont->cidata.common.MaxCID = pfont->cidata.common.CIDCount - 1;
2675
2676 cffcid->orig_glyph_info = pfont->procs.glyph_info;
2677 pfont->procs.glyph_info = pdfi_cff_cidfont_glyph_info;
2678
2679 pfdfont->FAPI = NULL;
2680 pfdfont->base = (gs_font *)pfdfont;
2681 pfdfont->client_data = fdcfffont;
2682 pdfi_array_put(ctx, cffcid->FDArray, 0, (pdf_obj *) fdcfffont);
2683
2684 fdcfffont->object_num = 0;
2685 fdcfffont->generation_num = 0;
2686
2687 (void)pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, &basefont);
2688 fdcfffont->BaseFont = basefont;
2689 fdcfffont->Name = basefont;
2690 pdfi_countup(basefont);
2691
2692 pdfi_countdown(cffpriv.pdfcffpriv.Encoding);
2693 cffpriv.pdfcffpriv.Encoding = NULL;
2694
2695 fdcfffont->CharStrings = cffpriv.pdfcffpriv.CharStrings;
2696 cffpriv.pdfcffpriv.CharStrings = NULL;
2697 fdcfffont->Subrs = cffpriv.pdfcffpriv.Subrs;
2698 cffpriv.pdfcffpriv.Subrs = NULL;
2699 fdcfffont->NumSubrs = cffpriv.pdfcffpriv.NumSubrs;
2700 fdcfffont->GlobalSubrs = cffpriv.pdfcffpriv.GlobalSubrs;
2701 cffpriv.pdfcffpriv.GlobalSubrs = NULL;
2702 fdcfffont->NumGlobalSubrs = cffpriv.pdfcffpriv.NumGlobalSubrs;
2703
2704 fdcfffont->copyright = cffpriv.pdfcffpriv.copyright;
2705 fdcfffont->notice = cffpriv.pdfcffpriv.notice;
2706 fdcfffont->fullname = cffpriv.pdfcffpriv.fullname;
2707 fdcfffont->familyname = cffpriv.pdfcffpriv.familyname;
2708 cffpriv.pdfcffpriv.copyright = cffpriv.pdfcffpriv.notice \
2709 = cffpriv.pdfcffpriv.fullname = cffpriv.pdfcffpriv.familyname = NULL;
2710
2711 cffcid->CharStrings = fdcfffont->CharStrings;
2712 pdfi_countup(cffcid->CharStrings);
2713 cffcid->Subrs = fdcfffont->Subrs;
2714 pdfi_countup(cffcid->Subrs);
2715 cffcid->GlobalSubrs = fdcfffont->GlobalSubrs;
2716 pdfi_countup(cffcid->GlobalSubrs);
2717 pdfi_countdown(fdcfffont);
2718
2719 cffcid->FontDescriptor = (pdf_dict *) fontdesc;
2720 fontdesc = NULL;
2721
2722 cffcid->registry = registry;
2723 cffcid->ordering = ordering;
2724 registry = ordering = NULL;
2725 cffcid->supplement = 0;
2726
2727 /* Because we're faking a CIDFont, we want to move the scaling to the "parent" fake
2728 CIDFont, and make the FDArrray use identity scaling
2729 */
2730 memcpy(&pfont->FontMatrix, &pfdfont->FontMatrix, sizeof(pfdfont->FontMatrix));
2731 memcpy(&pfont->orig_FontMatrix, &pfdfont->orig_FontMatrix, sizeof(pfdfont->orig_FontMatrix));
2732
2733 gs_make_identity(&pfdfont->FontMatrix);
2734 gs_make_identity(&pfdfont->orig_FontMatrix);
2735
2736 pfont->cidata.CIDMapOffset = 0;
2737 pfont->cidata.FDArray_size = 1;
2738 pfont->cidata.FDBytes = 0;
2739 pfont->cidata.glyph_data = pdfi_cff_cid_glyph_data;
2740 pfont->cidata.FDArray[0] = pfdfont;
2741 pfont->cidata.common.CIDSystemInfo.Registry.data = cffcid->registry->data;
2742 pfont->cidata.common.CIDSystemInfo.Registry.size = cffcid->registry->length;
2743 pfont->cidata.common.CIDSystemInfo.Ordering.data = cffcid->ordering->data;
2744 pfont->cidata.common.CIDSystemInfo.Ordering.size = cffcid->ordering->length;
2745 pfont->cidata.common.CIDSystemInfo.Supplement = cffcid->supplement;
2746 pfont->client_data = cffcid;
2747
2748 /* We don't need to bounds check these strings because they were checked when parsing
2749 * from the CFF stream.
2750 */
2751 memcpy(pfont->font_name.chars, cffpriv.font_name.chars, cffpriv.font_name.size);
2752 pfont->font_name.size = cffpriv.font_name.size;
2753 memcpy(pfont->key_name.chars, cffpriv.key_name.chars, cffpriv.key_name.size);
2754 pfont->key_name.size = cffpriv.key_name.size;
2755
2756 cffcid->object_num = font_dict->object_num;
2757 cffcid->generation_num = font_dict->generation_num;
2758 cffcid->indirect_num = font_dict->indirect_num;
2759 cffcid->indirect_gen = font_dict->indirect_gen;
2760
2761 cffcid->PDF_font = font_dict;
2762 pdfi_countup(font_dict);
2763
2764 cffcid->cidtogidmap = NULL;
2765 code = pdfi_dict_knownget(ctx, font_dict, "CIDToGIDMap", (pdf_obj **) &obj);
2766 if (code > 0) {
2767 byte *d;
2768 int64_t sz = 0;
2769 /* CIDToGIDMap can only be a stream or a name, and if it's a name
2770 it's only permitted to be "/Identity", so ignore it
2771 */
2772 if (pdfi_type_of(obj) == PDF_STREAM) {
2773 code = pdfi_object_alloc(ctx, PDF_BUFFER, 0, (pdf_obj **)&cffcid->cidtogidmap);
2774 if (code < 0) {
2775 goto error;
2776 }
2777 pdfi_countup(cffcid->cidtogidmap);
2778 code = pdfi_stream_to_buffer(ctx, (pdf_stream *)obj, &d, &sz);
2779 if (code < 0) {
2780 goto error;
2781 }
2782 code = pdfi_buffer_set_data((pdf_obj *)cffcid->cidtogidmap, d, (int32_t)sz);
2783 if (code < 0) {
2784 goto error;
2785 }
2786 }
2787 pdfi_countdown(obj);
2788 obj = NULL;
2789
2790 if (cffcid->cidtogidmap != NULL && cffcid->cidtogidmap->length > 0) {
2791 pfont->cidata.common.CIDCount = cffcid->cidtogidmap->length >> 1;
2792 }
2793 }
2794 pfont->cidata.common.MaxCID = pfont->cidata.common.CIDCount - 1;
2795
2796 code = pdfi_dict_knownget_number(ctx, font_dict, "DW", &cffcid->DW);
2797 if (code <= 0) {
2798 cffcid->DW = 1000;
2799 }
2800
2801 code = pdfi_dict_knownget_type(ctx, font_dict, "DW2", PDF_ARRAY, (pdf_obj **) &obj);
2802 if (code > 0) {
2803 cffcid->DW2 = (pdf_array *) obj;
2804 obj = NULL;
2805 }
2806 else {
2807 cffcid->DW2 = NULL;
2808 }
2809 code = pdfi_dict_knownget_type(ctx, font_dict, "W", PDF_ARRAY, (pdf_obj **) &obj);
2810 if (code > 0) {
2811 cffcid->W = (pdf_array *) obj;
2812 obj = NULL;
2813 }
2814 else {
2815 cffcid->W = NULL;
2816 }
2817 code = pdfi_dict_knownget_type(ctx, font_dict, "W2", PDF_ARRAY, (pdf_obj **) &obj);
2818 if (code > 0) {
2819 cffcid->W2 = (pdf_array *) obj;
2820 obj = NULL;
2821 }
2822 else {
2823 cffcid->W2 = NULL;
2824 }
2825
2826 cffcid->pfont->id = gs_next_ids(ctx->memory, 1);
2827 }
2828 else {
2829 pdf_font_cff *cfffont = NULL;
2830 gs_font_type1 *pfont = NULL;
2831 pdf_obj *tounicode = NULL;
2832
2833 code = pdfi_alloc_cff_font(ctx, &cfffont, font_dict != NULL ? font_dict->object_num : 0, false);
2834 if (code < 0)
2835 goto error;
2836
2837 pfont = (gs_font_type1 *) cfffont->pfont;
2838 ppdfont = (pdf_font *) cfffont;
2839
2840 memcpy(pfont, &cffpriv, sizeof(pdfi_gs_cff_font_common_priv));
2841 memcpy(&pfont->data, &cffpriv.type1data, sizeof(pfont->data));
2842 pfont->FAPI = NULL;
2843 pfont->client_data = cfffont;
2844 pfont->base = (gs_font *) cfffont->pfont;
2845
2846 pfont->procs.glyph_info = pdfi_cff_glyph_info;
2847
2848 if (font_dict) {
2849 cfffont->object_num = font_dict->object_num;
2850 cfffont->generation_num = font_dict->generation_num;
2851 cfffont->indirect_num = font_dict->indirect_num;
2852 cfffont->indirect_gen = font_dict->indirect_gen;
2853 (void)pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, &basefont);
2854 }
2855
2856 cfffont->BaseFont = basefont;
2857 cfffont->Name = basefont;
2858 pdfi_countup(basefont);
2859
2860 cfffont->CharStrings = cffpriv.pdfcffpriv.CharStrings;
2861 cffpriv.pdfcffpriv.CharStrings = NULL;
2862
2863 cfffont->Subrs = cffpriv.pdfcffpriv.Subrs;
2864 cfffont->NumSubrs = cffpriv.pdfcffpriv.NumSubrs;
2865 cffpriv.pdfcffpriv.Subrs = NULL;
2866
2867 cfffont->GlobalSubrs = cffpriv.pdfcffpriv.GlobalSubrs;
2868 cfffont->NumGlobalSubrs = cffpriv.pdfcffpriv.NumGlobalSubrs;
2869 cffpriv.pdfcffpriv.GlobalSubrs = NULL;
2870
2871 cfffont->FontDescriptor = (pdf_dict *) fontdesc;
2872 fontdesc = NULL;
2873
2874 cfffont->copyright = cffpriv.pdfcffpriv.copyright;
2875 cfffont->notice = cffpriv.pdfcffpriv.notice;
2876 cfffont->fullname = cffpriv.pdfcffpriv.fullname;
2877 cfffont->familyname = cffpriv.pdfcffpriv.familyname;
2878 cffpriv.pdfcffpriv.copyright = cffpriv.pdfcffpriv.notice \
2879 = cffpriv.pdfcffpriv.fullname = cffpriv.pdfcffpriv.familyname = NULL;
2880
2881 cfffont->PDF_font = font_dict;
2882 pdfi_countup(font_dict);
2883
2884 cfffont->descflags = 0;
2885 if (cfffont->FontDescriptor != NULL) {
2886 code = pdfi_dict_get_int(ctx, cfffont->FontDescriptor, "Flags", &cfffont->descflags);
2887 if (code >= 0) {
2888 /* If both the symbolic and non-symbolic flag are set,
2889 believe that latter.
2890 */
2891 if ((cfffont->descflags & 32) != 0)
2892 cfffont->descflags = (cfffont->descflags & ~4);
2893 }
2894 }
2895 /* ZapfDingbats and Symbol we just have to know are symbolic */
2896 if (pdfi_font_known_symbolic(basefont)) {
2897 cfffont->descflags |= 4;
2898 }
2899
2900 pdfi_font_set_first_last_char(ctx, font_dict, (pdf_font *)cfffont);
2901
2902 /* Widths are defined assuming a 1000x1000 design grid, but we apply
2903 * them in font space - so undo the 1000x1000 scaling, and apply
2904 * the inverse of the font's x scaling
2905 */
2906 if (font_dict != NULL) {
2907 /* ignore errors with widths... for now */
2908 (void)pdfi_font_create_widths(ctx, font_dict, (pdf_font*)cfffont, (double)(0.001 / hypot(pfont->FontMatrix.xx, pfont->FontMatrix.xy)));
2909 }
2910
2911 if (font_dict != NULL)
2912 code = pdfi_dict_knownget(ctx, font_dict, "Encoding", &tmp);
2913 else
2914 code = gs_error_undefined;
2915 if (code == 1) {
2916 if ((cfffont->descflags & 4) != 0 && pdfi_type_of(tmp) == PDF_DICT) {
2917 code = pdfi_create_Encoding(ctx, (pdf_font *)cfffont, tmp, (pdf_obj *)cffpriv.pdfcffpriv.Encoding, (pdf_obj **) &cfffont->Encoding);
2918 if (code >= 0) {
2919 pdfi_countdown(cffpriv.pdfcffpriv.Encoding);
2920 cffpriv.pdfcffpriv.Encoding = NULL;
2921 code = 1;
2922 }
2923 }
2924 else if ((pdfi_type_of(tmp) == PDF_NAME || pdfi_type_of(tmp) == PDF_DICT)) {
2925 code = pdfi_create_Encoding(ctx, (pdf_font *)cfffont, tmp, NULL, (pdf_obj **) &cfffont->Encoding);
2926 if (code >= 0) {
2927 pdfi_countdown(cffpriv.pdfcffpriv.Encoding);
2928 cffpriv.pdfcffpriv.Encoding = NULL;
2929 code = 1;
2930 }
2931 }
2932 else
2933 code = gs_error_undefined;
2934
2935 if (code == 1) {
2936 }
2937 pdfi_countdown(tmp);
2938 tmp = NULL;
2939 }
2940 else {
2941 pdfi_countdown(tmp);
2942 tmp = NULL;
2943 code = 0;
2944 }
2945 if (code <= 0) {
2946 cfffont->Encoding = cffpriv.pdfcffpriv.Encoding;
2947 cffpriv.pdfcffpriv.Encoding = NULL;
2948 cfffont->pfont->encoding_index = cffpriv.encoding_index;
2949 cfffont->pfont->nearest_encoding_index = cffpriv.nearest_encoding_index;
2950 }
2951
2952 cfffont->pfont->id = gs_next_ids(ctx->memory, 1);
2953
2954 if (ctx->args.ignoretounicode != true && font_dict != NULL) {
2955 code = pdfi_dict_get(ctx, font_dict, "ToUnicode", (pdf_obj **)&tounicode);
2956 if (code >= 0 && pdfi_type_of(tounicode) == PDF_STREAM) {
2957 pdf_cmap *tu = NULL;
2958 code = pdfi_read_cmap(ctx, tounicode, &tu);
2959 pdfi_countdown(tounicode);
2960 tounicode = (pdf_obj *)tu;
2961 }
2962 if (code < 0 || (tounicode != NULL && pdfi_type_of(tounicode) != PDF_CMAP)) {
2963 pdfi_countdown(tounicode);
2964 tounicode = NULL;
2965 code = 0;
2966 }
2967 }
2968 else {
2969 tounicode = NULL;
2970 }
2971 cfffont->ToUnicode = tounicode;
2972 tounicode = NULL;
2973 }
2974 }
2975 error:
2976 if (code < 0) {
2977 pdfi_countdown(cffpriv.pdfcffpriv.Subrs);
2978 pdfi_countdown(cffpriv.pdfcffpriv.GlobalSubrs);
2979 pdfi_countdown(cffpriv.pdfcffpriv.CharStrings);
2980 pdfi_countdown(cffpriv.pdfcffpriv.CIDSystemInfo);
2981 pdfi_countdown(cffpriv.pdfcffpriv.W);
2982 pdfi_countdown(cffpriv.pdfcffpriv.DW2);
2983 pdfi_countdown(cffpriv.pdfcffpriv.W2);
2984 pdfi_countdown(cffpriv.pdfcffpriv.FDArray);
2985 pdfi_countdown(cffpriv.pdfcffpriv.registry);
2986 pdfi_countdown(cffpriv.pdfcffpriv.ordering);
2987 pdfi_countdown(cffpriv.pdfcffpriv.Encoding);
2988 pdfi_countdown(cffpriv.pdfcffpriv.copyright);
2989 pdfi_countdown(cffpriv.pdfcffpriv.notice);
2990 pdfi_countdown(cffpriv.pdfcffpriv.fullname);
2991 pdfi_countdown(cffpriv.pdfcffpriv.familyname);
2992 if (cffpriv.FontType == ft_CID_encrypted) {
2993 if (ppdfont != NULL && ppdfont->pfont != NULL) {
2994 ((gs_font_cid0 *)ppdfont->pfont)->cidata.FDArray = NULL;
2995 }
2996 gs_free_object(ctx->memory, cffpriv.cidata.FDArray, "pdfi_read_cff_font(gs_font FDArray, error)");
2997 }
2998 }
2999 else {
3000 code = pdfi_font_generate_pseudo_XUID(ctx, font_dict, ppdfont->pfont);
3001 if (code < 0) {
3002 goto error;
3003 }
3004
3005 pdfi_font_set_orig_fonttype(ctx, (pdf_font *)ppdfont);
3006 code = gs_definefont(ctx->font_dir, (gs_font *) ppdfont->pfont);
3007
3008 if (code >= 0)
3009 code = pdfi_fapi_passfont((pdf_font *) ppdfont, 0, NULL, NULL, NULL, 0);
3010
3011 /* object_num can be zero if the dictionary was defined inline */
3012 if (code >= 0 && ppdfont->object_num != 0) {
3013 (void)replace_cache_entry(ctx, (pdf_obj *) ppdfont);
3014 }
3015
3016 if (code >= 0) {
3017 *ppdffont = (pdf_font *) ppdfont;
3018 ppdfont = NULL;
3019 }
3020 }
3021 }
3022 gs_free_object(ctx->memory, pfbuf, "pdfi_read_cff_font(fbuf)");
3023 pdfi_countdown(ppdfont);
3024 pdfi_countdown(fontdesc);
3025 pdfi_countdown(ordering);
3026 pdfi_countdown(registry);
3027
3028 if (code < 0) {
3029 tmp = NULL;
3030 if (font_dict != NULL) {
3031 if (pdfi_dict_get(ctx, font_dict, ".Path", &tmp) >= 0)
3032 {
3033 char fname[gp_file_name_sizeof + 1];
3034 pdf_string *fobj = (pdf_string *)tmp;
3035
3036 memcpy(fname, fobj->data, fobj->length > gp_file_name_sizeof ? gp_file_name_sizeof : fobj->length);
3037 fname[fobj->length > gp_file_name_sizeof ? gp_file_name_sizeof : fobj->length] = '\0';
3038
3039 (void)pdfi_set_error_var(ctx, code, NULL, E_PDF_BADSTREAM, "pdfi_read_cff_font", "Error reading CFF font file %s\n", fname);
3040 }
3041 else {
3042 (void)pdfi_set_error_var(ctx, code, NULL, E_PDF_BADSTREAM, "pdfi_read_cff_font", "Error reading embedded Type1C font object %u\n", font_dict->object_num);
3043 }
3044 }
3045 else {
3046 pdfi_set_error(ctx, code, NULL, E_PDF_BADSTREAM, "pdfi_read_truetype_font", "Error reading font\n");
3047 }
3048 pdfi_countdown(tmp);
3049 *ppdffont = NULL;
3050 return_error(gs_error_invalidfont);
3051 }
3052
3053 return code;
3054 }
3055
3056 int
3057 pdfi_read_type1C_font(pdf_context *ctx, pdf_dict *font_dict,
3058 pdf_dict *stream_dict, pdf_dict *page_dict, pdf_font **ppdffont)
3059 {
3060 int code;
3061 pdf_obj *fontdesc = NULL;
3062 pdf_obj *fontfile = NULL;
3063 byte *fbuf;
3064 int64_t fbuflen = 0;
3065
3066 code = pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, &fontdesc);
3067
3068 if (code >=0 && fontdesc != NULL) {
3069 code = pdfi_dict_get_type(ctx, (pdf_dict *) fontdesc, "FontFile", PDF_STREAM, &fontfile);
3070
3071 if (code < 0)
3072 code = pdfi_dict_get_type(ctx, (pdf_dict *) fontdesc, "FontFile2", PDF_STREAM, &fontfile);
3073
3074 if (code < 0)
3075 code = pdfi_dict_get_type(ctx, (pdf_dict *) fontdesc, "FontFile3", PDF_STREAM, &fontfile);
3076 }
3077 pdfi_countdown(fontdesc);
3078
3079 if (code >= 0 && fontfile != NULL) {
3080 code = pdfi_stream_to_buffer(ctx, (pdf_stream *) fontfile, &fbuf, &fbuflen);
3081 pdfi_countdown(fontfile);
3082 }
3083 else {
3084 /* TODO - handle non-emebedded case */
3085 return_error(gs_error_invalidfont);
3086 }
3087
3088 code = pdfi_read_cff_font(ctx, stream_dict, page_dict, font_dict, fbuf, fbuflen, false, ppdffont);
3089
3090 return code;
3091 }
3092
3093 int
3094 pdfi_copy_cff_font(pdf_context *ctx, pdf_font *spdffont, pdf_dict *font_dict, pdf_font **tpdffont)
3095 {
3096 int code = 0;
3097 pdf_font_cff *font = NULL;
3098 gs_font_type1 *spfont1 = (gs_font_type1 *) spdffont->pfont;
3099 gs_font_type1 *dpfont1;
3100 gs_id t_id;
3101 pdf_obj *tmp;
3102
3103 if (font_dict == NULL)
3104 return_error(gs_error_invalidfont);
3105
3106 code = pdfi_alloc_cff_font(ctx, &font, font_dict->object_num, false);
3107 if (code < 0)
3108 return code;
3109 dpfont1 = (gs_font_type1 *) font->pfont;
3110
3111 t_id = dpfont1->id;
3112 memcpy(dpfont1, spfont1, sizeof(gs_font_type1));
3113 dpfont1->id = t_id;
3114 dpfont1->FAPI = NULL;
3115 dpfont1->FAPI_font_data = NULL;
3116 dpfont1->notify_list.memory = NULL;
3117 dpfont1->notify_list.first = NULL;
3118 gs_notify_init(&dpfont1->notify_list, dpfont1->memory);
3119
3120 memcpy(font, spdffont, sizeof(pdf_font_type1));
3121 font->refcnt = 1;
3122 font->pfont = (gs_font_base *)dpfont1;
3123 dpfont1->client_data = (void *)font;
3124 font->filename = NULL;
3125
3126 font->PDF_font = font_dict;
3127 font->object_num = font_dict->object_num;
3128 font->generation_num = font_dict->generation_num;
3129 pdfi_countup(font->PDF_font);
3130
3131 /* We want basefont and descriptor, but we can live without them */
3132 font->BaseFont = NULL;
3133 (void)pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, &font->BaseFont);
3134 font->FontDescriptor = NULL;
3135 (void)pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, (pdf_obj **)&font->FontDescriptor);
3136
3137 pdfi_countup(font->Name);
3138 pdfi_countup(font->CharStrings);
3139 pdfi_countup(font->Subrs);
3140 pdfi_countup(font->GlobalSubrs);
3141 pdfi_countup(font->copyright);
3142 pdfi_countup(font->notice);
3143 pdfi_countup(font->fullname);
3144 pdfi_countup(font->familyname);
3145
3146 if (font->BaseFont != NULL && ((pdf_name *)font->BaseFont)->length <= gs_font_name_max) {
3147 memcpy(dpfont1->key_name.chars, ((pdf_name *)font->BaseFont)->data, ((pdf_name *)font->BaseFont)->length);
3148 dpfont1->key_name.size = ((pdf_name *)font->BaseFont)->length;
3149 memcpy(dpfont1->font_name.chars, ((pdf_name *)font->BaseFont)->data, ((pdf_name *)font->BaseFont)->length);
3150 dpfont1->font_name.size = ((pdf_name *)font->BaseFont)->length;
3151 }
3152
3153 font->Encoding = NULL;
3154 font->ToUnicode = NULL;
3155 font->Widths = NULL;
3156
3157 pdfi_font_set_first_last_char(ctx, font_dict, (pdf_font *)font);
3158 (void)pdfi_font_create_widths(ctx, font_dict, (pdf_font *)font, (double)(0.001 / hypot(dpfont1->FontMatrix.xx, dpfont1->FontMatrix.xy)));
3159
3160 font->descflags = 0;
3161 if (font->FontDescriptor != NULL) {
3162 code = pdfi_dict_get_int(ctx, font->FontDescriptor, "Flags", &font->descflags);
3163 if (code >= 0) {
3164 /* If both the symbolic and non-symbolic flag are set,
3165 believe that latter.
3166 */
3167 if ((font->descflags & 32) != 0)
3168 font->descflags = (font->descflags & ~4);
3169 }
3170 }
3171
3172 if (pdfi_font_known_symbolic(font->BaseFont)) {
3173 font->descflags |= 4;
3174 }
3175
3176
3177 tmp = NULL;
3178 code = pdfi_dict_knownget(ctx, font_dict, "Encoding", &tmp);
3179 if (code == 1) {
3180 if ((font->descflags & 4) != 0 && pdfi_type_of(tmp) == PDF_DICT) {
3181 code = pdfi_create_Encoding(ctx, (pdf_font *)font, tmp, NULL, (pdf_obj **) & font->Encoding);
3182 if (code >= 0)
3183 code = 1;
3184 }
3185 else if ((pdfi_type_of(tmp) == PDF_NAME || pdfi_type_of(tmp) == PDF_DICT)) {
3186 code = pdfi_create_Encoding(ctx, (pdf_font *)font, tmp, (pdf_obj *)spdffont->Encoding, (pdf_obj **) &font->Encoding);
3187 if (code >= 0)
3188 code = 1;
3189 }
3190 else
3191 code = gs_error_undefined;
3192 pdfi_countdown(tmp);
3193 tmp = NULL;
3194 }
3195 else {
3196 pdfi_countdown(tmp);
3197 tmp = NULL;
3198 code = 0;
3199 }
3200
3201 if (code <= 0) {
3202 font->Encoding = spdffont->Encoding;
3203 pdfi_countup(font->Encoding);
3204 }
3205
3206 code = uid_copy(&font->pfont->UID, font->pfont->memory, "pdfi_copy_cff_font");
3207 if (code < 0) {
3208 uid_set_invalid(&font->pfont->UID);
3209 }
3210
3211 if (spdffont->filename == NULL) {
3212 code = pdfi_font_generate_pseudo_XUID(ctx, font_dict, font->pfont);
3213 if (code < 0) {
3214 goto error;
3215 }
3216 }
3217
3218 if (ctx->args.ignoretounicode != true) {
3219 code = pdfi_dict_get(ctx, font_dict, "ToUnicode", (pdf_obj **)&tmp);
3220 if (code >= 0 && pdfi_type_of(tmp) == PDF_STREAM) {
3221 pdf_cmap *tu = NULL;
3222 code = pdfi_read_cmap(ctx, tmp, &tu);
3223 pdfi_countdown(tmp);
3224 tmp = (pdf_obj *)tu;
3225 }
3226 if (code < 0 || (tmp != NULL && pdfi_type_of(tmp) != PDF_CMAP)) {
3227 pdfi_countdown(tmp);
3228 tmp = NULL;
3229 code = 0;
3230 }
3231 }
3232 else {
3233 tmp = NULL;
3234 }
3235 font->ToUnicode = tmp;
3236
3237 pdfi_font_set_orig_fonttype(ctx, (pdf_font *)font);
3238 code = gs_definefont(ctx->font_dir, (gs_font *) font->pfont);
3239 if (code < 0) {
3240 goto error;
3241 }
3242
3243 code = pdfi_fapi_passfont((pdf_font *) font, 0, NULL, NULL, NULL, 0);
3244 if (code < 0) {
3245 goto error;
3246 }
3247 /* object_num can be zero if the dictionary was defined inline */
3248 if (font->object_num != 0) {
3249 (void)replace_cache_entry(ctx, (pdf_obj *) font);
3250 }
3251
3252 *tpdffont = (pdf_font *)font;
3253
3254 error:
3255 if (code < 0)
3256 pdfi_countdown(font);
3257 return code;
3258 }
3259
3260 int
3261 pdfi_free_font_cff(pdf_obj *font)
3262 {
3263 pdf_font_cff *pdfontcff = (pdf_font_cff *) font;
3264
3265 gs_free_object(OBJ_MEMORY(font), pdfontcff->pfont, "pdfi_free_font_cff(pfont)");
3266
3267 pdfi_countdown(pdfontcff->PDF_font);
3268 pdfi_countdown(pdfontcff->BaseFont);
3269 pdfi_countdown(pdfontcff->Name);
3270 pdfi_countdown(pdfontcff->FontDescriptor);
3271 pdfi_countdown(pdfontcff->CharStrings);
3272 pdfi_countdown(pdfontcff->Subrs);
3273 pdfi_countdown(pdfontcff->GlobalSubrs);
3274 pdfi_countdown(pdfontcff->Encoding);
3275 pdfi_countdown(pdfontcff->ToUnicode);
3276 pdfi_countdown(pdfontcff->filename);
3277 pdfi_countdown(pdfontcff->copyright);
3278 pdfi_countdown(pdfontcff->notice);
3279 pdfi_countdown(pdfontcff->fullname);
3280 pdfi_countdown(pdfontcff->familyname);
3281
3282 gs_free_object(OBJ_MEMORY(font), pdfontcff->Widths, "Type 2 fontWidths");
3283 gs_free_object(OBJ_MEMORY(font), pdfontcff, "pdfi_free_font_cff(pbfont)");
3284
3285 return 0;
3286 }
3287
3288 int
3289 pdfi_free_font_cidtype0(pdf_obj *font)
3290 {
3291 pdf_cidfont_type0 *pdfont0 = (pdf_cidfont_type0 *) font;
3292 gs_font_cid0 *pfont = (gs_font_cid0 *) pdfont0->pfont;
3293
3294 /* Only have to free the FDArray memory here. Each gs_font in the array is
3295 referenced by a pdfi font, reference by pdfont0->FDArray. Freeing that
3296 array will free each pdfi font, freeing the pdfi font will free the gs_font.
3297 gs_fonts are not reference counted
3298 */
3299 gs_free_object(OBJ_MEMORY(font), pfont->cidata.FDArray, "pdfi_free_font_cidtype0(pfont->fdarray)");
3300 gs_free_object(OBJ_MEMORY(font), pdfont0->pfont, "pdfi_free_font_cff(pfont)");
3301
3302 pdfi_countdown(pdfont0->PDF_font);
3303 pdfi_countdown(pdfont0->BaseFont);
3304 pdfi_countdown(pdfont0->FontDescriptor);
3305 pdfi_countdown(pdfont0->CharStrings);
3306 pdfi_countdown(pdfont0->Subrs);
3307 pdfi_countdown(pdfont0->GlobalSubrs);
3308 pdfi_countdown(pdfont0->CIDSystemInfo);
3309 pdfi_countdown(pdfont0->W);
3310 pdfi_countdown(pdfont0->DW2);
3311 pdfi_countdown(pdfont0->W2);
3312 pdfi_countdown(pdfont0->FDArray);
3313 pdfi_countdown(pdfont0->registry);
3314 pdfi_countdown(pdfont0->ordering);
3315 pdfi_countdown(pdfont0->cidtogidmap);
3316 pdfi_countdown(pdfont0->filename);
3317 pdfi_countdown(pdfont0->copyright);
3318 pdfi_countdown(pdfont0->notice);
3319 pdfi_countdown(pdfont0->fullname);
3320 pdfi_countdown(pdfont0->familyname);
3321
3322 gs_free_object(OBJ_MEMORY(font), pdfont0, "pdfi_free_font_cff(pbfont)");
3323
3324 return 0;
3325 }