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