read_fonts/tables/
avar.rs1use super::variations::{DeltaSetIndexMap, ItemVariationStore};
4
5include!("../../generated/generated_avar.rs");
6
7impl SegmentMaps<'_> {
8 pub fn apply(&self, coord: Fixed) -> Fixed {
11 let maps = self.axis_value_maps();
12 let len = maps.len();
13
14 #[inline]
16 fn from(m: &AxisValueMap) -> Fixed {
17 m.from_coordinate().to_fixed()
18 }
19 #[inline]
20 fn to_(m: &AxisValueMap) -> Fixed {
21 m.to_coordinate().to_fixed()
22 }
23
24 if len < 2 {
26 return if len == 0 {
27 coord
28 } else {
29 coord - from(&maps[0]) + to_(&maps[0])
31 };
32 }
33
34 let neg1 = Fixed::from_i32(-1);
37 let pos1 = Fixed::from_i32(1);
38
39 let mut start = 0usize;
40 let mut end = len;
41
42 if from(&maps[start]) == neg1 && to_(&maps[start]) == neg1 && from(&maps[start + 1]) == neg1
43 {
44 start += 1;
45 }
46
47 if from(&maps[end - 1]) == pos1
48 && to_(&maps[end - 1]) == pos1
49 && from(&maps[end - 2]) == pos1
50 {
51 end -= 1;
52 }
53
54 let mut i = start;
56 while i < end {
57 if coord == from(&maps[i]) {
58 break;
59 }
60 i += 1;
61 }
62
63 if i < end {
64 let mut j = i;
66 while j + 1 < end && coord == from(&maps[j + 1]) {
67 j += 1;
68 }
69
70 if i == j {
74 return to_(&maps[i]);
75 }
76
77 if i + 2 == j {
79 return to_(&maps[i + 1]);
80 }
81
82 if coord < Fixed::ZERO {
85 return to_(&maps[j]);
86 }
87 if coord > Fixed::ZERO {
88 return to_(&maps[i]);
89 }
90
91 let ti = to_(&maps[i]);
93 let tj = to_(&maps[j]);
94 return if ti.abs() <= tj.abs() { ti } else { tj };
95 }
96
97 let mut k = start;
99 while k < end {
100 if coord < from(&maps[k]) {
101 break;
102 }
103 k += 1;
104 }
105
106 if k == 0 {
107 return coord - from(&maps[0]) + to_(&maps[0]);
109 }
110 if k == end {
111 return coord - from(&maps[end - 1]) + to_(&maps[end - 1]);
113 }
114
115 let before = &maps[k - 1];
117 let after = &maps[k];
118
119 let bf = from(before);
120 let bt = to_(before);
121 let af = from(after);
122 let at = to_(after);
123
124 let denom = af - bf; bt + (at - bt).mul_div(coord - bf, denom)
126 }
127}
128
129impl VarSize for SegmentMaps<'_> {
130 type Size = u16;
131
132 fn read_len_at(data: FontData, pos: usize) -> Option<usize> {
133 Some(
134 data.read_at::<u16>(pos).ok()? as usize * AxisValueMap::RAW_BYTE_LEN
135 + u16::RAW_BYTE_LEN,
136 )
137 }
138}
139
140impl<'a> FontRead<'a> for SegmentMaps<'a> {
141 fn read(data: FontData<'a>) -> Result<Self, ReadError> {
142 let mut cursor = data.cursor();
143 let position_map_count: BigEndian<u16> = cursor.read_be()?;
144 let axis_value_maps = cursor.read_array(position_map_count.get() as _)?;
145 Ok(SegmentMaps {
146 position_map_count,
147 axis_value_maps,
148 })
149 }
150}
151
152#[cfg(test)]
153mod tests {
154
155 use font_test_data::bebuffer::BeBuffer;
156
157 use super::*;
158 use crate::{FontRef, TableProvider};
159
160 fn value_map(from: f32, to: f32) -> [F2Dot14; 2] {
161 [F2Dot14::from_f32(from), F2Dot14::from_f32(to)]
162 }
163
164 impl PartialEq<[F2Dot14; 2]> for AxisValueMap {
167 fn eq(&self, other: &[F2Dot14; 2]) -> bool {
168 self.from_coordinate == other[0] && self.to_coordinate == other[1]
169 }
170 }
171
172 #[test]
173 fn segment_maps() {
174 let font = FontRef::new(font_test_data::VAZIRMATN_VAR).unwrap();
175 let avar = font.avar().unwrap();
176 assert_eq!(avar.axis_count(), 1);
177 let expected_segment_maps = &[vec![
178 value_map(-1.0, -1.0),
179 value_map(-0.6667, -0.5),
180 value_map(-0.3333, -0.25),
181 value_map(0.0, 0.0),
182 value_map(0.2, 0.3674),
183 value_map(0.4, 0.52246),
184 value_map(0.6, 0.67755),
185 value_map(0.8, 0.83875),
186 value_map(1.0, 1.0),
187 ]];
188 let segment_maps = avar
189 .axis_segment_maps()
190 .iter()
191 .map(|segment_map| segment_map.unwrap().axis_value_maps().to_owned())
192 .collect::<Vec<_>>();
193 assert_eq!(segment_maps, expected_segment_maps);
194 }
195
196 #[test]
197 fn segment_maps_multi_axis() {
198 let segment_one_maps = [
199 value_map(-1.0, -1.0),
200 value_map(-0.6667, -0.5),
201 value_map(-0.3333, -0.25),
202 ];
203 let segment_two_maps = [value_map(0.8, 0.83875), value_map(1.0, 1.0)];
204
205 let data = BeBuffer::new()
206 .push(MajorMinor::VERSION_1_0)
207 .push(0u16) .push(2u16) .push(3u16) .extend(segment_one_maps[0])
212 .extend(segment_one_maps[1])
213 .extend(segment_one_maps[2])
214 .push(2u16) .extend(segment_two_maps[0])
217 .extend(segment_two_maps[1]);
218
219 let avar = super::Avar::read(data.data().into()).unwrap();
220 assert_eq!(avar.axis_segment_maps().iter().count(), 2);
221 assert_eq!(
222 avar.axis_segment_maps()
223 .get(0)
224 .unwrap()
225 .unwrap()
226 .axis_value_maps,
227 segment_one_maps,
228 );
229 assert_eq!(
230 avar.axis_segment_maps()
231 .get(1)
232 .unwrap()
233 .unwrap()
234 .axis_value_maps,
235 segment_two_maps,
236 );
237 }
238
239 #[test]
240 fn piecewise_linear() {
241 let font = FontRef::new(font_test_data::VAZIRMATN_VAR).unwrap();
242 let avar = font.avar().unwrap();
243 let segment_map = avar.axis_segment_maps().get(0).unwrap().unwrap();
244 let coords = [-1.0, -0.5, 0.0, 0.5, 1.0];
245 let expected_result = [-1.0, -0.375, 0.0, 0.600006103515625, 1.0];
246 assert_eq!(
247 &expected_result[..],
248 &coords
249 .iter()
250 .map(|coord| segment_map.apply(Fixed::from_f64(*coord)).to_f64())
251 .collect::<Vec<_>>()
252 );
253 }
254
255 #[test]
256 fn avar2() {
257 let font = FontRef::new(font_test_data::AVAR2_CHECKER).unwrap();
258 let avar = font.avar().unwrap();
259 assert_eq!(avar.version(), MajorMinor::VERSION_2_0);
260 assert!(avar.axis_index_map_offset().is_some());
261 assert!(avar.var_store_offset().is_some());
262 assert!(avar.var_store().is_some());
263 }
264}