Coverage Report

Created: 2026-12-01 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/read-fonts-0.47.0/generated/font.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 TableDirectory<'a> {
9
0
    fn min_byte_range(&self) -> Range<usize> {
10
0
        0..self.table_records_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 ReadArgs for TableDirectory<'_> {
19
    type Args = ();
20
}
21
22
impl<'a> FontRead<'a> for TableDirectory<'a> {
23
101k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
24
        #[allow(clippy::absurd_extreme_comparisons)]
25
101k
        if data.len() < Self::MIN_SIZE {
26
235
            return Err(ReadError::OutOfBounds);
27
101k
        }
28
101k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
29
0
            return Err(ReadError::OutOfBounds);
30
        };
31
101k
        Ok(Self { data })
32
101k
    }
33
}
34
35
/// The fields at the start of the table, which are read directly.
36
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
37
#[repr(C, packed)]
38
#[doc(hidden)]
39
pub struct TableDirectoryFixedHeader {
40
    pub sfnt_version: BigEndian<u32>,
41
    pub num_tables: BigEndian<u16>,
42
    pub search_range: BigEndian<u16>,
43
    pub entry_selector: BigEndian<u16>,
44
    pub range_shift: BigEndian<u16>,
45
}
46
47
#[allow(clippy::absurd_extreme_comparisons)]
48
const _: () = assert!(std::mem::size_of::<TableDirectoryFixedHeader>() <= TableDirectory::MIN_SIZE);
49
50
/// The OpenType [Table Directory](https://docs.microsoft.com/en-us/typography/opentype/spec/otff#table-directory)
51
#[derive(Clone)]
52
pub struct TableDirectory<'a> {
53
    data: TableDataRef<'a, TableDirectoryFixedHeader>,
54
}
55
56
#[allow(clippy::needless_lifetimes)]
57
impl<'a> TableDirectory<'a> {
58
    pub const MIN_SIZE: usize = (u32::RAW_BYTE_LEN
59
        + u16::RAW_BYTE_LEN
60
        + u16::RAW_BYTE_LEN
61
        + u16::RAW_BYTE_LEN
62
        + u16::RAW_BYTE_LEN);
63
    basic_table_impls!(impl_the_methods);
64
65
    /// 0x00010000 or 0x4F54544F
66
105k
    pub fn sfnt_version(&self) -> u32 {
67
105k
        self.data.header().sfnt_version.get()
68
105k
    }
69
70
    /// Number of tables.
71
245k
    pub fn num_tables(&self) -> u16 {
72
245k
        self.data.header().num_tables.get()
73
245k
    }
74
75
0
    pub fn search_range(&self) -> u16 {
76
0
        self.data.header().search_range.get()
77
0
    }
78
79
0
    pub fn entry_selector(&self) -> u16 {
80
0
        self.data.header().entry_selector.get()
81
0
    }
82
83
0
    pub fn range_shift(&self) -> u16 {
84
0
        self.data.header().range_shift.get()
85
0
    }
86
87
    /// Table records array—one for each top-level table in the font
88
245k
    pub fn table_records(&self) -> &'a [TableRecord] {
89
245k
        let range = self.table_records_byte_range();
90
245k
        self.offset_data()
91
245k
            .read_array(range)
92
245k
            .ok()
93
245k
            .unwrap_or_default()
94
245k
    }
95
96
245k
    pub fn sfnt_version_byte_range(&self) -> Range<usize> {
97
245k
        let start = 0usize;
98
245k
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
99
245k
        start..end
100
245k
    }
101
102
245k
    pub fn num_tables_byte_range(&self) -> Range<usize> {
103
245k
        let start = self.sfnt_version_byte_range().end;
104
245k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
105
245k
        start..end
106
245k
    }
107
108
245k
    pub fn search_range_byte_range(&self) -> Range<usize> {
109
245k
        let start = self.num_tables_byte_range().end;
110
245k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
111
245k
        start..end
112
245k
    }
113
114
245k
    pub fn entry_selector_byte_range(&self) -> Range<usize> {
115
245k
        let start = self.search_range_byte_range().end;
116
245k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
117
245k
        start..end
118
245k
    }
119
120
245k
    pub fn range_shift_byte_range(&self) -> Range<usize> {
121
245k
        let start = self.entry_selector_byte_range().end;
122
245k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
123
245k
        start..end
124
245k
    }
125
126
245k
    pub fn table_records_byte_range(&self) -> Range<usize> {
127
245k
        let num_tables = self.num_tables();
128
245k
        let start = self.range_shift_byte_range().end;
129
245k
        let end = start.saturating_add(
130
245k
            (transforms::to_usize(num_tables)).saturating_mul(TableRecord::RAW_BYTE_LEN),
131
        );
132
245k
        start..end
133
245k
    }
134
}
135
136
const _: () = assert!(FontData::default_data_long_enough(TableDirectory::MIN_SIZE));
137
138
impl Default for TableDirectory<'_> {
139
0
    fn default() -> Self {
140
        Self {
141
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
142
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
143
        }
144
0
    }
145
}
146
147
/// Record for a table in a font.
148
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
149
#[repr(C)]
150
#[repr(packed)]
151
pub struct TableRecord {
152
    /// Table identifier.
153
    pub tag: BigEndian<Tag>,
154
    /// Checksum for the table.
155
    pub checksum: BigEndian<u32>,
156
    /// Offset from the beginning of the font data.
157
    pub offset: BigEndian<u32>,
158
    /// Length of the table.
159
    pub length: BigEndian<u32>,
160
}
161
162
impl TableRecord {
163
    /// Table identifier.
164
23.6M
    pub fn tag(&self) -> Tag {
165
23.6M
        self.tag.get()
166
23.6M
    }
167
168
    /// Checksum for the table.
169
0
    pub fn checksum(&self) -> u32 {
170
0
        self.checksum.get()
171
0
    }
172
173
    /// Offset from the beginning of the font data.
174
92.9k
    pub fn offset(&self) -> u32 {
175
92.9k
        self.offset.get()
176
92.9k
    }
177
178
    /// Length of the table.
179
92.8k
    pub fn length(&self) -> u32 {
180
92.8k
        self.length.get()
181
92.8k
    }
182
}
183
184
impl FixedSize for TableRecord {
185
    const RAW_BYTE_LEN: usize =
186
        Tag::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN;
187
}
188
189
impl<'a> MinByteRange<'a> for TTCHeader<'a> {
190
0
    fn min_byte_range(&self) -> Range<usize> {
191
0
        0..self.table_directory_offsets_byte_range().end
192
0
    }
193
0
    fn min_table_bytes(&self) -> &'a [u8] {
194
0
        let range = self.min_byte_range();
195
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
196
0
    }
197
}
198
199
impl ReadArgs for TTCHeader<'_> {
200
    type Args = ();
201
}
202
203
impl<'a> FontRead<'a> for TTCHeader<'a> {
204
96.3k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
205
        #[allow(clippy::absurd_extreme_comparisons)]
206
96.3k
        if data.len() < Self::MIN_SIZE {
207
31
            return Err(ReadError::OutOfBounds);
208
96.2k
        }
209
96.2k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
210
0
            return Err(ReadError::OutOfBounds);
211
        };
212
96.2k
        Ok(Self { data })
213
96.3k
    }
214
}
215
216
/// The fields at the start of the table, which are read directly.
217
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
218
#[repr(C, packed)]
219
#[doc(hidden)]
220
pub struct TTCHeaderFixedHeader {
221
    pub ttc_tag: BigEndian<Tag>,
222
    pub version: BigEndian<MajorMinor>,
223
    pub num_fonts: BigEndian<u32>,
224
}
225
226
#[allow(clippy::absurd_extreme_comparisons)]
227
const _: () = assert!(std::mem::size_of::<TTCHeaderFixedHeader>() <= TTCHeader::MIN_SIZE);
228
229
/// [TTC Header](https://learn.microsoft.com/en-us/typography/opentype/spec/otff#ttc-header)
230
#[derive(Clone)]
231
pub struct TTCHeader<'a> {
232
    data: TableDataRef<'a, TTCHeaderFixedHeader>,
233
}
234
235
#[allow(clippy::needless_lifetimes)]
236
impl<'a> TTCHeader<'a> {
237
    pub const MIN_SIZE: usize = (Tag::RAW_BYTE_LEN + MajorMinor::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
238
    basic_table_impls!(impl_the_methods);
239
240
    /// Font Collection ID string: \"ttcf\"
241
192k
    pub fn ttc_tag(&self) -> Tag {
242
192k
        self.data.header().ttc_tag.get()
243
192k
    }
244
245
    /// Major/minor version of the TTC Header
246
301
    pub fn version(&self) -> MajorMinor {
247
301
        self.data.header().version.get()
248
301
    }
249
250
    /// Number of fonts in TTC
251
301
    pub fn num_fonts(&self) -> u32 {
252
301
        self.data.header().num_fonts.get()
253
301
    }
254
255
    /// Array of offsets to the TableDirectory for each font from the beginning of the file
256
0
    pub fn table_directory_offsets(&self) -> &'a [BigEndian<u32>] {
257
0
        let range = self.table_directory_offsets_byte_range();
258
0
        self.offset_data()
259
0
            .read_array(range)
260
0
            .ok()
261
0
            .unwrap_or_default()
262
0
    }
263
264
    /// Tag indicating that a DSIG table exists, 0x44534947 ('DSIG') (null if no signature)
265
0
    pub fn dsig_tag(&self) -> Option<u32> {
266
0
        let range = self.dsig_tag_byte_range();
267
0
        (!range.is_empty())
268
0
            .then(|| self.offset_data().read_at(range.start).ok())
269
0
            .flatten()
270
0
    }
271
272
    /// The length (in bytes) of the DSIG table (null if no signature)
273
0
    pub fn dsig_length(&self) -> Option<u32> {
274
0
        let range = self.dsig_length_byte_range();
275
0
        (!range.is_empty())
276
0
            .then(|| self.offset_data().read_at(range.start).ok())
277
0
            .flatten()
278
0
    }
279
280
    /// The offset (in bytes) of the DSIG table from the beginning of the TTC file (null if no signature)
281
0
    pub fn dsig_offset(&self) -> Option<u32> {
282
0
        let range = self.dsig_offset_byte_range();
283
0
        (!range.is_empty())
284
0
            .then(|| self.offset_data().read_at(range.start).ok())
285
0
            .flatten()
286
0
    }
287
288
0
    pub fn ttc_tag_byte_range(&self) -> Range<usize> {
289
0
        let start = 0usize;
290
0
        let end = start.saturating_add(Tag::RAW_BYTE_LEN);
291
0
        start..end
292
0
    }
293
294
0
    pub fn version_byte_range(&self) -> Range<usize> {
295
0
        let start = self.ttc_tag_byte_range().end;
296
0
        let end = start.saturating_add(MajorMinor::RAW_BYTE_LEN);
297
0
        start..end
298
0
    }
299
300
0
    pub fn num_fonts_byte_range(&self) -> Range<usize> {
301
0
        let start = self.version_byte_range().end;
302
0
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
303
0
        start..end
304
0
    }
305
306
0
    pub fn table_directory_offsets_byte_range(&self) -> Range<usize> {
307
0
        let num_fonts = self.num_fonts();
308
0
        let start = self.num_fonts_byte_range().end;
309
0
        let end = start
310
0
            .saturating_add((transforms::to_usize(num_fonts)).saturating_mul(u32::RAW_BYTE_LEN));
311
0
        start..end
312
0
    }
313
314
0
    pub fn dsig_tag_byte_range(&self) -> Range<usize> {
315
0
        let start = self.table_directory_offsets_byte_range().end;
316
0
        let end = if self.version().compatible((2u16, 0u16)) {
317
0
            start.saturating_add(u32::RAW_BYTE_LEN)
318
        } else {
319
0
            start
320
        };
321
0
        start..end
322
0
    }
323
324
0
    pub fn dsig_length_byte_range(&self) -> Range<usize> {
325
0
        let start = self.dsig_tag_byte_range().end;
326
0
        let end = if self.version().compatible((2u16, 0u16)) {
327
0
            start.saturating_add(u32::RAW_BYTE_LEN)
328
        } else {
329
0
            start
330
        };
331
0
        start..end
332
0
    }
333
334
0
    pub fn dsig_offset_byte_range(&self) -> Range<usize> {
335
0
        let start = self.dsig_length_byte_range().end;
336
0
        let end = if self.version().compatible((2u16, 0u16)) {
337
0
            start.saturating_add(u32::RAW_BYTE_LEN)
338
        } else {
339
0
            start
340
        };
341
0
        start..end
342
0
    }
343
}
344
345
const _: () = assert!(FontData::default_data_long_enough(TTCHeader::MIN_SIZE));
346
347
impl Default for TTCHeader<'_> {
348
0
    fn default() -> Self {
349
        Self {
350
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
351
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
352
        }
353
0
    }
354
}