Skip to main content

read_fonts/generated/
generated_hvar.rs

1// THIS FILE IS AUTOGENERATED.
2// Any changes to this file will be overwritten.
3// For more information about how codegen works, see font-codegen/README.md
4
5#[allow(unused_imports)]
6use crate::codegen_prelude::*;
7
8impl<'a> MinByteRange<'a> for Hvar<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.rsb_mapping_offset_byte_range().end
11    }
12    fn min_table_bytes(&self) -> &'a [u8] {
13        let range = self.min_byte_range();
14        self.data.as_bytes().get(range).unwrap_or_default()
15    }
16}
17
18impl TopLevelTable for Hvar<'_> {
19    /// `HVAR`
20    const TAG: Tag = Tag::new(b"HVAR");
21}
22
23impl<'a> FontRead<'a> for Hvar<'a> {
24    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
25        #[allow(clippy::absurd_extreme_comparisons)]
26        if data.len() < Self::MIN_SIZE {
27            return Err(ReadError::OutOfBounds);
28        }
29        Ok(Self { data })
30    }
31}
32
33/// The [HVAR (Horizontal Metrics Variations)](https://docs.microsoft.com/en-us/typography/opentype/spec/hvar) table
34#[derive(Clone)]
35pub struct Hvar<'a> {
36    data: FontData<'a>,
37}
38
39#[allow(clippy::needless_lifetimes)]
40impl<'a> Hvar<'a> {
41    pub const MIN_SIZE: usize = (MajorMinor::RAW_BYTE_LEN
42        + Offset32::RAW_BYTE_LEN
43        + Offset32::RAW_BYTE_LEN
44        + Offset32::RAW_BYTE_LEN
45        + Offset32::RAW_BYTE_LEN);
46    basic_table_impls!(impl_the_methods);
47
48    /// Major version number of the horizontal metrics variations table — set to 1.
49    /// Minor version number of the horizontal metrics variations table — set to 0.
50    pub fn version(&self) -> MajorMinor {
51        let range = self.version_byte_range();
52        self.data.read_at(range.start).ok().unwrap()
53    }
54
55    /// Offset in bytes from the start of this table to the item variation store table.
56    pub fn item_variation_store_offset(&self) -> Offset32 {
57        let range = self.item_variation_store_offset_byte_range();
58        self.data.read_at(range.start).ok().unwrap()
59    }
60
61    /// Attempt to resolve [`item_variation_store_offset`][Self::item_variation_store_offset].
62    pub fn item_variation_store(&self) -> Result<ItemVariationStore<'a>, ReadError> {
63        let data = self.data;
64        self.item_variation_store_offset().resolve(data)
65    }
66
67    /// Offset in bytes from the start of this table to the delta-set index mapping for advance widths (may be NULL).
68    pub fn advance_width_mapping_offset(&self) -> Nullable<Offset32> {
69        let range = self.advance_width_mapping_offset_byte_range();
70        self.data.read_at(range.start).ok().unwrap()
71    }
72
73    /// Attempt to resolve [`advance_width_mapping_offset`][Self::advance_width_mapping_offset].
74    pub fn advance_width_mapping(&self) -> Option<Result<DeltaSetIndexMap<'a>, ReadError>> {
75        let data = self.data;
76        self.advance_width_mapping_offset().resolve(data)
77    }
78
79    /// Offset in bytes from the start of this table to the delta-set index mapping for left side bearings (may be NULL).
80    pub fn lsb_mapping_offset(&self) -> Nullable<Offset32> {
81        let range = self.lsb_mapping_offset_byte_range();
82        self.data.read_at(range.start).ok().unwrap()
83    }
84
85    /// Attempt to resolve [`lsb_mapping_offset`][Self::lsb_mapping_offset].
86    pub fn lsb_mapping(&self) -> Option<Result<DeltaSetIndexMap<'a>, ReadError>> {
87        let data = self.data;
88        self.lsb_mapping_offset().resolve(data)
89    }
90
91    /// Offset in bytes from the start of this table to the delta-set index mapping for right side bearings (may be NULL).
92    pub fn rsb_mapping_offset(&self) -> Nullable<Offset32> {
93        let range = self.rsb_mapping_offset_byte_range();
94        self.data.read_at(range.start).ok().unwrap()
95    }
96
97    /// Attempt to resolve [`rsb_mapping_offset`][Self::rsb_mapping_offset].
98    pub fn rsb_mapping(&self) -> Option<Result<DeltaSetIndexMap<'a>, ReadError>> {
99        let data = self.data;
100        self.rsb_mapping_offset().resolve(data)
101    }
102
103    pub fn version_byte_range(&self) -> Range<usize> {
104        let start = 0;
105        let end = start + MajorMinor::RAW_BYTE_LEN;
106        start..end
107    }
108
109    pub fn item_variation_store_offset_byte_range(&self) -> Range<usize> {
110        let start = self.version_byte_range().end;
111        let end = start + Offset32::RAW_BYTE_LEN;
112        start..end
113    }
114
115    pub fn advance_width_mapping_offset_byte_range(&self) -> Range<usize> {
116        let start = self.item_variation_store_offset_byte_range().end;
117        let end = start + Offset32::RAW_BYTE_LEN;
118        start..end
119    }
120
121    pub fn lsb_mapping_offset_byte_range(&self) -> Range<usize> {
122        let start = self.advance_width_mapping_offset_byte_range().end;
123        let end = start + Offset32::RAW_BYTE_LEN;
124        start..end
125    }
126
127    pub fn rsb_mapping_offset_byte_range(&self) -> Range<usize> {
128        let start = self.lsb_mapping_offset_byte_range().end;
129        let end = start + Offset32::RAW_BYTE_LEN;
130        start..end
131    }
132}
133
134const _: () = assert!(FontData::default_data_long_enough(Hvar::MIN_SIZE));
135
136impl Default for Hvar<'_> {
137    fn default() -> Self {
138        Self {
139            data: FontData::default_table_data(),
140        }
141    }
142}
143
144#[cfg(feature = "experimental_traverse")]
145impl<'a> SomeTable<'a> for Hvar<'a> {
146    fn type_name(&self) -> &str {
147        "Hvar"
148    }
149    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
150        match idx {
151            0usize => Some(Field::new("version", self.version())),
152            1usize => Some(Field::new(
153                "item_variation_store_offset",
154                FieldType::offset(
155                    self.item_variation_store_offset(),
156                    self.item_variation_store(),
157                ),
158            )),
159            2usize => Some(Field::new(
160                "advance_width_mapping_offset",
161                FieldType::offset(
162                    self.advance_width_mapping_offset(),
163                    self.advance_width_mapping(),
164                ),
165            )),
166            3usize => Some(Field::new(
167                "lsb_mapping_offset",
168                FieldType::offset(self.lsb_mapping_offset(), self.lsb_mapping()),
169            )),
170            4usize => Some(Field::new(
171                "rsb_mapping_offset",
172                FieldType::offset(self.rsb_mapping_offset(), self.rsb_mapping()),
173            )),
174            _ => None,
175        }
176    }
177}
178
179#[cfg(feature = "experimental_traverse")]
180#[allow(clippy::needless_lifetimes)]
181impl<'a> std::fmt::Debug for Hvar<'a> {
182    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
183        (self as &dyn SomeTable<'a>).fmt(f)
184    }
185}