/src/harfbuzz/src/hb-ot-var-avar-table.hh
Line | Count | Source (jump to first uncovered line) |
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 | | #include "hb-ot-var-common.hh" |
32 | | |
33 | | |
34 | | /* |
35 | | * avar -- Axis Variations |
36 | | * https://docs.microsoft.com/en-us/typography/opentype/spec/avar |
37 | | */ |
38 | | |
39 | | #define HB_OT_TAG_avar HB_TAG('a','v','a','r') |
40 | | |
41 | | |
42 | | namespace OT { |
43 | | |
44 | | |
45 | | /* "Spec": https://github.com/be-fonts/boring-expansion-spec/issues/14 */ |
46 | | struct avarV2Tail |
47 | | { |
48 | | friend struct avar; |
49 | | |
50 | | bool sanitize (hb_sanitize_context_t *c, |
51 | | const void *base) const |
52 | 414 | { |
53 | 414 | TRACE_SANITIZE (this); |
54 | 414 | return_trace (varIdxMap.sanitize (c, base) && |
55 | 414 | varStore.sanitize (c, base)); |
56 | 414 | } |
57 | | |
58 | | protected: |
59 | | Offset32To<DeltaSetIndexMap> varIdxMap; /* Offset from the beginning of 'avar' table. */ |
60 | | Offset32To<VariationStore> varStore; /* Offset from the beginning of 'avar' table. */ |
61 | | |
62 | | public: |
63 | | DEFINE_SIZE_STATIC (8); |
64 | | }; |
65 | | |
66 | | |
67 | | struct AxisValueMap |
68 | | { |
69 | | bool sanitize (hb_sanitize_context_t *c) const |
70 | 0 | { |
71 | 0 | TRACE_SANITIZE (this); |
72 | 0 | return_trace (c->check_struct (this)); |
73 | 0 | } |
74 | | |
75 | | public: |
76 | | F2DOT14 coords[2]; |
77 | | // F2DOT14 fromCoord; /* A normalized coordinate value obtained using |
78 | | // * default normalization. */ |
79 | | // F2DOT14 toCoord; /* The modified, normalized coordinate value. */ |
80 | | |
81 | | public: |
82 | | DEFINE_SIZE_STATIC (4); |
83 | | }; |
84 | | |
85 | | struct SegmentMaps : Array16Of<AxisValueMap> |
86 | | { |
87 | | int map (int value, unsigned int from_offset = 0, unsigned int to_offset = 1) const |
88 | 2.91k | { |
89 | 560k | #define fromCoord coords[from_offset].to_int () |
90 | 2.91k | #define toCoord coords[to_offset].to_int () |
91 | | /* The following special-cases are not part of OpenType, which requires |
92 | | * that at least -1, 0, and +1 must be mapped. But we include these as |
93 | | * part of a better error recovery scheme. */ |
94 | 2.91k | if (len < 2) |
95 | 2.06k | { |
96 | 2.06k | if (!len) |
97 | 1.74k | return value; |
98 | 322 | else /* len == 1*/ |
99 | 322 | return value - arrayZ[0].fromCoord + arrayZ[0].toCoord; |
100 | 2.06k | } |
101 | | |
102 | 851 | if (value <= arrayZ[0].fromCoord) |
103 | 369 | return value - arrayZ[0].fromCoord + arrayZ[0].toCoord; |
104 | | |
105 | 482 | unsigned int i; |
106 | 482 | unsigned int count = len - 1; |
107 | 558k | for (i = 1; i < count && value > arrayZ[i].fromCoord; i++) |
108 | 557k | ; |
109 | | |
110 | 482 | if (value >= arrayZ[i].fromCoord) |
111 | 177 | return value - arrayZ[i].fromCoord + arrayZ[i].toCoord; |
112 | | |
113 | 305 | if (unlikely (arrayZ[i-1].fromCoord == arrayZ[i].fromCoord)) |
114 | 0 | return arrayZ[i-1].toCoord; |
115 | | |
116 | 305 | int denom = arrayZ[i].fromCoord - arrayZ[i-1].fromCoord; |
117 | 305 | return roundf (arrayZ[i-1].toCoord + ((float) (arrayZ[i].toCoord - arrayZ[i-1].toCoord) * |
118 | 305 | (value - arrayZ[i-1].fromCoord)) / denom); |
119 | 305 | #undef toCoord |
120 | 305 | #undef fromCoord |
121 | 305 | } |
122 | | |
123 | 2.44k | int unmap (int value) const { return map (value, 1, 0); } |
124 | | |
125 | | public: |
126 | | DEFINE_SIZE_ARRAY (2, *this); |
127 | | }; |
128 | | |
129 | | struct avar |
130 | | { |
131 | | static constexpr hb_tag_t tableTag = HB_OT_TAG_avar; |
132 | | |
133 | 1.13k | bool has_data () const { return version.to_int (); } |
134 | | |
135 | | const SegmentMaps* get_segment_maps () const |
136 | 13 | { return &firstAxisSegmentMaps; } |
137 | | |
138 | | unsigned get_axis_count () const |
139 | 13 | { return axisCount; } |
140 | | |
141 | | bool sanitize (hb_sanitize_context_t *c) const |
142 | 1.18k | { |
143 | 1.18k | TRACE_SANITIZE (this); |
144 | 1.18k | if (!(version.sanitize (c) && |
145 | 1.18k | (version.major == 1 |
146 | 1.18k | #ifndef HB_NO_AVAR2 |
147 | 1.18k | || version.major == 2 |
148 | 1.18k | #endif |
149 | 1.18k | ) && |
150 | 1.18k | c->check_struct (this))) |
151 | 288 | return_trace (false); |
152 | | |
153 | 899 | const SegmentMaps *map = &firstAxisSegmentMaps; |
154 | 899 | unsigned int count = axisCount; |
155 | 570k | for (unsigned int i = 0; i < count; i++) |
156 | 569k | { |
157 | 569k | if (unlikely (!map->sanitize (c))) |
158 | 252 | return_trace (false); |
159 | 569k | map = &StructAfter<SegmentMaps> (*map); |
160 | 569k | } |
161 | | |
162 | 647 | #ifndef HB_NO_AVAR2 |
163 | 647 | if (version.major < 2) |
164 | 233 | return_trace (true); |
165 | | |
166 | 414 | const auto &v2 = * (const avarV2Tail *) map; |
167 | 414 | if (unlikely (!v2.sanitize (c, this))) |
168 | 195 | return_trace (false); |
169 | 219 | #endif |
170 | | |
171 | 414 | return_trace (true); |
172 | 414 | } |
173 | | |
174 | | void map_coords (int *coords, unsigned int coords_length) const |
175 | 67.9k | { |
176 | 67.9k | unsigned int count = hb_min (coords_length, axisCount); |
177 | | |
178 | 67.9k | const SegmentMaps *map = &firstAxisSegmentMaps; |
179 | 67.9k | for (unsigned int i = 0; i < count; i++) |
180 | 0 | { |
181 | 0 | coords[i] = map->map (coords[i]); |
182 | 0 | map = &StructAfter<SegmentMaps> (*map); |
183 | 0 | } |
184 | | |
185 | 67.9k | #ifndef HB_NO_AVAR2 |
186 | 67.9k | if (version.major < 2) |
187 | 67.9k | return; |
188 | | |
189 | 1.79k | for (; count < axisCount; count++) |
190 | 1.73k | map = &StructAfter<SegmentMaps> (*map); |
191 | | |
192 | 64 | const auto &v2 = * (const avarV2Tail *) map; |
193 | | |
194 | 64 | const auto &varidx_map = this+v2.varIdxMap; |
195 | 64 | const auto &var_store = this+v2.varStore; |
196 | 64 | auto *var_store_cache = var_store.create_cache (); |
197 | | |
198 | 64 | hb_vector_t<int> out; |
199 | 64 | out.alloc (coords_length); |
200 | 64 | for (unsigned i = 0; i < coords_length; i++) |
201 | 0 | { |
202 | 0 | int v = coords[i]; |
203 | 0 | uint32_t varidx = varidx_map.map (i); |
204 | 0 | float delta = var_store.get_delta (varidx, coords, coords_length, var_store_cache); |
205 | 0 | v += roundf (delta); |
206 | 0 | v = hb_clamp (v, -(1<<14), +(1<<14)); |
207 | 0 | out.push (v); |
208 | 0 | } |
209 | 64 | for (unsigned i = 0; i < coords_length; i++) |
210 | 0 | coords[i] = out[i]; |
211 | | |
212 | 64 | OT::VariationStore::destroy_cache (var_store_cache); |
213 | 64 | #endif |
214 | 64 | } |
215 | | |
216 | | void unmap_coords (int *coords, unsigned int coords_length) const |
217 | 56.2k | { |
218 | 56.2k | unsigned int count = hb_min (coords_length, axisCount); |
219 | | |
220 | 56.2k | const SegmentMaps *map = &firstAxisSegmentMaps; |
221 | 58.7k | for (unsigned int i = 0; i < count; i++) |
222 | 2.44k | { |
223 | 2.44k | coords[i] = map->unmap (coords[i]); |
224 | 2.44k | map = &StructAfter<SegmentMaps> (*map); |
225 | 2.44k | } |
226 | 56.2k | } |
227 | | |
228 | | protected: |
229 | | FixedVersion<>version; /* Version of the avar table |
230 | | * initially set to 0x00010000u */ |
231 | | HBUINT16 reserved; /* This field is permanently reserved. Set to 0. */ |
232 | | HBUINT16 axisCount; /* The number of variation axes in the font. This |
233 | | * must be the same number as axisCount in the |
234 | | * 'fvar' table. */ |
235 | | SegmentMaps firstAxisSegmentMaps; |
236 | | |
237 | | public: |
238 | | DEFINE_SIZE_MIN (8); |
239 | | }; |
240 | | |
241 | | } /* namespace OT */ |
242 | | |
243 | | |
244 | | #endif /* HB_OT_VAR_AVAR_TABLE_HH */ |