Behdad Esfahbod | 8a577aa | 2017-01-22 18:22:40 -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_AVAR_TABLE_HH |
| 28 | #define HB_OT_VAR_AVAR_TABLE_HH |
| 29 | |
| 30 | #include "hb-open-type-private.hh" |
Behdad Esfahbod | 8a577aa | 2017-01-22 18:22:40 -0800 | [diff] [blame] | 31 | |
Ebrahim Byagowi | a02c3ee | 2018-04-12 13:38:19 +0430 | [diff] [blame] | 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 | |
Behdad Esfahbod | 8a577aa | 2017-01-22 18:22:40 -0800 | [diff] [blame] | 40 | namespace OT { |
| 41 | |
| 42 | |
| 43 | struct AxisValueMap |
| 44 | { |
| 45 | inline 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 fromCoord; /* A normalized coordinate value obtained using |
| 53 | * default normalization. */ |
| 54 | F2DOT14 toCoord; /* The modified, normalized coordinate value. */ |
| 55 | |
| 56 | public: |
| 57 | DEFINE_SIZE_STATIC (4); |
| 58 | }; |
| 59 | |
Behdad Esfahbod | a484e23 | 2017-01-22 19:41:33 -0800 | [diff] [blame] | 60 | struct SegmentMaps : ArrayOf<AxisValueMap> |
| 61 | { |
| 62 | inline int map (int value) const |
| 63 | { |
| 64 | /* The following special-cases are not part of OpenType, which requires |
| 65 | * that at least -1, 0, and +1 must be mapped. But we include these as |
| 66 | * part of a better error recovery scheme. */ |
| 67 | |
Behdad Esfahbod | 5dc3045 | 2017-08-07 21:37:07 -0700 | [diff] [blame] | 68 | if (len < 2) |
| 69 | { |
| 70 | if (!len) |
| 71 | return value; |
| 72 | else /* len == 1*/ |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 73 | return value - arrayZ[0].fromCoord + arrayZ[0].toCoord; |
Behdad Esfahbod | 5dc3045 | 2017-08-07 21:37:07 -0700 | [diff] [blame] | 74 | } |
Behdad Esfahbod | a484e23 | 2017-01-22 19:41:33 -0800 | [diff] [blame] | 75 | |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 76 | if (value <= arrayZ[0].fromCoord) |
| 77 | return value - arrayZ[0].fromCoord + arrayZ[0].toCoord; |
Behdad Esfahbod | a484e23 | 2017-01-22 19:41:33 -0800 | [diff] [blame] | 78 | |
| 79 | unsigned int i; |
| 80 | unsigned int count = len; |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 81 | for (i = 1; i < count && value > arrayZ[i].fromCoord; i++) |
Behdad Esfahbod | a484e23 | 2017-01-22 19:41:33 -0800 | [diff] [blame] | 82 | ; |
| 83 | |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 84 | if (value >= arrayZ[i].fromCoord) |
| 85 | return value - arrayZ[i].fromCoord + arrayZ[i].toCoord; |
Behdad Esfahbod | a484e23 | 2017-01-22 19:41:33 -0800 | [diff] [blame] | 86 | |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 87 | if (unlikely (arrayZ[i-1].fromCoord == arrayZ[i].fromCoord)) |
| 88 | return arrayZ[i-1].toCoord; |
Behdad Esfahbod | a484e23 | 2017-01-22 19:41:33 -0800 | [diff] [blame] | 89 | |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 90 | int denom = arrayZ[i].fromCoord - arrayZ[i-1].fromCoord; |
| 91 | return arrayZ[i-1].toCoord + |
| 92 | ((arrayZ[i].toCoord - arrayZ[i-1].toCoord) * |
| 93 | (value - arrayZ[i-1].fromCoord) + denom/2) / denom; |
Behdad Esfahbod | a484e23 | 2017-01-22 19:41:33 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 96 | DEFINE_SIZE_ARRAY (2, arrayZ); |
Behdad Esfahbod | a484e23 | 2017-01-22 19:41:33 -0800 | [diff] [blame] | 97 | }; |
Behdad Esfahbod | 8a577aa | 2017-01-22 18:22:40 -0800 | [diff] [blame] | 98 | |
Behdad Esfahbod | 8a577aa | 2017-01-22 18:22:40 -0800 | [diff] [blame] | 99 | struct avar |
| 100 | { |
| 101 | static const hb_tag_t tableTag = HB_OT_TAG_avar; |
| 102 | |
| 103 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 104 | { |
| 105 | TRACE_SANITIZE (this); |
| 106 | if (unlikely (!(version.sanitize (c) && |
| 107 | version.major == 1 && |
| 108 | c->check_struct (this)))) |
| 109 | return_trace (false); |
| 110 | |
Behdad Esfahbod | 5b93f69 | 2018-05-02 14:59:14 -0400 | [diff] [blame] | 111 | const SegmentMaps *map = axisSegmentMapsZ; |
Behdad Esfahbod | 8a577aa | 2017-01-22 18:22:40 -0800 | [diff] [blame] | 112 | unsigned int count = axisCount; |
| 113 | for (unsigned int i = 0; i < count; i++) |
| 114 | { |
| 115 | if (unlikely (!map->sanitize (c))) |
| 116 | return_trace (false); |
| 117 | map = &StructAfter<SegmentMaps> (*map); |
| 118 | } |
| 119 | |
| 120 | return_trace (true); |
| 121 | } |
| 122 | |
Behdad Esfahbod | a484e23 | 2017-01-22 19:41:33 -0800 | [diff] [blame] | 123 | inline void map_coords (int *coords, unsigned int coords_length) const |
| 124 | { |
| 125 | unsigned int count = MIN<unsigned int> (coords_length, axisCount); |
| 126 | |
Behdad Esfahbod | 5b93f69 | 2018-05-02 14:59:14 -0400 | [diff] [blame] | 127 | const SegmentMaps *map = axisSegmentMapsZ; |
Behdad Esfahbod | a484e23 | 2017-01-22 19:41:33 -0800 | [diff] [blame] | 128 | for (unsigned int i = 0; i < count; i++) |
| 129 | { |
| 130 | coords[i] = map->map (coords[i]); |
| 131 | map = &StructAfter<SegmentMaps> (*map); |
| 132 | } |
| 133 | } |
| 134 | |
Behdad Esfahbod | 8a577aa | 2017-01-22 18:22:40 -0800 | [diff] [blame] | 135 | protected: |
| 136 | FixedVersion<>version; /* Version of the avar table |
| 137 | * initially set to 0x00010000u */ |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 138 | HBUINT16 reserved; /* This field is permanently reserved. Set to 0. */ |
| 139 | HBUINT16 axisCount; /* The number of variation axes in the font. This |
Behdad Esfahbod | 8a577aa | 2017-01-22 18:22:40 -0800 | [diff] [blame] | 140 | * must be the same number as axisCount in the |
| 141 | * 'fvar' table. */ |
Behdad Esfahbod | 5b93f69 | 2018-05-02 14:59:14 -0400 | [diff] [blame] | 142 | SegmentMaps axisSegmentMapsZ[VAR]; |
Behdad Esfahbod | 8a577aa | 2017-01-22 18:22:40 -0800 | [diff] [blame] | 143 | |
| 144 | public: |
| 145 | DEFINE_SIZE_MIN (8); |
| 146 | }; |
| 147 | |
| 148 | } /* namespace OT */ |
| 149 | |
| 150 | |
| 151 | #endif /* HB_OT_VAR_AVAR_TABLE_HH */ |