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