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.offset_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 = u32;
25}
26
27impl<'a> FontRead<'a> for Hdmx<'a> {
28    fn read_with_args(data: FontData<'a>, args: u32) -> 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        let Some(data) = TableDataRef::new(data.as_bytes()) else {
36            return Err(ReadError::OutOfBounds);
37        };
38        Ok(Self { data, num_glyphs })
39    }
40}
41
42impl<'a> Hdmx<'a> {
43    /// A constructor that requires additional arguments.
44    ///
45    /// This type requires some external state in order to be
46    /// parsed.
47    pub fn read(data: FontData<'a>, num_glyphs: u32) -> Result<Self, ReadError> {
48        let args = num_glyphs;
49        Self::read_with_args(data, args)
50    }
51}
52
53/// The fields at the start of the table, which are read directly.
54#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
55#[repr(C, packed)]
56#[doc(hidden)]
57pub struct HdmxFixedHeader {
58    pub version: BigEndian<u16>,
59    pub num_records: BigEndian<u16>,
60    pub size_device_record: BigEndian<u32>,
61}
62
63#[allow(clippy::absurd_extreme_comparisons)]
64const _: () = assert!(std::mem::size_of::<HdmxFixedHeader>() <= Hdmx::MIN_SIZE);
65
66/// The [Horizontal Device Metrics](https://learn.microsoft.com/en-us/typography/opentype/spec/hdmx) table.
67#[derive(Clone)]
68pub struct Hdmx<'a> {
69    data: TableDataRef<'a, HdmxFixedHeader>,
70    num_glyphs: u32,
71}
72
73#[allow(clippy::needless_lifetimes)]
74impl<'a> Hdmx<'a> {
75    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
76    basic_table_impls!(impl_the_methods);
77
78    /// Table version number (set to 0).
79    pub fn version(&self) -> u16 {
80        self.data.header().version.get()
81    }
82
83    /// Number of device records.
84    pub fn num_records(&self) -> u16 {
85        self.data.header().num_records.get()
86    }
87
88    /// Size of device record, 32-bit aligned.
89    pub fn size_device_record(&self) -> u32 {
90        self.data.header().size_device_record.get()
91    }
92
93    /// Array of device records.
94    pub fn records(&self) -> ComputedArray<'a, DeviceRecord<'a>> {
95        let range = self.records_byte_range();
96        ComputedArray::new(
97            self.offset_data(),
98            range,
99            (self.num_glyphs(), self.size_device_record()),
100        )
101        .unwrap_or_default()
102    }
103
104    pub(crate) fn num_glyphs(&self) -> u32 {
105        self.num_glyphs
106    }
107
108    pub fn version_byte_range(&self) -> Range<usize> {
109        let start = 0;
110        let end = start + u16::RAW_BYTE_LEN;
111        start..end
112    }
113
114    pub fn num_records_byte_range(&self) -> Range<usize> {
115        let start = self.version_byte_range().end;
116        let end = start + u16::RAW_BYTE_LEN;
117        start..end
118    }
119
120    pub fn size_device_record_byte_range(&self) -> Range<usize> {
121        let start = self.num_records_byte_range().end;
122        let end = start + u32::RAW_BYTE_LEN;
123        start..end
124    }
125
126    pub fn records_byte_range(&self) -> Range<usize> {
127        let num_records = self.num_records();
128        let start = self.size_device_record_byte_range().end;
129        let end = start
130            + (transforms::to_usize(num_records)).saturating_mul(
131                <DeviceRecord as ComputeSize>::compute_size((
132                    self.num_glyphs(),
133                    self.size_device_record(),
134                ))
135                .unwrap_or(0),
136            );
137        start..end
138    }
139}
140
141const _: () = assert!(FontData::default_data_long_enough(Hdmx::MIN_SIZE));
142
143impl Default for Hdmx<'_> {
144    fn default() -> Self {
145        Self {
146            data: TableDataRef::new(FontData::default_table_data().as_bytes())
147                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
148            num_glyphs: Default::default(),
149        }
150    }
151}