Coverage Report

Created: 2023-06-07 06:14

/src/harfbuzz/src/hb-ot-cff2-table.hh
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
#ifndef HB_OT_CFF2_TABLE_HH
28
#define HB_OT_CFF2_TABLE_HH
29
30
#include "hb-ot-cff-common.hh"
31
#include "hb-subset-cff2.hh"
32
#include "hb-draw.hh"
33
#include "hb-paint.hh"
34
35
namespace CFF {
36
37
/*
38
 * CFF2 -- Compact Font Format (CFF) Version 2
39
 * https://docs.microsoft.com/en-us/typography/opentype/spec/cff2
40
 */
41
#define HB_OT_TAG_CFF2 HB_TAG('C','F','F','2')
42
43
typedef CFFIndex<HBUINT32>  CFF2Index;
44
45
typedef CFF2Index         CFF2CharStrings;
46
typedef Subrs<HBUINT32>   CFF2Subrs;
47
48
typedef FDSelect3_4<HBUINT32, HBUINT16> FDSelect4;
49
typedef FDSelect3_4_Range<HBUINT32, HBUINT16> FDSelect4_Range;
50
51
struct CFF2FDSelect
52
{
53
  bool serialize (hb_serialize_context_t *c, const CFF2FDSelect &src, unsigned int num_glyphs)
54
0
  {
55
0
    TRACE_SERIALIZE (this);
56
0
    unsigned int size = src.get_size (num_glyphs);
57
0
    CFF2FDSelect *dest = c->allocate_size<CFF2FDSelect> (size);
58
0
    if (unlikely (!dest)) return_trace (false);
59
0
    hb_memcpy (dest, &src, size);
60
0
    return_trace (true);
61
0
  }
62
63
  unsigned int get_size (unsigned int num_glyphs) const
64
0
  {
65
0
    switch (format)
66
0
    {
67
0
    case 0: return format.static_size + u.format0.get_size (num_glyphs);
68
0
    case 3: return format.static_size + u.format3.get_size ();
69
0
    case 4: return format.static_size + u.format4.get_size ();
70
0
    default:return 0;
71
0
    }
72
0
  }
73
74
  hb_codepoint_t get_fd (hb_codepoint_t glyph) const
75
0
  {
76
0
    if (this == &Null (CFF2FDSelect))
77
0
      return 0;
78
79
0
    switch (format)
80
0
    {
81
0
    case 0: return u.format0.get_fd (glyph);
82
0
    case 3: return u.format3.get_fd (glyph);
83
0
    case 4: return u.format4.get_fd (glyph);
84
0
    default:return 0;
85
0
    }
86
0
  }
87
88
  bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
89
0
  {
90
0
    TRACE_SANITIZE (this);
91
0
    if (unlikely (!c->check_struct (this)))
92
0
      return_trace (false);
93
94
0
    switch (format)
95
0
    {
96
0
    case 0: return_trace (u.format0.sanitize (c, fdcount));
97
0
    case 3: return_trace (u.format3.sanitize (c, fdcount));
98
0
    case 4: return_trace (u.format4.sanitize (c, fdcount));
99
0
    default:return_trace (false);
100
0
    }
101
0
  }
102
103
  HBUINT8 format;
104
  union {
105
  FDSelect0 format0;
106
  FDSelect3 format3;
107
  FDSelect4 format4;
108
  } u;
109
  public:
110
  DEFINE_SIZE_MIN (2);
111
};
112
113
struct CFF2VariationStore
114
{
115
  bool sanitize (hb_sanitize_context_t *c) const
116
0
  {
117
0
    TRACE_SANITIZE (this);
118
0
    return_trace (likely (c->check_struct (this)) && c->check_range (&varStore, size) && varStore.sanitize (c));
119
0
  }
120
121
  bool serialize (hb_serialize_context_t *c, const CFF2VariationStore *varStore)
122
0
  {
123
0
    TRACE_SERIALIZE (this);
124
0
    unsigned int size_ = varStore->get_size ();
125
0
    CFF2VariationStore *dest = c->allocate_size<CFF2VariationStore> (size_);
126
0
    if (unlikely (!dest)) return_trace (false);
127
0
    hb_memcpy (dest, varStore, size_);
128
0
    return_trace (true);
129
0
  }
130
131
0
  unsigned int get_size () const { return HBUINT16::static_size + size; }
132
133
  HBUINT16  size;
134
  VariationStore  varStore;
135
136
  DEFINE_SIZE_MIN (2 + VariationStore::min_size);
137
};
138
139
struct cff2_top_dict_values_t : top_dict_values_t<>
140
{
141
  void init ()
142
0
  {
143
0
    top_dict_values_t<>::init ();
144
0
    vstoreOffset = 0;
145
0
    FDSelectOffset = 0;
146
0
  }
147
0
  void fini () { top_dict_values_t<>::fini (); }
148
149
  unsigned int  vstoreOffset;
150
  unsigned int  FDSelectOffset;
151
};
152
153
struct cff2_top_dict_opset_t : top_dict_opset_t<>
154
{
155
  static void process_op (op_code_t op, num_interp_env_t& env, cff2_top_dict_values_t& dictval)
156
0
  {
157
0
    switch (op) {
158
0
      case OpCode_FontMatrix:
159
0
  {
160
0
    dict_val_t val;
161
0
    val.init ();
162
0
    dictval.add_op (op, env.str_ref);
163
0
    env.clear_args ();
164
0
  }
165
0
  break;
166
167
0
      case OpCode_vstore:
168
0
  dictval.vstoreOffset = env.argStack.pop_uint ();
169
0
  env.clear_args ();
170
0
  break;
171
0
      case OpCode_FDSelect:
172
0
  dictval.FDSelectOffset = env.argStack.pop_uint ();
173
0
  env.clear_args ();
174
0
  break;
175
176
0
      default:
177
0
  SUPER::process_op (op, env, dictval);
178
  /* Record this operand below if stack is empty, otherwise done */
179
0
  if (!env.argStack.is_empty ()) return;
180
0
    }
181
182
0
    if (unlikely (env.in_error ())) return;
183
184
0
    dictval.add_op (op, env.str_ref);
185
0
  }
186
187
  typedef top_dict_opset_t<> SUPER;
188
};
189
190
struct cff2_font_dict_values_t : dict_values_t<op_str_t>
191
{
192
  void init ()
193
0
  {
194
0
    dict_values_t<op_str_t>::init ();
195
0
    privateDictInfo.init ();
196
0
  }
197
0
  void fini () { dict_values_t<op_str_t>::fini (); }
198
199
  table_info_t    privateDictInfo;
200
};
201
202
struct cff2_font_dict_opset_t : dict_opset_t
203
{
204
  static void process_op (op_code_t op, num_interp_env_t& env, cff2_font_dict_values_t& dictval)
205
0
  {
206
0
    switch (op) {
207
0
      case OpCode_Private:
208
0
  dictval.privateDictInfo.offset = env.argStack.pop_uint ();
209
0
  dictval.privateDictInfo.size = env.argStack.pop_uint ();
210
0
  env.clear_args ();
211
0
  break;
212
213
0
      default:
214
0
  SUPER::process_op (op, env);
215
0
  if (!env.argStack.is_empty ())
216
0
    return;
217
0
    }
218
219
0
    if (unlikely (env.in_error ())) return;
220
221
0
    dictval.add_op (op, env.str_ref);
222
0
  }
223
224
  private:
225
  typedef dict_opset_t SUPER;
226
};
227
228
template <typename VAL>
229
struct cff2_private_dict_values_base_t : dict_values_t<VAL>
230
{
231
  void init ()
232
0
  {
233
0
    dict_values_t<VAL>::init ();
234
0
    subrsOffset = 0;
235
0
    localSubrs = &Null (CFF2Subrs);
236
0
    ivs = 0;
237
0
  }
238
  void fini () { dict_values_t<VAL>::fini (); }
239
240
  unsigned int      subrsOffset;
241
  const CFF2Subrs   *localSubrs;
242
  unsigned int      ivs;
243
};
244
245
typedef cff2_private_dict_values_base_t<op_str_t> cff2_private_dict_values_subset_t;
246
typedef cff2_private_dict_values_base_t<num_dict_val_t> cff2_private_dict_values_t;
247
248
struct cff2_priv_dict_interp_env_t : num_interp_env_t
249
{
250
  cff2_priv_dict_interp_env_t (const hb_ubytes_t &str) :
251
0
    num_interp_env_t (str) {}
252
253
  void process_vsindex ()
254
0
  {
255
0
    if (likely (!seen_vsindex))
256
0
    {
257
0
      set_ivs (argStack.pop_uint ());
258
0
    }
259
0
    seen_vsindex = true;
260
0
  }
261
262
0
  unsigned int get_ivs () const { return ivs; }
263
0
  void   set_ivs (unsigned int ivs_) { ivs = ivs_; }
264
265
  protected:
266
  unsigned int  ivs = 0;
267
  bool    seen_vsindex = false;
268
};
269
270
struct cff2_private_dict_opset_t : dict_opset_t
271
{
272
  static void process_op (op_code_t op, cff2_priv_dict_interp_env_t& env, cff2_private_dict_values_t& dictval)
273
0
  {
274
0
    num_dict_val_t val;
275
0
    val.init ();
276
277
0
    switch (op) {
278
0
      case OpCode_StdHW:
279
0
      case OpCode_StdVW:
280
0
      case OpCode_BlueScale:
281
0
      case OpCode_BlueShift:
282
0
      case OpCode_BlueFuzz:
283
0
      case OpCode_ExpansionFactor:
284
0
      case OpCode_LanguageGroup:
285
0
      case OpCode_BlueValues:
286
0
      case OpCode_OtherBlues:
287
0
      case OpCode_FamilyBlues:
288
0
      case OpCode_FamilyOtherBlues:
289
0
      case OpCode_StemSnapH:
290
0
      case OpCode_StemSnapV:
291
0
  env.clear_args ();
292
0
  break;
293
0
      case OpCode_Subrs:
294
0
  dictval.subrsOffset = env.argStack.pop_uint ();
295
0
  env.clear_args ();
296
0
  break;
297
0
      case OpCode_vsindexdict:
298
0
  env.process_vsindex ();
299
0
  dictval.ivs = env.get_ivs ();
300
0
  env.clear_args ();
301
0
  break;
302
0
      case OpCode_blenddict:
303
0
  break;
304
305
0
      default:
306
0
  dict_opset_t::process_op (op, env);
307
0
  if (!env.argStack.is_empty ()) return;
308
0
  break;
309
0
    }
310
311
0
    if (unlikely (env.in_error ())) return;
312
313
0
    dictval.add_op (op, env.str_ref, val);
314
0
  }
315
};
316
317
struct cff2_private_dict_opset_subset_t : dict_opset_t
318
{
319
  static void process_op (op_code_t op, cff2_priv_dict_interp_env_t& env, cff2_private_dict_values_subset_t& dictval)
320
0
  {
321
0
    switch (op) {
322
0
      case OpCode_BlueValues:
323
0
      case OpCode_OtherBlues:
324
0
      case OpCode_FamilyBlues:
325
0
      case OpCode_FamilyOtherBlues:
326
0
      case OpCode_StdHW:
327
0
      case OpCode_StdVW:
328
0
      case OpCode_BlueScale:
329
0
      case OpCode_BlueShift:
330
0
      case OpCode_BlueFuzz:
331
0
      case OpCode_StemSnapH:
332
0
      case OpCode_StemSnapV:
333
0
      case OpCode_LanguageGroup:
334
0
      case OpCode_ExpansionFactor:
335
0
  env.clear_args ();
336
0
  break;
337
0
338
0
      case OpCode_blenddict:
339
0
  env.clear_args ();
340
0
  return;
341
0
342
0
      case OpCode_Subrs:
343
0
  dictval.subrsOffset = env.argStack.pop_uint ();
344
0
  env.clear_args ();
345
0
  break;
346
0
347
0
      default:
348
0
  SUPER::process_op (op, env);
349
0
  if (!env.argStack.is_empty ()) return;
350
0
  break;
351
0
    }
352
0
353
0
    if (unlikely (env.in_error ())) return;
354
0
355
0
    dictval.add_op (op, env.str_ref);
356
0
  }
357
358
  private:
359
  typedef dict_opset_t SUPER;
360
};
361
362
typedef dict_interpreter_t<cff2_top_dict_opset_t, cff2_top_dict_values_t> cff2_top_dict_interpreter_t;
363
typedef dict_interpreter_t<cff2_font_dict_opset_t, cff2_font_dict_values_t> cff2_font_dict_interpreter_t;
364
365
struct CFF2FDArray : FDArray<HBUINT32>
366
{
367
  /* FDArray::serialize does not compile without this partial specialization */
368
  template <typename ITER, typename OP_SERIALIZER>
369
  bool serialize (hb_serialize_context_t *c, ITER it, OP_SERIALIZER& opszr)
370
  { return FDArray<HBUINT32>::serialize<cff2_font_dict_values_t, table_info_t> (c, it, opszr); }
371
};
372
373
} /* namespace CFF */
374
375
namespace OT {
376
377
using namespace CFF;
378
379
struct cff2
380
{
381
  static constexpr hb_tag_t tableTag = HB_OT_TAG_CFF2;
382
383
  bool sanitize (hb_sanitize_context_t *c) const
384
0
  {
385
0
    TRACE_SANITIZE (this);
386
0
    return_trace (c->check_struct (this) &&
387
0
      likely (version.major == 2));
388
0
  }
389
390
  template <typename PRIVOPSET, typename PRIVDICTVAL>
391
  struct accelerator_templ_t
392
  {
393
    accelerator_templ_t (hb_face_t *face)
394
0
    {
395
0
      topDict.init ();
396
0
      fontDicts.init ();
397
0
      privateDicts.init ();
398
399
0
      this->blob = sc.reference_table<cff2> (face);
400
401
      /* setup for run-time santization */
402
0
      sc.init (this->blob);
403
0
      sc.start_processing ();
404
405
0
      const OT::cff2 *cff2 = this->blob->template as<OT::cff2> ();
406
407
0
      if (cff2 == &Null (OT::cff2))
408
0
        goto fail;
409
410
0
      { /* parse top dict */
411
0
  hb_ubytes_t topDictStr = (cff2 + cff2->topDict).as_ubytes (cff2->topDictSize);
412
0
  if (unlikely (!topDictStr.sanitize (&sc))) goto fail;
413
0
  num_interp_env_t env (topDictStr);
414
0
  cff2_top_dict_interpreter_t top_interp (env);
415
0
  topDict.init ();
416
0
  if (unlikely (!top_interp.interpret (topDict))) goto fail;
417
0
      }
418
419
0
      globalSubrs = &StructAtOffset<CFF2Subrs> (cff2, cff2->topDict + cff2->topDictSize);
420
0
      varStore = &StructAtOffsetOrNull<CFF2VariationStore> (cff2, topDict.vstoreOffset);
421
0
      charStrings = &StructAtOffsetOrNull<CFF2CharStrings> (cff2, topDict.charStringsOffset);
422
0
      fdArray = &StructAtOffsetOrNull<CFF2FDArray> (cff2, topDict.FDArrayOffset);
423
0
      fdSelect = &StructAtOffsetOrNull<CFF2FDSelect> (cff2, topDict.FDSelectOffset);
424
425
0
      if (((varStore != &Null (CFF2VariationStore)) && unlikely (!varStore->sanitize (&sc))) ||
426
0
    (charStrings == &Null (CFF2CharStrings)) || unlikely (!charStrings->sanitize (&sc)) ||
427
0
    (globalSubrs == &Null (CFF2Subrs)) || unlikely (!globalSubrs->sanitize (&sc)) ||
428
0
    (fdArray == &Null (CFF2FDArray)) || unlikely (!fdArray->sanitize (&sc)) ||
429
0
    (((fdSelect != &Null (CFF2FDSelect)) && unlikely (!fdSelect->sanitize (&sc, fdArray->count)))))
430
0
        goto fail;
431
432
0
      num_glyphs = charStrings->count;
433
0
      if (num_glyphs != sc.get_num_glyphs ())
434
0
        goto fail;
435
436
0
      fdCount = fdArray->count;
437
0
      if (!privateDicts.resize (fdCount))
438
0
        goto fail;
439
440
      /* parse font dicts and gather private dicts */
441
0
      for (unsigned int i = 0; i < fdCount; i++)
442
0
      {
443
0
  const hb_ubytes_t fontDictStr = (*fdArray)[i];
444
0
  if (unlikely (!fontDictStr.sanitize (&sc))) goto fail;
445
0
  cff2_font_dict_values_t  *font;
446
0
  num_interp_env_t env (fontDictStr);
447
0
  cff2_font_dict_interpreter_t font_interp (env);
448
0
  font = fontDicts.push ();
449
0
  if (unlikely (font == &Crap (cff2_font_dict_values_t))) goto fail;
450
0
  font->init ();
451
0
  if (unlikely (!font_interp.interpret (*font))) goto fail;
452
453
0
  const hb_ubytes_t privDictStr = StructAtOffsetOrNull<UnsizedByteStr> (cff2, font->privateDictInfo.offset).as_ubytes (font->privateDictInfo.size);
454
0
  if (unlikely (!privDictStr.sanitize (&sc))) goto fail;
455
0
  cff2_priv_dict_interp_env_t env2 (privDictStr);
456
0
  dict_interpreter_t<PRIVOPSET, PRIVDICTVAL, cff2_priv_dict_interp_env_t> priv_interp (env2);
457
0
  privateDicts[i].init ();
458
0
  if (unlikely (!priv_interp.interpret (privateDicts[i]))) goto fail;
459
460
0
  privateDicts[i].localSubrs = &StructAtOffsetOrNull<CFF2Subrs> (&privDictStr[0], privateDicts[i].subrsOffset);
461
0
  if (privateDicts[i].localSubrs != &Null (CFF2Subrs) &&
462
0
    unlikely (!privateDicts[i].localSubrs->sanitize (&sc)))
463
0
    goto fail;
464
0
      }
465
466
467
0
      return;
468
469
0
      fail:
470
0
        _fini ();
471
0
    }
472
0
    ~accelerator_templ_t () { _fini (); }
473
    void _fini ()
474
0
    {
475
0
      sc.end_processing ();
476
0
      topDict.fini ();
477
0
      fontDicts.fini ();
478
0
      privateDicts.fini ();
479
0
      hb_blob_destroy (blob);
480
0
      blob = nullptr;
481
0
    }
482
483
    hb_vector_t<uint16_t> *create_glyph_to_sid_map () const
484
    {
485
      return nullptr;
486
    }
487
488
0
    bool is_valid () const { return blob; }
489
490
    protected:
491
    hb_sanitize_context_t sc;
492
493
    public:
494
    hb_blob_t     *blob = nullptr;
495
    cff2_top_dict_values_t  topDict;
496
    const CFF2Subrs   *globalSubrs = nullptr;
497
    const CFF2VariationStore  *varStore = nullptr;
498
    const CFF2CharStrings *charStrings = nullptr;
499
    const CFF2FDArray   *fdArray = nullptr;
500
    const CFF2FDSelect    *fdSelect = nullptr;
501
    unsigned int    fdCount = 0;
502
503
    hb_vector_t<cff2_font_dict_values_t>     fontDicts;
504
    hb_vector_t<PRIVDICTVAL>  privateDicts;
505
506
    unsigned int        num_glyphs = 0;
507
  };
508
509
  struct accelerator_t : accelerator_templ_t<cff2_private_dict_opset_t, cff2_private_dict_values_t>
510
  {
511
0
    accelerator_t (hb_face_t *face) : accelerator_templ_t (face) {}
512
513
    HB_INTERNAL bool get_extents (hb_font_t *font,
514
          hb_codepoint_t glyph,
515
          hb_glyph_extents_t *extents) const;
516
    HB_INTERNAL bool paint_glyph (hb_font_t *font, hb_codepoint_t glyph, hb_paint_funcs_t *funcs, void *data, hb_color_t foreground) const;
517
    HB_INTERNAL bool get_path (hb_font_t *font, hb_codepoint_t glyph, hb_draw_session_t &draw_session) const;
518
  };
519
520
  typedef accelerator_templ_t<cff2_private_dict_opset_subset_t, cff2_private_dict_values_subset_t> accelerator_subset_t;
521
522
0
  bool subset (hb_subset_context_t *c) const { return hb_subset_cff2 (c); }
523
524
  public:
525
  FixedVersion<HBUINT8>   version;  /* Version of CFF2 table. set to 0x0200u */
526
  NNOffsetTo<TopDict, HBUINT8>  topDict;  /* headerSize = Offset to Top DICT. */
527
  HBUINT16      topDictSize;  /* Top DICT size */
528
529
  public:
530
  DEFINE_SIZE_STATIC (5);
531
};
532
533
struct cff2_accelerator_t : cff2::accelerator_t {
534
0
  cff2_accelerator_t (hb_face_t *face) : cff2::accelerator_t (face) {}
535
};
536
537
} /* namespace OT */
538
539
#endif /* HB_OT_CFF2_TABLE_HH */