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#include "hb.hh"
28
29#ifndef HB_NO_SUBSET_CFF
30
31#include "hb-open-type.hh"
32#include "hb-ot-cff1-table.hh"
33#include "hb-set.h"
34#include "hb-bimap.hh"
35#include "hb-subset-cff1.hh"
36#include "hb-subset-plan.hh"
37#include "hb-subset-cff-common.hh"
38#include "hb-cff1-interp-cs.hh"
39
40using namespace CFF;
41
42struct remap_sid_t : hb_inc_bimap_t
43{
44 unsigned int add (unsigned int sid)
45 {
46 if ((sid != CFF_UNDEF_SID) && !is_std_std (sid))
47 return offset_sid (sid: hb_inc_bimap_t::add (lhs: unoffset_sid (sid)));
48 else
49 return sid;
50 }
51
52 unsigned int operator[] (unsigned int sid) const
53 {
54 if (is_std_std (sid) || (sid == CFF_UNDEF_SID))
55 return sid;
56 else
57 return offset_sid (sid: get (lhs: unoffset_sid (sid)));
58 }
59
60 static const unsigned int num_std_strings = 391;
61
62 static bool is_std_std (unsigned int sid) { return sid < num_std_strings; }
63 static unsigned int offset_sid (unsigned int sid) { return sid + num_std_strings; }
64 static unsigned int unoffset_sid (unsigned int sid) { return sid - num_std_strings; }
65};
66
67struct cff1_sub_table_info_t : cff_sub_table_info_t
68{
69 cff1_sub_table_info_t ()
70 : cff_sub_table_info_t (),
71 encoding_link (0),
72 charset_link (0)
73 {
74 privateDictInfo.init ();
75 }
76
77 objidx_t encoding_link;
78 objidx_t charset_link;
79 table_info_t privateDictInfo;
80};
81
82/* a copy of a parsed out cff1_top_dict_values_t augmented with additional operators */
83struct cff1_top_dict_values_mod_t : cff1_top_dict_values_t
84{
85 void init (const cff1_top_dict_values_t *base_= &Null (cff1_top_dict_values_t))
86 {
87 SUPER::init ();
88 base = base_;
89 }
90
91 void fini () { SUPER::fini (); }
92
93 unsigned get_count () const { return base->get_count () + SUPER::get_count (); }
94 const cff1_top_dict_val_t &get_value (unsigned int i) const
95 {
96 if (i < base->get_count ())
97 return (*base)[i];
98 else
99 return SUPER::values[i - base->get_count ()];
100 }
101 const cff1_top_dict_val_t &operator [] (unsigned int i) const { return get_value (i); }
102
103 void reassignSIDs (const remap_sid_t& sidmap)
104 {
105 for (unsigned int i = 0; i < name_dict_values_t::ValCount; i++)
106 nameSIDs[i] = sidmap[base->nameSIDs[i]];
107 }
108
109 protected:
110 typedef cff1_top_dict_values_t SUPER;
111 const cff1_top_dict_values_t *base;
112};
113
114struct top_dict_modifiers_t
115{
116 top_dict_modifiers_t (const cff1_sub_table_info_t &info_,
117 const unsigned int (&nameSIDs_)[name_dict_values_t::ValCount])
118 : info (info_),
119 nameSIDs (nameSIDs_)
120 {}
121
122 const cff1_sub_table_info_t &info;
123 const unsigned int (&nameSIDs)[name_dict_values_t::ValCount];
124};
125
126struct cff1_top_dict_op_serializer_t : cff_top_dict_op_serializer_t<cff1_top_dict_val_t>
127{
128 bool serialize (hb_serialize_context_t *c,
129 const cff1_top_dict_val_t &opstr,
130 const top_dict_modifiers_t &mod) const
131 {
132 TRACE_SERIALIZE (this);
133
134 op_code_t op = opstr.op;
135 switch (op)
136 {
137 case OpCode_charset:
138 if (mod.info.charset_link)
139 return_trace (FontDict::serialize_link4_op(c, op, mod.info.charset_link, whence_t::Absolute));
140 else
141 goto fall_back;
142
143 case OpCode_Encoding:
144 if (mod.info.encoding_link)
145 return_trace (FontDict::serialize_link4_op(c, op, mod.info.encoding_link, whence_t::Absolute));
146 else
147 goto fall_back;
148
149 case OpCode_Private:
150 return_trace (UnsizedByteStr::serialize_int2 (c, mod.info.privateDictInfo.size) &&
151 Dict::serialize_link4_op (c, op, mod.info.privateDictInfo.link, whence_t::Absolute));
152
153 case OpCode_version:
154 case OpCode_Notice:
155 case OpCode_Copyright:
156 case OpCode_FullName:
157 case OpCode_FamilyName:
158 case OpCode_Weight:
159 case OpCode_PostScript:
160 case OpCode_BaseFontName:
161 case OpCode_FontName:
162 return_trace (FontDict::serialize_int2_op (c, op, mod.nameSIDs[name_dict_values_t::name_op_to_index (op)]));
163
164 case OpCode_ROS:
165 {
166 /* for registry & ordering, reassigned SIDs are serialized
167 * for supplement, the original byte string is copied along with the op code */
168 op_str_t supp_op;
169 supp_op.op = op;
170 if ( unlikely (!(opstr.length >= opstr.last_arg_offset + 3)))
171 return_trace (false);
172 supp_op.ptr = opstr.ptr + opstr.last_arg_offset;
173 supp_op.length = opstr.length - opstr.last_arg_offset;
174 return_trace (UnsizedByteStr::serialize_int2 (c, mod.nameSIDs[name_dict_values_t::registry]) &&
175 UnsizedByteStr::serialize_int2 (c, mod.nameSIDs[name_dict_values_t::ordering]) &&
176 copy_opstr (c, supp_op));
177 }
178 fall_back:
179 default:
180 return_trace (cff_top_dict_op_serializer_t<cff1_top_dict_val_t>::serialize (c, opstr, mod.info));
181 }
182 return_trace (true);
183 }
184
185};
186
187struct cff1_font_dict_op_serializer_t : cff_font_dict_op_serializer_t
188{
189 bool serialize (hb_serialize_context_t *c,
190 const op_str_t &opstr,
191 const cff1_font_dict_values_mod_t &mod) const
192 {
193 TRACE_SERIALIZE (this);
194
195 if (opstr.op == OpCode_FontName)
196 return_trace (FontDict::serialize_int2_op (c, opstr.op, mod.fontName));
197 else
198 return_trace (SUPER::serialize (c, opstr, mod.privateDictInfo));
199 }
200
201 private:
202 typedef cff_font_dict_op_serializer_t SUPER;
203};
204
205struct cff1_cs_opset_flatten_t : cff1_cs_opset_t<cff1_cs_opset_flatten_t, flatten_param_t>
206{
207 static void flush_args_and_op (op_code_t op, cff1_cs_interp_env_t &env, flatten_param_t& param)
208 {
209 if (env.arg_start > 0)
210 flush_width (env, param);
211
212 switch (op)
213 {
214 case OpCode_hstem:
215 case OpCode_hstemhm:
216 case OpCode_vstem:
217 case OpCode_vstemhm:
218 case OpCode_hintmask:
219 case OpCode_cntrmask:
220 case OpCode_dotsection:
221 if (param.drop_hints)
222 {
223 env.clear_args ();
224 return;
225 }
226 HB_FALLTHROUGH;
227
228 default:
229 SUPER::flush_args_and_op (op, env, param);
230 break;
231 }
232 }
233 static void flush_args (cff1_cs_interp_env_t &env, flatten_param_t& param)
234 {
235 str_encoder_t encoder (param.flatStr);
236 for (unsigned int i = env.arg_start; i < env.argStack.get_count (); i++)
237 encoder.encode_num_cs (n: env.eval_arg (i));
238 SUPER::flush_args (env, param);
239 }
240
241 static void flush_op (op_code_t op, cff1_cs_interp_env_t &env, flatten_param_t& param)
242 {
243 str_encoder_t encoder (param.flatStr);
244 encoder.encode_op (op);
245 }
246
247 static void flush_width (cff1_cs_interp_env_t &env, flatten_param_t& param)
248 {
249 assert (env.has_width);
250 str_encoder_t encoder (param.flatStr);
251 encoder.encode_num_cs (n: env.width);
252 }
253
254 static void flush_hintmask (op_code_t op, cff1_cs_interp_env_t &env, flatten_param_t& param)
255 {
256 SUPER::flush_hintmask (op, env, param);
257 if (!param.drop_hints)
258 {
259 str_encoder_t encoder (param.flatStr);
260 for (unsigned int i = 0; i < env.hintmask_size; i++)
261 encoder.encode_byte (b: env.str_ref[i]);
262 }
263 }
264
265 private:
266 typedef cff1_cs_opset_t<cff1_cs_opset_flatten_t, flatten_param_t> SUPER;
267};
268
269struct range_list_t : hb_vector_t<code_pair_t>
270{
271 /* replace the first glyph ID in the "glyph" field each range with a nLeft value */
272 bool complete (unsigned int last_glyph)
273 {
274 bool two_byte = false;
275 unsigned count = this->length;
276 for (unsigned int i = count; i; i--)
277 {
278 code_pair_t &pair = arrayZ[i - 1];
279 unsigned int nLeft = last_glyph - pair.glyph - 1;
280 two_byte |= nLeft >= 0x100;
281 last_glyph = pair.glyph;
282 pair.glyph = nLeft;
283 }
284 return two_byte;
285 }
286};
287
288struct cff1_cs_opset_subr_subset_t : cff1_cs_opset_t<cff1_cs_opset_subr_subset_t, subr_subset_param_t>
289{
290 static void process_op (op_code_t op, cff1_cs_interp_env_t &env, subr_subset_param_t& param)
291 {
292 switch (op) {
293
294 case OpCode_return:
295 param.current_parsed_str->add_op (op, str_ref: env.str_ref);
296 param.current_parsed_str->set_parsed ();
297 env.return_from_subr ();
298 param.set_current_str (env, calling: false);
299 break;
300
301 case OpCode_endchar:
302 param.current_parsed_str->add_op (op, str_ref: env.str_ref);
303 param.current_parsed_str->set_parsed ();
304 SUPER::process_op (op, env, param);
305 break;
306
307 case OpCode_callsubr:
308 process_call_subr (op, type: CSType_LocalSubr, env, param, subrs&: env.localSubrs, closure: param.local_closure);
309 break;
310
311 case OpCode_callgsubr:
312 process_call_subr (op, type: CSType_GlobalSubr, env, param, subrs&: env.globalSubrs, closure: param.global_closure);
313 break;
314
315 default:
316 SUPER::process_op (op, env, param);
317 param.current_parsed_str->add_op (op, str_ref: env.str_ref);
318 break;
319 }
320 }
321
322 protected:
323 static void process_call_subr (op_code_t op, cs_type_t type,
324 cff1_cs_interp_env_t &env, subr_subset_param_t& param,
325 cff1_biased_subrs_t& subrs, hb_set_t *closure)
326 {
327 byte_str_ref_t str_ref = env.str_ref;
328 env.call_subr (biasedSubrs: subrs, type);
329 param.current_parsed_str->add_call_op (op, str_ref, subr_num: env.context.subr_num);
330 closure->add (g: env.context.subr_num);
331 param.set_current_str (env, calling: true);
332 }
333
334 private:
335 typedef cff1_cs_opset_t<cff1_cs_opset_subr_subset_t, subr_subset_param_t> SUPER;
336};
337
338struct cff1_private_dict_op_serializer_t : op_serializer_t
339{
340 cff1_private_dict_op_serializer_t (bool desubroutinize_, bool drop_hints_)
341 : desubroutinize (desubroutinize_), drop_hints (drop_hints_) {}
342
343 bool serialize (hb_serialize_context_t *c,
344 const op_str_t &opstr,
345 objidx_t subrs_link) const
346 {
347 TRACE_SERIALIZE (this);
348
349 if (drop_hints && dict_opset_t::is_hint_op (op: opstr.op))
350 return_trace (true);
351
352 if (opstr.op == OpCode_Subrs)
353 {
354 if (desubroutinize || !subrs_link)
355 return_trace (true);
356 else
357 return_trace (FontDict::serialize_link2_op (c, opstr.op, subrs_link));
358 }
359
360 return_trace (copy_opstr (c, opstr));
361 }
362
363 protected:
364 const bool desubroutinize;
365 const bool drop_hints;
366};
367
368struct cff1_subr_subsetter_t : subr_subsetter_t<cff1_subr_subsetter_t, CFF1Subrs, const OT::cff1::accelerator_subset_t, cff1_cs_interp_env_t, cff1_cs_opset_subr_subset_t, OpCode_endchar>
369{
370 cff1_subr_subsetter_t (const OT::cff1::accelerator_subset_t &acc_, const hb_subset_plan_t *plan_)
371 : subr_subsetter_t (acc_, plan_) {}
372
373 static void complete_parsed_str (cff1_cs_interp_env_t &env, subr_subset_param_t& param, parsed_cs_str_t &charstring)
374 {
375 /* insert width at the beginning of the charstring as necessary */
376 if (env.has_width)
377 charstring.set_prefix (num: env.width);
378
379 /* subroutines/charstring left on the call stack are legally left unmarked
380 * unmarked when a subroutine terminates with endchar. mark them.
381 */
382 param.current_parsed_str->set_parsed ();
383 for (unsigned int i = 0; i < env.callStack.get_count (); i++)
384 {
385 parsed_cs_str_t *parsed_str = param.get_parsed_str_for_context (context&: env.callStack[i]);
386 if (likely (parsed_str))
387 parsed_str->set_parsed ();
388 else
389 env.set_error ();
390 }
391 }
392};
393
394struct cff_subset_plan {
395 cff_subset_plan ()
396 {
397 for (unsigned int i = 0; i < name_dict_values_t::ValCount; i++)
398 topDictModSIDs[i] = CFF_UNDEF_SID;
399 }
400
401 void plan_subset_encoding (const OT::cff1::accelerator_subset_t &acc, hb_subset_plan_t *plan)
402 {
403 const Encoding *encoding = acc.encoding;
404 unsigned int size0, size1;
405 hb_codepoint_t code, last_code = CFF_UNDEF_CODE;
406 hb_vector_t<hb_codepoint_t> supp_codes;
407
408 if (unlikely (!subset_enc_code_ranges.resize (0)))
409 {
410 plan->check_success (success: false);
411 return;
412 }
413
414 supp_codes.init ();
415
416 subset_enc_num_codes = plan->num_output_glyphs () - 1;
417 unsigned int glyph;
418 for (glyph = 1; glyph < plan->num_output_glyphs (); glyph++)
419 {
420 hb_codepoint_t old_glyph;
421 if (!plan->old_gid_for_new_gid (new_gid: glyph, old_gid: &old_glyph))
422 {
423 /* Retain the code for the old missing glyph ID */
424 old_glyph = glyph;
425 }
426 code = acc.glyph_to_code (glyph: old_glyph);
427 if (code == CFF_UNDEF_CODE)
428 {
429 subset_enc_num_codes = glyph - 1;
430 break;
431 }
432
433 if ((last_code == CFF_UNDEF_CODE) || (code != last_code + 1))
434 {
435 code_pair_t pair = { .code: code, .glyph: glyph };
436 subset_enc_code_ranges.push (v&: pair);
437 }
438 last_code = code;
439
440 if (encoding != &Null (Encoding))
441 {
442 hb_codepoint_t sid = acc.glyph_to_sid (glyph: old_glyph);
443 encoding->get_supplement_codes (sid, codes&: supp_codes);
444 for (unsigned int i = 0; i < supp_codes.length; i++)
445 {
446 code_pair_t pair = { .code: supp_codes[i], .glyph: sid };
447 subset_enc_supp_codes.push (v&: pair);
448 }
449 }
450 }
451 supp_codes.fini ();
452
453 subset_enc_code_ranges.complete (last_glyph: glyph);
454
455 assert (subset_enc_num_codes <= 0xFF);
456 size0 = Encoding0::min_size + HBUINT8::static_size * subset_enc_num_codes;
457 size1 = Encoding1::min_size + Encoding1_Range::static_size * subset_enc_code_ranges.length;
458
459 if (size0 < size1)
460 subset_enc_format = 0;
461 else
462 subset_enc_format = 1;
463 }
464
465 void plan_subset_charset (const OT::cff1::accelerator_subset_t &acc, hb_subset_plan_t *plan)
466 {
467 unsigned int size0, size_ranges;
468 hb_codepoint_t sid, last_sid = CFF_UNDEF_CODE;
469
470 if (unlikely (!subset_charset_ranges.resize (0)))
471 {
472 plan->check_success (success: false);
473 return;
474 }
475
476 hb_map_t *glyph_to_sid_map = (plan->accelerator && plan->accelerator->cff_accelerator) ?
477 plan->accelerator->cff_accelerator->glyph_to_sid_map :
478 nullptr;
479 bool created_map = false;
480 if (!glyph_to_sid_map &&
481 ((plan->accelerator && plan->accelerator->cff_accelerator) ||
482 plan->num_output_glyphs () > plan->source->get_num_glyphs () / 8.))
483 {
484 created_map = true;
485 glyph_to_sid_map = acc.create_glyph_to_sid_map ();
486 }
487
488 unsigned int glyph;
489 for (glyph = 1; glyph < plan->num_output_glyphs (); glyph++)
490 {
491 hb_codepoint_t old_glyph;
492 if (!plan->old_gid_for_new_gid (new_gid: glyph, old_gid: &old_glyph))
493 {
494 /* Retain the SID for the old missing glyph ID */
495 old_glyph = glyph;
496 }
497 sid = glyph_to_sid_map ? glyph_to_sid_map->get (key: old_glyph) : acc.glyph_to_sid (glyph: old_glyph);
498
499 if (!acc.is_CID ())
500 sid = sidmap.add (sid);
501
502 if ((last_sid == CFF_UNDEF_CODE) || (sid != last_sid + 1))
503 {
504 code_pair_t pair = { .code: sid, .glyph: glyph };
505 subset_charset_ranges.push (v&: pair);
506 }
507 last_sid = sid;
508 }
509
510 if (created_map)
511 {
512 if (!(plan->accelerator && plan->accelerator->cff_accelerator) ||
513 !plan->accelerator->cff_accelerator->glyph_to_sid_map.cmpexch (old: nullptr, new_: glyph_to_sid_map))
514 hb_map_destroy (map: glyph_to_sid_map);
515 }
516
517 bool two_byte = subset_charset_ranges.complete (last_glyph: glyph);
518
519 size0 = Charset0::min_size + HBUINT16::static_size * (plan->num_output_glyphs () - 1);
520 if (!two_byte)
521 size_ranges = Charset1::min_size + Charset1_Range::static_size * subset_charset_ranges.length;
522 else
523 size_ranges = Charset2::min_size + Charset2_Range::static_size * subset_charset_ranges.length;
524
525 if (size0 < size_ranges)
526 subset_charset_format = 0;
527 else if (!two_byte)
528 subset_charset_format = 1;
529 else
530 subset_charset_format = 2;
531 }
532
533 bool collect_sids_in_dicts (const OT::cff1::accelerator_subset_t &acc)
534 {
535 sidmap.reset ();
536
537 for (unsigned int i = 0; i < name_dict_values_t::ValCount; i++)
538 {
539 unsigned int sid = acc.topDict.nameSIDs[i];
540 if (sid != CFF_UNDEF_SID)
541 {
542 (void)sidmap.add (sid);
543 topDictModSIDs[i] = sidmap[sid];
544 }
545 }
546
547 if (acc.fdArray != &Null (CFF1FDArray))
548 for (unsigned int i = 0; i < orig_fdcount; i++)
549 if (fdmap.has (lhs: i))
550 (void)sidmap.add (sid: acc.fontDicts[i].fontName);
551
552 return true;
553 }
554
555 bool create (const OT::cff1::accelerator_subset_t &acc,
556 hb_subset_plan_t *plan)
557 {
558 /* make sure notdef is first */
559 hb_codepoint_t old_glyph;
560 if (!plan->old_gid_for_new_gid (new_gid: 0, old_gid: &old_glyph) || (old_glyph != 0)) return false;
561
562 num_glyphs = plan->num_output_glyphs ();
563 orig_fdcount = acc.fdCount;
564 drop_hints = plan->flags & HB_SUBSET_FLAGS_NO_HINTING;
565 desubroutinize = plan->flags & HB_SUBSET_FLAGS_DESUBROUTINIZE;
566
567 /* check whether the subset renumbers any glyph IDs */
568 gid_renum = false;
569 for (hb_codepoint_t new_glyph = 0; new_glyph < plan->num_output_glyphs (); new_glyph++)
570 {
571 if (!plan->old_gid_for_new_gid(new_gid: new_glyph, old_gid: &old_glyph))
572 continue;
573 if (new_glyph != old_glyph) {
574 gid_renum = true;
575 break;
576 }
577 }
578
579 subset_charset = gid_renum || !acc.is_predef_charset ();
580 subset_encoding = !acc.is_CID() && !acc.is_predef_encoding ();
581
582 /* top dict INDEX */
583 {
584 /* Add encoding/charset to a (copy of) top dict as necessary */
585 topdict_mod.init (base_: &acc.topDict);
586 bool need_to_add_enc = (subset_encoding && !acc.topDict.has_op (OpCode_Encoding));
587 bool need_to_add_set = (subset_charset && !acc.topDict.has_op (OpCode_charset));
588 if (need_to_add_enc || need_to_add_set)
589 {
590 if (need_to_add_enc)
591 topdict_mod.add_op (OpCode_Encoding);
592 if (need_to_add_set)
593 topdict_mod.add_op (OpCode_charset);
594 }
595 }
596
597 /* Determine re-mapping of font index as fdmap among other info */
598 if (acc.fdSelect != &Null (CFF1FDSelect))
599 {
600 if (unlikely (!hb_plan_subset_cff_fdselect (plan,
601 orig_fdcount,
602 *acc.fdSelect,
603 subset_fdcount,
604 info.fd_select.size,
605 subset_fdselect_format,
606 subset_fdselect_ranges,
607 fdmap)))
608 return false;
609 }
610 else
611 fdmap.identity (size: 1);
612
613 /* remove unused SIDs & reassign SIDs */
614 {
615 /* SIDs for name strings in dicts are added before glyph names so they fit in 16-bit int range */
616 if (unlikely (!collect_sids_in_dicts (acc)))
617 return false;
618 if (unlikely (sidmap.get_population () > 0x8000)) /* assumption: a dict won't reference that many strings */
619 return false;
620
621 if (subset_charset) plan_subset_charset (acc, plan);
622
623 topdict_mod.reassignSIDs (sidmap);
624 }
625
626 if (desubroutinize)
627 {
628 /* Flatten global & local subrs */
629 subr_flattener_t<const OT::cff1::accelerator_subset_t, cff1_cs_interp_env_t, cff1_cs_opset_flatten_t, OpCode_endchar>
630 flattener(acc, plan);
631 if (!flattener.flatten (flat_charstrings&: subset_charstrings))
632 return false;
633 }
634 else
635 {
636 cff1_subr_subsetter_t subr_subsetter (acc, plan);
637
638 /* Subset subrs: collect used subroutines, leaving all unused ones behind */
639 if (!subr_subsetter.subset ())
640 return false;
641
642 /* encode charstrings, global subrs, local subrs with new subroutine numbers */
643 if (!subr_subsetter.encode_charstrings (buffArray&: subset_charstrings))
644 return false;
645
646 if (!subr_subsetter.encode_globalsubrs (buffArray&: subset_globalsubrs))
647 return false;
648
649 /* local subrs */
650 if (!subset_localsubrs.resize (size_: orig_fdcount))
651 return false;
652 for (unsigned int fd = 0; fd < orig_fdcount; fd++)
653 {
654 subset_localsubrs[fd].init ();
655 if (fdmap.has (lhs: fd))
656 {
657 if (!subr_subsetter.encode_localsubrs (fd, buffArray&: subset_localsubrs[fd]))
658 return false;
659 }
660 }
661 }
662
663 /* Encoding */
664 if (subset_encoding)
665 plan_subset_encoding (acc, plan);
666
667 /* private dicts & local subrs */
668 if (!acc.is_CID ())
669 fontdicts_mod.push (v: cff1_font_dict_values_mod_t ());
670 else
671 {
672 + hb_iter (acc.fontDicts)
673 | hb_filter ([&] (const cff1_font_dict_values_t &_)
674 { return fdmap.has (lhs: &_ - &acc.fontDicts[0]); } )
675 | hb_map ([&] (const cff1_font_dict_values_t &_)
676 {
677 cff1_font_dict_values_mod_t mod;
678 mod.init (base_: &_, fontName_: sidmap[_.fontName]);
679 return mod;
680 })
681 | hb_sink (fontdicts_mod)
682 ;
683 }
684
685 return ((subset_charstrings.length == plan->num_output_glyphs ())
686 && (fontdicts_mod.length == subset_fdcount));
687 }
688
689 cff1_top_dict_values_mod_t topdict_mod;
690 cff1_sub_table_info_t info;
691
692 unsigned int num_glyphs;
693 unsigned int orig_fdcount = 0;
694 unsigned int subset_fdcount = 1;
695 unsigned int subset_fdselect_format = 0;
696 hb_vector_t<code_pair_t> subset_fdselect_ranges;
697
698 /* font dict index remap table from fullset FDArray to subset FDArray.
699 * set to CFF_UNDEF_CODE if excluded from subset */
700 hb_inc_bimap_t fdmap;
701
702 str_buff_vec_t subset_charstrings;
703 str_buff_vec_t subset_globalsubrs;
704 hb_vector_t<str_buff_vec_t> subset_localsubrs;
705 hb_vector_t<cff1_font_dict_values_mod_t> fontdicts_mod;
706
707 bool drop_hints = false;
708
709 bool gid_renum;
710 bool subset_encoding;
711 uint8_t subset_enc_format;
712 unsigned int subset_enc_num_codes;
713 range_list_t subset_enc_code_ranges;
714 hb_vector_t<code_pair_t> subset_enc_supp_codes;
715
716 uint8_t subset_charset_format;
717 range_list_t subset_charset_ranges;
718 bool subset_charset;
719
720 remap_sid_t sidmap;
721 unsigned int topDictModSIDs[name_dict_values_t::ValCount];
722
723 bool desubroutinize = false;
724};
725
726static bool _serialize_cff1 (hb_serialize_context_t *c,
727 cff_subset_plan &plan,
728 const OT::cff1::accelerator_subset_t &acc,
729 unsigned int num_glyphs)
730{
731 /* private dicts & local subrs */
732 for (int i = (int)acc.privateDicts.length; --i >= 0 ;)
733 {
734 if (plan.fdmap.has (lhs: i))
735 {
736 objidx_t subrs_link = 0;
737 if (plan.subset_localsubrs[i].length > 0)
738 {
739 CFF1Subrs *dest = c->start_embed <CFF1Subrs> ();
740 if (unlikely (!dest)) return false;
741 c->push ();
742 if (likely (dest && dest->serialize (c, plan.subset_localsubrs[i])))
743 subrs_link = c->pop_pack ();
744 else
745 {
746 c->pop_discard ();
747 return false;
748 }
749 }
750
751 PrivateDict *pd = c->start_embed<PrivateDict> ();
752 if (unlikely (!pd)) return false;
753 c->push ();
754 cff1_private_dict_op_serializer_t privSzr (plan.desubroutinize, plan.drop_hints);
755 /* N.B. local subrs immediately follows its corresponding private dict. i.e., subr offset == private dict size */
756 if (likely (pd->serialize (c, acc.privateDicts[i], privSzr, subrs_link)))
757 {
758 unsigned fd = plan.fdmap[i];
759 plan.fontdicts_mod[fd].privateDictInfo.size = c->length ();
760 plan.fontdicts_mod[fd].privateDictInfo.link = c->pop_pack ();
761 }
762 else
763 {
764 c->pop_discard ();
765 return false;
766 }
767 }
768 }
769
770 if (!acc.is_CID ())
771 plan.info.privateDictInfo = plan.fontdicts_mod[0].privateDictInfo;
772
773 /* CharStrings */
774 {
775 c->push<CFF1CharStrings> ();
776
777 unsigned total_size = CFF1CharStrings::total_size (iterable: plan.subset_charstrings);
778 if (unlikely (!c->start_zerocopy (total_size)))
779 return false;
780
781 CFF1CharStrings *cs = c->start_embed<CFF1CharStrings> ();
782 if (unlikely (!cs)) return false;
783
784 if (likely (cs->serialize (c, plan.subset_charstrings)))
785 plan.info.char_strings_link = c->pop_pack (share: false);
786 else
787 {
788 c->pop_discard ();
789 return false;
790 }
791 }
792
793 /* FDArray (FD Index) */
794 if (acc.fdArray != &Null (CFF1FDArray))
795 {
796 CFF1FDArray *fda = c->start_embed<CFF1FDArray> ();
797 if (unlikely (!fda)) return false;
798 c->push ();
799 cff1_font_dict_op_serializer_t fontSzr;
800 auto it = + hb_zip (+ hb_iter (plan.fontdicts_mod), + hb_iter (plan.fontdicts_mod));
801 if (likely (fda->serialize (c, it, fontSzr)))
802 plan.info.fd_array_link = c->pop_pack (share: false);
803 else
804 {
805 c->pop_discard ();
806 return false;
807 }
808 }
809
810 /* FDSelect */
811 if (acc.fdSelect != &Null (CFF1FDSelect))
812 {
813 c->push ();
814 if (likely (hb_serialize_cff_fdselect (c, num_glyphs, *acc.fdSelect, acc.fdCount,
815 plan.subset_fdselect_format, plan.info.fd_select.size,
816 plan.subset_fdselect_ranges)))
817 plan.info.fd_select.link = c->pop_pack ();
818 else
819 {
820 c->pop_discard ();
821 return false;
822 }
823 }
824
825 /* Charset */
826 if (plan.subset_charset)
827 {
828 Charset *dest = c->start_embed<Charset> ();
829 if (unlikely (!dest)) return false;
830 c->push ();
831 if (likely (dest->serialize (c,
832 plan.subset_charset_format,
833 plan.num_glyphs,
834 plan.subset_charset_ranges)))
835 plan.info.charset_link = c->pop_pack ();
836 else
837 {
838 c->pop_discard ();
839 return false;
840 }
841 }
842
843 /* Encoding */
844 if (plan.subset_encoding)
845 {
846 Encoding *dest = c->start_embed<Encoding> ();
847 if (unlikely (!dest)) return false;
848 c->push ();
849 if (likely (dest->serialize (c,
850 plan.subset_enc_format,
851 plan.subset_enc_num_codes,
852 plan.subset_enc_code_ranges,
853 plan.subset_enc_supp_codes)))
854 plan.info.encoding_link = c->pop_pack ();
855 else
856 {
857 c->pop_discard ();
858 return false;
859 }
860 }
861
862 /* global subrs */
863 {
864 c->push ();
865 CFF1Subrs *dest = c->start_embed <CFF1Subrs> ();
866 if (unlikely (!dest)) return false;
867 if (likely (dest->serialize (c, plan.subset_globalsubrs)))
868 c->pop_pack (share: false);
869 else
870 {
871 c->pop_discard ();
872 return false;
873 }
874 }
875
876 /* String INDEX */
877 {
878 CFF1StringIndex *dest = c->start_embed<CFF1StringIndex> ();
879 if (unlikely (!dest)) return false;
880 c->push ();
881 if (likely (dest->serialize (c, *acc.stringIndex, plan.sidmap)))
882 c->pop_pack ();
883 else
884 {
885 c->pop_discard ();
886 return false;
887 }
888 }
889
890 OT::cff1 *cff = c->allocate_min<OT::cff1> ();
891 if (unlikely (!cff))
892 return false;
893
894 /* header */
895 cff->version.major = 0x01;
896 cff->version.minor = 0x00;
897 cff->nameIndex = cff->min_size;
898 cff->offSize = 4; /* unused? */
899
900 /* name INDEX */
901 if (unlikely (!(*acc.nameIndex).copy (c))) return false;
902
903 /* top dict INDEX */
904 {
905 /* serialize singleton TopDict */
906 TopDict *top = c->start_embed<TopDict> ();
907 if (!top) return false;
908 c->push ();
909 cff1_top_dict_op_serializer_t topSzr;
910 unsigned top_size = 0;
911 top_dict_modifiers_t modifier (plan.info, plan.topDictModSIDs);
912 if (likely (top->serialize (c, plan.topdict_mod, topSzr, modifier)))
913 {
914 top_size = c->length ();
915 c->pop_pack (share: false);
916 }
917 else
918 {
919 c->pop_discard ();
920 return false;
921 }
922 /* serialize INDEX header for above */
923 CFF1Index *dest = c->start_embed<CFF1Index> ();
924 if (!dest) return false;
925 return dest->serialize_header (c, it: hb_iter (hb_array_t<unsigned> (&top_size, 1)));
926 }
927}
928
929static bool
930_hb_subset_cff1 (const OT::cff1::accelerator_subset_t &acc,
931 hb_subset_context_t *c)
932{
933 cff_subset_plan cff_plan;
934
935 if (unlikely (!cff_plan.create (acc, c->plan)))
936 {
937 DEBUG_MSG(SUBSET, nullptr, "Failed to generate a cff subsetting plan.");
938 return false;
939 }
940
941 return _serialize_cff1 (c: c->serializer, plan&: cff_plan, acc, num_glyphs: c->plan->num_output_glyphs ());
942}
943
944bool
945hb_subset_cff1 (hb_subset_context_t *c)
946{
947 OT::cff1::accelerator_subset_t acc;
948 acc.init (face: c->plan->source);
949 bool result = likely (acc.is_valid ()) && _hb_subset_cff1 (acc, c);
950 acc.fini ();
951
952 return result;
953}
954
955
956#endif
957

source code of flutter_engine/third_party/harfbuzz/src/hb-subset-cff1.cc