Coverage Report

Created: 2022-10-31 07:00

/src/ghostpdl/pdf/pdf_font1.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2019-2022 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.,  1305 Grant Avenue - Suite 200, Novato,
13
   CA 94945, U.S.A., +1(415)492-9861, 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
23.6k
{
67
23.6k
    int code = 0;
68
23.6k
    pdf_obj *o;
69
23.6k
    const pdfi_t1_glyph_name_equivalents_t *gne = pdfi_t1_glyph_name_equivalents;
70
118k
    while(gne->name != NULL && code >= 0) {
71
94.4k
        code = pdfi_dict_get(cstrings->ctx, cstrings, gne->name, &o);
72
94.4k
        if (code >= 0) {
73
74.0k
            code = pdfi_dict_put(cstrings->ctx, cstrings, gne->altname, o);
74
74.0k
            pdfi_countdown(o);
75
74.0k
        }
76
94.4k
        if (code == gs_error_undefined)
77
20.4k
            code = 0;
78
94.4k
        gne++;
79
94.4k
    }
80
81
23.6k
    if (code >= 0) {
82
23.6k
        bool key_known;
83
23.6k
        pdf_string *pstr;
84
23.6k
        byte notdefstr[] = { 0x9E, 0x35, 0xCE, 0xD7, 0xFF, 0xD3, 0x62, 0x2F, 0x09 };
85
86
23.6k
        code = pdfi_dict_known(cstrings->ctx, cstrings, ".notdef", &key_known);
87
23.6k
        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
482
            code = pdfi_object_alloc(cstrings->ctx, PDF_STRING, sizeof(notdefstr), (pdf_obj **) &pstr);
92
482
            if (code >= 0) {
93
482
                memcpy(pstr->data, notdefstr, sizeof(notdefstr));
94
482
                (void)pdfi_dict_put(cstrings->ctx, cstrings, ".notdef", (pdf_obj *) pstr);
95
482
            }
96
482
        }
97
23.6k
    }
98
23.6k
}
99
100
/* CALLBACKS */
101
static int
102
pdfi_t1_glyph_data(gs_font_type1 *pfont, gs_glyph glyph, gs_glyph_data_t *pgd)
103
28.6M
{
104
28.6M
    int code = 0;
105
28.6M
    pdf_font_type1 *pdffont1 = (pdf_font_type1 *) pfont->client_data;
106
28.6M
    pdf_context *ctx = (pdf_context *) pdffont1->ctx;
107
28.6M
    pdf_name *glyphname = NULL;
108
28.6M
    pdf_string *charstring = NULL;
109
28.6M
    gs_const_string gname;
110
111
28.6M
    code = (*ctx->get_glyph_name)((gs_font *)pfont, glyph, &gname);
112
28.6M
    if (code >= 0) {
113
28.6M
        code = pdfi_name_alloc(ctx, (byte *) gname.data, gname.size, (pdf_obj **)&glyphname);
114
28.6M
        if (code >= 0)
115
28.6M
            pdfi_countup(glyphname);
116
28.6M
    }
117
118
28.6M
    if (code >= 0) {
119
28.6M
        code = pdfi_dict_get_by_key(ctx, pdffont1->CharStrings, glyphname, (pdf_obj **)&charstring);
120
28.6M
        if (code < 0) {
121
101k
            code = pdfi_map_glyph_name_via_agl(pdffont1->CharStrings, glyphname, &charstring);
122
101k
        }
123
28.6M
        if (code >= 0)
124
28.5M
            gs_glyph_data_from_bytes(pgd, charstring->data, 0, charstring->length, NULL);
125
28.6M
    }
126
28.6M
    pdfi_countdown(charstring);
127
28.6M
    pdfi_countdown(glyphname);
128
129
28.6M
    return code;
130
28.6M
}
131
132
static int
133
pdfi_t1_subr_data(gs_font_type1 *pfont, int index, bool global, gs_glyph_data_t *pgd)
134
14.1M
{
135
14.1M
    int code = 0;
136
14.1M
    pdf_font_type1 *pdffont1 = (pdf_font_type1 *) pfont->client_data;
137
138
14.1M
    if (global == true || index < 0 || index >= (pdffont1->Subrs == NULL ? 0 : pdfi_array_size(pdffont1->Subrs))) {
139
92.3k
        code = gs_note_error(gs_error_rangecheck);
140
92.3k
    }
141
14.0M
    else {
142
14.0M
        pdf_string *subr_str = NULL;
143
14.0M
        code = pdfi_array_get_type(pdffont1->ctx, pdffont1->Subrs, index, PDF_STRING, (pdf_obj **)&subr_str);
144
14.0M
        if (code >= 0) {
145
14.0M
            gs_glyph_data_from_bytes(pgd, subr_str->data, 0, subr_str->length, NULL);
146
14.0M
        }
147
        /* decrementing is safe here, because the reference in the pdffont1->Subrs will persist */
148
14.0M
        pdfi_countdown(subr_str);
149
14.0M
    }
150
14.1M
    return code;
151
14.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
1.01k
{
156
1.01k
    int code = 0;
157
1.01k
    pdf_font_type1 *pdffont1 = (pdf_font_type1 *) pfont->client_data;
158
1.01k
    pdf_context *ctx = (pdf_context *) pdffont1->ctx;
159
1.01k
    gs_glyph glyph = gs_c_known_encode((gs_char)ccode, ENCODING_INDEX_STANDARD);
160
161
1.01k
    if (glyph == GS_NO_GLYPH)
162
5
        return_error(gs_error_rangecheck);
163
164
1.00k
    code = gs_c_glyph_name(glyph, gstr);
165
1.00k
    if (code >= 0) {
166
1.00k
        unsigned int nindex;
167
1.00k
        code = (*ctx->get_glyph_index)((gs_font *)pfont, (byte *)gstr->data, gstr->size, &nindex);
168
1.00k
        if (pglyph != NULL)
169
1.00k
            *pglyph = (gs_glyph)nindex;
170
1.00k
    }
171
172
1.00k
    if (code >= 0) {
173
1.00k
        pdf_name *glyphname = NULL;
174
1.00k
        pdf_string *charstring = NULL;
175
1.00k
        code = pdfi_name_alloc(ctx, (byte *) gstr->data, gstr->size, (pdf_obj **) &glyphname);
176
1.00k
        if (code >= 0) {
177
1.00k
            pdfi_countup(glyphname);
178
1.00k
            code = pdfi_dict_get_by_key(ctx, pdffont1->CharStrings, glyphname, (pdf_obj **)&charstring);
179
1.00k
            pdfi_countdown(glyphname);
180
1.00k
            if (code >= 0) {
181
1.00k
                if (pgd != NULL) {
182
0
                    gs_glyph_data_from_bytes(pgd, charstring->data, 0, charstring->length, NULL);
183
0
                }
184
1.00k
                pdfi_countdown(charstring);
185
1.00k
            }
186
1.00k
        }
187
1.00k
    }
188
189
1.00k
    return code;
190
1.01k
}
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
35.1M
{
213
35.1M
    int code;
214
35.1M
    pdf_font_type1 *t1font = (pdf_font_type1 *) pfont->client_data;
215
35.1M
    pdf_context *ctx = (pdf_context *) t1font->ctx;
216
35.1M
    pdf_name *key;
217
35.1M
    uint64_t i = (uint64_t) *pindex;
218
219
35.1M
    (void)glyph_space;
220
221
35.1M
    if (*pindex <= 0)
222
171k
        code = pdfi_dict_key_first(ctx, t1font->CharStrings, (pdf_obj **) & key, &i);
223
34.9M
    else
224
34.9M
        code = pdfi_dict_key_next(ctx, t1font->CharStrings, (pdf_obj **) & key, &i);
225
35.1M
    if (code < 0) {
226
39.2k
        *pindex = 0;
227
39.2k
        code = 0;
228
39.2k
    }
229
35.1M
    else {
230
35.1M
        uint dummy = GS_NO_GLYPH;
231
232
35.1M
        code = (*ctx->get_glyph_index)(pfont, key->data, key->length, &dummy);
233
35.1M
        if (code < 0) {
234
0
            *pglyph = (gs_glyph) *pindex;
235
0
            goto exit;
236
0
        }
237
35.1M
        *pglyph = dummy;
238
35.1M
        if (*pglyph == GS_NO_GLYPH)
239
0
            *pglyph = (gs_glyph) *pindex;
240
35.1M
        *pindex = (int)i;
241
35.1M
    }
242
35.1M
  exit:
243
35.1M
    pdfi_countdown(key);
244
35.1M
    return code;
245
35.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
0
{
253
0
    *pglyph = gs_c_name_glyph(gstr->data, gstr->size);
254
0
    return 0;
255
0
}
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
261k
{
261
261k
    gs_glyph_data_t gd;
262
261k
    gs_glyph_data_t *pgd = &gd;
263
261k
    gs_font_type1 *pfont1 = (gs_font_type1 *) pfont;
264
261k
    int code = pdfi_t1_glyph_data(pfont1, glyph, pgd);
265
266
261k
    if (code >= 0) {
267
259k
        gs_type1_state cis = { 0 };
268
259k
        gs_type1_state *pcis = &cis;
269
259k
        gs_gstate gs;
270
259k
        int value;
271
272
259k
        if (pmat)
273
241k
            gs_matrix_fixed_from_matrix(&gs.ctm, pmat);
274
17.2k
        else {
275
17.2k
            gs_matrix imat;
276
277
17.2k
            gs_make_identity(&imat);
278
17.2k
            gs_matrix_fixed_from_matrix(&gs.ctm, &imat);
279
17.2k
        }
280
259k
        gs.flatness = 0;
281
259k
        code = gs_type1_interp_init(pcis, &gs, ppath, NULL, NULL, true, 0, pfont1);
282
259k
        if (code < 0)
283
0
            return code;
284
285
259k
        pcis->no_grid_fitting = true;
286
259k
        gs_type1_set_callback_data(pcis, NULL);
287
        /* Continue interpreting. */
288
518k
      icont:
289
518k
        code = pfont1->data.interpret(pcis, pgd, &value);
290
518k
        switch (code) {
291
259k
            case 0:            /* all done */
292
                /* falls through */
293
259k
            default:           /* code < 0, error */
294
259k
                return code;
295
0
            case type1_result_callothersubr:   /* unknown OtherSubr */
296
0
                return_error(gs_error_rangecheck);      /* can't handle it */
297
259k
            case type1_result_sbw:     /* [h]sbw, just continue */
298
259k
                type1_cis_get_metrics(pcis, sbw);
299
259k
                pgd = 0;
300
259k
                goto icont;
301
518k
        }
302
518k
    }
303
2.22k
    return code;
304
261k
}
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
20.4M
{
309
20.4M
    if ((members & GLYPH_INFO_OUTLINE_WIDTHS) == 0)
310
20.1M
        return gs_type1_glyph_info(font, glyph, pmat, members, info);
311
312
243k
    return gs_default_glyph_info(font, glyph, pmat, members, info);
313
20.4M
}
314
315
/* END CALLBACKS */
316
317
static stream *
318
push_pfb_filter(gs_memory_t *mem, byte *buf, byte *bufend)
319
50
{
320
50
    stream *fs, *ffs = NULL;
321
50
    stream *sstrm;
322
50
    stream_PFBD_state *st;
323
50
    byte *strbuf;
324
325
50
    sstrm = file_alloc_stream(mem, "push_pfb_filter(buf stream)");
326
50
    if (sstrm == NULL)
327
0
        return NULL;
328
329
50
    sread_string(sstrm, buf, bufend - buf);
330
50
    sstrm->close_at_eod = false;
331
332
50
    fs = s_alloc(mem, "push_pfb_filter(fs)");
333
50
    strbuf = gs_alloc_bytes(mem, 4096, "push_pfb_filter(buf)");
334
50
    st = gs_alloc_struct(mem, stream_PFBD_state, s_PFBD_template.stype, "push_pfb_filter(st)");
335
50
    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
50
    memset(st, 0x00, sizeof(stream_PFBD_state));
343
50
    (*s_PFBD_template.init)((stream_state *)st);
344
50
    st->binary_to_hex = true;
345
50
    s_std_init(fs, strbuf, 4096, &s_filter_read_procs, s_mode_read);
346
50
    st->memory = mem;
347
50
    st->templat = &s_PFBD_template;
348
50
    fs->state = (stream_state *) st;
349
50
    fs->procs.process = s_PFBD_template.process;
350
50
    fs->strm = sstrm;
351
50
    fs->close_at_eod = false;
352
50
    ffs = fs;
353
50
  done:
354
50
    return ffs;
355
50
}
356
357
static void
358
pop_pfb_filter(gs_memory_t *mem, stream *s)
359
50
{
360
50
    stream *src = s->strm;
361
50
    byte *b = s->cbuf;
362
363
50
    sclose(s);
364
50
    gs_free_object(mem, s, "push_pfb_filter(s)");
365
50
    gs_free_object(mem, b, "push_pfb_filter(b)");
366
50
    if (src)
367
50
        sclose(src);
368
50
    gs_free_object(mem, src, "push_pfb_filter(strm)");
369
50
}
370
371
static int
372
pdfi_t1_decode_pfb(pdf_context *ctx, byte *inbuf, int inlen, byte **outbuf, int *outlen)
373
25
{
374
25
    stream *strm;
375
25
    int c, code = 0;
376
25
    int decodelen = 0;
377
25
    byte *d, *decodebuf = NULL;
378
379
25
    *outbuf = NULL;
380
25
    *outlen = 0;
381
382
25
    strm = push_pfb_filter(ctx->memory, inbuf, inbuf + inlen);
383
25
    if (strm == NULL) {
384
0
        code = gs_note_error(gs_error_VMerror);
385
0
    }
386
25
    else {
387
1.14M
        while (1) {
388
1.14M
            c = sgetc(strm);
389
1.14M
            if (c < 0)
390
25
                break;
391
1.14M
            decodelen++;
392
1.14M
        }
393
25
        pop_pfb_filter(ctx->memory, strm);
394
25
        decodebuf = gs_alloc_bytes(ctx->memory, decodelen, "pdfi_t1_decode_pfb(decodebuf)");
395
25
        if (decodebuf == NULL) {
396
0
            code = gs_note_error(gs_error_VMerror);
397
0
        }
398
25
        else {
399
25
            d = decodebuf;
400
25
            strm = push_pfb_filter(ctx->memory, inbuf, inbuf + inlen);
401
1.14M
            while (1) {
402
1.14M
                c = sgetc(strm);
403
1.14M
                if (c < 0)
404
25
                    break;
405
1.14M
                *d = c;
406
1.14M
                d++;
407
1.14M
            }
408
25
            pop_pfb_filter(ctx->memory, strm);
409
25
            *outbuf = decodebuf;
410
25
            *outlen = decodelen;
411
25
        }
412
25
    }
413
25
    return code;
414
25
}
415
416
static int
417
pdfi_alloc_t1_font(pdf_context *ctx, pdf_font_type1 **font, uint32_t obj_num)
418
189k
{
419
189k
    pdf_font_type1 *t1font = NULL;
420
189k
    gs_font_type1 *pfont = NULL;
421
422
189k
    t1font = (pdf_font_type1 *) gs_alloc_bytes(ctx->memory, sizeof(pdf_font_type1), "pdfi (type 1 pdf_font)");
423
189k
    if (t1font == NULL)
424
0
        return_error(gs_error_VMerror);
425
426
189k
    memset(t1font, 0x00, sizeof(pdf_font_type1));
427
189k
    t1font->ctx = ctx;
428
189k
    t1font->type = PDF_FONT;
429
189k
    t1font->ctx = ctx;
430
189k
    t1font->pdfi_font_type = e_pdf_font_type1;
431
432
#if REFCNT_DEBUG
433
    t1font->UID = ctx->UID++;
434
    dmprintf2(ctx->memory,
435
              "Allocated object of type %c with UID %" PRIi64 "\n", t1font->type, t1font->UID);
436
#endif
437
438
189k
    pdfi_countup(t1font);
439
440
189k
    pfont = (gs_font_type1 *) gs_alloc_struct(ctx->memory, gs_font_type1, &st_gs_font_type1, "pdfi (Type 1 pfont)");
441
189k
    if (pfont == NULL) {
442
0
        pdfi_countdown(t1font);
443
0
        return_error(gs_error_VMerror);
444
0
    }
445
189k
    memset(pfont, 0x00, sizeof(gs_font_type1));
446
447
189k
    t1font->pfont = (gs_font_base *) pfont;
448
449
189k
    gs_make_identity(&pfont->orig_FontMatrix);
450
189k
    gs_make_identity(&pfont->FontMatrix);
451
189k
    pfont->next = pfont->prev = 0;
452
189k
    pfont->memory = ctx->memory;
453
189k
    pfont->dir = ctx->font_dir;
454
189k
    pfont->is_resource = false;
455
189k
    gs_notify_init(&pfont->notify_list, ctx->memory);
456
189k
    pfont->base = (gs_font *) t1font->pfont;
457
189k
    pfont->client_data = t1font;
458
189k
    pfont->WMode = 0;
459
189k
    pfont->PaintType = 0;
460
189k
    pfont->StrokeWidth = 0;
461
189k
    pfont->is_cached = 0;
462
189k
    pfont->FAPI = NULL;
463
189k
    pfont->FAPI_font_data = NULL;
464
189k
    pfont->procs.init_fstack = gs_default_init_fstack;
465
189k
    pfont->procs.next_char_glyph = gs_default_next_char_glyph;
466
189k
    pfont->FontType = ft_encrypted;
467
189k
    pfont->ExactSize = fbit_use_outlines;
468
189k
    pfont->InBetweenSize = fbit_use_outlines;
469
189k
    pfont->TransformedChar = fbit_use_outlines;
470
    /* We may want to do something clever with an XUID here */
471
189k
    pfont->id = gs_next_ids(ctx->memory, 1);
472
189k
    uid_set_UniqueID(&pfont->UID, pfont->id);
473
474
189k
    pfont->encoding_index = 1;          /****** WRONG ******/
475
189k
    pfont->nearest_encoding_index = 1;          /****** WRONG ******/
476
477
189k
    pfont->client_data = (void *)t1font;
478
479
189k
    *font = t1font;
480
189k
    return 0;
481
189k
}
482
483
static void
484
pdfi_t1_font_set_procs(pdf_context *ctx, pdf_font_type1 *font)
485
23.6k
{
486
23.6k
    gs_font_type1 *pfont = (gs_font_type1 *) font->pfont;
487
488
    /* The build_char proc will be filled in by FAPI -
489
       we won't worry about working without FAPI */
490
23.6k
    pfont->procs.build_char = NULL;
491
492
23.6k
    pfont->procs.encode_char = pdfi_encode_char;
493
23.6k
    pfont->procs.glyph_name = ctx->get_glyph_name;
494
23.6k
    pfont->procs.decode_glyph = pdfi_decode_glyph;
495
23.6k
    pfont->procs.define_font = gs_no_define_font;
496
23.6k
    pfont->procs.make_font = gs_no_make_font;
497
23.6k
    pfont->procs.font_info = gs_default_font_info;
498
23.6k
    pfont->procs.glyph_info = pdfi_t1_glyph_info;
499
23.6k
    pfont->procs.glyph_outline = pdfi_t1_glyph_outline;
500
23.6k
    pfont->procs.same_font = gs_default_same_font;
501
23.6k
    pfont->procs.enumerate_glyph = pdfi_t1_enumerate_glyph;
502
503
23.6k
    pfont->data.procs.glyph_data = pdfi_t1_glyph_data;
504
23.6k
    pfont->data.procs.subr_data = pdfi_t1_subr_data;
505
23.6k
    pfont->data.procs.seac_data = pdfi_t1_seac_data;
506
23.6k
    pfont->data.procs.push_values = pdfi_t1_push;
507
23.6k
    pfont->data.procs.pop_value = pdfi_t1_pop;
508
23.6k
    pfont->data.interpret = gs_type1_interpret;
509
23.6k
}
510
511
int
512
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)
513
25.0k
{
514
25.0k
    int code = 0;
515
25.0k
    double x_scale;
516
25.0k
    pdf_obj *fontdesc = NULL;
517
25.0k
    pdf_obj *basefont = NULL;
518
25.0k
    pdf_obj *mapname = NULL;
519
25.0k
    pdf_obj *tmp = NULL;
520
25.0k
    pdf_font_type1 *t1f = NULL;
521
25.0k
    pdf_obj *tounicode = NULL;
522
25.0k
    ps_font_interp_private fpriv = { 0 };
523
25.0k
    bool key_known;
524
525
25.0k
    if (font_dict != NULL)
526
4.67k
        (void)pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, &fontdesc);
527
528
25.0k
    if (fbuf[0] == 128 && fbuf[1] == 1) {
529
25
        byte *decodebuf = NULL;
530
25
        int decodelen;
531
532
25
        code = pdfi_t1_decode_pfb(ctx, fbuf, fbuflen, &decodebuf, &decodelen);
533
25
        gs_free_object(ctx->memory, fbuf, "pdfi_read_type1_font");
534
25
        if (code < 0) {
535
0
            gs_free_object(ctx->memory, decodebuf, "pdfi_read_type1_font");
536
0
        }
537
25
        fbuf = decodebuf;
538
25
        fbuflen = decodelen;
539
25
    }
540
541
25.0k
    if (code >= 0) {
542
25.0k
        fpriv.gsu.gst1.data.lenIV = 4;
543
25.0k
        code = pdfi_read_ps_font(ctx, font_dict, fbuf, fbuflen, &fpriv);
544
25.0k
        gs_free_object(ctx->memory, fbuf, "pdfi_read_type1_font");
545
546
        /* If we have a full CharStrings dictionary, we probably have enough to make a font */
547
25.0k
        if (code < 0 || fpriv.u.t1.CharStrings == NULL || pdfi_type_of(fpriv.u.t1.CharStrings) != PDF_DICT
548
25.0k
            || fpriv.u.t1.CharStrings->entries == 0) {
549
1.42k
                code = gs_note_error(gs_error_invalidfont);
550
1.42k
                goto error;
551
1.42k
        }
552
23.6k
        code = pdfi_alloc_t1_font(ctx, &t1f, font_dict != NULL ? font_dict->object_num : 0);
553
23.6k
        if (code >= 0) {
554
23.6k
            gs_font_type1 *pfont1 = (gs_font_type1 *) t1f->pfont;
555
556
23.6k
            memcpy(&pfont1->data, &fpriv.gsu.gst1.data, sizeof(pfont1->data));
557
558
23.6k
            pdfi_t1_font_set_procs(ctx, t1f);
559
560
23.6k
            memcpy(&pfont1->FontMatrix, &fpriv.gsu.gst1.FontMatrix, sizeof(pfont1->FontMatrix));
561
23.6k
            memcpy(&pfont1->orig_FontMatrix, &fpriv.gsu.gst1.orig_FontMatrix, sizeof(pfont1->orig_FontMatrix));
562
23.6k
            memcpy(&pfont1->FontBBox, &fpriv.gsu.gst1.FontBBox, sizeof(pfont1->FontBBox));
563
23.6k
            memcpy(&pfont1->key_name, &fpriv.gsu.gst1.key_name, sizeof(pfont1->key_name));
564
23.6k
            memcpy(&pfont1->font_name, &fpriv.gsu.gst1.font_name, sizeof(pfont1->font_name));
565
23.6k
            if (fpriv.gsu.gst1.UID.id != 0)
566
5.00k
                memcpy(&pfont1->UID, &fpriv.gsu.gst1.UID, sizeof(pfont1->UID));
567
23.6k
            fpriv.gsu.gst1.UID.xvalues = NULL; /* In case of error */
568
23.6k
            pfont1->WMode = fpriv.gsu.gst1.WMode;
569
23.6k
            pfont1->PaintType = fpriv.gsu.gst1.PaintType;
570
23.6k
            pfont1->StrokeWidth = fpriv.gsu.gst1.StrokeWidth;
571
572
23.6k
            if (font_dict != NULL) {
573
3.25k
                t1f->object_num = font_dict->object_num;
574
3.25k
                t1f->generation_num = font_dict->generation_num;
575
3.25k
                t1f->indirect_num = font_dict->indirect_num;
576
3.25k
                t1f->indirect_gen = font_dict->indirect_gen;
577
3.25k
            }
578
579
23.6k
            t1f->PDF_font = font_dict;
580
23.6k
            pdfi_countup(font_dict);
581
23.6k
            t1f->FontDescriptor = (pdf_dict *) fontdesc;
582
23.6k
            pdfi_countup(fontdesc);
583
23.6k
            t1f->Name = mapname;
584
23.6k
            pdfi_countup(mapname);
585
586
            /* We want basefont, but we can live without it */
587
23.6k
            if (font_dict != NULL) {
588
3.25k
                (void)pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, &basefont);
589
3.25k
                t1f->BaseFont = basefont;
590
3.25k
                pdfi_countup(basefont);
591
3.25k
            }
592
593
23.6k
            t1f->descflags = 0;
594
23.6k
            if (t1f->FontDescriptor != NULL) {
595
3.25k
                code = pdfi_dict_get_int(ctx, t1f->FontDescriptor, "Flags", &t1f->descflags);
596
3.25k
                if (code >= 0) {
597
                    /* If both the symbolic and non-symbolic flag are set,
598
                       believe that latter.
599
                     */
600
3.25k
                    if ((t1f->descflags & 32) != 0)
601
850
                        t1f->descflags = (t1f->descflags & ~4);
602
3.25k
                }
603
3.25k
            }
604
605
23.6k
            if (pdfi_font_known_symbolic(basefont)) {
606
12
                t1f->descflags |= 4;
607
12
            }
608
609
23.6k
            if (ctx->args.ignoretounicode != true && font_dict != NULL) {
610
3.25k
                code = pdfi_dict_get(ctx, font_dict, "ToUnicode", (pdf_obj **)&tounicode);
611
3.25k
                if (code >= 0 && pdfi_type_of(tounicode) == PDF_STREAM) {
612
306
                    pdf_cmap *tu = NULL;
613
306
                    code = pdfi_read_cmap(ctx, tounicode, &tu);
614
306
                    pdfi_countdown(tounicode);
615
306
                    tounicode = (pdf_obj *)tu;
616
306
                }
617
3.25k
                if (code < 0 || (tounicode != NULL && pdfi_type_of(tounicode) != PDF_CMAP)) {
618
2.95k
                    pdfi_countdown(tounicode);
619
2.95k
                    tounicode = NULL;
620
2.95k
                    code = 0;
621
2.95k
                }
622
3.25k
            }
623
20.3k
            else {
624
20.3k
                tounicode = NULL;
625
20.3k
            }
626
23.6k
            t1f->ToUnicode = tounicode;
627
23.6k
            tounicode = NULL;
628
629
23.6k
            pdfi_font_set_first_last_char(ctx, font_dict, (pdf_font *)t1f);
630
631
            /* Widths are defined assuming a 1000x1000 design grid, but we apply
632
             * them in font space - so undo the 1000x1000 scaling, and apply
633
             * the inverse of the font's x scaling
634
             */
635
23.6k
            x_scale = 0.001 / hypot(pfont1->FontMatrix.xx, pfont1->FontMatrix.xy);
636
637
            /* ignore errors with widths... for now */
638
23.6k
            if (font_dict != NULL)
639
3.25k
                (void)pdfi_font_create_widths(ctx, font_dict, (pdf_font*)t1f, x_scale);
640
641
23.6k
            if (font_dict != NULL)
642
3.25k
                code = pdfi_dict_knownget(ctx, font_dict, "Encoding", &tmp);
643
20.3k
            else
644
20.3k
                code = gs_error_undefined;
645
23.6k
            if (code == 1) {
646
2.91k
                if ((pdfi_type_of(tmp) == PDF_NAME || pdfi_type_of(tmp) == PDF_DICT) && (t1f->descflags & 4) == 0) {
647
826
                    code = pdfi_create_Encoding(ctx, tmp, NULL, (pdf_obj **) & t1f->Encoding);
648
826
                    if (code >= 0)
649
826
                        code = 1;
650
826
                }
651
2.09k
                else if (pdfi_type_of(tmp) == PDF_DICT && (t1f->descflags & 4) != 0) {
652
2.06k
                    code = pdfi_create_Encoding(ctx, tmp, (pdf_obj *)fpriv.u.t1.Encoding, (pdf_obj **) & t1f->Encoding);
653
2.06k
                    if (code >= 0)
654
2.06k
                        code = 1;
655
2.06k
                }
656
25
                else
657
25
                    code = gs_error_undefined;
658
2.91k
                pdfi_countdown(tmp);
659
2.91k
                tmp = NULL;
660
2.91k
                if (code == 1) {
661
2.89k
                }
662
2.91k
            }
663
20.7k
            else {
664
20.7k
                pdfi_countdown(tmp);
665
20.7k
                tmp = NULL;
666
20.7k
                code = 0;
667
20.7k
            }
668
669
23.6k
            if (code <= 0) {
670
20.7k
                t1f->Encoding = fpriv.u.t1.Encoding;
671
20.7k
                pdfi_countup(t1f->Encoding);
672
20.7k
            }
673
            /* Since the underlying font stream can be shared between font descriptors,
674
               and the font descriptors can be shared between font objects, if we change
675
               the encoding, we can't share cached glyphs with other instances of this
676
               underlying font, so invalidate the UniqueID/XUID so the glyph cache won't
677
               try.
678
            */
679
23.6k
            if (uid_is_XUID(&t1f->pfont->UID))
680
23.6k
                uid_free(&t1f->pfont->UID, t1f->pfont->memory, "pdfi_read_type1_font");
681
23.6k
            uid_set_invalid(&t1f->pfont->UID);
682
683
23.6k
            t1f->CharStrings = fpriv.u.t1.CharStrings;
684
23.6k
            pdfi_countup(t1f->CharStrings);
685
23.6k
            pdfi_patch_charstrings_dict(t1f->CharStrings);
686
687
23.6k
            t1f->Subrs = fpriv.u.t1.Subrs;
688
23.6k
            fpriv.u.t1.Subrs = NULL;
689
690
23.6k
            code = pdfi_font_generate_pseudo_XUID(ctx, font_dict, t1f->pfont);
691
23.6k
            if (code < 0) {
692
0
                goto error;
693
0
            }
694
695
23.6k
            t1f->blenddesignpositions = fpriv.u.t1.blenddesignpositions;
696
23.6k
            pdfi_countup(t1f->blenddesignpositions);
697
23.6k
            t1f->blenddesignmap = fpriv.u.t1.blenddesignmap;
698
23.6k
            pdfi_countup(t1f->blenddesignmap);
699
23.6k
            t1f->blendfontbbox = fpriv.u.t1.blendfontbbox;
700
23.6k
            pdfi_countup(t1f->blendfontbbox);
701
23.6k
            t1f->blendaxistypes = fpriv.u.t1.blendaxistypes;
702
23.6k
            pdfi_countup(t1f->blendaxistypes);
703
704
23.6k
            key_known = false;
705
23.6k
            if (t1f->FontDescriptor != NULL) {
706
3.25k
                 code = pdfi_dict_known(ctx, t1f->FontDescriptor, "FontFile", &key_known);
707
3.25k
                 if (code < 0 || key_known == false) {
708
0
                     code = pdfi_dict_known(ctx, t1f->FontDescriptor, "FontFile2", &key_known);
709
0
                     if (code < 0 || key_known == false) {
710
0
                         code = pdfi_dict_known(ctx, t1f->FontDescriptor, "FontFile3", &key_known);
711
0
                         if (code < 0) {
712
0
                             key_known = false;
713
0
                         }
714
0
                     }
715
0
                 }
716
3.25k
            }
717
23.6k
            t1f->pfont->is_resource = (key_known == false);
718
719
23.6k
            code = gs_definefont(ctx->font_dir, (gs_font *) t1f->pfont);
720
23.6k
            if (code < 0) {
721
0
                goto error;
722
0
            }
723
724
23.6k
            code = pdfi_fapi_passfont((pdf_font *) t1f, 0, NULL, NULL, NULL, 0);
725
23.6k
            if (code < 0) {
726
0
                goto error;
727
0
            }
728
            /* object_num can be zero if the dictionary was defined inline */
729
23.6k
            if (t1f->object_num != 0) {
730
3.25k
                (void)replace_cache_entry(ctx, (pdf_obj *) t1f);
731
3.25k
            }
732
23.6k
            *ppdffont = (pdf_font *) t1f;
733
23.6k
        }
734
23.6k
    }
735
736
25.0k
  error:
737
25.0k
    pdfi_countdown(fontdesc);
738
25.0k
    pdfi_countdown(basefont);
739
25.0k
    pdfi_countdown(tounicode);
740
25.0k
    pdfi_countdown(mapname);
741
25.0k
    pdfi_countdown(tmp);
742
25.0k
    pdfi_countdown(fpriv.u.t1.Encoding);
743
25.0k
    pdfi_countdown(fpriv.u.t1.CharStrings);
744
25.0k
    pdfi_countdown(fpriv.u.t1.blenddesignpositions);
745
25.0k
    pdfi_countdown(fpriv.u.t1.blenddesignmap);
746
25.0k
    pdfi_countdown(fpriv.u.t1.blendfontbbox);
747
25.0k
    pdfi_countdown(fpriv.u.t1.blendaxistypes);
748
25.0k
    pdfi_countdown(fpriv.u.t1.Subrs);
749
25.0k
    if (fpriv.gsu.gst1.UID.xvalues != NULL) {
750
0
        gs_free_object(ctx->memory, fpriv.gsu.gst1.UID.xvalues, "pdfi_read_type1_font(xuid)");
751
0
    }
752
753
25.0k
    if (code < 0) {
754
1.42k
        pdfi_countdown(t1f);
755
1.42k
    }
756
25.0k
    return code;
757
25.0k
}
758
759
int
760
pdfi_copy_type1_font(pdf_context *ctx, pdf_font *spdffont, pdf_dict *font_dict, pdf_font **tpdffont)
761
165k
{
762
165k
    int code = 0;
763
165k
    pdf_font_type1 *font = NULL;
764
165k
    gs_font_type1 *spfont1 = (gs_font_type1 *) spdffont->pfont;
765
165k
    gs_font_type1 *dpfont1;
766
165k
    gs_id t_id;
767
165k
    pdf_obj *tmp;
768
769
165k
    if (font_dict == NULL)
770
0
        return_error(gs_error_invalidfont);
771
772
165k
    code = pdfi_alloc_t1_font(ctx, &font, font_dict->object_num);
773
165k
    if (code < 0)
774
0
        return code;
775
165k
    dpfont1 = (gs_font_type1 *) font->pfont;
776
777
165k
    t_id = dpfont1->id;
778
165k
    memcpy(dpfont1, spfont1, sizeof(gs_font_type1));
779
165k
    dpfont1->id = t_id;
780
165k
    dpfont1->FAPI = NULL;
781
165k
    dpfont1->FAPI_font_data = NULL;
782
783
165k
    memcpy(font, spdffont, sizeof(pdf_font_type1));
784
165k
    font->pfont = (gs_font_base *)dpfont1;
785
165k
    font->refcnt = 1;
786
165k
    dpfont1->client_data = (void *)font;
787
165k
    font->filename = NULL;
788
789
165k
    dpfont1->notify_list.memory = NULL;
790
165k
    dpfont1->notify_list.first = NULL;
791
165k
    gs_notify_init(&dpfont1->notify_list, dpfont1->memory);
792
793
165k
    font->PDF_font = font_dict;
794
165k
    font->object_num = font_dict->object_num;
795
165k
    font->generation_num = font_dict->generation_num;
796
165k
    pdfi_countup(font->PDF_font);
797
798
    /* We want basefont and descriptor, but we can live without them */
799
165k
    font->BaseFont = NULL;
800
165k
    code = pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, &font->BaseFont);
801
165k
    if (code < 0) {
802
52
        pdfi_countdown(font->BaseFont);
803
52
        font->BaseFont = NULL;
804
52
    }
805
165k
    font->FontDescriptor = NULL;
806
165k
    code = pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, (pdf_obj **)&font->FontDescriptor);
807
165k
    if (code < 0) {
808
1.27k
        pdfi_countdown(font->FontDescriptor);
809
1.27k
        font->FontDescriptor = NULL;
810
1.27k
    }
811
812
165k
    pdfi_countup(font->Name);
813
165k
    pdfi_countup(font->CharStrings);
814
165k
    pdfi_countup(font->blenddesignpositions);
815
165k
    pdfi_countup(font->blenddesignmap);
816
165k
    pdfi_countup(font->blendfontbbox);
817
165k
    pdfi_countup(font->blendaxistypes);
818
165k
    pdfi_countup(font->Subrs);
819
820
165k
    if (font->BaseFont != NULL && ((pdf_name *)font->BaseFont)->length <= gs_font_name_max - 1) {
821
165k
        memcpy(dpfont1->key_name.chars, ((pdf_name *)font->BaseFont)->data, ((pdf_name *)font->BaseFont)->length);
822
165k
        dpfont1->key_name.size = ((pdf_name *)font->BaseFont)->length;
823
165k
        dpfont1->key_name.chars[dpfont1->key_name.size] = '\0';
824
165k
        memcpy(dpfont1->font_name.chars, ((pdf_name *)font->BaseFont)->data, ((pdf_name *)font->BaseFont)->length);
825
165k
        dpfont1->font_name.size = ((pdf_name *)font->BaseFont)->length;
826
165k
        dpfont1->font_name.chars[dpfont1->font_name.size] = '\0';
827
165k
    }
828
829
165k
    font->Encoding = NULL;
830
165k
    font->ToUnicode = NULL;
831
165k
    font->Widths = NULL;
832
833
165k
    pdfi_font_set_first_last_char(ctx, font_dict, (pdf_font *)font);
834
165k
    (void)pdfi_font_create_widths(ctx, font_dict, (pdf_font *)font, (double)(0.001 / hypot(dpfont1->FontMatrix.xx, dpfont1->FontMatrix.xy)));
835
836
165k
    font->descflags = 0;
837
165k
    if (font->FontDescriptor != NULL) {
838
13.5k
        code = pdfi_dict_get_int(ctx, font->FontDescriptor, "Flags", &font->descflags);
839
13.5k
        if (code >= 0) {
840
            /* If both the symbolic and non-symbolic flag are set,
841
               believe that latter.
842
             */
843
13.4k
            if ((font->descflags & 32) != 0)
844
11.7k
                font->descflags = (font->descflags & ~4);
845
13.4k
        }
846
13.5k
    }
847
848
165k
    if (pdfi_font_known_symbolic(font->BaseFont)) {
849
2.02k
        font->descflags |= 4;
850
2.02k
    }
851
852
165k
    tmp = NULL;
853
165k
    code = pdfi_dict_knownget(ctx, font_dict, "Encoding", &tmp);
854
165k
    if (code == 1) {
855
18.4k
        if ((pdfi_type_of(tmp) == PDF_NAME || pdfi_type_of(tmp) == PDF_DICT) && (font->descflags & 4) == 0) {
856
17.4k
            code = pdfi_create_Encoding(ctx, tmp, NULL, (pdf_obj **) & font->Encoding);
857
17.4k
            if (code >= 0)
858
17.2k
                code = 1;
859
17.4k
        }
860
1.01k
        else if (pdfi_type_of(tmp) == PDF_DICT && (font->descflags & 4) != 0) {
861
863
            code = pdfi_create_Encoding(ctx, tmp, (pdf_obj *)spdffont->Encoding, (pdf_obj **) &font->Encoding);
862
863
            if (code >= 0)
863
863
                code = 1;
864
863
        }
865
153
        else
866
153
            code = gs_error_undefined;
867
18.4k
        pdfi_countdown(tmp);
868
18.4k
        tmp = NULL;
869
18.4k
    }
870
146k
    else {
871
146k
        pdfi_countdown(tmp);
872
146k
        tmp = NULL;
873
146k
        code = 0;
874
146k
    }
875
876
165k
    if (code <= 0) {
877
147k
        font->Encoding = spdffont->Encoding;
878
147k
        pdfi_countup(font->Encoding);
879
147k
    }
880
881
    /* Since various aspects of the font may differ (widths, encoding, etc)
882
       we cannot reliably use the UniqueID/XUID for copied fonts.
883
     */
884
165k
    if (uid_is_XUID(&font->pfont->UID))
885
165k
        uid_free(&font->pfont->UID, font->pfont->memory, "pdfi_read_type1_font");
886
165k
    uid_set_invalid(&font->pfont->UID);
887
888
165k
    code = pdfi_font_generate_pseudo_XUID(ctx, font_dict, font->pfont);
889
165k
    if (code < 0) {
890
0
        goto error;
891
0
    }
892
893
165k
    if (ctx->args.ignoretounicode != true) {
894
165k
        code = pdfi_dict_get(ctx, font_dict, "ToUnicode", (pdf_obj **)&tmp);
895
165k
        if (code >= 0 && pdfi_type_of(tmp) == PDF_STREAM) {
896
1.68k
            pdf_cmap *tu = NULL;
897
1.68k
            code = pdfi_read_cmap(ctx, tmp, &tu);
898
1.68k
            pdfi_countdown(tmp);
899
1.68k
            tmp = (pdf_obj *)tu;
900
1.68k
        }
901
165k
        if (code < 0 || (tmp != NULL && pdfi_type_of(tmp) != PDF_CMAP)) {
902
163k
            pdfi_countdown(tmp);
903
163k
            tmp = NULL;
904
163k
            code = 0;
905
163k
        }
906
165k
    }
907
0
    else {
908
0
        tmp = NULL;
909
0
    }
910
165k
    font->ToUnicode = tmp;
911
912
165k
    code = gs_definefont(ctx->font_dir, (gs_font *) font->pfont);
913
165k
    if (code < 0) {
914
0
        goto error;
915
0
    }
916
917
165k
    code = pdfi_fapi_passfont((pdf_font *) font, 0, NULL, NULL, NULL, 0);
918
165k
    if (code < 0) {
919
413
        goto error;
920
413
    }
921
    /* object_num can be zero if the dictionary was defined inline */
922
165k
    if (font->object_num != 0) {
923
21.8k
        (void)replace_cache_entry(ctx, (pdf_obj *) font);
924
21.8k
    }
925
926
165k
    *tpdffont = (pdf_font *)font;
927
928
165k
error:
929
165k
    if (code < 0)
930
413
        pdfi_countdown(font);
931
932
165k
    return code;
933
165k
}
934
935
int
936
pdfi_free_font_type1(pdf_obj *font)
937
189k
{
938
189k
    pdf_font_type1 *t1f = (pdf_font_type1 *) font;
939
940
189k
    gs_free_object(OBJ_MEMORY(font), t1f->pfont, "Free Type 1 gs_font");
941
942
189k
    pdfi_countdown(t1f->PDF_font);
943
189k
    pdfi_countdown(t1f->BaseFont);
944
189k
    pdfi_countdown(t1f->FontDescriptor);
945
189k
    pdfi_countdown(t1f->Name);
946
189k
    pdfi_countdown(t1f->Encoding);
947
189k
    pdfi_countdown(t1f->ToUnicode);
948
189k
    pdfi_countdown(t1f->CharStrings);
949
189k
    pdfi_countdown(t1f->blenddesignpositions);
950
189k
    pdfi_countdown(t1f->blenddesignmap);
951
189k
    pdfi_countdown(t1f->blendfontbbox);
952
189k
    pdfi_countdown(t1f->blendaxistypes);
953
189k
    pdfi_countdown(t1f->Subrs);
954
189k
    pdfi_countdown(t1f->filename);
955
956
189k
    gs_free_object(OBJ_MEMORY(font), t1f->Widths, "Free Type 1 fontWidths");
957
189k
    gs_free_object(OBJ_MEMORY(font), t1f, "Free Type 1 font");
958
189k
    return 0;
959
189k
}