Coverage Report

Created: 2021-08-22 09:07

/src/skia/third_party/externals/harfbuzz/src/hb-subset-cff1.cc
Line
Count
Source (jump to first uncovered line)
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
#include "hb.hh"
28
29
#ifndef HB_NO_SUBSET_CFF
30
31
#include "hb-open-type.hh"
32
#include "hb-ot-cff1-table.hh"
33
#include "hb-set.h"
34
#include "hb-bimap.hh"
35
#include "hb-subset-cff1.hh"
36
#include "hb-subset-plan.hh"
37
#include "hb-subset-cff-common.hh"
38
#include "hb-cff1-interp-cs.hh"
39
40
using namespace CFF;
41
42
struct remap_sid_t : hb_inc_bimap_t
43
{
44
  unsigned int add (unsigned int sid)
45
0
  {
46
0
    if ((sid != CFF_UNDEF_SID) && !is_std_std (sid))
47
0
      return offset_sid (hb_inc_bimap_t::add (unoffset_sid (sid)));
48
0
    else
49
0
      return sid;
50
0
  }
51
52
  unsigned int operator[] (unsigned int sid) const
53
0
  {
54
0
    if (is_std_std (sid) || (sid == CFF_UNDEF_SID))
55
0
      return sid;
56
0
    else
57
0
      return offset_sid (get (unoffset_sid (sid)));
58
0
  }
59
60
  static const unsigned int num_std_strings = 391;
61
62
0
  static bool is_std_std (unsigned int sid) { return sid < num_std_strings; }
63
0
  static unsigned int offset_sid (unsigned int sid) { return sid + num_std_strings; }
64
0
  static unsigned int unoffset_sid (unsigned int sid) { return sid - num_std_strings; }
65
};
66
67
struct cff1_sub_table_info_t : cff_sub_table_info_t
68
{
69
  cff1_sub_table_info_t ()
70
    : cff_sub_table_info_t (),
71
      encoding_link (0),
72
      charset_link (0)
73
0
   {
74
0
    privateDictInfo.init ();
75
0
  }
76
77
  objidx_t  encoding_link;
78
  objidx_t  charset_link;
79
  table_info_t  privateDictInfo;
80
};
81
82
/* a copy of a parsed out cff1_top_dict_values_t augmented with additional operators */
83
struct cff1_top_dict_values_mod_t : cff1_top_dict_values_t
84
{
85
  void init (const cff1_top_dict_values_t *base_= &Null (cff1_top_dict_values_t))
86
0
  {
87
0
    SUPER::init ();
88
0
    base = base_;
89
0
  }
90
91
0
  void fini () { SUPER::fini (); }
92
93
0
  unsigned get_count () const { return base->get_count () + SUPER::get_count (); }
94
  const cff1_top_dict_val_t &get_value (unsigned int i) const
95
0
  {
96
0
    if (i < base->get_count ())
97
0
      return (*base)[i];
98
0
    else
99
0
      return SUPER::values[i - base->get_count ()];
100
0
  }
101
0
  const cff1_top_dict_val_t &operator [] (unsigned int i) const { return get_value (i); }
102
103
  void reassignSIDs (const remap_sid_t& sidmap)
104
0
  {
105
0
    for (unsigned int i = 0; i < name_dict_values_t::ValCount; i++)
106
0
      nameSIDs[i] = sidmap[base->nameSIDs[i]];
107
0
  }
108
109
  protected:
110
  typedef cff1_top_dict_values_t SUPER;
111
  const cff1_top_dict_values_t *base;
112
};
113
114
struct top_dict_modifiers_t
115
{
116
  top_dict_modifiers_t (const cff1_sub_table_info_t &info_,
117
      const unsigned int (&nameSIDs_)[name_dict_values_t::ValCount])
118
    : info (info_),
119
      nameSIDs (nameSIDs_)
120
0
  {}
121
122
  const cff1_sub_table_info_t &info;
123
  const unsigned int  (&nameSIDs)[name_dict_values_t::ValCount];
124
};
125
126
struct cff1_top_dict_op_serializer_t : cff_top_dict_op_serializer_t<cff1_top_dict_val_t>
127
{
128
  bool serialize (hb_serialize_context_t *c,
129
      const cff1_top_dict_val_t &opstr,
130
      const top_dict_modifiers_t &mod) const
131
0
  {
132
0
    TRACE_SERIALIZE (this);
133
134
0
    op_code_t op = opstr.op;
135
0
    switch (op)
136
0
    {
137
0
      case OpCode_charset:
138
0
  if (mod.info.charset_link)
139
0
    return_trace (FontDict::serialize_link4_op(c, op, mod.info.charset_link, whence_t::Absolute));
140
0
  else
141
0
    goto fall_back;
142
143
0
      case OpCode_Encoding:
144
0
  if (mod.info.encoding_link)
145
0
    return_trace (FontDict::serialize_link4_op(c, op, mod.info.encoding_link, whence_t::Absolute));
146
0
  else
147
0
    goto fall_back;
148
149
0
      case OpCode_Private:
150
0
  return_trace (UnsizedByteStr::serialize_int2 (c, mod.info.privateDictInfo.size) &&
151
0
          Dict::serialize_link4_op (c, op, mod.info.privateDictInfo.link, whence_t::Absolute));
152
153
0
      case OpCode_version:
154
0
      case OpCode_Notice:
155
0
      case OpCode_Copyright:
156
0
      case OpCode_FullName:
157
0
      case OpCode_FamilyName:
158
0
      case OpCode_Weight:
159
0
      case OpCode_PostScript:
160
0
      case OpCode_BaseFontName:
161
0
      case OpCode_FontName:
162
0
  return_trace (FontDict::serialize_int2_op (c, op, mod.nameSIDs[name_dict_values_t::name_op_to_index (op)]));
163
164
0
      case OpCode_ROS:
165
0
  {
166
    /* for registry & ordering, reassigned SIDs are serialized
167
     * for supplement, the original byte string is copied along with the op code */
168
0
    op_str_t supp_op;
169
0
    supp_op.op = op;
170
0
    if ( unlikely (!(opstr.str.length >= opstr.last_arg_offset + 3)))
171
0
      return_trace (false);
172
0
    supp_op.str = byte_str_t (&opstr.str + opstr.last_arg_offset, opstr.str.length - opstr.last_arg_offset);
173
0
    return_trace (UnsizedByteStr::serialize_int2 (c, mod.nameSIDs[name_dict_values_t::registry]) &&
174
0
      UnsizedByteStr::serialize_int2 (c, mod.nameSIDs[name_dict_values_t::ordering]) &&
175
0
      copy_opstr (c, supp_op));
176
0
  }
177
0
      fall_back:
178
0
      default:
179
0
  return_trace (cff_top_dict_op_serializer_t<cff1_top_dict_val_t>::serialize (c, opstr, mod.info));
180
0
    }
181
0
    return_trace (true);
182
0
  }
183
184
};
185
186
struct cff1_font_dict_op_serializer_t : cff_font_dict_op_serializer_t
187
{
188
  bool serialize (hb_serialize_context_t *c,
189
      const op_str_t &opstr,
190
      const cff1_font_dict_values_mod_t &mod) const
191
0
  {
192
0
    TRACE_SERIALIZE (this);
193
194
0
    if (opstr.op == OpCode_FontName)
195
0
      return_trace (FontDict::serialize_int2_op (c, opstr.op, mod.fontName));
196
0
    else
197
0
      return_trace (SUPER::serialize (c, opstr, mod.privateDictInfo));
198
0
  }
199
200
  private:
201
  typedef cff_font_dict_op_serializer_t SUPER;
202
};
203
204
struct cff1_cs_opset_flatten_t : cff1_cs_opset_t<cff1_cs_opset_flatten_t, flatten_param_t>
205
{
206
  static void flush_args_and_op (op_code_t op, cff1_cs_interp_env_t &env, flatten_param_t& param)
207
0
  {
208
0
    if (env.arg_start > 0)
209
0
      flush_width (env, param);
210
211
0
    switch (op)
212
0
    {
213
0
      case OpCode_hstem:
214
0
      case OpCode_hstemhm:
215
0
      case OpCode_vstem:
216
0
      case OpCode_vstemhm:
217
0
      case OpCode_hintmask:
218
0
      case OpCode_cntrmask:
219
0
      case OpCode_dotsection:
220
0
  if (param.drop_hints)
221
0
  {
222
0
    env.clear_args ();
223
0
    return;
224
0
  }
225
0
  HB_FALLTHROUGH;
226
227
0
      default:
228
0
  SUPER::flush_args_and_op (op, env, param);
229
0
  break;
230
0
    }
231
0
  }
232
  static void flush_args (cff1_cs_interp_env_t &env, flatten_param_t& param)
233
0
  {
234
0
    str_encoder_t  encoder (param.flatStr);
235
0
    for (unsigned int i = env.arg_start; i < env.argStack.get_count (); i++)
236
0
      encoder.encode_num (env.eval_arg (i));
237
0
    SUPER::flush_args (env, param);
238
0
  }
239
240
  static void flush_op (op_code_t op, cff1_cs_interp_env_t &env, flatten_param_t& param)
241
0
  {
242
0
    str_encoder_t  encoder (param.flatStr);
243
0
    encoder.encode_op (op);
244
0
  }
245
246
  static void flush_width (cff1_cs_interp_env_t &env, flatten_param_t& param)
247
0
  {
248
0
    assert (env.has_width);
249
0
    str_encoder_t  encoder (param.flatStr);
250
0
    encoder.encode_num (env.width);
251
0
  }
252
253
  static void flush_hintmask (op_code_t op, cff1_cs_interp_env_t &env, flatten_param_t& param)
254
0
  {
255
0
    SUPER::flush_hintmask (op, env, param);
256
0
    if (!param.drop_hints)
257
0
    {
258
0
      str_encoder_t  encoder (param.flatStr);
259
0
      for (unsigned int i = 0; i < env.hintmask_size; i++)
260
0
  encoder.encode_byte (env.str_ref[i]);
261
0
    }
262
0
  }
263
264
  private:
265
  typedef cff1_cs_opset_t<cff1_cs_opset_flatten_t, flatten_param_t> SUPER;
266
};
267
268
struct range_list_t : hb_vector_t<code_pair_t>
269
{
270
  /* replace the first glyph ID in the "glyph" field each range with a nLeft value */
271
  bool complete (unsigned int last_glyph)
272
0
  {
273
0
    bool  two_byte = false;
274
0
    for (unsigned int i = (*this).length; i > 0; i--)
275
0
    {
276
0
      code_pair_t &pair = (*this)[i - 1];
277
0
      unsigned int  nLeft = last_glyph - pair.glyph - 1;
278
0
      if (nLeft >= 0x100)
279
0
  two_byte = true;
280
0
      last_glyph = pair.glyph;
281
0
      pair.glyph = nLeft;
282
0
    }
283
0
    return two_byte;
284
0
  }
285
};
286
287
struct cff1_cs_opset_subr_subset_t : cff1_cs_opset_t<cff1_cs_opset_subr_subset_t, subr_subset_param_t>
288
{
289
  static void process_op (op_code_t op, cff1_cs_interp_env_t &env, subr_subset_param_t& param)
290
0
  {
291
0
    switch (op) {
292
293
0
      case OpCode_return:
294
0
  param.current_parsed_str->add_op (op, env.str_ref);
295
0
  param.current_parsed_str->set_parsed ();
296
0
  env.return_from_subr ();
297
0
  param.set_current_str (env, false);
298
0
  break;
299
300
0
      case OpCode_endchar:
301
0
  param.current_parsed_str->add_op (op, env.str_ref);
302
0
  param.current_parsed_str->set_parsed ();
303
0
  SUPER::process_op (op, env, param);
304
0
  break;
305
306
0
      case OpCode_callsubr:
307
0
  process_call_subr (op, CSType_LocalSubr, env, param, env.localSubrs, param.local_closure);
308
0
  break;
309
310
0
      case OpCode_callgsubr:
311
0
  process_call_subr (op, CSType_GlobalSubr, env, param, env.globalSubrs, param.global_closure);
312
0
  break;
313
314
0
      default:
315
0
  SUPER::process_op (op, env, param);
316
0
  param.current_parsed_str->add_op (op, env.str_ref);
317
0
  break;
318
0
    }
319
0
  }
320
321
  protected:
322
  static void process_call_subr (op_code_t op, cs_type_t type,
323
         cff1_cs_interp_env_t &env, subr_subset_param_t& param,
324
         cff1_biased_subrs_t& subrs, hb_set_t *closure)
325
0
  {
326
0
    byte_str_ref_t    str_ref = env.str_ref;
327
0
    env.call_subr (subrs, type);
328
0
    param.current_parsed_str->add_call_op (op, str_ref, env.context.subr_num);
329
0
    closure->add (env.context.subr_num);
330
0
    param.set_current_str (env, true);
331
0
  }
332
333
  private:
334
  typedef cff1_cs_opset_t<cff1_cs_opset_subr_subset_t, subr_subset_param_t> SUPER;
335
};
336
337
struct cff1_subr_subsetter_t : subr_subsetter_t<cff1_subr_subsetter_t, CFF1Subrs, const OT::cff1::accelerator_subset_t, cff1_cs_interp_env_t, cff1_cs_opset_subr_subset_t, OpCode_endchar>
338
{
339
  cff1_subr_subsetter_t (const OT::cff1::accelerator_subset_t &acc_, const hb_subset_plan_t *plan_)
340
0
    : subr_subsetter_t (acc_, plan_) {}
341
342
  static void complete_parsed_str (cff1_cs_interp_env_t &env, subr_subset_param_t& param, parsed_cs_str_t &charstring)
343
0
  {
344
    /* insert width at the beginning of the charstring as necessary */
345
0
    if (env.has_width)
346
0
      charstring.set_prefix (env.width);
347
348
    /* subroutines/charstring left on the call stack are legally left unmarked
349
     * unmarked when a subroutine terminates with endchar. mark them.
350
     */
351
0
    param.current_parsed_str->set_parsed ();
352
0
    for (unsigned int i = 0; i < env.callStack.get_count (); i++)
353
0
    {
354
0
      parsed_cs_str_t *parsed_str = param.get_parsed_str_for_context (env.callStack[i]);
355
0
      if (likely (parsed_str))
356
0
  parsed_str->set_parsed ();
357
0
      else
358
0
  env.set_error ();
359
0
    }
360
0
  }
361
};
362
363
struct cff_subset_plan {
364
  cff_subset_plan ()
365
    : info (),
366
      orig_fdcount (0),
367
      subset_fdcount (1),
368
      subset_fdselect_format (0),
369
      drop_hints (false),
370
      desubroutinize(false)
371
0
  {
372
0
    topdict_mod.init ();
373
0
    subset_fdselect_ranges.init ();
374
0
    fdmap.init ();
375
0
    subset_charstrings.init ();
376
0
    subset_globalsubrs.init ();
377
0
    subset_localsubrs.init ();
378
0
    fontdicts_mod.init ();
379
0
    subset_enc_code_ranges.init ();
380
0
    subset_enc_supp_codes.init ();
381
0
    subset_charset_ranges.init ();
382
0
    sidmap.init ();
383
0
    for (unsigned int i = 0; i < name_dict_values_t::ValCount; i++)
384
0
      topDictModSIDs[i] = CFF_UNDEF_SID;
385
0
  }
386
387
  ~cff_subset_plan ()
388
0
  {
389
0
    topdict_mod.fini ();
390
0
    subset_fdselect_ranges.fini ();
391
0
    fdmap.fini ();
392
0
    subset_charstrings.fini_deep ();
393
0
    subset_globalsubrs.fini_deep ();
394
0
    subset_localsubrs.fini_deep ();
395
0
    fontdicts_mod.fini ();
396
0
    subset_enc_code_ranges.fini ();
397
0
    subset_enc_supp_codes.fini ();
398
0
    subset_charset_ranges.fini ();
399
0
    sidmap.fini ();
400
0
  }
401
402
  void plan_subset_encoding (const OT::cff1::accelerator_subset_t &acc, hb_subset_plan_t *plan)
403
0
  {
404
0
    const Encoding *encoding = acc.encoding;
405
0
    unsigned int  size0, size1;
406
0
    hb_codepoint_t  code, last_code = CFF_UNDEF_CODE;
407
0
    hb_vector_t<hb_codepoint_t> supp_codes;
408
409
0
    if (unlikely (!subset_enc_code_ranges.resize (0)))
410
0
    {
411
0
      plan->check_success (false);
412
0
      return;
413
0
    }
414
415
0
    supp_codes.init ();
416
417
0
    subset_enc_num_codes = plan->num_output_glyphs () - 1;
418
0
    unsigned int glyph;
419
0
    for (glyph = 1; glyph < plan->num_output_glyphs (); glyph++)
420
0
    {
421
0
      hb_codepoint_t  old_glyph;
422
0
      if (!plan->old_gid_for_new_gid (glyph, &old_glyph))
423
0
      {
424
  /* Retain the code for the old missing glyph ID */
425
0
  old_glyph = glyph;
426
0
      }
427
0
      code = acc.glyph_to_code (old_glyph);
428
0
      if (code == CFF_UNDEF_CODE)
429
0
      {
430
0
  subset_enc_num_codes = glyph - 1;
431
0
  break;
432
0
      }
433
434
0
      if ((last_code == CFF_UNDEF_CODE) || (code != last_code + 1))
435
0
      {
436
0
  code_pair_t pair = { code, glyph };
437
0
  subset_enc_code_ranges.push (pair);
438
0
      }
439
0
      last_code = code;
440
441
0
      if (encoding != &Null (Encoding))
442
0
      {
443
0
  hb_codepoint_t  sid = acc.glyph_to_sid (old_glyph);
444
0
  encoding->get_supplement_codes (sid, supp_codes);
445
0
  for (unsigned int i = 0; i < supp_codes.length; i++)
446
0
  {
447
0
    code_pair_t pair = { supp_codes[i], sid };
448
0
    subset_enc_supp_codes.push (pair);
449
0
  }
450
0
      }
451
0
    }
452
0
    supp_codes.fini ();
453
454
0
    subset_enc_code_ranges.complete (glyph);
455
456
0
    assert (subset_enc_num_codes <= 0xFF);
457
0
    size0 = Encoding0::min_size + HBUINT8::static_size * subset_enc_num_codes;
458
0
    size1 = Encoding1::min_size + Encoding1_Range::static_size * subset_enc_code_ranges.length;
459
460
0
    if (size0 < size1)
461
0
      subset_enc_format = 0;
462
0
    else
463
0
      subset_enc_format = 1;
464
0
  }
465
466
  void plan_subset_charset (const OT::cff1::accelerator_subset_t &acc, hb_subset_plan_t *plan)
467
0
  {
468
0
    unsigned int  size0, size_ranges;
469
0
    hb_codepoint_t  sid, last_sid = CFF_UNDEF_CODE;
470
471
0
    if (unlikely (!subset_charset_ranges.resize (0)))
472
0
    {
473
0
      plan->check_success (false);
474
0
      return;
475
0
    }
476
477
0
    unsigned int glyph;
478
0
    for (glyph = 1; glyph < plan->num_output_glyphs (); glyph++)
479
0
    {
480
0
      hb_codepoint_t  old_glyph;
481
0
      if (!plan->old_gid_for_new_gid (glyph, &old_glyph))
482
0
      {
483
  /* Retain the SID for the old missing glyph ID */
484
0
  old_glyph = glyph;
485
0
      }
486
0
      sid = acc.glyph_to_sid (old_glyph);
487
488
0
      if (!acc.is_CID ())
489
0
  sid = sidmap.add (sid);
490
491
0
      if ((last_sid == CFF_UNDEF_CODE) || (sid != last_sid + 1))
492
0
      {
493
0
  code_pair_t pair = { sid, glyph };
494
0
  subset_charset_ranges.push (pair);
495
0
      }
496
0
      last_sid = sid;
497
0
    }
498
499
0
    bool two_byte = subset_charset_ranges.complete (glyph);
500
501
0
    size0 = Charset0::min_size + HBUINT16::static_size * (plan->num_output_glyphs () - 1);
502
0
    if (!two_byte)
503
0
      size_ranges = Charset1::min_size + Charset1_Range::static_size * subset_charset_ranges.length;
504
0
    else
505
0
      size_ranges = Charset2::min_size + Charset2_Range::static_size * subset_charset_ranges.length;
506
507
0
    if (size0 < size_ranges)
508
0
      subset_charset_format = 0;
509
0
    else if (!two_byte)
510
0
      subset_charset_format = 1;
511
0
    else
512
0
      subset_charset_format = 2;
513
0
  }
514
515
  bool collect_sids_in_dicts (const OT::cff1::accelerator_subset_t &acc)
516
0
  {
517
0
    sidmap.reset ();
518
519
0
    for (unsigned int i = 0; i < name_dict_values_t::ValCount; i++)
520
0
    {
521
0
      unsigned int sid = acc.topDict.nameSIDs[i];
522
0
      if (sid != CFF_UNDEF_SID)
523
0
      {
524
0
  (void)sidmap.add (sid);
525
0
  topDictModSIDs[i] = sidmap[sid];
526
0
      }
527
0
    }
528
529
0
    if (acc.fdArray != &Null (CFF1FDArray))
530
0
      for (unsigned int i = 0; i < orig_fdcount; i++)
531
0
  if (fdmap.has (i))
532
0
    (void)sidmap.add (acc.fontDicts[i].fontName);
533
534
0
    return true;
535
0
  }
536
537
  bool create (const OT::cff1::accelerator_subset_t &acc,
538
         hb_subset_plan_t *plan)
539
0
  {
540
    /* make sure notdef is first */
541
0
    hb_codepoint_t old_glyph;
542
0
    if (!plan->old_gid_for_new_gid (0, &old_glyph) || (old_glyph != 0)) return false;
543
544
0
    num_glyphs = plan->num_output_glyphs ();
545
0
    orig_fdcount = acc.fdCount;
546
0
    drop_hints = plan->flags & HB_SUBSET_FLAGS_NO_HINTING;
547
0
    desubroutinize = plan->flags & HB_SUBSET_FLAGS_DESUBROUTINIZE;
548
549
    /* check whether the subset renumbers any glyph IDs */
550
0
    gid_renum = false;
551
0
    for (hb_codepoint_t new_glyph = 0; new_glyph < plan->num_output_glyphs (); new_glyph++)
552
0
    {
553
0
      if (!plan->old_gid_for_new_gid(new_glyph, &old_glyph))
554
0
  continue;
555
0
      if (new_glyph != old_glyph) {
556
0
  gid_renum = true;
557
0
  break;
558
0
      }
559
0
    }
560
561
0
    subset_charset = gid_renum || !acc.is_predef_charset ();
562
0
    subset_encoding = !acc.is_CID() && !acc.is_predef_encoding ();
563
564
    /* top dict INDEX */
565
0
    {
566
      /* Add encoding/charset to a (copy of) top dict as necessary */
567
0
      topdict_mod.init (&acc.topDict);
568
0
      bool need_to_add_enc = (subset_encoding && !acc.topDict.has_op (OpCode_Encoding));
569
0
      bool need_to_add_set = (subset_charset && !acc.topDict.has_op (OpCode_charset));
570
0
      if (need_to_add_enc || need_to_add_set)
571
0
      {
572
0
  if (need_to_add_enc)
573
0
    topdict_mod.add_op (OpCode_Encoding);
574
0
  if (need_to_add_set)
575
0
    topdict_mod.add_op (OpCode_charset);
576
0
      }
577
0
    }
578
579
    /* Determine re-mapping of font index as fdmap among other info */
580
0
    if (acc.fdSelect != &Null (CFF1FDSelect))
581
0
    {
582
0
  if (unlikely (!hb_plan_subset_cff_fdselect (plan,
583
0
          orig_fdcount,
584
0
          *acc.fdSelect,
585
0
          subset_fdcount,
586
0
          info.fd_select.size,
587
0
          subset_fdselect_format,
588
0
          subset_fdselect_ranges,
589
0
          fdmap)))
590
0
  return false;
591
0
    }
592
0
    else
593
0
      fdmap.identity (1);
594
595
    /* remove unused SIDs & reassign SIDs */
596
0
    {
597
      /* SIDs for name strings in dicts are added before glyph names so they fit in 16-bit int range */
598
0
      if (unlikely (!collect_sids_in_dicts (acc)))
599
0
  return false;
600
0
      if (unlikely (sidmap.get_population () > 0x8000)) /* assumption: a dict won't reference that many strings */
601
0
  return false;
602
603
0
      if (subset_charset) plan_subset_charset (acc, plan);
604
605
0
      topdict_mod.reassignSIDs (sidmap);
606
0
    }
607
608
0
    if (desubroutinize)
609
0
    {
610
      /* Flatten global & local subrs */
611
0
      subr_flattener_t<const OT::cff1::accelerator_subset_t, cff1_cs_interp_env_t, cff1_cs_opset_flatten_t, OpCode_endchar>
612
0
        flattener(acc, plan);
613
0
      if (!flattener.flatten (subset_charstrings))
614
0
  return false;
615
0
    }
616
0
    else
617
0
    {
618
0
      cff1_subr_subsetter_t       subr_subsetter (acc, plan);
619
620
      /* Subset subrs: collect used subroutines, leaving all unused ones behind */
621
0
      if (!subr_subsetter.subset ())
622
0
  return false;
623
624
      /* encode charstrings, global subrs, local subrs with new subroutine numbers */
625
0
      if (!subr_subsetter.encode_charstrings (subset_charstrings))
626
0
  return false;
627
628
0
      if (!subr_subsetter.encode_globalsubrs (subset_globalsubrs))
629
0
  return false;
630
631
      /* local subrs */
632
0
      if (!subset_localsubrs.resize (orig_fdcount))
633
0
  return false;
634
0
      for (unsigned int fd = 0; fd < orig_fdcount; fd++)
635
0
      {
636
0
  subset_localsubrs[fd].init ();
637
0
  if (fdmap.has (fd))
638
0
  {
639
0
    if (!subr_subsetter.encode_localsubrs (fd, subset_localsubrs[fd]))
640
0
      return false;
641
0
  }
642
0
      }
643
0
    }
644
645
    /* Encoding */
646
0
    if (subset_encoding)
647
0
      plan_subset_encoding (acc, plan);
648
649
    /* private dicts & local subrs */
650
0
    if (!acc.is_CID ())
651
0
      fontdicts_mod.push (cff1_font_dict_values_mod_t ());
652
0
    else
653
0
    {
654
0
      + hb_iter (acc.fontDicts)
655
0
      | hb_filter ([&] (const cff1_font_dict_values_t &_)
656
0
  { return fdmap.has (&_ - &acc.fontDicts[0]); } )
657
0
      | hb_map ([&] (const cff1_font_dict_values_t &_)
658
0
  {
659
0
    cff1_font_dict_values_mod_t mod;
660
0
    mod.init (&_, sidmap[_.fontName]);
661
0
    return mod;
662
0
  })
663
0
      | hb_sink (fontdicts_mod)
664
0
      ;
665
0
    }
666
667
0
    return ((subset_charstrings.length == plan->num_output_glyphs ())
668
0
     && (fontdicts_mod.length == subset_fdcount));
669
0
  }
670
671
  cff1_top_dict_values_mod_t  topdict_mod;
672
  cff1_sub_table_info_t   info;
673
674
  unsigned int    num_glyphs;
675
  unsigned int    orig_fdcount;
676
  unsigned int    subset_fdcount;
677
  unsigned int    subset_fdselect_format;
678
  hb_vector_t<code_pair_t>   subset_fdselect_ranges;
679
680
  /* font dict index remap table from fullset FDArray to subset FDArray.
681
   * set to CFF_UNDEF_CODE if excluded from subset */
682
  hb_inc_bimap_t   fdmap;
683
684
  str_buff_vec_t    subset_charstrings;
685
  str_buff_vec_t    subset_globalsubrs;
686
  hb_vector_t<str_buff_vec_t> subset_localsubrs;
687
  hb_vector_t<cff1_font_dict_values_mod_t>  fontdicts_mod;
688
689
  bool    drop_hints;
690
691
  bool    gid_renum;
692
  bool    subset_encoding;
693
  uint8_t subset_enc_format;
694
  unsigned int  subset_enc_num_codes;
695
  range_list_t  subset_enc_code_ranges;
696
  hb_vector_t<code_pair_t>  subset_enc_supp_codes;
697
698
  uint8_t subset_charset_format;
699
  range_list_t  subset_charset_ranges;
700
  bool    subset_charset;
701
702
  remap_sid_t sidmap;
703
  unsigned int  topDictModSIDs[name_dict_values_t::ValCount];
704
705
  bool    desubroutinize;
706
};
707
708
static bool _serialize_cff1 (hb_serialize_context_t *c,
709
           cff_subset_plan &plan,
710
           const OT::cff1::accelerator_subset_t  &acc,
711
           unsigned int num_glyphs)
712
0
{
713
  /* private dicts & local subrs */
714
0
  for (int i = (int)acc.privateDicts.length; --i >= 0 ;)
715
0
  {
716
0
    if (plan.fdmap.has (i))
717
0
    {
718
0
      objidx_t  subrs_link = 0;
719
0
      if (plan.subset_localsubrs[i].length > 0)
720
0
      {
721
0
  CFF1Subrs *dest = c->start_embed <CFF1Subrs> ();
722
0
  if (unlikely (!dest)) return false;
723
0
  c->push ();
724
0
  if (likely (dest && dest->serialize (c, plan.subset_localsubrs[i])))
725
0
    subrs_link = c->pop_pack ();
726
0
  else
727
0
  {
728
0
    c->pop_discard ();
729
0
    return false;
730
0
  }
731
0
      }
732
733
0
      PrivateDict *pd = c->start_embed<PrivateDict> ();
734
0
      if (unlikely (!pd)) return false;
735
0
      c->push ();
736
0
      cff_private_dict_op_serializer_t privSzr (plan.desubroutinize, plan.drop_hints);
737
      /* N.B. local subrs immediately follows its corresponding private dict. i.e., subr offset == private dict size */
738
0
      if (likely (pd->serialize (c, acc.privateDicts[i], privSzr, subrs_link)))
739
0
      {
740
0
  unsigned fd = plan.fdmap[i];
741
0
  plan.fontdicts_mod[fd].privateDictInfo.size = c->length ();
742
0
  plan.fontdicts_mod[fd].privateDictInfo.link = c->pop_pack ();
743
0
      }
744
0
      else
745
0
      {
746
0
  c->pop_discard ();
747
0
  return false;
748
0
      }
749
0
    }
750
0
  }
751
752
0
  if (!acc.is_CID ())
753
0
    plan.info.privateDictInfo = plan.fontdicts_mod[0].privateDictInfo;
754
755
  /* CharStrings */
756
0
  {
757
0
    CFF1CharStrings  *cs = c->start_embed<CFF1CharStrings> ();
758
0
    if (unlikely (!cs)) return false;
759
0
    c->push ();
760
0
    if (likely (cs->serialize (c, plan.subset_charstrings)))
761
0
      plan.info.char_strings_link = c->pop_pack ();
762
0
    else
763
0
    {
764
0
      c->pop_discard ();
765
0
      return false;
766
0
    }
767
0
  }
768
769
  /* FDArray (FD Index) */
770
0
  if (acc.fdArray != &Null (CFF1FDArray))
771
0
  {
772
0
    CFF1FDArray *fda = c->start_embed<CFF1FDArray> ();
773
0
    if (unlikely (!fda)) return false;
774
0
    c->push ();
775
0
    cff1_font_dict_op_serializer_t  fontSzr;
776
0
    auto it = + hb_zip (+ hb_iter (plan.fontdicts_mod), + hb_iter (plan.fontdicts_mod));
777
0
    if (likely (fda->serialize (c, it, fontSzr)))
778
0
      plan.info.fd_array_link = c->pop_pack (false);
779
0
    else
780
0
    {
781
0
      c->pop_discard ();
782
0
      return false;
783
0
    }
784
0
  }
785
786
  /* FDSelect */
787
0
  if (acc.fdSelect != &Null (CFF1FDSelect))
788
0
  {
789
0
    c->push ();
790
0
    if (likely (hb_serialize_cff_fdselect (c, num_glyphs, *acc.fdSelect, acc.fdCount,
791
0
             plan.subset_fdselect_format, plan.info.fd_select.size,
792
0
             plan.subset_fdselect_ranges)))
793
0
      plan.info.fd_select.link = c->pop_pack ();
794
0
    else
795
0
    {
796
0
      c->pop_discard ();
797
0
      return false;
798
0
    }
799
0
  }
800
801
  /* Charset */
802
0
  if (plan.subset_charset)
803
0
  {
804
0
    Charset *dest = c->start_embed<Charset> ();
805
0
    if (unlikely (!dest)) return false;
806
0
    c->push ();
807
0
    if (likely (dest->serialize (c,
808
0
         plan.subset_charset_format,
809
0
         plan.num_glyphs,
810
0
         plan.subset_charset_ranges)))
811
0
      plan.info.charset_link = c->pop_pack ();
812
0
    else
813
0
    {
814
0
      c->pop_discard ();
815
0
      return false;
816
0
    }
817
0
  }
818
819
  /* Encoding */
820
0
  if (plan.subset_encoding)
821
0
  {
822
0
    Encoding *dest = c->start_embed<Encoding> ();
823
0
    if (unlikely (!dest)) return false;
824
0
    c->push ();
825
0
    if (likely (dest->serialize (c,
826
0
         plan.subset_enc_format,
827
0
         plan.subset_enc_num_codes,
828
0
         plan.subset_enc_code_ranges,
829
0
         plan.subset_enc_supp_codes)))
830
0
      plan.info.encoding_link = c->pop_pack ();
831
0
    else
832
0
    {
833
0
      c->pop_discard ();
834
0
      return false;
835
0
    }
836
0
  }
837
838
  /* global subrs */
839
0
  {
840
0
    c->push ();
841
0
    CFF1Subrs *dest = c->start_embed <CFF1Subrs> ();
842
0
    if (unlikely (!dest)) return false;
843
0
    if (likely (dest->serialize (c, plan.subset_globalsubrs)))
844
0
      c->pop_pack ();
845
0
    else
846
0
    {
847
0
      c->pop_discard ();
848
0
      return false;
849
0
    }
850
0
  }
851
852
  /* String INDEX */
853
0
  {
854
0
    CFF1StringIndex *dest = c->start_embed<CFF1StringIndex> ();
855
0
    if (unlikely (!dest)) return false;
856
0
    c->push ();
857
0
    if (likely (dest->serialize (c, *acc.stringIndex, plan.sidmap)))
858
0
      c->pop_pack ();
859
0
    else
860
0
    {
861
0
      c->pop_discard ();
862
0
      return false;
863
0
    }
864
0
  }
865
866
0
  OT::cff1 *cff = c->allocate_min<OT::cff1> ();
867
0
  if (unlikely (!cff))
868
0
    return false;
869
870
  /* header */
871
0
  cff->version.major = 0x01;
872
0
  cff->version.minor = 0x00;
873
0
  cff->nameIndex = cff->min_size;
874
0
  cff->offSize = 4; /* unused? */
875
876
  /* name INDEX */
877
0
  if (unlikely (!(*acc.nameIndex).copy (c))) return false;
878
879
  /* top dict INDEX */
880
0
  {
881
    /* serialize singleton TopDict */
882
0
    TopDict *top = c->start_embed<TopDict> ();
883
0
    if (!top) return false;
884
0
    c->push ();
885
0
    cff1_top_dict_op_serializer_t topSzr;
886
0
    unsigned top_size = 0;
887
0
    top_dict_modifiers_t  modifier (plan.info, plan.topDictModSIDs);
888
0
    if (likely (top->serialize (c, plan.topdict_mod, topSzr, modifier)))
889
0
    {
890
0
      top_size = c->length ();
891
0
      c->pop_pack (false);
892
0
    }
893
0
    else
894
0
    {
895
0
      c->pop_discard ();
896
0
      return false;
897
0
    }
898
    /* serialize INDEX header for above */
899
0
    CFF1Index *dest = c->start_embed<CFF1Index> ();
900
0
    if (!dest) return false;
901
0
    return dest->serialize_header (c, hb_iter (hb_array_t<unsigned> (&top_size, 1)));
902
0
  }
903
0
}
904
905
static bool
906
_hb_subset_cff1 (const OT::cff1::accelerator_subset_t  &acc,
907
    hb_subset_context_t *c)
908
0
{
909
0
  cff_subset_plan cff_plan;
910
911
0
  if (unlikely (!cff_plan.create (acc, c->plan)))
912
0
  {
913
0
    DEBUG_MSG(SUBSET, nullptr, "Failed to generate a cff subsetting plan.");
914
0
    return false;
915
0
  }
916
917
0
  return _serialize_cff1 (c->serializer, cff_plan, acc, c->plan->num_output_glyphs ());
918
0
}
919
920
bool
921
hb_subset_cff1 (hb_subset_context_t *c)
922
0
{
923
0
  OT::cff1::accelerator_subset_t acc;
924
0
  acc.init (c->plan->source);
925
0
  bool result = likely (acc.is_valid ()) && _hb_subset_cff1 (acc, c);
926
0
  acc.fini ();
927
928
0
  return result;
929
0
}
930
931
932
#endif