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> FontRead<'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        ComputedArray::new(
84            self.data,
85            range,
86            (self.num_glyphs(), self.size_device_record()),
87        )
88        .unwrap_or_default()
89    }
90
91    pub(crate) fn num_glyphs(&self) -> u16 {
92        self.num_glyphs
93    }
94
95    pub fn version_byte_range(&self) -> Range<usize> {
96        let start = 0;
97        let end = start + u16::RAW_BYTE_LEN;
98        start..end
99    }
100
101    pub fn num_records_byte_range(&self) -> Range<usize> {
102        let start = self.version_byte_range().end;
103        let end = start + u16::RAW_BYTE_LEN;
104        start..end
105    }
106
107    pub fn size_device_record_byte_range(&self) -> Range<usize> {
108        let start = self.num_records_byte_range().end;
109        let end = start + u32::RAW_BYTE_LEN;
110        start..end
111    }
112
113    pub fn records_byte_range(&self) -> Range<usize> {
114        let num_records = self.num_records();
115        let start = self.size_device_record_byte_range().end;
116        let end = start
117            + (transforms::to_usize(num_records)).saturating_mul(
118                <DeviceRecord as ComputeSize>::compute_size((
119                    self.num_glyphs(),
120                    self.size_device_record(),
121                ))
122                .unwrap_or(0),
123            );
124        start..end
125    }
126}
127
128const _: () = assert!(FontData::default_data_long_enough(Hdmx::MIN_SIZE));
129
130impl Default for Hdmx<'_> {
131    fn default() -> Self {
132        Self {
133            data: FontData::default_table_data(),
134            num_glyphs: Default::default(),
135        }
136    }
137}