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 protected: 146 Tag axisTag; /* Tag identifying the design variation for the axis. */ 147 HBFixed minValue; /* The minimum coordinate value for the axis. */ 148 HBFixed defaultValue; /* The default coordinate value for the axis. */ 149 HBFixed maxValue; /* The maximum coordinate value for the axis. */ 150 HBUINT16 flags; /* Axis flags. */ 151 NameID axisNameID; /* The name ID for entries in the 'name' table that 152 * provide a display name for this axis. */ 153 154 public: 155 DEFINE_SIZE_STATIC (20); 156 }; 157 158 struct fvar 159 { 160 static constexpr hb_tag_t tableTag = HB_OT_TAG_fvar; 161 has_dataOT::fvar162 bool has_data () const { return version.to_int (); } 163 sanitizeOT::fvar164 bool sanitize (hb_sanitize_context_t *c) const 165 { 166 TRACE_SANITIZE (this); 167 return_trace (version.sanitize (c) && 168 likely (version.major == 1) && 169 c->check_struct (this) && 170 axisSize == 20 && /* Assumed in our code. */ 171 instanceSize >= axisCount * 4 + 4 && 172 get_axes ().sanitize (c) && 173 c->check_range (get_instance (0), instanceCount, instanceSize)); 174 } 175 get_axis_countOT::fvar176 unsigned int get_axis_count () const { return axisCount; } 177 178 #ifndef HB_DISABLE_DEPRECATED get_axes_deprecatedOT::fvar179 unsigned int get_axes_deprecated (unsigned int start_offset, 180 unsigned int *axes_count /* IN/OUT */, 181 hb_ot_var_axis_t *axes_array /* OUT */) const 182 { 183 if (axes_count) 184 { 185 hb_array_t<const AxisRecord> arr = get_axes ().sub_array (start_offset, axes_count); 186 for (unsigned i = 0; i < arr.length; ++i) 187 arr[i].get_axis_deprecated (&axes_array[i]); 188 } 189 return axisCount; 190 } 191 #endif 192 get_axis_infosOT::fvar193 unsigned int get_axis_infos (unsigned int start_offset, 194 unsigned int *axes_count /* IN/OUT */, 195 hb_ot_var_axis_info_t *axes_array /* OUT */) const 196 { 197 if (axes_count) 198 { 199 hb_array_t<const AxisRecord> arr = get_axes ().sub_array (start_offset, axes_count); 200 for (unsigned i = 0; i < arr.length; ++i) 201 arr[i].get_axis_info (start_offset + i, &axes_array[i]); 202 } 203 return axisCount; 204 } 205 206 #ifndef HB_DISABLE_DEPRECATED 207 bool find_axis_deprecatedOT::fvar208 find_axis_deprecated (hb_tag_t tag, unsigned *axis_index, hb_ot_var_axis_t *info) const 209 { 210 unsigned i; 211 if (!axis_index) axis_index = &i; 212 *axis_index = HB_OT_VAR_NO_AXIS_INDEX; 213 auto axes = get_axes (); 214 return axes.lfind (tag, axis_index) && (axes[*axis_index].get_axis_deprecated (info), true); 215 } 216 #endif 217 218 bool find_axis_infoOT::fvar219 find_axis_info (hb_tag_t tag, hb_ot_var_axis_info_t *info) const 220 { 221 unsigned i; 222 auto axes = get_axes (); 223 return axes.lfind (tag, &i) && (axes[i].get_axis_info (i, info), true); 224 } 225 normalize_axis_valueOT::fvar226 int normalize_axis_value (unsigned int axis_index, float v) const 227 { return get_axes ()[axis_index].normalize_axis_value (v); } 228 unnormalize_axis_valueOT::fvar229 float unnormalize_axis_value (unsigned int axis_index, int v) const 230 { return get_axes ()[axis_index].unnormalize_axis_value (v); } 231 get_instance_countOT::fvar232 unsigned int get_instance_count () const { return instanceCount; } 233 get_instance_subfamily_name_idOT::fvar234 hb_ot_name_id_t get_instance_subfamily_name_id (unsigned int instance_index) const 235 { 236 const InstanceRecord *instance = get_instance (instance_index); 237 if (unlikely (!instance)) return HB_OT_NAME_ID_INVALID; 238 return instance->subfamilyNameID; 239 } 240 get_instance_postscript_name_idOT::fvar241 hb_ot_name_id_t get_instance_postscript_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 if (instanceSize >= axisCount * 4 + 6) 246 return StructAfter<NameID> (instance->get_coordinates (axisCount)); 247 return HB_OT_NAME_ID_INVALID; 248 } 249 get_instance_coordsOT::fvar250 unsigned int get_instance_coords (unsigned int instance_index, 251 unsigned int *coords_length, /* IN/OUT */ 252 float *coords /* OUT */) const 253 { 254 const InstanceRecord *instance = get_instance (instance_index); 255 if (unlikely (!instance)) 256 { 257 if (coords_length) 258 *coords_length = 0; 259 return 0; 260 } 261 262 if (coords_length && *coords_length) 263 { 264 hb_array_t<const HBFixed> instanceCoords = instance->get_coordinates (axisCount) 265 .sub_array (0, *coords_length); 266 for (unsigned int i = 0; i < instanceCoords.length; i++) 267 coords[i] = instanceCoords.arrayZ[i].to_float (); 268 } 269 return axisCount; 270 } 271 collect_name_idsOT::fvar272 void collect_name_ids (hb_set_t *nameids) const 273 { 274 if (!has_data ()) return; 275 276 + get_axes () 277 | hb_map (&AxisRecord::get_name_id) 278 | hb_sink (nameids) 279 ; 280 281 + hb_range ((unsigned) instanceCount) 282 | hb_map ([this] (const unsigned _) { return get_instance_subfamily_name_id (_); }) 283 | hb_sink (nameids) 284 ; 285 286 + hb_range ((unsigned) instanceCount) 287 | hb_map ([this] (const unsigned _) { return get_instance_postscript_name_id (_); }) 288 | hb_sink (nameids) 289 ; 290 } 291 292 protected: get_axesOT::fvar293 hb_array_t<const AxisRecord> get_axes () const 294 { return hb_array (&(this+firstAxis), axisCount); } 295 get_instanceOT::fvar296 const InstanceRecord *get_instance (unsigned int i) const 297 { 298 if (unlikely (i >= instanceCount)) return nullptr; 299 return &StructAtOffset<InstanceRecord> (&StructAfter<InstanceRecord> (get_axes ()), 300 i * instanceSize); 301 } 302 303 protected: 304 FixedVersion<>version; /* Version of the fvar table 305 * initially set to 0x00010000u */ 306 Offset16To<AxisRecord> 307 firstAxis; /* Offset in bytes from the beginning of the table 308 * to the start of the AxisRecord array. */ 309 HBUINT16 reserved; /* This field is permanently reserved. Set to 2. */ 310 HBUINT16 axisCount; /* The number of variation axes in the font (the 311 * number of records in the axes array). */ 312 HBUINT16 axisSize; /* The size in bytes of each VariationAxisRecord — 313 * set to 20 (0x0014) for this version. */ 314 HBUINT16 instanceCount; /* The number of named instances defined in the font 315 * (the number of records in the instances array). */ 316 HBUINT16 instanceSize; /* The size in bytes of each InstanceRecord — set 317 * to either axisCount * sizeof(HBFixed) + 4, or to 318 * axisCount * sizeof(HBFixed) + 6. */ 319 320 public: 321 DEFINE_SIZE_STATIC (16); 322 }; 323 324 } /* namespace OT */ 325 326 327 #endif /* HB_OT_VAR_FVAR_TABLE_HH */ 328