| 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 |  | 
|---|
| 47 | hb_array_t<const HBFixed> get_coordinates (unsigned int axis_count) const | 
|---|
| 48 | { return coordinatesZ.as_array (axis_count); } | 
|---|
| 49 |  | 
|---|
| 50 | 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 |  | 
|---|
| 78 | 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 |  | 
|---|
| 101 | bool has_data () const { return version.to_int (); } | 
|---|
| 102 |  | 
|---|
| 103 | 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 |  | 
|---|
| 115 | unsigned int get_axis_count () const { return axisCount; } | 
|---|
| 116 |  | 
|---|
| 117 | #ifndef HB_DISABLE_DEPRECATED | 
|---|
| 118 | 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 |  | 
|---|
| 131 | 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 | 
|---|
| 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 | 
|---|
| 150 | { | 
|---|
| 151 | if (axes_count) | 
|---|
| 152 | { | 
|---|
| 153 | /* TODO Rewrite as hb_array_t<>::sub-array() */ | 
|---|
| 154 | unsigned int count = axisCount; | 
|---|
| 155 | start_offset = hb_min (start_offset, count); | 
|---|
| 156 |  | 
|---|
| 157 | count -= start_offset; | 
|---|
| 158 | axes_array += start_offset; | 
|---|
| 159 |  | 
|---|
| 160 | count = hb_min (count, *axes_count); | 
|---|
| 161 | *axes_count = count; | 
|---|
| 162 |  | 
|---|
| 163 | for (unsigned int i = 0; i < count; i++) | 
|---|
| 164 | get_axis_deprecated (start_offset + i, axes_array + i); | 
|---|
| 165 | } | 
|---|
| 166 | return axisCount; | 
|---|
| 167 | } | 
|---|
| 168 | #endif | 
|---|
| 169 |  | 
|---|
| 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 | 
|---|
| 173 | { | 
|---|
| 174 | if (axes_count) | 
|---|
| 175 | { | 
|---|
| 176 | /* TODO Rewrite as hb_array_t<>::sub-array() */ | 
|---|
| 177 | unsigned int count = axisCount; | 
|---|
| 178 | start_offset = hb_min (start_offset, count); | 
|---|
| 179 |  | 
|---|
| 180 | count -= start_offset; | 
|---|
| 181 | axes_array += start_offset; | 
|---|
| 182 |  | 
|---|
| 183 | count = hb_min (count, *axes_count); | 
|---|
| 184 | *axes_count = count; | 
|---|
| 185 |  | 
|---|
| 186 | for (unsigned int i = 0; i < count; i++) | 
|---|
| 187 | get_axis_info (start_offset + i, axes_array + i); | 
|---|
| 188 | } | 
|---|
| 189 | return axisCount; | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 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 | 
|---|
| 196 | { | 
|---|
| 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) | 
|---|
| 201 | { | 
|---|
| 202 | if (axis_index) | 
|---|
| 203 | *axis_index = i; | 
|---|
| 204 | get_axis_deprecated (i, info); | 
|---|
| 205 | return true; | 
|---|
| 206 | } | 
|---|
| 207 | if (axis_index) | 
|---|
| 208 | *axis_index = HB_OT_VAR_NO_AXIS_INDEX; | 
|---|
| 209 | return false; | 
|---|
| 210 | } | 
|---|
| 211 | #endif | 
|---|
| 212 |  | 
|---|
| 213 | bool find_axis_info (hb_tag_t tag, | 
|---|
| 214 | hb_ot_var_axis_info_t *info) const | 
|---|
| 215 | { | 
|---|
| 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) | 
|---|
| 220 | { | 
|---|
| 221 | get_axis_info (i, info); | 
|---|
| 222 | return true; | 
|---|
| 223 | } | 
|---|
| 224 | return false; | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | int normalize_axis_value (unsigned int axis_index, float v) const | 
|---|
| 228 | { | 
|---|
| 229 | hb_ot_var_axis_info_t axis; | 
|---|
| 230 | get_axis_info (axis_index, &axis); | 
|---|
| 231 |  | 
|---|
| 232 | v = hb_max (hb_min (v, axis.max_value), axis.min_value); /* Clamp. */ | 
|---|
| 233 |  | 
|---|
| 234 | if (v == axis.default_value) | 
|---|
| 235 | return 0; | 
|---|
| 236 | else if (v < axis.default_value) | 
|---|
| 237 | v = (v - axis.default_value) / (axis.default_value - axis.min_value); | 
|---|
| 238 | else | 
|---|
| 239 | v = (v - axis.default_value) / (axis.max_value - axis.default_value); | 
|---|
| 240 | return roundf (v * 16384.f); | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|
| 243 | float unnormalize_axis_value (unsigned int axis_index, float v) const | 
|---|
| 244 | { | 
|---|
| 245 | hb_ot_var_axis_info_t axis; | 
|---|
| 246 | get_axis_info (axis_index, &axis); | 
|---|
| 247 |  | 
|---|
| 248 | if (v == 0) | 
|---|
| 249 | return axis.default_value; | 
|---|
| 250 | else if (v < 0) | 
|---|
| 251 | v = v * (axis.default_value - axis.min_value) / 16384.f + axis.default_value; | 
|---|
| 252 | else | 
|---|
| 253 | v = v * (axis.max_value - axis.default_value) / 16384.f + axis.default_value; | 
|---|
| 254 | return v; | 
|---|
| 255 | } | 
|---|
| 256 |  | 
|---|
| 257 | unsigned int get_instance_count () const { return instanceCount; } | 
|---|
| 258 |  | 
|---|
| 259 | hb_ot_name_id_t get_instance_subfamily_name_id (unsigned int instance_index) const | 
|---|
| 260 | { | 
|---|
| 261 | const InstanceRecord *instance = get_instance (instance_index); | 
|---|
| 262 | if (unlikely (!instance)) return HB_OT_NAME_ID_INVALID; | 
|---|
| 263 | return instance->subfamilyNameID; | 
|---|
| 264 | } | 
|---|
| 265 |  | 
|---|
| 266 | hb_ot_name_id_t get_instance_postscript_name_id (unsigned int instance_index) const | 
|---|
| 267 | { | 
|---|
| 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; | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | unsigned int get_instance_coords (unsigned int  instance_index, | 
|---|
| 276 | unsigned int *coords_length, /* IN/OUT */ | 
|---|
| 277 | float        *coords         /* OUT */) const | 
|---|
| 278 | { | 
|---|
| 279 | const InstanceRecord *instance = get_instance (instance_index); | 
|---|
| 280 | if (unlikely (!instance)) | 
|---|
| 281 | { | 
|---|
| 282 | if (coords_length) | 
|---|
| 283 | *coords_length = 0; | 
|---|
| 284 | return 0; | 
|---|
| 285 | } | 
|---|
| 286 |  | 
|---|
| 287 | if (coords_length && *coords_length) | 
|---|
| 288 | { | 
|---|
| 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 (); | 
|---|
| 293 | } | 
|---|
| 294 | return axisCount; | 
|---|
| 295 | } | 
|---|
| 296 |  | 
|---|
| 297 | void collect_name_ids (hb_set_t *nameids) const | 
|---|
| 298 | { | 
|---|
| 299 | if (!has_data ()) return; | 
|---|
| 300 |  | 
|---|
| 301 | + get_axes () | 
|---|
| 302 | | hb_map (&AxisRecord::axisNameID) | 
|---|
| 303 | | hb_sink (nameids) | 
|---|
| 304 | ; | 
|---|
| 305 |  | 
|---|
| 306 | + hb_range ((unsigned) instanceCount) | 
|---|
| 307 | | hb_map ([this] (const unsigned _) { return get_instance_subfamily_name_id (_); }) | 
|---|
| 308 | | hb_sink (nameids) | 
|---|
| 309 | ; | 
|---|
| 310 |  | 
|---|
| 311 | + hb_range ((unsigned) instanceCount) | 
|---|
| 312 | | hb_map ([this] (const unsigned _) { return get_instance_postscript_name_id (_); }) | 
|---|
| 313 | | hb_sink (nameids) | 
|---|
| 314 | ; | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 |  | 
|---|
| 318 | protected: | 
|---|
| 319 | hb_array_t<const AxisRecord> get_axes () const | 
|---|
| 320 | { return hb_array (&(this+firstAxis), axisCount); } | 
|---|
| 321 |  | 
|---|
| 322 | const InstanceRecord *get_instance (unsigned int i) const | 
|---|
| 323 | { | 
|---|
| 324 | if (unlikely (i >= instanceCount)) return nullptr; | 
|---|
| 325 | return &StructAtOffset<InstanceRecord> (&StructAfter<InstanceRecord> (get_axes ()), | 
|---|
| 326 | i * instanceSize); | 
|---|
| 327 | } | 
|---|
| 328 |  | 
|---|
| 329 | protected: | 
|---|
| 330 | FixedVersion<>version;	/* Version of the fvar table | 
|---|
| 331 | * initially set to 0x00010000u */ | 
|---|
| 332 | OffsetTo<AxisRecord> | 
|---|
| 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. */ | 
|---|
| 345 |  | 
|---|
| 346 | public: | 
|---|
| 347 | DEFINE_SIZE_STATIC (16); | 
|---|
| 348 | }; | 
|---|
| 349 |  | 
|---|
| 350 | } /* namespace OT */ | 
|---|
| 351 |  | 
|---|
| 352 |  | 
|---|
| 353 | #endif /* HB_OT_VAR_FVAR_TABLE_HH */ | 
|---|
| 354 |  | 
|---|