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 
40 using namespace CFF;
41 
42 struct remap_sid_t : hb_inc_bimap_t
43 {
addremap_sid_t44   unsigned int add (unsigned int sid)
45   {
46     if ((sid != CFF_UNDEF_SID) && !is_std_std (sid))
47       return offset_sid (hb_inc_bimap_t::add (unoffset_sid (sid)));
48     else
49       return sid;
50   }
51 
operator []remap_sid_t52   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 (get (unoffset_sid (sid)));
58   }
59 
60   static const unsigned int num_std_strings = 391;
61 
is_std_stdremap_sid_t62   static bool is_std_std (unsigned int sid) { return sid < num_std_strings; }
offset_sidremap_sid_t63   static unsigned int offset_sid (unsigned int sid) { return sid + num_std_strings; }
unoffset_sidremap_sid_t64   static unsigned int unoffset_sid (unsigned int sid) { return sid - num_std_strings; }
65 };
66 
67 struct cff1_sub_table_offsets_t : cff_sub_table_offsets_t
68 {
cff1_sub_table_offsets_tcff1_sub_table_offsets_t69   cff1_sub_table_offsets_t ()
70     : cff_sub_table_offsets_t (),
71       nameIndexOffset (0),
72       encodingOffset (0)
73   {
74     stringIndexInfo.init ();
75     charsetInfo.init ();
76     privateDictInfo.init ();
77   }
78 
79   unsigned int  nameIndexOffset;
80   table_info_t	stringIndexInfo;
81   unsigned int  encodingOffset;
82   table_info_t	charsetInfo;
83   table_info_t	privateDictInfo;
84 };
85 
86 /* a copy of a parsed out cff1_top_dict_values_t augmented with additional operators */
87 struct cff1_top_dict_values_mod_t : cff1_top_dict_values_t
88 {
initcff1_top_dict_values_mod_t89   void init (const cff1_top_dict_values_t *base_= &Null(cff1_top_dict_values_t))
90   {
91     SUPER::init ();
92     base = base_;
93   }
94 
finicff1_top_dict_values_mod_t95   void fini () { SUPER::fini (); }
96 
get_countcff1_top_dict_values_mod_t97   unsigned get_count () const { return base->get_count () + SUPER::get_count (); }
get_valuecff1_top_dict_values_mod_t98   const cff1_top_dict_val_t &get_value (unsigned int i) const
99   {
100     if (i < base->get_count ())
101       return (*base)[i];
102     else
103       return SUPER::values[i - base->get_count ()];
104   }
operator []cff1_top_dict_values_mod_t105   const cff1_top_dict_val_t &operator [] (unsigned int i) const { return get_value (i); }
106 
reassignSIDscff1_top_dict_values_mod_t107   void reassignSIDs (const remap_sid_t& sidmap)
108   {
109     for (unsigned int i = 0; i < name_dict_values_t::ValCount; i++)
110       nameSIDs[i] = sidmap[base->nameSIDs[i]];
111   }
112 
113   protected:
114   typedef cff1_top_dict_values_t SUPER;
115   const cff1_top_dict_values_t *base;
116 };
117 
118 struct top_dict_modifiers_t
119 {
top_dict_modifiers_ttop_dict_modifiers_t120   top_dict_modifiers_t (const cff1_sub_table_offsets_t &offsets_,
121 			   const unsigned int (&nameSIDs_)[name_dict_values_t::ValCount])
122     : offsets (offsets_),
123       nameSIDs (nameSIDs_)
124   {}
125 
126   const cff1_sub_table_offsets_t &offsets;
127   const unsigned int	(&nameSIDs)[name_dict_values_t::ValCount];
128 };
129 
130 struct cff1_top_dict_op_serializer_t : cff_top_dict_op_serializer_t<cff1_top_dict_val_t>
131 {
serializecff1_top_dict_op_serializer_t132   bool serialize (hb_serialize_context_t *c,
133 		  const cff1_top_dict_val_t &opstr,
134 		  const top_dict_modifiers_t &mod) const
135   {
136     TRACE_SERIALIZE (this);
137 
138     op_code_t op = opstr.op;
139     switch (op)
140     {
141       case OpCode_charset:
142 	return_trace (FontDict::serialize_offset4_op(c, op, mod.offsets.charsetInfo.offset));
143 
144       case OpCode_Encoding:
145 	return_trace (FontDict::serialize_offset4_op(c, op, mod.offsets.encodingOffset));
146 
147       case OpCode_Private:
148 	{
149 	  if (unlikely (!UnsizedByteStr::serialize_int2 (c, mod.offsets.privateDictInfo.size)))
150 	    return_trace (false);
151 	  if (unlikely (!UnsizedByteStr::serialize_int4 (c, mod.offsets.privateDictInfo.offset)))
152 	    return_trace (false);
153 	  HBUINT8 *p = c->allocate_size<HBUINT8> (1);
154 	  if (unlikely (p == nullptr)) return_trace (false);
155 	  *p = OpCode_Private;
156 	}
157 	break;
158 
159       case OpCode_version:
160       case OpCode_Notice:
161       case OpCode_Copyright:
162       case OpCode_FullName:
163       case OpCode_FamilyName:
164       case OpCode_Weight:
165       case OpCode_PostScript:
166       case OpCode_BaseFontName:
167       case OpCode_FontName:
168 	return_trace (FontDict::serialize_offset2_op(c, op, mod.nameSIDs[name_dict_values_t::name_op_to_index (op)]));
169 
170       case OpCode_ROS:
171 	{
172 	  /* for registry & ordering, reassigned SIDs are serialized
173 	   * for supplement, the original byte string is copied along with the op code */
174 	  op_str_t supp_op;
175 	  supp_op.op = op;
176 	  if ( unlikely (!(opstr.str.length >= opstr.last_arg_offset + 3)))
177 	    return_trace (false);
178 	  supp_op.str = byte_str_t (&opstr.str + opstr.last_arg_offset, opstr.str.length - opstr.last_arg_offset);
179 	  return_trace (UnsizedByteStr::serialize_int2 (c, mod.nameSIDs[name_dict_values_t::registry]) &&
180 			UnsizedByteStr::serialize_int2 (c, mod.nameSIDs[name_dict_values_t::ordering]) &&
181 			copy_opstr (c, supp_op));
182 	}
183       default:
184 	return_trace (cff_top_dict_op_serializer_t<cff1_top_dict_val_t>::serialize (c, opstr, mod.offsets));
185     }
186     return_trace (true);
187   }
188 
calculate_serialized_sizecff1_top_dict_op_serializer_t189   unsigned int calculate_serialized_size (const cff1_top_dict_val_t &opstr) const
190   {
191     op_code_t op = opstr.op;
192     switch (op)
193     {
194       case OpCode_charset:
195       case OpCode_Encoding:
196 	return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (op);
197 
198       case OpCode_Private:
199 	return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (OpCode_Private);
200 
201       case OpCode_version:
202       case OpCode_Notice:
203       case OpCode_Copyright:
204       case OpCode_FullName:
205       case OpCode_FamilyName:
206       case OpCode_Weight:
207       case OpCode_PostScript:
208       case OpCode_BaseFontName:
209       case OpCode_FontName:
210 	return OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (op);
211 
212       case OpCode_ROS:
213 	return ((OpCode_Size (OpCode_shortint) + 2) * 2) + (opstr.str.length - opstr.last_arg_offset)/* supplement + op */;
214 
215       default:
216 	return cff_top_dict_op_serializer_t<cff1_top_dict_val_t>::calculate_serialized_size (opstr);
217     }
218   }
219 };
220 
221 struct font_dict_values_mod_t
222 {
initfont_dict_values_mod_t223   void init (const cff1_font_dict_values_t *base_,
224 	     unsigned int fontName_,
225 	     const table_info_t &privateDictInfo_)
226   {
227     base = base_;
228     fontName = fontName_;
229     privateDictInfo = privateDictInfo_;
230   }
231 
get_countfont_dict_values_mod_t232   unsigned get_count () const { return base->get_count (); }
233 
operator []font_dict_values_mod_t234   const op_str_t &operator [] (unsigned int i) const { return (*base)[i]; }
235 
236   const cff1_font_dict_values_t    *base;
237   table_info_t		   privateDictInfo;
238   unsigned int		fontName;
239 };
240 
241 struct cff1_font_dict_op_serializer_t : cff_font_dict_op_serializer_t
242 {
serializecff1_font_dict_op_serializer_t243   bool serialize (hb_serialize_context_t *c,
244 		  const op_str_t &opstr,
245 		  const font_dict_values_mod_t &mod) const
246   {
247     TRACE_SERIALIZE (this);
248 
249     if (opstr.op == OpCode_FontName)
250       return_trace (FontDict::serialize_uint2_op (c, opstr.op, mod.fontName));
251     else
252       return_trace (SUPER::serialize (c, opstr, mod.privateDictInfo));
253   }
254 
calculate_serialized_sizecff1_font_dict_op_serializer_t255   unsigned int calculate_serialized_size (const op_str_t &opstr) const
256   {
257     if (opstr.op == OpCode_FontName)
258       return OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (OpCode_FontName);
259     else
260       return SUPER::calculate_serialized_size (opstr);
261   }
262 
263   private:
264   typedef cff_font_dict_op_serializer_t SUPER;
265 };
266 
267 struct cff1_cs_opset_flatten_t : cff1_cs_opset_t<cff1_cs_opset_flatten_t, flatten_param_t>
268 {
flush_args_and_opcff1_cs_opset_flatten_t269   static void flush_args_and_op (op_code_t op, cff1_cs_interp_env_t &env, flatten_param_t& param)
270   {
271     if (env.arg_start > 0)
272       flush_width (env, param);
273 
274     switch (op)
275     {
276       case OpCode_hstem:
277       case OpCode_hstemhm:
278       case OpCode_vstem:
279       case OpCode_vstemhm:
280       case OpCode_hintmask:
281       case OpCode_cntrmask:
282       case OpCode_dotsection:
283 	if (param.drop_hints)
284 	{
285 	  env.clear_args ();
286 	  return;
287 	}
288 	HB_FALLTHROUGH;
289 
290       default:
291 	SUPER::flush_args_and_op (op, env, param);
292 	break;
293     }
294   }
flush_argscff1_cs_opset_flatten_t295   static void flush_args (cff1_cs_interp_env_t &env, flatten_param_t& param)
296   {
297     str_encoder_t  encoder (param.flatStr);
298     for (unsigned int i = env.arg_start; i < env.argStack.get_count (); i++)
299       encoder.encode_num (env.eval_arg (i));
300     SUPER::flush_args (env, param);
301   }
302 
flush_opcff1_cs_opset_flatten_t303   static void flush_op (op_code_t op, cff1_cs_interp_env_t &env, flatten_param_t& param)
304   {
305     str_encoder_t  encoder (param.flatStr);
306     encoder.encode_op (op);
307   }
308 
flush_widthcff1_cs_opset_flatten_t309   static void flush_width (cff1_cs_interp_env_t &env, flatten_param_t& param)
310   {
311     assert (env.has_width);
312     str_encoder_t  encoder (param.flatStr);
313     encoder.encode_num (env.width);
314   }
315 
flush_hintmaskcff1_cs_opset_flatten_t316   static void flush_hintmask (op_code_t op, cff1_cs_interp_env_t &env, flatten_param_t& param)
317   {
318     SUPER::flush_hintmask (op, env, param);
319     if (!param.drop_hints)
320     {
321       str_encoder_t  encoder (param.flatStr);
322       for (unsigned int i = 0; i < env.hintmask_size; i++)
323 	encoder.encode_byte (env.str_ref[i]);
324     }
325   }
326 
327   private:
328   typedef cff1_cs_opset_t<cff1_cs_opset_flatten_t, flatten_param_t> SUPER;
329 };
330 
331 struct range_list_t : hb_vector_t<code_pair_t>
332 {
333   /* replace the first glyph ID in the "glyph" field each range with a nLeft value */
finalizerange_list_t334   bool finalize (unsigned int last_glyph)
335   {
336     bool  two_byte = false;
337     for (unsigned int i = (*this).length; i > 0; i--)
338     {
339       code_pair_t &pair = (*this)[i - 1];
340       unsigned int  nLeft = last_glyph - pair.glyph - 1;
341       if (nLeft >= 0x100)
342 	two_byte = true;
343       last_glyph = pair.glyph;
344       pair.glyph = nLeft;
345     }
346     return two_byte;
347   }
348 };
349 
350 struct cff1_cs_opset_subr_subset_t : cff1_cs_opset_t<cff1_cs_opset_subr_subset_t, subr_subset_param_t>
351 {
process_opcff1_cs_opset_subr_subset_t352   static void process_op (op_code_t op, cff1_cs_interp_env_t &env, subr_subset_param_t& param)
353   {
354     switch (op) {
355 
356       case OpCode_return:
357 	param.current_parsed_str->add_op (op, env.str_ref);
358 	param.current_parsed_str->set_parsed ();
359 	env.return_from_subr ();
360 	param.set_current_str (env, false);
361 	break;
362 
363       case OpCode_endchar:
364 	param.current_parsed_str->add_op (op, env.str_ref);
365 	param.current_parsed_str->set_parsed ();
366 	SUPER::process_op (op, env, param);
367 	break;
368 
369       case OpCode_callsubr:
370 	process_call_subr (op, CSType_LocalSubr, env, param, env.localSubrs, param.local_closure);
371 	break;
372 
373       case OpCode_callgsubr:
374 	process_call_subr (op, CSType_GlobalSubr, env, param, env.globalSubrs, param.global_closure);
375 	break;
376 
377       default:
378 	SUPER::process_op (op, env, param);
379 	param.current_parsed_str->add_op (op, env.str_ref);
380 	break;
381     }
382   }
383 
384   protected:
process_call_subrcff1_cs_opset_subr_subset_t385   static void process_call_subr (op_code_t op, cs_type_t type,
386 				 cff1_cs_interp_env_t &env, subr_subset_param_t& param,
387 				 cff1_biased_subrs_t& subrs, hb_set_t *closure)
388   {
389     byte_str_ref_t    str_ref = env.str_ref;
390     env.call_subr (subrs, type);
391     param.current_parsed_str->add_call_op (op, str_ref, env.context.subr_num);
392     closure->add (env.context.subr_num);
393     param.set_current_str (env, true);
394   }
395 
396   private:
397   typedef cff1_cs_opset_t<cff1_cs_opset_subr_subset_t, subr_subset_param_t> SUPER;
398 };
399 
400 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, OpCode_endchar>
401 {
cff1_subr_subsetter_tcff1_subr_subsetter_t402   cff1_subr_subsetter_t (const OT::cff1::accelerator_subset_t &acc_, const hb_subset_plan_t *plan_)
403     : subr_subsetter_t (acc_, plan_) {}
404 
finalize_parsed_strcff1_subr_subsetter_t405   static void finalize_parsed_str (cff1_cs_interp_env_t &env, subr_subset_param_t& param, parsed_cs_str_t &charstring)
406   {
407     /* insert width at the beginning of the charstring as necessary */
408     if (env.has_width)
409       charstring.set_prefix (env.width);
410 
411     /* subroutines/charstring left on the call stack are legally left unmarked
412      * unmarked when a subroutine terminates with endchar. mark them.
413      */
414     param.current_parsed_str->set_parsed ();
415     for (unsigned int i = 0; i < env.callStack.get_count (); i++)
416     {
417       parsed_cs_str_t  *parsed_str = param.get_parsed_str_for_context (env.callStack[i]);
418       if (likely (parsed_str != nullptr))
419 	parsed_str->set_parsed ();
420       else
421 	env.set_error ();
422     }
423   }
424 };
425 
426 struct cff_subset_plan {
cff_subset_plancff_subset_plan427   cff_subset_plan ()
428     : final_size (0),
429       offsets (),
430       orig_fdcount (0),
431       subset_fdcount (1),
432       subset_fdselect_format (0),
433       drop_hints (false),
434       desubroutinize(false)
435   {
436     topdict_sizes.init ();
437     topdict_sizes.resize (1);
438     topdict_mod.init ();
439     subset_fdselect_ranges.init ();
440     fdmap.init ();
441     subset_charstrings.init ();
442     subset_globalsubrs.init ();
443     subset_localsubrs.init ();
444     fontdicts_mod.init ();
445     subset_enc_code_ranges.init ();
446     subset_enc_supp_codes.init ();
447     subset_charset_ranges.init ();
448     sidmap.init ();
449     for (unsigned int i = 0; i < name_dict_values_t::ValCount; i++)
450       topDictModSIDs[i] = CFF_UNDEF_SID;
451   }
452 
~cff_subset_plancff_subset_plan453   ~cff_subset_plan ()
454   {
455     topdict_sizes.fini ();
456     topdict_mod.fini ();
457     subset_fdselect_ranges.fini ();
458     fdmap.fini ();
459     subset_charstrings.fini_deep ();
460     subset_globalsubrs.fini_deep ();
461     subset_localsubrs.fini_deep ();
462     fontdicts_mod.fini ();
463     subset_enc_code_ranges.fini ();
464     subset_enc_supp_codes.fini ();
465     subset_charset_ranges.fini ();
466     sidmap.fini ();
467   }
468 
plan_subset_encodingcff_subset_plan469   unsigned int plan_subset_encoding (const OT::cff1::accelerator_subset_t &acc, hb_subset_plan_t *plan)
470   {
471     const Encoding *encoding = acc.encoding;
472     unsigned int  size0, size1, supp_size;
473     hb_codepoint_t  code, last_code = CFF_UNDEF_CODE;
474     hb_vector_t<hb_codepoint_t> supp_codes;
475 
476     subset_enc_code_ranges.resize (0);
477     supp_size = 0;
478     supp_codes.init ();
479 
480     subset_enc_num_codes = plan->num_output_glyphs () - 1;
481     unsigned int glyph;
482     for (glyph = 1; glyph < plan->num_output_glyphs (); glyph++)
483     {
484       hb_codepoint_t  old_glyph;
485       if (!plan->old_gid_for_new_gid (glyph, &old_glyph))
486       {
487       	/* Retain the code for the old missing glyph ID */
488 	old_glyph = glyph;
489       }
490       code = acc.glyph_to_code (old_glyph);
491       if (code == CFF_UNDEF_CODE)
492       {
493 	subset_enc_num_codes = glyph - 1;
494 	break;
495       }
496 
497       if ((last_code == CFF_UNDEF_CODE) || (code != last_code + 1))
498       {
499 	code_pair_t pair = { code, glyph };
500 	subset_enc_code_ranges.push (pair);
501       }
502       last_code = code;
503 
504       if (encoding != &Null(Encoding))
505       {
506 	hb_codepoint_t  sid = acc.glyph_to_sid (old_glyph);
507 	encoding->get_supplement_codes (sid, supp_codes);
508 	for (unsigned int i = 0; i < supp_codes.length; i++)
509 	{
510 	  code_pair_t pair = { supp_codes[i], sid };
511 	  subset_enc_supp_codes.push (pair);
512 	}
513 	supp_size += SuppEncoding::static_size * supp_codes.length;
514       }
515     }
516     supp_codes.fini ();
517 
518     subset_enc_code_ranges.finalize (glyph);
519 
520     assert (subset_enc_num_codes <= 0xFF);
521     size0 = Encoding0::min_size + HBUINT8::static_size * subset_enc_num_codes;
522     size1 = Encoding1::min_size + Encoding1_Range::static_size * subset_enc_code_ranges.length;
523 
524     if (size0 < size1)
525       subset_enc_format = 0;
526     else
527       subset_enc_format = 1;
528 
529     return Encoding::calculate_serialized_size (
530 			subset_enc_format,
531 			subset_enc_format? subset_enc_code_ranges.length: subset_enc_num_codes,
532 			subset_enc_supp_codes.length);
533   }
534 
plan_subset_charsetcff_subset_plan535   unsigned int plan_subset_charset (const OT::cff1::accelerator_subset_t &acc, hb_subset_plan_t *plan)
536   {
537     unsigned int  size0, size_ranges;
538     hb_codepoint_t  sid, last_sid = CFF_UNDEF_CODE;
539 
540     subset_charset_ranges.resize (0);
541     unsigned int glyph;
542     for (glyph = 1; glyph < plan->num_output_glyphs (); glyph++)
543     {
544       hb_codepoint_t  old_glyph;
545       if (!plan->old_gid_for_new_gid (glyph, &old_glyph))
546       {
547       	/* Retain the SID for the old missing glyph ID */
548 	old_glyph = glyph;
549       }
550       sid = acc.glyph_to_sid (old_glyph);
551 
552       if (!acc.is_CID ())
553 	sid = sidmap.add (sid);
554 
555       if ((last_sid == CFF_UNDEF_CODE) || (sid != last_sid + 1))
556       {
557 	code_pair_t pair = { sid, glyph };
558 	subset_charset_ranges.push (pair);
559       }
560       last_sid = sid;
561     }
562 
563     bool two_byte = subset_charset_ranges.finalize (glyph);
564 
565     size0 = Charset0::min_size + HBUINT16::static_size * (plan->num_output_glyphs () - 1);
566     if (!two_byte)
567       size_ranges = Charset1::min_size + Charset1_Range::static_size * subset_charset_ranges.length;
568     else
569       size_ranges = Charset2::min_size + Charset2_Range::static_size * subset_charset_ranges.length;
570 
571     if (size0 < size_ranges)
572       subset_charset_format = 0;
573     else if (!two_byte)
574       subset_charset_format = 1;
575     else
576       subset_charset_format = 2;
577 
578     return Charset::calculate_serialized_size (
579 			subset_charset_format,
580 			subset_charset_format? subset_charset_ranges.length: plan->num_output_glyphs ());
581   }
582 
collect_sids_in_dictscff_subset_plan583   bool collect_sids_in_dicts (const OT::cff1::accelerator_subset_t &acc)
584   {
585     sidmap.reset ();
586 
587     for (unsigned int i = 0; i < name_dict_values_t::ValCount; i++)
588     {
589       unsigned int sid = acc.topDict.nameSIDs[i];
590       if (sid != CFF_UNDEF_SID)
591       {
592 	(void)sidmap.add (sid);
593 	topDictModSIDs[i] = sidmap[sid];
594       }
595     }
596 
597     if (acc.fdArray != &Null(CFF1FDArray))
598       for (unsigned int i = 0; i < orig_fdcount; i++)
599 	if (fdmap.has (i))
600 	  (void)sidmap.add (acc.fontDicts[i].fontName);
601 
602     return true;
603   }
604 
createcff_subset_plan605   bool create (const OT::cff1::accelerator_subset_t &acc,
606 	       hb_subset_plan_t *plan)
607   {
608     /* make sure notdef is first */
609     hb_codepoint_t old_glyph;
610     if (!plan->old_gid_for_new_gid (0, &old_glyph) || (old_glyph != 0)) return false;
611 
612     final_size = 0;
613     num_glyphs = plan->num_output_glyphs ();
614     orig_fdcount = acc.fdCount;
615     drop_hints = plan->drop_hints;
616     desubroutinize = plan->desubroutinize;
617 
618     /* check whether the subset renumbers any glyph IDs */
619     gid_renum = false;
620     for (hb_codepoint_t new_glyph = 0; new_glyph < plan->num_output_glyphs (); new_glyph++)
621     {
622       if (!plan->old_gid_for_new_gid(new_glyph, &old_glyph))
623       	continue;
624       if (new_glyph != old_glyph) {
625 	gid_renum = true;
626 	break;
627       }
628     }
629 
630     subset_charset = gid_renum || !acc.is_predef_charset ();
631     subset_encoding = !acc.is_CID() && !acc.is_predef_encoding ();
632 
633     /* CFF header */
634     final_size += OT::cff1::static_size;
635 
636     /* Name INDEX */
637     offsets.nameIndexOffset = final_size;
638     final_size += acc.nameIndex->get_size ();
639 
640     /* top dict INDEX */
641     {
642       /* Add encoding/charset to a (copy of) top dict as necessary */
643       topdict_mod.init (&acc.topDict);
644       bool need_to_add_enc = (subset_encoding && !acc.topDict.has_op (OpCode_Encoding));
645       bool need_to_add_set = (subset_charset && !acc.topDict.has_op (OpCode_charset));
646       if (need_to_add_enc || need_to_add_set)
647       {
648 	if (need_to_add_enc)
649 	  topdict_mod.add_op (OpCode_Encoding);
650 	if (need_to_add_set)
651 	  topdict_mod.add_op (OpCode_charset);
652       }
653       offsets.topDictInfo.offset = final_size;
654       cff1_top_dict_op_serializer_t topSzr;
655       unsigned int topDictSize = TopDict::calculate_serialized_size (topdict_mod, topSzr);
656       offsets.topDictInfo.offSize = calcOffSize(topDictSize);
657       if (unlikely (offsets.topDictInfo.offSize > 4))
658       	return false;
659       final_size += CFF1IndexOf<TopDict>::calculate_serialized_size<cff1_top_dict_values_mod_t>
660 						(offsets.topDictInfo.offSize,
661 						 &topdict_mod, 1, topdict_sizes, topSzr);
662     }
663 
664     /* Determine re-mapping of font index as fdmap among other info */
665     if (acc.fdSelect != &Null(CFF1FDSelect))
666     {
667 	if (unlikely (!hb_plan_subset_cff_fdselect (plan,
668 				  orig_fdcount,
669 				  *acc.fdSelect,
670 				  subset_fdcount,
671 				  offsets.FDSelectInfo.size,
672 				  subset_fdselect_format,
673 				  subset_fdselect_ranges,
674 				  fdmap)))
675 	return false;
676     }
677     else
678       fdmap.identity (1);
679 
680     /* remove unused SIDs & reassign SIDs */
681     {
682       /* SIDs for name strings in dicts are added before glyph names so they fit in 16-bit int range */
683       if (unlikely (!collect_sids_in_dicts (acc)))
684 	return false;
685       if (unlikely (sidmap.get_population () > 0x8000))	/* assumption: a dict won't reference that many strings */
686       	return false;
687       if (subset_charset)
688 	offsets.charsetInfo.size = plan_subset_charset (acc, plan);
689 
690       topdict_mod.reassignSIDs (sidmap);
691     }
692 
693     /* String INDEX */
694     {
695       offsets.stringIndexInfo.offset = final_size;
696       offsets.stringIndexInfo.size = acc.stringIndex->calculate_serialized_size (offsets.stringIndexInfo.offSize, sidmap);
697       final_size += offsets.stringIndexInfo.size;
698     }
699 
700     if (desubroutinize)
701     {
702       /* Flatten global & local subrs */
703       subr_flattener_t<const OT::cff1::accelerator_subset_t, cff1_cs_interp_env_t, cff1_cs_opset_flatten_t, OpCode_endchar>
704 		    flattener(acc, plan);
705       if (!flattener.flatten (subset_charstrings))
706 	return false;
707 
708       /* no global/local subroutines */
709       offsets.globalSubrsInfo.size = CFF1Subrs::calculate_serialized_size (1, 0, 0);
710     }
711     else
712     {
713       cff1_subr_subsetter_t       subr_subsetter (acc, plan);
714 
715       /* Subset subrs: collect used subroutines, leaving all unused ones behind */
716       if (!subr_subsetter.subset ())
717 	return false;
718 
719       /* encode charstrings, global subrs, local subrs with new subroutine numbers */
720       if (!subr_subsetter.encode_charstrings (subset_charstrings))
721 	return false;
722 
723       if (!subr_subsetter.encode_globalsubrs (subset_globalsubrs))
724 	return false;
725 
726       /* global subrs */
727       unsigned int dataSize = subset_globalsubrs.total_size ();
728       offsets.globalSubrsInfo.offSize = calcOffSize (dataSize);
729       if (unlikely (offsets.globalSubrsInfo.offSize > 4))
730       	return false;
731       offsets.globalSubrsInfo.size = CFF1Subrs::calculate_serialized_size (offsets.globalSubrsInfo.offSize, subset_globalsubrs.length, dataSize);
732 
733       /* local subrs */
734       if (!offsets.localSubrsInfos.resize (orig_fdcount))
735 	return false;
736       if (!subset_localsubrs.resize (orig_fdcount))
737 	return false;
738       for (unsigned int fd = 0; fd < orig_fdcount; fd++)
739       {
740 	subset_localsubrs[fd].init ();
741 	offsets.localSubrsInfos[fd].init ();
742 	if (fdmap.has (fd))
743 	{
744 	  if (!subr_subsetter.encode_localsubrs (fd, subset_localsubrs[fd]))
745 	    return false;
746 
747 	  unsigned int dataSize = subset_localsubrs[fd].total_size ();
748 	  if (dataSize > 0)
749 	  {
750 	    offsets.localSubrsInfos[fd].offset = final_size;
751 	    offsets.localSubrsInfos[fd].offSize = calcOffSize (dataSize);
752 	    if (unlikely (offsets.localSubrsInfos[fd].offSize > 4))
753 	      return false;
754 	    offsets.localSubrsInfos[fd].size = CFF1Subrs::calculate_serialized_size (offsets.localSubrsInfos[fd].offSize, subset_localsubrs[fd].length, dataSize);
755 	  }
756 	}
757       }
758     }
759 
760     /* global subrs */
761     offsets.globalSubrsInfo.offset = final_size;
762     final_size += offsets.globalSubrsInfo.size;
763 
764     /* Encoding */
765     if (!subset_encoding)
766       offsets.encodingOffset = acc.topDict.EncodingOffset;
767     else
768     {
769       offsets.encodingOffset = final_size;
770       final_size += plan_subset_encoding (acc, plan);
771     }
772 
773     /* Charset */
774     if (!subset_charset && acc.is_predef_charset ())
775       offsets.charsetInfo.offset = acc.topDict.CharsetOffset;
776     else
777       offsets.charsetInfo.offset = final_size;
778     final_size += offsets.charsetInfo.size;
779 
780     /* FDSelect */
781     if (acc.fdSelect != &Null(CFF1FDSelect))
782     {
783       offsets.FDSelectInfo.offset = final_size;
784       final_size += offsets.FDSelectInfo.size;
785     }
786 
787     /* FDArray (FDIndex) */
788     if (acc.fdArray != &Null(CFF1FDArray)) {
789       offsets.FDArrayInfo.offset = final_size;
790       cff1_font_dict_op_serializer_t fontSzr;
791       unsigned int dictsSize = 0;
792       for (unsigned int i = 0; i < acc.fontDicts.length; i++)
793 	if (fdmap.has (i))
794 	  dictsSize += FontDict::calculate_serialized_size (acc.fontDicts[i], fontSzr);
795 
796       offsets.FDArrayInfo.offSize = calcOffSize (dictsSize);
797       if (unlikely (offsets.FDArrayInfo.offSize > 4))
798       	return false;
799       final_size += CFF1Index::calculate_serialized_size (offsets.FDArrayInfo.offSize, subset_fdcount, dictsSize);
800     }
801 
802     /* CharStrings */
803     {
804       offsets.charStringsInfo.offset = final_size;
805       unsigned int dataSize = subset_charstrings.total_size ();
806       offsets.charStringsInfo.offSize = calcOffSize (dataSize);
807       if (unlikely (offsets.charStringsInfo.offSize > 4))
808       	return false;
809       final_size += CFF1CharStrings::calculate_serialized_size (offsets.charStringsInfo.offSize, plan->num_output_glyphs (), dataSize);
810     }
811 
812     /* private dicts & local subrs */
813     offsets.privateDictInfo.offset = final_size;
814     for (unsigned int i = 0; i < orig_fdcount; i++)
815     {
816       if (fdmap.has (i))
817       {
818 	bool  has_localsubrs = offsets.localSubrsInfos[i].size > 0;
819 	cff_private_dict_op_serializer_t privSzr (desubroutinize, plan->drop_hints);
820 	unsigned int  priv_size = PrivateDict::calculate_serialized_size (acc.privateDicts[i], privSzr, has_localsubrs);
821 	table_info_t  privInfo = { final_size, priv_size, 0 };
822 	font_dict_values_mod_t fontdict_mod;
823 	if (!acc.is_CID ())
824 	  fontdict_mod.init ( &Null(cff1_font_dict_values_t), CFF_UNDEF_SID, privInfo );
825 	else
826 	  fontdict_mod.init ( &acc.fontDicts[i], sidmap[acc.fontDicts[i].fontName], privInfo );
827 	fontdicts_mod.push (fontdict_mod);
828 	final_size += privInfo.size;
829 
830 	if (!plan->desubroutinize && has_localsubrs)
831 	{
832 	  offsets.localSubrsInfos[i].offset = final_size;
833 	  final_size += offsets.localSubrsInfos[i].size;
834 	}
835       }
836     }
837 
838     if (!acc.is_CID ())
839       offsets.privateDictInfo = fontdicts_mod[0].privateDictInfo;
840 
841     return ((subset_charstrings.length == plan->num_output_glyphs ())
842 	   && (fontdicts_mod.length == subset_fdcount));
843   }
844 
get_final_sizecff_subset_plan845   unsigned int get_final_size () const  { return final_size; }
846 
847   unsigned int	      final_size;
848   hb_vector_t<unsigned int>	topdict_sizes;
849   cff1_top_dict_values_mod_t	topdict_mod;
850   cff1_sub_table_offsets_t	offsets;
851 
852   unsigned int    num_glyphs;
853   unsigned int    orig_fdcount;
854   unsigned int    subset_fdcount;
855   unsigned int    subset_fdselect_format;
856   hb_vector_t<code_pair_t>   subset_fdselect_ranges;
857 
858   /* font dict index remap table from fullset FDArray to subset FDArray.
859    * set to CFF_UNDEF_CODE if excluded from subset */
860   hb_inc_bimap_t   fdmap;
861 
862   str_buff_vec_t		subset_charstrings;
863   str_buff_vec_t		subset_globalsubrs;
864   hb_vector_t<str_buff_vec_t>	subset_localsubrs;
865   hb_vector_t<font_dict_values_mod_t>  fontdicts_mod;
866 
867   bool		drop_hints;
868 
869   bool		gid_renum;
870   bool		subset_encoding;
871   uint8_t	subset_enc_format;
872   unsigned int	subset_enc_num_codes;
873   range_list_t	subset_enc_code_ranges;
874   hb_vector_t<code_pair_t>  subset_enc_supp_codes;
875 
876   uint8_t	subset_charset_format;
877   range_list_t	subset_charset_ranges;
878   bool		subset_charset;
879 
880   remap_sid_t	sidmap;
881   unsigned int	topDictModSIDs[name_dict_values_t::ValCount];
882 
883   bool		desubroutinize;
884 };
885 
_write_cff1(const cff_subset_plan & plan,const OT::cff1::accelerator_subset_t & acc,unsigned int num_glyphs,unsigned int dest_sz,void * dest)886 static inline bool _write_cff1 (const cff_subset_plan &plan,
887 				const OT::cff1::accelerator_subset_t  &acc,
888 				unsigned int num_glyphs,
889 				unsigned int dest_sz,
890 				void *dest)
891 {
892   hb_serialize_context_t c (dest, dest_sz);
893 
894   OT::cff1 *cff = c.start_serialize<OT::cff1> ();
895   if (unlikely (!c.extend_min (*cff)))
896     return false;
897 
898   /* header */
899   cff->version.major = 0x01;
900   cff->version.minor = 0x00;
901   cff->nameIndex = cff->min_size;
902   cff->offSize = 4; /* unused? */
903 
904   /* name INDEX */
905   {
906     assert (cff->nameIndex == (unsigned) (c.head - c.start));
907     CFF1NameIndex *dest = c.start_embed<CFF1NameIndex> ();
908     if (unlikely (dest == nullptr)) return false;
909     if (unlikely (!dest->serialize (&c, *acc.nameIndex)))
910     {
911       DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF name INDEX");
912       return false;
913     }
914   }
915 
916   /* top dict INDEX */
917   {
918     assert (plan.offsets.topDictInfo.offset == (unsigned) (c.head - c.start));
919     CFF1IndexOf<TopDict> *dest = c.start_embed< CFF1IndexOf<TopDict>> ();
920     if (dest == nullptr) return false;
921     cff1_top_dict_op_serializer_t topSzr;
922     top_dict_modifiers_t  modifier (plan.offsets, plan.topDictModSIDs);
923     if (unlikely (!dest->serialize (&c, plan.offsets.topDictInfo.offSize,
924 				    &plan.topdict_mod, 1,
925 				    plan.topdict_sizes, topSzr, modifier)))
926     {
927       DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF top dict");
928       return false;
929     }
930   }
931 
932   /* String INDEX */
933   {
934     assert (plan.offsets.stringIndexInfo.offset == (unsigned) (c.head - c.start));
935     CFF1StringIndex *dest = c.start_embed<CFF1StringIndex> ();
936     if (unlikely (dest == nullptr)) return false;
937     if (unlikely (!dest->serialize (&c, *acc.stringIndex, plan.offsets.stringIndexInfo.offSize, plan.sidmap)))
938     {
939       DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF string INDEX");
940       return false;
941     }
942   }
943 
944   /* global subrs */
945   {
946     assert (plan.offsets.globalSubrsInfo.offset != 0);
947     assert (plan.offsets.globalSubrsInfo.offset == (unsigned) (c.head - c.start));
948 
949     CFF1Subrs *dest = c.start_embed <CFF1Subrs> ();
950     if (unlikely (dest == nullptr)) return false;
951     if (unlikely (!dest->serialize (&c, plan.offsets.globalSubrsInfo.offSize, plan.subset_globalsubrs)))
952     {
953       DEBUG_MSG (SUBSET, nullptr, "failed to serialize global subroutines");
954       return false;
955     }
956   }
957 
958   /* Encoding */
959   if (plan.subset_encoding)
960   {
961     assert (plan.offsets.encodingOffset == (unsigned) (c.head - c.start));
962     Encoding *dest = c.start_embed<Encoding> ();
963     if (unlikely (dest == nullptr)) return false;
964     if (unlikely (!dest->serialize (&c,
965 				    plan.subset_enc_format,
966 				    plan.subset_enc_num_codes,
967 				    plan.subset_enc_code_ranges,
968 				    plan.subset_enc_supp_codes)))
969     {
970       DEBUG_MSG (SUBSET, nullptr, "failed to serialize Encoding");
971       return false;
972     }
973   }
974 
975   /* Charset */
976   if (plan.subset_charset)
977   {
978     assert (plan.offsets.charsetInfo.offset == (unsigned) (c.head - c.start));
979     Charset *dest = c.start_embed<Charset> ();
980     if (unlikely (dest == nullptr)) return false;
981     if (unlikely (!dest->serialize (&c,
982 				    plan.subset_charset_format,
983 				    plan.num_glyphs,
984 				    plan.subset_charset_ranges)))
985     {
986       DEBUG_MSG (SUBSET, nullptr, "failed to serialize Charset");
987       return false;
988     }
989   }
990 
991   /* FDSelect */
992   if (acc.fdSelect != &Null(CFF1FDSelect))
993   {
994     assert (plan.offsets.FDSelectInfo.offset == (unsigned) (c.head - c.start));
995 
996     if (unlikely (!hb_serialize_cff_fdselect (&c, num_glyphs, *acc.fdSelect, acc.fdCount,
997 					      plan.subset_fdselect_format, plan.offsets.FDSelectInfo.size,
998 					      plan.subset_fdselect_ranges)))
999     {
1000       DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF subset FDSelect");
1001       return false;
1002     }
1003   }
1004 
1005   /* FDArray (FD Index) */
1006   if (acc.fdArray != &Null(CFF1FDArray))
1007   {
1008     assert (plan.offsets.FDArrayInfo.offset == (unsigned) (c.head - c.start));
1009     CFF1FDArray  *fda = c.start_embed<CFF1FDArray> ();
1010     if (unlikely (fda == nullptr)) return false;
1011     cff1_font_dict_op_serializer_t  fontSzr;
1012     if (unlikely (!fda->serialize (&c, plan.offsets.FDArrayInfo.offSize,
1013 				   plan.fontdicts_mod,
1014 				   fontSzr)))
1015     {
1016       DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF FDArray");
1017       return false;
1018     }
1019   }
1020 
1021   /* CharStrings */
1022   {
1023     assert (plan.offsets.charStringsInfo.offset == (unsigned) (c.head - c.start));
1024     CFF1CharStrings  *cs = c.start_embed<CFF1CharStrings> ();
1025     if (unlikely (cs == nullptr)) return false;
1026     if (unlikely (!cs->serialize (&c, plan.offsets.charStringsInfo.offSize, plan.subset_charstrings)))
1027     {
1028       DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF CharStrings");
1029       return false;
1030     }
1031   }
1032 
1033   /* private dicts & local subrs */
1034   assert (plan.offsets.privateDictInfo.offset == (unsigned) (c.head - c.start));
1035   for (unsigned int i = 0; i < acc.privateDicts.length; i++)
1036   {
1037     if (plan.fdmap.has (i))
1038     {
1039       PrivateDict  *pd = c.start_embed<PrivateDict> ();
1040       if (unlikely (pd == nullptr)) return false;
1041       unsigned int priv_size = plan.fontdicts_mod[plan.fdmap[i]].privateDictInfo.size;
1042       bool result;
1043       cff_private_dict_op_serializer_t privSzr (plan.desubroutinize, plan.drop_hints);
1044       /* N.B. local subrs immediately follows its corresponding private dict. i.e., subr offset == private dict size */
1045       unsigned int subroffset = (plan.offsets.localSubrsInfos[i].size > 0) ? priv_size : 0;
1046       result = pd->serialize (&c, acc.privateDicts[i], privSzr, subroffset);
1047       if (unlikely (!result))
1048       {
1049 	DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF Private Dict[%d]", i);
1050 	return false;
1051       }
1052       if (plan.offsets.localSubrsInfos[i].size > 0)
1053       {
1054 	CFF1Subrs *dest = c.start_embed <CFF1Subrs> ();
1055 	if (unlikely (dest == nullptr)) return false;
1056 	if (unlikely (!dest->serialize (&c, plan.offsets.localSubrsInfos[i].offSize, plan.subset_localsubrs[i])))
1057 	{
1058 	  DEBUG_MSG (SUBSET, nullptr, "failed to serialize local subroutines");
1059 	  return false;
1060 	}
1061       }
1062     }
1063   }
1064 
1065   assert (c.head == c.end);
1066   c.end_serialize ();
1067 
1068   return true;
1069 }
1070 
1071 static inline bool
_hb_subset_cff1(const OT::cff1::accelerator_subset_t & acc,const char * data,hb_subset_plan_t * plan,hb_blob_t ** prime)1072 _hb_subset_cff1 (const OT::cff1::accelerator_subset_t  &acc,
1073 		const char		*data,
1074 		hb_subset_plan_t	*plan,
1075 		hb_blob_t		**prime /* OUT */)
1076 {
1077   cff_subset_plan cff_plan;
1078 
1079   if (unlikely (!cff_plan.create (acc, plan)))
1080   {
1081     DEBUG_MSG(SUBSET, nullptr, "Failed to generate a cff subsetting plan.");
1082     return false;
1083   }
1084 
1085   unsigned int  cff_prime_size = cff_plan.get_final_size ();
1086   char *cff_prime_data = (char *) calloc (1, cff_prime_size);
1087 
1088   if (unlikely (!_write_cff1 (cff_plan, acc, plan->num_output_glyphs (),
1089 			      cff_prime_size, cff_prime_data))) {
1090     DEBUG_MSG(SUBSET, nullptr, "Failed to write a subset cff.");
1091     free (cff_prime_data);
1092     return false;
1093   }
1094 
1095   *prime = hb_blob_create (cff_prime_data,
1096 			   cff_prime_size,
1097 			   HB_MEMORY_MODE_READONLY,
1098 			   cff_prime_data,
1099 			   free);
1100   return true;
1101 }
1102 
1103 /**
1104  * hb_subset_cff1:
1105  * Subsets the CFF table according to a provided plan.
1106  *
1107  * Return value: subsetted cff table.
1108  **/
1109 bool
hb_subset_cff1(hb_subset_plan_t * plan,hb_blob_t ** prime)1110 hb_subset_cff1 (hb_subset_plan_t *plan,
1111 		hb_blob_t       **prime /* OUT */)
1112 {
1113   hb_blob_t *cff_blob = hb_sanitize_context_t().reference_table<CFF::cff1> (plan->source);
1114   const char *data = hb_blob_get_data(cff_blob, nullptr);
1115 
1116   OT::cff1::accelerator_subset_t acc;
1117   acc.init(plan->source);
1118   bool result = likely (acc.is_valid ()) &&
1119 			_hb_subset_cff1 (acc, data, plan, prime);
1120   hb_blob_destroy (cff_blob);
1121   acc.fini ();
1122 
1123   return result;
1124 }
1125 
1126 
1127 #endif
1128