tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

hb-subset-cff-common.hh (34658B)


      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 {
     40  str_encoder_t (str_buff_t &buff_)
     41    : buff (buff_) {}
     42 
     43  void reset () { buff.reset (); }
     44 
     45  void encode_byte (unsigned char b)
     46  {
     47    if (likely ((signed) buff.length < buff.allocated))
     48      buff.arrayZ[buff.length++] = b;
     49    else
     50      buff.push (b);
     51  }
     52 
     53  void encode_int (int v)
     54  {
     55    if ((-1131 <= v) && (v <= 1131))
     56    {
     57      if ((-107 <= v) && (v <= 107))
     58 encode_byte (v + 139);
     59      else if (v > 0)
     60      {
     61 v -= 108;
     62 encode_byte ((v >> 8) + OpCode_TwoBytePosInt0);
     63 encode_byte (v & 0xFF);
     64      }
     65      else
     66      {
     67 v = -v - 108;
     68 encode_byte ((v >> 8) + OpCode_TwoByteNegInt0);
     69 encode_byte (v & 0xFF);
     70      }
     71    }
     72    else
     73    {
     74      if (unlikely (v < -32768))
     75 v = -32768;
     76      else if (unlikely (v > 32767))
     77 v = 32767;
     78      encode_byte (OpCode_shortint);
     79      encode_byte ((v >> 8) & 0xFF);
     80      encode_byte (v & 0xFF);
     81    }
     82  }
     83 
     84  // Encode number for CharString
     85  void encode_num_cs (const number_t& n)
     86  {
     87    if (n.in_int_range ())
     88    {
     89      encode_int (n.to_int ());
     90    }
     91    else
     92    {
     93      int32_t v = n.to_fixed ();
     94      encode_byte (OpCode_fixedcs);
     95      encode_byte ((v >> 24) & 0xFF);
     96      encode_byte ((v >> 16) & 0xFF);
     97      encode_byte ((v >> 8) & 0xFF);
     98      encode_byte (v & 0xFF);
     99    }
    100  }
    101 
    102  // Encode number for TopDict / Private
    103  void encode_num_tp (const number_t& n)
    104  {
    105    if (n.in_int_range ())
    106    {
    107      // TODO longint
    108      encode_int (n.to_int ());
    109    }
    110    else
    111    {
    112      // Sigh. BCD
    113      // https://learn.microsoft.com/en-us/typography/opentype/spec/cff2#table-5-nibble-definitions
    114      double v = n.to_real ();
    115      encode_byte (OpCode_BCD);
    116 
    117      // Based on:
    118      // https://github.com/fonttools/fonttools/blob/0738c41dfbcbc213ab9263f486ef0cccc6eb5ce5/Lib/fontTools/misc/psCharStrings.py#L267-L316
    119 
    120      char buf[16];
    121      /* FontTools has the following comment:
    122       *
    123       * # Note: 14 decimal digits seems to be the limitation for CFF real numbers
    124       * # in macOS. However, we use 8 here to match the implementation of AFDKO.
    125       *
    126       * We use 8 here to match FontTools X-).
    127       */
    128 
    129      hb_locale_t clocale HB_UNUSED;
    130      hb_locale_t oldlocale HB_UNUSED;
    131      oldlocale = hb_uselocale (clocale = newlocale (LC_ALL_MASK, "C", NULL));
    132      snprintf (buf, sizeof (buf), "%.8G", v);
    133      (void) hb_uselocale (((void) freelocale (clocale), oldlocale));
    134 
    135      char *s = buf;
    136      size_t len;
    137      char *comma = strchr (s, ',');
    138      if (comma) // Comma for some European locales in case no uselocale available.
    139 *comma = '.';
    140      if (s[0] == '0' && s[1] == '.')
    141 s++;
    142      else if (s[0] == '-' && s[1] == '0' && s[2] == '.')
    143      {
    144 s[1] = '-';
    145 s++;
    146      }
    147      else if ((len = strlen (s)) > 3 && !strcmp (s + len - 3, "000"))
    148      {
    149 unsigned exponent = len - 3;
    150 char *s2 = s + exponent - 1;
    151 while (*s2 == '0' && exponent > 1)
    152 {
    153   s2--;
    154   exponent++;
    155 }
    156 snprintf (s2 + 1, sizeof (buf) - (s2 + 1 - buf), "E%u", exponent);
    157      }
    158      else
    159      {
    160 char *dot = strchr (s, '.');
    161 char *e = strchr (s, 'E');
    162 if (dot && e)
    163 {
    164   memmove (dot, dot + 1, e - (dot + 1));
    165   int exponent = atoi (e + 1);
    166   int new_exponent = exponent - (e - (dot + 1));
    167   if (new_exponent == 1)
    168   {
    169     e[-1] = '0';
    170     e[0] = '\0';
    171   }
    172   else
    173     snprintf (e - 1, sizeof (buf) - (e - 1 - buf), "E%d", new_exponent);
    174 }
    175      }
    176      if ((s[0] == '.' && s[1] == '0') || (s[0] == '-' && s[1] == '.' && s[2] == '0'))
    177      {
    178 int sign = s[0] == '-';
    179 char *s2 = s + sign + 1;
    180 while (*s2 == '0')
    181   s2++;
    182 len = strlen (s2);
    183 memmove (s + sign, s2, len);
    184 snprintf (s + sign + len, sizeof (buf) - (s + sign + len - buf), "E-%u", (unsigned) (strlen (s + sign) - 1));
    185      }
    186      hb_vector_t<char> nibbles;
    187      while (*s)
    188      {
    189 char c = s[0];
    190 s++;
    191 
    192 switch (c)
    193 {
    194   case 'E':
    195   {
    196     char c2 = *s;
    197     if (c2 == '-')
    198     {
    199       s++;
    200       nibbles.push (0x0C); // E-
    201     } else {
    202       if (c2 == '+')
    203 	s++;
    204       nibbles.push (0x0B); // E
    205     }
    206     if (*s == '0')
    207       s++;
    208     continue;
    209   }
    210 
    211   case '.':
    212     nibbles.push (0x0A); // .
    213     continue;
    214 
    215   case '-':
    216     nibbles.push (0x0E); // -
    217     continue;
    218 }
    219 
    220 nibbles.push (c - '0');
    221      }
    222      nibbles.push (0x0F);
    223      if (nibbles.length % 2)
    224 nibbles.push (0x0F);
    225 
    226      unsigned count = nibbles.length;
    227      for (unsigned i = 0; i < count; i += 2)
    228        encode_byte ((nibbles[i] << 4) | nibbles[i+1]);
    229    }
    230  }
    231 
    232  void encode_op (op_code_t op)
    233  {
    234    if (Is_OpCode_ESC (op))
    235    {
    236      encode_byte (OpCode_escape);
    237      encode_byte (Unmake_OpCode_ESC (op));
    238    }
    239    else
    240      encode_byte (op);
    241  }
    242 
    243  void copy_str (const unsigned char *str, unsigned length)
    244  {
    245    assert ((signed) (buff.length + length) <= buff.allocated);
    246    hb_memcpy (buff.arrayZ + buff.length, str, length);
    247    buff.length += length;
    248  }
    249 
    250  bool in_error () const { return buff.in_error (); }
    251 
    252  protected:
    253 
    254  str_buff_t &buff;
    255 };
    256 
    257 struct cff_sub_table_info_t {
    258  cff_sub_table_info_t ()
    259    : fd_array_link (0),
    260      char_strings_link (0)
    261  {
    262    fd_select.init ();
    263  }
    264 
    265  table_info_t     fd_select;
    266  objidx_t     	   fd_array_link;
    267  objidx_t     	   char_strings_link;
    268 };
    269 
    270 template <typename OPSTR=op_str_t>
    271 struct cff_top_dict_op_serializer_t : op_serializer_t
    272 {
    273  bool serialize (hb_serialize_context_t *c,
    274 	  const OPSTR &opstr,
    275 	  const cff_sub_table_info_t &info) const
    276  {
    277    TRACE_SERIALIZE (this);
    278 
    279    switch (opstr.op)
    280    {
    281      case OpCode_CharStrings:
    282 return_trace (FontDict::serialize_link4_op(c, opstr.op, info.char_strings_link, whence_t::Absolute));
    283 
    284      case OpCode_FDArray:
    285 return_trace (FontDict::serialize_link4_op(c, opstr.op, info.fd_array_link, whence_t::Absolute));
    286 
    287      case OpCode_FDSelect:
    288 return_trace (FontDict::serialize_link4_op(c, opstr.op, info.fd_select.link, whence_t::Absolute));
    289 
    290      default:
    291 return_trace (copy_opstr (c, opstr));
    292    }
    293    return_trace (true);
    294  }
    295 };
    296 
    297 struct cff_font_dict_op_serializer_t : op_serializer_t
    298 {
    299  bool serialize (hb_serialize_context_t *c,
    300 	  const op_str_t &opstr,
    301 	  const table_info_t &privateDictInfo) const
    302  {
    303    TRACE_SERIALIZE (this);
    304 
    305    if (opstr.op == OpCode_Private)
    306    {
    307      /* serialize the private dict size & offset as 2-byte & 4-byte integers */
    308      return_trace (UnsizedByteStr::serialize_int2 (c, privateDictInfo.size) &&
    309 	    Dict::serialize_link4_op (c, opstr.op, privateDictInfo.link, whence_t::Absolute));
    310    }
    311    else
    312    {
    313      unsigned char *d = c->allocate_size<unsigned char> (opstr.length);
    314      if (unlikely (!d)) return_trace (false);
    315      /* Faster than hb_memcpy for small strings. */
    316      for (unsigned i = 0; i < opstr.length; i++)
    317 d[i] = opstr.ptr[i];
    318      //hb_memcpy (d, opstr.ptr, opstr.length);
    319    }
    320    return_trace (true);
    321  }
    322 };
    323 
    324 struct flatten_param_t
    325 {
    326  str_buff_t     &flatStr;
    327  bool	drop_hints;
    328  const hb_subset_plan_t *plan;
    329 };
    330 
    331 template <typename ACC, typename ENV, typename OPSET, op_code_t endchar_op=OpCode_Invalid>
    332 struct subr_flattener_t
    333 {
    334  subr_flattener_t (const ACC &acc_,
    335 	    const hb_subset_plan_t *plan_)
    336 	   : acc (acc_), plan (plan_) {}
    337 
    338  bool flatten (str_buff_vec_t &flat_charstrings)
    339  {
    340    unsigned count = plan->num_output_glyphs ();
    341    if (!flat_charstrings.resize_exact (count))
    342      return false;
    343    for (unsigned int i = 0; i < count; i++)
    344    {
    345      hb_codepoint_t  glyph;
    346      if (!plan->old_gid_for_new_gid (i, &glyph))
    347      {
    348 /* add an endchar only charstring for a missing glyph if CFF1 */
    349 if (endchar_op != OpCode_Invalid) flat_charstrings[i].push (endchar_op);
    350 continue;
    351      }
    352      const hb_ubytes_t str = (*acc.charStrings)[glyph];
    353      unsigned int fd = acc.fdSelect->get_fd (glyph);
    354      if (unlikely (fd >= acc.fdCount))
    355 return false;
    356 
    357 
    358      ENV env (str, acc, fd,
    359        plan->normalized_coords.arrayZ, plan->normalized_coords.length);
    360      cs_interpreter_t<ENV, OPSET, flatten_param_t> interp (env);
    361      flatten_param_t  param = {
    362        flat_charstrings.arrayZ[i],
    363        (bool) (plan->flags & HB_SUBSET_FLAGS_NO_HINTING),
    364 plan
    365      };
    366      if (unlikely (!interp.interpret (param)))
    367 return false;
    368    }
    369    return true;
    370  }
    371 
    372  const ACC &acc;
    373  const hb_subset_plan_t *plan;
    374 };
    375 
    376 struct subr_closures_t
    377 {
    378  subr_closures_t (unsigned int fd_count) : global_closure (), local_closures ()
    379  {
    380    local_closures.resize_exact (fd_count);
    381  }
    382 
    383  void reset ()
    384  {
    385    global_closure.clear();
    386    for (unsigned int i = 0; i < local_closures.length; i++)
    387      local_closures[i].clear();
    388  }
    389 
    390  bool in_error () const { return local_closures.in_error (); }
    391  hb_set_t  global_closure;
    392  hb_vector_t<hb_set_t> local_closures;
    393 };
    394 
    395 struct parsed_cs_op_t : op_str_t
    396 {
    397  parsed_cs_op_t (unsigned int subr_num_ = 0) :
    398    subr_num (subr_num_) {}
    399 
    400  bool is_hinting () const { return hinting_flag; }
    401  void set_hinting ()       { hinting_flag = true; }
    402 
    403  /* The layout of this struct is designed to fit within the
    404   * padding of op_str_t! */
    405 
    406  protected:
    407  bool	  hinting_flag = false;
    408 
    409  public:
    410  uint16_t subr_num;
    411 };
    412 
    413 struct parsed_cs_str_t : parsed_values_t<parsed_cs_op_t>
    414 {
    415  parsed_cs_str_t () :
    416    parsed (false),
    417    hint_dropped (false),
    418    has_prefix_ (false),
    419    has_calls_ (false)
    420  {
    421    SUPER::init ();
    422  }
    423 
    424  void add_op (op_code_t op, const byte_str_ref_t& str_ref)
    425  {
    426    if (!is_parsed ())
    427      SUPER::add_op (op, str_ref);
    428  }
    429 
    430  void add_call_op (op_code_t op, const byte_str_ref_t& str_ref, unsigned int subr_num)
    431  {
    432    if (!is_parsed ())
    433    {
    434      has_calls_ = true;
    435 
    436      /* Pop the subroutine number. */
    437      values.pop ();
    438 
    439      SUPER::add_op (op, str_ref, {subr_num});
    440    }
    441  }
    442 
    443  void set_prefix (const number_t &num, op_code_t op = OpCode_Invalid)
    444  {
    445    has_prefix_ = true;
    446    prefix_op_ = op;
    447    prefix_num_ = num;
    448  }
    449 
    450  bool at_end (unsigned int pos) const
    451  {
    452    return ((pos + 1 >= values.length) /* CFF2 */
    453 || (values[pos + 1].op == OpCode_return));
    454  }
    455 
    456  bool is_parsed () const { return parsed; }
    457  void set_parsed ()      { parsed = true; }
    458 
    459  bool is_hint_dropped () const { return hint_dropped; }
    460  void set_hint_dropped ()      { hint_dropped = true; }
    461 
    462  bool is_vsindex_dropped () const { return vsindex_dropped; }
    463  void set_vsindex_dropped ()      { vsindex_dropped = true; }
    464 
    465  bool has_prefix () const          { return has_prefix_; }
    466  op_code_t prefix_op () const         { return prefix_op_; }
    467  const number_t &prefix_num () const { return prefix_num_; }
    468 
    469  bool has_calls () const          { return has_calls_; }
    470 
    471  void compact ()
    472  {
    473    unsigned count = values.length;
    474    if (!count) return;
    475    auto &opstr = values.arrayZ;
    476    unsigned j = 0;
    477    for (unsigned i = 1; i < count; i++)
    478    {
    479      /* See if we can combine op j and op i. */
    480      bool combine =
    481        (opstr[j].op != OpCode_callsubr && opstr[j].op != OpCode_callgsubr) &&
    482        (opstr[i].op != OpCode_callsubr && opstr[i].op != OpCode_callgsubr) &&
    483        (opstr[j].is_hinting () == opstr[i].is_hinting ()) &&
    484        (opstr[j].ptr + opstr[j].length == opstr[i].ptr) &&
    485        (opstr[j].length + opstr[i].length <= 255);
    486 
    487      if (combine)
    488      {
    489 opstr[j].length += opstr[i].length;
    490 opstr[j].op = OpCode_Invalid;
    491      }
    492      else
    493      {
    494 opstr[++j] = opstr[i];
    495      }
    496    }
    497    values.shrink (j + 1);
    498  }
    499 
    500  protected:
    501  bool    parsed : 1;
    502  bool    hint_dropped : 1;
    503  bool    vsindex_dropped : 1;
    504  bool    has_prefix_ : 1;
    505  bool    has_calls_ : 1;
    506  op_code_t	prefix_op_;
    507  number_t	prefix_num_;
    508 
    509  private:
    510  typedef parsed_values_t<parsed_cs_op_t> SUPER;
    511 };
    512 
    513 struct parsed_cs_str_vec_t : hb_vector_t<parsed_cs_str_t>
    514 {
    515  private:
    516  typedef hb_vector_t<parsed_cs_str_t> SUPER;
    517 };
    518 
    519 struct cff_subset_accelerator_t
    520 {
    521  static cff_subset_accelerator_t* create (
    522      hb_blob_t* original_blob,
    523      const parsed_cs_str_vec_t& parsed_charstrings,
    524      const parsed_cs_str_vec_t& parsed_global_subrs,
    525      const hb_vector_t<parsed_cs_str_vec_t>& parsed_local_subrs) {
    526    cff_subset_accelerator_t* accel =
    527        (cff_subset_accelerator_t*) hb_malloc (sizeof(cff_subset_accelerator_t));
    528    if (unlikely (!accel)) return nullptr;
    529    new (accel) cff_subset_accelerator_t (original_blob,
    530                                          parsed_charstrings,
    531                                          parsed_global_subrs,
    532                                          parsed_local_subrs);
    533    return accel;
    534  }
    535 
    536  static void destroy (void* value) {
    537    if (!value) return;
    538 
    539    cff_subset_accelerator_t* accel = (cff_subset_accelerator_t*) value;
    540    accel->~cff_subset_accelerator_t ();
    541    hb_free (accel);
    542  }
    543 
    544  cff_subset_accelerator_t(
    545      hb_blob_t* original_blob_,
    546      const parsed_cs_str_vec_t& parsed_charstrings_,
    547      const parsed_cs_str_vec_t& parsed_global_subrs_,
    548      const hb_vector_t<parsed_cs_str_vec_t>& parsed_local_subrs_)
    549  {
    550    parsed_charstrings = parsed_charstrings_;
    551    parsed_global_subrs = parsed_global_subrs_;
    552    parsed_local_subrs = parsed_local_subrs_;
    553 
    554    // the parsed charstrings point to memory in the original CFF table so we must hold a reference
    555    // to it to keep the memory valid.
    556    original_blob = hb_blob_reference (original_blob_);
    557  }
    558 
    559  ~cff_subset_accelerator_t()
    560  {
    561    hb_blob_destroy (original_blob);
    562    auto *mapping = glyph_to_sid_map.get_relaxed ();
    563    if (mapping)
    564    {
    565      mapping->~glyph_to_sid_map_t ();
    566      hb_free (mapping);
    567    }
    568  }
    569 
    570  parsed_cs_str_vec_t parsed_charstrings;
    571  parsed_cs_str_vec_t parsed_global_subrs;
    572  hb_vector_t<parsed_cs_str_vec_t> parsed_local_subrs;
    573  mutable hb_atomic_t<glyph_to_sid_map_t *> glyph_to_sid_map;
    574 
    575 private:
    576  hb_blob_t* original_blob;
    577 };
    578 
    579 struct subr_subset_param_t
    580 {
    581  subr_subset_param_t (parsed_cs_str_t *parsed_charstring_,
    582 	       parsed_cs_str_vec_t *parsed_global_subrs_,
    583 	       parsed_cs_str_vec_t *parsed_local_subrs_,
    584 	       hb_set_t *global_closure_,
    585 	       hb_set_t *local_closure_,
    586 	       bool drop_hints_) :
    587      current_parsed_str (parsed_charstring_),
    588      parsed_charstring (parsed_charstring_),
    589      parsed_global_subrs (parsed_global_subrs_),
    590      parsed_local_subrs (parsed_local_subrs_),
    591      global_closure (global_closure_),
    592      local_closure (local_closure_),
    593      drop_hints (drop_hints_) {}
    594 
    595  parsed_cs_str_t *get_parsed_str_for_context (call_context_t &context)
    596  {
    597    switch (context.type)
    598    {
    599      case CSType_CharString:
    600 return parsed_charstring;
    601 
    602      case CSType_LocalSubr:
    603 if (likely (context.subr_num < parsed_local_subrs->length))
    604   return &(*parsed_local_subrs)[context.subr_num];
    605 break;
    606 
    607      case CSType_GlobalSubr:
    608 if (likely (context.subr_num < parsed_global_subrs->length))
    609   return &(*parsed_global_subrs)[context.subr_num];
    610 break;
    611    }
    612    return nullptr;
    613  }
    614 
    615  template <typename ENV>
    616  void set_current_str (ENV &env, bool calling)
    617  {
    618    parsed_cs_str_t *parsed_str = get_parsed_str_for_context (env.context);
    619    if (unlikely (!parsed_str))
    620    {
    621      env.set_error ();
    622      return;
    623    }
    624    /* If the called subroutine is parsed partially but not completely yet,
    625     * it must be because we are calling it recursively.
    626     * Handle it as an error. */
    627    if (unlikely (calling && !parsed_str->is_parsed () && (parsed_str->values.length > 0)))
    628      env.set_error ();
    629    else
    630    {
    631      if (!parsed_str->is_parsed ())
    632        parsed_str->alloc (env.str_ref.total_size ());
    633      current_parsed_str = parsed_str;
    634    }
    635  }
    636 
    637  parsed_cs_str_t	*current_parsed_str;
    638 
    639  parsed_cs_str_t	*parsed_charstring;
    640  parsed_cs_str_vec_t	*parsed_global_subrs;
    641  parsed_cs_str_vec_t	*parsed_local_subrs;
    642  hb_set_t      *global_closure;
    643  hb_set_t      *local_closure;
    644  bool	  drop_hints;
    645 };
    646 
    647 struct subr_remap_t : hb_inc_bimap_t
    648 {
    649  void create (const hb_set_t *closure)
    650  {
    651    /* create a remapping of subroutine numbers from old to new.
    652     * no optimization based on usage counts. fonttools doesn't appear doing that either.
    653     */
    654 
    655    alloc (closure->get_population ());
    656    for (auto old_num : *closure)
    657      add (old_num);
    658 
    659    if (get_population () < 1240)
    660      bias = 107;
    661    else if (get_population () < 33900)
    662      bias = 1131;
    663    else
    664      bias = 32768;
    665  }
    666 
    667  int biased_num (unsigned int old_num) const
    668  {
    669    hb_codepoint_t new_num = get (old_num);
    670    return (int)new_num - bias;
    671  }
    672 
    673  protected:
    674  int bias;
    675 };
    676 
    677 struct subr_remaps_t
    678 {
    679  subr_remaps_t (unsigned int fdCount)
    680  {
    681    local_remaps.resize (fdCount);
    682  }
    683 
    684  bool in_error()
    685  {
    686    return local_remaps.in_error ();
    687  }
    688 
    689  void create (subr_closures_t& closures)
    690  {
    691    global_remap.create (&closures.global_closure);
    692    for (unsigned int i = 0; i < local_remaps.length; i++)
    693      local_remaps.arrayZ[i].create (&closures.local_closures[i]);
    694  }
    695 
    696  subr_remap_t	       global_remap;
    697  hb_vector_t<subr_remap_t>  local_remaps;
    698 };
    699 
    700 template <typename SUBSETTER, typename SUBRS, typename ACC, typename ENV, typename OPSET, op_code_t endchar_op=OpCode_Invalid>
    701 struct subr_subsetter_t
    702 {
    703  subr_subsetter_t (ACC &acc_, const hb_subset_plan_t *plan_)
    704      : acc (acc_), plan (plan_), closures(acc_.fdCount),
    705        remaps(acc_.fdCount)
    706  {}
    707 
    708  /* Subroutine subsetting with --no-desubroutinize runs in phases:
    709   *
    710   * 1. execute charstrings/subroutines to determine subroutine closures
    711   * 2. parse out all operators and numbers
    712   * 3. mark hint operators and operands for removal if --no-hinting
    713   * 4. re-encode all charstrings and subroutines with new subroutine numbers
    714   *
    715   * Phases #1 and #2 are done at the same time in collect_subrs ().
    716   * Phase #3 walks charstrings/subroutines forward then backward (hence parsing required),
    717   * because we can't tell if a number belongs to a hint op until we see the first moveto.
    718   *
    719   * Assumption: a callsubr/callgsubr operator must immediately follow a (biased) subroutine number
    720   * within the same charstring/subroutine, e.g., not split across a charstring and a subroutine.
    721   */
    722  bool subset (void)
    723  {
    724    unsigned fd_count = acc.fdCount;
    725    const cff_subset_accelerator_t* cff_accelerator = nullptr;
    726    if (acc.cff_accelerator) {
    727      cff_accelerator = acc.cff_accelerator;
    728      fd_count = cff_accelerator->parsed_local_subrs.length;
    729    }
    730 
    731    if (cff_accelerator) {
    732      // If we are not dropping hinting then charstrings are not modified so we can
    733      // just use a reference to the cached copies.
    734      cached_charstrings.resize_exact (plan->num_output_glyphs ());
    735      parsed_global_subrs = &cff_accelerator->parsed_global_subrs;
    736      parsed_local_subrs = &cff_accelerator->parsed_local_subrs;
    737    } else {
    738      parsed_charstrings.resize_exact (plan->num_output_glyphs ());
    739      parsed_global_subrs_storage.resize_exact (acc.globalSubrs->count);
    740 
    741      if (unlikely (!parsed_local_subrs_storage.resize (fd_count))) return false;
    742 
    743      for (unsigned int i = 0; i < acc.fdCount; i++)
    744      {
    745        unsigned count = acc.privateDicts[i].localSubrs->count;
    746        parsed_local_subrs_storage[i].resize (count);
    747        if (unlikely (parsed_local_subrs_storage[i].in_error ())) return false;
    748      }
    749 
    750      parsed_global_subrs = &parsed_global_subrs_storage;
    751      parsed_local_subrs = &parsed_local_subrs_storage;
    752    }
    753 
    754    if (unlikely (remaps.in_error()
    755                  || cached_charstrings.in_error ()
    756                  || parsed_charstrings.in_error ()
    757                  || parsed_global_subrs->in_error ()
    758                  || closures.in_error ())) {
    759      return false;
    760    }
    761 
    762    /* phase 1 & 2 */
    763    for (auto _ : plan->new_to_old_gid_list)
    764    {
    765      hb_codepoint_t new_glyph = _.first;
    766      hb_codepoint_t old_glyph = _.second;
    767 
    768      const hb_ubytes_t str = (*acc.charStrings)[old_glyph];
    769      unsigned int fd = acc.fdSelect->get_fd (old_glyph);
    770      if (unlikely (fd >= acc.fdCount))
    771        return false;
    772 
    773      if (cff_accelerator)
    774      {
    775        // parsed string already exists in accelerator, copy it and move
    776        // on.
    777        if (cached_charstrings)
    778          cached_charstrings[new_glyph] = &cff_accelerator->parsed_charstrings[old_glyph];
    779        else
    780          parsed_charstrings[new_glyph] = cff_accelerator->parsed_charstrings[old_glyph];
    781 
    782        continue;
    783      }
    784 
    785      ENV env (str, acc, fd);
    786      cs_interpreter_t<ENV, OPSET, subr_subset_param_t> interp (env);
    787 
    788      parsed_charstrings[new_glyph].alloc (str.length);
    789      subr_subset_param_t  param (&parsed_charstrings[new_glyph],
    790                                  &parsed_global_subrs_storage,
    791                                  &parsed_local_subrs_storage[fd],
    792                                  &closures.global_closure,
    793                                  &closures.local_closures[fd],
    794                                  plan->flags & HB_SUBSET_FLAGS_NO_HINTING);
    795 
    796      if (unlikely (!interp.interpret (param)))
    797        return false;
    798 
    799      /* complete parsed string esp. copy CFF1 width or CFF2 vsindex to the parsed charstring for encoding */
    800      SUBSETTER::complete_parsed_str (interp.env, param, parsed_charstrings[new_glyph]);
    801 
    802      /* mark hint ops and arguments for drop */
    803      if ((plan->flags & HB_SUBSET_FLAGS_NO_HINTING) || plan->inprogress_accelerator)
    804      {
    805 subr_subset_param_t  param (&parsed_charstrings[new_glyph],
    806 			    &parsed_global_subrs_storage,
    807 			    &parsed_local_subrs_storage[fd],
    808 			    &closures.global_closure,
    809 			    &closures.local_closures[fd],
    810 			    plan->flags & HB_SUBSET_FLAGS_NO_HINTING);
    811 
    812 drop_hints_param_t  drop;
    813 if (drop_hints_in_str (parsed_charstrings[new_glyph], param, drop))
    814 {
    815   parsed_charstrings[new_glyph].set_hint_dropped ();
    816   if (drop.vsindex_dropped)
    817     parsed_charstrings[new_glyph].set_vsindex_dropped ();
    818 }
    819      }
    820 
    821      /* Doing this here one by one instead of compacting all at the end
    822       * has massive peak-memory saving.
    823       *
    824       * The compacting both saves memory and makes further operations
    825       * faster.
    826       */
    827      parsed_charstrings[new_glyph].compact ();
    828    }
    829 
    830    /* Since parsed strings were loaded from accelerator, we still need
    831     * to compute the subroutine closures which would have normally happened during
    832     * parsing.
    833     *
    834     * Or if we are dropping hinting, redo closure to get actually used subrs.
    835     */
    836    if ((cff_accelerator ||
    837 (!cff_accelerator && plan->flags & HB_SUBSET_FLAGS_NO_HINTING)) &&
    838        !closure_subroutines(*parsed_global_subrs,
    839                             *parsed_local_subrs))
    840      return false;
    841 
    842    remaps.create (closures);
    843 
    844    populate_subset_accelerator ();
    845    return true;
    846  }
    847 
    848  bool encode_charstrings (str_buff_vec_t &buffArray, bool encode_prefix = true) const
    849  {
    850    unsigned num_glyphs = plan->num_output_glyphs ();
    851    if (unlikely (!buffArray.resize_exact (num_glyphs)))
    852      return false;
    853    hb_codepoint_t last = 0;
    854    for (auto _ : plan->new_to_old_gid_list)
    855    {
    856      hb_codepoint_t gid = _.first;
    857      hb_codepoint_t old_glyph = _.second;
    858 
    859      if (endchar_op != OpCode_Invalid)
    860        for (; last < gid; last++)
    861 {
    862   // Hack to point vector to static string.
    863   auto &b = buffArray.arrayZ[last];
    864   b.set_storage (const_cast<unsigned char *>(endchar_str), 1);
    865 }
    866 
    867      last++; // Skip over gid
    868      unsigned int  fd = acc.fdSelect->get_fd (old_glyph);
    869      if (unlikely (fd >= acc.fdCount))
    870 return false;
    871      if (unlikely (!encode_str (get_parsed_charstring (gid), fd, buffArray.arrayZ[gid], encode_prefix)))
    872 return false;
    873    }
    874    if (endchar_op != OpCode_Invalid)
    875      for (; last < num_glyphs; last++)
    876      {
    877 // Hack to point vector to static string.
    878 auto &b = buffArray.arrayZ[last];
    879 b.set_storage (const_cast<unsigned char *>(endchar_str), 1);
    880      }
    881 
    882    return true;
    883  }
    884 
    885  bool encode_subrs (const parsed_cs_str_vec_t &subrs, const subr_remap_t& remap, unsigned int fd, str_buff_vec_t &buffArray) const
    886  {
    887    unsigned int  count = remap.get_population ();
    888 
    889    if (unlikely (!buffArray.resize_exact (count)))
    890      return false;
    891    for (unsigned int new_num = 0; new_num < count; new_num++)
    892    {
    893      hb_codepoint_t old_num = remap.backward (new_num);
    894      assert (old_num != CFF_UNDEF_CODE);
    895 
    896      if (unlikely (!encode_str (subrs[old_num], fd, buffArray[new_num])))
    897 return false;
    898    }
    899    return true;
    900  }
    901 
    902  bool encode_globalsubrs (str_buff_vec_t &buffArray)
    903  {
    904    return encode_subrs (*parsed_global_subrs, remaps.global_remap, 0, buffArray);
    905  }
    906 
    907  bool encode_localsubrs (unsigned int fd, str_buff_vec_t &buffArray) const
    908  {
    909    return encode_subrs ((*parsed_local_subrs)[fd], remaps.local_remaps[fd], fd, buffArray);
    910  }
    911 
    912  protected:
    913  struct drop_hints_param_t
    914  {
    915    drop_hints_param_t ()
    916      : seen_moveto (false),
    917 ends_in_hint (false),
    918 all_dropped (false),
    919 vsindex_dropped (false) {}
    920 
    921    bool  seen_moveto;
    922    bool  ends_in_hint;
    923    bool  all_dropped;
    924    bool  vsindex_dropped;
    925  };
    926 
    927  bool drop_hints_in_subr (parsed_cs_str_t &str, unsigned int pos,
    928 		   parsed_cs_str_vec_t &subrs, unsigned int subr_num,
    929 		   const subr_subset_param_t &param, drop_hints_param_t &drop)
    930  {
    931    drop.ends_in_hint = false;
    932    bool has_hint = drop_hints_in_str (subrs[subr_num], param, drop);
    933 
    934    /* if this subr ends with a stem hint (i.e., not a number; potential argument for moveto),
    935     * then this entire subroutine must be a hint. drop its call. */
    936    if (drop.ends_in_hint)
    937    {
    938      str.values[pos].set_hinting ();
    939      /* if this subr call is at the end of the parent subr, propagate the flag
    940       * otherwise reset the flag */
    941      if (!str.at_end (pos))
    942 drop.ends_in_hint = false;
    943    }
    944    else if (drop.all_dropped)
    945    {
    946      str.values[pos].set_hinting ();
    947    }
    948 
    949    return has_hint;
    950  }
    951 
    952  /* returns true if it sees a hint op before the first moveto */
    953  bool drop_hints_in_str (parsed_cs_str_t &str, const subr_subset_param_t &param, drop_hints_param_t &drop)
    954  {
    955    bool  seen_hint = false;
    956 
    957    unsigned count = str.values.length;
    958    auto *values = str.values.arrayZ;
    959    for (unsigned int pos = 0; pos < count; pos++)
    960    {
    961      bool  has_hint = false;
    962      switch (values[pos].op)
    963      {
    964 case OpCode_callsubr:
    965   has_hint = drop_hints_in_subr (str, pos,
    966 				*param.parsed_local_subrs, values[pos].subr_num,
    967 				param, drop);
    968   break;
    969 
    970 case OpCode_callgsubr:
    971   has_hint = drop_hints_in_subr (str, pos,
    972 				*param.parsed_global_subrs, values[pos].subr_num,
    973 				param, drop);
    974   break;
    975 
    976 case OpCode_rmoveto:
    977 case OpCode_hmoveto:
    978 case OpCode_vmoveto:
    979   drop.seen_moveto = true;
    980   break;
    981 
    982 case OpCode_hintmask:
    983 case OpCode_cntrmask:
    984   if (drop.seen_moveto)
    985   {
    986     values[pos].set_hinting ();
    987     break;
    988   }
    989   HB_FALLTHROUGH;
    990 
    991 case OpCode_hstemhm:
    992 case OpCode_vstemhm:
    993 case OpCode_hstem:
    994 case OpCode_vstem:
    995   has_hint = true;
    996   values[pos].set_hinting ();
    997   if (str.at_end (pos))
    998     drop.ends_in_hint = true;
    999   break;
   1000 
   1001 case OpCode_dotsection:
   1002   values[pos].set_hinting ();
   1003   break;
   1004 
   1005 default:
   1006   /* NONE */
   1007   break;
   1008      }
   1009      if (has_hint)
   1010      {
   1011 for (int i = pos - 1; i >= 0; i--)
   1012 {
   1013   parsed_cs_op_t  &csop = values[(unsigned)i];
   1014   if (csop.is_hinting ())
   1015     break;
   1016   csop.set_hinting ();
   1017   if (csop.op == OpCode_vsindexcs)
   1018     drop.vsindex_dropped = true;
   1019 }
   1020 seen_hint |= has_hint;
   1021      }
   1022    }
   1023 
   1024    /* Raise all_dropped flag if all operators except return are dropped from a subr.
   1025     * It may happen even after seeing the first moveto if a subr contains
   1026     * only (usually one) hintmask operator, then calls to this subr can be dropped.
   1027     */
   1028    drop.all_dropped = true;
   1029    for (unsigned int pos = 0; pos < count; pos++)
   1030    {
   1031      parsed_cs_op_t  &csop = values[pos];
   1032      if (csop.op == OpCode_return)
   1033 break;
   1034      if (!csop.is_hinting ())
   1035      {
   1036 drop.all_dropped = false;
   1037 break;
   1038      }
   1039    }
   1040 
   1041    return seen_hint;
   1042  }
   1043 
   1044  bool closure_subroutines (const parsed_cs_str_vec_t& global_subrs,
   1045                            const hb_vector_t<parsed_cs_str_vec_t>& local_subrs)
   1046  {
   1047    closures.reset ();
   1048    for (auto _ : plan->new_to_old_gid_list)
   1049    {
   1050      hb_codepoint_t new_glyph = _.first;
   1051      hb_codepoint_t old_glyph = _.second;
   1052      unsigned int fd = acc.fdSelect->get_fd (old_glyph);
   1053      if (unlikely (fd >= acc.fdCount))
   1054        return false;
   1055 
   1056      // Note: const cast is safe here because the collect_subr_refs_in_str only performs a
   1057      //       closure and does not modify any of the charstrings.
   1058      subr_subset_param_t  param (const_cast<parsed_cs_str_t*> (&get_parsed_charstring (new_glyph)),
   1059                                  const_cast<parsed_cs_str_vec_t*> (&global_subrs),
   1060                                  const_cast<parsed_cs_str_vec_t*> (&local_subrs[fd]),
   1061                                  &closures.global_closure,
   1062                                  &closures.local_closures[fd],
   1063                                  plan->flags & HB_SUBSET_FLAGS_NO_HINTING);
   1064      collect_subr_refs_in_str (get_parsed_charstring (new_glyph), param);
   1065    }
   1066 
   1067    return true;
   1068  }
   1069 
   1070  void collect_subr_refs_in_subr (unsigned int subr_num, parsed_cs_str_vec_t &subrs,
   1071 			  hb_set_t *closure,
   1072 			  const subr_subset_param_t &param)
   1073  {
   1074    if (closure->has (subr_num))
   1075      return;
   1076    closure->add (subr_num);
   1077    collect_subr_refs_in_str (subrs[subr_num], param);
   1078  }
   1079 
   1080  void collect_subr_refs_in_str (const parsed_cs_str_t &str,
   1081                                 const subr_subset_param_t &param)
   1082  {
   1083    if (!str.has_calls ())
   1084      return;
   1085 
   1086    for (auto &opstr : str.values)
   1087    {
   1088      if (!param.drop_hints || !opstr.is_hinting ())
   1089      {
   1090 switch (opstr.op)
   1091 {
   1092   case OpCode_callsubr:
   1093     collect_subr_refs_in_subr (opstr.subr_num, *param.parsed_local_subrs,
   1094 			       param.local_closure, param);
   1095     break;
   1096 
   1097   case OpCode_callgsubr:
   1098     collect_subr_refs_in_subr (opstr.subr_num, *param.parsed_global_subrs,
   1099 			       param.global_closure, param);
   1100     break;
   1101 
   1102   default: break;
   1103 }
   1104      }
   1105    }
   1106  }
   1107 
   1108  bool encode_str (const parsed_cs_str_t &str, const unsigned int fd, str_buff_t &buff, bool encode_prefix = true) const
   1109  {
   1110    str_encoder_t  encoder (buff);
   1111    encoder.reset ();
   1112    bool hinting = !(plan->flags & HB_SUBSET_FLAGS_NO_HINTING);
   1113    /* if a prefix (CFF1 width or CFF2 vsindex) has been removed along with hints,
   1114     * re-insert it at the beginning of charstreing */
   1115    if (encode_prefix && str.has_prefix () && !hinting && str.is_hint_dropped ())
   1116    {
   1117      encoder.encode_num_cs (str.prefix_num ());
   1118      if (str.prefix_op () != OpCode_Invalid)
   1119 encoder.encode_op (str.prefix_op ());
   1120    }
   1121 
   1122    unsigned size = 0;
   1123    for (auto &opstr : str.values)
   1124    {
   1125      size += opstr.length;
   1126      if (opstr.op == OpCode_callsubr || opstr.op == OpCode_callgsubr)
   1127        size += 3;
   1128    }
   1129    if (!buff.alloc_exact (buff.length + size))
   1130      return false;
   1131 
   1132    for (auto &opstr : str.values)
   1133    {
   1134      if (hinting || !opstr.is_hinting ())
   1135      {
   1136 switch (opstr.op)
   1137 {
   1138   case OpCode_callsubr:
   1139     encoder.encode_int (remaps.local_remaps[fd].biased_num (opstr.subr_num));
   1140     encoder.copy_str (opstr.ptr, opstr.length);
   1141     break;
   1142 
   1143   case OpCode_callgsubr:
   1144     encoder.encode_int (remaps.global_remap.biased_num (opstr.subr_num));
   1145     encoder.copy_str (opstr.ptr, opstr.length);
   1146     break;
   1147 
   1148   default:
   1149     encoder.copy_str (opstr.ptr, opstr.length);
   1150     break;
   1151 }
   1152      }
   1153    }
   1154    return !encoder.in_error ();
   1155  }
   1156 
   1157  void compact_parsed_subrs () const
   1158  {
   1159    for (auto &cs : parsed_global_subrs_storage)
   1160      cs.compact ();
   1161    for (auto &vec : parsed_local_subrs_storage)
   1162      for (auto &cs : vec)
   1163 cs.compact ();
   1164  }
   1165 
   1166  void populate_subset_accelerator () const
   1167  {
   1168    if (!plan->inprogress_accelerator) return;
   1169 
   1170    compact_parsed_subrs ();
   1171 
   1172    acc.cff_accelerator =
   1173        cff_subset_accelerator_t::create(acc.blob,
   1174                                         parsed_charstrings,
   1175                                         parsed_global_subrs_storage,
   1176                                         parsed_local_subrs_storage);
   1177  }
   1178 
   1179  const parsed_cs_str_t& get_parsed_charstring (unsigned i) const
   1180  {
   1181    if (cached_charstrings) return *(cached_charstrings[i]);
   1182    return parsed_charstrings[i];
   1183  }
   1184 
   1185  protected:
   1186  const ACC			&acc;
   1187  const hb_subset_plan_t	*plan;
   1188 
   1189  subr_closures_t		closures;
   1190 
   1191  hb_vector_t<const parsed_cs_str_t*>     cached_charstrings;
   1192  const parsed_cs_str_vec_t*              parsed_global_subrs;
   1193  const hb_vector_t<parsed_cs_str_vec_t>* parsed_local_subrs;
   1194 
   1195  subr_remaps_t			remaps;
   1196 
   1197  private:
   1198 
   1199  parsed_cs_str_vec_t		parsed_charstrings;
   1200  parsed_cs_str_vec_t		parsed_global_subrs_storage;
   1201  hb_vector_t<parsed_cs_str_vec_t>  parsed_local_subrs_storage;
   1202  typedef typename SUBRS::count_type subr_count_type;
   1203 };
   1204 
   1205 } /* namespace CFF */
   1206 
   1207 HB_INTERNAL bool
   1208 hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan,
   1209 		    unsigned int fdCount,
   1210 		    const CFF::FDSelect &src, /* IN */
   1211 		    unsigned int &subset_fd_count /* OUT */,
   1212 		    unsigned int &subset_fdselect_size /* OUT */,
   1213 		    unsigned int &subset_fdselect_format /* OUT */,
   1214 		    hb_vector_t<CFF::code_pair_t> &fdselect_ranges /* OUT */,
   1215 		    hb_inc_bimap_t &fdmap /* OUT */);
   1216 
   1217 HB_INTERNAL bool
   1218 hb_serialize_cff_fdselect (hb_serialize_context_t *c,
   1219 		  unsigned int num_glyphs,
   1220 		  const CFF::FDSelect &src,
   1221 		  unsigned int fd_count,
   1222 		  unsigned int fdselect_format,
   1223 		  unsigned int size,
   1224 		  const hb_vector_t<CFF::code_pair_t> &fdselect_ranges);
   1225 
   1226 #endif /* HB_SUBSET_CFF_COMMON_HH */