Coverage Report

Created: 2025-07-11 06:34

/src/harfbuzz/src/hb-ot-var-avar-table.hh
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright © 2017  Google, Inc.
3
 *
4
 *  This is part of HarfBuzz, a text shaping library.
5
 *
6
 * Permission is hereby granted, without written agreement and without
7
 * license or royalty fees, to use, copy, modify, and distribute this
8
 * software and its documentation for any purpose, provided that the
9
 * above copyright notice and the following two paragraphs appear in
10
 * all copies of this software.
11
 *
12
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16
 * DAMAGE.
17
 *
18
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23
 *
24
 * Google Author(s): Behdad Esfahbod
25
 */
26
27
#ifndef HB_OT_VAR_AVAR_TABLE_HH
28
#define HB_OT_VAR_AVAR_TABLE_HH
29
30
#include "hb-open-type.hh"
31
#include "hb-ot-var-common.hh"
32
33
34
/*
35
 * avar -- Axis Variations
36
 * https://docs.microsoft.com/en-us/typography/opentype/spec/avar
37
 */
38
39
#define HB_OT_TAG_avar HB_TAG('a','v','a','r')
40
41
42
namespace OT {
43
44
45
/* "Spec": https://github.com/be-fonts/boring-expansion-spec/issues/14 */
46
struct avarV2Tail
47
{
48
  friend struct avar;
49
50
  bool sanitize (hb_sanitize_context_t *c,
51
     const void *base) const
52
0
  {
53
0
    TRACE_SANITIZE (this);
54
0
    return_trace (varIdxMap.sanitize (c, base) &&
55
0
      varStore.sanitize (c, base));
56
0
  }
57
58
  protected:
59
  Offset32To<DeltaSetIndexMap>  varIdxMap;  /* Offset from the beginning of 'avar' table. */
60
  Offset32To<ItemVariationStore>  varStore; /* Offset from the beginning of 'avar' table. */
61
62
  public:
63
  DEFINE_SIZE_STATIC (8);
64
};
65
66
67
struct AxisValueMap
68
{
69
  bool sanitize (hb_sanitize_context_t *c) const
70
0
  {
71
0
    TRACE_SANITIZE (this);
72
0
    return_trace (c->check_struct (this));
73
0
  }
74
75
  void set_mapping (float from_coord, float to_coord)
76
0
  {
77
0
    coords[0].set_float (from_coord);
78
0
    coords[1].set_float (to_coord);
79
0
  }
80
81
  bool is_outside_axis_range (const Triple& axis_range) const
82
0
  {
83
0
    double from_coord = (double) coords[0].to_float ();
84
0
    return !axis_range.contains (from_coord);
85
0
  }
86
87
  bool must_include () const
88
0
  {
89
0
    float from_coord = coords[0].to_float ();
90
0
    float to_coord = coords[1].to_float ();
91
0
    return (from_coord == -1.f && to_coord == -1.f) ||
92
0
           (from_coord == 0.f && to_coord == 0.f) ||
93
0
           (from_coord == 1.f && to_coord == 1.f);
94
0
  }
95
96
  void instantiate (const Triple& axis_range,
97
                    const Triple& unmapped_range,
98
                    const TripleDistances& triple_distances)
99
0
  {
100
0
    float from_coord = coords[0].to_float ();
101
0
    float to_coord = coords[1].to_float ();
102
0
103
0
    from_coord = renormalizeValue ((double) from_coord, unmapped_range, triple_distances);
104
0
    to_coord = renormalizeValue ((double) to_coord, axis_range, triple_distances);
105
0
106
0
    coords[0].set_float (from_coord);
107
0
    coords[1].set_float (to_coord);
108
0
  }
109
110
  HB_INTERNAL static int cmp (const void *pa, const void *pb)
111
0
  {
112
0
    const AxisValueMap *a = (const AxisValueMap *) pa;
113
0
    const AxisValueMap *b = (const AxisValueMap *) pb;
114
0
115
0
    int a_from = a->coords[0].to_int ();
116
0
    int b_from = b->coords[0].to_int ();
117
0
    if (a_from != b_from)
118
0
      return a_from - b_from;
119
0
120
0
    /* this should never be reached. according to the spec, all of the axis
121
0
     * value map records for a given axis must have different fromCoord values
122
0
     * */
123
0
    int a_to = a->coords[1].to_int ();
124
0
    int b_to = b->coords[1].to_int ();
125
0
    return a_to - b_to;
126
0
  }
127
128
  bool serialize (hb_serialize_context_t *c) const
129
0
  {
130
0
    TRACE_SERIALIZE (this);
131
0
    return_trace (c->embed (this));
132
0
  }
133
134
  public:
135
  F2DOT14 coords[2];
136
//   F2DOT14  fromCoord;  /* A normalized coordinate value obtained using
137
//         * default normalization. */
138
//   F2DOT14  toCoord;  /* The modified, normalized coordinate value. */
139
140
  public:
141
  DEFINE_SIZE_STATIC (4);
142
};
143
144
struct SegmentMaps : Array16Of<AxisValueMap>
145
{
146
  float map_float (float value, unsigned int from_offset = 0, unsigned int to_offset = 1) const
147
0
  {
148
0
#define fromCoord coords[from_offset].to_float ()
149
0
#define toCoord coords[to_offset].to_float ()
150
151
0
    const auto *map = arrayZ;
152
153
    /* The following special-cases are not part of OpenType, which requires
154
     * that at least -1, 0, and +1 must be mapped. But we include these as
155
     * part of a better error recovery scheme. */
156
0
    if (len < 2)
157
0
    {
158
0
      if (!len)
159
0
  return value;
160
0
      else /* len == 1*/
161
0
  return value - map[0].fromCoord + map[0].toCoord;
162
0
    }
163
164
    // At least two mappings now.
165
166
    /* CoreText is wild...
167
     * PingFangUI avar needs all this special-casing...
168
     * So we implement an extended version of the spec here,
169
     * which is more robust and more likely to be compatible with
170
     * the wild. */
171
172
0
    unsigned start = 0;
173
0
    unsigned end = len;
174
0
    if (map[start].fromCoord == -1 && map[start].toCoord == -1 and map[start+1].fromCoord == -1)
175
0
      start++;
176
0
    if (map[end-1].fromCoord == +1 && map[end-1].toCoord == +1 and map[end-2].fromCoord == +1)
177
0
      end--;
178
179
    /* Look for exact match first, and do lots of special-casing. */
180
0
    unsigned i;
181
0
    for (i = start; i < end; i++)
182
0
      if (value == map[i].fromCoord)
183
0
  break;
184
0
    if (i < end)
185
0
    {
186
      // There's at least one exact match. See if there are more.
187
0
      unsigned j = i;
188
0
      for (; j + 1 < end; j++)
189
0
  if (value != map[j + 1].fromCoord)
190
0
    break;
191
192
      // [i,j] inclusive are all exact matches:
193
194
      // If there's only one, return it. This is the only spec-compliant case.
195
0
      if (i == j)
196
0
  return map[i].toCoord;
197
      // If there's exactly three, return the middle one.
198
0
      if (i + 2 == j)
199
0
  return map[i + 1].toCoord;
200
201
      // Ignore the middle ones. Return the one mapping closer to 0.
202
0
      if (value < 0) return map[j].toCoord;
203
0
      if (value > 0) return map[i].toCoord;
204
205
      // Mapping 0? CoreText seems confused. It seems to prefer 0 here...
206
      // So we'll just return the smallest one. lol
207
0
      return fabsf (map[i].toCoord) < fabsf (map[j].toCoord) ? map[i].toCoord : map[j].toCoord;
208
209
      // Mapping 0? Return one not mapping to 0.
210
0
      if (map[i].toCoord == 0)
211
0
  return map[j].toCoord;
212
0
      else
213
0
  return map[i].toCoord;
214
0
    }
215
216
    /* There's at least two and we're not an exact match. Prepare to lerp. */
217
218
    // Find the segment we're in.
219
0
    for (i = start; i < end; i++)
220
0
      if (value < map[i].fromCoord)
221
0
  break;
222
223
0
    if (i == 0)
224
0
    {
225
      // Value before all segments; Shift.
226
0
      return value - map[0].fromCoord + map[0].toCoord;
227
0
    }
228
0
    if (i == end)
229
0
    {
230
      // Value after all segments; Shift.
231
0
      return value - map[end - 1].fromCoord + map[end - 1].toCoord;
232
0
    }
233
234
    // Actually interpolate.
235
0
    auto &before = map[i-1];
236
0
    auto &after = map[i];
237
0
    float denom = after.fromCoord - before.fromCoord; // Can't be zero by now.
238
0
    return before.toCoord + ((after.toCoord - before.toCoord) * (value - before.fromCoord)) / denom;
239
240
0
#undef toCoord
241
0
#undef fromCoord
242
0
  }
243
244
0
  float unmap_float (float value) const { return map_float (value, 1, 0); }
245
246
247
  // TODO Kill this.
248
  Triple unmap_axis_range (const Triple& axis_range) const
249
0
  {
250
0
    float unmapped_min = unmap_float (axis_range.minimum);
251
0
    float unmapped_middle = unmap_float (axis_range.middle);
252
0
    float unmapped_max = unmap_float (axis_range.maximum);
253
0
254
0
    return Triple{(double) unmapped_min, (double) unmapped_middle, (double) unmapped_max};
255
0
  }
256
257
  bool subset (hb_subset_context_t *c, hb_tag_t axis_tag) const
258
0
  {
259
0
    TRACE_SUBSET (this);
260
0
261
0
    /* This function cannot work on avar2 table (and currently doesn't).
262
0
     * We should instead keep the design coords in the shape plan and use
263
0
     * those. unmap_axis_range needs to be killed. */
264
0
265
0
    /* avar mapped normalized axis range*/
266
0
    Triple *axis_range;
267
0
    if (!c->plan->axes_location.has (axis_tag, &axis_range))
268
0
      return c->serializer->embed (*this);
269
0
270
0
    TripleDistances *axis_triple_distances;
271
0
    if (!c->plan->axes_triple_distances.has (axis_tag, &axis_triple_distances))
272
0
      return_trace (false);
273
0
274
0
    auto *out = c->serializer->start_embed (this);
275
0
    if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
276
0
277
0
    Triple unmapped_range = unmap_axis_range (*axis_range);
278
0
279
0
    /* create a vector of retained mappings and sort */
280
0
    hb_vector_t<AxisValueMap> value_mappings;
281
0
    for (const auto& _ : as_array ())
282
0
    {
283
0
      if (_.is_outside_axis_range (unmapped_range))
284
0
        continue;
285
0
      AxisValueMap mapping;
286
0
      mapping = _;
287
0
      mapping.instantiate (*axis_range, unmapped_range, *axis_triple_distances);
288
0
      /* (-1, -1), (0, 0), (1, 1) mappings will be added later, so avoid
289
0
       * duplicates here */
290
0
      if (mapping.must_include ())
291
0
        continue;
292
0
      value_mappings.push (mapping);
293
0
    }
294
0
295
0
    AxisValueMap m;
296
0
    m.set_mapping (-1.f, -1.f);
297
0
    value_mappings.push (m);
298
0
299
0
    m.set_mapping (0.f, 0.f);
300
0
    value_mappings.push (m);
301
0
302
0
    m.set_mapping (1.f, 1.f);
303
0
    value_mappings.push (m);
304
0
305
0
    value_mappings.qsort ();
306
0
307
0
    for (const auto& _ : value_mappings)
308
0
    {
309
0
      if (!_.serialize (c->serializer))
310
0
        return_trace (false);
311
0
    }
312
0
    return_trace (c->serializer->check_assign (out->len, value_mappings.length, HB_SERIALIZE_ERROR_INT_OVERFLOW));
313
0
  }
314
315
  public:
316
  DEFINE_SIZE_ARRAY (2, *this);
317
};
318
319
struct avar
320
{
321
  static constexpr hb_tag_t tableTag = HB_OT_TAG_avar;
322
323
0
  bool has_data () const { return version.to_int (); }
324
325
  const SegmentMaps* get_segment_maps () const
326
0
  { return &firstAxisSegmentMaps; }
327
328
  unsigned get_axis_count () const
329
0
  { return axisCount; }
330
331
  bool sanitize (hb_sanitize_context_t *c) const
332
0
  {
333
0
    TRACE_SANITIZE (this);
334
0
    if (!(version.sanitize (c) &&
335
0
    hb_barrier () &&
336
0
    (version.major == 1
337
0
#ifndef HB_NO_AVAR2
338
0
     || version.major == 2
339
0
#endif
340
0
     ) &&
341
0
    c->check_struct (this)))
342
0
      return_trace (false);
343
344
0
    const SegmentMaps *map = &firstAxisSegmentMaps;
345
0
    unsigned int count = axisCount;
346
0
    for (unsigned int i = 0; i < count; i++)
347
0
    {
348
0
      if (unlikely (!map->sanitize (c)))
349
0
  return_trace (false);
350
0
      map = &StructAfter<SegmentMaps> (*map);
351
0
    }
352
353
0
#ifndef HB_NO_AVAR2
354
0
    if (version.major < 2)
355
0
      return_trace (true);
356
0
    hb_barrier ();
357
358
0
    const auto &v2 = * (const avarV2Tail *) map;
359
0
    if (unlikely (!v2.sanitize (c, this)))
360
0
      return_trace (false);
361
0
#endif
362
363
0
    return_trace (true);
364
0
  }
365
366
  void map_coords_16_16 (int *coords, unsigned int coords_length) const
367
8.00k
  {
368
8.00k
    unsigned int count = hb_min (coords_length, axisCount);
369
370
8.00k
    const SegmentMaps *map = &firstAxisSegmentMaps;
371
8.00k
    for (unsigned int i = 0; i < count; i++)
372
0
    {
373
0
      coords[i] = roundf (map->map_float (coords[i] / 65536.f) * 65536.f);
374
0
      map = &StructAfter<SegmentMaps> (*map);
375
0
    }
376
377
8.00k
#ifndef HB_NO_AVAR2
378
8.00k
    if (version.major < 2)
379
8.00k
      return;
380
0
    hb_barrier ();
381
382
0
    for (; count < axisCount; count++)
383
0
      map = &StructAfter<SegmentMaps> (*map);
384
385
0
    const auto &v2 = * (const avarV2Tail *) map;
386
387
0
    const auto &varidx_map = this+v2.varIdxMap;
388
0
    const auto &var_store = this+v2.varStore;
389
0
    auto *var_store_cache = var_store.create_cache ();
390
391
0
    hb_vector_t<int> out;
392
0
    out.alloc (coords_length);
393
0
    for (unsigned i = 0; i < coords_length; i++)
394
0
    {
395
0
      int v = coords[i];
396
0
      uint32_t varidx = varidx_map.map (i);
397
0
      float delta = var_store.get_delta (varidx, coords, coords_length, var_store_cache);
398
0
      v += roundf (delta * 4); // 2.14 -> 16.16
399
0
      v = hb_clamp (v, -(1<<16), +(1<<16));
400
0
      out.push (v);
401
0
    }
402
0
    for (unsigned i = 0; i < coords_length; i++)
403
0
      coords[i] = out[i];
404
405
0
    OT::ItemVariationStore::destroy_cache (var_store_cache);
406
0
#endif
407
0
  }
408
409
  bool subset (hb_subset_context_t *c) const
410
0
  {
411
0
    TRACE_SUBSET (this);
412
0
    unsigned retained_axis_count = c->plan->axes_index_map.get_population ();
413
0
    if (!retained_axis_count) //all axes are pinned/dropped
414
0
      return_trace (false);
415
0
416
0
    avar *out = c->serializer->allocate_min<avar> ();
417
0
    if (unlikely (!out)) return_trace (false);
418
0
419
0
    out->version.major = 1;
420
0
    out->version.minor = 0;
421
0
    if (!c->serializer->check_assign (out->axisCount, retained_axis_count, HB_SERIALIZE_ERROR_INT_OVERFLOW))
422
0
      return_trace (false);
423
0
424
0
    const hb_map_t& axes_index_map = c->plan->axes_index_map;
425
0
    const SegmentMaps *map = &firstAxisSegmentMaps;
426
0
    unsigned count = axisCount;
427
0
    for (unsigned int i = 0; i < count; i++)
428
0
    {
429
0
      if (axes_index_map.has (i))
430
0
      {
431
0
        hb_tag_t *axis_tag;
432
0
        if (!c->plan->axes_old_index_tag_map.has (i, &axis_tag))
433
0
          return_trace (false);
434
0
        if (!map->subset (c, *axis_tag))
435
0
          return_trace (false);
436
0
      }
437
0
      map = &StructAfter<SegmentMaps> (*map);
438
0
    }
439
0
    return_trace (true);
440
0
  }
441
442
  protected:
443
  FixedVersion<>version;  /* Version of the avar table
444
         * initially set to 0x00010000u */
445
  HBUINT16  reserved; /* This field is permanently reserved. Set to 0. */
446
  HBUINT16  axisCount;  /* The number of variation axes in the font. This
447
         * must be the same number as axisCount in the
448
         * 'fvar' table. */
449
  SegmentMaps firstAxisSegmentMaps;
450
451
  public:
452
  DEFINE_SIZE_MIN (8);
453
};
454
455
} /* namespace OT */
456
457
458
#endif /* HB_OT_VAR_AVAR_TABLE_HH */