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 Fixed> get_coordinates (unsigned int axis_count) const |
48 | { return coordinatesZ.as_array (axis_count); } |
49 | |
50 | bool sanitize (hb_sanitize_context_t *c, unsigned int axis_count) const |
51 | { |
52 | TRACE_SANITIZE (this); |
53 | return_trace (c->check_struct (this) && |
54 | c->check_array (coordinatesZ.arrayZ, axis_count)); |
55 | } |
56 | |
57 | protected: |
58 | NameID subfamilyNameID;/* The name ID for entries in the 'name' table |
59 | * that provide subfamily names for this instance. */ |
60 | HBUINT16 flags; /* Reserved for future use — set to 0. */ |
61 | UnsizedArrayOf<Fixed> |
62 | coordinatesZ; /* The coordinates array for this instance. */ |
63 | //NameID postScriptNameIDX;/*Optional. The name ID for entries in the 'name' |
64 | // * table that provide PostScript names for this |
65 | // * instance. */ |
66 | |
67 | public: |
68 | DEFINE_SIZE_UNBOUNDED (4); |
69 | }; |
70 | |
71 | struct AxisRecord |
72 | { |
73 | enum |
74 | { |
75 | AXIS_FLAG_HIDDEN = 0x0001, |
76 | }; |
77 | |
78 | bool sanitize (hb_sanitize_context_t *c) const |
79 | { |
80 | TRACE_SANITIZE (this); |
81 | return_trace (c->check_struct (this)); |
82 | } |
83 | |
84 | public: |
85 | Tag axisTag; /* Tag identifying the design variation for the axis. */ |
86 | Fixed minValue; /* The minimum coordinate value for the axis. */ |
87 | Fixed defaultValue; /* The default coordinate value for the axis. */ |
88 | Fixed maxValue; /* The maximum coordinate value for the axis. */ |
89 | HBUINT16 flags; /* Axis flags. */ |
90 | NameID axisNameID; /* The name ID for entries in the 'name' table that |
91 | * provide a display name for this axis. */ |
92 | |
93 | public: |
94 | DEFINE_SIZE_STATIC (20); |
95 | }; |
96 | |
97 | struct fvar |
98 | { |
99 | static constexpr hb_tag_t tableTag = HB_OT_TAG_fvar; |
100 | |
101 | bool has_data () const { return version.to_int (); } |
102 | |
103 | bool sanitize (hb_sanitize_context_t *c) const |
104 | { |
105 | TRACE_SANITIZE (this); |
106 | return_trace (version.sanitize (c) && |
107 | likely (version.major == 1) && |
108 | c->check_struct (this) && |
109 | axisSize == 20 && /* Assumed in our code. */ |
110 | instanceSize >= axisCount * 4 + 4 && |
111 | get_axes ().sanitize (c) && |
112 | c->check_range (get_instance (0), instanceCount, instanceSize)); |
113 | } |
114 | |
115 | unsigned int get_axis_count () const { return axisCount; } |
116 | |
117 | void get_axis_deprecated (unsigned int axis_index, |
118 | hb_ot_var_axis_t *info) const |
119 | { |
120 | const AxisRecord &axis = get_axes ()[axis_index]; |
121 | info->tag = axis.axisTag; |
122 | info->name_id = axis.axisNameID; |
123 | info->default_value = axis.defaultValue / 65536.; |
124 | /* Ensure order, to simplify client math. */ |
125 | info->min_value = MIN<float> (info->default_value, axis.minValue / 65536.); |
126 | info->max_value = MAX<float> (info->default_value, axis.maxValue / 65536.); |
127 | } |
128 | |
129 | void get_axis_info (unsigned int axis_index, |
130 | hb_ot_var_axis_info_t *info) const |
131 | { |
132 | const AxisRecord &axis = get_axes ()[axis_index]; |
133 | info->axis_index = axis_index; |
134 | info->tag = axis.axisTag; |
135 | info->name_id = axis.axisNameID; |
136 | info->flags = (hb_ot_var_axis_flags_t) (unsigned int) axis.flags; |
137 | info->default_value = axis.defaultValue / 65536.; |
138 | /* Ensure order, to simplify client math. */ |
139 | info->min_value = MIN<float> (info->default_value, axis.minValue / 65536.); |
140 | info->max_value = MAX<float> (info->default_value, axis.maxValue / 65536.); |
141 | info->reserved = 0; |
142 | } |
143 | |
144 | unsigned int get_axes_deprecated (unsigned int start_offset, |
145 | unsigned int *axes_count /* IN/OUT */, |
146 | hb_ot_var_axis_t *axes_array /* OUT */) const |
147 | { |
148 | if (axes_count) |
149 | { |
150 | /* TODO Rewrite as hb_array_t<>::sub-array() */ |
151 | unsigned int count = axisCount; |
152 | start_offset = MIN (start_offset, count); |
153 | |
154 | count -= start_offset; |
155 | axes_array += start_offset; |
156 | |
157 | count = MIN (count, *axes_count); |
158 | *axes_count = count; |
159 | |
160 | for (unsigned int i = 0; i < count; i++) |
161 | get_axis_deprecated (start_offset + i, axes_array + i); |
162 | } |
163 | return axisCount; |
164 | } |
165 | |
166 | unsigned int get_axis_infos (unsigned int start_offset, |
167 | unsigned int *axes_count /* IN/OUT */, |
168 | hb_ot_var_axis_info_t *axes_array /* OUT */) const |
169 | { |
170 | if (axes_count) |
171 | { |
172 | /* TODO Rewrite as hb_array_t<>::sub-array() */ |
173 | unsigned int count = axisCount; |
174 | start_offset = MIN (start_offset, count); |
175 | |
176 | count -= start_offset; |
177 | axes_array += start_offset; |
178 | |
179 | count = MIN (count, *axes_count); |
180 | *axes_count = count; |
181 | |
182 | for (unsigned int i = 0; i < count; i++) |
183 | get_axis_info (start_offset + i, axes_array + i); |
184 | } |
185 | return axisCount; |
186 | } |
187 | |
188 | bool find_axis_deprecated (hb_tag_t tag, |
189 | unsigned int *axis_index, |
190 | hb_ot_var_axis_t *info) const |
191 | { |
192 | const AxisRecord *axes = get_axes (); |
193 | unsigned int count = get_axis_count (); |
194 | for (unsigned int i = 0; i < count; i++) |
195 | if (axes[i].axisTag == tag) |
196 | { |
197 | if (axis_index) |
198 | *axis_index = i; |
199 | get_axis_deprecated (i, info); |
200 | return true; |
201 | } |
202 | if (axis_index) |
203 | *axis_index = HB_OT_VAR_NO_AXIS_INDEX; |
204 | return false; |
205 | } |
206 | |
207 | bool find_axis_info (hb_tag_t tag, |
208 | hb_ot_var_axis_info_t *info) const |
209 | { |
210 | const AxisRecord *axes = get_axes (); |
211 | unsigned int count = get_axis_count (); |
212 | for (unsigned int i = 0; i < count; i++) |
213 | if (axes[i].axisTag == tag) |
214 | { |
215 | get_axis_info (i, info); |
216 | return true; |
217 | } |
218 | return false; |
219 | } |
220 | |
221 | int normalize_axis_value (unsigned int axis_index, float v) const |
222 | { |
223 | hb_ot_var_axis_info_t axis; |
224 | get_axis_info (axis_index, &axis); |
225 | |
226 | v = MAX (MIN (v, axis.max_value), axis.min_value); /* Clamp. */ |
227 | |
228 | if (v == axis.default_value) |
229 | return 0; |
230 | else if (v < axis.default_value) |
231 | v = (v - axis.default_value) / (axis.default_value - axis.min_value); |
232 | else |
233 | v = (v - axis.default_value) / (axis.max_value - axis.default_value); |
234 | return (int) (v * 16384.f + (v >= 0.f ? .5f : -.5f)); |
235 | } |
236 | |
237 | unsigned int get_instance_count () const { return instanceCount; } |
238 | |
239 | hb_ot_name_id_t get_instance_subfamily_name_id (unsigned int instance_index) const |
240 | { |
241 | const InstanceRecord *instance = get_instance (instance_index); |
242 | if (unlikely (!instance)) return HB_OT_NAME_ID_INVALID; |
243 | return instance->subfamilyNameID; |
244 | } |
245 | |
246 | hb_ot_name_id_t get_instance_postscript_name_id (unsigned int instance_index) const |
247 | { |
248 | const InstanceRecord *instance = get_instance (instance_index); |
249 | if (unlikely (!instance)) return HB_OT_NAME_ID_INVALID; |
250 | if (instanceSize >= axisCount * 4 + 6) |
251 | return StructAfter<NameID> (instance->get_coordinates (axisCount)); |
252 | return HB_OT_NAME_ID_INVALID; |
253 | } |
254 | |
255 | unsigned int get_instance_coords (unsigned int instance_index, |
256 | unsigned int *coords_length, /* IN/OUT */ |
257 | float *coords /* OUT */) const |
258 | { |
259 | const InstanceRecord *instance = get_instance (instance_index); |
260 | if (unlikely (!instance)) |
261 | { |
262 | if (coords_length) |
263 | *coords_length = 0; |
264 | return 0; |
265 | } |
266 | |
267 | if (coords_length && *coords_length) |
268 | { |
269 | hb_array_t<const Fixed> instanceCoords = instance->get_coordinates (axisCount) |
270 | .sub_array (0, *coords_length); |
271 | for (unsigned int i = 0; i < instanceCoords.length; i++) |
272 | coords[i] = instanceCoords.arrayZ[i].to_float (); |
273 | } |
274 | return axisCount; |
275 | } |
276 | |
277 | protected: |
278 | hb_array_t<const AxisRecord> get_axes () const |
279 | { return hb_array (&(this+firstAxis), axisCount); } |
280 | |
281 | const InstanceRecord *get_instance (unsigned int i) const |
282 | { |
283 | if (unlikely (i >= instanceCount)) return nullptr; |
284 | return &StructAtOffset<InstanceRecord> (&StructAfter<InstanceRecord> (get_axes ()), |
285 | i * instanceSize); |
286 | } |
287 | |
288 | protected: |
289 | FixedVersion<>version; /* Version of the fvar table |
290 | * initially set to 0x00010000u */ |
291 | OffsetTo<AxisRecord> |
292 | firstAxis; /* Offset in bytes from the beginning of the table |
293 | * to the start of the AxisRecord array. */ |
294 | HBUINT16 reserved; /* This field is permanently reserved. Set to 2. */ |
295 | HBUINT16 axisCount; /* The number of variation axes in the font (the |
296 | * number of records in the axes array). */ |
297 | HBUINT16 axisSize; /* The size in bytes of each VariationAxisRecord — |
298 | * set to 20 (0x0014) for this version. */ |
299 | HBUINT16 instanceCount; /* The number of named instances defined in the font |
300 | * (the number of records in the instances array). */ |
301 | HBUINT16 instanceSize; /* The size in bytes of each InstanceRecord — set |
302 | * to either axisCount * sizeof(Fixed) + 4, or to |
303 | * axisCount * sizeof(Fixed) + 6. */ |
304 | |
305 | public: |
306 | DEFINE_SIZE_STATIC (16); |
307 | }; |
308 | |
309 | } /* namespace OT */ |
310 | |
311 | |
312 | #endif /* HB_OT_VAR_FVAR_TABLE_HH */ |
313 | |