Skip to main content

read_fonts/tables/
vvar.rs

1//! The [VVAR (Vertical Metrics Variation)](https://docs.microsoft.com/en-us/typography/opentype/spec/vvar) table
2
3use super::variations::{self, DeltaSetIndexMap, ItemVariationStore};
4use types::F48Dot16;
5
6include!("../../generated/generated_vvar.rs");
7
8impl Vvar<'_> {
9    /// Computes the scalar for each variation region at `coords`, in table
10    /// order, and returns how many were written.
11    ///
12    /// See [`ItemVariationStore::compute_scalars`] for what `out` receives.
13    ///
14    /// [`ItemVariationStore::compute_scalars`]: crate::tables::variations::ItemVariationStore::compute_scalars
15    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    /// Returns the change a location makes to the advance height of a glyph.
23    ///
24    /// The value carries every bit the item variation store computed. It is
25    /// a caller that decides how to round it into a whole design unit, and
26    /// implementations differ on that.
27    ///
28    /// Returns `None` where the table says nothing readable about the glyph.
29    pub fn advance_delta(&self, glyph_id: GlyphId, coords: &[F2Dot14]) -> Option<F48Dot16> {
30        self.advance_delta_with_scalars(glyph_id, coords, &[])
31    }
32
33    /// Returns the change a location makes to the advance height of a glyph, reusing
34    /// `scalars`.
35    ///
36    /// `scalars` holds one entry per variation region, from
37    /// [`compute_scalars`](Self::compute_scalars).
38    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_height_mapping(),
46            self.item_variation_store(),
47            glyph_id,
48            coords,
49            scalars,
50        )
51    }
52
53    /// Returns the change a location makes to the top side bearing of a glyph.
54    ///
55    /// The value carries every bit the item variation store computed. It is
56    /// a caller that decides how to round it into a whole design unit, and
57    /// implementations differ on that.
58    ///
59    /// Returns `None` where the table says nothing readable about the glyph.
60    pub fn tsb_delta(&self, glyph_id: GlyphId, coords: &[F2Dot14]) -> Option<F48Dot16> {
61        self.tsb_delta_with_scalars(glyph_id, coords, &[])
62    }
63
64    /// Returns the change a location makes to the top side bearing of a glyph, reusing
65    /// `scalars`.
66    ///
67    /// `scalars` holds one entry per variation region, from
68    /// [`compute_scalars`](Self::compute_scalars).
69    pub fn tsb_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.tsb_mapping(),
77            self.item_variation_store(),
78            glyph_id,
79            coords,
80            scalars,
81        )
82    }
83
84    /// Returns the change a location makes to the bottom side bearing of a glyph.
85    ///
86    /// The value carries every bit the item variation store computed. It is
87    /// a caller that decides how to round it into a whole design unit, and
88    /// implementations differ on that.
89    ///
90    /// Returns `None` where the table says nothing readable about the glyph.
91    pub fn bsb_delta(&self, glyph_id: GlyphId, coords: &[F2Dot14]) -> Option<F48Dot16> {
92        self.bsb_delta_with_scalars(glyph_id, coords, &[])
93    }
94
95    /// Returns the change a location makes to the bottom side bearing of a glyph, reusing
96    /// `scalars`.
97    ///
98    /// `scalars` holds one entry per variation region, from
99    /// [`compute_scalars`](Self::compute_scalars).
100    pub fn bsb_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.bsb_mapping(),
108            self.item_variation_store(),
109            glyph_id,
110            coords,
111            scalars,
112        )
113    }
114
115    /// Returns the change a location makes to the y coordinate of the vertical origin of a glyph.
116    ///
117    /// The value carries every bit the item variation store computed. It is
118    /// a caller that decides how to round it into a whole design unit, and
119    /// implementations differ on that.
120    ///
121    /// Returns `None` where the table says nothing readable about the glyph.
122    pub fn v_origin_y_delta(&self, glyph_id: GlyphId, coords: &[F2Dot14]) -> Option<F48Dot16> {
123        self.v_origin_y_delta_with_scalars(glyph_id, coords, &[])
124    }
125
126    /// Returns the change a location makes to the y coordinate of the vertical origin of a glyph, reusing
127    /// `scalars`.
128    ///
129    /// `scalars` holds one entry per variation region, from
130    /// [`compute_scalars`](Self::compute_scalars).
131    pub fn v_origin_y_delta_with_scalars(
132        &self,
133        glyph_id: GlyphId,
134        coords: &[F2Dot14],
135        scalars: &[Fixed],
136    ) -> Option<F48Dot16> {
137        variations::item_delta_with_scalars(
138            self.v_org_mapping(),
139            self.item_variation_store(),
140            glyph_id,
141            coords,
142            scalars,
143        )
144    }
145}
146
147#[cfg(test)]
148mod tests {
149    use crate::{FontRef, TableProvider};
150    use types::{F2Dot14, F48Dot16, GlyphId};
151
152    /// The only fixture stating `VVAR`. Every one of its mappings resolves to
153    /// zero at every location, so this covers the shape of the accessors
154    /// rather than their arithmetic; `HVAR` carries that, over the same code
155    /// in `variations`.
156    const VAR: &[u8] = font_test_data::ift::CFF2_FONT;
157
158    #[test]
159    fn a_default_location_moves_nothing() {
160        let font = FontRef::new(VAR).unwrap();
161        let vvar = font.vvar().unwrap();
162        let gid = GlyphId::new(1);
163        assert_eq!(vvar.advance_delta(gid, &[]), Some(F48Dot16::ZERO));
164    }
165
166    #[test]
167    fn every_glyph_answers_at_a_location() {
168        let font = FontRef::new(VAR).unwrap();
169        let vvar = font.vvar().unwrap();
170        let num_glyphs = font.maxp().unwrap().num_glyphs() as u32;
171        for coord in [-1.0, -0.4, 0.25, 1.0] {
172            let coords = [F2Dot14::from_f32(coord)];
173            for gid in (0..num_glyphs).map(GlyphId::new) {
174                assert!(
175                    vvar.advance_delta(gid, &coords).is_some(),
176                    "glyph {gid} at {coord}"
177                );
178            }
179        }
180    }
181
182    #[test]
183    fn a_mapping_the_font_does_not_state_is_absent() {
184        // This font maps advance heights and vertical origins, but neither
185        // side bearing.
186        let font = FontRef::new(VAR).unwrap();
187        let vvar = font.vvar().unwrap();
188        let gid = GlyphId::new(1);
189        let coords = [F2Dot14::from_f32(-1.0)];
190        assert!(vvar.advance_delta(gid, &coords).is_some());
191        assert!(vvar.v_origin_y_delta(gid, &coords).is_some());
192        assert_eq!(vvar.tsb_delta(gid, &coords), None);
193        assert_eq!(vvar.bsb_delta(gid, &coords), None);
194    }
195}