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 
get_coordinatesOT::InstanceRecord47   hb_array_t<const HBFixed> get_coordinates (unsigned int axis_count) const
48   { return coordinatesZ.as_array (axis_count); }
49 
sanitizeOT::InstanceRecord50   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<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
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 
sanitizeOT::AxisRecord78   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   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. */
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 
has_dataOT::fvar101   bool has_data () const { return version.to_int (); }
102 
sanitizeOT::fvar103   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 
get_axis_countOT::fvar115   unsigned int get_axis_count () const { return axisCount; }
116 
117 #ifndef HB_DISABLE_DEPRECATED
get_axis_deprecatedOT::fvar118   void get_axis_deprecated (unsigned int axis_index,
119 				   hb_ot_var_axis_t *info) const
120   {
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);
128   }
129 #endif
130 
get_axis_infoOT::fvar131   void get_axis_info (unsigned int axis_index,
132 		      hb_ot_var_axis_info_t *info) const
133   {
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);
143     info->reserved = 0;
144   }
145 
146 #ifndef HB_DISABLE_DEPRECATED
get_axes_deprecatedOT::fvar147   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
150   {
151     if (axes_count)
152     {
153       hb_array_t<const AxisRecord> arr = hb_array (&(this+firstAxis), axisCount).sub_array (start_offset, axes_count);
154       for (unsigned i = 0; i < arr.length; ++i)
155 	get_axis_deprecated (start_offset + i, axes_array + i);
156     }
157     return axisCount;
158   }
159 #endif
160 
get_axis_infosOT::fvar161   unsigned int get_axis_infos (unsigned int           start_offset,
162 			       unsigned int          *axes_count /* IN/OUT */,
163 			       hb_ot_var_axis_info_t *axes_array /* OUT */) const
164   {
165     if (axes_count)
166     {
167       hb_array_t<const AxisRecord> arr = hb_array (&(this+firstAxis), axisCount).sub_array (start_offset, axes_count);
168       for (unsigned i = 0; i < arr.length; ++i)
169 	get_axis_info (start_offset + i, axes_array + i);
170     }
171     return axisCount;
172   }
173 
174 #ifndef HB_DISABLE_DEPRECATED
find_axis_deprecatedOT::fvar175   bool find_axis_deprecated (hb_tag_t tag,
176 			     unsigned int *axis_index,
177 			     hb_ot_var_axis_t *info) const
178   {
179     const AxisRecord *axes = get_axes ();
180     unsigned int count = get_axis_count ();
181     for (unsigned int i = 0; i < count; i++)
182       if (axes[i].axisTag == tag)
183       {
184 	if (axis_index)
185 	  *axis_index = i;
186 	get_axis_deprecated (i, info);
187 	return true;
188       }
189     if (axis_index)
190       *axis_index = HB_OT_VAR_NO_AXIS_INDEX;
191     return false;
192   }
193 #endif
194 
find_axis_infoOT::fvar195   bool find_axis_info (hb_tag_t tag,
196 		       hb_ot_var_axis_info_t *info) const
197   {
198     const AxisRecord *axes = get_axes ();
199     unsigned int count = get_axis_count ();
200     for (unsigned int i = 0; i < count; i++)
201       if (axes[i].axisTag == tag)
202       {
203 	get_axis_info (i, info);
204 	return true;
205       }
206     return false;
207   }
208 
normalize_axis_valueOT::fvar209   int normalize_axis_value (unsigned int axis_index, float v) const
210   {
211     hb_ot_var_axis_info_t axis;
212     get_axis_info (axis_index, &axis);
213 
214     v = hb_clamp (v, axis.min_value, axis.max_value);
215 
216     if (v == axis.default_value)
217       return 0;
218     else if (v < axis.default_value)
219       v = (v - axis.default_value) / (axis.default_value - axis.min_value);
220     else
221       v = (v - axis.default_value) / (axis.max_value - axis.default_value);
222     return roundf (v * 16384.f);
223   }
224 
unnormalize_axis_valueOT::fvar225   float unnormalize_axis_value (unsigned int axis_index, float v) const
226   {
227     hb_ot_var_axis_info_t axis;
228     get_axis_info (axis_index, &axis);
229 
230     if (v == 0)
231       return axis.default_value;
232     else if (v < 0)
233       v = v * (axis.default_value - axis.min_value) / 16384.f + axis.default_value;
234     else
235       v = v * (axis.max_value - axis.default_value) / 16384.f + axis.default_value;
236     return v;
237   }
238 
get_instance_countOT::fvar239   unsigned int get_instance_count () const { return instanceCount; }
240 
get_instance_subfamily_name_idOT::fvar241   hb_ot_name_id_t get_instance_subfamily_name_id (unsigned int instance_index) const
242   {
243     const InstanceRecord *instance = get_instance (instance_index);
244     if (unlikely (!instance)) return HB_OT_NAME_ID_INVALID;
245     return instance->subfamilyNameID;
246   }
247 
get_instance_postscript_name_idOT::fvar248   hb_ot_name_id_t get_instance_postscript_name_id (unsigned int instance_index) const
249   {
250     const InstanceRecord *instance = get_instance (instance_index);
251     if (unlikely (!instance)) return HB_OT_NAME_ID_INVALID;
252     if (instanceSize >= axisCount * 4 + 6)
253       return StructAfter<NameID> (instance->get_coordinates (axisCount));
254     return HB_OT_NAME_ID_INVALID;
255   }
256 
get_instance_coordsOT::fvar257   unsigned int get_instance_coords (unsigned int  instance_index,
258 				    unsigned int *coords_length, /* IN/OUT */
259 				    float        *coords         /* OUT */) const
260   {
261     const InstanceRecord *instance = get_instance (instance_index);
262     if (unlikely (!instance))
263     {
264       if (coords_length)
265 	*coords_length = 0;
266       return 0;
267     }
268 
269     if (coords_length && *coords_length)
270     {
271       hb_array_t<const HBFixed> instanceCoords = instance->get_coordinates (axisCount)
272 							 .sub_array (0, *coords_length);
273       for (unsigned int i = 0; i < instanceCoords.length; i++)
274 	coords[i] = instanceCoords.arrayZ[i].to_float ();
275     }
276     return axisCount;
277   }
278 
collect_name_idsOT::fvar279   void collect_name_ids (hb_set_t *nameids) const
280   {
281     if (!has_data ()) return;
282 
283     + get_axes ()
284     | hb_map (&AxisRecord::axisNameID)
285     | hb_sink (nameids)
286     ;
287 
288     + hb_range ((unsigned) instanceCount)
289     | hb_map ([this] (const unsigned _) { return get_instance_subfamily_name_id (_); })
290     | hb_sink (nameids)
291     ;
292 
293     + hb_range ((unsigned) instanceCount)
294     | hb_map ([this] (const unsigned _) { return get_instance_postscript_name_id (_); })
295     | hb_sink (nameids)
296     ;
297   }
298 
299 
300   protected:
get_axesOT::fvar301   hb_array_t<const AxisRecord> get_axes () const
302   { return hb_array (&(this+firstAxis), axisCount); }
303 
get_instanceOT::fvar304   const InstanceRecord *get_instance (unsigned int i) const
305   {
306     if (unlikely (i >= instanceCount)) return nullptr;
307    return &StructAtOffset<InstanceRecord> (&StructAfter<InstanceRecord> (get_axes ()),
308 					   i * instanceSize);
309   }
310 
311   protected:
312   FixedVersion<>version;	/* Version of the fvar table
313 				 * initially set to 0x00010000u */
314   OffsetTo<AxisRecord>
315 		firstAxis;	/* Offset in bytes from the beginning of the table
316 				 * to the start of the AxisRecord array. */
317   HBUINT16	reserved;	/* This field is permanently reserved. Set to 2. */
318   HBUINT16	axisCount;	/* The number of variation axes in the font (the
319 				 * number of records in the axes array). */
320   HBUINT16	axisSize;	/* The size in bytes of each VariationAxisRecord —
321 				 * set to 20 (0x0014) for this version. */
322   HBUINT16	instanceCount;	/* The number of named instances defined in the font
323 				 * (the number of records in the instances array). */
324   HBUINT16	instanceSize;	/* The size in bytes of each InstanceRecord — set
325 				 * to either axisCount * sizeof(HBFixed) + 4, or to
326 				 * axisCount * sizeof(HBFixed) + 6. */
327 
328   public:
329   DEFINE_SIZE_STATIC (16);
330 };
331 
332 } /* namespace OT */
333 
334 
335 #endif /* HB_OT_VAR_FVAR_TABLE_HH */
336