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