Coverage Report

Created: 2026-10-14 09:33

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