summaryrefslogtreecommitdiff
path: root/pdf/pdf_font0.c
blob: b8901170c34114a02de2e3373c0af8d374a401d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/* Copyright (C) 2019-2021 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
*/

/* Include this first so that we don't get a macro redefnition of 'offsetof' */
#include "pdf_int.h"

#include "strmio.h"
#include "stream.h"

/* code for type 0 (CID) font handling */
#include "gxfont.h"
#include "gxfont0.h"

#include "pdf_int.h"
#include "pdf_font.h"
#include "pdf_font0.h"
#include "pdf_font1C.h"
#include "pdf_font_types.h"
#include "pdf_stack.h"
#include "pdf_array.h"
#include "pdf_dict.h"
#include "pdf_file.h"
#include "pdf_cmap.h"
#include "pdf_deref.h"

extern pdfi_cid_decoding_t *pdfi_cid_decoding_list[];


static int pdfi_font0_find_ordering_to_unicode(const char *reg, const int reglen, const char *ord, const int ordlen, pdfi_cid_decoding_t **decoding)
{
    int i;
    int code = gs_error_undefined;
    *decoding = NULL;
    /* This only makes sense for Adobe orderings */
    if (reglen == 5 && !memcmp(reg, "Adobe", 5)) {
        for (i = 0; pdfi_cid_decoding_list[i] != NULL; i++) {
            if (strlen(pdfi_cid_decoding_list[i]->s_order) == ordlen &&
                !memcmp(pdfi_cid_decoding_list[i]->s_order, ord, ordlen)) {
                *decoding = pdfi_cid_decoding_list[i];
                code = 0;
                break;
            }
        }
    }
    return code;
}

typedef enum
{
    decfont_type_cidtype0 = 1,
    decfont_type_cidtype0cff = 2,
    decfont_type_cidtype2 = 42
} decfont_type;

static decfont_type pdfi_fonttype_picker(byte *buf, int64_t buflen)
{
#define MAKEMAGIC(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))

    if (buflen >= 4) {
        if (MAKEMAGIC(buf[0], buf[1], buf[2], buf[3]) == MAKEMAGIC(0, 1, 0, 0)) {
            return decfont_type_cidtype2;
        }
        else if (MAKEMAGIC(buf[0], buf[1], buf[2], buf[3]) == MAKEMAGIC('O', 'T', 'T', 'O')) {
            return decfont_type_cidtype0cff; /* OTTO will end up as CFF */
        }
        else if (MAKEMAGIC(buf[0], buf[1], buf[2], 0) == MAKEMAGIC('%', '!', 'P', 0)) {
            return decfont_type_cidtype0; /* pfa */
        }
        else if (MAKEMAGIC(buf[0], buf[1], buf[2], buf[3]) == MAKEMAGIC(1, 0, 4, 1)) {
            return decfont_type_cidtype0cff; /* 1C/CFF */
        }
        else if (MAKEMAGIC(buf[0], buf[1], 0, 0) == MAKEMAGIC(128, 1, 0, 0)) {
            return decfont_type_cidtype0; /* pfb */
        }
    }
    return 0;

#undef MAKEMAGIC
}

/* Call with a CIDFont name to try to find the CIDFont on disk
   call if with ffname NULL to load the default fallback CIDFont
   substitue
   Currently only loads subsitute - DroidSansFallback
 */
static int
pdfi_open_CIDFont_file(pdf_context * ctx, const char *ffname, const int namelen, byte ** buf, int64_t * buflen)
{
    int code = gs_error_invalidfont;
    if (ffname != NULL) {
        code = gs_error_invalidfont;
    }
    else {
        char fontfname[gp_file_name_sizeof];
        const char *romfsprefix = "%rom%Resource/CIDFSubst/";
        const int romfsprefixlen = strlen(romfsprefix);
        const char *defcidfallack = "DroidSansFallback.ttf";
        const int defcidfallacklen = strlen(defcidfallack);
        stream *s;
        code = 0;

        memcpy(fontfname, romfsprefix, romfsprefixlen);
        memcpy(fontfname + romfsprefixlen, defcidfallack, defcidfallacklen);
        fontfname[romfsprefixlen + defcidfallacklen] = '\0';

        s = sfopen(fontfname, "r", ctx->memory);
        if (s == NULL) {
            code = gs_note_error(gs_error_invalidfont);
        }
        else {
            sfseek(s, 0, SEEK_END);
            *buflen = sftell(s);
            sfseek(s, 0, SEEK_SET);
            *buf = gs_alloc_bytes(ctx->memory, *buflen, "pdfi_open_CIDFont_file(buf)");
            if (*buf != NULL) {
                sfread(*buf, 1, *buflen, s);
            }
            else {
                code = gs_note_error(gs_error_VMerror);
            }
            sfclose(s);
        }
    }
    return code;
}

static int
pdfi_font0_glyph_name(gs_font *font, gs_glyph index, gs_const_string *pstr)
{
    return_error(gs_error_rangecheck);
}

static int
pdfi_font0_map_glyph_to_unicode(gs_font *font, gs_glyph glyph, int ch, ushort *u, unsigned int length)
{
    gs_glyph cc = glyph < GS_MIN_CID_GLYPH ? glyph : glyph - GS_MIN_CID_GLYPH;
    pdf_font_type0 *pt0font = (pdf_font_type0 *)font->client_data;
    int code = gs_error_undefined, i;
    uchar *unicode_return = (uchar *)u;
    pdf_cidfont_type2 *decfont;
    pdf_cmap *tounicode = (pdf_cmap *)pt0font->ToUnicode;

    code = pdfi_array_get(pt0font->ctx, pt0font->DescendantFonts, 0, (pdf_obj **)&decfont);
    if (code < 0 || decfont->type != PDF_FONT)
        return gs_error_undefined;

    code = gs_error_undefined;
    /* Favour the ToUnicode if one exists */
    if (tounicode) {
        int l = 0;
        gs_cmap_lookups_enum_t lenum;
        gs_cmap_lookups_enum_init((const gs_cmap_t *)tounicode->gscmap, 0, &lenum);
        while (l == 0 && (code = gs_cmap_enum_next_lookup(font->memory, &lenum)) == 0) {
            gs_cmap_lookups_enum_t counter = lenum;
            while (l == 0 && (code = gs_cmap_enum_next_entry(&counter) == 0)) {
                if (counter.entry.value_type == CODE_VALUE_CID) {
                    unsigned int v = 0;
                    for (i = 0; i < counter.entry.key_size; i++) {
                        v |= (counter.entry.key[0][counter.entry.key_size - i - 1]) << (i * 8);
                    }
                    if (ch == v) {
                        if (counter.entry.value.size == 1) {
                            l = 2;
                            if (unicode_return != NULL && length >= l) {
                                unicode_return[0] = counter.entry.value.data[0];
                                unicode_return[1] = counter.entry.value.data[1];
                            }
                        }
                        else if (counter.entry.value.size == 2) {
                            l = 2;
                            if (unicode_return != NULL && length >= l) {
                                unicode_return[0] = counter.entry.value.data[0];
                                unicode_return[1] = counter.entry.value.data[1];
                            }
                        }
                        else if (counter.entry.value.size == 3) {
                            l = 4;
                            if (unicode_return != NULL && length >= l) {
                                unicode_return[0] = counter.entry.value.data[0];
                                unicode_return[1] = counter.entry.value.data[1];
                                unicode_return[2] = counter.entry.value.data[2];
                                unicode_return[3] = 0;
                            }
                        }
                        else {
                            l = 4;
                            if (unicode_return != NULL && length >= l) {
                                unicode_return[0] = counter.entry.value.data[0];
                                unicode_return[1] = counter.entry.value.data[1];
                                unicode_return[2] = counter.entry.value.data[1];
                                unicode_return[3] = counter.entry.value.data[3];
                            }
                        }
                    }
                }
                else {
                    l = 0;
                }
            }
        }
        if (l > 0)
            code = l;
        else
            code = gs_error_undefined;
    }

    if (code == gs_error_undefined && pt0font->decoding) {
        const int *n;

        if (cc / 256 < pt0font->decoding->nranges) {
            n = (const int *)pt0font->decoding->ranges[cc / 256][cc % 256];
            for (i = 0; i < pt0font->decoding->val_sizes; i++) {
                unsigned int cmapcc;
                if (n[i] == -1)
                    break;
                cc = n[i];
                cmapcc = (unsigned int)cc;
                if (decfont->pdfi_font_type == e_pdf_cidfont_type2)
                    code = pdfi_fapi_check_cmap_for_GID((gs_font *)decfont->pfont, &cmapcc);
                else
                    code = 0;
                if (code >= 0 && cmapcc != 0){
                    code = 0;
                    break;
                }
            }
            /* If it's a TTF derived CIDFont, we prefer a code point supported by the cmap table
               but if not, use the first available one
             */
            if (code < 0 && n[0] != -1) {
                cc = n[0];
                code = 0;
            }
        }
        if (code >= 0) {
            if (cc > 65535) {
                code = 4;
                if (unicode_return != NULL && length >= code) {
                    unicode_return[0] = (cc & 0xFF000000)>> 24;
                    unicode_return[1] = (cc & 0x00FF0000) >> 16;
                    unicode_return[2] = (cc & 0x0000FF00) >> 8;
                    unicode_return[3] = (cc & 0x000000FF);
                }
            }
            else {
                code = 2;
                if (unicode_return != NULL && length >= code) {
                    unicode_return[0] = (cc & 0x0000FF00) >> 8;
                    unicode_return[1] = (cc & 0x000000FF);
                }
            }
        }
    }
    return (code < 0 ? 0 : code);
}

int pdfi_read_type0_font(pdf_context *ctx, pdf_dict *font_dict, pdf_dict *stream_dict, pdf_dict *page_dict, gs_font **pfont)
{
    int code, nlen;
    pdf_obj *cmap = NULL;
    pdf_cmap *pcmap = NULL;
    pdf_array *arr = NULL;
    pdf_dict *decfont = NULL; /* there can only be one */
    pdf_name *n = NULL;
    pdf_obj *basefont = NULL;
    pdf_obj *tounicode = NULL;
    const char *ffstrings[] = {"FontFile", "FontFile2", "FontFile3"};
    int ff;
    pdf_dict *fontdesc = NULL;
    pdf_stream *ffile = NULL;
    byte *buf;
    int64_t buflen;
    pdf_font *descpfont = NULL;
    pdf_font_type0 *pdft0 = NULL;
    gs_font_type0 *pfont0 = NULL;
    pdfi_cid_decoding_t *dec = NULL;
    bool descendant_substitute = false;

    code = pdfi_dict_get(ctx, font_dict, "Encoding", &cmap);
    if (code < 0) goto error;

    code = pdfi_read_cmap(ctx, cmap, &pcmap);
    pdfi_countdown(cmap);
    if (code < 0) goto error;

    code = pdfi_dict_get(ctx, font_dict, "DescendantFonts", (pdf_obj **)&arr);
    if (code < 0) goto error;

    if (arr->type != PDF_ARRAY || arr->size != 1) {
        code = gs_note_error(gs_error_invalidfont);
        goto error;
    }
    code = pdfi_array_get(ctx, arr, 0, (pdf_obj **)&decfont);
    pdfi_countdown(arr);
    arr = NULL;
    if (code < 0) goto error;
    if (decfont->type != PDF_DICT) {
        code = gs_note_error(gs_error_invalidfont);
        goto error;
    }
    code = pdfi_dict_get(ctx, (pdf_dict *)decfont, "Type", (pdf_obj **)&n);
    if (code < 0) goto error;
    if (n->type != PDF_NAME || n->length != 4 || memcmp(n->data, "Font", 4) != 0) {
        pdfi_countdown(n);
        code = gs_note_error(gs_error_invalidfont);
        goto error;
    }
    pdfi_countdown(n);

#if 0
    code = pdfi_dict_get(ctx, (pdf_dict *)decfont, "Subtype", (pdf_obj **)&n);
    if (code < 0)
        goto error;

    if (n->type != PDF_NAME || n->length != 12 || memcmp(n->data, "CIDFontType", 11) != 0) {
        pdfi_countdown(n);
        code = gs_note_error(gs_error_invalidfont);
        goto error;
    }
    /* cidftype is ignored for now, but we may need to know it when
       subsitutions are allowed
     */
    cidftype = n->data[11] - 48;

    pdfi_countdown(n);
#endif

    code = pdfi_dict_get(ctx, font_dict, "BaseFont", (pdf_obj **)&basefont);
    if (code < 0) {
        basefont = NULL;
    }

    code = pdfi_dict_get(ctx, font_dict, "ToUnicode", (pdf_obj **)&tounicode);
    if (code >= 0 && tounicode->type == PDF_STREAM) {
        pdf_cmap *tu = NULL;
        code = pdfi_read_cmap(ctx, tounicode, &tu);
        pdfi_countdown(tounicode);
        tounicode = (pdf_obj *)tu;
    }
    if (code < 0 || (tounicode != NULL && tounicode->type != PDF_CMAP)) {
        pdfi_countdown(tounicode);
        tounicode = NULL;
    }

    code = pdfi_dict_get(ctx, decfont, "FontDescriptor", (pdf_obj **)&fontdesc);
    if (code < 0)
        goto error;

    if (fontdesc->type != PDF_DICT) {
        code = gs_note_error(gs_error_invalidfont);
        goto error;
    }
    buf = NULL;
    for (ff = 0;ff < (sizeof(ffstrings) / sizeof(ffstrings[0])); ff++) {
        code = pdfi_dict_get(ctx, fontdesc, ffstrings[ff], (pdf_obj **)&ffile);
        if (code >= 0) break;
    }

    if (code < 0) { /* Try to load from disk, or default fallback */
        char *ffn = NULL;
        int ffnlen = 0;
        pdf_obj *csi = NULL;
        pdf_string *reg = NULL, *ord = NULL;
        char *r = NULL, *o = NULL;
        int rlen, olen;

        if (basefont->type == PDF_NAME || basefont->type == PDF_STRING) {
            ffn = (char *)((pdf_name *)basefont)->data;
            ffnlen = ((pdf_name *)basefont)->length;
        }
        code = pdfi_open_CIDFont_file(ctx, ffn, ffnlen, &buf, &buflen);
        if (code == gs_error_invalidfont) {
            code = pdfi_open_CIDFont_file(ctx, NULL, 0, &buf, &buflen);
            if (code >= 0) {
                descendant_substitute = true;
                code = pdfi_dict_get(ctx, decfont, "CIDSystemInfo", (pdf_obj **)&csi);
                if (code >= 0) {
                    (void)pdfi_dict_get(ctx, (pdf_dict *)csi, "Registry", (pdf_obj **)&reg);
                    (void)pdfi_dict_get(ctx, (pdf_dict *)csi, "Ordering", (pdf_obj **)&ord);
                    if (reg != NULL && ord != NULL) {
                        r = (char *)reg->data;
                        rlen = reg->length;
                        o = (char *)ord->data;
                        olen = ord->length;
                    }
                    pdfi_countdown(csi);
                    pdfi_countdown(reg);
                    pdfi_countdown(ord);
                }
                if (r == NULL || o == NULL) {
                    r = (char *)pcmap->csi_reg.data;
                    rlen = pcmap->csi_reg.size;
                    o = (char *)pcmap->csi_ord.data;
                    olen = pcmap->csi_ord.size;
                }
                (void)pdfi_font0_find_ordering_to_unicode(r, rlen, o, olen, &dec);
            }
        }
        if (code < 0)
            goto error;
    }

    if (!buf) {
        if (ffile->type != PDF_STREAM) {
            code = gs_note_error(gs_error_invalidfont);
            goto error;
        }
        pdfi_countdown(fontdesc);
        fontdesc = NULL;

        /* We are supposed to be able to discern the underlying font type
         * from FontFile, FontFile2, or FontFile3 + Subtype of the stream.
         * Typical PDF, none of these prove trustworthy, so we have to
         * create the memory buffer here, so we can get a font type from
         * the first few bytes of the font data.
         * Owndership of the buffer is passed to the font reading function.
         */
        code = pdfi_stream_to_buffer(ctx, ffile, &buf, &buflen);
        if (code < 0) {
            pdfi_countdown(ffile);
            ffile = NULL;
            goto error;
        }
    }
    if ((code = pdfi_fonttype_picker(buf, buflen)) == 0) {
        pdf_name *subtype = NULL;
        code = pdfi_dict_get(ctx, ffile->stream_dict, "Subtype", (pdf_obj **)&subtype);
        if (code < 0 || subtype->type != PDF_NAME) {
            /* Corrupt stream header, no subtype, <shrug> try Type 1 */
            code = decfont_type_cidtype0;
        }
        else {
            if (subtype->length >= 13 && !memcmp(subtype->data, "CIDFontType0C", 13)) {
                code = decfont_type_cidtype0cff;
            }
            else {
                /* <shrug> try Type 1 */
                code = decfont_type_cidtype0;
            }
        }
        pdfi_countdown(subtype);
    }
    pdfi_countdown(ffile);
    ffile = NULL;
    switch (code) {
        case decfont_type_cidtype2:
          code = pdfi_read_cidtype2_font(ctx, decfont, buf, buflen, &descpfont);
          break;
        case decfont_type_cidtype0cff:
        {
            code = pdfi_read_cff_font(ctx, decfont, buf, buflen, &descpfont, true);
            break;
        }
        case decfont_type_cidtype0:
        default:
          code = pdfi_read_cidtype0_font(ctx, decfont, buf, buflen, &descpfont);
          break;
    }
    /* reference is now owned by the descendent font created above */
    pdfi_countdown(decfont);
    decfont = NULL;
    if (code < 0) {
        code = gs_note_error(gs_error_invalidfont);
        goto error;
    }
    /* If we're got this far, we have a CMap and a descendant font, let's make the Type 0 */
    pdft0 = (pdf_font_type0 *)gs_alloc_bytes(ctx->memory, sizeof(pdf_font_type0), "pdfi (type0 pdf_font)");
    if (pdft0 == NULL) {
        code = gs_note_error(gs_error_VMerror);
        goto error;
    }
    code = pdfi_array_alloc(ctx, 1, &arr);
    if (code < 0) {
        gs_free_object(ctx->memory, pdft0, "pdfi_read_type0_font(pdft0)");
        goto error;
    }
    arr->refcnt = 1;
    code = pdfi_array_put(ctx, arr, 0, (pdf_obj *)descpfont);
    if (code < 0) {
        gs_free_object(ctx->memory, pdft0, "pdfi_read_type0_font(pdft0)");
        goto error;
    }

    pdft0->type = PDF_FONT;
    pdft0->pdfi_font_type = e_pdf_font_type0;
    pdft0->ctx = ctx;
#if REFCNT_DEBUG
    pdft0->UID = ctx->UID++;
    dmprintf2(ctx->memory, "Allocated object of type %c with UID %"PRIi64"\n", pdft0->type, pdft0->UID);
#endif
    pdft0->refcnt = 1;
    pdft0->object_num = font_dict->object_num;
    pdft0->generation_num = font_dict->generation_num;
    pdft0->indirect_num = font_dict->indirect_num;
    pdft0->indirect_gen = font_dict->indirect_gen;
    pdft0->Encoding = (pdf_obj *)pcmap;
    pdft0->ToUnicode = tounicode;
    pdft0->DescendantFonts = arr;
    pdft0->descendant_substitute = descendant_substitute;
    pdft0->PDF_font = font_dict;
    pdfi_countup(font_dict);
    pdft0->FontDescriptor = NULL;
    pdft0->BaseFont = basefont;
    pdft0->decoding = dec;

    /* Ownership transferred to pdft0, if we jump to error
     * these will now be freed by counting down pdft0.
     */
    tounicode = NULL;
    arr = NULL;
    basefont = NULL;

    pdft0->pfont = NULL; /* In case we error out */

    pfont0 = (gs_font_type0 *)gs_alloc_struct(ctx->memory, gs_font, &st_gs_font_type0, "pdfi gs type 0 font");
    if (pfont0 == NULL) {
        gs_free_object(ctx->memory, pdft0, "pdfi_read_type0_font(pdft0)");
        code = gs_note_error(gs_error_VMerror);
        goto error;
    }
    gs_make_identity(&pfont0->orig_FontMatrix);
    gs_make_identity(&pfont0->FontMatrix);
    pfont0->next = pfont0->prev = 0;
    pfont0->memory = ctx->memory;
    pfont0->dir = ctx->font_dir;
    pfont0->is_resource = false;
    gs_notify_init(&pfont0->notify_list, ctx->memory);
    pfont0->id = gs_next_ids(ctx->memory, 1);
    pfont0->base = (gs_font *) pfont0;
    pfont0->client_data = pdft0;
    pfont0->WMode = pcmap->wmode;
    pfont0->FontType = ft_composite;
    pfont0->PaintType = 0;
    pfont0->StrokeWidth = 0;
    pfont0->is_cached = 0;
    if (pdft0->BaseFont != NULL) {
        pdf_name *nobj = (pdf_name *)pdft0->BaseFont;
        nlen = nobj->length > gs_font_name_max ? gs_font_name_max : nobj->length;

        memcpy(pfont0->key_name.chars, nobj->data, nlen);
        pfont0->key_name.chars[nlen] = 0;
        pfont0->key_name.size = nlen;
        memcpy(pfont0->font_name.chars, nobj->data, nlen);
        pfont0->font_name.chars[nlen] = 0;
        pfont0->font_name.size = nlen;
    }
    else {
        nlen = descpfont->pfont->key_name.size > gs_font_name_max ? gs_font_name_max : descpfont->pfont->key_name.size;

        memcpy(pfont0->key_name.chars, descpfont->pfont->key_name.chars, nlen);
        pfont0->key_name.chars[nlen] = 0;
        pfont0->key_name.size = nlen;
        memcpy(pfont0->font_name.chars, descpfont->pfont->font_name.chars, nlen);
        pfont0->font_name.chars[nlen] = 0;
        pfont0->font_name.size = nlen;
    }
    if (pcmap->name.size > 0) {
        if (pfont0->key_name.size + pcmap->name.size + 1 < gs_font_name_max) {
            memcpy(pfont0->key_name.chars + pfont0->key_name.size, "-", 1);
            memcpy(pfont0->key_name.chars + pfont0->key_name.size + 1, pcmap->name.data, pcmap->name.size);
            pfont0->key_name.size += pcmap->name.size + 1;
            pfont0->key_name.chars[pfont0->key_name.size] = 0;
            memcpy(pfont0->font_name.chars + pfont0->font_name.size, "-", 1);
            memcpy(pfont0->font_name.chars + pfont0->font_name.size + 1, pcmap->name.data, pcmap->name.size);
            pfont0->font_name.size += pcmap->name.size + 1;
            pfont0->font_name.chars[pfont0->key_name.size] = 0;
        }
    }
    pfont0->procs.define_font = gs_no_define_font;
    pfont0->procs.make_font = gs_no_make_font;
    pfont0->procs.font_info = gs_default_font_info;
    pfont0->procs.same_font = gs_default_same_font;
    pfont0->procs.encode_char = pdfi_encode_char;
    pfont0->procs.decode_glyph = pdfi_font0_map_glyph_to_unicode;
    pfont0->procs.enumerate_glyph = gs_no_enumerate_glyph;
    pfont0->procs.glyph_info = gs_default_glyph_info;
    pfont0->procs.glyph_outline = gs_no_glyph_outline;
    pfont0->procs.glyph_name = pdfi_font0_glyph_name;
    pfont0->procs.init_fstack = gs_type0_init_fstack;
    pfont0->procs.next_char_glyph = gs_type0_next_char_glyph;
    pfont0->procs.build_char = gs_no_build_char;

    pfont0->data.FMapType = fmap_CMap;
    pfont0->data.EscChar = 0xff;
    pfont0->data.ShiftIn = 0x0f;
    pfont0->data.SubsVector.data = NULL;
    pfont0->data.SubsVector.size = 0;
    pfont0->data.subs_size = pfont0->data.subs_width = 0;

    pfont0->data.Encoding = (uint *)gs_alloc_bytes(ctx->memory, sizeof(uint), "pdfi_read_type0_font Encoding");
    if (pfont0->data.Encoding == NULL) {
        gs_free_object(ctx->memory, pfont0, "pdfi_read_type0_font(pfont0)");
        code = gs_note_error(gs_error_VMerror);
        goto error;
    }
    *pfont0->data.Encoding = 0;

    pfont0->data.encoding_size = 1;
    pfont0->data.FDepVector = (gs_font **)gs_alloc_bytes(ctx->memory, sizeof(gs_font *), "pdfi_read_type0_font FDepVector");
    if (pfont0->data.FDepVector == NULL) {
        /* We transferred ownership of pcmap to pfont0 above, but we didn't null the pointer
         * so we could keep using it. We must NULL it out before returning an error to prevent
         * reference counting problems.
         */
        pcmap = NULL;
        gs_free_object(ctx->memory, pfont0, "pdfi_read_type0_font(pfont0)");
        code = gs_note_error(gs_error_VMerror);
        goto error;
    }
    *pfont0->data.FDepVector = (gs_font *)descpfont->pfont;
    pdfi_countdown(descpfont);
    descpfont = NULL;
    pfont0->data.fdep_size = 1;
    pfont0->data.CMap = (gs_cmap_t *)pcmap->gscmap;

    /* NULL he pointer to prevent any reference counting problems, ownership was
     * transferred to pfont0, but we maintained the pointer for easy access until this
     * point.
     */
    pcmap = NULL;

    pdft0->pfont = (gs_font_base *)pfont0;

    code = gs_definefont(ctx->font_dir, (gs_font *)pdft0->pfont);
    if (code < 0) {
        gs_free_object(ctx->memory, pfont0, "pdfi_read_type0_font(pfont0)");
        code = gs_note_error(gs_error_VMerror);
        goto error;
    }

    /* object_num can be zero if the dictionary was defined inline */
    if (pdft0->object_num != 0) {
        code = replace_cache_entry(ctx, (pdf_obj *)pdft0);
        if (code < 0) {
            gs_free_object(ctx->memory, pfont0, "pdfi_read_type0_font(pfont0)");
            code = gs_note_error(gs_error_VMerror);
            goto error;
        }
    }

    *pfont = (gs_font *)pdft0->pfont;
    return 0;

error:
    pdfi_countdown(arr);
    pdfi_countdown(pcmap);
    pdfi_countdown(tounicode);
    pdfi_countdown(basefont);
    pdfi_countdown(decfont);
    pdfi_countdown(fontdesc);
    pdfi_countdown(ffile);
    pdfi_countdown(descpfont);
    pdfi_countdown(pdft0);

    return code;
}

int
pdfi_free_font_type0(pdf_obj *font)
{
    pdf_font_type0 *pdft0 = (pdf_font_type0 *)font;
    gs_font_type0 *pfont0 = (gs_font_type0 *)pdft0->pfont;
    pdfi_countdown(pdft0->PDF_font);
    pdfi_countdown(pdft0->BaseFont);
    pdfi_countdown(pdft0->FontDescriptor);
    pdfi_countdown(pdft0->Encoding);
    pdfi_countdown(pdft0->DescendantFonts);
    pdfi_countdown(pdft0->ToUnicode);
    gs_free_object(OBJ_MEMORY(pdft0), pfont0->data.Encoding, "pdfi_free_font_type0(data.Encoding)");
    /* We shouldn't need to free the fonts in the FDepVector, that should happen
        with DescendantFonts above.
     */
    gs_free_object(OBJ_MEMORY(pdft0), pfont0->data.FDepVector, "pdfi_free_font_type0(data.FDepVector)");
    gs_free_object(OBJ_MEMORY(pdft0), pfont0, "pdfi_free_font_type0(pfont0)");
    gs_free_object(OBJ_MEMORY(pdft0), pdft0, "pdfi_free_font_type0(pdft0)");

    return 0;
}