Coverage Report

Created: 2025-11-16 07:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pango/subprojects/harfbuzz/src/hb-ot-hmtx-table.hh
Line
Count
Source
1
/*
2
 * Copyright © 2011,2012  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, Roderick Sheeter
25
 */
26
27
#ifndef HB_OT_HMTX_TABLE_HH
28
#define HB_OT_HMTX_TABLE_HH
29
30
#include "hb-open-type.hh"
31
#include "hb-ot-maxp-table.hh"
32
#include "hb-ot-hhea-table.hh"
33
#include "hb-ot-var-hvar-table.hh"
34
#include "hb-ot-var-mvar-table.hh"
35
#include "hb-ot-metrics.hh"
36
37
/*
38
 * hmtx -- Horizontal Metrics
39
 * https://docs.microsoft.com/en-us/typography/opentype/spec/hmtx
40
 * vmtx -- Vertical Metrics
41
 * https://docs.microsoft.com/en-us/typography/opentype/spec/vmtx
42
 */
43
#define HB_OT_TAG_hmtx HB_TAG('h','m','t','x')
44
0
#define HB_OT_TAG_vmtx HB_TAG('v','m','t','x')
45
46
47
HB_INTERNAL bool
48
_glyf_get_leading_bearing_with_var_unscaled (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical, int *lsb);
49
50
HB_INTERNAL unsigned
51
_glyf_get_advance_with_var_unscaled (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical);
52
53
HB_INTERNAL bool
54
_glyf_get_leading_bearing_without_var_unscaled (hb_face_t *face, hb_codepoint_t gid, bool is_vertical, int *lsb);
55
56
57
namespace OT {
58
59
60
struct LongMetric
61
{
62
  UFWORD  advance; /* Advance width/height. */
63
  FWORD   sb; /* Leading (left/top) side bearing. */
64
  public:
65
  DEFINE_SIZE_STATIC (4);
66
};
67
68
69
template <typename T/*Data table type*/, typename H/*Header table type*/, typename V/*Var table type*/>
70
struct hmtxvmtx
71
{
72
  bool sanitize (hb_sanitize_context_t *c HB_UNUSED) const
73
0
  {
74
0
    TRACE_SANITIZE (this);
75
    /* We don't check for anything specific here.  The users of the
76
     * struct do all the hard work... */
77
0
    return_trace (true);
78
0
  }
Unexecuted instantiation: OT::hmtxvmtx<OT::hmtx, OT::hhea, OT::HVAR>::sanitize(hb_sanitize_context_t*) const
Unexecuted instantiation: OT::hmtxvmtx<OT::vmtx, OT::vhea, OT::VVAR>::sanitize(hb_sanitize_context_t*) const
79
80
  const hb_hashmap_t<hb_codepoint_t, hb_pair_t<unsigned, int>>* get_mtx_map (const hb_subset_plan_t *plan) const
81
  { return T::is_horizontal ? &plan->hmtx_map : &plan->vmtx_map; }
82
83
  bool subset_update_header (hb_subset_context_t *c,
84
           unsigned int num_hmetrics,
85
           const hb_hashmap_t<hb_codepoint_t, hb_pair_t<unsigned, int>> *mtx_map,
86
           const hb_vector_t<unsigned> &bounds_vec) const
87
  {
88
    hb_blob_t *src_blob = hb_sanitize_context_t ().reference_table<H> (c->plan->source, H::tableTag);
89
    hb_blob_t *dest_blob = hb_blob_copy_writable_or_fail (src_blob);
90
    hb_blob_destroy (src_blob);
91
92
    if (unlikely (!dest_blob)) {
93
      return false;
94
    }
95
96
    unsigned int length;
97
    H *table = (H *) hb_blob_get_data (dest_blob, &length);
98
    c->serializer->check_assign (table->numberOfLongMetrics, num_hmetrics, HB_SERIALIZE_ERROR_INT_OVERFLOW);
99
100
#ifndef HB_NO_VAR
101
    if (c->plan->normalized_coords)
102
    {
103
      auto &MVAR = *c->plan->source->table.MVAR;
104
      if (T::is_horizontal)
105
      {
106
  HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_HORIZONTAL_CARET_RISE,   caretSlopeRise);
107
  HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_HORIZONTAL_CARET_RUN,    caretSlopeRun);
108
  HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_HORIZONTAL_CARET_OFFSET, caretOffset);
109
      }
110
      else
111
      {
112
  HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_VERTICAL_CARET_RISE,     caretSlopeRise);
113
  HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_VERTICAL_CARET_RUN,      caretSlopeRun);
114
  HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_VERTICAL_CARET_OFFSET,   caretOffset);
115
      }
116
117
      bool empty = true;
118
      int min_lsb = 0x7FFF;
119
      int min_rsb = 0x7FFF;
120
      int max_extent = -0x7FFF;
121
      unsigned max_adv = 0;
122
      for (const auto _ : *mtx_map)
123
      {
124
        hb_codepoint_t gid = _.first;
125
        unsigned adv = _.second.first;
126
        int lsb = _.second.second;
127
        max_adv = hb_max (max_adv, adv);
128
129
        if (bounds_vec[gid] != 0xFFFFFFFF)
130
        {
131
    empty = false;
132
          unsigned bound_width = bounds_vec[gid];
133
          int rsb = adv - lsb - bound_width;
134
          int extent = lsb + bound_width;
135
          min_lsb = hb_min (min_lsb, lsb);
136
          min_rsb = hb_min (min_rsb, rsb);
137
          max_extent = hb_max (max_extent, extent);
138
        }
139
      }
140
141
      table->advanceMax = max_adv;
142
      if (!empty)
143
      {
144
        table->minLeadingBearing = min_lsb;
145
        table->minTrailingBearing = min_rsb;
146
        table->maxExtent = max_extent;
147
      }
148
149
      if (T::is_horizontal)
150
      {
151
        const auto &OS2 = *c->plan->source->table.OS2;
152
        if (OS2.has_data () &&
153
            table->ascender == OS2.sTypoAscender &&
154
            table->descender == OS2.sTypoDescender &&
155
            table->lineGap == OS2.sTypoLineGap)
156
        {
157
          table->ascender = static_cast<int> (roundf (OS2.sTypoAscender +
158
                                                      MVAR.get_var (HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER,
159
                                                                    c->plan->normalized_coords.arrayZ,
160
                                                                    c->plan->normalized_coords.length)));
161
          table->descender = static_cast<int> (roundf (OS2.sTypoDescender +
162
                                                       MVAR.get_var (HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER,
163
                                                                     c->plan->normalized_coords.arrayZ,
164
                                                                     c->plan->normalized_coords.length)));
165
          table->lineGap = static_cast<int> (roundf (OS2.sTypoLineGap +
166
                                                     MVAR.get_var (HB_OT_METRICS_TAG_HORIZONTAL_LINE_GAP,
167
                                                                   c->plan->normalized_coords.arrayZ,
168
                                                                   c->plan->normalized_coords.length)));
169
        }
170
      }
171
    }
172
#endif
173
174
    bool result = c->plan->add_table (H::tableTag, dest_blob);
175
    hb_blob_destroy (dest_blob);
176
177
    return result;
178
  }
179
180
  template<typename Iterator,
181
     hb_requires (hb_is_iterator (Iterator))>
182
  void serialize (hb_serialize_context_t *c,
183
      Iterator it,
184
      const hb_vector_t<hb_codepoint_pair_t> new_to_old_gid_list,
185
      unsigned num_long_metrics,
186
                  unsigned total_num_metrics)
187
  {
188
    LongMetric* long_metrics = c->allocate_size<LongMetric> (num_long_metrics * LongMetric::static_size);
189
    FWORD* short_metrics = c->allocate_size<FWORD> ((total_num_metrics - num_long_metrics) * FWORD::static_size);
190
    if (!long_metrics || !short_metrics) return;
191
192
    short_metrics -= num_long_metrics;
193
194
    for (auto _ : new_to_old_gid_list)
195
    {
196
      hb_codepoint_t gid = _.first;
197
      auto mtx = *it++;
198
199
      if (gid < num_long_metrics)
200
      {
201
  LongMetric& lm = long_metrics[gid];
202
  lm.advance = mtx.first;
203
  lm.sb = mtx.second;
204
      }
205
      // TODO(beyond-64k): This assumes that maxp.numGlyphs is 0xFFFF.
206
      else if (gid < 0x10000u)
207
        short_metrics[gid] = mtx.second;
208
      else
209
        ((UFWORD*) short_metrics)[gid] = mtx.first;
210
    }
211
  }
212
213
  bool subset (hb_subset_context_t *c) const
214
  {
215
    TRACE_SUBSET (this);
216
217
    auto *table_prime = c->serializer->start_embed <T> ();
218
219
    accelerator_t _mtx (c->plan->source);
220
    unsigned num_long_metrics;
221
    const hb_hashmap_t<hb_codepoint_t, hb_pair_t<unsigned, int>> *mtx_map = get_mtx_map (c->plan);
222
    {
223
      /* Determine num_long_metrics to encode. */
224
      auto& plan = c->plan;
225
226
      // TODO Don't consider retaingid holes here.
227
228
      num_long_metrics = hb_min (plan->num_output_glyphs (), 0xFFFFu);
229
      unsigned int last_advance = get_new_gid_advance_unscaled (plan, mtx_map, num_long_metrics - 1, _mtx);
230
      while (num_long_metrics > 1 &&
231
       last_advance == get_new_gid_advance_unscaled (plan, mtx_map, num_long_metrics - 2, _mtx))
232
      {
233
  num_long_metrics--;
234
      }
235
    }
236
237
    auto it =
238
    + hb_iter (c->plan->new_to_old_gid_list)
239
    | hb_map ([c, &_mtx, mtx_map] (hb_codepoint_pair_t _)
240
        {
241
    hb_codepoint_t new_gid = _.first;
242
    hb_codepoint_t old_gid = _.second;
243
244
    hb_pair_t<unsigned, int> *v = nullptr;
245
    if (!mtx_map->has (new_gid, &v))
246
    {
247
      int lsb = 0;
248
      if (!_mtx.get_leading_bearing_without_var_unscaled (old_gid, &lsb))
249
        (void) _glyf_get_leading_bearing_without_var_unscaled (c->plan->source, old_gid, !T::is_horizontal, &lsb);
250
      return hb_pair (_mtx.get_advance_without_var_unscaled (old_gid), +lsb);
251
    }
252
    return *v;
253
        })
254
    ;
255
256
    table_prime->serialize (c->serializer,
257
          it,
258
          c->plan->new_to_old_gid_list,
259
          num_long_metrics,
260
          c->plan->num_output_glyphs ());
261
262
    if (unlikely (c->serializer->in_error ()))
263
      return_trace (false);
264
265
    // Amend header num hmetrics
266
    if (unlikely (!subset_update_header (c, num_long_metrics, mtx_map,
267
                                         T::is_horizontal ? c->plan->bounds_width_vec : c->plan->bounds_height_vec)))
268
      return_trace (false);
269
270
    return_trace (true);
271
  }
272
273
  struct accelerator_t
274
  {
275
    friend struct hmtxvmtx;
276
277
    accelerator_t (hb_face_t *face)
278
0
    {
279
0
      table = hb_sanitize_context_t ().reference_table<hmtxvmtx> (face, T::tableTag);
280
0
      var_table = hb_sanitize_context_t ().reference_table<V> (face, T::variationsTag);
281
282
0
      default_advance = T::is_horizontal ? hb_face_get_upem (face) / 2 : hb_face_get_upem (face);
283
284
      /* Populate count variables and sort them out as we go */
285
286
0
      unsigned int len = table.get_length ();
287
0
      if (len & 1)
288
0
        len--;
289
290
0
      num_long_metrics = T::is_horizontal ?
291
0
       face->table.hhea->numberOfLongMetrics :
292
0
#ifndef HB_NO_VERTICAL
293
0
       face->table.vhea->numberOfLongMetrics
294
#else
295
       0
296
#endif
297
0
       ;
298
0
      if (unlikely (num_long_metrics * 4 > len))
299
0
  num_long_metrics = len / 4;
300
0
      len -= num_long_metrics * 4;
301
302
0
      num_bearings = face->table.maxp->get_num_glyphs ();
303
304
0
      if (unlikely (num_bearings < num_long_metrics))
305
0
        num_bearings = num_long_metrics;
306
0
      if (unlikely ((num_bearings - num_long_metrics) * 2 > len))
307
0
        num_bearings = num_long_metrics + len / 2;
308
0
      len -= (num_bearings - num_long_metrics) * 2;
309
310
      /* We MUST set num_bearings to zero if num_long_metrics is zero.
311
       * Our get_advance() depends on that. */
312
0
      if (unlikely (!num_long_metrics))
313
0
  num_bearings = num_long_metrics = 0;
314
315
0
      num_advances = num_bearings + len / 2;
316
0
      num_glyphs = face->get_num_glyphs ();
317
0
      if (num_glyphs < num_advances)
318
0
        num_glyphs = num_advances;
319
0
    }
Unexecuted instantiation: OT::hmtxvmtx<OT::hmtx, OT::hhea, OT::HVAR>::accelerator_t::accelerator_t(hb_face_t*)
Unexecuted instantiation: OT::hmtxvmtx<OT::vmtx, OT::vhea, OT::VVAR>::accelerator_t::accelerator_t(hb_face_t*)
320
    ~accelerator_t ()
321
0
    {
322
0
      table.destroy ();
323
0
      var_table.destroy ();
324
0
    }
Unexecuted instantiation: OT::hmtxvmtx<OT::hmtx, OT::hhea, OT::HVAR>::accelerator_t::~accelerator_t()
Unexecuted instantiation: OT::hmtxvmtx<OT::vmtx, OT::vhea, OT::VVAR>::accelerator_t::~accelerator_t()
325
326
0
    bool has_data () const { return (bool) num_bearings; }
327
328
    bool get_leading_bearing_without_var_unscaled (hb_codepoint_t glyph,
329
               int *lsb) const
330
0
    {
331
0
      if (glyph < num_long_metrics)
332
0
      {
333
0
  *lsb = table->longMetricZ[glyph].sb;
334
0
  return true;
335
0
      }
336
337
0
      if (unlikely (glyph >= num_bearings))
338
0
  return false;
339
340
0
      const FWORD *bearings = (const FWORD *) &table->longMetricZ[num_long_metrics];
341
0
      *lsb = bearings[glyph - num_long_metrics];
342
0
      return true;
343
0
    }
Unexecuted instantiation: OT::hmtxvmtx<OT::hmtx, OT::hhea, OT::HVAR>::accelerator_t::get_leading_bearing_without_var_unscaled(unsigned int, int*) const
Unexecuted instantiation: OT::hmtxvmtx<OT::vmtx, OT::vhea, OT::VVAR>::accelerator_t::get_leading_bearing_without_var_unscaled(unsigned int, int*) const
344
345
    bool get_leading_bearing_with_var_unscaled (hb_font_t *font,
346
            hb_codepoint_t glyph,
347
            int *lsb) const
348
0
    {
349
0
      if (!font->num_coords)
350
0
  return get_leading_bearing_without_var_unscaled (glyph, lsb);
351
352
0
#ifndef HB_NO_VAR
353
0
      float delta;
354
0
      if (var_table->get_lsb_delta_unscaled (glyph, font->coords, font->num_coords, &delta) &&
355
0
    get_leading_bearing_without_var_unscaled (glyph, lsb))
356
0
      {
357
0
  *lsb += roundf (delta);
358
0
  return true;
359
0
      }
360
361
0
      return _glyf_get_leading_bearing_with_var_unscaled (font, glyph, T::tableTag == HB_OT_TAG_vmtx, lsb);
362
#else
363
      return false;
364
#endif
365
0
    }
366
367
    unsigned int get_advance_without_var_unscaled (hb_codepoint_t glyph) const
368
0
    {
369
      /* OpenType case. */
370
0
      if (glyph < num_bearings)
371
0
  return table->longMetricZ[hb_min (glyph, (uint32_t) num_long_metrics - 1)].advance;
372
373
      /* If num_advances is zero, it means we don't have the metrics table
374
       * for this direction: return default advance.  Otherwise, there's a
375
       * well-defined answer. */
376
0
      if (unlikely (!num_advances))
377
0
  return default_advance;
378
379
0
#ifdef HB_NO_BEYOND_64K
380
0
      return 0;
381
0
#endif
382
383
0
      if (unlikely (glyph >= num_glyphs))
384
0
        return 0;
385
386
      /* num_bearings <= glyph < num_glyphs;
387
       * num_bearings <= num_advances */
388
389
0
      if (num_bearings == num_advances)
390
0
        return get_advance_without_var_unscaled (num_bearings - 1);
391
392
0
      const FWORD *bearings = (const FWORD *) &table->longMetricZ[num_long_metrics];
393
0
      const UFWORD *advances = (const UFWORD *) &bearings[num_bearings - num_long_metrics];
394
395
0
      return advances[hb_min (glyph - num_bearings, num_advances - num_bearings - 1)];
396
0
    }
Unexecuted instantiation: OT::hmtxvmtx<OT::hmtx, OT::hhea, OT::HVAR>::accelerator_t::get_advance_without_var_unscaled(unsigned int) const
Unexecuted instantiation: OT::hmtxvmtx<OT::vmtx, OT::vhea, OT::VVAR>::accelerator_t::get_advance_without_var_unscaled(unsigned int) const
397
398
    unsigned get_advance_with_var_unscaled (hb_codepoint_t  glyph,
399
              hb_font_t      *font,
400
              ItemVariationStore::cache_t *store_cache = nullptr) const
401
0
    {
402
0
      unsigned int advance = get_advance_without_var_unscaled (glyph);
403
404
0
#ifndef HB_NO_VAR
405
0
      if (unlikely (glyph >= num_bearings) || !font->num_coords)
406
0
  return advance;
407
408
0
      if (var_table.get_length ())
409
0
  return advance + roundf (var_table->get_advance_delta_unscaled (glyph,
410
0
                  font->coords, font->num_coords,
411
0
                  store_cache));
412
413
0
      return _glyf_get_advance_with_var_unscaled (font, glyph, T::tableTag == HB_OT_TAG_vmtx);
414
#else
415
      return advance;
416
#endif
417
0
    }
Unexecuted instantiation: OT::hmtxvmtx<OT::hmtx, OT::hhea, OT::HVAR>::accelerator_t::get_advance_with_var_unscaled(unsigned int, hb_font_t*, float*) const
Unexecuted instantiation: OT::hmtxvmtx<OT::vmtx, OT::vhea, OT::VVAR>::accelerator_t::get_advance_with_var_unscaled(unsigned int, hb_font_t*, float*) const
418
419
    protected:
420
    // 0 <= num_long_metrics <= num_bearings <= num_advances <= num_glyphs
421
    unsigned num_long_metrics;
422
    unsigned num_bearings;
423
    unsigned num_advances;
424
    unsigned num_glyphs;
425
426
    unsigned int default_advance;
427
428
    public:
429
    hb_blob_ptr_t<hmtxvmtx> table;
430
    hb_blob_ptr_t<V> var_table;
431
  };
432
433
  /* get advance: when no variations, call get_advance_without_var_unscaled.
434
   * when there're variations, get advance value from mtx_map in subset_plan*/
435
  unsigned get_new_gid_advance_unscaled (const hb_subset_plan_t *plan,
436
                                         const hb_hashmap_t<hb_codepoint_t, hb_pair_t<unsigned, int>> *mtx_map,
437
                                         unsigned new_gid,
438
                                         const accelerator_t &_mtx) const
439
  {
440
    if (mtx_map->is_empty ())
441
    {
442
      hb_codepoint_t old_gid = 0;
443
      return plan->old_gid_for_new_gid (new_gid, &old_gid) ?
444
             _mtx.get_advance_without_var_unscaled (old_gid) : 0;
445
    }
446
    return mtx_map->get (new_gid).first;
447
  }
448
449
  protected:
450
  UnsizedArrayOf<LongMetric>
451
    longMetricZ;  /* Paired advance width and leading
452
         * bearing values for each glyph. The
453
         * value numOfHMetrics comes from
454
         * the 'hhea' table. If the font is
455
         * monospaced, only one entry need
456
         * be in the array, but that entry is
457
         * required. The last entry applies to
458
         * all subsequent glyphs. */
459
/*UnsizedArrayOf<FWORD> leadingBearingX;*/
460
        /* Here the advance is assumed
461
         * to be the same as the advance
462
         * for the last entry above. The
463
         * number of entries in this array is
464
         * derived from numGlyphs (from 'maxp'
465
         * table) minus numberOfLongMetrics.
466
         * This generally is used with a run
467
         * of monospaced glyphs (e.g., Kanji
468
         * fonts or Courier fonts). Only one
469
         * run is allowed and it must be at
470
         * the end. This allows a monospaced
471
         * font to vary the side bearing
472
         * values for each glyph. */
473
/*UnsizedArrayOf<UFWORD>advancesX;*/
474
        /* TODO Document. */
475
  public:
476
  DEFINE_SIZE_ARRAY (0, longMetricZ);
477
};
478
479
struct hmtx : hmtxvmtx<hmtx, hhea, HVAR> {
480
  static constexpr hb_tag_t tableTag = HB_OT_TAG_hmtx;
481
  static constexpr hb_tag_t variationsTag = HB_OT_TAG_HVAR;
482
  static constexpr bool is_horizontal = true;
483
};
484
struct vmtx : hmtxvmtx<vmtx, vhea, VVAR> {
485
  static constexpr hb_tag_t tableTag = HB_OT_TAG_vmtx;
486
  static constexpr hb_tag_t variationsTag = HB_OT_TAG_VVAR;
487
  static constexpr bool is_horizontal = false;
488
};
489
490
struct hmtx_accelerator_t : hmtx::accelerator_t {
491
0
  hmtx_accelerator_t (hb_face_t *face) : hmtx::accelerator_t (face) {}
492
};
493
struct vmtx_accelerator_t : vmtx::accelerator_t {
494
0
  vmtx_accelerator_t (hb_face_t *face) : vmtx::accelerator_t (face) {}
495
};
496
497
} /* namespace OT */
498
499
500
#endif /* HB_OT_HMTX_TABLE_HH */