Coverage Report

Created: 2025-06-10 06:58

/src/ghostpdl/devices/gxfcopy.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2025 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
17
/* Font copying for high-level output */
18
#include "memory_.h"
19
#include "gx.h"
20
#include <stdlib.h>   /* for qsort */
21
#include "gscencs.h"
22
#include "gserrors.h"
23
#include "gsline.h"   /* for BuildChar */
24
#include "gspaint.h"    /* for BuildChar */
25
#include "gspath.h"   /* for gs_moveto in BuildChar */
26
#include "gsstruct.h"
27
#include "gsutil.h"
28
#include "gschar.h"
29
#include "stream.h"
30
#include "gxfont.h"
31
#include "gxfont1.h"
32
#include "gxfont42.h"
33
#include "gxchar.h"
34
#include "gxfcid.h"
35
#include "gxfcopy.h"
36
#include "gxfcache.h"   /* for gs_font_dir_s */
37
#include "gxgstate.h"   /* for Type 1 glyph_outline */
38
#include "gxtext.h"   /* for BuildChar */
39
#include "gxtype1.h"    /* for Type 1 glyph_outline */
40
#include "gzstate.h"    /* for path for BuildChar */
41
#include "gdevpsf.h"
42
#include "smd5.h"   /* Create MD5 hash of Subrs when comparing fonts */
43
44
#define GLYPHS_SIZE_IS_PRIME 1 /* Old code = 0, new code = 1. */
45
46
/* ================ Types and structures ================ */
47
48
typedef struct gs_copied_glyph_s gs_copied_glyph_t;
49
typedef struct gs_copied_font_data_s gs_copied_font_data_t;
50
51
typedef struct gs_copied_font_procs_s {
52
    int (*finish_copy_font)(gs_font *font, gs_font *copied);
53
    int (*copy_glyph)(gs_font *font, gs_glyph glyph, gs_font *copied,
54
                      int options);
55
    int (*add_encoding)(gs_font *copied, gs_char chr, gs_glyph glyph);
56
    int (*named_glyph_slot)(gs_copied_font_data_t *cfdata, gs_glyph glyph,
57
                            gs_copied_glyph_t **pslot);
58
    /* Font procedures */
59
    font_proc_encode_char((*encode_char));
60
    font_proc_glyph_info((*glyph_info));
61
    font_proc_glyph_outline((*glyph_outline));
62
    int (*uncopy_glyph)(gs_font *font, gs_glyph glyph, gs_font *copied,
63
                      int options);
64
} gs_copied_font_procs_t;
65
66
/*
67
 * We need to store the Subrs data for Type 1/2 and CIDFontType 0 fonts,
68
 * and the GlobalSubrs data for all but Type 1.
69
 */
70
typedef struct gs_subr_info_s {
71
    byte *data;   /* Subr data */
72
    int count;
73
    uint *starts; /* [count+1] Subr[i] = data[starts[i]..starts[i+1]] */
74
} gs_subr_info_t;
75
76
/*
77
 * The glyphs for copied fonts are stored explicitly in a table indexed by
78
 * glyph number.
79
 * For Type 1 fonts, the glyph numbers are parallel to the hashed name table.
80
 * For TrueType fonts, the glyph number is the TrueType GID.
81
 * For CIDFontType 0 fonts, the glyph number is the CID.
82
 * For CIDFontType 2 fonts, the glyph number is the TrueType GID;
83
 * a separate CIDMap array maps CIDs to GIDs.
84
 */
85
struct gs_copied_glyph_s {
86
    gs_const_string gdata;  /* vector data */
87
7.15M
#define HAS_DATA 1    /* entry is in use */
88
                                /* HAS_SBW* are only used for TT-based fonts */
89
33.3M
#define HAS_SBW0 2    /* has hmtx */
90
#define HAS_SBW1 4    /* has vmtx */
91
    byte used;      /* non-zero iff this entry is in use */
92
                                /* (if not, gdata.{data,size} = 0) */
93
    int order_index;            /* Index for the ordered glyph set. */
94
};
95
/*
96
 * We use a special GC descriptor to avoid large GC overhead.
97
 */
98
gs_private_st_composite(st_gs_copied_glyph_element, gs_copied_glyph_t,
99
                        "gs_copied_glyph_t[]", copied_glyph_element_enum_ptrs,
100
                        copied_glyph_element_reloc_ptrs);
101
64.9M
static ENUM_PTRS_WITH(copied_glyph_element_enum_ptrs, gs_copied_glyph_t *pcg)
102
64.9M
     if (index < size / (uint)sizeof(gs_copied_glyph_t))
103
64.9M
         return ENUM_CONST_STRING(&pcg[index].gdata);
104
23.0k
     return 0;
105
64.9M
ENUM_PTRS_END
106
23.0k
static RELOC_PTRS_WITH(copied_glyph_element_reloc_ptrs, gs_copied_glyph_t *pcg)
107
23.0k
{
108
23.0k
    uint count = size / (uint)sizeof(gs_copied_glyph_t);
109
23.0k
    gs_copied_glyph_t *p = pcg;
110
111
64.9M
    for (; count > 0; --count, ++p)
112
64.9M
        if (p->gdata.size > 0)
113
7.32M
            RELOC_CONST_STRING_VAR(p->gdata);
114
23.0k
}
115
23.0k
RELOC_PTRS_END
116
117
/*
118
 * Type 1 and TrueType fonts also have a 'names' table, parallel to the
119
 * 'glyphs' table.
120
 * For Type 1 fonts, this is a hash table; glyph numbers are assigned
121
 * arbitrarily, according to the hashed placement of the names.
122
 * For TrueType fonts, this is indexed by GID.
123
 * The strings in this table are either those returned by the font's
124
 * glyph_name procedure, which we assume are garbage-collected, or those
125
 * associated with the known encodings, which we assume are immutable.
126
 */
127
typedef struct gs_copied_glyph_name_s {
128
    gs_glyph glyph;   /* key (for comparison and glyph_name only) */
129
    gs_const_string str;  /* glyph name */
130
} gs_copied_glyph_name_t;
131
/*
132
 * We use the same special GC descriptor as above for 'names'.
133
 */
134
gs_private_st_composite(st_gs_copied_glyph_name_element,
135
                        gs_copied_glyph_name_t,
136
                        "gs_copied_glyph_name_t[]",
137
                        copied_glyph_name_enum_ptrs,
138
                        copied_glyph_name_reloc_ptrs);
139
36.5M
static ENUM_PTRS_WITH(copied_glyph_name_enum_ptrs, gs_copied_glyph_name_t *pcgn)
140
36.5M
     if (index < size / (uint)sizeof(gs_copied_glyph_name_t)) {
141
36.5M
         const gs_copied_glyph_name_t *const p = &pcgn[index];
142
143
36.5M
         return (p->str.size == 0 ||
144
36.5M
                 gs_is_c_glyph_name(p->str.data, p->str.size) ?
145
33.0M
                 ENUM_CONST_STRING2(0, 0) :
146
36.5M
                 ENUM_CONST_STRING(&p->str));
147
36.5M
     }
148
21.7k
     return 0;
149
     /* We should mark glyph name here, but we have no access to
150
        the gs_font_dir instance. Will mark in gs_copied_font_data_enum_ptrs.
151
      */
152
36.5M
ENUM_PTRS_END
153
21.7k
static RELOC_PTRS_WITH(copied_glyph_name_reloc_ptrs, gs_copied_glyph_name_t *pcgn)
154
21.7k
{
155
21.7k
    uint count = size / (uint)sizeof(gs_copied_glyph_name_t);
156
21.7k
    gs_copied_glyph_name_t *p = pcgn;
157
158
36.5M
    for (; count > 0; --count, ++p)
159
36.5M
        if (p->str.size > 0 && !gs_is_c_glyph_name(p->str.data, p->str.size))
160
3.51M
            RELOC_CONST_STRING_VAR(p->str);
161
21.7k
}
162
21.7k
RELOC_PTRS_END
163
164
/*
165
 * To accommodate glyphs with multiple names, there is an additional
166
 * 'extra_names' table.  Since this is rare, this table uses linear search.
167
 */
168
typedef struct gs_copied_glyph_extra_name_s gs_copied_glyph_extra_name_t;
169
struct gs_copied_glyph_extra_name_s {
170
    gs_copied_glyph_name_t name;
171
    uint gid;     /* index in glyphs table */
172
    gs_copied_glyph_extra_name_t *next;
173
};
174
BASIC_PTRS(gs_copied_glyph_extra_name_ptrs) {
175
    GC_STRING_ELT(gs_copied_glyph_extra_name_t, name.str),
176
    GC_OBJ_ELT(gs_copied_glyph_extra_name_t, next)
177
};
178
gs_private_st_basic(st_gs_copied_glyph_extra_name,
179
                    gs_copied_glyph_extra_name_t,
180
                    "gs_copied_glyph_extra_name_t",
181
                    gs_copied_glyph_extra_name_ptrs,
182
                    gs_copied_glyph_extra_name_data);
183
184
/*
185
 * The client_data of copied fonts points to an instance of
186
 * gs_copied_font_data_t.
187
 */
188
struct gs_copied_font_data_s {
189
    gs_font_info_t info;  /* from the original font, must be first */
190
    const gs_copied_font_procs_t *procs;
191
    gs_copied_glyph_t *glyphs;  /* [glyphs_size] */
192
    uint glyphs_size;   /* (a power of 2 or a prime number for Type 1/2) */
193
    uint num_glyphs;    /* The number of glyphs copied. */
194
    gs_glyph notdef;    /* CID 0 or .notdef glyph */
195
    /*
196
     * We don't use a union for the rest of the data, because some of the
197
     * cases overlap and it's just not worth the trouble.
198
     */
199
    gs_copied_glyph_name_t *names; /* (Type 1/2, TrueType) [glyphs_size] */
200
    gs_copied_glyph_extra_name_t *extra_names; /* (TrueType) */
201
    byte *data;     /* (TrueType and CID fonts) copied data */
202
    uint data_size;   /* (TrueType and CID fonts) */
203
    gs_glyph *Encoding;   /* (Type 1/2 and Type 42) [256] */
204
    ushort *CIDMap;   /* (CIDFontType 2) [CIDCount] */
205
    gs_subr_info_t subrs; /* (Type 1/2 and CIDFontType 0) */
206
    gs_subr_info_t global_subrs; /* (Type 2 and CIDFontType 0) */
207
    gs_font_cid0 *parent; /* (Type 1 subfont) => parent CIDFontType 0 */
208
    gs_font_dir *dir;
209
    bool ordered;
210
};
211
extern_st(st_gs_font_info);
212
static
213
397k
ENUM_PTRS_WITH(gs_copied_font_data_enum_ptrs, gs_copied_font_data_t *cfdata)
214
    /* See comments in gs_copy_font() below regarding the enumeration of names
215
     * and the font's 'dir' member
216
     */
217
116k
    if (index == 12 && cfdata->dir != NULL) {
218
120
        gs_copied_glyph_name_t *names = cfdata->names;
219
120
        gs_copied_glyph_extra_name_t *en = cfdata->extra_names;
220
120
        int i;
221
222
120
        if (names != NULL)
223
237k
            for (i = 0; i < cfdata->glyphs_size; ++i)
224
237k
                if (names[i].glyph < gs_c_min_std_encoding_glyph)
225
105k
                    cfdata->dir->ccache.mark_glyph(mem, names[i].glyph, NULL);
226
120
        for (; en != NULL; en = en->next)
227
0
            if (en->name.glyph < gs_c_min_std_encoding_glyph)
228
0
                cfdata->dir->ccache.mark_glyph(mem, en->name.glyph, NULL);
229
120
    }
230
116k
    return ENUM_USING(st_gs_font_info, &cfdata->info, sizeof(gs_font_info_t), index - 12);
231
116k
    ENUM_PTR3(0, gs_copied_font_data_t, glyphs, names, extra_names);
232
23.3k
    ENUM_PTR3(3, gs_copied_font_data_t, data, Encoding, CIDMap);
233
23.3k
    ENUM_PTR3(6, gs_copied_font_data_t, subrs.data, subrs.starts, global_subrs.data);
234
397k
    ENUM_PTR3(9, gs_copied_font_data_t, global_subrs.starts, parent, dir);
235
397k
ENUM_PTRS_END
236
237
23.3k
static RELOC_PTRS_WITH(gs_copied_font_data_reloc_ptrs, gs_copied_font_data_t *cfdata)
238
23.3k
{
239
23.3k
    RELOC_PTR3(gs_copied_font_data_t, glyphs, names, extra_names);
240
23.3k
    RELOC_PTR3(gs_copied_font_data_t, data, Encoding, CIDMap);
241
23.3k
    RELOC_PTR3(gs_copied_font_data_t, subrs.data, subrs.starts, global_subrs.data);
242
23.3k
    RELOC_PTR3(gs_copied_font_data_t, global_subrs.starts, parent, dir);
243
23.3k
    RELOC_USING(st_gs_font_info, &cfdata->info, sizeof(gs_font_info_t));
244
23.3k
}
245
23.3k
RELOC_PTRS_END
246
247
gs_private_st_composite(st_gs_copied_font_data, gs_copied_font_data_t, "gs_copied_font_data_t",\
248
    gs_copied_font_data_enum_ptrs, gs_copied_font_data_reloc_ptrs);
249
250
static inline gs_copied_font_data_t *
251
cf_data(const gs_font *font)
252
59.1M
{
253
59.1M
    return (gs_copied_font_data_t *)font->client_data;
254
59.1M
}
255
256
/* ================ Procedures ================ */
257
258
/* ---------------- Private utilities ---------------- */
259
260
/* Copy a string.  Return 0 or gs_error_VMerror. */
261
static int
262
copy_string(gs_memory_t *mem, gs_const_string *pstr, client_name_t cname)
263
3.54M
{
264
3.54M
    const byte *data = pstr->data;
265
3.54M
    uint size = pstr->size;
266
3.54M
    byte *str;
267
268
3.54M
    if (data == 0)
269
45.2k
        return 0;    /* empty string */
270
3.50M
    str = gs_alloc_string(mem, size, cname);
271
3.50M
    pstr->data = str;
272
3.50M
    if (str == 0)
273
0
        return_error(gs_error_VMerror);
274
3.50M
    memcpy(str, data, size);
275
3.50M
    return 0;
276
3.50M
}
277
278
/* Free a copied string. */
279
static void
280
uncopy_string(gs_memory_t *mem, gs_const_string *pstr, client_name_t cname)
281
132k
{
282
132k
    if (pstr->data)
283
87.4k
        gs_free_const_string(mem, pstr->data, pstr->size, cname);
284
132k
}
285
286
/*
287
 * Allocate an Encoding for a Type 1 or Type 42 font.
288
 */
289
static int
290
copied_Encoding_alloc(gs_font *copied)
291
33.0k
{
292
33.0k
    gs_copied_font_data_t *const cfdata = cf_data(copied);
293
33.0k
    gs_glyph *Encoding = (gs_glyph *)
294
33.0k
        gs_alloc_byte_array(copied->memory, 256, sizeof(*cfdata->Encoding),
295
33.0k
                            "copy_font_type1(Encoding)");
296
33.0k
    int i;
297
298
33.0k
    if (Encoding == 0)
299
0
        return_error(gs_error_VMerror);
300
8.48M
    for (i = 0; i < 256; ++i)
301
8.45M
        Encoding[i] = GS_NO_GLYPH;
302
33.0k
    cfdata->Encoding = Encoding;
303
33.0k
    return 0;
304
33.0k
}
305
306
/*
307
 * Allocate and set up data copied from a TrueType or CID font.
308
 * stell(*s) + extra is the length of the data.
309
 */
310
static int
311
copied_data_alloc(gs_font *copied, stream *s, uint extra, int code)
312
14.3k
{
313
14.3k
    gs_copied_font_data_t *const cfdata = cf_data(copied);
314
14.3k
    uint len = stell(s);
315
14.3k
    byte *fdata;
316
317
14.3k
    if (code < 0)
318
5.46k
        return code;
319
8.87k
    fdata = gs_alloc_bytes(copied->memory, len + extra, "copied_data_alloc");
320
8.87k
    if (fdata == 0)
321
0
        return_error(gs_error_VMerror);
322
8.87k
    s_init(s, copied->memory);
323
8.87k
    swrite_string(s, fdata, len);
324
8.87k
    cfdata->data = fdata;
325
8.87k
    cfdata->data_size = len + extra;
326
8.87k
    return 0;
327
8.87k
}
328
329
/*
330
 * Copy Subrs or GSubrs from a font.
331
 */
332
static int
333
copy_subrs(gs_font_type1 *pfont, bool global, gs_subr_info_t *psi,
334
           gs_memory_t *mem)
335
37.5k
{
336
37.5k
    int i, code;
337
37.5k
    uint size;
338
37.5k
    gs_glyph_data_t gdata;
339
37.5k
    byte *data;
340
37.5k
    uint *starts;
341
342
37.5k
    gdata.memory = pfont->memory;
343
    /* Scan the font to determine the size of the subrs. */
344
37.5k
    for (i = 0, size = 0;
345
510k
         (code = pfont->data.procs.subr_data(pfont, i, global, &gdata)) !=
346
510k
             gs_error_rangecheck;
347
473k
         ++i) {
348
473k
        if (code >= 0) {
349
473k
            size += gdata.bits.size;
350
473k
            gs_glyph_data_free(&gdata, "copy_subrs");
351
473k
        }
352
473k
    }
353
37.5k
    if (size == 0)
354
20.9k
        data = 0, starts = 0, i = 0;
355
16.6k
    else {
356
        /* Allocate the copy. */
357
16.6k
        data = gs_alloc_bytes(mem, size, "copy_subrs(data)");
358
16.6k
        starts = (uint *)gs_alloc_byte_array(mem, i + 1, sizeof(*starts),
359
16.6k
                                             "copy_subrs(starts)");
360
16.6k
        if (data == 0 || starts == 0) {
361
0
            gs_free_object(mem, starts, "copy_subrs(starts)");
362
0
            gs_free_object(mem, data, "copy_subrs(data)");
363
0
            return_error(gs_error_VMerror);
364
0
        }
365
366
        /* Copy the data. */
367
16.6k
        for (i = 0, size = 0;
368
489k
             (code = pfont->data.procs.subr_data(pfont, i, global, &gdata)) !=
369
489k
                 gs_error_rangecheck;
370
473k
             ++i) {
371
473k
            starts[i] = size;
372
473k
            if (code >= 0) {
373
473k
                memcpy(data + size, gdata.bits.data, gdata.bits.size);
374
473k
                size += gdata.bits.size;
375
473k
                gs_glyph_data_free(&gdata, "copy_subrs");
376
473k
            }
377
473k
        }
378
16.6k
        starts[i] = size;
379
16.6k
    }
380
381
37.5k
    psi->data = data;
382
37.5k
    psi->starts = starts;
383
37.5k
    psi->count = i;
384
37.5k
    return 0;
385
37.5k
}
386
387
/*
388
 * Return a pointer to the definition of a copied glyph, accessed either by
389
 * name or by glyph number.  If the glyph is out of range, return
390
 * gs_error_rangecheck; if the glyph is in range but undefined, store a
391
 * pointer to the slot where it would be added, which will have gdata.data
392
 * == 0, and return gs_error_undefined; if the glyph is defined, store the
393
 * pointer and return 0.
394
 *
395
 * NOTE:
396
 * The interim variable (idx) is used here as a workaround for what appears
397
 * to be a compiler optimiser bug in VS2019 - using "glyph - GS_MIN_CID_GLYPH"
398
 * directly to index into the cfdata->glyphs array results in a pointer value
399
 * in *pslot which is complete nonsense. Using the variable to store the
400
 * calculated value results in working code.
401
 */
402
static int
403
copied_glyph_slot(gs_copied_font_data_t *cfdata, gs_glyph glyph,
404
                  gs_copied_glyph_t **pslot)
405
24.6M
{
406
24.6M
    uint gsize = cfdata->glyphs_size;
407
24.6M
    unsigned int idx;
408
409
24.6M
    *pslot = 0;
410
24.6M
    if (glyph >= GS_MIN_GLYPH_INDEX) {
411
        /* CIDFontType 2 uses glyph indices for slots.  */
412
1.10M
        idx = (unsigned int)(glyph - GS_MIN_GLYPH_INDEX);
413
1.10M
        if (idx >= gsize)
414
8.23k
            return_error(gs_error_rangecheck);
415
1.09M
        *pslot = &cfdata->glyphs[idx];
416
23.5M
    } else if (glyph >= GS_MIN_CID_GLYPH) {
417
        /* CIDFontType 0 uses CIDS for slots.  */
418
79.6k
        idx = (unsigned int)(glyph - GS_MIN_CID_GLYPH);
419
79.6k
        if (idx >= gsize)
420
160
            return_error(gs_error_rangecheck);
421
79.5k
        *pslot = &cfdata->glyphs[idx];
422
23.4M
    } else if (cfdata->names == 0)
423
0
        return_error(gs_error_rangecheck);
424
23.4M
    else {
425
23.4M
        int code = cfdata->procs->named_glyph_slot(cfdata, glyph, pslot);
426
427
23.4M
        if (code < 0)
428
197k
            return code;
429
23.4M
    }
430
24.4M
    if (!(*pslot)->used)
431
7.43M
        return_error(gs_error_undefined);
432
17.0M
    return 0;
433
24.4M
}
434
static int
435
named_glyph_slot_none(gs_copied_font_data_t *cfdata, gs_glyph glyph,
436
                        gs_copied_glyph_t **pslot)
437
0
{
438
0
    return_error(gs_error_rangecheck);
439
0
}
440
static int
441
named_glyph_slot_hashed(gs_copied_font_data_t *cfdata, gs_glyph glyph,
442
                        gs_copied_glyph_t **pslot)
443
22.9M
{
444
22.9M
    uint gsize = cfdata->glyphs_size;
445
22.9M
    gs_copied_glyph_name_t *names = cfdata->names;
446
22.9M
    uint hash = (uint)glyph % gsize;
447
    /*
448
     * gsize is either a prime number or a power of 2.
449
     * If it is prime, any positive reprobe below gsize guarantees that we
450
     * will touch every slot.
451
     * If it is a power of 2, any odd reprobe guarantees that we
452
     * will touch every slot.
453
     */
454
22.9M
    uint hash2 = ((uint)glyph / gsize * 2 + 1) % gsize;
455
22.9M
    uint tries = gsize;
456
457
26.1M
    while (names[hash].str.data != 0 && names[hash].glyph != glyph) {
458
3.22M
        hash = (hash + hash2) % gsize;
459
3.22M
        if (!tries)
460
0
    return_error(gs_error_undefined);
461
3.22M
        tries--;
462
3.22M
    }
463
22.9M
    *pslot = &cfdata->glyphs[hash];
464
22.9M
    return 0;
465
22.9M
}
466
static int
467
named_glyph_slot_linear(gs_copied_font_data_t *cfdata, gs_glyph glyph,
468
                        gs_copied_glyph_t **pslot)
469
1.93M
{
470
1.93M
    {
471
1.93M
        gs_copied_glyph_name_t *names = cfdata->names;
472
1.93M
        int i;
473
474
107M
        for (i = 0; i < cfdata->glyphs_size; ++i)
475
106M
            if (names[i].glyph == glyph) {
476
1.58M
                *pslot = &cfdata->glyphs[i];
477
1.58M
                return 0;
478
1.58M
            }
479
1.93M
    }
480
    /* This might be a glyph with multiple names.  Search extra_names. */
481
344k
    {
482
344k
        gs_copied_glyph_extra_name_t *extra_name = cfdata->extra_names;
483
484
2.61M
        for (; extra_name != 0; extra_name = extra_name->next)
485
2.29M
            if (extra_name->name.glyph == glyph) {
486
25.5k
                *pslot = &cfdata->glyphs[extra_name->gid];
487
25.5k
                return 0;
488
25.5k
            }
489
344k
    }
490
344k
    return_error(gs_error_rangecheck);
491
344k
}
492
493
/*
494
 * Add glyph data to the glyph table.  This handles copying the vector
495
 * data, detecting attempted redefinitions, and freeing temporary glyph
496
 * data.  The glyph must be an integer, an index in the glyph table.
497
 * Return 1 if the glyph was already defined, 0 if newly added (or an
498
 * error per options).
499
 */
500
static int
501
copy_glyph_data(gs_font *font, gs_glyph glyph, gs_font *copied, int options,
502
                gs_glyph_data_t *pgdata, const byte *prefix, int prefix_bytes)
503
7.73M
{
504
7.73M
    gs_copied_font_data_t *const cfdata = cf_data(copied);
505
7.73M
    uint size = pgdata->bits.size;
506
7.73M
    gs_copied_glyph_t *pcg = 0;
507
7.73M
    int code = copied_glyph_slot(cfdata, glyph, &pcg);
508
509
7.73M
    if (cfdata->ordered)
510
0
        return_error(gs_error_unregistered); /* Must not happen. */
511
7.73M
    switch (code) {
512
576k
    case 0:     /* already defined */
513
576k
        if ((options & COPY_GLYPH_NO_OLD) ||
514
576k
            pcg->gdata.size != prefix_bytes + size ||
515
576k
            (prefix_bytes > 0 && memcmp(pcg->gdata.data, prefix, prefix_bytes)) ||
516
576k
            (size > 0 && memcmp(pcg->gdata.data + prefix_bytes,
517
468k
                   pgdata->bits.data, size))
518
576k
            )
519
0
            code = gs_note_error(gs_error_invalidaccess);
520
576k
        else
521
576k
            code = 1;
522
576k
        break;
523
7.15M
    case gs_error_undefined:
524
7.15M
        if (options & COPY_GLYPH_NO_NEW)
525
425
            code = gs_note_error(gs_error_undefined);
526
7.15M
        else if (pcg == NULL)
527
0
            code = gs_note_error(gs_error_undefined);
528
7.15M
        else {
529
7.15M
            uint str_size = prefix_bytes + size;
530
531
7.15M
            code = 0;
532
7.15M
            if (str_size > 0) {
533
7.12M
                byte *str = gs_alloc_string(copied->memory, str_size,
534
7.12M
                                            "copy_glyph_data(data)");
535
536
7.12M
                if (str == 0)
537
0
                    code = gs_note_error(gs_error_VMerror);
538
7.12M
                else {
539
7.12M
                    if (prefix_bytes)
540
603
                        memcpy(str, prefix, prefix_bytes);
541
7.12M
                    memcpy(str + prefix_bytes, pgdata->bits.data, size);
542
7.12M
                    pcg->gdata.data = str;
543
7.12M
                }
544
7.12M
            }
545
7.15M
            if (code >= 0) {
546
7.15M
                pcg->gdata.size = str_size;
547
7.15M
                pcg->used = HAS_DATA;
548
7.15M
                pcg->order_index = -1;
549
7.15M
                code = 0;
550
7.15M
                cfdata->num_glyphs++;
551
7.15M
            }
552
7.15M
        }
553
7.15M
    default:
554
7.15M
        break;
555
7.73M
    }
556
7.73M
    gs_glyph_data_free(pgdata, "copy_glyph_data");
557
7.73M
    return code;
558
7.73M
}
559
560
/*
561
 * Copy a glyph name into the names table.
562
 */
563
static int
564
copy_glyph_name(gs_font *font, gs_glyph glyph, gs_font *copied,
565
                gs_glyph copied_glyph)
566
7.17M
{
567
7.17M
    gs_glyph known_glyph;
568
7.17M
    gs_copied_font_data_t *const cfdata = cf_data(copied);
569
7.17M
    gs_copied_glyph_t *pcg;
570
7.17M
    int code = copied_glyph_slot(cfdata, copied_glyph, &pcg);
571
7.17M
    gs_copied_glyph_name_t *pcgn;
572
7.17M
    gs_const_string str;
573
574
7.17M
    if (cfdata->ordered)
575
0
        return_error(gs_error_unregistered); /* Must not happen. */
576
7.17M
    if (code < 0 ||
577
7.17M
        (code = font->procs.glyph_name(font, glyph, &str)) < 0
578
7.17M
        )
579
0
        return code;
580
    /* Try to share a permanently allocated known glyph name. */
581
7.17M
    if ((known_glyph = gs_c_name_glyph(str.data, str.size)) != GS_NO_GLYPH)
582
3.76M
        gs_c_glyph_name(known_glyph, &str);
583
3.40M
    else if ((code = copy_string(copied->memory, &str, "copy_glyph_name")) < 0)
584
0
        return code;
585
7.17M
    pcgn = cfdata->names + (pcg - cfdata->glyphs);
586
7.17M
    if (pcgn->glyph != GS_NO_GLYPH &&
587
7.17M
        (pcgn->str.size != str.size ||
588
92.3k
         memcmp(pcgn->str.data, str.data, str.size))
589
7.17M
        ) {
590
        /* This is a glyph with multiple names.  Add an extra_name entry. */
591
25.5k
        gs_copied_glyph_extra_name_t *extra_name =
592
25.5k
            gs_alloc_struct(copied->memory, gs_copied_glyph_extra_name_t,
593
25.5k
                            &st_gs_copied_glyph_extra_name,
594
25.5k
                            "copy_glyph_name(extra_name)");
595
596
25.5k
        if (extra_name == 0)
597
0
            return_error(gs_error_VMerror);
598
25.5k
        memset(extra_name, 0x00, sizeof(gs_copied_glyph_extra_name_t));
599
25.5k
        extra_name->next = cfdata->extra_names;
600
25.5k
        extra_name->gid = pcg - cfdata->glyphs;
601
25.5k
        cfdata->extra_names = extra_name;
602
25.5k
        pcgn = &extra_name->name;
603
25.5k
    }
604
7.17M
    if (pcgn->str.size != 0 && !gs_is_c_glyph_name(pcgn->str.data, pcgn->str.size))
605
1.47k
        gs_free_string(copied->memory, (byte *)pcgn->str.data, pcgn->str.size, "Free copied glyph name");
606
7.17M
    pcgn->glyph = glyph;
607
7.17M
    pcgn->str = str;
608
7.17M
    return 0;
609
7.17M
}
610
611
/*
612
 * Find the .notdef glyph in a font.
613
 */
614
static gs_glyph
615
find_notdef(gs_font_base *font)
616
33.0k
{
617
33.0k
    int index = 0;
618
33.0k
    gs_glyph glyph;
619
620
4.28M
    while (font->procs.enumerate_glyph((gs_font *)font, &index,
621
4.28M
                                       GLYPH_SPACE_NAME, &glyph),
622
4.28M
           index != 0)
623
4.27M
        if (gs_font_glyph_is_notdef(font, glyph))
624
28.5k
            return glyph;
625
4.48k
    return GS_NO_GLYPH;   /* best we can do */
626
33.0k
}
627
628
/*
629
 * Add an Encoding entry to a character-indexed font (Type 1/2/42).
630
 */
631
static int
632
copied_char_add_encoding(gs_font *copied, gs_char chr, gs_glyph glyph)
633
3.09M
{
634
3.09M
    gs_copied_font_data_t *const cfdata = cf_data(copied);
635
3.09M
    gs_glyph *Encoding = cfdata->Encoding;
636
3.09M
    gs_copied_glyph_t *pslot;
637
3.09M
    int code;
638
639
3.09M
    if (cfdata->ordered)
640
0
        return_error(gs_error_unregistered); /* Must not happen. */
641
3.09M
    if (Encoding == 0)
642
0
        return_error(gs_error_invalidaccess);
643
3.09M
    if (chr >= 256 || glyph >= GS_MIN_CID_GLYPH)
644
0
        return_error(gs_error_rangecheck);
645
3.09M
    code = copied_glyph_slot(cfdata, glyph, &pslot);
646
3.09M
    if (code < 0)
647
287k
        return code;
648
2.80M
    if (Encoding[chr] != glyph && Encoding[chr] != GS_NO_GLYPH)
649
31
        return_error(gs_error_invalidaccess);
650
2.80M
    Encoding[chr] = glyph;
651
2.80M
    return 0;
652
2.80M
}
653
654
/* Don't allow adding an Encoding entry. */
655
static int
656
copied_no_add_encoding(gs_font *copied, gs_char chr, gs_glyph glyph)
657
0
{
658
0
    return_error(gs_error_invalidaccess);
659
0
}
660
661
/* ---------------- Font procedures ---------------- */
662
663
static int
664
copied_font_info(gs_font *font, const gs_point *pscale, int members,
665
                 gs_font_info_t *info)
666
55.7k
{
667
55.7k
    if (pscale != 0)
668
0
        return_error(gs_error_rangecheck);
669
55.7k
    *info = cf_data(font)->info;
670
55.7k
    return 0;
671
55.7k
}
672
673
static gs_glyph
674
copied_encode_char(gs_font *copied, gs_char chr, gs_glyph_space_t glyph_space)
675
13.1M
{
676
13.1M
    gs_copied_font_data_t *const cfdata = cf_data(copied);
677
13.1M
    const gs_glyph *Encoding = cfdata->Encoding;
678
679
13.1M
    if (chr >= 256 || Encoding == 0)
680
0
        return GS_NO_GLYPH;
681
13.1M
    return Encoding[chr];
682
13.1M
}
683
684
static int
685
copied_enumerate_glyph(gs_font *font, int *pindex,
686
                       gs_glyph_space_t glyph_space, gs_glyph *pglyph)
687
1.24M
{
688
1.24M
    gs_copied_font_data_t *const cfdata = cf_data(font);
689
690
1.24M
    if (cfdata->ordered) {
691
700k
        if (*pindex >= cfdata->num_glyphs)
692
33.6k
            *pindex = 0;
693
666k
        else {
694
666k
            int i = cfdata->glyphs[*pindex].order_index;
695
696
666k
            *pglyph = cfdata->names[i].glyph;
697
666k
            ++(*pindex);
698
666k
        }
699
700k
        return 0;
700
700k
    }
701
133M
    for (; *pindex < cfdata->glyphs_size; ++*pindex)
702
133M
        if (cfdata->glyphs[*pindex].used) {
703
519k
            *pglyph =
704
519k
                (glyph_space == GLYPH_SPACE_NAME && cfdata->names != 0 ?
705
197k
                 cfdata->names[*pindex].glyph :
706
                 /* CIDFontType 0 uses CIDS as slot indices; CIDFontType 2 uses GIDs. */
707
519k
                 *pindex + (glyph_space == GLYPH_SPACE_NAME
708
322k
                            ? GS_MIN_CID_GLYPH : GS_MIN_GLYPH_INDEX));
709
519k
            ++(*pindex);
710
519k
            return 0;
711
519k
        }
712
24.7k
    *pindex = 0;
713
24.7k
    return 0;
714
543k
}
715
716
static int
717
copied_glyph_name(gs_font *font, gs_glyph glyph, gs_const_string *pstr)
718
2.05M
{
719
2.05M
    gs_copied_font_data_t *const cfdata = cf_data(font);
720
2.05M
    gs_copied_glyph_t *pcg;
721
722
2.05M
    if (glyph >= GS_MIN_CID_GLYPH)
723
33
        return_error(gs_error_rangecheck);
724
2.05M
    if (copied_glyph_slot(cfdata, glyph, &pcg) < 0)
725
0
        return_error(gs_error_undefined);
726
2.05M
    *pstr = cfdata->names[pcg - cfdata->glyphs].str;
727
2.05M
    return 0;
728
2.05M
}
729
730
static int
731
copied_build_char(gs_show_enum *pte, gs_gstate *pgs, gs_font *font,
732
                  gs_char chr, gs_glyph glyph)
733
0
{
734
0
    int wmode = font->WMode;
735
0
    int code;
736
0
    gs_glyph_info_t info;
737
0
    double wxy[6];
738
0
    double sbw_stub[4]; /* Currently glyph_outline retrieves sbw only with type 1,2,9 fonts. */
739
740
0
    if (glyph == GS_NO_GLYPH) {
741
0
        glyph = font->procs.encode_char(font, chr, GLYPH_SPACE_INDEX);
742
0
        if (glyph == GS_NO_GLYPH)
743
0
            glyph = cf_data(font)->notdef;
744
0
    }
745
    /*
746
     * Type 1/2 outlines don't require a current point, but TrueType
747
     * outlines do.  We might want to fix this someday....
748
     */
749
0
    if ((code = gs_moveto(pgs, 0.0, 0.0)) < 0 ||
750
0
        (code = font->procs.glyph_info(font, glyph, NULL,
751
0
                                       (GLYPH_INFO_WIDTH << wmode) |
752
0
                                       GLYPH_INFO_BBOX |
753
0
                                       GLYPH_INFO_OUTLINE_WIDTHS,
754
0
                                       &info)) < 0
755
0
        )
756
0
        return code;
757
0
    wxy[0] = info.width[wmode].x;
758
0
    wxy[1] = info.width[wmode].y;
759
0
    wxy[2] = info.bbox.p.x;
760
0
    wxy[3] = info.bbox.p.y;
761
0
    wxy[4] = info.bbox.q.x;
762
0
    wxy[5] = info.bbox.q.y;
763
0
    if ((code = gs_setcachedevice_double(pte, pte->pgs, wxy)) < 0 ||
764
0
        (code = font->procs.glyph_outline(font, wmode, glyph, &ctm_only(pgs),
765
0
                                          pgs->path, sbw_stub)) < 0
766
0
        )
767
0
        return code;
768
0
    if (font->PaintType != 0) {
769
0
        gs_setlinewidth(pgs, font->StrokeWidth);
770
0
        return gs_stroke(pgs);
771
0
    } else {
772
0
        return gs_fill(pgs);
773
0
    }
774
0
}
775
776
static inline bool
777
compare_arrays(const float *v0, int l0, const float *v1, int l1)
778
13.1M
{
779
13.1M
    if (l0 != l1)
780
13
        return false;
781
13.1M
    if (memcmp(v0, v1, l0 * sizeof(v0[0])))
782
25
        return false;
783
13.1M
    return true;
784
13.1M
}
785
786
13.1M
#define compare_tables(a, b) compare_arrays(a.values, a.count, b.values, b.count)
787
788
static int
789
compare_glyphs(const gs_font *cfont, const gs_font *ofont, gs_glyph *glyphs,
790
                           int num_glyphs, int glyphs_step, int level)
791
2.46M
{
792
    /*
793
     * Checking widths because we can synthesize fonts from random fonts
794
     * having same FontName and FontType.
795
     * We must request width explicitely because Type 42 stores widths
796
     * separately from outline data. We could skip it for Type 1, which doesn't.
797
     * We don't care of Metrics, Metrics2 because copied font never has them.
798
     */
799
2.46M
    int i, WMode = ofont->WMode;
800
2.46M
    int members = (GLYPH_INFO_WIDTH0 << WMode) | GLYPH_INFO_OUTLINE_WIDTHS | GLYPH_INFO_NUM_PIECES;
801
2.46M
    gs_matrix mat;
802
2.46M
    gs_copied_font_data_t *const cfdata = cf_data(cfont);
803
2.46M
    int num_new_glyphs = 0;
804
805
2.46M
    gs_make_identity(&mat);
806
3.29M
    for (i = 0; i < num_glyphs; i++) {
807
888k
        gs_glyph glyph = *(gs_glyph *)((byte *)glyphs + i * glyphs_step);
808
888k
        gs_glyph pieces0[40], *pieces = pieces0;
809
888k
        gs_glyph_info_t info0, info1;
810
888k
        int code0, code1, code2, code;
811
812
888k
        memset(&info0, 0x00, sizeof(gs_glyph_info_t));
813
888k
        code0 = ofont->procs.glyph_info((gs_font *)ofont, glyph, &mat, members, &info0);
814
888k
        memset(&info1, 0x00, sizeof(gs_glyph_info_t));
815
888k
        code1 = cfont->procs.glyph_info((gs_font *)cfont, glyph, &mat, members, &info1);
816
817
888k
        if (code0 == gs_error_undefined)
818
3.32k
            continue;
819
884k
        if (code1 == gs_error_undefined) {
820
223k
            num_new_glyphs++;
821
223k
            if (num_new_glyphs > cfdata->glyphs_size - cfdata->num_glyphs)
822
49.8k
                return 0;
823
174k
            continue;
824
223k
        }
825
660k
        if (code0 < 0)
826
45
            return code0;
827
660k
        if (code1 < 0)
828
3
            return code1;
829
660k
        if (info0.num_pieces != info1.num_pieces)
830
5
            return 0;
831
660k
        if (info0.num_pieces > 0) {
832
5.55k
            if(level > 5)
833
0
                return_error(gs_error_rangecheck); /* abnormal glyph recursion */
834
5.55k
            if (info0.num_pieces > countof(pieces0) / 2) {
835
0
                pieces = (gs_glyph *)gs_alloc_bytes(cfont->memory,
836
0
                    sizeof(gs_glyph) * info0.num_pieces * 2, "compare_glyphs");
837
0
                if (pieces == 0)
838
0
                    return_error(gs_error_VMerror);
839
0
            }
840
5.55k
            info0.pieces = pieces;
841
5.55k
            info1.pieces = pieces + info0.num_pieces;
842
5.55k
            code0 = ofont->procs.glyph_info((gs_font *)ofont, glyph, &mat,
843
5.55k
                                    GLYPH_INFO_PIECES, &info0);
844
5.55k
            code1 = cfont->procs.glyph_info((gs_font *)cfont, glyph, &mat,
845
5.55k
                                    GLYPH_INFO_PIECES, &info1);
846
5.55k
            if (code0 >= 0 && code1 >= 0) {
847
5.55k
                code2 = memcmp(info0.pieces, info1.pieces, info0.num_pieces * sizeof(*pieces));
848
5.55k
                if (!code2)
849
5.55k
                    code = compare_glyphs(cfont, ofont, pieces, info0.num_pieces, glyphs_step, level + 1);
850
0
                else
851
0
                    code = 0; /* Quiet compiler. */
852
5.55k
            } else
853
0
                code2 = code = 0;
854
5.55k
            if (pieces != pieces0)
855
0
                gs_free_object(cfont->memory, pieces, "compare_glyphs");
856
5.55k
            if (code0 == gs_error_undefined)
857
0
                continue;
858
5.55k
            if (code1 == gs_error_undefined) {
859
0
                num_new_glyphs++;
860
0
                if (num_new_glyphs > cfdata->glyphs_size - cfdata->num_glyphs)
861
0
                    return 0;
862
0
                continue;
863
0
            }
864
5.55k
            if (code0 < 0)
865
0
                return code0;
866
5.55k
            if (code1 < 0)
867
0
                return code1;
868
5.55k
            if (code2 || code == 0) {
869
0
                return 0;
870
0
            }
871
655k
        } else {
872
655k
            gs_glyph_data_t gdata0, gdata1;
873
874
655k
            switch(cfont->FontType) {
875
452k
                case ft_encrypted:
876
453k
                case ft_encrypted2: {
877
453k
                    gs_font_type1 *font0 = (gs_font_type1 *)cfont;
878
453k
                    gs_font_type1 *font1 = (gs_font_type1 *)ofont;
879
880
453k
                    gdata0.memory = font0->memory;
881
453k
                    gdata1.memory = font1->memory;
882
453k
                    code0 = font0->data.procs.glyph_data(font0, glyph, &gdata0);
883
453k
                    code1 = font1->data.procs.glyph_data(font1, glyph, &gdata1);
884
453k
                    break;
885
452k
                }
886
859
                case ft_TrueType:
887
198k
                case ft_CID_TrueType: {
888
198k
                    gs_font_type42 *font0 = (gs_font_type42 *)cfont;
889
198k
                    gs_font_type42 *font1 = (gs_font_type42 *)ofont;
890
198k
                    uint glyph_index0 = font0->data.get_glyph_index(font0, glyph);
891
198k
                    uint glyph_index1 = font1->data.get_glyph_index(font1, glyph);
892
893
198k
                    gdata0.memory = font0->memory;
894
198k
                    gdata1.memory = font1->memory;
895
198k
                    code0 = font0->data.get_outline(font0, glyph_index0, &gdata0);
896
198k
                    code1 = font1->data.get_outline(font1, glyph_index1, &gdata1);
897
198k
                    break;
898
859
                }
899
3.52k
                case ft_CID_encrypted: {
900
3.52k
                    gs_font_cid0 *font0 = (gs_font_cid0 *)cfont;
901
3.52k
                    gs_font_cid0 *font1 = (gs_font_cid0 *)ofont;
902
3.52k
                    int fidx0, fidx1;
903
904
3.52k
                    gdata0.memory = font0->memory;
905
3.52k
                    gdata1.memory = font1->memory;
906
3.52k
                    code0 = font0->cidata.glyph_data((gs_font_base *)font0, glyph, &gdata0, &fidx0);
907
3.52k
                    code1 = font1->cidata.glyph_data((gs_font_base *)font1, glyph, &gdata1, &fidx1);
908
3.52k
                    break;
909
859
                }
910
0
                default:
911
0
                    return_error(gs_error_unregistered); /* unimplemented */
912
655k
            }
913
655k
            if (code0 < 0) {
914
5.55k
                if (code1 >= 0)
915
0
                    gs_glyph_data_free(&gdata1, "compare_glyphs");
916
5.55k
                return code0;
917
5.55k
            }
918
649k
            if (code1 < 0) {
919
0
                if (code0 >= 0)
920
0
                    gs_glyph_data_free(&gdata0, "compare_glyphs");
921
0
                return code1;
922
0
            }
923
649k
            if (gdata0.bits.size != gdata1.bits.size)
924
0
                return 0;
925
649k
            if (memcmp(gdata0.bits.data, gdata0.bits.data, gdata0.bits.size))
926
0
                return 0;
927
649k
            gs_glyph_data_free(&gdata0, "compare_glyphs");
928
649k
            gs_glyph_data_free(&gdata1, "compare_glyphs");
929
649k
        }
930
660k
    }
931
2.40M
    return 1;
932
2.46M
}
933
934
/* ---------------- Individual FontTypes ---------------- */
935
936
/* ------ Type 1 ------ */
937
938
static int
939
copied_type1_glyph_data(gs_font_type1 * pfont, gs_glyph glyph,
940
                        gs_glyph_data_t *pgd)
941
3.80M
{
942
3.80M
    gs_copied_font_data_t *const cfdata = cf_data((gs_font *)pfont);
943
3.80M
    gs_copied_glyph_t *pslot;
944
3.80M
    int code = copied_glyph_slot(cfdata, glyph, &pslot);
945
946
3.80M
    if (code < 0)
947
127k
        return code;
948
3.68M
    gs_glyph_data_from_string(pgd, pslot->gdata.data, pslot->gdata.size,
949
3.68M
                              NULL);
950
3.68M
    return 0;
951
3.80M
}
952
953
static int
954
copied_type1_subr_data(gs_font_type1 * pfont, int subr_num, bool global,
955
                       gs_glyph_data_t *pgd)
956
3.68M
{
957
3.68M
    gs_copied_font_data_t *const cfdata = cf_data((gs_font *)pfont);
958
3.68M
    const gs_subr_info_t *psi =
959
3.68M
        (global ? &cfdata->global_subrs : &cfdata->subrs);
960
961
3.68M
    if (subr_num < 0 || subr_num >= psi->count)
962
17.8k
        return_error(gs_error_rangecheck);
963
3.66M
    gs_glyph_data_from_string(pgd, psi->data + psi->starts[subr_num],
964
3.66M
                              psi->starts[subr_num + 1] -
965
3.66M
                                psi->starts[subr_num],
966
3.66M
                              NULL);
967
3.66M
    return 0;
968
3.68M
}
969
970
static int
971
copied_type1_seac_data(gs_font_type1 * pfont, int ccode,
972
                       gs_glyph * pglyph, gs_const_string *gstr, gs_glyph_data_t *pgd)
973
66
{
974
    /*
975
     * This can only be invoked if the components have already been
976
     * copied to their proper positions, so it is simple.
977
     */
978
66
    gs_glyph glyph = gs_c_known_encode((gs_char)ccode, ENCODING_INDEX_STANDARD);
979
66
    gs_glyph glyph1;
980
66
    int code;
981
982
66
    if (glyph == GS_NO_GLYPH)
983
28
        return_error(gs_error_rangecheck);
984
38
    code = gs_c_glyph_name(glyph, gstr);
985
38
    if (code < 0)
986
0
        return code;
987
38
    code = pfont->dir->global_glyph_code((gs_font *)pfont, gstr, &glyph1);
988
38
    if (code < 0)
989
0
        return code;
990
38
    if (pglyph)
991
2
        *pglyph = glyph1;
992
38
    if (pgd)
993
36
        return copied_type1_glyph_data(pfont, glyph1, pgd);
994
2
    else
995
2
        return 0;
996
38
}
997
998
static int
999
copied_type1_push_values(void *callback_data, const fixed *values, int count)
1000
0
{
1001
0
    return_error(gs_error_unregistered);
1002
0
}
1003
1004
static int
1005
copied_type1_pop_value(void *callback_data, fixed *value)
1006
0
{
1007
0
    return_error(gs_error_unregistered);
1008
0
}
1009
1010
static int
1011
copy_font_type1(gs_font *font, gs_font *copied)
1012
18.6k
{
1013
18.6k
    gs_font_type1 *font1 = (gs_font_type1 *)font;
1014
18.6k
    gs_font_type1 *copied1 = (gs_font_type1 *)copied;
1015
18.6k
    gs_copied_font_data_t *const cfdata = cf_data(copied);
1016
18.6k
    int code;
1017
1018
18.6k
    cfdata->notdef = find_notdef((gs_font_base *)font);
1019
18.6k
    code = copied_Encoding_alloc(copied);
1020
18.6k
    if (code < 0)
1021
0
        return code;
1022
18.6k
    if ((code = copy_subrs(font1, false, &cfdata->subrs, copied->memory)) < 0 ||
1023
18.6k
        (code = copy_subrs(font1, true, &cfdata->global_subrs, copied->memory)) < 0
1024
18.6k
        ) {
1025
0
        gs_free_object(copied->memory, cfdata->Encoding,
1026
0
                       "copy_font_type1(Encoding)");
1027
0
        return code;
1028
0
    }
1029
    /*
1030
     * We don't need real push/pop procedures, because we can't do anything
1031
     * useful with fonts that have non-standard OtherSubrs anyway.
1032
     */
1033
18.6k
    copied1->data.procs.glyph_data = copied_type1_glyph_data;
1034
18.6k
    copied1->data.procs.subr_data = copied_type1_subr_data;
1035
18.6k
    copied1->data.procs.seac_data = copied_type1_seac_data;
1036
18.6k
    copied1->data.procs.push_values = copied_type1_push_values;
1037
18.6k
    copied1->data.procs.pop_value = copied_type1_pop_value;
1038
18.6k
    copied1->data.proc_data = 0;
1039
18.6k
    return 0;
1040
18.6k
}
1041
1042
static int
1043
uncopy_glyph_type1(gs_font *font, gs_glyph glyph, gs_font *copied, int options)
1044
0
{
1045
0
    gs_copied_glyph_t *pcg = NULL;
1046
0
    gs_copied_font_data_t *cfdata = cf_data(copied);
1047
1048
0
    (void)copied_glyph_slot(cfdata, glyph, &pcg);
1049
0
    if (pcg != NULL) {
1050
0
        if (pcg->gdata.data != NULL) {
1051
0
            gs_free_string(copied->memory, (byte *)pcg->gdata.data, pcg->gdata.size, "Free copied glyph name");
1052
0
            pcg->gdata.size = 0;
1053
0
            pcg->gdata.data = NULL;
1054
0
        }
1055
0
        pcg->used = 0;
1056
0
        cfdata->num_glyphs--;
1057
0
    }
1058
0
    return 0;
1059
0
}
1060
1061
static int
1062
copy_glyph_type1(gs_font *font, gs_glyph glyph, gs_font *copied, int options)
1063
7.26M
{
1064
7.26M
    gs_glyph_data_t gdata;
1065
7.26M
    gs_font_type1 *font1 = (gs_font_type1 *)font;
1066
7.26M
    int code;
1067
7.26M
    int rcode;
1068
1069
7.26M
    gdata.memory = font->memory;
1070
7.26M
    code = font1->data.procs.glyph_data(font1, glyph, &gdata);
1071
7.26M
    if (code < 0)
1072
3.91k
        return code;
1073
7.25M
    code = copy_glyph_data(font, glyph, copied, options, &gdata, NULL, 0);
1074
7.25M
    if (code < 0)
1075
30
        return code;
1076
7.25M
    rcode = code;
1077
7.25M
    if (code == 0)
1078
7.01M
        code = copy_glyph_name(font, glyph, copied, glyph);
1079
7.25M
    return (code < 0 ? code : rcode);
1080
7.25M
}
1081
1082
static int
1083
copied_type1_glyph_outline(gs_font *font, int WMode, gs_glyph glyph,
1084
                           const gs_matrix *pmat, gx_path *ppath, double sbw[4])
1085
164k
{   /*
1086
     * 'WMode' may be inherited from an upper font.
1087
     * We ignore in because Type 1,2 charstrings do not depend on it.
1088
     */
1089
1090
    /*
1091
     * This code duplicates much of zcharstring_outline in zchar1.c.
1092
     * This is unfortunate, but we don't see a simple way to refactor the
1093
     * code to avoid it.
1094
     */
1095
164k
    gs_glyph_data_t gdata;
1096
164k
    gs_font_type1 *const pfont1 = (gs_font_type1 *)font;
1097
164k
    int code;
1098
164k
    const gs_glyph_data_t *pgd = &gdata;
1099
164k
    gs_type1_state cis;
1100
164k
    gs_gstate ggs;
1101
1102
164k
    memset(&cis, 0x00, sizeof(cis));
1103
1104
164k
    gdata.memory = pfont1->memory;
1105
164k
    code = pfont1->data.procs.glyph_data(pfont1, glyph, &gdata);
1106
164k
    if (code < 0)
1107
0
        return code;
1108
164k
    if (pgd->bits.size <= max(pfont1->data.lenIV, 0))
1109
0
        return_error(gs_error_invalidfont);
1110
    /* Initialize just enough of the gs_gstate. */
1111
164k
    if (pmat)
1112
164k
        gs_matrix_fixed_from_matrix(&ggs.ctm, pmat);
1113
0
    else {
1114
0
        gs_matrix imat;
1115
1116
0
        gs_make_identity(&imat);
1117
0
        gs_matrix_fixed_from_matrix(&ggs.ctm, &imat);
1118
0
    }
1119
164k
    ggs.flatness = 0;
1120
164k
    code = gs_type1_interp_init(&cis, &ggs, ppath, NULL, NULL, true, 0,
1121
164k
                                pfont1);
1122
164k
    if (code < 0)
1123
0
        return code;
1124
164k
    cis.no_grid_fitting = true;
1125
    /* Continue interpreting. */
1126
329k
    for (;;) {
1127
329k
        int value;
1128
1129
329k
        code = pfont1->data.interpret(&cis, pgd, &value);
1130
329k
        switch (code) {
1131
163k
        case 0:   /* all done */
1132
            /* falls through */
1133
164k
        default:    /* code < 0, error */
1134
164k
            return code;
1135
0
        case type1_result_callothersubr: /* unknown OtherSubr */
1136
0
            return_error(gs_error_rangecheck); /* can't handle it */
1137
164k
        case type1_result_sbw: /* [h]sbw, just continue */
1138
164k
            pgd = 0;
1139
164k
            type1_cis_get_metrics(&cis, sbw);
1140
329k
        }
1141
329k
    }
1142
164k
}
1143
1144
static const gs_copied_font_procs_t copied_procs_type1 = {
1145
    copy_font_type1, copy_glyph_type1, copied_char_add_encoding,
1146
    named_glyph_slot_hashed,
1147
    copied_encode_char, gs_type1_glyph_info, copied_type1_glyph_outline,
1148
    uncopy_glyph_type1
1149
};
1150
1151
static void hash_subrs(gs_font_type1 *pfont)
1152
73.0k
{
1153
73.0k
    gs_type1_data *d0 = &pfont->data;
1154
73.0k
    gs_glyph_data_t gdata0;
1155
73.0k
    gs_md5_state_t md5;
1156
73.0k
    int i, exit = 0;
1157
1158
73.0k
    gs_md5_init(&md5);
1159
73.0k
    gdata0.memory = pfont->memory;
1160
    /* Scan the font to hash the global subrs. */
1161
480k
    for (i = 0; !exit; i++) {
1162
480k
        int code0 = pfont->data.procs.subr_data((gs_font_type1 *)pfont,
1163
480k
                                                i, true, &gdata0);
1164
480k
        if (code0 == gs_error_rangecheck)
1165
            /* rangecheck means we ran out of /Subrs */
1166
73.0k
            exit = true;
1167
480k
        if (code0 == gs_error_typecheck)
1168
            /* typecheck means that we may have encoutnered a null object
1169
             * for a Subr, we ignore this subr, but carry on hashing, as there
1170
             * may be more Subrs.
1171
             */
1172
0
            continue;
1173
480k
        if (code0 < 0)
1174
73.0k
            break;
1175
407k
        else {
1176
407k
            gs_md5_append(&md5, gdata0.bits.data, gdata0.bits.size);
1177
407k
            gs_glyph_data_free(&gdata0, "hash_type1_subrs");
1178
407k
        }
1179
480k
    }
1180
    /* For a 'belt and braces' approach, we also record the number of local
1181
     * and global /Subrs, and compare these below as well. Shifting the global
1182
     * subrs up means that we can avoid an accidental co-incidence where simply
1183
     * summing the two sets together might give the same result for different fonts.
1184
     */
1185
73.0k
    d0->num_subrs = i << 16;
1186
73.0k
    exit = 0;
1187
    /* Scan the font to hash the local subrs. */
1188
569k
    for (i = 0; !exit; i++) {
1189
569k
        int code0 = pfont->data.procs.subr_data((gs_font_type1 *)pfont,
1190
569k
                                                i, false, &gdata0);
1191
569k
        if (code0 == gs_error_rangecheck)
1192
            /* rangecheck means we ran out of /Subrs */
1193
73.0k
            exit = true;
1194
569k
        if (code0 == gs_error_typecheck)
1195
            /* typecheck means that we may have encoutnered a null object
1196
             * for a Subr, we ignore this subr, but carry on hashing, as there
1197
             * may be more Subrs.
1198
             */
1199
0
            continue;
1200
569k
        if (code0 < 0)
1201
73.0k
            break;
1202
496k
        else {
1203
496k
            gs_md5_append(&md5, gdata0.bits.data, gdata0.bits.size);
1204
496k
            gs_glyph_data_free(&gdata0, "hash_type1_subrs");
1205
496k
        }
1206
569k
    }
1207
73.0k
    gs_md5_finish(&md5, d0->hash_subrs);
1208
73.0k
    d0->num_subrs += i;
1209
73.0k
}
1210
1211
static bool
1212
same_type1_hinting(const gs_font_type1 *cfont, const gs_font_type1 *ofont)
1213
1.88M
{
1214
1.88M
    const gs_type1_data *d0 = &cfont->data, *d1 = &ofont->data;
1215
1.88M
    int *hash0 = (int *)&d0->hash_subrs;
1216
1.88M
    int *hash1 = (int *)&d1->hash_subrs;
1217
1218
1.88M
    if (d0->lenIV != d1->lenIV)
1219
0
        return false;
1220
    /*
1221
    if (d0->defaultWidthX != d1->defaultWidthX)
1222
        return false;
1223
    if (d0->nominalWidthX != d1->nominalWidthX)
1224
        return false;
1225
    */
1226
1.88M
    if (d0->BlueFuzz != d1->BlueFuzz)
1227
0
        return false;
1228
1.88M
    if (d0->BlueScale != d1->BlueScale)
1229
0
        return false;
1230
1.88M
    if (d0->BlueShift != d1->BlueShift)
1231
0
        return false;
1232
1.88M
    if (d0->ExpansionFactor != d1->ExpansionFactor)
1233
0
        return false;
1234
1.88M
    if (d0->ForceBold != d1->ForceBold)
1235
4
        return false;
1236
1.88M
    if (!compare_tables(d0->FamilyBlues, d1->FamilyBlues))
1237
0
        return false;
1238
1.88M
    if (!compare_tables(d0->FamilyOtherBlues, d1->FamilyOtherBlues))
1239
0
        return false;
1240
1.88M
    if (d0->LanguageGroup != d1->LanguageGroup)
1241
0
        return false;
1242
1.88M
    if (!compare_tables(d0->OtherBlues, d1->OtherBlues))
1243
7
        return false;
1244
1.88M
    if (d0->RndStemUp != d1->RndStemUp)
1245
0
        return false;
1246
1.88M
    if (!compare_tables(d0->StdHW, d1->StdHW))
1247
25
        return false;
1248
1.88M
    if (!compare_tables(d0->StemSnapH, d1->StemSnapH))
1249
6
        return false;
1250
1.88M
    if (!compare_tables(d0->StemSnapV, d1->StemSnapV))
1251
0
        return false;
1252
1.88M
    if (!compare_tables(d0->WeightVector, d1->WeightVector))
1253
0
        return false;
1254
1.88M
    if (hash0[0] == 0x00 && hash0[1] == 0x00 && hash0[2] == 0x00 && hash0[3] == 0x00)
1255
7.66k
        hash_subrs((gs_font_type1 *)cfont);
1256
1.88M
    if (hash1[0] == 0x00 && hash1[1] == 0x00 && hash1[2] == 0x00 && hash1[3] == 0x00)
1257
65.3k
        hash_subrs((gs_font_type1 *)ofont);
1258
1.88M
    if (memcmp(d0->hash_subrs, d1->hash_subrs, 16) != 0 || d0->num_subrs != d1->num_subrs)
1259
0
        return false;
1260
1261
    /*
1262
     *  We ignore differences in OtherSubrs because pdfwrite
1263
     *  must build without PS interpreter and therefore copied font
1264
     *  have no storage for them.
1265
     */
1266
1.88M
    return true;
1267
1.88M
}
1268
1269
/* ------ Type 42 ------ */
1270
1271
static int
1272
copied_type42_string_proc(gs_font_type42 *font, ulong offset, uint len,
1273
                          const byte **pstr)
1274
10.0M
{
1275
10.0M
    gs_copied_font_data_t *const cfdata = font->data.proc_data;
1276
1277
10.0M
    if (offset + len > cfdata->data_size)
1278
0
        return_error(gs_error_rangecheck);
1279
10.0M
    *pstr = cfdata->data + offset;
1280
10.0M
    return 0;
1281
10.0M
}
1282
1283
static int
1284
copied_type42_get_outline(gs_font_type42 *font, uint glyph_index,
1285
                          gs_glyph_data_t *pgd)
1286
2.56M
{
1287
2.56M
    gs_copied_font_data_t *const cfdata = font->data.proc_data;
1288
2.56M
    gs_copied_glyph_t *pcg;
1289
1290
2.56M
    if (glyph_index >= cfdata->glyphs_size)
1291
5.55k
        return_error(gs_error_rangecheck);
1292
2.55M
    pcg = &cfdata->glyphs[glyph_index];
1293
2.55M
    if (!pcg->used)
1294
0
        gs_glyph_data_from_null(pgd);
1295
2.55M
    else
1296
2.55M
        gs_glyph_data_from_string(pgd, pcg->gdata.data, pcg->gdata.size, NULL);
1297
2.55M
    return 0;
1298
2.56M
}
1299
1300
static int
1301
copied_type42_get_metrics(gs_font_type42 * pfont, uint glyph_index,
1302
                          gs_type42_metrics_options_t options, float *sbw)
1303
35.1M
{
1304
    /* Check whether we have metrics for this (glyph,wmode) pair. */
1305
35.1M
    gs_copied_font_data_t *const cfdata = pfont->data.proc_data;
1306
35.1M
    gs_copied_glyph_t *pcg;
1307
35.1M
    int wmode = gs_type42_metrics_options_wmode(options);
1308
1309
35.1M
    if (glyph_index >= cfdata->glyphs_size)
1310
2.34M
        return_error(gs_error_rangecheck);
1311
32.8M
    pcg = &cfdata->glyphs[glyph_index];
1312
32.8M
    if (!(pcg->used & (HAS_SBW0 << wmode)))
1313
31.8M
        return_error(gs_error_undefined);
1314
1.02M
    return gs_type42_default_get_metrics(pfont, glyph_index, options, sbw);
1315
32.8M
}
1316
1317
static uint
1318
copied_type42_get_glyph_index(gs_font_type42 *font, gs_glyph glyph)
1319
259k
{
1320
259k
    gs_copied_font_data_t *const cfdata = cf_data((gs_font *)font);
1321
259k
    gs_copied_glyph_t *pcg;
1322
259k
    int code = copied_glyph_slot(cfdata, glyph, &pcg);
1323
1324
259k
    if (code < 0)
1325
67.7k
        return GS_NO_GLYPH;
1326
191k
    return pcg - cfdata->glyphs;
1327
259k
}
1328
1329
static int
1330
copy_font_type42(gs_font *font, gs_font *copied)
1331
14.3k
{
1332
14.3k
    gs_font_type42 *const font42 = (gs_font_type42 *)font;
1333
14.3k
    gs_font_type42 *const copied42 = (gs_font_type42 *)copied;
1334
14.3k
    gs_copied_font_data_t *const cfdata = cf_data(copied);
1335
    /*
1336
     * We "write" the font, aside from the glyphs, into an in-memory
1337
     * structure, and access it from there.
1338
     * We allocate room at the end of the copied data for fake hmtx/vmtx.
1339
     */
1340
14.3k
    uint extra = font42->data.trueNumGlyphs * 8;
1341
14.3k
    stream fs;
1342
14.3k
    int code;
1343
1344
14.3k
    cfdata->notdef = find_notdef((gs_font_base *)font);
1345
14.3k
    code = copied_Encoding_alloc(copied);
1346
14.3k
    if (code < 0)
1347
0
        return code;
1348
14.3k
    s_init(&fs, font->memory);
1349
14.3k
    swrite_position_only(&fs);
1350
14.3k
    code = (font->FontType == ft_TrueType ? psf_write_truetype_stripped(&fs, font42)
1351
14.3k
                                          : psf_write_cid2_stripped(&fs, (gs_font_cid2 *)font42));
1352
14.3k
    code = copied_data_alloc(copied, &fs, extra, code);
1353
14.3k
    if (code < 0)
1354
5.46k
        goto fail;
1355
8.87k
    if (font->FontType == ft_TrueType)
1356
7.75k
        psf_write_truetype_stripped(&fs, font42);
1357
1.11k
    else
1358
1.11k
        psf_write_cid2_stripped(&fs, (gs_font_cid2 *)font42);
1359
8.87k
    copied42->data.string_proc = copied_type42_string_proc;
1360
8.87k
    copied42->data.proc_data = cfdata;
1361
8.87k
    code = gs_type42_font_init(copied42, 0);
1362
8.87k
    if (code < 0)
1363
0
        goto fail2;
1364
    /* gs_type42_font_init overwrites font_info. */
1365
8.87k
    copied->procs.font_info = copied_font_info;
1366
    /* gs_type42_font_init overwrites enumerate_glyph. */
1367
8.87k
    copied42->procs.enumerate_glyph = copied_enumerate_glyph;
1368
8.87k
    copied42->data.get_glyph_index = copied_type42_get_glyph_index;
1369
8.87k
    copied42->data.get_outline = copied_type42_get_outline;
1370
8.87k
    copied42->data.get_metrics = copied_type42_get_metrics;
1371
8.87k
    copied42->data.metrics[0].numMetrics =
1372
8.87k
        copied42->data.metrics[1].numMetrics =
1373
8.87k
        extra / 8;
1374
8.87k
    copied42->data.metrics[0].offset = cfdata->data_size - extra;
1375
8.87k
    copied42->data.metrics[1].offset = cfdata->data_size - extra / 2;
1376
8.87k
    copied42->data.metrics[0].length =
1377
8.87k
        copied42->data.metrics[1].length =
1378
8.87k
        extra / 2;
1379
8.87k
    memset(cfdata->data + cfdata->data_size - extra, 0, extra);
1380
8.87k
    copied42->data.numGlyphs = font42->data.numGlyphs;
1381
8.87k
    copied42->data.trueNumGlyphs = font42->data.trueNumGlyphs;
1382
8.87k
    return 0;
1383
0
 fail2:
1384
0
    gs_free_object(copied->memory, cfdata->data,
1385
0
                   "copy_font_type42(data)");
1386
5.46k
 fail:
1387
5.46k
    gs_free_object(copied->memory, cfdata->Encoding,
1388
5.46k
                   "copy_font_type42(Encoding)");
1389
5.46k
    return code;
1390
0
}
1391
1392
static int uncopy_glyph_type42(gs_font *font, gs_glyph glyph, gs_font *copied, int options)
1393
0
{
1394
0
    gs_copied_glyph_t *pcg = NULL;
1395
0
    gs_font_type42 *font42 = (gs_font_type42 *)font;
1396
0
    gs_font_cid2 *fontCID2 = (gs_font_cid2 *)font;
1397
0
    gs_copied_font_data_t *cfdata = cf_data(copied);
1398
0
    uint gid = (options & COPY_GLYPH_BY_INDEX ? glyph - GS_MIN_GLYPH_INDEX :
1399
0
                font->FontType == ft_CID_TrueType
1400
0
                    ? fontCID2->cidata.CIDMap_proc(fontCID2, glyph)
1401
0
                    : font42->data.get_glyph_index(font42, glyph));
1402
1403
0
    (void)copied_glyph_slot(cfdata, gid + GS_MIN_GLYPH_INDEX, &pcg);
1404
0
    if (pcg != NULL) {
1405
0
        if (pcg->gdata.data != NULL) {
1406
0
            gs_free_string(copied->memory, (byte *)pcg->gdata.data, pcg->gdata.size, "Free copied glyph name");
1407
0
            pcg->gdata.size = 0;
1408
0
            pcg->gdata.data = NULL;
1409
0
        }
1410
0
        pcg->used = 0;
1411
0
        cfdata->num_glyphs--;
1412
0
    }
1413
1414
0
    return 0;
1415
0
}
1416
1417
static int
1418
copy_glyph_type42(gs_font *font, gs_glyph glyph, gs_font *copied, int options)
1419
473k
{
1420
473k
    gs_glyph_data_t gdata;
1421
473k
    gs_font_type42 *font42 = (gs_font_type42 *)font;
1422
473k
    gs_font_cid2 *fontCID2 = (gs_font_cid2 *)font;
1423
473k
    gs_font_type42 *const copied42 = (gs_font_type42 *)copied;
1424
473k
    uint gid = (options & COPY_GLYPH_BY_INDEX ? glyph - GS_MIN_GLYPH_INDEX :
1425
473k
                font->FontType == ft_CID_TrueType
1426
449k
                    ? fontCID2->cidata.CIDMap_proc(fontCID2, glyph)
1427
449k
                    : font42->data.get_glyph_index(font42, glyph));
1428
473k
    int code;
1429
473k
    int rcode;
1430
473k
    gs_copied_font_data_t *const cfdata = cf_data(copied);
1431
473k
    gs_copied_glyph_t *pcg;
1432
473k
    float sbw[4];
1433
473k
    double factor = font42->data.unitsPerEm;
1434
473k
    int i;
1435
1436
    /* If we've been told to, use the TrueType GSUB table to find a possible replacement
1437
     * glyph for the one which was supplied by the CMAP subtable. This is slightly useful
1438
     * when using a TrueType as a replacement for a missing CIDFont, the CMap defines
1439
     * vertical writing and there is a suitable vertical glyph available to use as a
1440
     * replacement for a horizontal glyph (punctuation, basically). Not a common
1441
     * situation, of rather limited value, but....
1442
     */
1443
473k
    if (!(options & COPY_GLYPH_BY_INDEX) && (options & COPY_GLYPH_USE_GSUB) && font->FontType == ft_CID_TrueType)
1444
218k
        gid = fontCID2->data.substitute_glyph_index_vertical((gs_font_type42 *)font, gid, font->WMode, glyph);
1445
1446
473k
    gdata.memory = font42->memory;
1447
473k
    code = font42->data.get_outline(font42, gid, &gdata);
1448
    /* If the glyph is a /.notdef, and the GID is not 0, and we failed to find
1449
     * the /.notdef, try again with GID 0. We have seen fonts from GraphPad
1450
     * Prism which end up putting the /.notdef in the CharStrings dictionary
1451
     * with the wrong GID value (Bug #691573)
1452
     */
1453
473k
    if (code < 0 && gid != 0) {
1454
1.06k
        gs_const_string gnstr;
1455
1.06k
        if (font->procs.glyph_name(font, glyph, &gnstr) >= 0 && gnstr.size == 7
1456
1.06k
            && !memcmp(gnstr.data, ".notdef", 7)) {
1457
0
            gid = 0;
1458
0
            code = font42->data.get_outline(font42, gid, &gdata);
1459
0
        }
1460
1.06k
    }
1461
473k
    if (code < 0)
1462
1.06k
        return code;
1463
1464
472k
    code = copy_glyph_data(font, gid + GS_MIN_GLYPH_INDEX, copied, options,
1465
472k
                           &gdata, NULL, 0);
1466
472k
    if (code < 0)
1467
8.63k
        return code;
1468
463k
    rcode = code;
1469
463k
    if (glyph < GS_MIN_CID_GLYPH)
1470
162k
        code = copy_glyph_name(font, glyph, copied,
1471
162k
                               gid + GS_MIN_GLYPH_INDEX);
1472
463k
    DISCARD(copied_glyph_slot(cfdata, gid + GS_MIN_GLYPH_INDEX, &pcg)); /* can't fail */
1473
1.39M
    for (i = 0; i < 2; ++i) {
1474
927k
        if (font42->data.get_metrics(font42, gid, i, sbw) >= 0) {
1475
463k
            int sb = (int)(sbw[i] * factor + 0.5);
1476
463k
            uint width = (uint)(sbw[2 + i] * factor + 0.5);
1477
463k
            byte *pmetrics =
1478
463k
                cfdata->data + copied42->data.metrics[i].offset + gid * 4;
1479
1480
463k
            pmetrics[0] = (byte)(width >> 8);
1481
463k
            pmetrics[1] = (byte)width;
1482
463k
            pmetrics[2] = (byte)(sb >> 8);
1483
463k
            pmetrics[3] = (byte)sb;
1484
463k
            pcg->used |= HAS_SBW0 << i;
1485
463k
        }
1486
927k
        factor = -factor; /* values are negated for WMode = 1 */
1487
927k
    }
1488
463k
    return (code < 0 ? code : rcode);
1489
472k
}
1490
1491
static gs_glyph
1492
copied_type42_encode_char(gs_font *copied, gs_char chr,
1493
                          gs_glyph_space_t glyph_space)
1494
1.43M
{
1495
1.43M
    gs_copied_font_data_t *const cfdata = cf_data(copied);
1496
1.43M
    const gs_glyph *Encoding = cfdata->Encoding;
1497
1.43M
    gs_glyph glyph;
1498
1499
1.43M
    if (chr >= 256 || Encoding == 0)
1500
0
        return GS_NO_GLYPH;
1501
1.43M
    glyph = Encoding[chr];
1502
1.43M
    if (glyph_space == GLYPH_SPACE_INDEX) {
1503
        /* Search linearly for the glyph by name. */
1504
1.40M
        gs_copied_glyph_t *pcg;
1505
1.40M
        int code = named_glyph_slot_linear(cfdata, glyph, &pcg);
1506
1507
1.40M
        if (code < 0 || !pcg->used)
1508
1.27M
            return GS_NO_GLYPH;
1509
128k
        return GS_MIN_GLYPH_INDEX + (pcg - cfdata->glyphs);
1510
1.40M
    }
1511
32.0k
    return glyph;
1512
1.43M
}
1513
1514
static const gs_copied_font_procs_t copied_procs_type42 = {
1515
    copy_font_type42, copy_glyph_type42, copied_char_add_encoding,
1516
    named_glyph_slot_linear,
1517
    copied_type42_encode_char, gs_type42_glyph_info, gs_type42_glyph_outline,
1518
    uncopy_glyph_type42
1519
};
1520
1521
static inline int
1522
access_type42_data(gs_font_type42 *pfont, ulong base, ulong length,
1523
                   const byte **vptr)
1524
18.2M
{
1525
18.2M
    return pfont->data.string_proc(pfont, base, length, vptr);
1526
18.2M
}
1527
1528
static inline uint
1529
U16(const byte *p)
1530
1.22M
{
1531
1.22M
    return ((uint)p[0] << 8) + p[1];
1532
1.22M
}
1533
1534
static int
1535
same_maxp_values(gs_font_type42 *font0, gs_font_type42 *font1)
1536
611k
{
1537
611k
    gs_type42_data *d0 = &font0->data, *d1 = &font1->data;
1538
1539
611k
    if (d0->maxPoints < d1->maxPoints)
1540
22.3k
        return 0;
1541
589k
    if (d0->maxContours < d1->maxContours)
1542
0
        return 0;
1543
589k
    if (d0->maxCPoints < d1->maxCPoints)
1544
0
        return 0;
1545
589k
    if (d0->maxCContours < d1->maxCContours)
1546
0
        return 0;
1547
589k
    return 1;
1548
589k
}
1549
1550
static int
1551
same_type42_hinting(gs_font_type42 *font0, gs_font_type42 *font1)
1552
611k
{
1553
611k
    gs_type42_data *d0 = &font0->data, *d1 = &font1->data;
1554
611k
    gs_font_type42 *font[2];
1555
611k
    uint pos[2][3];
1556
611k
    uint len[2][3] = {{0,0,0}, {0,0,0}};
1557
611k
    int i, j, code;
1558
1559
611k
    if (d0->unitsPerEm != d1->unitsPerEm)
1560
0
        return 0;
1561
611k
    font[0] = font0;
1562
611k
    font[1] = font1;
1563
611k
    memset(pos, 0, sizeof(pos));
1564
1.83M
    for (j = 0; j < 2; j++) {
1565
1.22M
        const byte *OffsetTable;
1566
1.22M
        uint numTables;
1567
1568
1.22M
        code = access_type42_data(font[j], font[j]->data.subfontOffset, 12, &OffsetTable);
1569
1.22M
        if (code < 0)
1570
0
            return code;
1571
1.22M
        numTables = U16(OffsetTable + 4);
1572
14.6M
        for (i = 0; i < numTables; ++i) {
1573
13.4M
            const byte *tab;
1574
13.4M
            ulong start;
1575
13.4M
            uint length;
1576
1577
13.4M
            code = access_type42_data(font[j], font[j]->data.subfontOffset + 12 + i * 16, 16, &tab);
1578
13.4M
            if (code < 0)
1579
0
                return code;
1580
13.4M
            start = get_u32_msb(tab + 8);
1581
13.4M
            length = get_u32_msb(tab + 12);
1582
13.4M
            if (!memcmp("prep", tab, 4))
1583
1.20M
                pos[j][0] = start, len[j][0] = length;
1584
12.2M
            else if (!memcmp("cvt ", tab, 4))
1585
1.21M
                pos[j][1] = start, len[j][1] = length;
1586
11.0M
            else if (!memcmp("fpgm", tab, 4))
1587
1.21M
                pos[j][2] = start, len[j][2] = length;
1588
13.4M
        }
1589
1.22M
    }
1590
2.44M
    for (i = 0; i < 3; i++) {
1591
1.83M
        if (len[0][i] != len[1][i])
1592
0
            return 0;
1593
1.83M
    }
1594
2.44M
    for (i = 0; i < 3; i++) {
1595
1.83M
        if (len[0][i] != 0) {
1596
1.81M
            const byte *data0, *data1;
1597
1.81M
            ulong length = len[0][i], size0, size1, size;
1598
1.81M
            ulong pos0 = pos[0][i], pos1 = pos[1][i];
1599
1600
3.62M
            while (length > 0) {
1601
1.81M
                code = access_type42_data(font0, pos0, length, &data0);
1602
1.81M
                if (code < 0)
1603
0
                    return code;
1604
1.81M
                size0 = (code == 0 ? length : code);
1605
1.81M
                code = access_type42_data(font1, pos1, length, &data1);
1606
1.81M
                if (code < 0)
1607
0
                    return code;
1608
1.81M
                size1 = (code == 0 ? length : code);
1609
1.81M
                size = min(size0, size1);
1610
1.81M
                if (memcmp(data0, data1, size))
1611
0
                    return 0;
1612
1.81M
                pos0 += size;
1613
1.81M
                pos1 += size;
1614
1.81M
                length -= size;
1615
1.81M
            }
1616
1.81M
        }
1617
1.83M
    }
1618
611k
    return 1;
1619
611k
}
1620
1621
/* ------ CIDFont shared ------ */
1622
1623
static int
1624
copy_font_cid_common(gs_font *font, gs_font *copied, gs_font_cid_data *pcdata)
1625
2.01k
{
1626
2.01k
    return (copy_string(copied->memory, &pcdata->CIDSystemInfo.Registry,
1627
2.01k
                        "Registry") |
1628
2.01k
            copy_string(copied->memory, &pcdata->CIDSystemInfo.Ordering,
1629
2.01k
                        "Ordering"));
1630
2.01k
}
1631
1632
/* ------ CIDFontType 0 ------ */
1633
1634
static int
1635
copied_cid0_glyph_data(gs_font_base *font, gs_glyph glyph,
1636
                       gs_glyph_data_t *pgd, int *pfidx)
1637
78.0k
{
1638
78.0k
    gs_font_cid0 *fcid0 = (gs_font_cid0 *)font;
1639
78.0k
    gs_copied_font_data_t *const cfdata = cf_data((gs_font *)font);
1640
78.0k
    gs_copied_glyph_t *pcg;
1641
78.0k
    int code = copied_glyph_slot(cfdata, glyph, &pcg);
1642
78.0k
    int fdbytes = fcid0->cidata.FDBytes;
1643
78.0k
    int i;
1644
1645
78.0k
    if (pfidx)
1646
61.0k
        *pfidx = 0;
1647
78.0k
    if (code < 0) {
1648
1.69k
        if (pgd)
1649
0
            gs_glyph_data_from_null(pgd);
1650
1.69k
        return_error(gs_error_undefined);
1651
1.69k
    }
1652
76.3k
    if (pfidx)
1653
79.8k
        for (i = 0; i < fdbytes; ++i)
1654
20.4k
            *pfidx = (*pfidx << 8) + pcg->gdata.data[i];
1655
76.3k
    if (pgd)
1656
37.9k
        gs_glyph_data_from_string(pgd, pcg->gdata.data + fdbytes,
1657
37.9k
                                  pcg->gdata.size - fdbytes, NULL);
1658
76.3k
    return 0;
1659
78.0k
}
1660
static int
1661
copied_sub_type1_glyph_data(gs_font_type1 * pfont, gs_glyph glyph,
1662
                            gs_glyph_data_t *pgd)
1663
16.9k
{
1664
16.9k
    return
1665
16.9k
      copied_cid0_glyph_data((gs_font_base *)cf_data((gs_font *)pfont)->parent,
1666
16.9k
                             glyph, pgd, NULL);
1667
16.9k
}
1668
1669
static int
1670
cid0_subfont(gs_font *copied, gs_glyph glyph, gs_font_type1 **pfont1)
1671
20.8k
{
1672
20.8k
    int fidx;
1673
20.8k
    int code = copied_cid0_glyph_data((gs_font_base *)copied, glyph, NULL,
1674
20.8k
                                      &fidx);
1675
1676
20.8k
    if (code >= 0) {
1677
19.1k
        gs_font_cid0 *font0 = (gs_font_cid0 *)copied;
1678
1679
19.1k
        if (fidx >= font0->cidata.FDArray_size)
1680
0
            return_error(gs_error_unregistered); /* Must not happen. */
1681
19.1k
        *pfont1 = font0->cidata.FDArray[fidx];
1682
19.1k
    }
1683
20.8k
    return code;
1684
20.8k
}
1685
1686
static int
1687
copied_cid0_glyph_info(gs_font *font, gs_glyph glyph, const gs_matrix *pmat,
1688
                       int members, gs_glyph_info_t *info)
1689
20.8k
{
1690
20.8k
    gs_font_type1 *subfont1;
1691
20.8k
    int code = cid0_subfont(font, glyph, &subfont1);
1692
1693
20.8k
    if (code < 0)
1694
1.69k
        return code;
1695
19.1k
    if (members & GLYPH_INFO_WIDTH1) {
1696
        /* Hack : There is no way to pass WMode from font to glyph_info,
1697
         * and usually CID font has no metrics for WMode 1.
1698
         * Therefore we use FontBBox as default size.
1699
         * Warning : this incompletely implements the request :
1700
         * other requested members are not retrieved.
1701
         */
1702
810
        gs_font_info_t finfo;
1703
810
        int code = subfont1->procs.font_info(font, NULL, FONT_INFO_BBOX, &finfo);
1704
1705
810
        if (code < 0)
1706
0
            return code;
1707
810
        info->width[0].x = 0;
1708
810
        info->width[0].y = 0;
1709
810
        info->width[1].x = 0;
1710
810
        info->width[1].y = -finfo.BBox.q.x; /* Sic! */
1711
810
        info->v.x = finfo.BBox.q.x / 2;
1712
810
        info->v.y = finfo.BBox.q.y;
1713
810
        info->members = GLYPH_INFO_WIDTH1;
1714
810
        return 0;
1715
810
    }
1716
18.3k
    return subfont1->procs.glyph_info((gs_font *)subfont1, glyph, pmat,
1717
18.3k
                                      members, info);
1718
19.1k
}
1719
1720
static int
1721
copied_cid0_glyph_outline(gs_font *font, int WMode, gs_glyph glyph,
1722
                          const gs_matrix *pmat, gx_path *ppath, double sbw[4])
1723
0
{
1724
0
    gs_font_type1 *subfont1;
1725
0
    int code = cid0_subfont(font, glyph, &subfont1);
1726
1727
0
    if (code < 0)
1728
0
        return code;
1729
0
    return subfont1->procs.glyph_outline((gs_font *)subfont1, WMode, glyph, pmat,
1730
0
                                         ppath, sbw);
1731
0
}
1732
1733
static int
1734
copy_font_cid0(gs_font *font, gs_font *copied)
1735
168
{
1736
168
    gs_font_cid0 *copied0 = (gs_font_cid0 *)copied;
1737
168
    gs_copied_font_data_t *const cfdata = cf_data(copied);
1738
168
    gs_font_type1 **FDArray =
1739
168
        gs_alloc_struct_array(copied->memory, copied0->cidata.FDArray_size,
1740
168
                              gs_font_type1 *,
1741
168
                              &st_gs_font_type1_ptr_element, "FDArray");
1742
168
    int i = 0, code;
1743
1744
168
    if (FDArray == 0)
1745
0
        return_error(gs_error_VMerror);
1746
168
    code = copy_font_cid_common(font, copied, &copied0->cidata.common);
1747
168
    if (code < 0)
1748
0
        goto fail;
1749
449
    for (; i < copied0->cidata.FDArray_size; ++i) {
1750
281
        gs_font *subfont = (gs_font *)copied0->cidata.FDArray[i];
1751
281
        gs_font_type1 *subfont1 = (gs_font_type1 *)subfont;
1752
281
        gs_font *subcopy;
1753
281
        gs_font_type1 *subcopy1;
1754
281
        gs_copied_font_data_t *subdata;
1755
1756
281
        if (i == 0) {
1757
            /* copy_subrs requires a Type 1 font, even for GSubrs. */
1758
168
            code = copy_subrs(subfont1, true, &cfdata->global_subrs,
1759
168
                              copied->memory);
1760
168
            if (code < 0)
1761
0
                goto fail;
1762
168
        }
1763
281
        code = gs_copy_font(subfont, &subfont->FontMatrix, copied->memory, &subcopy, -1);
1764
281
        if (code < 0)
1765
0
            goto fail;
1766
281
        subcopy1 = (gs_font_type1 *)subcopy;
1767
281
        subcopy1->data.parent = NULL;
1768
281
        subdata = cf_data(subcopy);
1769
281
        subdata->parent = copied0;
1770
281
        gs_free_object(copied->memory, subdata->Encoding,
1771
281
                       "copy_font_cid0(Encoding)");
1772
281
        subdata->Encoding = 0;
1773
        /*
1774
         * Share the glyph data and global_subrs with the parent.  This
1775
         * allows copied_type1_glyph_data in the subfont to do the right
1776
         * thing.
1777
         */
1778
281
        gs_free_object(copied->memory, subdata->names,
1779
281
                       "copy_font_cid0(subfont names)");
1780
281
        gs_free_object(copied->memory, subdata->glyphs,
1781
281
                       "copy_font_cid0(subfont glyphs)");
1782
281
        subcopy1->data.procs.glyph_data = copied_sub_type1_glyph_data;
1783
281
        subdata->glyphs = cfdata->glyphs;
1784
281
        subdata->glyphs_size = cfdata->glyphs_size;
1785
281
        subdata->names = 0;
1786
281
        if (subdata->global_subrs.data != NULL)
1787
70
            gs_free_object(copied->memory, subdata->global_subrs.data, "copy parent global subrs to child, free child global subrs");
1788
281
        if (subdata->global_subrs.starts != NULL)
1789
70
            gs_free_object(copied->memory, subdata->global_subrs.starts, "copy parent global subrs to child, free child global subrs");
1790
281
        subdata->global_subrs = cfdata->global_subrs;
1791
281
        FDArray[i] = subcopy1;
1792
281
    }
1793
168
    cfdata->notdef = GS_MIN_CID_GLYPH;
1794
168
    copied0->cidata.FDArray = FDArray;
1795
168
    copied0->cidata.FDBytes =
1796
168
        (copied0->cidata.FDArray_size <= 1 ? 0 :
1797
168
         copied0->cidata.FDArray_size <= 256 ? 1 : 2);
1798
168
    copied0->cidata.glyph_data = copied_cid0_glyph_data;
1799
168
    return 0;
1800
0
 fail:
1801
0
    while (--i >= 0)
1802
0
        gs_free_object(copied->memory, FDArray[i], "copy_font_cid0(subfont)");
1803
0
    gs_free_object(copied->memory, FDArray, "FDArray");
1804
0
    return code;
1805
168
}
1806
1807
static int
1808
uncopy_glyph_cid0(gs_font *font, gs_glyph glyph, gs_font *copied, int options)
1809
0
{
1810
0
    gs_copied_glyph_t *pcg = NULL;
1811
0
    gs_copied_font_data_t *cfdata = cf_data(copied);
1812
1813
0
    (void)copied_glyph_slot(cfdata, glyph, &pcg);
1814
0
    if (pcg != NULL) {
1815
0
        if (pcg->gdata.data != NULL) {
1816
0
            gs_free_string(copied->memory, (byte *)pcg->gdata.data, pcg->gdata.size, "Free copied glyph name");
1817
0
            pcg->gdata.size = 0;
1818
0
            pcg->gdata.data = NULL;
1819
0
        }
1820
0
        pcg->used = 0;
1821
0
        cfdata->num_glyphs--;
1822
0
    }
1823
0
    return 0;
1824
0
}
1825
1826
static int
1827
copy_glyph_cid0(gs_font *font, gs_glyph glyph, gs_font *copied, int options)
1828
5.55k
{
1829
5.55k
    gs_glyph_data_t gdata;
1830
5.55k
    gs_font_cid0 *fcid0 = (gs_font_cid0 *)font;
1831
5.55k
    gs_font_cid0 *copied0 = (gs_font_cid0 *)copied;
1832
5.55k
    int fdbytes = copied0->cidata.FDBytes;
1833
5.55k
    int fidx;
1834
5.55k
    int code;
1835
5.55k
    byte prefix[MAX_FDBytes];
1836
5.55k
    int i;
1837
1838
5.55k
    gdata.memory = font->memory;
1839
5.55k
    code = fcid0->cidata.glyph_data((gs_font_base *)font, glyph,
1840
5.55k
                &gdata, &fidx);
1841
5.55k
    if (code < 0)
1842
186
        return code;
1843
7.10k
    for (i = fdbytes - 1; i >= 0; --i, fidx >>= 8)
1844
1.73k
        prefix[i] = (byte)fidx;
1845
5.37k
    if (fidx != 0)
1846
0
        return_error(gs_error_rangecheck);
1847
5.37k
    return copy_glyph_data(font, glyph, copied, options, &gdata, prefix, fdbytes);
1848
5.37k
}
1849
1850
static const gs_copied_font_procs_t copied_procs_cid0 = {
1851
    copy_font_cid0, copy_glyph_cid0, copied_no_add_encoding,
1852
    named_glyph_slot_none,
1853
    gs_no_encode_char, copied_cid0_glyph_info, copied_cid0_glyph_outline,
1854
    uncopy_glyph_cid0
1855
};
1856
1857
static int
1858
same_cid0_hinting(const gs_font_cid0 *cfont, const gs_font_cid0 *ofont)
1859
5.26k
{
1860
5.26k
    int i;
1861
1862
5.26k
    if (cfont->cidata.FDArray_size != ofont->cidata.FDArray_size)
1863
0
        return 0;
1864
1865
25.0k
    for (i = 0; i < cfont->cidata.FDArray_size; i++) {
1866
19.7k
        gs_font_type1 *subfont0 = cfont->cidata.FDArray[i];
1867
19.7k
        gs_font_type1 *subfont1 = ofont->cidata.FDArray[i];
1868
19.7k
        if (!same_type1_hinting(subfont0, subfont1))
1869
0
            return 0;
1870
19.7k
    }
1871
5.26k
    return 1;
1872
5.26k
}
1873
1874
/* ------ CIDFontType 2 ------ */
1875
1876
static int
1877
copied_cid2_CIDMap_proc(gs_font_cid2 *fcid2, gs_glyph glyph)
1878
1.09M
{
1879
1.09M
    uint cid = glyph - GS_MIN_CID_GLYPH;
1880
1.09M
    gs_copied_font_data_t *const cfdata = cf_data((gs_font *)fcid2);
1881
1.09M
    const ushort *CIDMap = cfdata->CIDMap;
1882
1883
1.09M
    if (glyph < GS_MIN_CID_GLYPH || cid >= fcid2->cidata.common.CIDCount)
1884
11.2k
        return_error(gs_error_rangecheck);
1885
1.08M
    if (CIDMap[cid] == 0xffff)
1886
33.6k
        return -1;
1887
1.05M
    return CIDMap[cid];
1888
1.08M
}
1889
1890
static uint
1891
copied_cid2_get_glyph_index(gs_font_type42 *font, gs_glyph glyph)
1892
1.09M
{
1893
1.09M
    int glyph_index = copied_cid2_CIDMap_proc((gs_font_cid2 *)font, glyph);
1894
1895
1.09M
    if (glyph_index < 0)
1896
44.9k
        return GS_NO_GLYPH;
1897
1.05M
    return glyph_index;
1898
1.09M
}
1899
1900
extern_st(st_subst_CID_on_WMode);
1901
1902
static int
1903
copy_font_cid2(gs_font *font, gs_font *copied)
1904
1.84k
{
1905
1.84k
    gs_font_cid2 *copied2 = (gs_font_cid2 *)copied;
1906
1.84k
    gs_copied_font_data_t *const cfdata = cf_data(copied);
1907
1.84k
    int code;
1908
1.84k
    int CIDCount = copied2->cidata.common.CIDCount;
1909
1.84k
    ushort *CIDMap = (ushort *)
1910
1.84k
        gs_alloc_byte_array(copied->memory, CIDCount, sizeof(ushort),
1911
1.84k
                            "copy_font_cid2(CIDMap");
1912
1913
1.84k
    if (CIDMap == 0)
1914
0
        return_error(gs_error_VMerror);
1915
1.84k
    code = copy_font_cid_common(font, copied, &copied2->cidata.common);
1916
1.84k
    if (code < 0 ||
1917
1.84k
        (code = copy_font_type42(font, copied)) < 0
1918
1.84k
        ) {
1919
731
        gs_free_object(copied->memory, CIDMap, "copy_font_cid2(CIDMap");
1920
731
        return code;
1921
731
    }
1922
1.11k
    cfdata->notdef = GS_MIN_CID_GLYPH;
1923
1.11k
    memset(CIDMap, 0xff, CIDCount * sizeof(*CIDMap));
1924
1.11k
    cfdata->CIDMap = CIDMap;
1925
1.11k
    copied2->cidata.MetricsCount = 0;
1926
1.11k
    copied2->cidata.CIDMap_proc = copied_cid2_CIDMap_proc;
1927
1.11k
    {
1928
1.11k
        gs_font_type42 *const copied42 = (gs_font_type42 *)copied;
1929
1930
1.11k
        copied42->data.get_glyph_index = copied_cid2_get_glyph_index;
1931
1.11k
    }
1932
1.11k
    if (copied2->subst_CID_on_WMode) {
1933
0
        gs_subst_CID_on_WMode_t *subst = NULL;
1934
1935
0
        rc_alloc_struct_1(subst, gs_subst_CID_on_WMode_t, &st_subst_CID_on_WMode,
1936
0
                            copied2->memory, return_error(gs_error_VMerror), "copy_font_cid2");
1937
0
        subst->data[0] = subst->data[1] = 0;
1938
0
        copied2->subst_CID_on_WMode = subst;
1939
0
    }
1940
1941
1.11k
    return 0;
1942
1.11k
}
1943
1944
static int expand_CIDMap(gs_font_cid2 *copied2, uint CIDCount)
1945
222k
{
1946
222k
    ushort *CIDMap;
1947
222k
    gs_copied_font_data_t *const cfdata = cf_data((gs_font *)copied2);
1948
1949
222k
    if (CIDCount <= copied2->cidata.common.CIDCount)
1950
221k
        return 0;
1951
215
    CIDMap = (ushort *)
1952
215
        gs_alloc_byte_array(copied2->memory, CIDCount, sizeof(ushort),
1953
215
                            "expand_CIDMap(new CIDMap)");
1954
215
    if (CIDMap == 0)
1955
0
        return_error(gs_error_VMerror);
1956
215
    memcpy(CIDMap, cfdata->CIDMap, copied2->cidata.common.CIDCount * sizeof(*CIDMap));
1957
215
    memset(CIDMap + copied2->cidata.common.CIDCount, 0xFF,
1958
215
            (CIDCount - copied2->cidata.common.CIDCount) * sizeof(*CIDMap));
1959
215
    gs_free_object(copied2->memory, cfdata->CIDMap, "expand_CIDMap(old CIDMap)");
1960
215
    cfdata->CIDMap = CIDMap;
1961
215
    copied2->cidata.common.CIDCount = CIDCount;
1962
215
    return 0;
1963
215
}
1964
1965
static int
1966
uncopy_glyph_cid2(gs_font *font, gs_glyph glyph, gs_font *copied, int options)
1967
0
{
1968
0
    gs_copied_glyph_t *pcg = NULL;
1969
0
    gs_copied_font_data_t *cfdata = cf_data(copied);
1970
1971
0
    (void)copied_glyph_slot(cf_data(copied), glyph, &pcg);
1972
0
    if (pcg != NULL) {
1973
0
        if (pcg->gdata.data != NULL) {
1974
0
            gs_free_string(copied->memory, (byte *)pcg->gdata.data, pcg->gdata.size, "Free copied glyph name");
1975
0
            pcg->gdata.size = 0;
1976
0
            pcg->gdata.data = NULL;
1977
0
        }
1978
0
        pcg->used = 0;
1979
0
        cfdata->num_glyphs--;
1980
0
    }
1981
0
    return 0;
1982
0
}
1983
1984
static int
1985
copy_glyph_cid2(gs_font *font, gs_glyph glyph, gs_font *copied, int options)
1986
227k
{
1987
227k
    gs_font_cid2 *fcid2 = (gs_font_cid2 *)font;
1988
227k
    gs_copied_font_data_t *const cfdata = cf_data(copied);
1989
227k
    gs_font_cid2 *copied2 = (gs_font_cid2 *)copied;
1990
227k
    int gid;
1991
227k
    int code;
1992
1993
227k
    if (!(options & COPY_GLYPH_BY_INDEX)) {
1994
222k
        uint cid = glyph - GS_MIN_CID_GLYPH;
1995
222k
        int CIDCount;
1996
1997
222k
        code = expand_CIDMap(copied2, cid + 1);
1998
222k
        if (code < 0)
1999
0
            return code;
2000
222k
        CIDCount = copied2->cidata.common.CIDCount;
2001
222k
        gid = fcid2->cidata.CIDMap_proc(fcid2, glyph);
2002
        /* If we've been told to, use the TrueType GSUB table to find a possible replacement
2003
         * glyph for the one which was supplied by the CMAP subtable. This is slightly useful
2004
         * when using a TrueType as a replacement for a missing CIDFont, the CMap defines
2005
         * vertical writing and there is a suitable vertical glyph available to use as a
2006
         * replacement for a horizontal glyph (punctuation, basically). Not a common
2007
         * situation, of rather limited value, but....
2008
         */
2009
222k
        if (options & COPY_GLYPH_USE_GSUB)
2010
220k
            gid = ((gs_font_cid2 *)font)->data.substitute_glyph_index_vertical((gs_font_type42 *)font, gid, font->WMode, glyph);
2011
222k
        if (gid < 0 || gid >= cfdata->glyphs_size)
2012
2.80k
            return_error(gs_error_rangecheck);
2013
219k
        if (cid > CIDCount)
2014
0
            return_error(gs_error_invalidaccess);
2015
219k
        if (cfdata->CIDMap[cid] != 0xffff && cfdata->CIDMap[cid] != gid)
2016
0
            return_error(gs_error_invalidaccess);
2017
219k
        code = copy_glyph_type42(font, glyph, copied, options);
2018
219k
        if (code < 0)
2019
487
            return code;
2020
218k
        cfdata->CIDMap[cid] = gid;
2021
218k
    } else {
2022
5.63k
        gid = glyph - GS_MIN_GLYPH_INDEX;
2023
5.63k
        if (gid < 0 || gid >= cfdata->glyphs_size)
2024
0
            return_error(gs_error_rangecheck);
2025
5.63k
        code = copy_glyph_type42(font, glyph, copied, options);
2026
5.63k
        if (code < 0)
2027
0
            return code;
2028
5.63k
    }
2029
224k
    return code;
2030
227k
}
2031
2032
static const gs_copied_font_procs_t copied_procs_cid2 = {
2033
    copy_font_cid2, copy_glyph_cid2, copied_no_add_encoding,
2034
    named_glyph_slot_none,
2035
    gs_no_encode_char, gs_type42_glyph_info, gs_type42_glyph_outline,
2036
    uncopy_glyph_cid2
2037
};
2038
2039
static int
2040
same_cid2_hinting(const gs_font_cid2 *cfont, const gs_font_cid2 *ofont)
2041
231k
{
2042
231k
    return same_type42_hinting((gs_font_type42 *)cfont, (gs_font_type42 *)ofont);
2043
231k
}
2044
2045
/* ---------------- Public ---------------- */
2046
2047
/*
2048
 * Procedure vector for copied fonts.
2049
 */
2050
static font_proc_font_info(copied_font_info);
2051
static font_proc_enumerate_glyph(copied_enumerate_glyph);
2052
static const gs_font_procs copied_font_procs = {
2053
    0,        /* define_font, not supported */
2054
    0,        /* make_font, not supported */
2055
    copied_font_info,
2056
    gs_default_same_font,
2057
    0,        /* encode_char, varies by FontType */
2058
    0,        /* decode_char, not supported */
2059
    copied_enumerate_glyph,
2060
    0,        /* glyph_info, varies by FontType */
2061
    0,        /* glyph_outline, varies by FontType */
2062
    copied_glyph_name,
2063
    gs_default_init_fstack,
2064
    gs_default_next_char_glyph,
2065
    copied_build_char
2066
};
2067
2068
#if GLYPHS_SIZE_IS_PRIME
2069
static const int some_primes[] = {
2070
    /* Arbitrary choosen prime numbers, being reasonable for a Type 1|2 font size.
2071
       We start with 257 to fit 256 glyphs and .notdef .
2072
       Smaller numbers aren't useful, because we don't know whether a font
2073
       will add more glyphs incrementally when we allocate its stable copy.
2074
    */
2075
    257, 359, 521, 769, 1031, 2053,
2076
    3079, 4099, 5101, 6101, 7109, 8209, 10007, 12007, 14009,
2077
    16411, 20107, 26501, 32771, 48857, 65537, 85229, 127837};
2078
#endif
2079
2080
/*
2081
 * Copy a font, aside from its glyphs.
2082
 */
2083
int
2084
gs_copy_font(gs_font *font, const gs_matrix *orig_matrix, gs_memory_t *mem, gs_font **pfont_new, int max_reserved_glyphs)
2085
36.3k
{
2086
36.3k
    gs_memory_type_ptr_t fstype = gs_object_type(font->memory, font);
2087
36.3k
    uint fssize = gs_struct_type_size(fstype);
2088
36.3k
    gs_font *copied = 0;
2089
36.3k
    gs_copied_font_data_t *cfdata = 0;
2090
36.3k
    gs_font_info_t info;
2091
36.3k
    gs_copied_glyph_t *glyphs = 0;
2092
36.3k
    uint glyphs_size;
2093
36.3k
    gs_copied_glyph_name_t *names = 0;
2094
36.3k
    bool have_names = false;
2095
36.3k
    const gs_copied_font_procs_t *procs;
2096
36.3k
    int code;
2097
2098
    /*
2099
     * Check for a supported FontType, and compute the size of its
2100
     * copied glyph table.
2101
     */
2102
36.3k
    switch (font->FontType) {
2103
15.5k
    case ft_TrueType:
2104
15.5k
        procs = &copied_procs_type42;
2105
15.5k
        glyphs_size = ((gs_font_type42 *)font)->data.trueNumGlyphs;
2106
15.5k
        have_names = true;
2107
15.5k
        break;
2108
17.0k
    case ft_encrypted:
2109
18.6k
    case ft_encrypted2:
2110
18.6k
        procs = &copied_procs_type1;
2111
        /* Count the glyphs. */
2112
18.6k
        glyphs_size = 0;
2113
18.6k
        {
2114
18.6k
            int index = 0;
2115
18.6k
            gs_glyph glyph;
2116
2117
12.8M
            while (font->procs.enumerate_glyph(font, &index, GLYPH_SPACE_NAME,
2118
12.8M
                                               &glyph), index != 0)
2119
12.8M
                ++glyphs_size;
2120
18.6k
        }
2121
18.6k
        if(glyphs_size > max_reserved_glyphs && max_reserved_glyphs != -1)
2122
0
            glyphs_size = max_reserved_glyphs;
2123
2124
18.6k
#if GLYPHS_SIZE_IS_PRIME
2125
18.6k
        if (glyphs_size < 257)
2126
3.92k
            glyphs_size = 257;
2127
        /*
2128
         * Make glyphs_size a prime number to ensure termination of the loop in
2129
         * named_glyphs_slot_hashed, q.v.
2130
         * Also reserve additional slots for the case of font merging and
2131
         * for possible font increments.
2132
         */
2133
18.6k
        glyphs_size = glyphs_size * 3 / 2;
2134
2135
18.6k
        { int i;
2136
100k
            for (i = 0; i < count_of(some_primes); i++)
2137
100k
                if (glyphs_size <= some_primes[i])
2138
18.6k
                    break;
2139
18.6k
            if (i >= count_of(some_primes))
2140
0
                return_error(gs_error_rangecheck);
2141
18.6k
            glyphs_size = some_primes[i];
2142
18.6k
        }
2143
#else
2144
        /*
2145
         * Make names_size a power of 2 to ensure termination of the loop in
2146
         * named_glyphs_slot_hashed, q.v.
2147
         */
2148
        glyphs_size = glyphs_size * 3 / 2;
2149
        while (glyphs_size & (glyphs_size - 1))
2150
            glyphs_size = (glyphs_size | (glyphs_size - 1)) + 1;
2151
        if (glyphs_size < 256)  /* probably incremental font */
2152
            glyphs_size = 256;
2153
#endif
2154
18.6k
        have_names = true;
2155
18.6k
        break;
2156
168
    case ft_CID_encrypted:
2157
168
        procs = &copied_procs_cid0;
2158
        /* We used to use the CIDCount here, but for CIDFonts with a GlyphDirectory
2159
         * (dictionary form) the number of CIDs is not the same as the highest CID.
2160
         * Because we use the CID as the slot, we need to assign the highest possible
2161
         * CID, not the number of CIDs. Don't forget to add one because CIDs
2162
         * count from 0.
2163
         */
2164
168
        glyphs_size = ((gs_font_cid0 *)font)->cidata.common.MaxCID + 1;
2165
168
        break;
2166
1.84k
    case ft_CID_TrueType:
2167
1.84k
        procs = &copied_procs_cid2;
2168
        /* Glyphs are indexed by GID, not by CID. */
2169
1.84k
        glyphs_size = ((gs_font_cid2 *)font)->data.trueNumGlyphs;
2170
1.84k
        break;
2171
0
    default:
2172
0
        return_error(gs_error_rangecheck);
2173
36.3k
    }
2174
2175
    /* Get the font_info for copying. */
2176
2177
36.3k
    memset(&info, 0, sizeof(info));
2178
36.3k
    info.Flags_requested = ~0;
2179
36.3k
    code = font->procs.font_info(font, NULL, ~0, &info);
2180
2181
    /* We can ignore a lack of FontInfo for TrueType fonts which
2182
     * are descendants of CID fonts
2183
     */
2184
36.3k
    if (code < 0 && !(font->FontType == ft_CID_TrueType))
2185
3.10k
        return code;
2186
2187
    /* Allocate the generic copied information. */
2188
2189
33.1k
    glyphs = gs_alloc_struct_array(mem, glyphs_size, gs_copied_glyph_t,
2190
33.1k
                                   &st_gs_copied_glyph_element,
2191
33.1k
                                   "gs_copy_font(glyphs)");
2192
33.1k
    if (have_names != 0)
2193
31.1k
        names = gs_alloc_struct_array(mem, glyphs_size, gs_copied_glyph_name_t,
2194
33.1k
                                      &st_gs_copied_glyph_name_element,
2195
33.1k
                                      "gs_copy_font(names)");
2196
33.1k
    copied = gs_alloc_struct(mem, gs_font, fstype,
2197
33.1k
                             "gs_copy_font(copied font)");
2198
33.1k
    if (copied) {
2199
33.1k
        gs_font_base *bfont = (gs_font_base *)copied;
2200
2201
        /* Initialize the copied font - minumum we need
2202
         * so we can safely free it in the "fail:" case
2203
         * below
2204
         */
2205
33.1k
        memcpy(copied, font, fssize);
2206
33.1k
        copied->next = copied->prev = 0;
2207
33.1k
        copied->memory = mem;
2208
33.1k
        copied->is_resource = false;
2209
33.1k
        gs_notify_init(&copied->notify_list, mem);
2210
33.1k
        copied->base = copied;
2211
2212
33.1k
        bfont->FAPI = 0;
2213
33.1k
        bfont->FAPI_font_data = 0;
2214
33.1k
        bfont->encoding_index = ENCODING_INDEX_UNKNOWN;
2215
33.1k
        code = uid_copy(&bfont->UID, mem, "gs_copy_font(UID)");
2216
33.1k
        if (code < 0) {
2217
0
            uid_set_invalid(&bfont->UID);
2218
0
            goto fail;
2219
0
        }
2220
33.1k
    }
2221
33.1k
    cfdata = gs_alloc_struct(mem, gs_copied_font_data_t,
2222
33.1k
                            &st_gs_copied_font_data,
2223
33.1k
                            "gs_copy_font(wrapper data)");
2224
33.1k
    if (cfdata)
2225
33.1k
        memset(cfdata, 0, sizeof(*cfdata));
2226
33.1k
    if (glyphs == 0 || (names == 0 && have_names) || copied == 0 ||
2227
33.1k
        cfdata == 0
2228
33.1k
        ) {
2229
0
        code = gs_note_error(gs_error_VMerror);
2230
0
        goto fail;
2231
0
    }
2232
33.1k
    cfdata->info = info;
2233
2234
    /* This is somewhat unpleasant. We use the 'glyph' as the unique ID for a number
2235
     * of purposes, but in particular for determining which CharStrings need to be written
2236
     * out by pdfwrite. The 'glyph' appears to be (sometimes) given by the index of the glyph name
2237
     * in the *interpreter* name table. For names in one of the standard encodings
2238
     * we find the name there and use its ID. However, if the glyph name is non-standard
2239
     * then it is added to the interpreter name table and the name index is used to
2240
     * identify the glyph. The problem arises if the font is restored away, and a
2241
     * vmreclaim causes the (now unreferenced) glyph names to be flushed. If we
2242
     * should then use the same font and glyph, its possible that the name table
2243
     * might be different, resulting in a different name index. We would then write
2244
     * duplicate CharStrings to the output, see Bug 687172.
2245
     * The GC enumeration (see top of file) marks the names in the name table to prevent
2246
     * them being flushed. As long as everything is in the same memory allocator this
2247
     * works (ugly though it is). However, if we are using the pdfi PDF interpreter
2248
     * inside the PostScript interpreter, then a problem arises. The pdfwrite device
2249
     * holds on to the font copies until the device is destroyed, by which time the
2250
     * PDF interpreter has already gone. The vmreclaim prior to the device destruction
2251
     * enumerates the name pointers. Because the font was allocated by pdfi these
2252
     * are no longer valid. They are also not needed, since the pdfi name table is
2253
     * not garbage collected..
2254
     * To cater for both conditions we test the memory allocator the font was using.
2255
     * If its a GC'ed allocater then we keep a pointer to the font 'dir' and we enumerate
2256
     * the names and mark them in the interpreter table. Otherwise we don't attempt to
2257
     * mark them. We use dir being NULL to control whether we mark the names.
2258
     */
2259
33.1k
    if (font->memory != font->memory->non_gc_memory)
2260
120
        cfdata->dir = font->dir;
2261
33.0k
    else
2262
33.0k
        cfdata->dir = NULL;
2263
2264
33.1k
    if ((code = (copy_string(mem, &cfdata->info.Copyright,
2265
33.1k
                             "gs_copy_font(Copyright)") |
2266
33.1k
                 copy_string(mem, &cfdata->info.Notice,
2267
33.1k
                             "gs_copy_font(Notice)") |
2268
33.1k
                 copy_string(mem, &cfdata->info.FamilyName,
2269
33.1k
                             "gs_copy_font(FamilyName)") |
2270
33.1k
                 copy_string(mem, &cfdata->info.FullName,
2271
33.1k
                             "gs_copy_font(FullName)"))) < 0
2272
33.1k
        )
2273
0
        goto fail;
2274
    /* set the remainder of the copied font contents */
2275
33.1k
    copied->FontMatrix = *orig_matrix;
2276
33.1k
    copied->client_data = cfdata;
2277
33.1k
    copied->procs = copied_font_procs;
2278
33.1k
    copied->procs.encode_char = procs->encode_char;
2279
33.1k
    copied->procs.glyph_info = procs->glyph_info;
2280
33.1k
    copied->procs.glyph_outline = procs->glyph_outline;
2281
2282
33.1k
    cfdata->procs = procs;
2283
33.1k
    memset(glyphs, 0, glyphs_size * sizeof(*glyphs));
2284
33.1k
    cfdata->glyphs = glyphs;
2285
33.1k
    cfdata->glyphs_size = glyphs_size;
2286
33.1k
    cfdata->num_glyphs = 0;
2287
33.1k
    cfdata->ordered = false;
2288
33.1k
    if (names)
2289
31.1k
        memset(names, 0, glyphs_size * sizeof(*names));
2290
33.1k
    cfdata->names = names;
2291
33.1k
    if (names != 0) {
2292
31.1k
        uint i;
2293
2294
57.4M
        for (i = 0; i < glyphs_size; ++i)
2295
57.4M
            names[i].glyph = GS_NO_GLYPH;
2296
31.1k
    }
2297
2298
    /* Do FontType-specific initialization. */
2299
2300
33.1k
    code = procs->finish_copy_font(font, copied);
2301
33.1k
    if (code < 0)
2302
5.46k
        goto fail;
2303
2304
27.7k
    if (cfdata->notdef != GS_NO_GLYPH)
2305
25.8k
        code = gs_copy_glyph(font, cfdata->notdef, copied);
2306
27.7k
    if (code < 0)
2307
3.75k
        gs_free_copied_font(copied);
2308
23.9k
    else
2309
23.9k
        *pfont_new = copied;
2310
2311
27.7k
    return code;
2312
2313
5.46k
 fail:
2314
    /* Free storage and exit. */
2315
5.46k
    if (cfdata) {
2316
5.46k
        uncopy_string(mem, &cfdata->info.FullName,
2317
5.46k
                      "gs_copy_font(FullName)");
2318
5.46k
        uncopy_string(mem, &cfdata->info.FamilyName,
2319
5.46k
                      "gs_copy_font(FamilyName)");
2320
5.46k
        uncopy_string(mem, &cfdata->info.Notice,
2321
5.46k
                      "gs_copy_font(Notice)");
2322
5.46k
        uncopy_string(mem, &cfdata->info.Copyright,
2323
5.46k
                      "gs_copy_font(Copyright)");
2324
5.46k
        gs_free_object(mem, cfdata, "gs_copy_font(wrapper data)");
2325
5.46k
    }
2326
5.46k
    gs_free_object(mem, copied, "gs_copy_font(copied font)");
2327
5.46k
    gs_free_object(mem, names, "gs_copy_font(names)");
2328
5.46k
    gs_free_object(mem, glyphs, "gs_copy_font(glyphs)");
2329
5.46k
    return code;
2330
33.1k
}
2331
2332
/* We only need this because the descendant(s) share the parent
2333
 * CIDFont glyph space, so we can't free that if we are a descendant.
2334
 */
2335
static int gs_free_copied_descendant_font(gs_font *font)
2336
281
{
2337
281
    gs_copied_font_data_t *cfdata = font->client_data;
2338
281
    gs_memory_t *mem = font->memory;
2339
2340
281
    if (cfdata) {
2341
281
        uncopy_string(mem, &cfdata->info.FullName,
2342
281
                      "gs_free_copied_font(FullName)");
2343
281
        uncopy_string(mem, &cfdata->info.FamilyName,
2344
281
                      "gs_free_copied_font(FamilyName)");
2345
281
        uncopy_string(mem, &cfdata->info.Notice,
2346
281
                      "gs_free_copied_font(Notice)");
2347
281
        uncopy_string(mem, &cfdata->info.Copyright,
2348
281
                      "gs_free_copied_font(Copyright)");
2349
281
        if (cfdata->Encoding)
2350
0
            gs_free_object(mem, cfdata->Encoding, "gs_free_copied_font(Encoding)");
2351
281
        gs_free_object(mem, cfdata->names, "gs_free_copied_font(names)");
2352
281
        gs_free_object(mem, cfdata->data, "gs_free_copied_font(data)");
2353
281
        if (cfdata->subrs.data != NULL)
2354
22
            gs_free_object(mem, cfdata->subrs.data, "gs_free_copied_font(subrs->data)");
2355
281
        if (cfdata->subrs.starts != NULL)
2356
22
            gs_free_object(mem, cfdata->subrs.starts, "gs_free_copied_font(subrs->starts)");
2357
        /* global subrs are 'shared with the parent', see copy_font_cid0()
2358
         * so we don't want to free them here, they are freed by the parent font.
2359
         */
2360
281
        gs_free_object(mem, cfdata, "gs_free_copied_font(wrapper data)");
2361
281
    }
2362
281
    gs_free_object(mem, font, "gs_free_copied_font(copied font)");
2363
281
    return 0;
2364
281
}
2365
2366
int gs_free_copied_font(gs_font *font)
2367
27.4k
{
2368
27.4k
    gs_copied_font_data_t *cfdata = font->client_data;
2369
27.4k
    gs_memory_t *mem = font->memory;
2370
27.4k
    int i, code;
2371
27.4k
    gs_copied_glyph_t *pcg = 0;
2372
27.4k
    gs_copied_glyph_name_t *pcgn = 0;
2373
2374
    /* For CID fonts, we must also free the descendants, which we copied
2375
     * at the time we copied the actual CIDFont itself
2376
     */
2377
27.4k
    if (font->FontType == ft_CID_encrypted) {
2378
168
        gs_font_cid0 *copied0 = (gs_font_cid0 *)font;
2379
2380
449
        for (i = 0; i < copied0->cidata.FDArray_size; ++i) {
2381
281
            code = gs_free_copied_descendant_font((gs_font *)copied0->cidata.FDArray[i]);
2382
281
            if (code < 0)
2383
0
                return code;
2384
281
        }
2385
168
        gs_free_object(mem, copied0->cidata.FDArray, "free copied CIDFont FDArray");
2386
168
        copied0->cidata.FDArray = 0;
2387
168
        gs_free_string(mem, (byte *)copied0->cidata.common.CIDSystemInfo.Registry.data, copied0->cidata.common.CIDSystemInfo.Registry.size, "Free copied Registry");
2388
168
        gs_free_string(mem, (byte *)copied0->cidata.common.CIDSystemInfo.Ordering.data, copied0->cidata.common.CIDSystemInfo.Ordering.size, "Free copied Registry");
2389
168
        copied0->cidata.common.CIDSystemInfo.Registry.data = copied0->cidata.common.CIDSystemInfo.Ordering.data = NULL;
2390
168
        copied0->cidata.common.CIDSystemInfo.Registry.size = copied0->cidata.common.CIDSystemInfo.Ordering.size = 0;
2391
168
    }
2392
2393
27.4k
    if (font->FontType == ft_CID_TrueType) {
2394
1.11k
        gs_font_cid2 *copied2 = (gs_font_cid2 *)font;
2395
2396
1.11k
        if (copied2->subst_CID_on_WMode)
2397
1.11k
            rc_decrement(copied2->subst_CID_on_WMode, "gs_free_copied_font(subst_CID_on_WMode");
2398
1.11k
        gs_free_string(mem, (byte *)copied2->cidata.common.CIDSystemInfo.Registry.data, copied2->cidata.common.CIDSystemInfo.Registry.size, "Free copied Registry");
2399
1.11k
        gs_free_string(mem, (byte *)copied2->cidata.common.CIDSystemInfo.Ordering.data, copied2->cidata.common.CIDSystemInfo.Ordering.size, "Free copied Registry");
2400
1.11k
        copied2->cidata.common.CIDSystemInfo.Registry.data = copied2->cidata.common.CIDSystemInfo.Ordering.data = NULL;
2401
1.11k
        copied2->cidata.common.CIDSystemInfo.Registry.size = copied2->cidata.common.CIDSystemInfo.Ordering.size = 0;
2402
1.11k
    }
2403
2404
27.4k
    if (cfdata) {
2405
        /* free copied glyph data */
2406
58.5M
        for (i=0;i < cfdata->glyphs_size;i++) {
2407
58.5M
            pcg = &cfdata->glyphs[i];
2408
58.5M
            if(pcg->gdata.data != NULL) {
2409
7.12M
                gs_free_string(font->memory, (byte *)pcg->gdata.data, pcg->gdata.size, "Free copied glyph");
2410
7.12M
            }
2411
58.5M
            if (cfdata->names) {
2412
35.7M
                pcgn = &cfdata->names[i];
2413
35.7M
                if (pcgn->str.data != NULL) {
2414
7.08M
                    if (!gs_is_c_glyph_name(pcgn->str.data, pcgn->str.size))
2415
3.40M
                        gs_free_string(font->memory, (byte *)pcgn->str.data, pcgn->str.size, "Free copied glyph name");
2416
7.08M
                }
2417
35.7M
            }
2418
58.5M
        }
2419
27.4k
        if (cfdata->extra_names) {
2420
208
            gs_copied_glyph_extra_name_t *extra_name = cfdata->extra_names, *next;
2421
2422
25.7k
            while (extra_name != NULL) {
2423
25.5k
                next = extra_name->next;
2424
25.5k
                if (!gs_is_c_glyph_name(extra_name->name.str.data, extra_name->name.str.size))
2425
271
                    gs_free_string(font->memory, (byte *)extra_name->name.str.data, extra_name->name.str.size, "Free extra name string");
2426
25.5k
                gs_free_object(font->memory, extra_name, "free copied font(extra_names)");
2427
25.5k
                extra_name = next;
2428
25.5k
            }
2429
208
            cfdata->extra_names = NULL;
2430
208
        }
2431
2432
27.4k
        uncopy_string(mem, &cfdata->info.FullName,
2433
27.4k
                      "gs_free_copied_font(FullName)");
2434
27.4k
        uncopy_string(mem, &cfdata->info.FamilyName,
2435
27.4k
                      "gs_free_copied_font(FamilyName)");
2436
27.4k
        uncopy_string(mem, &cfdata->info.Notice,
2437
27.4k
                      "gs_free_copied_font(Notice)");
2438
27.4k
        uncopy_string(mem, &cfdata->info.Copyright,
2439
27.4k
                      "gs_free_copied_font(Copyright)");
2440
27.4k
        if (cfdata->subrs.data != NULL)
2441
16.4k
            gs_free_object(mem, cfdata->subrs.data, "gs_free_copied_font(subrs.data)");
2442
27.4k
        if (cfdata->subrs.starts != NULL)
2443
16.4k
            gs_free_object(mem, cfdata->subrs.starts, "gs_free_copied_font(subrs.dtarts)");
2444
27.4k
        if (cfdata->global_subrs.data !=  NULL)
2445
54
            gs_free_object(mem, cfdata->global_subrs.data, "gs_free_copied_font(gsubrs.data)");
2446
27.4k
        if (cfdata->global_subrs.starts !=  NULL)
2447
54
            gs_free_object(mem, cfdata->global_subrs.starts, "gs_free_copied_font(gsubrs.starts)");
2448
27.4k
        if (cfdata->Encoding)
2449
27.2k
            gs_free_object(mem, cfdata->Encoding, "gs_free_copied_font(Encoding)");
2450
27.4k
        if (cfdata->CIDMap)
2451
1.11k
            gs_free_object(mem, cfdata->CIDMap, "gs_free_copied_font(CIDMap)");
2452
27.4k
        gs_free_object(mem, cfdata->glyphs, "gs_free_copied_font(glyphs)");
2453
27.4k
        gs_free_object(mem, cfdata->names, "gs_free_copied_font(names)");
2454
27.4k
        gs_free_object(mem, cfdata->data, "gs_free_copied_font(data)");
2455
27.4k
        gs_free_object(mem, cfdata, "gs_free_copied_font(wrapper data)");
2456
27.4k
    }
2457
27.4k
    gs_free_object(mem, font, "gs_free_copied_font(copied font)");
2458
27.4k
    return 0;
2459
27.4k
}
2460
2461
/*
2462
 * Copy a glyph, including any sub-glyphs.
2463
 */
2464
int
2465
gs_copy_glyph(gs_font *font, gs_glyph glyph, gs_font *copied)
2466
7.03M
{
2467
7.03M
    return gs_copy_glyph_options(font, glyph, copied, 0);
2468
7.03M
}
2469
int
2470
gs_copy_glyph_options(gs_font *font, gs_glyph glyph, gs_font *copied,
2471
                      int options)
2472
7.74M
{
2473
7.74M
    int code;
2474
21.4M
#define MAX_GLYPH_PIECES 64  /* arbitrary, but 32 is too small - bug 687698. */
2475
7.74M
    gs_glyph glyphs[MAX_GLYPH_PIECES];
2476
7.74M
    uint count = 1, i;
2477
7.74M
    gs_copied_font_data_t *cfdata = NULL;
2478
2479
7.74M
    if (copied->procs.font_info != copied_font_info)
2480
0
        return_error(gs_error_rangecheck);
2481
7.74M
    cfdata = cf_data(copied);
2482
7.74M
    code = cfdata->procs->copy_glyph(font, glyph, copied, options);
2483
7.74M
    if (code != 0)
2484
592k
        return code;
2485
    /* Copy any sub-glyphs. */
2486
7.15M
    glyphs[0] = glyph;
2487
7.15M
    code = psf_add_subset_pieces(glyphs, &count, MAX_GLYPH_PIECES, MAX_GLYPH_PIECES,
2488
7.15M
                          font);
2489
7.15M
    if (code < 0)
2490
0
        return code;
2491
7.15M
    if (count > MAX_GLYPH_PIECES)
2492
0
        return_error(gs_error_limitcheck);
2493
7.17M
    for (i = 1; i < count; ++i) {
2494
24.7k
        code = gs_copy_glyph_options(font, glyphs[i], copied,
2495
24.7k
                                     (options & ~COPY_GLYPH_NO_OLD) | COPY_GLYPH_BY_INDEX);
2496
24.7k
        if (code < 0) {
2497
0
            int j = 0;
2498
2499
0
            for (j = 0; j < i; j++) {
2500
0
                if (cfdata->procs->uncopy_glyph != NULL)
2501
0
                    (void)cfdata->procs->uncopy_glyph(font, glyph, copied, options);
2502
0
            }
2503
0
            return code;
2504
0
        }
2505
        /* if code > 0 then we already have the glyph, so no need to process further.
2506
         * If the original glyph was not a CID then we are copying by name, not by index.
2507
         * But the copy above copies by index which means we don't have an entry for
2508
         * the glyp-h component in the name table. If we are using names then we
2509
         * absolutely *must* have an entry in the name table, so go ahead and add
2510
         * one here. Note that the array returned by psf_add_subset_pieces has the
2511
         * GIDs with an offset of GS_MIN_GLYPH_INDEX added. Previously we removed this
2512
         * offset, but if the resulting GID referenced a name already in use (or later used)
2513
         * then the generated CMAP was incorrect. By leaving the offset in place we get
2514
         * a name generated (numeric name based on GID) which gurantees no name collisions.
2515
         * (Bug #693444).
2516
         */
2517
24.7k
        if (code == 0 && glyph < GS_MIN_CID_GLYPH && glyphs[i] > GS_MIN_GLYPH_INDEX) {
2518
677
            code = copy_glyph_name(font, glyphs[i], copied,
2519
677
                               glyphs[i]);
2520
677
            if (code < 0)
2521
0
                return code;
2522
677
        }
2523
24.7k
    }
2524
    /*
2525
     * Because 'seac' accesses the Encoding of the font as well as the
2526
     * glyphs, we have to copy the Encoding entries as well.
2527
     */
2528
7.15M
    if (count == 1)
2529
7.13M
        return 0;
2530
12.7k
    switch (font->FontType) {
2531
408
    case ft_encrypted:
2532
641
    case ft_encrypted2:
2533
641
        break;
2534
12.0k
    default:
2535
12.0k
        return 0;
2536
12.7k
    }
2537
#if 0 /* No need to add subglyphs to the Encoding because they always are
2538
         taken from StandardEncoding (See the Type 1 spec about 'seac').
2539
         Attempt to add them to the encoding can cause a conflict,
2540
         if the encoding specifies different glyphs for these char codes
2541
         (See the bug #687172). */
2542
    {
2543
        gs_copied_glyph_t *pcg;
2544
        gs_glyph_data_t gdata;
2545
        gs_char chars[2];
2546
2547
        gdata.memory = font->memory;
2548
        /* Since we just copied the glyph, copied_glyph_slot can't fail. */
2549
        DISCARD(copied_glyph_slot(cf_data(copied), glyph, &pcg));
2550
        gs_glyph_data_from_string(&gdata, pcg->gdata.data, pcg->gdata.size,
2551
                                  NULL);
2552
        code = gs_type1_piece_codes((gs_font_type1 *)font, &gdata, chars);
2553
        if (code <= 0 ||  /* 0 is not possible here */
2554
            (code = gs_copied_font_add_encoding(copied, chars[0], glyphs[1])) < 0 ||
2555
            (code = gs_copied_font_add_encoding(copied, chars[1], glyphs[2])) < 0
2556
            )
2557
            return code;
2558
    }
2559
#endif
2560
641
    return 0;
2561
12.7k
#undef MAX_GLYPH_PIECES
2562
12.7k
}
2563
2564
/*
2565
 * Add an Encoding entry to a copied font.  The glyph need not already have
2566
 * been copied.
2567
 */
2568
int
2569
gs_copied_font_add_encoding(gs_font *copied, gs_char chr, gs_glyph glyph)
2570
3.09M
{
2571
3.09M
    gs_copied_font_data_t *const cfdata = cf_data(copied);
2572
2573
3.09M
    if (copied->procs.font_info != copied_font_info)
2574
0
        return_error(gs_error_rangecheck);
2575
3.09M
    return cfdata->procs->add_encoding(copied, chr, glyph);
2576
3.09M
}
2577
2578
/*
2579
 * Copy all the glyphs and, if relevant, Encoding entries from a font.  This
2580
 * is equivalent to copying the glyphs and Encoding entries individually,
2581
 * and returns errors under the same conditions.
2582
 */
2583
int
2584
gs_copy_font_complete(gs_font *font, gs_font *copied)
2585
11.8k
{
2586
11.8k
    int index, code = 0;
2587
11.8k
    gs_glyph_space_t space = GLYPH_SPACE_NAME;
2588
11.8k
    gs_glyph glyph;
2589
2590
    /*
2591
     * For Type 1 fonts and CIDFonts, enumerating the glyphs using
2592
     * GLYPH_SPACE_NAME will cover all the glyphs.  (The "names" of glyphs
2593
     * in CIDFonts are CIDs, but that is not a problem.)  For Type 42 fonts,
2594
     * however, we have to copy by name once, so that we also copy the
2595
     * name-to-GID mapping (the CharStrings dictionary in PostScript), and
2596
     * then copy again by GID, to cover glyphs that don't have names.
2597
     */
2598
13.8k
    for (;;) {
2599
13.8k
        for (index = 0;
2600
7.02M
             code >= 0 &&
2601
7.02M
                 (font->procs.enumerate_glyph(font, &index, space, &glyph),
2602
7.01M
                  index != 0);
2603
7.00M
            ) {
2604
7.00M
            if (font->FontType == ft_TrueType &&
2605
7.00M
                    ((glyph >= GS_MIN_CID_GLYPH && glyph < GS_MIN_GLYPH_INDEX) || glyph == GS_NO_GLYPH ||
2606
163k
                    (space == GLYPH_SPACE_INDEX && glyph < GS_MIN_GLYPH_INDEX)))
2607
0
                return_error(gs_error_invalidfont); /* bug 688370. */
2608
7.00M
            code = gs_copy_glyph(font, glyph, copied);
2609
7.00M
        }
2610
        /* For Type 42 fonts, if we copied by name, now copy again by index. */
2611
13.8k
        if (space == GLYPH_SPACE_NAME && font->FontType == ft_TrueType)
2612
2.05k
            space = GLYPH_SPACE_INDEX;
2613
11.8k
        else
2614
11.8k
            break;
2615
13.8k
    }
2616
11.8k
    if (cf_data(copied)->Encoding != 0)
2617
2.70M
        for (index = 0; code >= 0 && index < 256; ++index) {
2618
2.69M
            glyph = font->procs.encode_char(font, (gs_char)index,
2619
2.69M
                                            GLYPH_SPACE_NAME);
2620
2.69M
            if (glyph != GS_NO_GLYPH) {
2621
2.68M
                code = gs_copied_font_add_encoding(copied, (gs_char)index,
2622
2.68M
                                                   glyph);
2623
2.68M
                if (code == gs_error_undefined) {
2624
                    /* Skip Encoding entries, which point to undefiuned glyphs -
2625
                       happens with 033-52-5873.pdf. */
2626
154k
                    code = 0;
2627
154k
                }
2628
2.68M
                if (code == gs_error_rangecheck) {
2629
                    /* Skip Encoding entries, which point to undefiuned glyphs -
2630
                       happens with 159.pdf. */
2631
129k
                    code = 0;
2632
129k
                }
2633
2.68M
            }
2634
2.69M
        }
2635
11.8k
    if (copied->FontType != ft_composite) {
2636
11.8k
        gs_font_base *bfont = (gs_font_base *)font;
2637
11.8k
        gs_font_base *bcopied = (gs_font_base *)copied;
2638
2639
11.8k
        bcopied->encoding_index = bfont->encoding_index;
2640
11.8k
        bcopied->nearest_encoding_index = bfont->nearest_encoding_index;
2641
11.8k
    }
2642
11.8k
    return code;
2643
11.8k
}
2644
2645
/*
2646
 * Check whether specified glyphs can be copied from another font.
2647
 * It means that (1) fonts have same hinting parameters and
2648
 * (2) font subsets for the specified glyph set don't include different
2649
 * outlines or metrics. Possible returned values :
2650
 * 0 (incompatible), 1 (compatible), < 0 (error)
2651
 */
2652
int
2653
gs_copied_can_copy_glyphs(const gs_font *cfont, const gs_font *ofont,
2654
                          gs_glyph *glyphs, int num_glyphs, int glyphs_step,
2655
                          bool check_hinting)
2656
2.63M
{
2657
2.63M
    int code = 0;
2658
2659
2.63M
    if (cfont == ofont)
2660
0
        return 1;
2661
2.63M
    if (cfont->FontType != ofont->FontType)
2662
0
        return 0;
2663
2.63M
    if (cfont->WMode != ofont->WMode)
2664
0
        return 0;
2665
2.63M
    if (cfont->font_name.size == 0 || ofont->font_name.size == 0) {
2666
29.8k
        if (cfont->key_name.size != ofont->key_name.size ||
2667
29.8k
            memcmp(cfont->key_name.chars, ofont->key_name.chars,
2668
28.5k
                        cfont->font_name.size))
2669
1.26k
            return 0; /* Don't allow to merge random fonts. */
2670
2.61M
    } else {
2671
2.61M
        if (cfont->font_name.size != ofont->font_name.size ||
2672
2.61M
            memcmp(cfont->font_name.chars, ofont->font_name.chars,
2673
2.53M
                            cfont->font_name.size))
2674
159k
            return 0; /* Don't allow to merge random fonts. */
2675
2.61M
    }
2676
2.47M
    if (check_hinting) {
2677
2.47M
        switch(cfont->FontType) {
2678
1.66M
            case ft_encrypted:
2679
1.86M
            case ft_encrypted2:
2680
1.86M
                if (!same_type1_hinting((const gs_font_type1 *)cfont,
2681
1.86M
                                        (const gs_font_type1 *)ofont))
2682
42
                    return 0;
2683
1.86M
                code = 1;
2684
1.86M
                break;
2685
379k
            case ft_TrueType:
2686
379k
                code = same_type42_hinting((gs_font_type42 *)cfont,
2687
379k
                                        (gs_font_type42 *)ofont);
2688
379k
                if (code > 0)
2689
379k
                    code = same_maxp_values((gs_font_type42 *)cfont,
2690
379k
                                        (gs_font_type42 *)ofont);
2691
379k
                break;
2692
5.26k
            case ft_CID_encrypted:
2693
5.26k
                if (!gs_is_CIDSystemInfo_compatible(
2694
5.26k
                                gs_font_cid_system_info(cfont),
2695
5.26k
                                gs_font_cid_system_info(ofont)))
2696
0
                    return 0;
2697
5.26k
                code = same_cid0_hinting((const gs_font_cid0 *)cfont,
2698
5.26k
                                         (const gs_font_cid0 *)ofont);
2699
5.26k
                break;
2700
231k
            case ft_CID_TrueType:
2701
231k
                if (!gs_is_CIDSystemInfo_compatible(
2702
231k
                                gs_font_cid_system_info(cfont),
2703
231k
                                gs_font_cid_system_info(ofont)))
2704
0
                    return 0;
2705
231k
                code = same_cid2_hinting((const gs_font_cid2 *)cfont,
2706
231k
                                         (const gs_font_cid2 *)ofont);
2707
231k
                if (code > 0)
2708
231k
                    code = same_maxp_values((gs_font_type42 *)cfont,
2709
231k
                                        (gs_font_type42 *)ofont);
2710
231k
                break;
2711
0
            default:
2712
0
                return_error(gs_error_unregistered); /* Must not happen. */
2713
2.47M
        }
2714
2.47M
        if (code <= 0) /* an error or false */
2715
22.3k
            return code;
2716
2.47M
    }
2717
2.45M
    return compare_glyphs(cfont, ofont, glyphs, num_glyphs, glyphs_step, 0);
2718
2.47M
}
2719
2720
/* Extension glyphs may be added to a font to resolve
2721
   glyph name conflicts while conwerting a PDF Widths into Metrics.
2722
   This function drops them before writing out an embedded font. */
2723
int
2724
copied_drop_extension_glyphs(gs_font *copied)
2725
9.82k
{
2726
    /* When we encounter a glyph used at multiple encoding positions, and
2727
     * the encoding positions have different Widths, we end up defining
2728
     * a new glyph name, because we can't have a PostScript glyph which has
2729
     * two sets of metrics. Here we are supposed to find such duplicates
2730
     * and 'drop' them. It appears that the original intention was to mark
2731
     * the 'slot'->used member as false, with the expectation that this
2732
     * would drop the glyph from the font.
2733
     */
2734
    /*  Note : This function drops 'used' flags for some glyphs
2735
        and truncates glyph names. Can't use the font
2736
        for outlining|rasterization|width after applying it.
2737
     */
2738
9.82k
    gs_copied_font_data_t *const cfdata = cf_data(copied);
2739
9.82k
    uint gsize = cfdata->glyphs_size, ext_name;
2740
9.82k
    const int sl = strlen(gx_extendeg_glyph_name_separator);
2741
2742
17.0M
    for (ext_name = 0; ext_name < gsize; ext_name++) {
2743
17.0M
        gs_copied_glyph_t *pslot = &cfdata->glyphs[ext_name];
2744
17.0M
        gs_copied_glyph_name_t *name;
2745
17.0M
        int l, j, k, non_ext_name;
2746
2747
17.0M
        if (!pslot->used)
2748
16.8M
            continue;
2749
197k
        name = &cfdata->names[ext_name];
2750
197k
        l = name->str.size - sl;
2751
2752
423k
        for (j = 0; j < l; j ++)
2753
226k
            if (!memcmp(gx_extendeg_glyph_name_separator, name->str.data + j, sl))
2754
0
                break;
2755
197k
        if (j >= l)
2756
197k
            continue;
2757
        /* Found an extension name.
2758
           Find the corresponding non-extended one. */
2759
0
        non_ext_name = ext_name;
2760
0
        for (k = 0; k < gsize; k++)
2761
0
            if (cfdata->glyphs[k].used &&
2762
0
                    cfdata->names[k].str.size == j &&
2763
0
                    !memcmp(cfdata->names[k].str.data, name->str.data, j) &&
2764
0
                    !bytes_compare(pslot->gdata.data, pslot->gdata.size,
2765
0
                            cfdata->glyphs[k].gdata.data, cfdata->glyphs[k].gdata.size)) {
2766
0
                non_ext_name = k;
2767
0
                break;
2768
0
            }
2769
        /* Drop others with same prefix. */
2770
0
        for (k = 0; k < gsize; k++)
2771
0
            if (k != non_ext_name && cfdata->glyphs[k].used &&
2772
0
                    cfdata->names[k].str.size >= j + sl &&
2773
0
                    !memcmp(cfdata->names[k].str.data, name->str.data, j) &&
2774
0
                    !memcmp(gx_extendeg_glyph_name_separator, name->str.data + j, sl) &&
2775
0
                    !bytes_compare(pslot->gdata.data, pslot->gdata.size,
2776
0
                    cfdata->glyphs[k].gdata.data, cfdata->glyphs[k].gdata.size)) {
2777
0
                cfdata->glyphs[k].used = false;
2778
0
                cfdata->names[k].str.size = j;
2779
0
            }
2780
        /* Truncate the extended glyph name. */
2781
0
        cfdata->names[ext_name].str.size = j;
2782
0
    }
2783
9.82k
    return 0;
2784
9.82k
}
2785
2786
static int
2787
compare_glyph_names(const void *pg1, const void *pg2)
2788
603k
{
2789
603k
    const gs_copied_glyph_name_t * gn1 = *(const gs_copied_glyph_name_t **)pg1;
2790
603k
    const gs_copied_glyph_name_t * gn2 = *(const gs_copied_glyph_name_t **)pg2;
2791
2792
603k
    return bytes_compare(gn1->str.data, gn1->str.size, gn2->str.data, gn2->str.size);
2793
603k
}
2794
2795
/* Order font data to avoid a serialization indeterminism. */
2796
static int
2797
order_font_data(gs_copied_font_data_t *cfdata, gs_memory_t *memory)
2798
8.42k
{
2799
8.42k
    int i, j = 0;
2800
2801
8.42k
    gs_copied_glyph_name_t **a = (gs_copied_glyph_name_t **)gs_alloc_byte_array(memory, cfdata->num_glyphs,
2802
8.42k
        sizeof(gs_copied_glyph_name_t *), "order_font_data");
2803
8.42k
    if (a == NULL)
2804
0
        return_error(gs_error_VMerror);
2805
8.42k
    j = 0;
2806
14.6M
    for (i = 0; i < cfdata->glyphs_size; i++) {
2807
14.6M
        if (cfdata->glyphs[i].used) {
2808
164k
            if (j >= cfdata->num_glyphs) {
2809
0
                gs_free_object(memory, a, "order_font_data");
2810
0
                return_error(gs_error_unregistered); /* Must not happen */
2811
0
            }
2812
164k
            a[j++] = &cfdata->names[i];
2813
164k
        }
2814
14.6M
    }
2815
8.42k
    qsort(a, j, sizeof(*a), compare_glyph_names);
2816
172k
    for (j--; j >= 0; j--)
2817
164k
        cfdata->glyphs[j].order_index = a[j] - cfdata->names;
2818
8.42k
    gs_free_object(memory, a, "order_font_data");
2819
8.42k
    return 0;
2820
8.42k
}
2821
2822
/* Order font to avoid a serialization indeterminism. */
2823
int
2824
copied_order_font(gs_font *font)
2825
10.8k
{
2826
2827
10.8k
    if (font->procs.enumerate_glyph != copied_enumerate_glyph)
2828
0
        return_error(gs_error_unregistered); /* Must not happen */
2829
10.8k
    if (font->FontType != ft_encrypted && font->FontType != ft_encrypted2) {
2830
         /* Don't need to order, because it is ordered by CIDs or glyph indices. */
2831
2.44k
        return 0;
2832
2.44k
    }
2833
8.42k
    { gs_copied_font_data_t * cfdata = cf_data(font);
2834
8.42k
        cfdata->ordered = true;
2835
8.42k
        return order_font_data(cfdata, font->memory);
2836
10.8k
    }
2837
10.8k
}
2838
2839
/* Get .nmotdef glyph. */
2840
gs_glyph
2841
copied_get_notdef(const gs_font *font)
2842
0
{
2843
0
    gs_copied_font_data_t * cfdata = cf_data(font);
2844
2845
0
    return cfdata->notdef;
2846
0
}