read_fonts/tables/
hvar.rs

1//! The [HVAR (Horizontal Metrics Variation)](https://docs.microsoft.com/en-us/typography/opentype/spec/hvar) table
2
3use super::variations::{self, DeltaSetIndexMap, ItemVariationStore};
4
5include!("../../generated/generated_hvar.rs");
6
7impl Hvar<'_> {
8    /// Returns the advance width delta for the specified glyph identifier and
9    /// normalized variation coordinates.
10    pub fn advance_width_delta(
11        &self,
12        glyph_id: GlyphId,
13        coords: &[F2Dot14],
14    ) -> Result<Fixed, ReadError> {
15        variations::advance_delta(
16            self.advance_width_mapping(),
17            self.item_variation_store(),
18            glyph_id,
19            coords,
20        )
21    }
22
23    /// Returns the left side bearing delta for the specified glyph identifier and
24    /// normalized variation coordinates.
25    pub fn lsb_delta(&self, glyph_id: GlyphId, coords: &[F2Dot14]) -> Result<Fixed, ReadError> {
26        variations::item_delta(
27            self.lsb_mapping(),
28            self.item_variation_store(),
29            glyph_id,
30            coords,
31        )
32    }
33
34    /// Returns the left side bearing delta for the specified glyph identifier and
35    /// normalized variation coordinates.
36    pub fn rsb_delta(&self, glyph_id: GlyphId, coords: &[F2Dot14]) -> Result<Fixed, ReadError> {
37        variations::item_delta(
38            self.rsb_mapping(),
39            self.item_variation_store(),
40            glyph_id,
41            coords,
42        )
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use crate::{tables::variations::DeltaSetIndexMap, FontRef, TableProvider};
49    use types::{F2Dot14, Fixed, GlyphId};
50
51    #[test]
52    fn advance_deltas() {
53        let font = FontRef::new(font_test_data::VAZIRMATN_VAR).unwrap();
54        let hvar = font.hvar().unwrap();
55        let gid_a = GlyphId::new(1);
56        assert_eq!(
57            hvar.advance_width_delta(gid_a, &[F2Dot14::from_f32(-1.0)])
58                .unwrap(),
59            Fixed::from_f64(-113.0)
60        );
61        assert_eq!(
62            hvar.advance_width_delta(gid_a, &[F2Dot14::from_f32(-0.75)])
63                .unwrap(),
64            Fixed::from_f64(-85.0)
65        );
66        assert_eq!(
67            hvar.advance_width_delta(gid_a, &[F2Dot14::from_f32(-0.5)])
68                .unwrap(),
69            Fixed::from_f64(-56.0)
70        );
71        assert_eq!(
72            hvar.advance_width_delta(gid_a, &[F2Dot14::from_f32(0.0)])
73                .unwrap(),
74            Fixed::from_f64(0.0)
75        );
76        assert_eq!(
77            hvar.advance_width_delta(gid_a, &[F2Dot14::from_f32(0.5)])
78                .unwrap(),
79            Fixed::from_f64(30.0)
80        );
81        assert_eq!(
82            hvar.advance_width_delta(gid_a, &[F2Dot14::from_f32(1.0)])
83                .unwrap(),
84            Fixed::from_f64(59.0)
85        );
86    }
87
88    #[test]
89    fn advance_deltas_from_hvar_with_truncated_adv_index_map() {
90        let font = FontRef::new(font_test_data::HVAR_WITH_TRUNCATED_ADVANCE_INDEX_MAP).unwrap();
91        let maxp = font.maxp().unwrap();
92        let num_glyphs = maxp.num_glyphs();
93        let hvar = font.hvar().unwrap();
94        let Ok(DeltaSetIndexMap::Format0(adv_index_map)) = hvar.advance_width_mapping().unwrap()
95        else {
96            panic!("Expected DeltaSetIndexMap::Format0 for hvar.advance_width_mapping()");
97        };
98        assert!(adv_index_map.map_count() < num_glyphs);
99        assert_eq!(num_glyphs, 24);
100        assert_eq!(adv_index_map.map_count(), 15);
101        let last_mapped_gid = adv_index_map.map_count() - 1;
102        // We expect the last 10 glyphs to have the same advance width delta as the last mapped glyph.
103        // Crucially, hvar.advance_width_delta() should not return OutOfBounds for these glyphs.
104        for idx in last_mapped_gid..num_glyphs {
105            let gid = GlyphId::new(idx as _);
106            assert_eq!(
107                hvar.advance_width_delta(gid, &[F2Dot14::from_f32(1.0)])
108                    .unwrap(),
109                Fixed::from_f64(100.0)
110            );
111        }
112    }
113}