Coverage Report

Created: 2026-05-30 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/harfbuzz/src/hb-ot-var-gvar-table.hh
Line
Count
Source
1
/*
2
 * Copyright © 2019  Adobe Inc.
3
 * Copyright © 2019  Ebrahim Byagowi
4
 *
5
 *  This is part of HarfBuzz, a text shaping library.
6
 *
7
 * Permission is hereby granted, without written agreement and without
8
 * license or royalty fees, to use, copy, modify, and distribute this
9
 * software and its documentation for any purpose, provided that the
10
 * above copyright notice and the following two paragraphs appear in
11
 * all copies of this software.
12
 *
13
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17
 * DAMAGE.
18
 *
19
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24
 *
25
 * Adobe Author(s): Michiharu Ariza
26
 */
27
28
#ifndef HB_OT_VAR_GVAR_TABLE_HH
29
#define HB_OT_VAR_GVAR_TABLE_HH
30
31
#include "hb-decycler.hh"
32
#include "hb-open-type.hh"
33
#include "hb-ot-var-common.hh"
34
35
/*
36
 * gvar -- Glyph Variation Table
37
 * https://docs.microsoft.com/en-us/typography/opentype/spec/gvar
38
 */
39
#define HB_OT_TAG_gvar HB_TAG('g','v','a','r')
40
#define HB_OT_TAG_GVAR HB_TAG('G','V','A','R')
41
42
struct hb_glyf_scratch_t
43
{
44
  // glyf
45
  contour_point_vector_t all_points;
46
  contour_point_vector_t comp_points;
47
  hb_decycler_t decycler;
48
49
  // gvar
50
  contour_point_vector_t orig_points;
51
  hb_vector_t<int> x_deltas;
52
  hb_vector_t<int> y_deltas;
53
  contour_point_vector_t deltas;
54
  hb_vector_t<unsigned int> shared_indices;
55
  hb_vector_t<unsigned int> private_indices;
56
};
57
58
namespace OT {
59
60
template <typename OffsetType>
61
struct glyph_variations_t
62
{
63
  // TODO: Move tuple_variations_t to outside of TupleVariationData
64
  using tuple_variations_t = typename TupleVariationData<OffsetType>::tuple_variations_t;
65
  using GlyphVariationData = TupleVariationData<OffsetType>;
66
67
  hb_vector_t<tuple_variations_t> glyph_variations;
68
69
  hb_vector_t<F2DOT14> compiled_shared_tuples;
70
  private:
71
  unsigned shared_tuples_count = 0;
72
73
  /* shared coords-> index map after instantiation */
74
  hb_hashmap_t<const hb_vector_t<F2DOT14>*, unsigned> shared_tuples_idx_map;
75
76
  hb_alloc_pool_t pool;
77
78
  public:
79
  unsigned compiled_shared_tuples_count () const
80
  { return shared_tuples_count; }
81
82
  unsigned compiled_byte_size () const
83
  {
84
    unsigned byte_size = 0;
85
    for (const auto& _ : glyph_variations)
86
      byte_size += _.get_compiled_byte_size ();
87
88
    return byte_size;
89
  }
90
91
  bool create_from_glyphs_var_data (unsigned axis_count,
92
                                    const hb_array_t<const F2DOT14> shared_tuples,
93
                                    const hb_subset_plan_t *plan,
94
                                    const hb_hashmap_t<hb_codepoint_t, hb_bytes_t>& new_gid_var_data_map)
95
  {
96
    if (unlikely (!glyph_variations.alloc_exact (plan->new_to_old_gid_list.length)))
97
      return false;
98
99
    auto it = hb_iter (plan->new_to_old_gid_list);
100
    for (auto &_ : it)
101
    {
102
      hb_codepoint_t new_gid = _.first;
103
      contour_point_vector_t *all_contour_points;
104
      if (!new_gid_var_data_map.has (new_gid) ||
105
          !plan->new_gid_contour_points_map.has (new_gid, &all_contour_points))
106
        return false;
107
      hb_bytes_t var_data = new_gid_var_data_map.get (new_gid);
108
109
      const GlyphVariationData* p = reinterpret_cast<const GlyphVariationData*> (var_data.arrayZ);
110
      typename GlyphVariationData::tuple_iterator_t iterator;
111
      tuple_variations_t tuple_vars;
112
113
      hb_vector_t<unsigned> shared_indices;
114
115
      /* in case variation data is empty, push an empty struct into the vector,
116
       * keep the vector in sync with the new_to_old_gid_list */
117
      if (!var_data || ! p->has_data () || !all_contour_points->length ||
118
          !GlyphVariationData::get_tuple_iterator (var_data, axis_count,
119
                                                   var_data.arrayZ,
120
                                                   shared_indices, &iterator))
121
      {
122
        glyph_variations.push (std::move (tuple_vars));
123
        continue;
124
      }
125
126
      bool is_composite_glyph = false;
127
      is_composite_glyph = plan->composite_new_gids.has (new_gid);
128
129
      if (!p->decompile_tuple_variations (all_contour_points->length, true /* is_gvar */,
130
                                          iterator, &(plan->axes_old_index_tag_map),
131
                                          shared_indices, shared_tuples,
132
                                          tuple_vars, /* OUT */
133
            &pool,
134
                                          is_composite_glyph))
135
        return false;
136
      glyph_variations.push (std::move (tuple_vars));
137
    }
138
    return !glyph_variations.in_error () && glyph_variations.length == plan->new_to_old_gid_list.length;
139
  }
140
141
  bool instantiate (const hb_subset_plan_t *plan)
142
  {
143
    unsigned count = plan->new_to_old_gid_list.length;
144
    bool iup_optimize = false;
145
    optimize_scratch_t scratch;
146
    iup_optimize = plan->flags & HB_SUBSET_FLAGS_OPTIMIZE_IUP_DELTAS;
147
    for (unsigned i = 0; i < count; i++)
148
    {
149
      hb_codepoint_t new_gid = plan->new_to_old_gid_list[i].first;
150
      contour_point_vector_t *all_points;
151
      if (!plan->new_gid_contour_points_map.has (new_gid, &all_points))
152
        return false;
153
      if (!glyph_variations[i].instantiate (plan->axes_location, plan->axes_triple_distances, scratch, &pool, all_points, iup_optimize))
154
        return false;
155
    }
156
    return true;
157
  }
158
159
  bool compile_bytes (const hb_map_t& axes_index_map,
160
                      const hb_map_t& axes_old_index_tag_map)
161
  {
162
    if (!compile_shared_tuples (axes_index_map, axes_old_index_tag_map))
163
      return false;
164
    for (tuple_variations_t& vars: glyph_variations)
165
      if (!vars.compile_bytes (axes_index_map, axes_old_index_tag_map,
166
                               true, /* use shared points*/
167
                               true,
168
                               &shared_tuples_idx_map,
169
             &pool))
170
        return false;
171
172
    return true;
173
  }
174
175
  bool compile_shared_tuples (const hb_map_t& axes_index_map,
176
                              const hb_map_t& axes_old_index_tag_map)
177
  {
178
    /* key is pointer to compiled_peak_coords inside each tuple, hashing
179
     * function will always deref pointers first */
180
    hb_hashmap_t<const hb_vector_t<F2DOT14>*, unsigned> coords_count_map;
181
182
    /* count the num of shared coords */
183
    for (tuple_variations_t& vars: glyph_variations)
184
    {
185
      for (tuple_delta_t& var : vars.tuple_vars)
186
      {
187
        if (!var.compile_coords (axes_index_map, axes_old_index_tag_map, &pool))
188
          return false;
189
        unsigned *count;
190
  unsigned hash = hb_hash (&var.compiled_peak_coords);
191
        if (coords_count_map.has_with_hash (&(var.compiled_peak_coords), hash, &count))
192
    (*count)++;
193
        else
194
          coords_count_map.set_with_hash (&(var.compiled_peak_coords), hash, 1);
195
      }
196
    }
197
198
    if (!coords_count_map || coords_count_map.in_error ())
199
      return false;
200
201
    /* add only those coords that are used more than once into the vector and sort */
202
    hb_vector_t<hb_pair_t<const hb_vector_t<F2DOT14>*, unsigned>> shared_coords {
203
      + hb_iter (coords_count_map)
204
      | hb_filter ([] (const hb_pair_t<const hb_vector_t<F2DOT14>*, unsigned>& p) { return p.second > 1; })
205
    };
206
    if (unlikely (shared_coords.in_error ())) return false;
207
208
    /* no shared tuples: no coords are used more than once */
209
    if (!shared_coords) return true;
210
    /* sorting based on the coords frequency first (high to low), then compare
211
     * the coords bytes */
212
    shared_coords.qsort (_cmp_coords);
213
214
    /* build shared_coords->idx map and shared tuples byte array */
215
216
    shared_tuples_count = hb_min (0xFFFu + 1, shared_coords.length);
217
    unsigned len = shared_tuples_count * (shared_coords[0].first->length);
218
    if (unlikely (!compiled_shared_tuples.alloc (len)))
219
      return false;
220
221
    for (unsigned i = 0; i < shared_tuples_count; i++)
222
    {
223
      shared_tuples_idx_map.set (shared_coords[i].first, i);
224
      /* add a concat() in hb_vector_t? */
225
      for (auto c : shared_coords[i].first->iter ())
226
        compiled_shared_tuples.push (c);
227
    }
228
229
    return true;
230
  }
231
232
  static int _cmp_coords (const void *pa, const void *pb)
233
  {
234
    const hb_pair_t<hb_vector_t<F2DOT14> *, unsigned> *a = (const hb_pair_t<hb_vector_t<F2DOT14> *, unsigned> *) pa;
235
    const hb_pair_t<hb_vector_t<F2DOT14> *, unsigned> *b = (const hb_pair_t<hb_vector_t<F2DOT14> *, unsigned> *) pb;
236
237
    if (a->second != b->second)
238
      return b->second - a->second; // high to low
239
240
    return b->first->as_array().cmp (a->first->as_array ());
241
  }
242
243
  template<typename Iterator,
244
           hb_requires (hb_is_iterator (Iterator))>
245
  bool serialize_glyph_var_data (hb_serialize_context_t *c,
246
                                 Iterator it,
247
                                 bool long_offset,
248
                                 unsigned num_glyphs,
249
                                 char* glyph_var_data_offsets /* OUT: glyph var data offsets array */) const
250
  {
251
    TRACE_SERIALIZE (this);
252
253
    if (long_offset)
254
    {
255
      ((HBUINT32 *) glyph_var_data_offsets)[0] = 0;
256
      glyph_var_data_offsets += 4;
257
    }
258
    else
259
    {
260
      ((HBUINT16 *) glyph_var_data_offsets)[0] = 0;
261
      glyph_var_data_offsets += 2;
262
    }
263
    unsigned glyph_offset = 0;
264
    hb_codepoint_t last_gid = 0;
265
    unsigned idx = 0;
266
267
    GlyphVariationData* cur_glyph = c->start_embed<GlyphVariationData> ();
268
    if (!cur_glyph) return_trace (false);
269
    for (auto &_ : it)
270
    {
271
      hb_codepoint_t gid = _.first;
272
      if (long_offset)
273
        for (; last_gid < gid; last_gid++)
274
          ((HBUINT32 *) glyph_var_data_offsets)[last_gid] = glyph_offset;
275
      else
276
        for (; last_gid < gid; last_gid++)
277
          ((HBUINT16 *) glyph_var_data_offsets)[last_gid] = glyph_offset / 2;
278
279
      if (idx >= glyph_variations.length) return_trace (false);
280
      if (!cur_glyph->serialize (c, true, glyph_variations[idx])) return_trace (false);
281
      GlyphVariationData* next_glyph = c->start_embed<GlyphVariationData> ();
282
      glyph_offset += (char *) next_glyph - (char *) cur_glyph;
283
284
      if (long_offset)
285
        ((HBUINT32 *) glyph_var_data_offsets)[gid] = glyph_offset;
286
      else
287
        ((HBUINT16 *) glyph_var_data_offsets)[gid] = glyph_offset / 2;
288
289
      last_gid++;
290
      idx++;
291
      cur_glyph = next_glyph;
292
    }
293
294
    if (long_offset)
295
      for (; last_gid < num_glyphs; last_gid++)
296
        ((HBUINT32 *) glyph_var_data_offsets)[last_gid] = glyph_offset;
297
    else
298
      for (; last_gid < num_glyphs; last_gid++)
299
        ((HBUINT16 *) glyph_var_data_offsets)[last_gid] = glyph_offset / 2;
300
    return_trace (true);
301
  }
302
};
303
304
template <typename GidOffsetType, unsigned TableTag>
305
struct gvar_GVAR
306
{
307
  static constexpr hb_tag_t tableTag = TableTag;
308
309
  using GlyphVariationData = TupleVariationData<GidOffsetType>;
310
311
0
  bool has_data () const { return version.to_int () != 0; }
312
313
  bool sanitize_shallow (hb_sanitize_context_t *c) const
314
0
  {
315
0
    TRACE_SANITIZE (this);
316
0
    return_trace (c->check_struct (this) &&
317
0
      hb_barrier () &&
318
0
      (version.major == 1) &&
319
0
      sharedTuples.sanitize (c, this, axisCount * sharedTupleCount) &&
320
0
      (is_long_offset () ?
321
0
         c->check_array (get_long_offset_array (), c->get_num_glyphs () + 1) :
322
0
         c->check_array (get_short_offset_array (), c->get_num_glyphs () + 1)));
323
0
  }
Unexecuted instantiation: OT::gvar_GVAR<OT::NumType<true, unsigned short, 2u>, 1735811442u>::sanitize_shallow(hb_sanitize_context_t*) const
Unexecuted instantiation: OT::gvar_GVAR<OT::NumType<true, unsigned int, 3u>, 1196835154u>::sanitize_shallow(hb_sanitize_context_t*) const
324
325
  /* GlyphVariationData not sanitized here; must be checked while accessing each glyph variation data */
326
  bool sanitize (hb_sanitize_context_t *c) const
327
0
  { return sanitize_shallow (c); }
Unexecuted instantiation: OT::gvar_GVAR<OT::NumType<true, unsigned short, 2u>, 1735811442u>::sanitize(hb_sanitize_context_t*) const
Unexecuted instantiation: OT::gvar_GVAR<OT::NumType<true, unsigned int, 3u>, 1196835154u>::sanitize(hb_sanitize_context_t*) const
328
329
  bool decompile_glyph_variations (hb_subset_context_t *c,
330
                                   glyph_variations_t<GidOffsetType>& glyph_vars /* OUT */) const
331
  {
332
    hb_hashmap_t<hb_codepoint_t, hb_bytes_t> new_gid_var_data_map;
333
    auto it = hb_iter (c->plan->new_to_old_gid_list);
334
    if (it->first == 0 && !(c->plan->flags & HB_SUBSET_FLAGS_NOTDEF_OUTLINE))
335
    {
336
      new_gid_var_data_map.set (0, hb_bytes_t ());
337
      it++;
338
    }
339
340
    for (auto &_ : it)
341
    {
342
      hb_codepoint_t new_gid = _.first;
343
      hb_codepoint_t old_gid = _.second;
344
      hb_bytes_t var_data_bytes = get_glyph_var_data_bytes (c->source_blob, glyphCountX, old_gid);
345
      new_gid_var_data_map.set (new_gid, var_data_bytes);
346
    }
347
348
    if (new_gid_var_data_map.in_error ()) return false;
349
350
    hb_array_t<const F2DOT14> shared_tuples = (this+sharedTuples).as_array ((unsigned) sharedTupleCount * (unsigned) axisCount);
351
    return glyph_vars.create_from_glyphs_var_data (axisCount, shared_tuples, c->plan, new_gid_var_data_map);
352
  }
353
354
  template<typename Iterator,
355
           hb_requires (hb_is_iterator (Iterator))>
356
  bool serialize (hb_serialize_context_t *c,
357
                  const glyph_variations_t<GidOffsetType>& glyph_vars,
358
                  Iterator it,
359
                  unsigned axis_count,
360
                  unsigned num_glyphs,
361
                  bool force_long_offsets) const
362
  {
363
    TRACE_SERIALIZE (this);
364
    gvar_GVAR *out = c->allocate_min<gvar_GVAR> ();
365
    if (unlikely (!out)) return_trace (false);
366
367
    out->version.major = 1;
368
    out->version.minor = 0;
369
    out->axisCount = axis_count;
370
    out->glyphCountX = hb_min (0xFFFFu, num_glyphs);
371
372
    unsigned glyph_var_data_size = glyph_vars.compiled_byte_size ();
373
    /* According to the spec: If the short format (Offset16) is used for offsets,
374
     * the value stored is the offset divided by 2, so the maximum data size should
375
     * be 2 * 0xFFFFu, which is 0x1FFFEu */
376
    bool long_offset = glyph_var_data_size > 0x1FFFEu || force_long_offsets;
377
    out->flags = long_offset ? 1 : 0;
378
379
    HBUINT8 *glyph_var_data_offsets = c->allocate_size<HBUINT8> ((long_offset ? 4 : 2) * (num_glyphs + 1), false);
380
    if (!glyph_var_data_offsets) return_trace (false);
381
382
    /* shared tuples */
383
    unsigned shared_tuple_count = glyph_vars.compiled_shared_tuples_count ();
384
    out->sharedTupleCount = shared_tuple_count;
385
386
    if (!shared_tuple_count)
387
      out->sharedTuples = 0;
388
    else
389
    {
390
      hb_array_t<const F2DOT14> shared_tuples = glyph_vars.compiled_shared_tuples.as_array ().copy (c);
391
      if (!shared_tuples.arrayZ) return_trace (false);
392
      out->sharedTuples = (const char *) shared_tuples.arrayZ - (char *) out;
393
    }
394
395
    char *glyph_var_data = c->start_embed<char> ();
396
    if (!glyph_var_data) return_trace (false);
397
    out->dataZ = glyph_var_data - (char *) out;
398
399
    return_trace (glyph_vars.serialize_glyph_var_data (c, it, long_offset, num_glyphs,
400
                                                       (char *) glyph_var_data_offsets));
401
  }
402
403
  bool instantiate (hb_subset_context_t *c) const
404
  {
405
    TRACE_SUBSET (this);
406
    glyph_variations_t<GidOffsetType> glyph_vars;
407
    if (!decompile_glyph_variations (c, glyph_vars))
408
      return_trace (false);
409
410
    if (!glyph_vars.instantiate (c->plan)) return_trace (false);
411
    if (!glyph_vars.compile_bytes (c->plan->axes_index_map, c->plan->axes_old_index_tag_map))
412
      return_trace (false);
413
414
    unsigned axis_count = c->plan->axes_index_map.get_population ();
415
    unsigned num_glyphs = c->plan->num_output_glyphs ();
416
    auto it = hb_iter (c->plan->new_to_old_gid_list);
417
418
    bool force_long_offsets = false;
419
#ifdef HB_EXPERIMENTAL_API
420
    force_long_offsets = c->plan->flags & HB_SUBSET_FLAGS_IFTB_REQUIREMENTS;
421
#endif
422
    return_trace (serialize (c->serializer, glyph_vars, it, axis_count, num_glyphs, force_long_offsets));
423
  }
424
425
  bool subset (hb_subset_context_t *c) const
426
  {
427
    TRACE_SUBSET (this);
428
    if (c->plan->all_axes_pinned)
429
      return_trace (false);
430
431
    if (c->plan->normalized_coords)
432
      return_trace (instantiate (c));
433
434
    unsigned glyph_count = version.to_int () ? c->plan->source->get_num_glyphs () : 0;
435
436
    gvar_GVAR *out = c->serializer->allocate_min<gvar_GVAR> ();
437
    if (unlikely (!out)) return_trace (false);
438
439
    out->version.major = 1;
440
    out->version.minor = 0;
441
    out->axisCount = axisCount;
442
    out->sharedTupleCount = sharedTupleCount;
443
444
    unsigned int num_glyphs = c->plan->num_output_glyphs ();
445
    out->glyphCountX = hb_min (0xFFFFu, num_glyphs);
446
447
    auto it = hb_iter (c->plan->new_to_old_gid_list);
448
    if (it->first == 0 && !(c->plan->flags & HB_SUBSET_FLAGS_NOTDEF_OUTLINE))
449
      it++;
450
    unsigned subset_data_size = 0;
451
    unsigned padding_size = 0;
452
    for (auto &_ : it)
453
    {
454
      hb_codepoint_t old_gid = _.second;
455
      unsigned glyph_data_size = get_glyph_var_data_bytes (c->source_blob, glyph_count, old_gid).length;
456
      if (glyph_data_size % 2)
457
      {
458
        glyph_data_size++;
459
        padding_size++;
460
      }
461
462
      if (unlikely (hb_unsigned_add_overflows (subset_data_size, glyph_data_size, &subset_data_size)))
463
  return_trace (false);
464
    }
465
466
    /* According to the spec: If the short format (Offset16) is used for offsets,
467
     * the value stored is the offset divided by 2, so the maximum data size should
468
     * be 2 * 0xFFFFu, which is 0x1FFFEu */
469
    bool long_offset = subset_data_size > 0x1FFFEu;
470
#ifdef HB_EXPERIMENTAL_API
471
    long_offset = long_offset || (c->plan->flags & HB_SUBSET_FLAGS_IFTB_REQUIREMENTS);
472
#endif
473
    out->flags = long_offset ? 1 : 0;
474
475
    HBUINT8 *subset_offsets = c->serializer->allocate_size<HBUINT8> ((long_offset ? 4 : 2) * (num_glyphs + 1), false);
476
    if (!subset_offsets) return_trace (false);
477
478
    /* shared tuples */
479
    if (!sharedTupleCount || !sharedTuples)
480
      out->sharedTuples = 0;
481
    else
482
    {
483
      unsigned int shared_tuple_size = F2DOT14::static_size * axisCount * sharedTupleCount;
484
      F2DOT14 *tuples = c->serializer->allocate_size<F2DOT14> (shared_tuple_size);
485
      if (!tuples) return_trace (false);
486
      out->sharedTuples = (char *) tuples - (char *) out;
487
      hb_memcpy (tuples, this+sharedTuples, shared_tuple_size);
488
    }
489
490
    /* This ordering relative to the shared tuples array, which puts the glyphVariationData
491
       last in the table, is required when HB_SUBSET_FLAGS_IFTB_REQUIREMENTS is set */
492
    if (long_offset)
493
      subset_data_size -= padding_size;
494
    char *subset_data = c->serializer->allocate_size<char> (subset_data_size, false);
495
    if (!subset_data) return_trace (false);
496
    out->dataZ = subset_data - (char *) out;
497
498
499
    if (long_offset)
500
    {
501
      ((HBUINT32 *) subset_offsets)[0] = 0;
502
      subset_offsets += 4;
503
    }
504
    else
505
    {
506
      ((HBUINT16 *) subset_offsets)[0] = 0;
507
      subset_offsets += 2;
508
    }
509
    unsigned int glyph_offset = 0;
510
511
    hb_codepoint_t last = 0;
512
    it = hb_iter (c->plan->new_to_old_gid_list);
513
    if (it->first == 0 && !(c->plan->flags & HB_SUBSET_FLAGS_NOTDEF_OUTLINE))
514
      it++;
515
    for (auto &_ : it)
516
    {
517
      hb_codepoint_t gid = _.first;
518
      hb_codepoint_t old_gid = _.second;
519
520
      if (long_offset)
521
  for (; last < gid; last++)
522
    ((HBUINT32 *) subset_offsets)[last] = glyph_offset;
523
      else
524
  for (; last < gid; last++)
525
    ((HBUINT16 *) subset_offsets)[last] = glyph_offset / 2;
526
527
      hb_bytes_t var_data_bytes = get_glyph_var_data_bytes (c->source_blob,
528
                  glyph_count,
529
                  old_gid);
530
531
      hb_memcpy (subset_data, var_data_bytes.arrayZ, var_data_bytes.length);
532
      unsigned glyph_data_size = var_data_bytes.length;
533
      subset_data += glyph_data_size;
534
      glyph_offset += glyph_data_size;
535
536
      if (!long_offset && (glyph_data_size % 2))
537
      {
538
        *subset_data = 0;
539
        subset_data++;
540
        glyph_offset++;
541
      }
542
543
      if (long_offset)
544
  ((HBUINT32 *) subset_offsets)[gid] = glyph_offset;
545
      else
546
  ((HBUINT16 *) subset_offsets)[gid] = glyph_offset / 2;
547
548
      last++; // Skip over gid
549
    }
550
551
    if (long_offset)
552
      for (; last < num_glyphs; last++)
553
  ((HBUINT32 *) subset_offsets)[last] = glyph_offset;
554
    else
555
      for (; last < num_glyphs; last++)
556
  ((HBUINT16 *) subset_offsets)[last] = glyph_offset / 2;
557
558
    return_trace (true);
559
  }
560
561
  protected:
562
  const hb_bytes_t get_glyph_var_data_bytes (hb_blob_t *blob,
563
               unsigned glyph_count,
564
               hb_codepoint_t glyph) const
565
0
  {
566
0
    unsigned start_offset = get_offset (glyph_count, glyph);
567
0
    unsigned end_offset = get_offset (glyph_count, glyph+1);
568
0
    if (unlikely (end_offset < start_offset)) return hb_bytes_t ();
569
0
    unsigned length = end_offset - start_offset;
570
0
    hb_bytes_t var_data = blob->as_bytes ().sub_array (((unsigned) dataZ) + start_offset, length);
571
0
    return likely (var_data.length >= GlyphVariationData::min_size) ? var_data : hb_bytes_t ();
572
0
  }
573
574
0
  bool is_long_offset () const { return flags & 1; }
Unexecuted instantiation: OT::gvar_GVAR<OT::NumType<true, unsigned short, 2u>, 1735811442u>::is_long_offset() const
Unexecuted instantiation: OT::gvar_GVAR<OT::NumType<true, unsigned int, 3u>, 1196835154u>::is_long_offset() const
575
576
  unsigned get_offset (unsigned glyph_count, unsigned i) const
577
0
  {
578
0
    if (unlikely (i > glyph_count)) return 0;
579
0
    hb_barrier ();
580
0
    return is_long_offset () ? get_long_offset_array ()[i] : get_short_offset_array ()[i] * 2;
581
0
  }
582
583
0
  const HBUINT32 * get_long_offset_array () const { return (const HBUINT32 *) &offsetZ; }
Unexecuted instantiation: OT::gvar_GVAR<OT::NumType<true, unsigned short, 2u>, 1735811442u>::get_long_offset_array() const
Unexecuted instantiation: OT::gvar_GVAR<OT::NumType<true, unsigned int, 3u>, 1196835154u>::get_long_offset_array() const
584
0
  const HBUINT16 *get_short_offset_array () const { return (const HBUINT16 *) &offsetZ; }
Unexecuted instantiation: OT::gvar_GVAR<OT::NumType<true, unsigned short, 2u>, 1735811442u>::get_short_offset_array() const
Unexecuted instantiation: OT::gvar_GVAR<OT::NumType<true, unsigned int, 3u>, 1196835154u>::get_short_offset_array() const
585
586
  public:
587
  struct accelerator_t
588
  {
589
590
    hb_scalar_cache_t *create_cache () const
591
0
    {
592
0
      return hb_scalar_cache_t::create (table->sharedTupleCount);
593
0
    }
594
595
    static void destroy_cache (hb_scalar_cache_t *cache)
596
0
    {
597
0
      hb_scalar_cache_t::destroy (cache);
598
0
    }
599
600
0
    bool has_data () const { return table->has_data (); }
601
602
    accelerator_t (hb_face_t *face)
603
0
    {
604
0
      table = hb_sanitize_context_t ().reference_table<gvar_GVAR> (face);
605
      /* If sanitize failed, set glyphCount to 0. */
606
0
      glyphCount = table->version.to_int () ? face->get_num_glyphs () : 0;
607
0
    }
608
0
    ~accelerator_t () { table.destroy (); }
609
610
    private:
611
612
    static float infer_delta (const hb_array_t<contour_point_t> points,
613
            const hb_array_t<contour_point_t> deltas,
614
            unsigned int target, unsigned int prev, unsigned int next,
615
            float contour_point_t::*m)
616
0
    {
617
0
      float target_val = points.arrayZ[target].*m;
618
0
      float prev_val = points.arrayZ[prev].*m;
619
0
      float next_val = points.arrayZ[next].*m;
620
0
      float prev_delta =  deltas.arrayZ[prev].*m;
621
0
      float next_delta =  deltas.arrayZ[next].*m;
622
623
0
      if (prev_val == next_val)
624
0
  return (prev_delta == next_delta) ? prev_delta : 0.f;
625
0
      else if (target_val <= hb_min (prev_val, next_val))
626
0
  return (prev_val < next_val) ? prev_delta : next_delta;
627
0
      else if (target_val >= hb_max (prev_val, next_val))
628
0
  return (prev_val > next_val) ? prev_delta : next_delta;
629
630
      /* linear interpolation */
631
0
      float r = (target_val - prev_val) / (next_val - prev_val);
632
0
      return prev_delta + r * (next_delta - prev_delta);
633
0
    }
634
635
    static unsigned int next_index (unsigned int i, unsigned int start, unsigned int end)
636
0
    { return (i >= end) ? start : (i + 1); }
637
638
#ifndef HB_OPTIMIZE_SIZE
639
    template <bool is_x>
640
#endif
641
    static bool decompile_deltas_add_to_points (const HBUINT8 *&p /* IN/OUT */,
642
            hb_array_t<contour_point_t> points,
643
            float scalar,
644
            const HBUINT8 *end,
645
            unsigned start
646
#ifdef HB_OPTIMIZE_SIZE
647
            , bool is_x
648
#endif
649
            )
650
0
    {
651
0
      unsigned i = 0;
652
0
      unsigned count = points.length;
653
0
      while (i < count)
654
0
      {
655
0
  if (unlikely (p + 1 > end)) return false;
656
0
  unsigned control = *p++;
657
0
  unsigned run_count = (control & TupleValues::VALUE_RUN_COUNT_MASK) + 1;
658
0
  unsigned stop = i + run_count;
659
0
  if (unlikely (stop > count)) return false;
660
661
0
  unsigned skip = i < start ? hb_min (start - i, run_count) : 0;
662
0
  i += skip;
663
664
0
  switch (control & TupleValues::VALUES_SIZE_MASK)
665
0
  {
666
0
    case TupleValues::VALUES_ARE_ZEROS:
667
0
      i = stop;
668
0
      break;
669
0
    case TupleValues::VALUES_ARE_WORDS:
670
0
    {
671
0
      if (unlikely (p + run_count * HBINT16::static_size > end)) return false;
672
0
      p += skip * HBINT16::static_size;
673
0
      const auto *pp = (const HBINT16 *) p;
674
0
      for (; i < stop; i++)
675
0
      {
676
0
        float v = *pp++ * scalar;
677
0
        if (is_x) points.arrayZ[i].x += v;
678
0
        else points.arrayZ[i].y += v;
679
0
      }
680
0
      p = (const HBUINT8 *) pp;
681
0
    }
682
0
    break;
683
0
    case TupleValues::VALUES_ARE_LONGS:
684
0
    {
685
0
      if (unlikely (p + run_count * HBINT32::static_size > end)) return false;
686
0
      p += skip * HBINT32::static_size;
687
0
      const auto *pp = (const HBINT32 *) p;
688
0
      for (; i < stop; i++)
689
0
      {
690
0
        float v = *pp++ * scalar;
691
0
        if (is_x) points.arrayZ[i].x += v;
692
0
        else points.arrayZ[i].y += v;
693
0
      }
694
0
      p = (const HBUINT8 *) pp;
695
0
    }
696
0
    break;
697
0
    case TupleValues::VALUES_ARE_BYTES:
698
0
    {
699
0
      if (unlikely (p + run_count > end)) return false;
700
0
      p += skip * HBINT8::static_size;
701
0
      const auto *pp = (const HBINT8 *) p;
702
0
      for (; i < stop; i++)
703
0
      {
704
0
        float v = *pp++ * scalar;
705
0
        if (is_x) points.arrayZ[i].x += v;
706
0
        else points.arrayZ[i].y += v;
707
0
      }
708
0
      p = (const HBUINT8 *) pp;
709
0
    }
710
0
    break;
711
0
  }
712
0
      }
713
0
      return true;
714
0
    }
Unexecuted instantiation: bool OT::gvar_GVAR<OT::NumType<true, unsigned short, 2u>, 1735811442u>::accelerator_t::decompile_deltas_add_to_points<true>(OT::NumType<true, unsigned char, 1u> const*&, hb_array_t<contour_point_t>, float, OT::NumType<true, unsigned char, 1u> const*, unsigned int)
Unexecuted instantiation: bool OT::gvar_GVAR<OT::NumType<true, unsigned short, 2u>, 1735811442u>::accelerator_t::decompile_deltas_add_to_points<false>(OT::NumType<true, unsigned char, 1u> const*&, hb_array_t<contour_point_t>, float, OT::NumType<true, unsigned char, 1u> const*, unsigned int)
715
    public:
716
    bool apply_deltas_to_points (hb_codepoint_t glyph,
717
         hb_array_t<const int> coords,
718
         const hb_array_t<contour_point_t> points,
719
         hb_glyf_scratch_t &scratch,
720
         hb_scalar_cache_t *gvar_cache = nullptr,
721
         bool phantom_only = false) const
722
0
    {
723
0
      if (unlikely (glyph >= glyphCount)) return true;
724
725
0
      hb_bytes_t var_data_bytes = table->get_glyph_var_data_bytes (table.get_blob (), glyphCount, glyph);
726
0
      if (!var_data_bytes.as<GlyphVariationData> ()->has_data ()) return true;
727
728
0
      auto &shared_indices = scratch.shared_indices;
729
0
      shared_indices.clear ();
730
731
0
      typename GlyphVariationData::tuple_iterator_t iterator;
732
0
      if (!GlyphVariationData::get_tuple_iterator (var_data_bytes, table->axisCount,
733
0
               var_data_bytes.arrayZ,
734
0
               shared_indices, &iterator))
735
0
  return true; /* so isn't applied at all */
736
737
0
      bool any_private_points = false;
738
0
      bool private_points_checked = false;
739
740
      /* Save original points for inferred delta calculation */
741
0
      auto &orig_points_vec = scratch.orig_points;
742
0
      orig_points_vec.clear (); // Populated lazily
743
0
      auto orig_points = orig_points_vec.as_array ();
744
745
      /* flag is used to indicate referenced point */
746
0
      auto &deltas_vec = scratch.deltas;
747
0
      deltas_vec.clear (); // Populated lazily
748
0
      auto deltas = deltas_vec.as_array ();
749
750
0
      unsigned num_coords = table->axisCount;
751
0
      hb_array_t<const F2DOT14> shared_tuples = (table+table->sharedTuples).as_array (table->sharedTupleCount * num_coords);
752
753
0
      auto &private_indices = scratch.private_indices;
754
0
      auto &x_deltas = scratch.x_deltas;
755
0
      auto &y_deltas = scratch.y_deltas;
756
757
0
      unsigned count = points.length;
758
0
      bool flush = false;
759
760
0
      do
761
0
      {
762
0
  float scalar = iterator.current_tuple->calculate_scalar (coords, num_coords, shared_tuples,
763
0
                 gvar_cache);
764
765
0
  if (scalar == 0.f) continue;
766
767
0
  if (!private_points_checked)
768
0
  {
769
0
    auto scan = iterator;
770
0
    do
771
0
    {
772
0
      if (scan.current_tuple->has_private_points ())
773
0
      {
774
0
        any_private_points = true;
775
0
        break;
776
0
      }
777
0
    } while (scan.move_to_next ());
778
0
    private_points_checked = true;
779
0
  }
780
0
  const HBUINT8 *p = iterator.get_serialized_data ();
781
0
  unsigned int length = iterator.current_tuple->get_data_size ();
782
0
  if (unlikely (!iterator.var_data_bytes.check_range (p, length)))
783
0
    return false;
784
785
0
  if (!deltas)
786
0
  {
787
0
    if (unlikely (!deltas_vec.resize_dirty  (count))) return false;
788
0
    deltas = deltas_vec.as_array ();
789
0
    hb_memset (deltas.arrayZ + (phantom_only ? count - 4 : 0), 0,
790
0
         (phantom_only ? 4 : count) * sizeof (deltas[0]));
791
0
  }
792
793
0
  const HBUINT8 *end = p + length;
794
795
0
  bool has_private_points = iterator.current_tuple->has_private_points ();
796
0
  if (has_private_points &&
797
0
      !GlyphVariationData::decompile_points (p, private_indices, end))
798
0
    return false;
799
0
  const hb_array_t<unsigned int> &indices = has_private_points ? private_indices : shared_indices;
800
801
0
  bool apply_to_all = (indices.length == 0);
802
0
  unsigned num_deltas = apply_to_all ? points.length : indices.length;
803
0
  unsigned start_deltas = (apply_to_all && phantom_only && num_deltas >= 4 ? num_deltas - 4 : 0);
804
805
0
  if (apply_to_all && !any_private_points)
806
0
  {
807
#ifdef HB_OPTIMIZE_SIZE
808
    if (unlikely (!decompile_deltas_add_to_points (p, points, scalar, end, start_deltas, true))) return false;
809
    if (unlikely (!decompile_deltas_add_to_points (p, points, scalar, end, start_deltas, false))) return false;
810
#else
811
0
    if (unlikely (!decompile_deltas_add_to_points<true> (p, points, scalar, end, start_deltas))) return false;
812
0
    if (unlikely (!decompile_deltas_add_to_points<false> (p, points, scalar, end, start_deltas))) return false;
813
0
#endif
814
0
    continue;
815
0
  }
816
817
0
  if (unlikely (!x_deltas.resize_dirty  (num_deltas))) return false;
818
0
  if (unlikely (!GlyphVariationData::decompile_deltas (p, x_deltas, end, false, start_deltas))) return false;
819
0
  if (unlikely (!y_deltas.resize_dirty  (num_deltas))) return false;
820
0
  if (unlikely (!GlyphVariationData::decompile_deltas (p, y_deltas, end, false, start_deltas))) return false;
821
822
0
  if (!apply_to_all)
823
0
  {
824
0
    if (!orig_points && !phantom_only)
825
0
    {
826
0
      orig_points_vec.extend (points);
827
0
      if (unlikely (orig_points_vec.in_error ())) return false;
828
0
      orig_points = orig_points_vec.as_array ();
829
0
    }
830
831
0
    if (flush)
832
0
    {
833
0
      for (unsigned int i = phantom_only ? count - 4 : 0; i < count; i++)
834
0
        points.arrayZ[i].translate (deltas.arrayZ[i]);
835
0
    }
836
0
    hb_memset (deltas.arrayZ + (phantom_only ? count - 4 : 0), 0,
837
0
         (phantom_only ? 4 : count) * sizeof (deltas[0]));
838
0
  }
839
840
0
  if (HB_OPTIMIZE_SIZE_VAL)
841
0
  {
842
0
    for (unsigned int i = 0; i < num_deltas; i++)
843
0
    {
844
0
      unsigned int pt_index;
845
0
      if (apply_to_all)
846
0
        pt_index = i;
847
0
      else
848
0
      {
849
0
        pt_index = indices[i];
850
0
        if (unlikely (pt_index >= deltas.length)) continue;
851
0
      }
852
0
      if (phantom_only && pt_index < count - 4) continue;
853
0
      auto &delta = deltas.arrayZ[pt_index];
854
0
      delta.flag = 1; /* this point is referenced, i.e., explicit deltas specified */
855
0
      delta.add_delta (x_deltas.arrayZ[i] * scalar,
856
0
           y_deltas.arrayZ[i] * scalar);
857
0
    }
858
0
  }
859
0
  else
860
0
  {
861
    /* Ouch. Four cases... for optimization. */
862
0
    if (scalar != 1.0f)
863
0
    {
864
0
      if (apply_to_all)
865
0
        for (unsigned int i = phantom_only ? count - 4 : 0; i < count; i++)
866
0
        {
867
0
    auto &delta = deltas.arrayZ[i];
868
0
    delta.add_delta (x_deltas.arrayZ[i] * scalar,
869
0
         y_deltas.arrayZ[i] * scalar);
870
0
        }
871
0
      else
872
0
        for (unsigned int i = 0; i < num_deltas; i++)
873
0
        {
874
0
    unsigned int pt_index = indices[i];
875
0
    if (unlikely (pt_index >= deltas.length)) continue;
876
0
    if (phantom_only && pt_index < count - 4) continue;
877
0
    auto &delta = deltas.arrayZ[pt_index];
878
0
    delta.flag = 1; /* this point is referenced, i.e., explicit deltas specified */
879
0
    delta.add_delta (x_deltas.arrayZ[i] * scalar,
880
0
         y_deltas.arrayZ[i] * scalar);
881
0
        }
882
0
    }
883
0
    else
884
0
    {
885
0
      if (apply_to_all)
886
0
        for (unsigned int i = phantom_only ? count - 4 : 0; i < count; i++)
887
0
        {
888
0
    auto &delta = deltas.arrayZ[i];
889
0
    delta.add_delta (x_deltas.arrayZ[i],
890
0
         y_deltas.arrayZ[i]);
891
0
        }
892
0
      else
893
0
        for (unsigned int i = 0; i < num_deltas; i++)
894
0
        {
895
0
    unsigned int pt_index = indices[i];
896
0
    if (unlikely (pt_index >= deltas.length)) continue;
897
0
    if (phantom_only && pt_index < count - 4) continue;
898
0
    auto &delta = deltas.arrayZ[pt_index];
899
0
    delta.flag = 1; /* this point is referenced, i.e., explicit deltas specified */
900
0
    delta.add_delta (x_deltas.arrayZ[i],
901
0
         y_deltas.arrayZ[i]);
902
0
        }
903
0
    }
904
0
  }
905
906
  /* infer deltas for unreferenced points */
907
0
  if (!apply_to_all && !phantom_only)
908
0
  {
909
0
    unsigned start_point = 0;
910
0
    unsigned end_point = 0;
911
0
    while (true)
912
0
    {
913
0
      while (end_point < count && !points.arrayZ[end_point].is_end_point)
914
0
        end_point++;
915
0
      if (unlikely (end_point == count)) break;
916
917
      /* Check the number of unreferenced points in a contour. If no unref points or no ref points, nothing to do. */
918
0
      unsigned unref_count = 0;
919
0
      for (unsigned i = start_point; i < end_point + 1; i++)
920
0
        unref_count += deltas.arrayZ[i].flag;
921
0
      unref_count = (end_point - start_point + 1) - unref_count;
922
923
0
      unsigned j = start_point;
924
0
      if (unref_count == 0 || unref_count > end_point - start_point)
925
0
        goto no_more_gaps;
926
927
0
      for (;;)
928
0
      {
929
        /* Locate the next gap of unreferenced points between two referenced points prev and next.
930
         * Note that a gap may wrap around at left (start_point) and/or at right (end_point).
931
         */
932
0
        unsigned int prev, next, i;
933
0
        for (;;)
934
0
        {
935
0
    i = j;
936
0
    j = next_index (i, start_point, end_point);
937
0
    if (deltas.arrayZ[i].flag && !deltas.arrayZ[j].flag) break;
938
0
        }
939
0
        prev = j = i;
940
0
        for (;;)
941
0
        {
942
0
    i = j;
943
0
    j = next_index (i, start_point, end_point);
944
0
    if (!deltas.arrayZ[i].flag && deltas.arrayZ[j].flag) break;
945
0
        }
946
0
        next = j;
947
        /* Infer deltas for all unref points in the gap between prev and next */
948
0
        i = prev;
949
0
        for (;;)
950
0
        {
951
0
    i = next_index (i, start_point, end_point);
952
0
    if (i == next) break;
953
0
    deltas.arrayZ[i].x = infer_delta (orig_points, deltas, i, prev, next, &contour_point_t::x);
954
0
    deltas.arrayZ[i].y = infer_delta (orig_points, deltas, i, prev, next, &contour_point_t::y);
955
0
    if (--unref_count == 0) goto no_more_gaps;
956
0
        }
957
0
      }
958
0
    no_more_gaps:
959
0
      start_point = end_point = end_point + 1;
960
0
    }
961
0
  }
962
963
0
  flush = true;
964
965
0
      } while (iterator.move_to_next ());
966
967
0
      if (flush)
968
0
      {
969
0
  for (unsigned int i = phantom_only ? count - 4 : 0; i < count; i++)
970
0
    points.arrayZ[i].translate (deltas.arrayZ[i]);
971
0
      }
972
973
0
      return true;
974
0
    }
975
976
    unsigned int get_axis_count () const { return table->axisCount; }
977
978
    private:
979
    hb_blob_ptr_t<gvar_GVAR> table;
980
    unsigned glyphCount;
981
  };
982
983
  protected:
984
  FixedVersion<>version;  /* Version number of the glyph variations table
985
         * Set to 0x00010000u. */
986
  HBUINT16  axisCount;  /* The number of variation axes for this font. This must be
987
         * the same number as axisCount in the 'fvar' table. */
988
  HBUINT16  sharedTupleCount;
989
        /* The number of shared tuple records. Shared tuple records
990
         * can be referenced within glyph variation data tables for
991
         * multiple glyphs, as opposed to other tuple records stored
992
         * directly within a glyph variation data table. */
993
  NNOffset32To<UnsizedArrayOf<F2DOT14>>
994
    sharedTuples; /* Offset from the start of this table to the shared tuple records.
995
         * Array of tuple records shared across all glyph variation data tables. */
996
  GidOffsetType glyphCountX;  /* The number of glyphs in this font. This must match the number of
997
         * glyphs stored elsewhere in the font. */
998
  HBUINT16  flags;    /* Bit-field that gives the format of the offset array that follows.
999
         * If bit 0 is clear, the offsets are uint16; if bit 0 is set, the
1000
         * offsets are uint32. */
1001
  Offset32To<GlyphVariationData>
1002
    dataZ;    /* Offset from the start of this table to the array of
1003
         * GlyphVariationData tables. */
1004
  UnsizedArrayOf<HBUINT8>
1005
    offsetZ;  /* Offsets from the start of the GlyphVariationData array
1006
         * to each GlyphVariationData table. */
1007
  public:
1008
  DEFINE_SIZE_ARRAY (20, offsetZ);
1009
};
1010
1011
using gvar = gvar_GVAR<HBUINT16, HB_OT_TAG_gvar>;
1012
using GVAR = gvar_GVAR<HBUINT24, HB_OT_TAG_GVAR>;
1013
1014
struct gvar_accelerator_t : gvar::accelerator_t {
1015
0
  gvar_accelerator_t (hb_face_t *face) : gvar::accelerator_t (face) {}
1016
};
1017
struct GVAR_accelerator_t : GVAR::accelerator_t {
1018
0
  GVAR_accelerator_t (hb_face_t *face) : GVAR::accelerator_t (face) {}
1019
};
1020
1021
} /* namespace OT */
1022
1023
#endif /* HB_OT_VAR_GVAR_TABLE_HH */