Coverage Report

Created: 2025-12-14 06:53

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