2 * Copyright © 2017 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Behdad Esfahbod
27 #ifndef HB_OT_VAR_FVAR_TABLE_HH
28 #define HB_OT_VAR_FVAR_TABLE_HH
30 #include "hb-open-type.hh"
33 * fvar -- Font Variations
34 * https://docs.microsoft.com/en-us/typography/opentype/spec/fvar
37 #define HB_OT_TAG_fvar HB_TAG('f','v','a','r')
47 hb_array_t<const HBFixed> get_coordinates (unsigned int axis_count) const
48 { return coordinatesZ.as_array (axis_count); }
50 bool sanitize (hb_sanitize_context_t *c, unsigned int axis_count) const
52 TRACE_SANITIZE (this);
53 return_trace (c->check_struct (this) &&
54 c->check_array (coordinatesZ.arrayZ, axis_count));
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<HBFixed>
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
68 DEFINE_SIZE_UNBOUNDED (4);
73 int cmp (hb_tag_t key) const { return axisTag.cmp (key); }
77 AXIS_FLAG_HIDDEN = 0x0001,
80 #ifndef HB_DISABLE_DEPRECATED
81 void get_axis_deprecated (hb_ot_var_axis_t *info) const
84 info->name_id = axisNameID;
85 get_coordinates (info->min_value, info->default_value, info->max_value);
89 void get_axis_info (unsigned axis_index, hb_ot_var_axis_info_t *info) const
91 info->axis_index = axis_index;
93 info->name_id = axisNameID;
94 info->flags = (hb_ot_var_axis_flags_t) (unsigned int) flags;
95 get_coordinates (info->min_value, info->default_value, info->max_value);
99 int normalize_axis_value (float v) const
101 float min_value, default_value, max_value;
102 get_coordinates (min_value, default_value, max_value);
104 v = hb_clamp (v, min_value, max_value);
106 if (v == default_value)
108 else if (v < default_value)
109 v = (v - default_value) / (default_value - min_value);
111 v = (v - default_value) / (max_value - default_value);
112 return roundf (v * 16384.f);
115 float unnormalize_axis_value (int v) const
117 float min_value, default_value, max_value;
118 get_coordinates (min_value, default_value, max_value);
121 return default_value;
123 return v * (default_value - min_value) / 16384.f + default_value;
125 return v * (max_value - default_value) / 16384.f + default_value;
128 hb_ot_name_id_t get_name_id () const { return axisNameID; }
130 bool sanitize (hb_sanitize_context_t *c) const
132 TRACE_SANITIZE (this);
133 return_trace (c->check_struct (this));
137 void get_coordinates (float &min, float &default_, float &max) const
139 default_ = defaultValue / 65536.f;
140 /* Ensure order, to simplify client math. */
141 min = hb_min (default_, minValue / 65536.f);
142 max = hb_max (default_, maxValue / 65536.f);
146 Tag axisTag; /* Tag identifying the design variation for the axis. */
148 HBFixed minValue; /* The minimum coordinate value for the axis. */
149 HBFixed defaultValue; /* The default coordinate value for the axis. */
150 HBFixed maxValue; /* The maximum coordinate value for the axis. */
152 HBUINT16 flags; /* Axis flags. */
153 NameID axisNameID; /* The name ID for entries in the 'name' table that
154 * provide a display name for this axis. */
157 DEFINE_SIZE_STATIC (20);
162 static constexpr hb_tag_t tableTag = HB_OT_TAG_fvar;
164 bool has_data () const { return version.to_int (); }
166 bool sanitize (hb_sanitize_context_t *c) const
168 TRACE_SANITIZE (this);
169 return_trace (version.sanitize (c) &&
170 likely (version.major == 1) &&
171 c->check_struct (this) &&
172 axisSize == 20 && /* Assumed in our code. */
173 instanceSize >= axisCount * 4 + 4 &&
174 get_axes ().sanitize (c) &&
175 c->check_range (get_instance (0), instanceCount, instanceSize));
178 unsigned int get_axis_count () const { return axisCount; }
180 #ifndef HB_DISABLE_DEPRECATED
181 unsigned int get_axes_deprecated (unsigned int start_offset,
182 unsigned int *axes_count /* IN/OUT */,
183 hb_ot_var_axis_t *axes_array /* OUT */) const
187 hb_array_t<const AxisRecord> arr = get_axes ().sub_array (start_offset, axes_count);
188 for (unsigned i = 0; i < arr.length; ++i)
189 arr[i].get_axis_deprecated (&axes_array[i]);
195 unsigned int get_axis_infos (unsigned int start_offset,
196 unsigned int *axes_count /* IN/OUT */,
197 hb_ot_var_axis_info_t *axes_array /* OUT */) const
201 hb_array_t<const AxisRecord> arr = get_axes ().sub_array (start_offset, axes_count);
202 for (unsigned i = 0; i < arr.length; ++i)
203 arr[i].get_axis_info (start_offset + i, &axes_array[i]);
208 #ifndef HB_DISABLE_DEPRECATED
210 find_axis_deprecated (hb_tag_t tag, unsigned *axis_index, hb_ot_var_axis_t *info) const
213 if (!axis_index) axis_index = &i;
214 *axis_index = HB_OT_VAR_NO_AXIS_INDEX;
215 auto axes = get_axes ();
216 return axes.lfind (tag, axis_index) && (axes[*axis_index].get_axis_deprecated (info), true);
220 find_axis_info (hb_tag_t tag, hb_ot_var_axis_info_t *info) const
223 auto axes = get_axes ();
224 return axes.lfind (tag, &i) && (axes[i].get_axis_info (i, info), true);
227 int normalize_axis_value (unsigned int axis_index, float v) const
228 { return get_axes ()[axis_index].normalize_axis_value (v); }
230 float unnormalize_axis_value (unsigned int axis_index, int v) const
231 { return get_axes ()[axis_index].unnormalize_axis_value (v); }
233 unsigned int get_instance_count () const { return instanceCount; }
235 hb_ot_name_id_t get_instance_subfamily_name_id (unsigned int instance_index) const
237 const InstanceRecord *instance = get_instance (instance_index);
238 if (unlikely (!instance)) return HB_OT_NAME_ID_INVALID;
239 return instance->subfamilyNameID;
242 hb_ot_name_id_t get_instance_postscript_name_id (unsigned int instance_index) const
244 const InstanceRecord *instance = get_instance (instance_index);
245 if (unlikely (!instance)) return HB_OT_NAME_ID_INVALID;
246 if (instanceSize >= axisCount * 4 + 6)
247 return StructAfter<NameID> (instance->get_coordinates (axisCount));
248 return HB_OT_NAME_ID_INVALID;
251 unsigned int get_instance_coords (unsigned int instance_index,
252 unsigned int *coords_length, /* IN/OUT */
253 float *coords /* OUT */) const
255 const InstanceRecord *instance = get_instance (instance_index);
256 if (unlikely (!instance))
263 if (coords_length && *coords_length)
265 hb_array_t<const HBFixed> instanceCoords = instance->get_coordinates (axisCount)
266 .sub_array (0, coords_length);
267 for (unsigned int i = 0; i < instanceCoords.length; i++)
268 coords[i] = instanceCoords.arrayZ[i].to_float ();
273 void collect_name_ids (hb_set_t *nameids) const
275 if (!has_data ()) return;
278 | hb_map (&AxisRecord::get_name_id)
282 + hb_range ((unsigned) instanceCount)
283 | hb_map ([this] (const unsigned _) { return get_instance_subfamily_name_id (_); })
287 + hb_range ((unsigned) instanceCount)
288 | hb_map ([this] (const unsigned _) { return get_instance_postscript_name_id (_); })
294 hb_array_t<const AxisRecord> get_axes () const
295 { return hb_array (&(this+firstAxis), axisCount); }
297 const InstanceRecord *get_instance (unsigned int i) const
299 if (unlikely (i >= instanceCount)) return nullptr;
300 return &StructAtOffset<InstanceRecord> (&StructAfter<InstanceRecord> (get_axes ()),
305 FixedVersion<>version; /* Version of the fvar table
306 * initially set to 0x00010000u */
307 Offset16To<AxisRecord>
308 firstAxis; /* Offset in bytes from the beginning of the table
309 * to the start of the AxisRecord array. */
310 HBUINT16 reserved; /* This field is permanently reserved. Set to 2. */
311 HBUINT16 axisCount; /* The number of variation axes in the font (the
312 * number of records in the axes array). */
313 HBUINT16 axisSize; /* The size in bytes of each VariationAxisRecord —
314 * set to 20 (0x0014) for this version. */
315 HBUINT16 instanceCount; /* The number of named instances defined in the font
316 * (the number of records in the instances array). */
317 HBUINT16 instanceSize; /* The size in bytes of each InstanceRecord — set
318 * to either axisCount * sizeof(HBFixed) + 4, or to
319 * axisCount * sizeof(HBFixed) + 6. */
322 DEFINE_SIZE_STATIC (16);
328 #endif /* HB_OT_VAR_FVAR_TABLE_HH */