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        start..start + MajorMinor::RAW_BYTE_LEN
106    }
107
108    pub fn item_variation_store_offset_byte_range(&self) -> Range<usize> {
109        let start = self.version_byte_range().end;
110        start..start + Offset32::RAW_BYTE_LEN
111    }
112
113    pub fn advance_width_mapping_offset_byte_range(&self) -> Range<usize> {
114        let start = self.item_variation_store_offset_byte_range().end;
115        start..start + Offset32::RAW_BYTE_LEN
116    }
117
118    pub fn lsb_mapping_offset_byte_range(&self) -> Range<usize> {
119        let start = self.advance_width_mapping_offset_byte_range().end;
120        start..start + Offset32::RAW_BYTE_LEN
121    }
122
123    pub fn rsb_mapping_offset_byte_range(&self) -> Range<usize> {
124        let start = self.lsb_mapping_offset_byte_range().end;
125        start..start + Offset32::RAW_BYTE_LEN
126    }
127}
128
129#[cfg(feature = "experimental_traverse")]
130impl<'a> SomeTable<'a> for Hvar<'a> {
131    fn type_name(&self) -> &str {
132        "Hvar"
133    }
134    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
135        match idx {
136            0usize => Some(Field::new("version", self.version())),
137            1usize => Some(Field::new(
138                "item_variation_store_offset",
139                FieldType::offset(
140                    self.item_variation_store_offset(),
141                    self.item_variation_store(),
142                ),
143            )),
144            2usize => Some(Field::new(
145                "advance_width_mapping_offset",
146                FieldType::offset(
147                    self.advance_width_mapping_offset(),
148                    self.advance_width_mapping(),
149                ),
150            )),
151            3usize => Some(Field::new(
152                "lsb_mapping_offset",
153                FieldType::offset(self.lsb_mapping_offset(), self.lsb_mapping()),
154            )),
155            4usize => Some(Field::new(
156                "rsb_mapping_offset",
157                FieldType::offset(self.rsb_mapping_offset(), self.rsb_mapping()),
158            )),
159            _ => None,
160        }
161    }
162}
163
164#[cfg(feature = "experimental_traverse")]
165#[allow(clippy::needless_lifetimes)]
166impl<'a> std::fmt::Debug for Hvar<'a> {
167    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
168        (self as &dyn SomeTable<'a>).fmt(f)
169    }
170}