/src/fontations/read-fonts/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 | | /// The [Horizontal Device Metrics](https://learn.microsoft.com/en-us/typography/opentype/spec/hdmx) table. |
9 | | #[derive(Debug, Clone, Copy)] |
10 | | #[doc(hidden)] |
11 | | pub struct HdmxMarker { |
12 | | num_glyphs: u16, |
13 | | records_byte_len: usize, |
14 | | } |
15 | | |
16 | | impl HdmxMarker { |
17 | 11.7k | pub fn version_byte_range(&self) -> Range<usize> { |
18 | 11.7k | let start = 0; |
19 | 11.7k | start..start + u16::RAW_BYTE_LEN |
20 | 11.7k | } |
21 | | |
22 | 11.7k | pub fn num_records_byte_range(&self) -> Range<usize> { |
23 | 11.7k | let start = self.version_byte_range().end; |
24 | 11.7k | start..start + u16::RAW_BYTE_LEN |
25 | 11.7k | } |
26 | | |
27 | 11.7k | pub fn size_device_record_byte_range(&self) -> Range<usize> { |
28 | 11.7k | let start = self.num_records_byte_range().end; |
29 | 11.7k | start..start + u32::RAW_BYTE_LEN |
30 | 11.7k | } |
31 | | |
32 | 5.85k | pub fn records_byte_range(&self) -> Range<usize> { |
33 | 5.85k | let start = self.size_device_record_byte_range().end; |
34 | 5.85k | start..start + self.records_byte_len |
35 | 5.85k | } |
36 | | } |
37 | | |
38 | | impl MinByteRange for HdmxMarker { |
39 | 0 | fn min_byte_range(&self) -> Range<usize> { |
40 | 0 | 0..self.records_byte_range().end |
41 | 0 | } |
42 | | } |
43 | | |
44 | | impl TopLevelTable for Hdmx<'_> { |
45 | | /// `hdmx` |
46 | | const TAG: Tag = Tag::new(b"hdmx"); |
47 | | } |
48 | | |
49 | | impl ReadArgs for Hdmx<'_> { |
50 | | type Args = u16; |
51 | | } |
52 | | |
53 | | impl<'a> FontReadWithArgs<'a> for Hdmx<'a> { |
54 | 23.7k | fn read_with_args(data: FontData<'a>, args: &u16) -> Result<Self, ReadError> { |
55 | 23.7k | let num_glyphs = *args; |
56 | 23.7k | let mut cursor = data.cursor(); |
57 | 23.7k | cursor.advance::<u16>(); |
58 | 23.7k | let num_records: u16 = cursor.read()?; |
59 | 23.6k | let size_device_record: u32 = cursor.read()?; |
60 | 23.5k | let records_byte_len = (num_records as usize) |
61 | 23.5k | .checked_mul(<DeviceRecord as ComputeSize>::compute_size(&( |
62 | 23.5k | num_glyphs, |
63 | 23.5k | size_device_record, |
64 | 23.5k | ))?) |
65 | 23.5k | .ok_or(ReadError::OutOfBounds)?; |
66 | 23.5k | cursor.advance_by(records_byte_len); |
67 | 23.5k | cursor.finish(HdmxMarker { |
68 | 23.5k | num_glyphs, |
69 | 23.5k | records_byte_len, |
70 | 23.5k | }) |
71 | 23.7k | } |
72 | | } |
73 | | |
74 | | impl<'a> Hdmx<'a> { |
75 | | /// A constructor that requires additional arguments. |
76 | | /// |
77 | | /// This type requires some external state in order to be |
78 | | /// parsed. |
79 | 23.7k | pub fn read(data: FontData<'a>, num_glyphs: u16) -> Result<Self, ReadError> { |
80 | 23.7k | let args = num_glyphs; |
81 | 23.7k | Self::read_with_args(data, &args) |
82 | 23.7k | } |
83 | | } |
84 | | |
85 | | /// The [Horizontal Device Metrics](https://learn.microsoft.com/en-us/typography/opentype/spec/hdmx) table. |
86 | | pub type Hdmx<'a> = TableRef<'a, HdmxMarker>; |
87 | | |
88 | | #[allow(clippy::needless_lifetimes)] |
89 | | impl<'a> Hdmx<'a> { |
90 | | /// Table version number (set to 0). |
91 | 0 | pub fn version(&self) -> u16 { |
92 | 0 | let range = self.shape.version_byte_range(); |
93 | 0 | self.data.read_at(range.start).unwrap() |
94 | 0 | } |
95 | | |
96 | | /// Number of device records. |
97 | 0 | pub fn num_records(&self) -> u16 { |
98 | 0 | let range = self.shape.num_records_byte_range(); |
99 | 0 | self.data.read_at(range.start).unwrap() |
100 | 0 | } |
101 | | |
102 | | /// Size of device record, 32-bit aligned. |
103 | 5.85k | pub fn size_device_record(&self) -> u32 { |
104 | 5.85k | let range = self.shape.size_device_record_byte_range(); |
105 | 5.85k | self.data.read_at(range.start).unwrap() |
106 | 5.85k | } |
107 | | |
108 | | /// Array of device records. |
109 | 5.85k | pub fn records(&self) -> ComputedArray<'a, DeviceRecord<'a>> { |
110 | 5.85k | let range = self.shape.records_byte_range(); |
111 | 5.85k | self.data |
112 | 5.85k | .read_with_args(range, &(self.num_glyphs(), self.size_device_record())) |
113 | 5.85k | .unwrap() |
114 | 5.85k | } |
115 | | |
116 | 5.85k | pub(crate) fn num_glyphs(&self) -> u16 { |
117 | 5.85k | self.shape.num_glyphs |
118 | 5.85k | } |
119 | | } |
120 | | |
121 | | #[cfg(feature = "experimental_traverse")] |
122 | | impl<'a> SomeTable<'a> for Hdmx<'a> { |
123 | | fn type_name(&self) -> &str { |
124 | | "Hdmx" |
125 | | } |
126 | | fn get_field(&self, idx: usize) -> Option<Field<'a>> { |
127 | | match idx { |
128 | | 0usize => Some(Field::new("version", self.version())), |
129 | | 1usize => Some(Field::new("num_records", self.num_records())), |
130 | | 2usize => Some(Field::new("size_device_record", self.size_device_record())), |
131 | | 3usize => Some(Field::new( |
132 | | "records", |
133 | | traversal::FieldType::computed_array( |
134 | | "DeviceRecord", |
135 | | self.records(), |
136 | | self.offset_data(), |
137 | | ), |
138 | | )), |
139 | | _ => None, |
140 | | } |
141 | | } |
142 | | } |
143 | | |
144 | | #[cfg(feature = "experimental_traverse")] |
145 | | #[allow(clippy::needless_lifetimes)] |
146 | | impl<'a> std::fmt::Debug for Hdmx<'a> { |
147 | | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
148 | | (self as &dyn SomeTable<'a>).fmt(f) |
149 | | } |
150 | | } |