summaryrefslogtreecommitdiff
blob: c9e3ae04b13e7f56ef2362105526e347563ee6c2 (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
/* Copyright (C) 2001-2020 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.
*/


/* CIDFontType 0 operators */
#include "memory_.h"
#include "ghost.h"
#include "oper.h"
#include "gsmatrix.h"
#include "gsccode.h"
#include "gsstruct.h"
#include "gxfcid.h"
#include "gxfont1.h"
#include "gxalloc.h"		/* for gs_ref_memory_t */
#include "stream.h"		/* for files.h */
#include "bfont.h"
#include "files.h"
#include "ichar.h"
#include "ichar1.h"
#include "icid.h"
#include "idict.h"
#include "idparam.h"
#include "ifcid.h"
#include "ifont1.h"
#include "ifont2.h"
#include "ifont42.h"
#include "store.h"
#include "imain.h"
#include "iapi.h"
#include "iminst.h"

/* Type 1 font procedures (defined in zchar1.c) */
font_proc_glyph_outline(zcharstring_glyph_outline);

/* ---------------- CIDFontType 0 (FontType 9) ---------------- */

/* ------ Accessing ------ */

/* Parse a multi-byte integer from a string. */
static int
get_index(gs_glyph_data_t *pgd, int count, ulong *pval)
{
    int i;

    if (pgd->bits.size < count)
        return_error(gs_error_rangecheck);
    *pval = 0;
    for (i = 0; i < count; ++i)
        *pval = (*pval << 8) + pgd->bits.data[i];
    pgd->bits.data += count;
    pgd->bits.size -= count;
    return 0;
}

/* Get bytes from GlyphData or DataSource. */
static int
cid0_read_bytes(gs_font_cid0 *pfont, ulong base, uint count, byte *buf,
                gs_glyph_data_t *pgd)
{
    const font_data *pfdata = pfont_data(pfont);
    byte *data = buf;
    gs_font *gdfont = 0;	/* pfont if newly allocated, 0 if not */
    int code = 0;

    /* Check for overflow. */
    if (base != (long)base || base > base + count)
        return_error(gs_error_rangecheck);
    if (r_has_type(&pfdata->u.cid0.DataSource, t_null)) {
        /* Get the bytes from GlyphData (a string or array of strings). */
        const ref *pgdata = &pfdata->u.cid0.GlyphData;

        if (r_has_type(pgdata, t_string)) {  /* single string */
            uint size = r_size(pgdata);

            if (base >= size || count > size - base)
                return_error(gs_error_rangecheck);
            data = pgdata->value.bytes + base;
        } else {		/* array of strings */
            /*
             * The algorithm is similar to the one in
             * string_array_access_proc in zfont42.c, but it also has to
             * deal with the case where the requested string crosses array
             * elements.
             */
            ulong skip = base;
            uint copied = 0;
            uint index = 0;
            ref rstr;
            uint size;

            for (;; skip -= size, ++index) {
                int code = array_get(pfont->memory, pgdata, index, &rstr);

                if (code < 0)
                    return code;
                if (!r_has_type(&rstr, t_string))
                    return_error(gs_error_typecheck);
                size = r_size(&rstr);
                if (skip < size)
                    break;
            }
            size -= skip;
            if (count <= size) {
                data = rstr.value.bytes + skip;
            } else {		/* multiple strings needed */
                if (data == 0) {  /* no buffer provided */
                    data = gs_alloc_string(pfont->memory, count,
                                           "cid0_read_bytes");
                    if (data == 0)
                        return_error(gs_error_VMerror);
                    gdfont = (gs_font *)pfont; /* newly allocated */
                }
                memcpy(data, rstr.value.bytes + skip, size);
                copied = size;
                while (copied < count) {
                    int code = array_get(pfont->memory, pgdata, ++index, &rstr);

                    if (code < 0)
                        goto err;
                    if (!r_has_type(&rstr, t_string)) {
                        code = gs_note_error(gs_error_typecheck);
                        goto err;
                    }
                    size = r_size(&rstr);
                    if (size > count - copied)
                        size = count - copied;
                    memcpy(data + copied, rstr.value.bytes, size);
                    copied += size;
                }
            }
        }
    } else {
        /* Get the bytes from DataSource (a stream). */
        stream *s;
        uint nread;
        i_ctx_t *i_ctx_p = get_minst_from_memory(pfont->memory)->i_ctx_p;

        check_read_known_file(i_ctx_p, s, &pfdata->u.cid0.DataSource, return_error);
        if (sseek(s, base) < 0)
            return_error(gs_error_ioerror);
        if (data == 0) {	/* no buffer provided */
            data = gs_alloc_string(pfont->memory, count, "cid0_read_bytes");
            if (data == 0)
                return_error(gs_error_VMerror);
            gdfont = (gs_font *)pfont; /* newly allocated */
        }
        if (sgets(s, data, count, &nread) < 0 || nread != count) {
            code = gs_note_error(gs_error_ioerror);
            goto err;
        }
    }
    gs_glyph_data_from_string(pgd, data, count, gdfont);
    return code;
 err:
    if (data != buf)
        gs_free_string(pfont->memory, data, count, "cid0_read_bytes");
    return code;
}

/* Get the CharString data for a CIDFontType 0 font. */
/* This is the glyph_data procedure in the font itself. */
/* Note that pgd may be NULL. */
static int
z9_glyph_data(gs_font_base *pbfont, gs_glyph glyph, gs_glyph_data_t *pgd,
              int *pfidx)
{
    gs_font_cid0 *pfont = (gs_font_cid0 *)pbfont;
    const font_data *pfdata = pfont_data(pfont);
    long glyph_index = (long)(glyph - GS_MIN_CID_GLYPH);
    gs_glyph_data_t gdata;
    ulong fidx;
    int code;

    gdata.memory = pfont->memory;
    if (!r_has_type(&pfdata->u.cid0.GlyphDirectory, t_null)) {
        code = font_gdir_get_outline(pfont->memory,
                                     &pfdata->u.cid0.GlyphDirectory,
                                     glyph_index, &gdata);
        if (code < 0)
            return code;
        /* Get the definition from GlyphDirectory. */
        if (!gdata.bits.data)
            return_error(gs_error_rangecheck);
        code = get_index(&gdata, pfont->cidata.FDBytes, &fidx);
        if (code < 0)
            return code;
        if (fidx >= pfont->cidata.FDArray_size)
            return_error(gs_error_rangecheck);
        if (pgd)
            *pgd = gdata;
        *pfidx = (int)fidx;
        return code;
    }
    /* Get the definition from the binary data (GlyphData or DataSource). */
    if (glyph_index < 0 || glyph_index >= pfont->cidata.common.CIDCount) {
        *pfidx = 0;
        if (pgd)
            gs_glyph_data_from_null(pgd);
        return_error(gs_error_rangecheck);
    }
    {
        byte fd_gd[(MAX_FDBytes + MAX_GDBytes) * 2];
        uint num_bytes = pfont->cidata.FDBytes + pfont->cidata.common.GDBytes;
        ulong base = pfont->cidata.CIDMapOffset + glyph_index * num_bytes;
        ulong gidx, fidx_next, gidx_next;
        int rcode = cid0_read_bytes(pfont, base, (ulong)(num_bytes * 2), fd_gd,
                                    &gdata);
        gs_glyph_data_t orig_data;

        if (rcode < 0)
            return rcode;
        orig_data = gdata;
        if ((code = get_index(&gdata, pfont->cidata.FDBytes, &fidx)) < 0 ||
            (code = get_index(&gdata, pfont->cidata.common.GDBytes, &gidx)) < 0 ||
            (code = get_index(&gdata, pfont->cidata.FDBytes, &fidx_next)) < 0 ||
            (code = get_index(&gdata, pfont->cidata.common.GDBytes, &gidx_next)) < 0
            )
            DO_NOTHING;
        gs_glyph_data_free(&orig_data, "z9_glyph_data");
        if (code < 0)
            return code;
        /*
         * Some CID fonts (from Adobe!) have invalid font indexes for
         * missing glyphs.  Handle this now.
         */
        if (gidx_next <= gidx) { /* missing glyph */
            *pfidx = 0;
            if (pgd)
                gs_glyph_data_from_null(pgd);
            return_error(gs_error_undefined);
        }
        if (fidx >= pfont->cidata.FDArray_size)
            return_error(gs_error_rangecheck);
        *pfidx = (int)fidx;
        if (pgd == 0)
            return 0;
        return cid0_read_bytes(pfont, gidx, gidx_next - gidx, NULL, pgd);
    }
}

/* Get the outline of a CIDFontType 0 glyph. */
static int
z9_glyph_outline(gs_font *font, int WMode, gs_glyph glyph, const gs_matrix *pmat,
                 gx_path *ppath, double sbw[4])
{
    gs_font_cid0 *const pfcid = (gs_font_cid0 *)font;
    ref gref;
    gs_glyph_data_t gdata;
    int code, fidx, ocode;

    gdata.memory = font->memory;
    code = pfcid->cidata.glyph_data((gs_font_base *)pfcid, glyph, &gdata,
                                    &fidx);
    if (code < 0)
        return code;
    glyph_ref(font->memory, glyph, &gref);
    ocode = zcharstring_outline(pfcid->cidata.FDArray[fidx], WMode, &gref, &gdata,
                                pmat, ppath, sbw);
    gs_glyph_data_free(&gdata, "z9_glyph_outline");
    return ocode;
}

static int
z9_glyph_info(gs_font *font, gs_glyph glyph, const gs_matrix *pmat,
                     int members, gs_glyph_info_t *info)
{   /* fixme : same as z11_glyph_info. */
    int wmode = (members & GLYPH_INFO_WIDTH0 ? 0 : 1);

    return z1_glyph_info_generic(font, glyph, pmat, members, info,
                                    &gs_default_glyph_info, wmode);
}

/*
 * The "fonts" in the FDArray don't have access to their outlines -- the
 * outlines are always provided externally.  Replace the accessor procedures
 * with ones that will give an error if called.
 */
static int
z9_FDArray_glyph_data(gs_font_type1 * pfont, gs_glyph glyph,
                      gs_glyph_data_t *pgd)
{
    return_error(gs_error_invalidfont);
}
static int
z9_FDArray_seac_data(gs_font_type1 *pfont, int ccode, gs_glyph *pglyph,
                     gs_const_string *gstr, gs_glyph_data_t *pgd)
{
    return_error(gs_error_invalidfont);
}

/* ------ Defining ------ */

/* Get one element of a FDArray. */
static int
fd_array_element(i_ctx_t *i_ctx_p, gs_font_type1 **ppfont, ref *prfd)
{
    charstring_font_refs_t refs;
    gs_type1_data data1;
    build_proc_refs build;
    gs_font_base *pbfont;
    gs_font_type1 *pfont;
    /*
     * Standard CIDFontType 0 fonts have Type 1 fonts in the FDArray, but
     * CFF CIDFontType 0 fonts have Type 2 fonts there.
     */
    int fonttype = 1;		/* default */
    int code = charstring_font_get_refs(prfd, &refs);

    if (code < 0 ||
        (code = dict_int_param(prfd, "FontType", 1, 2, 1, &fonttype)) < 0
        )
        return code;
    /*
     * We don't handle the alternate Subr representation (SubrCount,
     * SDBytes, SubrMapOffset) here: currently that is handled in
     * PostScript code (lib/gs_cidfn.ps).
     */
    switch (fonttype) {
    case 1:
        data1.interpret = gs_type1_interpret;
        data1.subroutineNumberBias = 0;
        data1.lenIV = DEFAULT_LENIV_1;
        code = charstring_font_params(imemory, prfd, &refs, &data1);
        if (code < 0)
            return code;
        code = build_proc_name_refs(imemory, &build,
                                    "%Type1BuildChar", "%Type1BuildGlyph");
        break;
    case 2:
        code = type2_font_params(prfd, &refs, &data1);
        if (code < 0)
            return code;
        code = charstring_font_params(imemory, prfd, &refs, &data1);
        if (code < 0)
            return code;
        code = build_proc_name_refs(imemory, &build,
                                    "%Type2BuildChar", "%Type2BuildGlyph");
        break;
    default:			/* can't happen */
        return_error(gs_error_Fatal);
    }
    if (code < 0)
        return code;
    code = build_gs_FDArray_font(i_ctx_p, prfd, &pbfont, fonttype,
                                 &st_gs_font_type1, &build);
    if (code < 0)
        return code;
    pfont = (gs_font_type1 *)pbfont;
    pbfont->FAPI = NULL;
    pbfont->FAPI_font_data = NULL;
    charstring_font_init(pfont, &refs, &data1);
    pfont->data.procs.glyph_data = z9_FDArray_glyph_data;
    pfont->data.procs.seac_data = z9_FDArray_seac_data;
    *ppfont = pfont;
    return 0;
}

static int
notify_remove_font_type9(void *proc_data, void *event_data)
{  /* Likely type 9 font descendents are never released explicitly.
      So releaseing a type 9 font we must reset pointers in descendents.
    */
    /* gs_font_finalize passes event_data == NULL, so check it here. */
    if (event_data == NULL) {
        gs_font_cid0 *pfcid = proc_data;
        int i;

        for (i = 0; i < pfcid->cidata.FDArray_size; ++i) {
            if (pfcid->cidata.FDArray[i]->data.parent == (gs_font_base *)pfcid)
                pfcid->cidata.FDArray[i]->data.parent = NULL;
        }
    }
    return 0;
}

/* <string|name> <font_dict> .buildfont9 <string|name> <font> */
static int
zbuildfont9(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    build_proc_refs build;
    int code = build_proc_name_refs(imemory, &build, NULL, "%Type9BuildGlyph");
    gs_font_cid_data common;
    ref GlyphDirectory, GlyphData, DataSource;
    ref *prfda, cfnstr;
    ref *pCIDFontName, CIDFontName;
    gs_font_type1 **FDArray;
    uint FDArray_size;
    int FDBytes;
    uint CIDMapOffset;
    gs_font_base *pfont;
    gs_font_cid0 *pfcid;
    uint i;

    /*
     * If the CIDFont's data have been loaded into VM, GlyphData will be
     * a string or an array of strings; if they are loaded incrementally
     * from a file, GlyphData will be an integer, and DataSource will be
     * a (reusable) stream.
     */
    if (code < 0)
        return code;
    code = cid_font_data_param(op, &common, &GlyphDirectory);
    if (code < 0)
        return code;
    code = dict_find_string(op, "FDArray", &prfda);
    if (code < 0)
        return code;
    if (code == 0)
        return_error(gs_error_undefined);
    code = dict_find_string(op, "CIDFontName", &pCIDFontName);
    if (code < 0)
        return code;
    if (code == 0)
        return_error(gs_error_undefined);
    code = dict_int_param(op, "FDBytes", 0, MAX_FDBytes, -1, &FDBytes);
    if (code < 0)
        return code;

    /*
     * Since build_gs_simple_font may resize the dictionary and cause
     * pointers to become invalid, save CIDFontName
     */
    CIDFontName = *pCIDFontName;
    if (r_has_type(&GlyphDirectory, t_null)) {
        /* Standard CIDFont, require GlyphData and CIDMapOffset. */
        ref *pGlyphData;

        code = dict_find_string(op, "GlyphData", &pGlyphData);
        if (code < 0)
            return code;
        if (code == 0)
            return_error(gs_error_undefined);
        code = dict_uint_param(op, "CIDMapOffset", 0, max_uint - 1, max_uint, &CIDMapOffset);
        if (code < 0)
            return code;
        GlyphData = *pGlyphData;
        if (r_has_type(&GlyphData, t_integer)) {
            ref *pds;
            stream *ignore_s;

            code = dict_find_string(op, "DataSource", &pds);
            if (code < 0)
                return code;
            if (code == 0)
                return_error(gs_error_undefined);
            check_read_file(i_ctx_p, ignore_s, pds);
            DataSource = *pds;
        } else {
            if (!r_has_type(&GlyphData, t_string) && !r_is_array(&GlyphData))
                return_error(gs_error_typecheck);
            make_null(&DataSource);
        }
    } else {
        make_null(&GlyphData);
        make_null(&DataSource);
        CIDMapOffset = 0;
    }
    if (!r_is_array(prfda))
        return_error(gs_error_invalidfont);
    FDArray_size = r_size(prfda);
    if (FDArray_size == 0)
        return_error(gs_error_invalidfont);
    FDArray = ialloc_struct_array(FDArray_size, gs_font_type1 *,
                                  &st_gs_font_type1_ptr_element,
                                  "buildfont9(FDarray)");
    if (FDArray == 0)
        return_error(gs_error_VMerror);
    memset(FDArray, 0, sizeof(gs_font_type1 *) * FDArray_size);
    for (i = 0; i < FDArray_size; ++i) {
        ref rfd;

        array_get(imemory, prfda, (long)i, &rfd);
        code = fd_array_element(i_ctx_p, &FDArray[i], &rfd);
        if (code < 0)
            goto fail;
    }
    code = build_gs_outline_font(i_ctx_p, op, &pfont, ft_CID_encrypted,
                                &st_gs_font_cid0, &build,
                                bf_Encoding_optional | bf_UniqueID_ignored,
                                build_gs_simple_font);
    if (code < 0)
        goto fail;
    if (code == 1) {
        /* The font already has a FID, don't need to build it again.
           Release FDArray and return normally.
           fixme: FDArray fonts are thrown for garbager.
           We're not safe to build them after
           build_gs_simple_font(..., &pfont, ...),
           because a failure in building them would throw
           an underbuilt font with unclear consequences.
         */
        ifree_object(FDArray, "buildfont9(FDarray)");
        return 0;
    }
    pfont->procs.enumerate_glyph = gs_font_cid0_enumerate_glyph;
    pfont->procs.glyph_outline = z9_glyph_outline;
    pfont->procs.glyph_info = z9_glyph_info;
    pfcid = (gs_font_cid0 *)pfont;
    pfcid->cidata.common = common;
    pfcid->cidata.CIDMapOffset = CIDMapOffset;
    pfcid->cidata.FDArray = FDArray;
    pfcid->cidata.FDArray_size = FDArray_size;
    pfcid->cidata.FDBytes = FDBytes;
    pfcid->cidata.glyph_data = z9_glyph_data;
    pfcid->cidata.proc_data = 0;	/* for GC */
    if (pfcid->font_name.size == 0) {
        get_font_name(imemory, &cfnstr, &CIDFontName);
        copy_font_name(&pfcid->font_name, &cfnstr);
    }
    ref_assign(&pfont_data(pfont)->u.cid0.GlyphDirectory, &GlyphDirectory);
    ref_assign(&pfont_data(pfont)->u.cid0.GlyphData, &GlyphData);
    ref_assign(&pfont_data(pfont)->u.cid0.DataSource, &DataSource);
    code = define_gs_font(i_ctx_p, (gs_font *)pfont);
    if (code >= 0)
       code = gs_notify_register(&pfont->notify_list, notify_remove_font_type9, pfont);
    if (code >= 0) {
        for (i = 0; i < FDArray_size; ++i) {
            FDArray[i]->dir = pfont->dir;
            FDArray[i]->data.parent = pfont;
        }
        return code;
    }
 fail:
    ifree_object(FDArray, "buildfont9(FDarray)");
    return code;
}

/* <cid9font> <cid> .type9mapcid <charstring> <font_index> */
int
ztype9mapcid(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    gs_font *pfont;
    gs_font_cid0 *pfcid;
    int code = font_param(op - 1, &pfont);
    gs_glyph_data_t gdata;
    int fidx;

    if (code < 0)
        return code;
    if (pfont->FontType != ft_CID_encrypted)
        return_error(gs_error_invalidfont);
    check_type(*op, t_integer);
    pfcid = (gs_font_cid0 *)pfont;
    gdata.memory = pfont->memory;
    code = pfcid->cidata.glyph_data((gs_font_base *)pfcid,
                        (gs_glyph)(GS_MIN_CID_GLYPH + op->value.intval),
                                    &gdata, &fidx);

    /* return code; original error-sensitive & fragile code */
    if (code < 0) { /* failed to load glyph data, put CID 0 */
       ps_int default_fallback_CID = 0 ;

       if_debug2m('J', imemory, "[J]ztype9cidmap() use CID %" PRIpsint " instead of glyph-missing CID %" PRIpsint "\n", default_fallback_CID, op->value.intval);

       op->value.intval = default_fallback_CID;

       /* reload glyph for default_fallback_CID */

       code = pfcid->cidata.glyph_data((gs_font_base *)pfcid,
                    (gs_glyph)(GS_MIN_CID_GLYPH + default_fallback_CID),
                                   &gdata, &fidx);

       if (code < 0) {
           if_debug1m('J', imemory, "[J]ztype9cidmap() could not load default glyph (CID %" PRIpsint ")\n", op->value.intval);
           return_error(gs_error_invalidfont);
       }

    }

    /****** FOLLOWING IS NOT GENERAL W.R.T. ALLOCATION OF GLYPH DATA ******/
    make_const_string(op - 1,
                      a_readonly | imemory_space((gs_ref_memory_t *)pfont->memory),
                      gdata.bits.size,
                      gdata.bits.data);
    make_int(op, fidx);
    return code;
}

/* ------ Initialization procedure ------ */

const op_def zfcid0_op_defs[] =
{
    {"2.buildfont9", zbuildfont9},
    {"2.type9mapcid", ztype9mapcid},
    op_def_end(0)
};