/rust/registry/src/index.crates.io-1949cf8c6b5b557f/read-fonts-0.46.0/generated/generated_hdmx.rs
Line | Count | Source |
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)] |
6 | | use crate::codegen_prelude::*; |
7 | | |
8 | | impl<'a> MinByteRange<'a> for Hdmx<'a> { |
9 | 0 | fn min_byte_range(&self) -> Range<usize> { |
10 | 0 | 0..self.records_byte_range().end |
11 | 0 | } |
12 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
13 | 0 | let range = self.min_byte_range(); |
14 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
15 | 0 | } |
16 | | } |
17 | | |
18 | | impl TopLevelTable for Hdmx<'_> { |
19 | | /// `hdmx` |
20 | | const TAG: Tag = Tag::new(b"hdmx"); |
21 | | } |
22 | | |
23 | | impl ReadArgs for Hdmx<'_> { |
24 | | type Args = u32; |
25 | | } |
26 | | |
27 | | impl<'a> FontRead<'a> for Hdmx<'a> { |
28 | 0 | fn read_with_args(data: FontData<'a>, args: u32) -> Result<Self, ReadError> { |
29 | 0 | let num_glyphs = args; |
30 | | |
31 | | #[allow(clippy::absurd_extreme_comparisons)] |
32 | 0 | if data.len() < Self::MIN_SIZE { |
33 | 0 | return Err(ReadError::OutOfBounds); |
34 | 0 | } |
35 | 0 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
36 | 0 | return Err(ReadError::OutOfBounds); |
37 | | }; |
38 | 0 | Ok(Self { data, num_glyphs }) |
39 | 0 | } |
40 | | } |
41 | | |
42 | | impl<'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 | 0 | pub fn read(data: FontData<'a>, num_glyphs: u32) -> Result<Self, ReadError> { |
48 | 0 | let args = num_glyphs; |
49 | 0 | Self::read_with_args(data, args) |
50 | 0 | } |
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)] |
57 | | pub 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)] |
64 | | const _: () = 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)] |
68 | | pub struct Hdmx<'a> { |
69 | | data: TableDataRef<'a, HdmxFixedHeader>, |
70 | | num_glyphs: u32, |
71 | | } |
72 | | |
73 | | #[allow(clippy::needless_lifetimes)] |
74 | | impl<'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 | 0 | pub fn version(&self) -> u16 { |
80 | 0 | self.data.header().version.get() |
81 | 0 | } |
82 | | |
83 | | /// Number of device records. |
84 | 0 | pub fn num_records(&self) -> u16 { |
85 | 0 | self.data.header().num_records.get() |
86 | 0 | } |
87 | | |
88 | | /// Size of device record, 32-bit aligned. |
89 | 0 | pub fn size_device_record(&self) -> u32 { |
90 | 0 | self.data.header().size_device_record.get() |
91 | 0 | } |
92 | | |
93 | | /// Array of device records. |
94 | 0 | pub fn records(&self) -> ComputedArray<'a, DeviceRecord<'a>> { |
95 | 0 | let range = self.records_byte_range(); |
96 | 0 | ComputedArray::new( |
97 | 0 | self.offset_data(), |
98 | 0 | range, |
99 | 0 | (self.num_glyphs(), self.size_device_record()), |
100 | | ) |
101 | 0 | .unwrap_or_default() |
102 | 0 | } |
103 | | |
104 | 0 | pub(crate) fn num_glyphs(&self) -> u32 { |
105 | 0 | self.num_glyphs |
106 | 0 | } |
107 | | |
108 | 0 | pub fn version_byte_range(&self) -> Range<usize> { |
109 | 0 | let start = 0; |
110 | 0 | let end = start + u16::RAW_BYTE_LEN; |
111 | 0 | start..end |
112 | 0 | } |
113 | | |
114 | 0 | pub fn num_records_byte_range(&self) -> Range<usize> { |
115 | 0 | let start = self.version_byte_range().end; |
116 | 0 | let end = start + u16::RAW_BYTE_LEN; |
117 | 0 | start..end |
118 | 0 | } |
119 | | |
120 | 0 | pub fn size_device_record_byte_range(&self) -> Range<usize> { |
121 | 0 | let start = self.num_records_byte_range().end; |
122 | 0 | let end = start + u32::RAW_BYTE_LEN; |
123 | 0 | start..end |
124 | 0 | } |
125 | | |
126 | 0 | pub fn records_byte_range(&self) -> Range<usize> { |
127 | 0 | let num_records = self.num_records(); |
128 | 0 | let start = self.size_device_record_byte_range().end; |
129 | 0 | let end = start |
130 | 0 | + (transforms::to_usize(num_records)).saturating_mul( |
131 | 0 | <DeviceRecord as ComputeSize>::compute_size(( |
132 | 0 | self.num_glyphs(), |
133 | 0 | self.size_device_record(), |
134 | 0 | )) |
135 | 0 | .unwrap_or(0), |
136 | 0 | ); |
137 | 0 | start..end |
138 | 0 | } |
139 | | } |
140 | | |
141 | | const _: () = assert!(FontData::default_data_long_enough(Hdmx::MIN_SIZE)); |
142 | | |
143 | | impl Default for Hdmx<'_> { |
144 | 0 | fn default() -> Self { |
145 | | Self { |
146 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
147 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
148 | 0 | num_glyphs: Default::default(), |
149 | | } |
150 | 0 | } |
151 | | } |