/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 | 0 | { |
53 | 0 | TRACE_SANITIZE (this); |
54 | 0 | return_trace (varIdxMap.sanitize (c, base) && |
55 | 0 | varStore.sanitize (c, base)); |
56 | 0 | } |
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 | | void set_mapping (float from_coord, float to_coord) |
76 | 0 | { |
77 | 0 | coords[0].set_float (from_coord); |
78 | 0 | coords[1].set_float (to_coord); |
79 | 0 | } |
80 | | |
81 | | bool is_outside_axis_range (const Triple& axis_range) const |
82 | 0 | { |
83 | 0 | float from_coord = coords[0].to_float (); |
84 | 0 | return !axis_range.contains (from_coord); |
85 | 0 | } |
86 | | |
87 | | bool must_include () const |
88 | 0 | { |
89 | 0 | float from_coord = coords[0].to_float (); |
90 | 0 | float to_coord = coords[1].to_float (); |
91 | 0 | return (from_coord == -1.f && to_coord == -1.f) || |
92 | 0 | (from_coord == 0.f && to_coord == 0.f) || |
93 | 0 | (from_coord == 1.f && to_coord == 1.f); |
94 | 0 | } |
95 | | |
96 | | void instantiate (const Triple& axis_range, |
97 | | const Triple& unmapped_range, |
98 | | const TripleDistances& triple_distances) |
99 | 0 | { |
100 | 0 | float from_coord = coords[0].to_float (); |
101 | 0 | float to_coord = coords[1].to_float (); |
102 | 0 |
|
103 | 0 | from_coord = renormalizeValue (from_coord, unmapped_range, triple_distances); |
104 | 0 | to_coord = renormalizeValue (to_coord, axis_range, triple_distances); |
105 | 0 |
|
106 | 0 | coords[0].set_float (from_coord); |
107 | 0 | coords[1].set_float (to_coord); |
108 | 0 | } |
109 | | |
110 | | HB_INTERNAL static int cmp (const void *pa, const void *pb) |
111 | 0 | { |
112 | 0 | const AxisValueMap *a = (const AxisValueMap *) pa; |
113 | 0 | const AxisValueMap *b = (const AxisValueMap *) pb; |
114 | 0 |
|
115 | 0 | int a_from = a->coords[0].to_int (); |
116 | 0 | int b_from = b->coords[0].to_int (); |
117 | 0 | if (a_from != b_from) |
118 | 0 | return a_from - b_from; |
119 | 0 |
|
120 | 0 | /* this should never be reached. according to the spec, all of the axis |
121 | 0 | * value map records for a given axis must have different fromCoord values |
122 | 0 | * */ |
123 | 0 | int a_to = a->coords[1].to_int (); |
124 | 0 | int b_to = b->coords[1].to_int (); |
125 | 0 | return a_to - b_to; |
126 | 0 | } |
127 | | |
128 | | bool serialize (hb_serialize_context_t *c) const |
129 | 0 | { |
130 | 0 | TRACE_SERIALIZE (this); |
131 | 0 | return_trace (c->embed (this)); |
132 | 0 | } |
133 | | |
134 | | public: |
135 | | F2DOT14 coords[2]; |
136 | | // F2DOT14 fromCoord; /* A normalized coordinate value obtained using |
137 | | // * default normalization. */ |
138 | | // F2DOT14 toCoord; /* The modified, normalized coordinate value. */ |
139 | | |
140 | | public: |
141 | | DEFINE_SIZE_STATIC (4); |
142 | | }; |
143 | | |
144 | | struct SegmentMaps : Array16Of<AxisValueMap> |
145 | | { |
146 | | int map (int value, unsigned int from_offset = 0, unsigned int to_offset = 1) const |
147 | 0 | { |
148 | 0 | #define fromCoord coords[from_offset].to_int () |
149 | 0 | #define toCoord coords[to_offset].to_int () |
150 | | /* The following special-cases are not part of OpenType, which requires |
151 | | * that at least -1, 0, and +1 must be mapped. But we include these as |
152 | | * part of a better error recovery scheme. */ |
153 | 0 | if (len < 2) |
154 | 0 | { |
155 | 0 | if (!len) |
156 | 0 | return value; |
157 | 0 | else /* len == 1*/ |
158 | 0 | return value - arrayZ[0].fromCoord + arrayZ[0].toCoord; |
159 | 0 | } |
160 | | |
161 | 0 | if (value <= arrayZ[0].fromCoord) |
162 | 0 | return value - arrayZ[0].fromCoord + arrayZ[0].toCoord; |
163 | | |
164 | 0 | unsigned int i; |
165 | 0 | unsigned int count = len - 1; |
166 | 0 | for (i = 1; i < count && value > arrayZ[i].fromCoord; i++) |
167 | 0 | ; |
168 | |
|
169 | 0 | if (value >= arrayZ[i].fromCoord) |
170 | 0 | return value - arrayZ[i].fromCoord + arrayZ[i].toCoord; |
171 | | |
172 | 0 | if (unlikely (arrayZ[i-1].fromCoord == arrayZ[i].fromCoord)) |
173 | 0 | return arrayZ[i-1].toCoord; |
174 | | |
175 | 0 | int denom = arrayZ[i].fromCoord - arrayZ[i-1].fromCoord; |
176 | 0 | return roundf (arrayZ[i-1].toCoord + ((float) (arrayZ[i].toCoord - arrayZ[i-1].toCoord) * |
177 | 0 | (value - arrayZ[i-1].fromCoord)) / denom); |
178 | 0 | #undef toCoord |
179 | 0 | #undef fromCoord |
180 | 0 | } |
181 | | |
182 | 0 | int unmap (int value) const { return map (value, 1, 0); } |
183 | | |
184 | | Triple unmap_axis_range (const Triple& axis_range) const |
185 | 0 | { |
186 | 0 | F2DOT14 val, unmapped_val; |
187 | 0 |
|
188 | 0 | val.set_float (axis_range.minimum); |
189 | 0 | unmapped_val.set_int (unmap (val.to_int ())); |
190 | 0 | float unmapped_min = unmapped_val.to_float (); |
191 | 0 |
|
192 | 0 | val.set_float (axis_range.middle); |
193 | 0 | unmapped_val.set_int (unmap (val.to_int ())); |
194 | 0 | float unmapped_middle = unmapped_val.to_float (); |
195 | 0 |
|
196 | 0 | val.set_float (axis_range.maximum); |
197 | 0 | unmapped_val.set_int (unmap (val.to_int ())); |
198 | 0 | float unmapped_max = unmapped_val.to_float (); |
199 | 0 |
|
200 | 0 | return Triple{unmapped_min, unmapped_middle, unmapped_max}; |
201 | 0 | } |
202 | | |
203 | | bool subset (hb_subset_context_t *c, hb_tag_t axis_tag) const |
204 | 0 | { |
205 | 0 | TRACE_SUBSET (this); |
206 | 0 | /* avar mapped normalized axis range*/ |
207 | 0 | Triple *axis_range; |
208 | 0 | if (!c->plan->axes_location.has (axis_tag, &axis_range)) |
209 | 0 | return c->serializer->embed (*this); |
210 | 0 |
|
211 | 0 | TripleDistances *axis_triple_distances; |
212 | 0 | if (!c->plan->axes_triple_distances.has (axis_tag, &axis_triple_distances)) |
213 | 0 | return_trace (false); |
214 | 0 |
|
215 | 0 | auto *out = c->serializer->start_embed (this); |
216 | 0 | if (unlikely (!c->serializer->extend_min (out))) return_trace (false); |
217 | 0 |
|
218 | 0 | Triple unmapped_range = unmap_axis_range (*axis_range); |
219 | 0 |
|
220 | 0 | /* create a vector of retained mappings and sort */ |
221 | 0 | hb_vector_t<AxisValueMap> value_mappings; |
222 | 0 | for (const auto& _ : as_array ()) |
223 | 0 | { |
224 | 0 | if (_.is_outside_axis_range (unmapped_range)) |
225 | 0 | continue; |
226 | 0 | AxisValueMap mapping; |
227 | 0 | mapping = _; |
228 | 0 | mapping.instantiate (*axis_range, unmapped_range, *axis_triple_distances); |
229 | 0 | /* (-1, -1), (0, 0), (1, 1) mappings will be added later, so avoid |
230 | 0 | * duplicates here */ |
231 | 0 | if (mapping.must_include ()) |
232 | 0 | continue; |
233 | 0 | value_mappings.push (std::move (mapping)); |
234 | 0 | } |
235 | 0 |
|
236 | 0 | AxisValueMap m; |
237 | 0 | m.set_mapping (-1.f, -1.f); |
238 | 0 | value_mappings.push (m); |
239 | 0 |
|
240 | 0 | m.set_mapping (0.f, 0.f); |
241 | 0 | value_mappings.push (m); |
242 | 0 |
|
243 | 0 | m.set_mapping (1.f, 1.f); |
244 | 0 | value_mappings.push (m); |
245 | 0 |
|
246 | 0 | value_mappings.qsort (); |
247 | 0 |
|
248 | 0 | for (const auto& _ : value_mappings) |
249 | 0 | { |
250 | 0 | if (!_.serialize (c->serializer)) |
251 | 0 | return_trace (false); |
252 | 0 | } |
253 | 0 | return_trace (c->serializer->check_assign (out->len, value_mappings.length, HB_SERIALIZE_ERROR_INT_OVERFLOW)); |
254 | 0 | } |
255 | | |
256 | | public: |
257 | | DEFINE_SIZE_ARRAY (2, *this); |
258 | | }; |
259 | | |
260 | | struct avar |
261 | | { |
262 | | static constexpr hb_tag_t tableTag = HB_OT_TAG_avar; |
263 | | |
264 | 0 | bool has_data () const { return version.to_int (); } |
265 | | |
266 | | const SegmentMaps* get_segment_maps () const |
267 | 0 | { return &firstAxisSegmentMaps; } |
268 | | |
269 | | unsigned get_axis_count () const |
270 | 0 | { return axisCount; } |
271 | | |
272 | | bool sanitize (hb_sanitize_context_t *c) const |
273 | 0 | { |
274 | 0 | TRACE_SANITIZE (this); |
275 | 0 | if (!(version.sanitize (c) && |
276 | 0 | (version.major == 1 |
277 | 0 | #ifndef HB_NO_AVAR2 |
278 | 0 | || version.major == 2 |
279 | 0 | #endif |
280 | 0 | ) && |
281 | 0 | c->check_struct (this))) |
282 | 0 | return_trace (false); |
283 | | |
284 | 0 | const SegmentMaps *map = &firstAxisSegmentMaps; |
285 | 0 | unsigned int count = axisCount; |
286 | 0 | for (unsigned int i = 0; i < count; i++) |
287 | 0 | { |
288 | 0 | if (unlikely (!map->sanitize (c))) |
289 | 0 | return_trace (false); |
290 | 0 | map = &StructAfter<SegmentMaps> (*map); |
291 | 0 | } |
292 | | |
293 | 0 | #ifndef HB_NO_AVAR2 |
294 | 0 | if (version.major < 2) |
295 | 0 | return_trace (true); |
296 | | |
297 | 0 | const auto &v2 = * (const avarV2Tail *) map; |
298 | 0 | if (unlikely (!v2.sanitize (c, this))) |
299 | 0 | return_trace (false); |
300 | 0 | #endif |
301 | | |
302 | 0 | return_trace (true); |
303 | 0 | } |
304 | | |
305 | | void map_coords (int *coords, unsigned int coords_length) const |
306 | 0 | { |
307 | 0 | unsigned int count = hb_min (coords_length, axisCount); |
308 | |
|
309 | 0 | const SegmentMaps *map = &firstAxisSegmentMaps; |
310 | 0 | for (unsigned int i = 0; i < count; i++) |
311 | 0 | { |
312 | 0 | coords[i] = map->map (coords[i]); |
313 | 0 | map = &StructAfter<SegmentMaps> (*map); |
314 | 0 | } |
315 | |
|
316 | 0 | #ifndef HB_NO_AVAR2 |
317 | 0 | if (version.major < 2) |
318 | 0 | return; |
319 | | |
320 | 0 | for (; count < axisCount; count++) |
321 | 0 | map = &StructAfter<SegmentMaps> (*map); |
322 | |
|
323 | 0 | const auto &v2 = * (const avarV2Tail *) map; |
324 | |
|
325 | 0 | const auto &varidx_map = this+v2.varIdxMap; |
326 | 0 | const auto &var_store = this+v2.varStore; |
327 | 0 | auto *var_store_cache = var_store.create_cache (); |
328 | |
|
329 | 0 | hb_vector_t<int> out; |
330 | 0 | out.alloc (coords_length); |
331 | 0 | for (unsigned i = 0; i < coords_length; i++) |
332 | 0 | { |
333 | 0 | int v = coords[i]; |
334 | 0 | uint32_t varidx = varidx_map.map (i); |
335 | 0 | float delta = var_store.get_delta (varidx, coords, coords_length, var_store_cache); |
336 | 0 | v += roundf (delta); |
337 | 0 | v = hb_clamp (v, -(1<<14), +(1<<14)); |
338 | 0 | out.push (v); |
339 | 0 | } |
340 | 0 | for (unsigned i = 0; i < coords_length; i++) |
341 | 0 | coords[i] = out[i]; |
342 | |
|
343 | 0 | OT::VariationStore::destroy_cache (var_store_cache); |
344 | 0 | #endif |
345 | 0 | } |
346 | | |
347 | | void unmap_coords (int *coords, unsigned int coords_length) const |
348 | 0 | { |
349 | 0 | unsigned int count = hb_min (coords_length, axisCount); |
350 | |
|
351 | 0 | const SegmentMaps *map = &firstAxisSegmentMaps; |
352 | 0 | for (unsigned int i = 0; i < count; i++) |
353 | 0 | { |
354 | 0 | coords[i] = map->unmap (coords[i]); |
355 | 0 | map = &StructAfter<SegmentMaps> (*map); |
356 | 0 | } |
357 | 0 | } |
358 | | |
359 | | bool subset (hb_subset_context_t *c) const |
360 | 0 | { |
361 | 0 | TRACE_SUBSET (this); |
362 | 0 | unsigned retained_axis_count = c->plan->axes_index_map.get_population (); |
363 | 0 | if (!retained_axis_count) //all axes are pinned/dropped |
364 | 0 | return_trace (false); |
365 | 0 |
|
366 | 0 | avar *out = c->serializer->allocate_min<avar> (); |
367 | 0 | if (unlikely (!out)) return_trace (false); |
368 | 0 |
|
369 | 0 | out->version.major = 1; |
370 | 0 | out->version.minor = 0; |
371 | 0 | if (!c->serializer->check_assign (out->axisCount, retained_axis_count, HB_SERIALIZE_ERROR_INT_OVERFLOW)) |
372 | 0 | return_trace (false); |
373 | 0 |
|
374 | 0 | const hb_map_t& axes_index_map = c->plan->axes_index_map; |
375 | 0 | const SegmentMaps *map = &firstAxisSegmentMaps; |
376 | 0 | unsigned count = axisCount; |
377 | 0 | for (unsigned int i = 0; i < count; i++) |
378 | 0 | { |
379 | 0 | if (axes_index_map.has (i)) |
380 | 0 | { |
381 | 0 | hb_tag_t *axis_tag; |
382 | 0 | if (!c->plan->axes_old_index_tag_map.has (i, &axis_tag)) |
383 | 0 | return_trace (false); |
384 | 0 | if (!map->subset (c, *axis_tag)) |
385 | 0 | return_trace (false); |
386 | 0 | } |
387 | 0 | map = &StructAfter<SegmentMaps> (*map); |
388 | 0 | } |
389 | 0 | return_trace (true); |
390 | 0 | } |
391 | | |
392 | | protected: |
393 | | FixedVersion<>version; /* Version of the avar table |
394 | | * initially set to 0x00010000u */ |
395 | | HBUINT16 reserved; /* This field is permanently reserved. Set to 0. */ |
396 | | HBUINT16 axisCount; /* The number of variation axes in the font. This |
397 | | * must be the same number as axisCount in the |
398 | | * 'fvar' table. */ |
399 | | SegmentMaps firstAxisSegmentMaps; |
400 | | |
401 | | public: |
402 | | DEFINE_SIZE_MIN (8); |
403 | | }; |
404 | | |
405 | | } /* namespace OT */ |
406 | | |
407 | | |
408 | | #endif /* HB_OT_VAR_AVAR_TABLE_HH */ |