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     buff.resize (offset + str.length);
114     if (unlikely (buff.length < offset + str.length))
115     {
116       set_error ();
117       return;
118     }
119     memcpy (&buff[offset], &str[0], str.length);
120   }
121 
is_errorCFF::str_encoder_t122   bool is_error () const { return error; }
123 
124   protected:
set_errorCFF::str_encoder_t125   void set_error () { error = true; }
126 
127   str_buff_t &buff;
128   bool    error;
129 };
130 
131 struct cff_sub_table_offsets_t {
cff_sub_table_offsets_tCFF::cff_sub_table_offsets_t132   cff_sub_table_offsets_t () : privateDictsOffset (0)
133   {
134     topDictInfo.init ();
135     FDSelectInfo.init ();
136     FDArrayInfo.init ();
137     charStringsInfo.init ();
138     globalSubrsInfo.init ();
139     localSubrsInfos.init ();
140   }
141 
~cff_sub_table_offsets_tCFF::cff_sub_table_offsets_t142   ~cff_sub_table_offsets_t () { localSubrsInfos.fini (); }
143 
144   table_info_t     topDictInfo;
145   table_info_t     FDSelectInfo;
146   table_info_t     FDArrayInfo;
147   table_info_t     charStringsInfo;
148   unsigned int  privateDictsOffset;
149   table_info_t     globalSubrsInfo;
150   hb_vector_t<table_info_t>  localSubrsInfos;
151 };
152 
153 template <typename OPSTR=op_str_t>
154 struct cff_top_dict_op_serializer_t : op_serializer_t
155 {
serializeCFF::cff_top_dict_op_serializer_t156   bool serialize (hb_serialize_context_t *c,
157 		  const OPSTR &opstr,
158 		  const cff_sub_table_offsets_t &offsets) const
159   {
160     TRACE_SERIALIZE (this);
161 
162     switch (opstr.op)
163     {
164       case OpCode_CharStrings:
165 	return_trace (FontDict::serialize_offset4_op(c, opstr.op, offsets.charStringsInfo.offset));
166 
167       case OpCode_FDArray:
168 	return_trace (FontDict::serialize_offset4_op(c, opstr.op, offsets.FDArrayInfo.offset));
169 
170       case OpCode_FDSelect:
171 	return_trace (FontDict::serialize_offset4_op(c, opstr.op, offsets.FDSelectInfo.offset));
172 
173       default:
174 	return_trace (copy_opstr (c, opstr));
175     }
176     return_trace (true);
177   }
178 
calculate_serialized_sizeCFF::cff_top_dict_op_serializer_t179   unsigned int calculate_serialized_size (const OPSTR &opstr) const
180   {
181     switch (opstr.op)
182     {
183       case OpCode_CharStrings:
184       case OpCode_FDArray:
185       case OpCode_FDSelect:
186 	return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (opstr.op);
187 
188       default:
189 	return opstr.str.length;
190     }
191   }
192 };
193 
194 struct cff_font_dict_op_serializer_t : op_serializer_t
195 {
serializeCFF::cff_font_dict_op_serializer_t196   bool serialize (hb_serialize_context_t *c,
197 		  const op_str_t &opstr,
198 		  const table_info_t &privateDictInfo) const
199   {
200     TRACE_SERIALIZE (this);
201 
202     if (opstr.op == OpCode_Private)
203     {
204       /* serialize the private dict size & offset as 2-byte & 4-byte integers */
205       if (unlikely (!UnsizedByteStr::serialize_int2 (c, privateDictInfo.size) ||
206 		    !UnsizedByteStr::serialize_int4 (c, privateDictInfo.offset)))
207 	return_trace (false);
208 
209       /* serialize the opcode */
210       HBUINT8 *p = c->allocate_size<HBUINT8> (1);
211       if (unlikely (p == nullptr)) return_trace (false);
212       *p = OpCode_Private;
213 
214       return_trace (true);
215     }
216     else
217     {
218       HBUINT8 *d = c->allocate_size<HBUINT8> (opstr.str.length);
219       if (unlikely (d == nullptr)) return_trace (false);
220       memcpy (d, &opstr.str[0], opstr.str.length);
221     }
222     return_trace (true);
223   }
224 
calculate_serialized_sizeCFF::cff_font_dict_op_serializer_t225   unsigned int calculate_serialized_size (const op_str_t &opstr) const
226   {
227     if (opstr.op == OpCode_Private)
228       return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (OpCode_Private);
229     else
230       return opstr.str.length;
231   }
232 };
233 
234 struct cff_private_dict_op_serializer_t : op_serializer_t
235 {
cff_private_dict_op_serializer_tCFF::cff_private_dict_op_serializer_t236   cff_private_dict_op_serializer_t (bool desubroutinize_, bool drop_hints_)
237     : desubroutinize (desubroutinize_), drop_hints (drop_hints_) {}
238 
serializeCFF::cff_private_dict_op_serializer_t239   bool serialize (hb_serialize_context_t *c,
240 		  const op_str_t &opstr,
241 		  const unsigned int subrsOffset) const
242   {
243     TRACE_SERIALIZE (this);
244 
245     if (drop_hints && dict_opset_t::is_hint_op (opstr.op))
246       return true;
247     if (opstr.op == OpCode_Subrs)
248     {
249       if (desubroutinize || (subrsOffset == 0))
250 	return_trace (true);
251       else
252 	return_trace (FontDict::serialize_offset2_op (c, opstr.op, subrsOffset));
253     }
254     else
255       return_trace (copy_opstr (c, opstr));
256   }
257 
calculate_serialized_sizeCFF::cff_private_dict_op_serializer_t258   unsigned int calculate_serialized_size (const op_str_t &opstr,
259 					  bool has_localsubr=true) const
260   {
261     if (drop_hints && dict_opset_t::is_hint_op (opstr.op))
262       return 0;
263     if (opstr.op == OpCode_Subrs)
264     {
265       if (desubroutinize || !has_localsubr)
266 	return 0;
267       else
268 	return OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (opstr.op);
269     }
270     else
271       return opstr.str.length;
272   }
273 
274   protected:
275   const bool  desubroutinize;
276   const bool  drop_hints;
277 };
278 
279 struct flatten_param_t
280 {
281   str_buff_t     &flatStr;
282   bool	drop_hints;
283 };
284 
285 template <typename ACC, typename ENV, typename OPSET, op_code_t endchar_op=OpCode_Invalid>
286 struct subr_flattener_t
287 {
subr_flattener_tCFF::subr_flattener_t288   subr_flattener_t (const ACC &acc_,
289 		    const hb_subset_plan_t *plan_)
290 		   : acc (acc_), plan (plan_) {}
291 
flattenCFF::subr_flattener_t292   bool flatten (str_buff_vec_t &flat_charstrings)
293   {
294     if (!flat_charstrings.resize (plan->num_output_glyphs ()))
295       return false;
296     for (unsigned int i = 0; i < plan->num_output_glyphs (); i++)
297       flat_charstrings[i].init ();
298     for (unsigned int i = 0; i < plan->num_output_glyphs (); i++)
299     {
300       hb_codepoint_t  glyph;
301       if (!plan->old_gid_for_new_gid (i, &glyph))
302       {
303       	/* add an endchar only charstring for a missing glyph if CFF1 */
304       	if (endchar_op != OpCode_Invalid) flat_charstrings[i].push (endchar_op);
305       	continue;
306       }
307       const byte_str_t str = (*acc.charStrings)[glyph];
308       unsigned int fd = acc.fdSelect->get_fd (glyph);
309       if (unlikely (fd >= acc.fdCount))
310       	return false;
311       cs_interpreter_t<ENV, OPSET, flatten_param_t> interp;
312       interp.env.init (str, acc, fd);
313       flatten_param_t  param = { flat_charstrings[i], plan->drop_hints };
314       if (unlikely (!interp.interpret (param)))
315 	return false;
316     }
317     return true;
318   }
319 
320   const ACC &acc;
321   const hb_subset_plan_t *plan;
322 };
323 
324 struct subr_closures_t
325 {
subr_closures_tCFF::subr_closures_t326   subr_closures_t () : valid (false), global_closure (nullptr)
327   { local_closures.init (); }
328 
initCFF::subr_closures_t329   void init (unsigned int fd_count)
330   {
331     valid = true;
332     global_closure = hb_set_create ();
333     if (global_closure == hb_set_get_empty ())
334       valid = false;
335     if (!local_closures.resize (fd_count))
336       valid = false;
337 
338     for (unsigned int i = 0; i < local_closures.length; i++)
339     {
340       local_closures[i] = hb_set_create ();
341       if (local_closures[i] == hb_set_get_empty ())
342 	valid = false;
343     }
344   }
345 
finiCFF::subr_closures_t346   void fini ()
347   {
348     hb_set_destroy (global_closure);
349     for (unsigned int i = 0; i < local_closures.length; i++)
350       hb_set_destroy (local_closures[i]);
351     local_closures.fini ();
352   }
353 
resetCFF::subr_closures_t354   void reset ()
355   {
356     hb_set_clear (global_closure);
357     for (unsigned int i = 0; i < local_closures.length; i++)
358       hb_set_clear (local_closures[i]);
359   }
360 
is_validCFF::subr_closures_t361   bool is_valid () const { return valid; }
362   bool  valid;
363   hb_set_t  *global_closure;
364   hb_vector_t<hb_set_t *> local_closures;
365 };
366 
367 struct parsed_cs_op_t : op_str_t
368 {
initCFF::parsed_cs_op_t369   void init (unsigned int subr_num_ = 0)
370   {
371     op_str_t::init ();
372     subr_num = subr_num_;
373     drop_flag = false;
374     keep_flag = false;
375     skip_flag = false;
376   }
377 
finiCFF::parsed_cs_op_t378   void fini () { op_str_t::fini (); }
379 
for_dropCFF::parsed_cs_op_t380   bool for_drop () const { return drop_flag; }
set_dropCFF::parsed_cs_op_t381   void set_drop ()       { if (!for_keep ()) drop_flag = true; }
382 
for_keepCFF::parsed_cs_op_t383   bool for_keep () const { return keep_flag; }
set_keepCFF::parsed_cs_op_t384   void set_keep ()       { keep_flag = true; }
385 
for_skipCFF::parsed_cs_op_t386   bool for_skip () const { return skip_flag; }
set_skipCFF::parsed_cs_op_t387   void set_skip ()       { skip_flag = true; }
388 
389   unsigned int  subr_num;
390 
391   protected:
392   bool	  drop_flag : 1;
393   bool	  keep_flag : 1;
394   bool	  skip_flag : 1;
395 };
396 
397 struct parsed_cs_str_t : parsed_values_t<parsed_cs_op_t>
398 {
initCFF::parsed_cs_str_t399   void init ()
400   {
401     SUPER::init ();
402     parsed = false;
403     hint_dropped = false;
404     has_prefix_ = false;
405   }
406 
add_opCFF::parsed_cs_str_t407   void add_op (op_code_t op, const byte_str_ref_t& str_ref)
408   {
409     if (!is_parsed ())
410       SUPER::add_op (op, str_ref);
411   }
412 
add_call_opCFF::parsed_cs_str_t413   void add_call_op (op_code_t op, const byte_str_ref_t& str_ref, unsigned int subr_num)
414   {
415     if (!is_parsed ())
416     {
417       unsigned int parsed_len = get_count ();
418       if (likely (parsed_len > 0))
419 	values[parsed_len-1].set_skip ();
420 
421       parsed_cs_op_t val;
422       val.init (subr_num);
423       SUPER::add_op (op, str_ref, val);
424     }
425   }
426 
set_prefixCFF::parsed_cs_str_t427   void set_prefix (const number_t &num, op_code_t op = OpCode_Invalid)
428   {
429     has_prefix_ = true;
430     prefix_op_ = op;
431     prefix_num_ = num;
432   }
433 
at_endCFF::parsed_cs_str_t434   bool at_end (unsigned int pos) const
435   {
436     return ((pos + 1 >= values.length) /* CFF2 */
437 	|| (values[pos + 1].op == OpCode_return));
438   }
439 
is_parsedCFF::parsed_cs_str_t440   bool is_parsed () const { return parsed; }
set_parsedCFF::parsed_cs_str_t441   void set_parsed ()      { parsed = true; }
442 
is_hint_droppedCFF::parsed_cs_str_t443   bool is_hint_dropped () const { return hint_dropped; }
set_hint_droppedCFF::parsed_cs_str_t444   void set_hint_dropped ()      { hint_dropped = true; }
445 
is_vsindex_droppedCFF::parsed_cs_str_t446   bool is_vsindex_dropped () const { return vsindex_dropped; }
set_vsindex_droppedCFF::parsed_cs_str_t447   void set_vsindex_dropped ()      { vsindex_dropped = true; }
448 
has_prefixCFF::parsed_cs_str_t449   bool has_prefix () const          { return has_prefix_; }
prefix_opCFF::parsed_cs_str_t450   op_code_t prefix_op () const         { return prefix_op_; }
prefix_numCFF::parsed_cs_str_t451   const number_t &prefix_num () const { return prefix_num_; }
452 
453   protected:
454   bool    parsed;
455   bool    hint_dropped;
456   bool    vsindex_dropped;
457   bool    has_prefix_;
458   op_code_t	prefix_op_;
459   number_t 	prefix_num_;
460 
461   private:
462   typedef parsed_values_t<parsed_cs_op_t> SUPER;
463 };
464 
465 struct parsed_cs_str_vec_t : hb_vector_t<parsed_cs_str_t>
466 {
initCFF::parsed_cs_str_vec_t467   void init (unsigned int len_ = 0)
468   {
469     SUPER::init ();
470     resize (len_);
471     for (unsigned int i = 0; i < length; i++)
472       (*this)[i].init ();
473   }
finiCFF::parsed_cs_str_vec_t474   void fini () { SUPER::fini_deep (); }
475 
476   private:
477   typedef hb_vector_t<parsed_cs_str_t> SUPER;
478 };
479 
480 struct subr_subset_param_t
481 {
initCFF::subr_subset_param_t482   void init (parsed_cs_str_t *parsed_charstring_,
483 	     parsed_cs_str_vec_t *parsed_global_subrs_, parsed_cs_str_vec_t *parsed_local_subrs_,
484 	     hb_set_t *global_closure_, hb_set_t *local_closure_,
485 	     bool drop_hints_)
486   {
487     parsed_charstring = parsed_charstring_;
488     current_parsed_str = parsed_charstring;
489     parsed_global_subrs = parsed_global_subrs_;
490     parsed_local_subrs = parsed_local_subrs_;
491     global_closure = global_closure_;
492     local_closure = local_closure_;
493     drop_hints = drop_hints_;
494   }
495 
get_parsed_str_for_contextCFF::subr_subset_param_t496   parsed_cs_str_t *get_parsed_str_for_context (call_context_t &context)
497   {
498     switch (context.type)
499     {
500       case CSType_CharString:
501 	return parsed_charstring;
502 
503       case CSType_LocalSubr:
504 	if (likely (context.subr_num < parsed_local_subrs->length))
505 	  return &(*parsed_local_subrs)[context.subr_num];
506 	break;
507 
508       case CSType_GlobalSubr:
509 	if (likely (context.subr_num < parsed_global_subrs->length))
510 	  return &(*parsed_global_subrs)[context.subr_num];
511 	break;
512     }
513     return nullptr;
514   }
515 
516   template <typename ENV>
set_current_strCFF::subr_subset_param_t517   void set_current_str (ENV &env, bool calling)
518   {
519     parsed_cs_str_t  *parsed_str = get_parsed_str_for_context (env.context);
520     if (likely (parsed_str != nullptr))
521     {
522       /* If the called subroutine is parsed partially but not completely yet,
523        * it must be because we are calling it recursively.
524        * Handle it as an error. */
525       if (unlikely (calling && !parsed_str->is_parsed () && (parsed_str->values.length > 0)))
526       	env.set_error ();
527       else
528       	current_parsed_str = parsed_str;
529     }
530     else
531       env.set_error ();
532   }
533 
534   parsed_cs_str_t	*current_parsed_str;
535 
536   parsed_cs_str_t	*parsed_charstring;
537   parsed_cs_str_vec_t	*parsed_global_subrs;
538   parsed_cs_str_vec_t	*parsed_local_subrs;
539   hb_set_t      *global_closure;
540   hb_set_t      *local_closure;
541   bool	  drop_hints;
542 };
543 
544 struct subr_remap_t : hb_inc_bimap_t
545 {
createCFF::subr_remap_t546   void create (hb_set_t *closure)
547   {
548     /* create a remapping of subroutine numbers from old to new.
549      * no optimization based on usage counts. fonttools doesn't appear doing that either.
550      */
551 
552     hb_codepoint_t old_num = HB_SET_VALUE_INVALID;
553     while (hb_set_next (closure, &old_num))
554       add (old_num);
555 
556     if (get_population () < 1240)
557       bias = 107;
558     else if (get_population () < 33900)
559       bias = 1131;
560     else
561       bias = 32768;
562   }
563 
biased_numCFF::subr_remap_t564   int biased_num (unsigned int old_num) const
565   {
566     hb_codepoint_t new_num = get (old_num);
567     return (int)new_num - bias;
568   }
569 
570   protected:
571   int bias;
572 };
573 
574 struct subr_remaps_t
575 {
subr_remaps_tCFF::subr_remaps_t576   subr_remaps_t ()
577   {
578     global_remap.init ();
579     local_remaps.init ();
580   }
581 
~subr_remaps_tCFF::subr_remaps_t582   ~subr_remaps_t () { fini (); }
583 
initCFF::subr_remaps_t584   void init (unsigned int fdCount)
585   {
586     local_remaps.resize (fdCount);
587     for (unsigned int i = 0; i < fdCount; i++)
588       local_remaps[i].init ();
589   }
590 
createCFF::subr_remaps_t591   void create (subr_closures_t& closures)
592   {
593     global_remap.create (closures.global_closure);
594     for (unsigned int i = 0; i < local_remaps.length; i++)
595       local_remaps[i].create (closures.local_closures[i]);
596   }
597 
finiCFF::subr_remaps_t598   void fini ()
599   {
600     global_remap.fini ();
601     local_remaps.fini_deep ();
602   }
603 
604   subr_remap_t	       global_remap;
605   hb_vector_t<subr_remap_t>  local_remaps;
606 };
607 
608 template <typename SUBSETTER, typename SUBRS, typename ACC, typename ENV, typename OPSET, op_code_t endchar_op=OpCode_Invalid>
609 struct subr_subsetter_t
610 {
subr_subsetter_tCFF::subr_subsetter_t611   subr_subsetter_t (ACC &acc_, const hb_subset_plan_t *plan_)
612     : acc (acc_), plan (plan_)
613   {
614     parsed_charstrings.init ();
615     parsed_global_subrs.init ();
616     parsed_local_subrs.init ();
617   }
618 
~subr_subsetter_tCFF::subr_subsetter_t619   ~subr_subsetter_t ()
620   {
621     closures.fini ();
622     remaps.fini ();
623     parsed_charstrings.fini_deep ();
624     parsed_global_subrs.fini_deep ();
625     parsed_local_subrs.fini_deep ();
626   }
627 
628   /* Subroutine subsetting with --no-desubroutinize runs in phases:
629    *
630    * 1. execute charstrings/subroutines to determine subroutine closures
631    * 2. parse out all operators and numbers
632    * 3. mark hint operators and operands for removal if --no-hinting
633    * 4. re-encode all charstrings and subroutines with new subroutine numbers
634    *
635    * Phases #1 and #2 are done at the same time in collect_subrs ().
636    * Phase #3 walks charstrings/subroutines forward then backward (hence parsing required),
637    * because we can't tell if a number belongs to a hint op until we see the first moveto.
638    *
639    * Assumption: a callsubr/callgsubr operator must immediately follow a (biased) subroutine number
640    * within the same charstring/subroutine, e.g., not split across a charstring and a subroutine.
641    */
subsetCFF::subr_subsetter_t642   bool subset (void)
643   {
644     closures.init (acc.fdCount);
645     remaps.init (acc.fdCount);
646 
647     parsed_charstrings.init (plan->num_output_glyphs ());
648     parsed_global_subrs.init (acc.globalSubrs->count);
649     parsed_local_subrs.resize (acc.fdCount);
650     for (unsigned int i = 0; i < acc.fdCount; i++)
651     {
652       parsed_local_subrs[i].init (acc.privateDicts[i].localSubrs->count);
653     }
654     if (unlikely (!closures.valid))
655       return false;
656 
657     /* phase 1 & 2 */
658     for (unsigned int i = 0; i < plan->num_output_glyphs (); i++)
659     {
660       hb_codepoint_t  glyph;
661       if (!plan->old_gid_for_new_gid (i, &glyph))
662       	continue;
663       const byte_str_t str = (*acc.charStrings)[glyph];
664       unsigned int fd = acc.fdSelect->get_fd (glyph);
665       if (unlikely (fd >= acc.fdCount))
666       	return false;
667 
668       cs_interpreter_t<ENV, OPSET, subr_subset_param_t> interp;
669       interp.env.init (str, acc, fd);
670 
671       subr_subset_param_t  param;
672       param.init (&parsed_charstrings[i],
673 		  &parsed_global_subrs,  &parsed_local_subrs[fd],
674 		  closures.global_closure, closures.local_closures[fd],
675 		  plan->drop_hints);
676 
677       if (unlikely (!interp.interpret (param)))
678 	return false;
679 
680       /* finalize parsed string esp. copy CFF1 width or CFF2 vsindex to the parsed charstring for encoding */
681       SUBSETTER::finalize_parsed_str (interp.env, param, parsed_charstrings[i]);
682     }
683 
684     if (plan->drop_hints)
685     {
686       /* mark hint ops and arguments for drop */
687       for (unsigned int i = 0; i < plan->num_output_glyphs (); i++)
688       {
689 	hb_codepoint_t  glyph;
690 	if (!plan->old_gid_for_new_gid (i, &glyph))
691 	  continue;
692 	unsigned int fd = acc.fdSelect->get_fd (glyph);
693 	if (unlikely (fd >= acc.fdCount))
694 	  return false;
695 	subr_subset_param_t  param;
696 	param.init (&parsed_charstrings[i],
697 		    &parsed_global_subrs,  &parsed_local_subrs[fd],
698 		    closures.global_closure, closures.local_closures[fd],
699 		    plan->drop_hints);
700 
701 	drop_hints_param_t  drop;
702 	if (drop_hints_in_str (parsed_charstrings[i], param, drop))
703 	{
704 	  parsed_charstrings[i].set_hint_dropped ();
705 	  if (drop.vsindex_dropped)
706 	    parsed_charstrings[i].set_vsindex_dropped ();
707 	}
708       }
709 
710       /* after dropping hints recreate closures of actually used subrs */
711       closures.reset ();
712       for (unsigned int i = 0; i < plan->num_output_glyphs (); i++)
713       {
714 	hb_codepoint_t  glyph;
715 	if (!plan->old_gid_for_new_gid (i, &glyph))
716 	  continue;
717 	unsigned int fd = acc.fdSelect->get_fd (glyph);
718 	if (unlikely (fd >= acc.fdCount))
719 	  return false;
720 	subr_subset_param_t  param;
721 	param.init (&parsed_charstrings[i],
722 		    &parsed_global_subrs,  &parsed_local_subrs[fd],
723 		    closures.global_closure, closures.local_closures[fd],
724 		    plan->drop_hints);
725 	collect_subr_refs_in_str (parsed_charstrings[i], param);
726       }
727     }
728 
729     remaps.create (closures);
730 
731     return true;
732   }
733 
encode_charstringsCFF::subr_subsetter_t734   bool encode_charstrings (str_buff_vec_t &buffArray) const
735   {
736     if (unlikely (!buffArray.resize (plan->num_output_glyphs ())))
737       return false;
738     for (unsigned int i = 0; i < plan->num_output_glyphs (); i++)
739     {
740       hb_codepoint_t  glyph;
741       if (!plan->old_gid_for_new_gid (i, &glyph))
742       {
743       	/* add an endchar only charstring for a missing glyph if CFF1 */
744       	if (endchar_op != OpCode_Invalid) buffArray[i].push (endchar_op);
745       	continue;
746       }
747       unsigned int  fd = acc.fdSelect->get_fd (glyph);
748       if (unlikely (fd >= acc.fdCount))
749       	return false;
750       if (unlikely (!encode_str (parsed_charstrings[i], fd, buffArray[i])))
751 	return false;
752     }
753     return true;
754   }
755 
encode_subrsCFF::subr_subsetter_t756   bool encode_subrs (const parsed_cs_str_vec_t &subrs, const subr_remap_t& remap, unsigned int fd, str_buff_vec_t &buffArray) const
757   {
758     unsigned int  count = remap.get_population ();
759 
760     if (unlikely (!buffArray.resize (count)))
761       return false;
762     for (unsigned int old_num = 0; old_num < subrs.length; old_num++)
763     {
764       hb_codepoint_t new_num = remap[old_num];
765       if (new_num != CFF_UNDEF_CODE)
766       {
767 	if (unlikely (!encode_str (subrs[old_num], fd, buffArray[new_num])))
768 	  return false;
769       }
770     }
771     return true;
772   }
773 
encode_globalsubrsCFF::subr_subsetter_t774   bool encode_globalsubrs (str_buff_vec_t &buffArray)
775   {
776     return encode_subrs (parsed_global_subrs, remaps.global_remap, 0, buffArray);
777   }
778 
encode_localsubrsCFF::subr_subsetter_t779   bool encode_localsubrs (unsigned int fd, str_buff_vec_t &buffArray) const
780   {
781     return encode_subrs (parsed_local_subrs[fd], remaps.local_remaps[fd], fd, buffArray);
782   }
783 
784   protected:
785   struct drop_hints_param_t
786   {
drop_hints_param_tCFF::subr_subsetter_t::drop_hints_param_t787     drop_hints_param_t ()
788       : seen_moveto (false),
789 	ends_in_hint (false),
790 	all_dropped (false),
791 	vsindex_dropped (false) {}
792 
793     bool  seen_moveto;
794     bool  ends_in_hint;
795     bool  all_dropped;
796     bool  vsindex_dropped;
797   };
798 
drop_hints_in_subrCFF::subr_subsetter_t799   bool drop_hints_in_subr (parsed_cs_str_t &str, unsigned int pos,
800 			   parsed_cs_str_vec_t &subrs, unsigned int subr_num,
801 			   const subr_subset_param_t &param, drop_hints_param_t &drop)
802   {
803     drop.ends_in_hint = false;
804     bool has_hint = drop_hints_in_str (subrs[subr_num], param, drop);
805 
806     /* if this subr ends with a stem hint (i.e., not a number; potential argument for moveto),
807      * then this entire subroutine must be a hint. drop its call. */
808     if (drop.ends_in_hint)
809     {
810       str.values[pos].set_drop ();
811       /* if this subr call is at the end of the parent subr, propagate the flag
812        * otherwise reset the flag */
813       if (!str.at_end (pos))
814 	drop.ends_in_hint = false;
815     }
816     else if (drop.all_dropped)
817     {
818       str.values[pos].set_drop ();
819     }
820 
821     return has_hint;
822   }
823 
824   /* returns true if it sees a hint op before the first moveto */
drop_hints_in_strCFF::subr_subsetter_t825   bool drop_hints_in_str (parsed_cs_str_t &str, const subr_subset_param_t &param, drop_hints_param_t &drop)
826   {
827     bool  seen_hint = false;
828 
829     for (unsigned int pos = 0; pos < str.values.length; pos++)
830     {
831       bool  has_hint = false;
832       switch (str.values[pos].op)
833       {
834 	case OpCode_callsubr:
835 	  has_hint = drop_hints_in_subr (str, pos,
836 					*param.parsed_local_subrs, str.values[pos].subr_num,
837 					param, drop);
838 	  break;
839 
840 	case OpCode_callgsubr:
841 	  has_hint = drop_hints_in_subr (str, pos,
842 					*param.parsed_global_subrs, str.values[pos].subr_num,
843 					param, drop);
844 	  break;
845 
846 	case OpCode_rmoveto:
847 	case OpCode_hmoveto:
848 	case OpCode_vmoveto:
849 	  drop.seen_moveto = true;
850 	  break;
851 
852 	case OpCode_hintmask:
853 	case OpCode_cntrmask:
854 	  if (drop.seen_moveto)
855 	  {
856 	    str.values[pos].set_drop ();
857 	    break;
858 	  }
859 	  HB_FALLTHROUGH;
860 
861 	case OpCode_hstemhm:
862 	case OpCode_vstemhm:
863 	case OpCode_hstem:
864 	case OpCode_vstem:
865 	  has_hint = true;
866 	  str.values[pos].set_drop ();
867 	  if (str.at_end (pos))
868 	    drop.ends_in_hint = true;
869 	  break;
870 
871 	case OpCode_dotsection:
872 	  str.values[pos].set_drop ();
873 	  break;
874 
875 	default:
876 	  /* NONE */
877 	  break;
878       }
879       if (has_hint)
880       {
881 	for (int i = pos - 1; i >= 0; i--)
882 	{
883 	  parsed_cs_op_t  &csop = str.values[(unsigned)i];
884 	  if (csop.for_drop ())
885 	    break;
886 	  csop.set_drop ();
887 	  if (csop.op == OpCode_vsindexcs)
888 	    drop.vsindex_dropped = true;
889 	}
890 	seen_hint |= has_hint;
891       }
892     }
893 
894     /* Raise all_dropped flag if all operators except return are dropped from a subr.
895      * It may happen even after seeing the first moveto if a subr contains
896      * only (usually one) hintmask operator, then calls to this subr can be dropped.
897      */
898     drop.all_dropped = true;
899     for (unsigned int pos = 0; pos < str.values.length; pos++)
900     {
901       parsed_cs_op_t  &csop = str.values[pos];
902       if (csop.op == OpCode_return)
903       	break;
904       if (!csop.for_drop ())
905       {
906       	drop.all_dropped = false;
907       	break;
908       }
909     }
910 
911     return seen_hint;
912   }
913 
collect_subr_refs_in_subrCFF::subr_subsetter_t914   void collect_subr_refs_in_subr (parsed_cs_str_t &str, unsigned int pos,
915 				  unsigned int subr_num, parsed_cs_str_vec_t &subrs,
916 				  hb_set_t *closure,
917 				  const subr_subset_param_t &param)
918   {
919     closure->add (subr_num);
920     collect_subr_refs_in_str (subrs[subr_num], param);
921   }
922 
collect_subr_refs_in_strCFF::subr_subsetter_t923   void collect_subr_refs_in_str (parsed_cs_str_t &str, const subr_subset_param_t &param)
924   {
925     for (unsigned int pos = 0; pos < str.values.length; pos++)
926     {
927       if (!str.values[pos].for_drop ())
928       {
929 	switch (str.values[pos].op)
930 	{
931 	  case OpCode_callsubr:
932 	    collect_subr_refs_in_subr (str, pos,
933 				       str.values[pos].subr_num, *param.parsed_local_subrs,
934 				       param.local_closure, param);
935 	    break;
936 
937 	  case OpCode_callgsubr:
938 	    collect_subr_refs_in_subr (str, pos,
939 				       str.values[pos].subr_num, *param.parsed_global_subrs,
940 				       param.global_closure, param);
941 	    break;
942 
943 	  default: break;
944 	}
945       }
946     }
947   }
948 
encode_strCFF::subr_subsetter_t949   bool encode_str (const parsed_cs_str_t &str, const unsigned int fd, str_buff_t &buff) const
950   {
951     buff.init ();
952     str_encoder_t  encoder (buff);
953     encoder.reset ();
954     /* if a prefix (CFF1 width or CFF2 vsindex) has been removed along with hints,
955      * re-insert it at the beginning of charstreing */
956     if (str.has_prefix () && str.is_hint_dropped ())
957     {
958       encoder.encode_num (str.prefix_num ());
959       if (str.prefix_op () != OpCode_Invalid)
960 	encoder.encode_op (str.prefix_op ());
961     }
962     for (unsigned int i = 0; i < str.get_count(); i++)
963     {
964       const parsed_cs_op_t  &opstr = str.values[i];
965       if (!opstr.for_drop () && !opstr.for_skip ())
966       {
967 	switch (opstr.op)
968 	{
969 	  case OpCode_callsubr:
970 	    encoder.encode_int (remaps.local_remaps[fd].biased_num (opstr.subr_num));
971 	    encoder.encode_op (OpCode_callsubr);
972 	    break;
973 
974 	  case OpCode_callgsubr:
975 	    encoder.encode_int (remaps.global_remap.biased_num (opstr.subr_num));
976 	    encoder.encode_op (OpCode_callgsubr);
977 	    break;
978 
979 	  default:
980 	    encoder.copy_str (opstr.str);
981 	    break;
982 	}
983       }
984     }
985     return !encoder.is_error ();
986   }
987 
988   protected:
989   const ACC   			&acc;
990   const hb_subset_plan_t	*plan;
991 
992   subr_closures_t		closures;
993 
994   parsed_cs_str_vec_t		parsed_charstrings;
995   parsed_cs_str_vec_t		parsed_global_subrs;
996   hb_vector_t<parsed_cs_str_vec_t>  parsed_local_subrs;
997 
998   subr_remaps_t			remaps;
999 
1000   private:
1001   typedef typename SUBRS::count_type subr_count_type;
1002 };
1003 
1004 } /* namespace CFF */
1005 
1006 HB_INTERNAL bool
1007 hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan,
1008 			    unsigned int fdCount,
1009 			    const CFF::FDSelect &src, /* IN */
1010 			    unsigned int &subset_fd_count /* OUT */,
1011 			    unsigned int &subset_fdselect_size /* OUT */,
1012 			    unsigned int &subset_fdselect_format /* OUT */,
1013 			    hb_vector_t<CFF::code_pair_t> &fdselect_ranges /* OUT */,
1014 			    hb_inc_bimap_t &fdmap /* OUT */);
1015 
1016 HB_INTERNAL bool
1017 hb_serialize_cff_fdselect (hb_serialize_context_t *c,
1018 			  unsigned int num_glyphs,
1019 			  const CFF::FDSelect &src,
1020 			  unsigned int fd_count,
1021 			  unsigned int fdselect_format,
1022 			  unsigned int size,
1023 			  const hb_vector_t<CFF::code_pair_t> &fdselect_ranges);
1024 
1025 #endif /* HB_SUBSET_CFF_COMMON_HH */
1026