/src/harfbuzz/src/hb-ot-var-fvar-table.hh
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright © 2017 Google, Inc. |
3 | | * |
4 | | * This is part of HarfBuzz, a text shaping library. |
5 | | * |
6 | | * Permission is hereby granted, without written agreement and without |
7 | | * license or royalty fees, to use, copy, modify, and distribute this |
8 | | * software and its documentation for any purpose, provided that the |
9 | | * above copyright notice and the following two paragraphs appear in |
10 | | * all copies of this software. |
11 | | * |
12 | | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
13 | | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
14 | | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
15 | | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
16 | | * DAMAGE. |
17 | | * |
18 | | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
19 | | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
20 | | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
21 | | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
22 | | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
23 | | * |
24 | | * Google Author(s): Behdad Esfahbod |
25 | | */ |
26 | | |
27 | | #ifndef HB_OT_VAR_FVAR_TABLE_HH |
28 | | #define HB_OT_VAR_FVAR_TABLE_HH |
29 | | |
30 | | #include "hb-open-type.hh" |
31 | | |
32 | | /* |
33 | | * fvar -- Font Variations |
34 | | * https://docs.microsoft.com/en-us/typography/opentype/spec/fvar |
35 | | */ |
36 | | |
37 | | #define HB_OT_TAG_fvar HB_TAG('f','v','a','r') |
38 | | |
39 | | |
40 | | namespace OT { |
41 | | |
42 | | |
43 | | struct InstanceRecord |
44 | | { |
45 | | friend struct fvar; |
46 | | |
47 | | hb_array_t<const F16DOT16> get_coordinates (unsigned int axis_count) const |
48 | 0 | { return coordinatesZ.as_array (axis_count); } |
49 | | |
50 | | bool subset (hb_subset_context_t *c, |
51 | | unsigned axis_count, |
52 | | bool has_postscript_nameid) const |
53 | 0 | { |
54 | 0 | TRACE_SUBSET (this); |
55 | 0 | if (unlikely (!c->serializer->embed (subfamilyNameID))) return_trace (false); |
56 | 0 | if (unlikely (!c->serializer->embed (flags))) return_trace (false); |
57 | 0 |
|
58 | 0 | const hb_array_t<const F16DOT16> coords = get_coordinates (axis_count); |
59 | 0 | const hb_hashmap_t<hb_tag_t, float> *axes_location = &c->plan->user_axes_location; |
60 | 0 | for (unsigned i = 0 ; i < axis_count; i++) |
61 | 0 | { |
62 | 0 | uint32_t *axis_tag; |
63 | 0 | // only keep instances whose coordinates == pinned axis location |
64 | 0 | if (!c->plan->axes_old_index_tag_map.has (i, &axis_tag)) continue; |
65 | 0 |
|
66 | 0 | if (axes_location->has (*axis_tag) && |
67 | 0 | fabsf (axes_location->get (*axis_tag) - coords[i].to_float ()) > 0.001f) |
68 | 0 | return_trace (false); |
69 | 0 |
|
70 | 0 | if (!c->plan->axes_index_map.has (i)) |
71 | 0 | continue; |
72 | 0 |
|
73 | 0 | if (!c->serializer->embed (coords[i])) |
74 | 0 | return_trace (false); |
75 | 0 | } |
76 | 0 |
|
77 | 0 | if (has_postscript_nameid) |
78 | 0 | { |
79 | 0 | NameID name_id; |
80 | 0 | name_id = StructAfter<NameID> (coords); |
81 | 0 | if (!c->serializer->embed (name_id)) |
82 | 0 | return_trace (false); |
83 | 0 | } |
84 | 0 |
|
85 | 0 | return_trace (true); |
86 | 0 | } |
87 | | |
88 | | bool sanitize (hb_sanitize_context_t *c, unsigned int axis_count) const |
89 | 0 | { |
90 | 0 | TRACE_SANITIZE (this); |
91 | 0 | return_trace (c->check_struct (this) && |
92 | 0 | c->check_array (coordinatesZ.arrayZ, axis_count)); |
93 | 0 | } |
94 | | |
95 | | protected: |
96 | | NameID subfamilyNameID;/* The name ID for entries in the 'name' table |
97 | | * that provide subfamily names for this instance. */ |
98 | | HBUINT16 flags; /* Reserved for future use — set to 0. */ |
99 | | UnsizedArrayOf<F16DOT16> |
100 | | coordinatesZ; /* The coordinates array for this instance. */ |
101 | | //NameID postScriptNameIDX;/*Optional. The name ID for entries in the 'name' |
102 | | // * table that provide PostScript names for this |
103 | | // * instance. */ |
104 | | |
105 | | public: |
106 | | DEFINE_SIZE_UNBOUNDED (4); |
107 | | }; |
108 | | |
109 | | struct AxisRecord |
110 | | { |
111 | 0 | int cmp (hb_tag_t key) const { return axisTag.cmp (key); } |
112 | | |
113 | | enum |
114 | | { |
115 | | AXIS_FLAG_HIDDEN = 0x0001, |
116 | | }; |
117 | | |
118 | | #ifndef HB_DISABLE_DEPRECATED |
119 | | void get_axis_deprecated (hb_ot_var_axis_t *info) const |
120 | 0 | { |
121 | 0 | info->tag = axisTag; |
122 | 0 | info->name_id = axisNameID; |
123 | 0 | get_coordinates (info->min_value, info->default_value, info->max_value); |
124 | 0 | } |
125 | | #endif |
126 | | |
127 | | void get_axis_info (unsigned axis_index, hb_ot_var_axis_info_t *info) const |
128 | 0 | { |
129 | 0 | info->axis_index = axis_index; |
130 | 0 | info->tag = axisTag; |
131 | 0 | info->name_id = axisNameID; |
132 | 0 | info->flags = (hb_ot_var_axis_flags_t) (unsigned int) flags; |
133 | 0 | get_coordinates (info->min_value, info->default_value, info->max_value); |
134 | 0 | info->reserved = 0; |
135 | 0 | } |
136 | | |
137 | 0 | hb_tag_t get_axis_tag () const { return axisTag; } |
138 | | |
139 | | int normalize_axis_value (float v) const |
140 | 0 | { |
141 | 0 | float min_value, default_value, max_value; |
142 | 0 | get_coordinates (min_value, default_value, max_value); |
143 | |
|
144 | 0 | v = hb_clamp (v, min_value, max_value); |
145 | |
|
146 | 0 | if (v == default_value) |
147 | 0 | return 0; |
148 | 0 | else if (v < default_value) |
149 | 0 | v = (v - default_value) / (default_value - min_value); |
150 | 0 | else |
151 | 0 | v = (v - default_value) / (max_value - default_value); |
152 | 0 | return roundf (v * 16384.f); |
153 | 0 | } |
154 | | |
155 | | float unnormalize_axis_value (int v) const |
156 | 33.9k | { |
157 | 33.9k | float min_value, default_value, max_value; |
158 | 33.9k | get_coordinates (min_value, default_value, max_value); |
159 | | |
160 | 33.9k | if (v == 0) |
161 | 152 | return default_value; |
162 | 33.8k | else if (v < 0) |
163 | 23.7k | return v * (default_value - min_value) / 16384.f + default_value; |
164 | 10.0k | else |
165 | 10.0k | return v * (max_value - default_value) / 16384.f + default_value; |
166 | 33.9k | } |
167 | | |
168 | 0 | hb_ot_name_id_t get_name_id () const { return axisNameID; } |
169 | | |
170 | | bool sanitize (hb_sanitize_context_t *c) const |
171 | 0 | { |
172 | 0 | TRACE_SANITIZE (this); |
173 | 0 | return_trace (c->check_struct (this)); |
174 | 0 | } |
175 | | |
176 | | void get_coordinates (float &min, float &default_, float &max) const |
177 | 33.9k | { |
178 | 33.9k | default_ = defaultValue.to_float (); |
179 | | /* Ensure order, to simplify client math. */ |
180 | 33.9k | min = hb_min (default_, minValue.to_float ()); |
181 | 33.9k | max = hb_max (default_, maxValue.to_float ()); |
182 | 33.9k | } |
183 | | |
184 | | float get_default () const |
185 | 0 | { |
186 | 0 | return defaultValue.to_float (); |
187 | 0 | } |
188 | | |
189 | | public: |
190 | | Tag axisTag; /* Tag identifying the design variation for the axis. */ |
191 | | protected: |
192 | | F16DOT16 minValue; /* The minimum coordinate value for the axis. */ |
193 | | F16DOT16 defaultValue; /* The default coordinate value for the axis. */ |
194 | | F16DOT16 maxValue; /* The maximum coordinate value for the axis. */ |
195 | | public: |
196 | | HBUINT16 flags; /* Axis flags. */ |
197 | | NameID axisNameID; /* The name ID for entries in the 'name' table that |
198 | | * provide a display name for this axis. */ |
199 | | |
200 | | public: |
201 | | DEFINE_SIZE_STATIC (20); |
202 | | }; |
203 | | |
204 | | struct fvar |
205 | | { |
206 | | static constexpr hb_tag_t tableTag = HB_OT_TAG_fvar; |
207 | | |
208 | 0 | bool has_data () const { return version.to_int (); } |
209 | | |
210 | | bool sanitize (hb_sanitize_context_t *c) const |
211 | 41.2k | { |
212 | 41.2k | TRACE_SANITIZE (this); |
213 | 41.2k | return_trace (version.sanitize (c) && |
214 | 41.2k | likely (version.major == 1) && |
215 | 41.2k | c->check_struct (this) && |
216 | 41.2k | axisSize == 20 && /* Assumed in our code. */ |
217 | 41.2k | instanceSize >= axisCount * 4 + 4 && |
218 | 41.2k | get_axes ().sanitize (c) && |
219 | 41.2k | c->check_range (get_instance (0), instanceCount, instanceSize)); |
220 | 41.2k | } |
221 | | |
222 | 1.26M | unsigned int get_axis_count () const { return axisCount; } |
223 | | |
224 | | #ifndef HB_DISABLE_DEPRECATED |
225 | | unsigned int get_axes_deprecated (unsigned int start_offset, |
226 | | unsigned int *axes_count /* IN/OUT */, |
227 | | hb_ot_var_axis_t *axes_array /* OUT */) const |
228 | 0 | { |
229 | 0 | if (axes_count) |
230 | 0 | { |
231 | 0 | hb_array_t<const AxisRecord> arr = get_axes ().sub_array (start_offset, axes_count); |
232 | 0 | for (unsigned i = 0; i < arr.length; ++i) |
233 | 0 | arr[i].get_axis_deprecated (&axes_array[i]); |
234 | 0 | } |
235 | 0 | return axisCount; |
236 | 0 | } |
237 | | #endif |
238 | | |
239 | | unsigned int get_axis_infos (unsigned int start_offset, |
240 | | unsigned int *axes_count /* IN/OUT */, |
241 | | hb_ot_var_axis_info_t *axes_array /* OUT */) const |
242 | 423k | { |
243 | 423k | if (axes_count) |
244 | 0 | { |
245 | 0 | hb_array_t<const AxisRecord> arr = get_axes ().sub_array (start_offset, axes_count); |
246 | 0 | for (unsigned i = 0; i < arr.length; ++i) |
247 | 0 | arr[i].get_axis_info (start_offset + i, &axes_array[i]); |
248 | 0 | } |
249 | 423k | return axisCount; |
250 | 423k | } |
251 | | |
252 | | #ifndef HB_DISABLE_DEPRECATED |
253 | | bool |
254 | | find_axis_deprecated (hb_tag_t tag, unsigned *axis_index, hb_ot_var_axis_t *info) const |
255 | 0 | { |
256 | 0 | unsigned i; |
257 | 0 | if (!axis_index) axis_index = &i; |
258 | 0 | *axis_index = HB_OT_VAR_NO_AXIS_INDEX; |
259 | 0 | auto axes = get_axes (); |
260 | 0 | return axes.lfind (tag, axis_index) && ((void) axes[*axis_index].get_axis_deprecated (info), true); |
261 | 0 | } |
262 | | #endif |
263 | | bool |
264 | | find_axis_info (hb_tag_t tag, hb_ot_var_axis_info_t *info) const |
265 | 0 | { |
266 | 0 | unsigned i; |
267 | 0 | auto axes = get_axes (); |
268 | 0 | return axes.lfind (tag, &i) && ((void) axes[i].get_axis_info (i, info), true); |
269 | 0 | } |
270 | | |
271 | | int normalize_axis_value (unsigned int axis_index, float v) const |
272 | 0 | { return get_axes ()[axis_index].normalize_axis_value (v); } |
273 | | |
274 | | float unnormalize_axis_value (unsigned int axis_index, int v) const |
275 | 33.9k | { return get_axes ()[axis_index].unnormalize_axis_value (v); } |
276 | | |
277 | 0 | unsigned int get_instance_count () const { return instanceCount; } |
278 | | |
279 | | hb_ot_name_id_t get_instance_subfamily_name_id (unsigned int instance_index) const |
280 | 0 | { |
281 | 0 | const InstanceRecord *instance = get_instance (instance_index); |
282 | 0 | if (unlikely (!instance)) return HB_OT_NAME_ID_INVALID; |
283 | 0 | return instance->subfamilyNameID; |
284 | 0 | } |
285 | | |
286 | | hb_ot_name_id_t get_instance_postscript_name_id (unsigned int instance_index) const |
287 | 0 | { |
288 | 0 | const InstanceRecord *instance = get_instance (instance_index); |
289 | 0 | if (unlikely (!instance)) return HB_OT_NAME_ID_INVALID; |
290 | 0 | if (instanceSize >= axisCount * 4 + 6) |
291 | 0 | return StructAfter<NameID> (instance->get_coordinates (axisCount)); |
292 | 0 | return HB_OT_NAME_ID_INVALID; |
293 | 0 | } |
294 | | |
295 | | unsigned int get_instance_coords (unsigned int instance_index, |
296 | | unsigned int *coords_length, /* IN/OUT */ |
297 | | float *coords /* OUT */) const |
298 | 0 | { |
299 | 0 | const InstanceRecord *instance = get_instance (instance_index); |
300 | 0 | if (unlikely (!instance)) |
301 | 0 | { |
302 | 0 | if (coords_length) |
303 | 0 | *coords_length = 0; |
304 | 0 | return 0; |
305 | 0 | } |
306 | | |
307 | 0 | if (coords_length && *coords_length) |
308 | 0 | { |
309 | 0 | hb_array_t<const F16DOT16> instanceCoords = instance->get_coordinates (axisCount) |
310 | 0 | .sub_array (0, coords_length); |
311 | 0 | for (unsigned int i = 0; i < instanceCoords.length; i++) |
312 | 0 | coords[i] = instanceCoords.arrayZ[i].to_float (); |
313 | 0 | } |
314 | 0 | return axisCount; |
315 | 0 | } |
316 | | |
317 | | void collect_name_ids (hb_hashmap_t<hb_tag_t, float> *user_axes_location, |
318 | | hb_set_t *nameids /* IN/OUT */) const |
319 | 0 | { |
320 | 0 | if (!has_data ()) return; |
321 | 0 | hb_map_t pinned_axes; |
322 | 0 |
|
323 | 0 | auto axis_records = get_axes (); |
324 | 0 | for (unsigned i = 0 ; i < (unsigned)axisCount; i++) |
325 | 0 | { |
326 | 0 | hb_tag_t axis_tag = axis_records[i].get_axis_tag (); |
327 | 0 | if (user_axes_location->has (axis_tag)) |
328 | 0 | { |
329 | 0 | pinned_axes.set (i, axis_tag); |
330 | 0 | continue; |
331 | 0 | } |
332 | 0 |
|
333 | 0 | nameids->add (axis_records[i].get_name_id ()); |
334 | 0 | } |
335 | 0 |
|
336 | 0 | for (unsigned i = 0 ; i < (unsigned)instanceCount; i++) |
337 | 0 | { |
338 | 0 | const InstanceRecord *instance = get_instance (i); |
339 | 0 |
|
340 | 0 | if (hb_any (+ hb_enumerate (instance->get_coordinates (axisCount)) |
341 | 0 | | hb_filter (pinned_axes, hb_first) |
342 | 0 | | hb_map ([&] (const hb_pair_t<unsigned, const F16DOT16&>& _) |
343 | 0 | { |
344 | 0 | hb_tag_t axis_tag = pinned_axes.get (_.first); |
345 | 0 | float location = user_axes_location->get (axis_tag); |
346 | 0 | if (fabs ((double)location - (double)_.second.to_float ()) > 0.001) return true; |
347 | 0 | return false; |
348 | 0 | }) |
349 | 0 | )) |
350 | 0 | continue; |
351 | 0 |
|
352 | 0 | nameids->add (instance->subfamilyNameID); |
353 | 0 |
|
354 | 0 | if (instanceSize >= axisCount * 4 + 6) |
355 | 0 | { |
356 | 0 | unsigned post_script_name_id = StructAfter<NameID> (instance->get_coordinates (axisCount)); |
357 | 0 | if (post_script_name_id != HB_OT_NAME_ID_INVALID) nameids->add (post_script_name_id); |
358 | 0 | } |
359 | 0 | } |
360 | 0 | } |
361 | | |
362 | | bool subset (hb_subset_context_t *c) const |
363 | 0 | { |
364 | 0 | TRACE_SUBSET (this); |
365 | 0 | unsigned retained_axis_count = c->plan->axes_index_map.get_population (); |
366 | 0 | if (!retained_axis_count) //all axes are pinned |
367 | 0 | return_trace (false); |
368 | 0 |
|
369 | 0 | fvar *out = c->serializer->embed (this); |
370 | 0 | if (unlikely (!out)) return_trace (false); |
371 | 0 |
|
372 | 0 | if (!c->serializer->check_assign (out->axisCount, retained_axis_count, HB_SERIALIZE_ERROR_INT_OVERFLOW)) |
373 | 0 | return_trace (false); |
374 | 0 |
|
375 | 0 | bool has_postscript_nameid = false; |
376 | 0 | if (instanceSize >= axisCount * 4 + 6) |
377 | 0 | has_postscript_nameid = true; |
378 | 0 |
|
379 | 0 | if (!c->serializer->check_assign (out->instanceSize, retained_axis_count * 4 + (has_postscript_nameid ? 6 : 4), |
380 | 0 | HB_SERIALIZE_ERROR_INT_OVERFLOW)) |
381 | 0 | return_trace (false); |
382 | 0 |
|
383 | 0 | auto axes_records = get_axes (); |
384 | 0 | for (unsigned i = 0 ; i < (unsigned)axisCount; i++) |
385 | 0 | { |
386 | 0 | if (!c->plan->axes_index_map.has (i)) continue; |
387 | 0 | if (unlikely (!c->serializer->embed (axes_records[i]))) |
388 | 0 | return_trace (false); |
389 | 0 | } |
390 | 0 |
|
391 | 0 | if (!c->serializer->check_assign (out->firstAxis, get_size (), HB_SERIALIZE_ERROR_INT_OVERFLOW)) |
392 | 0 | return_trace (false); |
393 | 0 |
|
394 | 0 | for (unsigned i = 0 ; i < (unsigned)instanceCount; i++) |
395 | 0 | { |
396 | 0 | const InstanceRecord *instance = get_instance (i); |
397 | 0 | auto snap = c->serializer->snapshot (); |
398 | 0 | if (!instance->subset (c, axisCount, has_postscript_nameid)) |
399 | 0 | c->serializer->revert (snap); |
400 | 0 | } |
401 | 0 | return_trace (true); |
402 | 0 | } |
403 | | |
404 | | public: |
405 | | hb_array_t<const AxisRecord> get_axes () const |
406 | 86.5k | { return hb_array (&(this+firstAxis), axisCount); } |
407 | | |
408 | | const InstanceRecord *get_instance (unsigned int i) const |
409 | 27.5k | { |
410 | 27.5k | if (unlikely (i >= instanceCount)) return nullptr; |
411 | 24.9k | return &StructAtOffset<InstanceRecord> (&StructAfter<InstanceRecord> (get_axes ()), |
412 | 24.9k | i * instanceSize); |
413 | 27.5k | } |
414 | | |
415 | | protected: |
416 | | FixedVersion<>version; /* Version of the fvar table |
417 | | * initially set to 0x00010000u */ |
418 | | Offset16To<AxisRecord> |
419 | | firstAxis; /* Offset in bytes from the beginning of the table |
420 | | * to the start of the AxisRecord array. */ |
421 | | HBUINT16 reserved; /* This field is permanently reserved. Set to 2. */ |
422 | | HBUINT16 axisCount; /* The number of variation axes in the font (the |
423 | | * number of records in the axes array). */ |
424 | | HBUINT16 axisSize; /* The size in bytes of each VariationAxisRecord — |
425 | | * set to 20 (0x0014) for this version. */ |
426 | | HBUINT16 instanceCount; /* The number of named instances defined in the font |
427 | | * (the number of records in the instances array). */ |
428 | | HBUINT16 instanceSize; /* The size in bytes of each InstanceRecord — set |
429 | | * to either axisCount * sizeof(F16DOT16) + 4, or to |
430 | | * axisCount * sizeof(F16DOT16) + 6. */ |
431 | | |
432 | | public: |
433 | | DEFINE_SIZE_STATIC (16); |
434 | | }; |
435 | | |
436 | | } /* namespace OT */ |
437 | | |
438 | | |
439 | | #endif /* HB_OT_VAR_FVAR_TABLE_HH */ |