/src/harfbuzz/src/hb-ot-var-gvar-table.hh
Line | Count | Source |
1 | | /* |
2 | | * Copyright © 2019 Adobe Inc. |
3 | | * Copyright © 2019 Ebrahim Byagowi |
4 | | * |
5 | | * This is part of HarfBuzz, a text shaping library. |
6 | | * |
7 | | * Permission is hereby granted, without written agreement and without |
8 | | * license or royalty fees, to use, copy, modify, and distribute this |
9 | | * software and its documentation for any purpose, provided that the |
10 | | * above copyright notice and the following two paragraphs appear in |
11 | | * all copies of this software. |
12 | | * |
13 | | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
14 | | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
15 | | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
16 | | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
17 | | * DAMAGE. |
18 | | * |
19 | | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
20 | | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
21 | | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
22 | | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
23 | | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
24 | | * |
25 | | * Adobe Author(s): Michiharu Ariza |
26 | | */ |
27 | | |
28 | | #ifndef HB_OT_VAR_GVAR_TABLE_HH |
29 | | #define HB_OT_VAR_GVAR_TABLE_HH |
30 | | |
31 | | #include "hb-decycler.hh" |
32 | | #include "hb-open-type.hh" |
33 | | #include "hb-ot-dual.hh" |
34 | | #include "hb-ot-var-common.hh" |
35 | | |
36 | | /* |
37 | | * gvar -- Glyph Variation Table |
38 | | * https://docs.microsoft.com/en-us/typography/opentype/spec/gvar |
39 | | */ |
40 | | #define HB_OT_TAG_gvar HB_TAG('g','v','a','r') |
41 | | #define HB_OT_TAG_GVAR HB_TAG('G','V','A','R') |
42 | | |
43 | | struct hb_glyf_scratch_t |
44 | | { |
45 | | // glyf |
46 | | contour_point_vector_t all_points; |
47 | | contour_point_vector_t comp_points; |
48 | | hb_decycler_t decycler; |
49 | | |
50 | | // gvar |
51 | | contour_point_vector_t orig_points; |
52 | | hb_vector_t<int> x_deltas; |
53 | | hb_vector_t<int> y_deltas; |
54 | | contour_point_vector_t deltas; |
55 | | hb_vector_t<unsigned int> shared_indices; |
56 | | hb_vector_t<unsigned int> private_indices; |
57 | | }; |
58 | | |
59 | | namespace OT { |
60 | | |
61 | | template <typename OffsetType> |
62 | | struct glyph_variations_t |
63 | | { |
64 | | // TODO: Move tuple_variations_t to outside of TupleVariationData |
65 | | using tuple_variations_t = typename TupleVariationData<OffsetType>::tuple_variations_t; |
66 | | using GlyphVariationData = TupleVariationData<OffsetType>; |
67 | | |
68 | | hb_vector_t<tuple_variations_t> glyph_variations; |
69 | | |
70 | | hb_vector_t<F2DOT14> compiled_shared_tuples; |
71 | | private: |
72 | | unsigned shared_tuples_count = 0; |
73 | | |
74 | | /* shared coords-> index map after instantiation */ |
75 | | hb_hashmap_t<const hb_vector_t<F2DOT14>*, unsigned> shared_tuples_idx_map; |
76 | | |
77 | | hb_alloc_pool_t pool; |
78 | | |
79 | | /* Remaining tuple work and storage across this table. */ |
80 | | int64_t budget = 0; |
81 | | |
82 | | public: |
83 | | unsigned compiled_shared_tuples_count () const |
84 | | { return shared_tuples_count; } |
85 | | |
86 | | unsigned compiled_byte_size () const |
87 | | { |
88 | | unsigned byte_size = 0; |
89 | | for (const auto& _ : glyph_variations) |
90 | | byte_size += _.get_compiled_byte_size (); |
91 | | |
92 | | return byte_size; |
93 | | } |
94 | | |
95 | | bool create_from_glyphs_var_data (unsigned axis_count, |
96 | | const hb_array_t<const F2DOT14> shared_tuples, |
97 | | const hb_subset_plan_t *plan, |
98 | | const hb_hashmap_t<hb_codepoint_t, hb_bytes_t>& new_gid_var_data_map, |
99 | | unsigned table_length) |
100 | | { |
101 | | if (unlikely (!glyph_variations.alloc_exact (plan->new_to_old_gid_list.length))) |
102 | | return false; |
103 | | |
104 | | budget = hb_max ((int64_t) table_length * HB_BUDGET_SUBSET_TUPLE_POINTS_FACTOR, |
105 | | HB_BUDGET_SUBSET_TUPLE_POINTS_MIN); |
106 | | |
107 | | auto it = hb_iter (plan->new_to_old_gid_list); |
108 | | for (auto &_ : it) |
109 | | { |
110 | | hb_codepoint_t new_gid = _.first; |
111 | | contour_point_vector_t *all_contour_points; |
112 | | if (!new_gid_var_data_map.has (new_gid) || |
113 | | !plan->new_gid_contour_points_map.has (new_gid, &all_contour_points)) |
114 | | return false; |
115 | | hb_bytes_t var_data = new_gid_var_data_map.get (new_gid); |
116 | | |
117 | | const GlyphVariationData* p = reinterpret_cast<const GlyphVariationData*> (var_data.arrayZ); |
118 | | typename GlyphVariationData::tuple_iterator_t iterator; |
119 | | tuple_variations_t tuple_vars; |
120 | | |
121 | | hb_vector_t<unsigned> shared_indices; |
122 | | |
123 | | /* in case variation data is empty, push an empty struct into the vector, |
124 | | * keep the vector in sync with the new_to_old_gid_list */ |
125 | | if (!var_data || ! p->has_data () || !all_contour_points->length || |
126 | | !GlyphVariationData::get_tuple_iterator (var_data, axis_count, |
127 | | var_data.arrayZ, |
128 | | shared_indices, &iterator)) |
129 | | { |
130 | | glyph_variations.push (std::move (tuple_vars)); |
131 | | continue; |
132 | | } |
133 | | |
134 | | bool is_composite_glyph = false; |
135 | | is_composite_glyph = plan->composite_new_gids.has (new_gid); |
136 | | |
137 | | if (!p->decompile_tuple_variations (all_contour_points->length, true /* is_gvar */, |
138 | | iterator, &(plan->axes_old_index_tag_map), |
139 | | shared_indices, shared_tuples, |
140 | | tuple_vars, /* OUT */ |
141 | | &pool, |
142 | | is_composite_glyph, |
143 | | &budget)) |
144 | | return false; |
145 | | glyph_variations.push (std::move (tuple_vars)); |
146 | | } |
147 | | return !glyph_variations.in_error () && glyph_variations.length == plan->new_to_old_gid_list.length; |
148 | | } |
149 | | |
150 | | bool instantiate (const hb_subset_plan_t *plan) |
151 | | { |
152 | | unsigned count = plan->new_to_old_gid_list.length; |
153 | | bool iup_optimize = false; |
154 | | optimize_scratch_t scratch; |
155 | | iup_optimize = plan->flags & HB_SUBSET_FLAGS_OPTIMIZE_IUP_DELTAS; |
156 | | for (unsigned i = 0; i < count; i++) |
157 | | { |
158 | | hb_codepoint_t new_gid = plan->new_to_old_gid_list[i].first; |
159 | | contour_point_vector_t *all_points; |
160 | | if (!plan->new_gid_contour_points_map.has (new_gid, &all_points)) |
161 | | return false; |
162 | | /* IUP adds rebased constant deltas to the contour coordinates. Keep |
163 | | * the plan's original points intact across serialization retries. */ |
164 | | contour_point_vector_t points; |
165 | | if (iup_optimize) |
166 | | { |
167 | | points = *all_points; |
168 | | if (unlikely (points.in_error ())) return false; |
169 | | all_points = &points; |
170 | | } |
171 | | /* avar2 partial instancing: cull unreachable tuples. */ |
172 | | if (plan->has_avar2 && plan->avar2_reachable_ranges.get_population ()) |
173 | | glyph_variations[i].cull_unreachable (plan->avar2_reachable_ranges); |
174 | | if (!glyph_variations[i].instantiate (plan->axes_location, plan->axes_triple_distances, scratch, &pool, all_points, iup_optimize, &budget)) |
175 | | return false; |
176 | | } |
177 | | return true; |
178 | | } |
179 | | |
180 | | bool compile_bytes (const hb_map_t& axes_index_map, |
181 | | const hb_map_t& axes_old_index_tag_map) |
182 | | { |
183 | | if (!compile_shared_tuples (axes_index_map, axes_old_index_tag_map)) |
184 | | return false; |
185 | | for (tuple_variations_t& vars: glyph_variations) |
186 | | if (!vars.compile_bytes (axes_index_map, axes_old_index_tag_map, |
187 | | true, /* use shared points*/ |
188 | | true, |
189 | | &shared_tuples_idx_map, |
190 | | &pool)) |
191 | | return false; |
192 | | |
193 | | return true; |
194 | | } |
195 | | |
196 | | bool compile_shared_tuples (const hb_map_t& axes_index_map, |
197 | | const hb_map_t& axes_old_index_tag_map) |
198 | | { |
199 | | /* key is pointer to compiled_peak_coords inside each tuple, hashing |
200 | | * function will always deref pointers first */ |
201 | | hb_hashmap_t<const hb_vector_t<F2DOT14>*, unsigned> coords_count_map; |
202 | | |
203 | | /* count the num of shared coords */ |
204 | | for (tuple_variations_t& vars: glyph_variations) |
205 | | { |
206 | | for (tuple_delta_t& var : vars.tuple_vars) |
207 | | { |
208 | | /* Walk the original axes, then retain up to three F2DOT14 |
209 | | * coordinates per surviving axis in both arrays and headers. */ |
210 | | if (unlikely (!hb_budget_spend (budget, HB_BUDGET_1, axes_old_index_tag_map.get_population ()) || |
211 | | !hb_budget_spend (budget, 6 * F2DOT14::static_size, axes_index_map.get_population ()))) |
212 | | return false; |
213 | | if (!var.compile_coords (axes_index_map, axes_old_index_tag_map, &pool)) |
214 | | return false; |
215 | | unsigned *count; |
216 | | unsigned hash = hb_hash (&var.compiled_peak_coords); |
217 | | if (coords_count_map.has_with_hash (&(var.compiled_peak_coords), hash, &count)) |
218 | | (*count)++; |
219 | | else |
220 | | coords_count_map.set_with_hash (&(var.compiled_peak_coords), hash, 1); |
221 | | } |
222 | | } |
223 | | |
224 | | if (!coords_count_map || coords_count_map.in_error ()) |
225 | | return false; |
226 | | |
227 | | /* add only those coords that are used more than once into the vector and sort */ |
228 | | hb_vector_t<hb_pair_t<const hb_vector_t<F2DOT14>*, unsigned>> shared_coords { |
229 | | + hb_iter (coords_count_map) |
230 | | | hb_filter ([] (const hb_pair_t<const hb_vector_t<F2DOT14>*, unsigned>& p) { return p.second > 1; }) |
231 | | }; |
232 | | if (unlikely (shared_coords.in_error ())) return false; |
233 | | |
234 | | /* no shared tuples: no coords are used more than once */ |
235 | | if (!shared_coords) return true; |
236 | | /* sorting based on the coords frequency first (high to low), then compare |
237 | | * the coords bytes */ |
238 | | shared_coords.qsort (_cmp_coords); |
239 | | |
240 | | /* build shared_coords->idx map and shared tuples byte array */ |
241 | | |
242 | | shared_tuples_count = hb_min (0xFFFu + 1, shared_coords.length); |
243 | | unsigned len = shared_tuples_count * (shared_coords[0].first->length); |
244 | | if (unlikely (!compiled_shared_tuples.alloc (len))) |
245 | | return false; |
246 | | |
247 | | for (unsigned i = 0; i < shared_tuples_count; i++) |
248 | | { |
249 | | shared_tuples_idx_map.set (shared_coords[i].first, i); |
250 | | /* add a concat() in hb_vector_t? */ |
251 | | for (auto c : shared_coords[i].first->iter ()) |
252 | | compiled_shared_tuples.push (c); |
253 | | } |
254 | | |
255 | | return true; |
256 | | } |
257 | | |
258 | | static int _cmp_coords (const void *pa, const void *pb) |
259 | | { |
260 | | const hb_pair_t<hb_vector_t<F2DOT14> *, unsigned> *a = (const hb_pair_t<hb_vector_t<F2DOT14> *, unsigned> *) pa; |
261 | | const hb_pair_t<hb_vector_t<F2DOT14> *, unsigned> *b = (const hb_pair_t<hb_vector_t<F2DOT14> *, unsigned> *) pb; |
262 | | |
263 | | if (a->second != b->second) |
264 | | return b->second - a->second; // high to low |
265 | | |
266 | | return b->first->as_array().cmp (a->first->as_array ()); |
267 | | } |
268 | | |
269 | | template<typename Iterator, |
270 | | hb_requires (hb_is_iterator (Iterator))> |
271 | | bool serialize_glyph_var_data (hb_serialize_context_t *c, |
272 | | Iterator it, |
273 | | bool long_offset, |
274 | | unsigned num_glyphs, |
275 | | char* glyph_var_data_offsets /* OUT: glyph var data offsets array */) const |
276 | | { |
277 | | TRACE_SERIALIZE (this); |
278 | | |
279 | | if (long_offset) |
280 | | { |
281 | | ((HBUINT32 *) glyph_var_data_offsets)[0] = 0; |
282 | | glyph_var_data_offsets += 4; |
283 | | } |
284 | | else |
285 | | { |
286 | | ((HBUINT16 *) glyph_var_data_offsets)[0] = 0; |
287 | | glyph_var_data_offsets += 2; |
288 | | } |
289 | | unsigned glyph_offset = 0; |
290 | | hb_codepoint_t last_gid = 0; |
291 | | unsigned idx = 0; |
292 | | |
293 | | GlyphVariationData* cur_glyph = c->start_embed<GlyphVariationData> (); |
294 | | if (!cur_glyph) return_trace (false); |
295 | | for (auto &_ : it) |
296 | | { |
297 | | hb_codepoint_t gid = _.first; |
298 | | if (long_offset) |
299 | | for (; last_gid < gid; last_gid++) |
300 | | ((HBUINT32 *) glyph_var_data_offsets)[last_gid] = glyph_offset; |
301 | | else |
302 | | for (; last_gid < gid; last_gid++) |
303 | | ((HBUINT16 *) glyph_var_data_offsets)[last_gid] = glyph_offset / 2; |
304 | | |
305 | | if (idx >= glyph_variations.length) return_trace (false); |
306 | | if (!cur_glyph->serialize (c, true, glyph_variations[idx])) return_trace (false); |
307 | | GlyphVariationData* next_glyph = c->start_embed<GlyphVariationData> (); |
308 | | glyph_offset += (char *) next_glyph - (char *) cur_glyph; |
309 | | |
310 | | if (long_offset) |
311 | | ((HBUINT32 *) glyph_var_data_offsets)[gid] = glyph_offset; |
312 | | else |
313 | | ((HBUINT16 *) glyph_var_data_offsets)[gid] = glyph_offset / 2; |
314 | | |
315 | | last_gid++; |
316 | | idx++; |
317 | | cur_glyph = next_glyph; |
318 | | } |
319 | | |
320 | | if (long_offset) |
321 | | for (; last_gid < num_glyphs; last_gid++) |
322 | | ((HBUINT32 *) glyph_var_data_offsets)[last_gid] = glyph_offset; |
323 | | else |
324 | | for (; last_gid < num_glyphs; last_gid++) |
325 | | ((HBUINT16 *) glyph_var_data_offsets)[last_gid] = glyph_offset / 2; |
326 | | return_trace (true); |
327 | | } |
328 | | }; |
329 | | |
330 | | template <typename T, typename Types> |
331 | | struct gvar_GVAR |
332 | | { |
333 | | using GlyphCountType = typename Types::HBUINT; |
334 | | |
335 | | using GlyphVariationData = TupleVariationData<GlyphCountType>; |
336 | | static constexpr unsigned glyph_count_max () |
337 | | { return (1u << (8 * GlyphCountType::static_size)) - 1; } |
338 | | |
339 | 0 | bool has_data () const { return version.to_int () != 0; }Unexecuted instantiation: OT::gvar_GVAR<OT::gvar, OT::Layout::SmallTypes>::has_data() const Unexecuted instantiation: OT::gvar_GVAR<OT::GVAR, OT::Layout::MediumTypes>::has_data() const |
340 | | |
341 | | bool sanitize_shallow (hb_sanitize_context_t *c) const |
342 | 0 | { |
343 | 0 | TRACE_SANITIZE (this); |
344 | 0 | return_trace (c->check_struct (this) && |
345 | 0 | hb_barrier () && |
346 | 0 | (version.major == 1) && |
347 | 0 | sharedTuples.sanitize (c, this, axisCount * sharedTupleCount) && |
348 | 0 | (is_long_offset () ? |
349 | 0 | c->check_array (get_long_offset_array (), c->get_num_glyphs () + 1) : |
350 | 0 | c->check_array (get_short_offset_array (), c->get_num_glyphs () + 1))); |
351 | 0 | } Unexecuted instantiation: OT::gvar_GVAR<OT::GVAR, OT::Layout::MediumTypes>::sanitize_shallow(hb_sanitize_context_t*) const Unexecuted instantiation: OT::gvar_GVAR<OT::gvar, OT::Layout::SmallTypes>::sanitize_shallow(hb_sanitize_context_t*) const |
352 | | |
353 | | /* GlyphVariationData not sanitized here; must be checked while accessing each glyph variation data */ |
354 | | bool sanitize (hb_sanitize_context_t *c) const |
355 | 0 | { return sanitize_shallow (c); }Unexecuted instantiation: OT::gvar_GVAR<OT::GVAR, OT::Layout::MediumTypes>::sanitize(hb_sanitize_context_t*) const Unexecuted instantiation: OT::gvar_GVAR<OT::gvar, OT::Layout::SmallTypes>::sanitize(hb_sanitize_context_t*) const |
356 | | |
357 | | bool decompile_glyph_variations (hb_subset_context_t *c, |
358 | | glyph_variations_t<GlyphCountType>& glyph_vars /* OUT */) const |
359 | | { |
360 | | hb_hashmap_t<hb_codepoint_t, hb_bytes_t> new_gid_var_data_map; |
361 | | auto it = hb_iter (c->plan->new_to_old_gid_list); |
362 | | if (it->first == 0 && !(c->plan->flags & HB_SUBSET_FLAGS_NOTDEF_OUTLINE)) |
363 | | { |
364 | | new_gid_var_data_map.set (0, hb_bytes_t ()); |
365 | | it++; |
366 | | } |
367 | | |
368 | | for (auto &_ : it) |
369 | | { |
370 | | hb_codepoint_t new_gid = _.first; |
371 | | hb_codepoint_t old_gid = _.second; |
372 | | hb_bytes_t var_data_bytes = get_glyph_var_data_bytes (c->source_blob, glyphCountX, old_gid); |
373 | | new_gid_var_data_map.set (new_gid, var_data_bytes); |
374 | | } |
375 | | |
376 | | if (new_gid_var_data_map.in_error ()) return false; |
377 | | |
378 | | hb_array_t<const F2DOT14> shared_tuples = (this+sharedTuples).as_array ((unsigned) sharedTupleCount * (unsigned) axisCount); |
379 | | return glyph_vars.create_from_glyphs_var_data (axisCount, shared_tuples, c->plan, new_gid_var_data_map, |
380 | | c->source_blob->length); |
381 | | } |
382 | | |
383 | | template<typename Iterator, |
384 | | hb_requires (hb_is_iterator (Iterator))> |
385 | | bool serialize (hb_serialize_context_t *c, |
386 | | const glyph_variations_t<GlyphCountType>& glyph_vars, |
387 | | Iterator it, |
388 | | unsigned axis_count, |
389 | | unsigned num_glyphs, |
390 | | bool force_long_offsets) const |
391 | | { |
392 | | TRACE_SERIALIZE (this); |
393 | | gvar_GVAR *out = c->allocate_min<gvar_GVAR> (); |
394 | | if (unlikely (!out)) return_trace (false); |
395 | | |
396 | | out->version.major = 1; |
397 | | out->version.minor = 0; |
398 | | out->axisCount = axis_count; |
399 | | out->glyphCountX = hb_min (glyph_count_max (), num_glyphs); |
400 | | |
401 | | unsigned glyph_var_data_size = glyph_vars.compiled_byte_size (); |
402 | | /* According to the spec: If the short format (Offset16) is used for offsets, |
403 | | * the value stored is the offset divided by 2, so the maximum data size should |
404 | | * be 2 * 0xFFFFu, which is 0x1FFFEu */ |
405 | | bool long_offset = glyph_var_data_size > 0x1FFFEu || force_long_offsets; |
406 | | out->flags = long_offset ? 1 : 0; |
407 | | |
408 | | unsigned glyph_var_data_offsets_size = (long_offset ? 4 : 2) * (num_glyphs + 1); |
409 | | HBUINT8 *glyph_var_data_offsets = c->allocate_size<HBUINT8> (glyph_var_data_offsets_size, false); |
410 | | if (!glyph_var_data_offsets) return_trace (false); |
411 | | |
412 | | /* shared tuples */ |
413 | | unsigned shared_tuple_count = glyph_vars.compiled_shared_tuples_count (); |
414 | | out->sharedTupleCount = shared_tuple_count; |
415 | | |
416 | | if (!shared_tuple_count) |
417 | | out->sharedTuples = (char *) glyph_var_data_offsets + glyph_var_data_offsets_size - (char *) out; |
418 | | else |
419 | | { |
420 | | hb_array_t<const F2DOT14> shared_tuples = glyph_vars.compiled_shared_tuples.as_array ().copy (c); |
421 | | if (!shared_tuples.arrayZ) return_trace (false); |
422 | | out->sharedTuples = (const char *) shared_tuples.arrayZ - (char *) out; |
423 | | } |
424 | | |
425 | | char *glyph_var_data = c->start_embed<char> (); |
426 | | if (!glyph_var_data) return_trace (false); |
427 | | out->dataZ = glyph_var_data - (char *) out; |
428 | | |
429 | | return_trace (glyph_vars.serialize_glyph_var_data (c, it, long_offset, num_glyphs, |
430 | | (char *) glyph_var_data_offsets)); |
431 | | } |
432 | | |
433 | | bool instantiate (hb_subset_context_t *c) const |
434 | | { |
435 | | TRACE_SUBSET (this); |
436 | | glyph_variations_t<GlyphCountType> glyph_vars; |
437 | | if (!decompile_glyph_variations (c, glyph_vars)) |
438 | | return_trace (false); |
439 | | |
440 | | if (!glyph_vars.instantiate (c->plan)) return_trace (false); |
441 | | if (!glyph_vars.compile_bytes (c->plan->axes_index_map, c->plan->axes_old_index_tag_map)) |
442 | | return_trace (false); |
443 | | |
444 | | unsigned axis_count = c->plan->axes_index_map.get_population (); |
445 | | unsigned num_glyphs = c->plan->num_output_glyphs (); |
446 | | auto it = hb_iter (c->plan->new_to_old_gid_list); |
447 | | |
448 | | bool force_long_offsets = false; |
449 | | #ifdef HB_EXPERIMENTAL_API |
450 | | force_long_offsets = c->plan->flags & HB_SUBSET_FLAGS_IFTB_REQUIREMENTS; |
451 | | #endif |
452 | | return_trace (serialize (c->serializer, glyph_vars, it, axis_count, num_glyphs, force_long_offsets)); |
453 | | } |
454 | | |
455 | | bool subset (hb_subset_context_t *c) const |
456 | | { |
457 | | TRACE_SUBSET (this); |
458 | | if (c->plan->all_axes_pinned) |
459 | | return_trace (false); |
460 | | |
461 | | if (c->plan->normalized_coords) |
462 | | return_trace (instantiate (c)); |
463 | | |
464 | | unsigned glyph_count = version.to_int () ? c->plan->source->get_num_glyphs () : 0; |
465 | | |
466 | | gvar_GVAR *out = c->serializer->allocate_min<gvar_GVAR> (); |
467 | | if (unlikely (!out)) return_trace (false); |
468 | | |
469 | | out->version.major = 1; |
470 | | out->version.minor = 0; |
471 | | out->axisCount = axisCount; |
472 | | out->sharedTupleCount = sharedTupleCount; |
473 | | |
474 | | unsigned int num_glyphs = c->plan->num_output_glyphs (); |
475 | | out->glyphCountX = hb_min (glyph_count_max (), num_glyphs); |
476 | | |
477 | | /* avar2 partial instancing: cull tuple variations whose region falls |
478 | | * outside the reachable old-space final-coord ranges. Rewritten glyph |
479 | | * data is cached per new gid; untouched glyphs are copied verbatim. */ |
480 | | hb_vector_t<hb_vector_t<char>> culled_vars; |
481 | | hb_set_t culled_gids; |
482 | | if (c->plan->has_avar2 && c->plan->avar2_reachable_ranges.get_population ()) |
483 | | { |
484 | | hb_array_t<const F2DOT14> shared_tuples_array = |
485 | | (this+sharedTuples).as_array ((unsigned) sharedTupleCount * (unsigned) axisCount); |
486 | | if (unlikely (!culled_vars.resize (num_glyphs))) return_trace (false); |
487 | | /* The glyph data regions of a well-formed table are disjoint, so the |
488 | | * retained glyphs' data add up to no more than the table. Offsets are |
489 | | * used as they are, though, and malformed ones can point every glyph |
490 | | * at one region; charge each glyph's data to the table size so this |
491 | | * pass, and the rewritten copies it keeps, cannot cost glyph count |
492 | | * times table size. */ |
493 | | int64_t budget = c->source_blob->length; |
494 | | auto cull_it = hb_iter (c->plan->new_to_old_gid_list); |
495 | | if (cull_it->first == 0 && !(c->plan->flags & HB_SUBSET_FLAGS_NOTDEF_OUTLINE)) |
496 | | cull_it++; |
497 | | for (auto &_ : cull_it) |
498 | | { |
499 | | hb_bytes_t var_data_bytes = get_glyph_var_data_bytes (c->source_blob, glyph_count, _.second); |
500 | | if (var_data_bytes.length < GlyphVariationData::min_size) continue; |
501 | | if (unlikely (!hb_budget_spend (budget, HB_BUDGET_1, var_data_bytes.length))) |
502 | | { |
503 | | /* Fail the table rather than let the serializer retry it. */ |
504 | | c->serializer->err (HB_SERIALIZE_ERROR_OTHER); |
505 | | return_trace (false); |
506 | | } |
507 | | bool changed = false; |
508 | | if (unlikely (!var_data_bytes.as<GlyphVariationData> () |
509 | | ->cull_tuple_variations (var_data_bytes, axisCount, |
510 | | shared_tuples_array, |
511 | | &c->plan->axes_old_index_tag_map, |
512 | | c->plan->avar2_reachable_ranges, |
513 | | culled_vars[_.first], &changed))) |
514 | | return_trace (false); |
515 | | if (changed) culled_gids.add (_.first); |
516 | | } |
517 | | if (unlikely (culled_gids.in_error ())) return_trace (false); |
518 | | } |
519 | | |
520 | | auto it = hb_iter (c->plan->new_to_old_gid_list); |
521 | | if (it->first == 0 && !(c->plan->flags & HB_SUBSET_FLAGS_NOTDEF_OUTLINE)) |
522 | | it++; |
523 | | unsigned subset_data_size = 0; |
524 | | unsigned padding_size = 0; |
525 | | for (auto &_ : it) |
526 | | { |
527 | | hb_codepoint_t old_gid = _.second; |
528 | | unsigned glyph_data_size = culled_gids.has (_.first) |
529 | | ? culled_vars[_.first].length |
530 | | : get_glyph_var_data_bytes (c->source_blob, glyph_count, old_gid).length; |
531 | | if (glyph_data_size % 2) |
532 | | { |
533 | | glyph_data_size++; |
534 | | padding_size++; |
535 | | } |
536 | | |
537 | | if (unlikely (hb_unsigned_add_overflows (subset_data_size, glyph_data_size, &subset_data_size))) |
538 | | return_trace (false); |
539 | | } |
540 | | |
541 | | /* According to the spec: If the short format (Offset16) is used for offsets, |
542 | | * the value stored is the offset divided by 2, so the maximum data size should |
543 | | * be 2 * 0xFFFFu, which is 0x1FFFEu */ |
544 | | bool long_offset = subset_data_size > 0x1FFFEu; |
545 | | #ifdef HB_EXPERIMENTAL_API |
546 | | long_offset = long_offset || (c->plan->flags & HB_SUBSET_FLAGS_IFTB_REQUIREMENTS); |
547 | | #endif |
548 | | out->flags = long_offset ? 1 : 0; |
549 | | |
550 | | unsigned subset_offsets_size = (long_offset ? 4 : 2) * (num_glyphs + 1); |
551 | | HBUINT8 *subset_offsets = c->serializer->allocate_size<HBUINT8> (subset_offsets_size, false); |
552 | | if (!subset_offsets) return_trace (false); |
553 | | |
554 | | /* shared tuples */ |
555 | | if (!sharedTupleCount || !sharedTuples) |
556 | | out->sharedTuples = (char *) subset_offsets + subset_offsets_size - (char *) out; |
557 | | else |
558 | | { |
559 | | unsigned int shared_tuple_size = F2DOT14::static_size * axisCount * sharedTupleCount; |
560 | | F2DOT14 *tuples = c->serializer->allocate_size<F2DOT14> (shared_tuple_size); |
561 | | if (!tuples) return_trace (false); |
562 | | out->sharedTuples = (char *) tuples - (char *) out; |
563 | | hb_memcpy (tuples, this+sharedTuples, shared_tuple_size); |
564 | | } |
565 | | |
566 | | /* This ordering relative to the shared tuples array, which puts the glyphVariationData |
567 | | last in the table, is required when HB_SUBSET_FLAGS_IFTB_REQUIREMENTS is set */ |
568 | | if (long_offset) |
569 | | subset_data_size -= padding_size; |
570 | | char *subset_data = c->serializer->allocate_size<char> (subset_data_size, false); |
571 | | if (!subset_data) return_trace (false); |
572 | | out->dataZ = subset_data - (char *) out; |
573 | | |
574 | | |
575 | | if (long_offset) |
576 | | { |
577 | | ((HBUINT32 *) subset_offsets)[0] = 0; |
578 | | subset_offsets += 4; |
579 | | } |
580 | | else |
581 | | { |
582 | | ((HBUINT16 *) subset_offsets)[0] = 0; |
583 | | subset_offsets += 2; |
584 | | } |
585 | | unsigned int glyph_offset = 0; |
586 | | |
587 | | hb_codepoint_t last = 0; |
588 | | it = hb_iter (c->plan->new_to_old_gid_list); |
589 | | if (it->first == 0 && !(c->plan->flags & HB_SUBSET_FLAGS_NOTDEF_OUTLINE)) |
590 | | it++; |
591 | | for (auto &_ : it) |
592 | | { |
593 | | hb_codepoint_t gid = _.first; |
594 | | hb_codepoint_t old_gid = _.second; |
595 | | |
596 | | if (long_offset) |
597 | | for (; last < gid; last++) |
598 | | ((HBUINT32 *) subset_offsets)[last] = glyph_offset; |
599 | | else |
600 | | for (; last < gid; last++) |
601 | | ((HBUINT16 *) subset_offsets)[last] = glyph_offset / 2; |
602 | | |
603 | | hb_bytes_t var_data_bytes = culled_gids.has (gid) |
604 | | ? hb_bytes_t (culled_vars[gid].arrayZ, |
605 | | culled_vars[gid].length) |
606 | | : get_glyph_var_data_bytes (c->source_blob, |
607 | | glyph_count, |
608 | | old_gid); |
609 | | |
610 | | hb_memcpy (subset_data, var_data_bytes.arrayZ, var_data_bytes.length); |
611 | | unsigned glyph_data_size = var_data_bytes.length; |
612 | | subset_data += glyph_data_size; |
613 | | glyph_offset += glyph_data_size; |
614 | | |
615 | | if (!long_offset && (glyph_data_size % 2)) |
616 | | { |
617 | | *subset_data = 0; |
618 | | subset_data++; |
619 | | glyph_offset++; |
620 | | } |
621 | | |
622 | | if (long_offset) |
623 | | ((HBUINT32 *) subset_offsets)[gid] = glyph_offset; |
624 | | else |
625 | | ((HBUINT16 *) subset_offsets)[gid] = glyph_offset / 2; |
626 | | |
627 | | last++; // Skip over gid |
628 | | } |
629 | | |
630 | | if (long_offset) |
631 | | for (; last < num_glyphs; last++) |
632 | | ((HBUINT32 *) subset_offsets)[last] = glyph_offset; |
633 | | else |
634 | | for (; last < num_glyphs; last++) |
635 | | ((HBUINT16 *) subset_offsets)[last] = glyph_offset / 2; |
636 | | |
637 | | return_trace (true); |
638 | | } |
639 | | |
640 | | protected: |
641 | | const hb_bytes_t get_glyph_var_data_bytes (hb_blob_t *blob, |
642 | | unsigned glyph_count, |
643 | | hb_codepoint_t glyph) const |
644 | 0 | { |
645 | 0 | unsigned start_offset = get_offset (glyph_count, glyph); |
646 | 0 | unsigned end_offset = get_offset (glyph_count, glyph+1); |
647 | 0 | if (unlikely (end_offset < start_offset)) return hb_bytes_t (); |
648 | 0 | unsigned length = end_offset - start_offset; |
649 | 0 | hb_bytes_t var_data = blob->as_bytes ().sub_array ((unsigned) dataZ).sub_array (start_offset, length); |
650 | 0 | return likely (var_data.length >= GlyphVariationData::min_size) ? var_data : hb_bytes_t (); |
651 | 0 | } Unexecuted instantiation: OT::gvar_GVAR<OT::GVAR, OT::Layout::MediumTypes>::get_glyph_var_data_bytes(hb_blob_t*, unsigned int, unsigned int) const Unexecuted instantiation: OT::gvar_GVAR<OT::gvar, OT::Layout::SmallTypes>::get_glyph_var_data_bytes(hb_blob_t*, unsigned int, unsigned int) const |
652 | | |
653 | 0 | bool is_long_offset () const { return flags & 1; }Unexecuted instantiation: OT::gvar_GVAR<OT::GVAR, OT::Layout::MediumTypes>::is_long_offset() const Unexecuted instantiation: OT::gvar_GVAR<OT::gvar, OT::Layout::SmallTypes>::is_long_offset() const |
654 | | |
655 | | unsigned get_offset (unsigned glyph_count, unsigned i) const |
656 | 0 | { |
657 | 0 | if (unlikely (i > glyph_count)) return 0; |
658 | 0 | hb_barrier (); |
659 | 0 | return is_long_offset () ? get_long_offset_array ()[i] : get_short_offset_array ()[i] * 2; |
660 | 0 | } Unexecuted instantiation: OT::gvar_GVAR<OT::GVAR, OT::Layout::MediumTypes>::get_offset(unsigned int, unsigned int) const Unexecuted instantiation: OT::gvar_GVAR<OT::gvar, OT::Layout::SmallTypes>::get_offset(unsigned int, unsigned int) const |
661 | | |
662 | 0 | const HBUINT32 * get_long_offset_array () const { return (const HBUINT32 *) &offsetZ; }Unexecuted instantiation: OT::gvar_GVAR<OT::GVAR, OT::Layout::MediumTypes>::get_long_offset_array() const Unexecuted instantiation: OT::gvar_GVAR<OT::gvar, OT::Layout::SmallTypes>::get_long_offset_array() const |
663 | 0 | const HBUINT16 *get_short_offset_array () const { return (const HBUINT16 *) &offsetZ; }Unexecuted instantiation: OT::gvar_GVAR<OT::GVAR, OT::Layout::MediumTypes>::get_short_offset_array() const Unexecuted instantiation: OT::gvar_GVAR<OT::gvar, OT::Layout::SmallTypes>::get_short_offset_array() const |
664 | | |
665 | | public: |
666 | | struct accelerator_t |
667 | | { |
668 | | |
669 | | hb_scalar_cache_t *create_cache () const |
670 | 0 | { |
671 | 0 | return hb_scalar_cache_t::create (hb_min ((unsigned) table->sharedTupleCount, |
672 | 0 | +TupleVariationHeader::max_shared_tuple_count)); |
673 | 0 | } Unexecuted instantiation: OT::gvar_GVAR<OT::GVAR, OT::Layout::MediumTypes>::accelerator_t::create_cache() const Unexecuted instantiation: OT::gvar_GVAR<OT::gvar, OT::Layout::SmallTypes>::accelerator_t::create_cache() const |
674 | | |
675 | | static void destroy_cache (hb_scalar_cache_t *cache) |
676 | | { |
677 | | hb_scalar_cache_t::destroy (cache); |
678 | | } |
679 | | |
680 | 0 | bool has_data () const { return table->has_data (); }Unexecuted instantiation: OT::gvar_GVAR<OT::gvar, OT::Layout::SmallTypes>::accelerator_t::has_data() const Unexecuted instantiation: OT::gvar_GVAR<OT::GVAR, OT::Layout::MediumTypes>::accelerator_t::has_data() const |
681 | | |
682 | | accelerator_t (hb_face_t *face) |
683 | 0 | { |
684 | 0 | table = hb_sanitize_context_t ().reference_table<gvar_GVAR> (face, T::tableTag); |
685 | | /* If sanitize failed, set glyphCount to 0. */ |
686 | 0 | glyphCount = table->version.to_int () ? face->get_num_glyphs () : 0; |
687 | 0 | } Unexecuted instantiation: OT::gvar_GVAR<OT::GVAR, OT::Layout::MediumTypes>::accelerator_t::accelerator_t(hb_face_t*) Unexecuted instantiation: OT::gvar_GVAR<OT::gvar, OT::Layout::SmallTypes>::accelerator_t::accelerator_t(hb_face_t*) |
688 | 0 | ~accelerator_t () { table.destroy (); }Unexecuted instantiation: OT::gvar_GVAR<OT::gvar, OT::Layout::SmallTypes>::accelerator_t::~accelerator_t() Unexecuted instantiation: OT::gvar_GVAR<OT::GVAR, OT::Layout::MediumTypes>::accelerator_t::~accelerator_t() |
689 | | |
690 | | private: |
691 | | |
692 | | static float infer_delta (const hb_array_t<contour_point_t> points, |
693 | | const hb_array_t<contour_point_t> deltas, |
694 | | unsigned int target, unsigned int prev, unsigned int next, |
695 | | float contour_point_t::*m) |
696 | 0 | { |
697 | 0 | float target_val = points.arrayZ[target].*m; |
698 | 0 | float prev_val = points.arrayZ[prev].*m; |
699 | 0 | float next_val = points.arrayZ[next].*m; |
700 | 0 | float prev_delta = deltas.arrayZ[prev].*m; |
701 | 0 | float next_delta = deltas.arrayZ[next].*m; |
702 | |
|
703 | 0 | if (prev_val == next_val) |
704 | 0 | return (prev_delta == next_delta) ? prev_delta : 0.f; |
705 | 0 | else if (target_val <= hb_min (prev_val, next_val)) |
706 | 0 | return (prev_val < next_val) ? prev_delta : next_delta; |
707 | 0 | else if (target_val >= hb_max (prev_val, next_val)) |
708 | 0 | return (prev_val > next_val) ? prev_delta : next_delta; |
709 | | |
710 | | /* linear interpolation */ |
711 | 0 | float r = (target_val - prev_val) / (next_val - prev_val); |
712 | 0 | return prev_delta + r * (next_delta - prev_delta); |
713 | 0 | } Unexecuted instantiation: OT::gvar_GVAR<OT::GVAR, OT::Layout::MediumTypes>::accelerator_t::infer_delta(hb_array_t<contour_point_t>, hb_array_t<contour_point_t>, unsigned int, unsigned int, unsigned int, float contour_point_t::*) Unexecuted instantiation: OT::gvar_GVAR<OT::gvar, OT::Layout::SmallTypes>::accelerator_t::infer_delta(hb_array_t<contour_point_t>, hb_array_t<contour_point_t>, unsigned int, unsigned int, unsigned int, float contour_point_t::*) |
714 | | |
715 | | static unsigned int next_index (unsigned int i, unsigned int start, unsigned int end) |
716 | 0 | { return (i >= end) ? start : (i + 1); }Unexecuted instantiation: OT::gvar_GVAR<OT::GVAR, OT::Layout::MediumTypes>::accelerator_t::next_index(unsigned int, unsigned int, unsigned int) Unexecuted instantiation: OT::gvar_GVAR<OT::gvar, OT::Layout::SmallTypes>::accelerator_t::next_index(unsigned int, unsigned int, unsigned int) |
717 | | |
718 | | #ifndef HB_OPTIMIZE_SIZE |
719 | | template <bool is_x> |
720 | | #endif |
721 | | static bool decompile_deltas_add_to_points (const HBUINT8 *&p /* IN/OUT */, |
722 | | hb_array_t<contour_point_t> points, |
723 | | float scalar, |
724 | | const HBUINT8 *end, |
725 | | unsigned start |
726 | | #ifdef HB_OPTIMIZE_SIZE |
727 | | , bool is_x |
728 | | #endif |
729 | | ) |
730 | 0 | { |
731 | 0 | unsigned i = 0; |
732 | 0 | unsigned count = points.length; |
733 | 0 | while (i < count) |
734 | 0 | { |
735 | 0 | if (unlikely (p + 1 > end)) return false; |
736 | 0 | unsigned control = *p++; |
737 | 0 | unsigned run_count = (control & TupleValues::VALUE_RUN_COUNT_MASK) + 1; |
738 | 0 | unsigned stop = i + run_count; |
739 | 0 | if (unlikely (stop > count)) return false; |
740 | | |
741 | 0 | unsigned skip = i < start ? hb_min (start - i, run_count) : 0; |
742 | 0 | i += skip; |
743 | |
|
744 | 0 | switch (control & TupleValues::VALUES_SIZE_MASK) |
745 | 0 | { |
746 | 0 | case TupleValues::VALUES_ARE_ZEROS: |
747 | 0 | i = stop; |
748 | 0 | break; |
749 | 0 | case TupleValues::VALUES_ARE_WORDS: |
750 | 0 | { |
751 | 0 | if (unlikely (p + run_count * HBINT16::static_size > end)) return false; |
752 | 0 | p += skip * HBINT16::static_size; |
753 | 0 | const auto *pp = (const HBINT16 *) p; |
754 | 0 | for (; i < stop; i++) |
755 | 0 | { |
756 | 0 | float v = *pp++ * scalar; |
757 | 0 | if (is_x) points.arrayZ[i].x += v; |
758 | 0 | else points.arrayZ[i].y += v; |
759 | 0 | } |
760 | 0 | p = (const HBUINT8 *) pp; |
761 | 0 | } |
762 | 0 | break; |
763 | 0 | case TupleValues::VALUES_ARE_LONGS: |
764 | 0 | { |
765 | 0 | if (unlikely (p + run_count * HBINT32::static_size > end)) return false; |
766 | 0 | p += skip * HBINT32::static_size; |
767 | 0 | const auto *pp = (const HBINT32 *) p; |
768 | 0 | for (; i < stop; i++) |
769 | 0 | { |
770 | 0 | float v = *pp++ * scalar; |
771 | 0 | if (is_x) points.arrayZ[i].x += v; |
772 | 0 | else points.arrayZ[i].y += v; |
773 | 0 | } |
774 | 0 | p = (const HBUINT8 *) pp; |
775 | 0 | } |
776 | 0 | break; |
777 | 0 | case TupleValues::VALUES_ARE_BYTES: |
778 | 0 | { |
779 | 0 | if (unlikely (p + run_count > end)) return false; |
780 | 0 | p += skip * HBINT8::static_size; |
781 | 0 | const auto *pp = (const HBINT8 *) p; |
782 | 0 | for (; i < stop; i++) |
783 | 0 | { |
784 | 0 | float v = *pp++ * scalar; |
785 | 0 | if (is_x) points.arrayZ[i].x += v; |
786 | 0 | else points.arrayZ[i].y += v; |
787 | 0 | } |
788 | 0 | p = (const HBUINT8 *) pp; |
789 | 0 | } |
790 | 0 | break; |
791 | 0 | } |
792 | 0 | } |
793 | 0 | return true; |
794 | 0 | } Unexecuted instantiation: bool OT::gvar_GVAR<OT::GVAR, OT::Layout::MediumTypes>::accelerator_t::decompile_deltas_add_to_points<true>(OT::NumType<true, unsigned char, 1u> const*&, hb_array_t<contour_point_t>, float, OT::NumType<true, unsigned char, 1u> const*, unsigned int) Unexecuted instantiation: bool OT::gvar_GVAR<OT::GVAR, OT::Layout::MediumTypes>::accelerator_t::decompile_deltas_add_to_points<false>(OT::NumType<true, unsigned char, 1u> const*&, hb_array_t<contour_point_t>, float, OT::NumType<true, unsigned char, 1u> const*, unsigned int) Unexecuted instantiation: bool OT::gvar_GVAR<OT::gvar, OT::Layout::SmallTypes>::accelerator_t::decompile_deltas_add_to_points<true>(OT::NumType<true, unsigned char, 1u> const*&, hb_array_t<contour_point_t>, float, OT::NumType<true, unsigned char, 1u> const*, unsigned int) Unexecuted instantiation: bool OT::gvar_GVAR<OT::gvar, OT::Layout::SmallTypes>::accelerator_t::decompile_deltas_add_to_points<false>(OT::NumType<true, unsigned char, 1u> const*&, hb_array_t<contour_point_t>, float, OT::NumType<true, unsigned char, 1u> const*, unsigned int) |
795 | | public: |
796 | | bool apply_deltas_to_points (hb_codepoint_t glyph, |
797 | | hb_array_t<const int> coords, |
798 | | const hb_array_t<contour_point_t> points, |
799 | | hb_glyf_scratch_t &scratch, |
800 | | hb_scalar_cache_t *gvar_cache = nullptr, |
801 | | bool phantom_only = false, |
802 | | int64_t *budget = nullptr) const |
803 | 0 | { |
804 | 0 | if (unlikely (glyph >= glyphCount)) return true; |
805 | 0 | hb_scalar_cache_t *scalar_cache = gvar_cache ? |
806 | 0 | gvar_cache : |
807 | 0 | (hb_scalar_cache_t *) &Null(hb_scalar_cache_t); |
808 | |
|
809 | 0 | hb_bytes_t var_data_bytes = table->get_glyph_var_data_bytes (table.get_blob (), glyphCount, glyph); |
810 | 0 | if (!var_data_bytes.as<GlyphVariationData> ()->has_data ()) return true; |
811 | | |
812 | 0 | auto &shared_indices = scratch.shared_indices; |
813 | 0 | shared_indices.clear (); |
814 | |
|
815 | 0 | typename GlyphVariationData::tuple_iterator_t iterator; |
816 | 0 | bool has_tuples = GlyphVariationData::get_tuple_iterator (var_data_bytes, table->axisCount, |
817 | 0 | var_data_bytes.arrayZ, |
818 | 0 | shared_indices, &iterator); |
819 | | /* The shared point numbers are decoded on every load. */ |
820 | 0 | if (budget && |
821 | 0 | unlikely (!hb_budget_spend (*budget, HB_BUDGET_1, shared_indices.length))) |
822 | 0 | return false; |
823 | 0 | if (!has_tuples) |
824 | 0 | return true; /* so isn't applied at all */ |
825 | | |
826 | 0 | bool any_private_points = false; |
827 | 0 | bool private_points_checked = false; |
828 | | |
829 | | /* Save original points for inferred delta calculation */ |
830 | 0 | auto &orig_points_vec = scratch.orig_points; |
831 | 0 | orig_points_vec.clear (); // Populated lazily |
832 | 0 | auto orig_points = orig_points_vec.as_array (); |
833 | | |
834 | | /* flag is used to indicate referenced point */ |
835 | 0 | auto &deltas_vec = scratch.deltas; |
836 | 0 | deltas_vec.clear (); // Populated lazily |
837 | 0 | auto deltas = deltas_vec.as_array (); |
838 | |
|
839 | 0 | unsigned num_coords = table->axisCount; |
840 | 0 | hb_array_t<const F2DOT14> shared_tuples = (table+table->sharedTuples).as_array (table->sharedTupleCount * num_coords); |
841 | |
|
842 | 0 | auto &private_indices = scratch.private_indices; |
843 | 0 | auto &x_deltas = scratch.x_deltas; |
844 | 0 | auto &y_deltas = scratch.y_deltas; |
845 | |
|
846 | 0 | unsigned count = points.length; |
847 | 0 | bool flush = false; |
848 | |
|
849 | 0 | do |
850 | 0 | { |
851 | | /* Evaluating a tuple's region walks the axes, whether or not the |
852 | | * tuple ends up applying. */ |
853 | 0 | if (budget && |
854 | 0 | unlikely (!hb_budget_spend (*budget, HB_BUDGET_1, 1 + num_coords))) |
855 | 0 | return false; |
856 | | |
857 | 0 | float scalar = iterator.current_tuple->calculate_scalar (coords, num_coords, shared_tuples, |
858 | 0 | scalar_cache); |
859 | |
|
860 | 0 | if (scalar == 0.f) continue; |
861 | | |
862 | 0 | if (budget && |
863 | 0 | unlikely (!hb_budget_spend (*budget, HB_BUDGET_1, |
864 | 0 | phantom_only && count >= 4 ? 4 : count))) |
865 | 0 | return false; |
866 | | |
867 | 0 | if (!private_points_checked) |
868 | 0 | { |
869 | 0 | auto scan = iterator; |
870 | 0 | do |
871 | 0 | { |
872 | 0 | if (scan.current_tuple->has_private_points ()) |
873 | 0 | { |
874 | 0 | any_private_points = true; |
875 | 0 | break; |
876 | 0 | } |
877 | 0 | } while (scan.move_to_next ()); |
878 | 0 | private_points_checked = true; |
879 | 0 | } |
880 | 0 | const HBUINT8 *p = iterator.get_serialized_data (); |
881 | 0 | unsigned int length = iterator.current_tuple->get_data_size (); |
882 | 0 | if (unlikely (!iterator.var_data_bytes.check_range (p, length))) |
883 | 0 | return false; |
884 | | |
885 | 0 | if (!deltas) |
886 | 0 | { |
887 | 0 | if (unlikely (!deltas_vec.resize_dirty (count))) return false; |
888 | 0 | deltas = deltas_vec.as_array (); |
889 | 0 | hb_memset (deltas.arrayZ + (phantom_only ? count - 4 : 0), 0, |
890 | 0 | (phantom_only ? 4 : count) * sizeof (deltas[0])); |
891 | 0 | } |
892 | | |
893 | 0 | const HBUINT8 *end = p + length; |
894 | |
|
895 | 0 | bool has_private_points = iterator.current_tuple->has_private_points (); |
896 | 0 | if (has_private_points && |
897 | 0 | !GlyphVariationData::decompile_points (p, private_indices, end)) |
898 | 0 | return false; |
899 | 0 | const hb_array_t<unsigned int> &indices = has_private_points ? private_indices : shared_indices; |
900 | | |
901 | | /* Point numbers can outnumber the points charged above. */ |
902 | 0 | if (budget && |
903 | 0 | unlikely (!hb_budget_spend (*budget, HB_BUDGET_1, indices.length))) |
904 | 0 | return false; |
905 | | |
906 | 0 | bool apply_to_all = (indices.length == 0); |
907 | 0 | unsigned num_deltas = apply_to_all ? points.length : indices.length; |
908 | 0 | unsigned start_deltas = (apply_to_all && phantom_only && num_deltas >= 4 ? num_deltas - 4 : 0); |
909 | |
|
910 | 0 | if (apply_to_all && !any_private_points) |
911 | 0 | { |
912 | | #ifdef HB_OPTIMIZE_SIZE |
913 | | if (unlikely (!decompile_deltas_add_to_points (p, points, scalar, end, start_deltas, true))) return false; |
914 | | if (unlikely (!decompile_deltas_add_to_points (p, points, scalar, end, start_deltas, false))) return false; |
915 | | #else |
916 | 0 | if (unlikely (!decompile_deltas_add_to_points<true> (p, points, scalar, end, start_deltas))) return false; |
917 | 0 | if (unlikely (!decompile_deltas_add_to_points<false> (p, points, scalar, end, start_deltas))) return false; |
918 | 0 | #endif |
919 | 0 | continue; |
920 | 0 | } |
921 | | |
922 | 0 | if (unlikely (!x_deltas.resize_dirty (num_deltas))) return false; |
923 | 0 | if (unlikely (!GlyphVariationData::decompile_deltas (p, x_deltas, end, false, start_deltas))) return false; |
924 | 0 | if (unlikely (!y_deltas.resize_dirty (num_deltas))) return false; |
925 | 0 | if (unlikely (!GlyphVariationData::decompile_deltas (p, y_deltas, end, false, start_deltas))) return false; |
926 | | |
927 | 0 | if (!apply_to_all) |
928 | 0 | { |
929 | 0 | if (!orig_points && !phantom_only) |
930 | 0 | { |
931 | 0 | orig_points_vec.extend (points); |
932 | 0 | if (unlikely (orig_points_vec.in_error ())) return false; |
933 | 0 | orig_points = orig_points_vec.as_array (); |
934 | 0 | } |
935 | | |
936 | 0 | if (flush) |
937 | 0 | { |
938 | 0 | for (unsigned int i = phantom_only ? count - 4 : 0; i < count; i++) |
939 | 0 | points.arrayZ[i].translate (deltas.arrayZ[i]); |
940 | 0 | } |
941 | 0 | hb_memset (deltas.arrayZ + (phantom_only ? count - 4 : 0), 0, |
942 | 0 | (phantom_only ? 4 : count) * sizeof (deltas[0])); |
943 | 0 | } |
944 | | |
945 | 0 | if (HB_OPTIMIZE_SIZE_VAL) |
946 | 0 | { |
947 | 0 | for (unsigned int i = 0; i < num_deltas; i++) |
948 | 0 | { |
949 | 0 | unsigned int pt_index; |
950 | 0 | if (apply_to_all) |
951 | 0 | pt_index = i; |
952 | 0 | else |
953 | 0 | { |
954 | 0 | pt_index = indices[i]; |
955 | 0 | if (unlikely (pt_index >= deltas.length)) continue; |
956 | 0 | } |
957 | 0 | if (phantom_only && pt_index < count - 4) continue; |
958 | 0 | auto &delta = deltas.arrayZ[pt_index]; |
959 | 0 | delta.flag = 1; /* this point is referenced, i.e., explicit deltas specified */ |
960 | 0 | delta.add_delta (x_deltas.arrayZ[i] * scalar, |
961 | 0 | y_deltas.arrayZ[i] * scalar); |
962 | 0 | } |
963 | 0 | } |
964 | 0 | else |
965 | 0 | { |
966 | | /* Ouch. Four cases... for optimization. */ |
967 | 0 | if (scalar != 1.0f) |
968 | 0 | { |
969 | 0 | if (apply_to_all) |
970 | 0 | for (unsigned int i = phantom_only ? count - 4 : 0; i < count; i++) |
971 | 0 | { |
972 | 0 | auto &delta = deltas.arrayZ[i]; |
973 | 0 | delta.add_delta (x_deltas.arrayZ[i] * scalar, |
974 | 0 | y_deltas.arrayZ[i] * scalar); |
975 | 0 | } |
976 | 0 | else |
977 | 0 | for (unsigned int i = 0; i < num_deltas; i++) |
978 | 0 | { |
979 | 0 | unsigned int pt_index = indices[i]; |
980 | 0 | if (unlikely (pt_index >= deltas.length)) continue; |
981 | 0 | if (phantom_only && pt_index < count - 4) continue; |
982 | 0 | auto &delta = deltas.arrayZ[pt_index]; |
983 | 0 | delta.flag = 1; /* this point is referenced, i.e., explicit deltas specified */ |
984 | 0 | delta.add_delta (x_deltas.arrayZ[i] * scalar, |
985 | 0 | y_deltas.arrayZ[i] * scalar); |
986 | 0 | } |
987 | 0 | } |
988 | 0 | else |
989 | 0 | { |
990 | 0 | if (apply_to_all) |
991 | 0 | for (unsigned int i = phantom_only ? count - 4 : 0; i < count; i++) |
992 | 0 | { |
993 | 0 | auto &delta = deltas.arrayZ[i]; |
994 | 0 | delta.add_delta (x_deltas.arrayZ[i], |
995 | 0 | y_deltas.arrayZ[i]); |
996 | 0 | } |
997 | 0 | else |
998 | 0 | for (unsigned int i = 0; i < num_deltas; i++) |
999 | 0 | { |
1000 | 0 | unsigned int pt_index = indices[i]; |
1001 | 0 | if (unlikely (pt_index >= deltas.length)) continue; |
1002 | 0 | if (phantom_only && pt_index < count - 4) continue; |
1003 | 0 | auto &delta = deltas.arrayZ[pt_index]; |
1004 | 0 | delta.flag = 1; /* this point is referenced, i.e., explicit deltas specified */ |
1005 | 0 | delta.add_delta (x_deltas.arrayZ[i], |
1006 | 0 | y_deltas.arrayZ[i]); |
1007 | 0 | } |
1008 | 0 | } |
1009 | 0 | } |
1010 | | |
1011 | | /* infer deltas for unreferenced points */ |
1012 | 0 | if (!apply_to_all && !phantom_only) |
1013 | 0 | { |
1014 | 0 | unsigned start_point = 0; |
1015 | 0 | unsigned end_point = 0; |
1016 | 0 | while (true) |
1017 | 0 | { |
1018 | 0 | while (end_point < count && !points.arrayZ[end_point].is_end_point) |
1019 | 0 | end_point++; |
1020 | 0 | if (unlikely (end_point == count)) break; |
1021 | | |
1022 | | /* Check the number of unreferenced points in a contour. If no unref points or no ref points, nothing to do. */ |
1023 | 0 | unsigned unref_count = 0; |
1024 | 0 | for (unsigned i = start_point; i < end_point + 1; i++) |
1025 | 0 | unref_count += deltas.arrayZ[i].flag; |
1026 | 0 | unref_count = (end_point - start_point + 1) - unref_count; |
1027 | |
|
1028 | 0 | unsigned j = start_point; |
1029 | 0 | if (unref_count == 0 || unref_count > end_point - start_point) |
1030 | 0 | goto no_more_gaps; |
1031 | | |
1032 | 0 | for (;;) |
1033 | 0 | { |
1034 | | /* Locate the next gap of unreferenced points between two referenced points prev and next. |
1035 | | * Note that a gap may wrap around at left (start_point) and/or at right (end_point). |
1036 | | */ |
1037 | 0 | unsigned int prev, next, i; |
1038 | 0 | for (;;) |
1039 | 0 | { |
1040 | 0 | i = j; |
1041 | 0 | j = next_index (i, start_point, end_point); |
1042 | 0 | if (deltas.arrayZ[i].flag && !deltas.arrayZ[j].flag) break; |
1043 | 0 | } |
1044 | 0 | prev = j = i; |
1045 | 0 | for (;;) |
1046 | 0 | { |
1047 | 0 | i = j; |
1048 | 0 | j = next_index (i, start_point, end_point); |
1049 | 0 | if (!deltas.arrayZ[i].flag && deltas.arrayZ[j].flag) break; |
1050 | 0 | } |
1051 | 0 | next = j; |
1052 | | /* Infer deltas for all unref points in the gap between prev and next */ |
1053 | 0 | i = prev; |
1054 | 0 | for (;;) |
1055 | 0 | { |
1056 | 0 | i = next_index (i, start_point, end_point); |
1057 | 0 | if (i == next) break; |
1058 | 0 | deltas.arrayZ[i].x = infer_delta (orig_points, deltas, i, prev, next, &contour_point_t::x); |
1059 | 0 | deltas.arrayZ[i].y = infer_delta (orig_points, deltas, i, prev, next, &contour_point_t::y); |
1060 | 0 | if (--unref_count == 0) goto no_more_gaps; |
1061 | 0 | } |
1062 | 0 | } |
1063 | 0 | no_more_gaps: |
1064 | 0 | start_point = end_point = end_point + 1; |
1065 | 0 | } |
1066 | 0 | } |
1067 | | |
1068 | 0 | flush = true; |
1069 | |
|
1070 | 0 | } while (iterator.move_to_next ()); |
1071 | | |
1072 | 0 | if (flush) |
1073 | 0 | { |
1074 | 0 | for (unsigned int i = phantom_only ? count - 4 : 0; i < count; i++) |
1075 | 0 | points.arrayZ[i].translate (deltas.arrayZ[i]); |
1076 | 0 | } |
1077 | |
|
1078 | 0 | return true; |
1079 | 0 | } Unexecuted instantiation: OT::gvar_GVAR<OT::GVAR, OT::Layout::MediumTypes>::accelerator_t::apply_deltas_to_points(unsigned int, hb_array_t<int const>, hb_array_t<contour_point_t>, hb_glyf_scratch_t&, OT::hb_scalar_cache_t*, bool, long*) const Unexecuted instantiation: OT::gvar_GVAR<OT::gvar, OT::Layout::SmallTypes>::accelerator_t::apply_deltas_to_points(unsigned int, hb_array_t<int const>, hb_array_t<contour_point_t>, hb_glyf_scratch_t&, OT::hb_scalar_cache_t*, bool, long*) const |
1080 | | |
1081 | 0 | unsigned int get_axis_count () const { return table->axisCount; }Unexecuted instantiation: OT::gvar_GVAR<OT::GVAR, OT::Layout::MediumTypes>::accelerator_t::get_axis_count() const Unexecuted instantiation: OT::gvar_GVAR<OT::gvar, OT::Layout::SmallTypes>::accelerator_t::get_axis_count() const |
1082 | | |
1083 | | private: |
1084 | | hb_blob_ptr_t<gvar_GVAR> table; |
1085 | | unsigned glyphCount; |
1086 | | }; |
1087 | | |
1088 | | protected: |
1089 | | FixedVersion<>version; /* Version number of the glyph variations table |
1090 | | * Set to 0x00010000u. */ |
1091 | | HBUINT16 axisCount; /* The number of variation axes for this font. This must be |
1092 | | * the same number as axisCount in the 'fvar' table. */ |
1093 | | HBUINT16 sharedTupleCount; |
1094 | | /* The number of shared tuple records. Shared tuple records |
1095 | | * can be referenced within glyph variation data tables for |
1096 | | * multiple glyphs, as opposed to other tuple records stored |
1097 | | * directly within a glyph variation data table. */ |
1098 | | NNOffset32To<UnsizedArrayOf<F2DOT14>> |
1099 | | sharedTuples; /* Offset from the start of this table to the shared tuple records. |
1100 | | * Array of tuple records shared across all glyph variation data tables. */ |
1101 | | GlyphCountType |
1102 | | glyphCountX; /* The number of glyphs in this font. This must match the number of |
1103 | | * glyphs stored elsewhere in the font. */ |
1104 | | HBUINT16 flags; /* Bit-field that gives the format of the offset array that follows. |
1105 | | * If bit 0 is clear, the offsets are uint16; if bit 0 is set, the |
1106 | | * offsets are uint32. */ |
1107 | | Offset32To<GlyphVariationData> |
1108 | | dataZ; /* Offset from the start of this table to the array of |
1109 | | * GlyphVariationData tables. */ |
1110 | | UnsizedArrayOf<HBUINT8> |
1111 | | offsetZ; /* Offsets from the start of the GlyphVariationData array |
1112 | | * to each GlyphVariationData table. */ |
1113 | | public: |
1114 | | DEFINE_SIZE_ARRAY (18 + GlyphCountType::static_size, offsetZ); |
1115 | | }; |
1116 | | |
1117 | | struct gvar : gvar_GVAR<gvar, SmallTypes> |
1118 | | { |
1119 | | static constexpr hb_tag_t tableTag = HB_OT_TAG_gvar; |
1120 | | }; |
1121 | | |
1122 | | struct GVAR : gvar_GVAR<GVAR, MediumTypes> |
1123 | | { |
1124 | | static constexpr hb_tag_t tableTag = HB_OT_TAG_GVAR; |
1125 | | }; |
1126 | | |
1127 | | struct gvar_accelerator_t : hb_dual_accelerator_t<gvar::accelerator_t, |
1128 | | GVAR::accelerator_t> |
1129 | | { |
1130 | | gvar_accelerator_t (hb_face_t *face) : |
1131 | 0 | hb_dual_accelerator_t<gvar::accelerator_t, GVAR::accelerator_t> (face) {} |
1132 | | |
1133 | | hb_scalar_cache_t *create_cache () const |
1134 | 0 | { |
1135 | 0 | if (has_data ()) return HB_DUAL_GET (*this, create_cache ()); |
1136 | 0 | return nullptr; |
1137 | 0 | } |
1138 | | |
1139 | | static void destroy_cache (hb_scalar_cache_t *cache) |
1140 | 0 | { |
1141 | 0 | hb_scalar_cache_t::destroy (cache); |
1142 | 0 | } |
1143 | | |
1144 | | bool apply_deltas_to_points (hb_codepoint_t glyph, |
1145 | | hb_array_t<const int> coords, |
1146 | | const hb_array_t<contour_point_t> points, |
1147 | | hb_glyf_scratch_t &scratch, |
1148 | | hb_scalar_cache_t *gvar_cache = nullptr, |
1149 | | bool phantom_only = false, |
1150 | | int64_t *budget = nullptr) const |
1151 | 0 | { |
1152 | 0 | return HB_DUAL_GET (*this, apply_deltas_to_points (glyph, coords, points, |
1153 | 0 | scratch, gvar_cache, |
1154 | 0 | phantom_only, budget)); |
1155 | 0 | } |
1156 | | |
1157 | | unsigned int get_axis_count () const |
1158 | 0 | { |
1159 | 0 | if (has_data ()) return HB_DUAL_GET (*this, get_axis_count ()); |
1160 | 0 | return 0; |
1161 | 0 | } |
1162 | | }; |
1163 | | |
1164 | | } /* namespace OT */ |
1165 | | |
1166 | | #endif /* HB_OT_VAR_GVAR_TABLE_HH */ |