• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2018 Adobe Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Adobe Author(s): Michiharu Ariza
25  */
26 
27 #ifndef HB_SUBSET_CFF_COMMON_HH
28 #define HB_SUBSET_CFF_COMMON_HH
29 
30 #include "hb.hh"
31 
32 #include "hb-subset-plan.hh"
33 #include "hb-cff-interp-cs-common.hh"
34 
35 namespace CFF {
36 
37 /* Used for writing a temporary charstring */
38 struct str_encoder_t
39 {
str_encoder_tCFF::str_encoder_t40   str_encoder_t (str_buff_t &buff_)
41     : buff (buff_), error (false) {}
42 
resetCFF::str_encoder_t43   void reset () { buff.resize (0); }
44 
encode_byteCFF::str_encoder_t45   void encode_byte (unsigned char b)
46   {
47     if (unlikely (buff.push (b) == &Crap (unsigned char)))
48       set_error ();
49   }
50 
encode_intCFF::str_encoder_t51   void encode_int (int v)
52   {
53     if ((-1131 <= v) && (v <= 1131))
54     {
55       if ((-107 <= v) && (v <= 107))
56 	encode_byte (v + 139);
57       else if (v > 0)
58       {
59 	v -= 108;
60 	encode_byte ((v >> 8) + OpCode_TwoBytePosInt0);
61 	encode_byte (v & 0xFF);
62       }
63       else
64       {
65 	v = -v - 108;
66 	encode_byte ((v >> 8) + OpCode_TwoByteNegInt0);
67 	encode_byte (v & 0xFF);
68       }
69     }
70     else
71     {
72       if (unlikely (v < -32768))
73 	v = -32768;
74       else if (unlikely (v > 32767))
75 	v = 32767;
76       encode_byte (OpCode_shortint);
77       encode_byte ((v >> 8) & 0xFF);
78       encode_byte (v & 0xFF);
79     }
80   }
81 
encode_numCFF::str_encoder_t82   void encode_num (const number_t& n)
83   {
84     if (n.in_int_range ())
85     {
86       encode_int (n.to_int ());
87     }
88     else
89     {
90       int32_t v = n.to_fixed ();
91       encode_byte (OpCode_fixedcs);
92       encode_byte ((v >> 24) & 0xFF);
93       encode_byte ((v >> 16) & 0xFF);
94       encode_byte ((v >> 8) & 0xFF);
95       encode_byte (v & 0xFF);
96     }
97   }
98 
encode_opCFF::str_encoder_t99   void encode_op (op_code_t op)
100   {
101     if (Is_OpCode_ESC (op))
102     {
103       encode_byte (OpCode_escape);
104       encode_byte (Unmake_OpCode_ESC (op));
105     }
106     else
107       encode_byte (op);
108   }
109 
copy_strCFF::str_encoder_t110   void copy_str (const byte_str_t &str)
111   {
112     unsigned int  offset = buff.length;
113     if (unlikely (!buff.resize (offset + str.length)))
114     {
115       set_error ();
116       return;
117     }
118     if (unlikely (buff.length < offset + str.length))
119     {
120       set_error ();
121       return;
122     }
123     memcpy (&buff[offset], &str[0], str.length);
124   }
125 
is_errorCFF::str_encoder_t126   bool is_error () const { return error; }
127 
128   protected:
set_errorCFF::str_encoder_t129   void set_error () { error = true; }
130 
131   str_buff_t &buff;
132   bool    error;
133 };
134 
135 struct cff_sub_table_info_t {
cff_sub_table_info_tCFF::cff_sub_table_info_t136   cff_sub_table_info_t ()
137     : fd_array_link (0),
138       char_strings_link (0)
139   {
140     fd_select.init ();
141   }
142 
143   table_info_t     fd_select;
144   objidx_t     	   fd_array_link;
145   objidx_t     	   char_strings_link;
146 };
147 
148 template <typename OPSTR=op_str_t>
149 struct cff_top_dict_op_serializer_t : op_serializer_t
150 {
serializeCFF::cff_top_dict_op_serializer_t151   bool serialize (hb_serialize_context_t *c,
152 		  const OPSTR &opstr,
153 		  const cff_sub_table_info_t &info) const
154   {
155     TRACE_SERIALIZE (this);
156 
157     switch (opstr.op)
158     {
159       case OpCode_CharStrings:
160 	return_trace (FontDict::serialize_link4_op(c, opstr.op, info.char_strings_link, whence_t::Absolute));
161 
162       case OpCode_FDArray:
163 	return_trace (FontDict::serialize_link4_op(c, opstr.op, info.fd_array_link, whence_t::Absolute));
164 
165       case OpCode_FDSelect:
166 	return_trace (FontDict::serialize_link4_op(c, opstr.op, info.fd_select.link, whence_t::Absolute));
167 
168       default:
169 	return_trace (copy_opstr (c, opstr));
170     }
171     return_trace (true);
172   }
173 };
174 
175 struct cff_font_dict_op_serializer_t : op_serializer_t
176 {
serializeCFF::cff_font_dict_op_serializer_t177   bool serialize (hb_serialize_context_t *c,
178 		  const op_str_t &opstr,
179 		  const table_info_t &privateDictInfo) const
180   {
181     TRACE_SERIALIZE (this);
182 
183     if (opstr.op == OpCode_Private)
184     {
185       /* serialize the private dict size & offset as 2-byte & 4-byte integers */
186       return_trace (UnsizedByteStr::serialize_int2 (c, privateDictInfo.size) &&
187 		    Dict::serialize_link4_op (c, opstr.op, privateDictInfo.link, whence_t::Absolute));
188     }
189     else
190     {
191       HBUINT8 *d = c->allocate_size<HBUINT8> (opstr.str.length);
192       if (unlikely (!d)) return_trace (false);
193       memcpy (d, &opstr.str[0], opstr.str.length);
194     }
195     return_trace (true);
196   }
197 };
198 
199 struct cff_private_dict_op_serializer_t : op_serializer_t
200 {
cff_private_dict_op_serializer_tCFF::cff_private_dict_op_serializer_t201   cff_private_dict_op_serializer_t (bool desubroutinize_, bool drop_hints_)
202     : desubroutinize (desubroutinize_), drop_hints (drop_hints_) {}
203 
serializeCFF::cff_private_dict_op_serializer_t204   bool serialize (hb_serialize_context_t *c,
205 		  const op_str_t &opstr,
206 		  objidx_t subrs_link) const
207   {
208     TRACE_SERIALIZE (this);
209 
210     if (drop_hints && dict_opset_t::is_hint_op (opstr.op))
211       return true;
212     if (opstr.op == OpCode_Subrs)
213     {
214       if (desubroutinize || !subrs_link)
215 	return_trace (true);
216       else
217 	return_trace (FontDict::serialize_link2_op (c, opstr.op, subrs_link));
218     }
219     else
220       return_trace (copy_opstr (c, opstr));
221   }
222 
223   protected:
224   const bool  desubroutinize;
225   const bool  drop_hints;
226 };
227 
228 struct flatten_param_t
229 {
230   str_buff_t     &flatStr;
231   bool	drop_hints;
232 };
233 
234 template <typename ACC, typename ENV, typename OPSET, op_code_t endchar_op=OpCode_Invalid>
235 struct subr_flattener_t
236 {
subr_flattener_tCFF::subr_flattener_t237   subr_flattener_t (const ACC &acc_,
238 		    const hb_subset_plan_t *plan_)
239 		   : acc (acc_), plan (plan_) {}
240 
flattenCFF::subr_flattener_t241   bool flatten (str_buff_vec_t &flat_charstrings)
242   {
243     if (!flat_charstrings.resize (plan->num_output_glyphs ()))
244       return false;
245     for (unsigned int i = 0; i < plan->num_output_glyphs (); i++)
246       flat_charstrings[i].init ();
247     for (unsigned int i = 0; i < plan->num_output_glyphs (); i++)
248     {
249       hb_codepoint_t  glyph;
250       if (!plan->old_gid_for_new_gid (i, &glyph))
251       {
252 	/* add an endchar only charstring for a missing glyph if CFF1 */
253 	if (endchar_op != OpCode_Invalid) flat_charstrings[i].push (endchar_op);
254 	continue;
255       }
256       const byte_str_t str = (*acc.charStrings)[glyph];
257       unsigned int fd = acc.fdSelect->get_fd (glyph);
258       if (unlikely (fd >= acc.fdCount))
259 	return false;
260       cs_interpreter_t<ENV, OPSET, flatten_param_t> interp;
261       interp.env.init (str, acc, fd);
262       flatten_param_t  param = { flat_charstrings[i], plan->drop_hints };
263       if (unlikely (!interp.interpret (param)))
264 	return false;
265     }
266     return true;
267   }
268 
269   const ACC &acc;
270   const hb_subset_plan_t *plan;
271 };
272 
273 struct subr_closures_t
274 {
subr_closures_tCFF::subr_closures_t275   subr_closures_t () : valid (false), global_closure (nullptr)
276   { local_closures.init (); }
277 
initCFF::subr_closures_t278   void init (unsigned int fd_count)
279   {
280     valid = true;
281     global_closure = hb_set_create ();
282     if (global_closure == hb_set_get_empty ())
283       valid = false;
284     if (!local_closures.resize (fd_count))
285       valid = false;
286 
287     for (unsigned int i = 0; i < local_closures.length; i++)
288     {
289       local_closures[i] = hb_set_create ();
290       if (local_closures[i] == hb_set_get_empty ())
291 	valid = false;
292     }
293   }
294 
finiCFF::subr_closures_t295   void fini ()
296   {
297     hb_set_destroy (global_closure);
298     for (unsigned int i = 0; i < local_closures.length; i++)
299       hb_set_destroy (local_closures[i]);
300     local_closures.fini ();
301   }
302 
resetCFF::subr_closures_t303   void reset ()
304   {
305     hb_set_clear (global_closure);
306     for (unsigned int i = 0; i < local_closures.length; i++)
307       hb_set_clear (local_closures[i]);
308   }
309 
is_validCFF::subr_closures_t310   bool is_valid () const { return valid; }
311   bool  valid;
312   hb_set_t  *global_closure;
313   hb_vector_t<hb_set_t *> local_closures;
314 };
315 
316 struct parsed_cs_op_t : op_str_t
317 {
initCFF::parsed_cs_op_t318   void init (unsigned int subr_num_ = 0)
319   {
320     op_str_t::init ();
321     subr_num = subr_num_;
322     drop_flag = false;
323     keep_flag = false;
324     skip_flag = false;
325   }
326 
finiCFF::parsed_cs_op_t327   void fini () { op_str_t::fini (); }
328 
for_dropCFF::parsed_cs_op_t329   bool for_drop () const { return drop_flag; }
set_dropCFF::parsed_cs_op_t330   void set_drop ()       { if (!for_keep ()) drop_flag = true; }
331 
for_keepCFF::parsed_cs_op_t332   bool for_keep () const { return keep_flag; }
set_keepCFF::parsed_cs_op_t333   void set_keep ()       { keep_flag = true; }
334 
for_skipCFF::parsed_cs_op_t335   bool for_skip () const { return skip_flag; }
set_skipCFF::parsed_cs_op_t336   void set_skip ()       { skip_flag = true; }
337 
338   unsigned int  subr_num;
339 
340   protected:
341   bool	  drop_flag : 1;
342   bool	  keep_flag : 1;
343   bool	  skip_flag : 1;
344 };
345 
346 struct parsed_cs_str_t : parsed_values_t<parsed_cs_op_t>
347 {
initCFF::parsed_cs_str_t348   void init ()
349   {
350     SUPER::init ();
351     parsed = false;
352     hint_dropped = false;
353     has_prefix_ = false;
354   }
355 
add_opCFF::parsed_cs_str_t356   void add_op (op_code_t op, const byte_str_ref_t& str_ref)
357   {
358     if (!is_parsed ())
359       SUPER::add_op (op, str_ref);
360   }
361 
add_call_opCFF::parsed_cs_str_t362   void add_call_op (op_code_t op, const byte_str_ref_t& str_ref, unsigned int subr_num)
363   {
364     if (!is_parsed ())
365     {
366       unsigned int parsed_len = get_count ();
367       if (likely (parsed_len > 0))
368 	values[parsed_len-1].set_skip ();
369 
370       parsed_cs_op_t val;
371       val.init (subr_num);
372       SUPER::add_op (op, str_ref, val);
373     }
374   }
375 
set_prefixCFF::parsed_cs_str_t376   void set_prefix (const number_t &num, op_code_t op = OpCode_Invalid)
377   {
378     has_prefix_ = true;
379     prefix_op_ = op;
380     prefix_num_ = num;
381   }
382 
at_endCFF::parsed_cs_str_t383   bool at_end (unsigned int pos) const
384   {
385     return ((pos + 1 >= values.length) /* CFF2 */
386 	|| (values[pos + 1].op == OpCode_return));
387   }
388 
is_parsedCFF::parsed_cs_str_t389   bool is_parsed () const { return parsed; }
set_parsedCFF::parsed_cs_str_t390   void set_parsed ()      { parsed = true; }
391 
is_hint_droppedCFF::parsed_cs_str_t392   bool is_hint_dropped () const { return hint_dropped; }
set_hint_droppedCFF::parsed_cs_str_t393   void set_hint_dropped ()      { hint_dropped = true; }
394 
is_vsindex_droppedCFF::parsed_cs_str_t395   bool is_vsindex_dropped () const { return vsindex_dropped; }
set_vsindex_droppedCFF::parsed_cs_str_t396   void set_vsindex_dropped ()      { vsindex_dropped = true; }
397 
has_prefixCFF::parsed_cs_str_t398   bool has_prefix () const          { return has_prefix_; }
prefix_opCFF::parsed_cs_str_t399   op_code_t prefix_op () const         { return prefix_op_; }
prefix_numCFF::parsed_cs_str_t400   const number_t &prefix_num () const { return prefix_num_; }
401 
402   protected:
403   bool    parsed;
404   bool    hint_dropped;
405   bool    vsindex_dropped;
406   bool    has_prefix_;
407   op_code_t	prefix_op_;
408   number_t	prefix_num_;
409 
410   private:
411   typedef parsed_values_t<parsed_cs_op_t> SUPER;
412 };
413 
414 struct parsed_cs_str_vec_t : hb_vector_t<parsed_cs_str_t>
415 {
initCFF::parsed_cs_str_vec_t416   void init (unsigned int len_ = 0)
417   {
418     SUPER::init ();
419     if (unlikely (!resize (len_)))
420       return;
421     for (unsigned int i = 0; i < length; i++)
422       (*this)[i].init ();
423   }
finiCFF::parsed_cs_str_vec_t424   void fini () { SUPER::fini_deep (); }
425 
426   private:
427   typedef hb_vector_t<parsed_cs_str_t> SUPER;
428 };
429 
430 struct subr_subset_param_t
431 {
initCFF::subr_subset_param_t432   void init (parsed_cs_str_t *parsed_charstring_,
433 	     parsed_cs_str_vec_t *parsed_global_subrs_, parsed_cs_str_vec_t *parsed_local_subrs_,
434 	     hb_set_t *global_closure_, hb_set_t *local_closure_,
435 	     bool drop_hints_)
436   {
437     parsed_charstring = parsed_charstring_;
438     current_parsed_str = parsed_charstring;
439     parsed_global_subrs = parsed_global_subrs_;
440     parsed_local_subrs = parsed_local_subrs_;
441     global_closure = global_closure_;
442     local_closure = local_closure_;
443     drop_hints = drop_hints_;
444   }
445 
get_parsed_str_for_contextCFF::subr_subset_param_t446   parsed_cs_str_t *get_parsed_str_for_context (call_context_t &context)
447   {
448     switch (context.type)
449     {
450       case CSType_CharString:
451 	return parsed_charstring;
452 
453       case CSType_LocalSubr:
454 	if (likely (context.subr_num < parsed_local_subrs->length))
455 	  return &(*parsed_local_subrs)[context.subr_num];
456 	break;
457 
458       case CSType_GlobalSubr:
459 	if (likely (context.subr_num < parsed_global_subrs->length))
460 	  return &(*parsed_global_subrs)[context.subr_num];
461 	break;
462     }
463     return nullptr;
464   }
465 
466   template <typename ENV>
set_current_strCFF::subr_subset_param_t467   void set_current_str (ENV &env, bool calling)
468   {
469     parsed_cs_str_t *parsed_str = get_parsed_str_for_context (env.context);
470     if (unlikely (!parsed_str))
471     {
472       env.set_error ();
473       return;
474     }
475     /* If the called subroutine is parsed partially but not completely yet,
476      * it must be because we are calling it recursively.
477      * Handle it as an error. */
478     if (unlikely (calling && !parsed_str->is_parsed () && (parsed_str->values.length > 0)))
479       env.set_error ();
480     else
481       current_parsed_str = parsed_str;
482   }
483 
484   parsed_cs_str_t	*current_parsed_str;
485 
486   parsed_cs_str_t	*parsed_charstring;
487   parsed_cs_str_vec_t	*parsed_global_subrs;
488   parsed_cs_str_vec_t	*parsed_local_subrs;
489   hb_set_t      *global_closure;
490   hb_set_t      *local_closure;
491   bool	  drop_hints;
492 };
493 
494 struct subr_remap_t : hb_inc_bimap_t
495 {
createCFF::subr_remap_t496   void create (hb_set_t *closure)
497   {
498     /* create a remapping of subroutine numbers from old to new.
499      * no optimization based on usage counts. fonttools doesn't appear doing that either.
500      */
501 
502     hb_codepoint_t old_num = HB_SET_VALUE_INVALID;
503     while (hb_set_next (closure, &old_num))
504       add (old_num);
505 
506     if (get_population () < 1240)
507       bias = 107;
508     else if (get_population () < 33900)
509       bias = 1131;
510     else
511       bias = 32768;
512   }
513 
biased_numCFF::subr_remap_t514   int biased_num (unsigned int old_num) const
515   {
516     hb_codepoint_t new_num = get (old_num);
517     return (int)new_num - bias;
518   }
519 
520   protected:
521   int bias;
522 };
523 
524 struct subr_remaps_t
525 {
subr_remaps_tCFF::subr_remaps_t526   subr_remaps_t ()
527   {
528     global_remap.init ();
529     local_remaps.init ();
530   }
531 
~subr_remaps_tCFF::subr_remaps_t532   ~subr_remaps_t () { fini (); }
533 
initCFF::subr_remaps_t534   void init (unsigned int fdCount)
535   {
536     if (unlikely (!local_remaps.resize (fdCount))) return;
537     for (unsigned int i = 0; i < fdCount; i++)
538       local_remaps[i].init ();
539   }
540 
in_errorCFF::subr_remaps_t541   bool in_error()
542   {
543     return local_remaps.in_error ();
544   }
545 
createCFF::subr_remaps_t546   void create (subr_closures_t& closures)
547   {
548     global_remap.create (closures.global_closure);
549     for (unsigned int i = 0; i < local_remaps.length; i++)
550       local_remaps[i].create (closures.local_closures[i]);
551   }
552 
finiCFF::subr_remaps_t553   void fini ()
554   {
555     global_remap.fini ();
556     local_remaps.fini_deep ();
557   }
558 
559   subr_remap_t	       global_remap;
560   hb_vector_t<subr_remap_t>  local_remaps;
561 };
562 
563 template <typename SUBSETTER, typename SUBRS, typename ACC, typename ENV, typename OPSET, op_code_t endchar_op=OpCode_Invalid>
564 struct subr_subsetter_t
565 {
subr_subsetter_tCFF::subr_subsetter_t566   subr_subsetter_t (ACC &acc_, const hb_subset_plan_t *plan_)
567     : acc (acc_), plan (plan_)
568   {
569     parsed_charstrings.init ();
570     parsed_global_subrs.init ();
571     parsed_local_subrs.init ();
572   }
573 
~subr_subsetter_tCFF::subr_subsetter_t574   ~subr_subsetter_t ()
575   {
576     closures.fini ();
577     remaps.fini ();
578     parsed_charstrings.fini_deep ();
579     parsed_global_subrs.fini_deep ();
580     parsed_local_subrs.fini_deep ();
581   }
582 
583   /* Subroutine subsetting with --no-desubroutinize runs in phases:
584    *
585    * 1. execute charstrings/subroutines to determine subroutine closures
586    * 2. parse out all operators and numbers
587    * 3. mark hint operators and operands for removal if --no-hinting
588    * 4. re-encode all charstrings and subroutines with new subroutine numbers
589    *
590    * Phases #1 and #2 are done at the same time in collect_subrs ().
591    * Phase #3 walks charstrings/subroutines forward then backward (hence parsing required),
592    * because we can't tell if a number belongs to a hint op until we see the first moveto.
593    *
594    * Assumption: a callsubr/callgsubr operator must immediately follow a (biased) subroutine number
595    * within the same charstring/subroutine, e.g., not split across a charstring and a subroutine.
596    */
subsetCFF::subr_subsetter_t597   bool subset (void)
598   {
599     closures.init (acc.fdCount);
600     remaps.init (acc.fdCount);
601 
602     parsed_charstrings.init (plan->num_output_glyphs ());
603     parsed_global_subrs.init (acc.globalSubrs->count);
604 
605     if (unlikely (remaps.in_error()
606                   || parsed_charstrings.in_error ()
607                   || parsed_global_subrs.in_error ())) {
608       return false;
609     }
610 
611     if (unlikely (!parsed_local_subrs.resize (acc.fdCount))) return false;
612 
613     for (unsigned int i = 0; i < acc.fdCount; i++)
614     {
615       parsed_local_subrs[i].init (acc.privateDicts[i].localSubrs->count);
616       if (unlikely (parsed_local_subrs[i].in_error ())) return false;
617     }
618     if (unlikely (!closures.valid))
619       return false;
620 
621     /* phase 1 & 2 */
622     for (unsigned int i = 0; i < plan->num_output_glyphs (); i++)
623     {
624       hb_codepoint_t  glyph;
625       if (!plan->old_gid_for_new_gid (i, &glyph))
626 	continue;
627       const byte_str_t str = (*acc.charStrings)[glyph];
628       unsigned int fd = acc.fdSelect->get_fd (glyph);
629       if (unlikely (fd >= acc.fdCount))
630 	return false;
631 
632       cs_interpreter_t<ENV, OPSET, subr_subset_param_t> interp;
633       interp.env.init (str, acc, fd);
634 
635       subr_subset_param_t  param;
636       param.init (&parsed_charstrings[i],
637 		  &parsed_global_subrs,  &parsed_local_subrs[fd],
638 		  closures.global_closure, closures.local_closures[fd],
639 		  plan->drop_hints);
640 
641       if (unlikely (!interp.interpret (param)))
642 	return false;
643 
644       /* complete parsed string esp. copy CFF1 width or CFF2 vsindex to the parsed charstring for encoding */
645       SUBSETTER::complete_parsed_str (interp.env, param, parsed_charstrings[i]);
646     }
647 
648     if (plan->drop_hints)
649     {
650       /* mark hint ops and arguments for drop */
651       for (unsigned int i = 0; i < plan->num_output_glyphs (); i++)
652       {
653 	hb_codepoint_t  glyph;
654 	if (!plan->old_gid_for_new_gid (i, &glyph))
655 	  continue;
656 	unsigned int fd = acc.fdSelect->get_fd (glyph);
657 	if (unlikely (fd >= acc.fdCount))
658 	  return false;
659 	subr_subset_param_t  param;
660 	param.init (&parsed_charstrings[i],
661 		    &parsed_global_subrs,  &parsed_local_subrs[fd],
662 		    closures.global_closure, closures.local_closures[fd],
663 		    plan->drop_hints);
664 
665 	drop_hints_param_t  drop;
666 	if (drop_hints_in_str (parsed_charstrings[i], param, drop))
667 	{
668 	  parsed_charstrings[i].set_hint_dropped ();
669 	  if (drop.vsindex_dropped)
670 	    parsed_charstrings[i].set_vsindex_dropped ();
671 	}
672       }
673 
674       /* after dropping hints recreate closures of actually used subrs */
675       closures.reset ();
676       for (unsigned int i = 0; i < plan->num_output_glyphs (); i++)
677       {
678 	hb_codepoint_t  glyph;
679 	if (!plan->old_gid_for_new_gid (i, &glyph))
680 	  continue;
681 	unsigned int fd = acc.fdSelect->get_fd (glyph);
682 	if (unlikely (fd >= acc.fdCount))
683 	  return false;
684 	subr_subset_param_t  param;
685 	param.init (&parsed_charstrings[i],
686 		    &parsed_global_subrs,  &parsed_local_subrs[fd],
687 		    closures.global_closure, closures.local_closures[fd],
688 		    plan->drop_hints);
689 	collect_subr_refs_in_str (parsed_charstrings[i], param);
690       }
691     }
692 
693     remaps.create (closures);
694 
695     return true;
696   }
697 
encode_charstringsCFF::subr_subsetter_t698   bool encode_charstrings (str_buff_vec_t &buffArray) const
699   {
700     if (unlikely (!buffArray.resize (plan->num_output_glyphs ())))
701       return false;
702     for (unsigned int i = 0; i < plan->num_output_glyphs (); i++)
703     {
704       hb_codepoint_t  glyph;
705       if (!plan->old_gid_for_new_gid (i, &glyph))
706       {
707 	/* add an endchar only charstring for a missing glyph if CFF1 */
708 	if (endchar_op != OpCode_Invalid) buffArray[i].push (endchar_op);
709 	continue;
710       }
711       unsigned int  fd = acc.fdSelect->get_fd (glyph);
712       if (unlikely (fd >= acc.fdCount))
713 	return false;
714       if (unlikely (!encode_str (parsed_charstrings[i], fd, buffArray[i])))
715 	return false;
716     }
717     return true;
718   }
719 
encode_subrsCFF::subr_subsetter_t720   bool encode_subrs (const parsed_cs_str_vec_t &subrs, const subr_remap_t& remap, unsigned int fd, str_buff_vec_t &buffArray) const
721   {
722     unsigned int  count = remap.get_population ();
723 
724     if (unlikely (!buffArray.resize (count)))
725       return false;
726     for (unsigned int old_num = 0; old_num < subrs.length; old_num++)
727     {
728       hb_codepoint_t new_num = remap[old_num];
729       if (new_num != CFF_UNDEF_CODE)
730       {
731 	if (unlikely (!encode_str (subrs[old_num], fd, buffArray[new_num])))
732 	  return false;
733       }
734     }
735     return true;
736   }
737 
encode_globalsubrsCFF::subr_subsetter_t738   bool encode_globalsubrs (str_buff_vec_t &buffArray)
739   {
740     return encode_subrs (parsed_global_subrs, remaps.global_remap, 0, buffArray);
741   }
742 
encode_localsubrsCFF::subr_subsetter_t743   bool encode_localsubrs (unsigned int fd, str_buff_vec_t &buffArray) const
744   {
745     return encode_subrs (parsed_local_subrs[fd], remaps.local_remaps[fd], fd, buffArray);
746   }
747 
748   protected:
749   struct drop_hints_param_t
750   {
drop_hints_param_tCFF::subr_subsetter_t::drop_hints_param_t751     drop_hints_param_t ()
752       : seen_moveto (false),
753 	ends_in_hint (false),
754 	all_dropped (false),
755 	vsindex_dropped (false) {}
756 
757     bool  seen_moveto;
758     bool  ends_in_hint;
759     bool  all_dropped;
760     bool  vsindex_dropped;
761   };
762 
drop_hints_in_subrCFF::subr_subsetter_t763   bool drop_hints_in_subr (parsed_cs_str_t &str, unsigned int pos,
764 			   parsed_cs_str_vec_t &subrs, unsigned int subr_num,
765 			   const subr_subset_param_t &param, drop_hints_param_t &drop)
766   {
767     drop.ends_in_hint = false;
768     bool has_hint = drop_hints_in_str (subrs[subr_num], param, drop);
769 
770     /* if this subr ends with a stem hint (i.e., not a number; potential argument for moveto),
771      * then this entire subroutine must be a hint. drop its call. */
772     if (drop.ends_in_hint)
773     {
774       str.values[pos].set_drop ();
775       /* if this subr call is at the end of the parent subr, propagate the flag
776        * otherwise reset the flag */
777       if (!str.at_end (pos))
778 	drop.ends_in_hint = false;
779     }
780     else if (drop.all_dropped)
781     {
782       str.values[pos].set_drop ();
783     }
784 
785     return has_hint;
786   }
787 
788   /* returns true if it sees a hint op before the first moveto */
drop_hints_in_strCFF::subr_subsetter_t789   bool drop_hints_in_str (parsed_cs_str_t &str, const subr_subset_param_t &param, drop_hints_param_t &drop)
790   {
791     bool  seen_hint = false;
792 
793     for (unsigned int pos = 0; pos < str.values.length; pos++)
794     {
795       bool  has_hint = false;
796       switch (str.values[pos].op)
797       {
798 	case OpCode_callsubr:
799 	  has_hint = drop_hints_in_subr (str, pos,
800 					*param.parsed_local_subrs, str.values[pos].subr_num,
801 					param, drop);
802 	  break;
803 
804 	case OpCode_callgsubr:
805 	  has_hint = drop_hints_in_subr (str, pos,
806 					*param.parsed_global_subrs, str.values[pos].subr_num,
807 					param, drop);
808 	  break;
809 
810 	case OpCode_rmoveto:
811 	case OpCode_hmoveto:
812 	case OpCode_vmoveto:
813 	  drop.seen_moveto = true;
814 	  break;
815 
816 	case OpCode_hintmask:
817 	case OpCode_cntrmask:
818 	  if (drop.seen_moveto)
819 	  {
820 	    str.values[pos].set_drop ();
821 	    break;
822 	  }
823 	  HB_FALLTHROUGH;
824 
825 	case OpCode_hstemhm:
826 	case OpCode_vstemhm:
827 	case OpCode_hstem:
828 	case OpCode_vstem:
829 	  has_hint = true;
830 	  str.values[pos].set_drop ();
831 	  if (str.at_end (pos))
832 	    drop.ends_in_hint = true;
833 	  break;
834 
835 	case OpCode_dotsection:
836 	  str.values[pos].set_drop ();
837 	  break;
838 
839 	default:
840 	  /* NONE */
841 	  break;
842       }
843       if (has_hint)
844       {
845 	for (int i = pos - 1; i >= 0; i--)
846 	{
847 	  parsed_cs_op_t  &csop = str.values[(unsigned)i];
848 	  if (csop.for_drop ())
849 	    break;
850 	  csop.set_drop ();
851 	  if (csop.op == OpCode_vsindexcs)
852 	    drop.vsindex_dropped = true;
853 	}
854 	seen_hint |= has_hint;
855       }
856     }
857 
858     /* Raise all_dropped flag if all operators except return are dropped from a subr.
859      * It may happen even after seeing the first moveto if a subr contains
860      * only (usually one) hintmask operator, then calls to this subr can be dropped.
861      */
862     drop.all_dropped = true;
863     for (unsigned int pos = 0; pos < str.values.length; pos++)
864     {
865       parsed_cs_op_t  &csop = str.values[pos];
866       if (csop.op == OpCode_return)
867 	break;
868       if (!csop.for_drop ())
869       {
870 	drop.all_dropped = false;
871 	break;
872       }
873     }
874 
875     return seen_hint;
876   }
877 
collect_subr_refs_in_subrCFF::subr_subsetter_t878   void collect_subr_refs_in_subr (parsed_cs_str_t &str, unsigned int pos,
879 				  unsigned int subr_num, parsed_cs_str_vec_t &subrs,
880 				  hb_set_t *closure,
881 				  const subr_subset_param_t &param)
882   {
883     closure->add (subr_num);
884     collect_subr_refs_in_str (subrs[subr_num], param);
885   }
886 
collect_subr_refs_in_strCFF::subr_subsetter_t887   void collect_subr_refs_in_str (parsed_cs_str_t &str, const subr_subset_param_t &param)
888   {
889     for (unsigned int pos = 0; pos < str.values.length; pos++)
890     {
891       if (!str.values[pos].for_drop ())
892       {
893 	switch (str.values[pos].op)
894 	{
895 	  case OpCode_callsubr:
896 	    collect_subr_refs_in_subr (str, pos,
897 				       str.values[pos].subr_num, *param.parsed_local_subrs,
898 				       param.local_closure, param);
899 	    break;
900 
901 	  case OpCode_callgsubr:
902 	    collect_subr_refs_in_subr (str, pos,
903 				       str.values[pos].subr_num, *param.parsed_global_subrs,
904 				       param.global_closure, param);
905 	    break;
906 
907 	  default: break;
908 	}
909       }
910     }
911   }
912 
encode_strCFF::subr_subsetter_t913   bool encode_str (const parsed_cs_str_t &str, const unsigned int fd, str_buff_t &buff) const
914   {
915     buff.init ();
916     str_encoder_t  encoder (buff);
917     encoder.reset ();
918     /* if a prefix (CFF1 width or CFF2 vsindex) has been removed along with hints,
919      * re-insert it at the beginning of charstreing */
920     if (str.has_prefix () && str.is_hint_dropped ())
921     {
922       encoder.encode_num (str.prefix_num ());
923       if (str.prefix_op () != OpCode_Invalid)
924 	encoder.encode_op (str.prefix_op ());
925     }
926     for (unsigned int i = 0; i < str.get_count(); i++)
927     {
928       const parsed_cs_op_t  &opstr = str.values[i];
929       if (!opstr.for_drop () && !opstr.for_skip ())
930       {
931 	switch (opstr.op)
932 	{
933 	  case OpCode_callsubr:
934 	    encoder.encode_int (remaps.local_remaps[fd].biased_num (opstr.subr_num));
935 	    encoder.encode_op (OpCode_callsubr);
936 	    break;
937 
938 	  case OpCode_callgsubr:
939 	    encoder.encode_int (remaps.global_remap.biased_num (opstr.subr_num));
940 	    encoder.encode_op (OpCode_callgsubr);
941 	    break;
942 
943 	  default:
944 	    encoder.copy_str (opstr.str);
945 	    break;
946 	}
947       }
948     }
949     return !encoder.is_error ();
950   }
951 
952   protected:
953   const ACC			&acc;
954   const hb_subset_plan_t	*plan;
955 
956   subr_closures_t		closures;
957 
958   parsed_cs_str_vec_t		parsed_charstrings;
959   parsed_cs_str_vec_t		parsed_global_subrs;
960   hb_vector_t<parsed_cs_str_vec_t>  parsed_local_subrs;
961 
962   subr_remaps_t			remaps;
963 
964   private:
965   typedef typename SUBRS::count_type subr_count_type;
966 };
967 
968 } /* namespace CFF */
969 
970 HB_INTERNAL bool
971 hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan,
972 			    unsigned int fdCount,
973 			    const CFF::FDSelect &src, /* IN */
974 			    unsigned int &subset_fd_count /* OUT */,
975 			    unsigned int &subset_fdselect_size /* OUT */,
976 			    unsigned int &subset_fdselect_format /* OUT */,
977 			    hb_vector_t<CFF::code_pair_t> &fdselect_ranges /* OUT */,
978 			    hb_inc_bimap_t &fdmap /* OUT */);
979 
980 HB_INTERNAL bool
981 hb_serialize_cff_fdselect (hb_serialize_context_t *c,
982 			  unsigned int num_glyphs,
983 			  const CFF::FDSelect &src,
984 			  unsigned int fd_count,
985 			  unsigned int fdselect_format,
986 			  unsigned int size,
987 			  const hb_vector_t<CFF::code_pair_t> &fdselect_ranges);
988 
989 #endif /* HB_SUBSET_CFF_COMMON_HH */
990