Behdad Esfahbod | 55d42fd | 2017-01-19 19:35:48 -0800 | [diff] [blame] | 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-private.hh" |
Behdad Esfahbod | 55d42fd | 2017-01-19 19:35:48 -0800 | [diff] [blame] | 31 | |
Ebrahim Byagowi | a02c3ee | 2018-04-12 13:38:19 +0430 | [diff] [blame] | 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 | |
Behdad Esfahbod | 55d42fd | 2017-01-19 19:35:48 -0800 | [diff] [blame] | 40 | namespace OT { |
| 41 | |
| 42 | |
| 43 | struct InstanceRecord |
| 44 | { |
| 45 | inline bool sanitize (hb_sanitize_context_t *c, unsigned int axis_count) const |
| 46 | { |
| 47 | TRACE_SANITIZE (this); |
| 48 | return_trace (c->check_struct (this) && |
| 49 | c->check_array (coordinates, coordinates[0].static_size, axis_count)); |
| 50 | } |
| 51 | |
| 52 | protected: |
Behdad Esfahbod | a0dccb6 | 2018-03-14 16:31:53 +0100 | [diff] [blame] | 53 | NameID subfamilyNameID;/* The name ID for entries in the 'name' table |
Behdad Esfahbod | 55d42fd | 2017-01-19 19:35:48 -0800 | [diff] [blame] | 54 | * that provide subfamily names for this instance. */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 55 | HBUINT16 reserved; /* Reserved for future use — set to 0. */ |
Behdad Esfahbod | 55d42fd | 2017-01-19 19:35:48 -0800 | [diff] [blame] | 56 | Fixed coordinates[VAR];/* The coordinates array for this instance. */ |
Behdad Esfahbod | a0dccb6 | 2018-03-14 16:31:53 +0100 | [diff] [blame] | 57 | //NameID postScriptNameIDX;/*Optional. The name ID for entries in the 'name' |
Behdad Esfahbod | 55d42fd | 2017-01-19 19:35:48 -0800 | [diff] [blame] | 58 | // * table that provide PostScript names for this |
| 59 | // * instance. */ |
| 60 | |
| 61 | public: |
| 62 | DEFINE_SIZE_ARRAY (4, coordinates); |
| 63 | }; |
| 64 | |
| 65 | struct AxisRecord |
| 66 | { |
| 67 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 68 | { |
| 69 | TRACE_SANITIZE (this); |
| 70 | return_trace (c->check_struct (this)); |
| 71 | } |
| 72 | |
Behdad Esfahbod | 422c0c3 | 2017-01-20 19:14:54 -0800 | [diff] [blame] | 73 | public: |
Behdad Esfahbod | 55d42fd | 2017-01-19 19:35:48 -0800 | [diff] [blame] | 74 | Tag axisTag; /* Tag identifying the design variation for the axis. */ |
| 75 | Fixed minValue; /* The minimum coordinate value for the axis. */ |
| 76 | Fixed defaultValue; /* The default coordinate value for the axis. */ |
| 77 | Fixed maxValue; /* The maximum coordinate value for the axis. */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 78 | HBUINT16 reserved; /* Reserved for future use — set to 0. */ |
Behdad Esfahbod | a0dccb6 | 2018-03-14 16:31:53 +0100 | [diff] [blame] | 79 | NameID axisNameID; /* The name ID for entries in the 'name' table that |
Behdad Esfahbod | 55d42fd | 2017-01-19 19:35:48 -0800 | [diff] [blame] | 80 | * provide a display name for this axis. */ |
| 81 | |
| 82 | public: |
| 83 | DEFINE_SIZE_STATIC (20); |
| 84 | }; |
| 85 | |
Behdad Esfahbod | 55d42fd | 2017-01-19 19:35:48 -0800 | [diff] [blame] | 86 | struct fvar |
| 87 | { |
| 88 | static const hb_tag_t tableTag = HB_OT_TAG_fvar; |
| 89 | |
| 90 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 91 | { |
| 92 | TRACE_SANITIZE (this); |
| 93 | return_trace (version.sanitize (c) && |
Behdad Esfahbod | 422c0c3 | 2017-01-20 19:14:54 -0800 | [diff] [blame] | 94 | likely (version.major == 1) && |
| 95 | c->check_struct (this) && |
| 96 | instanceSize >= axisCount * 4 + 4 && |
| 97 | axisSize <= 1024 && /* Arbitrary, just to simplify overflow checks. */ |
| 98 | instanceSize <= 1024 && /* Arbitrary, just to simplify overflow checks. */ |
| 99 | c->check_range (this, things) && |
| 100 | c->check_range (&StructAtOffset<char> (this, things), |
| 101 | axisCount * axisSize + instanceCount * instanceSize)); |
Behdad Esfahbod | 55d42fd | 2017-01-19 19:35:48 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Behdad Esfahbod | 422c0c3 | 2017-01-20 19:14:54 -0800 | [diff] [blame] | 104 | inline unsigned int get_axis_count (void) const |
| 105 | { return axisCount; } |
| 106 | |
Behdad Esfahbod | 785982b | 2017-01-20 19:57:27 -0800 | [diff] [blame] | 107 | inline bool get_axis (unsigned int index, hb_ot_var_axis_t *info) const |
| 108 | { |
| 109 | if (unlikely (index >= axisCount)) |
| 110 | return false; |
| 111 | |
| 112 | if (info) |
| 113 | { |
| 114 | const AxisRecord &axis = get_axes ()[index]; |
| 115 | info->tag = axis.axisTag; |
| 116 | info->name_id = axis.axisNameID; |
| 117 | info->default_value = axis.defaultValue / 65536.; |
| 118 | /* Ensure order, to simplify client math. */ |
| 119 | info->min_value = MIN<float> (info->default_value, axis.minValue / 65536.); |
| 120 | info->max_value = MAX<float> (info->default_value, axis.maxValue / 65536.); |
| 121 | } |
| 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | inline unsigned int get_axis_infos (unsigned int start_offset, |
| 127 | unsigned int *axes_count /* IN/OUT */, |
| 128 | hb_ot_var_axis_t *axes_array /* OUT */) const |
| 129 | { |
| 130 | if (axes_count) |
| 131 | { |
| 132 | unsigned int count = axisCount; |
| 133 | start_offset = MIN (start_offset, count); |
| 134 | |
| 135 | count -= start_offset; |
| 136 | axes_array += start_offset; |
| 137 | |
| 138 | count = MIN (count, *axes_count); |
| 139 | *axes_count = count; |
| 140 | |
| 141 | for (unsigned int i = 0; i < count; i++) |
| 142 | get_axis (start_offset + i, axes_array + i); |
| 143 | } |
| 144 | return axisCount; |
| 145 | } |
| 146 | |
Behdad Esfahbod | 422c0c3 | 2017-01-20 19:14:54 -0800 | [diff] [blame] | 147 | inline bool find_axis (hb_tag_t tag, unsigned int *index, hb_ot_var_axis_t *info) const |
| 148 | { |
| 149 | const AxisRecord *axes = get_axes (); |
| 150 | unsigned int count = get_axis_count (); |
| 151 | for (unsigned int i = 0; i < count; i++) |
| 152 | if (axes[i].axisTag == tag) |
| 153 | { |
| 154 | if (index) |
| 155 | *index = i; |
Behdad Esfahbod | 785982b | 2017-01-20 19:57:27 -0800 | [diff] [blame] | 156 | return get_axis (i, info); |
Behdad Esfahbod | 422c0c3 | 2017-01-20 19:14:54 -0800 | [diff] [blame] | 157 | } |
| 158 | if (index) |
| 159 | *index = HB_OT_VAR_NO_AXIS_INDEX; |
| 160 | return false; |
| 161 | } |
| 162 | |
Behdad Esfahbod | 785982b | 2017-01-20 19:57:27 -0800 | [diff] [blame] | 163 | inline int normalize_axis_value (unsigned int axis_index, float v) const |
Behdad Esfahbod | 422c0c3 | 2017-01-20 19:14:54 -0800 | [diff] [blame] | 164 | { |
| 165 | hb_ot_var_axis_t axis; |
Behdad Esfahbod | 785982b | 2017-01-20 19:57:27 -0800 | [diff] [blame] | 166 | if (!get_axis (axis_index, &axis)) |
Behdad Esfahbod | 422c0c3 | 2017-01-20 19:14:54 -0800 | [diff] [blame] | 167 | return 0; |
| 168 | |
| 169 | v = MAX (MIN (v, axis.max_value), axis.min_value); /* Clamp. */ |
| 170 | |
| 171 | if (v == axis.default_value) |
| 172 | return 0; |
| 173 | else if (v < axis.default_value) |
| 174 | v = (v - axis.default_value) / (axis.default_value - axis.min_value); |
| 175 | else |
| 176 | v = (v - axis.default_value) / (axis.max_value - axis.default_value); |
| 177 | return (int) (v * 16384. + (v >= 0. ? .5 : -.5)); |
| 178 | } |
Behdad Esfahbod | 55d42fd | 2017-01-19 19:35:48 -0800 | [diff] [blame] | 179 | |
| 180 | protected: |
Behdad Esfahbod | 785982b | 2017-01-20 19:57:27 -0800 | [diff] [blame] | 181 | inline const AxisRecord * get_axes (void) const |
| 182 | { return &StructAtOffset<AxisRecord> (this, things); } |
| 183 | |
| 184 | inline const InstanceRecord * get_instances (void) const |
| 185 | { return &StructAtOffset<InstanceRecord> (get_axes () + axisCount, 0); } |
| 186 | |
| 187 | protected: |
Behdad Esfahbod | 55d42fd | 2017-01-19 19:35:48 -0800 | [diff] [blame] | 188 | FixedVersion<>version; /* Version of the fvar table |
| 189 | * initially set to 0x00010000u */ |
Behdad Esfahbod | c6173a3 | 2017-11-14 21:09:03 -0800 | [diff] [blame] | 190 | Offset16 things; /* Offset in bytes from the beginning of the table |
Behdad Esfahbod | 55d42fd | 2017-01-19 19:35:48 -0800 | [diff] [blame] | 191 | * to the start of the AxisRecord array. */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 192 | HBUINT16 reserved; /* This field is permanently reserved. Set to 2. */ |
| 193 | HBUINT16 axisCount; /* The number of variation axes in the font (the |
Behdad Esfahbod | 55d42fd | 2017-01-19 19:35:48 -0800 | [diff] [blame] | 194 | * number of records in the axes array). */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 195 | HBUINT16 axisSize; /* The size in bytes of each VariationAxisRecord — |
Behdad Esfahbod | 55d42fd | 2017-01-19 19:35:48 -0800 | [diff] [blame] | 196 | * set to 20 (0x0014) for this version. */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 197 | HBUINT16 instanceCount; /* The number of named instances defined in the font |
Behdad Esfahbod | 55d42fd | 2017-01-19 19:35:48 -0800 | [diff] [blame] | 198 | * (the number of records in the instances array). */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 199 | HBUINT16 instanceSize; /* The size in bytes of each InstanceRecord — set |
Behdad Esfahbod | 55d42fd | 2017-01-19 19:35:48 -0800 | [diff] [blame] | 200 | * to either axisCount * sizeof(Fixed) + 4, or to |
| 201 | * axisCount * sizeof(Fixed) + 6. */ |
| 202 | |
| 203 | public: |
| 204 | DEFINE_SIZE_STATIC (16); |
| 205 | }; |
| 206 | |
| 207 | } /* namespace OT */ |
| 208 | |
| 209 | |
| 210 | #endif /* HB_OT_VAR_FVAR_TABLE_HH */ |