read_fonts/tables/
vorg.rs

1//! The [VORG (Vertical Origin)](https://docs.microsoft.com/en-us/typography/opentype/spec/vorg) table.
2
3include!("../../generated/generated_vorg.rs");
4
5impl Vorg<'_> {
6    /// Returns the y coordinate of the of the glyph's vertical origin.
7    pub fn vertical_origin_y(&self, glyph_id: GlyphId) -> i16 {
8        let gid = glyph_id.to_u32();
9        let metrics = self.vert_origin_y_metrics();
10        match metrics.binary_search_by(|rec| rec.glyph_index().to_u32().cmp(&gid)) {
11            Ok(ix) => metrics
12                .get(ix)
13                .map(|metric| metric.vert_origin_y())
14                .unwrap_or_default(),
15            _ => self.default_vert_origin_y(),
16        }
17    }
18}
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23    use crate::{FontRef, TableProvider};
24
25    #[test]
26    fn vertical_origins() {
27        let font = FontRef::new(font_test_data::VORG).unwrap();
28        let vorg = font.vorg().unwrap();
29        // Glyphs 1 and 3 have entries while 0 and 2 use the default value
30        // of 880
31        assert_eq!(vorg.vertical_origin_y(GlyphId::new(0)), 880);
32        assert_eq!(vorg.vertical_origin_y(GlyphId::new(1)), 867);
33        assert_eq!(vorg.vertical_origin_y(GlyphId::new(2)), 880);
34        assert_eq!(vorg.vertical_origin_y(GlyphId::new(3)), 824);
35    }
36}