Coverage Report

Created: 2023-12-13 20:01

/src/harfbuzz/src/hb-ot-var-gvar-table.hh
Line
Count
Source (jump to first uncovered line)
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-open-type.hh"
32
33
/*
34
 * gvar -- Glyph Variation Table
35
 * https://docs.microsoft.com/en-us/typography/opentype/spec/gvar
36
 */
37
#define HB_OT_TAG_gvar HB_TAG('g','v','a','r')
38
39
namespace OT {
40
41
struct contour_point_t
42
{
43
  void init (float x_ = 0.f, float y_ = 0.f, bool is_end_point_ = false)
44
2.98M
  { flag = 0; x = x_; y = y_; is_end_point = is_end_point_; }
45
46
24.5M
  void translate (const contour_point_t &p) { x += p.x; y += p.y; }
47
48
  float x = 0.f;
49
  float y = 0.f;
50
  uint8_t flag = 0;
51
  bool is_end_point = false;
52
};
53
54
struct contour_point_vector_t : hb_vector_t<contour_point_t>
55
{
56
  void extend (const hb_array_t<contour_point_t> &a)
57
764k
  {
58
764k
    unsigned int old_len = length;
59
764k
    if (unlikely (!resize (old_len + a.length)))
60
2.94k
      return;
61
761k
    auto arrayZ = this->arrayZ + old_len;
62
761k
    unsigned count = a.length;
63
11.6M
    for (unsigned int i = 0; i < count; i++)
64
10.8M
      arrayZ[i] = a.arrayZ[i];
65
761k
  }
66
67
  void transform (const float (&matrix)[4])
68
324k
  {
69
324k
    if (matrix[0] == 1.f && matrix[1] == 0.f &&
70
324k
  matrix[2] == 0.f && matrix[3] == 1.f)
71
18.9k
      return;
72
305k
    auto arrayZ = this->arrayZ;
73
305k
    unsigned count = length;
74
9.04M
    for (unsigned i = 0; i < count; i++)
75
8.73M
    {
76
8.73M
      contour_point_t &p = arrayZ[i];
77
8.73M
      float x_ = p.x * matrix[0] + p.y * matrix[2];
78
8.73M
     p.y = p.x * matrix[1] + p.y * matrix[3];
79
8.73M
      p.x = x_;
80
8.73M
    }
81
305k
  }
82
83
  void translate (const contour_point_t& delta)
84
739k
  {
85
739k
    if (delta.x == 0.f && delta.y == 0.f)
86
449k
      return;
87
290k
    auto arrayZ = this->arrayZ;
88
290k
    unsigned count = length;
89
24.8M
    for (unsigned i = 0; i < count; i++)
90
24.5M
      arrayZ[i].translate (delta);
91
290k
  }
92
};
93
94
/* https://docs.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#tuplevariationheader */
95
struct TupleVariationHeader
96
{
97
  unsigned get_size (unsigned axis_count) const
98
510k
  { return min_size + get_all_tuples (axis_count).get_size (); }
99
100
405k
  unsigned get_data_size () const { return varDataSize; }
101
102
  const TupleVariationHeader &get_next (unsigned axis_count) const
103
130k
  { return StructAtOffset<TupleVariationHeader> (this, get_size (axis_count)); }
104
105
  float calculate_scalar (hb_array_t<int> coords, unsigned int coord_count,
106
        const hb_array_t<const F2DOT14> shared_tuples) const
107
176k
  {
108
176k
    hb_array_t<const F2DOT14> peak_tuple;
109
110
176k
    if (has_peak ())
111
62.7k
      peak_tuple = get_peak_tuple (coord_count);
112
113k
    else
113
113k
    {
114
113k
      unsigned int index = get_index ();
115
113k
      if (unlikely (index * coord_count >= shared_tuples.length))
116
31.0k
  return 0.f;
117
82.5k
      peak_tuple = shared_tuples.sub_array (coord_count * index, coord_count);
118
82.5k
    }
119
120
145k
    hb_array_t<const F2DOT14> start_tuple;
121
145k
    hb_array_t<const F2DOT14> end_tuple;
122
145k
    if (has_intermediate ())
123
31.7k
    {
124
31.7k
      start_tuple = get_start_tuple (coord_count);
125
31.7k
      end_tuple = get_end_tuple (coord_count);
126
31.7k
    }
127
128
145k
    float scalar = 1.f;
129
536k
    for (unsigned int i = 0; i < coord_count; i++)
130
457k
    {
131
457k
      int v = coords[i];
132
457k
      int peak = peak_tuple[i];
133
457k
      if (!peak || v == peak) continue;
134
135
179k
      if (has_intermediate ())
136
70.2k
      {
137
70.2k
  int start = start_tuple[i];
138
70.2k
  int end = end_tuple[i];
139
70.2k
  if (unlikely (start > peak || peak > end ||
140
70.2k
          (start < 0 && end > 0 && peak))) continue;
141
15.9k
  if (v < start || v > end) return 0.f;
142
4.94k
  if (v < peak)
143
3.09k
  { if (peak != start) scalar *= (float) (v - start) / (peak - start); }
144
1.84k
  else
145
1.84k
  { if (peak != end) scalar *= (float) (end - v) / (end - peak); }
146
4.94k
      }
147
109k
      else if (!v || v < hb_min (0, peak) || v > hb_max (0, peak)) return 0.f;
148
54.7k
      else
149
54.7k
  scalar *= (float) v / peak;
150
179k
    }
151
79.3k
    return scalar;
152
145k
  }
153
154
876k
  bool           has_peak () const { return tupleIndex & TuppleIndex::EmbeddedPeakTuple; }
155
962k
  bool   has_intermediate () const { return tupleIndex & TuppleIndex::IntermediateRegion; }
156
68.1k
  bool has_private_points () const { return tupleIndex & TuppleIndex::PrivatePointNumbers; }
157
113k
  unsigned      get_index () const { return tupleIndex & TuppleIndex::TupleIndexMask; }
158
159
  protected:
160
  struct TuppleIndex : HBUINT16
161
  {
162
    enum Flags {
163
      EmbeddedPeakTuple   = 0x8000u,
164
      IntermediateRegion  = 0x4000u,
165
      PrivatePointNumbers = 0x2000u,
166
      TupleIndexMask      = 0x0FFFu
167
    };
168
169
    DEFINE_SIZE_STATIC (2);
170
  };
171
172
  hb_array_t<const F2DOT14> get_all_tuples (unsigned axis_count) const
173
636k
  { return StructAfter<UnsizedArrayOf<F2DOT14>> (tupleIndex).as_array ((has_peak () + has_intermediate () * 2) * axis_count); }
174
  hb_array_t<const F2DOT14> get_peak_tuple (unsigned axis_count) const
175
62.7k
  { return get_all_tuples (axis_count).sub_array (0, axis_count); }
176
  hb_array_t<const F2DOT14> get_start_tuple (unsigned axis_count) const
177
31.7k
  { return get_all_tuples (axis_count).sub_array (has_peak () * axis_count, axis_count); }
178
  hb_array_t<const F2DOT14> get_end_tuple (unsigned axis_count) const
179
31.7k
  { return get_all_tuples (axis_count).sub_array (has_peak () * axis_count + axis_count, axis_count); }
180
181
  HBUINT16  varDataSize;  /* The size in bytes of the serialized
182
         * data for this tuple variation table. */
183
  TuppleIndex tupleIndex; /* A packed field. The high 4 bits are flags (see below).
184
           The low 12 bits are an index into a shared tuple
185
           records array. */
186
  /* UnsizedArrayOf<F2DOT14> peakTuple - optional */
187
        /* Peak tuple record for this tuple variation table — optional,
188
         * determined by flags in the tupleIndex value.
189
         *
190
         * Note that this must always be included in the 'cvar' table. */
191
  /* UnsizedArrayOf<F2DOT14> intermediateStartTuple - optional */
192
        /* Intermediate start tuple record for this tuple variation table — optional,
193
           determined by flags in the tupleIndex value. */
194
  /* UnsizedArrayOf<F2DOT14> intermediateEndTuple - optional */
195
        /* Intermediate end tuple record for this tuple variation table — optional,
196
         * determined by flags in the tupleIndex value. */
197
  public:
198
  DEFINE_SIZE_MIN (4);
199
};
200
201
struct GlyphVariationData
202
{
203
  const TupleVariationHeader &get_tuple_var_header (void) const
204
99.6k
  { return StructAfter<TupleVariationHeader> (data); }
205
206
  struct tuple_iterator_t
207
  {
208
    void init (hb_bytes_t var_data_bytes_, unsigned int axis_count_)
209
99.6k
    {
210
99.6k
      var_data_bytes = var_data_bytes_;
211
99.6k
      var_data = var_data_bytes_.as<GlyphVariationData> ();
212
99.6k
      index = 0;
213
99.6k
      axis_count = axis_count_;
214
99.6k
      current_tuple = &var_data->get_tuple_var_header ();
215
99.6k
      data_offset = 0;
216
99.6k
    }
217
218
    bool get_shared_indices (hb_vector_t<unsigned int> &shared_indices /* OUT */)
219
99.6k
    {
220
99.6k
      if (var_data->has_shared_point_numbers ())
221
41.4k
      {
222
41.4k
  const HBUINT8 *base = &(var_data+var_data->data);
223
41.4k
  const HBUINT8 *p = base;
224
41.4k
  if (!unpack_points (p, shared_indices, (const HBUINT8 *) (var_data_bytes.arrayZ + var_data_bytes.length))) return false;
225
31.7k
  data_offset = p - base;
226
31.7k
      }
227
89.9k
      return true;
228
99.6k
    }
229
230
    bool is_valid () const
231
220k
    {
232
220k
      return (index < var_data->tupleVarCount.get_count ()) &&
233
220k
       var_data_bytes.check_range (current_tuple, TupleVariationHeader::min_size) &&
234
220k
       var_data_bytes.check_range (current_tuple, hb_max (current_tuple->get_data_size (), current_tuple->get_size (axis_count))) &&
235
220k
       current_tuple->get_size (axis_count);
236
220k
    }
237
238
    bool move_to_next ()
239
130k
    {
240
130k
      data_offset += current_tuple->get_data_size ();
241
130k
      current_tuple = &current_tuple->get_next (axis_count);
242
130k
      index++;
243
130k
      return is_valid ();
244
130k
    }
245
246
    const HBUINT8 *get_serialized_data () const
247
77.7k
    { return &(var_data+var_data->data) + data_offset; }
248
249
    private:
250
    const GlyphVariationData *var_data;
251
    unsigned int index;
252
    unsigned int axis_count;
253
    unsigned int data_offset;
254
255
    public:
256
    hb_bytes_t var_data_bytes;
257
    const TupleVariationHeader *current_tuple;
258
  };
259
260
  static bool get_tuple_iterator (hb_bytes_t var_data_bytes, unsigned axis_count,
261
          hb_vector_t<unsigned int> &shared_indices /* OUT */,
262
          tuple_iterator_t *iterator /* OUT */)
263
99.6k
  {
264
99.6k
    iterator->init (var_data_bytes, axis_count);
265
99.6k
    if (!iterator->get_shared_indices (shared_indices))
266
9.68k
      return false;
267
89.9k
    return iterator->is_valid ();
268
99.6k
  }
269
270
99.6k
  bool has_shared_point_numbers () const { return tupleVarCount.has_shared_point_numbers (); }
271
272
  static bool unpack_points (const HBUINT8 *&p /* IN/OUT */,
273
           hb_vector_t<unsigned int> &points /* OUT */,
274
           const HBUINT8 *end)
275
89.3k
  {
276
89.3k
    enum packed_point_flag_t
277
89.3k
    {
278
89.3k
      POINTS_ARE_WORDS     = 0x80,
279
89.3k
      POINT_RUN_COUNT_MASK = 0x7F
280
89.3k
    };
281
282
89.3k
    if (unlikely (p + 1 > end)) return false;
283
284
85.5k
    uint16_t count = *p++;
285
85.5k
    if (count & POINTS_ARE_WORDS)
286
6.33k
    {
287
6.33k
      if (unlikely (p + 1 > end)) return false;
288
6.21k
      count = ((count & POINT_RUN_COUNT_MASK) << 8) | *p++;
289
6.21k
    }
290
85.4k
    if (unlikely (!points.resize (count))) return false;
291
292
83.8k
    unsigned int n = 0;
293
83.8k
    uint16_t i = 0;
294
175k
    while (i < count)
295
108k
    {
296
108k
      if (unlikely (p + 1 > end)) return false;
297
105k
      uint16_t j;
298
105k
      uint8_t control = *p++;
299
105k
      uint16_t run_count = (control & POINT_RUN_COUNT_MASK) + 1;
300
105k
      if (control & POINTS_ARE_WORDS)
301
13.6k
      {
302
254k
  for (j = 0; j < run_count && i < count; j++, i++)
303
245k
  {
304
245k
    if (unlikely (p + HBUINT16::static_size > end)) return false;
305
240k
    n += *(const HBUINT16 *)p;
306
240k
    points[i] = n;
307
240k
    p += HBUINT16::static_size;
308
240k
  }
309
13.6k
      }
310
91.7k
      else
311
91.7k
      {
312
1.41M
  for (j = 0; j < run_count && i < count; j++, i++)
313
1.32M
  {
314
1.32M
    if (unlikely (p + 1 > end)) return false;
315
1.32M
    n += *p++;
316
1.32M
    points[i] = n;
317
1.32M
  }
318
91.7k
      }
319
97.2k
      if (j < run_count) return false;
320
97.2k
    }
321
67.2k
    return true;
322
83.8k
  }
323
324
  static bool unpack_deltas (const HBUINT8 *&p /* IN/OUT */,
325
           hb_vector_t<int> &deltas /* IN/OUT */,
326
           const HBUINT8 *end)
327
92.8k
  {
328
92.8k
    enum packed_delta_flag_t
329
92.8k
    {
330
92.8k
      DELTAS_ARE_ZERO      = 0x80,
331
92.8k
      DELTAS_ARE_WORDS     = 0x40,
332
92.8k
      DELTA_RUN_COUNT_MASK = 0x3F
333
92.8k
    };
334
335
92.8k
    unsigned int i = 0;
336
92.8k
    unsigned int count = deltas.length;
337
544k
    while (i < count)
338
472k
    {
339
472k
      if (unlikely (p + 1 > end)) return false;
340
466k
      uint8_t control = *p++;
341
466k
      unsigned int run_count = (control & DELTA_RUN_COUNT_MASK) + 1;
342
466k
      unsigned int j;
343
466k
      if (control & DELTAS_ARE_ZERO)
344
839k
  for (j = 0; j < run_count && i < count; j++, i++)
345
641k
    deltas[i] = 0;
346
267k
      else if (control & DELTAS_ARE_WORDS)
347
57.3k
  for (j = 0; j < run_count && i < count; j++, i++)
348
50.8k
  {
349
50.8k
    if (unlikely (p + HBUINT16::static_size > end)) return false;
350
47.1k
    deltas[i] = *(const HBINT16 *) p;
351
47.1k
    p += HBUINT16::static_size;
352
47.1k
  }
353
257k
      else
354
1.16M
  for (j = 0; j < run_count && i < count; j++, i++)
355
911k
  {
356
911k
    if (unlikely (p + 1 > end)) return false;
357
909k
    deltas[i] = *(const HBINT8 *) p++;
358
909k
  }
359
460k
      if (j < run_count)
360
8.77k
  return false;
361
460k
    }
362
71.9k
    return true;
363
92.8k
  }
364
365
161k
  bool has_data () const { return tupleVarCount; }
366
367
  protected:
368
  struct TupleVarCount : HBUINT16
369
  {
370
99.6k
    bool has_shared_point_numbers () const { return ((*this) & SharedPointNumbers); }
371
220k
    unsigned int get_count () const { return (*this) & CountMask; }
372
373
    protected:
374
    enum Flags
375
    {
376
      SharedPointNumbers= 0x8000u,
377
      CountMask   = 0x0FFFu
378
    };
379
    public:
380
    DEFINE_SIZE_STATIC (2);
381
  };
382
383
  TupleVarCount tupleVarCount;  /* A packed field. The high 4 bits are flags, and the
384
         * low 12 bits are the number of tuple variation tables
385
         * for this glyph. The number of tuple variation tables
386
         * can be any number between 1 and 4095. */
387
  Offset16To<HBUINT8>
388
    data;   /* Offset from the start of the GlyphVariationData table
389
         * to the serialized data. */
390
  /* TupleVariationHeader tupleVariationHeaders[] *//* Array of tuple variation headers. */
391
  public:
392
  DEFINE_SIZE_MIN (4);
393
};
394
395
struct gvar
396
{
397
  static constexpr hb_tag_t tableTag = HB_OT_TAG_gvar;
398
399
  bool sanitize_shallow (hb_sanitize_context_t *c) const
400
14.2k
  {
401
14.2k
    TRACE_SANITIZE (this);
402
14.2k
    return_trace (c->check_struct (this) && (version.major == 1) &&
403
14.2k
      sharedTuples.sanitize (c, this, axisCount * sharedTupleCount) &&
404
14.2k
      (is_long_offset () ?
405
14.2k
         c->check_array (get_long_offset_array (), glyphCount+1) :
406
14.2k
         c->check_array (get_short_offset_array (), glyphCount+1)));
407
14.2k
  }
408
409
  /* GlyphVariationData not sanitized here; must be checked while accessing each glyph variation data */
410
  bool sanitize (hb_sanitize_context_t *c) const
411
14.2k
  { return sanitize_shallow (c); }
412
413
  bool subset (hb_subset_context_t *c) const
414
0
  {
415
0
    TRACE_SUBSET (this);
416
0
417
0
    gvar *out = c->serializer->allocate_min<gvar> ();
418
0
    if (unlikely (!out)) return_trace (false);
419
0
420
0
    out->version.major = 1;
421
0
    out->version.minor = 0;
422
0
    out->axisCount = axisCount;
423
0
    out->sharedTupleCount = sharedTupleCount;
424
0
425
0
    unsigned int num_glyphs = c->plan->num_output_glyphs ();
426
0
    out->glyphCount = num_glyphs;
427
0
428
0
    unsigned int subset_data_size = 0;
429
0
    for (hb_codepoint_t gid = (c->plan->flags & HB_SUBSET_FLAGS_NOTDEF_OUTLINE) ? 0 : 1;
430
0
         gid < num_glyphs;
431
0
         gid++)
432
0
    {
433
0
      hb_codepoint_t old_gid;
434
0
      if (!c->plan->old_gid_for_new_gid (gid, &old_gid)) continue;
435
0
      subset_data_size += get_glyph_var_data_bytes (c->source_blob, old_gid).length;
436
0
    }
437
0
438
0
    bool long_offset = subset_data_size & ~0xFFFFu;
439
0
    out->flags = long_offset ? 1 : 0;
440
0
441
0
    HBUINT8 *subset_offsets = c->serializer->allocate_size<HBUINT8> ((long_offset ? 4 : 2) * (num_glyphs + 1));
442
0
    if (!subset_offsets) return_trace (false);
443
0
444
0
    /* shared tuples */
445
0
    if (!sharedTupleCount || !sharedTuples)
446
0
      out->sharedTuples = 0;
447
0
    else
448
0
    {
449
0
      unsigned int shared_tuple_size = F2DOT14::static_size * axisCount * sharedTupleCount;
450
0
      F2DOT14 *tuples = c->serializer->allocate_size<F2DOT14> (shared_tuple_size);
451
0
      if (!tuples) return_trace (false);
452
0
      out->sharedTuples = (char *) tuples - (char *) out;
453
0
      memcpy (tuples, this+sharedTuples, shared_tuple_size);
454
0
    }
455
0
456
0
    char *subset_data = c->serializer->allocate_size<char> (subset_data_size);
457
0
    if (!subset_data) return_trace (false);
458
0
    out->dataZ = subset_data - (char *) out;
459
0
460
0
    unsigned int glyph_offset = 0;
461
0
    for (hb_codepoint_t gid = (c->plan->flags & HB_SUBSET_FLAGS_NOTDEF_OUTLINE) ? 0 : 1;
462
0
         gid < num_glyphs;
463
0
         gid++)
464
0
    {
465
0
      hb_codepoint_t old_gid;
466
0
      hb_bytes_t var_data_bytes = c->plan->old_gid_for_new_gid (gid, &old_gid)
467
0
        ? get_glyph_var_data_bytes (c->source_blob, old_gid)
468
0
        : hb_bytes_t ();
469
0
470
0
      if (long_offset)
471
0
  ((HBUINT32 *) subset_offsets)[gid] = glyph_offset;
472
0
      else
473
0
  ((HBUINT16 *) subset_offsets)[gid] = glyph_offset / 2;
474
0
475
0
      if (var_data_bytes.length > 0)
476
0
  memcpy (subset_data, var_data_bytes.arrayZ, var_data_bytes.length);
477
0
      subset_data += var_data_bytes.length;
478
0
      glyph_offset += var_data_bytes.length;
479
0
    }
480
0
    if (long_offset)
481
0
      ((HBUINT32 *) subset_offsets)[num_glyphs] = glyph_offset;
482
0
    else
483
0
      ((HBUINT16 *) subset_offsets)[num_glyphs] = glyph_offset / 2;
484
0
485
0
    return_trace (true);
486
0
  }
487
488
  protected:
489
  const hb_bytes_t get_glyph_var_data_bytes (hb_blob_t *blob, hb_codepoint_t glyph) const
490
161k
  {
491
161k
    unsigned start_offset = get_offset (glyph);
492
161k
    unsigned end_offset = get_offset (glyph+1);
493
161k
    if (unlikely (end_offset < start_offset)) return hb_bytes_t ();
494
147k
    unsigned length = end_offset - start_offset;
495
147k
    hb_bytes_t var_data = blob->as_bytes ().sub_array (((unsigned) dataZ) + start_offset, length);
496
147k
    return likely (var_data.length >= GlyphVariationData::min_size) ? var_data : hb_bytes_t ();
497
161k
  }
498
499
332k
  bool is_long_offset () const { return flags & 1; }
500
501
  unsigned get_offset (unsigned i) const
502
323k
  {
503
323k
    if (unlikely (i > glyphCount)) return 0;
504
323k
    return is_long_offset () ? get_long_offset_array ()[i] : get_short_offset_array ()[i] * 2;
505
323k
  }
506
507
81.2k
  const HBUINT32 * get_long_offset_array () const { return (const HBUINT32 *) &offsetZ; }
508
250k
  const HBUINT16 *get_short_offset_array () const { return (const HBUINT16 *) &offsetZ; }
509
510
  public:
511
  struct accelerator_t
512
  {
513
    accelerator_t (hb_face_t *face)
514
288k
    { table = hb_sanitize_context_t ().reference_table<gvar> (face); }
515
286k
    ~accelerator_t () { table.destroy (); }
516
517
    private:
518
519
    static float infer_delta (const hb_array_t<contour_point_t> points,
520
            const hb_array_t<contour_point_t> deltas,
521
            unsigned int target, unsigned int prev, unsigned int next,
522
            float contour_point_t::*m)
523
3.34k
    {
524
3.34k
      float target_val = points[target].*m;
525
3.34k
      float prev_val = points[prev].*m;
526
3.34k
      float next_val = points[next].*m;
527
3.34k
      float prev_delta =  deltas[prev].*m;
528
3.34k
      float next_delta =  deltas[next].*m;
529
530
3.34k
      if (prev_val == next_val)
531
1.88k
  return (prev_delta == next_delta) ? prev_delta : 0.f;
532
1.45k
      else if (target_val <= hb_min (prev_val, next_val))
533
534
  return (prev_val < next_val) ? prev_delta : next_delta;
534
923
      else if (target_val >= hb_max (prev_val, next_val))
535
519
  return (prev_val > next_val) ? prev_delta : next_delta;
536
537
      /* linear interpolation */
538
404
      float r = (target_val - prev_val) / (next_val - prev_val);
539
404
      return prev_delta + r * (next_delta - prev_delta);
540
3.34k
    }
541
542
    static unsigned int next_index (unsigned int i, unsigned int start, unsigned int end)
543
5.04k
    { return (i >= end) ? start : (i + 1); }
544
545
    public:
546
    bool apply_deltas_to_points (hb_codepoint_t glyph, hb_font_t *font,
547
         const hb_array_t<contour_point_t> points) const
548
582k
    {
549
582k
      if (!font->num_coords) return true;
550
551
232k
      if (unlikely (glyph >= table->glyphCount)) return true;
552
553
161k
      hb_bytes_t var_data_bytes = table->get_glyph_var_data_bytes (table.get_blob (), glyph);
554
161k
      if (!var_data_bytes.as<GlyphVariationData> ()->has_data ()) return true;
555
99.6k
      hb_vector_t<unsigned int> shared_indices;
556
99.6k
      GlyphVariationData::tuple_iterator_t iterator;
557
99.6k
      if (!GlyphVariationData::get_tuple_iterator (var_data_bytes, table->axisCount,
558
99.6k
               shared_indices, &iterator))
559
17.7k
  return true; /* so isn't applied at all */
560
561
      /* Save original points for inferred delta calculation */
562
81.8k
      contour_point_vector_t orig_points;
563
81.8k
      if (unlikely (!orig_points.resize (points.length))) return false;
564
16.2M
      for (unsigned int i = 0; i < orig_points.length; i++)
565
16.1M
  orig_points.arrayZ[i] = points.arrayZ[i];
566
567
79.1k
      contour_point_vector_t deltas; /* flag is used to indicate referenced point */
568
79.1k
      if (unlikely (!deltas.resize (points.length))) return false;
569
570
75.1k
      hb_vector_t<unsigned> end_points;
571
15.3M
      for (unsigned i = 0; i < points.length; ++i)
572
15.2M
  if (points[i].is_end_point)
573
5.10k
    end_points.push (i);
574
575
75.1k
      auto coords = hb_array (font->coords, font->num_coords);
576
75.1k
      unsigned num_coords = table->axisCount;
577
75.1k
      hb_array_t<const F2DOT14> shared_tuples = (table+table->sharedTuples).as_array (table->sharedTupleCount * table->axisCount);
578
579
75.1k
      hb_vector_t<unsigned int> private_indices;
580
75.1k
      hb_vector_t<int> x_deltas;
581
75.1k
      hb_vector_t<int> y_deltas;
582
75.1k
      do
583
176k
      {
584
176k
  float scalar = iterator.current_tuple->calculate_scalar (coords, num_coords, shared_tuples);
585
176k
  if (scalar == 0.f) continue;
586
77.7k
  const HBUINT8 *p = iterator.get_serialized_data ();
587
77.7k
  unsigned int length = iterator.current_tuple->get_data_size ();
588
77.7k
  if (unlikely (!iterator.var_data_bytes.check_range (p, length)))
589
9.58k
    return false;
590
591
68.1k
  const HBUINT8 *end = p + length;
592
593
68.1k
  bool has_private_points = iterator.current_tuple->has_private_points ();
594
68.1k
  if (has_private_points &&
595
68.1k
      !GlyphVariationData::unpack_points (p, private_indices, end))
596
12.4k
    return false;
597
55.7k
  const hb_array_t<unsigned int> &indices = has_private_points ? private_indices : shared_indices;
598
599
55.7k
  bool apply_to_all = (indices.length == 0);
600
55.7k
  unsigned int num_deltas = apply_to_all ? points.length : indices.length;
601
55.7k
  if (unlikely (!x_deltas.resize (num_deltas))) return false;
602
53.9k
  if (unlikely (!GlyphVariationData::unpack_deltas (p, x_deltas, end))) return false;
603
39.9k
  if (unlikely (!y_deltas.resize (num_deltas))) return false;
604
38.9k
  if (unlikely (!GlyphVariationData::unpack_deltas (p, y_deltas, end))) return false;
605
606
2.53M
  for (unsigned int i = 0; i < deltas.length; i++)
607
2.50M
    deltas[i].init ();
608
625k
  for (unsigned int i = 0; i < num_deltas; i++)
609
593k
  {
610
593k
    unsigned int pt_index = apply_to_all ? i : indices[i];
611
593k
    if (unlikely (pt_index >= deltas.length)) continue;
612
206k
    deltas.arrayZ[pt_index].flag = 1; /* this point is referenced, i.e., explicit deltas specified */
613
206k
    deltas.arrayZ[pt_index].x += x_deltas.arrayZ[i] * scalar;
614
206k
    deltas.arrayZ[pt_index].y += y_deltas.arrayZ[i] * scalar;
615
206k
  }
616
617
  /* infer deltas for unreferenced points */
618
31.9k
  unsigned start_point = 0;
619
35.3k
  for (unsigned c = 0; c < end_points.length; c++)
620
3.39k
  {
621
3.39k
    unsigned end_point = end_points[c];
622
623
    /* Check the number of unreferenced points in a contour. If no unref points or no ref points, nothing to do. */
624
3.39k
    unsigned unref_count = 0;
625
20.4k
    for (unsigned i = start_point; i <= end_point; i++)
626
17.0k
      if (!deltas[i].flag) unref_count++;
627
628
3.39k
    unsigned j = start_point;
629
3.39k
    if (unref_count == 0 || unref_count > end_point - start_point)
630
3.12k
      goto no_more_gaps;
631
632
270
    for (;;)
633
334
    {
634
      /* Locate the next gap of unreferenced points between two referenced points prev and next.
635
       * Note that a gap may wrap around at left (start_point) and/or at right (end_point).
636
       */
637
334
      unsigned int prev, next, i;
638
334
      for (;;)
639
1.29k
      {
640
1.29k
        i = j;
641
1.29k
        j = next_index (i, start_point, end_point);
642
1.29k
        if (deltas[i].flag && !deltas[j].flag) break;
643
1.29k
      }
644
334
      prev = j = i;
645
334
      for (;;)
646
2.00k
      {
647
2.00k
        i = j;
648
2.00k
        j = next_index (i, start_point, end_point);
649
2.00k
        if (!deltas[i].flag && deltas[j].flag) break;
650
2.00k
      }
651
334
      next = j;
652
      /* Infer deltas for all unref points in the gap between prev and next */
653
334
      i = prev;
654
334
      for (;;)
655
1.73k
      {
656
1.73k
        i = next_index (i, start_point, end_point);
657
1.73k
        if (i == next) break;
658
1.67k
        deltas[i].x = infer_delta (orig_points.as_array (), deltas.as_array (), i, prev, next, &contour_point_t::x);
659
1.67k
        deltas[i].y = infer_delta (orig_points.as_array (), deltas.as_array (), i, prev, next, &contour_point_t::y);
660
1.67k
        if (--unref_count == 0) goto no_more_gaps;
661
1.67k
      }
662
334
    }
663
3.39k
  no_more_gaps:
664
3.39k
    start_point = end_point + 1;
665
3.39k
  }
666
667
  /* apply specified / inferred deltas to points */
668
2.53M
  for (unsigned int i = 0; i < points.length; i++)
669
2.50M
  {
670
2.50M
    points.arrayZ[i].x += deltas.arrayZ[i].x;
671
2.50M
    points.arrayZ[i].y += deltas.arrayZ[i].y;
672
2.50M
  }
673
130k
      } while (iterator.move_to_next ());
674
675
29.3k
      return true;
676
75.1k
    }
677
678
0
    unsigned int get_axis_count () const { return table->axisCount; }
679
680
    private:
681
    hb_blob_ptr_t<gvar> table;
682
  };
683
684
  protected:
685
  FixedVersion<>version;  /* Version number of the glyph variations table
686
         * Set to 0x00010000u. */
687
  HBUINT16  axisCount;  /* The number of variation axes for this font. This must be
688
         * the same number as axisCount in the 'fvar' table. */
689
  HBUINT16  sharedTupleCount;
690
        /* The number of shared tuple records. Shared tuple records
691
         * can be referenced within glyph variation data tables for
692
         * multiple glyphs, as opposed to other tuple records stored
693
         * directly within a glyph variation data table. */
694
  NNOffset32To<UnsizedArrayOf<F2DOT14>>
695
    sharedTuples; /* Offset from the start of this table to the shared tuple records.
696
         * Array of tuple records shared across all glyph variation data tables. */
697
  HBUINT16  glyphCount; /* The number of glyphs in this font. This must match the number of
698
         * glyphs stored elsewhere in the font. */
699
  HBUINT16  flags;    /* Bit-field that gives the format of the offset array that follows.
700
         * If bit 0 is clear, the offsets are uint16; if bit 0 is set, the
701
         * offsets are uint32. */
702
  Offset32To<GlyphVariationData>
703
    dataZ;    /* Offset from the start of this table to the array of
704
         * GlyphVariationData tables. */
705
  UnsizedArrayOf<HBUINT8>
706
    offsetZ;  /* Offsets from the start of the GlyphVariationData array
707
         * to each GlyphVariationData table. */
708
  public:
709
  DEFINE_SIZE_ARRAY (20, offsetZ);
710
};
711
712
struct gvar_accelerator_t : gvar::accelerator_t {
713
288k
  gvar_accelerator_t (hb_face_t *face) : gvar::accelerator_t (face) {}
714
};
715
716
} /* namespace OT */
717
718
#endif /* HB_OT_VAR_GVAR_TABLE_HH */