Coverage Report

Created: 2023-05-18 19:05

/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
842
  {
53
842
    TRACE_SANITIZE (this);
54
842
    return_trace (varIdxMap.sanitize (c, base) &&
55
842
      varStore.sanitize (c, base));
56
842
  }
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.68k
  {
89
24.7k
#define fromCoord coords[from_offset].to_int ()
90
3.68k
#define toCoord coords[to_offset].to_int ()
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.68k
    if (len < 2)
95
153
    {
96
153
      if (!len)
97
130
  return value;
98
23
      else /* len == 1*/
99
23
  return value - arrayZ[0].fromCoord + arrayZ[0].toCoord;
100
153
    }
101
102
3.53k
    if (value <= arrayZ[0].fromCoord)
103
430
      return value - arrayZ[0].fromCoord + arrayZ[0].toCoord;
104
105
3.10k
    unsigned int i;
106
3.10k
    unsigned int count = len - 1;
107
11.8k
    for (i = 1; i < count && value > arrayZ[i].fromCoord; i++)
108
8.75k
      ;
109
110
3.10k
    if (value >= arrayZ[i].fromCoord)
111
134
      return value - arrayZ[i].fromCoord + arrayZ[i].toCoord;
112
113
2.96k
    if (unlikely (arrayZ[i-1].fromCoord == arrayZ[i].fromCoord))
114
0
      return arrayZ[i-1].toCoord;
115
116
2.96k
    int denom = arrayZ[i].fromCoord - arrayZ[i-1].fromCoord;
117
2.96k
    return roundf (arrayZ[i-1].toCoord + ((float) (arrayZ[i].toCoord - arrayZ[i-1].toCoord) *
118
2.96k
            (value - arrayZ[i-1].fromCoord)) / denom);
119
2.96k
#undef toCoord
120
2.96k
#undef fromCoord
121
2.96k
  }
122
123
3.68k
  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.83k
  {
143
9.83k
    TRACE_SANITIZE (this);
144
9.83k
    if (!(version.sanitize (c) &&
145
9.83k
    (version.major == 1
146
9.81k
#ifndef HB_NO_AVAR2
147
9.81k
     || version.major == 2
148
9.81k
#endif
149
9.81k
     ) &&
150
9.83k
    c->check_struct (this)))
151
3.79k
      return_trace (false);
152
153
6.04k
    const SegmentMaps *map = &firstAxisSegmentMaps;
154
6.04k
    unsigned int count = axisCount;
155
73.6k
    for (unsigned int i = 0; i < count; i++)
156
68.5k
    {
157
68.5k
      if (unlikely (!map->sanitize (c)))
158
973
  return_trace (false);
159
67.5k
      map = &StructAfter<SegmentMaps> (*map);
160
67.5k
    }
161
162
5.06k
#ifndef HB_NO_AVAR2
163
5.06k
    if (version.major < 2)
164
4.22k
      return_trace (true);
165
166
842
    const auto &v2 = * (const avarV2Tail *) map;
167
842
    if (unlikely (!v2.sanitize (c, this)))
168
312
      return_trace (false);
169
530
#endif
170
171
842
    return_trace (true);
172
842
  }
173
174
  void map_coords (int *coords, unsigned int coords_length) const
175
440k
  {
176
440k
    unsigned int count = hb_min (coords_length, axisCount);
177
178
440k
    const SegmentMaps *map = &firstAxisSegmentMaps;
179
440k
    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
440k
#ifndef HB_NO_AVAR2
186
440k
    if (version.major < 2)
187
440k
      return;
188
189
44.3k
    for (; count < axisCount; count++)
190
43.7k
      map = &StructAfter<SegmentMaps> (*map);
191
192
606
    const auto &v2 = * (const avarV2Tail *) map;
193
194
606
    const auto &varidx_map = this+v2.varIdxMap;
195
606
    const auto &var_store = this+v2.varStore;
196
606
    auto *var_store_cache = var_store.create_cache ();
197
198
606
    hb_vector_t<int> out;
199
606
    out.alloc (coords_length);
200
606
    for (unsigned i = 0; i < coords_length; i++)
201
0
    {
202
0
      int v = coords[i];
203
0
      uint32_t varidx = varidx_map.map (i);
204
0
      float delta = var_store.get_delta (varidx, coords, coords_length, var_store_cache);
205
0
      v += roundf (delta);
206
0
      v = hb_clamp (v, -(1<<14), +(1<<14));
207
0
      out.push (v);
208
0
    }
209
606
    for (unsigned i = 0; i < coords_length; i++)
210
0
      coords[i] = out[i];
211
212
606
    OT::VariationStore::destroy_cache (var_store_cache);
213
606
#endif
214
606
  }
215
216
  void unmap_coords (int *coords, unsigned int coords_length) const
217
219k
  {
218
219k
    unsigned int count = hb_min (coords_length, axisCount);
219
220
219k
    const SegmentMaps *map = &firstAxisSegmentMaps;
221
223k
    for (unsigned int i = 0; i < count; i++)
222
3.68k
    {
223
3.68k
      coords[i] = map->unmap (coords[i]);
224
3.68k
      map = &StructAfter<SegmentMaps> (*map);
225
3.68k
    }
226
219k
  }
227
228
  protected:
229
  FixedVersion<>version;  /* Version of the avar table
230
         * initially set to 0x00010000u */
231
  HBUINT16  reserved; /* This field is permanently reserved. Set to 0. */
232
  HBUINT16  axisCount;  /* The number of variation axes in the font. This
233
         * must be the same number as axisCount in the
234
         * 'fvar' table. */
235
  SegmentMaps firstAxisSegmentMaps;
236
237
  public:
238
  DEFINE_SIZE_MIN (8);
239
};
240
241
} /* namespace OT */
242
243
244
#endif /* HB_OT_VAR_AVAR_TABLE_HH */