| 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_AVAR_TABLE_HH |
| 28 | #define HB_OT_VAR_AVAR_TABLE_HH |
| 29 | |
| 30 | #include "hb-open-type.hh" |
| 31 | |
| 32 | /* |
| 33 | * avar -- Axis Variations |
| 34 | * https://docs.microsoft.com/en-us/typography/opentype/spec/avar |
| 35 | */ |
| 36 | |
| 37 | #define HB_OT_TAG_avar HB_TAG('a','v','a','r') |
| 38 | |
| 39 | |
| 40 | namespace OT { |
| 41 | |
| 42 | |
| 43 | struct AxisValueMap |
| 44 | { |
| 45 | bool sanitize (hb_sanitize_context_t *c) const |
| 46 | { |
| 47 | TRACE_SANITIZE (this); |
| 48 | return_trace (c->check_struct (this)); |
| 49 | } |
| 50 | |
| 51 | public: |
| 52 | F2DOT14 coords[2]; |
| 53 | // F2DOT14 fromCoord; /* A normalized coordinate value obtained using |
| 54 | // * default normalization. */ |
| 55 | // F2DOT14 toCoord; /* The modified, normalized coordinate value. */ |
| 56 | |
| 57 | public: |
| 58 | DEFINE_SIZE_STATIC (4); |
| 59 | }; |
| 60 | |
| 61 | struct SegmentMaps : ArrayOf<AxisValueMap> |
| 62 | { |
| 63 | int map (int value, unsigned int from_offset = 0, unsigned int to_offset = 1) const |
| 64 | { |
| 65 | #define fromCoord coords[from_offset] |
| 66 | #define toCoord coords[to_offset] |
| 67 | /* The following special-cases are not part of OpenType, which requires |
| 68 | * that at least -1, 0, and +1 must be mapped. But we include these as |
| 69 | * part of a better error recovery scheme. */ |
| 70 | if (len < 2) |
| 71 | { |
| 72 | if (!len) |
| 73 | return value; |
| 74 | else /* len == 1*/ |
| 75 | return value - arrayZ[0].fromCoord + arrayZ[0].toCoord; |
| 76 | } |
| 77 | |
| 78 | if (value <= arrayZ[0].fromCoord) |
| 79 | return value - arrayZ[0].fromCoord + arrayZ[0].toCoord; |
| 80 | |
| 81 | unsigned int i; |
| 82 | unsigned int count = len; |
| 83 | for (i = 1; i < count && value > arrayZ[i].fromCoord; i++) |
| 84 | ; |
| 85 | |
| 86 | if (value >= arrayZ[i].fromCoord) |
| 87 | return value - arrayZ[i].fromCoord + arrayZ[i].toCoord; |
| 88 | |
| 89 | if (unlikely (arrayZ[i-1].fromCoord == arrayZ[i].fromCoord)) |
| 90 | return arrayZ[i-1].toCoord; |
| 91 | |
| 92 | int denom = arrayZ[i].fromCoord - arrayZ[i-1].fromCoord; |
| 93 | return arrayZ[i-1].toCoord + |
| 94 | ((arrayZ[i].toCoord - arrayZ[i-1].toCoord) * |
| 95 | (value - arrayZ[i-1].fromCoord) + denom/2) / denom; |
| 96 | #undef toCoord |
| 97 | #undef fromCoord |
| 98 | } |
| 99 | |
| 100 | int unmap (int value) const { return map (value, 1, 0); } |
| 101 | |
| 102 | public: |
| 103 | DEFINE_SIZE_ARRAY (2, *this); |
| 104 | }; |
| 105 | |
| 106 | struct avar |
| 107 | { |
| 108 | static constexpr hb_tag_t tableTag = HB_OT_TAG_avar; |
| 109 | |
| 110 | bool sanitize (hb_sanitize_context_t *c) const |
| 111 | { |
| 112 | TRACE_SANITIZE (this); |
| 113 | if (unlikely (!(version.sanitize (c) && |
| 114 | version.major == 1 && |
| 115 | c->check_struct (this)))) |
| 116 | return_trace (false); |
| 117 | |
| 118 | const SegmentMaps *map = &firstAxisSegmentMaps; |
| 119 | unsigned int count = axisCount; |
| 120 | for (unsigned int i = 0; i < count; i++) |
| 121 | { |
| 122 | if (unlikely (!map->sanitize (c))) |
| 123 | return_trace (false); |
| 124 | map = &StructAfter<SegmentMaps> (*map); |
| 125 | } |
| 126 | |
| 127 | return_trace (true); |
| 128 | } |
| 129 | |
| 130 | void map_coords (int *coords, unsigned int coords_length) const |
| 131 | { |
| 132 | unsigned int count = hb_min (coords_length, axisCount); |
| 133 | |
| 134 | const SegmentMaps *map = &firstAxisSegmentMaps; |
| 135 | for (unsigned int i = 0; i < count; i++) |
| 136 | { |
| 137 | coords[i] = map->map (coords[i]); |
| 138 | map = &StructAfter<SegmentMaps> (*map); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void unmap_coords (int *coords, unsigned int coords_length) const |
| 143 | { |
| 144 | unsigned int count = hb_min (coords_length, axisCount); |
| 145 | |
| 146 | const SegmentMaps *map = &firstAxisSegmentMaps; |
| 147 | for (unsigned int i = 0; i < count; i++) |
| 148 | { |
| 149 | coords[i] = map->unmap (coords[i]); |
| 150 | map = &StructAfter<SegmentMaps> (*map); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | protected: |
| 155 | FixedVersion<>version; /* Version of the avar table |
| 156 | * initially set to 0x00010000u */ |
| 157 | HBUINT16 reserved; /* This field is permanently reserved. Set to 0. */ |
| 158 | HBUINT16 axisCount; /* The number of variation axes in the font. This |
| 159 | * must be the same number as axisCount in the |
| 160 | * 'fvar' table. */ |
| 161 | SegmentMaps firstAxisSegmentMaps; |
| 162 | |
| 163 | public: |
| 164 | DEFINE_SIZE_MIN (8); |
| 165 | }; |
| 166 | |
| 167 | } /* namespace OT */ |
| 168 | |
| 169 | |
| 170 | #endif /* HB_OT_VAR_AVAR_TABLE_HH */ |
| 171 | |