/src/qtbase/src/3rdparty/harfbuzz-ng/src/hb-subset-cff-common.hh
Line | Count | Source |
1 | | /* |
2 | | * Copyright © 2018 Adobe Inc. |
3 | | * |
4 | | * This is part of HarfBuzz, a text shaping library. |
5 | | * |
6 | | * Permission is hereby granted, without written agreement and without |
7 | | * license or royalty fees, to use, copy, modify, and distribute this |
8 | | * software and its documentation for any purpose, provided that the |
9 | | * above copyright notice and the following two paragraphs appear in |
10 | | * all copies of this software. |
11 | | * |
12 | | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
13 | | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
14 | | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
15 | | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
16 | | * DAMAGE. |
17 | | * |
18 | | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
19 | | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
20 | | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
21 | | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
22 | | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
23 | | * |
24 | | * Adobe Author(s): Michiharu Ariza |
25 | | */ |
26 | | |
27 | | #ifndef HB_SUBSET_CFF_COMMON_HH |
28 | | #define HB_SUBSET_CFF_COMMON_HH |
29 | | |
30 | | #include "hb.hh" |
31 | | |
32 | | #include "hb-subset-plan.hh" |
33 | | #include "hb-cff-interp-cs-common.hh" |
34 | | |
35 | | namespace CFF { |
36 | | |
37 | | /* Used for writing a temporary charstring */ |
38 | | struct str_encoder_t |
39 | | { |
40 | | str_encoder_t (str_buff_t &buff_) |
41 | 0 | : buff (buff_) {} |
42 | | |
43 | 0 | void reset () { buff.reset (); } |
44 | | |
45 | | void encode_byte (unsigned char b) |
46 | 0 | { |
47 | 0 | if (likely ((signed) buff.length < buff.allocated)) |
48 | 0 | buff.arrayZ[buff.length++] = b; |
49 | 0 | else |
50 | 0 | buff.push (b); |
51 | 0 | } |
52 | | |
53 | | void encode_int (int v) |
54 | 0 | { |
55 | 0 | if ((-1131 <= v) && (v <= 1131)) |
56 | 0 | { |
57 | 0 | if ((-107 <= v) && (v <= 107)) |
58 | 0 | encode_byte (v + 139); |
59 | 0 | else if (v > 0) |
60 | 0 | { |
61 | 0 | v -= 108; |
62 | 0 | encode_byte ((v >> 8) + OpCode_TwoBytePosInt0); |
63 | 0 | encode_byte (v & 0xFF); |
64 | 0 | } |
65 | 0 | else |
66 | 0 | { |
67 | 0 | v = -v - 108; |
68 | 0 | encode_byte ((v >> 8) + OpCode_TwoByteNegInt0); |
69 | 0 | encode_byte (v & 0xFF); |
70 | 0 | } |
71 | 0 | } |
72 | 0 | else |
73 | 0 | { |
74 | 0 | if (unlikely (v < -32768)) |
75 | 0 | v = -32768; |
76 | 0 | else if (unlikely (v > 32767)) |
77 | 0 | v = 32767; |
78 | 0 | encode_byte (OpCode_shortint); |
79 | 0 | encode_byte ((v >> 8) & 0xFF); |
80 | 0 | encode_byte (v & 0xFF); |
81 | 0 | } |
82 | 0 | } |
83 | | |
84 | | // Encode number for CharString |
85 | | void encode_num_cs (const number_t& n) |
86 | 0 | { |
87 | 0 | if (n.in_int_range ()) |
88 | 0 | { |
89 | 0 | encode_int (n.to_int ()); |
90 | 0 | } |
91 | 0 | else |
92 | 0 | { |
93 | 0 | int32_t v = n.to_fixed (); |
94 | 0 | encode_byte (OpCode_fixedcs); |
95 | 0 | encode_byte ((v >> 24) & 0xFF); |
96 | 0 | encode_byte ((v >> 16) & 0xFF); |
97 | 0 | encode_byte ((v >> 8) & 0xFF); |
98 | 0 | encode_byte (v & 0xFF); |
99 | 0 | } |
100 | 0 | } |
101 | | |
102 | | // Encode number for TopDict / Private |
103 | | void encode_num_tp (const number_t& n) |
104 | 0 | { |
105 | 0 | if (n.in_int_range ()) |
106 | 0 | { |
107 | 0 | // TODO longint |
108 | 0 | encode_int (n.to_int ()); |
109 | 0 | } |
110 | 0 | else |
111 | 0 | { |
112 | 0 | // Sigh. BCD |
113 | 0 | // https://learn.microsoft.com/en-us/typography/opentype/spec/cff2#table-5-nibble-definitions |
114 | 0 | double v = n.to_real (); |
115 | 0 | encode_byte (OpCode_BCD); |
116 | 0 |
|
117 | 0 | // Based on: |
118 | 0 | // https://github.com/fonttools/fonttools/blob/0738c41dfbcbc213ab9263f486ef0cccc6eb5ce5/Lib/fontTools/misc/psCharStrings.py#L267-L316 |
119 | 0 |
|
120 | 0 | char buf[16]; |
121 | 0 | /* FontTools has the following comment: |
122 | 0 | * |
123 | 0 | * # Note: 14 decimal digits seems to be the limitation for CFF real numbers |
124 | 0 | * # in macOS. However, we use 8 here to match the implementation of AFDKO. |
125 | 0 | * |
126 | 0 | * We use 8 here to match FontTools X-). |
127 | 0 | */ |
128 | 0 |
|
129 | 0 | hb_locale_t clocale HB_UNUSED; |
130 | 0 | hb_locale_t oldlocale HB_UNUSED; |
131 | 0 | oldlocale = hb_uselocale (clocale = newlocale (LC_ALL_MASK, "C", NULL)); |
132 | 0 | snprintf (buf, sizeof (buf), "%.8G", v); |
133 | 0 | (void) hb_uselocale (((void) freelocale (clocale), oldlocale)); |
134 | 0 |
|
135 | 0 | char *s = buf; |
136 | 0 | size_t len; |
137 | 0 | char *comma = strchr (s, ','); |
138 | 0 | if (comma) // Comma for some European locales in case no uselocale available. |
139 | 0 | *comma = '.'; |
140 | 0 | if (s[0] == '0' && s[1] == '.') |
141 | 0 | s++; |
142 | 0 | else if (s[0] == '-' && s[1] == '0' && s[2] == '.') |
143 | 0 | { |
144 | 0 | s[1] = '-'; |
145 | 0 | s++; |
146 | 0 | } |
147 | 0 | else if ((len = strlen (s)) > 3 && !strcmp (s + len - 3, "000")) |
148 | 0 | { |
149 | 0 | unsigned exponent = len - 3; |
150 | 0 | char *s2 = s + exponent - 1; |
151 | 0 | while (*s2 == '0' && exponent > 1) |
152 | 0 | { |
153 | 0 | s2--; |
154 | 0 | exponent++; |
155 | 0 | } |
156 | 0 | snprintf (s2 + 1, sizeof (buf) - (s2 + 1 - buf), "E%u", exponent); |
157 | 0 | } |
158 | 0 | else |
159 | 0 | { |
160 | 0 | char *dot = strchr (s, '.'); |
161 | 0 | char *e = strchr (s, 'E'); |
162 | 0 | if (dot && e) |
163 | 0 | { |
164 | 0 | memmove (dot, dot + 1, e - (dot + 1)); |
165 | 0 | int exponent = atoi (e + 1); |
166 | 0 | int new_exponent = exponent - (e - (dot + 1)); |
167 | 0 | if (new_exponent == 1) |
168 | 0 | { |
169 | 0 | e[-1] = '0'; |
170 | 0 | e[0] = '\0'; |
171 | 0 | } |
172 | 0 | else |
173 | 0 | snprintf (e - 1, sizeof (buf) - (e - 1 - buf), "E%d", new_exponent); |
174 | 0 | } |
175 | 0 | } |
176 | 0 | if ((s[0] == '.' && s[1] == '0') || (s[0] == '-' && s[1] == '.' && s[2] == '0')) |
177 | 0 | { |
178 | 0 | int sign = s[0] == '-'; |
179 | 0 | char *s2 = s + sign + 1; |
180 | 0 | while (*s2 == '0') |
181 | 0 | s2++; |
182 | 0 | len = strlen (s2); |
183 | 0 | memmove (s + sign, s2, len); |
184 | 0 | snprintf (s + sign + len, sizeof (buf) - (s + sign + len - buf), "E-%u", (unsigned) (strlen (s + sign) - 1)); |
185 | 0 | } |
186 | 0 | hb_vector_t<char> nibbles; |
187 | 0 | while (*s) |
188 | 0 | { |
189 | 0 | char c = s[0]; |
190 | 0 | s++; |
191 | 0 |
|
192 | 0 | switch (c) |
193 | 0 | { |
194 | 0 | case 'E': |
195 | 0 | { |
196 | 0 | char c2 = *s; |
197 | 0 | if (c2 == '-') |
198 | 0 | { |
199 | 0 | s++; |
200 | 0 | nibbles.push (0x0C); // E- |
201 | 0 | } else { |
202 | 0 | if (c2 == '+') |
203 | 0 | s++; |
204 | 0 | nibbles.push (0x0B); // E |
205 | 0 | } |
206 | 0 | if (*s == '0') |
207 | 0 | s++; |
208 | 0 | continue; |
209 | 0 | } |
210 | 0 |
|
211 | 0 | case '.': |
212 | 0 | nibbles.push (0x0A); // . |
213 | 0 | continue; |
214 | 0 |
|
215 | 0 | case '-': |
216 | 0 | nibbles.push (0x0E); // - |
217 | 0 | continue; |
218 | 0 | } |
219 | 0 |
|
220 | 0 | nibbles.push (c - '0'); |
221 | 0 | } |
222 | 0 | nibbles.push (0x0F); |
223 | 0 | if (nibbles.length % 2) |
224 | 0 | nibbles.push (0x0F); |
225 | 0 |
|
226 | 0 | unsigned count = nibbles.length; |
227 | 0 | for (unsigned i = 0; i < count; i += 2) |
228 | 0 | encode_byte ((nibbles[i] << 4) | nibbles[i+1]); |
229 | 0 | } |
230 | 0 | } |
231 | | |
232 | | void encode_op (op_code_t op) |
233 | 0 | { |
234 | 0 | if (Is_OpCode_ESC (op)) |
235 | 0 | { |
236 | 0 | encode_byte (OpCode_escape); |
237 | 0 | encode_byte (Unmake_OpCode_ESC (op)); |
238 | 0 | } |
239 | 0 | else |
240 | 0 | encode_byte (op); |
241 | 0 | } |
242 | | |
243 | | void copy_str (const unsigned char *str, unsigned length) |
244 | 0 | { |
245 | 0 | assert ((signed) (buff.length + length) <= buff.allocated); |
246 | 0 | hb_memcpy (buff.arrayZ + buff.length, str, length); |
247 | 0 | buff.length += length; |
248 | 0 | } |
249 | | |
250 | 0 | bool in_error () const { return buff.in_error (); } |
251 | | |
252 | | protected: |
253 | | |
254 | | str_buff_t &buff; |
255 | | }; |
256 | | |
257 | | struct cff_sub_table_info_t { |
258 | | cff_sub_table_info_t () |
259 | | : fd_array_link (0), |
260 | | char_strings_link (0) |
261 | 0 | { |
262 | 0 | fd_select.init (); |
263 | 0 | } |
264 | | |
265 | | table_info_t fd_select; |
266 | | objidx_t fd_array_link; |
267 | | objidx_t char_strings_link; |
268 | | }; |
269 | | |
270 | | template <typename OPSTR=op_str_t> |
271 | | struct cff_top_dict_op_serializer_t : op_serializer_t |
272 | | { |
273 | | bool serialize (hb_serialize_context_t *c, |
274 | | const OPSTR &opstr, |
275 | | const cff_sub_table_info_t &info) const |
276 | | { |
277 | | TRACE_SERIALIZE (this); |
278 | | |
279 | | switch (opstr.op) |
280 | | { |
281 | | case OpCode_CharStrings: |
282 | | return_trace (FontDict::serialize_link4_op(c, opstr.op, info.char_strings_link, whence_t::Absolute)); |
283 | | |
284 | | case OpCode_FDArray: |
285 | | return_trace (FontDict::serialize_link4_op(c, opstr.op, info.fd_array_link, whence_t::Absolute)); |
286 | | |
287 | | case OpCode_FDSelect: |
288 | | return_trace (FontDict::serialize_link4_op(c, opstr.op, info.fd_select.link, whence_t::Absolute)); |
289 | | |
290 | | default: |
291 | | return_trace (copy_opstr (c, opstr)); |
292 | | } |
293 | | return_trace (true); |
294 | | } |
295 | | }; |
296 | | |
297 | | struct cff_font_dict_op_serializer_t : op_serializer_t |
298 | | { |
299 | | bool serialize (hb_serialize_context_t *c, |
300 | | const op_str_t &opstr, |
301 | | const table_info_t &privateDictInfo) const |
302 | 0 | { |
303 | 0 | TRACE_SERIALIZE (this); |
304 | 0 |
|
305 | 0 | if (opstr.op == OpCode_Private) |
306 | 0 | { |
307 | 0 | /* serialize the private dict size & offset as 2-byte & 4-byte integers */ |
308 | 0 | return_trace (UnsizedByteStr::serialize_int2 (c, privateDictInfo.size) && |
309 | 0 | Dict::serialize_link4_op (c, opstr.op, privateDictInfo.link, whence_t::Absolute)); |
310 | 0 | } |
311 | 0 | else |
312 | 0 | { |
313 | 0 | unsigned char *d = c->allocate_size<unsigned char> (opstr.length); |
314 | 0 | if (unlikely (!d)) return_trace (false); |
315 | 0 | /* Faster than hb_memcpy for small strings. */ |
316 | 0 | for (unsigned i = 0; i < opstr.length; i++) |
317 | 0 | d[i] = opstr.ptr[i]; |
318 | 0 | //hb_memcpy (d, opstr.ptr, opstr.length); |
319 | 0 | } |
320 | 0 | return_trace (true); |
321 | 0 | } |
322 | | }; |
323 | | |
324 | | /* CharString command for specialization */ |
325 | | struct cs_command_t |
326 | | { |
327 | | hb_vector_t<number_t> args; |
328 | | hb_vector_t<unsigned char> mask_bytes; /* For hintmask/cntrmask payload bytes. */ |
329 | | op_code_t op; |
330 | | |
331 | 0 | cs_command_t () : op (OpCode_Invalid) {} |
332 | 0 | cs_command_t (op_code_t op_) : op (op_) {} |
333 | | }; |
334 | | |
335 | | typedef hb_vector_t<cs_command_t> *cs_command_vec_t; |
336 | | |
337 | | struct cff2_instancing_plan_t; |
338 | | |
339 | | struct flatten_param_t |
340 | | { |
341 | | flatten_param_t (str_buff_t &flatStr_, |
342 | | bool drop_hints_, |
343 | | const hb_subset_plan_t *plan_, |
344 | | cs_command_vec_t commands_ = nullptr) |
345 | 0 | : flatStr (flatStr_), drop_hints (drop_hints_), plan (plan_), commands (commands_) {} |
346 | | |
347 | | str_buff_t &flatStr; |
348 | | bool drop_hints; |
349 | | const hb_subset_plan_t *plan; |
350 | | cs_command_vec_t commands; /* Optional: capture parsed commands for specialization */ |
351 | | |
352 | | /* CFF2 partial instancing: when set, blends are rewritten against the |
353 | | * instanced variation store instead of being copied or flattened. */ |
354 | | const cff2_instancing_plan_t *instancer = nullptr; |
355 | | bool emitted_blend = false; |
356 | | }; |
357 | | |
358 | | template <typename ACC, typename ENV, typename OPSET, op_code_t endchar_op=OpCode_Invalid> |
359 | | struct subr_flattener_t |
360 | | { |
361 | | subr_flattener_t (const ACC &acc_, |
362 | | const hb_subset_plan_t *plan_) |
363 | | : acc (acc_), plan (plan_) {} |
364 | | |
365 | | bool flatten (str_buff_vec_t &flat_charstrings, |
366 | | hb_vector_t<hb_vector_t<cs_command_t>> *command_capture = nullptr) |
367 | | { |
368 | | unsigned count = plan->num_output_glyphs (); |
369 | | if (!flat_charstrings.resize_exact (count)) |
370 | | return false; |
371 | | for (unsigned int i = 0; i < count; i++) |
372 | | { |
373 | | hb_codepoint_t glyph; |
374 | | if (!plan->old_gid_for_new_gid (i, &glyph)) |
375 | | { |
376 | | /* add an endchar only charstring for a missing glyph if CFF1 */ |
377 | | if (endchar_op != OpCode_Invalid) flat_charstrings[i].push (endchar_op); |
378 | | continue; |
379 | | } |
380 | | const hb_ubytes_t str = (*acc.charStrings)[glyph]; |
381 | | unsigned int fd = acc.fdSelect->get_fd (glyph); |
382 | | if (unlikely (fd >= acc.fdCount)) |
383 | | return false; |
384 | | |
385 | | |
386 | | ENV env (str, acc, fd, |
387 | | plan->normalized_coords.arrayZ, plan->normalized_coords.length); |
388 | | cs_interpreter_t<ENV, OPSET, flatten_param_t> interp (env); |
389 | | flatten_param_t param = { |
390 | | flat_charstrings.arrayZ[i], |
391 | | (bool) (plan->flags & HB_SUBSET_FLAGS_NO_HINTING), |
392 | | plan, |
393 | | command_capture ? &(*command_capture)[i] : nullptr |
394 | | }; |
395 | | if (unlikely (!interp.interpret (param))) |
396 | | return false; |
397 | | } |
398 | | return true; |
399 | | } |
400 | | |
401 | | const ACC &acc; |
402 | | const hb_subset_plan_t *plan; |
403 | | }; |
404 | | |
405 | | struct subr_closures_t |
406 | | { |
407 | | subr_closures_t (unsigned int fd_count) : global_closure (), local_closures () |
408 | 0 | { |
409 | 0 | local_closures.resize_exact (fd_count); |
410 | 0 | } |
411 | | |
412 | | void reset () |
413 | 0 | { |
414 | 0 | global_closure.clear(); |
415 | 0 | for (unsigned int i = 0; i < local_closures.length; i++) |
416 | 0 | local_closures[i].clear(); |
417 | 0 | } |
418 | | |
419 | 0 | bool in_error () const { return local_closures.in_error (); } |
420 | | hb_set_t global_closure; |
421 | | hb_vector_t<hb_set_t> local_closures; |
422 | | }; |
423 | | |
424 | | struct parsed_cs_op_t : op_str_t |
425 | | { |
426 | | parsed_cs_op_t (unsigned int subr_num_ = 0) : |
427 | 0 | subr_num (subr_num_) {} |
428 | | |
429 | 0 | bool is_hinting () const { return hinting_flag; } |
430 | 0 | void set_hinting () { hinting_flag = true; } |
431 | | |
432 | | /* The layout of this struct is designed to fit within the |
433 | | * padding of op_str_t! */ |
434 | | |
435 | | protected: |
436 | | bool hinting_flag = false; |
437 | | |
438 | | public: |
439 | | uint16_t subr_num; |
440 | | }; |
441 | | |
442 | | struct parsed_cs_str_t : parsed_values_t<parsed_cs_op_t> |
443 | | { |
444 | | parsed_cs_str_t () : |
445 | | parsed (false), |
446 | | hint_dropped (false), |
447 | | has_prefix_ (false), |
448 | | has_calls_ (false), |
449 | | coalescing_ (false) |
450 | 0 | { |
451 | 0 | SUPER::init (); |
452 | 0 | } |
453 | | |
454 | | HB_ALWAYS_INLINE |
455 | | void add_op (op_code_t op, const byte_str_ref_t& str_ref) |
456 | 0 | { |
457 | 0 | if (is_parsed ()) return; |
458 | 0 | if (coalescing_) |
459 | 0 | { |
460 | 0 | /* Do not record individual tokens; only note their boundaries. |
461 | 0 | * Bytes are flushed as verbatim segments at call sites and at |
462 | 0 | * the end of the string. Only enabled when per-op granularity |
463 | 0 | * is not needed later for hint analysis. */ |
464 | 0 | penultimate_end_ = last_end_; |
465 | 0 | last_end_ = str_ref.get_offset (); |
466 | 0 | if (unlikely (op == OpCode_return || op == OpCode_endchar)) |
467 | 0 | flush_segment (str_ref, last_end_); |
468 | 0 | return; |
469 | 0 | } |
470 | 0 | SUPER::add_op (op, str_ref); |
471 | 0 | } |
472 | | |
473 | | void add_call_op (op_code_t op, const byte_str_ref_t& str_ref, unsigned int subr_num) |
474 | 0 | { |
475 | 0 | if (is_parsed ()) return; |
476 | 0 | has_calls_ = true; |
477 | 0 |
|
478 | 0 | if (coalescing_) |
479 | 0 | { |
480 | 0 | /* Flush bytes preceding the subroutine-number token, then skip |
481 | 0 | * over the number: it is re-encoded with the new bias. */ |
482 | 0 | flush_segment (str_ref, penultimate_end_); |
483 | 0 | opStart = last_end_; |
484 | 0 | SUPER::add_op (op, str_ref, {subr_num}); |
485 | 0 | penultimate_end_ = last_end_ = str_ref.get_offset (); |
486 | 0 | return; |
487 | 0 | } |
488 | 0 |
|
489 | 0 | /* Pop the subroutine number. */ |
490 | 0 | values.pop (); |
491 | 0 |
|
492 | 0 | SUPER::add_op (op, str_ref, {subr_num}); |
493 | 0 | } |
494 | | |
495 | | /* Flush pending bytes [opStart, end) as verbatim segment entries. */ |
496 | | void flush_segment (const byte_str_ref_t& str_ref, unsigned end) |
497 | 0 | { |
498 | 0 | unsigned start = opStart; |
499 | 0 | if (end <= start) return; |
500 | 0 | while (start < end) |
501 | 0 | { |
502 | 0 | auto arr = str_ref.sub_array (start, hb_min (end - start, 255u)); |
503 | 0 | if (unlikely (!arr.length)) break; |
504 | 0 | parsed_cs_op_t *val = values.push (); |
505 | 0 | val->ptr = arr.arrayZ; |
506 | 0 | val->length = arr.length; |
507 | 0 | start += arr.length; |
508 | 0 | } |
509 | 0 | opStart = end; |
510 | 0 | } |
511 | | |
512 | | /* For coalescing mode: flush any pending bytes through the current |
513 | | * position; used where the string ends without an explicit |
514 | | * return/endchar op (CFF2). */ |
515 | | void flush_coalesced (const byte_str_ref_t& str_ref) |
516 | 0 | { |
517 | 0 | if (coalescing_ && !is_parsed ()) |
518 | 0 | flush_segment (str_ref, str_ref.get_offset ()); |
519 | 0 | } |
520 | | |
521 | 0 | void enable_coalescing () { coalescing_ = true; } |
522 | 0 | bool is_coalescing () const { return coalescing_; } |
523 | | |
524 | | void set_prefix (const number_t &num, op_code_t op = OpCode_Invalid) |
525 | 0 | { |
526 | 0 | has_prefix_ = true; |
527 | 0 | prefix_op_ = op; |
528 | 0 | prefix_num_ = num; |
529 | 0 | } |
530 | | |
531 | | bool at_end (unsigned int pos) const |
532 | 0 | { |
533 | 0 | return ((pos + 1 >= values.length) /* CFF2 */ |
534 | 0 | || (values[pos + 1].op == OpCode_return)); |
535 | 0 | } |
536 | | |
537 | 0 | bool is_parsed () const { return parsed; } |
538 | 0 | void set_parsed () { parsed = true; } |
539 | | |
540 | 0 | bool is_hint_dropped () const { return hint_dropped; } |
541 | 0 | void set_hint_dropped () { hint_dropped = true; } |
542 | | |
543 | 0 | bool is_vsindex_dropped () const { return vsindex_dropped; } |
544 | 0 | void set_vsindex_dropped () { vsindex_dropped = true; } |
545 | | |
546 | 0 | bool has_prefix () const { return has_prefix_; } |
547 | 0 | op_code_t prefix_op () const { return prefix_op_; } |
548 | 0 | const number_t &prefix_num () const { return prefix_num_; } |
549 | | |
550 | 0 | bool has_calls () const { return has_calls_; } |
551 | | |
552 | | void compact () |
553 | 0 | { |
554 | 0 | unsigned count = values.length; |
555 | 0 | if (!count) return; |
556 | 0 | auto &opstr = values.arrayZ; |
557 | 0 | unsigned j = 0; |
558 | 0 | for (unsigned i = 1; i < count; i++) |
559 | 0 | { |
560 | 0 | /* See if we can combine op j and op i. */ |
561 | 0 | bool combine = |
562 | 0 | (opstr[j].op != OpCode_callsubr && opstr[j].op != OpCode_callgsubr) && |
563 | 0 | (opstr[i].op != OpCode_callsubr && opstr[i].op != OpCode_callgsubr) && |
564 | 0 | (opstr[j].is_hinting () == opstr[i].is_hinting ()) && |
565 | 0 | (opstr[j].ptr + opstr[j].length == opstr[i].ptr) && |
566 | 0 | (opstr[j].length + opstr[i].length <= 255); |
567 | 0 |
|
568 | 0 | if (combine) |
569 | 0 | { |
570 | 0 | opstr[j].length += opstr[i].length; |
571 | 0 | opstr[j].op = OpCode_Invalid; |
572 | 0 | } |
573 | 0 | else |
574 | 0 | { |
575 | 0 | opstr[++j] = opstr[i]; |
576 | 0 | } |
577 | 0 | } |
578 | 0 | values.shrink (j + 1); |
579 | 0 | } |
580 | | |
581 | | protected: |
582 | | bool parsed : 1; |
583 | | bool hint_dropped : 1; |
584 | | bool vsindex_dropped : 1; |
585 | | bool has_prefix_ : 1; |
586 | | bool has_calls_ : 1; |
587 | | /* Record verbatim byte segments instead of individual tokens, |
588 | | * making a separate compact() pass unnecessary; incompatible with |
589 | | * hint analysis. */ |
590 | | bool coalescing_ : 1; |
591 | | /* End offsets of the last two tokens seen (coalescing mode). */ |
592 | | unsigned penultimate_end_ = 0; |
593 | | unsigned last_end_ = 0; |
594 | | op_code_t prefix_op_; |
595 | | number_t prefix_num_; |
596 | | |
597 | | private: |
598 | | typedef parsed_values_t<parsed_cs_op_t> SUPER; |
599 | | }; |
600 | | |
601 | | struct parsed_cs_str_vec_t : hb_vector_t<parsed_cs_str_t> |
602 | | { |
603 | | private: |
604 | | typedef hb_vector_t<parsed_cs_str_t> SUPER; |
605 | | }; |
606 | | |
607 | | struct cff_subset_accelerator_t |
608 | | { |
609 | | static cff_subset_accelerator_t* create ( |
610 | | hb_blob_t* original_blob, |
611 | | const parsed_cs_str_vec_t& parsed_charstrings, |
612 | | const parsed_cs_str_vec_t& parsed_global_subrs, |
613 | 0 | const hb_vector_t<parsed_cs_str_vec_t>& parsed_local_subrs) { |
614 | 0 | cff_subset_accelerator_t* accel = |
615 | 0 | (cff_subset_accelerator_t*) hb_malloc (sizeof(cff_subset_accelerator_t)); |
616 | 0 | if (unlikely (!accel)) return nullptr; |
617 | 0 | new (accel) cff_subset_accelerator_t (original_blob, |
618 | 0 | parsed_charstrings, |
619 | 0 | parsed_global_subrs, |
620 | 0 | parsed_local_subrs); |
621 | 0 | return accel; |
622 | 0 | } |
623 | | |
624 | 0 | static void destroy (void* value) { |
625 | 0 | if (!value) return; |
626 | 0 |
|
627 | 0 | cff_subset_accelerator_t* accel = (cff_subset_accelerator_t*) value; |
628 | 0 | accel->~cff_subset_accelerator_t (); |
629 | 0 | hb_free (accel); |
630 | 0 | } |
631 | | |
632 | | cff_subset_accelerator_t( |
633 | | hb_blob_t* original_blob_, |
634 | | const parsed_cs_str_vec_t& parsed_charstrings_, |
635 | | const parsed_cs_str_vec_t& parsed_global_subrs_, |
636 | | const hb_vector_t<parsed_cs_str_vec_t>& parsed_local_subrs_) |
637 | 0 | { |
638 | 0 | parsed_charstrings = parsed_charstrings_; |
639 | 0 | parsed_global_subrs = parsed_global_subrs_; |
640 | 0 | parsed_local_subrs = parsed_local_subrs_; |
641 | 0 |
|
642 | 0 | // the parsed charstrings point to memory in the original CFF table so we must hold a reference |
643 | 0 | // to it to keep the memory valid. |
644 | 0 | original_blob = hb_blob_reference (original_blob_); |
645 | 0 | } |
646 | | |
647 | | ~cff_subset_accelerator_t() |
648 | 0 | { |
649 | 0 | hb_blob_destroy (original_blob); |
650 | 0 | auto *mapping = glyph_to_sid_map.get_relaxed (); |
651 | 0 | if (mapping) |
652 | 0 | { |
653 | 0 | mapping->~glyph_to_sid_map_t (); |
654 | 0 | hb_free (mapping); |
655 | 0 | } |
656 | 0 | } |
657 | | |
658 | | parsed_cs_str_vec_t parsed_charstrings; |
659 | | parsed_cs_str_vec_t parsed_global_subrs; |
660 | | hb_vector_t<parsed_cs_str_vec_t> parsed_local_subrs; |
661 | | mutable hb_atomic_t<glyph_to_sid_map_t *> glyph_to_sid_map; |
662 | | |
663 | | private: |
664 | | hb_blob_t* original_blob; |
665 | | }; |
666 | | |
667 | | struct subr_subset_param_t |
668 | | { |
669 | | subr_subset_param_t (parsed_cs_str_t *parsed_charstring_, |
670 | | parsed_cs_str_vec_t *parsed_global_subrs_, |
671 | | parsed_cs_str_vec_t *parsed_local_subrs_, |
672 | | hb_set_t *global_closure_, |
673 | | hb_set_t *local_closure_, |
674 | | bool drop_hints_, |
675 | | bool coalesce_ = false) : |
676 | | current_parsed_str (parsed_charstring_), |
677 | | parsed_charstring (parsed_charstring_), |
678 | | parsed_global_subrs (parsed_global_subrs_), |
679 | | parsed_local_subrs (parsed_local_subrs_), |
680 | | global_closure (global_closure_), |
681 | | local_closure (local_closure_), |
682 | | drop_hints (drop_hints_), |
683 | | coalesce (coalesce_) |
684 | 0 | { |
685 | 0 | if (coalesce) parsed_charstring->enable_coalescing (); |
686 | 0 | } |
687 | | |
688 | | parsed_cs_str_t *get_parsed_str_for_context (call_context_t &context) |
689 | 0 | { |
690 | 0 | switch (context.type) |
691 | 0 | { |
692 | 0 | case CSType_CharString: |
693 | 0 | return parsed_charstring; |
694 | 0 |
|
695 | 0 | case CSType_LocalSubr: |
696 | 0 | if (likely (context.subr_num < parsed_local_subrs->length)) |
697 | 0 | return &(*parsed_local_subrs)[context.subr_num]; |
698 | 0 | break; |
699 | 0 |
|
700 | 0 | case CSType_GlobalSubr: |
701 | 0 | if (likely (context.subr_num < parsed_global_subrs->length)) |
702 | 0 | return &(*parsed_global_subrs)[context.subr_num]; |
703 | 0 | break; |
704 | 0 | } |
705 | 0 | return nullptr; |
706 | 0 | } |
707 | | |
708 | | template <typename ENV> |
709 | | void set_current_str (ENV &env, bool calling) |
710 | | { |
711 | | parsed_cs_str_t *parsed_str = get_parsed_str_for_context (env.context); |
712 | | if (unlikely (!parsed_str)) |
713 | | { |
714 | | env.set_error (); |
715 | | return; |
716 | | } |
717 | | /* If the called subroutine is parsed partially but not completely yet, |
718 | | * it must be because we are calling it recursively. |
719 | | * Handle it as an error. */ |
720 | | if (unlikely (calling && !parsed_str->is_parsed () && (parsed_str->values.length > 0))) |
721 | | env.set_error (); |
722 | | else |
723 | | { |
724 | | if (!parsed_str->is_parsed ()) |
725 | | { |
726 | | parsed_str->alloc (env.str_ref.total_size ()); |
727 | | if (coalesce) parsed_str->enable_coalescing (); |
728 | | } |
729 | | current_parsed_str = parsed_str; |
730 | | } |
731 | | } |
732 | | |
733 | | parsed_cs_str_t *current_parsed_str; |
734 | | |
735 | | parsed_cs_str_t *parsed_charstring; |
736 | | parsed_cs_str_vec_t *parsed_global_subrs; |
737 | | parsed_cs_str_vec_t *parsed_local_subrs; |
738 | | hb_set_t *global_closure; |
739 | | hb_set_t *local_closure; |
740 | | bool drop_hints; |
741 | | bool coalesce; |
742 | | }; |
743 | | |
744 | | struct subr_remap_t : hb_inc_bimap_t |
745 | | { |
746 | | void create (const hb_set_t *closure) |
747 | 0 | { |
748 | 0 | /* create a remapping of subroutine numbers from old to new. |
749 | 0 | * no optimization based on usage counts. fonttools doesn't appear doing that either. |
750 | 0 | */ |
751 | 0 |
|
752 | 0 | alloc (closure->get_population ()); |
753 | 0 | for (auto old_num : *closure) |
754 | 0 | add (old_num); |
755 | 0 |
|
756 | 0 | if (get_population () < 1240) |
757 | 0 | bias = 107; |
758 | 0 | else if (get_population () < 33900) |
759 | 0 | bias = 1131; |
760 | 0 | else |
761 | 0 | bias = 32768; |
762 | 0 | } |
763 | | |
764 | | int biased_num (unsigned int old_num) const |
765 | 0 | { |
766 | 0 | hb_codepoint_t new_num = get (old_num); |
767 | 0 | return (int)new_num - bias; |
768 | 0 | } |
769 | | |
770 | | protected: |
771 | | int bias; |
772 | | }; |
773 | | |
774 | | struct subr_remaps_t |
775 | | { |
776 | | subr_remaps_t (unsigned int fdCount) |
777 | 0 | { |
778 | 0 | local_remaps.resize (fdCount); |
779 | 0 | } |
780 | | |
781 | | bool in_error() |
782 | 0 | { |
783 | 0 | return local_remaps.in_error (); |
784 | 0 | } |
785 | | |
786 | | void create (subr_closures_t& closures) |
787 | 0 | { |
788 | 0 | global_remap.create (&closures.global_closure); |
789 | 0 | for (unsigned int i = 0; i < local_remaps.length; i++) |
790 | 0 | local_remaps.arrayZ[i].create (&closures.local_closures[i]); |
791 | 0 | } |
792 | | |
793 | | subr_remap_t global_remap; |
794 | | hb_vector_t<subr_remap_t> local_remaps; |
795 | | }; |
796 | | |
797 | | template <typename SUBSETTER, typename SUBRS, typename ACC, typename ENV, typename OPSET, op_code_t endchar_op=OpCode_Invalid> |
798 | | struct subr_subsetter_t |
799 | | { |
800 | | subr_subsetter_t (ACC &acc_, const hb_subset_plan_t *plan_) |
801 | | : acc (acc_), plan (plan_), closures(acc_.fdCount), |
802 | | remaps(acc_.fdCount) |
803 | | {} |
804 | | |
805 | | /* Subroutine subsetting with --no-desubroutinize runs in phases: |
806 | | * |
807 | | * 1. execute charstrings/subroutines to determine subroutine closures |
808 | | * 2. parse out all operators and numbers |
809 | | * 3. mark hint operators and operands for removal if --no-hinting |
810 | | * 4. re-encode all charstrings and subroutines with new subroutine numbers |
811 | | * |
812 | | * Phases #1 and #2 are done at the same time in collect_subrs (). |
813 | | * Phase #3 walks charstrings/subroutines forward then backward (hence parsing required), |
814 | | * because we can't tell if a number belongs to a hint op until we see the first moveto. |
815 | | * |
816 | | * Assumption: a callsubr/callgsubr operator must immediately follow a (biased) subroutine number |
817 | | * within the same charstring/subroutine, e.g., not split across a charstring and a subroutine. |
818 | | */ |
819 | | bool subset (void) |
820 | | { |
821 | | unsigned fd_count = acc.fdCount; |
822 | | const cff_subset_accelerator_t* cff_accelerator = nullptr; |
823 | | if (acc.cff_accelerator) { |
824 | | cff_accelerator = acc.cff_accelerator; |
825 | | fd_count = cff_accelerator->parsed_local_subrs.length; |
826 | | } |
827 | | |
828 | | if (cff_accelerator) { |
829 | | // If we are not dropping hinting then charstrings are not modified so we can |
830 | | // just use a reference to the cached copies. |
831 | | cached_charstrings.resize_exact (plan->num_output_glyphs ()); |
832 | | parsed_global_subrs = &cff_accelerator->parsed_global_subrs; |
833 | | parsed_local_subrs = &cff_accelerator->parsed_local_subrs; |
834 | | } else { |
835 | | parsed_charstrings.resize_exact (plan->num_output_glyphs ()); |
836 | | parsed_global_subrs_storage.resize_exact (acc.globalSubrs->count); |
837 | | |
838 | | if (unlikely (!parsed_local_subrs_storage.resize (fd_count))) return false; |
839 | | |
840 | | for (unsigned int i = 0; i < acc.fdCount; i++) |
841 | | { |
842 | | unsigned count = acc.privateDicts[i].localSubrs->count; |
843 | | parsed_local_subrs_storage[i].resize (count); |
844 | | if (unlikely (parsed_local_subrs_storage[i].in_error ())) return false; |
845 | | } |
846 | | |
847 | | parsed_global_subrs = &parsed_global_subrs_storage; |
848 | | parsed_local_subrs = &parsed_local_subrs_storage; |
849 | | } |
850 | | |
851 | | if (unlikely (remaps.in_error() |
852 | | || cached_charstrings.in_error () |
853 | | || parsed_charstrings.in_error () |
854 | | || parsed_global_subrs->in_error () |
855 | | || closures.in_error ())) { |
856 | | return false; |
857 | | } |
858 | | |
859 | | /* When hints are not analyzed (not dropping hints, not populating |
860 | | * the accelerator), coalesce parsed tokens as they are added, |
861 | | * instead of a separate compact() pass. */ |
862 | | bool coalesce = !(plan->flags & HB_SUBSET_FLAGS_NO_HINTING) && |
863 | | !plan->inprogress_accelerator; |
864 | | |
865 | | /* phase 1 & 2 */ |
866 | | for (auto _ : plan->new_to_old_gid_list) |
867 | | { |
868 | | hb_codepoint_t new_glyph = _.first; |
869 | | hb_codepoint_t old_glyph = _.second; |
870 | | |
871 | | const hb_ubytes_t str = (*acc.charStrings)[old_glyph]; |
872 | | unsigned int fd = acc.fdSelect->get_fd (old_glyph); |
873 | | if (unlikely (fd >= acc.fdCount)) |
874 | | return false; |
875 | | |
876 | | if (cff_accelerator) |
877 | | { |
878 | | // parsed string already exists in accelerator, copy it and move |
879 | | // on. |
880 | | if (cached_charstrings) |
881 | | cached_charstrings[new_glyph] = &cff_accelerator->parsed_charstrings[old_glyph]; |
882 | | else |
883 | | parsed_charstrings[new_glyph] = cff_accelerator->parsed_charstrings[old_glyph]; |
884 | | |
885 | | continue; |
886 | | } |
887 | | |
888 | | ENV env (str, acc, fd); |
889 | | cs_interpreter_t<ENV, OPSET, subr_subset_param_t> interp (env); |
890 | | |
891 | | parsed_charstrings[new_glyph].alloc (str.length); |
892 | | subr_subset_param_t param (&parsed_charstrings[new_glyph], |
893 | | &parsed_global_subrs_storage, |
894 | | &parsed_local_subrs_storage[fd], |
895 | | &closures.global_closure, |
896 | | &closures.local_closures[fd], |
897 | | plan->flags & HB_SUBSET_FLAGS_NO_HINTING, |
898 | | coalesce); |
899 | | |
900 | | if (unlikely (!interp.interpret (param))) |
901 | | return false; |
902 | | |
903 | | /* complete parsed string esp. copy CFF1 width or CFF2 vsindex to the parsed charstring for encoding */ |
904 | | SUBSETTER::complete_parsed_str (interp.env, param, parsed_charstrings[new_glyph]); |
905 | | |
906 | | /* mark hint ops and arguments for drop */ |
907 | | if ((plan->flags & HB_SUBSET_FLAGS_NO_HINTING) || plan->inprogress_accelerator) |
908 | | { |
909 | | subr_subset_param_t param (&parsed_charstrings[new_glyph], |
910 | | &parsed_global_subrs_storage, |
911 | | &parsed_local_subrs_storage[fd], |
912 | | &closures.global_closure, |
913 | | &closures.local_closures[fd], |
914 | | plan->flags & HB_SUBSET_FLAGS_NO_HINTING); |
915 | | |
916 | | drop_hints_param_t drop; |
917 | | if (drop_hints_in_str (parsed_charstrings[new_glyph], param, drop)) |
918 | | { |
919 | | parsed_charstrings[new_glyph].set_hint_dropped (); |
920 | | if (drop.vsindex_dropped) |
921 | | parsed_charstrings[new_glyph].set_vsindex_dropped (); |
922 | | } |
923 | | } |
924 | | |
925 | | /* Doing this here one by one instead of compacting all at the end |
926 | | * has massive peak-memory saving. |
927 | | * |
928 | | * The compacting both saves memory and makes further operations |
929 | | * faster. |
930 | | * |
931 | | * Not needed when tokens were coalesced during parsing. |
932 | | */ |
933 | | if (!coalesce) |
934 | | parsed_charstrings[new_glyph].compact (); |
935 | | } |
936 | | |
937 | | /* Since parsed strings were loaded from accelerator, we still need |
938 | | * to compute the subroutine closures which would have normally happened during |
939 | | * parsing. |
940 | | * |
941 | | * Or if we are dropping hinting, redo closure to get actually used subrs. |
942 | | */ |
943 | | if ((cff_accelerator || |
944 | | (!cff_accelerator && plan->flags & HB_SUBSET_FLAGS_NO_HINTING)) && |
945 | | !closure_subroutines(*parsed_global_subrs, |
946 | | *parsed_local_subrs)) |
947 | | return false; |
948 | | |
949 | | remaps.create (closures); |
950 | | |
951 | | populate_subset_accelerator (); |
952 | | return true; |
953 | | } |
954 | | |
955 | | bool encode_charstrings (str_buff_vec_t &buffArray, bool encode_prefix = true) const |
956 | | { |
957 | | unsigned num_glyphs = plan->num_output_glyphs (); |
958 | | if (unlikely (!buffArray.resize_exact (num_glyphs))) |
959 | | return false; |
960 | | hb_codepoint_t last = 0; |
961 | | for (auto _ : plan->new_to_old_gid_list) |
962 | | { |
963 | | hb_codepoint_t gid = _.first; |
964 | | hb_codepoint_t old_glyph = _.second; |
965 | | |
966 | | if (endchar_op != OpCode_Invalid) |
967 | | for (; last < gid; last++) |
968 | | { |
969 | | // Hack to point vector to static string. |
970 | | auto &b = buffArray.arrayZ[last]; |
971 | | b.set_storage (const_cast<unsigned char *>(endchar_str), 1); |
972 | | } |
973 | | |
974 | | last++; // Skip over gid |
975 | | unsigned int fd = acc.fdSelect->get_fd (old_glyph); |
976 | | if (unlikely (fd >= acc.fdCount)) |
977 | | return false; |
978 | | if (unlikely (!encode_str (get_parsed_charstring (gid), fd, buffArray.arrayZ[gid], encode_prefix))) |
979 | | return false; |
980 | | } |
981 | | if (endchar_op != OpCode_Invalid) |
982 | | for (; last < num_glyphs; last++) |
983 | | { |
984 | | // Hack to point vector to static string. |
985 | | auto &b = buffArray.arrayZ[last]; |
986 | | b.set_storage (const_cast<unsigned char *>(endchar_str), 1); |
987 | | } |
988 | | |
989 | | return true; |
990 | | } |
991 | | |
992 | | bool encode_subrs (const parsed_cs_str_vec_t &subrs, const subr_remap_t& remap, unsigned int fd, str_buff_vec_t &buffArray) const |
993 | | { |
994 | | unsigned int count = remap.get_population (); |
995 | | |
996 | | if (unlikely (!buffArray.resize_exact (count))) |
997 | | return false; |
998 | | for (unsigned int new_num = 0; new_num < count; new_num++) |
999 | | { |
1000 | | hb_codepoint_t old_num = remap.backward (new_num); |
1001 | | assert (old_num != CFF_UNDEF_CODE); |
1002 | | |
1003 | | if (unlikely (!encode_str (subrs[old_num], fd, buffArray[new_num]))) |
1004 | | return false; |
1005 | | } |
1006 | | return true; |
1007 | | } |
1008 | | |
1009 | | bool encode_globalsubrs (str_buff_vec_t &buffArray) |
1010 | | { |
1011 | | return encode_subrs (*parsed_global_subrs, remaps.global_remap, 0, buffArray); |
1012 | | } |
1013 | | |
1014 | | bool encode_localsubrs (unsigned int fd, str_buff_vec_t &buffArray) const |
1015 | | { |
1016 | | return encode_subrs ((*parsed_local_subrs)[fd], remaps.local_remaps[fd], fd, buffArray); |
1017 | | } |
1018 | | |
1019 | | protected: |
1020 | | struct drop_hints_param_t |
1021 | | { |
1022 | | drop_hints_param_t () |
1023 | | : seen_moveto (false), |
1024 | | ends_in_hint (false), |
1025 | | all_dropped (false), |
1026 | | vsindex_dropped (false) {} |
1027 | | |
1028 | | bool seen_moveto; |
1029 | | bool ends_in_hint; |
1030 | | bool all_dropped; |
1031 | | bool vsindex_dropped; |
1032 | | }; |
1033 | | |
1034 | | bool drop_hints_in_subr (parsed_cs_str_t &str, unsigned int pos, |
1035 | | parsed_cs_str_vec_t &subrs, unsigned int subr_num, |
1036 | | const subr_subset_param_t ¶m, drop_hints_param_t &drop) |
1037 | | { |
1038 | | drop.ends_in_hint = false; |
1039 | | bool has_hint = drop_hints_in_str (subrs[subr_num], param, drop); |
1040 | | |
1041 | | /* if this subr ends with a stem hint (i.e., not a number; potential argument for moveto), |
1042 | | * then this entire subroutine must be a hint. drop its call. */ |
1043 | | if (drop.ends_in_hint) |
1044 | | { |
1045 | | str.values[pos].set_hinting (); |
1046 | | /* if this subr call is at the end of the parent subr, propagate the flag |
1047 | | * otherwise reset the flag */ |
1048 | | if (!str.at_end (pos)) |
1049 | | drop.ends_in_hint = false; |
1050 | | } |
1051 | | else if (drop.all_dropped) |
1052 | | { |
1053 | | str.values[pos].set_hinting (); |
1054 | | } |
1055 | | |
1056 | | return has_hint; |
1057 | | } |
1058 | | |
1059 | | /* returns true if it sees a hint op before the first moveto */ |
1060 | | bool drop_hints_in_str (parsed_cs_str_t &str, const subr_subset_param_t ¶m, drop_hints_param_t &drop) |
1061 | | { |
1062 | | bool seen_hint = false; |
1063 | | |
1064 | | unsigned count = str.values.length; |
1065 | | auto *values = str.values.arrayZ; |
1066 | | for (unsigned int pos = 0; pos < count; pos++) |
1067 | | { |
1068 | | bool has_hint = false; |
1069 | | switch (values[pos].op) |
1070 | | { |
1071 | | case OpCode_callsubr: |
1072 | | has_hint = drop_hints_in_subr (str, pos, |
1073 | | *param.parsed_local_subrs, values[pos].subr_num, |
1074 | | param, drop); |
1075 | | break; |
1076 | | |
1077 | | case OpCode_callgsubr: |
1078 | | has_hint = drop_hints_in_subr (str, pos, |
1079 | | *param.parsed_global_subrs, values[pos].subr_num, |
1080 | | param, drop); |
1081 | | break; |
1082 | | |
1083 | | case OpCode_rmoveto: |
1084 | | case OpCode_hmoveto: |
1085 | | case OpCode_vmoveto: |
1086 | | drop.seen_moveto = true; |
1087 | | break; |
1088 | | |
1089 | | case OpCode_hintmask: |
1090 | | case OpCode_cntrmask: |
1091 | | if (drop.seen_moveto) |
1092 | | { |
1093 | | values[pos].set_hinting (); |
1094 | | break; |
1095 | | } |
1096 | | HB_FALLTHROUGH; |
1097 | | |
1098 | | case OpCode_hstemhm: |
1099 | | case OpCode_vstemhm: |
1100 | | case OpCode_hstem: |
1101 | | case OpCode_vstem: |
1102 | | has_hint = true; |
1103 | | values[pos].set_hinting (); |
1104 | | if (str.at_end (pos)) |
1105 | | drop.ends_in_hint = true; |
1106 | | break; |
1107 | | |
1108 | | case OpCode_dotsection: |
1109 | | values[pos].set_hinting (); |
1110 | | break; |
1111 | | |
1112 | | default: |
1113 | | /* NONE */ |
1114 | | break; |
1115 | | } |
1116 | | if (has_hint) |
1117 | | { |
1118 | | for (int i = pos - 1; i >= 0; i--) |
1119 | | { |
1120 | | parsed_cs_op_t &csop = values[(unsigned)i]; |
1121 | | if (csop.is_hinting ()) |
1122 | | break; |
1123 | | csop.set_hinting (); |
1124 | | if (csop.op == OpCode_vsindexcs) |
1125 | | drop.vsindex_dropped = true; |
1126 | | } |
1127 | | seen_hint |= has_hint; |
1128 | | } |
1129 | | } |
1130 | | |
1131 | | /* Raise all_dropped flag if all operators except return are dropped from a subr. |
1132 | | * It may happen even after seeing the first moveto if a subr contains |
1133 | | * only (usually one) hintmask operator, then calls to this subr can be dropped. |
1134 | | */ |
1135 | | drop.all_dropped = true; |
1136 | | for (unsigned int pos = 0; pos < count; pos++) |
1137 | | { |
1138 | | parsed_cs_op_t &csop = values[pos]; |
1139 | | if (csop.op == OpCode_return) |
1140 | | break; |
1141 | | if (!csop.is_hinting ()) |
1142 | | { |
1143 | | drop.all_dropped = false; |
1144 | | break; |
1145 | | } |
1146 | | } |
1147 | | |
1148 | | return seen_hint; |
1149 | | } |
1150 | | |
1151 | | bool closure_subroutines (const parsed_cs_str_vec_t& global_subrs, |
1152 | | const hb_vector_t<parsed_cs_str_vec_t>& local_subrs) |
1153 | | { |
1154 | | closures.reset (); |
1155 | | for (auto _ : plan->new_to_old_gid_list) |
1156 | | { |
1157 | | hb_codepoint_t new_glyph = _.first; |
1158 | | hb_codepoint_t old_glyph = _.second; |
1159 | | unsigned int fd = acc.fdSelect->get_fd (old_glyph); |
1160 | | if (unlikely (fd >= acc.fdCount)) |
1161 | | return false; |
1162 | | |
1163 | | // Note: const cast is safe here because the collect_subr_refs_in_str only performs a |
1164 | | // closure and does not modify any of the charstrings. |
1165 | | subr_subset_param_t param (const_cast<parsed_cs_str_t*> (&get_parsed_charstring (new_glyph)), |
1166 | | const_cast<parsed_cs_str_vec_t*> (&global_subrs), |
1167 | | const_cast<parsed_cs_str_vec_t*> (&local_subrs[fd]), |
1168 | | &closures.global_closure, |
1169 | | &closures.local_closures[fd], |
1170 | | plan->flags & HB_SUBSET_FLAGS_NO_HINTING); |
1171 | | collect_subr_refs_in_str (get_parsed_charstring (new_glyph), param); |
1172 | | } |
1173 | | |
1174 | | return true; |
1175 | | } |
1176 | | |
1177 | | void collect_subr_refs_in_subr (unsigned int subr_num, parsed_cs_str_vec_t &subrs, |
1178 | | hb_set_t *closure, |
1179 | | const subr_subset_param_t ¶m) |
1180 | | { |
1181 | | if (closure->has (subr_num)) |
1182 | | return; |
1183 | | closure->add (subr_num); |
1184 | | collect_subr_refs_in_str (subrs[subr_num], param); |
1185 | | } |
1186 | | |
1187 | | void collect_subr_refs_in_str (const parsed_cs_str_t &str, |
1188 | | const subr_subset_param_t ¶m) |
1189 | | { |
1190 | | if (!str.has_calls ()) |
1191 | | return; |
1192 | | |
1193 | | for (auto &opstr : str.values) |
1194 | | { |
1195 | | if (!param.drop_hints || !opstr.is_hinting ()) |
1196 | | { |
1197 | | switch (opstr.op) |
1198 | | { |
1199 | | case OpCode_callsubr: |
1200 | | collect_subr_refs_in_subr (opstr.subr_num, *param.parsed_local_subrs, |
1201 | | param.local_closure, param); |
1202 | | break; |
1203 | | |
1204 | | case OpCode_callgsubr: |
1205 | | collect_subr_refs_in_subr (opstr.subr_num, *param.parsed_global_subrs, |
1206 | | param.global_closure, param); |
1207 | | break; |
1208 | | |
1209 | | default: break; |
1210 | | } |
1211 | | } |
1212 | | } |
1213 | | } |
1214 | | |
1215 | | bool encode_str (const parsed_cs_str_t &str, const unsigned int fd, str_buff_t &buff, bool encode_prefix = true) const |
1216 | | { |
1217 | | str_encoder_t encoder (buff); |
1218 | | encoder.reset (); |
1219 | | bool hinting = !(plan->flags & HB_SUBSET_FLAGS_NO_HINTING); |
1220 | | /* if a prefix (CFF1 width or CFF2 vsindex) has been removed along with hints, |
1221 | | * re-insert it at the beginning of charstreing */ |
1222 | | if (encode_prefix && str.has_prefix () && !hinting && str.is_hint_dropped ()) |
1223 | | { |
1224 | | encoder.encode_num_cs (str.prefix_num ()); |
1225 | | if (str.prefix_op () != OpCode_Invalid) |
1226 | | encoder.encode_op (str.prefix_op ()); |
1227 | | } |
1228 | | |
1229 | | unsigned size = 0; |
1230 | | for (auto &opstr : str.values) |
1231 | | { |
1232 | | size += opstr.length; |
1233 | | if (opstr.op == OpCode_callsubr || opstr.op == OpCode_callgsubr) |
1234 | | size += 3; |
1235 | | } |
1236 | | if (!buff.alloc_exact (buff.length + size)) |
1237 | | return false; |
1238 | | |
1239 | | for (auto &opstr : str.values) |
1240 | | { |
1241 | | if (hinting || !opstr.is_hinting ()) |
1242 | | { |
1243 | | switch (opstr.op) |
1244 | | { |
1245 | | case OpCode_callsubr: |
1246 | | encoder.encode_int (remaps.local_remaps[fd].biased_num (opstr.subr_num)); |
1247 | | encoder.copy_str (opstr.ptr, opstr.length); |
1248 | | break; |
1249 | | |
1250 | | case OpCode_callgsubr: |
1251 | | encoder.encode_int (remaps.global_remap.biased_num (opstr.subr_num)); |
1252 | | encoder.copy_str (opstr.ptr, opstr.length); |
1253 | | break; |
1254 | | |
1255 | | default: |
1256 | | encoder.copy_str (opstr.ptr, opstr.length); |
1257 | | break; |
1258 | | } |
1259 | | } |
1260 | | } |
1261 | | return !encoder.in_error (); |
1262 | | } |
1263 | | |
1264 | | void compact_parsed_subrs () const |
1265 | | { |
1266 | | for (auto &cs : parsed_global_subrs_storage) |
1267 | | cs.compact (); |
1268 | | for (auto &vec : parsed_local_subrs_storage) |
1269 | | for (auto &cs : vec) |
1270 | | cs.compact (); |
1271 | | } |
1272 | | |
1273 | | void populate_subset_accelerator () const |
1274 | | { |
1275 | | if (!plan->inprogress_accelerator) return; |
1276 | | |
1277 | | compact_parsed_subrs (); |
1278 | | |
1279 | | acc.cff_accelerator = |
1280 | | cff_subset_accelerator_t::create(acc.blob, |
1281 | | parsed_charstrings, |
1282 | | parsed_global_subrs_storage, |
1283 | | parsed_local_subrs_storage); |
1284 | | } |
1285 | | |
1286 | | const parsed_cs_str_t& get_parsed_charstring (unsigned i) const |
1287 | | { |
1288 | | if (cached_charstrings) return *(cached_charstrings[i]); |
1289 | | return parsed_charstrings[i]; |
1290 | | } |
1291 | | |
1292 | | protected: |
1293 | | const ACC &acc; |
1294 | | const hb_subset_plan_t *plan; |
1295 | | |
1296 | | subr_closures_t closures; |
1297 | | |
1298 | | hb_vector_t<const parsed_cs_str_t*> cached_charstrings; |
1299 | | const parsed_cs_str_vec_t* parsed_global_subrs; |
1300 | | const hb_vector_t<parsed_cs_str_vec_t>* parsed_local_subrs; |
1301 | | |
1302 | | subr_remaps_t remaps; |
1303 | | |
1304 | | private: |
1305 | | |
1306 | | parsed_cs_str_vec_t parsed_charstrings; |
1307 | | parsed_cs_str_vec_t parsed_global_subrs_storage; |
1308 | | hb_vector_t<parsed_cs_str_vec_t> parsed_local_subrs_storage; |
1309 | | typedef typename SUBRS::count_type subr_count_type; |
1310 | | }; |
1311 | | |
1312 | | } /* namespace CFF */ |
1313 | | |
1314 | | HB_INTERNAL bool |
1315 | | hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan, |
1316 | | unsigned int fdCount, |
1317 | | const CFF::FDSelect &src, /* IN */ |
1318 | | unsigned int &subset_fd_count /* OUT */, |
1319 | | unsigned int &subset_fdselect_size /* OUT */, |
1320 | | unsigned int &subset_fdselect_format /* OUT */, |
1321 | | hb_vector_t<CFF::code_pair_t> &fdselect_ranges /* OUT */, |
1322 | | hb_inc_bimap_t &fdmap /* OUT */); |
1323 | | |
1324 | | HB_INTERNAL bool |
1325 | | hb_serialize_cff_fdselect (hb_serialize_context_t *c, |
1326 | | unsigned int num_glyphs, |
1327 | | const CFF::FDSelect &src, |
1328 | | unsigned int fd_count, |
1329 | | unsigned int fdselect_format, |
1330 | | unsigned int size, |
1331 | | const hb_vector_t<CFF::code_pair_t> &fdselect_ranges); |
1332 | | |
1333 | | #endif /* HB_SUBSET_CFF_COMMON_HH */ |