Skip to main content

read_fonts/generated/
generated_hdmx.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 Hdmx<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.records_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 Hdmx<'_> {
19    /// `hdmx`
20    const TAG: Tag = Tag::new(b"hdmx");
21}
22
23impl ReadArgs for Hdmx<'_> {
24    type Args = u16;
25}
26
27impl<'a> FontReadWithArgs<'a> for Hdmx<'a> {
28    fn read_with_args(data: FontData<'a>, args: &u16) -> Result<Self, ReadError> {
29        let num_glyphs = *args;
30
31        #[allow(clippy::absurd_extreme_comparisons)]
32        if data.len() < Self::MIN_SIZE {
33            return Err(ReadError::OutOfBounds);
34        }
35        Ok(Self { data, num_glyphs })
36    }
37}
38
39impl<'a> Hdmx<'a> {
40    /// A constructor that requires additional arguments.
41    ///
42    /// This type requires some external state in order to be
43    /// parsed.
44    pub fn read(data: FontData<'a>, num_glyphs: u16) -> Result<Self, ReadError> {
45        let args = num_glyphs;
46        Self::read_with_args(data, &args)
47    }
48}
49
50/// The [Horizontal Device Metrics](https://learn.microsoft.com/en-us/typography/opentype/spec/hdmx) table.
51#[derive(Clone)]
52pub struct Hdmx<'a> {
53    data: FontData<'a>,
54    num_glyphs: u16,
55}
56
57#[allow(clippy::needless_lifetimes)]
58impl<'a> Hdmx<'a> {
59    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
60    basic_table_impls!(impl_the_methods);
61
62    /// Table version number (set to 0).
63    pub fn version(&self) -> u16 {
64        let range = self.version_byte_range();
65        self.data.read_at(range.start).ok().unwrap()
66    }
67
68    /// Number of device records.
69    pub fn num_records(&self) -> u16 {
70        let range = self.num_records_byte_range();
71        self.data.read_at(range.start).ok().unwrap()
72    }
73
74    /// Size of device record, 32-bit aligned.
75    pub fn size_device_record(&self) -> u32 {
76        let range = self.size_device_record_byte_range();
77        self.data.read_at(range.start).ok().unwrap()
78    }
79
80    /// Array of device records.
81    pub fn records(&self) -> ComputedArray<'a, DeviceRecord<'a>> {
82        let range = self.records_byte_range();
83        self.data
84            .read_with_args(range, &(self.num_glyphs(), self.size_device_record()))
85            .unwrap_or_default()
86    }
87
88    pub(crate) fn num_glyphs(&self) -> u16 {
89        self.num_glyphs
90    }
91
92    pub fn version_byte_range(&self) -> Range<usize> {
93        let start = 0;
94        let end = start + u16::RAW_BYTE_LEN;
95        start..end
96    }
97
98    pub fn num_records_byte_range(&self) -> Range<usize> {
99        let start = self.version_byte_range().end;
100        let end = start + u16::RAW_BYTE_LEN;
101        start..end
102    }
103
104    pub fn size_device_record_byte_range(&self) -> Range<usize> {
105        let start = self.num_records_byte_range().end;
106        let end = start + u32::RAW_BYTE_LEN;
107        start..end
108    }
109
110    pub fn records_byte_range(&self) -> Range<usize> {
111        let num_records = self.num_records();
112        let start = self.size_device_record_byte_range().end;
113        let end = start
114            + (transforms::to_usize(num_records)).saturating_mul(
115                <DeviceRecord as ComputeSize>::compute_size(&(
116                    self.num_glyphs(),
117                    self.size_device_record(),
118                ))
119                .unwrap_or(0),
120            );
121        start..end
122    }
123}
124
125const _: () = assert!(FontData::default_data_long_enough(Hdmx::MIN_SIZE));
126
127impl Default for Hdmx<'_> {
128    fn default() -> Self {
129        Self {
130            data: FontData::default_table_data(),
131            num_glyphs: Default::default(),
132        }
133    }
134}
135
136#[cfg(feature = "experimental_traverse")]
137impl<'a> SomeTable<'a> for Hdmx<'a> {
138    fn type_name(&self) -> &str {
139        "Hdmx"
140    }
141    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
142        match idx {
143            0usize => Some(Field::new("version", self.version())),
144            1usize => Some(Field::new("num_records", self.num_records())),
145            2usize => Some(Field::new("size_device_record", self.size_device_record())),
146            3usize => Some(Field::new(
147                "records",
148                traversal::FieldType::computed_array(
149                    "DeviceRecord",
150                    self.records(),
151                    self.offset_data(),
152                ),
153            )),
154            _ => None,
155        }
156    }
157}
158
159#[cfg(feature = "experimental_traverse")]
160#[allow(clippy::needless_lifetimes)]
161impl<'a> std::fmt::Debug for Hdmx<'a> {
162    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
163        (self as &dyn SomeTable<'a>).fmt(f)
164    }
165}