Coverage Report

Created: 2026-07-24 07:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/pdf/pdf_font1.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 type 1 font handling */
17
#include "pdf_int.h"
18
19
#include "gsgdata.h"
20
#include "gstype1.h"
21
#include "gscencs.h"
22
23
#include "strmio.h"
24
#include "strimpl.h"
25
#include "stream.h"
26
#include "sfilter.h"
27
28
#include "pdf_deref.h"
29
#include "pdf_types.h"
30
#include "pdf_array.h"
31
#include "pdf_dict.h"
32
#include "pdf_file.h"
33
#include "pdf_font_types.h"
34
#include "pdf_font.h"
35
#include "pdf_fmap.h"
36
#include "pdf_font1.h"
37
#include "pdf_font1C.h"
38
#include "pdf_fontps.h"
39
#include "pdf_fontTT.h"
40
41
#include "gxtype1.h"        /* for gs_type1_state_s */
42
#include "gsutil.h"        /* For gs_next_ids() */
43
44
/* These are fonts for which we have to ignore "named" encodings */
45
typedef struct pdfi_t1_glyph_name_equivalents_s
46
{
47
    const char *name;
48
    const char *altname;
49
} pdfi_t1_glyph_name_equivalents_t;
50
51
static const pdfi_t1_glyph_name_equivalents_t pdfi_t1_glyph_name_equivalents[] =
52
{
53
  {"Ohungarumlaut", "Odblacute"},
54
  {"Uhungarumlaut", "Udblacute"},
55
  {"ohungarumlaut", "odblacute"},
56
  {"uhungarumlaut", "udblacute"},
57
  {NULL , NULL}
58
};
59
60
/* The Postscript code trawls the AGL to find all the equivalents.
61
   let's hope we can avoid that...
62
   Since none of the following show be required for a remotely valid
63
   Type 1, we just ignore errors (at least for now).
64
 */
65
static void pdfi_patch_charstrings_dict(pdf_dict *cstrings)
66
53.8k
{
67
53.8k
    int code = 0;
68
53.8k
    pdf_obj *o;
69
53.8k
    const pdfi_t1_glyph_name_equivalents_t *gne = pdfi_t1_glyph_name_equivalents;
70
269k
    while(gne->name != NULL && code >= 0) {
71
215k
        code = pdfi_dict_get(cstrings->ctx, cstrings, gne->name, &o);
72
215k
        if (code >= 0) {
73
183k
            code = pdfi_dict_put(cstrings->ctx, cstrings, gne->altname, o);
74
183k
            pdfi_countdown(o);
75
183k
        }
76
215k
        if (code == gs_error_undefined)
77
31.8k
            code = 0;
78
215k
        gne++;
79
215k
    }
80
81
53.8k
    if (code >= 0) {
82
53.8k
        bool key_known;
83
53.8k
        pdf_string *pstr;
84
53.8k
        byte notdefstr[] = { 0x9E, 0x35, 0xCE, 0xD7, 0xFF, 0xD3, 0x62, 0x2F, 0x09 };
85
86
53.8k
        code = pdfi_dict_known(cstrings->ctx, cstrings, ".notdef", &key_known);
87
53.8k
        if (code >=0 && key_known != true) {
88
            /* Seems there are plently of invalid Type 1 fonts without a .notdef,
89
               so make a fake one - a valid font will end up replacing this.
90
             */
91
1.10k
            code = pdfi_object_alloc(cstrings->ctx, PDF_STRING, sizeof(notdefstr), (pdf_obj **) &pstr);
92
1.10k
            if (code >= 0) {
93
1.10k
                memcpy(pstr->data, notdefstr, sizeof(notdefstr));
94
1.10k
                (void)pdfi_dict_put(cstrings->ctx, cstrings, ".notdef", (pdf_obj *) pstr);
95
1.10k
            }
96
1.10k
        }
97
53.8k
    }
98
53.8k
}
99
100
/* CALLBACKS */
101
static int
102
pdfi_t1_glyph_data(gs_font_type1 *pfont, gs_glyph glyph, gs_glyph_data_t *pgd)
103
78.0M
{
104
78.0M
    int code = 0;
105
78.0M
    pdf_font_type1 *pdffont1 = (pdf_font_type1 *) pfont->client_data;
106
78.0M
    pdf_context *ctx = (pdf_context *) pdffont1->ctx;
107
78.0M
    pdf_name *glyphname = NULL;
108
78.0M
    pdf_string *charstring = NULL;
109
78.0M
    gs_const_string gname;
110
111
78.0M
    code = (*ctx->get_glyph_name)((gs_font *)pfont, glyph, &gname);
112
78.0M
    if (code >= 0) {
113
78.0M
        code = pdfi_name_alloc(ctx, (byte *) gname.data, gname.size, (pdf_obj **)&glyphname);
114
78.0M
        if (code >= 0)
115
78.0M
            pdfi_countup(glyphname);
116
78.0M
    }
117
118
78.0M
    if (code >= 0) {
119
78.0M
        code = pdfi_dict_get_by_key(ctx, pdffont1->CharStrings, glyphname, (pdf_obj **)&charstring);
120
78.0M
        if (code < 0) {
121
284k
            code = pdfi_map_glyph_name_via_agl(pdffont1->CharStrings, glyphname, &charstring);
122
284k
        }
123
78.0M
        if (code >= 0)
124
77.7M
            gs_glyph_data_from_bytes(pgd, charstring->data, 0, charstring->length, NULL);
125
78.0M
    }
126
78.0M
    pdfi_countdown(charstring);
127
78.0M
    pdfi_countdown(glyphname);
128
129
78.0M
    return code;
130
78.0M
}
131
132
static int
133
pdfi_t1_subr_data(gs_font_type1 *pfont, int index, bool global, gs_glyph_data_t *pgd)
134
38.4M
{
135
38.4M
    int code = 0;
136
38.4M
    pdf_font_type1 *pdffont1 = (pdf_font_type1 *) pfont->client_data;
137
138
38.4M
    if (global == true || index < 0 || index >= (pdffont1->Subrs == NULL ? 0 : pdfi_array_size(pdffont1->Subrs))) {
139
346k
        code = gs_note_error(gs_error_rangecheck);
140
346k
    }
141
38.0M
    else {
142
38.0M
        pdf_string *subr_str = NULL;
143
38.0M
        code = pdfi_array_get_type(pdffont1->ctx, pdffont1->Subrs, index, PDF_STRING, (pdf_obj **)&subr_str);
144
38.0M
        if (code >= 0) {
145
38.0M
            gs_glyph_data_from_bytes(pgd, subr_str->data, 0, subr_str->length, NULL);
146
38.0M
        }
147
        /* decrementing is safe here, because the reference in the pdffont1->Subrs will persist */
148
38.0M
        pdfi_countdown(subr_str);
149
38.0M
    }
150
38.4M
    return code;
151
38.4M
}
152
153
static int
154
pdfi_t1_seac_data(gs_font_type1 *pfont, int ccode, gs_glyph *pglyph, gs_const_string *gstr, gs_glyph_data_t *pgd)
155
4.64k
{
156
4.64k
    int code = 0;
157
4.64k
    pdf_font_type1 *pdffont1 = (pdf_font_type1 *) pfont->client_data;
158
4.64k
    pdf_context *ctx = (pdf_context *) pdffont1->ctx;
159
4.64k
    gs_glyph glyph = gs_c_known_encode((gs_char)ccode, ENCODING_INDEX_STANDARD);
160
161
4.64k
    if (glyph == GS_NO_GLYPH)
162
37
        return_error(gs_error_rangecheck);
163
164
4.60k
    code = gs_c_glyph_name(glyph, gstr);
165
4.60k
    if (code >= 0) {
166
4.60k
        unsigned int nindex;
167
4.60k
        code = (*ctx->get_glyph_index)((gs_font *)pfont, (byte *)gstr->data, gstr->size, &nindex);
168
4.60k
        if (pglyph != NULL)
169
4.60k
            *pglyph = (gs_glyph)nindex;
170
4.60k
    }
171
172
4.60k
    if (code >= 0) {
173
4.60k
        pdf_name *glyphname = NULL;
174
4.60k
        pdf_string *charstring = NULL;
175
4.60k
        code = pdfi_name_alloc(ctx, (byte *) gstr->data, gstr->size, (pdf_obj **) &glyphname);
176
4.60k
        if (code >= 0) {
177
4.60k
            pdfi_countup(glyphname);
178
4.60k
            code = pdfi_dict_get_by_key(ctx, pdffont1->CharStrings, glyphname, (pdf_obj **)&charstring);
179
4.60k
            pdfi_countdown(glyphname);
180
4.60k
            if (code >= 0) {
181
4.58k
                if (pgd != NULL) {
182
0
                    gs_glyph_data_from_bytes(pgd, charstring->data, 0, charstring->length, NULL);
183
0
                }
184
4.58k
                pdfi_countdown(charstring);
185
4.58k
            }
186
4.60k
        }
187
4.60k
    }
188
189
4.60k
    return code;
190
4.64k
}
191
192
/* push/pop are null ops here */
193
static int
194
pdfi_t1_push(void *callback_data, const fixed *pf, int count)
195
0
{
196
0
    (void)callback_data;
197
0
    (void)pf;
198
0
    (void)count;
199
0
    return 0;
200
0
}
201
static int
202
pdfi_t1_pop(void *callback_data, fixed *pf)
203
0
{
204
0
    (void)callback_data;
205
0
    (void)pf;
206
0
    return 0;
207
0
}
208
209
static int
210
pdfi_t1_enumerate_glyph(gs_font *pfont, int *pindex,
211
                        gs_glyph_space_t glyph_space, gs_glyph *pglyph)
212
95.1M
{
213
95.1M
    int code;
214
95.1M
    pdf_font_type1 *t1font = (pdf_font_type1 *) pfont->client_data;
215
95.1M
    pdf_context *ctx = (pdf_context *) t1font->ctx;
216
95.1M
    pdf_name *key;
217
95.1M
    uint64_t i = (uint64_t) *pindex;
218
219
95.1M
    (void)glyph_space;
220
221
95.1M
    if (*pindex <= 0)
222
464k
        code = pdfi_dict_key_first(ctx, t1font->CharStrings, (pdf_obj **) & key, &i);
223
94.7M
    else
224
94.7M
        code = pdfi_dict_key_next(ctx, t1font->CharStrings, (pdf_obj **) & key, &i);
225
95.1M
    if (code < 0) {
226
103k
        *pindex = 0;
227
103k
        code = 0;
228
103k
    }
229
95.0M
    else {
230
95.0M
        uint dummy = GS_NO_GLYPH;
231
232
95.0M
        code = (*ctx->get_glyph_index)(pfont, key->data, key->length, &dummy);
233
95.0M
        if (code < 0) {
234
0
            *pglyph = (gs_glyph) *pindex;
235
0
            goto exit;
236
0
        }
237
95.0M
        *pglyph = dummy;
238
95.0M
        if (*pglyph == GS_NO_GLYPH)
239
0
            *pglyph = (gs_glyph) *pindex;
240
95.0M
        *pindex = (int)i;
241
95.0M
    }
242
95.1M
  exit:
243
95.1M
    pdfi_countdown(key);
244
95.1M
    return code;
245
95.1M
}
246
247
/* This *should* only get called for SEAC lookups, which have to come from StandardEncoding
248
   so just try to lookup the string in the standard encodings
249
 */
250
int
251
pdfi_t1_global_glyph_code(const gs_font *pfont, gs_const_string *gstr, gs_glyph *pglyph)
252
3
{
253
3
    *pglyph = gs_c_name_glyph(gstr->data, gstr->size);
254
3
    return 0;
255
3
}
256
257
static int
258
pdfi_t1_glyph_outline(gs_font *pfont, int WMode, gs_glyph glyph,
259
                      const gs_matrix *pmat, gx_path *ppath, double sbw[4])
260
1.03M
{
261
1.03M
    gs_glyph_data_t gd;
262
1.03M
    gs_glyph_data_t *pgd = &gd;
263
1.03M
    gs_font_type1 *pfont1 = (gs_font_type1 *) pfont;
264
1.03M
    int code = pdfi_t1_glyph_data(pfont1, glyph, pgd);
265
266
1.03M
    if (code >= 0) {
267
1.02M
        gs_type1_state cis = { 0 };
268
1.02M
        gs_type1_state *pcis = &cis;
269
1.02M
        gs_gstate gs;
270
1.02M
        int value;
271
272
1.02M
        if (pmat)
273
1.02M
            gs_matrix_fixed_from_matrix(&gs.ctm, pmat);
274
607
        else {
275
607
            gs_matrix imat;
276
277
607
            gs_make_identity(&imat);
278
607
            gs_matrix_fixed_from_matrix(&gs.ctm, &imat);
279
607
        }
280
1.02M
        gs.flatness = 0;
281
1.02M
        code = gs_type1_interp_init(pcis, &gs, ppath, NULL, NULL, true, 0, pfont1);
282
1.02M
        if (code < 0)
283
0
            return code;
284
285
1.02M
        pcis->no_grid_fitting = true;
286
1.02M
        gs_type1_set_callback_data(pcis, NULL);
287
        /* Continue interpreting. */
288
2.05M
      icont:
289
2.05M
        code = pfont1->data.interpret(pcis, pgd, &value);
290
2.05M
        switch (code) {
291
1.02M
            case 0:            /* all done */
292
                /* falls through */
293
1.02M
            default:           /* code < 0, error */
294
1.02M
                return code;
295
0
            case type1_result_callothersubr:   /* unknown OtherSubr */
296
0
                return_error(gs_error_rangecheck);      /* can't handle it */
297
1.02M
            case type1_result_sbw:     /* [h]sbw, just continue */
298
1.02M
                type1_cis_get_metrics(pcis, sbw);
299
1.02M
                pgd = 0;
300
1.02M
                goto icont;
301
2.05M
        }
302
2.05M
    }
303
7.30k
    return code;
304
1.03M
}
305
306
static int
307
pdfi_t1_glyph_info(gs_font *font, gs_glyph glyph, const gs_matrix *pmat, int members, gs_glyph_info_t *info)
308
55.7M
{
309
55.7M
    if ((members & GLYPH_INFO_OUTLINE_WIDTHS) == 0)
310
54.7M
        return gs_type1_glyph_info(font, glyph, pmat, members, info);
311
312
1.03M
    return gs_default_glyph_info(font, glyph, pmat, members, info);
313
55.7M
}
314
315
/* END CALLBACKS */
316
317
static stream *
318
push_pfb_filter(gs_memory_t *mem, byte *buf, byte *bufend)
319
368
{
320
368
    stream *fs, *ffs = NULL;
321
368
    stream *sstrm;
322
368
    stream_PFBD_state *st;
323
368
    byte *strbuf;
324
325
368
    sstrm = file_alloc_stream(mem, "push_pfb_filter(buf stream)");
326
368
    if (sstrm == NULL)
327
0
        return NULL;
328
329
368
    sread_string(sstrm, buf, bufend - buf);
330
368
    sstrm->close_at_eod = false;
331
332
368
    fs = s_alloc(mem, "push_pfb_filter(fs)");
333
368
    strbuf = gs_alloc_bytes(mem, 4096, "push_pfb_filter(buf)");
334
368
    st = gs_alloc_struct(mem, stream_PFBD_state, s_PFBD_template.stype, "push_pfb_filter(st)");
335
368
    if (fs == NULL || st == NULL || strbuf == NULL) {
336
0
        sclose(sstrm);
337
0
        gs_free_object(mem, sstrm, "push_pfb_filter(buf stream)");
338
0
        gs_free_object(mem, fs, "push_pfb_filter(fs)");
339
0
        gs_free_object(mem, st, "push_pfb_filter(st)");
340
0
        goto done;
341
0
    }
342
368
    memset(st, 0x00, sizeof(stream_PFBD_state));
343
368
    (*s_PFBD_template.init)((stream_state *)st);
344
368
    st->binary_to_hex = true;
345
368
    s_std_init(fs, strbuf, 4096, &s_filter_read_procs, s_mode_read);
346
368
    st->memory = mem;
347
368
    st->templat = &s_PFBD_template;
348
368
    fs->state = (stream_state *) st;
349
368
    fs->procs.process = s_PFBD_template.process;
350
368
    fs->strm = sstrm;
351
368
    fs->close_at_eod = false;
352
368
    ffs = fs;
353
368
  done:
354
368
    return ffs;
355
368
}
356
357
static void
358
pop_pfb_filter(gs_memory_t *mem, stream *s)
359
368
{
360
368
    stream *src = s->strm;
361
368
    byte *b = s->cbuf;
362
363
368
    sclose(s);
364
368
    gs_free_object(mem, s, "push_pfb_filter(s)");
365
368
    gs_free_object(mem, b, "push_pfb_filter(b)");
366
368
    if (src)
367
368
        sclose(src);
368
368
    gs_free_object(mem, src, "push_pfb_filter(strm)");
369
368
}
370
371
static int
372
pdfi_t1_decode_pfb(pdf_context *ctx, byte *inbuf, int inlen, byte **outbuf, int *outlen)
373
184
{
374
184
    stream *strm;
375
184
    int c, code = 0;
376
184
    int decodelen = 0;
377
184
    byte *d, *decodebuf = NULL;
378
379
184
    *outbuf = NULL;
380
184
    *outlen = 0;
381
382
184
    strm = push_pfb_filter(ctx->memory, inbuf, inbuf + inlen);
383
184
    if (strm == NULL) {
384
0
        code = gs_note_error(gs_error_VMerror);
385
0
    }
386
184
    else {
387
4.24M
        while (1) {
388
4.24M
            c = sgetc(strm);
389
4.24M
            if (c < 0)
390
184
                break;
391
4.24M
            decodelen++;
392
4.24M
        }
393
184
        pop_pfb_filter(ctx->memory, strm);
394
184
        decodebuf = gs_alloc_bytes(ctx->memory, decodelen, "pdfi_t1_decode_pfb(decodebuf)");
395
184
        if (decodebuf == NULL) {
396
0
            code = gs_note_error(gs_error_VMerror);
397
0
        }
398
184
        else {
399
184
            d = decodebuf;
400
184
            strm = push_pfb_filter(ctx->memory, inbuf, inbuf + inlen);
401
4.24M
            while (1) {
402
4.24M
                c = sgetc(strm);
403
4.24M
                if (c < 0)
404
184
                    break;
405
4.24M
                *d = c;
406
4.24M
                d++;
407
4.24M
            }
408
184
            pop_pfb_filter(ctx->memory, strm);
409
184
            *outbuf = decodebuf;
410
184
            *outlen = decodelen;
411
184
        }
412
184
    }
413
184
    return code;
414
184
}
415
416
static int
417
pdfi_alloc_t1_font(pdf_context *ctx, pdf_font_type1 **font, uint32_t obj_num)
418
543k
{
419
543k
    pdf_font_type1 *t1font = NULL;
420
543k
    gs_font_type1 *pfont = NULL;
421
422
543k
    t1font = (pdf_font_type1 *) gs_alloc_bytes(ctx->memory, sizeof(pdf_font_type1), "pdfi (type 1 pdf_font)");
423
543k
    if (t1font == NULL)
424
0
        return_error(gs_error_VMerror);
425
426
543k
    memset(t1font, 0x00, sizeof(pdf_font_type1));
427
543k
    t1font->ctx = ctx;
428
543k
    t1font->type = PDF_FONT;
429
543k
    t1font->ctx = ctx;
430
543k
    t1font->pdfi_font_type = e_pdf_font_type1;
431
432
#if REFCNT_DEBUG
433
    t1font->UID = ctx->UID++;
434
    outprintf(ctx->memory, "Allocated object of type %c with UID %" PRIi64 "\n", t1font->type, t1font->UID);
435
#endif
436
437
543k
    pdfi_countup(t1font);
438
439
543k
    pfont = (gs_font_type1 *) gs_alloc_struct(ctx->memory, gs_font_type1, &st_gs_font_type1, "pdfi (Type 1 pfont)");
440
543k
    if (pfont == NULL) {
441
0
        pdfi_countdown(t1font);
442
0
        return_error(gs_error_VMerror);
443
0
    }
444
543k
    memset(pfont, 0x00, sizeof(gs_font_type1));
445
446
543k
    t1font->pfont = (gs_font_base *) pfont;
447
448
543k
    gs_make_identity(&pfont->orig_FontMatrix);
449
543k
    gs_make_identity(&pfont->FontMatrix);
450
543k
    pfont->next = pfont->prev = 0;
451
543k
    pfont->memory = ctx->memory;
452
543k
    pfont->dir = ctx->font_dir;
453
543k
    pfont->is_resource = false;
454
543k
    gs_notify_init(&pfont->notify_list, ctx->memory);
455
543k
    pfont->base = (gs_font *) t1font->pfont;
456
543k
    pfont->client_data = t1font;
457
543k
    pfont->WMode = 0;
458
543k
    pfont->PaintType = 0;
459
543k
    pfont->StrokeWidth = 0;
460
543k
    pfont->is_cached = 0;
461
543k
    pfont->FAPI = NULL;
462
543k
    pfont->FAPI_font_data = NULL;
463
543k
    pfont->procs.init_fstack = gs_default_init_fstack;
464
543k
    pfont->procs.next_char_glyph = gs_default_next_char_glyph;
465
543k
    pfont->FontType = ft_encrypted;
466
543k
    pfont->ExactSize = fbit_use_outlines;
467
543k
    pfont->InBetweenSize = fbit_use_outlines;
468
543k
    pfont->TransformedChar = fbit_use_outlines;
469
    /* We may want to do something clever with an XUID here */
470
543k
    pfont->id = gs_next_ids(ctx->memory, 1);
471
543k
    uid_set_UniqueID(&pfont->UID, pfont->id);
472
473
543k
    pfont->encoding_index = ENCODING_INDEX_UNKNOWN;
474
543k
    pfont->nearest_encoding_index = ENCODING_INDEX_UNKNOWN;
475
476
543k
    pfont->client_data = (void *)t1font;
477
478
543k
    *font = t1font;
479
543k
    return 0;
480
543k
}
481
482
static void
483
pdfi_t1_font_set_procs(pdf_context *ctx, pdf_font_type1 *font)
484
53.8k
{
485
53.8k
    gs_font_type1 *pfont = (gs_font_type1 *) font->pfont;
486
487
    /* The build_char proc will be filled in by FAPI -
488
       we won't worry about working without FAPI */
489
53.8k
    pfont->procs.build_char = NULL;
490
491
53.8k
    pfont->procs.encode_char = pdfi_encode_char;
492
53.8k
    pfont->procs.glyph_name = ctx->get_glyph_name;
493
53.8k
    pfont->procs.decode_glyph = pdfi_decode_glyph;
494
53.8k
    pfont->procs.define_font = gs_no_define_font;
495
53.8k
    pfont->procs.make_font = gs_no_make_font;
496
497
53.8k
    font->default_font_info = gs_default_font_info;
498
53.8k
    pfont->procs.font_info = pdfi_default_font_info;
499
500
53.8k
    pfont->procs.glyph_info = pdfi_t1_glyph_info;
501
53.8k
    pfont->procs.glyph_outline = pdfi_t1_glyph_outline;
502
53.8k
    pfont->procs.same_font = gs_default_same_font;
503
53.8k
    pfont->procs.enumerate_glyph = pdfi_t1_enumerate_glyph;
504
505
53.8k
    pfont->data.procs.glyph_data = pdfi_t1_glyph_data;
506
53.8k
    pfont->data.procs.subr_data = pdfi_t1_subr_data;
507
53.8k
    pfont->data.procs.seac_data = pdfi_t1_seac_data;
508
53.8k
    pfont->data.procs.push_values = pdfi_t1_push;
509
53.8k
    pfont->data.procs.pop_value = pdfi_t1_pop;
510
53.8k
    pfont->data.interpret = gs_type1_interpret;
511
53.8k
}
512
513
static inline void
514
pdfi_type1_font_priv_defaults(ps_font_interp_private *pfpriv)
515
59.0k
{
516
59.0k
    pfpriv->gsu.gst1.data.lenIV = 4;
517
59.0k
    pfpriv->gsu.gst1.data.ExpansionFactor = 0.06f;
518
59.0k
    pfpriv->gsu.gst1.data.BlueShift = 7;
519
59.0k
    pfpriv->gsu.gst1.data.BlueFuzz = 1;
520
59.0k
    pfpriv->gsu.gst1.data.BlueScale = 0.039625f;
521
59.0k
    uid_set_invalid(&pfpriv->gsu.gst1.UID);
522
59.0k
}
523
524
int
525
pdfi_read_type1_font(pdf_context *ctx, pdf_dict *font_dict, pdf_dict *stream_dict, pdf_dict *page_dict, byte *fbuf, int64_t fbuflen, pdf_font **ppdffont)
526
59.0k
{
527
59.0k
    int code = 0;
528
59.0k
    double x_scale;
529
59.0k
    pdf_obj *fontdesc = NULL;
530
59.0k
    pdf_obj *basefont = NULL;
531
59.0k
    pdf_obj *mapname = NULL;
532
59.0k
    pdf_obj *tmp = NULL;
533
59.0k
    pdf_font_type1 *t1f = NULL;
534
59.0k
    pdf_obj *tounicode = NULL;
535
59.0k
    ps_font_interp_private fpriv = { 0 };
536
59.0k
    bool key_known;
537
59.0k
    bool force_symbolic = false;
538
539
59.0k
    if (font_dict != NULL)
540
11.4k
        (void)pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, &fontdesc);
541
542
59.0k
    if (fbuflen > 1 && fbuf[0] == 128 && fbuf[1] == 1) {
543
184
        byte *decodebuf = NULL;
544
184
        int decodelen;
545
546
184
        code = pdfi_t1_decode_pfb(ctx, fbuf, fbuflen, &decodebuf, &decodelen);
547
184
        gs_free_object(ctx->memory, fbuf, "pdfi_read_type1_font");
548
184
        if (code < 0) {
549
0
            gs_free_object(ctx->memory, decodebuf, "pdfi_read_type1_font");
550
0
        }
551
184
        fbuf = decodebuf;
552
184
        fbuflen = decodelen;
553
184
    }
554
555
59.0k
    if (code >= 0) {
556
59.0k
        pdfi_type1_font_priv_defaults(&fpriv);
557
59.0k
        code = pdfi_read_ps_font(ctx, font_dict, fbuf, fbuflen, &fpriv);
558
59.0k
        gs_free_object(ctx->memory, fbuf, "pdfi_read_type1_font");
559
560
        /* If we have a full CharStrings dictionary, we probably have enough to make a font */
561
59.0k
        if (code < 0 || fpriv.u.t1.CharStrings == NULL || pdfi_type_of(fpriv.u.t1.CharStrings) != PDF_DICT
562
53.9k
            || fpriv.u.t1.CharStrings->entries == 0) {
563
5.19k
                code = gs_note_error(gs_error_invalidfont);
564
5.19k
                goto error;
565
5.19k
        }
566
53.8k
        code = pdfi_alloc_t1_font(ctx, &t1f, font_dict != NULL ? font_dict->object_num : 0);
567
53.8k
        if (code >= 0) {
568
53.8k
            gs_font_type1 *pfont1 = (gs_font_type1 *) t1f->pfont;
569
570
53.8k
            memcpy(&pfont1->data, &fpriv.gsu.gst1.data, sizeof(pfont1->data));
571
572
53.8k
            pdfi_t1_font_set_procs(ctx, t1f);
573
574
53.8k
            memcpy(&pfont1->FontMatrix, &fpriv.gsu.gst1.FontMatrix, sizeof(pfont1->FontMatrix));
575
53.8k
            memcpy(&pfont1->orig_FontMatrix, &fpriv.gsu.gst1.orig_FontMatrix, sizeof(pfont1->orig_FontMatrix));
576
53.8k
            memcpy(&pfont1->FontBBox, &fpriv.gsu.gst1.FontBBox, sizeof(pfont1->FontBBox));
577
53.8k
            memcpy(&pfont1->key_name, &fpriv.gsu.gst1.key_name, sizeof(pfont1->key_name));
578
53.8k
            memcpy(&pfont1->font_name, &fpriv.gsu.gst1.font_name, sizeof(pfont1->font_name));
579
53.8k
            if (fpriv.gsu.gst1.UID.id != 0)
580
53.8k
                memcpy(&pfont1->UID, &fpriv.gsu.gst1.UID, sizeof(pfont1->UID));
581
53.8k
            fpriv.gsu.gst1.UID.xvalues = NULL; /* In case of error */
582
53.8k
            if (fpriv.gsu.gst1.WMode != 0) {
583
0
                if (fpriv.gsu.gst1.WMode != 1)
584
0
                    pdfi_set_warning(ctx, 0, NULL, W_PDF_BAD_WMODE, "pdfi_read_type1_font", NULL);
585
0
                pfont1->WMode = 1;
586
0
            }
587
53.8k
            else
588
53.8k
                pfont1->WMode = 0;
589
53.8k
            pfont1->PaintType = fpriv.gsu.gst1.PaintType;
590
53.8k
            pfont1->StrokeWidth = fpriv.gsu.gst1.StrokeWidth;
591
592
53.8k
            if (font_dict != NULL) {
593
6.22k
                t1f->object_num = font_dict->object_num;
594
6.22k
                t1f->generation_num = font_dict->generation_num;
595
6.22k
                t1f->indirect_num = font_dict->indirect_num;
596
6.22k
                t1f->indirect_gen = font_dict->indirect_gen;
597
6.22k
            }
598
599
53.8k
            t1f->PDF_font = font_dict;
600
53.8k
            pdfi_countup(font_dict);
601
53.8k
            t1f->FontDescriptor = (pdf_dict *) fontdesc;
602
53.8k
            pdfi_countup(fontdesc);
603
53.8k
            t1f->Name = mapname;
604
53.8k
            pdfi_countup(mapname);
605
606
            /* We want basefont, but we can live without it */
607
53.8k
            if (font_dict != NULL) {
608
6.22k
                (void)pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, &basefont);
609
6.22k
                t1f->BaseFont = basefont;
610
6.22k
                pdfi_countup(basefont);
611
6.22k
            }
612
613
53.8k
            if (t1f->FontDescriptor != NULL) {
614
6.22k
                code = pdfi_dict_get_int(ctx, t1f->FontDescriptor, "Flags", &t1f->descflags);
615
6.22k
                if (code >= 0) {
616
                    /* If both the symbolic and non-symbolic flag are set,
617
                       believe that latter.
618
                     */
619
6.21k
                    if ((t1f->descflags & 32) != 0)
620
2.08k
                        t1f->descflags = (t1f->descflags & ~4);
621
6.21k
                }
622
6.22k
            }
623
624
53.8k
            if (pdfi_font_known_symbolic(basefont)) {
625
50
                force_symbolic = true;
626
50
                t1f->descflags |= 4;
627
50
            }
628
629
53.8k
            if (ctx->args.ignoretounicode != true && font_dict != NULL) {
630
6.22k
                code = pdfi_dict_get(ctx, font_dict, "ToUnicode", (pdf_obj **)&tounicode);
631
6.22k
                if (code >= 0 && pdfi_type_of(tounicode) == PDF_STREAM) {
632
692
                    pdf_cmap *tu = NULL;
633
692
                    code = pdfi_read_cmap(ctx, tounicode, &tu);
634
692
                    pdfi_countdown(tounicode);
635
692
                    tounicode = (pdf_obj *)tu;
636
692
                }
637
6.22k
                if (code < 0 || (tounicode != NULL && pdfi_type_of(tounicode) != PDF_CMAP)) {
638
5.53k
                    pdfi_countdown(tounicode);
639
5.53k
                    tounicode = NULL;
640
5.53k
                    code = 0;
641
5.53k
                }
642
6.22k
            }
643
47.6k
            else {
644
47.6k
                tounicode = NULL;
645
47.6k
            }
646
53.8k
            t1f->ToUnicode = tounicode;
647
53.8k
            tounicode = NULL;
648
649
53.8k
            pdfi_font_set_first_last_char(ctx, font_dict, (pdf_font *)t1f);
650
651
            /* Widths are defined assuming a 1000x1000 design grid, but we apply
652
             * them in font space - so undo the 1000x1000 scaling, and apply
653
             * the inverse of the font's x scaling
654
             */
655
53.8k
            x_scale = 0.001 / hypot(pfont1->FontMatrix.xx, pfont1->FontMatrix.xy);
656
657
            /* ignore errors with widths... for now */
658
53.8k
            if (font_dict != NULL)
659
6.22k
                (void)pdfi_font_create_widths(ctx, font_dict, (pdf_font*)t1f, x_scale);
660
661
53.8k
            if (font_dict != NULL)
662
6.22k
                code = pdfi_dict_knownget(ctx, font_dict, "Encoding", &tmp);
663
47.6k
            else
664
47.6k
                code = gs_error_undefined;
665
53.8k
            if (code == 1) {
666
4.91k
                if (pdfi_type_of(tmp) == PDF_NAME && force_symbolic == true) {
667
0
                    t1f->Encoding = fpriv.u.t1.Encoding;
668
0
                    pdfi_countup(t1f->Encoding);
669
0
                }
670
4.91k
                else if (pdfi_type_of(tmp) == PDF_DICT && (t1f->descflags & 4) != 0) {
671
3.17k
                    code = pdfi_create_Encoding(ctx, (pdf_font *)t1f, tmp, (pdf_obj *)fpriv.u.t1.Encoding, (pdf_obj **) & t1f->Encoding);
672
3.17k
                    if (code >= 0)
673
3.17k
                        code = 1;
674
3.17k
                }
675
1.74k
                else {
676
1.74k
                    code = pdfi_create_Encoding(ctx, (pdf_font *)t1f, tmp, NULL, (pdf_obj **) & t1f->Encoding);
677
1.74k
                    if (code >= 0)
678
1.70k
                        code = 1;
679
1.74k
                }
680
4.91k
                pdfi_countdown(tmp);
681
4.91k
                tmp = NULL;
682
4.91k
            }
683
48.9k
            else {
684
48.9k
                pdfi_countdown(tmp);
685
48.9k
                tmp = NULL;
686
48.9k
                code = 0;
687
48.9k
            }
688
689
53.8k
            if (code <= 0) {
690
48.9k
                t1f->Encoding = fpriv.u.t1.Encoding;
691
48.9k
                pdfi_countup(t1f->Encoding);
692
48.9k
            }
693
            /* Since the underlying font stream can be shared between font descriptors,
694
               and the font descriptors can be shared between font objects, if we change
695
               the encoding, we can't share cached glyphs with other instances of this
696
               underlying font, so invalidate the UniqueID/XUID so the glyph cache won't
697
               try.
698
            */
699
53.8k
            if (uid_is_XUID(&t1f->pfont->UID))
700
53.8k
                uid_free(&t1f->pfont->UID, t1f->pfont->memory, "pdfi_read_type1_font");
701
53.8k
            uid_set_invalid(&t1f->pfont->UID);
702
703
53.8k
            t1f->CharStrings = fpriv.u.t1.CharStrings;
704
53.8k
            pdfi_countup(t1f->CharStrings);
705
53.8k
            pdfi_patch_charstrings_dict(t1f->CharStrings);
706
707
53.8k
            t1f->Subrs = fpriv.u.t1.Subrs;
708
53.8k
            fpriv.u.t1.Subrs = NULL;
709
710
53.8k
            t1f->copyright = fpriv.u.t1.copyright;
711
53.8k
            t1f->notice = fpriv.u.t1.notice;
712
53.8k
            t1f->fullname = fpriv.u.t1.fullname;
713
53.8k
            t1f->familyname = fpriv.u.t1.familyname;
714
53.8k
            fpriv.u.t1.copyright = fpriv.u.t1.notice = fpriv.u.t1.fullname = fpriv.u.t1.familyname = NULL;
715
716
53.8k
            code = pdfi_font_generate_pseudo_XUID(ctx, font_dict, t1f->pfont);
717
53.8k
            if (code < 0) {
718
0
                goto error;
719
0
            }
720
721
53.8k
            t1f->blenddesignpositions = fpriv.u.t1.blenddesignpositions;
722
53.8k
            pdfi_countup(t1f->blenddesignpositions);
723
53.8k
            t1f->blenddesignmap = fpriv.u.t1.blenddesignmap;
724
53.8k
            pdfi_countup(t1f->blenddesignmap);
725
53.8k
            t1f->blendfontbbox = fpriv.u.t1.blendfontbbox;
726
53.8k
            pdfi_countup(t1f->blendfontbbox);
727
53.8k
            t1f->blendaxistypes = fpriv.u.t1.blendaxistypes;
728
53.8k
            pdfi_countup(t1f->blendaxistypes);
729
730
53.8k
            key_known = false;
731
53.8k
            if (t1f->FontDescriptor != NULL) {
732
6.22k
                 code = pdfi_dict_known(ctx, t1f->FontDescriptor, "FontFile", &key_known);
733
6.22k
                 if (code < 0 || key_known == false) {
734
351
                     code = pdfi_dict_known(ctx, t1f->FontDescriptor, "FontFile2", &key_known);
735
351
                     if (code < 0 || key_known == false) {
736
0
                         code = pdfi_dict_known(ctx, t1f->FontDescriptor, "FontFile3", &key_known);
737
0
                         if (code < 0) {
738
0
                             key_known = false;
739
0
                         }
740
0
                     }
741
351
                 }
742
6.22k
            }
743
53.8k
            t1f->pfont->is_resource = (key_known == false);
744
745
53.8k
            pdfi_font_set_orig_fonttype(ctx, (pdf_font *)t1f);
746
53.8k
            code = gs_definefont(ctx->font_dir, (gs_font *) t1f->pfont);
747
53.8k
            if (code < 0) {
748
0
                goto error;
749
0
            }
750
751
53.8k
            code = pdfi_fapi_passfont((pdf_font *) t1f, 0, NULL, NULL, NULL, 0);
752
53.8k
            if (code < 0) {
753
0
                goto error;
754
0
            }
755
            /* object_num can be zero if the dictionary was defined inline */
756
53.8k
            if (t1f->object_num != 0) {
757
5.86k
                (void)replace_cache_entry(ctx, (pdf_obj *) t1f);
758
5.86k
            }
759
53.8k
            *ppdffont = (pdf_font *) t1f;
760
53.8k
        }
761
53.8k
    }
762
763
59.0k
  error:
764
59.0k
    pdfi_countdown(fontdesc);
765
59.0k
    pdfi_countdown(basefont);
766
59.0k
    pdfi_countdown(tounicode);
767
59.0k
    pdfi_countdown(mapname);
768
59.0k
    pdfi_countdown(tmp);
769
59.0k
    pdfi_countdown(fpriv.u.t1.Encoding);
770
59.0k
    pdfi_countdown(fpriv.u.t1.CharStrings);
771
59.0k
    pdfi_countdown(fpriv.u.t1.blenddesignpositions);
772
59.0k
    pdfi_countdown(fpriv.u.t1.blenddesignmap);
773
59.0k
    pdfi_countdown(fpriv.u.t1.blendfontbbox);
774
59.0k
    pdfi_countdown(fpriv.u.t1.blendaxistypes);
775
59.0k
    pdfi_countdown(fpriv.u.t1.Subrs);
776
59.0k
    pdfi_countdown(fpriv.u.t1.copyright);
777
59.0k
    pdfi_countdown(fpriv.u.t1.notice);
778
59.0k
    pdfi_countdown(fpriv.u.t1.fullname);
779
59.0k
    pdfi_countdown(fpriv.u.t1.familyname);
780
59.0k
    if (fpriv.gsu.gst1.UID.xvalues != NULL) {
781
0
        gs_free_object(ctx->memory, fpriv.gsu.gst1.UID.xvalues, "pdfi_read_type1_font(xuid)");
782
0
    }
783
784
59.0k
    if (code < 0) {
785
5.19k
        tmp = NULL;
786
5.19k
        if (font_dict != NULL) {
787
5.19k
            if (pdfi_dict_get_type(ctx, font_dict, ".Path", PDF_STRING, &tmp) >= 0)
788
0
            {
789
0
                char fname[gp_file_name_sizeof + 1];
790
0
                pdf_string *fobj = (pdf_string *)tmp;
791
792
0
                memcpy(fname, fobj->data, fobj->length > gp_file_name_sizeof ? gp_file_name_sizeof : fobj->length);
793
0
                fname[fobj->length > gp_file_name_sizeof ? gp_file_name_sizeof : fobj->length] = '\0';
794
795
0
                (void)pdfi_set_error_var(ctx, code, NULL, E_PDF_BADSTREAM, "pdfi_read_type1_font", "Error reading Type 1 font file %s\n", fname);
796
0
            }
797
5.19k
            else {
798
5.19k
                (void)pdfi_set_error_var(ctx, code, NULL, E_PDF_BADSTREAM, "pdfi_read_type1_font", "Error reading embedded Type 1 font object %u\n", font_dict->object_num);
799
5.19k
            }
800
5.19k
        }
801
0
        else {
802
0
            pdfi_set_error(ctx, code, NULL, E_PDF_BADSTREAM, "pdfi_read_truetype_font", "Error reading font\n");
803
0
        }
804
5.19k
        pdfi_countdown(tmp);
805
5.19k
        pdfi_countdown(t1f);
806
5.19k
    }
807
59.0k
    return code;
808
59.0k
}
809
810
int
811
pdfi_copy_type1_font(pdf_context *ctx, pdf_font *spdffont, pdf_dict *font_dict, pdf_font **tpdffont)
812
489k
{
813
489k
    int code = 0;
814
489k
    pdf_font_type1 *font = NULL;
815
489k
    gs_font_type1 *spfont1 = (gs_font_type1 *) spdffont->pfont;
816
489k
    gs_font_type1 *dpfont1;
817
489k
    gs_id t_id;
818
489k
    pdf_obj *tmp;
819
489k
    bool force_symbolic = false;
820
821
489k
    if (font_dict == NULL)
822
0
        return_error(gs_error_invalidfont);
823
824
489k
    code = pdfi_alloc_t1_font(ctx, &font, font_dict->object_num);
825
489k
    if (code < 0)
826
0
        return code;
827
489k
    dpfont1 = (gs_font_type1 *) font->pfont;
828
829
489k
    t_id = dpfont1->id;
830
489k
    memcpy(dpfont1, spfont1, sizeof(gs_font_type1));
831
489k
    dpfont1->id = t_id;
832
489k
    dpfont1->FAPI = NULL;
833
489k
    dpfont1->FAPI_font_data = NULL;
834
835
489k
    memcpy(font, spdffont, sizeof(pdf_font_type1));
836
489k
    font->pfont = (gs_font_base *)dpfont1;
837
489k
    font->refcnt = 1;
838
489k
    dpfont1->client_data = (void *)font;
839
489k
    font->filename = NULL;
840
841
489k
    dpfont1->notify_list.memory = NULL;
842
489k
    dpfont1->notify_list.first = NULL;
843
489k
    gs_notify_init(&dpfont1->notify_list, dpfont1->memory);
844
845
489k
    font->PDF_font = font_dict;
846
489k
    font->object_num = font_dict->object_num;
847
489k
    font->generation_num = font_dict->generation_num;
848
489k
    pdfi_countup(font->PDF_font);
849
850
    /* We want basefont and descriptor, but we can live without them */
851
489k
    font->BaseFont = NULL;
852
489k
    code = pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, &font->BaseFont);
853
489k
    if (code < 0) {
854
261
        pdfi_countdown(font->BaseFont);
855
261
        font->BaseFont = NULL;
856
261
    }
857
489k
    font->FontDescriptor = NULL;
858
489k
    code = pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, (pdf_obj **)&font->FontDescriptor);
859
489k
    if (code < 0) {
860
5.66k
        pdfi_countdown(font->FontDescriptor);
861
5.66k
        font->FontDescriptor = NULL;
862
5.66k
    }
863
864
489k
    pdfi_countup(font->Name);
865
489k
    pdfi_countup(font->CharStrings);
866
489k
    pdfi_countup(font->blenddesignpositions);
867
489k
    pdfi_countup(font->blenddesignmap);
868
489k
    pdfi_countup(font->blendfontbbox);
869
489k
    pdfi_countup(font->blendaxistypes);
870
489k
    pdfi_countup(font->Subrs);
871
489k
    pdfi_countup(font->copyright);
872
489k
    pdfi_countup(font->notice);
873
489k
    pdfi_countup(font->fullname);
874
489k
    pdfi_countup(font->familyname);
875
876
489k
    if (font->BaseFont != NULL && ((pdf_name *)font->BaseFont)->length <= gs_font_name_max - 1) {
877
488k
        memcpy(dpfont1->key_name.chars, ((pdf_name *)font->BaseFont)->data, ((pdf_name *)font->BaseFont)->length);
878
488k
        dpfont1->key_name.size = ((pdf_name *)font->BaseFont)->length;
879
488k
        dpfont1->key_name.chars[dpfont1->key_name.size] = '\0';
880
488k
        memcpy(dpfont1->font_name.chars, ((pdf_name *)font->BaseFont)->data, ((pdf_name *)font->BaseFont)->length);
881
488k
        dpfont1->font_name.size = ((pdf_name *)font->BaseFont)->length;
882
488k
        dpfont1->font_name.chars[dpfont1->font_name.size] = '\0';
883
488k
    }
884
885
489k
    font->Encoding = NULL;
886
489k
    font->ToUnicode = NULL;
887
489k
    font->Widths = NULL;
888
889
489k
    pdfi_font_set_first_last_char(ctx, font_dict, (pdf_font *)font);
890
489k
    (void)pdfi_font_create_widths(ctx, font_dict, (pdf_font *)font, (double)(0.001 / hypot(dpfont1->FontMatrix.xx, dpfont1->FontMatrix.xy)));
891
892
489k
    font->descflags = 0;
893
489k
    if (font->FontDescriptor != NULL) {
894
28.1k
        code = pdfi_dict_get_int(ctx, font->FontDescriptor, "Flags", &font->descflags);
895
28.1k
        if (code >= 0) {
896
            /* If both the symbolic and non-symbolic flag are set,
897
               believe that latter.
898
             */
899
27.9k
            if ((font->descflags & 32) != 0)
900
20.6k
                font->descflags = (font->descflags & ~4);
901
27.9k
        }
902
28.1k
    }
903
904
489k
    if (pdfi_font_known_symbolic(font->BaseFont)) {
905
1.99k
        force_symbolic = true;
906
1.99k
        font->descflags |= 4;
907
1.99k
    }
908
909
489k
    tmp = NULL;
910
489k
    code = pdfi_dict_knownget(ctx, font_dict, "Encoding", &tmp);
911
489k
    if (code == 1) {
912
38.4k
        if (pdfi_type_of(tmp) == PDF_NAME && force_symbolic == true) {
913
0
            font->Encoding = spdffont->Encoding;
914
0
            pdfi_countup(font->Encoding);
915
0
        }
916
38.4k
        else if (pdfi_type_of(tmp) == PDF_DICT && (font->descflags & 4) != 0) {
917
3.13k
            code = pdfi_create_Encoding(ctx, (pdf_font *)font, tmp, (pdf_obj *)spdffont->Encoding, (pdf_obj **) &font->Encoding);
918
3.13k
            if (code >= 0)
919
3.13k
                code = 1;
920
3.13k
        }
921
35.3k
        else {
922
35.3k
            code = pdfi_create_Encoding(ctx, (pdf_font *)font, tmp, NULL, (pdf_obj **) & font->Encoding);
923
35.3k
            if (code >= 0)
924
34.5k
                code = 1;
925
35.3k
        }
926
38.4k
        pdfi_countdown(tmp);
927
38.4k
        tmp = NULL;
928
38.4k
    }
929
451k
    else {
930
451k
        pdfi_countdown(tmp);
931
451k
        tmp = NULL;
932
451k
        code = 0;
933
451k
    }
934
935
489k
    if (code <= 0) {
936
451k
        font->Encoding = spdffont->Encoding;
937
451k
        pdfi_countup(font->Encoding);
938
451k
    }
939
940
489k
    code = uid_copy(&font->pfont->UID, font->pfont->memory, "pdfi_copy_type1_font");
941
489k
    if (code < 0) {
942
0
        uid_set_invalid(&font->pfont->UID);
943
0
    }
944
489k
    if (spdffont->filename == NULL) {
945
136
        code = pdfi_font_generate_pseudo_XUID(ctx, font_dict, font->pfont);
946
136
        if (code < 0) {
947
0
            goto error;
948
0
        }
949
136
    }
950
951
489k
    if (ctx->args.ignoretounicode != true) {
952
489k
        code = pdfi_dict_get(ctx, font_dict, "ToUnicode", (pdf_obj **)&tmp);
953
489k
        if (code >= 0 && pdfi_type_of(tmp) == PDF_STREAM) {
954
7.39k
            pdf_cmap *tu = NULL;
955
7.39k
            code = pdfi_read_cmap(ctx, tmp, &tu);
956
7.39k
            pdfi_countdown(tmp);
957
7.39k
            tmp = (pdf_obj *)tu;
958
7.39k
        }
959
489k
        if (code < 0 || (tmp != NULL && pdfi_type_of(tmp) != PDF_CMAP)) {
960
482k
            pdfi_countdown(tmp);
961
482k
            tmp = NULL;
962
482k
            code = 0;
963
482k
        }
964
489k
    }
965
0
    else {
966
0
        tmp = NULL;
967
0
    }
968
489k
    font->ToUnicode = tmp;
969
970
489k
    pdfi_font_set_orig_fonttype(ctx, (pdf_font *)font);
971
489k
    code = gs_definefont(ctx->font_dir, (gs_font *) font->pfont);
972
489k
    if (code < 0) {
973
0
        goto error;
974
0
    }
975
976
489k
    code = pdfi_fapi_passfont((pdf_font *) font, 0, NULL, NULL, NULL, 0);
977
489k
    if (code < 0) {
978
0
        goto error;
979
0
    }
980
    /* object_num can be zero if the dictionary was defined inline */
981
489k
    if (font->object_num != 0) {
982
45.6k
        (void)replace_cache_entry(ctx, (pdf_obj *) font);
983
45.6k
    }
984
985
489k
    *tpdffont = (pdf_font *)font;
986
987
489k
error:
988
489k
    if (code < 0)
989
0
        pdfi_countdown(font);
990
991
489k
    return code;
992
489k
}
993
994
int
995
pdfi_free_font_type1(pdf_obj *font)
996
543k
{
997
543k
    pdf_font_type1 *t1f = (pdf_font_type1 *) font;
998
999
543k
    gs_free_object(OBJ_MEMORY(font), t1f->pfont, "Free Type 1 gs_font");
1000
1001
543k
    pdfi_countdown(t1f->PDF_font);
1002
543k
    pdfi_countdown(t1f->BaseFont);
1003
543k
    pdfi_countdown(t1f->FontDescriptor);
1004
543k
    pdfi_countdown(t1f->Name);
1005
543k
    pdfi_countdown(t1f->Encoding);
1006
543k
    pdfi_countdown(t1f->ToUnicode);
1007
543k
    pdfi_countdown(t1f->CharStrings);
1008
543k
    pdfi_countdown(t1f->blenddesignpositions);
1009
543k
    pdfi_countdown(t1f->blenddesignmap);
1010
543k
    pdfi_countdown(t1f->blendfontbbox);
1011
543k
    pdfi_countdown(t1f->blendaxistypes);
1012
543k
    pdfi_countdown(t1f->Subrs);
1013
543k
    pdfi_countdown(t1f->filename);
1014
543k
    pdfi_countdown(t1f->copyright);
1015
543k
    pdfi_countdown(t1f->notice);
1016
543k
    pdfi_countdown(t1f->fullname);
1017
543k
    pdfi_countdown(t1f->familyname);
1018
1019
543k
    gs_free_object(OBJ_MEMORY(font), t1f->Widths, "Free Type 1 fontWidths");
1020
    gs_free_object(OBJ_MEMORY(font), t1f, "Free Type 1 font");
1021
543k
    return 0;
1022
543k
}