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#[cfg(feature = "experimental_traverse")]
96impl<'a> SomeTable<'a> for Hmtx<'a> {
97    fn type_name(&self) -> &str {
98        "Hmtx"
99    }
100    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
101        match idx {
102            0usize => Some(Field::new(
103                "h_metrics",
104                traversal::FieldType::array_of_records(
105                    stringify!(LongMetric),
106                    self.h_metrics(),
107                    self.offset_data(),
108                ),
109            )),
110            1usize => Some(Field::new("left_side_bearings", self.left_side_bearings())),
111            _ => None,
112        }
113    }
114}
115
116#[cfg(feature = "experimental_traverse")]
117#[allow(clippy::needless_lifetimes)]
118impl<'a> std::fmt::Debug for Hmtx<'a> {
119    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
120        (self as &dyn SomeTable<'a>).fmt(f)
121    }
122}
123
124#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
125#[repr(C)]
126#[repr(packed)]
127pub struct LongMetric {
128    /// Advance width/height, in font design units.
129    pub advance: BigEndian<u16>,
130    /// Glyph leading (left/top) side bearing, in font design units.
131    pub side_bearing: BigEndian<i16>,
132}
133
134impl LongMetric {
135    /// Advance width/height, in font design units.
136    pub fn advance(&self) -> u16 {
137        self.advance.get()
138    }
139
140    /// Glyph leading (left/top) side bearing, in font design units.
141    pub fn side_bearing(&self) -> i16 {
142        self.side_bearing.get()
143    }
144}
145
146impl FixedSize for LongMetric {
147    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + i16::RAW_BYTE_LEN;
148}
149
150#[cfg(feature = "experimental_traverse")]
151impl<'a> SomeRecord<'a> for LongMetric {
152    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
153        RecordResolver {
154            name: "LongMetric",
155            get_field: Box::new(move |idx, _data| match idx {
156                0usize => Some(Field::new("advance", self.advance())),
157                1usize => Some(Field::new("side_bearing", self.side_bearing())),
158                _ => None,
159            }),
160            data,
161        }
162    }
163}