/src/harfbuzz/src/hb-ot-var-avar-table.hh
Line | Count | Source |
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 | | #include "hb-ot-var-fvar-table.hh" |
33 | | |
34 | | |
35 | | /* |
36 | | * avar -- Axis Variations |
37 | | * https://docs.microsoft.com/en-us/typography/opentype/spec/avar |
38 | | */ |
39 | | |
40 | | #define HB_OT_TAG_avar HB_TAG('a','v','a','r') |
41 | | |
42 | | |
43 | | namespace OT { |
44 | | |
45 | | |
46 | | /* "Spec": https://github.com/be-fonts/boring-expansion-spec/issues/14 */ |
47 | | struct avarV2Tail |
48 | | { |
49 | | friend struct avar; |
50 | | |
51 | | bool sanitize (hb_sanitize_context_t *c, |
52 | | const void *base) const |
53 | 0 | { |
54 | 0 | TRACE_SANITIZE (this); |
55 | 0 | return_trace (varIdxMap.sanitize (c, base) && |
56 | 0 | varStore.sanitize (c, base)); |
57 | 0 | } |
58 | | |
59 | | protected: |
60 | | Offset32To<DeltaSetIndexMap> varIdxMap; /* Offset from the beginning of 'avar' table. */ |
61 | | Offset32To<ItemVariationStore> varStore; /* Offset from the beginning of 'avar' table. */ |
62 | | |
63 | | public: |
64 | | DEFINE_SIZE_STATIC (8); |
65 | | }; |
66 | | |
67 | | |
68 | | struct AxisValueMap |
69 | | { |
70 | | bool sanitize (hb_sanitize_context_t *c) const |
71 | 0 | { |
72 | 0 | TRACE_SANITIZE (this); |
73 | 0 | return_trace (c->check_struct (this)); |
74 | 0 | } |
75 | | |
76 | | void set_mapping (float from_coord, float to_coord) |
77 | 0 | { |
78 | 0 | coords[0].set_float (from_coord); |
79 | 0 | coords[1].set_float (to_coord); |
80 | 0 | } |
81 | | |
82 | | bool is_outside_axis_range (const Triple& axis_range) const |
83 | 0 | { |
84 | 0 | double from_coord = (double) coords[0].to_float (); |
85 | 0 | return !axis_range.contains (from_coord); |
86 | 0 | } |
87 | | |
88 | | bool must_include () const |
89 | 0 | { |
90 | 0 | float from_coord = coords[0].to_float (); |
91 | 0 | float to_coord = coords[1].to_float (); |
92 | 0 | return (from_coord == -1.f && to_coord == -1.f) || |
93 | 0 | (from_coord == 0.f && to_coord == 0.f) || |
94 | 0 | (from_coord == 1.f && to_coord == 1.f); |
95 | 0 | } |
96 | | |
97 | | void instantiate (const Triple& axis_range, |
98 | | const Triple& unmapped_range, |
99 | | const TripleDistances& triple_distances) |
100 | 0 | { |
101 | 0 | float from_coord = coords[0].to_float (); |
102 | 0 | float to_coord = coords[1].to_float (); |
103 | 0 |
|
104 | 0 | from_coord = renormalizeValue ((double) from_coord, unmapped_range, triple_distances); |
105 | 0 | to_coord = renormalizeValue ((double) to_coord, axis_range, triple_distances); |
106 | 0 |
|
107 | 0 | coords[0].set_float (from_coord); |
108 | 0 | coords[1].set_float (to_coord); |
109 | 0 | } |
110 | | |
111 | | HB_INTERNAL static int cmp (const void *pa, const void *pb) |
112 | 0 | { |
113 | 0 | const AxisValueMap *a = (const AxisValueMap *) pa; |
114 | 0 | const AxisValueMap *b = (const AxisValueMap *) pb; |
115 | 0 |
|
116 | 0 | int a_from = a->coords[0].to_int (); |
117 | 0 | int b_from = b->coords[0].to_int (); |
118 | 0 | if (a_from != b_from) |
119 | 0 | return a_from - b_from; |
120 | 0 |
|
121 | 0 | /* this should never be reached. according to the spec, all of the axis |
122 | 0 | * value map records for a given axis must have different fromCoord values |
123 | 0 | * */ |
124 | 0 | int a_to = a->coords[1].to_int (); |
125 | 0 | int b_to = b->coords[1].to_int (); |
126 | 0 | return a_to - b_to; |
127 | 0 | } |
128 | | |
129 | | bool serialize (hb_serialize_context_t *c) const |
130 | 0 | { |
131 | 0 | TRACE_SERIALIZE (this); |
132 | 0 | return_trace (c->embed (this)); |
133 | 0 | } |
134 | | |
135 | | public: |
136 | | F2DOT14 coords[2]; |
137 | | // F2DOT14 fromCoord; /* A normalized coordinate value obtained using |
138 | | // * default normalization. */ |
139 | | // F2DOT14 toCoord; /* The modified, normalized coordinate value. */ |
140 | | |
141 | | public: |
142 | | DEFINE_SIZE_STATIC (4); |
143 | | }; |
144 | | |
145 | | struct SegmentMaps : Array16Of<AxisValueMap> |
146 | | { |
147 | | float map_float (float value, unsigned int from_offset = 0, unsigned int to_offset = 1) const |
148 | 0 | { |
149 | 0 | #define fromCoord coords[from_offset].to_float () |
150 | 0 | #define toCoord coords[to_offset].to_float () |
151 | |
|
152 | 0 | const auto *map = arrayZ; |
153 | | |
154 | | /* The following special-cases are not part of OpenType, which requires |
155 | | * that at least -1, 0, and +1 must be mapped. But we include these as |
156 | | * part of a better error recovery scheme. */ |
157 | 0 | if (len < 2) |
158 | 0 | { |
159 | 0 | if (!len) |
160 | 0 | return value; |
161 | 0 | else /* len == 1*/ |
162 | 0 | return value - map[0].fromCoord + map[0].toCoord; |
163 | 0 | } |
164 | | |
165 | | // At least two mappings now. |
166 | | |
167 | | /* CoreText is wild... |
168 | | * PingFangUI avar needs all this special-casing... |
169 | | * So we implement an extended version of the spec here, |
170 | | * which is more robust and more likely to be compatible with |
171 | | * the wild. */ |
172 | | |
173 | 0 | unsigned start = 0; |
174 | 0 | unsigned end = len; |
175 | 0 | if (map[start].fromCoord == -1 && map[start].toCoord == -1 && map[start+1].fromCoord == -1) |
176 | 0 | start++; |
177 | 0 | if (map[end-1].fromCoord == +1 && map[end-1].toCoord == +1 && map[end-2].fromCoord == +1) |
178 | 0 | end--; |
179 | | |
180 | | /* Look for exact match first, and do lots of special-casing. */ |
181 | 0 | unsigned i; |
182 | 0 | for (i = start; i < end; i++) |
183 | 0 | if (value == map[i].fromCoord) |
184 | 0 | break; |
185 | 0 | if (i < end) |
186 | 0 | { |
187 | | // There's at least one exact match. See if there are more. |
188 | 0 | unsigned j = i; |
189 | 0 | for (; j + 1 < end; j++) |
190 | 0 | if (value != map[j + 1].fromCoord) |
191 | 0 | break; |
192 | | |
193 | | // [i,j] inclusive are all exact matches: |
194 | | |
195 | | // If there's only one, return it. This is the only spec-compliant case. |
196 | 0 | if (i == j) |
197 | 0 | return map[i].toCoord; |
198 | | // If there's exactly three, return the middle one. |
199 | 0 | if (i + 2 == j) |
200 | 0 | return map[i + 1].toCoord; |
201 | | |
202 | | // Ignore the middle ones. Return the one mapping closer to 0. |
203 | 0 | if (value < 0) return map[j].toCoord; |
204 | 0 | if (value > 0) return map[i].toCoord; |
205 | | |
206 | | // Mapping 0? CoreText seems confused. It seems to prefer 0 here... |
207 | | // So we'll just return the smallest one. lol |
208 | 0 | return fabsf (map[i].toCoord) < fabsf (map[j].toCoord) ? map[i].toCoord : map[j].toCoord; |
209 | | |
210 | | // Mapping 0? Return one not mapping to 0. |
211 | 0 | if (map[i].toCoord == 0) |
212 | 0 | return map[j].toCoord; |
213 | 0 | else |
214 | 0 | return map[i].toCoord; |
215 | 0 | } |
216 | | |
217 | | /* There's at least two and we're not an exact match. Prepare to lerp. */ |
218 | | |
219 | | // Find the segment we're in. |
220 | 0 | for (i = start; i < end; i++) |
221 | 0 | if (value < map[i].fromCoord) |
222 | 0 | break; |
223 | |
|
224 | 0 | if (i == start) |
225 | 0 | { |
226 | | // Value before all segments; Shift. |
227 | 0 | return value - map[start].fromCoord + map[start].toCoord; |
228 | 0 | } |
229 | 0 | if (i == end) |
230 | 0 | { |
231 | | // Value after all segments; Shift. |
232 | 0 | return value - map[end - 1].fromCoord + map[end - 1].toCoord; |
233 | 0 | } |
234 | | |
235 | | // Actually interpolate. |
236 | 0 | auto &before = map[i-1]; |
237 | 0 | auto &after = map[i]; |
238 | 0 | float denom = after.fromCoord - before.fromCoord; // Can't be zero by now. |
239 | 0 | return before.toCoord + ((after.toCoord - before.toCoord) * (value - before.fromCoord)) / denom; |
240 | |
|
241 | 0 | #undef toCoord |
242 | 0 | #undef fromCoord |
243 | 0 | } |
244 | | |
245 | 0 | float unmap_float (float value) const { return map_float (value, 1, 0); } |
246 | | |
247 | | |
248 | | // TODO Kill this. |
249 | | Triple unmap_axis_range (const Triple& axis_range) const |
250 | 0 | { |
251 | 0 | float unmapped_min = unmap_float (axis_range.minimum); |
252 | 0 | float unmapped_middle = unmap_float (axis_range.middle); |
253 | 0 | float unmapped_max = unmap_float (axis_range.maximum); |
254 | 0 |
|
255 | 0 | return Triple{(double) unmapped_min, (double) unmapped_middle, (double) unmapped_max}; |
256 | 0 | } |
257 | | |
258 | | bool subset (hb_subset_context_t *c, hb_tag_t axis_tag, |
259 | | hb_vector_t<AxisValueMap> *out_mappings = nullptr) const |
260 | 0 | { |
261 | 0 | TRACE_SUBSET (this); |
262 | 0 |
|
263 | 0 | /* This function cannot work on avar2 table (and currently doesn't). |
264 | 0 | * We should instead keep the design coords in the shape plan and use |
265 | 0 | * those. unmap_axis_range needs to be killed. */ |
266 | 0 |
|
267 | 0 | /* avar mapped normalized axis range. Under avar2, axes_location holds |
268 | 0 | * only the self-contained pins for the other tables; avar itself uses |
269 | 0 | * the intermediate-space ranges of all restricted axes. */ |
270 | 0 | const auto &axes_location = c->plan->has_avar2 |
271 | 0 | ? c->plan->old_intermediates |
272 | 0 | : c->plan->axes_location; |
273 | 0 | Triple *axis_range; |
274 | 0 | if (!axes_location.has (axis_tag, &axis_range)) |
275 | 0 | return c->serializer->embed (*this); |
276 | 0 |
|
277 | 0 | TripleDistances *axis_triple_distances; |
278 | 0 | if (!c->plan->axes_triple_distances.has (axis_tag, &axis_triple_distances)) |
279 | 0 | return_trace (false); |
280 | 0 |
|
281 | 0 | auto *out = c->serializer->start_embed (this); |
282 | 0 | if (unlikely (!c->serializer->extend_min (out))) return_trace (false); |
283 | 0 |
|
284 | 0 | Triple unmapped_range = unmap_axis_range (*axis_range); |
285 | 0 |
|
286 | 0 | /* create a vector of retained mappings and sort */ |
287 | 0 | hb_vector_t<AxisValueMap> value_mappings; |
288 | 0 | for (const auto& _ : as_array ()) |
289 | 0 | { |
290 | 0 | if (_.is_outside_axis_range (unmapped_range)) |
291 | 0 | continue; |
292 | 0 | AxisValueMap mapping; |
293 | 0 | mapping = _; |
294 | 0 | mapping.instantiate (*axis_range, unmapped_range, *axis_triple_distances); |
295 | 0 | /* (-1, -1), (0, 0), (1, 1) mappings will be added later, so avoid |
296 | 0 | * duplicates here */ |
297 | 0 | if (mapping.must_include ()) |
298 | 0 | continue; |
299 | 0 | value_mappings.push (mapping); |
300 | 0 | } |
301 | 0 |
|
302 | 0 | AxisValueMap m; |
303 | 0 | m.set_mapping (-1.f, -1.f); |
304 | 0 | value_mappings.push (m); |
305 | 0 |
|
306 | 0 | m.set_mapping (0.f, 0.f); |
307 | 0 | value_mappings.push (m); |
308 | 0 |
|
309 | 0 | m.set_mapping (1.f, 1.f); |
310 | 0 | value_mappings.push (m); |
311 | 0 |
|
312 | 0 | value_mappings.qsort (); |
313 | 0 |
|
314 | 0 | if (unlikely (value_mappings.in_error ())) |
315 | 0 | return_trace (false); |
316 | 0 |
|
317 | 0 | for (const auto& _ : value_mappings) |
318 | 0 | { |
319 | 0 | if (!_.serialize (c->serializer)) |
320 | 0 | return_trace (false); |
321 | 0 | } |
322 | 0 | if (!c->serializer->check_assign (out->len, value_mappings.length, HB_SERIALIZE_ERROR_INT_OVERFLOW)) |
323 | 0 | return_trace (false); |
324 | 0 | /* Hand the instantiated mappings to the caller; avar2 offset |
325 | 0 | * compensation needs them to locate the new mapping's kinks. */ |
326 | 0 | if (out_mappings) |
327 | 0 | *out_mappings = std::move (value_mappings); |
328 | 0 | return_trace (true); |
329 | 0 | } |
330 | | |
331 | | public: |
332 | | DEFINE_SIZE_ARRAY (2, *this); |
333 | | }; |
334 | | |
335 | | /* One knot of the avar2 offset-compensation function |
336 | | * offset(z) = inv_renorm(z) - z, in new intermediate space. */ |
337 | | struct avar2_offset_knot_t |
338 | | { |
339 | | double z; |
340 | | double offset; |
341 | | }; |
342 | | |
343 | | /* Insert a knot keeping the vector sorted by z; first insertion wins on |
344 | | * duplicate z. */ |
345 | | static inline void |
346 | | _avar2_add_knot (hb_vector_t<avar2_offset_knot_t> &knots, double z, double offset) |
347 | 0 | { |
348 | 0 | unsigned i = 0; |
349 | 0 | while (i < knots.length && knots.arrayZ[i].z < z) i++; |
350 | 0 | if (i < knots.length && knots.arrayZ[i].z == z) return; |
351 | 0 | knots.push (avar2_offset_knot_t {0.0, 0.0}); |
352 | 0 | if (unlikely (knots.in_error ())) return; |
353 | 0 | for (unsigned j = knots.length - 1; j > i; j--) |
354 | 0 | knots.arrayZ[j] = knots.arrayZ[j - 1]; |
355 | 0 | knots.arrayZ[i] = avar2_offset_knot_t {z, offset}; |
356 | 0 | } Unexecuted instantiation: hb-font.cc:OT::_avar2_add_knot(hb_vector_t<OT::avar2_offset_knot_t, false>&, double, double) Unexecuted instantiation: hb-ot-var.cc:OT::_avar2_add_knot(hb_vector_t<OT::avar2_offset_knot_t, false>&, double, double) |
357 | | |
358 | | /* Piecewise-linear evaluation over the sorted knots. Inputs are always |
359 | | * within [-1, +1] and anchor knots at -1/0/+1 always exist. */ |
360 | | static inline double |
361 | | _avar2_eval_offset (const hb_vector_t<avar2_offset_knot_t> &knots, double z) |
362 | 0 | { |
363 | 0 | unsigned len = knots.length; |
364 | 0 | for (unsigned i = 0; i < len; i++) |
365 | 0 | { |
366 | 0 | if (z == knots.arrayZ[i].z) return knots.arrayZ[i].offset; |
367 | 0 | if (z < knots.arrayZ[i].z) |
368 | 0 | { |
369 | 0 | if (!i) return knots.arrayZ[0].offset; |
370 | 0 | const auto &before = knots.arrayZ[i - 1]; |
371 | 0 | const auto &after = knots.arrayZ[i]; |
372 | 0 | double denom = after.z - before.z; |
373 | 0 | return before.offset + (after.offset - before.offset) * (z - before.z) / denom; |
374 | 0 | } |
375 | 0 | } |
376 | 0 | return len ? knots.arrayZ[len - 1].offset : 0.0; |
377 | 0 | } Unexecuted instantiation: hb-font.cc:OT::_avar2_eval_offset(hb_vector_t<OT::avar2_offset_knot_t, false> const&, double) Unexecuted instantiation: hb-ot-var.cc:OT::_avar2_eval_offset(hb_vector_t<OT::avar2_offset_knot_t, false> const&, double) |
378 | | |
379 | | /* Piecewise-linear evaluation over instantiated avar v1 mappings, as |
380 | | * produced by SegmentMaps::subset (sorted, with -1/0/+1 anchors). |
381 | | * Matches SegmentMaps::map_float for such well-formed mappings. */ |
382 | | static inline double |
383 | | _avar2_map_new_mapping (const hb_vector_t<AxisValueMap> &mappings, double v, |
384 | | unsigned from_offset = 0, unsigned to_offset = 1) |
385 | 0 | { |
386 | 0 | unsigned len = mappings.length; |
387 | 0 | if (!len) return v; |
388 | 0 |
|
389 | 0 | for (unsigned i = 0; i < len; i++) |
390 | 0 | { |
391 | 0 | double from = (double) mappings.arrayZ[i].coords[from_offset].to_float (); |
392 | 0 | if (v == from) |
393 | 0 | return (double) mappings.arrayZ[i].coords[to_offset].to_float (); |
394 | 0 | if (v < from) |
395 | 0 | { |
396 | 0 | double to = (double) mappings.arrayZ[i].coords[to_offset].to_float (); |
397 | 0 | if (!i) return v - from + to; |
398 | 0 | double prev_from = (double) mappings.arrayZ[i - 1].coords[from_offset].to_float (); |
399 | 0 | double prev_to = (double) mappings.arrayZ[i - 1].coords[to_offset].to_float (); |
400 | 0 | double denom = from - prev_from; |
401 | 0 | if (denom == 0.0) return prev_to; |
402 | 0 | return prev_to + (to - prev_to) * (v - prev_from) / denom; |
403 | 0 | } |
404 | 0 | } |
405 | 0 | return v - (double) mappings.arrayZ[len - 1].coords[from_offset].to_float () |
406 | 0 | + (double) mappings.arrayZ[len - 1].coords[to_offset].to_float (); |
407 | 0 | } Unexecuted instantiation: hb-font.cc:OT::_avar2_map_new_mapping(hb_vector_t<OT::AxisValueMap, false> const&, double, unsigned int, unsigned int) Unexecuted instantiation: hb-ot-var.cc:OT::_avar2_map_new_mapping(hb_vector_t<OT::AxisValueMap, false> const&, double, unsigned int, unsigned int) |
408 | | |
409 | | /* Inverse of _avar2_map_new_mapping: pull an output coordinate back to a |
410 | | * preimage input coordinate. For a non-strictly-monotone mapping this picks |
411 | | * one preimage, which is fine for its only use (augmenting the error |
412 | | * estimator's sample set). */ |
413 | | static inline double |
414 | | _avar2_unmap_new_mapping (const hb_vector_t<AxisValueMap> &mappings, double v) |
415 | 0 | { |
416 | 0 | return _avar2_map_new_mapping (mappings, v, 1, 0); |
417 | 0 | } Unexecuted instantiation: hb-font.cc:OT::_avar2_unmap_new_mapping(hb_vector_t<OT::AxisValueMap, false> const&, double) Unexecuted instantiation: hb-ot-var.cc:OT::_avar2_unmap_new_mapping(hb_vector_t<OT::AxisValueMap, false> const&, double) |
418 | | |
419 | | /* Plain (non-avar) fvar-style normalization of a user value against a |
420 | | * user-space (min, default, max) triple. */ |
421 | | static inline double |
422 | | _avar2_normalize_value (double v, double min, double def, double max) |
423 | 0 | { |
424 | 0 | v = hb_clamp (v, min, max); |
425 | 0 | if (v == def) return 0.0; |
426 | 0 | if (v < def) return def == min ? 0.0 : (v - def) / (def - min); |
427 | 0 | return def == max ? 0.0 : (v - def) / (max - def); |
428 | 0 | } Unexecuted instantiation: hb-font.cc:OT::_avar2_normalize_value(double, double, double, double) Unexecuted instantiation: hb-ot-var.cc:OT::_avar2_normalize_value(double, double, double, double) |
429 | | |
430 | | /* Inverse of the above: map a normalized value back to user space. */ |
431 | | static inline double |
432 | | _avar2_denormalize_value (double v, double min, double def, double max) |
433 | 0 | { |
434 | 0 | if (v == 0.0) return def; |
435 | 0 | return v < 0.0 ? def + v * (def - min) : def + v * (max - def); |
436 | 0 | } Unexecuted instantiation: hb-font.cc:OT::_avar2_denormalize_value(double, double, double, double) Unexecuted instantiation: hb-ot-var.cc:OT::_avar2_denormalize_value(double, double, double, double) |
437 | | |
438 | | /* Estimate the residual offset-compensation error for one restricted axis: |
439 | | * the max |old-avar1-final - (new-avar1 + offset)| over the retained user |
440 | | * range, in F2Dot14 units. offset(z) is the piecewise-linear function |
441 | | * through the knots. The residual is dominated by F2Dot14 requantization of |
442 | | * a steep retained avar v1 segment (e.g. a moved default compressing part |
443 | | * of the axis into a narrow z band); offset compensation cannot remove it. |
444 | | * Used only to pick the better knot set and decide whether to warn. |
445 | | * |
446 | | * Sampled on a uniform grid augmented with the user-space preimages of |
447 | | * every kink of the residual (old/new avar v1 breakpoints and offset(z) |
448 | | * knots), so the worst kink cannot fall between uniform samples. The |
449 | | * pointwise F2Dot14 rounding makes this an estimate rather than an exact |
450 | | * bound, but every piecewise-linear extremum is visited. */ |
451 | | static inline unsigned |
452 | | _avar2_estimate_offset_error (const SegmentMaps &old_seg, |
453 | | const hb_vector_t<AxisValueMap> &new_mapping, |
454 | | double old_min, double old_def, double old_max, |
455 | | double new_min, double new_def, double new_max, |
456 | | const hb_vector_t<avar2_offset_knot_t> &knots) |
457 | 0 | { |
458 | 0 | constexpr unsigned samples = 257; |
459 | 0 | hb_vector_t<double> us; |
460 | 0 | if (unlikely (!us.alloc (samples + old_seg.as_array ().length + |
461 | 0 | new_mapping.length + knots.length))) |
462 | 0 | return UINT_MAX; /* estimator only; fail towards "worse" */ |
463 | 0 | for (unsigned i = 0; i < samples; i++) |
464 | 0 | us.push (new_min + (new_max - new_min) * i / (samples - 1)); |
465 | 0 | for (const auto &_ : old_seg.as_array ()) |
466 | 0 | us.push (_avar2_denormalize_value ((double) _.coords[0].to_float (), |
467 | 0 | old_min, old_def, old_max)); |
468 | 0 | for (const auto &_ : new_mapping) |
469 | 0 | us.push (_avar2_denormalize_value ((double) _.coords[0].to_float (), |
470 | 0 | new_min, new_def, new_max)); |
471 | 0 | for (const auto &knot : knots) |
472 | 0 | us.push (_avar2_denormalize_value ( |
473 | 0 | _avar2_unmap_new_mapping (new_mapping, knot.z), |
474 | 0 | new_min, new_def, new_max)); |
475 | 0 | if (unlikely (us.in_error ())) return UINT_MAX; |
476 | 0 |
|
477 | 0 | unsigned max_err = 0; |
478 | 0 | for (double u : us) |
479 | 0 | { |
480 | 0 | if (u < new_min || u > new_max) continue; |
481 | 0 | double n_old = _avar2_normalize_value (u, old_min, old_def, old_max); |
482 | 0 | double old_final = (double) old_seg.map_float ((float) n_old); |
483 | 0 | double n_new = _avar2_normalize_value (u, new_min, new_def, new_max); |
484 | 0 | double z = _avar2_map_new_mapping (new_mapping, n_new); |
485 | 0 | double new_final = z + _avar2_eval_offset (knots, z); |
486 | 0 | int err = abs ((int) roundf ((float) (old_final * 16384.0)) - |
487 | 0 | (int) roundf ((float) (new_final * 16384.0))); |
488 | 0 | if ((unsigned) err > max_err) max_err = err; |
489 | 0 | } |
490 | 0 | return max_err; |
491 | 0 | } Unexecuted instantiation: hb-font.cc:OT::_avar2_estimate_offset_error(OT::SegmentMaps const&, hb_vector_t<OT::AxisValueMap, false> const&, double, double, double, double, double, double, hb_vector_t<OT::avar2_offset_knot_t, false> const&) Unexecuted instantiation: hb-ot-var.cc:OT::_avar2_estimate_offset_error(OT::SegmentMaps const&, hb_vector_t<OT::AxisValueMap, false> const&, double, double, double, double, double, double, hb_vector_t<OT::avar2_offset_knot_t, false> const&) |
492 | | |
493 | | struct avar |
494 | | { |
495 | | static constexpr hb_tag_t tableTag = HB_OT_TAG_avar; |
496 | | |
497 | 0 | bool has_data () const { return version.to_int (); } |
498 | | |
499 | | const SegmentMaps* get_segment_maps () const |
500 | 0 | { return &firstAxisSegmentMaps; } |
501 | | |
502 | | unsigned get_axis_count () const |
503 | 0 | { return axisCount; } |
504 | | |
505 | | bool sanitize (hb_sanitize_context_t *c) const |
506 | 0 | { |
507 | 0 | TRACE_SANITIZE (this); |
508 | 0 | if (!(version.sanitize (c) && |
509 | 0 | hb_barrier () && |
510 | 0 | (version.major == 1 |
511 | 0 | #ifndef HB_NO_AVAR2 |
512 | 0 | || version.major == 2 |
513 | 0 | #endif |
514 | 0 | ) && |
515 | 0 | c->check_struct (this))) |
516 | 0 | return_trace (false); |
517 | | |
518 | 0 | const SegmentMaps *map = &firstAxisSegmentMaps; |
519 | 0 | unsigned int count = axisCount; |
520 | 0 | for (unsigned int i = 0; i < count; i++) |
521 | 0 | { |
522 | 0 | if (unlikely (!map->sanitize (c))) |
523 | 0 | return_trace (false); |
524 | 0 | map = &StructAfter<SegmentMaps> (*map); |
525 | 0 | } |
526 | | |
527 | 0 | #ifndef HB_NO_AVAR2 |
528 | 0 | if (version.major < 2) |
529 | 0 | return_trace (true); |
530 | 0 | hb_barrier (); |
531 | |
|
532 | 0 | const auto &v2 = * (const avarV2Tail *) map; |
533 | 0 | if (unlikely (!v2.sanitize (c, this))) |
534 | 0 | return_trace (false); |
535 | 0 | #endif |
536 | | |
537 | 0 | return_trace (true); |
538 | 0 | } |
539 | | |
540 | | void map_coords_16_16 (int *coords, unsigned int coords_length) const |
541 | 10.6k | { |
542 | 10.6k | unsigned int count = hb_min (coords_length, axisCount); |
543 | | |
544 | 10.6k | const SegmentMaps *map = &firstAxisSegmentMaps; |
545 | 10.6k | for (unsigned int i = 0; i < count; i++) |
546 | 0 | { |
547 | 0 | coords[i] = roundf (map->map_float (coords[i] / 65536.f) * 65536.f); |
548 | 0 | map = &StructAfter<SegmentMaps> (*map); |
549 | 0 | } |
550 | | |
551 | 10.6k | #ifndef HB_NO_AVAR2 |
552 | 10.6k | if (version.major < 2) |
553 | 10.6k | return; |
554 | 0 | hb_barrier (); |
555 | |
|
556 | 0 | for (; count < axisCount; count++) |
557 | 0 | map = &StructAfter<SegmentMaps> (*map); |
558 | |
|
559 | 0 | const auto &v2 = * (const avarV2Tail *) map; |
560 | |
|
561 | 0 | const auto &varidx_map = this+v2.varIdxMap; |
562 | 0 | const auto &var_store = this+v2.varStore; |
563 | 0 | auto *var_store_cache = var_store.create_cache (); |
564 | |
|
565 | 0 | hb_vector_t<int> coords_2_14; |
566 | 0 | coords_2_14.resize (coords_length); |
567 | 0 | for (unsigned i = 0; i < coords_length; i++) |
568 | 0 | coords_2_14[i] = roundf (coords[i] / 4.f); // 16.16 -> 2.14 |
569 | |
|
570 | 0 | hb_vector_t<int> out; |
571 | 0 | out.alloc (coords_length); |
572 | 0 | for (unsigned i = 0; i < coords_length; i++) |
573 | 0 | { |
574 | 0 | int v = coords[i]; |
575 | 0 | uint32_t varidx = varidx_map.map (i); |
576 | 0 | float delta = var_store.get_delta (varidx, coords_2_14.arrayZ, coords_2_14.length, var_store_cache); |
577 | | /* Apply the delta unclamped and clamp only the result to [-1, +1], |
578 | | * matching fontTools. Since inputs and results are in [-1, +1], |
579 | | * deltas beyond ±2 are equivalent to ±2; clamp to that range only |
580 | | * to keep the float->int conversion safe. */ |
581 | 0 | float d = hb_clamp (delta * 4, -(float) (1<<17), +(float) (1<<17)); // 2.14 -> 16.16 |
582 | 0 | v += (int) roundf (d); |
583 | 0 | v = hb_clamp (v, -(1<<16), +(1<<16)); |
584 | 0 | out.push (v); |
585 | 0 | } |
586 | 0 | for (unsigned i = 0; i < coords_length; i++) |
587 | 0 | coords[i] = out[i]; |
588 | |
|
589 | 0 | OT::ItemVariationStore::destroy_cache (var_store_cache); |
590 | 0 | #endif |
591 | 0 | } |
592 | | |
593 | | /* An avar version 2 font is never downgraded to v1: even with a NULL |
594 | | * varStore offset (legal; maps like plain v1) the avar2 subsetting path |
595 | | * applies, with "no variation" semantics for rows that don't resolve — |
596 | | * offset compensation then creates delta rows on demand. */ |
597 | 0 | bool has_v2_data () const { return version.major > 1; } |
598 | | |
599 | | /* Resolve the avar2 VarStore and VarIdxMap, for the subset planner. |
600 | | * Either pointer may be to the Null object (nullable offsets). */ |
601 | | bool get_v2_store_and_map (const ItemVariationStore **store, |
602 | | const DeltaSetIndexMap **varidx_map) const |
603 | 0 | { |
604 | 0 | #ifndef HB_NO_AVAR2 |
605 | 0 | if (version.major < 2) return false; |
606 | 0 | const SegmentMaps *map = &firstAxisSegmentMaps; |
607 | 0 | for (unsigned i = 0; i < axisCount; i++) |
608 | 0 | map = &StructAfter<SegmentMaps> (*map); |
609 | 0 | const auto &v2 = * (const avarV2Tail *) map; |
610 | 0 | *store = &(this+v2.varStore); |
611 | 0 | *varidx_map = &(this+v2.varIdxMap); |
612 | 0 | return true; |
613 | 0 | #else |
614 | 0 | return false; |
615 | 0 | #endif |
616 | 0 | } |
617 | | |
618 | | // axis normalization is done in 2.14 here |
619 | | // TODO: deprecate this API once fonttools is updated to use 16.16 normalization |
620 | | bool map_coords_2_14 (float *coords, unsigned int coords_length, |
621 | | bool v1_only = false) const |
622 | 0 | { |
623 | 0 | hb_vector_t<int> coords_2_14; |
624 | 0 | if (!v1_only && !coords_2_14.resize (coords_length)) return false; |
625 | 0 | unsigned int count = hb_min (coords_length, axisCount); |
626 | 0 |
|
627 | 0 | const SegmentMaps *map = &firstAxisSegmentMaps; |
628 | 0 | for (unsigned int i = 0; i < count; i++) |
629 | 0 | { |
630 | 0 | int v = roundf (map->map_float (coords[i]) * 16384.f); |
631 | 0 | if (!v1_only) |
632 | 0 | coords_2_14[i] = v; |
633 | 0 | coords[i] = v / 16384.f; |
634 | 0 | map = &StructAfter<SegmentMaps> (*map); |
635 | 0 | } |
636 | 0 |
|
637 | 0 | if (v1_only) |
638 | 0 | return true; |
639 | 0 |
|
640 | 0 | #ifndef HB_NO_AVAR2 |
641 | 0 | if (version.major < 2) |
642 | 0 | return true; |
643 | 0 | hb_barrier (); |
644 | 0 |
|
645 | 0 | for (; count < axisCount; count++) |
646 | 0 | map = &StructAfter<SegmentMaps> (*map); |
647 | 0 |
|
648 | 0 | const auto &v2 = * (const avarV2Tail *) map; |
649 | 0 |
|
650 | 0 | const auto &varidx_map = this+v2.varIdxMap; |
651 | 0 | const auto &var_store = this+v2.varStore; |
652 | 0 | auto *var_store_cache = var_store.create_cache (); |
653 | 0 |
|
654 | 0 | for (unsigned i = 0; i < coords_length; i++) |
655 | 0 | { |
656 | 0 | int v = coords_2_14[i]; |
657 | 0 | uint32_t varidx = varidx_map.map (i); |
658 | 0 | float delta = var_store.get_delta (varidx, coords_2_14.arrayZ, coords_2_14.length, var_store_cache); |
659 | 0 | /* As above: apply the delta unclamped (±2 covers every useful case) |
660 | 0 | * and clamp the result to [-1, +1], matching fontTools. */ |
661 | 0 | v += (int) hb_clamp (roundf (delta), -(float) (1<<15), +(float) (1<<15)); |
662 | 0 | v = hb_clamp (v, -(1<<14), +(1<<14)); |
663 | 0 | coords[i] = v / 16384.f; |
664 | 0 | } |
665 | 0 |
|
666 | 0 | OT::ItemVariationStore::destroy_cache (var_store_cache); |
667 | 0 | return true; |
668 | 0 | #else |
669 | 0 | return version.major < 2; |
670 | 0 | #endif |
671 | 0 | } |
672 | | |
673 | | bool subset (hb_subset_context_t *c) const |
674 | 0 | { |
675 | 0 | TRACE_SUBSET (this); |
676 | 0 | unsigned retained_axis_count = c->plan->axes_index_map.get_population (); |
677 | 0 | if (!retained_axis_count) //all axes are pinned/dropped |
678 | 0 | return_trace (false); |
679 | 0 |
|
680 | 0 | avar *out = c->serializer->allocate_min<avar> (); |
681 | 0 | if (unlikely (!out)) return_trace (false); |
682 | 0 |
|
683 | 0 | out->version.major = c->plan->has_avar2 ? 2 : 1; |
684 | 0 | out->version.minor = 0; |
685 | 0 | if (!c->serializer->check_assign (out->axisCount, retained_axis_count, HB_SERIALIZE_ERROR_INT_OVERFLOW)) |
686 | 0 | return_trace (false); |
687 | 0 |
|
688 | 0 | /* For avar2, keep the instantiated v1 mappings around; offset |
689 | 0 | * compensation needs them to locate the new mappings' kinks. */ |
690 | 0 | hb_vector_t<hb_vector_t<AxisValueMap>> new_mappings; |
691 | 0 | if (c->plan->has_avar2 && !new_mappings.resize (axisCount)) |
692 | 0 | return_trace (false); |
693 | 0 |
|
694 | 0 | const hb_map_t& axes_index_map = c->plan->axes_index_map; |
695 | 0 | const SegmentMaps *map = &firstAxisSegmentMaps; |
696 | 0 | unsigned count = axisCount; |
697 | 0 | for (unsigned int i = 0; i < count; i++) |
698 | 0 | { |
699 | 0 | if (axes_index_map.has (i)) |
700 | 0 | { |
701 | 0 | hb_tag_t *axis_tag; |
702 | 0 | if (!c->plan->axes_old_index_tag_map.has (i, &axis_tag)) |
703 | 0 | return_trace (false); |
704 | 0 |
|
705 | 0 | Triple *axis_location; |
706 | 0 | if (c->plan->has_avar2 && |
707 | 0 | c->plan->user_axes_location.has (*axis_tag, &axis_location) && |
708 | 0 | axis_location->is_point ()) |
709 | 0 | { |
710 | 0 | /* Pinned axis in avar2 mode: serialize identity segment map |
711 | 0 | * {-1->-1, 0->0, 1->1}. The axis is kept in fvar as hidden, |
712 | 0 | * so avar needs a segment map entry for it. */ |
713 | 0 | auto *identity_map = c->serializer->start_embed<SegmentMaps> (); |
714 | 0 | if (unlikely (!c->serializer->extend_min (identity_map))) |
715 | 0 | return_trace (false); |
716 | 0 | AxisValueMap m; |
717 | 0 | m.set_mapping (-1.f, -1.f); |
718 | 0 | if (!m.serialize (c->serializer)) return_trace (false); |
719 | 0 | m.set_mapping (0.f, 0.f); |
720 | 0 | if (!m.serialize (c->serializer)) return_trace (false); |
721 | 0 | m.set_mapping (1.f, 1.f); |
722 | 0 | if (!m.serialize (c->serializer)) return_trace (false); |
723 | 0 | if (!c->serializer->check_assign (identity_map->len, 3u, |
724 | 0 | HB_SERIALIZE_ERROR_INT_OVERFLOW)) |
725 | 0 | return_trace (false); |
726 | 0 | } |
727 | 0 | else |
728 | 0 | { |
729 | 0 | /* Restricted or free axis: use standard SegmentMaps::subset() */ |
730 | 0 | if (!map->subset (c, *axis_tag, |
731 | 0 | c->plan->has_avar2 ? &new_mappings[i] : nullptr)) |
732 | 0 | return_trace (false); |
733 | 0 | } |
734 | 0 | } |
735 | 0 | map = &StructAfter<SegmentMaps> (*map); |
736 | 0 | } |
737 | 0 |
|
738 | 0 | if (c->plan->has_avar2) |
739 | 0 | return_trace (_subset_avar2 (c, new_mappings)); |
740 | 0 |
|
741 | 0 | return_trace (true); |
742 | 0 | } |
743 | | |
744 | | private: |
745 | | struct avar2_index_map_plan_t |
746 | | { |
747 | | bool init (const hb_vector_t<uint32_t> &varidx_mapping, |
748 | | const hb_map_t &axes_index_map, |
749 | | unsigned axis_count) |
750 | 0 | { |
751 | 0 | if (!output_map.alloc (axes_index_map.get_population ())) |
752 | 0 | return false; |
753 | 0 |
|
754 | 0 | bool has_no_variation = false; |
755 | 0 | unsigned max_outer = 0, max_inner = 0; |
756 | 0 | for (unsigned i = 0; i < axis_count; i++) |
757 | 0 | { |
758 | 0 | if (!axes_index_map.has (i)) continue; |
759 | 0 | uint32_t varidx = varidx_mapping[i]; |
760 | 0 | output_map.push (varidx); |
761 | 0 | if (varidx == HB_OT_LAYOUT_NO_VARIATIONS_INDEX) |
762 | 0 | { |
763 | 0 | has_no_variation = true; |
764 | 0 | continue; |
765 | 0 | } |
766 | 0 | max_outer = hb_max (max_outer, varidx >> 16); |
767 | 0 | max_inner = hb_max (max_inner, varidx & 0xFFFF); |
768 | 0 | } |
769 | 0 | if (output_map.in_error () || |
770 | 0 | output_map.length != axes_index_map.get_population ()) |
771 | 0 | return false; |
772 | 0 |
|
773 | 0 | if (has_no_variation) |
774 | 0 | { |
775 | 0 | width = 4; |
776 | 0 | inner_bit_count = 16; |
777 | 0 | } |
778 | 0 | else |
779 | 0 | { |
780 | 0 | inner_bit_count = hb_max (1u, hb_bit_storage (max_inner)); |
781 | 0 | unsigned outer_bit_count = hb_max (1u, hb_bit_storage (max_outer)); |
782 | 0 | width = hb_clamp ((inner_bit_count + outer_bit_count + 7) / 8, 1u, 4u); |
783 | 0 | if (inner_bit_count + outer_bit_count > width * 8) |
784 | 0 | inner_bit_count = width * 8 - outer_bit_count; |
785 | 0 | } |
786 | 0 | return true; |
787 | 0 | } |
788 | | |
789 | 0 | unsigned get_inner_bit_count () const { return inner_bit_count; } |
790 | 0 | unsigned get_width () const { return width; } |
791 | 0 | hb_array_t<const uint32_t> get_output_map () const { return output_map.as_array (); } |
792 | | |
793 | | unsigned inner_bit_count = 1; |
794 | | unsigned width = 1; |
795 | | hb_vector_t<uint32_t> output_map; |
796 | | }; |
797 | | |
798 | | bool _subset_avar2 (hb_subset_context_t *c, |
799 | | const hb_vector_t<hb_vector_t<AxisValueMap>> &new_mappings) const |
800 | 0 | { |
801 | 0 | #if defined (HB_NO_VAR) || defined (HB_NO_AVAR2) |
802 | 0 | /* Not reachable: the plan never sets has_avar2 in these configurations. */ |
803 | 0 | return false; |
804 | 0 | #else |
805 | 0 |
|
806 | 0 | /* 1. Locate original avar2 data, keeping per-axis old segment maps */ |
807 | 0 | hb_vector_t<const SegmentMaps *> old_seg_maps; |
808 | 0 | if (!old_seg_maps.alloc (axisCount)) return false; |
809 | 0 | const SegmentMaps *map = &firstAxisSegmentMaps; |
810 | 0 | for (unsigned i = 0; i < axisCount; i++) |
811 | 0 | { |
812 | 0 | old_seg_maps.push (map); |
813 | 0 | map = &StructAfter<SegmentMaps> (*map); |
814 | 0 | } |
815 | 0 |
|
816 | 0 | const auto &v2 = * (const avarV2Tail *) map; |
817 | 0 | const auto &varidx_map = this+v2.varIdxMap; |
818 | 0 | const auto &var_store = this+v2.varStore; |
819 | 0 |
|
820 | 0 | auto fvar_axes = c->plan->source->table.fvar->get_axes (); |
821 | 0 |
|
822 | 0 | /* 2. Compute default deltas by evaluating VarStore at old defaults */ |
823 | 0 | hb_vector_t<int> default_coords; |
824 | 0 | if (!default_coords.resize (axisCount)) return false; |
825 | 0 | for (unsigned i = 0; i < axisCount; i++) |
826 | 0 | { |
827 | 0 | hb_tag_t *axis_tag; |
828 | 0 | if (c->plan->axes_old_index_tag_map.has (i, &axis_tag) && |
829 | 0 | c->plan->old_intermediates.has (*axis_tag)) |
830 | 0 | { |
831 | 0 | float d_i = (float) c->plan->old_intermediates.get (*axis_tag).middle; |
832 | 0 | default_coords[i] = roundf (d_i * 16384.f); |
833 | 0 | } |
834 | 0 | else |
835 | 0 | default_coords[i] = 0; |
836 | 0 | } |
837 | 0 |
|
838 | 0 | auto *store_cache = var_store.create_cache (); |
839 | 0 | hb_vector_t<float> default_deltas; |
840 | 0 | if (!default_deltas.resize (axisCount)) { |
841 | 0 | ItemVariationStore::destroy_cache (store_cache); |
842 | 0 | return false; |
843 | 0 | } |
844 | 0 | for (unsigned i = 0; i < axisCount; i++) |
845 | 0 | { |
846 | 0 | uint32_t varidx = varidx_map.map (i); |
847 | 0 | if (varidx == HB_OT_LAYOUT_NO_VARIATIONS_INDEX) |
848 | 0 | default_deltas[i] = 0.f; |
849 | 0 | else |
850 | 0 | default_deltas[i] = var_store.get_delta (varidx, default_coords.arrayZ, |
851 | 0 | default_coords.length, store_cache); |
852 | 0 | } |
853 | 0 | ItemVariationStore::destroy_cache (store_cache); |
854 | 0 |
|
855 | 0 | /* 3. Rebase IVS regions */ |
856 | 0 | item_variations_t item_vars; |
857 | 0 | if (!item_vars.create_from_item_varstore (var_store, c->plan->axes_old_index_tag_map)) |
858 | 0 | return false; |
859 | 0 | if (!item_vars.instantiate_tuple_vars (c->plan->old_intermediates, |
860 | 0 | c->plan->axes_triple_distances, |
861 | 0 | false)) |
862 | 0 | return false; |
863 | 0 |
|
864 | 0 | /* 4. Self-contained pinned axes (whose final coordinate is constant over |
865 | 0 | * the retained box) were detected at plan time |
866 | 0 | * (_compute_avar2_reachable_ranges) and removed from axes_index_map. |
867 | 0 | * They are skipped below; their constant contribution is baked into the |
868 | 0 | * other variation tables by standard instancing at the plan's |
869 | 0 | * axes_location/normalized_coords. */ |
870 | 0 |
|
871 | 0 | /* 5. Build per-axis varIdx mapping (may create new VarDatas). |
872 | 0 | * Entries (or the implicit identity mapping) that don't resolve to a |
873 | 0 | * real store row behave as "no variation" at runtime; normalize them to |
874 | 0 | * NO_VARIATIONS_INDEX so the offset loop creates fresh rows instead of |
875 | 0 | * writing into nonexistent ones. This also covers a NULL VarStore |
876 | 0 | * (never downgraded: rows are created on demand). */ |
877 | 0 | hb_vector_t<uint32_t> new_varidx_mapping; |
878 | 0 | if (!new_varidx_mapping.resize (axisCount)) return false; |
879 | 0 | for (unsigned i = 0; i < axisCount; i++) |
880 | 0 | { |
881 | 0 | uint32_t varidx = varidx_map.map (i); |
882 | 0 | if (varidx != HB_OT_LAYOUT_NO_VARIATIONS_INDEX && |
883 | 0 | !var_store.has_delta_set (varidx)) |
884 | 0 | varidx = HB_OT_LAYOUT_NO_VARIATIONS_INDEX; |
885 | 0 | new_varidx_mapping[i] = varidx; |
886 | 0 | } |
887 | 0 |
|
888 | 0 | /* 5.5. Privatize shared varIdx delta rows before adding offset |
889 | 0 | * compensation. avar2's VarIdxMap may map several fvar axes to the SAME |
890 | 0 | * IVS delta row. Writing one axis's offset-compensation deltas into a |
891 | 0 | * shared row would corrupt every other axis that reads that row. So give |
892 | 0 | * each offset-receiving axis whose row is shared its own private copy of |
893 | 0 | * the row (identical contents, preserving the rebased deltas), then |
894 | 0 | * repoint its varIdx. Sharers keep the clean row; the varstore |
895 | 0 | * optimization pass re-merges identical rows afterwards. */ |
896 | 0 | hb_hashmap_t<uint32_t, unsigned> varidx_ref_count; |
897 | 0 | for (unsigned i = 0; i < axisCount; i++) |
898 | 0 | { |
899 | 0 | if (!c->plan->axes_index_map.has (i)) continue; /* self-contained: dropped */ |
900 | 0 | uint32_t varidx = new_varidx_mapping[i]; |
901 | 0 | if (varidx == HB_OT_LAYOUT_NO_VARIATIONS_INDEX) continue; |
902 | 0 | unsigned *count; |
903 | 0 | if (varidx_ref_count.has (varidx, &count)) |
904 | 0 | (*count)++; |
905 | 0 | else if (!varidx_ref_count.set (varidx, 1)) |
906 | 0 | return false; |
907 | 0 | } |
908 | 0 | for (unsigned i = 0; i < axisCount; i++) |
909 | 0 | { |
910 | 0 | hb_tag_t *axis_tag_ptr; |
911 | 0 | if (!c->plan->axes_old_index_tag_map.has (i, &axis_tag_ptr)) |
912 | 0 | return false; |
913 | 0 | /* Only axes that will receive offset compensation (restricted or |
914 | 0 | * pinned) can contaminate a shared row. */ |
915 | 0 | if (!c->plan->axes_index_map.has (i) || |
916 | 0 | !c->plan->user_axes_location.has (*axis_tag_ptr)) |
917 | 0 | continue; |
918 | 0 | uint32_t varidx = new_varidx_mapping[i]; |
919 | 0 | if (varidx == HB_OT_LAYOUT_NO_VARIATIONS_INDEX) |
920 | 0 | continue; /* gets a fresh, private VarData in the offset loop below */ |
921 | 0 | unsigned *count; |
922 | 0 | if (!varidx_ref_count.has (varidx, &count) || *count <= 1) |
923 | 0 | continue; /* sole owner: safe to write offsets in place */ |
924 | 0 | unsigned outer = varidx >> 16; |
925 | 0 | unsigned new_inner = item_vars.duplicate_row (outer, varidx & 0xFFFF); |
926 | 0 | if (unlikely (new_inner == (unsigned) -1)) return false; |
927 | 0 | new_varidx_mapping[i] = (outer << 16) | new_inner; |
928 | 0 | (*count)--; |
929 | 0 | } |
930 | 0 |
|
931 | 0 | /* 6. Add offset compensation tuples. |
932 | 0 | * Track processed (outer,inner) pairs to avoid adding duplicate biases |
933 | 0 | * when multiple axes share the same varIdx. */ |
934 | 0 | hb_set_t processed_varidxes; |
935 | 0 | for (unsigned i = 0; i < axisCount; i++) |
936 | 0 | { |
937 | 0 | hb_tag_t *axis_tag_ptr; |
938 | 0 | if (!c->plan->axes_old_index_tag_map.has (i, &axis_tag_ptr)) |
939 | 0 | return false; |
940 | 0 | hb_tag_t axis_tag = *axis_tag_ptr; |
941 | 0 |
|
942 | 0 | /* Self-contained pinned axes are removed from fvar/avar; their |
943 | 0 | * contribution is baked into the variation tables instead. */ |
944 | 0 | if (!c->plan->axes_index_map.has (i)) |
945 | 0 | continue; |
946 | 0 |
|
947 | 0 | Triple *new_user; |
948 | 0 | if (c->plan->user_axes_location.has (axis_tag, &new_user)) |
949 | 0 | { |
950 | 0 | /* This axis is being restricted or pinned */ |
951 | 0 | Triple *old_int; |
952 | 0 | if (!c->plan->old_intermediates.has (axis_tag, &old_int)) |
953 | 0 | return false; |
954 | 0 |
|
955 | 0 | float a_i = (float) old_int->minimum; |
956 | 0 | float d_i = (float) old_int->middle; |
957 | 0 | float b_i = (float) old_int->maximum; |
958 | 0 |
|
959 | 0 | int d_int = roundf (d_i * 16384.f); |
960 | 0 |
|
961 | 0 | bool is_pinned = new_user->is_point (); |
962 | 0 |
|
963 | 0 | uint32_t varidx = new_varidx_mapping[i]; |
964 | 0 | unsigned outer, inner, item_count; |
965 | 0 |
|
966 | 0 | if (varidx == HB_OT_LAYOUT_NO_VARIATIONS_INDEX) |
967 | 0 | { |
968 | 0 | /* No existing avar2 mapping. Create new VarData. */ |
969 | 0 | outer = item_vars.add_vardata (1); |
970 | 0 | inner = 0; |
971 | 0 | item_count = 1; |
972 | 0 | new_varidx_mapping[i] = (outer << 16) | inner; |
973 | 0 | default_deltas[i] = 0.f; /* no prior default delta */ |
974 | 0 | } |
975 | 0 | else |
976 | 0 | { |
977 | 0 | outer = varidx >> 16; |
978 | 0 | inner = varidx & 0xFFFF; |
979 | 0 | item_count = item_vars.get_item_count (outer); |
980 | 0 | } |
981 | 0 |
|
982 | 0 | /* Empty-region bias: d_int + round(defaultDelta) */ |
983 | 0 | int bias = d_int + (int) roundf (default_deltas[i]); |
984 | 0 | if (bias != 0) |
985 | 0 | { |
986 | 0 | hb_hashmap_t<hb_tag_t, Triple> empty_region; |
987 | 0 | item_vars.add_tuple (outer, std::move (empty_region), |
988 | 0 | inner, bias, item_count); |
989 | 0 | } |
990 | 0 |
|
991 | 0 | if (!is_pinned) |
992 | 0 | { |
993 | 0 | /* Offset compensation encodes, as avar2 deltas on this axis, the |
994 | 0 | * piecewise-linear function offset(z) = inv_renorm(z) - z, where |
995 | 0 | * inv_renorm maps a new intermediate coordinate z back to the old |
996 | 0 | * intermediate coordinate. It is known at these knots in the new |
997 | 0 | * intermediate space: |
998 | 0 | * z = -1 -> a_i + 1 (new minimum) |
999 | 0 | * z = 0 -> d_i (new default) |
1000 | 0 | * z = +1 -> b_i - 1 (new maximum) |
1001 | 0 | */ |
1002 | 0 | hb_vector_t<avar2_offset_knot_t> knots; |
1003 | 0 | _avar2_add_knot (knots, -1.0, (double) a_i + 1.0); |
1004 | 0 | _avar2_add_knot (knots, 0.0, (double) d_i); |
1005 | 0 | _avar2_add_knot (knots, 1.0, (double) b_i - 1.0); |
1006 | 0 |
|
1007 | 0 | const hb_vector_t<AxisValueMap> &new_mapping = new_mappings[i]; |
1008 | 0 |
|
1009 | 0 | float min_f = 0.f, def_f = 0.f, max_f = 0.f; |
1010 | 0 | if (likely (i < fvar_axes.length)) |
1011 | 0 | fvar_axes[i].get_coordinates (min_f, def_f, max_f); |
1012 | 0 | double old_min = (double) min_f; |
1013 | 0 | double old_def = (double) def_f; |
1014 | 0 | double old_max = (double) max_f; |
1015 | 0 |
|
1016 | 0 | /* If the axis default MOVED, inv_renorm also kinks where the OLD |
1017 | 0 | * default lands in the new space (the old intermediate coordinate |
1018 | 0 | * crosses 0 there), at |
1019 | 0 | * z = z_old -> -z_old |
1020 | 0 | * Omitting that knot (as a plain two-tent encoding would) makes |
1021 | 0 | * interior coordinates wrong. */ |
1022 | 0 | double z_old = _avar2_normalize_value ((double) old_def, |
1023 | 0 | new_user->minimum, |
1024 | 0 | new_user->middle, |
1025 | 0 | new_user->maximum); |
1026 | 0 | z_old = _avar2_map_new_mapping (new_mapping, z_old); |
1027 | 0 | z_old = (double) roundf ((float) (z_old * 16384.0)) / 16384.0; |
1028 | 0 | if (-1.0 < z_old && z_old < 1.0) |
1029 | 0 | _avar2_add_knot (knots, z_old, -z_old); |
1030 | 0 |
|
1031 | 0 | /* Interior avar v1 breakpoints inside the retained range each put |
1032 | 0 | * a kink in offset(z). Sampling only {-1, 0, +1, z_old} would |
1033 | 0 | * linearly interpolate across those kinks. The instantiated |
1034 | 0 | * mapping keeps exactly the in-range old breakpoints, and the new |
1035 | 0 | * mapping kinks at each one's output coordinate; add that z with |
1036 | 0 | * its old intermediate value so offset(z) is reproduced at every |
1037 | 0 | * kink. */ |
1038 | 0 | hb_vector_t<avar2_offset_knot_t> with_breakpoints (knots); |
1039 | 0 | for (const auto &m : new_mapping) |
1040 | 0 | { |
1041 | 0 | double from = (double) m.coords[0].to_float (); |
1042 | 0 | if (from == -1.0 || from == 0.0 || from == 1.0) |
1043 | 0 | continue; /* anchors already seeded */ |
1044 | 0 | double z = (double) m.coords[1].to_float (); |
1045 | 0 | if (!(-1.0 < z && z < 1.0)) |
1046 | 0 | continue; |
1047 | 0 | double user = _avar2_denormalize_value (from, |
1048 | 0 | new_user->minimum, |
1049 | 0 | new_user->middle, |
1050 | 0 | new_user->maximum); |
1051 | 0 | double n_old = _avar2_normalize_value (user, old_min, old_def, old_max); |
1052 | 0 | double x_old = (double) old_seg_maps[i]->map_float ((float) n_old); |
1053 | 0 | x_old = (double) roundf ((float) (x_old * 16384.0)) / 16384.0; |
1054 | 0 | _avar2_add_knot (with_breakpoints, z, x_old - z); |
1055 | 0 | } |
1056 | 0 |
|
1057 | 0 | if (unlikely (knots.in_error () || with_breakpoints.in_error ())) |
1058 | 0 | return false; |
1059 | 0 |
|
1060 | 0 | /* Extra tents cost F2Dot14 rounding, so for a steep segment they |
1061 | 0 | * can add more quantization noise than the structural error they |
1062 | 0 | * remove. Keep the interior breakpoints only when they do not |
1063 | 0 | * increase the estimated residual; this makes the collection a |
1064 | 0 | * strict (never-worse) improvement over the {-1, 0, +1, z_old} |
1065 | 0 | * anchors. Warn when even the better choice is not bit-exact (a |
1066 | 0 | * steep retained segment that cannot be reproduced in F2Dot14). */ |
1067 | 0 | unsigned err = _avar2_estimate_offset_error (*old_seg_maps[i], new_mapping, |
1068 | 0 | old_min, old_def, old_max, |
1069 | 0 | new_user->minimum, |
1070 | 0 | new_user->middle, |
1071 | 0 | new_user->maximum, |
1072 | 0 | knots); |
1073 | 0 | if (with_breakpoints.length != knots.length) |
1074 | 0 | { |
1075 | 0 | unsigned err_with = _avar2_estimate_offset_error (*old_seg_maps[i], new_mapping, |
1076 | 0 | old_min, old_def, old_max, |
1077 | 0 | new_user->minimum, |
1078 | 0 | new_user->middle, |
1079 | 0 | new_user->maximum, |
1080 | 0 | with_breakpoints); |
1081 | 0 | if (err_with <= err) |
1082 | 0 | { |
1083 | 0 | knots = std::move (with_breakpoints); |
1084 | 0 | err = err_with; |
1085 | 0 | } |
1086 | 0 | } |
1087 | 0 | if (err > 8) |
1088 | 0 | DEBUG_MSG (SUBSET, nullptr, |
1089 | 0 | "avar2 offset compensation is approximate for axis %c%c%c%c: " |
1090 | 0 | "max residual %u F2Dot14 units", |
1091 | 0 | HB_UNTAG (axis_tag), err); |
1092 | 0 |
|
1093 | 0 | /* Synthesize tents. Adjacent tents evaluate to zero at each |
1094 | 0 | * other's peaks, so each knot's delta is offset(z) - offset(0); |
1095 | 0 | * the base value offset(0) = d_i is carried by the empty-region |
1096 | 0 | * bias above. This reduces to the classic pair of tents |
1097 | 0 | * (-1,-1,0) / (0,+1,+1) when the default is unchanged and there |
1098 | 0 | * are no interior knots. */ |
1099 | 0 | for (unsigned k = 0; k < knots.length; k++) |
1100 | 0 | { |
1101 | 0 | double z = knots.arrayZ[k].z; |
1102 | 0 | if (z == 0.0) continue; |
1103 | 0 | int delta = (int) roundf ((float) ((knots.arrayZ[k].offset - (double) d_i) * 16384.0)); |
1104 | 0 | if (!delta) continue; |
1105 | 0 | double lower, upper; |
1106 | 0 | if (z > 0.0) |
1107 | 0 | { |
1108 | 0 | lower = knots.arrayZ[k - 1].z; |
1109 | 0 | upper = k + 1 < knots.length ? knots.arrayZ[k + 1].z : z; |
1110 | 0 | } |
1111 | 0 | else |
1112 | 0 | { |
1113 | 0 | upper = knots.arrayZ[k + 1].z; |
1114 | 0 | lower = k > 0 ? knots.arrayZ[k - 1].z : z; |
1115 | 0 | } |
1116 | 0 | hb_hashmap_t<hb_tag_t, Triple> region; |
1117 | 0 | if (unlikely (!region.set (axis_tag, Triple (lower, z, upper)))) |
1118 | 0 | return false; |
1119 | 0 | item_vars.add_tuple (outer, std::move (region), |
1120 | 0 | inner, delta, item_count); |
1121 | 0 | } |
1122 | 0 | } |
1123 | 0 | } |
1124 | 0 | else |
1125 | 0 | { |
1126 | 0 | /* Free or private axis — not being restricted. |
1127 | 0 | * If it has a non-zero default delta, add it back as a bias. |
1128 | 0 | * Skip if this (outer,inner) was already processed (shared varIdx). */ |
1129 | 0 | uint32_t varidx = new_varidx_mapping[i]; |
1130 | 0 | if (varidx == HB_OT_LAYOUT_NO_VARIATIONS_INDEX) |
1131 | 0 | continue; |
1132 | 0 |
|
1133 | 0 | if (processed_varidxes.has (varidx)) |
1134 | 0 | continue; |
1135 | 0 | processed_varidxes.add (varidx); |
1136 | 0 |
|
1137 | 0 | unsigned outer = varidx >> 16; |
1138 | 0 | unsigned inner = varidx & 0xFFFF; |
1139 | 0 | int dd = (int) roundf (default_deltas[i]); |
1140 | 0 | if (dd != 0) |
1141 | 0 | { |
1142 | 0 | unsigned item_count = item_vars.get_item_count (outer); |
1143 | 0 | hb_hashmap_t<hb_tag_t, Triple> empty_region; |
1144 | 0 | item_vars.add_tuple (outer, std::move (empty_region), |
1145 | 0 | inner, dd, item_count); |
1146 | 0 | } |
1147 | 0 | } |
1148 | 0 | } |
1149 | 0 |
|
1150 | 0 | /* 7. Finalize: build region list + convert to varstore */ |
1151 | 0 | if (!item_vars.build_region_list ()) return false; |
1152 | 0 | if (!item_vars.as_item_varstore (true /* optimize */, |
1153 | 0 | false /* use_no_variation_idx */)) |
1154 | 0 | return false; |
1155 | 0 |
|
1156 | 0 | /* 8. Apply varidx_map optimization remapping */ |
1157 | 0 | const auto &opt_varidx_map = item_vars.get_varidx_map (); |
1158 | 0 | for (unsigned i = 0; i < axisCount; i++) |
1159 | 0 | { |
1160 | 0 | uint32_t varidx = new_varidx_mapping[i]; |
1161 | 0 | if (varidx == HB_OT_LAYOUT_NO_VARIATIONS_INDEX) |
1162 | 0 | continue; |
1163 | 0 | uint32_t *new_idx; |
1164 | 0 | if (opt_varidx_map.has (varidx, &new_idx)) |
1165 | 0 | new_varidx_mapping[i] = *new_idx; |
1166 | 0 | } |
1167 | 0 |
|
1168 | 0 | /* 9. Serialize avarV2Tail. Entries cover the retained axes only; |
1169 | 0 | * self-contained pinned axes are removed from fvar. */ |
1170 | 0 | avar2_index_map_plan_t index_map_plan; |
1171 | 0 | if (!index_map_plan.init (new_varidx_mapping, |
1172 | 0 | c->plan->axes_index_map, |
1173 | 0 | axisCount)) |
1174 | 0 | return false; |
1175 | 0 |
|
1176 | 0 | auto *tail = c->serializer->allocate_size<avarV2Tail> (avarV2Tail::static_size); |
1177 | 0 | if (unlikely (!tail)) return false; |
1178 | 0 | if (!tail->varIdxMap.serialize_serialize (c->serializer, index_map_plan)) |
1179 | 0 | return false; |
1180 | 0 | if (!tail->varStore.serialize_serialize (c->serializer, |
1181 | 0 | item_vars.has_long_word (), |
1182 | 0 | c->plan->axis_tags, |
1183 | 0 | item_vars.get_region_list (), |
1184 | 0 | item_vars.get_vardata_encodings ())) |
1185 | 0 | return false; |
1186 | 0 |
|
1187 | 0 | return true; |
1188 | 0 | #endif |
1189 | 0 | } |
1190 | | |
1191 | | public: |
1192 | | |
1193 | | protected: |
1194 | | FixedVersion<>version; /* Version of the avar table |
1195 | | * initially set to 0x00010000u */ |
1196 | | HBUINT16 reserved; /* This field is permanently reserved. Set to 0. */ |
1197 | | HBUINT16 axisCount; /* The number of variation axes in the font. This |
1198 | | * must be the same number as axisCount in the |
1199 | | * 'fvar' table. */ |
1200 | | SegmentMaps firstAxisSegmentMaps; |
1201 | | |
1202 | | public: |
1203 | | DEFINE_SIZE_MIN (8); |
1204 | | }; |
1205 | | |
1206 | | } /* namespace OT */ |
1207 | | |
1208 | | |
1209 | | #endif /* HB_OT_VAR_AVAR_TABLE_HH */ |