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.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> FontReadWithArgs<'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        Ok(Self {
36            data,
37            number_of_h_metrics,
38        })
39    }
40}
41
42impl<'a> Hmtx<'a> {
43    /// A constructor that requires additional arguments.
44    ///
45    /// This type requires some external state in order to be
46    /// parsed.
47    pub fn read(data: FontData<'a>, number_of_h_metrics: u16) -> Result<Self, ReadError> {
48        let args = number_of_h_metrics;
49        Self::read_with_args(data, &args)
50    }
51}
52
53/// The [hmtx (Horizontal Metrics)](https://docs.microsoft.com/en-us/typography/opentype/spec/hmtx) table
54#[derive(Clone)]
55pub struct Hmtx<'a> {
56    data: FontData<'a>,
57    number_of_h_metrics: u16,
58}
59
60#[allow(clippy::needless_lifetimes)]
61impl<'a> Hmtx<'a> {
62    pub const MIN_SIZE: usize = 0;
63    basic_table_impls!(impl_the_methods);
64
65    /// Paired advance width/height and left/top side bearing values for each
66    /// glyph. Records are indexed by glyph ID.
67    pub fn h_metrics(&self) -> &'a [LongMetric] {
68        let range = self.h_metrics_byte_range();
69        self.data.read_array(range).ok().unwrap_or_default()
70    }
71
72    /// Leading (left/top) side bearings for glyph IDs greater than or equal to
73    /// numberOfLongMetrics.
74    pub fn left_side_bearings(&self) -> &'a [BigEndian<i16>] {
75        let range = self.left_side_bearings_byte_range();
76        self.data.read_array(range).ok().unwrap_or_default()
77    }
78
79    pub(crate) fn number_of_h_metrics(&self) -> u16 {
80        self.number_of_h_metrics
81    }
82
83    pub fn h_metrics_byte_range(&self) -> Range<usize> {
84        let number_of_h_metrics = self.number_of_h_metrics();
85        let start = 0;
86        start..start + (number_of_h_metrics as usize).saturating_mul(LongMetric::RAW_BYTE_LEN)
87    }
88
89    pub fn left_side_bearings_byte_range(&self) -> Range<usize> {
90        let start = self.h_metrics_byte_range().end;
91        start..start + self.data.len().saturating_sub(start) / i16::RAW_BYTE_LEN * i16::RAW_BYTE_LEN
92    }
93}
94
95#[allow(clippy::absurd_extreme_comparisons)]
96const _: () = assert!(FontData::default_data_long_enough(Hmtx::MIN_SIZE));
97
98impl Default for Hmtx<'_> {
99    fn default() -> Self {
100        Self {
101            data: FontData::default_table_data(),
102            number_of_h_metrics: Default::default(),
103        }
104    }
105}
106
107#[cfg(feature = "experimental_traverse")]
108impl<'a> SomeTable<'a> for Hmtx<'a> {
109    fn type_name(&self) -> &str {
110        "Hmtx"
111    }
112    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
113        match idx {
114            0usize => Some(Field::new(
115                "h_metrics",
116                traversal::FieldType::array_of_records(
117                    stringify!(LongMetric),
118                    self.h_metrics(),
119                    self.offset_data(),
120                ),
121            )),
122            1usize => Some(Field::new("left_side_bearings", self.left_side_bearings())),
123            _ => None,
124        }
125    }
126}
127
128#[cfg(feature = "experimental_traverse")]
129#[allow(clippy::needless_lifetimes)]
130impl<'a> std::fmt::Debug for Hmtx<'a> {
131    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
132        (self as &dyn SomeTable<'a>).fmt(f)
133    }
134}
135
136#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
137#[repr(C)]
138#[repr(packed)]
139pub struct LongMetric {
140    /// Advance width/height, in font design units.
141    pub advance: BigEndian<u16>,
142    /// Glyph leading (left/top) side bearing, in font design units.
143    pub side_bearing: BigEndian<i16>,
144}
145
146impl LongMetric {
147    /// Advance width/height, in font design units.
148    pub fn advance(&self) -> u16 {
149        self.advance.get()
150    }
151
152    /// Glyph leading (left/top) side bearing, in font design units.
153    pub fn side_bearing(&self) -> i16 {
154        self.side_bearing.get()
155    }
156}
157
158impl FixedSize for LongMetric {
159    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + i16::RAW_BYTE_LEN;
160}
161
162#[cfg(feature = "experimental_traverse")]
163impl<'a> SomeRecord<'a> for LongMetric {
164    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
165        RecordResolver {
166            name: "LongMetric",
167            get_field: Box::new(move |idx, _data| match idx {
168                0usize => Some(Field::new("advance", self.advance())),
169                1usize => Some(Field::new("side_bearing", self.side_bearing())),
170                _ => None,
171            }),
172            data,
173        }
174    }
175}