Coverage Report

Created: 2026-04-09 07:06

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