Coverage Report

Created: 2026-08-31 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/harfbuzz/src/hb-ot-var-hvar-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_HVAR_TABLE_HH
28
#define HB_OT_VAR_HVAR_TABLE_HH
29
30
#include "hb-ot-layout-common.hh"
31
#include "hb-ot-var-common.hh"
32
33
namespace OT {
34
35
36
struct index_map_subset_plan_t
37
{
38
  enum index_map_index_t {
39
    ADV_INDEX,
40
    LSB_INDEX,  /* dual as TSB */
41
    RSB_INDEX,  /* dual as BSB */
42
    VORG_INDEX
43
  };
44
45
  void init (const DeltaSetIndexMap  *index_map,
46
       hb_inc_bimap_t      &outer_map,
47
       hb_vector_t<hb_set_t *> &inner_sets,
48
       const hb_subset_plan_t  *plan,
49
       bool bypass_empty = true)
50
0
  {
51
0
    map_count = 0;
52
0
    full_map_count = plan->new_to_old_gid_list.length ?
53
0
         plan->new_to_old_gid_list.tail ().first + 1 : 0;
54
0
    /* Only the advance mapping may be dropped in favor of implicit
55
0
     * glyph-id indexing (for the others a null offset means no deltas),
56
0
     * and only when the source was map-less too, preserving its form. */
57
0
    can_drop_to_implicit = !bypass_empty && !index_map;
58
0
    max_inners.init ();
59
0
    output_map.init ();
60
0
61
0
    if (bypass_empty && (!index_map || !index_map->get_map_count ())) return;
62
0
63
0
    unsigned int  last_val = (unsigned int)-1;
64
0
    hb_codepoint_t  last_gid = HB_CODEPOINT_INVALID;
65
0
66
0
    max_inners.resize (inner_sets.length);
67
0
    for (unsigned i = 0; i < inner_sets.length; i++) max_inners[i] = 0;
68
0
69
0
    /* Search backwards for a map value different from the last map value */
70
0
    auto &new_to_old_gid_list = plan->new_to_old_gid_list;
71
0
    unsigned count = new_to_old_gid_list.length;
72
0
    if (!index_map)
73
0
    {
74
0
      map_count = new_to_old_gid_list.tail ().first + 1;
75
0
    }
76
0
    else
77
0
    {
78
0
      for (unsigned j = count; j; j--)
79
0
      {
80
0
        hb_codepoint_t gid = new_to_old_gid_list.arrayZ[j - 1].first;
81
0
        hb_codepoint_t old_gid = new_to_old_gid_list.arrayZ[j - 1].second;
82
0
  
83
0
        unsigned int v = index_map->map (old_gid);
84
0
        if (last_gid == HB_CODEPOINT_INVALID)
85
0
        {
86
0
    last_val = v;
87
0
    last_gid = gid;
88
0
    continue;
89
0
        }
90
0
        if (v != last_val)
91
0
    break;
92
0
  
93
0
        last_gid = gid;
94
0
      }
95
0
  
96
0
      if (unlikely (last_gid == (hb_codepoint_t)-1)) return;
97
0
      map_count = last_gid + 1;
98
0
    }
99
0
100
0
    for (auto _ : plan->new_to_old_gid_list)
101
0
    {
102
0
      hb_codepoint_t gid = _.first;
103
0
      if (gid >= map_count) break;
104
0
105
0
      hb_codepoint_t old_gid = _.second;
106
0
      unsigned int v = index_map ? index_map->map (old_gid): old_gid;
107
0
      unsigned int outer = v >> 16;
108
0
      unsigned int inner = v & 0xFFFF;
109
0
      outer_map.add (outer);
110
0
      if (inner > max_inners[outer]) max_inners[outer] = inner;
111
0
      if (outer >= inner_sets.length) return;
112
0
      inner_sets[outer]->add (inner);
113
0
    }
114
0
  }
115
116
  void fini ()
117
0
  {
118
0
    max_inners.fini ();
119
0
    output_map.fini ();
120
0
  }
121
122
  void remap (const DeltaSetIndexMap *input_map,
123
        const hb_inc_bimap_t &outer_map,
124
        const hb_vector_t<hb_inc_bimap_t> &inner_maps,
125
        const hb_subset_plan_t *plan)
126
0
  {
127
0
    outer_bit_count = 1;
128
0
    inner_bit_count = 1;
129
0
130
0
    for (unsigned int i = 0; i < max_inners.length; i++)
131
0
    {
132
0
      if (inner_maps[i].get_population () == 0) continue;
133
0
      unsigned int bit_count = (max_inners[i]==0)? 1: hb_bit_storage (inner_maps[i][max_inners[i]]);
134
0
      if (bit_count > inner_bit_count) inner_bit_count = bit_count;
135
0
    }
136
0
137
0
    if (unlikely (!output_map.resize (map_count))) return;
138
0
    for (const auto &_ : plan->new_to_old_gid_list)
139
0
    {
140
0
      hb_codepoint_t new_gid = _.first;
141
0
      hb_codepoint_t old_gid = _.second;
142
0
143
0
      if (unlikely (new_gid >= map_count)) break;
144
0
145
0
      uint32_t v = input_map? input_map->map (old_gid) : old_gid;
146
0
      unsigned outer = v >> 16;
147
0
      unsigned new_outer = outer_map[outer];
148
0
      unsigned bit_count = (new_outer == 0) ? 1 : hb_bit_storage (new_outer);
149
0
      outer_bit_count = hb_max (bit_count, outer_bit_count);
150
0
151
0
      output_map.arrayZ[new_gid] = (new_outer << 16) | (inner_maps[outer][v & 0xFFFF]);
152
0
    }
153
0
  }
154
155
  bool remap_after_instantiation (const hb_subset_plan_t *plan,
156
                                  const hb_map_t& varidx_map)
157
0
  {
158
0
    /* recalculate bit_count after remapping */
159
0
    outer_bit_count = 1;
160
0
    inner_bit_count = 1;
161
0
162
0
    for (const auto &_ : plan->new_to_old_gid_list)
163
0
    {
164
0
      hb_codepoint_t new_gid = _.first;
165
0
      if (unlikely (new_gid >= map_count)) break;
166
0
167
0
      uint32_t v = output_map.arrayZ[new_gid];
168
0
      uint32_t *new_varidx;
169
0
      if (!varidx_map.has (v, &new_varidx))
170
0
        return false;
171
0
172
0
      output_map.arrayZ[new_gid] = *new_varidx;
173
0
174
0
      unsigned outer = (*new_varidx) >> 16;
175
0
      unsigned bit_count = (outer == 0) ? 1 : hb_bit_storage (outer);
176
0
      outer_bit_count = hb_max (bit_count, outer_bit_count);
177
0
178
0
      unsigned inner = (*new_varidx) & 0xFFFF;
179
0
      bit_count = (inner == 0) ? 1 : hb_bit_storage (inner);
180
0
      inner_bit_count = hb_max (bit_count, inner_bit_count);
181
0
    }
182
0
    return true;
183
0
  }
184
185
0
  unsigned int get_inner_bit_count () const { return inner_bit_count; }
186
0
  unsigned int get_width ()           const { return ((outer_bit_count + inner_bit_count + 7) / 8); }
187
0
  unsigned int get_map_count ()       const { return map_count; }
188
189
  size_t get_size () const
190
0
  {
191
0
    if (!map_count) return 0;
192
0
    return hb_unsigned_mul_add_saturate (get_width (), map_count,
193
0
           DeltaSetIndexMap::min_size);
194
0
  }
195
196
  bool is_identity () const
197
0
  {
198
0
    if (!output_map) return true;
199
0
    /* An advance map whose entries are the identity over every output
200
0
     * glyph is equivalent to implicit glyph-id indexing; drop it.  A
201
0
     * truncated map doesn't qualify: glyphs past its end share its last
202
0
     * value, not their own ids. */
203
0
    if (!can_drop_to_implicit || map_count != full_map_count) return false;
204
0
    for (unsigned i = 0; i < map_count; i++)
205
0
      if (output_map.arrayZ[i] != i) return false;
206
0
    return true;
207
0
  }
208
0
  hb_array_t<const uint32_t> get_output_map () const { return output_map.as_array (); }
209
210
  protected:
211
  unsigned int map_count;
212
  unsigned int full_map_count;
213
  bool can_drop_to_implicit;
214
  hb_vector_t<unsigned int> max_inners;
215
  unsigned int outer_bit_count;
216
  unsigned int inner_bit_count;
217
  hb_vector_t<uint32_t> output_map;
218
};
219
220
struct hvarvvar_subset_plan_t
221
{
222
0
  hvarvvar_subset_plan_t() : inner_maps (), index_map_plans () {}
223
0
  ~hvarvvar_subset_plan_t() { fini (); }
224
225
  void init (const hb_array_t<const DeltaSetIndexMap *> &index_maps,
226
       const ItemVariationStore &_var_store,
227
       const hb_subset_plan_t *plan)
228
0
  {
229
0
    index_map_plans.resize (index_maps.length);
230
0
231
0
    var_store = &_var_store;
232
0
    inner_sets.resize (var_store->get_sub_table_count ());
233
0
    for (unsigned int i = 0; i < inner_sets.length; i++)
234
0
      inner_sets[i] = hb_set_create ();
235
0
    adv_set = hb_set_create ();
236
0
237
0
    inner_maps.resize (var_store->get_sub_table_count ());
238
0
239
0
    if (unlikely (!index_map_plans.length || !inner_sets.length || !inner_maps.length)) return;
240
0
241
0
    bool retain_adv_map = false;
242
0
    index_map_plans[0].init (index_maps[0], outer_map, inner_sets, plan, false);
243
0
    if (!index_maps[0])
244
0
    {
245
0
      retain_adv_map = plan->flags & HB_SUBSET_FLAGS_RETAIN_GIDS;
246
0
      outer_map.add (0);
247
0
      for (hb_codepoint_t old_gid : plan->glyphset()->iter())
248
0
        inner_sets[0]->add (old_gid);
249
0
      hb_set_union (adv_set, inner_sets[0]);
250
0
    }
251
0
252
0
    for (unsigned int i = 1; i < index_maps.length; i++)
253
0
      index_map_plans[i].init (index_maps[i], outer_map, inner_sets, plan);
254
0
255
0
    outer_map.sort ();
256
0
257
0
    if (retain_adv_map)
258
0
    {
259
0
      for (const auto &_ : plan->new_to_old_gid_list)
260
0
      {
261
0
        hb_codepoint_t old_gid = _.second;
262
0
  inner_maps[0].add (old_gid);
263
0
      }
264
0
    }
265
0
    else
266
0
    {
267
0
      inner_maps[0].add_set (adv_set);
268
0
      hb_set_subtract (inner_sets[0], adv_set);
269
0
      inner_maps[0].add_set (inner_sets[0]);
270
0
    }
271
0
272
0
    for (unsigned int i = 1; i < inner_maps.length; i++)
273
0
      inner_maps[i].add_set (inner_sets[i]);
274
0
275
0
    for (unsigned int i = 0; i < index_maps.length; i++)
276
0
      index_map_plans[i].remap (index_maps[i], outer_map, inner_maps, plan);
277
0
  }
278
279
  /* remap */
280
  bool remap_index_map_plans (const hb_subset_plan_t *plan,
281
                              const hb_map_t& varidx_map)
282
0
  {
283
0
    for (unsigned i = 0; i < index_map_plans.length; i++)
284
0
      if (!index_map_plans[i].remap_after_instantiation (plan, varidx_map))
285
0
        return false;
286
0
    return true;
287
0
  }
288
289
  void fini ()
290
0
  {
291
0
    for (unsigned int i = 0; i < inner_sets.length; i++)
292
0
      hb_set_destroy (inner_sets[i]);
293
0
    hb_set_destroy (adv_set);
294
0
    inner_maps.fini ();
295
0
    index_map_plans.fini ();
296
0
  }
297
298
  hb_inc_bimap_t outer_map;
299
  hb_vector_t<hb_inc_bimap_t> inner_maps;
300
  hb_vector_t<index_map_subset_plan_t> index_map_plans;
301
  const ItemVariationStore *var_store;
302
303
  protected:
304
  hb_vector_t<hb_set_t *> inner_sets;
305
  hb_set_t *adv_set;
306
};
307
308
/*
309
 * HVAR -- Horizontal Metrics Variations
310
 * https://docs.microsoft.com/en-us/typography/opentype/spec/hvar
311
 * VVAR -- Vertical Metrics Variations
312
 * https://docs.microsoft.com/en-us/typography/opentype/spec/vvar
313
 */
314
#define HB_OT_TAG_HVAR HB_TAG('H','V','A','R')
315
#define HB_OT_TAG_VVAR HB_TAG('V','V','A','R')
316
317
struct HVARVVAR
318
{
319
  static constexpr hb_tag_t HVARTag = HB_OT_TAG_HVAR;
320
  static constexpr hb_tag_t VVARTag = HB_OT_TAG_VVAR;
321
322
0
  bool has_data () const { return version.major != 0; }
323
324
  bool sanitize (hb_sanitize_context_t *c) const
325
0
  {
326
0
    TRACE_SANITIZE (this);
327
0
    return_trace (version.sanitize (c) &&
328
0
      hb_barrier () &&
329
0
      likely (version.major == 1) &&
330
0
      varStore.sanitize (c, this) &&
331
0
      advMap.sanitize (c, this) &&
332
0
      lsbMap.sanitize (c, this) &&
333
0
      rsbMap.sanitize (c, this));
334
0
  }
335
336
  const ItemVariationStore& get_var_store () const
337
0
  { return this+varStore; }
338
339
  void listup_index_maps (hb_vector_t<const DeltaSetIndexMap *> &index_maps) const
340
0
  {
341
0
    if (advMap) index_maps.push (&(this+advMap));
342
0
    else index_maps.push (nullptr);
343
0
344
0
    if (lsbMap) index_maps.push (&(this+lsbMap));
345
0
    else index_maps.push (nullptr);
346
0
347
0
    if (rsbMap) index_maps.push (&(this+rsbMap));
348
0
    else index_maps.push (nullptr);
349
0
  }
350
351
  bool serialize_index_maps (hb_serialize_context_t *c,
352
           const hb_array_t<index_map_subset_plan_t> &im_plans)
353
0
  {
354
0
    TRACE_SERIALIZE (this);
355
0
    if (im_plans[index_map_subset_plan_t::ADV_INDEX].is_identity ())
356
0
      advMap = 0;
357
0
    else if (unlikely (!advMap.serialize_serialize (c, im_plans[index_map_subset_plan_t::ADV_INDEX])))
358
0
      return_trace (false);
359
0
    if (im_plans[index_map_subset_plan_t::LSB_INDEX].is_identity ())
360
0
      lsbMap = 0;
361
0
    else if (unlikely (!lsbMap.serialize_serialize (c, im_plans[index_map_subset_plan_t::LSB_INDEX])))
362
0
      return_trace (false);
363
0
    if (im_plans[index_map_subset_plan_t::RSB_INDEX].is_identity ())
364
0
      rsbMap = 0;
365
0
    else if (unlikely (!rsbMap.serialize_serialize (c, im_plans[index_map_subset_plan_t::RSB_INDEX])))
366
0
      return_trace (false);
367
0
368
0
    return_trace (true);
369
0
  }
370
371
  template <typename T>
372
  bool _subset (hb_subset_context_t *c) const
373
0
  {
374
0
    TRACE_SUBSET (this);
375
0
    if (c->plan->all_axes_pinned)
376
0
      return_trace (false);
377
0
378
0
    hvarvvar_subset_plan_t  hvar_plan;
379
0
    hb_vector_t<const DeltaSetIndexMap *>
380
0
        index_maps;
381
0
382
0
    ((T*)this)->listup_index_maps (index_maps);
383
0
    hvar_plan.init (index_maps.as_array (), this+varStore, c->plan);
384
0
385
0
    T *out = c->serializer->allocate_min<T> ();
386
0
    if (unlikely (!out)) return_trace (false);
387
0
388
0
    out->version.major = 1;
389
0
    out->version.minor = 0;
390
0
391
0
    if (c->plan->normalized_coords)
392
0
    {
393
0
      item_variations_t item_vars;
394
0
      if (!item_vars.instantiate (this+varStore, c->plan,
395
0
                                  advMap == 0 ? false : true,
396
0
                                  false, /* use_no_variation_idx = false */
397
0
                                  hvar_plan.inner_maps.as_array ()))
398
0
        return_trace (false);
399
0
400
0
      if (!out->varStore.serialize_serialize (c->serializer,
401
0
                                              item_vars.has_long_word (),
402
0
                                              c->plan->axis_tags,
403
0
                                              item_vars.get_region_list (),
404
0
                                              item_vars.get_vardata_encodings ()))
405
0
        return_trace (false);
406
0
407
0
      /* if varstore is optimized, remap output_map */
408
0
      if (advMap)
409
0
      {
410
0
        if (!hvar_plan.remap_index_map_plans (c->plan, item_vars.get_varidx_map ()))
411
0
          return_trace (false);
412
0
      }
413
0
    }
414
0
    else
415
0
    {
416
0
      /* avar2 partial instancing: cull unreachable regions. */
417
0
      hb_set_t dead_regions;
418
0
      if (c->plan->has_avar2)
419
0
  hvar_plan.var_store->collect_dead_regions (c->plan->axes_old_index_tag_map,
420
0
               c->plan->avar2_reachable_ranges,
421
0
               dead_regions);
422
0
423
0
      if (unlikely (!out->varStore
424
0
        .serialize_serialize (c->serializer,
425
0
            hvar_plan.var_store,
426
0
            hvar_plan.inner_maps.as_array (),
427
0
            dead_regions.get_population () ? &dead_regions : nullptr)))
428
0
      return_trace (false);
429
0
    }
430
0
431
0
    return_trace (out->T::serialize_index_maps (c->serializer,
432
0
            hvar_plan.index_map_plans.as_array ()));
433
0
  }
Unexecuted instantiation: bool OT::HVARVVAR::_subset<OT::HVAR>(hb_subset_context_t*) const
Unexecuted instantiation: bool OT::HVARVVAR::_subset<OT::VVAR>(hb_subset_context_t*) const
434
435
  HB_ALWAYS_INLINE
436
  float get_advance_delta_unscaled (hb_codepoint_t  glyph,
437
            const int *coords, unsigned int coord_count,
438
            hb_scalar_cache_t *store_cache = nullptr) const
439
0
  {
440
0
    uint32_t varidx = (this+advMap).map (glyph);
441
0
    return (this+varStore).get_delta (varidx,
442
0
              coords, coord_count,
443
0
              store_cache);
444
0
  }
445
446
  public:
447
  FixedVersion<>version;  /* Version of the metrics variation table
448
         * initially set to 0x00010000u */
449
  Offset32To<ItemVariationStore>
450
    varStore; /* Offset to item variation store table. */
451
  Offset32To<DeltaSetIndexMap>
452
    advMap;   /* Offset to advance var-idx mapping. */
453
  Offset32To<DeltaSetIndexMap>
454
    lsbMap;   /* Offset to lsb/tsb var-idx mapping. */
455
  Offset32To<DeltaSetIndexMap>
456
    rsbMap;   /* Offset to rsb/bsb var-idx mapping. */
457
458
  public:
459
  DEFINE_SIZE_STATIC (20);
460
};
461
462
struct HVAR : HVARVVAR {
463
  static constexpr hb_tag_t tableTag = HB_OT_TAG_HVAR;
464
0
  bool subset (hb_subset_context_t *c) const { return HVARVVAR::_subset<HVAR> (c); }
465
};
466
struct VVAR : HVARVVAR {
467
  static constexpr hb_tag_t tableTag = HB_OT_TAG_VVAR;
468
469
  bool sanitize (hb_sanitize_context_t *c) const
470
0
  {
471
0
    TRACE_SANITIZE (this);
472
0
    return_trace (static_cast<const HVARVVAR *> (this)->sanitize (c) &&
473
0
      vorgMap.sanitize (c, this));
474
0
  }
475
476
  void listup_index_maps (hb_vector_t<const DeltaSetIndexMap *> &index_maps) const
477
0
  {
478
0
    HVARVVAR::listup_index_maps (index_maps);
479
0
    if (vorgMap) index_maps.push (&(this+vorgMap));
480
0
    else index_maps.push (nullptr);
481
0
  }
482
483
  bool serialize_index_maps (hb_serialize_context_t *c,
484
           const hb_array_t<index_map_subset_plan_t> &im_plans)
485
0
  {
486
0
    TRACE_SERIALIZE (this);
487
0
    if (unlikely (!HVARVVAR::serialize_index_maps (c, im_plans)))
488
0
      return_trace (false);
489
0
    if (!im_plans[index_map_subset_plan_t::VORG_INDEX].get_map_count ())
490
0
      vorgMap = 0;
491
0
    else if (unlikely (!vorgMap.serialize_serialize (c, im_plans[index_map_subset_plan_t::VORG_INDEX])))
492
0
      return_trace (false);
493
0
494
0
    return_trace (true);
495
0
  }
496
497
0
  bool subset (hb_subset_context_t *c) const { return HVARVVAR::_subset<VVAR> (c); }
498
499
  HB_ALWAYS_INLINE
500
  float get_vorg_delta_unscaled (hb_codepoint_t glyph,
501
         const int *coords, unsigned int coord_count,
502
         hb_scalar_cache_t *store_cache = nullptr) const
503
0
  {
504
0
    if (!vorgMap) return 0.f;
505
0
    uint32_t varidx = (this+vorgMap).map (glyph);
506
0
    return (this+varStore).get_delta (varidx,
507
0
              coords, coord_count,
508
0
              store_cache);
509
0
  }
510
511
  protected:
512
  Offset32To<DeltaSetIndexMap>
513
    vorgMap;  /* Offset to vertical-origin var-idx mapping. */
514
515
  public:
516
  DEFINE_SIZE_STATIC (24);
517
};
518
519
} /* namespace OT */
520
521
522
#endif /* HB_OT_VAR_HVAR_TABLE_HH */