Coverage Report

Created: 2026-10-28 08:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/pdf/pdf_fontTT.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 TrueType font handling */
17
18
#include "pdf_int.h"
19
#include "pdf_font.h"
20
#include "pdf_fontTT.h"
21
#include "pdf_font1C.h" /* OTTO support */
22
#include "pdf_font_types.h"
23
#include "pdf_stack.h"
24
#include "pdf_file.h"
25
#include "pdf_dict.h"
26
#include "pdf_array.h"
27
#include "pdf_deref.h"
28
#include "gxfont42.h"
29
#include "gscencs.h"
30
#include "gsagl.h"
31
#include "gsutil.h"        /* For gs_next_ids() */
32
33
enum {
34
    CMAP_TABLE_NONE = 0,
35
    CMAP_TABLE_10_PRESENT = 1,
36
    CMAP_TABLE_30_PRESENT = 2,
37
    CMAP_TABLE_31_PRESENT = 4,
38
    CMAP_TABLE_310_PRESENT = 8
39
};
40
41
static int
42
pdfi_ttf_string_proc(gs_font_type42 * pfont, ulong offset, uint length, const byte ** pdata)
43
2.21G
{
44
2.21G
    pdf_font_truetype *ttfont = (pdf_font_truetype *)pfont->client_data;
45
2.21G
    int code = 0;
46
47
2.21G
    if ((uint64_t)offset + length > ttfont->sfnt->length) {
48
12.2M
        *pdata = NULL;
49
12.2M
        code = gs_note_error(gs_error_invalidfont);
50
12.2M
    }
51
2.19G
    else {
52
2.19G
        *pdata = ttfont->sfnt->data + offset;
53
2.19G
    }
54
2.21G
    return code;
55
2.21G
}
56
57
static gs_glyph pdfi_ttf_encode_char(gs_font *pfont, gs_char chr, gs_glyph_space_t sp)
58
10.7M
{
59
10.7M
    pdf_font_truetype *ttfont = (pdf_font_truetype *)pfont->client_data;
60
10.7M
    gs_glyph g = GS_NO_GLYPH;
61
10.7M
    uint ID;
62
10.7M
    int code;
63
64
10.7M
    if ((ttfont->descflags & 4) != 0 || sp == GLYPH_SPACE_INDEX) {
65
6.00M
        int code = pdfi_fapi_check_cmap_for_GID(pfont, (uint)chr, &ID);
66
6.00M
        if (code < 0 || ID == 0)
67
923k
            code = pdfi_fapi_check_cmap_for_GID(pfont, (uint)(chr | 0xf0 << 8), &ID);
68
6.00M
        g = (gs_glyph)ID;
69
6.00M
    }
70
4.77M
    else {
71
4.77M
        pdf_context *ctx = (pdf_context *)ttfont->ctx;
72
73
4.77M
        if (ttfont->Encoding != NULL) { /* safety */
74
4.77M
            pdf_name *GlyphName = NULL;
75
4.77M
            code = pdfi_array_get(ctx, ttfont->Encoding, (uint64_t)chr, (pdf_obj **)&GlyphName);
76
4.77M
            if (code >= 0) {
77
4.76M
                code = (*ctx->get_glyph_index)(pfont, (byte *)GlyphName->data, GlyphName->length, &ID);
78
4.76M
                pdfi_countdown(GlyphName);
79
4.76M
                if (code >= 0)
80
4.76M
                    g = (gs_glyph)ID;
81
4.76M
            }
82
4.77M
        }
83
4.77M
    }
84
85
10.7M
    return g;
86
10.7M
}
87
88
int pdfi_find_post_entry(gs_font_type42 *pfont, gs_const_string *gname, uint *cc)
89
1.14M
{
90
1.14M
    pdf_font_truetype *ttfont = (pdf_font_truetype *)pfont->client_data;
91
1.14M
    pdf_context *ctx = (pdf_context *)ttfont->ctx;
92
1.14M
    int code = 0;
93
1.14M
    if (ttfont->post != NULL) {
94
1.09M
        pdf_name *name = NULL;
95
1.09M
        pdf_num *n = NULL;
96
97
1.09M
        code = pdfi_name_alloc(ctx, (byte *)gname->data, gname->size, (pdf_obj **)&name);
98
1.09M
        if (code >= 0) {
99
1.09M
            pdfi_countup(name);
100
1.09M
            code = pdfi_dict_get_by_key(ctx, ttfont->post, name, (pdf_obj **)&n);
101
1.09M
            if (code >= 0 && pdfi_type_of(n) == PDF_INT) {
102
4.58k
                *cc = (uint)n->value.i;
103
4.58k
            }
104
1.09M
            else
105
1.09M
              *cc = 0;
106
1.09M
            pdfi_countdown(name);
107
1.09M
            pdfi_countdown(n);
108
1.09M
        }
109
1.09M
    }
110
49.0k
    else
111
49.0k
        code = gs_error_VMerror;
112
113
1.14M
    if (code == gs_error_VMerror){
114
49.0k
        uint i;
115
49.0k
        gs_string postname = {0};
116
117
49.0k
        code = gs_error_undefined;
118
49.0k
        if (pfont->data.numGlyphs > 0) { /* protect from corrupt font */
119
0
            for (i = 0; i < pfont->data.numGlyphs; i++) {
120
0
                code = gs_type42_find_post_name(pfont, (gs_glyph)i, &postname);
121
0
                if (code >= 0) {
122
0
                    if (gname->data[0] == postname.data[0]
123
0
                        && gname->size == postname.size
124
0
                        && !strncmp((char *)gname->data, (char *)postname.data, postname.size))
125
0
                    {
126
0
                        *cc = i;
127
0
                        code = 0;
128
0
                        break;
129
0
                    }
130
0
                }
131
0
            }
132
0
        }
133
49.0k
    }
134
1.14M
    return code;
135
1.14M
}
136
137
extern single_glyph_list_t SingleGlyphList[];
138
139
static uint pdfi_type42_get_glyph_index(gs_font_type42 *pfont, gs_glyph glyph)
140
4.40M
{
141
4.40M
    pdf_font_truetype *ttfont = (pdf_font_truetype *)pfont->client_data;
142
4.40M
    uint gind = 0;
143
4.40M
    uint cc = 0;
144
4.40M
    int code = 0;
145
146
4.40M
    if ((ttfont->descflags & 4) != 0 || glyph >= GS_MIN_GLYPH_INDEX) {
147
1.65M
        gind = (uint)glyph < GS_MIN_GLYPH_INDEX ? glyph : glyph - GS_MIN_GLYPH_INDEX;
148
1.65M
    }
149
2.74M
    else {
150
2.74M
        pdf_context *ctx = (pdf_context *)ttfont->ctx;
151
2.74M
        gs_const_string gname;
152
153
2.74M
        code = (*ctx->get_glyph_name)((gs_font *)pfont, glyph, &gname);
154
2.74M
        if (code < 0 || gname.data == NULL) {
155
0
            return (uint)glyph;
156
0
        }
157
2.74M
        if (gname.size == 7 && gname.data[0] == '.' && strncmp((char *)gname.data, ".notdef", 7) == 0) {
158
557k
            return 0; /* .notdef is GID 0, so short cut the rest of the function */
159
557k
        }
160
161
2.19M
        if (ttfont->cmap == pdfi_truetype_cmap_10) {
162
356k
            gs_glyph g;
163
164
356k
            g = gs_c_name_glyph((const byte *)gname.data, gname.size);
165
356k
            if (g != GS_NO_GLYPH) {
166
356k
                g = (gs_glyph)gs_c_decode(g, ENCODING_INDEX_MACROMAN);
167
356k
            }
168
0
            else {
169
0
                g = GS_NO_CHAR;
170
0
            }
171
172
356k
            if (g != GS_NO_CHAR) {
173
350k
                code = pdfi_fapi_check_cmap_for_GID((gs_font *)pfont, (uint)g, &cc);
174
350k
            }
175
176
356k
            if (code < 0 || cc == 0) {
177
241k
                code = pdfi_find_post_entry(pfont, &gname, &cc);
178
241k
                if (code < 0) {
179
239k
                    cc = 0;
180
239k
                    code = 0;
181
239k
                }
182
241k
            }
183
356k
        }
184
1.83M
        else {
185
            /* In theory, this should be 3,1 cmap, but we have examples that use 0,1 or other
186
               Unicode "platform" cmap tables, so "hail mary" just try it
187
             */
188
1.83M
            single_glyph_list_t *sgl = (single_glyph_list_t *)&(SingleGlyphList);
189
            /* Not to spec, but... if we get a "uni..." formatted name, use
190
               the hex value from that.
191
             */
192
1.83M
            if (gname.size == 7 && !strncmp((char *)gname.data, "uni", 3)) {
193
27.9k
                char gnbuf[64];
194
27.9k
                int l = (gname.size - 3) > 63 ? 63 : gname.size - 3;
195
196
27.9k
                memcpy(gnbuf, gname.data + 3, l);
197
27.9k
                gnbuf[l] = '\0';
198
27.9k
                l = sscanf(gnbuf, "%x", &gind);
199
27.9k
                if (l > 0)
200
27.9k
                    (void)pdfi_fapi_check_cmap_for_GID((gs_font *)pfont, (uint)gind, &cc);
201
0
                else
202
0
                    cc = 0;
203
27.9k
            }
204
1.80M
            else {
205
                /* Slow linear search */
206
3.49G
                for (; sgl->Glyph != 0x00;) {
207
3.49G
                    if (sgl->Glyph[0] == gname.data[0]
208
96.7M
                        && strlen(sgl->Glyph) == gname.size
209
9.10M
                        && !strncmp((char *)sgl->Glyph, (char *)gname.data, gname.size))
210
1.80M
                        break;
211
3.48G
                    sgl++;
212
3.48G
                }
213
1.80M
                if (sgl->Glyph != NULL) {
214
1.80M
                    code = pdfi_fapi_check_cmap_for_GID((gs_font *)pfont, (uint)sgl->Unicode, &cc);
215
1.80M
                    if (code < 0 || cc == 0)
216
878k
                        cc = 0;
217
1.80M
                }
218
75
                else
219
75
                    cc = 0;
220
221
1.80M
                if (cc == 0) {
222
878k
                    code = pdfi_find_post_entry(pfont, &gname, &cc);
223
878k
                    if (code < 0) {
224
876k
                        cc = (uint)glyph;
225
876k
                        code = 0;
226
876k
                    }
227
878k
                }
228
1.80M
            }
229
1.83M
        }
230
2.19M
        gind = cc;
231
2.19M
    }
232
3.84M
    return gind;
233
4.40M
}
234
235
static int
236
pdfi_ttf_enumerate_glyph(gs_font *font, int *pindex, gs_glyph_space_t glyph_space, gs_glyph *pglyph)
237
2.05M
{
238
2.05M
    if (glyph_space == GLYPH_SPACE_INDEX) {
239
97.5k
        return gs_type42_enumerate_glyph(font, pindex, glyph_space, pglyph);
240
97.5k
    }
241
1.95M
    else if (glyph_space == GLYPH_SPACE_NAME) {
242
1.95M
        pdf_font_truetype *ttfont = (pdf_font_truetype *)font->client_data;
243
244
1.95M
        if ((ttfont->descflags & 4) == 0) {
245
1.94M
            if (*pindex <= 0) {
246
29.7k
                *pindex = 0;
247
29.7k
            }
248
1.94M
            *pglyph = (*font->procs.encode_char)(font, (gs_char)*pindex, glyph_space);
249
1.94M
            if (*pglyph == GS_NO_GLYPH)
250
4.91k
                *pindex = 0;
251
1.93M
            else
252
1.93M
                (*pindex)++;
253
1.94M
        }
254
1.95M
    }
255
0
    else
256
0
        *pindex = 0;
257
1.95M
    return 0;
258
2.05M
}
259
260
static int pdfi_ttf_glyph_name(gs_font *pfont, gs_glyph glyph, gs_const_string * pstr)
261
358k
{
262
358k
    pdf_font_truetype *ttfont = (pdf_font_truetype *)pfont->client_data;
263
358k
    pdf_context *ctx = (pdf_context *)ttfont->ctx;
264
358k
    uint ID = 0;
265
358k
    int l, code = -1;
266
267
358k
    if (glyph >= GS_MIN_CID_GLYPH) { /* Fabricate a numeric name. */
268
927
        char cid_name[sizeof(gs_glyph) * 3 + 1];
269
270
927
        l = gs_snprintf(cid_name, sizeof(cid_name), "%lu", (ulong) glyph);
271
        /* We need to create the entry in the name table... */
272
927
        code = (*ctx->get_glyph_index)(pfont, (byte *)cid_name, l, &ID);
273
927
        if (code < 0)
274
0
            return -1;
275
276
927
        code = (*ctx->get_glyph_name)(pfont, (gs_glyph)ID, pstr);
277
927
        if (code < 0)
278
0
            return -1; /* No name, trigger pdfwrite Type 3 fallback */
279
357k
    } else {
280
357k
        if ((ttfont->descflags & 4) != 0) {
281
35.2k
            code = gs_type42_find_post_name((gs_font_type42 *)pfont, glyph, (gs_string *)pstr);
282
35.2k
            if (code < 0) {
283
31.0k
                char buf[64];
284
31.0k
                l = gs_snprintf(buf, sizeof(buf), "~gs~gName~%04x", (uint)glyph);
285
31.0k
                code = (*ctx->get_glyph_index)(pfont, (byte *)buf, l, &ID);
286
31.0k
            }
287
4.17k
            else {
288
4.17k
                code = (*ctx->get_glyph_index)(pfont, (byte *)pstr->data, pstr->size, &ID);
289
4.17k
            }
290
35.2k
            if (code < 0)
291
0
                return -1; /* No name, trigger pdfwrite Type 3 fallback */
292
293
35.2k
            code = (*ctx->get_glyph_name)(pfont, (gs_glyph)ID, pstr);
294
35.2k
            if (code < 0)
295
0
                return -1; /* No name, trigger pdfwrite Type 3 fallback */
296
35.2k
        }
297
321k
        else {
298
321k
            code = (*ctx->get_glyph_name)(pfont, glyph, pstr);
299
321k
            if (code < 0)
300
0
                return -1; /* No name, trigger pdfwrite Type 3 fallback */
301
321k
        }
302
357k
    }
303
358k
    return code;
304
305
358k
}
306
307
static void pdfi_make_post_dict(gs_font_type42 *pfont)
308
16.9k
{
309
16.9k
    pdf_font_truetype *ttfont = (pdf_font_truetype *)pfont->client_data;
310
16.9k
    pdf_context *ctx = (pdf_context *)ttfont->ctx;
311
16.9k
    int i, code = 0;
312
16.9k
    if (ttfont->post == NULL && pfont->data.numGlyphs > 0) {
313
16.7k
        code = pdfi_dict_alloc(ctx, pfont->data.numGlyphs, &ttfont->post);
314
16.7k
        if (code < 0)
315
0
            return;
316
317
16.7k
        pdfi_countup(ttfont->post);
318
319
20.6M
        for (i = 0; i < pfont->data.numGlyphs; i++) {
320
20.5M
            gs_string postname = {0};
321
20.5M
            pdf_name *key;
322
20.5M
            pdf_num *ind;
323
324
20.5M
            code = gs_type42_find_post_name(pfont, (gs_glyph)i, &postname);
325
20.5M
            if (code < 0) {
326
18.7M
                continue;
327
18.7M
            }
328
1.79M
            code = pdfi_name_alloc(ctx, postname.data, postname.size, (pdf_obj **)&key);
329
1.79M
            if (code < 0) {
330
0
               continue;
331
0
            }
332
1.79M
            pdfi_countup(key);
333
1.79M
            code = pdfi_object_alloc(ctx, PDF_INT, 0, (pdf_obj **)&ind);
334
1.79M
            if (code < 0) {
335
0
               pdfi_countdown(key);
336
0
               continue;
337
0
            }
338
1.79M
            pdfi_countup(ind);
339
1.79M
            ind->value.i = i;
340
1.79M
            (void)pdfi_dict_put_obj(ctx, ttfont->post, (pdf_obj *)key, (pdf_obj *)ind, false);
341
1.79M
            pdfi_countdown(key);
342
1.79M
            pdfi_countdown(ind);
343
1.79M
        }
344
16.7k
    }
345
16.9k
}
346
347
348
static int
349
pdfi_alloc_tt_font(pdf_context *ctx, pdf_font_truetype **font, bool is_cid)
350
26.1k
{
351
26.1k
    pdf_font_truetype *ttfont = NULL;
352
26.1k
    gs_font_type42 *pfont = NULL;
353
354
26.1k
    ttfont = (pdf_font_truetype *)gs_alloc_bytes(ctx->memory, sizeof(pdf_font_truetype), "pdfi (truetype pdf_font)");
355
26.1k
    if (ttfont == NULL)
356
0
        return_error(gs_error_VMerror);
357
358
26.1k
    memset(ttfont, 0x00, sizeof(pdf_font_truetype));
359
26.1k
    ttfont->type = PDF_FONT;
360
26.1k
    ttfont->ctx = ctx;
361
26.1k
    ttfont->pdfi_font_type = e_pdf_font_truetype;
362
363
#if REFCNT_DEBUG
364
    ttfont->UID = ctx->UID++;
365
    outprintf(ctx->memory, "Allocated object of type %c with UID %"PRIi64"\n", ttfont->type, ttfont->UID);
366
#endif
367
368
26.1k
    pdfi_countup(ttfont);
369
370
26.1k
    pfont = (gs_font_type42 *)gs_alloc_struct(ctx->memory, gs_font_type42, &st_gs_font_type42,
371
26.1k
                            "pdfi (truetype pfont)");
372
26.1k
    if (pfont == NULL) {
373
0
        pdfi_countdown(ttfont);
374
0
        return_error(gs_error_VMerror);
375
0
    }
376
26.1k
    memset(pfont, 0x00, sizeof(gs_font_type42));
377
378
26.1k
    ttfont->pfont = (gs_font_base *)pfont;
379
380
26.1k
    gs_make_identity(&pfont->orig_FontMatrix);
381
26.1k
    gs_make_identity(&pfont->FontMatrix);
382
26.1k
    pfont->next = pfont->prev = 0;
383
26.1k
    pfont->memory = ctx->memory;
384
26.1k
    pfont->dir = ctx->font_dir;
385
26.1k
    pfont->is_resource = false;
386
26.1k
    gs_notify_init(&pfont->notify_list, ctx->memory);
387
26.1k
    pfont->base = (gs_font *) ttfont->pfont;
388
26.1k
    pfont->client_data = ttfont;
389
26.1k
    pfont->WMode = 0;
390
26.1k
    pfont->PaintType = 0;
391
26.1k
    pfont->StrokeWidth = 0;
392
26.1k
    pfont->is_cached = 0;
393
26.1k
    pfont->FAPI = NULL;
394
26.1k
    pfont->FAPI_font_data = NULL;
395
26.1k
    pfont->procs.init_fstack = gs_default_init_fstack;
396
26.1k
    pfont->procs.next_char_glyph = gs_default_next_char_glyph;
397
26.1k
    pfont->FontType = ft_TrueType;
398
26.1k
    pfont->ExactSize = fbit_use_outlines;
399
26.1k
    pfont->InBetweenSize = fbit_use_outlines;
400
26.1k
    pfont->TransformedChar = fbit_use_outlines;
401
    /* We may want to do something clever with an XUID here */
402
26.1k
    pfont->id = gs_next_ids(ctx->memory, 1);
403
26.1k
    uid_set_UniqueID(&pfont->UID, pfont->id);
404
    /* The buildchar proc will be filled in by FAPI -
405
       we won't worry about working without FAPI */
406
26.1k
    pfont->procs.encode_char = pdfi_ttf_encode_char;
407
26.1k
    pfont->data.string_proc = pdfi_ttf_string_proc;
408
26.1k
    pfont->procs.glyph_name = pdfi_ttf_glyph_name;
409
26.1k
    pfont->procs.decode_glyph = pdfi_decode_glyph;
410
26.1k
    pfont->procs.define_font = gs_no_define_font;
411
26.1k
    pfont->procs.make_font = gs_no_make_font;
412
413
26.1k
    ttfont->default_font_info = gs_default_font_info;
414
26.1k
    pfont->procs.font_info = pdfi_default_font_info;
415
416
26.1k
    pfont->procs.glyph_info = gs_default_glyph_info;
417
26.1k
    pfont->procs.glyph_outline = gs_no_glyph_outline;
418
26.1k
    pfont->procs.build_char = NULL;
419
26.1k
    pfont->procs.same_font = gs_default_same_font;
420
26.1k
    pfont->procs.enumerate_glyph = gs_no_enumerate_glyph;
421
422
26.1k
    pfont->encoding_index = ENCODING_INDEX_UNKNOWN;
423
26.1k
    pfont->nearest_encoding_index = ENCODING_INDEX_UNKNOWN;
424
425
26.1k
    pfont->client_data = (void *)ttfont;
426
427
26.1k
    *font = ttfont;
428
26.1k
    return 0;
429
26.1k
}
430
431
static void pdfi_set_type42_custom_procs(pdf_font_truetype *pdfttfont)
432
17.1k
{
433
17.1k
    gs_font_type42 *pfont = (gs_font_type42 *)pdfttfont->pfont;
434
435
17.1k
    pdfttfont->default_font_info = pfont->procs.font_info;
436
17.1k
    pfont->procs.font_info = pdfi_default_font_info;
437
17.1k
    pfont->data.get_glyph_index = pdfi_type42_get_glyph_index;
438
17.1k
    pfont->procs.enumerate_glyph = pdfi_ttf_enumerate_glyph;
439
17.1k
}
440
441
int pdfi_read_truetype_font(pdf_context *ctx, pdf_dict *font_dict, pdf_dict *stream_dict, pdf_dict *page_dict, byte *buf, int64_t buflen, int findex, pdf_font **ppdffont)
442
22.7k
{
443
22.7k
    pdf_font_truetype *font = NULL;
444
22.7k
    int code = 0, i;
445
22.7k
    pdf_obj *fontdesc = NULL;
446
22.7k
    pdf_obj *obj = NULL;
447
22.7k
    pdf_obj *basefont = NULL;
448
22.7k
    int64_t descflags;
449
22.7k
    bool encoding_known = false;
450
22.7k
    bool forced_symbolic = false;
451
22.7k
    pdf_obj *tounicode = NULL;
452
453
22.7k
    if (ppdffont == NULL)
454
0
        return_error(gs_error_invalidaccess);
455
456
22.7k
    *ppdffont = NULL;
457
458
22.7k
    if (font_dict != NULL)
459
22.7k
        (void)pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, &fontdesc);
460
461
22.7k
    if ((code = pdfi_alloc_tt_font(ctx, &font, false)) < 0) {
462
0
        code = gs_note_error(gs_error_invalidfont);
463
0
        goto error;
464
0
    }
465
22.7k
    if (font_dict != NULL) {
466
22.7k
        font->object_num = font_dict->object_num;
467
22.7k
        font->generation_num = font_dict->generation_num;
468
22.7k
        font->indirect_num = font_dict->indirect_num;
469
22.7k
        font->indirect_gen = font_dict->indirect_gen;
470
22.7k
    }
471
472
22.7k
    font->FontDescriptor = (pdf_dict *)fontdesc;
473
22.7k
    fontdesc = NULL;
474
475
22.7k
    pdfi_font_set_first_last_char(ctx, font_dict, (pdf_font *)font);
476
477
22.7k
    code = pdfi_object_alloc(ctx, PDF_BUFFER, 0, (pdf_obj **)&font->sfnt);
478
22.7k
    if (code < 0) {
479
0
        goto error;
480
0
    }
481
22.7k
    pdfi_countup(font->sfnt);
482
22.7k
    code = pdfi_buffer_set_data((pdf_obj *)font->sfnt, buf, buflen);
483
22.7k
    if (code < 0) {
484
0
        goto error;
485
0
    }
486
22.7k
    buf = NULL;
487
488
    /* Strictly speaking BaseFont is required, but we can continue without one */
489
22.7k
    if (font_dict != NULL) {
490
22.7k
        code = pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, (pdf_obj **)&basefont);
491
22.7k
        if (code > 0) {
492
22.2k
            pdf_name *nobj = (pdf_name *)basefont;
493
22.2k
            int nlen = nobj->length > gs_font_name_max ? gs_font_name_max : nobj->length;
494
495
22.2k
            memcpy(font->pfont->key_name.chars, nobj->data, nlen);
496
22.2k
            font->pfont->key_name.chars[nlen] = 0;
497
22.2k
            font->pfont->key_name.size = nlen;
498
22.2k
            memcpy(font->pfont->font_name.chars, nobj->data, nlen);
499
22.2k
            font->pfont->font_name.chars[nlen] = 0;
500
22.2k
            font->pfont->font_name.size = nlen;
501
22.2k
            pdfi_countdown(obj);
502
22.2k
            obj = NULL;
503
22.2k
        }
504
22.7k
    }
505
22.7k
    font->BaseFont = basefont;
506
22.7k
    basefont = NULL;
507
22.7k
    font->PDF_font = font_dict;
508
22.7k
    pdfi_countup(font_dict);
509
510
    /* ignore errors with widths... for now */
511
22.7k
    if (font_dict != NULL)
512
22.7k
        (void)pdfi_font_create_widths(ctx, font_dict, (pdf_font*)font, 0.001);
513
514
22.7k
    if (ctx->args.ignoretounicode != true && font_dict != NULL) {
515
22.7k
        code = pdfi_dict_get(ctx, font_dict, "ToUnicode", (pdf_obj **)&tounicode);
516
22.7k
        if (code >= 0 && pdfi_type_of(tounicode) == PDF_STREAM) {
517
7.80k
            pdf_cmap *tu = NULL;
518
7.80k
            code = pdfi_read_cmap(ctx, tounicode, &tu);
519
7.80k
            pdfi_countdown(tounicode);
520
7.80k
            tounicode = (pdf_obj *)tu;
521
7.80k
        }
522
22.7k
        if (code < 0 || (tounicode != NULL && pdfi_type_of(tounicode) != PDF_CMAP)) {
523
14.8k
            pdfi_countdown(tounicode);
524
14.8k
            tounicode = NULL;
525
14.8k
            code = 0;
526
14.8k
        }
527
22.7k
    }
528
0
    else {
529
0
        tounicode = NULL;
530
0
    }
531
22.7k
    font->ToUnicode = tounicode;
532
22.7k
    tounicode = NULL;
533
534
22.7k
    if (font->FontDescriptor != NULL) {
535
22.7k
        code = pdfi_dict_get_int(ctx, font->FontDescriptor, "Flags", &descflags);
536
22.7k
        if (code < 0)
537
17
            descflags = 0;
538
22.7k
    }
539
0
    else {
540
0
        descflags = 0;
541
0
    }
542
543
22.7k
    if (font_dict != NULL)
544
22.7k
        code = pdfi_dict_get(ctx, font_dict, "Encoding", &obj);
545
0
    else
546
0
        code = gs_error_undefined;
547
548
22.7k
    if (code < 0) {
549
4.61k
        static const char encstr[] = "WinAnsiEncoding";
550
4.61k
        code = pdfi_name_alloc(ctx, (byte *)encstr, strlen(encstr), (pdf_obj **)&obj);
551
4.61k
        if (code >= 0)
552
4.61k
            pdfi_countup(obj);
553
0
        else
554
0
            goto error;
555
4.61k
    }
556
18.1k
    else {
557
18.1k
        encoding_known = true;
558
        /* If we have and encoding, and both the symbolic and non-symbolic flag are set,
559
           believe that latter.
560
         */
561
18.1k
        if ((descflags & 32) != 0)
562
17.8k
            descflags = (descflags & ~4);
563
18.1k
    }
564
565
22.7k
    if ((forced_symbolic = pdfi_font_known_symbolic(font->BaseFont)) == true) {
566
0
        descflags |= 4;
567
0
    }
568
569
22.7k
    code = pdfi_create_Encoding(ctx, (pdf_font *)font, obj, NULL, (pdf_obj **)&font->Encoding);
570
    /* If we get an error, and the font is non-symbolic, return the error */
571
22.7k
    if (code < 0 && (descflags & 4) == 0)
572
49
        goto error;
573
    /* If we get an error, and the font is symbolic, pretend we never saw an /Encoding */
574
22.6k
    if (code < 0)
575
5
        encoding_known = false;
576
22.6k
    pdfi_countdown(obj);
577
22.6k
    obj = NULL;
578
579
22.6k
    code = gs_type42_font_init((gs_font_type42 *)font->pfont, 0);
580
22.6k
    if (code < 0) {
581
5.55k
        goto error;
582
5.55k
    }
583
584
17.1k
    pdfi_set_type42_custom_procs(font);
585
586
    /* We're probably dead in the water without cmap tables, but make sure, for safety */
587
    /* This is a horrendous morass of guesses at what Acrobat does with files that contravene
588
       what the spec says about symbolic fonts, cmap tables and encodings.
589
     */
590
17.1k
    if (forced_symbolic != true && (descflags & 4) != 0 && ((gs_font_type42 *)font->pfont)->data.cmap != 0) {
591
3.36k
        gs_font_type42 *t42f = (gs_font_type42 *)font->pfont;
592
3.36k
        int numcmaps;
593
3.36k
        int cmaps_available = CMAP_TABLE_NONE;
594
3.36k
        const byte *d;
595
3.36k
        code = (*t42f->data.string_proc)(t42f, t42f->data.cmap + 2, 2, &d);
596
3.36k
        if (code < 0)
597
7
            goto error;
598
3.35k
        numcmaps = d[1] | d[0] << 8;
599
86.6k
        for (i = 0; i < numcmaps; i++) {
600
83.4k
            code = (*t42f->data.string_proc)(t42f, t42f->data.cmap + 4 + i * 8, 4, &d);
601
83.4k
            if (code < 0)
602
136
                goto error;
603
87.6k
#define CMAP_PLAT_ENC_ID(a,b,c,d) (a << 24 | b << 16 | c << 8 | d)
604
83.2k
            switch(CMAP_PLAT_ENC_ID(d[0], d[1], d[2], d[3])) {
605
3.00k
                case CMAP_PLAT_ENC_ID(0, 1, 0, 0):
606
3.00k
                    cmaps_available |= CMAP_TABLE_10_PRESENT;
607
3.00k
                    break;
608
1.25k
                case CMAP_PLAT_ENC_ID(0, 3, 0, 0):
609
1.25k
                    cmaps_available |= CMAP_TABLE_30_PRESENT;
610
1.25k
                    break;
611
85
                case CMAP_PLAT_ENC_ID(0, 3, 0, 1):
612
85
                    cmaps_available |= CMAP_TABLE_31_PRESENT;
613
85
                    break;
614
0
                case CMAP_PLAT_ENC_ID(0, 3, 1, 0):
615
0
                    cmaps_available |= CMAP_TABLE_310_PRESENT;
616
0
                    break;
617
78.9k
                default: /* Not one we're interested in */
618
78.9k
                    break;
619
83.2k
            }
620
83.2k
        }
621
3.22k
#undef CMAP_PLAT_ENC_ID
622
3.22k
        if ((cmaps_available & CMAP_TABLE_30_PRESENT) == CMAP_TABLE_30_PRESENT) {
623
1.22k
            font->descflags = descflags;
624
1.22k
        }
625
2.00k
        else if (encoding_known == true) {
626
36
            static const char mrencstr[] = "MacRomanEncoding";
627
36
            static const char waencstr[] = "WinAnsiEncoding";
628
36
            const char *encstr = ((cmaps_available & CMAP_TABLE_31_PRESENT) == CMAP_TABLE_31_PRESENT) ? waencstr : mrencstr;
629
36
            font->descflags = descflags & ~4;
630
36
            code = pdfi_name_alloc(ctx, (byte *)encstr, strlen(encstr), (pdf_obj **)&obj);
631
36
            if (code >= 0)
632
36
                pdfi_countup(obj);
633
0
            else
634
0
                goto error;
635
36
            pdfi_countdown(font->Encoding);
636
36
            code = pdfi_create_Encoding(ctx, (pdf_font *)font, obj, NULL, (pdf_obj **)&font->Encoding);
637
36
            if (code < 0)
638
0
                goto error;
639
36
            pdfi_countdown(obj);
640
36
            obj = NULL;
641
36
        }
642
1.96k
        else
643
1.96k
            font->descflags = descflags;
644
3.22k
    }
645
13.7k
    else {
646
13.7k
        font->descflags = descflags;
647
13.7k
    }
648
649
16.9k
    if (uid_is_XUID(&font->pfont->UID))
650
16.9k
        uid_free(&font->pfont->UID, font->pfont->memory, "pdfi_read_type1_font");
651
16.9k
    uid_set_invalid(&font->pfont->UID);
652
653
16.9k
    code = pdfi_font_generate_pseudo_XUID(ctx, font_dict, font->pfont);
654
16.9k
    if (code < 0) {
655
0
        goto error;
656
0
    }
657
658
16.9k
    if ((font->descflags & 4) == 0) {
659
        /* Horrid hacky solution */
660
        /* We don't want to draw the TTF notdef */
661
13.7k
        gs_font_type42 *gst42 = ((gs_font_type42 *)font->pfont);
662
13.7k
        if (gst42->data.len_glyphs != NULL && gst42->data.len_glyphs[0] > 10) {
663
10.8k
            gst42->data.len_glyphs[0] = 0;
664
10.8k
        }
665
13.7k
    }
666
667
16.9k
    pdfi_make_post_dict((gs_font_type42 *)font->pfont);
668
669
16.9k
    pdfi_font_set_orig_fonttype(ctx, (pdf_font *)font);
670
16.9k
    code = gs_definefont(ctx->font_dir, (gs_font *)font->pfont);
671
16.9k
    if (code < 0) {
672
0
        goto error;
673
0
    }
674
675
16.9k
    code = pdfi_fapi_passfont((pdf_font *)font, 0, NULL, NULL, font->sfnt->data, font->sfnt->length);
676
16.9k
    if (code < 0) {
677
1.02k
        goto error;
678
1.02k
    }
679
680
    /* object_num can be zero if the dictionary was defined inline */
681
15.9k
    if (font->object_num != 0) {
682
15.7k
        (void)replace_cache_entry(ctx, (pdf_obj *)font);
683
15.7k
    }
684
685
15.9k
    *ppdffont = (pdf_font *)font;
686
15.9k
    return code;
687
6.77k
error:
688
6.77k
    pdfi_countdown(obj);
689
6.77k
    obj = NULL;
690
6.77k
    if (font_dict != NULL) {
691
6.77k
        if (pdfi_dict_get(ctx, font_dict, ".Path", &obj) >= 0)
692
0
        {
693
0
            char fname[gp_file_name_sizeof + 1];
694
0
            pdf_string *fobj = (pdf_string *)obj;
695
696
0
            memcpy(fname, fobj->data, fobj->length > gp_file_name_sizeof ? gp_file_name_sizeof : fobj->length);
697
0
            fname[fobj->length > gp_file_name_sizeof ? gp_file_name_sizeof : fobj->length] = '\0';
698
699
0
            (void)pdfi_set_error_var(ctx, code, NULL, E_PDF_BADSTREAM, "pdfi_read_truetype_font", "Error reading TrueType font file %s\n", fname);
700
0
        }
701
6.77k
        else {
702
6.77k
            (void)pdfi_set_error_var(ctx, code, NULL, E_PDF_BADSTREAM, "pdfi_read_truetype_font", "Error reading embedded TrueType font object %u\n", font_dict->object_num);
703
6.77k
        }
704
6.77k
    }
705
0
    else {
706
0
        pdfi_set_error(ctx, code, NULL, E_PDF_BADSTREAM, "pdfi_read_truetype_font", "Error reading font\n");
707
0
    }
708
6.77k
    if (buf != NULL)
709
0
        gs_free_object(ctx->memory, buf, "pdfi_read_truetype_font(buf)");
710
6.77k
    pdfi_countdown(fontdesc);
711
6.77k
    pdfi_countdown(basefont);
712
6.77k
    pdfi_countdown(font);
713
6.77k
    return code;
714
16.9k
}
715
716
int
717
pdfi_copy_truetype_font(pdf_context *ctx, pdf_font *spdffont, pdf_dict *font_dict, pdf_font **tpdffont)
718
3.44k
{
719
3.44k
    int code = 0;
720
3.44k
    pdf_font_truetype *font = NULL;
721
3.44k
    gs_font_type42 *spfont1 = (gs_font_type42 *) spdffont->pfont;
722
3.44k
    gs_font_type42 *dpfont42;
723
3.44k
    gs_id t_id;
724
3.44k
    pdf_obj *tmp;
725
726
3.44k
    if (font_dict == NULL)
727
0
        return_error(gs_error_invalidfont);
728
729
3.44k
    code = pdfi_alloc_tt_font(ctx, &font, font_dict->object_num);
730
3.44k
    if (code < 0)
731
0
        return code;
732
3.44k
    dpfont42 = (gs_font_type42 *) font->pfont;
733
734
3.44k
    t_id = dpfont42->id;
735
3.44k
    memcpy(dpfont42, spfont1, sizeof(gs_font_type42));
736
3.44k
    dpfont42->id = t_id;
737
3.44k
    dpfont42->FAPI = NULL;
738
3.44k
    dpfont42->FAPI_font_data = NULL;
739
3.44k
    dpfont42->notify_list.memory = NULL;
740
3.44k
    dpfont42->notify_list.first = NULL;
741
3.44k
    gs_notify_init(&dpfont42->notify_list, dpfont42->memory);
742
743
3.44k
    memcpy(font, spdffont, sizeof(pdf_font_truetype));
744
3.44k
    font->refcnt = 1;
745
3.44k
    font->pfont = (gs_font_base *)dpfont42;
746
3.44k
    dpfont42->client_data = (void *)font;
747
3.44k
    font->filename = NULL;
748
749
3.44k
    pdfi_countup(font->post);
750
3.44k
    pdfi_countup(font->sfnt);
751
3.44k
    pdfi_countup(font->copyright);
752
3.44k
    pdfi_countup(font->notice);
753
3.44k
    pdfi_countup(font->fullname);
754
3.44k
    pdfi_countup(font->familyname);
755
3.44k
    font->Encoding = NULL;
756
3.44k
    font->ToUnicode = NULL;
757
3.44k
    font->Widths = NULL;
758
759
3.44k
    if (dpfont42->data.len_glyphs != NULL) {
760
3.44k
        dpfont42->data.len_glyphs = (uint *)gs_alloc_byte_array(dpfont42->memory, dpfont42->data.numGlyphs + 1, sizeof(uint), "pdfi_copy_truetype_font");
761
3.44k
        if (dpfont42->data.len_glyphs == NULL) {
762
0
            pdfi_countdown(font);
763
0
            return_error(gs_error_VMerror);
764
0
        }
765
3.44k
        code = gs_font_notify_register((gs_font *)dpfont42, gs_len_glyphs_release, (void *)dpfont42);
766
3.44k
        if (code < 0) {
767
0
            gs_free_object(dpfont42->memory, dpfont42->data.len_glyphs, "gs_len_glyphs_release");
768
0
            dpfont42->data.len_glyphs = NULL;
769
0
            pdfi_countdown(font);
770
0
            return code;
771
0
        }
772
3.44k
        memcpy(dpfont42->data.len_glyphs, spfont1->data.len_glyphs, (dpfont42->data.numGlyphs + 1) * sizeof(uint));
773
3.44k
    }
774
3.44k
    if (dpfont42->data.gsub != NULL) {
775
0
        dpfont42->data.gsub_size = spfont1->data.gsub_size;
776
777
0
        dpfont42->data.gsub = gs_alloc_byte_array(dpfont42->memory, dpfont42->data.gsub_size, 1, "pdfi_copy_truetype_font");
778
0
        if (dpfont42->data.gsub == 0) {
779
0
            pdfi_countdown(font);
780
0
            return_error(gs_error_VMerror);
781
0
        }
782
783
0
        code = gs_font_notify_register((gs_font *)dpfont42, gs_gsub_release, (void *)dpfont42);
784
0
        if (code < 0) {
785
0
            gs_free_object(dpfont42->memory, dpfont42->data.gsub, "gs_len_glyphs_release");
786
0
            dpfont42->data.gsub = NULL;
787
0
            pdfi_countdown(font);
788
0
            return code;
789
0
        }
790
0
        memcpy(dpfont42->data.gsub, spfont1->data.gsub, dpfont42->data.gsub_size);
791
0
    }
792
793
3.44k
    font->PDF_font = font_dict;
794
3.44k
    font->object_num = font_dict->object_num;
795
3.44k
    font->generation_num = font_dict->generation_num;
796
3.44k
    pdfi_countup(font->PDF_font);
797
798
    /* We want basefont and descriptor, but we can live without them */
799
3.44k
    font->BaseFont = NULL;
800
3.44k
    (void)pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, &font->BaseFont);
801
3.44k
    font->FontDescriptor = NULL;
802
3.44k
    (void)pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, (pdf_obj **)&font->FontDescriptor);
803
804
3.44k
    if (font->BaseFont != NULL && ((pdf_name *)font->BaseFont)->length <= gs_font_name_max) {
805
3.40k
        memcpy(dpfont42->key_name.chars, ((pdf_name *)font->BaseFont)->data, ((pdf_name *)font->BaseFont)->length);
806
3.40k
        dpfont42->key_name.size = ((pdf_name *)font->BaseFont)->length;
807
3.40k
        memcpy(dpfont42->font_name.chars, ((pdf_name *)font->BaseFont)->data, ((pdf_name *)font->BaseFont)->length);
808
3.40k
        dpfont42->font_name.size = ((pdf_name *)font->BaseFont)->length;
809
3.40k
    }
810
811
3.44k
    pdfi_font_set_first_last_char(ctx, font_dict, (pdf_font *)font);
812
3.44k
    (void)pdfi_font_create_widths(ctx, font_dict, (pdf_font*)font, (double)0.001);
813
814
3.44k
    font->descflags = 0;
815
3.44k
    if (font->FontDescriptor != NULL) {
816
3.44k
        code = pdfi_dict_get_int(ctx, font->FontDescriptor, "Flags", &font->descflags);
817
3.44k
        if (code >= 0) {
818
            /* If both the symbolic and non-symbolic flag are set,
819
               believe that latter.
820
             */
821
3.44k
            if ((font->descflags & 32) != 0)
822
3.42k
                font->descflags = (font->descflags & ~4);
823
3.44k
        }
824
3.44k
    }
825
826
3.44k
    tmp = NULL;
827
3.44k
    code = pdfi_dict_knownget(ctx, font_dict, "Encoding", &tmp);
828
3.44k
    if (code == 1) {
829
3.42k
        if ((pdfi_type_of(tmp) == PDF_NAME || pdfi_type_of(tmp) == PDF_DICT) && (font->descflags & 4) == 0) {
830
3.42k
            code = pdfi_create_Encoding(ctx, (pdf_font *)font, tmp, NULL, (pdf_obj **) & font->Encoding);
831
3.42k
        }
832
0
        else if (pdfi_type_of(tmp) == PDF_DICT && (font->descflags & 4) != 0) {
833
0
            code = pdfi_create_Encoding(ctx, (pdf_font *)font, tmp, (pdf_obj *)spdffont->Encoding, (pdf_obj **) &font->Encoding);
834
0
        }
835
0
        else {
836
0
            code = gs_error_undefined;
837
0
        }
838
3.42k
        pdfi_countdown(tmp);
839
3.42k
        tmp = NULL;
840
3.42k
    }
841
17
    else {
842
17
        pdfi_countdown(tmp);
843
17
        tmp = NULL;
844
17
        code = gs_error_undefined;
845
17
    }
846
847
3.44k
    if (code < 0) {
848
17
        font->Encoding = spdffont->Encoding;
849
17
        pdfi_countup(font->Encoding);
850
17
    }
851
852
3.44k
    code = uid_copy(&font->pfont->UID, font->pfont->memory, "pdfi_copy_cff_font");
853
3.44k
    if (code < 0) {
854
0
        uid_set_invalid(&font->pfont->UID);
855
0
    }
856
3.44k
    if (spdffont->filename == NULL) {
857
3.44k
        code = pdfi_font_generate_pseudo_XUID(ctx, font_dict, font->pfont);
858
3.44k
        if (code < 0) {
859
0
            goto error;
860
0
        }
861
3.44k
    }
862
3.44k
    if (ctx->args.ignoretounicode != true) {
863
3.44k
        code = pdfi_dict_get(ctx, font_dict, "ToUnicode", (pdf_obj **)&tmp);
864
3.44k
        if (code >= 0 && pdfi_type_of(tmp) == PDF_STREAM) {
865
170
            pdf_cmap *tu = NULL;
866
170
            code = pdfi_read_cmap(ctx, tmp, &tu);
867
170
            pdfi_countdown(tmp);
868
170
            tmp = (pdf_obj *)tu;
869
170
        }
870
3.44k
        if (code < 0 || (tmp != NULL && pdfi_type_of(tmp) != PDF_CMAP)) {
871
3.27k
            pdfi_countdown(tmp);
872
3.27k
            tmp = NULL;
873
3.27k
            code = 0;
874
3.27k
        }
875
3.44k
    }
876
0
    else {
877
0
        tmp = NULL;
878
0
    }
879
3.44k
    font->ToUnicode = tmp;
880
3.44k
    pdfi_font_set_orig_fonttype(ctx, (pdf_font *)font);
881
882
3.44k
    code = gs_definefont(ctx->font_dir, (gs_font *) font->pfont);
883
3.44k
    if (code < 0) {
884
0
        goto error;
885
0
    }
886
887
3.44k
    code = pdfi_fapi_passfont((pdf_font *) font, 0, NULL, NULL, NULL, 0);
888
3.44k
    if (code < 0) {
889
0
        goto error;
890
0
    }
891
    /* object_num can be zero if the dictionary was defined inline */
892
3.44k
    if (font->object_num != 0) {
893
202
        (void)replace_cache_entry(ctx, (pdf_obj *) font);
894
202
    }
895
896
3.44k
    *tpdffont = (pdf_font *)font;
897
898
3.44k
error:
899
3.44k
    if (code < 0)
900
0
        pdfi_countdown(font);
901
3.44k
    return code;
902
3.44k
}
903
904
int pdfi_free_font_truetype(pdf_obj *font)
905
26.1k
{
906
26.1k
    pdf_font_truetype *ttfont = (pdf_font_truetype *)font;
907
908
26.1k
    if (ttfont->pfont)
909
26.1k
        gs_free_object(OBJ_MEMORY(ttfont), ttfont->pfont, "Free TrueType gs_font");
910
911
26.1k
    if (ttfont->Widths)
912
24.6k
        gs_free_object(OBJ_MEMORY(ttfont), ttfont->Widths, "Free TrueType font Widths array");
913
914
26.1k
    pdfi_countdown(ttfont->sfnt);
915
26.1k
    pdfi_countdown(ttfont->FontDescriptor);
916
26.1k
    pdfi_countdown(ttfont->Encoding);
917
26.1k
    pdfi_countdown(ttfont->BaseFont);
918
26.1k
    pdfi_countdown(ttfont->PDF_font);
919
26.1k
    pdfi_countdown(ttfont->ToUnicode);
920
26.1k
    pdfi_countdown(ttfont->filename);
921
26.1k
    pdfi_countdown(ttfont->post);
922
26.1k
    pdfi_countdown(ttfont->copyright);
923
26.1k
    pdfi_countdown(ttfont->notice);
924
26.1k
    pdfi_countdown(ttfont->fullname);
925
26.1k
    pdfi_countdown(ttfont->familyname);
926
927
26.1k
    gs_free_object(OBJ_MEMORY(ttfont), ttfont, "Free TrueType font");
928
929
26.1k
    return 0;
930
26.1k
}