Coverage Report

Created: 2025-12-31 07:31

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-2025 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
74.7k
{
67
74.7k
    int code = 0;
68
74.7k
    pdf_obj *o;
69
74.7k
    const pdfi_t1_glyph_name_equivalents_t *gne = pdfi_t1_glyph_name_equivalents;
70
373k
    while(gne->name != NULL && code >= 0) {
71
298k
        code = pdfi_dict_get(cstrings->ctx, cstrings, gne->name, &o);
72
298k
        if (code >= 0) {
73
238k
            code = pdfi_dict_put(cstrings->ctx, cstrings, gne->altname, o);
74
238k
            pdfi_countdown(o);
75
238k
        }
76
298k
        if (code == gs_error_undefined)
77
60.4k
            code = 0;
78
298k
        gne++;
79
298k
    }
80
81
74.7k
    if (code >= 0) {
82
74.7k
        bool key_known;
83
74.7k
        pdf_string *pstr;
84
74.7k
        byte notdefstr[] = { 0x9E, 0x35, 0xCE, 0xD7, 0xFF, 0xD3, 0x62, 0x2F, 0x09 };
85
86
74.7k
        code = pdfi_dict_known(cstrings->ctx, cstrings, ".notdef", &key_known);
87
74.7k
        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
2.07k
            code = pdfi_object_alloc(cstrings->ctx, PDF_STRING, sizeof(notdefstr), (pdf_obj **) &pstr);
92
2.07k
            if (code >= 0) {
93
2.07k
                memcpy(pstr->data, notdefstr, sizeof(notdefstr));
94
2.07k
                (void)pdfi_dict_put(cstrings->ctx, cstrings, ".notdef", (pdf_obj *) pstr);
95
2.07k
            }
96
2.07k
        }
97
74.7k
    }
98
74.7k
}
99
100
/* CALLBACKS */
101
static int
102
pdfi_t1_glyph_data(gs_font_type1 *pfont, gs_glyph glyph, gs_glyph_data_t *pgd)
103
94.1M
{
104
94.1M
    int code = 0;
105
94.1M
    pdf_font_type1 *pdffont1 = (pdf_font_type1 *) pfont->client_data;
106
94.1M
    pdf_context *ctx = (pdf_context *) pdffont1->ctx;
107
94.1M
    pdf_name *glyphname = NULL;
108
94.1M
    pdf_string *charstring = NULL;
109
94.1M
    gs_const_string gname;
110
111
94.1M
    code = (*ctx->get_glyph_name)((gs_font *)pfont, glyph, &gname);
112
94.1M
    if (code >= 0) {
113
94.1M
        code = pdfi_name_alloc(ctx, (byte *) gname.data, gname.size, (pdf_obj **)&glyphname);
114
94.1M
        if (code >= 0)
115
94.1M
            pdfi_countup(glyphname);
116
94.1M
    }
117
118
94.1M
    if (code >= 0) {
119
94.1M
        code = pdfi_dict_get_by_key(ctx, pdffont1->CharStrings, glyphname, (pdf_obj **)&charstring);
120
94.1M
        if (code < 0) {
121
451k
            code = pdfi_map_glyph_name_via_agl(pdffont1->CharStrings, glyphname, &charstring);
122
451k
        }
123
94.1M
        if (code >= 0)
124
93.7M
            gs_glyph_data_from_bytes(pgd, charstring->data, 0, charstring->length, NULL);
125
94.1M
    }
126
94.1M
    pdfi_countdown(charstring);
127
94.1M
    pdfi_countdown(glyphname);
128
129
94.1M
    return code;
130
94.1M
}
131
132
static int
133
pdfi_t1_subr_data(gs_font_type1 *pfont, int index, bool global, gs_glyph_data_t *pgd)
134
44.1M
{
135
44.1M
    int code = 0;
136
44.1M
    pdf_font_type1 *pdffont1 = (pdf_font_type1 *) pfont->client_data;
137
138
44.1M
    if (global == true || index < 0 || index >= (pdffont1->Subrs == NULL ? 0 : pdfi_array_size(pdffont1->Subrs))) {
139
483k
        code = gs_note_error(gs_error_rangecheck);
140
483k
    }
141
43.6M
    else {
142
43.6M
        pdf_string *subr_str = NULL;
143
43.6M
        code = pdfi_array_get_type(pdffont1->ctx, pdffont1->Subrs, index, PDF_STRING, (pdf_obj **)&subr_str);
144
43.6M
        if (code >= 0) {
145
43.6M
            gs_glyph_data_from_bytes(pgd, subr_str->data, 0, subr_str->length, NULL);
146
43.6M
        }
147
        /* decrementing is safe here, because the reference in the pdffont1->Subrs will persist */
148
43.6M
        pdfi_countdown(subr_str);
149
43.6M
    }
150
44.1M
    return code;
151
44.1M
}
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
6.85k
{
156
6.85k
    int code = 0;
157
6.85k
    pdf_font_type1 *pdffont1 = (pdf_font_type1 *) pfont->client_data;
158
6.85k
    pdf_context *ctx = (pdf_context *) pdffont1->ctx;
159
6.85k
    gs_glyph glyph = gs_c_known_encode((gs_char)ccode, ENCODING_INDEX_STANDARD);
160
161
6.85k
    if (glyph == GS_NO_GLYPH)
162
44
        return_error(gs_error_rangecheck);
163
164
6.81k
    code = gs_c_glyph_name(glyph, gstr);
165
6.81k
    if (code >= 0) {
166
6.81k
        unsigned int nindex;
167
6.81k
        code = (*ctx->get_glyph_index)((gs_font *)pfont, (byte *)gstr->data, gstr->size, &nindex);
168
6.81k
        if (pglyph != NULL)
169
6.81k
            *pglyph = (gs_glyph)nindex;
170
6.81k
    }
171
172
6.81k
    if (code >= 0) {
173
6.81k
        pdf_name *glyphname = NULL;
174
6.81k
        pdf_string *charstring = NULL;
175
6.81k
        code = pdfi_name_alloc(ctx, (byte *) gstr->data, gstr->size, (pdf_obj **) &glyphname);
176
6.81k
        if (code >= 0) {
177
6.81k
            pdfi_countup(glyphname);
178
6.81k
            code = pdfi_dict_get_by_key(ctx, pdffont1->CharStrings, glyphname, (pdf_obj **)&charstring);
179
6.81k
            pdfi_countdown(glyphname);
180
6.81k
            if (code >= 0) {
181
6.78k
                if (pgd != NULL) {
182
0
                    gs_glyph_data_from_bytes(pgd, charstring->data, 0, charstring->length, NULL);
183
0
                }
184
6.78k
                pdfi_countdown(charstring);
185
6.78k
            }
186
6.81k
        }
187
6.81k
    }
188
189
6.81k
    return code;
190
6.85k
}
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
107M
{
213
107M
    int code;
214
107M
    pdf_font_type1 *t1font = (pdf_font_type1 *) pfont->client_data;
215
107M
    pdf_context *ctx = (pdf_context *) t1font->ctx;
216
107M
    pdf_name *key;
217
107M
    uint64_t i = (uint64_t) *pindex;
218
219
107M
    (void)glyph_space;
220
221
107M
    if (*pindex <= 0)
222
655k
        code = pdfi_dict_key_first(ctx, t1font->CharStrings, (pdf_obj **) & key, &i);
223
106M
    else
224
106M
        code = pdfi_dict_key_next(ctx, t1font->CharStrings, (pdf_obj **) & key, &i);
225
107M
    if (code < 0) {
226
118k
        *pindex = 0;
227
118k
        code = 0;
228
118k
    }
229
107M
    else {
230
107M
        uint dummy = GS_NO_GLYPH;
231
232
107M
        code = (*ctx->get_glyph_index)(pfont, key->data, key->length, &dummy);
233
107M
        if (code < 0) {
234
0
            *pglyph = (gs_glyph) *pindex;
235
0
            goto exit;
236
0
        }
237
107M
        *pglyph = dummy;
238
107M
        if (*pglyph == GS_NO_GLYPH)
239
0
            *pglyph = (gs_glyph) *pindex;
240
107M
        *pindex = (int)i;
241
107M
    }
242
107M
  exit:
243
107M
    pdfi_countdown(key);
244
107M
    return code;
245
107M
}
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
2
{
253
2
    *pglyph = gs_c_name_glyph(gstr->data, gstr->size);
254
2
    return 0;
255
2
}
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.52M
{
261
1.52M
    gs_glyph_data_t gd;
262
1.52M
    gs_glyph_data_t *pgd = &gd;
263
1.52M
    gs_font_type1 *pfont1 = (gs_font_type1 *) pfont;
264
1.52M
    int code = pdfi_t1_glyph_data(pfont1, glyph, pgd);
265
266
1.52M
    if (code >= 0) {
267
1.51M
        gs_type1_state cis = { 0 };
268
1.51M
        gs_type1_state *pcis = &cis;
269
1.51M
        gs_gstate gs;
270
1.51M
        int value;
271
272
1.51M
        if (pmat)
273
1.51M
            gs_matrix_fixed_from_matrix(&gs.ctm, pmat);
274
449
        else {
275
449
            gs_matrix imat;
276
277
449
            gs_make_identity(&imat);
278
449
            gs_matrix_fixed_from_matrix(&gs.ctm, &imat);
279
449
        }
280
1.51M
        gs.flatness = 0;
281
1.51M
        code = gs_type1_interp_init(pcis, &gs, ppath, NULL, NULL, true, 0, pfont1);
282
1.51M
        if (code < 0)
283
0
            return code;
284
285
1.51M
        pcis->no_grid_fitting = true;
286
1.51M
        gs_type1_set_callback_data(pcis, NULL);
287
        /* Continue interpreting. */
288
3.03M
      icont:
289
3.03M
        code = pfont1->data.interpret(pcis, pgd, &value);
290
3.03M
        switch (code) {
291
1.51M
            case 0:            /* all done */
292
                /* falls through */
293
1.51M
            default:           /* code < 0, error */
294
1.51M
                return code;
295
0
            case type1_result_callothersubr:   /* unknown OtherSubr */
296
0
                return_error(gs_error_rangecheck);      /* can't handle it */
297
1.51M
            case type1_result_sbw:     /* [h]sbw, just continue */
298
1.51M
                type1_cis_get_metrics(pcis, sbw);
299
1.51M
                pgd = 0;
300
1.51M
                goto icont;
301
3.03M
        }
302
3.03M
    }
303
10.2k
    return code;
304
1.52M
}
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
68.6M
{
309
68.6M
    if ((members & GLYPH_INFO_OUTLINE_WIDTHS) == 0)
310
67.1M
        return gs_type1_glyph_info(font, glyph, pmat, members, info);
311
312
1.52M
    return gs_default_glyph_info(font, glyph, pmat, members, info);
313
68.6M
}
314
315
/* END CALLBACKS */
316
317
static stream *
318
push_pfb_filter(gs_memory_t *mem, byte *buf, byte *bufend)
319
458
{
320
458
    stream *fs, *ffs = NULL;
321
458
    stream *sstrm;
322
458
    stream_PFBD_state *st;
323
458
    byte *strbuf;
324
325
458
    sstrm = file_alloc_stream(mem, "push_pfb_filter(buf stream)");
326
458
    if (sstrm == NULL)
327
0
        return NULL;
328
329
458
    sread_string(sstrm, buf, bufend - buf);
330
458
    sstrm->close_at_eod = false;
331
332
458
    fs = s_alloc(mem, "push_pfb_filter(fs)");
333
458
    strbuf = gs_alloc_bytes(mem, 4096, "push_pfb_filter(buf)");
334
458
    st = gs_alloc_struct(mem, stream_PFBD_state, s_PFBD_template.stype, "push_pfb_filter(st)");
335
458
    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
458
    memset(st, 0x00, sizeof(stream_PFBD_state));
343
458
    (*s_PFBD_template.init)((stream_state *)st);
344
458
    st->binary_to_hex = true;
345
458
    s_std_init(fs, strbuf, 4096, &s_filter_read_procs, s_mode_read);
346
458
    st->memory = mem;
347
458
    st->templat = &s_PFBD_template;
348
458
    fs->state = (stream_state *) st;
349
458
    fs->procs.process = s_PFBD_template.process;
350
458
    fs->strm = sstrm;
351
458
    fs->close_at_eod = false;
352
458
    ffs = fs;
353
458
  done:
354
458
    return ffs;
355
458
}
356
357
static void
358
pop_pfb_filter(gs_memory_t *mem, stream *s)
359
458
{
360
458
    stream *src = s->strm;
361
458
    byte *b = s->cbuf;
362
363
458
    sclose(s);
364
458
    gs_free_object(mem, s, "push_pfb_filter(s)");
365
458
    gs_free_object(mem, b, "push_pfb_filter(b)");
366
458
    if (src)
367
458
        sclose(src);
368
458
    gs_free_object(mem, src, "push_pfb_filter(strm)");
369
458
}
370
371
static int
372
pdfi_t1_decode_pfb(pdf_context *ctx, byte *inbuf, int inlen, byte **outbuf, int *outlen)
373
229
{
374
229
    stream *strm;
375
229
    int c, code = 0;
376
229
    int decodelen = 0;
377
229
    byte *d, *decodebuf = NULL;
378
379
229
    *outbuf = NULL;
380
229
    *outlen = 0;
381
382
229
    strm = push_pfb_filter(ctx->memory, inbuf, inbuf + inlen);
383
229
    if (strm == NULL) {
384
0
        code = gs_note_error(gs_error_VMerror);
385
0
    }
386
229
    else {
387
5.35M
        while (1) {
388
5.35M
            c = sgetc(strm);
389
5.35M
            if (c < 0)
390
229
                break;
391
5.35M
            decodelen++;
392
5.35M
        }
393
229
        pop_pfb_filter(ctx->memory, strm);
394
229
        decodebuf = gs_alloc_bytes(ctx->memory, decodelen, "pdfi_t1_decode_pfb(decodebuf)");
395
229
        if (decodebuf == NULL) {
396
0
            code = gs_note_error(gs_error_VMerror);
397
0
        }
398
229
        else {
399
229
            d = decodebuf;
400
229
            strm = push_pfb_filter(ctx->memory, inbuf, inbuf + inlen);
401
5.35M
            while (1) {
402
5.35M
                c = sgetc(strm);
403
5.35M
                if (c < 0)
404
229
                    break;
405
5.35M
                *d = c;
406
5.35M
                d++;
407
5.35M
            }
408
229
            pop_pfb_filter(ctx->memory, strm);
409
229
            *outbuf = decodebuf;
410
229
            *outlen = decodelen;
411
229
        }
412
229
    }
413
229
    return code;
414
229
}
415
416
static int
417
pdfi_alloc_t1_font(pdf_context *ctx, pdf_font_type1 **font, uint32_t obj_num)
418
1.08M
{
419
1.08M
    pdf_font_type1 *t1font = NULL;
420
1.08M
    gs_font_type1 *pfont = NULL;
421
422
1.08M
    t1font = (pdf_font_type1 *) gs_alloc_bytes(ctx->memory, sizeof(pdf_font_type1), "pdfi (type 1 pdf_font)");
423
1.08M
    if (t1font == NULL)
424
0
        return_error(gs_error_VMerror);
425
426
1.08M
    memset(t1font, 0x00, sizeof(pdf_font_type1));
427
1.08M
    t1font->ctx = ctx;
428
1.08M
    t1font->type = PDF_FONT;
429
1.08M
    t1font->ctx = ctx;
430
1.08M
    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
1.08M
    pdfi_countup(t1font);
438
439
1.08M
    pfont = (gs_font_type1 *) gs_alloc_struct(ctx->memory, gs_font_type1, &st_gs_font_type1, "pdfi (Type 1 pfont)");
440
1.08M
    if (pfont == NULL) {
441
0
        pdfi_countdown(t1font);
442
0
        return_error(gs_error_VMerror);
443
0
    }
444
1.08M
    memset(pfont, 0x00, sizeof(gs_font_type1));
445
446
1.08M
    t1font->pfont = (gs_font_base *) pfont;
447
448
1.08M
    gs_make_identity(&pfont->orig_FontMatrix);
449
1.08M
    gs_make_identity(&pfont->FontMatrix);
450
1.08M
    pfont->next = pfont->prev = 0;
451
1.08M
    pfont->memory = ctx->memory;
452
1.08M
    pfont->dir = ctx->font_dir;
453
1.08M
    pfont->is_resource = false;
454
1.08M
    gs_notify_init(&pfont->notify_list, ctx->memory);
455
1.08M
    pfont->base = (gs_font *) t1font->pfont;
456
1.08M
    pfont->client_data = t1font;
457
1.08M
    pfont->WMode = 0;
458
1.08M
    pfont->PaintType = 0;
459
1.08M
    pfont->StrokeWidth = 0;
460
1.08M
    pfont->is_cached = 0;
461
1.08M
    pfont->FAPI = NULL;
462
1.08M
    pfont->FAPI_font_data = NULL;
463
1.08M
    pfont->procs.init_fstack = gs_default_init_fstack;
464
1.08M
    pfont->procs.next_char_glyph = gs_default_next_char_glyph;
465
1.08M
    pfont->FontType = ft_encrypted;
466
1.08M
    pfont->ExactSize = fbit_use_outlines;
467
1.08M
    pfont->InBetweenSize = fbit_use_outlines;
468
1.08M
    pfont->TransformedChar = fbit_use_outlines;
469
    /* We may want to do something clever with an XUID here */
470
1.08M
    pfont->id = gs_next_ids(ctx->memory, 1);
471
1.08M
    uid_set_UniqueID(&pfont->UID, pfont->id);
472
473
1.08M
    pfont->encoding_index = ENCODING_INDEX_UNKNOWN;
474
1.08M
    pfont->nearest_encoding_index = ENCODING_INDEX_UNKNOWN;
475
476
1.08M
    pfont->client_data = (void *)t1font;
477
478
1.08M
    *font = t1font;
479
1.08M
    return 0;
480
1.08M
}
481
482
static void
483
pdfi_t1_font_set_procs(pdf_context *ctx, pdf_font_type1 *font)
484
74.7k
{
485
74.7k
    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
74.7k
    pfont->procs.build_char = NULL;
490
491
74.7k
    pfont->procs.encode_char = pdfi_encode_char;
492
74.7k
    pfont->procs.glyph_name = ctx->get_glyph_name;
493
74.7k
    pfont->procs.decode_glyph = pdfi_decode_glyph;
494
74.7k
    pfont->procs.define_font = gs_no_define_font;
495
74.7k
    pfont->procs.make_font = gs_no_make_font;
496
497
74.7k
    font->default_font_info = gs_default_font_info;
498
74.7k
    pfont->procs.font_info = pdfi_default_font_info;
499
500
74.7k
    pfont->procs.glyph_info = pdfi_t1_glyph_info;
501
74.7k
    pfont->procs.glyph_outline = pdfi_t1_glyph_outline;
502
74.7k
    pfont->procs.same_font = gs_default_same_font;
503
74.7k
    pfont->procs.enumerate_glyph = pdfi_t1_enumerate_glyph;
504
505
74.7k
    pfont->data.procs.glyph_data = pdfi_t1_glyph_data;
506
74.7k
    pfont->data.procs.subr_data = pdfi_t1_subr_data;
507
74.7k
    pfont->data.procs.seac_data = pdfi_t1_seac_data;
508
74.7k
    pfont->data.procs.push_values = pdfi_t1_push;
509
74.7k
    pfont->data.procs.pop_value = pdfi_t1_pop;
510
74.7k
    pfont->data.interpret = gs_type1_interpret;
511
74.7k
}
512
513
static inline void
514
pdfi_type1_font_priv_defaults(ps_font_interp_private *pfpriv)
515
82.3k
{
516
82.3k
    pfpriv->gsu.gst1.data.lenIV = 4;
517
82.3k
    pfpriv->gsu.gst1.data.ExpansionFactor = 0.06;
518
82.3k
    pfpriv->gsu.gst1.data.BlueShift = 7;
519
82.3k
    pfpriv->gsu.gst1.data.BlueFuzz = 1;
520
82.3k
    pfpriv->gsu.gst1.data.BlueScale = 0.039625;
521
82.3k
    uid_set_invalid(&pfpriv->gsu.gst1.UID);
522
82.3k
}
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
82.3k
{
527
82.3k
    int code = 0;
528
82.3k
    double x_scale;
529
82.3k
    pdf_obj *fontdesc = NULL;
530
82.3k
    pdf_obj *basefont = NULL;
531
82.3k
    pdf_obj *mapname = NULL;
532
82.3k
    pdf_obj *tmp = NULL;
533
82.3k
    pdf_font_type1 *t1f = NULL;
534
82.3k
    pdf_obj *tounicode = NULL;
535
82.3k
    ps_font_interp_private fpriv = { 0 };
536
82.3k
    bool key_known;
537
82.3k
    bool force_symbolic = false;
538
539
82.3k
    if (font_dict != NULL)
540
20.4k
        (void)pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, &fontdesc);
541
542
82.3k
    if (fbuf[0] == 128 && fbuf[1] == 1) {
543
229
        byte *decodebuf = NULL;
544
229
        int decodelen;
545
546
229
        code = pdfi_t1_decode_pfb(ctx, fbuf, fbuflen, &decodebuf, &decodelen);
547
229
        gs_free_object(ctx->memory, fbuf, "pdfi_read_type1_font");
548
229
        if (code < 0) {
549
0
            gs_free_object(ctx->memory, decodebuf, "pdfi_read_type1_font");
550
0
        }
551
229
        fbuf = decodebuf;
552
229
        fbuflen = decodelen;
553
229
    }
554
555
82.3k
    if (code >= 0) {
556
82.3k
        pdfi_type1_font_priv_defaults(&fpriv);
557
82.3k
        code = pdfi_read_ps_font(ctx, font_dict, fbuf, fbuflen, &fpriv);
558
82.3k
        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
82.3k
        if (code < 0 || fpriv.u.t1.CharStrings == NULL || pdfi_type_of(fpriv.u.t1.CharStrings) != PDF_DICT
562
74.8k
            || fpriv.u.t1.CharStrings->entries == 0) {
563
7.63k
                code = gs_note_error(gs_error_invalidfont);
564
7.63k
                goto error;
565
7.63k
        }
566
74.7k
        code = pdfi_alloc_t1_font(ctx, &t1f, font_dict != NULL ? font_dict->object_num : 0);
567
74.7k
        if (code >= 0) {
568
74.7k
            gs_font_type1 *pfont1 = (gs_font_type1 *) t1f->pfont;
569
570
74.7k
            memcpy(&pfont1->data, &fpriv.gsu.gst1.data, sizeof(pfont1->data));
571
572
74.7k
            pdfi_t1_font_set_procs(ctx, t1f);
573
574
74.7k
            memcpy(&pfont1->FontMatrix, &fpriv.gsu.gst1.FontMatrix, sizeof(pfont1->FontMatrix));
575
74.7k
            memcpy(&pfont1->orig_FontMatrix, &fpriv.gsu.gst1.orig_FontMatrix, sizeof(pfont1->orig_FontMatrix));
576
74.7k
            memcpy(&pfont1->FontBBox, &fpriv.gsu.gst1.FontBBox, sizeof(pfont1->FontBBox));
577
74.7k
            memcpy(&pfont1->key_name, &fpriv.gsu.gst1.key_name, sizeof(pfont1->key_name));
578
74.7k
            memcpy(&pfont1->font_name, &fpriv.gsu.gst1.font_name, sizeof(pfont1->font_name));
579
74.7k
            if (fpriv.gsu.gst1.UID.id != 0)
580
74.7k
                memcpy(&pfont1->UID, &fpriv.gsu.gst1.UID, sizeof(pfont1->UID));
581
74.7k
            fpriv.gsu.gst1.UID.xvalues = NULL; /* In case of error */
582
74.7k
            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
74.7k
            else
588
74.7k
                pfont1->WMode = 0;
589
74.7k
            pfont1->PaintType = fpriv.gsu.gst1.PaintType;
590
74.7k
            pfont1->StrokeWidth = fpriv.gsu.gst1.StrokeWidth;
591
592
74.7k
            if (font_dict != NULL) {
593
12.8k
                t1f->object_num = font_dict->object_num;
594
12.8k
                t1f->generation_num = font_dict->generation_num;
595
12.8k
                t1f->indirect_num = font_dict->indirect_num;
596
12.8k
                t1f->indirect_gen = font_dict->indirect_gen;
597
12.8k
            }
598
599
74.7k
            t1f->PDF_font = font_dict;
600
74.7k
            pdfi_countup(font_dict);
601
74.7k
            t1f->FontDescriptor = (pdf_dict *) fontdesc;
602
74.7k
            pdfi_countup(fontdesc);
603
74.7k
            t1f->Name = mapname;
604
74.7k
            pdfi_countup(mapname);
605
606
            /* We want basefont, but we can live without it */
607
74.7k
            if (font_dict != NULL) {
608
12.8k
                (void)pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, &basefont);
609
12.8k
                t1f->BaseFont = basefont;
610
12.8k
                pdfi_countup(basefont);
611
12.8k
            }
612
613
74.7k
            if (t1f->FontDescriptor != NULL) {
614
12.8k
                code = pdfi_dict_get_int(ctx, t1f->FontDescriptor, "Flags", &t1f->descflags);
615
12.8k
                if (code >= 0) {
616
                    /* If both the symbolic and non-symbolic flag are set,
617
                       believe that latter.
618
                     */
619
12.8k
                    if ((t1f->descflags & 32) != 0)
620
3.11k
                        t1f->descflags = (t1f->descflags & ~4);
621
12.8k
                }
622
12.8k
            }
623
624
74.7k
            if (pdfi_font_known_symbolic(basefont)) {
625
46
                force_symbolic = true;
626
46
                t1f->descflags |= 4;
627
46
            }
628
629
74.7k
            if (ctx->args.ignoretounicode != true && font_dict != NULL) {
630
12.8k
                code = pdfi_dict_get(ctx, font_dict, "ToUnicode", (pdf_obj **)&tounicode);
631
12.8k
                if (code >= 0 && pdfi_type_of(tounicode) == PDF_STREAM) {
632
1.00k
                    pdf_cmap *tu = NULL;
633
1.00k
                    code = pdfi_read_cmap(ctx, tounicode, &tu);
634
1.00k
                    pdfi_countdown(tounicode);
635
1.00k
                    tounicode = (pdf_obj *)tu;
636
1.00k
                }
637
12.8k
                if (code < 0 || (tounicode != NULL && pdfi_type_of(tounicode) != PDF_CMAP)) {
638
11.8k
                    pdfi_countdown(tounicode);
639
11.8k
                    tounicode = NULL;
640
11.8k
                    code = 0;
641
11.8k
                }
642
12.8k
            }
643
61.8k
            else {
644
61.8k
                tounicode = NULL;
645
61.8k
            }
646
74.7k
            t1f->ToUnicode = tounicode;
647
74.7k
            tounicode = NULL;
648
649
74.7k
            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
74.7k
            x_scale = 0.001 / hypot(pfont1->FontMatrix.xx, pfont1->FontMatrix.xy);
656
657
            /* ignore errors with widths... for now */
658
74.7k
            if (font_dict != NULL)
659
12.8k
                (void)pdfi_font_create_widths(ctx, font_dict, (pdf_font*)t1f, x_scale);
660
661
74.7k
            if (font_dict != NULL)
662
12.8k
                code = pdfi_dict_knownget(ctx, font_dict, "Encoding", &tmp);
663
61.8k
            else
664
61.8k
                code = gs_error_undefined;
665
74.7k
            if (code == 1) {
666
10.8k
                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
10.8k
                else if (pdfi_type_of(tmp) == PDF_DICT && (t1f->descflags & 4) != 0) {
671
8.20k
                    code = pdfi_create_Encoding(ctx, (pdf_font *)t1f, tmp, (pdf_obj *)fpriv.u.t1.Encoding, (pdf_obj **) & t1f->Encoding);
672
8.20k
                    if (code >= 0)
673
8.20k
                        code = 1;
674
8.20k
                }
675
2.60k
                else {
676
2.60k
                    code = pdfi_create_Encoding(ctx, (pdf_font *)t1f, tmp, NULL, (pdf_obj **) & t1f->Encoding);
677
2.60k
                    if (code >= 0)
678
2.56k
                        code = 1;
679
2.60k
                }
680
10.8k
                pdfi_countdown(tmp);
681
10.8k
                tmp = NULL;
682
10.8k
            }
683
63.9k
            else {
684
63.9k
                pdfi_countdown(tmp);
685
63.9k
                tmp = NULL;
686
63.9k
                code = 0;
687
63.9k
            }
688
689
74.7k
            if (code <= 0) {
690
63.9k
                t1f->Encoding = fpriv.u.t1.Encoding;
691
63.9k
                pdfi_countup(t1f->Encoding);
692
63.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
74.7k
            if (uid_is_XUID(&t1f->pfont->UID))
700
74.7k
                uid_free(&t1f->pfont->UID, t1f->pfont->memory, "pdfi_read_type1_font");
701
74.7k
            uid_set_invalid(&t1f->pfont->UID);
702
703
74.7k
            t1f->CharStrings = fpriv.u.t1.CharStrings;
704
74.7k
            pdfi_countup(t1f->CharStrings);
705
74.7k
            pdfi_patch_charstrings_dict(t1f->CharStrings);
706
707
74.7k
            t1f->Subrs = fpriv.u.t1.Subrs;
708
74.7k
            fpriv.u.t1.Subrs = NULL;
709
710
74.7k
            t1f->copyright = fpriv.u.t1.copyright;
711
74.7k
            t1f->notice = fpriv.u.t1.notice;
712
74.7k
            t1f->fullname = fpriv.u.t1.fullname;
713
74.7k
            t1f->familyname = fpriv.u.t1.familyname;
714
74.7k
            fpriv.u.t1.copyright = fpriv.u.t1.notice = fpriv.u.t1.fullname = fpriv.u.t1.familyname = NULL;
715
716
74.7k
            code = pdfi_font_generate_pseudo_XUID(ctx, font_dict, t1f->pfont);
717
74.7k
            if (code < 0) {
718
0
                goto error;
719
0
            }
720
721
74.7k
            t1f->blenddesignpositions = fpriv.u.t1.blenddesignpositions;
722
74.7k
            pdfi_countup(t1f->blenddesignpositions);
723
74.7k
            t1f->blenddesignmap = fpriv.u.t1.blenddesignmap;
724
74.7k
            pdfi_countup(t1f->blenddesignmap);
725
74.7k
            t1f->blendfontbbox = fpriv.u.t1.blendfontbbox;
726
74.7k
            pdfi_countup(t1f->blendfontbbox);
727
74.7k
            t1f->blendaxistypes = fpriv.u.t1.blendaxistypes;
728
74.7k
            pdfi_countup(t1f->blendaxistypes);
729
730
74.7k
            key_known = false;
731
74.7k
            if (t1f->FontDescriptor != NULL) {
732
12.8k
                 code = pdfi_dict_known(ctx, t1f->FontDescriptor, "FontFile", &key_known);
733
12.8k
                 if (code < 0 || key_known == false) {
734
520
                     code = pdfi_dict_known(ctx, t1f->FontDescriptor, "FontFile2", &key_known);
735
520
                     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
520
                 }
742
12.8k
            }
743
74.7k
            t1f->pfont->is_resource = (key_known == false);
744
745
74.7k
            pdfi_font_set_orig_fonttype(ctx, (pdf_font *)t1f);
746
74.7k
            code = gs_definefont(ctx->font_dir, (gs_font *) t1f->pfont);
747
74.7k
            if (code < 0) {
748
0
                goto error;
749
0
            }
750
751
74.7k
            code = pdfi_fapi_passfont((pdf_font *) t1f, 0, NULL, NULL, NULL, 0);
752
74.7k
            if (code < 0) {
753
0
                goto error;
754
0
            }
755
            /* object_num can be zero if the dictionary was defined inline */
756
74.7k
            if (t1f->object_num != 0) {
757
12.3k
                (void)replace_cache_entry(ctx, (pdf_obj *) t1f);
758
12.3k
            }
759
74.7k
            *ppdffont = (pdf_font *) t1f;
760
74.7k
        }
761
74.7k
    }
762
763
82.3k
  error:
764
82.3k
    pdfi_countdown(fontdesc);
765
82.3k
    pdfi_countdown(basefont);
766
82.3k
    pdfi_countdown(tounicode);
767
82.3k
    pdfi_countdown(mapname);
768
82.3k
    pdfi_countdown(tmp);
769
82.3k
    pdfi_countdown(fpriv.u.t1.Encoding);
770
82.3k
    pdfi_countdown(fpriv.u.t1.CharStrings);
771
82.3k
    pdfi_countdown(fpriv.u.t1.blenddesignpositions);
772
82.3k
    pdfi_countdown(fpriv.u.t1.blenddesignmap);
773
82.3k
    pdfi_countdown(fpriv.u.t1.blendfontbbox);
774
82.3k
    pdfi_countdown(fpriv.u.t1.blendaxistypes);
775
82.3k
    pdfi_countdown(fpriv.u.t1.Subrs);
776
82.3k
    pdfi_countdown(fpriv.u.t1.copyright);
777
82.3k
    pdfi_countdown(fpriv.u.t1.notice);
778
82.3k
    pdfi_countdown(fpriv.u.t1.fullname);
779
82.3k
    pdfi_countdown(fpriv.u.t1.familyname);
780
82.3k
    if (fpriv.gsu.gst1.UID.xvalues != NULL) {
781
1
        gs_free_object(ctx->memory, fpriv.gsu.gst1.UID.xvalues, "pdfi_read_type1_font(xuid)");
782
1
    }
783
784
82.3k
    if (code < 0) {
785
7.63k
        tmp = NULL;
786
7.63k
        if (font_dict != NULL) {
787
7.63k
            if (pdfi_dict_get(ctx, font_dict, ".Path", &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
7.63k
            else {
798
7.63k
                (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
7.63k
            }
800
7.63k
        }
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
7.63k
        pdfi_countdown(tmp);
805
7.63k
        pdfi_countdown(t1f);
806
7.63k
    }
807
82.3k
    return code;
808
82.3k
}
809
810
int
811
pdfi_copy_type1_font(pdf_context *ctx, pdf_font *spdffont, pdf_dict *font_dict, pdf_font **tpdffont)
812
1.01M
{
813
1.01M
    int code = 0;
814
1.01M
    pdf_font_type1 *font = NULL;
815
1.01M
    gs_font_type1 *spfont1 = (gs_font_type1 *) spdffont->pfont;
816
1.01M
    gs_font_type1 *dpfont1;
817
1.01M
    gs_id t_id;
818
1.01M
    pdf_obj *tmp;
819
1.01M
    bool force_symbolic = false;
820
821
1.01M
    if (font_dict == NULL)
822
0
        return_error(gs_error_invalidfont);
823
824
1.01M
    code = pdfi_alloc_t1_font(ctx, &font, font_dict->object_num);
825
1.01M
    if (code < 0)
826
0
        return code;
827
1.01M
    dpfont1 = (gs_font_type1 *) font->pfont;
828
829
1.01M
    t_id = dpfont1->id;
830
1.01M
    memcpy(dpfont1, spfont1, sizeof(gs_font_type1));
831
1.01M
    dpfont1->id = t_id;
832
1.01M
    dpfont1->FAPI = NULL;
833
1.01M
    dpfont1->FAPI_font_data = NULL;
834
835
1.01M
    memcpy(font, spdffont, sizeof(pdf_font_type1));
836
1.01M
    font->pfont = (gs_font_base *)dpfont1;
837
1.01M
    font->refcnt = 1;
838
1.01M
    dpfont1->client_data = (void *)font;
839
1.01M
    font->filename = NULL;
840
841
1.01M
    dpfont1->notify_list.memory = NULL;
842
1.01M
    dpfont1->notify_list.first = NULL;
843
1.01M
    gs_notify_init(&dpfont1->notify_list, dpfont1->memory);
844
845
1.01M
    font->PDF_font = font_dict;
846
1.01M
    font->object_num = font_dict->object_num;
847
1.01M
    font->generation_num = font_dict->generation_num;
848
1.01M
    pdfi_countup(font->PDF_font);
849
850
    /* We want basefont and descriptor, but we can live without them */
851
1.01M
    font->BaseFont = NULL;
852
1.01M
    code = pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, &font->BaseFont);
853
1.01M
    if (code < 0) {
854
344
        pdfi_countdown(font->BaseFont);
855
344
        font->BaseFont = NULL;
856
344
    }
857
1.01M
    font->FontDescriptor = NULL;
858
1.01M
    code = pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, (pdf_obj **)&font->FontDescriptor);
859
1.01M
    if (code < 0) {
860
7.10k
        pdfi_countdown(font->FontDescriptor);
861
7.10k
        font->FontDescriptor = NULL;
862
7.10k
    }
863
864
1.01M
    pdfi_countup(font->Name);
865
1.01M
    pdfi_countup(font->CharStrings);
866
1.01M
    pdfi_countup(font->blenddesignpositions);
867
1.01M
    pdfi_countup(font->blenddesignmap);
868
1.01M
    pdfi_countup(font->blendfontbbox);
869
1.01M
    pdfi_countup(font->blendaxistypes);
870
1.01M
    pdfi_countup(font->Subrs);
871
1.01M
    pdfi_countup(font->copyright);
872
1.01M
    pdfi_countup(font->notice);
873
1.01M
    pdfi_countup(font->fullname);
874
1.01M
    pdfi_countup(font->familyname);
875
876
1.01M
    if (font->BaseFont != NULL && ((pdf_name *)font->BaseFont)->length <= gs_font_name_max - 1) {
877
1.01M
        memcpy(dpfont1->key_name.chars, ((pdf_name *)font->BaseFont)->data, ((pdf_name *)font->BaseFont)->length);
878
1.01M
        dpfont1->key_name.size = ((pdf_name *)font->BaseFont)->length;
879
1.01M
        dpfont1->key_name.chars[dpfont1->key_name.size] = '\0';
880
1.01M
        memcpy(dpfont1->font_name.chars, ((pdf_name *)font->BaseFont)->data, ((pdf_name *)font->BaseFont)->length);
881
1.01M
        dpfont1->font_name.size = ((pdf_name *)font->BaseFont)->length;
882
1.01M
        dpfont1->font_name.chars[dpfont1->font_name.size] = '\0';
883
1.01M
    }
884
885
1.01M
    font->Encoding = NULL;
886
1.01M
    font->ToUnicode = NULL;
887
1.01M
    font->Widths = NULL;
888
889
1.01M
    pdfi_font_set_first_last_char(ctx, font_dict, (pdf_font *)font);
890
1.01M
    (void)pdfi_font_create_widths(ctx, font_dict, (pdf_font *)font, (double)(0.001 / hypot(dpfont1->FontMatrix.xx, dpfont1->FontMatrix.xy)));
891
892
1.01M
    font->descflags = 0;
893
1.01M
    if (font->FontDescriptor != NULL) {
894
40.1k
        code = pdfi_dict_get_int(ctx, font->FontDescriptor, "Flags", &font->descflags);
895
40.1k
        if (code >= 0) {
896
            /* If both the symbolic and non-symbolic flag are set,
897
               believe that latter.
898
             */
899
39.8k
            if ((font->descflags & 32) != 0)
900
28.7k
                font->descflags = (font->descflags & ~4);
901
39.8k
        }
902
40.1k
    }
903
904
1.01M
    if (pdfi_font_known_symbolic(font->BaseFont)) {
905
2.64k
        force_symbolic = true;
906
2.64k
        font->descflags |= 4;
907
2.64k
    }
908
909
1.01M
    tmp = NULL;
910
1.01M
    code = pdfi_dict_knownget(ctx, font_dict, "Encoding", &tmp);
911
1.01M
    if (code == 1) {
912
54.2k
        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
54.2k
        else if (pdfi_type_of(tmp) == PDF_DICT && (font->descflags & 4) != 0) {
917
5.09k
            code = pdfi_create_Encoding(ctx, (pdf_font *)font, tmp, (pdf_obj *)spdffont->Encoding, (pdf_obj **) &font->Encoding);
918
5.09k
            if (code >= 0)
919
5.09k
                code = 1;
920
5.09k
        }
921
49.1k
        else {
922
49.1k
            code = pdfi_create_Encoding(ctx, (pdf_font *)font, tmp, NULL, (pdf_obj **) & font->Encoding);
923
49.1k
            if (code >= 0)
924
48.2k
                code = 1;
925
49.1k
        }
926
54.2k
        pdfi_countdown(tmp);
927
54.2k
        tmp = NULL;
928
54.2k
    }
929
957k
    else {
930
957k
        pdfi_countdown(tmp);
931
957k
        tmp = NULL;
932
957k
        code = 0;
933
957k
    }
934
935
1.01M
    if (code <= 0) {
936
957k
        font->Encoding = spdffont->Encoding;
937
957k
        pdfi_countup(font->Encoding);
938
957k
    }
939
940
1.01M
    code = uid_copy(&font->pfont->UID, font->pfont->memory, "pdfi_copy_type1_font");
941
1.01M
    if (code < 0) {
942
0
        uid_set_invalid(&font->pfont->UID);
943
0
    }
944
1.01M
    if (spdffont->filename == NULL) {
945
165
        code = pdfi_font_generate_pseudo_XUID(ctx, font_dict, font->pfont);
946
165
        if (code < 0) {
947
0
            goto error;
948
0
        }
949
165
    }
950
951
1.01M
    if (ctx->args.ignoretounicode != true) {
952
1.01M
        code = pdfi_dict_get(ctx, font_dict, "ToUnicode", (pdf_obj **)&tmp);
953
1.01M
        if (code >= 0 && pdfi_type_of(tmp) == PDF_STREAM) {
954
11.2k
            pdf_cmap *tu = NULL;
955
11.2k
            code = pdfi_read_cmap(ctx, tmp, &tu);
956
11.2k
            pdfi_countdown(tmp);
957
11.2k
            tmp = (pdf_obj *)tu;
958
11.2k
        }
959
1.01M
        if (code < 0 || (tmp != NULL && pdfi_type_of(tmp) != PDF_CMAP)) {
960
1.00M
            pdfi_countdown(tmp);
961
1.00M
            tmp = NULL;
962
1.00M
            code = 0;
963
1.00M
        }
964
1.01M
    }
965
0
    else {
966
0
        tmp = NULL;
967
0
    }
968
1.01M
    font->ToUnicode = tmp;
969
970
1.01M
    pdfi_font_set_orig_fonttype(ctx, (pdf_font *)font);
971
1.01M
    code = gs_definefont(ctx->font_dir, (gs_font *) font->pfont);
972
1.01M
    if (code < 0) {
973
0
        goto error;
974
0
    }
975
976
1.01M
    code = pdfi_fapi_passfont((pdf_font *) font, 0, NULL, NULL, NULL, 0);
977
1.01M
    if (code < 0) {
978
0
        goto error;
979
0
    }
980
    /* object_num can be zero if the dictionary was defined inline */
981
1.01M
    if (font->object_num != 0) {
982
64.7k
        (void)replace_cache_entry(ctx, (pdf_obj *) font);
983
64.7k
    }
984
985
1.01M
    *tpdffont = (pdf_font *)font;
986
987
1.01M
error:
988
1.01M
    if (code < 0)
989
0
        pdfi_countdown(font);
990
991
1.01M
    return code;
992
1.01M
}
993
994
int
995
pdfi_free_font_type1(pdf_obj *font)
996
1.08M
{
997
1.08M
    pdf_font_type1 *t1f = (pdf_font_type1 *) font;
998
999
1.08M
    gs_free_object(OBJ_MEMORY(font), t1f->pfont, "Free Type 1 gs_font");
1000
1001
1.08M
    pdfi_countdown(t1f->PDF_font);
1002
1.08M
    pdfi_countdown(t1f->BaseFont);
1003
1.08M
    pdfi_countdown(t1f->FontDescriptor);
1004
1.08M
    pdfi_countdown(t1f->Name);
1005
1.08M
    pdfi_countdown(t1f->Encoding);
1006
1.08M
    pdfi_countdown(t1f->ToUnicode);
1007
1.08M
    pdfi_countdown(t1f->CharStrings);
1008
1.08M
    pdfi_countdown(t1f->blenddesignpositions);
1009
1.08M
    pdfi_countdown(t1f->blenddesignmap);
1010
1.08M
    pdfi_countdown(t1f->blendfontbbox);
1011
1.08M
    pdfi_countdown(t1f->blendaxistypes);
1012
1.08M
    pdfi_countdown(t1f->Subrs);
1013
1.08M
    pdfi_countdown(t1f->filename);
1014
1.08M
    pdfi_countdown(t1f->copyright);
1015
1.08M
    pdfi_countdown(t1f->notice);
1016
1.08M
    pdfi_countdown(t1f->fullname);
1017
1.08M
    pdfi_countdown(t1f->familyname);
1018
1019
1.08M
    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
1.08M
    return 0;
1022
1.08M
}