Coverage Report

Created: 2026-06-30 11:14

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