Coverage Report

Created: 2026-10-24 07:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/read-fonts-0.46.0/generated/generated_trak.rs
Line
Count
Source
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)]
6
use crate::codegen_prelude::*;
7
8
impl<'a> MinByteRange<'a> for Trak<'a> {
9
0
    fn min_byte_range(&self) -> Range<usize> {
10
0
        0..self.reserved_byte_range().end
11
0
    }
12
0
    fn min_table_bytes(&self) -> &'a [u8] {
13
0
        let range = self.min_byte_range();
14
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
15
0
    }
16
}
17
18
impl TopLevelTable for Trak<'_> {
19
    /// `trak`
20
    const TAG: Tag = Tag::new(b"trak");
21
}
22
23
impl ReadArgs for Trak<'_> {
24
    type Args = ();
25
}
26
27
impl<'a> FontRead<'a> for Trak<'a> {
28
76
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
29
        #[allow(clippy::absurd_extreme_comparisons)]
30
76
        if data.len() < Self::MIN_SIZE {
31
11
            return Err(ReadError::OutOfBounds);
32
65
        }
33
65
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
34
0
            return Err(ReadError::OutOfBounds);
35
        };
36
65
        Ok(Self { data })
37
76
    }
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)]
44
pub 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)]
53
const _: () = 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)]
57
pub struct Trak<'a> {
58
    data: TableDataRef<'a, TrakFixedHeader>,
59
}
60
61
#[allow(clippy::needless_lifetimes)]
62
impl<'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
0
    pub fn version(&self) -> MajorMinor {
72
0
        self.data.header().version.get()
73
0
    }
74
75
    /// Format of the tracking table (set to 0).
76
0
    pub fn format(&self) -> u16 {
77
0
        self.data.header().format.get()
78
0
    }
79
80
    /// Offset from start of tracking table to TrackData for horizontal text (or 0 if none).
81
79
    pub fn horiz_offset(&self) -> Nullable<Offset16> {
82
79
        self.data.header().horiz_offset.get()
83
79
    }
84
85
    /// Attempt to resolve [`horiz_offset`][Self::horiz_offset].
86
79
    pub fn horiz(&self) -> Option<Result<TrackData<'a>, ReadError>> {
87
79
        let data = self.offset_data();
88
79
        self.horiz_offset().resolve(data)
89
79
    }
90
91
    /// Offset from start of tracking table to TrackData for vertical text (or 0 if none).
92
0
    pub fn vert_offset(&self) -> Nullable<Offset16> {
93
0
        self.data.header().vert_offset.get()
94
0
    }
95
96
    /// Attempt to resolve [`vert_offset`][Self::vert_offset].
97
0
    pub fn vert(&self) -> Option<Result<TrackData<'a>, ReadError>> {
98
0
        let data = self.offset_data();
99
0
        self.vert_offset().resolve(data)
100
0
    }
101
102
0
    pub fn version_byte_range(&self) -> Range<usize> {
103
0
        let start = 0;
104
0
        let end = start + MajorMinor::RAW_BYTE_LEN;
105
0
        start..end
106
0
    }
107
108
0
    pub fn format_byte_range(&self) -> Range<usize> {
109
0
        let start = self.version_byte_range().end;
110
0
        let end = start + u16::RAW_BYTE_LEN;
111
0
        start..end
112
0
    }
113
114
0
    pub fn horiz_offset_byte_range(&self) -> Range<usize> {
115
0
        let start = self.format_byte_range().end;
116
0
        let end = start + Offset16::RAW_BYTE_LEN;
117
0
        start..end
118
0
    }
119
120
0
    pub fn vert_offset_byte_range(&self) -> Range<usize> {
121
0
        let start = self.horiz_offset_byte_range().end;
122
0
        let end = start + Offset16::RAW_BYTE_LEN;
123
0
        start..end
124
0
    }
125
126
0
    pub fn reserved_byte_range(&self) -> Range<usize> {
127
0
        let start = self.vert_offset_byte_range().end;
128
0
        let end = start + u16::RAW_BYTE_LEN;
129
0
        start..end
130
0
    }
131
}
132
133
const _: () = assert!(FontData::default_data_long_enough(Trak::MIN_SIZE));
134
135
impl Default for Trak<'_> {
136
0
    fn default() -> Self {
137
        Self {
138
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
139
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
140
        }
141
0
    }
142
}
143
144
impl<'a> MinByteRange<'a> for TrackData<'a> {
145
0
    fn min_byte_range(&self) -> Range<usize> {
146
0
        0..self.track_table_byte_range().end
147
0
    }
148
0
    fn min_table_bytes(&self) -> &'a [u8] {
149
0
        let range = self.min_byte_range();
150
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
151
0
    }
152
}
153
154
impl ReadArgs for TrackData<'_> {
155
    type Args = ();
156
}
157
158
impl<'a> FontRead<'a> for TrackData<'a> {
159
54
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
160
        #[allow(clippy::absurd_extreme_comparisons)]
161
54
        if data.len() < Self::MIN_SIZE {
162
5
            return Err(ReadError::OutOfBounds);
163
49
        }
164
49
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
165
0
            return Err(ReadError::OutOfBounds);
166
        };
167
49
        Ok(Self { data })
168
54
    }
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)]
175
pub 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)]
182
const _: () = assert!(std::mem::size_of::<TrackDataFixedHeader>() <= TrackData::MIN_SIZE);
183
184
/// The tracking data table.
185
#[derive(Clone)]
186
pub struct TrackData<'a> {
187
    data: TableDataRef<'a, TrackDataFixedHeader>,
188
}
189
190
#[allow(clippy::needless_lifetimes)]
191
impl<'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
37
    pub fn n_tracks(&self) -> u16 {
197
37
        self.data.header().n_tracks.get()
198
37
    }
199
200
    /// Number of point sizes included in this table.
201
42
    pub fn n_sizes(&self) -> u16 {
202
42
        self.data.header().n_sizes.get()
203
42
    }
204
205
    /// Offset from the start of the tracking table to the start of the size subtable.
206
49
    pub fn size_table_offset(&self) -> u32 {
207
49
        self.data.header().size_table_offset.get()
208
49
    }
209
210
    /// Array of TrackTableEntry records.
211
37
    pub fn track_table(&self) -> &'a [TrackTableEntry] {
212
37
        let range = self.track_table_byte_range();
213
37
        self.offset_data()
214
37
            .read_array(range)
215
37
            .ok()
216
37
            .unwrap_or_default()
217
37
    }
218
219
37
    pub fn n_tracks_byte_range(&self) -> Range<usize> {
220
37
        let start = 0;
221
37
        let end = start + u16::RAW_BYTE_LEN;
222
37
        start..end
223
37
    }
224
225
37
    pub fn n_sizes_byte_range(&self) -> Range<usize> {
226
37
        let start = self.n_tracks_byte_range().end;
227
37
        let end = start + u16::RAW_BYTE_LEN;
228
37
        start..end
229
37
    }
230
231
37
    pub fn size_table_offset_byte_range(&self) -> Range<usize> {
232
37
        let start = self.n_sizes_byte_range().end;
233
37
        let end = start + u32::RAW_BYTE_LEN;
234
37
        start..end
235
37
    }
236
237
37
    pub fn track_table_byte_range(&self) -> Range<usize> {
238
37
        let n_tracks = self.n_tracks();
239
37
        let start = self.size_table_offset_byte_range().end;
240
37
        let end =
241
37
            start + (transforms::to_usize(n_tracks)).saturating_mul(TrackTableEntry::RAW_BYTE_LEN);
242
37
        start..end
243
37
    }
244
}
245
246
const _: () = assert!(FontData::default_data_long_enough(TrackData::MIN_SIZE));
247
248
impl Default for TrackData<'_> {
249
0
    fn default() -> Self {
250
        Self {
251
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
252
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
253
        }
254
0
    }
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)]
261
pub 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
270
impl TrackTableEntry {
271
    /// Track value for this record.
272
267
    pub fn track(&self) -> Fixed {
273
267
        self.track.get()
274
267
    }
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
0
    pub fn name_index(&self) -> NameId {
278
0
        self.name_index.get()
279
0
    }
280
281
    /// Offset from the start of the tracking table to per-size tracking values for this track.
282
58
    pub fn offset(&self) -> u16 {
283
58
        self.offset.get()
284
58
    }
285
}
286
287
impl FixedSize for TrackTableEntry {
288
    const RAW_BYTE_LEN: usize = Fixed::RAW_BYTE_LEN + NameId::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
289
}