Coverage Report

Created: 2024-05-18 02:13

/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
15.3M
  { flag = 0; x = x_; y = y_; is_end_point = is_end_point_; }
45
46
154M
  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
23.7M
  {
58
23.7M
    unsigned int old_len = length;
59
23.7M
    if (unlikely (!resize (old_len + a.length)))
60
1.75k
      return;
61
23.7M
    auto arrayZ = this->arrayZ + old_len;
62
23.7M
    unsigned count = a.length;
63
105M
    for (unsigned int i = 0; i < count; i++)
64
81.3M
      arrayZ[i] = a.arrayZ[i];
65
23.7M
  }
66
67
  void transform (const float (&matrix)[4])
68
11.7M
  {
69
11.7M
    if (matrix[0] == 1.f && matrix[1] == 0.f &&
70
11.7M
  matrix[2] == 0.f && matrix[3] == 1.f)
71
70.7k
      return;
72
11.7M
    auto arrayZ = this->arrayZ;
73
11.7M
    unsigned count = length;
74
76.1M
    for (unsigned i = 0; i < count; i++)
75
64.4M
    {
76
64.4M
      contour_point_t &p = arrayZ[i];
77
64.4M
      float x_ = p.x * matrix[0] + p.y * matrix[2];
78
64.4M
     p.y = p.x * matrix[1] + p.y * matrix[3];
79
64.4M
      p.x = x_;
80
64.4M
    }
81
11.7M
  }
82
83
  void translate (const contour_point_t& delta)
84
23.6M
  {
85
23.6M
    if (delta.x == 0.f && delta.y == 0.f)
86
17.4M
      return;
87
6.26M
    auto arrayZ = this->arrayZ;
88
6.26M
    unsigned count = length;
89
161M
    for (unsigned i = 0; i < count; i++)
90
154M
      arrayZ[i].translate (delta);
91
6.26M
  }
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
1.04M
  { return min_size + get_all_tuples (axis_count).get_size (); }
99
100
762k
  unsigned get_data_size () const { return varDataSize; }
101
102
  const TupleVariationHeader &get_next (unsigned axis_count) const
103
300k
  { 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
357k
  {
108
357k
    hb_array_t<const F2DOT14> peak_tuple;
109
110
357k
    if (has_peak ())
111
24.2k
      peak_tuple = get_peak_tuple (coord_count);
112
333k
    else
113
333k
    {
114
333k
      unsigned int index = get_index ();
115
333k
      if (unlikely (index * coord_count >= shared_tuples.length))
116
39.0k
  return 0.f;
117
294k
      peak_tuple = shared_tuples.sub_array (coord_count * index, coord_count);
118
294k
    }
119
120
318k
    hb_array_t<const F2DOT14> start_tuple;
121
318k
    hb_array_t<const F2DOT14> end_tuple;
122
318k
    if (has_intermediate ())
123
54.0k
    {
124
54.0k
      start_tuple = get_start_tuple (coord_count);
125
54.0k
      end_tuple = get_end_tuple (coord_count);
126
54.0k
    }
127
128
318k
    float scalar = 1.f;
129
622k
    for (unsigned int i = 0; i < coord_count; i++)
130
541k
    {
131
541k
      int v = coords[i];
132
541k
      int peak = peak_tuple[i];
133
541k
      if (!peak || v == peak) continue;
134
135
435k
      if (has_intermediate ())
136
170k
      {
137
170k
  int start = start_tuple[i];
138
170k
  int end = end_tuple[i];
139
170k
  if (unlikely (start > peak || peak > end ||
140
170k
          (start < 0 && end > 0 && peak))) continue;
141
31.5k
  if (v < start || v > end) return 0.f;
142
13.2k
  if (v < peak)
143
6.91k
  { if (peak != start) scalar *= (float) (v - start) / (peak - start); }
144
6.30k
  else
145
6.30k
  { if (peak != end) scalar *= (float) (end - v) / (end - peak); }
146
13.2k
      }
147
265k
      else if (!v || v < hb_min (0, peak) || v > hb_max (0, peak)) return 0.f;
148
45.9k
      else
149
45.9k
  scalar *= (float) v / peak;
150
435k
    }
151
81.0k
    return scalar;
152
318k
  }
153
154
1.64M
  bool           has_peak () const { return tupleIndex & TuppleIndex::EmbeddedPeakTuple; }
155
1.93M
  bool   has_intermediate () const { return tupleIndex & TuppleIndex::IntermediateRegion; }
156
64.3k
  bool has_private_points () const { return tupleIndex & TuppleIndex::PrivatePointNumbers; }
157
333k
  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
1.17M
  { 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
24.2k
  { 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
54.0k
  { 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
54.0k
  { 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
138k
  { 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
138k
    {
210
138k
      var_data_bytes = var_data_bytes_;
211
138k
      var_data = var_data_bytes_.as<GlyphVariationData> ();
212
138k
      index = 0;
213
138k
      axis_count = axis_count_;
214
138k
      current_tuple = &var_data->get_tuple_var_header ();
215
138k
      data_offset = 0;
216
138k
    }
217
218
    bool get_shared_indices (hb_vector_t<unsigned int> &shared_indices /* OUT */)
219
138k
    {
220
138k
      if (var_data->has_shared_point_numbers ())
221
77.6k
      {
222
77.6k
  const HBUINT8 *base = &(var_data+var_data->data);
223
77.6k
  const HBUINT8 *p = base;
224
77.6k
  if (!unpack_points (p, shared_indices, (const HBUINT8 *) (var_data_bytes.arrayZ + var_data_bytes.length))) return false;
225
55.6k
  data_offset = p - base;
226
55.6k
      }
227
116k
      return true;
228
138k
    }
229
230
    bool is_valid () const
231
416k
    {
232
416k
      return (index < var_data->tupleVarCount.get_count ()) &&
233
416k
       var_data_bytes.check_range (current_tuple, TupleVariationHeader::min_size) &&
234
416k
       var_data_bytes.check_range (current_tuple, hb_max (current_tuple->get_data_size (), current_tuple->get_size (axis_count))) &&
235
416k
       current_tuple->get_size (axis_count);
236
416k
    }
237
238
    bool move_to_next ()
239
300k
    {
240
300k
      data_offset += current_tuple->get_data_size ();
241
300k
      current_tuple = &current_tuple->get_next (axis_count);
242
300k
      index++;
243
300k
      return is_valid ();
244
300k
    }
245
246
    const HBUINT8 *get_serialized_data () const
247
79.5k
    { 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
138k
  {
264
138k
    iterator->init (var_data_bytes, axis_count);
265
138k
    if (!iterator->get_shared_indices (shared_indices))
266
22.0k
      return false;
267
116k
    return iterator->is_valid ();
268
138k
  }
269
270
138k
  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
114k
  {
276
114k
    enum packed_point_flag_t
277
114k
    {
278
114k
      POINTS_ARE_WORDS     = 0x80,
279
114k
      POINT_RUN_COUNT_MASK = 0x7F
280
114k
    };
281
282
114k
    if (unlikely (p + 1 > end)) return false;
283
284
106k
    uint16_t count = *p++;
285
106k
    if (count & POINTS_ARE_WORDS)
286
7.35k
    {
287
7.35k
      if (unlikely (p + 1 > end)) return false;
288
7.29k
      count = ((count & POINT_RUN_COUNT_MASK) << 8) | *p++;
289
7.29k
    }
290
106k
    if (unlikely (!points.resize (count))) return false;
291
292
105k
    unsigned int n = 0;
293
105k
    uint16_t i = 0;
294
181k
    while (i < count)
295
100k
    {
296
100k
      if (unlikely (p + 1 > end)) return false;
297
97.9k
      uint16_t j;
298
97.9k
      uint8_t control = *p++;
299
97.9k
      uint16_t run_count = (control & POINT_RUN_COUNT_MASK) + 1;
300
97.9k
      if (control & POINTS_ARE_WORDS)
301
13.9k
      {
302
214k
  for (j = 0; j < run_count && i < count; j++, i++)
303
205k
  {
304
205k
    if (unlikely (p + HBUINT16::static_size > end)) return false;
305
200k
    n += *(const HBUINT16 *)p;
306
200k
    points[i] = n;
307
200k
    p += HBUINT16::static_size;
308
200k
  }
309
13.9k
      }
310
83.9k
      else
311
83.9k
      {
312
1.17M
  for (j = 0; j < run_count && i < count; j++, i++)
313
1.09M
  {
314
1.09M
    if (unlikely (p + 1 > end)) return false;
315
1.08M
    n += *p++;
316
1.08M
    points[i] = n;
317
1.08M
  }
318
83.9k
      }
319
82.5k
      if (j < run_count) return false;
320
82.5k
    }
321
80.5k
    return true;
322
105k
  }
323
324
  static bool unpack_deltas (const HBUINT8 *&p /* IN/OUT */,
325
           hb_vector_t<int> &deltas /* IN/OUT */,
326
           const HBUINT8 *end)
327
81.8k
  {
328
81.8k
    enum packed_delta_flag_t
329
81.8k
    {
330
81.8k
      DELTAS_ARE_ZERO      = 0x80,
331
81.8k
      DELTAS_ARE_WORDS     = 0x40,
332
81.8k
      DELTA_RUN_COUNT_MASK = 0x3F
333
81.8k
    };
334
335
81.8k
    unsigned int i = 0;
336
81.8k
    unsigned int count = deltas.length;
337
396k
    while (i < count)
338
343k
    {
339
343k
      if (unlikely (p + 1 > end)) return false;
340
337k
      uint8_t control = *p++;
341
337k
      unsigned int run_count = (control & DELTA_RUN_COUNT_MASK) + 1;
342
337k
      unsigned int j;
343
337k
      if (control & DELTAS_ARE_ZERO)
344
780k
  for (j = 0; j < run_count && i < count; j++, i++)
345
686k
    deltas[i] = 0;
346
242k
      else if (control & DELTAS_ARE_WORDS)
347
79.8k
  for (j = 0; j < run_count && i < count; j++, i++)
348
65.5k
  {
349
65.5k
    if (unlikely (p + HBUINT16::static_size > end)) return false;
350
62.2k
    deltas[i] = *(const HBINT16 *) p;
351
62.2k
    p += HBUINT16::static_size;
352
62.2k
  }
353
224k
      else
354
1.18M
  for (j = 0; j < run_count && i < count; j++, i++)
355
968k
  {
356
968k
    if (unlikely (p + 1 > end)) return false;
357
963k
    deltas[i] = *(const HBINT8 *) p++;
358
963k
  }
359
329k
      if (j < run_count)
360
14.7k
  return false;
361
329k
    }
362
52.8k
    return true;
363
81.8k
  }
364
365
163k
  bool has_data () const { return tupleVarCount; }
366
367
  protected:
368
  struct TupleVarCount : HBUINT16
369
  {
370
138k
    bool has_shared_point_numbers () const { return ((*this) & SharedPointNumbers); }
371
416k
    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
11.6k
  {
401
11.6k
    TRACE_SANITIZE (this);
402
11.6k
    return_trace (c->check_struct (this) && (version.major == 1) &&
403
11.6k
      sharedTuples.sanitize (c, this, axisCount * sharedTupleCount) &&
404
11.6k
      (is_long_offset () ?
405
11.6k
         c->check_array (get_long_offset_array (), glyphCount+1) :
406
11.6k
         c->check_array (get_short_offset_array (), glyphCount+1)));
407
11.6k
  }
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
11.6k
  { 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
163k
  {
491
163k
    unsigned start_offset = get_offset (glyph);
492
163k
    unsigned end_offset = get_offset (glyph+1);
493
163k
    if (unlikely (end_offset < start_offset)) return hb_bytes_t ();
494
156k
    unsigned length = end_offset - start_offset;
495
156k
    hb_bytes_t var_data = blob->as_bytes ().sub_array (((unsigned) dataZ) + start_offset, length);
496
156k
    return likely (var_data.length >= GlyphVariationData::min_size) ? var_data : hb_bytes_t ();
497
163k
  }
498
499
334k
  bool is_long_offset () const { return flags & 1; }
500
501
  unsigned get_offset (unsigned i) const
502
327k
  {
503
327k
    if (unlikely (i > glyphCount)) return 0;
504
327k
    return is_long_offset () ? get_long_offset_array ()[i] : get_short_offset_array ()[i] * 2;
505
327k
  }
506
507
35.8k
  const HBUINT32 * get_long_offset_array () const { return (const HBUINT32 *) &offsetZ; }
508
298k
  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
368k
    { table = hb_sanitize_context_t ().reference_table<gvar> (face); }
515
367k
    ~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
188
    {
524
188
      float target_val = points[target].*m;
525
188
      float prev_val = points[prev].*m;
526
188
      float next_val = points[next].*m;
527
188
      float prev_delta =  deltas[prev].*m;
528
188
      float next_delta =  deltas[next].*m;
529
530
188
      if (prev_val == next_val)
531
152
  return (prev_delta == next_delta) ? prev_delta : 0.f;
532
36
      else if (target_val <= hb_min (prev_val, next_val))
533
10
  return (prev_val < next_val) ? prev_delta : next_delta;
534
26
      else if (target_val >= hb_max (prev_val, next_val))
535
18
  return (prev_val > next_val) ? prev_delta : next_delta;
536
537
      /* linear interpolation */
538
8
      float r = (target_val - prev_val) / (next_val - prev_val);
539
8
      return prev_delta + r * (next_delta - prev_delta);
540
188
    }
541
542
    static unsigned int next_index (unsigned int i, unsigned int start, unsigned int end)
543
328
    { 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
12.0M
    {
549
12.0M
      if (!font->num_coords) return true;
550
551
294k
      if (unlikely (glyph >= table->glyphCount)) return true;
552
553
163k
      hb_bytes_t var_data_bytes = table->get_glyph_var_data_bytes (table.get_blob (), glyph);
554
163k
      if (!var_data_bytes.as<GlyphVariationData> ()->has_data ()) return true;
555
138k
      hb_vector_t<unsigned int> shared_indices;
556
138k
      GlyphVariationData::tuple_iterator_t iterator;
557
138k
      if (!GlyphVariationData::get_tuple_iterator (var_data_bytes, table->axisCount,
558
138k
               shared_indices, &iterator))
559
34.9k
  return true; /* so isn't applied at all */
560
561
      /* Save original points for inferred delta calculation */
562
104k
      contour_point_vector_t orig_points;
563
104k
      if (unlikely (!orig_points.resize (points.length))) return false;
564
96.7M
      for (unsigned int i = 0; i < orig_points.length; i++)
565
96.6M
  orig_points.arrayZ[i] = points.arrayZ[i];
566
567
101k
      contour_point_vector_t deltas; /* flag is used to indicate referenced point */
568
101k
      if (unlikely (!deltas.resize (points.length))) return false;
569
570
99.6k
      hb_vector_t<unsigned> end_points;
571
93.5M
      for (unsigned i = 0; i < points.length; ++i)
572
93.4M
  if (points[i].is_end_point)
573
2.15k
    end_points.push (i);
574
575
99.6k
      auto coords = hb_array (font->coords, font->num_coords);
576
99.6k
      unsigned num_coords = table->axisCount;
577
99.6k
      hb_array_t<const F2DOT14> shared_tuples = (table+table->sharedTuples).as_array (table->sharedTupleCount * table->axisCount);
578
579
99.6k
      hb_vector_t<unsigned int> private_indices;
580
99.6k
      hb_vector_t<int> x_deltas;
581
99.6k
      hb_vector_t<int> y_deltas;
582
99.6k
      do
583
357k
      {
584
357k
  float scalar = iterator.current_tuple->calculate_scalar (coords, num_coords, shared_tuples);
585
357k
  if (scalar == 0.f) continue;
586
79.5k
  const HBUINT8 *p = iterator.get_serialized_data ();
587
79.5k
  unsigned int length = iterator.current_tuple->get_data_size ();
588
79.5k
  if (unlikely (!iterator.var_data_bytes.check_range (p, length)))
589
15.1k
    return false;
590
591
64.3k
  const HBUINT8 *end = p + length;
592
593
64.3k
  bool has_private_points = iterator.current_tuple->has_private_points ();
594
64.3k
  if (has_private_points &&
595
64.3k
      !GlyphVariationData::unpack_points (p, private_indices, end))
596
12.1k
    return false;
597
52.2k
  const hb_array_t<unsigned int> &indices = has_private_points ? private_indices : shared_indices;
598
599
52.2k
  bool apply_to_all = (indices.length == 0);
600
52.2k
  unsigned int num_deltas = apply_to_all ? points.length : indices.length;
601
52.2k
  if (unlikely (!x_deltas.resize (num_deltas))) return false;
602
51.2k
  if (unlikely (!GlyphVariationData::unpack_deltas (p, x_deltas, end))) return false;
603
31.2k
  if (unlikely (!y_deltas.resize (num_deltas))) return false;
604
30.5k
  if (unlikely (!GlyphVariationData::unpack_deltas (p, y_deltas, end))) return false;
605
606
3.38M
  for (unsigned int i = 0; i < deltas.length; i++)
607
3.36M
    deltas[i].init ();
608
485k
  for (unsigned int i = 0; i < num_deltas; i++)
609
463k
  {
610
463k
    unsigned int pt_index = apply_to_all ? i : indices[i];
611
463k
    if (unlikely (pt_index >= deltas.length)) continue;
612
308k
    deltas.arrayZ[pt_index].flag = 1; /* this point is referenced, i.e., explicit deltas specified */
613
308k
    deltas.arrayZ[pt_index].x += x_deltas.arrayZ[i] * scalar;
614
308k
    deltas.arrayZ[pt_index].y += y_deltas.arrayZ[i] * scalar;
615
308k
  }
616
617
  /* infer deltas for unreferenced points */
618
21.5k
  unsigned start_point = 0;
619
22.1k
  for (unsigned c = 0; c < end_points.length; c++)
620
510
  {
621
510
    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
510
    unsigned unref_count = 0;
625
3.97k
    for (unsigned i = start_point; i <= end_point; i++)
626
3.46k
      if (!deltas[i].flag) unref_count++;
627
628
510
    unsigned j = start_point;
629
510
    if (unref_count == 0 || unref_count > end_point - start_point)
630
486
      goto no_more_gaps;
631
632
24
    for (;;)
633
24
    {
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
24
      unsigned int prev, next, i;
638
24
      for (;;)
639
116
      {
640
116
        i = j;
641
116
        j = next_index (i, start_point, end_point);
642
116
        if (deltas[i].flag && !deltas[j].flag) break;
643
116
      }
644
24
      prev = j = i;
645
24
      for (;;)
646
118
      {
647
118
        i = j;
648
118
        j = next_index (i, start_point, end_point);
649
118
        if (!deltas[i].flag && deltas[j].flag) break;
650
118
      }
651
24
      next = j;
652
      /* Infer deltas for all unref points in the gap between prev and next */
653
24
      i = prev;
654
24
      for (;;)
655
94
      {
656
94
        i = next_index (i, start_point, end_point);
657
94
        if (i == next) break;
658
94
        deltas[i].x = infer_delta (orig_points.as_array (), deltas.as_array (), i, prev, next, &contour_point_t::x);
659
94
        deltas[i].y = infer_delta (orig_points.as_array (), deltas.as_array (), i, prev, next, &contour_point_t::y);
660
94
        if (--unref_count == 0) goto no_more_gaps;
661
94
      }
662
24
    }
663
510
  no_more_gaps:
664
510
    start_point = end_point + 1;
665
510
  }
666
667
  /* apply specified / inferred deltas to points */
668
3.38M
  for (unsigned int i = 0; i < points.length; i++)
669
3.36M
  {
670
3.36M
    points.arrayZ[i].x += deltas.arrayZ[i].x;
671
3.36M
    points.arrayZ[i].y += deltas.arrayZ[i].y;
672
3.36M
  }
673
300k
      } while (iterator.move_to_next ());
674
675
41.7k
      return true;
676
99.6k
    }
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
368k
  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 */