summaryrefslogtreecommitdiff
path: root/pdf/pdf_font11.c
blob: 9b1115d99b0a3f0139dee0a5d82df67c7ec88ba6 (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
/* Copyright (C) 2020-2023 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.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/

/* code for CIDFontType2/Type 9 font handling */
/* CIDFonts with Truetype outlines */

#include "pdf_int.h"
#include "pdf_font.h"
#include "pdf_font0.h"
#include "pdf_fontTT.h"
#include "pdf_font_types.h"
#include "pdf_stack.h"
#include "pdf_file.h"
#include "pdf_dict.h"
#include "pdf_array.h"
#include "pdf_deref.h"
#include "gxfont42.h"
#include "gxfcid.h"
#include "gsutil.h"        /* For gs_next_ids() */

static int pdfi_cidtype2_string_proc(gs_font_type42 * pfont, ulong offset, uint length,
                  const byte ** pdata)
{
    pdf_cidfont_type2 *ttfont = (pdf_cidfont_type2 *)pfont->client_data;
    int code = 0;

    if (offset + length > ttfont->sfnt->length) {
        *pdata = NULL;
        code = gs_note_error(gs_error_invalidfont);
    }
    else {
        *pdata = ttfont->sfnt->data + offset;
    }
    return code;
}

static int pdfi_cidtype2_CIDMap_proc(gs_font_cid2 *pfont, gs_glyph glyph)
{
    pdf_cidfont_type2 *pdffont11 = (pdf_cidfont_type2 *)pfont->client_data;
    uint gid = glyph - GS_MIN_CID_GLYPH;
    int code = 0;

    if (pdffont11->substitute == true) {
        unsigned int ucc = 0;
        int code = pfont->procs.decode_glyph((gs_font *)pfont, glyph, -1, NULL, 0);
        if (code == 2) {
            uchar sccode[2] = {0};
            (void)pfont->procs.decode_glyph((gs_font *)pfont, glyph, -1, (ushort *)&sccode, 2);
            ucc = (sccode[0] << 8) + sccode[1];
        }
        else if (code == 4) {
            uchar iccode[4] = {0};
            (void)pfont->procs.decode_glyph((gs_font *)pfont, glyph, -1, (ushort *)&iccode, 2);
            ucc = (iccode[0] << 24) + (iccode[1] << 16) + (iccode[2] << 8) + iccode[3];

        }
        if (code == 2 || code == 4) {
            code = pdfi_fapi_check_cmap_for_GID((gs_font *)pfont, (unsigned int)ucc, &gid);
            if (code < 0 || gid == 0)
                gid = glyph - GS_MIN_CID_GLYPH;
        }
    }

    if (code == 0 && pdffont11->cidtogidmap != NULL && pdffont11->cidtogidmap->length > (gid << 1) + 1) {
       gid = pdffont11->cidtogidmap->data[gid << 1] << 8 | pdffont11->cidtogidmap->data[(gid << 1) + 1];
    }

    return (int)gid;
}

static uint pdfi_cidtype2_get_glyph_index(gs_font_type42 *pfont, gs_glyph glyph)
{
    pdf_cidfont_type2 *pdffont11 = (pdf_cidfont_type2 *)pfont->client_data;
    uint gid = glyph - GS_MIN_CID_GLYPH;
    int code = 0;

    if (glyph < GS_MIN_CID_GLYPH) {
        gid = 0;
    }
    else {
        if (glyph < GS_MIN_GLYPH_INDEX) {
            if (pdffont11->substitute == true) {
                unsigned int ucc = 0;
                code = pfont->procs.decode_glyph((gs_font *)pfont, glyph, -1, NULL, 0);
                if (code == 2) {
                    uchar sccode[2] = {0};
                    (void)pfont->procs.decode_glyph((gs_font *)pfont, glyph, -1, (ushort *)&sccode, 2);
                    ucc = (sccode[0] << 8) + sccode[1];
                }
                else if (code == 4) {
                    uchar iccode[4] = {0};
                    (void)pfont->procs.decode_glyph((gs_font *)pfont, glyph, -1, (ushort *)&iccode, 2);
                    ucc = (iccode[0] << 24) + (iccode[1] << 16) + (iccode[2] << 8) + iccode[3];

                }
                if (code == 2 || code == 4) {
                    code = pdfi_fapi_check_cmap_for_GID((gs_font *)pfont, (unsigned int)ucc, &gid);
                    if (code < 0 || gid == 0)
                        gid = glyph - GS_MIN_CID_GLYPH;
                }
            }

            if (code == 0 && pdffont11->cidtogidmap != NULL && pdffont11->cidtogidmap->length > (gid << 1) + 1) {
               gid = pdffont11->cidtogidmap->data[gid << 1] << 8 | pdffont11->cidtogidmap->data[(gid << 1) + 1];
            }
        }
    }

    return gid;
}

static int
pdfi_cidtype2_glyph_info(gs_font *font, gs_glyph glyph, const gs_matrix *pmat,
                     int members, gs_glyph_info_t *info)
{
    int code;
    pdf_cidfont_type2 *pdffont11 = (pdf_cidfont_type2 *)font->client_data;
    int submembers = members & ~(GLYPH_INFO_WIDTHS | GLYPH_INFO_VVECTOR0 | GLYPH_INFO_VVECTOR1 | GLYPH_INFO_CDEVPROC);

    /* Call with the values we should get below removed from the "members" request */
    code = (*pdffont11->orig_glyph_info)(font, glyph, pmat, submembers, info);

    if (code >= 0 && (members & GLYPH_INFO_WIDTHS) != 0
      && glyph > GS_MIN_CID_GLYPH
      && glyph < GS_MIN_GLYPH_INDEX) {
        double widths[6] = {0};
        code = pdfi_get_cidfont_glyph_metrics(font, (glyph - GS_MIN_CID_GLYPH), widths, true);
        if (code < 0) {
             /* If we couldn't get values back from W/W2, give up, and fill everything in from glyph_info */
             code = (*pdffont11->orig_glyph_info)(font, glyph, pmat, members, info);
        }
        else {
            if (pmat == NULL) {
                info->width[0].x = widths[GLYPH_W0_WIDTH_INDEX] / 1000.0;
                info->width[0].y = widths[GLYPH_W0_HEIGHT_INDEX] / 1000.0;
            }
            else {
                code = gs_point_transform(widths[GLYPH_W0_WIDTH_INDEX] / 1000.0, widths[GLYPH_W0_HEIGHT_INDEX] / 1000.0, pmat, &info->width[0]);
                if (code < 0)
                    return code;
            }
            info->members |= GLYPH_INFO_WIDTH0;

            if ((members & GLYPH_INFO_WIDTH1) != 0
                && (widths[GLYPH_W1_WIDTH_INDEX] != 0
                || widths[GLYPH_W1_HEIGHT_INDEX] != 0)) {
                if (pmat == NULL) {
                    info->width[1].x = widths[GLYPH_W1_WIDTH_INDEX] / 1000.0;
                    info->width[1].y = widths[GLYPH_W1_HEIGHT_INDEX] / 1000.0;
                }
                else {
                    code = gs_point_transform(widths[GLYPH_W1_WIDTH_INDEX] / 1000.0, widths[GLYPH_W1_HEIGHT_INDEX] / 1000.0, pmat, &info->width[1]);
                    if (code < 0)
                        return code;
                }
                info->members |= GLYPH_INFO_WIDTH1;
            }
            if ((members & GLYPH_INFO_VVECTOR1) != 0) {
                if (pmat == NULL) {
                    info->v.x = widths[GLYPH_W1_V_X_INDEX] / 1000.0;
                    info->v.y = widths[GLYPH_W1_V_Y_INDEX] / 1000.0;
                }
                else {
                    code = gs_point_transform(widths[GLYPH_W1_V_X_INDEX] / 1000.0, widths[GLYPH_W1_V_Y_INDEX] / 1000.0, pmat, &info->v);
                    if (code < 0)
                        return code;
                }
                info->members |= GLYPH_INFO_VVECTOR1;
            }
        }
    }

    return code;
}

static int
pdfi_cidtype2_enumerate_glyph(gs_font *font, int *pindex,
                         gs_glyph_space_t glyph_space, gs_glyph *pglyph)
{
    int code = 0;
    gs_font_cid2 *cid2 = (gs_font_cid2 *)font;
    pdf_cidfont_type2 *pdffont11 = (pdf_cidfont_type2 *)font->client_data;
    *pglyph = 0;

    if (*pindex <= 0)
        *pindex = 0;

    if (pdffont11->cidtogidmap != NULL && pdffont11->cidtogidmap->length > 0) {
        do {
            *pglyph = pdffont11->cidtogidmap->data[(*pindex) << 1] << 8 | pdffont11->cidtogidmap->data[((*pindex) << 1) + 1];
            (*pindex)++;
            if (*pglyph == 0 && *pindex == 1) /* notdef - special case */
                break;
        } while (*pglyph == 0 && ((*pindex) << 1) < pdffont11->cidtogidmap->length);

        if (((*pindex) << 1) >= pdffont11->cidtogidmap->length) {
            *pindex = 0;
        }
        else {
            if (*pglyph != 0 || (*pglyph == 0 && *pindex == 1)) {
                if (glyph_space == GLYPH_SPACE_INDEX) {
                    *pglyph += GS_MIN_GLYPH_INDEX;
                }
                else {
                    *pglyph = (*pindex) + GS_MIN_CID_GLYPH;
                }
            }
        }
    }
    else {
        if (*pindex < cid2->cidata.common.CIDCount) {
           if (glyph_space == GLYPH_SPACE_INDEX) {
               *pglyph = *pindex + GS_MIN_GLYPH_INDEX;
           }
           else {
               *pglyph = (*pindex) + GS_MIN_CID_GLYPH;
           }
        }
        else {
            *pindex = 0;
        }
    }

    return code;
}

static int
pdfi_alloc_cidtype2_font(pdf_context *ctx, pdf_cidfont_type2 **font, bool is_cid)
{
    pdf_cidfont_type2 *ttfont = NULL;
    gs_font_cid2 *pfont = NULL;

    ttfont = (pdf_cidfont_type2 *)gs_alloc_bytes(ctx->memory, sizeof(pdf_cidfont_type2), "pdfi (cidtype2 pdf_font)");
    if (ttfont == NULL)
        return_error(gs_error_VMerror);

    memset(ttfont, 0x00, sizeof(pdf_cidfont_type2));
    ttfont->type = PDF_FONT;
    ttfont->ctx = ctx;
    ttfont->pdfi_font_type = e_pdf_cidfont_type2;

#if REFCNT_DEBUG
    ttfont->UID = ctx->UID++;
    dmprintf2(ctx->memory, "Allocated object of type %c with UID %"PRIi64"\n", ttfont->type, ttfont->UID);
#endif

    pdfi_countup(ttfont);

    pfont = (gs_font_cid2 *)gs_alloc_struct(ctx->memory, gs_font_cid2, &st_gs_font_cid2,
                            "pdfi (cidtype2 pfont)");
    if (pfont == NULL) {
        pdfi_countdown(ttfont);
        return_error(gs_error_VMerror);
    }
    memset(pfont, 0x00, sizeof(gs_font_cid2));

    ttfont->pfont = (gs_font_base *)pfont;

    gs_make_identity(&pfont->orig_FontMatrix);
    gs_make_identity(&pfont->FontMatrix);
    pfont->next = pfont->prev = 0;
    pfont->memory = ctx->memory;
    pfont->dir = ctx->font_dir;
    pfont->is_resource = false;
    gs_notify_init(&pfont->notify_list, ctx->memory);
    pfont->base = (gs_font *) ttfont->pfont;
    pfont->client_data = ttfont;
    pfont->WMode = 0;
    pfont->PaintType = 0;
    pfont->StrokeWidth = 0;
    pfont->is_cached = 0;
    pfont->FAPI = NULL;
    pfont->FAPI_font_data = NULL;
    pfont->procs.init_fstack = gs_default_init_fstack;
    pfont->procs.next_char_glyph = gs_default_next_char_glyph;
    pfont->FontType = ft_CID_TrueType;
    pfont->ExactSize = fbit_use_outlines;
    pfont->InBetweenSize = fbit_use_outlines;
    pfont->TransformedChar = fbit_use_outlines;
    /* We may want to do something clever with an XUID here */
    pfont->id = gs_next_ids(ctx->memory, 1);
    uid_set_UniqueID(&pfont->UID, pfont->id);
    /* The buildchar proc will be filled in by FAPI -
       we won't worry about working without FAPI */
    pfont->procs.encode_char = pdfi_encode_char;
    pfont->data.string_proc = pdfi_cidtype2_string_proc;
    pfont->procs.glyph_name = ctx->get_glyph_name;
    pfont->procs.decode_glyph = pdfi_cidfont_decode_glyph;
    pfont->procs.define_font = gs_no_define_font;
    pfont->procs.make_font = gs_no_make_font;
    pfont->procs.font_info = gs_default_font_info;
    pfont->procs.glyph_info = gs_default_glyph_info;
    pfont->procs.glyph_outline = gs_no_glyph_outline;
    pfont->procs.build_char = NULL;
    pfont->procs.same_font = gs_default_same_font;
    pfont->procs.enumerate_glyph = gs_no_enumerate_glyph;

    pfont->encoding_index = 1;          /****** WRONG ******/
    pfont->nearest_encoding_index = 1;          /****** WRONG ******/

    cid_system_info_set_null(&pfont->cidata.common.CIDSystemInfo);
    pfont->cidata.common.CIDCount = 0; /* set later */
    pfont->cidata.common.GDBytes = 2; /* not used */
    pfont->cidata.MetricsCount = 0;
    pfont->cidata.CIDMap_proc = pdfi_cidtype2_CIDMap_proc;

    pfont->client_data = (void *)ttfont;

    *font = ttfont;
    return 0;
}

int pdfi_read_cidtype2_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 **ppfont)
{
    pdf_cidfont_type2 *font;
    int code = 0;
    pdf_obj *fontdesc = NULL;
    pdf_obj *obj = NULL;
    gs_font_cid2 *cid2;

    if (ppfont == NULL)
        return_error(gs_error_invalidaccess);

    *ppfont = NULL;

    code = pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, &fontdesc);
    if (code <= 0) {
        /* We own the buffer now, so we must free it on error */
        gs_free_object(ctx->memory, buf, "pdfi_read_cidtype2_font");
        return_error(gs_error_invalidfont);
    }

    if ((code = pdfi_alloc_cidtype2_font(ctx, &font, false)) < 0) {
        /* We own the buffer now, so we must free it on error */
        gs_free_object(ctx->memory, buf, "pdfi_read_cidtype2_font");
        pdfi_countdown(fontdesc);
        return code;
    }
    font->PDF_font = font_dict;
    pdfi_countup(font_dict);
    font->object_num = font_dict->object_num;
    font->generation_num = font_dict->generation_num;
    font->indirect_num = font_dict->indirect_num;
    font->indirect_gen = font_dict->indirect_gen;

    font->FontDescriptor = (pdf_dict *)fontdesc;
    fontdesc = NULL;

    code = pdfi_object_alloc(ctx, PDF_BUFFER, 0, (pdf_obj **)&font->sfnt);
    if (code < 0) {
        goto error;
    }
    pdfi_countup(font->sfnt);
    code = pdfi_buffer_set_data((pdf_obj *)font->sfnt, buf, buflen);
    if (code < 0) {
        goto error;
    }
    buf = NULL;

    /* Strictly speaking BaseFont is required, but we can continue without one */
    code = pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, (pdf_obj **)&obj);
    if (code > 0) {
        pdf_name *nobj = (pdf_name *)obj;
        int nlen = nobj->length > gs_font_name_max ? gs_font_name_max : nobj->length;

        memcpy(font->pfont->key_name.chars, nobj->data, nlen);
        font->pfont->key_name.chars[nlen] = 0;
        font->pfont->key_name.size = nlen;
        memcpy(font->pfont->font_name.chars, nobj->data, nlen);
        font->pfont->font_name.chars[nlen] = 0;
        font->pfont->font_name.size = nlen;
        pdfi_countdown(obj);
        obj = NULL;
    }

    code = pdfi_dict_knownget_type(ctx, font_dict, "DW", PDF_INT, (pdf_obj **)&obj);
    if (code > 0) {
        font->DW = ((pdf_num *)obj)->value.i;
        pdfi_countdown(obj);
        obj = NULL;
    }
    else {
        font->DW = 1000;
    }
    code = pdfi_dict_knownget_type(ctx, font_dict, "DW2", PDF_ARRAY, (pdf_obj **)&obj);
    if (code > 0) {
        font->DW2 = (pdf_array *)obj;
        obj = NULL;
    }
    else {
        font->DW2 = NULL;
    }
    code = pdfi_dict_knownget_type(ctx, font_dict, "W", PDF_ARRAY, (pdf_obj **)&obj);
    if (code > 0) {
        font->W = (pdf_array *)obj;
        obj = NULL;
    }
    else {
        font->W = NULL;
    }
    code = pdfi_dict_knownget_type(ctx, font_dict, "W2", PDF_ARRAY, (pdf_obj **)&obj);
    if (code > 0) {
        font->W2 = (pdf_array *)obj;
        obj = NULL;
    }
    else {
        font->W2 = NULL;
    }

    code = pdfi_dict_knownget(ctx, font_dict, "CIDToGIDMap", (pdf_obj **)&obj);
    if (code > 0) {
        /* CIDToGIDMap can only be a stream or a name, and if it's a name
           it's only permitted to be "/Identity", so ignore it
         */
        if (pdfi_type_of(obj) == PDF_STREAM) {
            byte *d;
            int64_t sz = 0;

            code = pdfi_object_alloc(ctx, PDF_BUFFER, 0, (pdf_obj **)&font->cidtogidmap);
            if (code < 0) {
                goto error;
            }
            pdfi_countup(font->cidtogidmap);
            code = pdfi_stream_to_buffer(ctx, (pdf_stream *)obj, &d, &sz);
            if (code < 0) {
                goto error;
            }
            code = pdfi_buffer_set_data((pdf_obj *)font->cidtogidmap, d, (int32_t)sz);
            if (code < 0) {
                goto error;
            }
        }
        pdfi_countdown(obj);
        obj = NULL;
    }

    cid2 = (gs_font_cid2 *)font->pfont;

    code = pdfi_dict_knownget_type(ctx, font_dict, "CIDSystemInfo", PDF_DICT, (pdf_obj **)&obj);
    if (code <= 0) {
        cid2->cidata.common.CIDSystemInfo.Registry.data = NULL;
        cid2->cidata.common.CIDSystemInfo.Registry.size = 0;
        cid2->cidata.common.CIDSystemInfo.Ordering.data = NULL;
        cid2->cidata.common.CIDSystemInfo.Ordering.size = 0;
    }
    else {
        pdf_num *suppl = NULL;

        code = pdfi_dict_knownget_type(ctx, (pdf_dict *)obj, "Registry", PDF_STRING, (pdf_obj **)&font->registry);
        if (code <= 0) {
            cid2->cidata.common.CIDSystemInfo.Registry.data = NULL;
            cid2->cidata.common.CIDSystemInfo.Registry.size = 0;
        }
        else {
            cid2->cidata.common.CIDSystemInfo.Registry.data = font->registry->data;
            cid2->cidata.common.CIDSystemInfo.Registry.size = font->registry->length;
        }
        code = pdfi_dict_knownget_type(ctx, (pdf_dict *)obj, "Ordering", PDF_STRING, (pdf_obj **)&font->ordering);
        if (code <= 0) {
            cid2->cidata.common.CIDSystemInfo.Ordering.data = NULL;
            cid2->cidata.common.CIDSystemInfo.Ordering.size = 0;
        }
        else {
            cid2->cidata.common.CIDSystemInfo.Ordering.data = font->ordering->data;
            cid2->cidata.common.CIDSystemInfo.Ordering.size = font->ordering->length;
        }
        code = pdfi_dict_knownget_type(ctx, (pdf_dict *)obj, "Supplement", PDF_INT, (pdf_obj **)&suppl);
        if (code <= 0) {
            cid2->cidata.common.CIDSystemInfo.Supplement = font->supplement = 0;
        }
        else {
            cid2->cidata.common.CIDSystemInfo.Supplement = font->supplement = suppl->value.i;
        }
        pdfi_countdown(suppl);
    }
    pdfi_countdown(obj);
    obj = NULL;


    code = gs_type42_font_init((gs_font_type42 *)font->pfont, 0);
    if (code < 0) {
        goto error;
    }
    if (uid_is_XUID(&font->pfont->UID))
        uid_free(&font->pfont->UID, font->pfont->memory, "pdfi_read_type1_font");
    uid_set_invalid(&font->pfont->UID);
    font->pfont->id = gs_next_ids(ctx->memory, 1);

    code = pdfi_font_generate_pseudo_XUID(ctx, font_dict, font->pfont);
    if (code < 0)
        goto error;


    font->orig_glyph_info = font->pfont->procs.glyph_info;
    font->pfont->procs.glyph_info = pdfi_cidtype2_glyph_info;
    font->pfont->procs.enumerate_glyph = pdfi_cidtype2_enumerate_glyph;

    if (font->cidtogidmap != NULL) {
        gs_font_cid2 *cid2 = (gs_font_cid2 *)font->pfont;
        if (cid2->data.numGlyphs > font->cidtogidmap->length >> 1)
            cid2->cidata.common.CIDCount = cid2->data.numGlyphs;
        else {
            cid2->cidata.common.CIDCount = font->cidtogidmap->length >> 1;
        }
        cid2->cidata.common.MaxCID = cid2->cidata.common.CIDCount;
    }
    else {
        gs_font_cid2 *cid2 = (gs_font_cid2 *)font->pfont;
        cid2->cidata.common.CIDCount = cid2->data.numGlyphs;
        cid2->cidata.common.MaxCID = cid2->cidata.common.CIDCount;
    }
    cid2->data.substitute_glyph_index_vertical = gs_type42_substitute_glyph_index_vertical;
    cid2->cidata.orig_procs.get_outline = cid2->data.get_outline;
    cid2->data.get_glyph_index = pdfi_cidtype2_get_glyph_index;

    code = gs_definefont(ctx->font_dir, (gs_font *)font->pfont);
    if (code < 0) {
        goto error;
    }

    code = pdfi_fapi_passfont((pdf_font *)font, 0, NULL, NULL, font->sfnt->data, font->sfnt->length);
    if (code < 0) {
        goto error;
    }

    /* object_num can be zero if the dictionary was defined inline */
    if (font->object_num != 0) {
        (void)replace_cache_entry(ctx, (pdf_obj *)font);
    }

    *ppfont = (pdf_font *)font;
    return code;
error:

    pdfi_countdown(obj);
    pdfi_countdown(font);
    return code;
}

int pdfi_free_font_cidtype2(pdf_obj *font)
{
    pdf_cidfont_type2 *pdfcidf = (pdf_cidfont_type2 *)font;
    gs_font_cid2 *pfont = (gs_font_cid2 *)pdfcidf->pfont;
    gs_free_object(OBJ_MEMORY(pdfcidf), pfont, "pdfi_free_font_cidtype2(pfont)");

    pdfi_countdown(pdfcidf->cidtogidmap);

    pdfi_countdown(pdfcidf->sfnt);
    pdfi_countdown(pdfcidf->PDF_font);
    pdfi_countdown(pdfcidf->BaseFont);
    pdfi_countdown(pdfcidf->FontDescriptor);
    pdfi_countdown(pdfcidf->W);
    pdfi_countdown(pdfcidf->DW2);
    pdfi_countdown(pdfcidf->W2);
    pdfi_countdown(pdfcidf->registry);
    pdfi_countdown(pdfcidf->ordering);
    pdfi_countdown(pdfcidf->filename);

    gs_free_object(OBJ_MEMORY(pdfcidf), pdfcidf, "pdfi_free_font_cidtype2(pdfcidf)");
return 0;

    return 0;
}