Coverage Report

Created: 2026-11-10 07:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/harfbuzz/src/hb-ot-var-fvar-table.hh
Line
Count
Source
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_FVAR_TABLE_HH
28
#define HB_OT_VAR_FVAR_TABLE_HH
29
30
#include "hb-open-type.hh"
31
32
/*
33
 * fvar -- Font Variations
34
 * https://docs.microsoft.com/en-us/typography/opentype/spec/fvar
35
 */
36
37
#define HB_OT_TAG_fvar HB_TAG('f','v','a','r')
38
39
40
namespace OT {
41
42
static bool axis_coord_pinned_or_within_axis_range (const hb_array_t<const F16DOT16> coords,
43
                                                    unsigned axis_index,
44
                                                    Triple axis_limit)
45
0
{
46
0
  float axis_coord = coords[axis_index].to_float ();
47
0
  if (axis_limit.is_point ())
48
0
  {
49
0
    if (axis_limit.minimum != axis_coord)
50
0
      return false;
51
0
  }
52
0
  else
53
0
  {
54
0
    if (axis_coord < axis_limit.minimum ||
55
0
        axis_coord > axis_limit.maximum)
56
0
      return false;
57
0
  }
58
0
  return true;
59
0
}
Unexecuted instantiation: hb-subset.cc:OT::axis_coord_pinned_or_within_axis_range(hb_array_t<OT::HBFixed<OT::NumType<true, int, 4u>, 16u> const>, unsigned int, Triple)
Unexecuted instantiation: hb-subset-plan.cc:OT::axis_coord_pinned_or_within_axis_range(hb_array_t<OT::HBFixed<OT::NumType<true, int, 4u>, 16u> const>, unsigned int, Triple)
Unexecuted instantiation: hb-subset-plan-var.cc:OT::axis_coord_pinned_or_within_axis_range(hb_array_t<OT::HBFixed<OT::NumType<true, int, 4u>, 16u> const>, unsigned int, Triple)
Unexecuted instantiation: hb-subset-table-var.cc:OT::axis_coord_pinned_or_within_axis_range(hb_array_t<OT::HBFixed<OT::NumType<true, int, 4u>, 16u> const>, unsigned int, Triple)
Unexecuted instantiation: hb-font.cc:OT::axis_coord_pinned_or_within_axis_range(hb_array_t<OT::HBFixed<OT::NumType<true, int, 4u>, 16u> const>, unsigned int, Triple)
Unexecuted instantiation: hb-ot-var.cc:OT::axis_coord_pinned_or_within_axis_range(hb_array_t<OT::HBFixed<OT::NumType<true, int, 4u>, 16u> const>, unsigned int, Triple)
60
61
struct InstanceRecord
62
{
63
  friend struct fvar;
64
65
  hb_array_t<const F16DOT16> get_coordinates (unsigned int axis_count) const
66
0
  { return coordinatesZ.as_array (axis_count); }
67
68
  bool keep_instance (unsigned axis_count,
69
                      const hb_map_t *axes_index_tag_map,
70
                      const hb_hashmap_t<hb_tag_t, Triple> *axes_location) const
71
0
  {
72
0
    if (axes_location->is_empty ()) return true;
73
0
    const hb_array_t<const F16DOT16> coords = get_coordinates (axis_count);
74
0
    for (unsigned i = 0 ; i < axis_count; i++)
75
0
    {
76
0
      uint32_t *axis_tag;
77
0
      if (!axes_index_tag_map->has (i, &axis_tag))
78
0
        return false;
79
0
      if (!axes_location->has (*axis_tag))
80
0
        continue;
81
82
0
      Triple axis_limit = axes_location->get (*axis_tag);
83
0
      if (!axis_coord_pinned_or_within_axis_range (coords, i, axis_limit))
84
0
        return false;
85
0
    }
86
0
    return true;
87
0
  }
88
89
  bool subset (hb_subset_context_t *c,
90
               unsigned axis_count,
91
               bool has_postscript_nameid) const
92
0
  {
93
0
    TRACE_SUBSET (this);
94
0
    if (unlikely (!c->serializer->embed (subfamilyNameID))) return_trace (false);
95
0
    if (unlikely (!c->serializer->embed (flags))) return_trace (false);
96
97
0
    const hb_array_t<const F16DOT16> coords = get_coordinates (axis_count);
98
0
    const hb_hashmap_t<hb_tag_t, Triple> *axes_location = &c->plan->user_axes_location;
99
0
    for (unsigned i = 0 ; i < axis_count; i++)
100
0
    {
101
0
      uint32_t *axis_tag;
102
0
      Triple *axis_limit;
103
      // only keep instances whose coordinates == pinned axis location
104
0
      if (!c->plan->axes_old_index_tag_map.has (i, &axis_tag)) return_trace (false);
105
0
      if (axes_location->has (*axis_tag, &axis_limit))
106
0
      {
107
0
        if (!axis_coord_pinned_or_within_axis_range (coords, i, *axis_limit))
108
0
          return_trace (false);
109
110
        //skip axes dropped from fvar (pinned; or self-contained under avar2,
111
        //where other pinned axes stay in fvar as hidden)
112
0
        if (axis_limit->is_point () && !c->plan->axes_index_map.has (i))
113
0
          continue;
114
0
      }
115
116
0
      if (!c->serializer->embed (coords[i]))
117
0
        return_trace (false);
118
0
    }
119
120
0
    if (has_postscript_nameid)
121
0
    {
122
0
      NameID name_id;
123
0
      name_id = StructAfter<NameID> (coords);
124
0
      if (!c->serializer->embed (name_id))
125
0
        return_trace (false);
126
0
    }
127
128
0
    return_trace (true);
129
0
  }
130
131
  bool sanitize (hb_sanitize_context_t *c, unsigned int axis_count) const
132
0
  {
133
0
    TRACE_SANITIZE (this);
134
0
    return_trace (c->check_struct (this) &&
135
0
      hb_barrier () &&
136
0
      c->check_array (coordinatesZ.arrayZ, axis_count));
137
0
  }
138
139
  protected:
140
  NameID  subfamilyNameID;/* The name ID for entries in the 'name' table
141
         * that provide subfamily names for this instance. */
142
  HBUINT16  flags;    /* Reserved for future use — set to 0. */
143
  UnsizedArrayOf<F16DOT16>
144
    coordinatesZ; /* The coordinates array for this instance. */
145
  //NameID  postScriptNameIDX;/*Optional. The name ID for entries in the 'name'
146
  //          * table that provide PostScript names for this
147
  //          * instance. */
148
149
  public:
150
  DEFINE_SIZE_UNBOUNDED (4);
151
};
152
153
struct AxisRecord
154
{
155
0
  int cmp (hb_tag_t key) const { return axisTag.cmp (key); }
156
157
  enum
158
  {
159
    AXIS_FLAG_HIDDEN  = 0x0001,
160
  };
161
162
#ifndef HB_DISABLE_DEPRECATED
163
  void get_axis_deprecated (hb_ot_var_axis_t *info) const
164
0
  {
165
0
    info->tag = axisTag;
166
0
    info->name_id = axisNameID;
167
0
    get_coordinates (info->min_value, info->default_value, info->max_value);
168
0
  }
169
#endif
170
171
  void get_axis_info (unsigned axis_index, hb_ot_var_axis_info_t *info) const
172
0
  {
173
0
    info->axis_index = axis_index;
174
0
    info->tag = axisTag;
175
0
    info->name_id = axisNameID;
176
0
    info->flags = (hb_ot_var_axis_flags_t) (unsigned int) flags;
177
0
    get_coordinates (info->min_value, info->default_value, info->max_value);
178
0
    info->reserved = 0;
179
0
  }
180
181
0
  hb_tag_t get_axis_tag () const { return axisTag; }
182
183
0
  bool is_hidden () const { return flags & AXIS_FLAG_HIDDEN; }
184
185
  float normalize_axis_value (float v) const
186
0
  {
187
0
    float min_value, default_value, max_value;
188
0
    get_coordinates (min_value, default_value, max_value);
189
190
0
    v = hb_clamp (v, min_value, max_value);
191
192
0
    if (v == default_value)
193
0
      return 0;
194
0
    else if (v < default_value)
195
0
      return (v - default_value) / (default_value - min_value);
196
0
    else
197
0
      return (v - default_value) / (max_value - default_value);
198
0
  }
199
200
0
  hb_ot_name_id_t get_name_id () const { return axisNameID; }
201
202
  bool sanitize (hb_sanitize_context_t *c) const
203
0
  {
204
0
    TRACE_SANITIZE (this);
205
0
    return_trace (c->check_struct (this));
206
0
  }
207
208
  void get_coordinates (float &min, float &default_, float &max) const
209
0
  {
210
0
    default_ = defaultValue.to_float ();
211
    /* Ensure order, to simplify client math. */
212
0
    min = hb_min (default_, minValue.to_float ());
213
0
    max = hb_max (default_, maxValue.to_float ());
214
0
  }
215
216
  float get_default () const
217
0
  {
218
0
    return defaultValue.to_float ();
219
0
  }
220
221
  TripleDistances get_triple_distances () const
222
0
  {
223
0
    float min, default_, max;
224
0
    get_coordinates (min, default_, max);
225
0
    return TripleDistances (min, default_, max);
226
0
  }
227
228
  bool subset (hb_subset_context_t *c) const
229
0
  {
230
0
    TRACE_SUBSET (this);
231
0
    auto *out = c->serializer->embed (this);
232
0
    if (unlikely (!out)) return_trace (false);
233
234
0
    const hb_hashmap_t<hb_tag_t, Triple>& user_axes_location = c->plan->user_axes_location;
235
0
    Triple *axis_limit;
236
0
    if (user_axes_location.has (axisTag, &axis_limit))
237
0
    {
238
0
      if (c->plan->has_avar2 && axis_limit->is_point ())
239
0
      {
240
        /* Pinned axis in avar2 mode: keep in fvar as hidden with
241
         * min=default=max=pinned_value. */
242
0
        out->minValue.set_float (axis_limit->middle);
243
0
        out->defaultValue.set_float (axis_limit->middle);
244
0
        out->maxValue.set_float (axis_limit->middle);
245
0
        out->flags = out->flags | (HBUINT16) AXIS_FLAG_HIDDEN;
246
0
      }
247
0
      else
248
0
      {
249
0
        out->minValue.set_float (axis_limit->minimum);
250
0
        out->defaultValue.set_float (axis_limit->middle);
251
0
        out->maxValue.set_float (axis_limit->maximum);
252
0
      }
253
0
    }
254
0
    return_trace (true);
255
0
  }
256
257
  public:
258
  Tag   axisTag;  /* Tag identifying the design variation for the axis. */
259
  protected:
260
  F16DOT16  minValue; /* The minimum coordinate value for the axis. */
261
  F16DOT16  defaultValue; /* The default coordinate value for the axis. */
262
  F16DOT16  maxValue; /* The maximum coordinate value for the axis. */
263
  public:
264
  HBUINT16  flags;    /* Axis flags. */
265
  NameID  axisNameID; /* The name ID for entries in the 'name' table that
266
         * provide a display name for this axis. */
267
268
  public:
269
  DEFINE_SIZE_STATIC (20);
270
};
271
272
struct fvar
273
{
274
  static constexpr hb_tag_t tableTag = HB_OT_TAG_fvar;
275
276
0
  bool has_data () const { return version.to_int (); }
277
278
  bool sanitize (hb_sanitize_context_t *c) const
279
0
  {
280
0
    TRACE_SANITIZE (this);
281
0
    return_trace (version.sanitize (c) &&
282
0
      hb_barrier () &&
283
0
      likely (version.major == 1) &&
284
0
      c->check_struct (this) &&
285
0
      hb_barrier () &&
286
0
      axisSize == 20 && /* Assumed in our code. */
287
0
      instanceSize >= axisCount * 4 + 4 &&
288
0
      get_axes ().sanitize (c) &&
289
0
      c->check_range (&StructAfter<InstanceRecord> (get_axes ()),
290
0
          instanceCount, instanceSize));
291
0
  }
292
293
0
  unsigned int get_axis_count () const { return axisCount; }
294
295
#ifndef HB_DISABLE_DEPRECATED
296
  unsigned int get_axes_deprecated (unsigned int      start_offset,
297
            unsigned int     *axes_count /* IN/OUT */,
298
            hb_ot_var_axis_t *axes_array /* OUT */) const
299
0
  {
300
0
    if (axes_count && axes_array)
301
0
    {
302
0
      hb_array_t<const AxisRecord> arr = get_axes ().sub_array (start_offset, axes_count);
303
0
      for (unsigned i = 0; i < arr.length; ++i)
304
0
  arr[i].get_axis_deprecated (&axes_array[i]);
305
0
    }
306
0
    return axisCount;
307
0
  }
308
#endif
309
310
  unsigned int get_axis_infos (unsigned int           start_offset,
311
             unsigned int          *axes_count /* IN/OUT */,
312
             hb_ot_var_axis_info_t *axes_array /* OUT */) const
313
0
  {
314
0
    if (axes_count && axes_array)
315
0
    {
316
0
      hb_array_t<const AxisRecord> arr = get_axes ().sub_array (start_offset, axes_count);
317
0
      for (unsigned i = 0; i < arr.length; ++i)
318
0
  arr[i].get_axis_info (start_offset + i, &axes_array[i]);
319
0
    }
320
0
    return axisCount;
321
0
  }
322
323
#ifndef HB_DISABLE_DEPRECATED
324
  bool
325
  find_axis_deprecated (hb_tag_t tag, unsigned *axis_index, hb_ot_var_axis_t *info) const
326
0
  {
327
0
    unsigned i;
328
0
    if (!axis_index) axis_index = &i;
329
0
    *axis_index = HB_OT_VAR_NO_AXIS_INDEX;
330
0
    auto axes = get_axes ();
331
0
    return axes.lfind (tag, axis_index) && ((void) axes[*axis_index].get_axis_deprecated (info), true);
332
0
  }
333
#endif
334
  bool
335
  find_axis_info (hb_tag_t tag, hb_ot_var_axis_info_t *info) const
336
0
  {
337
0
    unsigned i;
338
0
    auto axes = get_axes ();
339
0
    return axes.lfind (tag, &i) && ((void) axes[i].get_axis_info (i, info), true);
340
0
  }
341
342
  float normalize_axis_value (unsigned int axis_index, float v) const
343
0
  { return get_axes ()[axis_index].normalize_axis_value (v); }
344
345
0
  unsigned int get_instance_count () const { return instanceCount; }
346
347
  hb_ot_name_id_t get_instance_subfamily_name_id (unsigned int instance_index) const
348
0
  {
349
0
    const InstanceRecord *instance = get_instance (instance_index);
350
0
    if (unlikely (!instance)) return HB_OT_NAME_ID_INVALID;
351
0
    return instance->subfamilyNameID;
352
0
  }
353
354
  hb_ot_name_id_t get_instance_postscript_name_id (unsigned int instance_index) const
355
0
  {
356
0
    const InstanceRecord *instance = get_instance (instance_index);
357
0
    if (unlikely (!instance)) return HB_OT_NAME_ID_INVALID;
358
0
    if (instanceSize >= axisCount * 4 + 6)
359
0
      return StructAfter<NameID> (instance->get_coordinates (axisCount));
360
0
    return HB_OT_NAME_ID_INVALID;
361
0
  }
362
363
  unsigned int get_instance_coords (unsigned int  instance_index,
364
            unsigned int *coords_length, /* IN/OUT */
365
            float        *coords         /* OUT */) const
366
0
  {
367
0
    const InstanceRecord *instance = get_instance (instance_index);
368
0
    if (unlikely (!instance))
369
0
    {
370
0
      if (coords_length)
371
0
  *coords_length = 0;
372
0
      return 0;
373
0
    }
374
375
0
    if (coords_length && *coords_length && coords)
376
0
    {
377
0
      hb_array_t<const F16DOT16> instanceCoords = instance->get_coordinates (axisCount)
378
0
               .sub_array (0, coords_length);
379
0
      for (unsigned int i = 0; i < instanceCoords.length; i++)
380
0
  coords[i] = instanceCoords.arrayZ[i].to_float ();
381
0
    }
382
0
    return axisCount;
383
0
  }
384
385
  void collect_name_ids (hb_hashmap_t<hb_tag_t, Triple> *user_axes_location,
386
       hb_map_t *axes_old_index_tag_map,
387
       hb_set_t *nameids  /* IN/OUT */) const
388
0
  {
389
0
    if (!has_data ()) return;
390
391
0
    auto axis_records = get_axes ();
392
0
    for (unsigned i = 0 ; i < (unsigned)axisCount; i++)
393
0
    {
394
0
      hb_tag_t axis_tag = axis_records[i].get_axis_tag ();
395
0
      if (user_axes_location->has (axis_tag) &&
396
0
          user_axes_location->get (axis_tag).is_point ())
397
0
        continue;
398
399
0
      nameids->add (axis_records[i].get_name_id ());
400
0
    }
401
402
0
    for (unsigned i = 0 ; i < (unsigned)instanceCount; i++)
403
0
    {
404
0
      const InstanceRecord *instance = get_instance (i);
405
406
0
      if (!instance->keep_instance (axisCount, axes_old_index_tag_map, user_axes_location))
407
0
        continue;
408
409
0
      nameids->add (instance->subfamilyNameID);
410
411
0
      if (instanceSize >= axisCount * 4 + 6)
412
0
      {
413
0
        unsigned post_script_name_id = StructAfter<NameID> (instance->get_coordinates (axisCount));
414
0
        if (post_script_name_id != HB_OT_NAME_ID_INVALID) nameids->add (post_script_name_id);
415
0
      }
416
0
    }
417
0
  }
418
419
  bool subset (hb_subset_context_t *c) const
420
0
  {
421
0
    TRACE_SUBSET (this);
422
0
    unsigned retained_axis_count = c->plan->axes_index_map.get_population ();
423
0
    if (!retained_axis_count) //all axes are pinned
424
0
      return_trace (false);
425
426
0
    fvar *out = c->serializer->embed (this);
427
0
    if (unlikely (!out)) return_trace (false);
428
429
0
    if (!c->serializer->check_assign (out->axisCount, retained_axis_count, HB_SERIALIZE_ERROR_INT_OVERFLOW))
430
0
      return_trace (false);
431
432
0
    bool has_postscript_nameid = false;
433
0
    if (instanceSize >= axisCount * 4 + 6)
434
0
      has_postscript_nameid = true;
435
436
0
    if (!c->serializer->check_assign (out->instanceSize, retained_axis_count * 4 + (has_postscript_nameid ? 6 : 4),
437
0
                                      HB_SERIALIZE_ERROR_INT_OVERFLOW))
438
0
      return_trace (false);
439
440
0
    auto axes_records = get_axes ();
441
0
    for (unsigned i = 0 ; i < (unsigned)axisCount; i++)
442
0
    {
443
0
      if (!c->plan->axes_index_map.has (i)) continue;
444
0
      if (unlikely (!axes_records[i].subset (c)))
445
0
        return_trace (false);
446
0
    }
447
448
0
    if (!c->serializer->check_assign (out->firstAxis, get_size (), HB_SERIALIZE_ERROR_INT_OVERFLOW))
449
0
      return_trace (false);
450
451
0
    unsigned num_retained_instances = 0;
452
0
    for (unsigned i = 0 ; i < (unsigned)instanceCount; i++)
453
0
    {
454
0
      const InstanceRecord *instance = get_instance (i);
455
0
      auto snap = c->serializer->snapshot ();
456
0
      if (!instance->subset (c, axisCount, has_postscript_nameid))
457
0
        c->serializer->revert (snap);
458
0
      else
459
0
        num_retained_instances++;
460
0
    }
461
462
0
    return_trace (c->serializer->check_assign (out->instanceCount, num_retained_instances, HB_SERIALIZE_ERROR_INT_OVERFLOW));
463
0
  }
464
465
  public:
466
  hb_array_t<const AxisRecord> get_axes () const
467
0
  { return hb_array (&(this+firstAxis), axisCount); }
468
469
  const InstanceRecord *get_instance (unsigned int i) const
470
0
  {
471
0
    if (unlikely (i >= instanceCount)) return nullptr;
472
0
   return &StructAtOffset<InstanceRecord> (&StructAfter<InstanceRecord> (get_axes ()),
473
0
             i * instanceSize);
474
0
  }
475
476
  protected:
477
  FixedVersion<>version;  /* Version of the fvar table
478
         * initially set to 0x00010000u */
479
  Offset16To<AxisRecord>
480
    firstAxis;  /* Offset in bytes from the beginning of the table
481
         * to the start of the AxisRecord array. */
482
  HBUINT16  reserved; /* This field is permanently reserved. Set to 2. */
483
  HBUINT16  axisCount;  /* The number of variation axes in the font (the
484
         * number of records in the axes array). */
485
  HBUINT16  axisSize; /* The size in bytes of each VariationAxisRecord —
486
         * set to 20 (0x0014) for this version. */
487
  HBUINT16  instanceCount;  /* The number of named instances defined in the font
488
         * (the number of records in the instances array). */
489
  HBUINT16  instanceSize; /* The size in bytes of each InstanceRecord — set
490
         * to either axisCount * sizeof(F16DOT16) + 4, or to
491
         * axisCount * sizeof(F16DOT16) + 6. */
492
493
  public:
494
  DEFINE_SIZE_STATIC (16);
495
};
496
497
} /* namespace OT */
498
499
500
#endif /* HB_OT_VAR_FVAR_TABLE_HH */