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