/src/skia/third_party/externals/harfbuzz/src/hb-ot-var-gvar-table.hh
Line | Count | Source (jump to first uncovered line) |
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-open-type.hh" |
32 | | |
33 | | /* |
34 | | * gvar -- Glyph Variation Table |
35 | | * https://docs.microsoft.com/en-us/typography/opentype/spec/gvar |
36 | | */ |
37 | 0 | #define HB_OT_TAG_gvar HB_TAG('g','v','a','r') |
38 | | |
39 | | namespace OT { |
40 | | |
41 | | struct contour_point_t |
42 | | { |
43 | | void init (float x_ = 0.f, float y_ = 0.f, bool is_end_point_ = false) |
44 | 0 | { flag = 0; x = x_; y = y_; is_end_point = is_end_point_; } |
45 | | |
46 | 0 | void translate (const contour_point_t &p) { x += p.x; y += p.y; } |
47 | | |
48 | | uint8_t flag; |
49 | | float x, y; |
50 | | bool is_end_point; |
51 | | }; |
52 | | |
53 | | struct contour_point_vector_t : hb_vector_t<contour_point_t> |
54 | | { |
55 | | void extend (const hb_array_t<contour_point_t> &a) |
56 | 0 | { |
57 | 0 | unsigned int old_len = length; |
58 | 0 | resize (old_len + a.length); |
59 | 0 | for (unsigned int i = 0; i < a.length; i++) |
60 | 0 | (*this)[old_len + i] = a[i]; |
61 | 0 | } |
62 | | |
63 | | void transform (const float (&matrix)[4]) |
64 | 0 | { |
65 | 0 | for (unsigned int i = 0; i < length; i++) |
66 | 0 | { |
67 | 0 | contour_point_t &p = (*this)[i]; |
68 | 0 | float x_ = p.x * matrix[0] + p.y * matrix[2]; |
69 | 0 | p.y = p.x * matrix[1] + p.y * matrix[3]; |
70 | 0 | p.x = x_; |
71 | 0 | } |
72 | 0 | } |
73 | | |
74 | | void translate (const contour_point_t& delta) |
75 | 0 | { |
76 | 0 | for (unsigned int i = 0; i < length; i++) |
77 | 0 | (*this)[i].translate (delta); |
78 | 0 | } |
79 | | }; |
80 | | |
81 | | /* https://docs.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#tuplevariationheader */ |
82 | | struct TupleVariationHeader |
83 | | { |
84 | | unsigned get_size (unsigned axis_count) const |
85 | 0 | { return min_size + get_all_tuples (axis_count).get_size (); } |
86 | | |
87 | 0 | unsigned get_data_size () const { return varDataSize; } |
88 | | |
89 | | const TupleVariationHeader &get_next (unsigned axis_count) const |
90 | 0 | { return StructAtOffset<TupleVariationHeader> (this, get_size (axis_count)); } |
91 | | |
92 | | float calculate_scalar (const int *coords, unsigned int coord_count, |
93 | | const hb_array_t<const F2DOT14> shared_tuples) const |
94 | 0 | { |
95 | 0 | hb_array_t<const F2DOT14> peak_tuple; |
96 | |
|
97 | 0 | if (has_peak ()) |
98 | 0 | peak_tuple = get_peak_tuple (coord_count); |
99 | 0 | else |
100 | 0 | { |
101 | 0 | unsigned int index = get_index (); |
102 | 0 | if (unlikely (index * coord_count >= shared_tuples.length)) |
103 | 0 | return 0.f; |
104 | 0 | peak_tuple = shared_tuples.sub_array (coord_count * index, coord_count); |
105 | 0 | } |
106 | |
|
107 | 0 | hb_array_t<const F2DOT14> start_tuple; |
108 | 0 | hb_array_t<const F2DOT14> end_tuple; |
109 | 0 | if (has_intermediate ()) |
110 | 0 | { |
111 | 0 | start_tuple = get_start_tuple (coord_count); |
112 | 0 | end_tuple = get_end_tuple (coord_count); |
113 | 0 | } |
114 | |
|
115 | 0 | float scalar = 1.f; |
116 | 0 | for (unsigned int i = 0; i < coord_count; i++) |
117 | 0 | { |
118 | 0 | int v = coords[i]; |
119 | 0 | int peak = peak_tuple[i]; |
120 | 0 | if (!peak || v == peak) continue; |
121 | | |
122 | 0 | if (has_intermediate ()) |
123 | 0 | { |
124 | 0 | int start = start_tuple[i]; |
125 | 0 | int end = end_tuple[i]; |
126 | 0 | if (unlikely (start > peak || peak > end || |
127 | 0 | (start < 0 && end > 0 && peak))) continue; |
128 | 0 | if (v < start || v > end) return 0.f; |
129 | 0 | if (v < peak) |
130 | 0 | { if (peak != start) scalar *= (float) (v - start) / (peak - start); } |
131 | 0 | else |
132 | 0 | { if (peak != end) scalar *= (float) (end - v) / (end - peak); } |
133 | 0 | } |
134 | 0 | else if (!v || v < hb_min (0, peak) || v > hb_max (0, peak)) return 0.f; |
135 | 0 | else |
136 | 0 | scalar *= (float) v / peak; |
137 | 0 | } |
138 | 0 | return scalar; |
139 | 0 | } |
140 | | |
141 | 0 | bool has_peak () const { return tupleIndex & TuppleIndex::EmbeddedPeakTuple; } |
142 | 0 | bool has_intermediate () const { return tupleIndex & TuppleIndex::IntermediateRegion; } |
143 | 0 | bool has_private_points () const { return tupleIndex & TuppleIndex::PrivatePointNumbers; } |
144 | 0 | unsigned get_index () const { return tupleIndex & TuppleIndex::TupleIndexMask; } |
145 | | |
146 | | protected: |
147 | | struct TuppleIndex : HBUINT16 |
148 | | { |
149 | | enum Flags { |
150 | | EmbeddedPeakTuple = 0x8000u, |
151 | | IntermediateRegion = 0x4000u, |
152 | | PrivatePointNumbers = 0x2000u, |
153 | | TupleIndexMask = 0x0FFFu |
154 | | }; |
155 | | |
156 | | DEFINE_SIZE_STATIC (2); |
157 | | }; |
158 | | |
159 | | hb_array_t<const F2DOT14> get_all_tuples (unsigned axis_count) const |
160 | 0 | { return StructAfter<UnsizedArrayOf<F2DOT14>> (tupleIndex).as_array ((has_peak () + has_intermediate () * 2) * axis_count); } |
161 | | hb_array_t<const F2DOT14> get_peak_tuple (unsigned axis_count) const |
162 | 0 | { return get_all_tuples (axis_count).sub_array (0, axis_count); } |
163 | | hb_array_t<const F2DOT14> get_start_tuple (unsigned axis_count) const |
164 | 0 | { return get_all_tuples (axis_count).sub_array (has_peak () * axis_count, axis_count); } |
165 | | hb_array_t<const F2DOT14> get_end_tuple (unsigned axis_count) const |
166 | 0 | { return get_all_tuples (axis_count).sub_array (has_peak () * axis_count + axis_count, axis_count); } |
167 | | |
168 | | HBUINT16 varDataSize; /* The size in bytes of the serialized |
169 | | * data for this tuple variation table. */ |
170 | | TuppleIndex tupleIndex; /* A packed field. The high 4 bits are flags (see below). |
171 | | The low 12 bits are an index into a shared tuple |
172 | | records array. */ |
173 | | /* UnsizedArrayOf<F2DOT14> peakTuple - optional */ |
174 | | /* Peak tuple record for this tuple variation table — optional, |
175 | | * determined by flags in the tupleIndex value. |
176 | | * |
177 | | * Note that this must always be included in the 'cvar' table. */ |
178 | | /* UnsizedArrayOf<F2DOT14> intermediateStartTuple - optional */ |
179 | | /* Intermediate start tuple record for this tuple variation table — optional, |
180 | | determined by flags in the tupleIndex value. */ |
181 | | /* UnsizedArrayOf<F2DOT14> intermediateEndTuple - optional */ |
182 | | /* Intermediate end tuple record for this tuple variation table — optional, |
183 | | * determined by flags in the tupleIndex value. */ |
184 | | public: |
185 | | DEFINE_SIZE_MIN (4); |
186 | | }; |
187 | | |
188 | | struct GlyphVariationData |
189 | | { |
190 | | const TupleVariationHeader &get_tuple_var_header (void) const |
191 | 0 | { return StructAfter<TupleVariationHeader> (data); } |
192 | | |
193 | | struct tuple_iterator_t |
194 | | { |
195 | | void init (hb_bytes_t var_data_bytes_, unsigned int axis_count_) |
196 | 0 | { |
197 | 0 | var_data_bytes = var_data_bytes_; |
198 | 0 | var_data = var_data_bytes_.as<GlyphVariationData> (); |
199 | 0 | index = 0; |
200 | 0 | axis_count = axis_count_; |
201 | 0 | current_tuple = &var_data->get_tuple_var_header (); |
202 | 0 | data_offset = 0; |
203 | 0 | } |
204 | | |
205 | | bool get_shared_indices (hb_vector_t<unsigned int> &shared_indices /* OUT */) |
206 | 0 | { |
207 | 0 | if (var_data->has_shared_point_numbers ()) |
208 | 0 | { |
209 | 0 | const HBUINT8 *base = &(var_data+var_data->data); |
210 | 0 | const HBUINT8 *p = base; |
211 | 0 | if (!unpack_points (p, shared_indices, var_data_bytes)) return false; |
212 | 0 | data_offset = p - base; |
213 | 0 | } |
214 | 0 | return true; |
215 | 0 | } |
216 | | |
217 | | bool is_valid () const |
218 | 0 | { |
219 | 0 | return (index < var_data->tupleVarCount.get_count ()) && |
220 | 0 | var_data_bytes.check_range (current_tuple, TupleVariationHeader::min_size) && |
221 | 0 | var_data_bytes.check_range (current_tuple, hb_max (current_tuple->get_data_size (), current_tuple->get_size (axis_count))) && |
222 | 0 | current_tuple->get_size (axis_count); |
223 | 0 | } |
224 | | |
225 | | bool move_to_next () |
226 | 0 | { |
227 | 0 | data_offset += current_tuple->get_data_size (); |
228 | 0 | current_tuple = ¤t_tuple->get_next (axis_count); |
229 | 0 | index++; |
230 | 0 | return is_valid (); |
231 | 0 | } |
232 | | |
233 | | const HBUINT8 *get_serialized_data () const |
234 | 0 | { return &(var_data+var_data->data) + data_offset; } |
235 | | |
236 | | private: |
237 | | const GlyphVariationData *var_data; |
238 | | unsigned int index; |
239 | | unsigned int axis_count; |
240 | | unsigned int data_offset; |
241 | | |
242 | | public: |
243 | | hb_bytes_t var_data_bytes; |
244 | | const TupleVariationHeader *current_tuple; |
245 | | }; |
246 | | |
247 | | static bool get_tuple_iterator (hb_bytes_t var_data_bytes, unsigned axis_count, |
248 | | hb_vector_t<unsigned int> &shared_indices /* OUT */, |
249 | | tuple_iterator_t *iterator /* OUT */) |
250 | 0 | { |
251 | 0 | iterator->init (var_data_bytes, axis_count); |
252 | 0 | if (!iterator->get_shared_indices (shared_indices)) |
253 | 0 | return false; |
254 | 0 | return iterator->is_valid (); |
255 | 0 | } |
256 | | |
257 | 0 | bool has_shared_point_numbers () const { return tupleVarCount.has_shared_point_numbers (); } |
258 | | |
259 | | static bool unpack_points (const HBUINT8 *&p /* IN/OUT */, |
260 | | hb_vector_t<unsigned int> &points /* OUT */, |
261 | | const hb_bytes_t &bytes) |
262 | 0 | { |
263 | 0 | enum packed_point_flag_t |
264 | 0 | { |
265 | 0 | POINTS_ARE_WORDS = 0x80, |
266 | 0 | POINT_RUN_COUNT_MASK = 0x7F |
267 | 0 | }; |
268 | |
|
269 | 0 | if (unlikely (!bytes.check_range (p))) return false; |
270 | | |
271 | 0 | uint16_t count = *p++; |
272 | 0 | if (count & POINTS_ARE_WORDS) |
273 | 0 | { |
274 | 0 | if (unlikely (!bytes.check_range (p))) return false; |
275 | 0 | count = ((count & POINT_RUN_COUNT_MASK) << 8) | *p++; |
276 | 0 | } |
277 | 0 | points.resize (count); |
278 | |
|
279 | 0 | unsigned int n = 0; |
280 | 0 | uint16_t i = 0; |
281 | 0 | while (i < count) |
282 | 0 | { |
283 | 0 | if (unlikely (!bytes.check_range (p))) return false; |
284 | 0 | uint16_t j; |
285 | 0 | uint8_t control = *p++; |
286 | 0 | uint16_t run_count = (control & POINT_RUN_COUNT_MASK) + 1; |
287 | 0 | if (control & POINTS_ARE_WORDS) |
288 | 0 | { |
289 | 0 | for (j = 0; j < run_count && i < count; j++, i++) |
290 | 0 | { |
291 | 0 | if (unlikely (!bytes.check_range ((const HBUINT16 *) p))) |
292 | 0 | return false; |
293 | 0 | n += *(const HBUINT16 *)p; |
294 | 0 | points[i] = n; |
295 | 0 | p += HBUINT16::static_size; |
296 | 0 | } |
297 | 0 | } |
298 | 0 | else |
299 | 0 | { |
300 | 0 | for (j = 0; j < run_count && i < count; j++, i++) |
301 | 0 | { |
302 | 0 | if (unlikely (!bytes.check_range (p))) return false; |
303 | 0 | n += *p++; |
304 | 0 | points[i] = n; |
305 | 0 | } |
306 | 0 | } |
307 | 0 | if (j < run_count) return false; |
308 | 0 | } |
309 | 0 | return true; |
310 | 0 | } |
311 | | |
312 | | static bool unpack_deltas (const HBUINT8 *&p /* IN/OUT */, |
313 | | hb_vector_t<int> &deltas /* IN/OUT */, |
314 | | const hb_bytes_t &bytes) |
315 | 0 | { |
316 | 0 | enum packed_delta_flag_t |
317 | 0 | { |
318 | 0 | DELTAS_ARE_ZERO = 0x80, |
319 | 0 | DELTAS_ARE_WORDS = 0x40, |
320 | 0 | DELTA_RUN_COUNT_MASK = 0x3F |
321 | 0 | }; |
322 | |
|
323 | 0 | unsigned int i = 0; |
324 | 0 | unsigned int count = deltas.length; |
325 | 0 | while (i < count) |
326 | 0 | { |
327 | 0 | if (unlikely (!bytes.check_range (p))) return false; |
328 | 0 | uint8_t control = *p++; |
329 | 0 | unsigned int run_count = (control & DELTA_RUN_COUNT_MASK) + 1; |
330 | 0 | unsigned int j; |
331 | 0 | if (control & DELTAS_ARE_ZERO) |
332 | 0 | for (j = 0; j < run_count && i < count; j++, i++) |
333 | 0 | deltas[i] = 0; |
334 | 0 | else if (control & DELTAS_ARE_WORDS) |
335 | 0 | for (j = 0; j < run_count && i < count; j++, i++) |
336 | 0 | { |
337 | 0 | if (unlikely (!bytes.check_range ((const HBUINT16 *) p))) |
338 | 0 | return false; |
339 | 0 | deltas[i] = *(const HBINT16 *) p; |
340 | 0 | p += HBUINT16::static_size; |
341 | 0 | } |
342 | 0 | else |
343 | 0 | for (j = 0; j < run_count && i < count; j++, i++) |
344 | 0 | { |
345 | 0 | if (unlikely (!bytes.check_range (p))) |
346 | 0 | return false; |
347 | 0 | deltas[i] = *(const HBINT8 *) p++; |
348 | 0 | } |
349 | 0 | if (j < run_count) |
350 | 0 | return false; |
351 | 0 | } |
352 | 0 | return true; |
353 | 0 | } |
354 | | |
355 | 0 | bool has_data () const { return tupleVarCount; } |
356 | | |
357 | | protected: |
358 | | struct TupleVarCount : HBUINT16 |
359 | | { |
360 | 0 | bool has_shared_point_numbers () const { return ((*this) & SharedPointNumbers); } |
361 | 0 | unsigned int get_count () const { return (*this) & CountMask; } |
362 | | |
363 | | protected: |
364 | | enum Flags |
365 | | { |
366 | | SharedPointNumbers= 0x8000u, |
367 | | CountMask = 0x0FFFu |
368 | | }; |
369 | | public: |
370 | | DEFINE_SIZE_STATIC (2); |
371 | | }; |
372 | | |
373 | | TupleVarCount tupleVarCount; /* A packed field. The high 4 bits are flags, and the |
374 | | * low 12 bits are the number of tuple variation tables |
375 | | * for this glyph. The number of tuple variation tables |
376 | | * can be any number between 1 and 4095. */ |
377 | | Offset16To<HBUINT8> |
378 | | data; /* Offset from the start of the GlyphVariationData table |
379 | | * to the serialized data. */ |
380 | | /* TupleVariationHeader tupleVariationHeaders[] *//* Array of tuple variation headers. */ |
381 | | public: |
382 | | DEFINE_SIZE_MIN (4); |
383 | | }; |
384 | | |
385 | | struct gvar |
386 | | { |
387 | | static constexpr hb_tag_t tableTag = HB_OT_TAG_gvar; |
388 | | |
389 | | bool sanitize_shallow (hb_sanitize_context_t *c) const |
390 | 0 | { |
391 | 0 | TRACE_SANITIZE (this); |
392 | 0 | return_trace (c->check_struct (this) && (version.major == 1) && |
393 | 0 | (glyphCount == c->get_num_glyphs ()) && |
394 | 0 | sharedTuples.sanitize (c, this, axisCount * sharedTupleCount) && |
395 | 0 | (is_long_offset () ? |
396 | 0 | c->check_array (get_long_offset_array (), glyphCount+1) : |
397 | 0 | c->check_array (get_short_offset_array (), glyphCount+1)) && |
398 | 0 | c->check_array (((const HBUINT8*)&(this+dataZ)) + get_offset (0), |
399 | 0 | get_offset (glyphCount) - get_offset (0))); |
400 | 0 | } |
401 | | |
402 | | /* GlyphVariationData not sanitized here; must be checked while accessing each glyph varation data */ |
403 | | bool sanitize (hb_sanitize_context_t *c) const |
404 | 0 | { return sanitize_shallow (c); } |
405 | | |
406 | | bool subset (hb_subset_context_t *c) const |
407 | 0 | { |
408 | 0 | TRACE_SUBSET (this); |
409 | |
|
410 | 0 | gvar *out = c->serializer->allocate_min<gvar> (); |
411 | 0 | if (unlikely (!out)) return_trace (false); |
412 | |
|
413 | 0 | out->version.major = 1; |
414 | 0 | out->version.minor = 0; |
415 | 0 | out->axisCount = axisCount; |
416 | 0 | out->sharedTupleCount = sharedTupleCount; |
417 | |
|
418 | 0 | unsigned int num_glyphs = c->plan->num_output_glyphs (); |
419 | 0 | out->glyphCount = num_glyphs; |
420 | |
|
421 | 0 | unsigned int subset_data_size = 0; |
422 | 0 | for (hb_codepoint_t gid = (c->plan->flags & HB_SUBSET_FLAGS_NOTDEF_OUTLINE) ? 0 : 1; |
423 | 0 | gid < num_glyphs; |
424 | 0 | gid++) |
425 | 0 | { |
426 | 0 | hb_codepoint_t old_gid; |
427 | 0 | if (!c->plan->old_gid_for_new_gid (gid, &old_gid)) continue; |
428 | 0 | subset_data_size += get_glyph_var_data_bytes (c->source_blob, old_gid).length; |
429 | 0 | } |
430 | |
|
431 | 0 | bool long_offset = subset_data_size & ~0xFFFFu; |
432 | 0 | out->flags = long_offset ? 1 : 0; |
433 | |
|
434 | 0 | HBUINT8 *subset_offsets = c->serializer->allocate_size<HBUINT8> ((long_offset ? 4 : 2) * (num_glyphs + 1)); |
435 | 0 | if (!subset_offsets) return_trace (false); |
436 | | |
437 | | /* shared tuples */ |
438 | 0 | if (!sharedTupleCount || !sharedTuples) |
439 | 0 | out->sharedTuples = 0; |
440 | 0 | else |
441 | 0 | { |
442 | 0 | unsigned int shared_tuple_size = F2DOT14::static_size * axisCount * sharedTupleCount; |
443 | 0 | F2DOT14 *tuples = c->serializer->allocate_size<F2DOT14> (shared_tuple_size); |
444 | 0 | if (!tuples) return_trace (false); |
445 | 0 | out->sharedTuples = (char *) tuples - (char *) out; |
446 | 0 | memcpy (tuples, this+sharedTuples, shared_tuple_size); |
447 | 0 | } |
448 | |
|
449 | 0 | char *subset_data = c->serializer->allocate_size<char> (subset_data_size); |
450 | 0 | if (!subset_data) return_trace (false); |
451 | 0 | out->dataZ = subset_data - (char *) out; |
452 | |
|
453 | 0 | unsigned int glyph_offset = 0; |
454 | 0 | for (hb_codepoint_t gid = (c->plan->flags & HB_SUBSET_FLAGS_NOTDEF_OUTLINE) ? 0 : 1; |
455 | 0 | gid < num_glyphs; |
456 | 0 | gid++) |
457 | 0 | { |
458 | 0 | hb_codepoint_t old_gid; |
459 | 0 | hb_bytes_t var_data_bytes = c->plan->old_gid_for_new_gid (gid, &old_gid) |
460 | 0 | ? get_glyph_var_data_bytes (c->source_blob, old_gid) |
461 | 0 | : hb_bytes_t (); |
462 | |
|
463 | 0 | if (long_offset) |
464 | 0 | ((HBUINT32 *) subset_offsets)[gid] = glyph_offset; |
465 | 0 | else |
466 | 0 | ((HBUINT16 *) subset_offsets)[gid] = glyph_offset / 2; |
467 | |
|
468 | 0 | if (var_data_bytes.length > 0) |
469 | 0 | memcpy (subset_data, var_data_bytes.arrayZ, var_data_bytes.length); |
470 | 0 | subset_data += var_data_bytes.length; |
471 | 0 | glyph_offset += var_data_bytes.length; |
472 | 0 | } |
473 | 0 | if (long_offset) |
474 | 0 | ((HBUINT32 *) subset_offsets)[num_glyphs] = glyph_offset; |
475 | 0 | else |
476 | 0 | ((HBUINT16 *) subset_offsets)[num_glyphs] = glyph_offset / 2; |
477 | |
|
478 | 0 | return_trace (true); |
479 | 0 | } Unexecuted instantiation: OT::gvar::subset(hb_subset_context_t*) const Unexecuted instantiation: OT::gvar::subset(hb_subset_context_t*) const |
480 | | |
481 | | protected: |
482 | | const hb_bytes_t get_glyph_var_data_bytes (hb_blob_t *blob, hb_codepoint_t glyph) const |
483 | 0 | { |
484 | 0 | unsigned start_offset = get_offset (glyph); |
485 | 0 | unsigned length = get_offset (glyph+1) - start_offset; |
486 | 0 | hb_bytes_t var_data = blob->as_bytes ().sub_array (((unsigned) dataZ) + start_offset, length); |
487 | 0 | return likely (var_data.length >= GlyphVariationData::min_size) ? var_data : hb_bytes_t (); |
488 | 0 | } |
489 | | |
490 | 0 | bool is_long_offset () const { return flags & 1; } |
491 | | |
492 | | unsigned get_offset (unsigned i) const |
493 | 0 | { return is_long_offset () ? get_long_offset_array ()[i] : get_short_offset_array ()[i] * 2; } |
494 | | |
495 | 0 | const HBUINT32 * get_long_offset_array () const { return (const HBUINT32 *) &offsetZ; } |
496 | 0 | const HBUINT16 *get_short_offset_array () const { return (const HBUINT16 *) &offsetZ; } |
497 | | |
498 | | public: |
499 | | struct accelerator_t |
500 | | { |
501 | | void init (hb_face_t *face) |
502 | 0 | { table = hb_sanitize_context_t ().reference_table<gvar> (face); } |
503 | 0 | void fini () { table.destroy (); } |
504 | | |
505 | | private: |
506 | 0 | struct x_getter { static float get (const contour_point_t &p) { return p.x; } }; |
507 | 0 | struct y_getter { static float get (const contour_point_t &p) { return p.y; } }; |
508 | | |
509 | | template <typename T> |
510 | | static float infer_delta (const hb_array_t<contour_point_t> points, |
511 | | const hb_array_t<contour_point_t> deltas, |
512 | | unsigned int target, unsigned int prev, unsigned int next) |
513 | 0 | { |
514 | 0 | float target_val = T::get (points[target]); |
515 | 0 | float prev_val = T::get (points[prev]); |
516 | 0 | float next_val = T::get (points[next]); |
517 | 0 | float prev_delta = T::get (deltas[prev]); |
518 | 0 | float next_delta = T::get (deltas[next]); |
519 | |
|
520 | 0 | if (prev_val == next_val) |
521 | 0 | return (prev_delta == next_delta) ? prev_delta : 0.f; |
522 | 0 | else if (target_val <= hb_min (prev_val, next_val)) |
523 | 0 | return (prev_val < next_val) ? prev_delta : next_delta; |
524 | 0 | else if (target_val >= hb_max (prev_val, next_val)) |
525 | 0 | return (prev_val > next_val) ? prev_delta : next_delta; |
526 | | |
527 | | /* linear interpolation */ |
528 | 0 | float r = (target_val - prev_val) / (next_val - prev_val); |
529 | 0 | return (1.f - r) * prev_delta + r * next_delta; |
530 | 0 | } Unexecuted instantiation: float OT::gvar::accelerator_t::infer_delta<OT::gvar::accelerator_t::x_getter>(hb_array_t<OT::contour_point_t>, hb_array_t<OT::contour_point_t>, unsigned int, unsigned int, unsigned int) Unexecuted instantiation: float OT::gvar::accelerator_t::infer_delta<OT::gvar::accelerator_t::y_getter>(hb_array_t<OT::contour_point_t>, hb_array_t<OT::contour_point_t>, unsigned int, unsigned int, unsigned int) |
531 | | |
532 | | static unsigned int next_index (unsigned int i, unsigned int start, unsigned int end) |
533 | 0 | { return (i >= end) ? start : (i + 1); } |
534 | | |
535 | | public: |
536 | | bool apply_deltas_to_points (hb_codepoint_t glyph, hb_font_t *font, |
537 | | const hb_array_t<contour_point_t> points) const |
538 | 0 | { |
539 | | /* num_coords should exactly match gvar's axisCount due to how GlyphVariationData tuples are aligned */ |
540 | 0 | if (!font->num_coords || font->num_coords != table->axisCount) return true; |
541 | | |
542 | 0 | if (unlikely (glyph >= table->glyphCount)) return true; |
543 | | |
544 | 0 | hb_bytes_t var_data_bytes = table->get_glyph_var_data_bytes (table.get_blob (), glyph); |
545 | 0 | if (!var_data_bytes.as<GlyphVariationData> ()->has_data ()) return true; |
546 | 0 | hb_vector_t<unsigned int> shared_indices; |
547 | 0 | GlyphVariationData::tuple_iterator_t iterator; |
548 | 0 | if (!GlyphVariationData::get_tuple_iterator (var_data_bytes, table->axisCount, |
549 | 0 | shared_indices, &iterator)) |
550 | 0 | return true; /* so isn't applied at all */ |
551 | | |
552 | | /* Save original points for inferred delta calculation */ |
553 | 0 | contour_point_vector_t orig_points; |
554 | 0 | orig_points.resize (points.length); |
555 | 0 | for (unsigned int i = 0; i < orig_points.length; i++) |
556 | 0 | orig_points[i] = points[i]; |
557 | |
|
558 | 0 | contour_point_vector_t deltas; /* flag is used to indicate referenced point */ |
559 | 0 | deltas.resize (points.length); |
560 | |
|
561 | 0 | hb_vector_t<unsigned> end_points; |
562 | 0 | for (unsigned i = 0; i < points.length; ++i) |
563 | 0 | if (points[i].is_end_point) |
564 | 0 | end_points.push (i); |
565 | |
|
566 | 0 | int *coords = font->coords; |
567 | 0 | unsigned num_coords = font->num_coords; |
568 | 0 | hb_array_t<const F2DOT14> shared_tuples = (table+table->sharedTuples).as_array (table->sharedTupleCount * table->axisCount); |
569 | 0 | do |
570 | 0 | { |
571 | 0 | float scalar = iterator.current_tuple->calculate_scalar (coords, num_coords, shared_tuples); |
572 | 0 | if (scalar == 0.f) continue; |
573 | 0 | const HBUINT8 *p = iterator.get_serialized_data (); |
574 | 0 | unsigned int length = iterator.current_tuple->get_data_size (); |
575 | 0 | if (unlikely (!iterator.var_data_bytes.check_range (p, length))) |
576 | 0 | return false; |
577 | | |
578 | 0 | hb_bytes_t bytes ((const char *) p, length); |
579 | 0 | hb_vector_t<unsigned int> private_indices; |
580 | 0 | if (iterator.current_tuple->has_private_points () && |
581 | 0 | !GlyphVariationData::unpack_points (p, private_indices, bytes)) |
582 | 0 | return false; |
583 | 0 | const hb_array_t<unsigned int> &indices = private_indices.length ? private_indices : shared_indices; |
584 | |
|
585 | 0 | bool apply_to_all = (indices.length == 0); |
586 | 0 | unsigned int num_deltas = apply_to_all ? points.length : indices.length; |
587 | 0 | hb_vector_t<int> x_deltas; |
588 | 0 | x_deltas.resize (num_deltas); |
589 | 0 | if (!GlyphVariationData::unpack_deltas (p, x_deltas, bytes)) |
590 | 0 | return false; |
591 | 0 | hb_vector_t<int> y_deltas; |
592 | 0 | y_deltas.resize (num_deltas); |
593 | 0 | if (!GlyphVariationData::unpack_deltas (p, y_deltas, bytes)) |
594 | 0 | return false; |
595 | | |
596 | 0 | for (unsigned int i = 0; i < deltas.length; i++) |
597 | 0 | deltas[i].init (); |
598 | 0 | for (unsigned int i = 0; i < num_deltas; i++) |
599 | 0 | { |
600 | 0 | unsigned int pt_index = apply_to_all ? i : indices[i]; |
601 | 0 | deltas[pt_index].flag = 1; /* this point is referenced, i.e., explicit deltas specified */ |
602 | 0 | deltas[pt_index].x += x_deltas[i] * scalar; |
603 | 0 | deltas[pt_index].y += y_deltas[i] * scalar; |
604 | 0 | } |
605 | | |
606 | | /* infer deltas for unreferenced points */ |
607 | 0 | unsigned start_point = 0; |
608 | 0 | for (unsigned c = 0; c < end_points.length; c++) |
609 | 0 | { |
610 | 0 | unsigned end_point = end_points[c]; |
611 | | |
612 | | /* Check the number of unreferenced points in a contour. If no unref points or no ref points, nothing to do. */ |
613 | 0 | unsigned unref_count = 0; |
614 | 0 | for (unsigned i = start_point; i <= end_point; i++) |
615 | 0 | if (!deltas[i].flag) unref_count++; |
616 | |
|
617 | 0 | unsigned j = start_point; |
618 | 0 | if (unref_count == 0 || unref_count > end_point - start_point) |
619 | 0 | goto no_more_gaps; |
620 | | |
621 | 0 | for (;;) |
622 | 0 | { |
623 | | /* Locate the next gap of unreferenced points between two referenced points prev and next. |
624 | | * Note that a gap may wrap around at left (start_point) and/or at right (end_point). |
625 | | */ |
626 | 0 | unsigned int prev, next, i; |
627 | 0 | for (;;) |
628 | 0 | { |
629 | 0 | i = j; |
630 | 0 | j = next_index (i, start_point, end_point); |
631 | 0 | if (deltas[i].flag && !deltas[j].flag) break; |
632 | 0 | } |
633 | 0 | prev = j = i; |
634 | 0 | for (;;) |
635 | 0 | { |
636 | 0 | i = j; |
637 | 0 | j = next_index (i, start_point, end_point); |
638 | 0 | if (!deltas[i].flag && deltas[j].flag) break; |
639 | 0 | } |
640 | 0 | next = j; |
641 | | /* Infer deltas for all unref points in the gap between prev and next */ |
642 | 0 | i = prev; |
643 | 0 | for (;;) |
644 | 0 | { |
645 | 0 | i = next_index (i, start_point, end_point); |
646 | 0 | if (i == next) break; |
647 | 0 | deltas[i].x = infer_delta<x_getter> (orig_points.as_array (), deltas.as_array (), i, prev, next); |
648 | 0 | deltas[i].y = infer_delta<y_getter> (orig_points.as_array (), deltas.as_array (), i, prev, next); |
649 | 0 | if (--unref_count == 0) goto no_more_gaps; |
650 | 0 | } |
651 | 0 | } |
652 | 0 | no_more_gaps: |
653 | 0 | start_point = end_point + 1; |
654 | 0 | } |
655 | | |
656 | | /* apply specified / inferred deltas to points */ |
657 | 0 | for (unsigned int i = 0; i < points.length; i++) |
658 | 0 | { |
659 | 0 | points[i].x += deltas[i].x; |
660 | 0 | points[i].y += deltas[i].y; |
661 | 0 | } |
662 | 0 | } while (iterator.move_to_next ()); |
663 | |
|
664 | 0 | return true; |
665 | 0 | } |
666 | | |
667 | 0 | unsigned int get_axis_count () const { return table->axisCount; } |
668 | | |
669 | | private: |
670 | | hb_blob_ptr_t<gvar> table; |
671 | | }; |
672 | | |
673 | | protected: |
674 | | FixedVersion<>version; /* Version number of the glyph variations table |
675 | | * Set to 0x00010000u. */ |
676 | | HBUINT16 axisCount; /* The number of variation axes for this font. This must be |
677 | | * the same number as axisCount in the 'fvar' table. */ |
678 | | HBUINT16 sharedTupleCount; |
679 | | /* The number of shared tuple records. Shared tuple records |
680 | | * can be referenced within glyph variation data tables for |
681 | | * multiple glyphs, as opposed to other tuple records stored |
682 | | * directly within a glyph variation data table. */ |
683 | | NNOffset32To<UnsizedArrayOf<F2DOT14>> |
684 | | sharedTuples; /* Offset from the start of this table to the shared tuple records. |
685 | | * Array of tuple records shared across all glyph variation data tables. */ |
686 | | HBUINT16 glyphCount; /* The number of glyphs in this font. This must match the number of |
687 | | * glyphs stored elsewhere in the font. */ |
688 | | HBUINT16 flags; /* Bit-field that gives the format of the offset array that follows. |
689 | | * If bit 0 is clear, the offsets are uint16; if bit 0 is set, the |
690 | | * offsets are uint32. */ |
691 | | Offset32To<GlyphVariationData> |
692 | | dataZ; /* Offset from the start of this table to the array of |
693 | | * GlyphVariationData tables. */ |
694 | | UnsizedArrayOf<HBUINT8> |
695 | | offsetZ; /* Offsets from the start of the GlyphVariationData array |
696 | | * to each GlyphVariationData table. */ |
697 | | public: |
698 | | DEFINE_SIZE_MIN (20); |
699 | | }; |
700 | | |
701 | | struct gvar_accelerator_t : gvar::accelerator_t {}; |
702 | | |
703 | | } /* namespace OT */ |
704 | | |
705 | | #endif /* HB_OT_VAR_GVAR_TABLE_HH */ |