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