/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 | 110k | #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 | 10.6k | { |
85 | 10.6k | int reprobe = 17; |
86 | | |
87 | 10.6k | memset(items, 0, size * sizeof(*items)); |
88 | 10.6k | pcst->items = items; |
89 | 10.6k | pcst->count = 0; |
90 | 10.6k | pcst->size = size; |
91 | 10.9k | while (reprobe != 1 && igcd(size, reprobe) != 1) |
92 | 322 | reprobe = (reprobe * 2 + 1) % size; |
93 | 10.6k | pcst->total = 0; |
94 | 10.6k | pcst->reprobe = reprobe; |
95 | 10.6k | } |
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 | 2.09M | { |
101 | 2.09M | int index; |
102 | | |
103 | 2.09M | if (pcst->count >= pcst->size) |
104 | 0 | return_error(gs_error_limitcheck); |
105 | 2.09M | index = pcst->count++; |
106 | 2.09M | pcst->items[index].key.data = data; |
107 | 2.09M | pcst->items[index].key.size = size; |
108 | 2.09M | pcst->total += size; |
109 | 2.09M | return index; |
110 | 2.09M | } |
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 | 2.78M | { |
118 | | /****** FAILS IF TABLE FULL AND KEY MISSING ******/ |
119 | 2.78M | int j = (size == 0 ? 0 : data[0] * 23 + data[size - 1] * 59 + size); |
120 | 2.78M | int index, c = 0; |
121 | | |
122 | 7.54M | while ((index = pcst->items[j %= pcst->size].index1) != 0) { |
123 | 5.33M | --index; |
124 | 5.33M | if (!bytes_compare(pcst->items[index].key.data, |
125 | 5.33M | pcst->items[index].key.size, data, size)) { |
126 | 575k | *pindex = index; |
127 | 575k | return 0; |
128 | 575k | } |
129 | 4.75M | j += pcst->reprobe; |
130 | 4.75M | if (++c >= pcst->size) |
131 | 0 | break; |
132 | 4.75M | } |
133 | 2.20M | if (!enter) |
134 | 110k | return_error(gs_error_undefined); |
135 | 2.09M | index = cff_string_add(pcst, data, size); |
136 | 2.09M | if (index < 0) |
137 | 0 | return index; |
138 | 2.09M | pcst->items[j].index1 = index + 1; |
139 | 2.09M | *pindex = index; |
140 | 2.09M | return 1; |
141 | 2.09M | } |
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 | 592k | { |
147 | 592k | int index; |
148 | 592k | int code = cff_string_index(&pcw->std_strings, data, size, false, &index); |
149 | | |
150 | 592k | if (code < 0) { |
151 | 110k | code = cff_string_index(&pcw->strings, data, size, true, &index); |
152 | 110k | if (code < 0) |
153 | 0 | return code; |
154 | 110k | index += NUM_STD_STRINGS; |
155 | 110k | } |
156 | 592k | return index; |
157 | 592k | } |
158 | | static int |
159 | | cff_glyph_sid(cff_writer_t *pcw, gs_glyph glyph) |
160 | 490k | { |
161 | 490k | gs_const_string str; |
162 | 490k | int code = |
163 | 490k | pcw->pfont->procs.glyph_name((gs_font *)pcw->pfont, glyph, &str); |
164 | | |
165 | 490k | if (code < 0) |
166 | 0 | return code; |
167 | 490k | return cff_string_sid(pcw, str.data, str.size); |
168 | 490k | } |
169 | | |
170 | | /* ------ Low level ------ */ |
171 | | |
172 | | static void |
173 | | put_card16(cff_writer_t *pcw, uint c16) |
174 | 844k | { |
175 | 844k | sputc(pcw->strm, (byte)(c16 >> 8)); |
176 | 844k | sputc(pcw->strm, (byte)c16); |
177 | 844k | } |
178 | | static int |
179 | | offset_size(uint offset) |
180 | 148k | { |
181 | 148k | int size = 1; |
182 | | |
183 | 200k | while (offset > 255) |
184 | 51.5k | offset >>= 8, ++size; |
185 | 148k | return size; |
186 | 148k | } |
187 | | static void |
188 | | put_offset(cff_writer_t *pcw, int offset) |
189 | 711k | { |
190 | 711k | int i; |
191 | | |
192 | 1.86M | for (i = pcw->offset_size - 1; i >= 0; --i) |
193 | 1.15M | sputc(pcw->strm, (byte)(offset >> (i * 8))); |
194 | 711k | } |
195 | | static int |
196 | | put_bytes(stream * s, const byte *ptr, uint count) |
197 | 154k | { |
198 | 154k | uint used; |
199 | | |
200 | 154k | sputs(s, ptr, count, &used); |
201 | 154k | return (int)used; |
202 | 154k | } |
203 | | static int |
204 | | check_ioerror(stream * s) |
205 | 84.7k | { |
206 | 84.7k | uint used = 0; |
207 | | |
208 | 84.7k | return sputs(s, (byte *)&used, 0, &used); |
209 | 84.7k | } |
210 | | |
211 | | /* ------ Data types ------ */ |
212 | | |
213 | 382k | #define CE_OFFSET 32 |
214 | | static void |
215 | | cff_put_op(cff_writer_t *pcw, int op) |
216 | 382k | { |
217 | 382k | if (op >= CE_OFFSET) { |
218 | 120k | sputc(pcw->strm, cx_escape); |
219 | 120k | sputc(pcw->strm, (byte)(op - CE_OFFSET)); |
220 | 120k | } else |
221 | 261k | sputc(pcw->strm, (byte)op); |
222 | 382k | } |
223 | | static void |
224 | | cff_put_int(cff_writer_t *pcw, int i) |
225 | 1.03M | { |
226 | 1.03M | stream *s = pcw->strm; |
227 | | |
228 | 1.03M | if (i >= -107 && i <= 107) |
229 | 728k | sputc(s, (byte)(i + 139)); |
230 | 302k | else if (i <= 1131 && i >= 0) |
231 | 249k | put_card16(pcw, (c_pos2_0 << 8) + i - 108); |
232 | 52.6k | else if (i >= -1131 && i < 0) |
233 | 19.0k | put_card16(pcw, (c_neg2_0 << 8) - i - 108); |
234 | 33.5k | else if (i >= -32768 && i <= 32767) { |
235 | 6.98k | sputc(s, c2_shortint); |
236 | 6.98k | put_card16(pcw, i & 0xffff); |
237 | 26.6k | } else { |
238 | 26.6k | sputc(s, CD_LONGINT); |
239 | 26.6k | put_card16(pcw, i >> 16); |
240 | 26.6k | put_card16(pcw, i & 0xffff); |
241 | 26.6k | } |
242 | 1.03M | } |
243 | | static void |
244 | | cff_put_int_value(cff_writer_t *pcw, int i, int op) |
245 | 149k | { |
246 | 149k | cff_put_int(pcw, i); |
247 | 149k | cff_put_op(pcw, op); |
248 | 149k | } |
249 | | static void |
250 | | cff_put_int_if_ne(cff_writer_t *pcw, int i, int i_default, int op) |
251 | 203k | { |
252 | 203k | if (i != i_default) |
253 | 96.3k | cff_put_int_value(pcw, i, op); |
254 | 203k | } |
255 | | static void |
256 | | cff_put_bool(cff_writer_t *pcw, bool b) |
257 | 429 | { |
258 | 429 | cff_put_int(pcw, (b ? 1 : 0)); |
259 | 429 | } |
260 | | static void |
261 | | cff_put_bool_value(cff_writer_t *pcw, bool b, int op) |
262 | 429 | { |
263 | 429 | cff_put_bool(pcw, b); |
264 | 429 | cff_put_op(pcw, op); |
265 | 429 | } |
266 | | static void |
267 | | cff_put_real(cff_writer_t *pcw, double f) |
268 | 754k | { |
269 | 754k | if (f == (int)f) |
270 | 752k | cff_put_int(pcw, (int)f); |
271 | 1.83k | else { |
272 | | /* Use decimal representation. */ |
273 | 1.83k | char str[50]; |
274 | 1.83k | byte b = 0xff; |
275 | 1.83k | const char *p; |
276 | | |
277 | 1.83k | gs_snprintf(str, sizeof(str), "%g", f); |
278 | 1.83k | sputc(pcw->strm, CD_REAL); |
279 | 14.0k | for (p = str; ; ++p) { |
280 | 14.0k | int digit; |
281 | | |
282 | 14.0k | switch (*p) { |
283 | 1.83k | case 0: |
284 | 1.83k | goto done; |
285 | 1.83k | case '.': |
286 | 1.83k | digit = 0xa; break; |
287 | 4 | case '+': |
288 | 4 | continue; |
289 | 0 | case '-': |
290 | 0 | 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 | 6.54k | case '0': case '1': case '2': case '3': case '4': |
298 | 10.3k | case '5': case '6': case '7': case '8': case '9': |
299 | 10.3k | digit = *p - '0'; |
300 | 10.3k | break; |
301 | 0 | default: /* can't happen */ |
302 | 0 | digit = 0xd; /* invalid */ |
303 | 0 | break; |
304 | 14.0k | } |
305 | 12.1k | if (b == 0xff) |
306 | 6.84k | b = (digit << 4) + 0xf; |
307 | 5.33k | else { |
308 | 5.33k | sputc(pcw->strm, (byte)((b & 0xf0) + digit)); |
309 | 5.33k | b = 0xff; |
310 | 5.33k | } |
311 | 12.1k | } |
312 | 1.83k | done: |
313 | 1.83k | sputc(pcw->strm, b); |
314 | 1.83k | } |
315 | 754k | } |
316 | | static void |
317 | | cff_put_real_value(cff_writer_t *pcw, double f, int op) |
318 | 44.0k | { |
319 | 44.0k | cff_put_real(pcw, f); |
320 | 44.0k | cff_put_op(pcw, op); |
321 | 44.0k | } |
322 | | static void |
323 | | cff_put_real_if_ne(cff_writer_t *pcw, double f, double f_default, int op) |
324 | 117k | { |
325 | 117k | if ((float)f != (float)f_default) |
326 | 1.47k | cff_put_real_value(pcw, f, op); |
327 | 117k | } |
328 | | static void |
329 | | cff_put_real_deltarray(cff_writer_t *pcw, const float *pf, int count, int op) |
330 | 128k | { |
331 | 128k | float prev = 0; |
332 | 128k | int i; |
333 | | |
334 | 128k | if (count <= 0) |
335 | 66.3k | return; |
336 | 673k | for (i = 0; i < count; ++i) { |
337 | 611k | float f = pf[i]; |
338 | | |
339 | 611k | cff_put_real(pcw, f - prev); |
340 | 611k | prev = f; |
341 | 611k | } |
342 | 61.6k | cff_put_op(pcw, op); |
343 | 61.6k | } |
344 | | static int |
345 | | cff_put_string(cff_writer_t *pcw, const byte *data, uint size) |
346 | 101k | { |
347 | 101k | int sid = cff_string_sid(pcw, data, size); |
348 | | |
349 | 101k | if (sid < 0) |
350 | 0 | return sid; |
351 | 101k | cff_put_int(pcw, sid); |
352 | 101k | return 0; |
353 | 101k | } |
354 | | static int |
355 | | cff_put_string_value(cff_writer_t *pcw, const byte *data, uint size, int op) |
356 | 101k | { |
357 | 101k | int code = cff_put_string(pcw, data, size); |
358 | | |
359 | 101k | if (code >= 0) |
360 | 101k | cff_put_op(pcw, op); |
361 | 101k | return code; |
362 | 101k | } |
363 | | static int |
364 | | cff_extra_lenIV(const cff_writer_t *pcw, const gs_font_type1 *pfont) |
365 | 512k | { |
366 | 512k | return (pcw->options & WRITE_TYPE2_NO_LENIV ? |
367 | 512k | max(pfont->data.lenIV, 0) : 0); |
368 | 512k | } |
369 | | static bool |
370 | | cff_convert_charstrings(const cff_writer_t *pcw, const gs_font_base *pfont) |
371 | 954k | { |
372 | 954k | return (pfont->FontType != ft_encrypted2 && |
373 | 954k | (pcw->options & WRITE_TYPE2_CHARSTRINGS) != 0); |
374 | 954k | } |
375 | | static int |
376 | | cff_put_CharString(cff_writer_t *pcw, const byte *data, uint size, |
377 | | gs_font_type1 *pfont) |
378 | 409k | { |
379 | 409k | int lenIV = pfont->data.lenIV; |
380 | 409k | stream *s = pcw->strm; |
381 | | |
382 | 409k | if (cff_convert_charstrings(pcw, (gs_font_base *)pfont)) { |
383 | 389k | gs_glyph_data_t gdata; |
384 | 389k | int code; |
385 | | |
386 | 389k | gdata.memory = pfont->memory; |
387 | 389k | gs_glyph_data_from_string(&gdata, data, size, NULL); |
388 | 389k | code = psf_convert_type1_to_type2(s, &gdata, pfont); |
389 | 389k | if (code < 0) |
390 | 0 | return code; |
391 | 389k | } else if (lenIV < 0 || !(pcw->options & WRITE_TYPE2_NO_LENIV)) |
392 | 19.9k | 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 | 409k | return 0; |
410 | 409k | } |
411 | | static uint |
412 | | cff_Index_size(uint count, uint total) |
413 | 79.7k | { |
414 | 79.7k | return (count == 0 ? 2 : |
415 | 79.7k | 3 + offset_size(total + 1) * (count + 1) + total); |
416 | 79.7k | } |
417 | | static void |
418 | | cff_put_Index_header(cff_writer_t *pcw, uint count, uint total) |
419 | 126k | { |
420 | 126k | put_card16(pcw, count); |
421 | 126k | if (count > 0) { |
422 | 85.1k | pcw->offset_size = offset_size(total + 1); |
423 | 85.1k | sputc(pcw->strm, (byte)pcw->offset_size); |
424 | 85.1k | put_offset(pcw, 1); |
425 | 85.1k | } |
426 | 126k | } |
427 | | static void |
428 | | cff_put_Index(cff_writer_t *pcw, const cff_string_table_t *pcst) |
429 | 21.2k | { |
430 | 21.2k | uint j, offset; |
431 | | |
432 | 21.2k | if (pcst->count == 0) { |
433 | 16 | put_card16(pcw, 0); |
434 | 16 | return; |
435 | 16 | } |
436 | 21.2k | cff_put_Index_header(pcw, pcst->count, pcst->total); |
437 | 92.2k | for (j = 0, offset = 1; j < pcst->count; ++j) { |
438 | 70.9k | offset += pcst->items[j].key.size; |
439 | 70.9k | put_offset(pcw, offset); |
440 | 70.9k | } |
441 | 92.2k | for (j = 0; j < pcst->count; ++j) |
442 | 70.9k | put_bytes(pcw->strm, pcst->items[j].key.data, pcst->items[j].key.size); |
443 | 21.2k | } |
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 | 21.2k | { |
453 | 21.2k | pcw->offset_size = (end_offset > 0x7fff ? 3 : 2); |
454 | 21.2k | put_bytes(pcw->strm, (const byte *)"\001\000\004", 3); |
455 | 21.2k | sputc(pcw->strm, (byte)pcw->offset_size); |
456 | 21.2k | return 0; |
457 | 21.2k | } |
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 | 26.5k | #define charset_DEFAULT 0 |
480 | | TOP_Encoding = 16, /* (offset or predefined index) */ |
481 | | #define Encoding_Standard 0 |
482 | | #define Encoding_Expert 1 |
483 | 26.4k | #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 | 53.4k | #define ItalicAngle_DEFAULT 0 |
491 | | TOP_UnderlinePosition = 35, |
492 | 53.4k | #define UnderlinePosition_DEFAULT (-100) |
493 | | TOP_UnderlineThickness = 36, |
494 | 53.4k | #define UnderlineThickness_DEFAULT 50 |
495 | | TOP_PaintType = 37, |
496 | 26.7k | #define PaintType_DEFAULT 0 |
497 | | TOP_CharstringType = 38, |
498 | 26.4k | #define CharstringType_DEFAULT 2 |
499 | | TOP_FontMatrix = 39, /* default is [0.001 0 0 0.001 0 0] */ |
500 | | TOP_StrokeWidth = 40, |
501 | 26.7k | #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 | 84 | #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 | 26.7k | { |
521 | 26.7k | pinfo->Flags_requested = FONT_IS_FIXED_WIDTH; |
522 | | /* Preset defaults */ |
523 | 26.7k | pinfo->members = 0; |
524 | 26.7k | pinfo->Flags = pinfo->Flags_returned = 0; |
525 | 26.7k | pinfo->ItalicAngle = ItalicAngle_DEFAULT; |
526 | 26.7k | pinfo->UnderlinePosition = UnderlinePosition_DEFAULT; |
527 | 26.7k | pinfo->UnderlineThickness = UnderlineThickness_DEFAULT; |
528 | 26.7k | return pbfont->procs.font_info |
529 | 26.7k | ((gs_font *)pbfont, NULL, |
530 | 26.7k | (full_info ? |
531 | 26.5k | FONT_INFO_FLAGS | FONT_INFO_ITALIC_ANGLE | |
532 | 26.5k | FONT_INFO_UNDERLINE_POSITION | |
533 | 26.5k | FONT_INFO_UNDERLINE_THICKNESS : 0) | |
534 | 26.7k | (FONT_INFO_COPYRIGHT | FONT_INFO_NOTICE | |
535 | 26.7k | FONT_INFO_FAMILY_NAME | FONT_INFO_FULL_NAME), |
536 | 26.7k | pinfo); |
537 | 26.7k | } |
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 | 26.7k | { |
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 | 26.7k | if (pinfo->members & FONT_INFO_NOTICE) |
552 | 26.0k | cff_put_string_value(pcw, pinfo->Notice.data, pinfo->Notice.size, |
553 | 26.0k | TOP_Notice); |
554 | 26.7k | if (pinfo->members & FONT_INFO_FULL_NAME) |
555 | 25.7k | cff_put_string_value(pcw, pinfo->FullName.data, pinfo->FullName.size, |
556 | 25.7k | TOP_FullName); |
557 | 26.7k | if (pinfo->members & FONT_INFO_FAMILY_NAME) |
558 | 25.7k | cff_put_string_value(pcw, pinfo->FamilyName.data, |
559 | 25.7k | pinfo->FamilyName.size, TOP_FamilyName); |
560 | 26.7k | if (pcw->FontBBox.p.x != 0 || pcw->FontBBox.p.y != 0 || |
561 | 26.7k | pcw->FontBBox.q.x != 0 || pcw->FontBBox.q.y != 0 |
562 | 26.7k | ) { |
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 | 24.0k | cff_put_real(pcw, pcw->FontBBox.p.x); |
571 | 24.0k | cff_put_real(pcw, pcw->FontBBox.p.y); |
572 | 24.0k | cff_put_real(pcw, pcw->FontBBox.q.x); |
573 | 24.0k | cff_put_real(pcw, pcw->FontBBox.q.y); |
574 | 24.0k | cff_put_op(pcw, TOP_FontBBox); |
575 | 24.0k | } |
576 | 26.7k | if (uid_is_UniqueID(&pbfont->UID)) |
577 | 15 | cff_put_int_value(pcw, pbfont->UID.id, TOP_UniqueID); |
578 | 26.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 | 26.7k | if (!(pcw->options & WRITE_TYPE2_AR3)) { |
595 | 26.7k | if (pinfo->members & FONT_INFO_COPYRIGHT) |
596 | 23.9k | cff_put_string_value(pcw, pinfo->Copyright.data, |
597 | 23.9k | pinfo->Copyright.size, TOP_Copyright); |
598 | 26.7k | if (pinfo->Flags & pinfo->Flags_returned & FONT_IS_FIXED_WIDTH) |
599 | 237 | cff_put_bool_value(pcw, true, TOP_isFixedPitch); |
600 | 26.7k | cff_put_real_if_ne(pcw, pinfo->ItalicAngle, ItalicAngle_DEFAULT, |
601 | 26.7k | TOP_ItalicAngle); |
602 | 26.7k | cff_put_int_if_ne(pcw, pinfo->UnderlinePosition, |
603 | 26.7k | UnderlinePosition_DEFAULT, TOP_UnderlinePosition); |
604 | 26.7k | cff_put_int_if_ne(pcw, pinfo->UnderlineThickness, |
605 | 26.7k | UnderlineThickness_DEFAULT, TOP_UnderlineThickness); |
606 | 26.7k | cff_put_int_if_ne(pcw, pbfont->PaintType, PaintType_DEFAULT, |
607 | 26.7k | TOP_PaintType); |
608 | 26.7k | } |
609 | 26.7k | { |
610 | 26.7k | static const gs_matrix fm_default = { |
611 | 26.7k | constant_matrix_body(0.001, 0, 0, 0.001, 0, 0) |
612 | 26.7k | }; |
613 | | |
614 | 26.7k | if (write_FontMatrix || |
615 | 26.7k | pbfont->FontMatrix.xx != fm_default.xx || |
616 | 26.7k | pbfont->FontMatrix.xy != 0 || pbfont->FontMatrix.yx != 0 || |
617 | 26.7k | pbfont->FontMatrix.yy != fm_default.yy || |
618 | 26.7k | pbfont->FontMatrix.tx != 0 || pbfont->FontMatrix.ty != 0 |
619 | 26.7k | ) { |
620 | 364 | cff_put_real(pcw, pbfont->FontMatrix.xx); |
621 | 364 | cff_put_real(pcw, pbfont->FontMatrix.xy); |
622 | 364 | cff_put_real(pcw, pbfont->FontMatrix.yx); |
623 | 364 | cff_put_real(pcw, pbfont->FontMatrix.yy); |
624 | 364 | cff_put_real(pcw, pbfont->FontMatrix.tx); |
625 | 364 | cff_put_real(pcw, pbfont->FontMatrix.ty); |
626 | 364 | cff_put_op(pcw, TOP_FontMatrix); |
627 | 364 | } |
628 | 26.7k | } |
629 | 26.7k | cff_put_real_if_ne(pcw, pbfont->StrokeWidth, StrokeWidth_DEFAULT, |
630 | 26.7k | TOP_StrokeWidth); |
631 | 26.7k | } |
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 | 26.4k | { |
639 | 26.4k | gs_font_base *pbfont = (gs_font_base *)pcw->pfont; |
640 | 26.4k | gs_font_info_t info; |
641 | | |
642 | 26.4k | cff_get_Top_info_common(pcw, pbfont, true, &info); |
643 | 26.4k | cff_write_Top_common(pcw, pbfont, false, &info); |
644 | 26.4k | cff_put_int(pcw, Private_size); |
645 | 26.4k | cff_put_int_value(pcw, Private_offset, TOP_Private); |
646 | 26.4k | cff_put_int_value(pcw, CharStrings_offset, TOP_CharStrings); |
647 | 26.4k | cff_put_int_if_ne(pcw, charset_offset, charset_DEFAULT, TOP_charset); |
648 | 26.4k | cff_put_int_if_ne(pcw, Encoding_offset, Encoding_DEFAULT, TOP_Encoding); |
649 | 26.4k | { |
650 | 26.4k | int type = (pcw->options & WRITE_TYPE2_CHARSTRINGS ? 2 : |
651 | 26.4k | pbfont->FontType == ft_encrypted2 ? 2 : 1); |
652 | | |
653 | 26.4k | cff_put_int_if_ne(pcw, type, CharstringType_DEFAULT, |
654 | 26.4k | TOP_CharstringType); |
655 | 26.4k | } |
656 | 26.4k | } |
657 | | |
658 | | /* CIDFontType 0 CIDFont */ |
659 | | static void |
660 | | cff_write_ROS(cff_writer_t *pcw, const gs_cid_system_info_t *pcidsi) |
661 | 105 | { |
662 | 105 | cff_put_string(pcw, pcidsi->Registry.data, pcidsi->Registry.size); |
663 | 105 | cff_put_string(pcw, pcidsi->Ordering.data, pcidsi->Ordering.size); |
664 | 105 | cff_put_int_value(pcw, pcidsi->Supplement, TOP_ROS); |
665 | 105 | } |
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 | 84 | { |
671 | 84 | gs_font_base *pbfont = (gs_font_base *)pcw->pfont; |
672 | 84 | gs_font_cid0 *pfont = (gs_font_cid0 *)pbfont; |
673 | | |
674 | 84 | cff_write_ROS(pcw, &pfont->cidata.common.CIDSystemInfo); |
675 | 84 | cff_write_Top_common(pcw, pbfont, true, pinfo); /* full_info = true */ |
676 | 84 | cff_put_int_if_ne(pcw, charset_offset, charset_DEFAULT, TOP_charset); |
677 | 84 | 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 | 84 | cff_put_int_if_ne(pcw, pfont->cidata.common.CIDCount, CIDCount_DEFAULT, |
683 | 84 | TOP_CIDCount); |
684 | | /* We don't use UIDBase. */ |
685 | 84 | cff_put_int_value(pcw, Font_offset, TOP_FDArray); |
686 | 84 | cff_put_int_value(pcw, FDSelect_offset, TOP_FDSelect); |
687 | 84 | } |
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 | 84 | { |
694 | 84 | int j; |
695 | | |
696 | 84 | cff_put_Index_header(pcw, num_fonts, |
697 | 84 | FDArray_offsets[num_fonts] - FDArray_offsets[0]); |
698 | 236 | for (j = 1; j <= num_fonts; ++j) |
699 | 152 | put_offset(pcw, FDArray_offsets[j] - FDArray_offsets[0] + 1); |
700 | 84 | } |
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 | 190 | { |
707 | 190 | const gs_font_name *pfname = &pbfont->font_name; |
708 | 190 | gs_font_info_t info; |
709 | | |
710 | 190 | cff_get_Top_info_common(pcw, pbfont, false, &info); |
711 | 190 | cff_write_Top_common(pcw, pbfont, true, &info); |
712 | 190 | cff_put_int(pcw, Private_size); |
713 | 190 | cff_put_int_value(pcw, Private_offset, TOP_Private); |
714 | 190 | if (pfname->size == 0) |
715 | 0 | pfname = &pbfont->key_name; |
716 | 190 | if (pfname->size) { |
717 | 190 | cff_put_string(pcw, pfname->chars, pfname->size); |
718 | 190 | cff_put_op(pcw, TOP_FontName); |
719 | 190 | } |
720 | 190 | } |
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 | 933 | #define defaultWidthX_DEFAULT fixed_0 |
735 | | PRIVATE_nominalWidthX = 21, |
736 | 933 | #define nominalWidthX_DEFAULT fixed_0 |
737 | | PRIVATE_BlueScale = 41, |
738 | 21.3k | #define BlueScale_DEFAULT 0.039625 |
739 | | PRIVATE_BlueShift = 42, |
740 | 21.3k | #define BlueShift_DEFAULT 7 |
741 | | PRIVATE_BlueFuzz = 43, |
742 | 21.3k | #define BlueFuzz_DEFAULT 1 |
743 | | PRIVATE_StemSnapH = 44, /* (deltarray) */ |
744 | | PRIVATE_StemSnapV = 45, /* (deltarray) */ |
745 | | PRIVATE_ForceBold = 46, |
746 | 21.3k | #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 | 21.3k | #define LanguageGroup_DEFAULT 0 |
753 | | PRIVATE_ExpansionFactor = 50, |
754 | 21.3k | #define ExpansionFactor_DEFAULT 0.06 |
755 | | PRIVATE_initialRandomSeed = 51 |
756 | 933 | #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 | 21.3k | { |
765 | 21.3k | #define PUT_FLOAT_TABLE(member, op)\ |
766 | 128k | cff_put_real_deltarray(pcw, pfont->data.member.values,\ |
767 | 128k | pfont->data.member.count, op) |
768 | | |
769 | 21.3k | PUT_FLOAT_TABLE(BlueValues, PRIVATE_BlueValues); |
770 | 21.3k | PUT_FLOAT_TABLE(OtherBlues, PRIVATE_OtherBlues); |
771 | 21.3k | PUT_FLOAT_TABLE(FamilyBlues, PRIVATE_FamilyBlues); |
772 | 21.3k | PUT_FLOAT_TABLE(FamilyOtherBlues, PRIVATE_FamilyOtherBlues); |
773 | 21.3k | if (pfont->data.StdHW.count > 0) |
774 | 20.7k | cff_put_real_value(pcw, pfont->data.StdHW.values[0], PRIVATE_StdHW); |
775 | 21.3k | if (pfont->data.StdVW.count > 0) |
776 | 20.8k | cff_put_real_value(pcw, pfont->data.StdVW.values[0], PRIVATE_StdVW); |
777 | 21.3k | if (Subrs_offset) |
778 | 0 | cff_put_int_value(pcw, Subrs_offset, PRIVATE_Subrs); |
779 | 21.3k | if (pfont->FontType != ft_encrypted) { |
780 | 933 | if (pfont->data.defaultWidthX != defaultWidthX_DEFAULT) |
781 | 543 | cff_put_real_value(pcw, fixed2float(pfont->data.defaultWidthX), |
782 | 543 | PRIVATE_defaultWidthX); |
783 | 933 | if (pfont->data.nominalWidthX != nominalWidthX_DEFAULT) |
784 | 416 | cff_put_real_value(pcw, fixed2float(pfont->data.nominalWidthX), |
785 | 416 | PRIVATE_nominalWidthX); |
786 | 933 | cff_put_int_if_ne(pcw, pfont->data.initialRandomSeed, |
787 | 933 | initialRandomSeed_DEFAULT, |
788 | 933 | PRIVATE_initialRandomSeed); |
789 | 933 | } |
790 | 21.3k | cff_put_real_if_ne(pcw, pfont->data.BlueScale, BlueScale_DEFAULT, |
791 | 21.3k | PRIVATE_BlueScale); |
792 | 21.3k | cff_put_real_if_ne(pcw, pfont->data.BlueShift, BlueShift_DEFAULT, |
793 | 21.3k | PRIVATE_BlueShift); |
794 | 21.3k | cff_put_int_if_ne(pcw, pfont->data.BlueFuzz, BlueFuzz_DEFAULT, |
795 | 21.3k | PRIVATE_BlueFuzz); |
796 | 21.3k | PUT_FLOAT_TABLE(StemSnapH, PRIVATE_StemSnapH); |
797 | 21.3k | PUT_FLOAT_TABLE(StemSnapV, PRIVATE_StemSnapV); |
798 | 21.3k | if (pfont->data.ForceBold != ForceBold_DEFAULT) |
799 | 192 | cff_put_bool_value(pcw, pfont->data.ForceBold, |
800 | 192 | PRIVATE_ForceBold); |
801 | | /* (ForceBoldThreshold) */ |
802 | 21.3k | 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 | 21.3k | cff_put_int_if_ne(pcw, pfont->data.LanguageGroup, LanguageGroup_DEFAULT, |
806 | 21.3k | PRIVATE_LanguageGroup); |
807 | 21.3k | cff_put_real_if_ne(pcw, pfont->data.ExpansionFactor, |
808 | 21.3k | ExpansionFactor_DEFAULT, PRIVATE_ExpansionFactor); |
809 | | /* initialRandomSeed was handled above */ |
810 | | |
811 | 21.3k | #undef PUT_FLOAT_TABLE |
812 | 21.3k | } |
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 | 26.6k | { |
821 | 26.6k | gs_font_base *pfont = pcw->pfont; |
822 | 26.6k | int offset; |
823 | 26.6k | gs_glyph glyph; |
824 | 26.6k | uint count; |
825 | 26.6k | stream poss; |
826 | 26.6k | int code; |
827 | | |
828 | 26.6k | s_init(&poss, NULL); |
829 | 26.6k | psf_enumerate_glyphs_reset(penum); |
830 | 26.6k | for (glyph = GS_NO_GLYPH, count = 0, offset = 1; |
831 | 539k | (code = psf_enumerate_glyphs_next(penum, &glyph)) != 1; |
832 | 512k | ) { |
833 | 512k | gs_glyph_data_t gdata; |
834 | 512k | gs_font_type1 *pfd; |
835 | 512k | int gcode; |
836 | | |
837 | 512k | gdata.memory = pfont->memory; |
838 | 512k | if (code == 0 && |
839 | 512k | (gcode = pcw->glyph_data(pfont, glyph, &gdata, &pfd)) >= 0 |
840 | 512k | ) { |
841 | 512k | int extra_lenIV; |
842 | | |
843 | 512k | if (gdata.bits.size >= (extra_lenIV = cff_extra_lenIV(pcw, pfd))) { |
844 | 512k | if (cff_convert_charstrings(pcw, (gs_font_base *)pfd)) { |
845 | 487k | swrite_position_only(&poss); |
846 | 487k | code = psf_convert_type1_to_type2(&poss, &gdata, pfd); |
847 | 487k | if (code < 0) |
848 | 30 | return code; |
849 | 487k | offset += stell(&poss); |
850 | 487k | } else |
851 | 24.7k | offset += gdata.bits.size - extra_lenIV; |
852 | 512k | } |
853 | 512k | gs_glyph_data_free(&gdata, "cff_write_CharStrings_offsets"); |
854 | 512k | put_offset(pcw, offset); |
855 | 512k | count++; |
856 | 512k | } |
857 | 512k | } |
858 | 26.5k | *pcount = count; |
859 | 26.5k | return offset - 1; |
860 | 26.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 | 21.2k | { |
865 | 21.2k | gs_font_base *pfont = pcw->pfont; |
866 | 21.2k | uint ignore_count; |
867 | 21.2k | gs_glyph glyph; |
868 | 21.2k | int code; |
869 | | |
870 | 21.2k | cff_put_Index_header(pcw, charstrings_count, charstrings_size); |
871 | 21.2k | cff_write_CharStrings_offsets(pcw, penum, &ignore_count); |
872 | 21.2k | psf_enumerate_glyphs_reset(penum); |
873 | 21.2k | for (glyph = GS_NO_GLYPH; |
874 | 431k | (code = psf_enumerate_glyphs_next(penum, &glyph)) != 1; |
875 | 409k | ) { |
876 | 409k | gs_glyph_data_t gdata; |
877 | 409k | gs_font_type1 *pfd; |
878 | | |
879 | 409k | gdata.memory = pfont->memory; |
880 | 409k | if (code == 0 && |
881 | 409k | (code = pcw->glyph_data(pfont, glyph, &gdata, &pfd)) >= 0 |
882 | 409k | ) { |
883 | 409k | cff_put_CharString(pcw, gdata.bits.data, gdata.bits.size, pfd); |
884 | 409k | gs_glyph_data_free(&gdata, "cff_write_CharStrings"); |
885 | 409k | } |
886 | 409k | } |
887 | 21.2k | } |
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 | 435 | { |
900 | 435 | int extra_lenIV = cff_extra_lenIV(pcw, pfont); |
901 | 435 | int j, offset; |
902 | 435 | int code; |
903 | 435 | gs_glyph_data_t gdata; |
904 | | |
905 | 435 | gdata.memory = pfont->memory; |
906 | 435 | for (j = 0, offset = 1; |
907 | 435 | (code = pfont->data.procs.subr_data(pfont, j, global, &gdata)) != |
908 | 435 | gs_error_rangecheck; |
909 | 435 | ++j) { |
910 | 0 | if (code >= 0 && gdata.bits.size >= extra_lenIV) |
911 | 0 | offset += gdata.bits.size - extra_lenIV; |
912 | 0 | put_offset(pcw, offset); |
913 | 0 | if (code >= 0) |
914 | 0 | gs_glyph_data_free(&gdata, "cff_write_Subrs_offsets"); |
915 | 0 | } |
916 | 435 | *pcount = j; |
917 | 435 | return offset - 1; |
918 | 435 | } |
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 | 0 | { |
924 | 0 | int j; |
925 | 0 | uint ignore_count; |
926 | 0 | gs_glyph_data_t gdata; |
927 | 0 | int code; |
928 | |
|
929 | 0 | gdata.memory = pfont->memory; |
930 | 0 | cff_put_Index_header(pcw, subrs_count, subrs_size); |
931 | 0 | cff_write_Subrs_offsets(pcw, &ignore_count, pfont, global); |
932 | 0 | for (j = 0; |
933 | 0 | (code = pfont->data.procs.subr_data(pfont, j, global, &gdata)) != |
934 | 0 | gs_error_rangecheck; |
935 | 0 | ++j) { |
936 | 0 | if (code >= 0) { |
937 | 0 | cff_put_CharString(pcw, gdata.bits.data, gdata.bits.size, pfont); |
938 | 0 | gs_glyph_data_free(&gdata, "cff_write_Subrs"); |
939 | 0 | } |
940 | 0 | } |
941 | 0 | } |
942 | | |
943 | | /* ------ Encoding/charset ------ */ |
944 | | |
945 | | static uint |
946 | | cff_Encoding_size(cff_writer_t *pcw, cff_glyph_subset_t *pgsub) |
947 | 5.31k | { |
948 | 5.31k | int j, code, max_enc = 0, nsupp = 0; |
949 | 5.31k | gs_font_type1 *pfont = (gs_font_type1 *)pcw->pfont; |
950 | 5.31k | byte used[255]; |
951 | 5.31k | gs_const_string str; |
952 | | |
953 | 5.31k | memset(used, 0, 255); |
954 | 1.36M | for (j = 0; j < 256; ++j) { |
955 | 1.35M | gs_glyph glyph = pfont->procs.encode_char((gs_font *)pfont, |
956 | 1.35M | (gs_char)j, |
957 | 1.35M | GLYPH_SPACE_NAME); |
958 | 1.35M | int i; |
959 | | |
960 | 1.35M | if (glyph == GS_NO_GLYPH || glyph == pgsub->glyphs.notdef) |
961 | 1.26M | continue; |
962 | 97.3k | i = psf_sorted_glyphs_index_of(pgsub->glyphs.subset_data + 1, |
963 | 97.3k | pgsub->num_encoded, glyph); |
964 | 97.3k | if (i < 0) |
965 | 0 | continue; /* encoded but not in subset */ |
966 | 97.3k | code = pcw->pfont->procs.glyph_name((gs_font *)pcw->pfont, glyph, &str); |
967 | 97.3k | if (code < 0) |
968 | 0 | continue; |
969 | 97.3k | if (i >= sizeof(used) || used[i]) |
970 | 20 | nsupp++; |
971 | 97.3k | else { |
972 | 97.3k | used[i] = 1; |
973 | 97.3k | if (i > max_enc) |
974 | 13.8k | max_enc = i; |
975 | 97.3k | } |
976 | 97.3k | } |
977 | 5.31k | return 2 + (max_enc+1) + (3 * nsupp) + (nsupp > 0 ? 1 : 0); |
978 | 5.31k | } |
979 | | |
980 | | static int |
981 | | cff_write_Encoding(cff_writer_t *pcw, cff_glyph_subset_t *pgsub) |
982 | 21.1k | { |
983 | 21.1k | stream *s = pcw->strm; |
984 | | /* This procedure is only used for Type 1 / Type 2 fonts. */ |
985 | 21.1k | gs_font_type1 *pfont = (gs_font_type1 *)pcw->pfont; |
986 | 21.1k | byte used[255], index[255], supplement[256]; |
987 | 21.1k | int num_enc = min(pgsub->num_encoded, sizeof(index)); |
988 | 21.1k | int nsupp = 0; |
989 | 21.1k | int j, code; |
990 | 21.1k | int max_enc = 0; |
991 | 21.1k | gs_const_string str; |
992 | | |
993 | 21.1k | memset(used, 0, num_enc); |
994 | 21.1k | memset(index, 0, sizeof(index)); |
995 | 5.44M | for (j = 0; j < 256; ++j) { |
996 | 5.42M | gs_glyph glyph = pfont->procs.encode_char((gs_font *)pfont, |
997 | 5.42M | (gs_char)j, |
998 | 5.42M | GLYPH_SPACE_NAME); |
999 | 5.42M | int i; |
1000 | | |
1001 | 5.42M | if (glyph == GS_NO_GLYPH || glyph == pgsub->glyphs.notdef) |
1002 | 5.03M | continue; |
1003 | 388k | i = psf_sorted_glyphs_index_of(pgsub->glyphs.subset_data + 1, |
1004 | 388k | pgsub->num_encoded, glyph); |
1005 | 388k | if (i < 0) |
1006 | 0 | continue; /* encoded but not in subset */ |
1007 | 388k | code = pcw->pfont->procs.glyph_name((gs_font *)pcw->pfont, glyph, &str); |
1008 | 388k | if (code < 0) |
1009 | 0 | continue; |
1010 | 388k | if (i >= sizeof(used) || used[i]) |
1011 | 80 | supplement[nsupp++] = j; |
1012 | 388k | else { |
1013 | 388k | index[i] = j; |
1014 | 388k | used[i] = 1; |
1015 | 388k | if (i > max_enc) |
1016 | 55.2k | max_enc = i; |
1017 | 388k | } |
1018 | 388k | } |
1019 | 21.1k | sputc(s, (byte)(nsupp ? 0x80 : 0)); |
1020 | 21.1k | 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 | 21.1k | put_bytes(s, index, max_enc+1); |
1034 | 21.1k | if (nsupp) { |
1035 | | /* Write supplementary entries for multiply-encoded glyphs. */ |
1036 | 20 | sputc(s, (byte)nsupp); |
1037 | 100 | for (j = 0; j < nsupp; ++j) { |
1038 | 80 | byte chr = supplement[j]; |
1039 | | |
1040 | 80 | sputc(s, chr); |
1041 | 80 | put_card16(pcw, |
1042 | 80 | cff_glyph_sid(pcw, |
1043 | 80 | pfont->procs.encode_char((gs_font *)pfont, |
1044 | 80 | (gs_char)chr, |
1045 | 80 | GLYPH_SPACE_NAME))); |
1046 | 80 | } |
1047 | 20 | } |
1048 | 21.1k | return 0; |
1049 | 21.1k | } |
1050 | | |
1051 | | static int |
1052 | | cff_write_charset(cff_writer_t *pcw, cff_glyph_subset_t *pgsub) |
1053 | 21.1k | { |
1054 | 21.1k | int j, code; |
1055 | | |
1056 | 21.1k | sputc(pcw->strm, 0); |
1057 | 409k | for (j = 1; j < pgsub->glyphs.subset_size; j++) { |
1058 | 388k | code = cff_glyph_sid(pcw, pgsub->glyphs.subset_data[j]); |
1059 | 388k | if (code < 0) |
1060 | 0 | continue; |
1061 | 388k | put_card16(pcw, code); |
1062 | 388k | } |
1063 | 21.1k | return 0; |
1064 | 21.1k | } |
1065 | | static int |
1066 | | cff_write_cidset(cff_writer_t *pcw, psf_glyph_enum_t *penum) |
1067 | 105 | { |
1068 | 105 | gs_glyph glyph; |
1069 | 105 | int code; |
1070 | | |
1071 | 105 | sputc(pcw->strm, 0); |
1072 | 105 | psf_enumerate_glyphs_reset(penum); |
1073 | 740 | while ((code = psf_enumerate_glyphs_next(penum, &glyph)) == 0) { |
1074 | | /* Skip glyph 0 (the .notdef glyph), which is always first. */ |
1075 | 635 | if (glyph != GS_MIN_CID_GLYPH) |
1076 | 530 | put_card16(pcw, (uint)(glyph - GS_MIN_CID_GLYPH)); |
1077 | 635 | } |
1078 | 105 | return min(code, 0); |
1079 | 105 | } |
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 | 21 | { |
1087 | 21 | gs_font_cid0 *const pfont = (gs_font_cid0 *)pcw->pfont; |
1088 | 21 | gs_font_base *const pbfont = (gs_font_base *)pfont; |
1089 | 21 | gs_glyph glyph; |
1090 | 21 | int prev = -1; |
1091 | 21 | uint linear_size = 1, range_size = 5; |
1092 | 21 | int code; |
1093 | | |
1094 | | /* Determine whether format 0 or 3 is more efficient. */ |
1095 | 21 | psf_enumerate_glyphs_reset(penum); |
1096 | 148 | while ((code = psf_enumerate_glyphs_next(penum, &glyph)) == 0) { |
1097 | 127 | int font_index; |
1098 | | |
1099 | 127 | code = pfont->cidata.glyph_data(pbfont, glyph, NULL, &font_index); |
1100 | 127 | if (code >= 0) { |
1101 | 127 | if (font_index != prev) |
1102 | 33 | range_size += 3, prev = font_index; |
1103 | 127 | ++linear_size; |
1104 | 127 | } |
1105 | 127 | } |
1106 | 21 | if (range_size < linear_size) { |
1107 | 4 | *pformat = 3; |
1108 | 4 | return range_size; |
1109 | 17 | } else { |
1110 | 17 | *pformat = 0; |
1111 | 17 | return linear_size; |
1112 | 17 | } |
1113 | 21 | } |
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 | 84 | { |
1120 | 84 | stream *s = pcw->strm; |
1121 | 84 | gs_font_cid0 *const pfont = (gs_font_cid0 *)pcw->pfont; |
1122 | 84 | gs_font_base *const pbfont = (gs_font_base *)pfont; |
1123 | 84 | gs_glyph glyph; |
1124 | 84 | int prev = -1; |
1125 | 84 | uint cid_count = 0; |
1126 | 84 | int code; |
1127 | | |
1128 | 84 | spputc(s, (byte)format); |
1129 | 84 | psf_enumerate_glyphs_reset(penum); |
1130 | 84 | switch (format) { |
1131 | 16 | case 3: /* ranges */ |
1132 | 16 | put_card16(pcw, (size - 5) / 3); |
1133 | 352 | while ((code = psf_enumerate_glyphs_next(penum, &glyph)) == 0) { |
1134 | 336 | int font_index; |
1135 | | |
1136 | 336 | code = pfont->cidata.glyph_data(pbfont, glyph, NULL, &font_index); |
1137 | 336 | if (code >= 0) { |
1138 | 336 | if (font_index != prev) { |
1139 | 56 | put_card16(pcw, cid_count); |
1140 | 56 | sputc(s, (byte)font_index); |
1141 | 56 | prev = font_index; |
1142 | 56 | } |
1143 | 336 | ++cid_count; |
1144 | 336 | } |
1145 | 336 | } |
1146 | 16 | put_card16(pcw, cid_count); |
1147 | 16 | break; |
1148 | 68 | case 0: /* linear table */ |
1149 | 240 | while ((code = psf_enumerate_glyphs_next(penum, &glyph)) == 0) { |
1150 | 172 | int font_index; |
1151 | | |
1152 | 172 | code = pfont->cidata.glyph_data(pbfont, glyph, NULL, &font_index); |
1153 | 172 | if (code >= 0) |
1154 | 172 | sputc(s, (byte)font_index); |
1155 | 172 | } |
1156 | 68 | break; |
1157 | 0 | default: /* not possible */ |
1158 | 0 | return_error(gs_error_rangecheck); |
1159 | 84 | } |
1160 | 84 | return 0; |
1161 | 84 | } |
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 | 5.31k | { |
1172 | 5.31k | gs_font_base *const pbfont = (gs_font_base *)pfont; |
1173 | 5.31k | cff_writer_t writer; |
1174 | 5.31k | cff_glyph_subset_t subset; |
1175 | 5.31k | cff_string_item_t *std_string_items; |
1176 | 5.31k | cff_string_item_t *string_items; |
1177 | 5.31k | gs_const_string font_name; |
1178 | 5.31k | stream poss; |
1179 | 5.31k | uint charstrings_count, charstrings_size; |
1180 | 5.31k | uint subrs_count, subrs_size; |
1181 | 5.31k | uint gsubrs_count, gsubrs_size, encoding_size; |
1182 | 5.31k | int charset_size = -1; |
1183 | 5.31k | 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 | 5.31k | uint |
1189 | 5.31k | Top_size = 0x7fffff, |
1190 | 5.31k | GSubrs_offset, |
1191 | 5.31k | Encoding_offset, |
1192 | 5.31k | charset_offset, |
1193 | 5.31k | CharStrings_offset, |
1194 | 5.31k | Private_offset, |
1195 | 5.31k | Private_size = 0x7fffff, |
1196 | 5.31k | Subrs_offset, |
1197 | 5.31k | End_offset = 0x7fffff; |
1198 | 5.31k | int j; |
1199 | 5.31k | psf_glyph_enum_t genum; |
1200 | 5.31k | gs_glyph glyph; |
1201 | 5.31k | long start_pos; |
1202 | 5.31k | uint offset; |
1203 | 5.31k | int code; |
1204 | | |
1205 | | /* Allocate the string tables. */ |
1206 | 5.31k | psf_enumerate_glyphs_begin(&genum, (gs_font *)pfont, |
1207 | 5.31k | NULL, 0, GLYPH_SPACE_NAME); |
1208 | 107k | while ((code = psf_enumerate_glyphs_next(&genum, &glyph)) != 1) |
1209 | 102k | number_of_glyphs++; |
1210 | 5.31k | subset.glyphs.subset_data = (gs_glyph *)gs_alloc_bytes(pfont->memory, |
1211 | 5.31k | number_of_glyphs * sizeof(glyph), "psf_write_type2_font"); |
1212 | 5.31k | number_of_strings = number_of_glyphs + MAX_CFF_MISC_STRINGS; |
1213 | 5.31k | std_string_items = (cff_string_item_t *)gs_alloc_bytes(pfont->memory, |
1214 | 5.31k | (MAX_CFF_STD_STRINGS + number_of_strings) * sizeof(cff_string_item_t), |
1215 | 5.31k | "psf_write_type2_font"); |
1216 | 5.31k | 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 | 5.31k | string_items = std_string_items + MAX_CFF_STD_STRINGS; |
1221 | | |
1222 | | /* Get subset glyphs. */ |
1223 | 5.31k | code = psf_get_type1_glyphs(&subset.glyphs, pfont, subset_glyphs, |
1224 | 5.31k | subset_size); |
1225 | 5.31k | if (code < 0) |
1226 | 0 | goto error; |
1227 | 5.31k | 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 | 5.31k | if (options & WRITE_TYPE2_CHARSTRINGS) { |
1234 | 5.31k | options |= WRITE_TYPE2_NO_LENIV; |
1235 | 5.31k | if (pfont->FontType != ft_encrypted2) |
1236 | 5.12k | pfont->data.defaultWidthX = pfont->data.nominalWidthX = 0; |
1237 | 5.31k | } |
1238 | 5.31k | writer.options = options; |
1239 | 5.31k | s_init(&poss, NULL); |
1240 | 5.31k | swrite_position_only(&poss); |
1241 | 5.31k | writer.strm = &poss; |
1242 | 5.31k | writer.pfont = pbfont; |
1243 | 5.31k | writer.glyph_data = psf_type1_glyph_data; |
1244 | 5.31k | writer.offset_size = 1; /* arbitrary */ |
1245 | 5.31k | writer.start_pos = stell(s); |
1246 | 5.31k | writer.FontBBox = *FontBBox; |
1247 | | |
1248 | | /* Initialize the enumeration of the glyphs. */ |
1249 | 5.31k | psf_enumerate_glyphs_begin(&genum, (gs_font *)pfont, |
1250 | 5.31k | subset.glyphs.subset_glyphs, |
1251 | 5.31k | (subset.glyphs.subset_glyphs ? |
1252 | 5.31k | subset.glyphs.subset_size : 0), |
1253 | 5.31k | GLYPH_SPACE_NAME); |
1254 | | |
1255 | | /* Shuffle the glyphs into the order .notdef, encoded, unencoded. */ |
1256 | 5.31k | { |
1257 | 5.31k | gs_glyph encoded[256]; |
1258 | 5.31k | int num_enc, num_enc_chars; |
1259 | | |
1260 | | /* Get the list of encoded glyphs. */ |
1261 | 1.36M | for (j = 0, num_enc_chars = 0; j < 256; ++j) { |
1262 | 1.35M | glyph = pfont->procs.encode_char((gs_font *)pfont, (gs_char)j, |
1263 | 1.35M | GLYPH_SPACE_NAME); |
1264 | 1.35M | if (glyph != GS_NO_GLYPH && glyph != subset.glyphs.notdef && |
1265 | 1.35M | (subset.glyphs.subset_glyphs == 0 || |
1266 | 97.3k | psf_sorted_glyphs_include(subset.glyphs.subset_data, |
1267 | 0 | subset.glyphs.subset_size, glyph))) |
1268 | 97.3k | encoded[num_enc_chars++] = glyph; |
1269 | 1.35M | } |
1270 | 5.31k | subset.num_encoded_chars = num_enc_chars; |
1271 | 5.31k | subset.num_encoded = num_enc = |
1272 | 5.31k | psf_sort_glyphs(encoded, num_enc_chars); |
1273 | | |
1274 | | /* Get the complete list of glyphs if we don't have it already. */ |
1275 | 5.31k | if (!subset.glyphs.subset_glyphs) { |
1276 | 5.31k | int num_glyphs = 0; |
1277 | | |
1278 | 5.31k | psf_enumerate_glyphs_reset(&genum); |
1279 | 107k | while ((code = psf_enumerate_glyphs_next(&genum, &glyph)) != 1) |
1280 | 102k | if (code == 0) { |
1281 | 102k | if (num_glyphs == number_of_glyphs){ |
1282 | 0 | code = gs_note_error(gs_error_limitcheck); |
1283 | 0 | goto error; |
1284 | 0 | } |
1285 | 102k | subset.glyphs.subset_data[num_glyphs++] = glyph; |
1286 | 102k | } |
1287 | 5.31k | subset.glyphs.subset_size = |
1288 | 5.31k | psf_sort_glyphs(subset.glyphs.subset_data, num_glyphs); |
1289 | 5.31k | subset.glyphs.subset_glyphs = subset.glyphs.subset_data; |
1290 | 5.31k | } |
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 | 5.31k | { |
1298 | 5.31k | int from = subset.glyphs.subset_size; |
1299 | 5.31k | int to = from; |
1300 | | |
1301 | 107k | while (from > 0) { |
1302 | 102k | glyph = subset.glyphs.subset_data[--from]; |
1303 | 102k | if (glyph != subset.glyphs.notdef && |
1304 | 102k | !psf_sorted_glyphs_include(encoded, num_enc, glyph)) |
1305 | 0 | subset.glyphs.subset_data[--to] = glyph; |
1306 | 102k | } |
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 | 5.31k | } |
1312 | | |
1313 | | /* Move .notdef and the encoded glyphs to the bottom of the list. */ |
1314 | 5.31k | subset.glyphs.subset_data[0] = subset.glyphs.notdef; |
1315 | 5.31k | memcpy(subset.glyphs.subset_data + 1, encoded, |
1316 | 5.31k | sizeof(encoded[0]) * num_enc); |
1317 | 5.31k | } |
1318 | | |
1319 | | /* Set the font name. */ |
1320 | 5.31k | if (alt_font_name) |
1321 | 5.31k | 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 | 5.31k | cff_string_table_init(&writer.std_strings, std_string_items, |
1328 | 5.31k | MAX_CFF_STD_STRINGS); |
1329 | 2.08M | for (j = 0; (glyph = gs_c_known_encode((gs_char)j, |
1330 | 2.08M | ENCODING_INDEX_CFFSTRINGS)) != GS_NO_GLYPH; |
1331 | 2.07M | ++j) { |
1332 | 2.07M | gs_const_string str; |
1333 | 2.07M | int ignore; |
1334 | | |
1335 | 2.07M | gs_c_glyph_name(glyph, &str); |
1336 | 2.07M | cff_string_index(&writer.std_strings, str.data, str.size, true, |
1337 | 2.07M | &ignore); |
1338 | 2.07M | } |
1339 | 5.31k | cff_string_table_init(&writer.strings, string_items, number_of_strings); |
1340 | | |
1341 | | /* Enter miscellaneous strings in the string table. */ |
1342 | 5.31k | 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 | 5.31k | psf_enumerate_glyphs_begin(&genum, (gs_font *)pfont, |
1347 | 5.31k | subset.glyphs.subset_data, |
1348 | 5.31k | subset.glyphs.subset_size, |
1349 | 5.31k | GLYPH_SPACE_NAME); |
1350 | 107k | while ((code = psf_enumerate_glyphs_next(&genum, &glyph)) != 1) |
1351 | 102k | if (code == 0) { |
1352 | 102k | code = cff_glyph_sid(&writer, glyph); |
1353 | 102k | if (code == gs_error_undefined) |
1354 | 0 | continue; |
1355 | 102k | if (code < 0) |
1356 | 0 | goto error; |
1357 | 102k | charset_size += 2; |
1358 | 102k | } |
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 | 5.31k | if ((options & WRITE_TYPE2_NO_GSUBRS) != 0 || |
1371 | 5.31k | cff_convert_charstrings(&writer, pbfont) /* we expand all Subrs */ |
1372 | 5.31k | ) |
1373 | 5.12k | gsubrs_count = 0, gsubrs_size = 0; |
1374 | 188 | else |
1375 | 188 | gsubrs_size = cff_write_Subrs_offsets(&writer, &gsubrs_count, |
1376 | 188 | 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 | 5.31k | encoding_size = cff_Encoding_size(&writer, &subset); |
1385 | | |
1386 | | /* Compute the size of the CharStrings Index. */ |
1387 | 5.31k | code = cff_write_CharStrings_offsets(&writer, &genum, &charstrings_count); |
1388 | 5.31k | if (code < 0) |
1389 | 30 | goto error; |
1390 | 5.28k | charstrings_size = (uint)code; |
1391 | | |
1392 | | /* Compute the size of the (local) Subrs Index. */ |
1393 | 5.28k | #ifdef SKIP_EMPTY_SUBRS |
1394 | 5.28k | subrs_size = |
1395 | 5.28k | (cff_convert_charstrings(&writer, pbfont) ? 0 : |
1396 | 5.28k | 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 | 15.9k | iter: |
1417 | 15.9k | swrite_position_only(&poss); |
1418 | 15.9k | writer.strm = &poss; |
1419 | | |
1420 | | /* Compute the offsets. */ |
1421 | 15.9k | GSubrs_offset = 4 + cff_Index_size(1, font_name.size) + |
1422 | 15.9k | cff_Index_size(1, Top_size) + |
1423 | 15.9k | cff_Index_size(writer.strings.count, writer.strings.total); |
1424 | 15.9k | Encoding_offset = GSubrs_offset + |
1425 | 15.9k | cff_Index_size(gsubrs_count, gsubrs_size); |
1426 | 15.9k | charset_offset = Encoding_offset + encoding_size; |
1427 | 15.9k | CharStrings_offset = charset_offset + charset_size; |
1428 | 15.9k | Private_offset = CharStrings_offset + |
1429 | 15.9k | cff_Index_size(charstrings_count, charstrings_size); |
1430 | 15.9k | Subrs_offset = Private_size; /* relative to Private Dict */ |
1431 | | |
1432 | 21.1k | write: |
1433 | 21.1k | if(check_ioerror(writer.strm)) { |
1434 | 0 | code = gs_note_error(gs_error_ioerror); |
1435 | 0 | goto error; |
1436 | 0 | } |
1437 | 21.1k | start_pos = stell(writer.strm); |
1438 | | /* Write the header, setting offset_size. */ |
1439 | 21.1k | cff_write_header(&writer, End_offset); |
1440 | | |
1441 | | /* Write the names Index. */ |
1442 | 21.1k | cff_put_Index_header(&writer, 1, font_name.size); |
1443 | 21.1k | put_offset(&writer, font_name.size + 1); |
1444 | 21.1k | put_bytes(writer.strm, font_name.data, font_name.size); |
1445 | | |
1446 | | /* Write the Top Index. */ |
1447 | 21.1k | cff_put_Index_header(&writer, 1, Top_size); |
1448 | 21.1k | put_offset(&writer, Top_size + 1); |
1449 | 21.1k | offset = stell(writer.strm) - start_pos; |
1450 | 21.1k | cff_write_Top_font(&writer, Encoding_offset, charset_offset, |
1451 | 21.1k | CharStrings_offset, |
1452 | 21.1k | Private_offset, Private_size); |
1453 | 21.1k | Top_size = stell(writer.strm) - start_pos - offset; |
1454 | | |
1455 | | /* Write the strings Index. */ |
1456 | 21.1k | cff_put_Index(&writer, &writer.strings); |
1457 | 21.1k | 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 | 21.1k | offset = stell(writer.strm) - start_pos; |
1464 | 21.1k | if_debug2m('l', s->memory, "[l]GSubrs = %u => %u\n", GSubrs_offset, offset); |
1465 | 21.1k | if (offset > GSubrs_offset) { |
1466 | 0 | code = gs_note_error(gs_error_rangecheck); |
1467 | 0 | goto error; |
1468 | 0 | } |
1469 | 21.1k | GSubrs_offset = offset; |
1470 | 21.1k | if (gsubrs_count == 0 || cff_convert_charstrings(&writer, pbfont)) |
1471 | 21.1k | cff_put_Index_header(&writer, 0, 0); |
1472 | 0 | else |
1473 | 0 | cff_write_Subrs(&writer, gsubrs_count, gsubrs_size, pfont, true); |
1474 | | |
1475 | | /* Write the Encoding. */ |
1476 | 21.1k | cff_write_Encoding(&writer, &subset); |
1477 | | |
1478 | | /* Write the charset. */ |
1479 | 21.1k | cff_write_charset(&writer, &subset); |
1480 | | |
1481 | | /* Write the CharStrings Index, checking the offset. */ |
1482 | 21.1k | offset = stell(writer.strm) - start_pos; |
1483 | 21.1k | if (offset > CharStrings_offset) { |
1484 | 0 | code = gs_note_error(gs_error_rangecheck); |
1485 | 0 | goto error; |
1486 | 0 | } |
1487 | 21.1k | CharStrings_offset = offset; |
1488 | 21.1k | cff_write_CharStrings(&writer, &genum, charstrings_count, |
1489 | 21.1k | charstrings_size); |
1490 | 21.1k | 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 | 21.1k | offset = stell(writer.strm) - start_pos; |
1497 | 21.1k | if (offset > Private_offset) { |
1498 | 0 | code = gs_note_error(gs_error_rangecheck); |
1499 | 0 | goto error; |
1500 | 0 | } |
1501 | 21.1k | Private_offset = offset; |
1502 | 21.1k | cff_write_Private(&writer, (subrs_size == 0 ? 0 : Subrs_offset), pfont); |
1503 | 21.1k | Private_size = stell(writer.strm) - start_pos - offset; |
1504 | | |
1505 | | /* Write the Subrs Index, checking the offset. */ |
1506 | 21.1k | offset = stell(writer.strm) - (start_pos + Private_offset); |
1507 | 21.1k | if (offset > Subrs_offset) { |
1508 | 0 | code = gs_note_error(gs_error_rangecheck); |
1509 | 0 | goto error; |
1510 | 0 | } |
1511 | 21.1k | Subrs_offset = offset; |
1512 | 21.1k | if (cff_convert_charstrings(&writer, pbfont)) |
1513 | 20.4k | cff_put_Index_header(&writer, 0, 0); |
1514 | 781 | else if (subrs_size != 0) |
1515 | 0 | cff_write_Subrs(&writer, subrs_count, subrs_size, pfont, false); |
1516 | | |
1517 | | /* Check the final offset. */ |
1518 | 21.1k | if(check_ioerror(writer.strm)) { |
1519 | 0 | code = gs_note_error(gs_error_ioerror); |
1520 | 0 | goto error; |
1521 | 0 | } |
1522 | 21.1k | offset = stell(writer.strm) - start_pos; |
1523 | 21.1k | if (offset > End_offset) { |
1524 | 0 | code = gs_note_error(gs_error_rangecheck); |
1525 | 0 | goto error; |
1526 | 0 | } |
1527 | 21.1k | if (offset == End_offset) { |
1528 | | /* The iteration has converged. Write the result. */ |
1529 | 10.5k | if (writer.strm == &poss) { |
1530 | 5.28k | writer.strm = s; |
1531 | 5.28k | goto write; |
1532 | 5.28k | } |
1533 | 10.6k | } else { |
1534 | | /* No convergence yet. */ |
1535 | 10.6k | End_offset = offset; |
1536 | 10.6k | goto iter; |
1537 | 10.6k | } |
1538 | | |
1539 | | /* All done. */ |
1540 | 5.28k | gs_free_object(pfont->memory, std_string_items, "psf_write_type2_font"); |
1541 | 5.28k | gs_free_object(pfont->memory, subset.glyphs.subset_data, "psf_write_type2_font"); |
1542 | 5.28k | return 0; |
1543 | | |
1544 | 30 | error: |
1545 | 30 | gs_free_object(pfont->memory, std_string_items, "psf_write_type2_font"); |
1546 | 30 | gs_free_object(pfont->memory, subset.glyphs.subset_data, "psf_write_type2_font"); |
1547 | 30 | subset.glyphs.subset_data = NULL; |
1548 | 30 | return code; |
1549 | 21.1k | } |
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 | 1.27k | { |
1556 | 1.27k | gs_font_cid0 *const pfont = (gs_font_cid0 *)pbfont; |
1557 | 1.27k | int font_index; |
1558 | 1.27k | int code = pfont->cidata.glyph_data(pbfont, glyph, pgd, &font_index); |
1559 | | |
1560 | 1.27k | if (code >= 0) |
1561 | 1.27k | *ppfont = pfont->cidata.FDArray[font_index]; |
1562 | 1.27k | return code; |
1563 | 1.27k | } |
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 | 21 | { |
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 | 21 | gs_font_base *const pbfont = (gs_font_base *)pfont; |
1593 | 21 | cff_writer_t writer; |
1594 | 21 | cff_string_item_t std_string_items[500]; /* 391 entries used */ |
1595 | | /****** HOW TO DETERMINE THE SIZE OF STRINGS? ******/ |
1596 | 21 | cff_string_item_t string_items[500 /* character names */ + |
1597 | 21 | 40 /* misc. values */]; |
1598 | 21 | gs_const_string font_name; |
1599 | 21 | stream poss; |
1600 | 21 | uint charstrings_count, charstrings_size; |
1601 | 21 | uint gsubrs_count, gsubrs_size; |
1602 | 21 | uint charset_size, fdselect_size, fdselect_format; |
1603 | 21 | uint subrs_count[256], subrs_size[256]; |
1604 | | /* |
1605 | | * Set the offsets and sizes to the largest reasonable values |
1606 | | * (see below). |
1607 | | */ |
1608 | 21 | uint |
1609 | 21 | Top_size = 0x7fffff, |
1610 | 21 | GSubrs_offset = 0x1ffffff, |
1611 | 21 | charset_offset = 0x1ffffff, |
1612 | 21 | FDSelect_offset = 0x1ffffff, |
1613 | 21 | CharStrings_offset = 0x1ffffff, |
1614 | 21 | Font_offset = 0x1ffffff, |
1615 | 21 | FDArray_offsets[257], |
1616 | 21 | Private_offsets[257], |
1617 | 21 | Subrs_offsets[257], |
1618 | 21 | End_offset = 0x1ffffff; |
1619 | 21 | int j; |
1620 | 21 | psf_glyph_enum_t genum; |
1621 | 21 | gs_font_info_t info; |
1622 | 21 | long start_pos; |
1623 | 21 | uint offset; |
1624 | 21 | int num_fonts = pfont->cidata.FDArray_size; |
1625 | 21 | int code; |
1626 | | |
1627 | 21 | if (num_fonts > 256) |
1628 | 0 | return_error(gs_error_invalidfont); |
1629 | | |
1630 | 21 | memset(&subrs_count, 0x00, 256 * sizeof(uint)); |
1631 | 21 | memset(&subrs_size, 0x00, 256 * sizeof(uint)); |
1632 | | |
1633 | | /* Initialize the enumeration of the glyphs. */ |
1634 | 21 | psf_enumerate_cids_begin(&genum, (gs_font *)pfont, subset_cids, |
1635 | 21 | subset_size); |
1636 | | |
1637 | | /* Check that the font can be written. */ |
1638 | 21 | code = psf_check_outline_glyphs((gs_font_base *)pfont, &genum, |
1639 | 21 | cid0_glyph_data); |
1640 | 21 | if (code < 0) |
1641 | 0 | return code; |
1642 | | /* The .notdef glyph (glyph 0) must be included. */ |
1643 | 21 | if (subset_cids && subset_size > 0 && !(subset_cids[0] & 0x80)) |
1644 | 0 | return_error(gs_error_rangecheck); |
1645 | | |
1646 | 21 | writer.options = options; |
1647 | 21 | s_init(&poss, NULL); |
1648 | 21 | swrite_position_only(&poss); |
1649 | 21 | writer.strm = &poss; |
1650 | 21 | writer.pfont = pbfont; |
1651 | 21 | writer.glyph_data = cid0_glyph_data; |
1652 | 21 | writer.offset_size = 1; /* arbitrary */ |
1653 | 21 | writer.start_pos = stell(s); |
1654 | 21 | writer.FontBBox.p.x = writer.FontBBox.p.y = 0; |
1655 | 21 | writer.FontBBox.q.x = writer.FontBBox.q.y = 0; |
1656 | | |
1657 | | /* Set the font name. */ |
1658 | 21 | if (alt_font_name) |
1659 | 21 | 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 | 21 | cff_string_table_init(&writer.std_strings, std_string_items, |
1669 | 21 | countof(std_string_items)); |
1670 | 21 | cff_string_table_init(&writer.strings, string_items, |
1671 | 21 | countof(string_items)); |
1672 | | |
1673 | | /* Make all entries in the string table. */ |
1674 | 21 | cff_write_ROS(&writer, &pfont->cidata.common.CIDSystemInfo); |
1675 | 59 | for (j = 0; j < num_fonts; ++j) { |
1676 | 38 | gs_font_type1 *pfd = pfont->cidata.FDArray[j]; |
1677 | | |
1678 | 38 | cff_write_Top_fdarray(&writer, (gs_font_base *)pfd, 0, 0); |
1679 | 38 | } |
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 | 80 | for (j = 0; j <= num_fonts; ++j) |
1690 | 59 | FDArray_offsets[j] = Private_offsets[j] = Subrs_offsets[j] = |
1691 | 59 | 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 | 21 | if ((options & WRITE_TYPE2_NO_GSUBRS) != 0 || |
1699 | 21 | cff_convert_charstrings(&writer, |
1700 | 21 | (const gs_font_base *)pfont->cidata.FDArray[0]) |
1701 | | /* we expand all Subrs */ |
1702 | 21 | ) |
1703 | 0 | gsubrs_count = 0, gsubrs_size = 0; |
1704 | 21 | else |
1705 | 21 | gsubrs_size = cff_write_Subrs_offsets(&writer, &gsubrs_count, |
1706 | 21 | 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 | 21 | swrite_position_only(&poss); |
1713 | 21 | cff_write_cidset(&writer, &genum); |
1714 | 21 | charset_size = stell(&poss); |
1715 | | |
1716 | | /* Compute the size of the FDSelect strucure. */ |
1717 | 21 | 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 | 21 | code = cff_write_CharStrings_offsets(&writer, &genum, &charstrings_count); |
1722 | 21 | if (code < 0) |
1723 | 0 | return code; |
1724 | 21 | charstrings_size = (uint)code; |
1725 | | |
1726 | | /* Compute the size of the (local) Subrs Indexes. */ |
1727 | 59 | for (j = 0; j < num_fonts; ++j) { |
1728 | 38 | gs_font_type1 *pfd = pfont->cidata.FDArray[j]; |
1729 | | |
1730 | 38 | #ifdef SKIP_EMPTY_SUBRS |
1731 | 38 | subrs_size[j] = |
1732 | 38 | (cff_convert_charstrings(&writer, (gs_font_base *)pfd) ? 0 : |
1733 | 38 | 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 | 38 | } |
1740 | | |
1741 | | /* Get the font_info once, since it may be expensive. */ |
1742 | 21 | 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 | 63 | iter: |
1758 | 63 | swrite_position_only(&poss); |
1759 | 63 | writer.strm = &poss; |
1760 | | |
1761 | | /* Compute the offsets. */ |
1762 | 63 | GSubrs_offset = 4 + cff_Index_size(1, font_name.size) + |
1763 | 63 | cff_Index_size(1, Top_size) + |
1764 | 63 | cff_Index_size(writer.strings.count, writer.strings.total); |
1765 | 63 | charset_offset = GSubrs_offset + |
1766 | 63 | cff_Index_size(gsubrs_count, gsubrs_size); |
1767 | 63 | FDSelect_offset = charset_offset + charset_size; |
1768 | 63 | CharStrings_offset = FDSelect_offset + fdselect_size; |
1769 | 63 | if_debug4m('l', s->memory, |
1770 | 63 | "[l]GSubrs at %u, charset at %u, FDSelect at %u, CharStrings at %u\n", |
1771 | 63 | GSubrs_offset, charset_offset, FDSelect_offset, CharStrings_offset); |
1772 | | |
1773 | 84 | write: |
1774 | 84 | start_pos = stell(writer.strm); |
1775 | 84 | if_debug1m('l', s->memory, "[l]start_pos = %ld\n", start_pos); |
1776 | | /* Write the header, setting offset_size. */ |
1777 | 84 | cff_write_header(&writer, End_offset); |
1778 | | |
1779 | | /* Write the names Index. */ |
1780 | 84 | cff_put_Index_header(&writer, 1, font_name.size); |
1781 | 84 | put_offset(&writer, font_name.size + 1); |
1782 | 84 | put_bytes(writer.strm, font_name.data, font_name.size); |
1783 | | |
1784 | | /* Write the Top Index. */ |
1785 | 84 | cff_put_Index_header(&writer, 1, Top_size); |
1786 | 84 | put_offset(&writer, Top_size + 1); |
1787 | 84 | offset = stell(writer.strm) - start_pos; |
1788 | 84 | cff_write_Top_cidfont(&writer, charset_offset, CharStrings_offset, |
1789 | 84 | FDSelect_offset, Font_offset, &info); |
1790 | 84 | Top_size = stell(writer.strm) - start_pos - offset; |
1791 | 84 | if_debug1m('l', s->memory, "[l]Top_size = %u\n", Top_size); |
1792 | | |
1793 | | /* Write the strings Index. */ |
1794 | 84 | cff_put_Index(&writer, &writer.strings); |
1795 | | |
1796 | | /* Write the GSubrs Index, if any, checking the offset. */ |
1797 | 84 | offset = stell(writer.strm) - start_pos; |
1798 | 84 | if_debug2m('l', s->memory, "[l]GSubrs = %u => %u\n", GSubrs_offset, offset); |
1799 | 84 | if (offset > GSubrs_offset) |
1800 | 0 | return_error(gs_error_rangecheck); |
1801 | 84 | GSubrs_offset = offset; |
1802 | 84 | if (gsubrs_count == 0 || |
1803 | 84 | cff_convert_charstrings(&writer, |
1804 | 0 | (const gs_font_base *)pfont->cidata.FDArray[0]) |
1805 | 84 | ) |
1806 | 84 | cff_put_Index_header(&writer, 0, 0); |
1807 | 0 | else |
1808 | 0 | cff_write_Subrs(&writer, gsubrs_count, gsubrs_size, |
1809 | 0 | pfont->cidata.FDArray[0], true); |
1810 | | |
1811 | | /* Write the charset. */ |
1812 | 84 | if_debug1m('l', s->memory, "[l]charset = %"PRId64"\n", (int64_t)(stell(writer.strm) - start_pos)); |
1813 | 84 | cff_write_cidset(&writer, &genum); |
1814 | | |
1815 | | /* Write the FDSelect structure, checking the offset. */ |
1816 | 84 | offset = stell(writer.strm) - start_pos; |
1817 | 84 | if_debug2m('l', s->memory, "[l]FDSelect = %u => %u\n", FDSelect_offset, offset); |
1818 | 84 | if (offset > FDSelect_offset) |
1819 | 0 | return_error(offset_error("FDselect")); |
1820 | 84 | FDSelect_offset = offset; |
1821 | 84 | cff_write_FDSelect(&writer, &genum, fdselect_size, fdselect_format); |
1822 | | |
1823 | | /* Write the CharStrings Index, checking the offset. */ |
1824 | 84 | offset = stell(writer.strm) - start_pos; |
1825 | 84 | if_debug2m('l', s->memory, "[l]CharStrings = %u => %u\n", CharStrings_offset, offset); |
1826 | 84 | if (offset > CharStrings_offset) |
1827 | 0 | return_error(offset_error("CharStrings")); |
1828 | 84 | CharStrings_offset = offset; |
1829 | 84 | cff_write_CharStrings(&writer, &genum, charstrings_count, |
1830 | 84 | charstrings_size); |
1831 | | |
1832 | | /* Write the Font Dict Index. */ |
1833 | 84 | offset = stell(writer.strm) - start_pos; |
1834 | 84 | if_debug2m('l', s->memory, "[l]Font = %u => %u\n", Font_offset, offset); |
1835 | 84 | if (offset > Font_offset) |
1836 | 0 | return_error(offset_error("Font")); |
1837 | 84 | Font_offset = offset; |
1838 | 84 | cff_write_FDArray_offsets(&writer, FDArray_offsets, num_fonts); |
1839 | 84 | offset = stell(writer.strm) - start_pos; |
1840 | 84 | if_debug2m('l', s->memory, "[l]FDArray[0] = %u => %u\n", FDArray_offsets[0], offset); |
1841 | 84 | if (offset > FDArray_offsets[0]) |
1842 | 0 | return_error(offset_error("FDArray[0]")); |
1843 | 84 | FDArray_offsets[0] = offset; |
1844 | 236 | for (j = 0; j < num_fonts; ++j) { |
1845 | 152 | gs_font_type1 *pfd = pfont->cidata.FDArray[j]; |
1846 | | |
1847 | | /* If we're writing Type 2 CharStrings, don't encrypt them. */ |
1848 | 152 | if (options & WRITE_TYPE2_CHARSTRINGS) { |
1849 | 152 | options |= WRITE_TYPE2_NO_LENIV; |
1850 | 152 | if (pfd->FontType != ft_encrypted2) |
1851 | 0 | pfd->data.defaultWidthX = pfd->data.nominalWidthX = 0; |
1852 | 152 | } |
1853 | 152 | cff_write_Top_fdarray(&writer, (gs_font_base *)pfd, Private_offsets[j], |
1854 | 152 | Private_offsets[j + 1] - Private_offsets[j]); |
1855 | 152 | offset = stell(writer.strm) - start_pos; |
1856 | 152 | if_debug3m('l', s->memory, "[l]FDArray[%d] = %u => %u\n", j + 1, |
1857 | 152 | FDArray_offsets[j + 1], offset); |
1858 | 152 | if (offset > FDArray_offsets[j + 1]) |
1859 | 0 | return_error(offset_error("FDArray")); |
1860 | 152 | FDArray_offsets[j + 1] = offset; |
1861 | 152 | } |
1862 | | |
1863 | | /* Write the Private Dicts, checking the offset. */ |
1864 | 236 | for (j = 0; ; ++j) { |
1865 | 236 | gs_font_type1 *pfd; |
1866 | | |
1867 | 236 | offset = stell(writer.strm) - start_pos; |
1868 | 236 | if_debug3m('l', s->memory, "[l]Private[%d] = %u => %u\n", |
1869 | 236 | j, Private_offsets[j], offset); |
1870 | 236 | if (offset > Private_offsets[j]) |
1871 | 0 | return_error(offset_error("Private")); |
1872 | 236 | Private_offsets[j] = offset; |
1873 | 236 | if (j == num_fonts) |
1874 | 84 | break; |
1875 | 152 | pfd = pfont->cidata.FDArray[j]; |
1876 | 152 | cff_write_Private(&writer, |
1877 | 152 | (subrs_size[j] == 0 ? 0 : Subrs_offsets[j]), pfd); |
1878 | 152 | } |
1879 | | |
1880 | | /* Write the Subrs Indexes, checking the offsets. */ |
1881 | 236 | for (j = 0; ; ++j) { |
1882 | 236 | gs_font_type1 *pfd; |
1883 | | |
1884 | 236 | offset = stell(writer.strm) - (start_pos + Private_offsets[j]); |
1885 | 236 | if_debug3m('l', s->memory, "[l]Subrs[%d] = %u => %u\n", |
1886 | 236 | j, Subrs_offsets[j], offset); |
1887 | 236 | if (offset > Subrs_offsets[j]) |
1888 | 0 | return_error(offset_error("Subrs")); |
1889 | 236 | Subrs_offsets[j] = offset; |
1890 | 236 | if (j == num_fonts) |
1891 | 84 | break; |
1892 | 152 | pfd = pfont->cidata.FDArray[j]; |
1893 | 152 | if (cff_convert_charstrings(&writer, (gs_font_base *)pfd)) |
1894 | 0 | cff_put_Index_header(&writer, 0, 0); |
1895 | 152 | else if (subrs_size[j] != 0) |
1896 | 0 | cff_write_Subrs(&writer, subrs_count[j], subrs_size[j], pfd, false); |
1897 | 152 | } |
1898 | | |
1899 | | /* Check the final offset. */ |
1900 | 84 | offset = stell(writer.strm) - start_pos; |
1901 | 84 | if_debug2m('l', s->memory, "[l]End = %u => %u\n", End_offset, offset); |
1902 | 84 | if (offset > End_offset) |
1903 | 0 | return_error(offset_error("End")); |
1904 | 84 | if (offset == End_offset) { |
1905 | | /* The iteration has converged. Write the result. */ |
1906 | 42 | if (writer.strm == &poss) { |
1907 | 21 | writer.strm = s; |
1908 | 21 | goto write; |
1909 | 21 | } |
1910 | 42 | } else { |
1911 | | /* No convergence yet. */ |
1912 | 42 | End_offset = offset; |
1913 | 42 | goto iter; |
1914 | 42 | } |
1915 | | |
1916 | | /* All done. */ |
1917 | 21 | return 0; |
1918 | 84 | } |