2 * Copyright © 2018 Adobe Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Adobe Author(s): Michiharu Ariza
27 #include "hb-open-type.hh"
28 #include "hb-ot-cff1-table.hh"
30 #include "hb-subset-cff1.hh"
31 #include "hb-subset-plan.hh"
32 #include "hb-subset-cff-common.hh"
33 #include "hb-cff1-interp-cs.hh"
37 struct remap_sid_t : remap_t
39 unsigned int add (unsigned int sid)
41 if ((sid != CFF_UNDEF_SID) && !is_std_std (sid))
42 return offset_sid (remap_t::add (unoffset_sid (sid)));
47 unsigned int operator[] (unsigned int sid) const
49 if (is_std_std (sid) || (sid == CFF_UNDEF_SID))
52 return offset_sid (remap_t::operator [] (unoffset_sid (sid)));
55 static const unsigned int num_std_strings = 391;
57 static bool is_std_std (unsigned int sid) { return sid < num_std_strings; }
58 static unsigned int offset_sid (unsigned int sid) { return sid + num_std_strings; }
59 static unsigned int unoffset_sid (unsigned int sid) { return sid - num_std_strings; }
62 struct cff1_sub_table_offsets_t : cff_sub_table_offsets_t
64 cff1_sub_table_offsets_t ()
65 : cff_sub_table_offsets_t (),
69 stringIndexInfo.init ();
71 privateDictInfo.init ();
74 unsigned int nameIndexOffset;
75 table_info_t stringIndexInfo;
76 unsigned int encodingOffset;
77 table_info_t charsetInfo;
78 table_info_t privateDictInfo;
81 /* a copy of a parsed out cff1_top_dict_values_t augmented with additional operators */
82 struct cff1_top_dict_values_mod_t : cff1_top_dict_values_t
84 void init (const cff1_top_dict_values_t *base_= &Null(cff1_top_dict_values_t))
90 void fini () { SUPER::fini (); }
92 unsigned get_count () const { return base->get_count () + SUPER::get_count (); }
93 const cff1_top_dict_val_t &get_value (unsigned int i) const
95 if (i < base->get_count ())
98 return SUPER::values[i - base->get_count ()];
100 const cff1_top_dict_val_t &operator [] (unsigned int i) const { return get_value (i); }
102 void reassignSIDs (const remap_sid_t& sidmap)
104 for (unsigned int i = 0; i < name_dict_values_t::ValCount; i++)
105 nameSIDs[i] = sidmap[base->nameSIDs[i]];
109 typedef cff1_top_dict_values_t SUPER;
110 const cff1_top_dict_values_t *base;
113 struct top_dict_modifiers_t
115 top_dict_modifiers_t (const cff1_sub_table_offsets_t &offsets_,
116 const unsigned int (&nameSIDs_)[name_dict_values_t::ValCount])
117 : offsets (offsets_),
121 const cff1_sub_table_offsets_t &offsets;
122 const unsigned int (&nameSIDs)[name_dict_values_t::ValCount];
125 struct cff1_top_dict_op_serializer_t : cff_top_dict_op_serializer_t<cff1_top_dict_val_t>
127 bool serialize (hb_serialize_context_t *c,
128 const cff1_top_dict_val_t &opstr,
129 const top_dict_modifiers_t &mod) const
131 TRACE_SERIALIZE (this);
133 op_code_t op = opstr.op;
137 return_trace (FontDict::serialize_offset4_op(c, op, mod.offsets.charsetInfo.offset));
139 case OpCode_Encoding:
140 return_trace (FontDict::serialize_offset4_op(c, op, mod.offsets.encodingOffset));
144 if (unlikely (!UnsizedByteStr::serialize_int2 (c, mod.offsets.privateDictInfo.size)))
145 return_trace (false);
146 if (unlikely (!UnsizedByteStr::serialize_int4 (c, mod.offsets.privateDictInfo.offset)))
147 return_trace (false);
148 HBUINT8 *p = c->allocate_size<HBUINT8> (1);
149 if (unlikely (p == nullptr)) return_trace (false);
150 p->set (OpCode_Private);
156 case OpCode_Copyright:
157 case OpCode_FullName:
158 case OpCode_FamilyName:
160 case OpCode_PostScript:
161 case OpCode_BaseFontName:
162 case OpCode_FontName:
163 return_trace (FontDict::serialize_offset2_op(c, op, mod.nameSIDs[name_dict_values_t::name_op_to_index (op)]));
167 /* for registry & ordering, reassigned SIDs are serialized
168 * for supplement, the original byte string is copied along with the op code */
171 if ( unlikely (!(opstr.str.length >= opstr.last_arg_offset + 3)))
172 return_trace (false);
173 supp_op.str = byte_str_t (&opstr.str + opstr.last_arg_offset, opstr.str.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));
179 return_trace (cff_top_dict_op_serializer_t<cff1_top_dict_val_t>::serialize (c, opstr, mod.offsets));
184 unsigned int calculate_serialized_size (const cff1_top_dict_val_t &opstr) const
186 op_code_t op = opstr.op;
190 case OpCode_Encoding:
191 return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (op);
194 return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (OpCode_Private);
198 case OpCode_Copyright:
199 case OpCode_FullName:
200 case OpCode_FamilyName:
202 case OpCode_PostScript:
203 case OpCode_BaseFontName:
204 case OpCode_FontName:
205 return OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (op);
208 return ((OpCode_Size (OpCode_shortint) + 2) * 2) + (opstr.str.length - opstr.last_arg_offset)/* supplement + op */;
211 return cff_top_dict_op_serializer_t<cff1_top_dict_val_t>::calculate_serialized_size (opstr);
216 struct font_dict_values_mod_t
218 void init (const cff1_font_dict_values_t *base_,
219 unsigned int fontName_,
220 const table_info_t &privateDictInfo_)
223 fontName = fontName_;
224 privateDictInfo = privateDictInfo_;
227 unsigned get_count () const { return base->get_count (); }
229 const op_str_t &operator [] (unsigned int i) const { return (*base)[i]; }
231 const cff1_font_dict_values_t *base;
232 table_info_t privateDictInfo;
233 unsigned int fontName;
236 struct cff1_font_dict_op_serializer_t : cff_font_dict_op_serializer_t
238 bool serialize (hb_serialize_context_t *c,
239 const op_str_t &opstr,
240 const font_dict_values_mod_t &mod) const
242 TRACE_SERIALIZE (this);
244 if (opstr.op == OpCode_FontName)
245 return_trace (FontDict::serialize_uint2_op (c, opstr.op, mod.fontName));
247 return_trace (SUPER::serialize (c, opstr, mod.privateDictInfo));
250 unsigned int calculate_serialized_size (const op_str_t &opstr) const
252 if (opstr.op == OpCode_FontName)
253 return OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (OpCode_FontName);
255 return SUPER::calculate_serialized_size (opstr);
259 typedef cff_font_dict_op_serializer_t SUPER;
262 struct cff1_cs_opset_flatten_t : cff1_cs_opset_t<cff1_cs_opset_flatten_t, flatten_param_t>
264 static void flush_args_and_op (op_code_t op, cff1_cs_interp_env_t &env, flatten_param_t& param)
266 if (env.arg_start > 0)
267 flush_width (env, param);
275 case OpCode_hintmask:
276 case OpCode_cntrmask:
277 case OpCode_dotsection:
278 if (param.drop_hints)
286 SUPER::flush_args_and_op (op, env, param);
290 static void flush_args (cff1_cs_interp_env_t &env, flatten_param_t& param)
292 str_encoder_t encoder (param.flatStr);
293 for (unsigned int i = env.arg_start; i < env.argStack.get_count (); i++)
294 encoder.encode_num (env.eval_arg (i));
295 SUPER::flush_args (env, param);
298 static void flush_op (op_code_t op, cff1_cs_interp_env_t &env, flatten_param_t& param)
300 str_encoder_t encoder (param.flatStr);
301 encoder.encode_op (op);
304 static void flush_width (cff1_cs_interp_env_t &env, flatten_param_t& param)
306 assert (env.has_width);
307 str_encoder_t encoder (param.flatStr);
308 encoder.encode_num (env.width);
311 static void flush_hintmask (op_code_t op, cff1_cs_interp_env_t &env, flatten_param_t& param)
313 SUPER::flush_hintmask (op, env, param);
314 if (!param.drop_hints)
316 str_encoder_t encoder (param.flatStr);
317 for (unsigned int i = 0; i < env.hintmask_size; i++)
318 encoder.encode_byte (env.str_ref[i]);
323 typedef cff1_cs_opset_t<cff1_cs_opset_flatten_t, flatten_param_t> SUPER;
326 struct range_list_t : hb_vector_t<code_pair_t>
328 /* replace the first glyph ID in the "glyph" field each range with a nLeft value */
329 bool finalize (unsigned int last_glyph)
331 bool two_byte = false;
332 for (unsigned int i = (*this).length; i > 0; i--)
334 code_pair_t &pair = (*this)[i - 1];
335 unsigned int nLeft = last_glyph - pair.glyph - 1;
338 last_glyph = pair.glyph;
345 struct cff1_cs_opset_subr_subset_t : cff1_cs_opset_t<cff1_cs_opset_subr_subset_t, subr_subset_param_t>
347 static void process_op (op_code_t op, cff1_cs_interp_env_t &env, subr_subset_param_t& param)
352 param.current_parsed_str->add_op (op, env.str_ref);
353 param.current_parsed_str->set_parsed ();
354 env.returnFromSubr ();
355 param.set_current_str (env, false);
359 param.current_parsed_str->add_op (op, env.str_ref);
360 param.current_parsed_str->set_parsed ();
361 SUPER::process_op (op, env, param);
364 case OpCode_callsubr:
365 process_call_subr (op, CSType_LocalSubr, env, param, env.localSubrs, param.local_closure);
368 case OpCode_callgsubr:
369 process_call_subr (op, CSType_GlobalSubr, env, param, env.globalSubrs, param.global_closure);
373 SUPER::process_op (op, env, param);
374 param.current_parsed_str->add_op (op, env.str_ref);
380 static void process_call_subr (op_code_t op, cs_type_t type,
381 cff1_cs_interp_env_t &env, subr_subset_param_t& param,
382 cff1_biased_subrs_t& subrs, hb_set_t *closure)
384 byte_str_ref_t str_ref = env.str_ref;
385 env.callSubr (subrs, type);
386 param.current_parsed_str->add_call_op (op, str_ref, env.context.subr_num);
387 hb_set_add (closure, env.context.subr_num);
388 param.set_current_str (env, true);
392 typedef cff1_cs_opset_t<cff1_cs_opset_subr_subset_t, subr_subset_param_t> SUPER;
395 struct 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>
397 static void finalize_parsed_str (cff1_cs_interp_env_t &env, subr_subset_param_t& param, parsed_cs_str_t &charstring)
399 /* insert width at the beginning of the charstring as necessary */
401 charstring.set_prefix (env.width);
403 /* subroutines/charstring left on the call stack are legally left unmarked
404 * unmarked when a subroutine terminates with endchar. mark them.
406 param.current_parsed_str->set_parsed ();
407 for (unsigned int i = 0; i < env.callStack.get_count (); i++)
409 parsed_cs_str_t *parsed_str = param.get_parsed_str_for_context (env.callStack[i]);
410 if (likely (parsed_str != nullptr))
411 parsed_str->set_parsed ();
418 struct cff_subset_plan {
424 subset_fdselect_format (0),
426 desubroutinize(false)
428 topdict_sizes.init ();
429 topdict_sizes.resize (1);
431 subset_fdselect_ranges.init ();
433 subset_charstrings.init ();
434 subset_globalsubrs.init ();
435 subset_localsubrs.init ();
436 fontdicts_mod.init ();
437 subset_enc_code_ranges.init ();
438 subset_enc_supp_codes.init ();
439 subset_charset_ranges.init ();
441 for (unsigned int i = 0; i < name_dict_values_t::ValCount; i++)
442 topDictModSIDs[i] = CFF_UNDEF_SID;
447 topdict_sizes.fini ();
449 subset_fdselect_ranges.fini ();
451 subset_charstrings.fini_deep ();
452 subset_globalsubrs.fini_deep ();
453 subset_localsubrs.fini_deep ();
454 fontdicts_mod.fini ();
455 subset_enc_code_ranges.fini ();
456 subset_enc_supp_codes.fini ();
457 subset_charset_ranges.fini ();
461 unsigned int plan_subset_encoding (const OT::cff1::accelerator_subset_t &acc, hb_subset_plan_t *plan)
463 const Encoding *encoding = acc.encoding;
464 unsigned int size0, size1, supp_size;
465 hb_codepoint_t code, last_code = CFF_UNDEF_CODE;
466 hb_vector_t<hb_codepoint_t> supp_codes;
468 subset_enc_code_ranges.resize (0);
472 subset_enc_num_codes = plan->glyphs.length - 1;
474 for (glyph = 1; glyph < plan->glyphs.length; glyph++)
476 hb_codepoint_t orig_glyph = plan->glyphs[glyph];
477 code = acc.glyph_to_code (orig_glyph);
478 if (code == CFF_UNDEF_CODE)
480 subset_enc_num_codes = glyph - 1;
484 if (code != last_code + 1)
486 code_pair_t pair = { code, glyph };
487 subset_enc_code_ranges.push (pair);
491 if (encoding != &Null(Encoding))
493 hb_codepoint_t sid = acc.glyph_to_sid (orig_glyph);
494 encoding->get_supplement_codes (sid, supp_codes);
495 for (unsigned int i = 0; i < supp_codes.length; i++)
497 code_pair_t pair = { supp_codes[i], sid };
498 subset_enc_supp_codes.push (pair);
500 supp_size += SuppEncoding::static_size * supp_codes.length;
505 subset_enc_code_ranges.finalize (glyph);
507 assert (subset_enc_num_codes <= 0xFF);
508 size0 = Encoding0::min_size + HBUINT8::static_size * subset_enc_num_codes;
509 size1 = Encoding1::min_size + Encoding1_Range::static_size * subset_enc_code_ranges.length;
512 subset_enc_format = 0;
514 subset_enc_format = 1;
516 return Encoding::calculate_serialized_size (
518 subset_enc_format? subset_enc_code_ranges.length: subset_enc_num_codes,
519 subset_enc_supp_codes.length);
522 unsigned int plan_subset_charset (const OT::cff1::accelerator_subset_t &acc, hb_subset_plan_t *plan)
524 unsigned int size0, size_ranges;
525 hb_codepoint_t sid, last_sid = CFF_UNDEF_CODE;
527 subset_charset_ranges.resize (0);
529 for (glyph = 1; glyph < plan->glyphs.length; glyph++)
531 hb_codepoint_t orig_glyph = plan->glyphs[glyph];
532 sid = acc.glyph_to_sid (orig_glyph);
535 sid = sidmap.add (sid);
537 if (sid != last_sid + 1)
539 code_pair_t pair = { sid, glyph };
540 subset_charset_ranges.push (pair);
545 bool two_byte = subset_charset_ranges.finalize (glyph);
547 size0 = Charset0::min_size + HBUINT16::static_size * (plan->glyphs.length - 1);
549 size_ranges = Charset1::min_size + Charset1_Range::static_size * subset_charset_ranges.length;
551 size_ranges = Charset2::min_size + Charset2_Range::static_size * subset_charset_ranges.length;
553 if (size0 < size_ranges)
554 subset_charset_format = 0;
556 subset_charset_format = 1;
558 subset_charset_format = 2;
560 return Charset::calculate_serialized_size (
561 subset_charset_format,
562 subset_charset_format? subset_charset_ranges.length: plan->glyphs.length);
565 bool collect_sids_in_dicts (const OT::cff1::accelerator_subset_t &acc)
567 if (unlikely (!sidmap.reset (acc.stringIndex->count)))
570 for (unsigned int i = 0; i < name_dict_values_t::ValCount; i++)
572 unsigned int sid = acc.topDict.nameSIDs[i];
573 if (sid != CFF_UNDEF_SID)
575 (void)sidmap.add (sid);
576 topDictModSIDs[i] = sidmap[sid];
580 if (acc.fdArray != &Null(CFF1FDArray))
581 for (unsigned int i = 0; i < orig_fdcount; i++)
582 if (fdmap.includes (i))
583 (void)sidmap.add (acc.fontDicts[i].fontName);
588 bool create (const OT::cff1::accelerator_subset_t &acc,
589 hb_subset_plan_t *plan)
591 /* make sure notdef is first */
592 if ((plan->glyphs.length == 0) || (plan->glyphs[0] != 0)) return false;
595 num_glyphs = plan->glyphs.length;
596 orig_fdcount = acc.fdCount;
597 drop_hints = plan->drop_hints;
598 desubroutinize = plan->desubroutinize;
600 /* check whether the subset renumbers any glyph IDs */
602 for (unsigned int glyph = 0; glyph < plan->glyphs.length; glyph++)
604 if (plan->glyphs[glyph] != glyph) {
610 subset_charset = gid_renum || !acc.is_predef_charset ();
611 subset_encoding = !acc.is_CID() && !acc.is_predef_encoding ();
614 final_size += OT::cff1::static_size;
617 offsets.nameIndexOffset = final_size;
618 final_size += acc.nameIndex->get_size ();
622 /* Add encoding/charset to a (copy of) top dict as necessary */
623 topdict_mod.init (&acc.topDict);
624 bool need_to_add_enc = (subset_encoding && !acc.topDict.has_op (OpCode_Encoding));
625 bool need_to_add_set = (subset_charset && !acc.topDict.has_op (OpCode_charset));
626 if (need_to_add_enc || need_to_add_set)
629 topdict_mod.add_op (OpCode_Encoding);
631 topdict_mod.add_op (OpCode_charset);
633 offsets.topDictInfo.offset = final_size;
634 cff1_top_dict_op_serializer_t topSzr;
635 unsigned int topDictSize = TopDict::calculate_serialized_size (topdict_mod, topSzr);
636 offsets.topDictInfo.offSize = calcOffSize(topDictSize);
637 if (unlikely (offsets.topDictInfo.offSize > 4))
639 final_size += CFF1IndexOf<TopDict>::calculate_serialized_size<cff1_top_dict_values_mod_t>
640 (offsets.topDictInfo.offSize,
641 &topdict_mod, 1, topdict_sizes, topSzr);
644 /* Determine re-mapping of font index as fdmap among other info */
645 if (acc.fdSelect != &Null(CFF1FDSelect))
647 if (unlikely (!hb_plan_subset_cff_fdselect (plan->glyphs,
651 offsets.FDSelectInfo.size,
652 subset_fdselect_format,
653 subset_fdselect_ranges,
660 /* remove unused SIDs & reassign SIDs */
662 /* SIDs for name strings in dicts are added before glyph names so they fit in 16-bit int range */
663 if (unlikely (!collect_sids_in_dicts (acc)))
665 if (unlikely (sidmap.get_count () > 0x8000)) /* assumption: a dict won't reference that many strings */
668 offsets.charsetInfo.size = plan_subset_charset (acc, plan);
670 topdict_mod.reassignSIDs (sidmap);
675 offsets.stringIndexInfo.offset = final_size;
676 offsets.stringIndexInfo.size = acc.stringIndex->calculate_serialized_size (offsets.stringIndexInfo.offSize, sidmap);
677 final_size += offsets.stringIndexInfo.size;
682 /* Flatten global & local subrs */
683 subr_flattener_t<const OT::cff1::accelerator_subset_t, cff1_cs_interp_env_t, cff1_cs_opset_flatten_t>
684 flattener(acc, plan->glyphs, plan->drop_hints);
685 if (!flattener.flatten (subset_charstrings))
688 /* no global/local subroutines */
689 offsets.globalSubrsInfo.size = CFF1Subrs::calculate_serialized_size (1, 0, 0);
693 /* Subset subrs: collect used subroutines, leaving all unused ones behind */
694 if (!subr_subsetter.subset (acc, plan->glyphs, plan->drop_hints))
697 /* encode charstrings, global subrs, local subrs with new subroutine numbers */
698 if (!subr_subsetter.encode_charstrings (acc, plan->glyphs, subset_charstrings))
701 if (!subr_subsetter.encode_globalsubrs (subset_globalsubrs))
705 unsigned int dataSize = subset_globalsubrs.total_size ();
706 offsets.globalSubrsInfo.offSize = calcOffSize (dataSize);
707 if (unlikely (offsets.globalSubrsInfo.offSize > 4))
709 offsets.globalSubrsInfo.size = CFF1Subrs::calculate_serialized_size (offsets.globalSubrsInfo.offSize, subset_globalsubrs.length, dataSize);
712 if (!offsets.localSubrsInfos.resize (orig_fdcount))
714 if (!subset_localsubrs.resize (orig_fdcount))
716 for (unsigned int fd = 0; fd < orig_fdcount; fd++)
718 subset_localsubrs[fd].init ();
719 offsets.localSubrsInfos[fd].init ();
720 if (fdmap.includes (fd))
722 if (!subr_subsetter.encode_localsubrs (fd, subset_localsubrs[fd]))
725 unsigned int dataSize = subset_localsubrs[fd].total_size ();
728 offsets.localSubrsInfos[fd].offset = final_size;
729 offsets.localSubrsInfos[fd].offSize = calcOffSize (dataSize);
730 if (unlikely (offsets.localSubrsInfos[fd].offSize > 4))
732 offsets.localSubrsInfos[fd].size = CFF1Subrs::calculate_serialized_size (offsets.localSubrsInfos[fd].offSize, subset_localsubrs[fd].length, dataSize);
739 offsets.globalSubrsInfo.offset = final_size;
740 final_size += offsets.globalSubrsInfo.size;
743 if (!subset_encoding)
744 offsets.encodingOffset = acc.topDict.EncodingOffset;
747 offsets.encodingOffset = final_size;
748 final_size += plan_subset_encoding (acc, plan);
752 if (!subset_charset && acc.is_predef_charset ())
753 offsets.charsetInfo.offset = acc.topDict.CharsetOffset;
755 offsets.charsetInfo.offset = final_size;
756 final_size += offsets.charsetInfo.size;
759 if (acc.fdSelect != &Null(CFF1FDSelect))
761 offsets.FDSelectInfo.offset = final_size;
762 final_size += offsets.FDSelectInfo.size;
765 /* FDArray (FDIndex) */
766 if (acc.fdArray != &Null(CFF1FDArray)) {
767 offsets.FDArrayInfo.offset = final_size;
768 cff1_font_dict_op_serializer_t fontSzr;
769 unsigned int dictsSize = 0;
770 for (unsigned int i = 0; i < acc.fontDicts.length; i++)
771 if (fdmap.includes (i))
772 dictsSize += FontDict::calculate_serialized_size (acc.fontDicts[i], fontSzr);
774 offsets.FDArrayInfo.offSize = calcOffSize (dictsSize);
775 if (unlikely (offsets.FDArrayInfo.offSize > 4))
777 final_size += CFF1Index::calculate_serialized_size (offsets.FDArrayInfo.offSize, subset_fdcount, dictsSize);
782 offsets.charStringsInfo.offset = final_size;
783 unsigned int dataSize = subset_charstrings.total_size ();
784 offsets.charStringsInfo.offSize = calcOffSize (dataSize);
785 if (unlikely (offsets.charStringsInfo.offSize > 4))
787 final_size += CFF1CharStrings::calculate_serialized_size (offsets.charStringsInfo.offSize, plan->glyphs.length, dataSize);
790 /* private dicts & local subrs */
791 offsets.privateDictInfo.offset = final_size;
792 for (unsigned int i = 0; i < orig_fdcount; i++)
794 if (fdmap.includes (i))
796 bool has_localsubrs = offsets.localSubrsInfos[i].size > 0;
797 cff_private_dict_op_serializer_t privSzr (desubroutinize, plan->drop_hints);
798 unsigned int priv_size = PrivateDict::calculate_serialized_size (acc.privateDicts[i], privSzr, has_localsubrs);
799 table_info_t privInfo = { final_size, priv_size, 0 };
800 font_dict_values_mod_t fontdict_mod;
802 fontdict_mod.init ( &Null(cff1_font_dict_values_t), CFF_UNDEF_SID, privInfo );
804 fontdict_mod.init ( &acc.fontDicts[i], sidmap[acc.fontDicts[i].fontName], privInfo );
805 fontdicts_mod.push (fontdict_mod);
806 final_size += privInfo.size;
808 if (!plan->desubroutinize && has_localsubrs)
810 offsets.localSubrsInfos[i].offset = final_size;
811 final_size += offsets.localSubrsInfos[i].size;
817 offsets.privateDictInfo = fontdicts_mod[0].privateDictInfo;
819 return ((subset_charstrings.length == plan->glyphs.length)
820 && (fontdicts_mod.length == subset_fdcount));
823 unsigned int get_final_size () const { return final_size; }
825 unsigned int final_size;
826 hb_vector_t<unsigned int> topdict_sizes;
827 cff1_top_dict_values_mod_t topdict_mod;
828 cff1_sub_table_offsets_t offsets;
830 unsigned int num_glyphs;
831 unsigned int orig_fdcount;
832 unsigned int subset_fdcount;
833 unsigned int subset_fdselect_format;
834 hb_vector_t<code_pair_t> subset_fdselect_ranges;
836 /* font dict index remap table from fullset FDArray to subset FDArray.
837 * set to CFF_UNDEF_CODE if excluded from subset */
840 str_buff_vec_t subset_charstrings;
841 str_buff_vec_t subset_globalsubrs;
842 hb_vector_t<str_buff_vec_t> subset_localsubrs;
843 hb_vector_t<font_dict_values_mod_t> fontdicts_mod;
848 bool subset_encoding;
849 uint8_t subset_enc_format;
850 unsigned int subset_enc_num_codes;
851 range_list_t subset_enc_code_ranges;
852 hb_vector_t<code_pair_t> subset_enc_supp_codes;
854 uint8_t subset_charset_format;
855 range_list_t subset_charset_ranges;
859 unsigned int topDictModSIDs[name_dict_values_t::ValCount];
862 cff1_subr_subsetter_t subr_subsetter;
865 static inline bool _write_cff1 (const cff_subset_plan &plan,
866 const OT::cff1::accelerator_subset_t &acc,
867 const hb_vector_t<hb_codepoint_t>& glyphs,
868 unsigned int dest_sz,
871 hb_serialize_context_t c (dest, dest_sz);
873 OT::cff1 *cff = c.start_serialize<OT::cff1> ();
874 if (unlikely (!c.extend_min (*cff)))
878 cff->version.major.set (0x01);
879 cff->version.minor.set (0x00);
880 cff->nameIndex.set (cff->min_size);
881 cff->offSize.set (4); /* unused? */
885 assert (cff->nameIndex == (unsigned) (c.head - c.start));
886 CFF1NameIndex *dest = c.start_embed<CFF1NameIndex> ();
887 if (unlikely (dest == nullptr)) return false;
888 if (unlikely (!dest->serialize (&c, *acc.nameIndex)))
890 DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF name INDEX");
897 assert (plan.offsets.topDictInfo.offset == (unsigned) (c.head - c.start));
898 CFF1IndexOf<TopDict> *dest = c.start_embed< CFF1IndexOf<TopDict> > ();
899 if (dest == nullptr) return false;
900 cff1_top_dict_op_serializer_t topSzr;
901 top_dict_modifiers_t modifier (plan.offsets, plan.topDictModSIDs);
902 if (unlikely (!dest->serialize (&c, plan.offsets.topDictInfo.offSize,
903 &plan.topdict_mod, 1,
904 plan.topdict_sizes, topSzr, modifier)))
906 DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF top dict");
913 assert (plan.offsets.stringIndexInfo.offset == (unsigned) (c.head - c.start));
914 CFF1StringIndex *dest = c.start_embed<CFF1StringIndex> ();
915 if (unlikely (dest == nullptr)) return false;
916 if (unlikely (!dest->serialize (&c, *acc.stringIndex, plan.offsets.stringIndexInfo.offSize, plan.sidmap)))
918 DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF string INDEX");
925 assert (plan.offsets.globalSubrsInfo.offset != 0);
926 assert (plan.offsets.globalSubrsInfo.offset == (unsigned) (c.head - c.start));
928 CFF1Subrs *dest = c.start_embed <CFF1Subrs> ();
929 if (unlikely (dest == nullptr)) return false;
930 if (unlikely (!dest->serialize (&c, plan.offsets.globalSubrsInfo.offSize, plan.subset_globalsubrs)))
932 DEBUG_MSG (SUBSET, nullptr, "failed to serialize global subroutines");
938 if (plan.subset_encoding)
940 assert (plan.offsets.encodingOffset == (unsigned) (c.head - c.start));
941 Encoding *dest = c.start_embed<Encoding> ();
942 if (unlikely (dest == nullptr)) return false;
943 if (unlikely (!dest->serialize (&c,
944 plan.subset_enc_format,
945 plan.subset_enc_num_codes,
946 plan.subset_enc_code_ranges,
947 plan.subset_enc_supp_codes)))
949 DEBUG_MSG (SUBSET, nullptr, "failed to serialize Encoding");
955 if (plan.subset_charset)
957 assert (plan.offsets.charsetInfo.offset == (unsigned) (c.head - c.start));
958 Charset *dest = c.start_embed<Charset> ();
959 if (unlikely (dest == nullptr)) return false;
960 if (unlikely (!dest->serialize (&c,
961 plan.subset_charset_format,
963 plan.subset_charset_ranges)))
965 DEBUG_MSG (SUBSET, nullptr, "failed to serialize Charset");
971 if (acc.fdSelect != &Null(CFF1FDSelect))
973 assert (plan.offsets.FDSelectInfo.offset == (unsigned) (c.head - c.start));
975 if (unlikely (!hb_serialize_cff_fdselect (&c, glyphs.length, *acc.fdSelect, acc.fdCount,
976 plan.subset_fdselect_format, plan.offsets.FDSelectInfo.size,
977 plan.subset_fdselect_ranges)))
979 DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF subset FDSelect");
984 /* FDArray (FD Index) */
985 if (acc.fdArray != &Null(CFF1FDArray))
987 assert (plan.offsets.FDArrayInfo.offset == (unsigned) (c.head - c.start));
988 CFF1FDArray *fda = c.start_embed<CFF1FDArray> ();
989 if (unlikely (fda == nullptr)) return false;
990 cff1_font_dict_op_serializer_t fontSzr;
991 if (unlikely (!fda->serialize (&c, plan.offsets.FDArrayInfo.offSize,
995 DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF FDArray");
1002 assert (plan.offsets.charStringsInfo.offset == (unsigned) (c.head - c.start));
1003 CFF1CharStrings *cs = c.start_embed<CFF1CharStrings> ();
1004 if (unlikely (cs == nullptr)) return false;
1005 if (unlikely (!cs->serialize (&c, plan.offsets.charStringsInfo.offSize, plan.subset_charstrings)))
1007 DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF CharStrings");
1012 /* private dicts & local subrs */
1013 assert (plan.offsets.privateDictInfo.offset == (unsigned) (c.head - c.start));
1014 for (unsigned int i = 0; i < acc.privateDicts.length; i++)
1016 if (plan.fdmap.includes (i))
1018 PrivateDict *pd = c.start_embed<PrivateDict> ();
1019 if (unlikely (pd == nullptr)) return false;
1020 unsigned int priv_size = plan.fontdicts_mod[plan.fdmap[i]].privateDictInfo.size;
1022 cff_private_dict_op_serializer_t privSzr (plan.desubroutinize, plan.drop_hints);
1023 /* N.B. local subrs immediately follows its corresponding private dict. i.e., subr offset == private dict size */
1024 unsigned int subroffset = (plan.offsets.localSubrsInfos[i].size > 0)? priv_size: 0;
1025 result = pd->serialize (&c, acc.privateDicts[i], privSzr, subroffset);
1026 if (unlikely (!result))
1028 DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF Private Dict[%d]", i);
1031 if (plan.offsets.localSubrsInfos[i].size > 0)
1033 CFF1Subrs *dest = c.start_embed <CFF1Subrs> ();
1034 if (unlikely (dest == nullptr)) return false;
1035 if (unlikely (!dest->serialize (&c, plan.offsets.localSubrsInfos[i].offSize, plan.subset_localsubrs[i])))
1037 DEBUG_MSG (SUBSET, nullptr, "failed to serialize local subroutines");
1044 assert (c.head == c.end);
1051 _hb_subset_cff1 (const OT::cff1::accelerator_subset_t &acc,
1053 hb_subset_plan_t *plan,
1054 hb_blob_t **prime /* OUT */)
1056 cff_subset_plan cff_plan;
1058 if (unlikely (!cff_plan.create (acc, plan)))
1060 DEBUG_MSG(SUBSET, nullptr, "Failed to generate a cff subsetting plan.");
1064 unsigned int cff_prime_size = cff_plan.get_final_size ();
1065 char *cff_prime_data = (char *) calloc (1, cff_prime_size);
1067 if (unlikely (!_write_cff1 (cff_plan, acc, plan->glyphs,
1068 cff_prime_size, cff_prime_data))) {
1069 DEBUG_MSG(SUBSET, nullptr, "Failed to write a subset cff.");
1070 free (cff_prime_data);
1074 *prime = hb_blob_create (cff_prime_data,
1076 HB_MEMORY_MODE_READONLY,
1084 * Subsets the CFF table according to a provided plan.
1086 * Return value: subsetted cff table.
1089 hb_subset_cff1 (hb_subset_plan_t *plan,
1090 hb_blob_t **prime /* OUT */)
1092 hb_blob_t *cff_blob = hb_sanitize_context_t().reference_table<CFF::cff1> (plan->source);
1093 const char *data = hb_blob_get_data(cff_blob, nullptr);
1095 OT::cff1::accelerator_subset_t acc;
1096 acc.init(plan->source);
1097 bool result = likely (acc.is_valid ()) &&
1098 _hb_subset_cff1 (acc, data, plan, prime);
1099 hb_blob_destroy (cff_blob);