Coverage Report

Created: 2026-02-26 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/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
0
      // TODO longint
108
0
      encode_int (n.to_int ());
109
0
    }
110
0
    else
111
0
    {
112
0
      // Sigh. BCD
113
0
      // 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
0
117
0
      // Based on:
118
0
      // https://github.com/fonttools/fonttools/blob/0738c41dfbcbc213ab9263f486ef0cccc6eb5ce5/Lib/fontTools/misc/psCharStrings.py#L267-L316
119
0
120
0
      char buf[16];
121
0
      /* FontTools has the following comment:
122
0
       *
123
0
       * # Note: 14 decimal digits seems to be the limitation for CFF real numbers
124
0
       * # in macOS. However, we use 8 here to match the implementation of AFDKO.
125
0
       *
126
0
       * We use 8 here to match FontTools X-).
127
0
       */
128
0
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
0
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
0
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
0
211
0
    case '.':
212
0
      nibbles.push (0x0A); // .
213
0
      continue;
214
0
215
0
    case '-':
216
0
      nibbles.push (0x0E); // -
217
0
      continue;
218
0
  }
219
0
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
0
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
    : fd_array_link (0),
260
      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
  {
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
0
  {
303
0
    TRACE_SERIALIZE (this);
304
0
305
0
    if (opstr.op == OpCode_Private)
306
0
    {
307
0
      /* 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
0
      /* 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
0
      //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
  op_code_t op;
329
330
0
  cs_command_t () : op (OpCode_Invalid) {}
331
0
  cs_command_t (op_code_t op_) : op (op_) {}
332
};
333
334
typedef hb_vector_t<cs_command_t> *cs_command_vec_t;
335
336
struct flatten_param_t
337
{
338
  flatten_param_t (str_buff_t &flatStr_,
339
                   bool drop_hints_,
340
                   const hb_subset_plan_t *plan_,
341
                   cs_command_vec_t commands_ = nullptr)
342
0
    : flatStr (flatStr_), drop_hints (drop_hints_), plan (plan_), commands (commands_) {}
343
344
  str_buff_t     &flatStr;
345
  bool  drop_hints;
346
  const hb_subset_plan_t *plan;
347
  cs_command_vec_t commands; /* Optional: capture parsed commands for specialization */
348
};
349
350
template <typename ACC, typename ENV, typename OPSET, op_code_t endchar_op=OpCode_Invalid>
351
struct subr_flattener_t
352
{
353
  subr_flattener_t (const ACC &acc_,
354
        const hb_subset_plan_t *plan_)
355
       : acc (acc_), plan (plan_) {}
356
357
  bool flatten (str_buff_vec_t &flat_charstrings,
358
                hb_vector_t<hb_vector_t<cs_command_t>> *command_capture = nullptr)
359
  {
360
    unsigned count = plan->num_output_glyphs ();
361
    if (!flat_charstrings.resize_exact (count))
362
      return false;
363
    for (unsigned int i = 0; i < count; i++)
364
    {
365
      hb_codepoint_t  glyph;
366
      if (!plan->old_gid_for_new_gid (i, &glyph))
367
      {
368
  /* add an endchar only charstring for a missing glyph if CFF1 */
369
  if (endchar_op != OpCode_Invalid) flat_charstrings[i].push (endchar_op);
370
  continue;
371
      }
372
      const hb_ubytes_t str = (*acc.charStrings)[glyph];
373
      unsigned int fd = acc.fdSelect->get_fd (glyph);
374
      if (unlikely (fd >= acc.fdCount))
375
  return false;
376
377
378
      ENV env (str, acc, fd,
379
         plan->normalized_coords.arrayZ, plan->normalized_coords.length);
380
      cs_interpreter_t<ENV, OPSET, flatten_param_t> interp (env);
381
      flatten_param_t  param = {
382
        flat_charstrings.arrayZ[i],
383
        (bool) (plan->flags & HB_SUBSET_FLAGS_NO_HINTING),
384
  plan,
385
  command_capture ? &(*command_capture)[i] : nullptr
386
      };
387
      if (unlikely (!interp.interpret (param)))
388
  return false;
389
    }
390
    return true;
391
  }
392
393
  const ACC &acc;
394
  const hb_subset_plan_t *plan;
395
};
396
397
struct subr_closures_t
398
{
399
  subr_closures_t (unsigned int fd_count) : global_closure (), local_closures ()
400
0
  {
401
0
    local_closures.resize_exact (fd_count);
402
0
  }
403
404
  void reset ()
405
0
  {
406
0
    global_closure.clear();
407
0
    for (unsigned int i = 0; i < local_closures.length; i++)
408
0
      local_closures[i].clear();
409
0
  }
410
411
0
  bool in_error () const { return local_closures.in_error (); }
412
  hb_set_t  global_closure;
413
  hb_vector_t<hb_set_t> local_closures;
414
};
415
416
struct parsed_cs_op_t : op_str_t
417
{
418
  parsed_cs_op_t (unsigned int subr_num_ = 0) :
419
0
    subr_num (subr_num_) {}
420
421
0
  bool is_hinting () const { return hinting_flag; }
422
0
  void set_hinting ()       { hinting_flag = true; }
423
424
  /* The layout of this struct is designed to fit within the
425
   * padding of op_str_t! */
426
427
  protected:
428
  bool    hinting_flag = false;
429
430
  public:
431
  uint16_t subr_num;
432
};
433
434
struct parsed_cs_str_t : parsed_values_t<parsed_cs_op_t>
435
{
436
  parsed_cs_str_t () :
437
    parsed (false),
438
    hint_dropped (false),
439
    has_prefix_ (false),
440
    has_calls_ (false)
441
0
  {
442
0
    SUPER::init ();
443
0
  }
444
445
  void add_op (op_code_t op, const byte_str_ref_t& str_ref)
446
0
  {
447
0
    if (!is_parsed ())
448
0
      SUPER::add_op (op, str_ref);
449
0
  }
450
451
  void add_call_op (op_code_t op, const byte_str_ref_t& str_ref, unsigned int subr_num)
452
0
  {
453
0
    if (!is_parsed ())
454
0
    {
455
0
      has_calls_ = true;
456
0
457
0
      /* Pop the subroutine number. */
458
0
      values.pop ();
459
0
460
0
      SUPER::add_op (op, str_ref, {subr_num});
461
0
    }
462
0
  }
463
464
  void set_prefix (const number_t &num, op_code_t op = OpCode_Invalid)
465
0
  {
466
0
    has_prefix_ = true;
467
0
    prefix_op_ = op;
468
0
    prefix_num_ = num;
469
0
  }
470
471
  bool at_end (unsigned int pos) const
472
0
  {
473
0
    return ((pos + 1 >= values.length) /* CFF2 */
474
0
  || (values[pos + 1].op == OpCode_return));
475
0
  }
476
477
0
  bool is_parsed () const { return parsed; }
478
0
  void set_parsed ()      { parsed = true; }
479
480
0
  bool is_hint_dropped () const { return hint_dropped; }
481
0
  void set_hint_dropped ()      { hint_dropped = true; }
482
483
0
  bool is_vsindex_dropped () const { return vsindex_dropped; }
484
0
  void set_vsindex_dropped ()      { vsindex_dropped = true; }
485
486
0
  bool has_prefix () const          { return has_prefix_; }
487
0
  op_code_t prefix_op () const         { return prefix_op_; }
488
0
  const number_t &prefix_num () const { return prefix_num_; }
489
490
0
  bool has_calls () const          { return has_calls_; }
491
492
  void compact ()
493
0
  {
494
0
    unsigned count = values.length;
495
0
    if (!count) return;
496
0
    auto &opstr = values.arrayZ;
497
0
    unsigned j = 0;
498
0
    for (unsigned i = 1; i < count; i++)
499
0
    {
500
0
      /* See if we can combine op j and op i. */
501
0
      bool combine =
502
0
        (opstr[j].op != OpCode_callsubr && opstr[j].op != OpCode_callgsubr) &&
503
0
        (opstr[i].op != OpCode_callsubr && opstr[i].op != OpCode_callgsubr) &&
504
0
        (opstr[j].is_hinting () == opstr[i].is_hinting ()) &&
505
0
        (opstr[j].ptr + opstr[j].length == opstr[i].ptr) &&
506
0
        (opstr[j].length + opstr[i].length <= 255);
507
0
508
0
      if (combine)
509
0
      {
510
0
  opstr[j].length += opstr[i].length;
511
0
  opstr[j].op = OpCode_Invalid;
512
0
      }
513
0
      else
514
0
      {
515
0
  opstr[++j] = opstr[i];
516
0
      }
517
0
    }
518
0
    values.shrink (j + 1);
519
0
  }
520
521
  protected:
522
  bool    parsed : 1;
523
  bool    hint_dropped : 1;
524
  bool    vsindex_dropped : 1;
525
  bool    has_prefix_ : 1;
526
  bool    has_calls_ : 1;
527
  op_code_t prefix_op_;
528
  number_t  prefix_num_;
529
530
  private:
531
  typedef parsed_values_t<parsed_cs_op_t> SUPER;
532
};
533
534
struct parsed_cs_str_vec_t : hb_vector_t<parsed_cs_str_t>
535
{
536
  private:
537
  typedef hb_vector_t<parsed_cs_str_t> SUPER;
538
};
539
540
struct cff_subset_accelerator_t
541
{
542
  static cff_subset_accelerator_t* create (
543
      hb_blob_t* original_blob,
544
      const parsed_cs_str_vec_t& parsed_charstrings,
545
      const parsed_cs_str_vec_t& parsed_global_subrs,
546
0
      const hb_vector_t<parsed_cs_str_vec_t>& parsed_local_subrs) {
547
0
    cff_subset_accelerator_t* accel =
548
0
        (cff_subset_accelerator_t*) hb_malloc (sizeof(cff_subset_accelerator_t));
549
0
    if (unlikely (!accel)) return nullptr;
550
0
    new (accel) cff_subset_accelerator_t (original_blob,
551
0
                                          parsed_charstrings,
552
0
                                          parsed_global_subrs,
553
0
                                          parsed_local_subrs);
554
0
    return accel;
555
0
  }
556
557
0
  static void destroy (void* value) {
558
0
    if (!value) return;
559
0
560
0
    cff_subset_accelerator_t* accel = (cff_subset_accelerator_t*) value;
561
0
    accel->~cff_subset_accelerator_t ();
562
0
    hb_free (accel);
563
0
  }
564
565
  cff_subset_accelerator_t(
566
      hb_blob_t* original_blob_,
567
      const parsed_cs_str_vec_t& parsed_charstrings_,
568
      const parsed_cs_str_vec_t& parsed_global_subrs_,
569
      const hb_vector_t<parsed_cs_str_vec_t>& parsed_local_subrs_)
570
0
  {
571
0
    parsed_charstrings = parsed_charstrings_;
572
0
    parsed_global_subrs = parsed_global_subrs_;
573
0
    parsed_local_subrs = parsed_local_subrs_;
574
0
575
0
    // the parsed charstrings point to memory in the original CFF table so we must hold a reference
576
0
    // to it to keep the memory valid.
577
0
    original_blob = hb_blob_reference (original_blob_);
578
0
  }
579
580
  ~cff_subset_accelerator_t()
581
0
  {
582
0
    hb_blob_destroy (original_blob);
583
0
    auto *mapping = glyph_to_sid_map.get_relaxed ();
584
0
    if (mapping)
585
0
    {
586
0
      mapping->~glyph_to_sid_map_t ();
587
0
      hb_free (mapping);
588
0
    }
589
0
  }
590
591
  parsed_cs_str_vec_t parsed_charstrings;
592
  parsed_cs_str_vec_t parsed_global_subrs;
593
  hb_vector_t<parsed_cs_str_vec_t> parsed_local_subrs;
594
  mutable hb_atomic_t<glyph_to_sid_map_t *> glyph_to_sid_map;
595
596
 private:
597
  hb_blob_t* original_blob;
598
};
599
600
struct subr_subset_param_t
601
{
602
  subr_subset_param_t (parsed_cs_str_t *parsed_charstring_,
603
           parsed_cs_str_vec_t *parsed_global_subrs_,
604
           parsed_cs_str_vec_t *parsed_local_subrs_,
605
           hb_set_t *global_closure_,
606
           hb_set_t *local_closure_,
607
           bool drop_hints_) :
608
      current_parsed_str (parsed_charstring_),
609
      parsed_charstring (parsed_charstring_),
610
      parsed_global_subrs (parsed_global_subrs_),
611
      parsed_local_subrs (parsed_local_subrs_),
612
      global_closure (global_closure_),
613
      local_closure (local_closure_),
614
0
      drop_hints (drop_hints_) {}
615
616
  parsed_cs_str_t *get_parsed_str_for_context (call_context_t &context)
617
0
  {
618
0
    switch (context.type)
619
0
    {
620
0
      case CSType_CharString:
621
0
  return parsed_charstring;
622
0
623
0
      case CSType_LocalSubr:
624
0
  if (likely (context.subr_num < parsed_local_subrs->length))
625
0
    return &(*parsed_local_subrs)[context.subr_num];
626
0
  break;
627
0
628
0
      case CSType_GlobalSubr:
629
0
  if (likely (context.subr_num < parsed_global_subrs->length))
630
0
    return &(*parsed_global_subrs)[context.subr_num];
631
0
  break;
632
0
    }
633
0
    return nullptr;
634
0
  }
635
636
  template <typename ENV>
637
  void set_current_str (ENV &env, bool calling)
638
  {
639
    parsed_cs_str_t *parsed_str = get_parsed_str_for_context (env.context);
640
    if (unlikely (!parsed_str))
641
    {
642
      env.set_error ();
643
      return;
644
    }
645
    /* If the called subroutine is parsed partially but not completely yet,
646
     * it must be because we are calling it recursively.
647
     * Handle it as an error. */
648
    if (unlikely (calling && !parsed_str->is_parsed () && (parsed_str->values.length > 0)))
649
      env.set_error ();
650
    else
651
    {
652
      if (!parsed_str->is_parsed ())
653
        parsed_str->alloc (env.str_ref.total_size ());
654
      current_parsed_str = parsed_str;
655
    }
656
  }
657
658
  parsed_cs_str_t *current_parsed_str;
659
660
  parsed_cs_str_t *parsed_charstring;
661
  parsed_cs_str_vec_t *parsed_global_subrs;
662
  parsed_cs_str_vec_t *parsed_local_subrs;
663
  hb_set_t      *global_closure;
664
  hb_set_t      *local_closure;
665
  bool    drop_hints;
666
};
667
668
struct subr_remap_t : hb_inc_bimap_t
669
{
670
  void create (const hb_set_t *closure)
671
0
  {
672
0
    /* create a remapping of subroutine numbers from old to new.
673
0
     * no optimization based on usage counts. fonttools doesn't appear doing that either.
674
0
     */
675
0
676
0
    alloc (closure->get_population ());
677
0
    for (auto old_num : *closure)
678
0
      add (old_num);
679
0
680
0
    if (get_population () < 1240)
681
0
      bias = 107;
682
0
    else if (get_population () < 33900)
683
0
      bias = 1131;
684
0
    else
685
0
      bias = 32768;
686
0
  }
687
688
  int biased_num (unsigned int old_num) const
689
0
  {
690
0
    hb_codepoint_t new_num = get (old_num);
691
0
    return (int)new_num - bias;
692
0
  }
693
694
  protected:
695
  int bias;
696
};
697
698
struct subr_remaps_t
699
{
700
  subr_remaps_t (unsigned int fdCount)
701
0
  {
702
0
    local_remaps.resize (fdCount);
703
0
  }
704
705
  bool in_error()
706
0
  {
707
0
    return local_remaps.in_error ();
708
0
  }
709
710
  void create (subr_closures_t& closures)
711
0
  {
712
0
    global_remap.create (&closures.global_closure);
713
0
    for (unsigned int i = 0; i < local_remaps.length; i++)
714
0
      local_remaps.arrayZ[i].create (&closures.local_closures[i]);
715
0
  }
716
717
  subr_remap_t         global_remap;
718
  hb_vector_t<subr_remap_t>  local_remaps;
719
};
720
721
template <typename SUBSETTER, typename SUBRS, typename ACC, typename ENV, typename OPSET, op_code_t endchar_op=OpCode_Invalid>
722
struct subr_subsetter_t
723
{
724
  subr_subsetter_t (ACC &acc_, const hb_subset_plan_t *plan_)
725
      : acc (acc_), plan (plan_), closures(acc_.fdCount),
726
        remaps(acc_.fdCount)
727
  {}
728
729
  /* Subroutine subsetting with --no-desubroutinize runs in phases:
730
   *
731
   * 1. execute charstrings/subroutines to determine subroutine closures
732
   * 2. parse out all operators and numbers
733
   * 3. mark hint operators and operands for removal if --no-hinting
734
   * 4. re-encode all charstrings and subroutines with new subroutine numbers
735
   *
736
   * Phases #1 and #2 are done at the same time in collect_subrs ().
737
   * Phase #3 walks charstrings/subroutines forward then backward (hence parsing required),
738
   * because we can't tell if a number belongs to a hint op until we see the first moveto.
739
   *
740
   * Assumption: a callsubr/callgsubr operator must immediately follow a (biased) subroutine number
741
   * within the same charstring/subroutine, e.g., not split across a charstring and a subroutine.
742
   */
743
  bool subset (void)
744
  {
745
    unsigned fd_count = acc.fdCount;
746
    const cff_subset_accelerator_t* cff_accelerator = nullptr;
747
    if (acc.cff_accelerator) {
748
      cff_accelerator = acc.cff_accelerator;
749
      fd_count = cff_accelerator->parsed_local_subrs.length;
750
    }
751
752
    if (cff_accelerator) {
753
      // If we are not dropping hinting then charstrings are not modified so we can
754
      // just use a reference to the cached copies.
755
      cached_charstrings.resize_exact (plan->num_output_glyphs ());
756
      parsed_global_subrs = &cff_accelerator->parsed_global_subrs;
757
      parsed_local_subrs = &cff_accelerator->parsed_local_subrs;
758
    } else {
759
      parsed_charstrings.resize_exact (plan->num_output_glyphs ());
760
      parsed_global_subrs_storage.resize_exact (acc.globalSubrs->count);
761
762
      if (unlikely (!parsed_local_subrs_storage.resize (fd_count))) return false;
763
764
      for (unsigned int i = 0; i < acc.fdCount; i++)
765
      {
766
        unsigned count = acc.privateDicts[i].localSubrs->count;
767
        parsed_local_subrs_storage[i].resize (count);
768
        if (unlikely (parsed_local_subrs_storage[i].in_error ())) return false;
769
      }
770
771
      parsed_global_subrs = &parsed_global_subrs_storage;
772
      parsed_local_subrs = &parsed_local_subrs_storage;
773
    }
774
775
    if (unlikely (remaps.in_error()
776
                  || cached_charstrings.in_error ()
777
                  || parsed_charstrings.in_error ()
778
                  || parsed_global_subrs->in_error ()
779
                  || closures.in_error ())) {
780
      return false;
781
    }
782
783
    /* phase 1 & 2 */
784
    for (auto _ : plan->new_to_old_gid_list)
785
    {
786
      hb_codepoint_t new_glyph = _.first;
787
      hb_codepoint_t old_glyph = _.second;
788
789
      const hb_ubytes_t str = (*acc.charStrings)[old_glyph];
790
      unsigned int fd = acc.fdSelect->get_fd (old_glyph);
791
      if (unlikely (fd >= acc.fdCount))
792
        return false;
793
794
      if (cff_accelerator)
795
      {
796
        // parsed string already exists in accelerator, copy it and move
797
        // on.
798
        if (cached_charstrings)
799
          cached_charstrings[new_glyph] = &cff_accelerator->parsed_charstrings[old_glyph];
800
        else
801
          parsed_charstrings[new_glyph] = cff_accelerator->parsed_charstrings[old_glyph];
802
803
        continue;
804
      }
805
806
      ENV env (str, acc, fd);
807
      cs_interpreter_t<ENV, OPSET, subr_subset_param_t> interp (env);
808
809
      parsed_charstrings[new_glyph].alloc (str.length);
810
      subr_subset_param_t  param (&parsed_charstrings[new_glyph],
811
                                  &parsed_global_subrs_storage,
812
                                  &parsed_local_subrs_storage[fd],
813
                                  &closures.global_closure,
814
                                  &closures.local_closures[fd],
815
                                  plan->flags & HB_SUBSET_FLAGS_NO_HINTING);
816
817
      if (unlikely (!interp.interpret (param)))
818
        return false;
819
820
      /* complete parsed string esp. copy CFF1 width or CFF2 vsindex to the parsed charstring for encoding */
821
      SUBSETTER::complete_parsed_str (interp.env, param, parsed_charstrings[new_glyph]);
822
823
      /* mark hint ops and arguments for drop */
824
      if ((plan->flags & HB_SUBSET_FLAGS_NO_HINTING) || plan->inprogress_accelerator)
825
      {
826
  subr_subset_param_t  param (&parsed_charstrings[new_glyph],
827
            &parsed_global_subrs_storage,
828
            &parsed_local_subrs_storage[fd],
829
            &closures.global_closure,
830
            &closures.local_closures[fd],
831
            plan->flags & HB_SUBSET_FLAGS_NO_HINTING);
832
833
  drop_hints_param_t  drop;
834
  if (drop_hints_in_str (parsed_charstrings[new_glyph], param, drop))
835
  {
836
    parsed_charstrings[new_glyph].set_hint_dropped ();
837
    if (drop.vsindex_dropped)
838
      parsed_charstrings[new_glyph].set_vsindex_dropped ();
839
  }
840
      }
841
842
      /* Doing this here one by one instead of compacting all at the end
843
       * has massive peak-memory saving.
844
       *
845
       * The compacting both saves memory and makes further operations
846
       * faster.
847
       */
848
      parsed_charstrings[new_glyph].compact ();
849
    }
850
851
    /* Since parsed strings were loaded from accelerator, we still need
852
     * to compute the subroutine closures which would have normally happened during
853
     * parsing.
854
     *
855
     * Or if we are dropping hinting, redo closure to get actually used subrs.
856
     */
857
    if ((cff_accelerator ||
858
  (!cff_accelerator && plan->flags & HB_SUBSET_FLAGS_NO_HINTING)) &&
859
        !closure_subroutines(*parsed_global_subrs,
860
                             *parsed_local_subrs))
861
      return false;
862
863
    remaps.create (closures);
864
865
    populate_subset_accelerator ();
866
    return true;
867
  }
868
869
  bool encode_charstrings (str_buff_vec_t &buffArray, bool encode_prefix = true) const
870
  {
871
    unsigned num_glyphs = plan->num_output_glyphs ();
872
    if (unlikely (!buffArray.resize_exact (num_glyphs)))
873
      return false;
874
    hb_codepoint_t last = 0;
875
    for (auto _ : plan->new_to_old_gid_list)
876
    {
877
      hb_codepoint_t gid = _.first;
878
      hb_codepoint_t old_glyph = _.second;
879
880
      if (endchar_op != OpCode_Invalid)
881
        for (; last < gid; last++)
882
  {
883
    // Hack to point vector to static string.
884
    auto &b = buffArray.arrayZ[last];
885
    b.set_storage (const_cast<unsigned char *>(endchar_str), 1);
886
  }
887
888
      last++; // Skip over gid
889
      unsigned int  fd = acc.fdSelect->get_fd (old_glyph);
890
      if (unlikely (fd >= acc.fdCount))
891
  return false;
892
      if (unlikely (!encode_str (get_parsed_charstring (gid), fd, buffArray.arrayZ[gid], encode_prefix)))
893
  return false;
894
    }
895
    if (endchar_op != OpCode_Invalid)
896
      for (; last < num_glyphs; last++)
897
      {
898
  // Hack to point vector to static string.
899
  auto &b = buffArray.arrayZ[last];
900
  b.set_storage (const_cast<unsigned char *>(endchar_str), 1);
901
      }
902
903
    return true;
904
  }
905
906
  bool encode_subrs (const parsed_cs_str_vec_t &subrs, const subr_remap_t& remap, unsigned int fd, str_buff_vec_t &buffArray) const
907
  {
908
    unsigned int  count = remap.get_population ();
909
910
    if (unlikely (!buffArray.resize_exact (count)))
911
      return false;
912
    for (unsigned int new_num = 0; new_num < count; new_num++)
913
    {
914
      hb_codepoint_t old_num = remap.backward (new_num);
915
      assert (old_num != CFF_UNDEF_CODE);
916
917
      if (unlikely (!encode_str (subrs[old_num], fd, buffArray[new_num])))
918
  return false;
919
    }
920
    return true;
921
  }
922
923
  bool encode_globalsubrs (str_buff_vec_t &buffArray)
924
  {
925
    return encode_subrs (*parsed_global_subrs, remaps.global_remap, 0, buffArray);
926
  }
927
928
  bool encode_localsubrs (unsigned int fd, str_buff_vec_t &buffArray) const
929
  {
930
    return encode_subrs ((*parsed_local_subrs)[fd], remaps.local_remaps[fd], fd, buffArray);
931
  }
932
933
  protected:
934
  struct drop_hints_param_t
935
  {
936
    drop_hints_param_t ()
937
      : seen_moveto (false),
938
  ends_in_hint (false),
939
  all_dropped (false),
940
  vsindex_dropped (false) {}
941
942
    bool  seen_moveto;
943
    bool  ends_in_hint;
944
    bool  all_dropped;
945
    bool  vsindex_dropped;
946
  };
947
948
  bool drop_hints_in_subr (parsed_cs_str_t &str, unsigned int pos,
949
         parsed_cs_str_vec_t &subrs, unsigned int subr_num,
950
         const subr_subset_param_t &param, drop_hints_param_t &drop)
951
  {
952
    drop.ends_in_hint = false;
953
    bool has_hint = drop_hints_in_str (subrs[subr_num], param, drop);
954
955
    /* if this subr ends with a stem hint (i.e., not a number; potential argument for moveto),
956
     * then this entire subroutine must be a hint. drop its call. */
957
    if (drop.ends_in_hint)
958
    {
959
      str.values[pos].set_hinting ();
960
      /* if this subr call is at the end of the parent subr, propagate the flag
961
       * otherwise reset the flag */
962
      if (!str.at_end (pos))
963
  drop.ends_in_hint = false;
964
    }
965
    else if (drop.all_dropped)
966
    {
967
      str.values[pos].set_hinting ();
968
    }
969
970
    return has_hint;
971
  }
972
973
  /* returns true if it sees a hint op before the first moveto */
974
  bool drop_hints_in_str (parsed_cs_str_t &str, const subr_subset_param_t &param, drop_hints_param_t &drop)
975
  {
976
    bool  seen_hint = false;
977
978
    unsigned count = str.values.length;
979
    auto *values = str.values.arrayZ;
980
    for (unsigned int pos = 0; pos < count; pos++)
981
    {
982
      bool  has_hint = false;
983
      switch (values[pos].op)
984
      {
985
  case OpCode_callsubr:
986
    has_hint = drop_hints_in_subr (str, pos,
987
          *param.parsed_local_subrs, values[pos].subr_num,
988
          param, drop);
989
    break;
990
991
  case OpCode_callgsubr:
992
    has_hint = drop_hints_in_subr (str, pos,
993
          *param.parsed_global_subrs, values[pos].subr_num,
994
          param, drop);
995
    break;
996
997
  case OpCode_rmoveto:
998
  case OpCode_hmoveto:
999
  case OpCode_vmoveto:
1000
    drop.seen_moveto = true;
1001
    break;
1002
1003
  case OpCode_hintmask:
1004
  case OpCode_cntrmask:
1005
    if (drop.seen_moveto)
1006
    {
1007
      values[pos].set_hinting ();
1008
      break;
1009
    }
1010
    HB_FALLTHROUGH;
1011
1012
  case OpCode_hstemhm:
1013
  case OpCode_vstemhm:
1014
  case OpCode_hstem:
1015
  case OpCode_vstem:
1016
    has_hint = true;
1017
    values[pos].set_hinting ();
1018
    if (str.at_end (pos))
1019
      drop.ends_in_hint = true;
1020
    break;
1021
1022
  case OpCode_dotsection:
1023
    values[pos].set_hinting ();
1024
    break;
1025
1026
  default:
1027
    /* NONE */
1028
    break;
1029
      }
1030
      if (has_hint)
1031
      {
1032
  for (int i = pos - 1; i >= 0; i--)
1033
  {
1034
    parsed_cs_op_t  &csop = values[(unsigned)i];
1035
    if (csop.is_hinting ())
1036
      break;
1037
    csop.set_hinting ();
1038
    if (csop.op == OpCode_vsindexcs)
1039
      drop.vsindex_dropped = true;
1040
  }
1041
  seen_hint |= has_hint;
1042
      }
1043
    }
1044
1045
    /* Raise all_dropped flag if all operators except return are dropped from a subr.
1046
     * It may happen even after seeing the first moveto if a subr contains
1047
     * only (usually one) hintmask operator, then calls to this subr can be dropped.
1048
     */
1049
    drop.all_dropped = true;
1050
    for (unsigned int pos = 0; pos < count; pos++)
1051
    {
1052
      parsed_cs_op_t  &csop = values[pos];
1053
      if (csop.op == OpCode_return)
1054
  break;
1055
      if (!csop.is_hinting ())
1056
      {
1057
  drop.all_dropped = false;
1058
  break;
1059
      }
1060
    }
1061
1062
    return seen_hint;
1063
  }
1064
1065
  bool closure_subroutines (const parsed_cs_str_vec_t& global_subrs,
1066
                            const hb_vector_t<parsed_cs_str_vec_t>& local_subrs)
1067
  {
1068
    closures.reset ();
1069
    for (auto _ : plan->new_to_old_gid_list)
1070
    {
1071
      hb_codepoint_t new_glyph = _.first;
1072
      hb_codepoint_t old_glyph = _.second;
1073
      unsigned int fd = acc.fdSelect->get_fd (old_glyph);
1074
      if (unlikely (fd >= acc.fdCount))
1075
        return false;
1076
1077
      // Note: const cast is safe here because the collect_subr_refs_in_str only performs a
1078
      //       closure and does not modify any of the charstrings.
1079
      subr_subset_param_t  param (const_cast<parsed_cs_str_t*> (&get_parsed_charstring (new_glyph)),
1080
                                  const_cast<parsed_cs_str_vec_t*> (&global_subrs),
1081
                                  const_cast<parsed_cs_str_vec_t*> (&local_subrs[fd]),
1082
                                  &closures.global_closure,
1083
                                  &closures.local_closures[fd],
1084
                                  plan->flags & HB_SUBSET_FLAGS_NO_HINTING);
1085
      collect_subr_refs_in_str (get_parsed_charstring (new_glyph), param);
1086
    }
1087
1088
    return true;
1089
  }
1090
1091
  void collect_subr_refs_in_subr (unsigned int subr_num, parsed_cs_str_vec_t &subrs,
1092
          hb_set_t *closure,
1093
          const subr_subset_param_t &param)
1094
  {
1095
    if (closure->has (subr_num))
1096
      return;
1097
    closure->add (subr_num);
1098
    collect_subr_refs_in_str (subrs[subr_num], param);
1099
  }
1100
1101
  void collect_subr_refs_in_str (const parsed_cs_str_t &str,
1102
                                 const subr_subset_param_t &param)
1103
  {
1104
    if (!str.has_calls ())
1105
      return;
1106
1107
    for (auto &opstr : str.values)
1108
    {
1109
      if (!param.drop_hints || !opstr.is_hinting ())
1110
      {
1111
  switch (opstr.op)
1112
  {
1113
    case OpCode_callsubr:
1114
      collect_subr_refs_in_subr (opstr.subr_num, *param.parsed_local_subrs,
1115
               param.local_closure, param);
1116
      break;
1117
1118
    case OpCode_callgsubr:
1119
      collect_subr_refs_in_subr (opstr.subr_num, *param.parsed_global_subrs,
1120
               param.global_closure, param);
1121
      break;
1122
1123
    default: break;
1124
  }
1125
      }
1126
    }
1127
  }
1128
1129
  bool encode_str (const parsed_cs_str_t &str, const unsigned int fd, str_buff_t &buff, bool encode_prefix = true) const
1130
  {
1131
    str_encoder_t  encoder (buff);
1132
    encoder.reset ();
1133
    bool hinting = !(plan->flags & HB_SUBSET_FLAGS_NO_HINTING);
1134
    /* if a prefix (CFF1 width or CFF2 vsindex) has been removed along with hints,
1135
     * re-insert it at the beginning of charstreing */
1136
    if (encode_prefix && str.has_prefix () && !hinting && str.is_hint_dropped ())
1137
    {
1138
      encoder.encode_num_cs (str.prefix_num ());
1139
      if (str.prefix_op () != OpCode_Invalid)
1140
  encoder.encode_op (str.prefix_op ());
1141
    }
1142
1143
    unsigned size = 0;
1144
    for (auto &opstr : str.values)
1145
    {
1146
      size += opstr.length;
1147
      if (opstr.op == OpCode_callsubr || opstr.op == OpCode_callgsubr)
1148
        size += 3;
1149
    }
1150
    if (!buff.alloc_exact (buff.length + size))
1151
      return false;
1152
1153
    for (auto &opstr : str.values)
1154
    {
1155
      if (hinting || !opstr.is_hinting ())
1156
      {
1157
  switch (opstr.op)
1158
  {
1159
    case OpCode_callsubr:
1160
      encoder.encode_int (remaps.local_remaps[fd].biased_num (opstr.subr_num));
1161
      encoder.copy_str (opstr.ptr, opstr.length);
1162
      break;
1163
1164
    case OpCode_callgsubr:
1165
      encoder.encode_int (remaps.global_remap.biased_num (opstr.subr_num));
1166
      encoder.copy_str (opstr.ptr, opstr.length);
1167
      break;
1168
1169
    default:
1170
      encoder.copy_str (opstr.ptr, opstr.length);
1171
      break;
1172
  }
1173
      }
1174
    }
1175
    return !encoder.in_error ();
1176
  }
1177
1178
  void compact_parsed_subrs () const
1179
  {
1180
    for (auto &cs : parsed_global_subrs_storage)
1181
      cs.compact ();
1182
    for (auto &vec : parsed_local_subrs_storage)
1183
      for (auto &cs : vec)
1184
  cs.compact ();
1185
  }
1186
1187
  void populate_subset_accelerator () const
1188
  {
1189
    if (!plan->inprogress_accelerator) return;
1190
1191
    compact_parsed_subrs ();
1192
1193
    acc.cff_accelerator =
1194
        cff_subset_accelerator_t::create(acc.blob,
1195
                                         parsed_charstrings,
1196
                                         parsed_global_subrs_storage,
1197
                                         parsed_local_subrs_storage);
1198
  }
1199
1200
  const parsed_cs_str_t& get_parsed_charstring (unsigned i) const
1201
  {
1202
    if (cached_charstrings) return *(cached_charstrings[i]);
1203
    return parsed_charstrings[i];
1204
  }
1205
1206
  protected:
1207
  const ACC     &acc;
1208
  const hb_subset_plan_t  *plan;
1209
1210
  subr_closures_t   closures;
1211
1212
  hb_vector_t<const parsed_cs_str_t*>     cached_charstrings;
1213
  const parsed_cs_str_vec_t*              parsed_global_subrs;
1214
  const hb_vector_t<parsed_cs_str_vec_t>* parsed_local_subrs;
1215
1216
  subr_remaps_t     remaps;
1217
1218
  private:
1219
1220
  parsed_cs_str_vec_t   parsed_charstrings;
1221
  parsed_cs_str_vec_t   parsed_global_subrs_storage;
1222
  hb_vector_t<parsed_cs_str_vec_t>  parsed_local_subrs_storage;
1223
  typedef typename SUBRS::count_type subr_count_type;
1224
};
1225
1226
} /* namespace CFF */
1227
1228
HB_INTERNAL bool
1229
hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan,
1230
          unsigned int fdCount,
1231
          const CFF::FDSelect &src, /* IN */
1232
          unsigned int &subset_fd_count /* OUT */,
1233
          unsigned int &subset_fdselect_size /* OUT */,
1234
          unsigned int &subset_fdselect_format /* OUT */,
1235
          hb_vector_t<CFF::code_pair_t> &fdselect_ranges /* OUT */,
1236
          hb_inc_bimap_t &fdmap /* OUT */);
1237
1238
HB_INTERNAL bool
1239
hb_serialize_cff_fdselect (hb_serialize_context_t *c,
1240
        unsigned int num_glyphs,
1241
        const CFF::FDSelect &src,
1242
        unsigned int fd_count,
1243
        unsigned int fdselect_format,
1244
        unsigned int size,
1245
        const hb_vector_t<CFF::code_pair_t> &fdselect_ranges);
1246
1247
#endif /* HB_SUBSET_CFF_COMMON_HH */