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);
75 AXIS_FLAG_HIDDEN = 0x0001,
78 bool sanitize (hb_sanitize_context_t *c) const
80 TRACE_SANITIZE (this);
81 return_trace (c->check_struct (this));
85 Tag axisTag; /* Tag identifying the design variation for the axis. */
86 HBFixed minValue; /* The minimum coordinate value for the axis. */
87 HBFixed defaultValue; /* The default coordinate value for the axis. */
88 HBFixed 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. */
94 DEFINE_SIZE_STATIC (20);
99 static constexpr hb_tag_t tableTag = HB_OT_TAG_fvar;
101 bool has_data () const { return version.to_int (); }
103 bool sanitize (hb_sanitize_context_t *c) const
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));
115 unsigned int get_axis_count () const { return axisCount; }
117 #ifndef HB_DISABLE_DEPRECATED
118 void get_axis_deprecated (unsigned int axis_index,
119 hb_ot_var_axis_t *info) const
121 const AxisRecord &axis = get_axes ()[axis_index];
122 info->tag = axis.axisTag;
123 info->name_id = axis.axisNameID;
124 info->default_value = axis.defaultValue / 65536.f;
125 /* Ensure order, to simplify client math. */
126 info->min_value = hb_min (info->default_value, axis.minValue / 65536.f);
127 info->max_value = hb_max (info->default_value, axis.maxValue / 65536.f);
131 void get_axis_info (unsigned int axis_index,
132 hb_ot_var_axis_info_t *info) const
134 const AxisRecord &axis = get_axes ()[axis_index];
135 info->axis_index = axis_index;
136 info->tag = axis.axisTag;
137 info->name_id = axis.axisNameID;
138 info->flags = (hb_ot_var_axis_flags_t) (unsigned int) axis.flags;
139 info->default_value = axis.defaultValue / 65536.f;
140 /* Ensure order, to simplify client math. */
141 info->min_value = hb_min (info->default_value, axis.minValue / 65536.f);
142 info->max_value = hb_max (info->default_value, axis.maxValue / 65536.f);
146 #ifndef HB_DISABLE_DEPRECATED
147 unsigned int get_axes_deprecated (unsigned int start_offset,
148 unsigned int *axes_count /* IN/OUT */,
149 hb_ot_var_axis_t *axes_array /* OUT */) const
153 /* TODO Rewrite as hb_array_t<>::sub-array() */
154 unsigned int count = axisCount;
155 start_offset = hb_min (start_offset, count);
157 count -= start_offset;
158 axes_array += start_offset;
160 count = hb_min (count, *axes_count);
163 for (unsigned int i = 0; i < count; i++)
164 get_axis_deprecated (start_offset + i, axes_array + i);
170 unsigned int get_axis_infos (unsigned int start_offset,
171 unsigned int *axes_count /* IN/OUT */,
172 hb_ot_var_axis_info_t *axes_array /* OUT */) const
176 /* TODO Rewrite as hb_array_t<>::sub-array() */
177 unsigned int count = axisCount;
178 start_offset = hb_min (start_offset, count);
180 count -= start_offset;
181 axes_array += start_offset;
183 count = hb_min (count, *axes_count);
186 for (unsigned int i = 0; i < count; i++)
187 get_axis_info (start_offset + i, axes_array + i);
192 #ifndef HB_DISABLE_DEPRECATED
193 bool find_axis_deprecated (hb_tag_t tag,
194 unsigned int *axis_index,
195 hb_ot_var_axis_t *info) const
197 const AxisRecord *axes = get_axes ();
198 unsigned int count = get_axis_count ();
199 for (unsigned int i = 0; i < count; i++)
200 if (axes[i].axisTag == tag)
204 get_axis_deprecated (i, info);
208 *axis_index = HB_OT_VAR_NO_AXIS_INDEX;
213 bool find_axis_info (hb_tag_t tag,
214 hb_ot_var_axis_info_t *info) const
216 const AxisRecord *axes = get_axes ();
217 unsigned int count = get_axis_count ();
218 for (unsigned int i = 0; i < count; i++)
219 if (axes[i].axisTag == tag)
221 get_axis_info (i, info);
227 int normalize_axis_value (unsigned int axis_index, float v) const
229 hb_ot_var_axis_info_t axis;
230 get_axis_info (axis_index, &axis);
232 v = hb_max (hb_min (v, axis.max_value), axis.min_value); /* Clamp. */
234 if (v == axis.default_value)
236 else if (v < axis.default_value)
237 v = (v - axis.default_value) / (axis.default_value - axis.min_value);
239 v = (v - axis.default_value) / (axis.max_value - axis.default_value);
240 return roundf (v * 16384.f);
243 float unnormalize_axis_value (unsigned int axis_index, float v) const
245 hb_ot_var_axis_info_t axis;
246 get_axis_info (axis_index, &axis);
249 return axis.default_value;
251 v = v * (axis.default_value - axis.min_value) / 16384.f + axis.default_value;
253 v = v * (axis.max_value - axis.default_value) / 16384.f + axis.default_value;
257 unsigned int get_instance_count () const { return instanceCount; }
259 hb_ot_name_id_t get_instance_subfamily_name_id (unsigned int instance_index) const
261 const InstanceRecord *instance = get_instance (instance_index);
262 if (unlikely (!instance)) return HB_OT_NAME_ID_INVALID;
263 return instance->subfamilyNameID;
266 hb_ot_name_id_t get_instance_postscript_name_id (unsigned int instance_index) const
268 const InstanceRecord *instance = get_instance (instance_index);
269 if (unlikely (!instance)) return HB_OT_NAME_ID_INVALID;
270 if (instanceSize >= axisCount * 4 + 6)
271 return StructAfter<NameID> (instance->get_coordinates (axisCount));
272 return HB_OT_NAME_ID_INVALID;
275 unsigned int get_instance_coords (unsigned int instance_index,
276 unsigned int *coords_length, /* IN/OUT */
277 float *coords /* OUT */) const
279 const InstanceRecord *instance = get_instance (instance_index);
280 if (unlikely (!instance))
287 if (coords_length && *coords_length)
289 hb_array_t<const HBFixed> instanceCoords = instance->get_coordinates (axisCount)
290 .sub_array (0, *coords_length);
291 for (unsigned int i = 0; i < instanceCoords.length; i++)
292 coords[i] = instanceCoords.arrayZ[i].to_float ();
297 void collect_name_ids (hb_set_t *nameids) const
299 if (!has_data ()) return;
302 | hb_map (&AxisRecord::axisNameID)
306 + hb_range ((unsigned) instanceCount)
307 | hb_map ([this] (const unsigned _) { return get_instance_subfamily_name_id (_); })
311 + hb_range ((unsigned) instanceCount)
312 | hb_map ([this] (const unsigned _) { return get_instance_postscript_name_id (_); })
319 hb_array_t<const AxisRecord> get_axes () const
320 { return hb_array (&(this+firstAxis), axisCount); }
322 const InstanceRecord *get_instance (unsigned int i) const
324 if (unlikely (i >= instanceCount)) return nullptr;
325 return &StructAtOffset<InstanceRecord> (&StructAfter<InstanceRecord> (get_axes ()),
330 FixedVersion<>version; /* Version of the fvar table
331 * initially set to 0x00010000u */
333 firstAxis; /* Offset in bytes from the beginning of the table
334 * to the start of the AxisRecord array. */
335 HBUINT16 reserved; /* This field is permanently reserved. Set to 2. */
336 HBUINT16 axisCount; /* The number of variation axes in the font (the
337 * number of records in the axes array). */
338 HBUINT16 axisSize; /* The size in bytes of each VariationAxisRecord —
339 * set to 20 (0x0014) for this version. */
340 HBUINT16 instanceCount; /* The number of named instances defined in the font
341 * (the number of records in the instances array). */
342 HBUINT16 instanceSize; /* The size in bytes of each InstanceRecord — set
343 * to either axisCount * sizeof(HBFixed) + 4, or to
344 * axisCount * sizeof(HBFixed) + 6. */
347 DEFINE_SIZE_STATIC (16);
353 #endif /* HB_OT_VAR_FVAR_TABLE_HH */