Coverage Report

Created: 2023-12-13 20:02

/src/harfbuzz/src/hb-ot-var-avar-table.hh
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright © 2017  Google, Inc.
3
 *
4
 *  This is part of HarfBuzz, a text shaping library.
5
 *
6
 * Permission is hereby granted, without written agreement and without
7
 * license or royalty fees, to use, copy, modify, and distribute this
8
 * software and its documentation for any purpose, provided that the
9
 * above copyright notice and the following two paragraphs appear in
10
 * all copies of this software.
11
 *
12
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16
 * DAMAGE.
17
 *
18
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23
 *
24
 * Google Author(s): Behdad Esfahbod
25
 */
26
27
#ifndef HB_OT_VAR_AVAR_TABLE_HH
28
#define HB_OT_VAR_AVAR_TABLE_HH
29
30
#include "hb-open-type.hh"
31
#include "hb-ot-var-common.hh"
32
33
34
/*
35
 * avar -- Axis Variations
36
 * https://docs.microsoft.com/en-us/typography/opentype/spec/avar
37
 */
38
39
#define HB_OT_TAG_avar HB_TAG('a','v','a','r')
40
41
42
namespace OT {
43
44
45
/* "Spec": https://github.com/be-fonts/boring-expansion-spec/issues/14 */
46
struct avarV2Tail
47
{
48
  friend struct avar;
49
50
  bool sanitize (hb_sanitize_context_t *c,
51
     const void *base) const
52
36.8k
  {
53
36.8k
    TRACE_SANITIZE (this);
54
36.8k
    return_trace (varIdxMap.sanitize (c, base) &&
55
36.8k
      varStore.sanitize (c, base));
56
36.8k
  }
57
58
  protected:
59
  Offset32To<DeltaSetIndexMap>  varIdxMap;  /* Offset from the beginning of 'avar' table. */
60
  Offset32To<VariationStore>  varStore; /* Offset from the beginning of 'avar' table. */
61
62
  public:
63
  DEFINE_SIZE_STATIC (8);
64
};
65
66
67
struct AxisValueMap
68
{
69
  bool sanitize (hb_sanitize_context_t *c) const
70
0
  {
71
0
    TRACE_SANITIZE (this);
72
0
    return_trace (c->check_struct (this));
73
0
  }
74
75
  public:
76
  F2DOT14 coords[2];
77
//   F2DOT14  fromCoord;  /* A normalized coordinate value obtained using
78
//         * default normalization. */
79
//   F2DOT14  toCoord;  /* The modified, normalized coordinate value. */
80
81
  public:
82
  DEFINE_SIZE_STATIC (4);
83
};
84
85
struct SegmentMaps : Array16Of<AxisValueMap>
86
{
87
  int map (int value, unsigned int from_offset = 0, unsigned int to_offset = 1) const
88
5.35k
  {
89
33.4k
#define fromCoord coords[from_offset]
90
5.35k
#define toCoord coords[to_offset]
91
    /* The following special-cases are not part of OpenType, which requires
92
     * that at least -1, 0, and +1 must be mapped. But we include these as
93
     * part of a better error recovery scheme. */
94
5.35k
    if (len < 2)
95
822
    {
96
822
      if (!len)
97
666
  return value;
98
156
      else /* len == 1*/
99
156
  return value - arrayZ[0].fromCoord + arrayZ[0].toCoord;
100
822
    }
101
102
4.53k
    if (value <= arrayZ[0].fromCoord)
103
237
      return value - arrayZ[0].fromCoord + arrayZ[0].toCoord;
104
105
4.29k
    unsigned int i;
106
4.29k
    unsigned int count = len - 1;
107
16.8k
    for (i = 1; i < count && value > arrayZ[i].fromCoord; i++)
108
12.5k
      ;
109
110
4.29k
    if (value >= arrayZ[i].fromCoord)
111
480
      return value - arrayZ[i].fromCoord + arrayZ[i].toCoord;
112
113
3.81k
    if (unlikely (arrayZ[i-1].fromCoord == arrayZ[i].fromCoord))
114
0
      return arrayZ[i-1].toCoord;
115
116
3.81k
    int denom = arrayZ[i].fromCoord - arrayZ[i-1].fromCoord;
117
3.81k
    return roundf (arrayZ[i-1].toCoord + ((float) (arrayZ[i].toCoord - arrayZ[i-1].toCoord) *
118
3.81k
            (value - arrayZ[i-1].fromCoord)) / denom);
119
3.81k
#undef toCoord
120
3.81k
#undef fromCoord
121
3.81k
  }
122
123
5.35k
  int unmap (int value) const { return map (value, 1, 0); }
124
125
  public:
126
  DEFINE_SIZE_ARRAY (2, *this);
127
};
128
129
struct avar
130
{
131
  static constexpr hb_tag_t tableTag = HB_OT_TAG_avar;
132
133
0
  bool has_data () const { return version.to_int (); }
134
135
  const SegmentMaps* get_segment_maps () const
136
0
  { return &firstAxisSegmentMaps; }
137
138
  unsigned get_axis_count () const
139
0
  { return axisCount; }
140
141
  bool sanitize (hb_sanitize_context_t *c) const
142
50.3k
  {
143
50.3k
    TRACE_SANITIZE (this);
144
50.3k
    if (!(version.sanitize (c) &&
145
50.3k
    (version.major == 1
146
50.2k
#ifndef HB_NO_VARIATIONS2
147
50.2k
     || version.major == 2
148
50.2k
#endif
149
50.2k
     ) &&
150
50.3k
    c->check_struct (this)))
151
5.20k
      return_trace (false);
152
153
45.1k
    const SegmentMaps *map = &firstAxisSegmentMaps;
154
45.1k
    unsigned int count = axisCount;
155
322k
    for (unsigned int i = 0; i < count; i++)
156
278k
    {
157
278k
      if (unlikely (!map->sanitize (c)))
158
838
  return_trace (false);
159
277k
      map = &StructAfter<SegmentMaps> (*map);
160
277k
    }
161
162
44.2k
#ifndef HB_NO_VARIATIONS2
163
44.2k
    if (version.major == 2)
164
36.8k
    {
165
36.8k
      const auto *v2 = (const avarV2Tail *) map;
166
36.8k
      if (unlikely (!v2->sanitize (c, this)))
167
12.3k
  return_trace (false);
168
36.8k
    }
169
31.9k
#endif
170
171
44.2k
    return_trace (true);
172
44.2k
  }
173
174
  void map_coords (int *coords, unsigned int coords_length) const
175
791k
  {
176
791k
    unsigned int count = hb_min (coords_length, axisCount);
177
178
791k
    const SegmentMaps *map = &firstAxisSegmentMaps;
179
791k
    for (unsigned int i = 0; i < count; i++)
180
0
    {
181
0
      coords[i] = map->map (coords[i]);
182
0
      map = &StructAfter<SegmentMaps> (*map);
183
0
    }
184
185
791k
#ifndef HB_NO_VARIATIONS2
186
791k
    if (version.major < 2)
187
778k
      return;
188
189
12.6k
    const auto *v2 = (const avarV2Tail *) map;
190
191
12.6k
    const auto &varidx_map = this+v2->varIdxMap;
192
12.6k
    const auto &var_store = this+v2->varStore;
193
12.6k
    auto *var_store_cache = var_store.create_cache ();
194
195
12.6k
    hb_vector_t<int> out;
196
12.6k
    out.alloc (coords_length);
197
12.6k
    for (unsigned i = 0; i < coords_length; i++)
198
0
    {
199
0
      int v = coords[i];
200
0
      uint32_t varidx = varidx_map.map (i);
201
0
      float delta = var_store.get_delta (varidx, coords, coords_length, var_store_cache);
202
0
      v += roundf (delta);
203
0
      v = hb_clamp (v, -(1<<14), +(1<<14));
204
0
      out.push (v);
205
0
    }
206
207
12.6k
    OT::VariationStore::destroy_cache (var_store_cache);
208
209
12.6k
    for (unsigned i = 0; i < coords_length; i++)
210
0
      coords[i] = out[i];
211
12.6k
#endif
212
12.6k
  }
213
214
  void unmap_coords (int *coords, unsigned int coords_length) const
215
401k
  {
216
401k
    unsigned int count = hb_min (coords_length, axisCount);
217
218
401k
    const SegmentMaps *map = &firstAxisSegmentMaps;
219
406k
    for (unsigned int i = 0; i < count; i++)
220
5.35k
    {
221
5.35k
      coords[i] = map->unmap (coords[i]);
222
5.35k
      map = &StructAfter<SegmentMaps> (*map);
223
5.35k
    }
224
401k
  }
225
226
  protected:
227
  FixedVersion<>version;  /* Version of the avar table
228
         * initially set to 0x00010000u */
229
  HBUINT16  reserved; /* This field is permanently reserved. Set to 0. */
230
  HBUINT16  axisCount;  /* The number of variation axes in the font. This
231
         * must be the same number as axisCount in the
232
         * 'fvar' table. */
233
  SegmentMaps firstAxisSegmentMaps;
234
235
  public:
236
  DEFINE_SIZE_MIN (8);
237
};
238
239
} /* namespace OT */
240
241
242
#endif /* HB_OT_VAR_AVAR_TABLE_HH */