Coverage Report

Created: 2023-09-25 06:24

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