Coverage Report

Created: 2023-03-09 17:58

/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
1.27k
  {
53
1.27k
    TRACE_SANITIZE (this);
54
1.27k
    return_trace (varIdxMap.sanitize (c, base) &&
55
1.27k
      varStore.sanitize (c, base));
56
1.27k
  }
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
3.65k
  {
89
26.0k
#define fromCoord coords[from_offset]
90
3.65k
#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
3.65k
    if (len < 2)
95
163
    {
96
163
      if (!len)
97
139
  return value;
98
24
      else /* len == 1*/
99
24
  return value - arrayZ[0].fromCoord + arrayZ[0].toCoord;
100
163
    }
101
102
3.48k
    if (value <= arrayZ[0].fromCoord)
103
399
      return value - arrayZ[0].fromCoord + arrayZ[0].toCoord;
104
105
3.08k
    unsigned int i;
106
3.08k
    unsigned int count = len - 1;
107
13.1k
    for (i = 1; i < count && value > arrayZ[i].fromCoord; i++)
108
10.0k
      ;
109
110
3.08k
    if (value >= arrayZ[i].fromCoord)
111
99
      return value - arrayZ[i].fromCoord + arrayZ[i].toCoord;
112
113
2.98k
    if (unlikely (arrayZ[i-1].fromCoord == arrayZ[i].fromCoord))
114
0
      return arrayZ[i-1].toCoord;
115
116
2.98k
    int denom = arrayZ[i].fromCoord - arrayZ[i-1].fromCoord;
117
2.98k
    return roundf (arrayZ[i-1].toCoord + ((float) (arrayZ[i].toCoord - arrayZ[i-1].toCoord) *
118
2.98k
            (value - arrayZ[i-1].fromCoord)) / denom);
119
2.98k
#undef toCoord
120
2.98k
#undef fromCoord
121
2.98k
  }
122
123
3.65k
  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
9.68k
  {
143
9.68k
    TRACE_SANITIZE (this);
144
9.68k
    if (!(version.sanitize (c) &&
145
9.68k
    (version.major == 1
146
9.65k
#ifndef HB_NO_VARIATIONS2
147
9.65k
     || version.major == 2
148
9.65k
#endif
149
9.65k
     ) &&
150
9.68k
    c->check_struct (this)))
151
3.20k
      return_trace (false);
152
153
6.47k
    const SegmentMaps *map = &firstAxisSegmentMaps;
154
6.47k
    unsigned int count = axisCount;
155
178k
    for (unsigned int i = 0; i < count; i++)
156
172k
    {
157
172k
      if (unlikely (!map->sanitize (c)))
158
848
  return_trace (false);
159
172k
      map = &StructAfter<SegmentMaps> (*map);
160
172k
    }
161
162
5.62k
#ifndef HB_NO_VARIATIONS2
163
5.62k
    if (version.major == 2)
164
1.27k
    {
165
1.27k
      const auto *v2 = (const avarV2Tail *) map;
166
1.27k
      if (unlikely (!v2->sanitize (c, this)))
167
320
  return_trace (false);
168
1.27k
    }
169
5.30k
#endif
170
171
5.62k
    return_trace (true);
172
5.62k
  }
173
174
  void map_coords (int *coords, unsigned int coords_length) const
175
310k
  {
176
310k
    unsigned int count = hb_min (coords_length, axisCount);
177
178
310k
    const SegmentMaps *map = &firstAxisSegmentMaps;
179
310k
    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
310k
#ifndef HB_NO_VARIATIONS2
186
310k
    if (version.major < 2)
187
309k
      return;
188
189
855
    const auto *v2 = (const avarV2Tail *) map;
190
191
855
    const auto &varidx_map = this+v2->varIdxMap;
192
855
    const auto &var_store = this+v2->varStore;
193
855
    auto *var_store_cache = var_store.create_cache ();
194
195
855
    hb_vector_t<int> out;
196
855
    out.alloc (coords_length);
197
855
    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
855
    OT::VariationStore::destroy_cache (var_store_cache);
208
209
855
    for (unsigned i = 0; i < coords_length; i++)
210
0
      coords[i] = out[i];
211
855
#endif
212
855
  }
213
214
  void unmap_coords (int *coords, unsigned int coords_length) const
215
154k
  {
216
154k
    unsigned int count = hb_min (coords_length, axisCount);
217
218
154k
    const SegmentMaps *map = &firstAxisSegmentMaps;
219
158k
    for (unsigned int i = 0; i < count; i++)
220
3.65k
    {
221
3.65k
      coords[i] = map->unmap (coords[i]);
222
3.65k
      map = &StructAfter<SegmentMaps> (*map);
223
3.65k
    }
224
154k
  }
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 */