Skip to main content

read_fonts/generated/
generated_ankr.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 Ankr<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.glyph_data_table_offset_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 Ankr<'_> {
19    /// `ankr`
20    const TAG: Tag = Tag::new(b"ankr");
21}
22
23impl<'a> FontRead<'a> for Ankr<'a> {
24    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
25        #[allow(clippy::absurd_extreme_comparisons)]
26        if data.len() < Self::MIN_SIZE {
27            return Err(ReadError::OutOfBounds);
28        }
29        Ok(Self { data })
30    }
31}
32
33/// The [anchor point](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6ankr.html) table.
34#[derive(Clone)]
35pub struct Ankr<'a> {
36    data: FontData<'a>,
37}
38
39#[allow(clippy::needless_lifetimes)]
40impl<'a> Ankr<'a> {
41    pub const MIN_SIZE: usize =
42        (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + Offset32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
43    basic_table_impls!(impl_the_methods);
44
45    /// Version number (set to zero).
46    pub fn version(&self) -> u16 {
47        let range = self.version_byte_range();
48        self.data.read_at(range.start).ok().unwrap()
49    }
50
51    /// Flags (currently unused; set to zero).
52    pub fn flags(&self) -> u16 {
53        let range = self.flags_byte_range();
54        self.data.read_at(range.start).ok().unwrap()
55    }
56
57    /// Offset to the table's lookup table; currently this is always `0x0000000C`.
58    ///
59    /// Lookup values are two byte offsets into the glyph data table.
60    pub fn lookup_table_offset(&self) -> Offset32 {
61        let range = self.lookup_table_offset_byte_range();
62        self.data.read_at(range.start).ok().unwrap()
63    }
64
65    /// Attempt to resolve [`lookup_table_offset`][Self::lookup_table_offset].
66    pub fn lookup_table(&self) -> Result<LookupU16<'a>, ReadError> {
67        let data = self.data;
68        self.lookup_table_offset().resolve(data)
69    }
70
71    /// Offset to the glyph data table.
72    pub fn glyph_data_table_offset(&self) -> u32 {
73        let range = self.glyph_data_table_offset_byte_range();
74        self.data.read_at(range.start).ok().unwrap()
75    }
76
77    pub fn version_byte_range(&self) -> Range<usize> {
78        let start = 0;
79        start..start + u16::RAW_BYTE_LEN
80    }
81
82    pub fn flags_byte_range(&self) -> Range<usize> {
83        let start = self.version_byte_range().end;
84        start..start + u16::RAW_BYTE_LEN
85    }
86
87    pub fn lookup_table_offset_byte_range(&self) -> Range<usize> {
88        let start = self.flags_byte_range().end;
89        start..start + Offset32::RAW_BYTE_LEN
90    }
91
92    pub fn glyph_data_table_offset_byte_range(&self) -> Range<usize> {
93        let start = self.lookup_table_offset_byte_range().end;
94        start..start + u32::RAW_BYTE_LEN
95    }
96}
97
98#[cfg(feature = "experimental_traverse")]
99impl<'a> SomeTable<'a> for Ankr<'a> {
100    fn type_name(&self) -> &str {
101        "Ankr"
102    }
103    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
104        match idx {
105            0usize => Some(Field::new("version", self.version())),
106            1usize => Some(Field::new("flags", self.flags())),
107            2usize => Some(Field::new(
108                "lookup_table_offset",
109                FieldType::offset(self.lookup_table_offset(), self.lookup_table()),
110            )),
111            3usize => Some(Field::new(
112                "glyph_data_table_offset",
113                self.glyph_data_table_offset(),
114            )),
115            _ => None,
116        }
117    }
118}
119
120#[cfg(feature = "experimental_traverse")]
121#[allow(clippy::needless_lifetimes)]
122impl<'a> std::fmt::Debug for Ankr<'a> {
123    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
124        (self as &dyn SomeTable<'a>).fmt(f)
125    }
126}
127
128impl<'a> MinByteRange<'a> for GlyphDataEntry<'a> {
129    fn min_byte_range(&self) -> Range<usize> {
130        0..self.anchor_points_byte_range().end
131    }
132    fn min_table_bytes(&self) -> &'a [u8] {
133        let range = self.min_byte_range();
134        self.data.as_bytes().get(range).unwrap_or_default()
135    }
136}
137
138impl<'a> FontRead<'a> for GlyphDataEntry<'a> {
139    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
140        #[allow(clippy::absurd_extreme_comparisons)]
141        if data.len() < Self::MIN_SIZE {
142            return Err(ReadError::OutOfBounds);
143        }
144        Ok(Self { data })
145    }
146}
147
148#[derive(Clone)]
149pub struct GlyphDataEntry<'a> {
150    data: FontData<'a>,
151}
152
153#[allow(clippy::needless_lifetimes)]
154impl<'a> GlyphDataEntry<'a> {
155    pub const MIN_SIZE: usize = u32::RAW_BYTE_LEN;
156    basic_table_impls!(impl_the_methods);
157
158    /// Number of anchor points for this glyph.
159    pub fn num_points(&self) -> u32 {
160        let range = self.num_points_byte_range();
161        self.data.read_at(range.start).ok().unwrap()
162    }
163
164    /// Individual anchor points.
165    pub fn anchor_points(&self) -> &'a [AnchorPoint] {
166        let range = self.anchor_points_byte_range();
167        self.data.read_array(range).ok().unwrap_or_default()
168    }
169
170    pub fn num_points_byte_range(&self) -> Range<usize> {
171        let start = 0;
172        start..start + u32::RAW_BYTE_LEN
173    }
174
175    pub fn anchor_points_byte_range(&self) -> Range<usize> {
176        let num_points = self.num_points();
177        let start = self.num_points_byte_range().end;
178        start..start + (num_points as usize).saturating_mul(AnchorPoint::RAW_BYTE_LEN)
179    }
180}
181
182#[cfg(feature = "experimental_traverse")]
183impl<'a> SomeTable<'a> for GlyphDataEntry<'a> {
184    fn type_name(&self) -> &str {
185        "GlyphDataEntry"
186    }
187    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
188        match idx {
189            0usize => Some(Field::new("num_points", self.num_points())),
190            1usize => Some(Field::new(
191                "anchor_points",
192                traversal::FieldType::array_of_records(
193                    stringify!(AnchorPoint),
194                    self.anchor_points(),
195                    self.offset_data(),
196                ),
197            )),
198            _ => None,
199        }
200    }
201}
202
203#[cfg(feature = "experimental_traverse")]
204#[allow(clippy::needless_lifetimes)]
205impl<'a> std::fmt::Debug for GlyphDataEntry<'a> {
206    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
207        (self as &dyn SomeTable<'a>).fmt(f)
208    }
209}
210
211/// Individual anchor point.
212#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
213#[repr(C)]
214#[repr(packed)]
215pub struct AnchorPoint {
216    pub x: BigEndian<i16>,
217    pub y: BigEndian<i16>,
218}
219
220impl AnchorPoint {
221    pub fn x(&self) -> i16 {
222        self.x.get()
223    }
224
225    pub fn y(&self) -> i16 {
226        self.y.get()
227    }
228}
229
230impl FixedSize for AnchorPoint {
231    const RAW_BYTE_LEN: usize = i16::RAW_BYTE_LEN + i16::RAW_BYTE_LEN;
232}
233
234#[cfg(feature = "experimental_traverse")]
235impl<'a> SomeRecord<'a> for AnchorPoint {
236    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
237        RecordResolver {
238            name: "AnchorPoint",
239            get_field: Box::new(move |idx, _data| match idx {
240                0usize => Some(Field::new("x", self.x())),
241                1usize => Some(Field::new("y", self.y())),
242                _ => None,
243            }),
244            data,
245        }
246    }
247}