Coverage Report

Created: 2026-11-12 07:09

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