/src/ghostpdl/devices/vector/gdevpsf2.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2001-2023 Artifex Software, Inc. |
2 | | All Rights Reserved. |
3 | | |
4 | | This software is provided AS-IS with no warranty, either express or |
5 | | implied. |
6 | | |
7 | | This software is distributed under license and may not be copied, |
8 | | modified or distributed except as expressly authorized under the terms |
9 | | of the license contained in the file LICENSE in this distribution. |
10 | | |
11 | | Refer to licensing information at http://www.artifex.com or contact |
12 | | Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
13 | | CA 94129, USA, for further information. |
14 | | */ |
15 | | |
16 | | |
17 | | /* Write an embedded CFF font with either Type 1 or Type 2 CharStrings */ |
18 | | #include "math_.h" /* for fabs */ |
19 | | #include "memory_.h" |
20 | | #include "gx.h" |
21 | | #include "gxarith.h" |
22 | | #include "gscencs.h" |
23 | | #include "gserrors.h" |
24 | | #include "gsccode.h" |
25 | | #include "gscrypt1.h" |
26 | | #include "gsmatrix.h" |
27 | | #include "gsutil.h" |
28 | | #include "gxfixed.h" |
29 | | #include "gxfont.h" |
30 | | #include "gxfont1.h" |
31 | | #include "gxfcid.h" |
32 | | #include "stream.h" |
33 | | #include "gdevpsf.h" |
34 | | |
35 | | /* Define additional opcodes used in Dicts, but not in CharStrings. */ |
36 | | #define CD_LONGINT 29 |
37 | | #define CD_REAL 30 |
38 | | |
39 | | /* Define the count of standard strings. */ |
40 | 262k | #define NUM_STD_STRINGS 391 |
41 | | |
42 | | /* Define whether or not to skip writing an empty Subrs Index. */ |
43 | | #define SKIP_EMPTY_SUBRS |
44 | | |
45 | | /* Define the structure for the string table. */ |
46 | | typedef struct cff_string_item_s { |
47 | | gs_const_string key; |
48 | | int index1; /* index + 1, 0 means empty */ |
49 | | } cff_string_item_t; |
50 | | typedef struct cff_string_table_s { |
51 | | cff_string_item_t *items; |
52 | | int count; |
53 | | int size; |
54 | | uint total; |
55 | | int reprobe; |
56 | | } cff_string_table_t; |
57 | | |
58 | | /* Define the state of the CFF writer. */ |
59 | | typedef struct cff_writer_s { |
60 | | int options; |
61 | | stream *strm; |
62 | | gs_font_base *pfont; /* type1 or cid0 */ |
63 | | glyph_data_proc_t glyph_data; |
64 | | gs_offset_t offset_size; |
65 | | gs_offset_t start_pos; |
66 | | cff_string_table_t std_strings; |
67 | | cff_string_table_t strings; |
68 | | gs_int_rect FontBBox; |
69 | | } cff_writer_t; |
70 | | typedef struct cff_glyph_subset_s { |
71 | | psf_outline_glyphs_t glyphs; |
72 | | int num_encoded; /* glyphs.subset_data[1..num_e] are encoded */ |
73 | | int num_encoded_chars; /* Encoding has num_e_chars defined entries */ |
74 | | } cff_glyph_subset_t; |
75 | | |
76 | | /* ---------------- Output utilities ---------------- */ |
77 | | |
78 | | /* ------ String tables ------ */ |
79 | | |
80 | | /* Initialize a string table. */ |
81 | | static void |
82 | | cff_string_table_init(cff_string_table_t *pcst, cff_string_item_t *items, |
83 | | int size) |
84 | 25.9k | { |
85 | 25.9k | int reprobe = 17; |
86 | | |
87 | 25.9k | memset(items, 0, size * sizeof(*items)); |
88 | 25.9k | pcst->items = items; |
89 | 25.9k | pcst->count = 0; |
90 | 25.9k | pcst->size = size; |
91 | 26.6k | while (reprobe != 1 && igcd(size, reprobe) != 1) |
92 | 740 | reprobe = (reprobe * 2 + 1) % size; |
93 | 25.9k | pcst->total = 0; |
94 | 25.9k | pcst->reprobe = reprobe; |
95 | 25.9k | } |
96 | | |
97 | | /* Add a string to a string table. */ |
98 | | static int |
99 | | cff_string_add(cff_string_table_t *pcst, const byte *data, uint size) |
100 | 5.04M | { |
101 | 5.04M | int index; |
102 | | |
103 | 5.04M | if (pcst->count >= pcst->size) |
104 | 0 | return_error(gs_error_limitcheck); |
105 | 5.04M | index = pcst->count++; |
106 | 5.04M | pcst->items[index].key.data = data; |
107 | 5.04M | pcst->items[index].key.size = size; |
108 | 5.04M | pcst->total += size; |
109 | 5.04M | return index; |
110 | 5.04M | } |
111 | | |
112 | | /* Look up a string, optionally adding it. */ |
113 | | /* Return 1 if the string was added. */ |
114 | | static int |
115 | | cff_string_index(cff_string_table_t *pcst, const byte *data, uint size, |
116 | | bool enter, int *pindex) |
117 | 6.68M | { |
118 | | /****** FAILS IF TABLE FULL AND KEY MISSING ******/ |
119 | 6.68M | int j = (size == 0 ? 0 : data[0] * 23 + data[size - 1] * 59 + size); |
120 | 6.68M | int index, c = 0; |
121 | | |
122 | 18.1M | while ((index = pcst->items[j %= pcst->size].index1) != 0) { |
123 | 12.8M | --index; |
124 | 12.8M | if (!bytes_compare(pcst->items[index].key.data, |
125 | 12.8M | pcst->items[index].key.size, data, size)) { |
126 | 1.37M | *pindex = index; |
127 | 1.37M | return 0; |
128 | 1.37M | } |
129 | 11.4M | j += pcst->reprobe; |
130 | 11.4M | if (++c >= pcst->size) |
131 | 0 | break; |
132 | 11.4M | } |
133 | 5.30M | if (!enter) |
134 | 262k | return_error(gs_error_undefined); |
135 | 5.04M | index = cff_string_add(pcst, data, size); |
136 | 5.04M | if (index < 0) |
137 | 0 | return index; |
138 | 5.04M | pcst->items[j].index1 = index + 1; |
139 | 5.04M | *pindex = index; |
140 | 5.04M | return 1; |
141 | 5.04M | } |
142 | | |
143 | | /* Get the SID for a string or a glyph. */ |
144 | | static int |
145 | | cff_string_sid(cff_writer_t *pcw, const byte *data, uint size) |
146 | 1.41M | { |
147 | 1.41M | int index; |
148 | 1.41M | int code = cff_string_index(&pcw->std_strings, data, size, false, &index); |
149 | | |
150 | 1.41M | if (code < 0) { |
151 | 262k | code = cff_string_index(&pcw->strings, data, size, true, &index); |
152 | 262k | if (code < 0) |
153 | 0 | return code; |
154 | 262k | index += NUM_STD_STRINGS; |
155 | 262k | } |
156 | 1.41M | return index; |
157 | 1.41M | } |
158 | | static int |
159 | | cff_glyph_sid(cff_writer_t *pcw, gs_glyph glyph) |
160 | 1.17M | { |
161 | 1.17M | gs_const_string str; |
162 | 1.17M | int code = |
163 | 1.17M | pcw->pfont->procs.glyph_name((gs_font *)pcw->pfont, glyph, &str); |
164 | | |
165 | 1.17M | if (code < 0) |
166 | 0 | return code; |
167 | 1.17M | return cff_string_sid(pcw, str.data, str.size); |
168 | 1.17M | } |
169 | | |
170 | | /* ------ Low level ------ */ |
171 | | |
172 | | static void |
173 | | put_card16(cff_writer_t *pcw, uint c16) |
174 | 2.04M | { |
175 | 2.04M | sputc(pcw->strm, (byte)(c16 >> 8)); |
176 | 2.04M | sputc(pcw->strm, (byte)c16); |
177 | 2.04M | } |
178 | | static int |
179 | | offset_size(uint offset) |
180 | 362k | { |
181 | 362k | int size = 1; |
182 | | |
183 | 485k | while (offset > 255) |
184 | 123k | offset >>= 8, ++size; |
185 | 362k | return size; |
186 | 362k | } |
187 | | static void |
188 | | put_offset(cff_writer_t *pcw, int offset) |
189 | 1.81M | { |
190 | 1.81M | int i; |
191 | | |
192 | 4.75M | for (i = pcw->offset_size - 1; i >= 0; --i) |
193 | 2.93M | sputc(pcw->strm, (byte)(offset >> (i * 8))); |
194 | 1.81M | } |
195 | | static int |
196 | | put_bytes(stream * s, const byte *ptr, uint count) |
197 | 482k | { |
198 | 482k | uint used; |
199 | | |
200 | 482k | sputs(s, ptr, count, &used); |
201 | 482k | return (int)used; |
202 | 482k | } |
203 | | static int |
204 | | check_ioerror(stream * s) |
205 | 204k | { |
206 | 204k | uint used = 0; |
207 | | |
208 | 204k | return sputs(s, (byte *)&used, 0, &used); |
209 | 204k | } |
210 | | |
211 | | /* ------ Data types ------ */ |
212 | | |
213 | 932k | #define CE_OFFSET 32 |
214 | | static void |
215 | | cff_put_op(cff_writer_t *pcw, int op) |
216 | 932k | { |
217 | 932k | if (op >= CE_OFFSET) { |
218 | 297k | sputc(pcw->strm, cx_escape); |
219 | 297k | sputc(pcw->strm, (byte)(op - CE_OFFSET)); |
220 | 297k | } else |
221 | 634k | sputc(pcw->strm, (byte)op); |
222 | 932k | } |
223 | | static void |
224 | | cff_put_int(cff_writer_t *pcw, int i) |
225 | 2.49M | { |
226 | 2.49M | stream *s = pcw->strm; |
227 | | |
228 | 2.49M | if (i >= -107 && i <= 107) |
229 | 1.75M | sputc(s, (byte)(i + 139)); |
230 | 737k | else if (i <= 1131 && i >= 0) |
231 | 604k | put_card16(pcw, (c_pos2_0 << 8) + i - 108); |
232 | 133k | else if (i >= -1131 && i < 0) |
233 | 48.9k | put_card16(pcw, (c_neg2_0 << 8) - i - 108); |
234 | 84.4k | else if (i >= -32768 && i <= 32767) { |
235 | 19.4k | sputc(s, c2_shortint); |
236 | 19.4k | put_card16(pcw, i & 0xffff); |
237 | 65.0k | } else { |
238 | 65.0k | sputc(s, CD_LONGINT); |
239 | 65.0k | put_card16(pcw, i >> 16); |
240 | 65.0k | put_card16(pcw, i & 0xffff); |
241 | 65.0k | } |
242 | 2.49M | } |
243 | | static void |
244 | | cff_put_int_value(cff_writer_t *pcw, int i, int op) |
245 | 368k | { |
246 | 368k | cff_put_int(pcw, i); |
247 | 368k | cff_put_op(pcw, op); |
248 | 368k | } |
249 | | static void |
250 | | cff_put_int_if_ne(cff_writer_t *pcw, int i, int i_default, int op) |
251 | 499k | { |
252 | 499k | if (i != i_default) |
253 | 236k | cff_put_int_value(pcw, i, op); |
254 | 499k | } |
255 | | static void |
256 | | cff_put_bool(cff_writer_t *pcw, bool b) |
257 | 1.59k | { |
258 | 1.59k | cff_put_int(pcw, (b ? 1 : 0)); |
259 | 1.59k | } |
260 | | static void |
261 | | cff_put_bool_value(cff_writer_t *pcw, bool b, int op) |
262 | 1.59k | { |
263 | 1.59k | cff_put_bool(pcw, b); |
264 | 1.59k | cff_put_op(pcw, op); |
265 | 1.59k | } |
266 | | static void |
267 | | cff_put_real(cff_writer_t *pcw, double f) |
268 | 1.82M | { |
269 | 1.82M | if (f == (int)f) |
270 | 1.81M | cff_put_int(pcw, (int)f); |
271 | 6.86k | else { |
272 | | /* Use decimal representation. */ |
273 | 6.86k | char str[50]; |
274 | 6.86k | byte b = 0xff; |
275 | 6.86k | const char *p; |
276 | | |
277 | 6.86k | gs_snprintf(str, sizeof(str), "%g", f); |
278 | 6.86k | sputc(pcw->strm, CD_REAL); |
279 | 52.5k | for (p = str; ; ++p) { |
280 | 52.5k | int digit; |
281 | | |
282 | 52.5k | switch (*p) { |
283 | 6.86k | case 0: |
284 | 6.86k | goto done; |
285 | 6.75k | case '.': |
286 | 6.75k | digit = 0xa; break; |
287 | 4 | case '+': |
288 | 4 | continue; |
289 | 5 | case '-': |
290 | 5 | digit = 0xe; break; |
291 | 4 | case 'e': case 'E': |
292 | 4 | if (p[1] == '-') |
293 | 0 | digit = 0xc, ++p; |
294 | 4 | else |
295 | 4 | digit = 0xb; |
296 | 4 | break; |
297 | 25.5k | case '0': case '1': case '2': case '3': case '4': |
298 | 38.4k | case '5': case '6': case '7': case '8': case '9': |
299 | 38.4k | digit = *p - '0'; |
300 | 38.4k | break; |
301 | 432 | default: /* can't happen */ |
302 | 432 | digit = 0xd; /* invalid */ |
303 | 432 | break; |
304 | 52.5k | } |
305 | 45.6k | if (b == 0xff) |
306 | 25.5k | b = (digit << 4) + 0xf; |
307 | 20.0k | else { |
308 | 20.0k | sputc(pcw->strm, (byte)((b & 0xf0) + digit)); |
309 | 20.0k | b = 0xff; |
310 | 20.0k | } |
311 | 45.6k | } |
312 | 6.86k | done: |
313 | 6.86k | sputc(pcw->strm, b); |
314 | 6.86k | } |
315 | 1.82M | } |
316 | | static void |
317 | | cff_put_real_value(cff_writer_t *pcw, double f, int op) |
318 | 110k | { |
319 | 110k | cff_put_real(pcw, f); |
320 | 110k | cff_put_op(pcw, op); |
321 | 110k | } |
322 | | static void |
323 | | cff_put_real_if_ne(cff_writer_t *pcw, double f, double f_default, int op) |
324 | 288k | { |
325 | 288k | if ((float)f != (float)f_default) |
326 | 5.95k | cff_put_real_value(pcw, f, op); |
327 | 288k | } |
328 | | static void |
329 | | cff_put_real_deltarray(cff_writer_t *pcw, const float *pf, int count, int op) |
330 | 312k | { |
331 | 312k | float prev = 0; |
332 | 312k | int i; |
333 | | |
334 | 312k | if (count <= 0) |
335 | 163k | return; |
336 | 1.61M | for (i = 0; i < count; ++i) { |
337 | 1.46M | float f = pf[i]; |
338 | | |
339 | 1.46M | cff_put_real(pcw, f - prev); |
340 | 1.46M | prev = f; |
341 | 1.46M | } |
342 | 149k | cff_put_op(pcw, op); |
343 | 149k | } |
344 | | static int |
345 | | cff_put_string(cff_writer_t *pcw, const byte *data, uint size) |
346 | 243k | { |
347 | 243k | int sid = cff_string_sid(pcw, data, size); |
348 | | |
349 | 243k | if (sid < 0) |
350 | 0 | return sid; |
351 | 243k | cff_put_int(pcw, sid); |
352 | 243k | return 0; |
353 | 243k | } |
354 | | static int |
355 | | cff_put_string_value(cff_writer_t *pcw, const byte *data, uint size, int op) |
356 | 240k | { |
357 | 240k | int code = cff_put_string(pcw, data, size); |
358 | | |
359 | 240k | if (code >= 0) |
360 | 240k | cff_put_op(pcw, op); |
361 | 240k | return code; |
362 | 240k | } |
363 | | static int |
364 | | cff_extra_lenIV(const cff_writer_t *pcw, const gs_font_type1 *pfont) |
365 | 1.23M | { |
366 | 1.23M | return (pcw->options & WRITE_TYPE2_NO_LENIV ? |
367 | 1.23M | max(pfont->data.lenIV, 0) : 0); |
368 | 1.23M | } |
369 | | static bool |
370 | | cff_convert_charstrings(const cff_writer_t *pcw, const gs_font_base *pfont) |
371 | 2.38M | { |
372 | 2.38M | return (pfont->FontType != ft_encrypted2 && |
373 | 2.38M | (pcw->options & WRITE_TYPE2_CHARSTRINGS) != 0); |
374 | 2.38M | } |
375 | | static int |
376 | | cff_put_CharString(cff_writer_t *pcw, const byte *data, uint size, |
377 | | gs_font_type1 *pfont) |
378 | 1.06M | { |
379 | 1.06M | int lenIV = pfont->data.lenIV; |
380 | 1.06M | stream *s = pcw->strm; |
381 | | |
382 | 1.06M | if (cff_convert_charstrings(pcw, (gs_font_base *)pfont)) { |
383 | 909k | gs_glyph_data_t gdata; |
384 | 909k | int code; |
385 | | |
386 | 909k | gdata.memory = pfont->memory; |
387 | 909k | gs_glyph_data_from_string(&gdata, data, size, NULL); |
388 | 909k | code = psf_convert_type1_to_type2(s, &gdata, pfont); |
389 | 909k | if (code < 0) |
390 | 0 | return code; |
391 | 909k | } else if (lenIV < 0 || !(pcw->options & WRITE_TYPE2_NO_LENIV)) |
392 | 160k | put_bytes(s, data, size); |
393 | 0 | else if (size >= lenIV) { |
394 | | /* Remove encryption. */ |
395 | 0 | crypt_state state = crypt_charstring_seed; |
396 | 0 | byte buf[50]; /* arbitrary */ |
397 | 0 | uint left, n; |
398 | |
|
399 | 0 | for (left = lenIV; left > 0; left -= n) { |
400 | 0 | n = min(left, sizeof(buf)); |
401 | 0 | gs_type1_decrypt(buf, data + lenIV - left, n, &state); |
402 | 0 | } |
403 | 0 | for (left = size - lenIV; left > 0; left -= n) { |
404 | 0 | n = min(left, sizeof(buf)); |
405 | 0 | gs_type1_decrypt(buf, data + size - left, n, &state); |
406 | 0 | put_bytes(s, buf, n); |
407 | 0 | } |
408 | 0 | } |
409 | 1.06M | return 0; |
410 | 1.06M | } |
411 | | static uint |
412 | | cff_Index_size(uint count, uint total) |
413 | 193k | { |
414 | 193k | return (count == 0 ? 2 : |
415 | 193k | 3 + offset_size(total + 1) * (count + 1) + total); |
416 | 193k | } |
417 | | static void |
418 | | cff_put_Index_header(cff_writer_t *pcw, uint count, uint total) |
419 | 306k | { |
420 | 306k | put_card16(pcw, count); |
421 | 306k | if (count > 0) { |
422 | 207k | pcw->offset_size = offset_size(total + 1); |
423 | 207k | sputc(pcw->strm, (byte)pcw->offset_size); |
424 | 207k | put_offset(pcw, 1); |
425 | 207k | } |
426 | 306k | } |
427 | | static void |
428 | | cff_put_Index(cff_writer_t *pcw, const cff_string_table_t *pcst) |
429 | 51.6k | { |
430 | 51.6k | uint j, offset; |
431 | | |
432 | 51.6k | if (pcst->count == 0) { |
433 | 238 | put_card16(pcw, 0); |
434 | 238 | return; |
435 | 238 | } |
436 | 51.4k | cff_put_Index_header(pcw, pcst->count, pcst->total); |
437 | 219k | for (j = 0, offset = 1; j < pcst->count; ++j) { |
438 | 167k | offset += pcst->items[j].key.size; |
439 | 167k | put_offset(pcw, offset); |
440 | 167k | } |
441 | 219k | for (j = 0; j < pcst->count; ++j) |
442 | 167k | put_bytes(pcw->strm, pcst->items[j].key.data, pcst->items[j].key.size); |
443 | 51.4k | } |
444 | | |
445 | | /* ---------------- Main code ---------------- */ |
446 | | |
447 | | /* ------ Header ------ */ |
448 | | |
449 | | /* Write the header, setting offset_size. */ |
450 | | static int |
451 | | cff_write_header(cff_writer_t *pcw, uint end_offset) |
452 | 51.6k | { |
453 | 51.6k | pcw->offset_size = (end_offset > 0x7fff ? 3 : 2); |
454 | 51.6k | put_bytes(pcw->strm, (const byte *)"\001\000\004", 3); |
455 | 51.6k | sputc(pcw->strm, (byte)pcw->offset_size); |
456 | 51.6k | return 0; |
457 | 51.6k | } |
458 | | |
459 | | /* ------ Top Dict ------ */ |
460 | | |
461 | | /* |
462 | | * There are 3 variants of this: Type 1 / Type 2 font, CIDFontType 0 |
463 | | * CIDFont, and FDArray entry for CIDFont. |
464 | | */ |
465 | | |
466 | | typedef enum { |
467 | | TOP_version = 0, |
468 | | TOP_Notice = 1, |
469 | | TOP_FullName = 2, |
470 | | TOP_FamilyName = 3, |
471 | | TOP_Weight = 4, |
472 | | TOP_FontBBox = 5, |
473 | | TOP_UniqueID = 13, |
474 | | TOP_XUID = 14, |
475 | | TOP_charset = 15, /* (offset or predefined index) */ |
476 | | #define charset_ISOAdobe 0 |
477 | | #define charset_Expert 1 |
478 | | #define charset_ExpertSubset 2 |
479 | 64.4k | #define charset_DEFAULT 0 |
480 | | TOP_Encoding = 16, /* (offset or predefined index) */ |
481 | | #define Encoding_Standard 0 |
482 | | #define Encoding_Expert 1 |
483 | 63.8k | #define Encoding_DEFAULT 0 |
484 | | TOP_CharStrings = 17, /* (offset) */ |
485 | | TOP_Private = 18, /* (offset) */ |
486 | | TOP_Copyright = 32, |
487 | | TOP_isFixedPitch = 33, |
488 | | #define isFixedPitch_DEFAULT false |
489 | | TOP_ItalicAngle = 34, |
490 | 131k | #define ItalicAngle_DEFAULT 0 |
491 | | TOP_UnderlinePosition = 35, |
492 | 131k | #define UnderlinePosition_DEFAULT (-100) |
493 | | TOP_UnderlineThickness = 36, |
494 | 131k | #define UnderlineThickness_DEFAULT 50 |
495 | | TOP_PaintType = 37, |
496 | 65.8k | #define PaintType_DEFAULT 0 |
497 | | TOP_CharstringType = 38, |
498 | 63.8k | #define CharstringType_DEFAULT 2 |
499 | | TOP_FontMatrix = 39, /* default is [0.001 0 0 0.001 0 0] */ |
500 | | TOP_StrokeWidth = 40, |
501 | 65.8k | #define StrokeWidth_DEFAULT 0 |
502 | | TOP_ROS = 62, |
503 | | TOP_CIDFontVersion = 63, |
504 | | #define CIDFontVersion_DEFAULT 0 |
505 | | TOP_CIDFontRevision = 64, |
506 | | #define CIDFontRevision_DEFAULT 0 |
507 | | TOP_CIDFontType = 65, |
508 | | #define CIDFontType_DEFAULT 0 |
509 | | TOP_CIDCount = 66, |
510 | 670 | #define CIDCount_DEFAULT 8720 |
511 | | TOP_UIDBase = 67, |
512 | | TOP_FDArray = 68, /* (offset) */ |
513 | | TOP_FDSelect = 69, /* (offset) */ |
514 | | TOP_FontName = 70 /* only used in FDArray "fonts" */ |
515 | | } Top_op; |
516 | | |
517 | | static int |
518 | | cff_get_Top_info_common(cff_writer_t *pcw, gs_font_base *pbfont, |
519 | | bool full_info, gs_font_info_t *pinfo) |
520 | 65.3k | { |
521 | 65.3k | pinfo->Flags_requested = FONT_IS_FIXED_WIDTH; |
522 | | /* Preset defaults */ |
523 | 65.3k | pinfo->members = 0; |
524 | 65.3k | pinfo->Flags = pinfo->Flags_returned = 0; |
525 | 65.3k | pinfo->ItalicAngle = ItalicAngle_DEFAULT; |
526 | 65.3k | pinfo->UnderlinePosition = UnderlinePosition_DEFAULT; |
527 | 65.3k | pinfo->UnderlineThickness = UnderlineThickness_DEFAULT; |
528 | 65.3k | return pbfont->procs.font_info |
529 | 65.3k | ((gs_font *)pbfont, NULL, |
530 | 65.3k | (full_info ? |
531 | 63.9k | FONT_INFO_FLAGS | FONT_INFO_ITALIC_ANGLE | |
532 | 63.9k | FONT_INFO_UNDERLINE_POSITION | |
533 | 63.9k | FONT_INFO_UNDERLINE_THICKNESS : 0) | |
534 | 65.3k | (FONT_INFO_COPYRIGHT | FONT_INFO_NOTICE | |
535 | 65.3k | FONT_INFO_FAMILY_NAME | FONT_INFO_FULL_NAME), |
536 | 65.3k | pinfo); |
537 | 65.3k | } |
538 | | static void |
539 | | cff_write_Top_common(cff_writer_t *pcw, gs_font_base *pbfont, |
540 | | bool write_FontMatrix, const gs_font_info_t *pinfo) |
541 | 65.8k | { |
542 | | /* |
543 | | * The Adobe documentation doesn't make it at all clear that if the |
544 | | * FontMatrix is missing (defaulted) in a CFF CIDFont, all of the |
545 | | * FontMatrices of the subfonts in FDArray are multiplied by 1000. |
546 | | * (This is documented for ordinary CIDFonts, but not for CFF CIDFonts.) |
547 | | * Because of this, the FontMatrix for a CFF CIDFont must be written |
548 | | * even if if is the default. write_FontMatrix controls this. |
549 | | */ |
550 | | /* (version) */ |
551 | 65.8k | if (pinfo->members & FONT_INFO_NOTICE) |
552 | 62.8k | cff_put_string_value(pcw, pinfo->Notice.data, pinfo->Notice.size, |
553 | 62.8k | TOP_Notice); |
554 | 65.8k | if (pinfo->members & FONT_INFO_FULL_NAME) |
555 | 61.1k | cff_put_string_value(pcw, pinfo->FullName.data, pinfo->FullName.size, |
556 | 61.1k | TOP_FullName); |
557 | 65.8k | if (pinfo->members & FONT_INFO_FAMILY_NAME) |
558 | 61.0k | cff_put_string_value(pcw, pinfo->FamilyName.data, |
559 | 61.0k | pinfo->FamilyName.size, TOP_FamilyName); |
560 | 65.8k | if (pcw->FontBBox.p.x != 0 || pcw->FontBBox.p.y != 0 || |
561 | 65.8k | pcw->FontBBox.q.x != 0 || pcw->FontBBox.q.y != 0 |
562 | 65.8k | ) { |
563 | | /* An omitted FontBBox is equivalent to an empty one. */ |
564 | | /* |
565 | | * Since Acrobat Reader 4 on Solaris doesn't like |
566 | | * an omitted FontBBox, we copy it here from |
567 | | * the font descriptor, because the base font |
568 | | * is allowed to omit it's FontBBox. |
569 | | */ |
570 | 58.1k | cff_put_real(pcw, pcw->FontBBox.p.x); |
571 | 58.1k | cff_put_real(pcw, pcw->FontBBox.p.y); |
572 | 58.1k | cff_put_real(pcw, pcw->FontBBox.q.x); |
573 | 58.1k | cff_put_real(pcw, pcw->FontBBox.q.y); |
574 | 58.1k | cff_put_op(pcw, TOP_FontBBox); |
575 | 58.1k | } |
576 | 65.8k | if (uid_is_UniqueID(&pbfont->UID)) |
577 | 110 | cff_put_int_value(pcw, pbfont->UID.id, TOP_UniqueID); |
578 | 65.7k | else if (uid_is_XUID(&pbfont->UID) && (pcw->options & WRITE_TYPE2_XUID) != 0) { |
579 | 0 | int j, k = uid_XUID_size(&pbfont->UID); |
580 | | |
581 | | /* Adobe products (specifically Acrobat but the same limitation is mentioned |
582 | | * in the PLRM) cannot handle XUIDs > 16 entries. |
583 | | */ |
584 | 0 | if (k > 16) |
585 | 0 | k = 16; |
586 | 0 | for (j = 0; j < uid_XUID_size(&pbfont->UID); ++j) |
587 | 0 | cff_put_int(pcw, uid_XUID_values(&pbfont->UID)[j]); |
588 | 0 | cff_put_op(pcw, TOP_XUID); |
589 | 0 | } |
590 | | /* |
591 | | * Acrobat Reader 3 gives an error if a CFF font includes any of the |
592 | | * following opcodes. |
593 | | */ |
594 | 65.8k | if (!(pcw->options & WRITE_TYPE2_AR3)) { |
595 | 65.8k | if (pinfo->members & FONT_INFO_COPYRIGHT) |
596 | 55.9k | cff_put_string_value(pcw, pinfo->Copyright.data, |
597 | 55.9k | pinfo->Copyright.size, TOP_Copyright); |
598 | 65.8k | if (pinfo->Flags & pinfo->Flags_returned & FONT_IS_FIXED_WIDTH) |
599 | 801 | cff_put_bool_value(pcw, true, TOP_isFixedPitch); |
600 | 65.8k | cff_put_real_if_ne(pcw, pinfo->ItalicAngle, ItalicAngle_DEFAULT, |
601 | 65.8k | TOP_ItalicAngle); |
602 | 65.8k | cff_put_int_if_ne(pcw, pinfo->UnderlinePosition, |
603 | 65.8k | UnderlinePosition_DEFAULT, TOP_UnderlinePosition); |
604 | 65.8k | cff_put_int_if_ne(pcw, pinfo->UnderlineThickness, |
605 | 65.8k | UnderlineThickness_DEFAULT, TOP_UnderlineThickness); |
606 | 65.8k | cff_put_int_if_ne(pcw, pbfont->PaintType, PaintType_DEFAULT, |
607 | 65.8k | TOP_PaintType); |
608 | 65.8k | } |
609 | 65.8k | { |
610 | 65.8k | static const gs_matrix fm_default = { |
611 | 65.8k | constant_matrix_body(0.001, 0, 0, 0.001, 0, 0) |
612 | 65.8k | }; |
613 | | |
614 | 65.8k | if (write_FontMatrix || |
615 | 65.8k | pbfont->FontMatrix.xx != fm_default.xx || |
616 | 65.8k | pbfont->FontMatrix.xy != 0 || pbfont->FontMatrix.yx != 0 || |
617 | 65.8k | pbfont->FontMatrix.yy != fm_default.yy || |
618 | 65.8k | pbfont->FontMatrix.tx != 0 || pbfont->FontMatrix.ty != 0 |
619 | 65.8k | ) { |
620 | 2.47k | cff_put_real(pcw, pbfont->FontMatrix.xx); |
621 | 2.47k | cff_put_real(pcw, pbfont->FontMatrix.xy); |
622 | 2.47k | cff_put_real(pcw, pbfont->FontMatrix.yx); |
623 | 2.47k | cff_put_real(pcw, pbfont->FontMatrix.yy); |
624 | 2.47k | cff_put_real(pcw, pbfont->FontMatrix.tx); |
625 | 2.47k | cff_put_real(pcw, pbfont->FontMatrix.ty); |
626 | 2.47k | cff_put_op(pcw, TOP_FontMatrix); |
627 | 2.47k | } |
628 | 65.8k | } |
629 | 65.8k | cff_put_real_if_ne(pcw, pbfont->StrokeWidth, StrokeWidth_DEFAULT, |
630 | 65.8k | TOP_StrokeWidth); |
631 | 65.8k | } |
632 | | |
633 | | /* Type 1 or Type 2 font */ |
634 | | static void |
635 | | cff_write_Top_font(cff_writer_t *pcw, uint Encoding_offset, |
636 | | uint charset_offset, uint CharStrings_offset, |
637 | | uint Private_offset, uint Private_size) |
638 | 63.8k | { |
639 | 63.8k | gs_font_base *pbfont = (gs_font_base *)pcw->pfont; |
640 | 63.8k | gs_font_info_t info; |
641 | | |
642 | 63.8k | cff_get_Top_info_common(pcw, pbfont, true, &info); |
643 | 63.8k | cff_write_Top_common(pcw, pbfont, false, &info); |
644 | 63.8k | cff_put_int(pcw, Private_size); |
645 | 63.8k | cff_put_int_value(pcw, Private_offset, TOP_Private); |
646 | 63.8k | cff_put_int_value(pcw, CharStrings_offset, TOP_CharStrings); |
647 | 63.8k | cff_put_int_if_ne(pcw, charset_offset, charset_DEFAULT, TOP_charset); |
648 | 63.8k | cff_put_int_if_ne(pcw, Encoding_offset, Encoding_DEFAULT, TOP_Encoding); |
649 | 63.8k | { |
650 | 63.8k | int type = (pcw->options & WRITE_TYPE2_CHARSTRINGS ? 2 : |
651 | 63.8k | pbfont->FontType == ft_encrypted2 ? 2 : 1); |
652 | | |
653 | 63.8k | cff_put_int_if_ne(pcw, type, CharstringType_DEFAULT, |
654 | 63.8k | TOP_CharstringType); |
655 | 63.8k | } |
656 | 63.8k | } |
657 | | |
658 | | /* CIDFontType 0 CIDFont */ |
659 | | static void |
660 | | cff_write_ROS(cff_writer_t *pcw, const gs_cid_system_info_t *pcidsi) |
661 | 836 | { |
662 | 836 | cff_put_string(pcw, pcidsi->Registry.data, pcidsi->Registry.size); |
663 | 836 | cff_put_string(pcw, pcidsi->Ordering.data, pcidsi->Ordering.size); |
664 | 836 | cff_put_int_value(pcw, pcidsi->Supplement, TOP_ROS); |
665 | 836 | } |
666 | | static void |
667 | | cff_write_Top_cidfont(cff_writer_t *pcw, uint charset_offset, |
668 | | uint CharStrings_offset, uint FDSelect_offset, |
669 | | uint Font_offset, const gs_font_info_t *pinfo) |
670 | 670 | { |
671 | 670 | gs_font_base *pbfont = (gs_font_base *)pcw->pfont; |
672 | 670 | gs_font_cid0 *pfont = (gs_font_cid0 *)pbfont; |
673 | | |
674 | 670 | cff_write_ROS(pcw, &pfont->cidata.common.CIDSystemInfo); |
675 | 670 | cff_write_Top_common(pcw, pbfont, true, pinfo); /* full_info = true */ |
676 | 670 | cff_put_int_if_ne(pcw, charset_offset, charset_DEFAULT, TOP_charset); |
677 | 670 | cff_put_int_value(pcw, CharStrings_offset, TOP_CharStrings); |
678 | | /* |
679 | | * CIDFontVersion and CIDFontRevision aren't used consistently, |
680 | | * so we don't currently write them. CIDFontType is always 0. |
681 | | */ |
682 | 670 | cff_put_int_if_ne(pcw, pfont->cidata.common.CIDCount, CIDCount_DEFAULT, |
683 | 670 | TOP_CIDCount); |
684 | | /* We don't use UIDBase. */ |
685 | 670 | cff_put_int_value(pcw, Font_offset, TOP_FDArray); |
686 | 670 | cff_put_int_value(pcw, FDSelect_offset, TOP_FDSelect); |
687 | 670 | } |
688 | | |
689 | | /* FDArray Index for CIDFont (offsets only) */ |
690 | | static void |
691 | | cff_write_FDArray_offsets(cff_writer_t *pcw, uint *FDArray_offsets, |
692 | | int num_fonts) |
693 | 670 | { |
694 | 670 | int j; |
695 | | |
696 | 670 | cff_put_Index_header(pcw, num_fonts, |
697 | 670 | FDArray_offsets[num_fonts] - FDArray_offsets[0]); |
698 | 1.79k | for (j = 1; j <= num_fonts; ++j) |
699 | 1.12k | put_offset(pcw, FDArray_offsets[j] - FDArray_offsets[0] + 1); |
700 | 670 | } |
701 | | |
702 | | /* FDArray entry for CIDFont */ |
703 | | static void |
704 | | cff_write_Top_fdarray(cff_writer_t *pcw, gs_font_base *pbfont, |
705 | | uint Private_offset, uint Private_size) |
706 | 1.39k | { |
707 | 1.39k | const gs_font_name *pfname = &pbfont->font_name; |
708 | 1.39k | gs_font_info_t info; |
709 | | |
710 | 1.39k | cff_get_Top_info_common(pcw, pbfont, false, &info); |
711 | 1.39k | cff_write_Top_common(pcw, pbfont, true, &info); |
712 | 1.39k | cff_put_int(pcw, Private_size); |
713 | 1.39k | cff_put_int_value(pcw, Private_offset, TOP_Private); |
714 | 1.39k | if (pfname->size == 0) |
715 | 217 | pfname = &pbfont->key_name; |
716 | 1.39k | if (pfname->size) { |
717 | 1.18k | cff_put_string(pcw, pfname->chars, pfname->size); |
718 | 1.18k | cff_put_op(pcw, TOP_FontName); |
719 | 1.18k | } |
720 | 1.39k | } |
721 | | |
722 | | /* ------ Private Dict ------ */ |
723 | | |
724 | | /* Defaults are noted in comments. */ |
725 | | typedef enum { |
726 | | PRIVATE_BlueValues = 6, /* (deltarray) */ |
727 | | PRIVATE_OtherBlues = 7, /* (deltarray) */ |
728 | | PRIVATE_FamilyBlues = 8, /* (deltarray) */ |
729 | | PRIVATE_FamilyOtherBlues = 9, /* (deltarray) */ |
730 | | PRIVATE_StdHW = 10, |
731 | | PRIVATE_StdVW = 11, |
732 | | PRIVATE_Subrs = 19, /* (offset, relative to Private Dict) */ |
733 | | PRIVATE_defaultWidthX = 20, |
734 | 4.55k | #define defaultWidthX_DEFAULT fixed_0 |
735 | | PRIVATE_nominalWidthX = 21, |
736 | 4.55k | #define nominalWidthX_DEFAULT fixed_0 |
737 | | PRIVATE_BlueScale = 41, |
738 | 52.1k | #define BlueScale_DEFAULT 0.039625 |
739 | | PRIVATE_BlueShift = 42, |
740 | 52.1k | #define BlueShift_DEFAULT 7 |
741 | | PRIVATE_BlueFuzz = 43, |
742 | 52.1k | #define BlueFuzz_DEFAULT 1 |
743 | | PRIVATE_StemSnapH = 44, /* (deltarray) */ |
744 | | PRIVATE_StemSnapV = 45, /* (deltarray) */ |
745 | | PRIVATE_ForceBold = 46, |
746 | 52.1k | #define ForceBold_DEFAULT false |
747 | | PRIVATE_ForceBoldThreshold = 47, |
748 | | #define ForceBoldThreshold_DEFAULT 0 |
749 | | PRIVATE_lenIV = 48, |
750 | 0 | #define lenIV_DEFAULT (-1) |
751 | | PRIVATE_LanguageGroup = 49, |
752 | 52.1k | #define LanguageGroup_DEFAULT 0 |
753 | | PRIVATE_ExpansionFactor = 50, |
754 | 52.1k | #define ExpansionFactor_DEFAULT 0.06 |
755 | | PRIVATE_initialRandomSeed = 51 |
756 | 4.55k | #define initialRandomSeed_DEFAULT 0 |
757 | | } Private_op; |
758 | | |
759 | | const long default_defaultWidthX = defaultWidthX_DEFAULT; /* For gdevpsfx.c */ |
760 | | |
761 | | static void |
762 | | cff_write_Private(cff_writer_t *pcw, uint Subrs_offset, |
763 | | const gs_font_type1 *pfont) |
764 | 52.1k | { |
765 | 52.1k | #define PUT_FLOAT_TABLE(member, op)\ |
766 | 312k | cff_put_real_deltarray(pcw, pfont->data.member.values,\ |
767 | 312k | pfont->data.member.count, op) |
768 | | |
769 | 52.1k | PUT_FLOAT_TABLE(BlueValues, PRIVATE_BlueValues); |
770 | 52.1k | PUT_FLOAT_TABLE(OtherBlues, PRIVATE_OtherBlues); |
771 | 52.1k | PUT_FLOAT_TABLE(FamilyBlues, PRIVATE_FamilyBlues); |
772 | 52.1k | PUT_FLOAT_TABLE(FamilyOtherBlues, PRIVATE_FamilyOtherBlues); |
773 | 52.1k | if (pfont->data.StdHW.count > 0) |
774 | 49.6k | cff_put_real_value(pcw, pfont->data.StdHW.values[0], PRIVATE_StdHW); |
775 | 52.1k | if (pfont->data.StdVW.count > 0) |
776 | 49.7k | cff_put_real_value(pcw, pfont->data.StdVW.values[0], PRIVATE_StdVW); |
777 | 52.1k | if (Subrs_offset) |
778 | 136 | cff_put_int_value(pcw, Subrs_offset, PRIVATE_Subrs); |
779 | 52.1k | if (pfont->FontType != ft_encrypted) { |
780 | 4.55k | if (pfont->data.defaultWidthX != defaultWidthX_DEFAULT) |
781 | 2.76k | cff_put_real_value(pcw, fixed2float(pfont->data.defaultWidthX), |
782 | 2.76k | PRIVATE_defaultWidthX); |
783 | 4.55k | if (pfont->data.nominalWidthX != nominalWidthX_DEFAULT) |
784 | 2.37k | cff_put_real_value(pcw, fixed2float(pfont->data.nominalWidthX), |
785 | 2.37k | PRIVATE_nominalWidthX); |
786 | 4.55k | cff_put_int_if_ne(pcw, pfont->data.initialRandomSeed, |
787 | 4.55k | initialRandomSeed_DEFAULT, |
788 | 4.55k | PRIVATE_initialRandomSeed); |
789 | 4.55k | } |
790 | 52.1k | cff_put_real_if_ne(pcw, pfont->data.BlueScale, BlueScale_DEFAULT, |
791 | 52.1k | PRIVATE_BlueScale); |
792 | 52.1k | cff_put_real_if_ne(pcw, pfont->data.BlueShift, BlueShift_DEFAULT, |
793 | 52.1k | PRIVATE_BlueShift); |
794 | 52.1k | cff_put_int_if_ne(pcw, pfont->data.BlueFuzz, BlueFuzz_DEFAULT, |
795 | 52.1k | PRIVATE_BlueFuzz); |
796 | 52.1k | PUT_FLOAT_TABLE(StemSnapH, PRIVATE_StemSnapH); |
797 | 52.1k | PUT_FLOAT_TABLE(StemSnapV, PRIVATE_StemSnapV); |
798 | 52.1k | if (pfont->data.ForceBold != ForceBold_DEFAULT) |
799 | 792 | cff_put_bool_value(pcw, pfont->data.ForceBold, |
800 | 792 | PRIVATE_ForceBold); |
801 | | /* (ForceBoldThreshold) */ |
802 | 52.1k | if (!(pcw->options & WRITE_TYPE2_NO_LENIV)) |
803 | 0 | cff_put_int_if_ne(pcw, pfont->data.lenIV, lenIV_DEFAULT, |
804 | 0 | PRIVATE_lenIV); |
805 | 52.1k | cff_put_int_if_ne(pcw, pfont->data.LanguageGroup, LanguageGroup_DEFAULT, |
806 | 52.1k | PRIVATE_LanguageGroup); |
807 | 52.1k | cff_put_real_if_ne(pcw, pfont->data.ExpansionFactor, |
808 | 52.1k | ExpansionFactor_DEFAULT, PRIVATE_ExpansionFactor); |
809 | | /* initialRandomSeed was handled above */ |
810 | | |
811 | 52.1k | #undef PUT_FLOAT_TABLE |
812 | 52.1k | } |
813 | | |
814 | | /* ------ CharStrings Index ------ */ |
815 | | |
816 | | /* These are separate procedures only for readability. */ |
817 | | static int |
818 | | cff_write_CharStrings_offsets(cff_writer_t *pcw, psf_glyph_enum_t *penum, |
819 | | uint *pcount) |
820 | 64.6k | { |
821 | 64.6k | gs_font_base *pfont = pcw->pfont; |
822 | 64.6k | int offset; |
823 | 64.6k | gs_glyph glyph; |
824 | 64.6k | uint count; |
825 | 64.6k | stream poss; |
826 | 64.6k | int code; |
827 | | |
828 | 64.6k | s_init(&poss, NULL); |
829 | 64.6k | psf_enumerate_glyphs_reset(penum); |
830 | 64.6k | for (glyph = GS_NO_GLYPH, count = 0, offset = 1; |
831 | 1.29M | (code = psf_enumerate_glyphs_next(penum, &glyph)) != 1; |
832 | 1.23M | ) { |
833 | 1.23M | gs_glyph_data_t gdata; |
834 | 1.23M | gs_font_type1 *pfd; |
835 | 1.23M | int gcode; |
836 | | |
837 | 1.23M | gdata.memory = pfont->memory; |
838 | 1.23M | if (code == 0 && |
839 | 1.23M | (gcode = pcw->glyph_data(pfont, glyph, &gdata, &pfd)) >= 0 |
840 | 1.23M | ) { |
841 | 1.23M | int extra_lenIV; |
842 | | |
843 | 1.23M | if (gdata.bits.size >= (extra_lenIV = cff_extra_lenIV(pcw, pfd))) { |
844 | 1.23M | if (cff_convert_charstrings(pcw, (gs_font_base *)pfd)) { |
845 | 1.13M | swrite_position_only(&poss); |
846 | 1.13M | code = psf_convert_type1_to_type2(&poss, &gdata, pfd); |
847 | 1.13M | if (code < 0) |
848 | 86 | return code; |
849 | 1.13M | offset += stell(&poss); |
850 | 1.13M | } else |
851 | 95.9k | offset += gdata.bits.size - extra_lenIV; |
852 | 1.23M | } |
853 | 1.23M | gs_glyph_data_free(&gdata, "cff_write_CharStrings_offsets"); |
854 | 1.23M | put_offset(pcw, offset); |
855 | 1.23M | count++; |
856 | 1.23M | } |
857 | 1.23M | } |
858 | 64.5k | *pcount = count; |
859 | 64.5k | return offset - 1; |
860 | 64.6k | } |
861 | | static void |
862 | | cff_write_CharStrings(cff_writer_t *pcw, psf_glyph_enum_t *penum, |
863 | | uint charstrings_count, uint charstrings_size) |
864 | 51.6k | { |
865 | 51.6k | gs_font_base *pfont = pcw->pfont; |
866 | 51.6k | uint ignore_count; |
867 | 51.6k | gs_glyph glyph; |
868 | 51.6k | int code; |
869 | | |
870 | 51.6k | cff_put_Index_header(pcw, charstrings_count, charstrings_size); |
871 | 51.6k | cff_write_CharStrings_offsets(pcw, penum, &ignore_count); |
872 | 51.6k | psf_enumerate_glyphs_reset(penum); |
873 | 51.6k | for (glyph = GS_NO_GLYPH; |
874 | 1.03M | (code = psf_enumerate_glyphs_next(penum, &glyph)) != 1; |
875 | 986k | ) { |
876 | 986k | gs_glyph_data_t gdata; |
877 | 986k | gs_font_type1 *pfd; |
878 | | |
879 | 986k | gdata.memory = pfont->memory; |
880 | 986k | if (code == 0 && |
881 | 986k | (code = pcw->glyph_data(pfont, glyph, &gdata, &pfd)) >= 0 |
882 | 986k | ) { |
883 | 986k | cff_put_CharString(pcw, gdata.bits.data, gdata.bits.size, pfd); |
884 | 986k | gs_glyph_data_free(&gdata, "cff_write_CharStrings"); |
885 | 986k | } |
886 | 986k | } |
887 | 51.6k | } |
888 | | |
889 | | /* ------ [G]Subrs Index ------ */ |
890 | | |
891 | | /* |
892 | | * Currently, we always write all the Subrs, even for subsets. |
893 | | * We will fix this someday. |
894 | | */ |
895 | | |
896 | | static uint |
897 | | cff_write_Subrs_offsets(cff_writer_t *pcw, uint *pcount, gs_font_type1 *pfont, |
898 | | bool global) |
899 | 2.38k | { |
900 | 2.38k | int extra_lenIV = cff_extra_lenIV(pcw, pfont); |
901 | 2.38k | int j, offset; |
902 | 2.38k | int code; |
903 | 2.38k | gs_glyph_data_t gdata; |
904 | | |
905 | 2.38k | gdata.memory = pfont->memory; |
906 | 2.38k | for (j = 0, offset = 1; |
907 | 106k | (code = pfont->data.procs.subr_data(pfont, j, global, &gdata)) != |
908 | 106k | gs_error_rangecheck; |
909 | 104k | ++j) { |
910 | 104k | if (code >= 0 && gdata.bits.size >= extra_lenIV) |
911 | 104k | offset += gdata.bits.size - extra_lenIV; |
912 | 104k | put_offset(pcw, offset); |
913 | 104k | if (code >= 0) |
914 | 104k | gs_glyph_data_free(&gdata, "cff_write_Subrs_offsets"); |
915 | 104k | } |
916 | 2.38k | *pcount = j; |
917 | 2.38k | return offset - 1; |
918 | 2.38k | } |
919 | | |
920 | | static void |
921 | | cff_write_Subrs(cff_writer_t *pcw, uint subrs_count, uint subrs_size, |
922 | | gs_font_type1 *pfont, bool global) |
923 | 256 | { |
924 | 256 | int j; |
925 | 256 | uint ignore_count; |
926 | 256 | gs_glyph_data_t gdata; |
927 | 256 | int code; |
928 | | |
929 | 256 | gdata.memory = pfont->memory; |
930 | 256 | cff_put_Index_header(pcw, subrs_count, subrs_size); |
931 | 256 | cff_write_Subrs_offsets(pcw, &ignore_count, pfont, global); |
932 | 256 | for (j = 0; |
933 | 83.6k | (code = pfont->data.procs.subr_data(pfont, j, global, &gdata)) != |
934 | 83.6k | gs_error_rangecheck; |
935 | 83.3k | ++j) { |
936 | 83.3k | if (code >= 0) { |
937 | 83.3k | cff_put_CharString(pcw, gdata.bits.data, gdata.bits.size, pfont); |
938 | 83.3k | gs_glyph_data_free(&gdata, "cff_write_Subrs"); |
939 | 83.3k | } |
940 | 83.3k | } |
941 | 256 | } |
942 | | |
943 | | /* ------ Encoding/charset ------ */ |
944 | | |
945 | | static uint |
946 | | cff_Encoding_size(cff_writer_t *pcw, cff_glyph_subset_t *pgsub) |
947 | 12.7k | { |
948 | 12.7k | int j, code, max_enc = 0, nsupp = 0; |
949 | 12.7k | gs_font_type1 *pfont = (gs_font_type1 *)pcw->pfont; |
950 | 12.7k | byte used[255]; |
951 | 12.7k | gs_const_string str; |
952 | | |
953 | 12.7k | memset(used, 0, 255); |
954 | 3.28M | for (j = 0; j < 256; ++j) { |
955 | 3.27M | gs_glyph glyph = pfont->procs.encode_char((gs_font *)pfont, |
956 | 3.27M | (gs_char)j, |
957 | 3.27M | GLYPH_SPACE_NAME); |
958 | 3.27M | int i; |
959 | | |
960 | 3.27M | if (glyph == GS_NO_GLYPH || glyph == pgsub->glyphs.notdef) |
961 | 3.04M | continue; |
962 | 232k | i = psf_sorted_glyphs_index_of(pgsub->glyphs.subset_data + 1, |
963 | 232k | pgsub->num_encoded, glyph); |
964 | 232k | if (i < 0) |
965 | 0 | continue; /* encoded but not in subset */ |
966 | 232k | code = pcw->pfont->procs.glyph_name((gs_font *)pcw->pfont, glyph, &str); |
967 | 232k | if (code < 0) |
968 | 0 | continue; |
969 | 232k | if (i >= sizeof(used) || used[i]) |
970 | 44 | nsupp++; |
971 | 232k | else { |
972 | 232k | used[i] = 1; |
973 | 232k | if (i > max_enc) |
974 | 34.4k | max_enc = i; |
975 | 232k | } |
976 | 232k | } |
977 | 12.7k | return 2 + (max_enc+1) + (3 * nsupp) + (nsupp > 0 ? 1 : 0); |
978 | 12.7k | } |
979 | | |
980 | | static int |
981 | | cff_write_Encoding(cff_writer_t *pcw, cff_glyph_subset_t *pgsub) |
982 | 51.0k | { |
983 | 51.0k | stream *s = pcw->strm; |
984 | | /* This procedure is only used for Type 1 / Type 2 fonts. */ |
985 | 51.0k | gs_font_type1 *pfont = (gs_font_type1 *)pcw->pfont; |
986 | 51.0k | byte used[255], index[255], supplement[256]; |
987 | 51.0k | int num_enc = min(pgsub->num_encoded, sizeof(index)); |
988 | 51.0k | int nsupp = 0; |
989 | 51.0k | int j, code; |
990 | 51.0k | int max_enc = 0; |
991 | 51.0k | gs_const_string str; |
992 | | |
993 | 51.0k | memset(used, 0, num_enc); |
994 | 51.0k | memset(index, 0, sizeof(index)); |
995 | 13.1M | for (j = 0; j < 256; ++j) { |
996 | 13.0M | gs_glyph glyph = pfont->procs.encode_char((gs_font *)pfont, |
997 | 13.0M | (gs_char)j, |
998 | 13.0M | GLYPH_SPACE_NAME); |
999 | 13.0M | int i; |
1000 | | |
1001 | 13.0M | if (glyph == GS_NO_GLYPH || glyph == pgsub->glyphs.notdef) |
1002 | 12.1M | continue; |
1003 | 928k | i = psf_sorted_glyphs_index_of(pgsub->glyphs.subset_data + 1, |
1004 | 928k | pgsub->num_encoded, glyph); |
1005 | 928k | if (i < 0) |
1006 | 0 | continue; /* encoded but not in subset */ |
1007 | 928k | code = pcw->pfont->procs.glyph_name((gs_font *)pcw->pfont, glyph, &str); |
1008 | 928k | if (code < 0) |
1009 | 0 | continue; |
1010 | 928k | if (i >= sizeof(used) || used[i]) |
1011 | 176 | supplement[nsupp++] = j; |
1012 | 927k | else { |
1013 | 927k | index[i] = j; |
1014 | 927k | used[i] = 1; |
1015 | 927k | if (i > max_enc) |
1016 | 137k | max_enc = i; |
1017 | 927k | } |
1018 | 928k | } |
1019 | 51.0k | sputc(s, (byte)(nsupp ? 0x80 : 0)); |
1020 | 51.0k | sputc(s, (byte)max_enc+1); |
1021 | | #ifdef DEBUG |
1022 | | { int num_enc_chars = pgsub->num_encoded_chars; |
1023 | | |
1024 | | if (nsupp != num_enc_chars - num_enc) |
1025 | | lprintf3("nsupp = %d, num_enc_chars = %d, num_enc = %d\n", |
1026 | | nsupp, num_enc_chars, num_enc); |
1027 | | for (j = 0; j < num_enc; ++j) |
1028 | | if (!used[j]) |
1029 | | lprintf2("glyph %d = 0x%lx not used\n", j, |
1030 | | pgsub->glyphs.subset_data[j + 1]); |
1031 | | } |
1032 | | #endif |
1033 | 51.0k | put_bytes(s, index, max_enc+1); |
1034 | 51.0k | if (nsupp) { |
1035 | | /* Write supplementary entries for multiply-encoded glyphs. */ |
1036 | 52 | sputc(s, (byte)nsupp); |
1037 | 228 | for (j = 0; j < nsupp; ++j) { |
1038 | 176 | byte chr = supplement[j]; |
1039 | | |
1040 | 176 | sputc(s, chr); |
1041 | 176 | put_card16(pcw, |
1042 | 176 | cff_glyph_sid(pcw, |
1043 | 176 | pfont->procs.encode_char((gs_font *)pfont, |
1044 | 176 | (gs_char)chr, |
1045 | 176 | GLYPH_SPACE_NAME))); |
1046 | 176 | } |
1047 | 52 | } |
1048 | 51.0k | return 0; |
1049 | 51.0k | } |
1050 | | |
1051 | | static int |
1052 | | cff_write_charset(cff_writer_t *pcw, cff_glyph_subset_t *pgsub) |
1053 | 51.0k | { |
1054 | 51.0k | int j, code; |
1055 | | |
1056 | 51.0k | sputc(pcw->strm, 0); |
1057 | 978k | for (j = 1; j < pgsub->glyphs.subset_size; j++) { |
1058 | 927k | code = cff_glyph_sid(pcw, pgsub->glyphs.subset_data[j]); |
1059 | 927k | if (code < 0) |
1060 | 0 | continue; |
1061 | 927k | put_card16(pcw, code); |
1062 | 927k | } |
1063 | 51.0k | return 0; |
1064 | 51.0k | } |
1065 | | static int |
1066 | | cff_write_cidset(cff_writer_t *pcw, psf_glyph_enum_t *penum) |
1067 | 836 | { |
1068 | 836 | gs_glyph glyph; |
1069 | 836 | int code; |
1070 | | |
1071 | 836 | sputc(pcw->strm, 0); |
1072 | 836 | psf_enumerate_glyphs_reset(penum); |
1073 | 10.1k | while ((code = psf_enumerate_glyphs_next(penum, &glyph)) == 0) { |
1074 | | /* Skip glyph 0 (the .notdef glyph), which is always first. */ |
1075 | 9.34k | if (glyph != GS_MIN_CID_GLYPH) |
1076 | 8.50k | put_card16(pcw, (uint)(glyph - GS_MIN_CID_GLYPH)); |
1077 | 9.34k | } |
1078 | 836 | return min(code, 0); |
1079 | 836 | } |
1080 | | |
1081 | | /* ------ FDSelect ------ */ |
1082 | | |
1083 | | /* Determine the size of FDSelect. */ |
1084 | | static uint |
1085 | | cff_FDSelect_size(cff_writer_t *pcw, psf_glyph_enum_t *penum, uint *pformat) |
1086 | 166 | { |
1087 | 166 | gs_font_cid0 *const pfont = (gs_font_cid0 *)pcw->pfont; |
1088 | 166 | gs_font_base *const pbfont = (gs_font_base *)pfont; |
1089 | 166 | gs_glyph glyph; |
1090 | 166 | int prev = -1; |
1091 | 166 | uint linear_size = 1, range_size = 5; |
1092 | 166 | int code; |
1093 | | |
1094 | | /* Determine whether format 0 or 3 is more efficient. */ |
1095 | 166 | psf_enumerate_glyphs_reset(penum); |
1096 | 2.02k | while ((code = psf_enumerate_glyphs_next(penum, &glyph)) == 0) { |
1097 | 1.85k | int font_index; |
1098 | | |
1099 | 1.85k | code = pfont->cidata.glyph_data(pbfont, glyph, NULL, &font_index); |
1100 | 1.85k | if (code >= 0) { |
1101 | 1.85k | if (font_index != prev) |
1102 | 236 | range_size += 3, prev = font_index; |
1103 | 1.85k | ++linear_size; |
1104 | 1.85k | } |
1105 | 1.85k | } |
1106 | 166 | if (range_size < linear_size) { |
1107 | 74 | *pformat = 3; |
1108 | 74 | return range_size; |
1109 | 92 | } else { |
1110 | 92 | *pformat = 0; |
1111 | 92 | return linear_size; |
1112 | 92 | } |
1113 | 166 | } |
1114 | | |
1115 | | /* Write FDSelect. size and format were returned by cff_FDSelect_size. */ |
1116 | | static int |
1117 | | cff_write_FDSelect(cff_writer_t *pcw, psf_glyph_enum_t *penum, uint size, |
1118 | | int format) |
1119 | 670 | { |
1120 | 670 | stream *s = pcw->strm; |
1121 | 670 | gs_font_cid0 *const pfont = (gs_font_cid0 *)pcw->pfont; |
1122 | 670 | gs_font_base *const pbfont = (gs_font_base *)pfont; |
1123 | 670 | gs_glyph glyph; |
1124 | 670 | int prev = -1; |
1125 | 670 | uint cid_count = 0; |
1126 | 670 | int code; |
1127 | | |
1128 | 670 | spputc(s, (byte)format); |
1129 | 670 | psf_enumerate_glyphs_reset(penum); |
1130 | 670 | switch (format) { |
1131 | 300 | case 3: /* ranges */ |
1132 | 300 | put_card16(pcw, (size - 5) / 3); |
1133 | 6.62k | while ((code = psf_enumerate_glyphs_next(penum, &glyph)) == 0) { |
1134 | 6.32k | int font_index; |
1135 | | |
1136 | 6.32k | code = pfont->cidata.glyph_data(pbfont, glyph, NULL, &font_index); |
1137 | 6.32k | if (code >= 0) { |
1138 | 6.32k | if (font_index != prev) { |
1139 | 520 | put_card16(pcw, cid_count); |
1140 | 520 | sputc(s, (byte)font_index); |
1141 | 520 | prev = font_index; |
1142 | 520 | } |
1143 | 6.32k | ++cid_count; |
1144 | 6.32k | } |
1145 | 6.32k | } |
1146 | 300 | put_card16(pcw, cid_count); |
1147 | 300 | break; |
1148 | 370 | case 0: /* linear table */ |
1149 | 1.52k | while ((code = psf_enumerate_glyphs_next(penum, &glyph)) == 0) { |
1150 | 1.15k | int font_index; |
1151 | | |
1152 | 1.15k | code = pfont->cidata.glyph_data(pbfont, glyph, NULL, &font_index); |
1153 | 1.15k | if (code >= 0) |
1154 | 1.15k | sputc(s, (byte)font_index); |
1155 | 1.15k | } |
1156 | 370 | break; |
1157 | 0 | default: /* not possible */ |
1158 | 0 | return_error(gs_error_rangecheck); |
1159 | 670 | } |
1160 | 670 | return 0; |
1161 | 670 | } |
1162 | | |
1163 | | /* ------ Main procedure ------ */ |
1164 | | |
1165 | | /* Write the CFF definition of a Type 1 or Type 2 font. */ |
1166 | | int |
1167 | | psf_write_type2_font(stream *s, gs_font_type1 *pfont, int options, |
1168 | | gs_glyph *subset_glyphs, uint subset_size, |
1169 | | const gs_const_string *alt_font_name, |
1170 | | gs_int_rect *FontBBox) |
1171 | 12.7k | { |
1172 | 12.7k | gs_font_base *const pbfont = (gs_font_base *)pfont; |
1173 | 12.7k | cff_writer_t writer; |
1174 | 12.7k | cff_glyph_subset_t subset; |
1175 | 12.7k | cff_string_item_t *std_string_items; |
1176 | 12.7k | cff_string_item_t *string_items; |
1177 | 12.7k | gs_const_string font_name; |
1178 | 12.7k | stream poss; |
1179 | 12.7k | uint charstrings_count, charstrings_size; |
1180 | 12.7k | uint subrs_count, subrs_size; |
1181 | 12.7k | uint gsubrs_count, gsubrs_size, encoding_size; |
1182 | 12.7k | int charset_size = -1; |
1183 | 12.7k | uint number_of_glyphs = 0, number_of_strings; |
1184 | | /* |
1185 | | * Set the offsets and sizes to the largest reasonable values |
1186 | | * (see below). |
1187 | | */ |
1188 | 12.7k | uint |
1189 | 12.7k | Top_size = 0x7fffff, |
1190 | 12.7k | GSubrs_offset, |
1191 | 12.7k | Encoding_offset, |
1192 | 12.7k | charset_offset, |
1193 | 12.7k | CharStrings_offset, |
1194 | 12.7k | Private_offset, |
1195 | 12.7k | Private_size = 0x7fffff, |
1196 | 12.7k | Subrs_offset, |
1197 | 12.7k | End_offset = 0x7fffff; |
1198 | 12.7k | int j; |
1199 | 12.7k | psf_glyph_enum_t genum; |
1200 | 12.7k | gs_glyph glyph; |
1201 | 12.7k | long start_pos; |
1202 | 12.7k | uint offset; |
1203 | 12.7k | int code; |
1204 | | |
1205 | | /* Allocate the string tables. */ |
1206 | 12.7k | psf_enumerate_glyphs_begin(&genum, (gs_font *)pfont, |
1207 | 12.7k | NULL, 0, GLYPH_SPACE_NAME); |
1208 | 258k | while ((code = psf_enumerate_glyphs_next(&genum, &glyph)) != 1) |
1209 | 245k | number_of_glyphs++; |
1210 | 12.7k | subset.glyphs.subset_data = (gs_glyph *)gs_alloc_bytes(pfont->memory, |
1211 | 12.7k | number_of_glyphs * sizeof(glyph), "psf_write_type2_font"); |
1212 | 12.7k | number_of_strings = number_of_glyphs + MAX_CFF_MISC_STRINGS; |
1213 | 12.7k | std_string_items = (cff_string_item_t *)gs_alloc_bytes(pfont->memory, |
1214 | 12.7k | (MAX_CFF_STD_STRINGS + number_of_strings) * sizeof(cff_string_item_t), |
1215 | 12.7k | "psf_write_type2_font"); |
1216 | 12.7k | if (std_string_items == NULL || subset.glyphs.subset_data == NULL) { |
1217 | 0 | code = gs_note_error(gs_error_VMerror); |
1218 | 0 | goto error; |
1219 | 0 | } |
1220 | 12.7k | string_items = std_string_items + MAX_CFF_STD_STRINGS; |
1221 | | |
1222 | | /* Get subset glyphs. */ |
1223 | 12.7k | code = psf_get_type1_glyphs(&subset.glyphs, pfont, subset_glyphs, |
1224 | 12.7k | subset_size); |
1225 | 12.7k | if (code < 0) |
1226 | 0 | goto error; |
1227 | 12.7k | if (subset.glyphs.notdef == GS_NO_GLYPH) { |
1228 | 0 | code = gs_note_error(gs_error_rangecheck); /* notdef is required */ |
1229 | 0 | goto error; |
1230 | 0 | } |
1231 | | |
1232 | | /* If we're writing Type 2 CharStrings, don't encrypt them. */ |
1233 | 12.7k | if (options & WRITE_TYPE2_CHARSTRINGS) { |
1234 | 12.7k | options |= WRITE_TYPE2_NO_LENIV; |
1235 | 12.7k | if (pfont->FontType != ft_encrypted2) |
1236 | 11.9k | pfont->data.defaultWidthX = pfont->data.nominalWidthX = 0; |
1237 | 12.7k | } |
1238 | 12.7k | writer.options = options; |
1239 | 12.7k | s_init(&poss, NULL); |
1240 | 12.7k | swrite_position_only(&poss); |
1241 | 12.7k | writer.strm = &poss; |
1242 | 12.7k | writer.pfont = pbfont; |
1243 | 12.7k | writer.glyph_data = psf_type1_glyph_data; |
1244 | 12.7k | writer.offset_size = 1; /* arbitrary */ |
1245 | 12.7k | writer.start_pos = stell(s); |
1246 | 12.7k | writer.FontBBox = *FontBBox; |
1247 | | |
1248 | | /* Initialize the enumeration of the glyphs. */ |
1249 | 12.7k | psf_enumerate_glyphs_begin(&genum, (gs_font *)pfont, |
1250 | 12.7k | subset.glyphs.subset_glyphs, |
1251 | 12.7k | (subset.glyphs.subset_glyphs ? |
1252 | 12.7k | subset.glyphs.subset_size : 0), |
1253 | 12.7k | GLYPH_SPACE_NAME); |
1254 | | |
1255 | | /* Shuffle the glyphs into the order .notdef, encoded, unencoded. */ |
1256 | 12.7k | { |
1257 | 12.7k | gs_glyph encoded[256]; |
1258 | 12.7k | int num_enc, num_enc_chars; |
1259 | | |
1260 | | /* Get the list of encoded glyphs. */ |
1261 | 3.28M | for (j = 0, num_enc_chars = 0; j < 256; ++j) { |
1262 | 3.27M | glyph = pfont->procs.encode_char((gs_font *)pfont, (gs_char)j, |
1263 | 3.27M | GLYPH_SPACE_NAME); |
1264 | 3.27M | if (glyph != GS_NO_GLYPH && glyph != subset.glyphs.notdef && |
1265 | 3.27M | (subset.glyphs.subset_glyphs == 0 || |
1266 | 232k | psf_sorted_glyphs_include(subset.glyphs.subset_data, |
1267 | 0 | subset.glyphs.subset_size, glyph))) |
1268 | 232k | encoded[num_enc_chars++] = glyph; |
1269 | 3.27M | } |
1270 | 12.7k | subset.num_encoded_chars = num_enc_chars; |
1271 | 12.7k | subset.num_encoded = num_enc = |
1272 | 12.7k | psf_sort_glyphs(encoded, num_enc_chars); |
1273 | | |
1274 | | /* Get the complete list of glyphs if we don't have it already. */ |
1275 | 12.7k | if (!subset.glyphs.subset_glyphs) { |
1276 | 12.7k | int num_glyphs = 0; |
1277 | | |
1278 | 12.7k | psf_enumerate_glyphs_reset(&genum); |
1279 | 258k | while ((code = psf_enumerate_glyphs_next(&genum, &glyph)) != 1) |
1280 | 245k | if (code == 0) { |
1281 | 245k | if (num_glyphs == number_of_glyphs){ |
1282 | 0 | code = gs_note_error(gs_error_limitcheck); |
1283 | 0 | goto error; |
1284 | 0 | } |
1285 | 245k | subset.glyphs.subset_data[num_glyphs++] = glyph; |
1286 | 245k | } |
1287 | 12.7k | subset.glyphs.subset_size = |
1288 | 12.7k | psf_sort_glyphs(subset.glyphs.subset_data, num_glyphs); |
1289 | 12.7k | subset.glyphs.subset_glyphs = subset.glyphs.subset_data; |
1290 | 12.7k | } |
1291 | | |
1292 | | /* Move the unencoded glyphs to the top of the list. */ |
1293 | | /* |
1294 | | * We could do this in time N rather than N log N with a two-finger |
1295 | | * algorithm, but it doesn't seem worth the trouble right now. |
1296 | | */ |
1297 | 12.7k | { |
1298 | 12.7k | int from = subset.glyphs.subset_size; |
1299 | 12.7k | int to = from; |
1300 | | |
1301 | 258k | while (from > 0) { |
1302 | 245k | glyph = subset.glyphs.subset_data[--from]; |
1303 | 245k | if (glyph != subset.glyphs.notdef && |
1304 | 245k | !psf_sorted_glyphs_include(encoded, num_enc, glyph)) |
1305 | 10 | subset.glyphs.subset_data[--to] = glyph; |
1306 | 245k | } |
1307 | | #ifdef DEBUG |
1308 | | if (to != num_enc + 1) |
1309 | | lprintf2("to = %d, num_enc + 1 = %d\n", to, num_enc + 1); |
1310 | | #endif |
1311 | 12.7k | } |
1312 | | |
1313 | | /* Move .notdef and the encoded glyphs to the bottom of the list. */ |
1314 | 12.7k | subset.glyphs.subset_data[0] = subset.glyphs.notdef; |
1315 | 12.7k | memcpy(subset.glyphs.subset_data + 1, encoded, |
1316 | 12.7k | sizeof(encoded[0]) * num_enc); |
1317 | 12.7k | } |
1318 | | |
1319 | | /* Set the font name. */ |
1320 | 12.7k | if (alt_font_name) |
1321 | 12.7k | font_name = *alt_font_name; |
1322 | 0 | else |
1323 | 0 | font_name.data = pfont->font_name.chars, |
1324 | 0 | font_name.size = pfont->font_name.size; |
1325 | | |
1326 | | /* Initialize the string tables. */ |
1327 | 12.7k | cff_string_table_init(&writer.std_strings, std_string_items, |
1328 | 12.7k | MAX_CFF_STD_STRINGS); |
1329 | 5.01M | for (j = 0; (glyph = gs_c_known_encode((gs_char)j, |
1330 | 5.01M | ENCODING_INDEX_CFFSTRINGS)) != GS_NO_GLYPH; |
1331 | 5.00M | ++j) { |
1332 | 5.00M | gs_const_string str; |
1333 | 5.00M | int ignore; |
1334 | | |
1335 | 5.00M | gs_c_glyph_name(glyph, &str); |
1336 | 5.00M | cff_string_index(&writer.std_strings, str.data, str.size, true, |
1337 | 5.00M | &ignore); |
1338 | 5.00M | } |
1339 | 12.7k | cff_string_table_init(&writer.strings, string_items, number_of_strings); |
1340 | | |
1341 | | /* Enter miscellaneous strings in the string table. */ |
1342 | 12.7k | cff_write_Top_font(&writer, 0, 0, 0, 0, 0); |
1343 | | |
1344 | | /* Enter the glyph names in the string table. */ |
1345 | | /* (Note that we have changed the glyph list.) */ |
1346 | 12.7k | psf_enumerate_glyphs_begin(&genum, (gs_font *)pfont, |
1347 | 12.7k | subset.glyphs.subset_data, |
1348 | 12.7k | subset.glyphs.subset_size, |
1349 | 12.7k | GLYPH_SPACE_NAME); |
1350 | 258k | while ((code = psf_enumerate_glyphs_next(&genum, &glyph)) != 1) |
1351 | 245k | if (code == 0) { |
1352 | 245k | code = cff_glyph_sid(&writer, glyph); |
1353 | 245k | if (code == gs_error_undefined) |
1354 | 0 | continue; |
1355 | 245k | if (code < 0) |
1356 | 0 | goto error; |
1357 | 245k | charset_size += 2; |
1358 | 245k | } |
1359 | | |
1360 | | /* |
1361 | | * The CFF specification says that the Encoding, charset, CharStrings, |
1362 | | * Private, and Local Subr sections may be in any order. To minimize |
1363 | | * the risk of incompatibility with Adobe software, we produce them in |
1364 | | * the order just mentioned. |
1365 | | */ |
1366 | | |
1367 | | /* |
1368 | | * Compute the size of the GSubrs Index, if not omitted. |
1369 | | */ |
1370 | 12.7k | if ((options & WRITE_TYPE2_NO_GSUBRS) != 0 || |
1371 | 12.7k | cff_convert_charstrings(&writer, pbfont) /* we expand all Subrs */ |
1372 | 12.7k | ) |
1373 | 11.9k | gsubrs_count = 0, gsubrs_size = 0; |
1374 | 840 | else |
1375 | 840 | gsubrs_size = cff_write_Subrs_offsets(&writer, &gsubrs_count, |
1376 | 840 | pfont, true); |
1377 | | |
1378 | | /* |
1379 | | * Compute the size of the Encoding. For simplicity, we currently |
1380 | | * always store the Encoding explicitly. Note that because CFF stores |
1381 | | * the Encoding in an "inverted" form, we need to count the number of |
1382 | | * glyphs that occur at more than one place in the Encoding. |
1383 | | */ |
1384 | 12.7k | encoding_size = cff_Encoding_size(&writer, &subset); |
1385 | | |
1386 | | /* Compute the size of the CharStrings Index. */ |
1387 | 12.7k | code = cff_write_CharStrings_offsets(&writer, &genum, &charstrings_count); |
1388 | 12.7k | if (code < 0) |
1389 | 86 | goto error; |
1390 | 12.7k | charstrings_size = (uint)code; |
1391 | | |
1392 | | /* Compute the size of the (local) Subrs Index. */ |
1393 | 12.7k | #ifdef SKIP_EMPTY_SUBRS |
1394 | 12.7k | subrs_size = |
1395 | 12.7k | (cff_convert_charstrings(&writer, pbfont) ? 0 : |
1396 | 12.7k | cff_write_Subrs_offsets(&writer, &subrs_count, pfont, false)); |
1397 | | #else |
1398 | | if (cff_convert_charstrings(&writer, pbfont)) |
1399 | | subrs_count = 0; /* we expand all Subrs */ |
1400 | | subrs_size = cff_write_Subrs_offsets(&writer, &subrs_count, pfont, false); |
1401 | | #endif |
1402 | | |
1403 | | /* |
1404 | | * The offsets of the Private Dict and the CharStrings Index |
1405 | | * depend on the size of the Top Dict; the offset of the Subrs also |
1406 | | * depends on the size of the Private Dict. However, the size of the |
1407 | | * Top Dict depends on the offsets of the CharStrings Index, the |
1408 | | * charset, and the Encoding, and on the offset and size of the Private |
1409 | | * Dict, because of the variable-length encoding of the offsets and |
1410 | | * size; for the same reason, the size of the Private Dict depends on |
1411 | | * the offset of the Subrs. Fortunately, the relationship between the |
1412 | | * value of an offset or size and the size of its encoding is monotonic. |
1413 | | * Therefore, we start by assuming the largest reasonable value for all |
1414 | | * the sizes and iterate until everything converges. |
1415 | | */ |
1416 | 38.3k | iter: |
1417 | 38.3k | swrite_position_only(&poss); |
1418 | 38.3k | writer.strm = &poss; |
1419 | | |
1420 | | /* Compute the offsets. */ |
1421 | 38.3k | GSubrs_offset = 4 + cff_Index_size(1, font_name.size) + |
1422 | 38.3k | cff_Index_size(1, Top_size) + |
1423 | 38.3k | cff_Index_size(writer.strings.count, writer.strings.total); |
1424 | 38.3k | Encoding_offset = GSubrs_offset + |
1425 | 38.3k | cff_Index_size(gsubrs_count, gsubrs_size); |
1426 | 38.3k | charset_offset = Encoding_offset + encoding_size; |
1427 | 38.3k | CharStrings_offset = charset_offset + charset_size; |
1428 | 38.3k | Private_offset = CharStrings_offset + |
1429 | 38.3k | cff_Index_size(charstrings_count, charstrings_size); |
1430 | 38.3k | Subrs_offset = Private_size; /* relative to Private Dict */ |
1431 | | |
1432 | 51.0k | write: |
1433 | 51.0k | if(check_ioerror(writer.strm)) { |
1434 | 0 | code = gs_note_error(gs_error_ioerror); |
1435 | 0 | goto error; |
1436 | 0 | } |
1437 | 51.0k | start_pos = stell(writer.strm); |
1438 | | /* Write the header, setting offset_size. */ |
1439 | 51.0k | cff_write_header(&writer, End_offset); |
1440 | | |
1441 | | /* Write the names Index. */ |
1442 | 51.0k | cff_put_Index_header(&writer, 1, font_name.size); |
1443 | 51.0k | put_offset(&writer, font_name.size + 1); |
1444 | 51.0k | put_bytes(writer.strm, font_name.data, font_name.size); |
1445 | | |
1446 | | /* Write the Top Index. */ |
1447 | 51.0k | cff_put_Index_header(&writer, 1, Top_size); |
1448 | 51.0k | put_offset(&writer, Top_size + 1); |
1449 | 51.0k | offset = stell(writer.strm) - start_pos; |
1450 | 51.0k | cff_write_Top_font(&writer, Encoding_offset, charset_offset, |
1451 | 51.0k | CharStrings_offset, |
1452 | 51.0k | Private_offset, Private_size); |
1453 | 51.0k | Top_size = stell(writer.strm) - start_pos - offset; |
1454 | | |
1455 | | /* Write the strings Index. */ |
1456 | 51.0k | cff_put_Index(&writer, &writer.strings); |
1457 | 51.0k | if(check_ioerror(writer.strm)){ |
1458 | 0 | code = gs_note_error(gs_error_ioerror); |
1459 | 0 | goto error; |
1460 | 0 | } |
1461 | | |
1462 | | /* Write the GSubrs Index, if any, checking the offset. */ |
1463 | 51.0k | offset = stell(writer.strm) - start_pos; |
1464 | 51.0k | if_debug2m('l', s->memory, "[l]GSubrs = %u => %u\n", GSubrs_offset, offset); |
1465 | 51.0k | if (offset > GSubrs_offset) { |
1466 | 0 | code = gs_note_error(gs_error_rangecheck); |
1467 | 0 | goto error; |
1468 | 0 | } |
1469 | 51.0k | GSubrs_offset = offset; |
1470 | 51.0k | if (gsubrs_count == 0 || cff_convert_charstrings(&writer, pbfont)) |
1471 | 50.9k | cff_put_Index_header(&writer, 0, 0); |
1472 | 76 | else |
1473 | 76 | cff_write_Subrs(&writer, gsubrs_count, gsubrs_size, pfont, true); |
1474 | | |
1475 | | /* Write the Encoding. */ |
1476 | 51.0k | cff_write_Encoding(&writer, &subset); |
1477 | | |
1478 | | /* Write the charset. */ |
1479 | 51.0k | cff_write_charset(&writer, &subset); |
1480 | | |
1481 | | /* Write the CharStrings Index, checking the offset. */ |
1482 | 51.0k | offset = stell(writer.strm) - start_pos; |
1483 | 51.0k | if (offset > CharStrings_offset) { |
1484 | 0 | code = gs_note_error(gs_error_rangecheck); |
1485 | 0 | goto error; |
1486 | 0 | } |
1487 | 51.0k | CharStrings_offset = offset; |
1488 | 51.0k | cff_write_CharStrings(&writer, &genum, charstrings_count, |
1489 | 51.0k | charstrings_size); |
1490 | 51.0k | if(check_ioerror(writer.strm)) { |
1491 | 0 | code = gs_note_error(gs_error_ioerror); |
1492 | 0 | goto error; |
1493 | 0 | } |
1494 | | |
1495 | | /* Write the Private Dict, checking the offset. */ |
1496 | 51.0k | offset = stell(writer.strm) - start_pos; |
1497 | 51.0k | if (offset > Private_offset) { |
1498 | 0 | code = gs_note_error(gs_error_rangecheck); |
1499 | 0 | goto error; |
1500 | 0 | } |
1501 | 51.0k | Private_offset = offset; |
1502 | 51.0k | cff_write_Private(&writer, (subrs_size == 0 ? 0 : Subrs_offset), pfont); |
1503 | 51.0k | Private_size = stell(writer.strm) - start_pos - offset; |
1504 | | |
1505 | | /* Write the Subrs Index, checking the offset. */ |
1506 | 51.0k | offset = stell(writer.strm) - (start_pos + Private_offset); |
1507 | 51.0k | if (offset > Subrs_offset) { |
1508 | 0 | code = gs_note_error(gs_error_rangecheck); |
1509 | 0 | goto error; |
1510 | 0 | } |
1511 | 51.0k | Subrs_offset = offset; |
1512 | 51.0k | if (cff_convert_charstrings(&writer, pbfont)) |
1513 | 47.5k | cff_put_Index_header(&writer, 0, 0); |
1514 | 3.43k | else if (subrs_size != 0) |
1515 | 64 | cff_write_Subrs(&writer, subrs_count, subrs_size, pfont, false); |
1516 | | |
1517 | | /* Check the final offset. */ |
1518 | 51.0k | if(check_ioerror(writer.strm)) { |
1519 | 0 | code = gs_note_error(gs_error_ioerror); |
1520 | 0 | goto error; |
1521 | 0 | } |
1522 | 51.0k | offset = stell(writer.strm) - start_pos; |
1523 | 51.0k | if (offset > End_offset) { |
1524 | 0 | code = gs_note_error(gs_error_rangecheck); |
1525 | 0 | goto error; |
1526 | 0 | } |
1527 | 51.0k | if (offset == End_offset) { |
1528 | | /* The iteration has converged. Write the result. */ |
1529 | 25.4k | if (writer.strm == &poss) { |
1530 | 12.7k | writer.strm = s; |
1531 | 12.7k | goto write; |
1532 | 12.7k | } |
1533 | 25.5k | } else { |
1534 | | /* No convergence yet. */ |
1535 | 25.5k | End_offset = offset; |
1536 | 25.5k | goto iter; |
1537 | 25.5k | } |
1538 | | |
1539 | | /* All done. */ |
1540 | 12.7k | gs_free_object(pfont->memory, std_string_items, "psf_write_type2_font"); |
1541 | 12.7k | gs_free_object(pfont->memory, subset.glyphs.subset_data, "psf_write_type2_font"); |
1542 | 12.7k | return 0; |
1543 | | |
1544 | 86 | error: |
1545 | 86 | gs_free_object(pfont->memory, std_string_items, "psf_write_type2_font"); |
1546 | 86 | gs_free_object(pfont->memory, subset.glyphs.subset_data, "psf_write_type2_font"); |
1547 | 86 | subset.glyphs.subset_data = NULL; |
1548 | 86 | return code; |
1549 | 51.0k | } |
1550 | | |
1551 | | /* Write the CFF definition of a CIDFontType 0 font (CIDFont). */ |
1552 | | static int |
1553 | | cid0_glyph_data(gs_font_base *pbfont, gs_glyph glyph, gs_glyph_data_t *pgd, |
1554 | | gs_font_type1 **ppfont) |
1555 | 18.6k | { |
1556 | 18.6k | gs_font_cid0 *const pfont = (gs_font_cid0 *)pbfont; |
1557 | 18.6k | int font_index; |
1558 | 18.6k | int code = pfont->cidata.glyph_data(pbfont, glyph, pgd, &font_index); |
1559 | | |
1560 | 18.6k | if (code >= 0) |
1561 | 18.6k | *ppfont = pfont->cidata.FDArray[font_index]; |
1562 | 18.6k | return code; |
1563 | 18.6k | } |
1564 | | #ifdef DEBUG |
1565 | | static int |
1566 | | offset_error(const char *msg) |
1567 | | { |
1568 | | if_debug1('l', "[l]%s offset error\n", msg); |
1569 | | return_error(gs_error_rangecheck); |
1570 | | } |
1571 | | #else |
1572 | | # define offset_error(msg) gs_error_rangecheck |
1573 | | #endif |
1574 | | int |
1575 | | psf_write_cid0_font(stream *s, gs_font_cid0 *pfont, int options, |
1576 | | const byte *subset_cids, uint subset_size, |
1577 | | const gs_const_string *alt_font_name) |
1578 | 166 | { |
1579 | | /* |
1580 | | * CIDFontType 0 fonts differ from ordinary Type 1 / Type 2 fonts |
1581 | | * as follows: |
1582 | | * The TOP Dict starts with a ROS operator. |
1583 | | * The TOP Dict must include FDArray and FDSelect operators. |
1584 | | * The TOP Dict may include CIDFontVersion, CIDFontRevision, |
1585 | | * CIDFontType, CIDCount, and UIDBase operators. |
1586 | | * The TOP Dict must not include an Encoding operator. |
1587 | | * The charset is defined in terms of CIDs rather than SIDs. |
1588 | | * FDArray references a Font Index in which each element is a Dict |
1589 | | * defining a font without charset, Encoding, or CharStrings. |
1590 | | * FDSelect references a structure mapping CIDs to font numbers. |
1591 | | */ |
1592 | 166 | gs_font_base *const pbfont = (gs_font_base *)pfont; |
1593 | 166 | cff_writer_t writer; |
1594 | 166 | cff_string_item_t std_string_items[500]; /* 391 entries used */ |
1595 | | /****** HOW TO DETERMINE THE SIZE OF STRINGS? ******/ |
1596 | 166 | cff_string_item_t string_items[500 /* character names */ + |
1597 | 166 | 40 /* misc. values */]; |
1598 | 166 | gs_const_string font_name; |
1599 | 166 | stream poss; |
1600 | 166 | uint charstrings_count, charstrings_size; |
1601 | 166 | uint gsubrs_count, gsubrs_size; |
1602 | 166 | uint charset_size, fdselect_size, fdselect_format; |
1603 | 166 | uint subrs_count[256], subrs_size[256]; |
1604 | | /* |
1605 | | * Set the offsets and sizes to the largest reasonable values |
1606 | | * (see below). |
1607 | | */ |
1608 | 166 | uint |
1609 | 166 | Top_size = 0x7fffff, |
1610 | 166 | GSubrs_offset = 0x1ffffff, |
1611 | 166 | charset_offset = 0x1ffffff, |
1612 | 166 | FDSelect_offset = 0x1ffffff, |
1613 | 166 | CharStrings_offset = 0x1ffffff, |
1614 | 166 | Font_offset = 0x1ffffff, |
1615 | 166 | FDArray_offsets[257], |
1616 | 166 | Private_offsets[257], |
1617 | 166 | Subrs_offsets[257], |
1618 | 166 | End_offset = 0x1ffffff; |
1619 | 166 | int j; |
1620 | 166 | psf_glyph_enum_t genum; |
1621 | 166 | gs_font_info_t info; |
1622 | 166 | long start_pos; |
1623 | 166 | uint offset; |
1624 | 166 | int num_fonts = pfont->cidata.FDArray_size; |
1625 | 166 | int code; |
1626 | | |
1627 | 166 | if (num_fonts > 256) |
1628 | 0 | return_error(gs_error_invalidfont); |
1629 | | |
1630 | 166 | memset(&subrs_count, 0x00, 256 * sizeof(uint)); |
1631 | 166 | memset(&subrs_size, 0x00, 256 * sizeof(uint)); |
1632 | | |
1633 | | /* Initialize the enumeration of the glyphs. */ |
1634 | 166 | psf_enumerate_cids_begin(&genum, (gs_font *)pfont, subset_cids, |
1635 | 166 | subset_size); |
1636 | | |
1637 | | /* Check that the font can be written. */ |
1638 | 166 | code = psf_check_outline_glyphs((gs_font_base *)pfont, &genum, |
1639 | 166 | cid0_glyph_data); |
1640 | 166 | if (code < 0) |
1641 | 0 | return code; |
1642 | | /* The .notdef glyph (glyph 0) must be included. */ |
1643 | 166 | if (subset_cids && subset_size > 0 && !(subset_cids[0] & 0x80)) |
1644 | 0 | return_error(gs_error_rangecheck); |
1645 | | |
1646 | 166 | writer.options = options; |
1647 | 166 | s_init(&poss, NULL); |
1648 | 166 | swrite_position_only(&poss); |
1649 | 166 | writer.strm = &poss; |
1650 | 166 | writer.pfont = pbfont; |
1651 | 166 | writer.glyph_data = cid0_glyph_data; |
1652 | 166 | writer.offset_size = 1; /* arbitrary */ |
1653 | 166 | writer.start_pos = stell(s); |
1654 | 166 | writer.FontBBox.p.x = writer.FontBBox.p.y = 0; |
1655 | 166 | writer.FontBBox.q.x = writer.FontBBox.q.y = 0; |
1656 | | |
1657 | | /* Set the font name. */ |
1658 | 166 | if (alt_font_name) |
1659 | 166 | font_name = *alt_font_name; |
1660 | 0 | else if (pfont->font_name.size) |
1661 | 0 | font_name.data = pfont->font_name.chars, |
1662 | 0 | font_name.size = pfont->font_name.size; |
1663 | 0 | else |
1664 | 0 | font_name.data = pfont->key_name.chars, |
1665 | 0 | font_name.size = pfont->key_name.size; |
1666 | | |
1667 | | /* Initialize the string tables. */ |
1668 | 166 | cff_string_table_init(&writer.std_strings, std_string_items, |
1669 | 166 | countof(std_string_items)); |
1670 | 166 | cff_string_table_init(&writer.strings, string_items, |
1671 | 166 | countof(string_items)); |
1672 | | |
1673 | | /* Make all entries in the string table. */ |
1674 | 166 | cff_write_ROS(&writer, &pfont->cidata.common.CIDSystemInfo); |
1675 | 444 | for (j = 0; j < num_fonts; ++j) { |
1676 | 278 | gs_font_type1 *pfd = pfont->cidata.FDArray[j]; |
1677 | | |
1678 | 278 | cff_write_Top_fdarray(&writer, (gs_font_base *)pfd, 0, 0); |
1679 | 278 | } |
1680 | | |
1681 | | /* |
1682 | | * The CFF specification says that sections after the initial Indexes |
1683 | | * may be in any order. To minimize the risk of incompatibility with |
1684 | | * Adobe software, we produce them in the order illustrated in the |
1685 | | * specification. |
1686 | | */ |
1687 | | |
1688 | | /* Initialize the offset arrays. */ |
1689 | 610 | for (j = 0; j <= num_fonts; ++j) |
1690 | 444 | FDArray_offsets[j] = Private_offsets[j] = Subrs_offsets[j] = |
1691 | 444 | 0x7effffff / num_fonts * j + 0x1000000; |
1692 | | |
1693 | | /* |
1694 | | * Compute the size of the GSubrs Index, if not omitted. |
1695 | | * Arbitrarily use FDArray[0] to access the GSubrs and to determine |
1696 | | * the CharString type. |
1697 | | */ |
1698 | 166 | if ((options & WRITE_TYPE2_NO_GSUBRS) != 0 || |
1699 | 166 | cff_convert_charstrings(&writer, |
1700 | 166 | (const gs_font_base *)pfont->cidata.FDArray[0]) |
1701 | | /* we expand all Subrs */ |
1702 | 166 | ) |
1703 | 0 | gsubrs_count = 0, gsubrs_size = 0; |
1704 | 166 | else |
1705 | 166 | gsubrs_size = cff_write_Subrs_offsets(&writer, &gsubrs_count, |
1706 | 166 | pfont->cidata.FDArray[0], true); |
1707 | | |
1708 | | /* |
1709 | | * Compute the size of the charset. For simplicity, we currently |
1710 | | * always store the charset explicitly. |
1711 | | */ |
1712 | 166 | swrite_position_only(&poss); |
1713 | 166 | cff_write_cidset(&writer, &genum); |
1714 | 166 | charset_size = stell(&poss); |
1715 | | |
1716 | | /* Compute the size of the FDSelect strucure. */ |
1717 | 166 | fdselect_size = cff_FDSelect_size(&writer, &genum, &fdselect_format); |
1718 | | |
1719 | | /* Compute the size of the CharStrings Index. */ |
1720 | | /* Compute the size of the CharStrings Index. */ |
1721 | 166 | code = cff_write_CharStrings_offsets(&writer, &genum, &charstrings_count); |
1722 | 166 | if (code < 0) |
1723 | 0 | return code; |
1724 | 166 | charstrings_size = (uint)code; |
1725 | | |
1726 | | /* Compute the size of the (local) Subrs Indexes. */ |
1727 | 444 | for (j = 0; j < num_fonts; ++j) { |
1728 | 278 | gs_font_type1 *pfd = pfont->cidata.FDArray[j]; |
1729 | | |
1730 | 278 | #ifdef SKIP_EMPTY_SUBRS |
1731 | 278 | subrs_size[j] = |
1732 | 278 | (cff_convert_charstrings(&writer, (gs_font_base *)pfd) ? 0 : |
1733 | 278 | cff_write_Subrs_offsets(&writer, &subrs_count[j], pfd, false)); |
1734 | | #else |
1735 | | if (cff_convert_charstrings(&writer, (gs_font_base *)pfd)) |
1736 | | subrs_count[j] = 0; /* we expand all Subrs */ |
1737 | | subrs_size[j] = cff_write_Subrs_offsets(&writer, &subrs_count[j], pfd, false); |
1738 | | #endif |
1739 | 278 | } |
1740 | | |
1741 | | /* Get the font_info once, since it may be expensive. */ |
1742 | 166 | cff_get_Top_info_common(&writer, (gs_font_base *)pfont, true, &info); |
1743 | | |
1744 | | /* |
1745 | | * The offsets of the Private Dict and the CharStrings Index |
1746 | | * depend on the size of the Top Dict; the offset of the Subrs also |
1747 | | * depends on the size of the Private Dict. However, the size of the |
1748 | | * Top Dict depends on the offsets of the CharStrings Index and the |
1749 | | * charset, and on the offset and size of the Private Dict, |
1750 | | * because of the variable-length encoding of the offsets and |
1751 | | * size; for the same reason, the size of the Private Dict depends on |
1752 | | * the offset of the Subrs. Fortunately, the relationship between the |
1753 | | * value of an offset or size and the size of its encoding is monotonic. |
1754 | | * Therefore, we start by assuming the largest reasonable value for all |
1755 | | * the sizes and iterate until everything converges. |
1756 | | */ |
1757 | 504 | iter: |
1758 | 504 | swrite_position_only(&poss); |
1759 | 504 | writer.strm = &poss; |
1760 | | |
1761 | | /* Compute the offsets. */ |
1762 | 504 | GSubrs_offset = 4 + cff_Index_size(1, font_name.size) + |
1763 | 504 | cff_Index_size(1, Top_size) + |
1764 | 504 | cff_Index_size(writer.strings.count, writer.strings.total); |
1765 | 504 | charset_offset = GSubrs_offset + |
1766 | 504 | cff_Index_size(gsubrs_count, gsubrs_size); |
1767 | 504 | FDSelect_offset = charset_offset + charset_size; |
1768 | 504 | CharStrings_offset = FDSelect_offset + fdselect_size; |
1769 | 504 | if_debug4m('l', s->memory, |
1770 | 504 | "[l]GSubrs at %u, charset at %u, FDSelect at %u, CharStrings at %u\n", |
1771 | 504 | GSubrs_offset, charset_offset, FDSelect_offset, CharStrings_offset); |
1772 | | |
1773 | 670 | write: |
1774 | 670 | start_pos = stell(writer.strm); |
1775 | 670 | if_debug1m('l', s->memory, "[l]start_pos = %ld\n", start_pos); |
1776 | | /* Write the header, setting offset_size. */ |
1777 | 670 | cff_write_header(&writer, End_offset); |
1778 | | |
1779 | | /* Write the names Index. */ |
1780 | 670 | cff_put_Index_header(&writer, 1, font_name.size); |
1781 | 670 | put_offset(&writer, font_name.size + 1); |
1782 | 670 | put_bytes(writer.strm, font_name.data, font_name.size); |
1783 | | |
1784 | | /* Write the Top Index. */ |
1785 | 670 | cff_put_Index_header(&writer, 1, Top_size); |
1786 | 670 | put_offset(&writer, Top_size + 1); |
1787 | 670 | offset = stell(writer.strm) - start_pos; |
1788 | 670 | cff_write_Top_cidfont(&writer, charset_offset, CharStrings_offset, |
1789 | 670 | FDSelect_offset, Font_offset, &info); |
1790 | 670 | Top_size = stell(writer.strm) - start_pos - offset; |
1791 | 670 | if_debug1m('l', s->memory, "[l]Top_size = %u\n", Top_size); |
1792 | | |
1793 | | /* Write the strings Index. */ |
1794 | 670 | cff_put_Index(&writer, &writer.strings); |
1795 | | |
1796 | | /* Write the GSubrs Index, if any, checking the offset. */ |
1797 | 670 | offset = stell(writer.strm) - start_pos; |
1798 | 670 | if_debug2m('l', s->memory, "[l]GSubrs = %u => %u\n", GSubrs_offset, offset); |
1799 | 670 | if (offset > GSubrs_offset) |
1800 | 0 | return_error(gs_error_rangecheck); |
1801 | 670 | GSubrs_offset = offset; |
1802 | 670 | if (gsubrs_count == 0 || |
1803 | 670 | cff_convert_charstrings(&writer, |
1804 | 44 | (const gs_font_base *)pfont->cidata.FDArray[0]) |
1805 | 670 | ) |
1806 | 626 | cff_put_Index_header(&writer, 0, 0); |
1807 | 44 | else |
1808 | 44 | cff_write_Subrs(&writer, gsubrs_count, gsubrs_size, |
1809 | 44 | pfont->cidata.FDArray[0], true); |
1810 | | |
1811 | | /* Write the charset. */ |
1812 | 670 | if_debug1m('l', s->memory, "[l]charset = %"PRId64"\n", (int64_t)(stell(writer.strm) - start_pos)); |
1813 | 670 | cff_write_cidset(&writer, &genum); |
1814 | | |
1815 | | /* Write the FDSelect structure, checking the offset. */ |
1816 | 670 | offset = stell(writer.strm) - start_pos; |
1817 | 670 | if_debug2m('l', s->memory, "[l]FDSelect = %u => %u\n", FDSelect_offset, offset); |
1818 | 670 | if (offset > FDSelect_offset) |
1819 | 0 | return_error(offset_error("FDselect")); |
1820 | 670 | FDSelect_offset = offset; |
1821 | 670 | cff_write_FDSelect(&writer, &genum, fdselect_size, fdselect_format); |
1822 | | |
1823 | | /* Write the CharStrings Index, checking the offset. */ |
1824 | 670 | offset = stell(writer.strm) - start_pos; |
1825 | 670 | if_debug2m('l', s->memory, "[l]CharStrings = %u => %u\n", CharStrings_offset, offset); |
1826 | 670 | if (offset > CharStrings_offset) |
1827 | 0 | return_error(offset_error("CharStrings")); |
1828 | 670 | CharStrings_offset = offset; |
1829 | 670 | cff_write_CharStrings(&writer, &genum, charstrings_count, |
1830 | 670 | charstrings_size); |
1831 | | |
1832 | | /* Write the Font Dict Index. */ |
1833 | 670 | offset = stell(writer.strm) - start_pos; |
1834 | 670 | if_debug2m('l', s->memory, "[l]Font = %u => %u\n", Font_offset, offset); |
1835 | 670 | if (offset > Font_offset) |
1836 | 0 | return_error(offset_error("Font")); |
1837 | 670 | Font_offset = offset; |
1838 | 670 | cff_write_FDArray_offsets(&writer, FDArray_offsets, num_fonts); |
1839 | 670 | offset = stell(writer.strm) - start_pos; |
1840 | 670 | if_debug2m('l', s->memory, "[l]FDArray[0] = %u => %u\n", FDArray_offsets[0], offset); |
1841 | 670 | if (offset > FDArray_offsets[0]) |
1842 | 0 | return_error(offset_error("FDArray[0]")); |
1843 | 670 | FDArray_offsets[0] = offset; |
1844 | 1.79k | for (j = 0; j < num_fonts; ++j) { |
1845 | 1.12k | gs_font_type1 *pfd = pfont->cidata.FDArray[j]; |
1846 | | |
1847 | | /* If we're writing Type 2 CharStrings, don't encrypt them. */ |
1848 | 1.12k | if (options & WRITE_TYPE2_CHARSTRINGS) { |
1849 | 1.12k | options |= WRITE_TYPE2_NO_LENIV; |
1850 | 1.12k | if (pfd->FontType != ft_encrypted2) |
1851 | 0 | pfd->data.defaultWidthX = pfd->data.nominalWidthX = 0; |
1852 | 1.12k | } |
1853 | 1.12k | cff_write_Top_fdarray(&writer, (gs_font_base *)pfd, Private_offsets[j], |
1854 | 1.12k | Private_offsets[j + 1] - Private_offsets[j]); |
1855 | 1.12k | offset = stell(writer.strm) - start_pos; |
1856 | 1.12k | if_debug3m('l', s->memory, "[l]FDArray[%d] = %u => %u\n", j + 1, |
1857 | 1.12k | FDArray_offsets[j + 1], offset); |
1858 | 1.12k | if (offset > FDArray_offsets[j + 1]) |
1859 | 0 | return_error(offset_error("FDArray")); |
1860 | 1.12k | FDArray_offsets[j + 1] = offset; |
1861 | 1.12k | } |
1862 | | |
1863 | | /* Write the Private Dicts, checking the offset. */ |
1864 | 1.79k | for (j = 0; ; ++j) { |
1865 | 1.79k | gs_font_type1 *pfd; |
1866 | | |
1867 | 1.79k | offset = stell(writer.strm) - start_pos; |
1868 | 1.79k | if_debug3m('l', s->memory, "[l]Private[%d] = %u => %u\n", |
1869 | 1.79k | j, Private_offsets[j], offset); |
1870 | 1.79k | if (offset > Private_offsets[j]) |
1871 | 0 | return_error(offset_error("Private")); |
1872 | 1.79k | Private_offsets[j] = offset; |
1873 | 1.79k | if (j == num_fonts) |
1874 | 670 | break; |
1875 | 1.12k | pfd = pfont->cidata.FDArray[j]; |
1876 | 1.12k | cff_write_Private(&writer, |
1877 | 1.12k | (subrs_size[j] == 0 ? 0 : Subrs_offsets[j]), pfd); |
1878 | 1.12k | } |
1879 | | |
1880 | | /* Write the Subrs Indexes, checking the offsets. */ |
1881 | 1.79k | for (j = 0; ; ++j) { |
1882 | 1.79k | gs_font_type1 *pfd; |
1883 | | |
1884 | 1.79k | offset = stell(writer.strm) - (start_pos + Private_offsets[j]); |
1885 | 1.79k | if_debug3m('l', s->memory, "[l]Subrs[%d] = %u => %u\n", |
1886 | 1.79k | j, Subrs_offsets[j], offset); |
1887 | 1.79k | if (offset > Subrs_offsets[j]) |
1888 | 0 | return_error(offset_error("Subrs")); |
1889 | 1.79k | Subrs_offsets[j] = offset; |
1890 | 1.79k | if (j == num_fonts) |
1891 | 670 | break; |
1892 | 1.12k | pfd = pfont->cidata.FDArray[j]; |
1893 | 1.12k | if (cff_convert_charstrings(&writer, (gs_font_base *)pfd)) |
1894 | 0 | cff_put_Index_header(&writer, 0, 0); |
1895 | 1.12k | else if (subrs_size[j] != 0) |
1896 | 72 | cff_write_Subrs(&writer, subrs_count[j], subrs_size[j], pfd, false); |
1897 | 1.12k | } |
1898 | | |
1899 | | /* Check the final offset. */ |
1900 | 670 | offset = stell(writer.strm) - start_pos; |
1901 | 670 | if_debug2m('l', s->memory, "[l]End = %u => %u\n", End_offset, offset); |
1902 | 670 | if (offset > End_offset) |
1903 | 0 | return_error(offset_error("End")); |
1904 | 670 | if (offset == End_offset) { |
1905 | | /* The iteration has converged. Write the result. */ |
1906 | 332 | if (writer.strm == &poss) { |
1907 | 166 | writer.strm = s; |
1908 | 166 | goto write; |
1909 | 166 | } |
1910 | 338 | } else { |
1911 | | /* No convergence yet. */ |
1912 | 338 | End_offset = offset; |
1913 | 338 | goto iter; |
1914 | 338 | } |
1915 | | |
1916 | | /* All done. */ |
1917 | 166 | return 0; |
1918 | 670 | } |