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 { cmpOT::AxisRecord73 int cmp (hb_tag_t key) const { return axisTag.cmp (key); } 74 75 enum 76 { 77 AXIS_FLAG_HIDDEN = 0x0001, 78 }; 79 80 #ifndef HB_DISABLE_DEPRECATED get_axis_deprecatedOT::AxisRecord81 void get_axis_deprecated (hb_ot_var_axis_t *info) const 82 { 83 info->tag = axisTag; 84 info->name_id = axisNameID; 85 get_coordinates (info->min_value, info->default_value, info->max_value); 86 } 87 #endif 88 get_axis_infoOT::AxisRecord89 void get_axis_info (unsigned axis_index, hb_ot_var_axis_info_t *info) const 90 { 91 info->axis_index = axis_index; 92 info->tag = axisTag; 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); 96 info->reserved = 0; 97 } 98 normalize_axis_valueOT::AxisRecord99 int normalize_axis_value (float v) const 100 { 101 float min_value, default_value, max_value; 102 get_coordinates (min_value, default_value, max_value); 103 104 v = hb_clamp (v, min_value, max_value); 105 106 if (v == default_value) 107 return 0; 108 else if (v < default_value) 109 v = (v - default_value) / (default_value - min_value); 110 else 111 v = (v - default_value) / (max_value - default_value); 112 return roundf (v * 16384.f); 113 } 114 unnormalize_axis_valueOT::AxisRecord115 float unnormalize_axis_value (int v) const 116 { 117 float min_value, default_value, max_value; 118 get_coordinates (min_value, default_value, max_value); 119 120 if (v == 0) 121 return default_value; 122 else if (v < 0) 123 return v * (default_value - min_value) / 16384.f + default_value; 124 else 125 return v * (max_value - default_value) / 16384.f + default_value; 126 } 127 get_name_idOT::AxisRecord128 hb_ot_name_id_t get_name_id () const { return axisNameID; } 129 sanitizeOT::AxisRecord130 bool sanitize (hb_sanitize_context_t *c) const 131 { 132 TRACE_SANITIZE (this); 133 return_trace (c->check_struct (this)); 134 } 135 136 protected: get_coordinatesOT::AxisRecord137 void get_coordinates (float &min, float &default_, float &max) const 138 { 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); 143 } 144 145 public: 146 Tag axisTag; /* Tag identifying the design variation for the axis. */ 147 protected: 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. */ 151 public: 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. */ 155 156 public: 157 DEFINE_SIZE_STATIC (20); 158 }; 159 160 struct fvar 161 { 162 static constexpr hb_tag_t tableTag = HB_OT_TAG_fvar; 163 has_dataOT::fvar164 bool has_data () const { return version.to_int (); } 165 sanitizeOT::fvar166 bool sanitize (hb_sanitize_context_t *c) const 167 { 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)); 176 } 177 get_axis_countOT::fvar178 unsigned int get_axis_count () const { return axisCount; } 179 180 #ifndef HB_DISABLE_DEPRECATED get_axes_deprecatedOT::fvar181 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 184 { 185 if (axes_count) 186 { 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]); 190 } 191 return axisCount; 192 } 193 #endif 194 get_axis_infosOT::fvar195 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 198 { 199 if (axes_count) 200 { 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]); 204 } 205 return axisCount; 206 } 207 208 #ifndef HB_DISABLE_DEPRECATED 209 bool find_axis_deprecatedOT::fvar210 find_axis_deprecated (hb_tag_t tag, unsigned *axis_index, hb_ot_var_axis_t *info) const 211 { 212 unsigned i; 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); 217 } 218 #endif 219 bool find_axis_infoOT::fvar220 find_axis_info (hb_tag_t tag, hb_ot_var_axis_info_t *info) const 221 { 222 unsigned i; 223 auto axes = get_axes (); 224 return axes.lfind (tag, &i) && (axes[i].get_axis_info (i, info), true); 225 } 226 normalize_axis_valueOT::fvar227 int normalize_axis_value (unsigned int axis_index, float v) const 228 { return get_axes ()[axis_index].normalize_axis_value (v); } 229 unnormalize_axis_valueOT::fvar230 float unnormalize_axis_value (unsigned int axis_index, int v) const 231 { return get_axes ()[axis_index].unnormalize_axis_value (v); } 232 get_instance_countOT::fvar233 unsigned int get_instance_count () const { return instanceCount; } 234 get_instance_subfamily_name_idOT::fvar235 hb_ot_name_id_t get_instance_subfamily_name_id (unsigned int instance_index) const 236 { 237 const InstanceRecord *instance = get_instance (instance_index); 238 if (unlikely (!instance)) return HB_OT_NAME_ID_INVALID; 239 return instance->subfamilyNameID; 240 } 241 get_instance_postscript_name_idOT::fvar242 hb_ot_name_id_t get_instance_postscript_name_id (unsigned int instance_index) const 243 { 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; 249 } 250 get_instance_coordsOT::fvar251 unsigned int get_instance_coords (unsigned int instance_index, 252 unsigned int *coords_length, /* IN/OUT */ 253 float *coords /* OUT */) const 254 { 255 const InstanceRecord *instance = get_instance (instance_index); 256 if (unlikely (!instance)) 257 { 258 if (coords_length) 259 *coords_length = 0; 260 return 0; 261 } 262 263 if (coords_length && *coords_length) 264 { 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 (); 269 } 270 return axisCount; 271 } 272 collect_name_idsOT::fvar273 void collect_name_ids (hb_set_t *nameids) const 274 { 275 if (!has_data ()) return; 276 277 + get_axes () 278 | hb_map (&AxisRecord::get_name_id) 279 | hb_sink (nameids) 280 ; 281 282 + hb_range ((unsigned) instanceCount) 283 | hb_map ([this] (const unsigned _) { return get_instance_subfamily_name_id (_); }) 284 | hb_sink (nameids) 285 ; 286 287 + hb_range ((unsigned) instanceCount) 288 | hb_map ([this] (const unsigned _) { return get_instance_postscript_name_id (_); }) 289 | hb_sink (nameids) 290 ; 291 } 292 293 public: get_axesOT::fvar294 hb_array_t<const AxisRecord> get_axes () const 295 { return hb_array (&(this+firstAxis), axisCount); } 296 get_instanceOT::fvar297 const InstanceRecord *get_instance (unsigned int i) const 298 { 299 if (unlikely (i >= instanceCount)) return nullptr; 300 return &StructAtOffset<InstanceRecord> (&StructAfter<InstanceRecord> (get_axes ()), 301 i * instanceSize); 302 } 303 304 protected: 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. */ 320 321 public: 322 DEFINE_SIZE_STATIC (16); 323 }; 324 325 } /* namespace OT */ 326 327 328 #endif /* HB_OT_VAR_FVAR_TABLE_HH */ 329