Skip to main content

read_fonts/generated/
generated_hmtx.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 Hmtx<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.left_side_bearings_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 Hmtx<'_> {
19    /// `hmtx`
20    const TAG: Tag = Tag::new(b"hmtx");
21}
22
23impl ReadArgs for Hmtx<'_> {
24    type Args = u16;
25}
26
27impl<'a> FontRead<'a> for Hmtx<'a> {
28    fn read_with_args(data: FontData<'a>, args: u16) -> Result<Self, ReadError> {
29        let number_of_h_metrics = 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 {
39            data,
40            number_of_h_metrics,
41        })
42    }
43}
44
45impl<'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    pub fn read(data: FontData<'a>, number_of_h_metrics: u16) -> Result<Self, ReadError> {
51        let args = number_of_h_metrics;
52        Self::read_with_args(data, args)
53    }
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)]
60pub struct HmtxFixedHeader {}
61
62#[allow(clippy::absurd_extreme_comparisons)]
63const _: () = 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)]
67pub struct Hmtx<'a> {
68    data: TableDataRef<'a, HmtxFixedHeader>,
69    number_of_h_metrics: u16,
70}
71
72#[allow(clippy::needless_lifetimes)]
73impl<'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    pub fn h_metrics(&self) -> &'a [LongMetric] {
80        let range = self.h_metrics_byte_range();
81        self.offset_data()
82            .read_array(range)
83            .ok()
84            .unwrap_or_default()
85    }
86
87    /// Leading (left/top) side bearings for glyph IDs greater than or equal to
88    /// numberOfLongMetrics.
89    pub fn left_side_bearings(&self) -> &'a [BigEndian<i16>] {
90        let range = self.left_side_bearings_byte_range();
91        self.offset_data()
92            .read_array(range)
93            .ok()
94            .unwrap_or_default()
95    }
96
97    pub(crate) fn number_of_h_metrics(&self) -> u16 {
98        self.number_of_h_metrics
99    }
100
101    pub fn h_metrics_byte_range(&self) -> Range<usize> {
102        let number_of_h_metrics = self.number_of_h_metrics();
103        let start = 0;
104        let end = start
105            + (transforms::to_usize(number_of_h_metrics)).saturating_mul(LongMetric::RAW_BYTE_LEN);
106        start..end
107    }
108
109    pub fn left_side_bearings_byte_range(&self) -> Range<usize> {
110        let start = self.h_metrics_byte_range().end;
111        let end = start
112            + self.offset_data().len().saturating_sub(start) / i16::RAW_BYTE_LEN
113                * i16::RAW_BYTE_LEN;
114        start..end
115    }
116}
117
118#[allow(clippy::absurd_extreme_comparisons)]
119const _: () = assert!(FontData::default_data_long_enough(Hmtx::MIN_SIZE));
120
121impl Default for Hmtx<'_> {
122    fn default() -> Self {
123        Self {
124            data: TableDataRef::new(FontData::default_table_data().as_bytes())
125                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
126            number_of_h_metrics: Default::default(),
127        }
128    }
129}
130
131impl<'a> MinByteRange<'a> for HmtxExtended<'a> {
132    fn min_byte_range(&self) -> Range<usize> {
133        0..self.left_side_bearings_byte_range().end
134    }
135    fn min_table_bytes(&self) -> &'a [u8] {
136        let range = self.min_byte_range();
137        self.offset_data().as_bytes().get(range).unwrap_or_default()
138    }
139}
140
141impl TopLevelTable for HmtxExtended<'_> {
142    /// `HMTX`
143    const TAG: Tag = Tag::new(b"HMTX");
144}
145
146impl ReadArgs for HmtxExtended<'_> {
147    type Args = u32;
148}
149
150impl<'a> FontRead<'a> for HmtxExtended<'a> {
151    fn read_with_args(data: FontData<'a>, args: u32) -> Result<Self, ReadError> {
152        let number_of_h_metrics = args;
153
154        #[allow(clippy::absurd_extreme_comparisons)]
155        if data.len() < Self::MIN_SIZE {
156            return Err(ReadError::OutOfBounds);
157        }
158        let Some(data) = TableDataRef::new(data.as_bytes()) else {
159            return Err(ReadError::OutOfBounds);
160        };
161        Ok(Self {
162            data,
163            number_of_h_metrics,
164        })
165    }
166}
167
168impl<'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    pub fn read(data: FontData<'a>, number_of_h_metrics: u32) -> Result<Self, ReadError> {
174        let args = number_of_h_metrics;
175        Self::read_with_args(data, args)
176    }
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)]
183pub struct HmtxExtendedFixedHeader {}
184
185#[allow(clippy::absurd_extreme_comparisons)]
186const _: () = 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)]
190pub struct HmtxExtended<'a> {
191    data: TableDataRef<'a, HmtxExtendedFixedHeader>,
192    number_of_h_metrics: u32,
193}
194
195#[allow(clippy::needless_lifetimes)]
196impl<'a> HmtxExtended<'a> {
197    pub const MIN_SIZE: usize = 0;
198    basic_table_impls!(impl_the_methods);
199
200    pub fn h_metrics(&self) -> &'a [LongMetric] {
201        let range = self.h_metrics_byte_range();
202        self.offset_data()
203            .read_array(range)
204            .ok()
205            .unwrap_or_default()
206    }
207
208    pub fn left_side_bearings(&self) -> &'a [BigEndian<i16>] {
209        let range = self.left_side_bearings_byte_range();
210        self.offset_data()
211            .read_array(range)
212            .ok()
213            .unwrap_or_default()
214    }
215
216    pub(crate) fn number_of_h_metrics(&self) -> u32 {
217        self.number_of_h_metrics
218    }
219
220    pub fn h_metrics_byte_range(&self) -> Range<usize> {
221        let number_of_h_metrics = self.number_of_h_metrics();
222        let start = 0;
223        let end = start
224            + (transforms::to_usize(number_of_h_metrics)).saturating_mul(LongMetric::RAW_BYTE_LEN);
225        start..end
226    }
227
228    pub fn left_side_bearings_byte_range(&self) -> Range<usize> {
229        let start = self.h_metrics_byte_range().end;
230        let end = start
231            + self.offset_data().len().saturating_sub(start) / i16::RAW_BYTE_LEN
232                * i16::RAW_BYTE_LEN;
233        start..end
234    }
235}
236
237#[allow(clippy::absurd_extreme_comparisons)]
238const _: () = assert!(FontData::default_data_long_enough(HmtxExtended::MIN_SIZE));
239
240impl Default for HmtxExtended<'_> {
241    fn default() -> Self {
242        Self {
243            data: TableDataRef::new(FontData::default_table_data().as_bytes())
244                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
245            number_of_h_metrics: Default::default(),
246        }
247    }
248}
249
250#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
251#[repr(C)]
252#[repr(packed)]
253pub 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
260impl LongMetric {
261    /// Advance width/height, in font design units.
262    pub fn advance(&self) -> u16 {
263        self.advance.get()
264    }
265
266    /// Glyph leading (left/top) side bearing, in font design units.
267    pub fn side_bearing(&self) -> i16 {
268        self.side_bearing.get()
269    }
270}
271
272impl FixedSize for LongMetric {
273    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + i16::RAW_BYTE_LEN;
274}