Coverage Report

Created: 2026-03-31 11:00

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