/rust/registry/src/index.crates.io-1949cf8c6b5b557f/read-fonts-0.47.0/generated/generated_hmtx.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 Hmtx<'a> { |
9 | 0 | fn min_byte_range(&self) -> Range<usize> { |
10 | 0 | 0..self.left_side_bearings_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 Hmtx<'_> { |
19 | | /// `hmtx` |
20 | | const TAG: Tag = Tag::new(b"hmtx"); |
21 | | } |
22 | | |
23 | | impl ReadArgs for Hmtx<'_> { |
24 | | type Args = u16; |
25 | | } |
26 | | |
27 | | impl<'a> FontRead<'a> for Hmtx<'a> { |
28 | 601 | fn read_with_args(data: FontData<'a>, args: u16) -> Result<Self, ReadError> { |
29 | 601 | let number_of_h_metrics = args; |
30 | | |
31 | | #[allow(clippy::absurd_extreme_comparisons)] |
32 | 601 | if data.len() < Self::MIN_SIZE { |
33 | 0 | return Err(ReadError::OutOfBounds); |
34 | 601 | } |
35 | 601 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
36 | 0 | return Err(ReadError::OutOfBounds); |
37 | | }; |
38 | 601 | Ok(Self { |
39 | 601 | data, |
40 | 601 | number_of_h_metrics, |
41 | 601 | }) |
42 | 601 | } |
43 | | } |
44 | | |
45 | | impl<'a> Hmtx<'a> { |
46 | | /// A constructor that requires additional arguments. |
47 | | /// |
48 | | /// This type requires some external state in order to be |
49 | | /// parsed. |
50 | 0 | pub fn read(data: FontData<'a>, number_of_h_metrics: u16) -> Result<Self, ReadError> { |
51 | 0 | let args = number_of_h_metrics; |
52 | 0 | Self::read_with_args(data, args) |
53 | 0 | } |
54 | | } |
55 | | |
56 | | /// The fields at the start of the table, which are read directly. |
57 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
58 | | #[repr(C, packed)] |
59 | | #[doc(hidden)] |
60 | | pub struct HmtxFixedHeader {} |
61 | | |
62 | | #[allow(clippy::absurd_extreme_comparisons)] |
63 | | const _: () = assert!(std::mem::size_of::<HmtxFixedHeader>() <= Hmtx::MIN_SIZE); |
64 | | |
65 | | /// The [hmtx (Horizontal Metrics)](https://docs.microsoft.com/en-us/typography/opentype/spec/hmtx) table |
66 | | #[derive(Clone)] |
67 | | pub struct Hmtx<'a> { |
68 | | data: TableDataRef<'a, HmtxFixedHeader>, |
69 | | number_of_h_metrics: u16, |
70 | | } |
71 | | |
72 | | #[allow(clippy::needless_lifetimes)] |
73 | | impl<'a> Hmtx<'a> { |
74 | | pub const MIN_SIZE: usize = 0; |
75 | | basic_table_impls!(impl_the_methods); |
76 | | |
77 | | /// Paired advance width/height and left/top side bearing values for each |
78 | | /// glyph. Records are indexed by glyph ID. |
79 | 601 | pub fn h_metrics(&self) -> &'a [LongMetric] { |
80 | 601 | let range = self.h_metrics_byte_range(); |
81 | 601 | self.offset_data() |
82 | 601 | .read_array(range) |
83 | 601 | .ok() |
84 | 601 | .unwrap_or_default() |
85 | 601 | } |
86 | | |
87 | | /// Leading (left/top) side bearings for glyph IDs greater than or equal to |
88 | | /// numberOfLongMetrics. |
89 | 601 | pub fn left_side_bearings(&self) -> &'a [BigEndian<i16>] { |
90 | 601 | let range = self.left_side_bearings_byte_range(); |
91 | 601 | self.offset_data() |
92 | 601 | .read_array(range) |
93 | 601 | .ok() |
94 | 601 | .unwrap_or_default() |
95 | 601 | } |
96 | | |
97 | 1.20k | pub(crate) fn number_of_h_metrics(&self) -> u16 { |
98 | 1.20k | self.number_of_h_metrics |
99 | 1.20k | } |
100 | | |
101 | 1.20k | pub fn h_metrics_byte_range(&self) -> Range<usize> { |
102 | 1.20k | let number_of_h_metrics = self.number_of_h_metrics(); |
103 | 1.20k | let start = 0usize; |
104 | 1.20k | let end = start.saturating_add( |
105 | 1.20k | (transforms::to_usize(number_of_h_metrics)).saturating_mul(LongMetric::RAW_BYTE_LEN), |
106 | | ); |
107 | 1.20k | start..end |
108 | 1.20k | } |
109 | | |
110 | 601 | pub fn left_side_bearings_byte_range(&self) -> Range<usize> { |
111 | 601 | let start = self.h_metrics_byte_range().end; |
112 | 601 | let end = start.saturating_add( |
113 | 601 | self.offset_data().len().saturating_sub(start) / i16::RAW_BYTE_LEN * i16::RAW_BYTE_LEN, |
114 | | ); |
115 | 601 | start..end |
116 | 601 | } |
117 | | } |
118 | | |
119 | | #[allow(clippy::absurd_extreme_comparisons)] |
120 | | const _: () = assert!(FontData::default_data_long_enough(Hmtx::MIN_SIZE)); |
121 | | |
122 | | impl Default for Hmtx<'_> { |
123 | 0 | fn default() -> Self { |
124 | | Self { |
125 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
126 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
127 | 0 | number_of_h_metrics: Default::default(), |
128 | | } |
129 | 0 | } |
130 | | } |
131 | | |
132 | | impl<'a> MinByteRange<'a> for HmtxExtended<'a> { |
133 | 0 | fn min_byte_range(&self) -> Range<usize> { |
134 | 0 | 0..self.left_side_bearings_byte_range().end |
135 | 0 | } |
136 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
137 | 0 | let range = self.min_byte_range(); |
138 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
139 | 0 | } |
140 | | } |
141 | | |
142 | | impl TopLevelTable for HmtxExtended<'_> { |
143 | | /// `HMTX` |
144 | | const TAG: Tag = Tag::new(b"HMTX"); |
145 | | } |
146 | | |
147 | | impl ReadArgs for HmtxExtended<'_> { |
148 | | type Args = u32; |
149 | | } |
150 | | |
151 | | impl<'a> FontRead<'a> for HmtxExtended<'a> { |
152 | 0 | fn read_with_args(data: FontData<'a>, args: u32) -> Result<Self, ReadError> { |
153 | 0 | let number_of_h_metrics = args; |
154 | | |
155 | | #[allow(clippy::absurd_extreme_comparisons)] |
156 | 0 | if data.len() < Self::MIN_SIZE { |
157 | 0 | return Err(ReadError::OutOfBounds); |
158 | 0 | } |
159 | 0 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
160 | 0 | return Err(ReadError::OutOfBounds); |
161 | | }; |
162 | 0 | Ok(Self { |
163 | 0 | data, |
164 | 0 | number_of_h_metrics, |
165 | 0 | }) |
166 | 0 | } |
167 | | } |
168 | | |
169 | | impl<'a> HmtxExtended<'a> { |
170 | | /// A constructor that requires additional arguments. |
171 | | /// |
172 | | /// This type requires some external state in order to be |
173 | | /// parsed. |
174 | 0 | pub fn read(data: FontData<'a>, number_of_h_metrics: u32) -> Result<Self, ReadError> { |
175 | 0 | let args = number_of_h_metrics; |
176 | 0 | Self::read_with_args(data, args) |
177 | 0 | } |
178 | | } |
179 | | |
180 | | /// The fields at the start of the table, which are read directly. |
181 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
182 | | #[repr(C, packed)] |
183 | | #[doc(hidden)] |
184 | | pub struct HmtxExtendedFixedHeader {} |
185 | | |
186 | | #[allow(clippy::absurd_extreme_comparisons)] |
187 | | const _: () = assert!(std::mem::size_of::<HmtxExtendedFixedHeader>() <= HmtxExtended::MIN_SIZE); |
188 | | |
189 | | /// HMTX horizontal metrics (ISO/IEC 14496-22:2026, 5.1.11). |
190 | | #[derive(Clone)] |
191 | | pub struct HmtxExtended<'a> { |
192 | | data: TableDataRef<'a, HmtxExtendedFixedHeader>, |
193 | | number_of_h_metrics: u32, |
194 | | } |
195 | | |
196 | | #[allow(clippy::needless_lifetimes)] |
197 | | impl<'a> HmtxExtended<'a> { |
198 | | pub const MIN_SIZE: usize = 0; |
199 | | basic_table_impls!(impl_the_methods); |
200 | | |
201 | 0 | pub fn h_metrics(&self) -> &'a [LongMetric] { |
202 | 0 | let range = self.h_metrics_byte_range(); |
203 | 0 | self.offset_data() |
204 | 0 | .read_array(range) |
205 | 0 | .ok() |
206 | 0 | .unwrap_or_default() |
207 | 0 | } |
208 | | |
209 | 0 | pub fn left_side_bearings(&self) -> &'a [BigEndian<i16>] { |
210 | 0 | let range = self.left_side_bearings_byte_range(); |
211 | 0 | self.offset_data() |
212 | 0 | .read_array(range) |
213 | 0 | .ok() |
214 | 0 | .unwrap_or_default() |
215 | 0 | } |
216 | | |
217 | 0 | pub(crate) fn number_of_h_metrics(&self) -> u32 { |
218 | 0 | self.number_of_h_metrics |
219 | 0 | } |
220 | | |
221 | 0 | pub fn h_metrics_byte_range(&self) -> Range<usize> { |
222 | 0 | let number_of_h_metrics = self.number_of_h_metrics(); |
223 | 0 | let start = 0usize; |
224 | 0 | let end = start.saturating_add( |
225 | 0 | (transforms::to_usize(number_of_h_metrics)).saturating_mul(LongMetric::RAW_BYTE_LEN), |
226 | | ); |
227 | 0 | start..end |
228 | 0 | } |
229 | | |
230 | 0 | pub fn left_side_bearings_byte_range(&self) -> Range<usize> { |
231 | 0 | let start = self.h_metrics_byte_range().end; |
232 | 0 | let end = start.saturating_add( |
233 | 0 | self.offset_data().len().saturating_sub(start) / i16::RAW_BYTE_LEN * i16::RAW_BYTE_LEN, |
234 | | ); |
235 | 0 | start..end |
236 | 0 | } |
237 | | } |
238 | | |
239 | | #[allow(clippy::absurd_extreme_comparisons)] |
240 | | const _: () = assert!(FontData::default_data_long_enough(HmtxExtended::MIN_SIZE)); |
241 | | |
242 | | impl Default for HmtxExtended<'_> { |
243 | 0 | fn default() -> Self { |
244 | | Self { |
245 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
246 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
247 | 0 | number_of_h_metrics: Default::default(), |
248 | | } |
249 | 0 | } |
250 | | } |
251 | | |
252 | | #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)] |
253 | | #[repr(C)] |
254 | | #[repr(packed)] |
255 | | pub struct LongMetric { |
256 | | /// Advance width/height, in font design units. |
257 | | pub advance: BigEndian<u16>, |
258 | | /// Glyph leading (left/top) side bearing, in font design units. |
259 | | pub side_bearing: BigEndian<i16>, |
260 | | } |
261 | | |
262 | | impl LongMetric { |
263 | | /// Advance width/height, in font design units. |
264 | 3.90M | pub fn advance(&self) -> u16 { |
265 | 3.90M | self.advance.get() |
266 | 3.90M | } |
267 | | |
268 | | /// Glyph leading (left/top) side bearing, in font design units. |
269 | 13.0k | pub fn side_bearing(&self) -> i16 { |
270 | 13.0k | self.side_bearing.get() |
271 | 13.0k | } |
272 | | } |
273 | | |
274 | | impl FixedSize for LongMetric { |
275 | | const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + i16::RAW_BYTE_LEN; |
276 | | } |