1use super::variations::{self, DeltaSetIndexMap, ItemVariationStore};
4use types::F48Dot16;
5
6include!("../../generated/generated_hvar.rs");
7
8impl Hvar<'_> {
9 pub fn compute_scalars(&self, coords: &[F2Dot14], out: &mut [Fixed]) -> usize {
16 match self.item_variation_store() {
17 Ok(store) => store.compute_scalars(coords, out),
18 Err(_) => 0,
19 }
20 }
21
22 pub fn advance_delta(&self, glyph_id: GlyphId, coords: &[F2Dot14]) -> Option<F48Dot16> {
30 self.advance_delta_with_scalars(glyph_id, coords, &[])
31 }
32
33 pub fn advance_delta_with_scalars(
39 &self,
40 glyph_id: GlyphId,
41 coords: &[F2Dot14],
42 scalars: &[Fixed],
43 ) -> Option<F48Dot16> {
44 variations::advance_delta_with_scalars(
45 self.advance_width_mapping(),
46 self.item_variation_store(),
47 glyph_id,
48 coords,
49 scalars,
50 )
51 }
52
53 pub fn lsb_delta(&self, glyph_id: GlyphId, coords: &[F2Dot14]) -> Option<F48Dot16> {
61 self.lsb_delta_with_scalars(glyph_id, coords, &[])
62 }
63
64 pub fn lsb_delta_with_scalars(
70 &self,
71 glyph_id: GlyphId,
72 coords: &[F2Dot14],
73 scalars: &[Fixed],
74 ) -> Option<F48Dot16> {
75 variations::item_delta_with_scalars(
76 self.lsb_mapping(),
77 self.item_variation_store(),
78 glyph_id,
79 coords,
80 scalars,
81 )
82 }
83
84 pub fn rsb_delta(&self, glyph_id: GlyphId, coords: &[F2Dot14]) -> Option<F48Dot16> {
92 self.rsb_delta_with_scalars(glyph_id, coords, &[])
93 }
94
95 pub fn rsb_delta_with_scalars(
101 &self,
102 glyph_id: GlyphId,
103 coords: &[F2Dot14],
104 scalars: &[Fixed],
105 ) -> Option<F48Dot16> {
106 variations::item_delta_with_scalars(
107 self.rsb_mapping(),
108 self.item_variation_store(),
109 glyph_id,
110 coords,
111 scalars,
112 )
113 }
114}
115
116#[cfg(test)]
117mod tests {
118 use crate::{tables::variations::DeltaSetIndexMap, FontRef, TableProvider};
119 use types::{F2Dot14, F48Dot16, GlyphId};
120
121 #[test]
122 fn a_delta_keeps_the_fraction_the_variation_store_computed() {
123 let font = FontRef::new(font_test_data::VAZIRMATN_VAR).unwrap();
127 let hvar = font.hvar().unwrap();
128 let gid = GlyphId::new(1);
129 for (coord, expected) in [(-1.0, -113.0), (-0.75, -84.75), (-0.5, -56.5)] {
130 let coords = [F2Dot14::from_f32(coord)];
131 assert_eq!(
132 hvar.advance_delta(gid, &coords),
133 Some(F48Dot16::from_f64(expected)),
134 "at {coord}"
135 );
136 }
137 }
138
139 #[test]
140 fn a_default_location_moves_nothing() {
141 let font = FontRef::new(font_test_data::VAZIRMATN_VAR).unwrap();
142 let hvar = font.hvar().unwrap();
143 let gid = GlyphId::new(1);
144 assert_eq!(hvar.advance_delta(gid, &[]), Some(F48Dot16::ZERO));
145 assert_eq!(hvar.lsb_delta(gid, &[]), Some(F48Dot16::ZERO));
146 assert_eq!(hvar.rsb_delta(gid, &[]), Some(F48Dot16::ZERO));
147 }
148
149 #[test]
156 fn a_cached_scalar_changes_nothing() {
157 use types::Fixed;
158
159 let font = FontRef::new(font_test_data::VAZIRMATN_VAR).unwrap();
160 let hvar = font.hvar().unwrap();
161 let coords = [F2Dot14::from_f32(-0.75)];
162
163 let mut buf = [Fixed::ZERO; 32];
164 let written = hvar.compute_scalars(&coords, &mut buf);
165 assert!(written > 0, "the font varies, so something must be written");
166
167 for gid in (0..font.maxp().unwrap().num_glyphs()).map(GlyphId::from) {
168 let plain = hvar.advance_delta(gid, &coords);
169 for (name, scalars) in [
170 ("full", &buf[..written]),
171 ("partial", &buf[..written / 2]),
172 ("empty", &[][..]),
173 ] {
174 assert_eq!(
175 hvar.advance_delta_with_scalars(gid, &coords, scalars),
176 plain,
177 "{name} cache disagreed at {gid}"
178 );
179 }
180 }
181 }
182
183 #[test]
186 fn compute_scalars_reports_what_it_wrote() {
187 use types::Fixed;
188 let font = FontRef::new(font_test_data::VAZIRMATN_VAR).unwrap();
189 let hvar = font.hvar().unwrap();
190 let coords = [F2Dot14::from_f32(-0.75)];
191 let mut big = [Fixed::ZERO; 32];
192 let all = hvar.compute_scalars(&coords, &mut big);
193 let mut small = [Fixed::ZERO; 1];
195 assert_eq!(hvar.compute_scalars(&coords, &mut small), all.min(1));
196 assert_eq!(small[0], big[0]);
197 assert_eq!(hvar.compute_scalars(&coords, &mut []), 0);
199 }
200
201 #[test]
202 fn a_mapping_the_font_does_not_state_is_absent() {
203 let font = FontRef::new(font_test_data::VAZIRMATN_VAR).unwrap();
205 let hvar = font.hvar().unwrap();
206 let gid = GlyphId::new(1);
207 let coords = [F2Dot14::from_f32(-0.75)];
208 assert!(hvar.advance_delta(gid, &coords).is_some());
209 assert_eq!(hvar.lsb_delta(gid, &coords), None);
210 assert_eq!(hvar.rsb_delta(gid, &coords), None);
211 }
212
213 #[test]
214 fn advance_deltas() {
215 let font = FontRef::new(font_test_data::VAZIRMATN_VAR).unwrap();
216 let hvar = font.hvar().unwrap();
217 let gid_a = GlyphId::new(1);
218 for (coord, expected) in [
221 (-1.0, -113.0),
222 (-0.75, -84.75),
223 (-0.5, -56.5),
224 (0.0, 0.0),
225 (0.5, 29.5),
226 (1.0, 59.0),
227 ] {
228 assert_eq!(
229 hvar.advance_delta(gid_a, &[F2Dot14::from_f32(coord)]),
230 Some(F48Dot16::from_f64(expected)),
231 "at {coord}"
232 );
233 }
234 }
235
236 #[test]
237 fn advance_deltas_from_hvar_with_truncated_adv_index_map() {
238 let font = FontRef::new(font_test_data::HVAR_WITH_TRUNCATED_ADVANCE_INDEX_MAP).unwrap();
239 let maxp = font.maxp().unwrap();
240 let num_glyphs = maxp.num_glyphs();
241 let hvar = font.hvar().unwrap();
242 let Ok(DeltaSetIndexMap::Format0(adv_index_map)) = hvar.advance_width_mapping().unwrap()
243 else {
244 panic!("Expected DeltaSetIndexMap::Format0 for hvar.advance_width_mapping()");
245 };
246 assert!(adv_index_map.map_count() < num_glyphs);
247 assert_eq!(num_glyphs, 24);
248 assert_eq!(adv_index_map.map_count(), 15);
249 let last_mapped_gid = adv_index_map.map_count() - 1;
250 for idx in last_mapped_gid..num_glyphs {
254 let gid = GlyphId::new(idx as _);
255 assert_eq!(
256 hvar.advance_delta(gid, &[F2Dot14::from_f32(1.0)]),
257 Some(F48Dot16::from_f64(100.0))
258 );
259 }
260 }
261}