read_fonts/tables/
stat.rs

1//! The [STAT](https://learn.microsoft.com/en-us/typography/opentype/spec/stat) table
2
3include!("../../generated/generated_stat.rs");
4
5impl AxisValue<'_> {
6    /// Returns axis value for format 1, 2 and 3 axis value tables
7    ///
8    /// For axis value format 2, returns the nominal value.
9    pub fn value(&self) -> Option<Fixed> {
10        match self {
11            Self::Format1(item) => Some(item.value()),
12            Self::Format2(item) => Some(item.nominal_value()),
13            Self::Format3(item) => Some(item.value()),
14            Self::Format4(_) => None,
15        }
16    }
17
18    /// Returns linked value for format 3 axis value tables
19    pub fn linked_value(&self) -> Option<Fixed> {
20        match self {
21            Self::Format3(item) => Some(item.linked_value()),
22            _ => None,
23        }
24    }
25
26    /// Returns axis index for format 1, 2 and 3 axis value tables
27    pub fn axis_index(&self) -> Option<u16> {
28        match self {
29            Self::Format1(item) => Some(item.axis_index()),
30            Self::Format2(item) => Some(item.axis_index()),
31            Self::Format3(item) => Some(item.axis_index()),
32            Self::Format4(_) => None,
33        }
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use types::{Fixed, NameId};
40
41    use crate::{table_provider::TableProvider, FontRef};
42
43    use super::*;
44
45    #[test]
46    fn smoke_test() {
47        let font = FontRef::new(font_test_data::VAZIRMATN_VAR).unwrap();
48        let table = font.stat().unwrap();
49        assert_eq!(table.design_axis_count(), 1);
50        let axis_record = &table.design_axes().unwrap()[0];
51        assert_eq!(axis_record.axis_tag(), Tag::new(b"wght"));
52        assert_eq!(axis_record.axis_name_id(), NameId::new(257));
53        assert_eq!(axis_record.axis_ordering(), 0);
54        let axis_values = table.offset_to_axis_values().unwrap().unwrap();
55        let axis_values = axis_values
56            .axis_values()
57            .iter()
58            .map(|x| x.unwrap())
59            .collect::<Vec<_>>();
60
61        assert_eq!(axis_values.len(), 3);
62        let last = &axis_values[2];
63        if let AxisValue::Format1(table) = last {
64            assert_eq!(table.axis_index(), 0);
65            assert_eq!(table.value_name_id(), NameId::new(264));
66            assert_eq!(table.value(), Fixed::from_f64(700.0));
67        }
68    }
69}