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