Skip to main content

read_fonts/generated/
generated_trak.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 Trak<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.reserved_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 Trak<'_> {
19    /// `trak`
20    const TAG: Tag = Tag::new(b"trak");
21}
22
23impl ReadArgs for Trak<'_> {
24    type Args = ();
25}
26
27impl<'a> FontRead<'a> for Trak<'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 TrakFixedHeader {
45    pub version: BigEndian<MajorMinor>,
46    pub format: BigEndian<u16>,
47    pub horiz_offset: BigEndian<Nullable<Offset16>>,
48    pub vert_offset: BigEndian<Nullable<Offset16>>,
49    pub reserved: BigEndian<u16>,
50}
51
52#[allow(clippy::absurd_extreme_comparisons)]
53const _: () = assert!(std::mem::size_of::<TrakFixedHeader>() <= Trak::MIN_SIZE);
54
55/// The [tracking (trak)](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6trak.html) table.
56#[derive(Clone)]
57pub struct Trak<'a> {
58    data: TableDataRef<'a, TrakFixedHeader>,
59}
60
61#[allow(clippy::needless_lifetimes)]
62impl<'a> Trak<'a> {
63    pub const MIN_SIZE: usize = (MajorMinor::RAW_BYTE_LEN
64        + u16::RAW_BYTE_LEN
65        + Offset16::RAW_BYTE_LEN
66        + Offset16::RAW_BYTE_LEN
67        + u16::RAW_BYTE_LEN);
68    basic_table_impls!(impl_the_methods);
69
70    /// Version number of the tracking table (0x00010000 for the current version).
71    pub fn version(&self) -> MajorMinor {
72        self.data.header().version.get()
73    }
74
75    /// Format of the tracking table (set to 0).
76    pub fn format(&self) -> u16 {
77        self.data.header().format.get()
78    }
79
80    /// Offset from start of tracking table to TrackData for horizontal text (or 0 if none).
81    pub fn horiz_offset(&self) -> Nullable<Offset16> {
82        self.data.header().horiz_offset.get()
83    }
84
85    /// Attempt to resolve [`horiz_offset`][Self::horiz_offset].
86    pub fn horiz(&self) -> Option<Result<TrackData<'a>, ReadError>> {
87        let data = self.offset_data();
88        self.horiz_offset().resolve(data)
89    }
90
91    /// Offset from start of tracking table to TrackData for vertical text (or 0 if none).
92    pub fn vert_offset(&self) -> Nullable<Offset16> {
93        self.data.header().vert_offset.get()
94    }
95
96    /// Attempt to resolve [`vert_offset`][Self::vert_offset].
97    pub fn vert(&self) -> Option<Result<TrackData<'a>, ReadError>> {
98        let data = self.offset_data();
99        self.vert_offset().resolve(data)
100    }
101
102    pub fn version_byte_range(&self) -> Range<usize> {
103        let start = 0;
104        let end = start + MajorMinor::RAW_BYTE_LEN;
105        start..end
106    }
107
108    pub fn format_byte_range(&self) -> Range<usize> {
109        let start = self.version_byte_range().end;
110        let end = start + u16::RAW_BYTE_LEN;
111        start..end
112    }
113
114    pub fn horiz_offset_byte_range(&self) -> Range<usize> {
115        let start = self.format_byte_range().end;
116        let end = start + Offset16::RAW_BYTE_LEN;
117        start..end
118    }
119
120    pub fn vert_offset_byte_range(&self) -> Range<usize> {
121        let start = self.horiz_offset_byte_range().end;
122        let end = start + Offset16::RAW_BYTE_LEN;
123        start..end
124    }
125
126    pub fn reserved_byte_range(&self) -> Range<usize> {
127        let start = self.vert_offset_byte_range().end;
128        let end = start + u16::RAW_BYTE_LEN;
129        start..end
130    }
131}
132
133const _: () = assert!(FontData::default_data_long_enough(Trak::MIN_SIZE));
134
135impl Default for Trak<'_> {
136    fn default() -> Self {
137        Self {
138            data: TableDataRef::new(FontData::default_table_data().as_bytes())
139                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
140        }
141    }
142}
143
144impl<'a> MinByteRange<'a> for TrackData<'a> {
145    fn min_byte_range(&self) -> Range<usize> {
146        0..self.track_table_byte_range().end
147    }
148    fn min_table_bytes(&self) -> &'a [u8] {
149        let range = self.min_byte_range();
150        self.offset_data().as_bytes().get(range).unwrap_or_default()
151    }
152}
153
154impl ReadArgs for TrackData<'_> {
155    type Args = ();
156}
157
158impl<'a> FontRead<'a> for TrackData<'a> {
159    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
160        #[allow(clippy::absurd_extreme_comparisons)]
161        if data.len() < Self::MIN_SIZE {
162            return Err(ReadError::OutOfBounds);
163        }
164        let Some(data) = TableDataRef::new(data.as_bytes()) else {
165            return Err(ReadError::OutOfBounds);
166        };
167        Ok(Self { data })
168    }
169}
170
171/// The fields at the start of the table, which are read directly.
172#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
173#[repr(C, packed)]
174#[doc(hidden)]
175pub struct TrackDataFixedHeader {
176    pub n_tracks: BigEndian<u16>,
177    pub n_sizes: BigEndian<u16>,
178    pub size_table_offset: BigEndian<u32>,
179}
180
181#[allow(clippy::absurd_extreme_comparisons)]
182const _: () = assert!(std::mem::size_of::<TrackDataFixedHeader>() <= TrackData::MIN_SIZE);
183
184/// The tracking data table.
185#[derive(Clone)]
186pub struct TrackData<'a> {
187    data: TableDataRef<'a, TrackDataFixedHeader>,
188}
189
190#[allow(clippy::needless_lifetimes)]
191impl<'a> TrackData<'a> {
192    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
193    basic_table_impls!(impl_the_methods);
194
195    /// Number of separate tracks included in this table.
196    pub fn n_tracks(&self) -> u16 {
197        self.data.header().n_tracks.get()
198    }
199
200    /// Number of point sizes included in this table.
201    pub fn n_sizes(&self) -> u16 {
202        self.data.header().n_sizes.get()
203    }
204
205    /// Offset from the start of the tracking table to the start of the size subtable.
206    pub fn size_table_offset(&self) -> u32 {
207        self.data.header().size_table_offset.get()
208    }
209
210    /// Array of TrackTableEntry records.
211    pub fn track_table(&self) -> &'a [TrackTableEntry] {
212        let range = self.track_table_byte_range();
213        self.offset_data()
214            .read_array(range)
215            .ok()
216            .unwrap_or_default()
217    }
218
219    pub fn n_tracks_byte_range(&self) -> Range<usize> {
220        let start = 0;
221        let end = start + u16::RAW_BYTE_LEN;
222        start..end
223    }
224
225    pub fn n_sizes_byte_range(&self) -> Range<usize> {
226        let start = self.n_tracks_byte_range().end;
227        let end = start + u16::RAW_BYTE_LEN;
228        start..end
229    }
230
231    pub fn size_table_offset_byte_range(&self) -> Range<usize> {
232        let start = self.n_sizes_byte_range().end;
233        let end = start + u32::RAW_BYTE_LEN;
234        start..end
235    }
236
237    pub fn track_table_byte_range(&self) -> Range<usize> {
238        let n_tracks = self.n_tracks();
239        let start = self.size_table_offset_byte_range().end;
240        let end =
241            start + (transforms::to_usize(n_tracks)).saturating_mul(TrackTableEntry::RAW_BYTE_LEN);
242        start..end
243    }
244}
245
246const _: () = assert!(FontData::default_data_long_enough(TrackData::MIN_SIZE));
247
248impl Default for TrackData<'_> {
249    fn default() -> Self {
250        Self {
251            data: TableDataRef::new(FontData::default_table_data().as_bytes())
252                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
253        }
254    }
255}
256
257/// Single entry in a tracking table.
258#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
259#[repr(C)]
260#[repr(packed)]
261pub struct TrackTableEntry {
262    /// Track value for this record.
263    pub track: BigEndian<Fixed>,
264    /// The 'name' table index for this track (a short word or phrase like \"loose\" or \"very tight\"). NameIndex has a value greater than 255 and less than 32768.
265    pub name_index: BigEndian<NameId>,
266    /// Offset from the start of the tracking table to per-size tracking values for this track.
267    pub offset: BigEndian<u16>,
268}
269
270impl TrackTableEntry {
271    /// Track value for this record.
272    pub fn track(&self) -> Fixed {
273        self.track.get()
274    }
275
276    /// The 'name' table index for this track (a short word or phrase like \"loose\" or \"very tight\"). NameIndex has a value greater than 255 and less than 32768.
277    pub fn name_index(&self) -> NameId {
278        self.name_index.get()
279    }
280
281    /// Offset from the start of the tracking table to per-size tracking values for this track.
282    pub fn offset(&self) -> u16 {
283        self.offset.get()
284    }
285}
286
287impl FixedSize for TrackTableEntry {
288    const RAW_BYTE_LEN: usize = Fixed::RAW_BYTE_LEN + NameId::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
289}