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