Coverage Report

Created: 2026-10-28 07:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fontations/read-fonts/generated/generated_cff2.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 Cff2Header<'a> {
9
0
    fn min_byte_range(&self) -> Range<usize> {
10
0
        0..self.trailing_data_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 Cff2Header<'_> {
19
    type Args = ();
20
}
21
22
impl<'a> FontRead<'a> for Cff2Header<'a> {
23
0
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
24
        #[allow(clippy::absurd_extreme_comparisons)]
25
0
        if data.len() < Self::MIN_SIZE {
26
0
            return Err(ReadError::OutOfBounds);
27
0
        }
28
0
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
29
0
            return Err(ReadError::OutOfBounds);
30
        };
31
0
        Ok(Self { data })
32
0
    }
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 Cff2HeaderFixedHeader {
40
    pub major_version: u8,
41
    pub minor_version: u8,
42
    pub header_size: u8,
43
    pub top_dict_length: BigEndian<u16>,
44
}
45
46
#[allow(clippy::absurd_extreme_comparisons)]
47
const _: () = assert!(std::mem::size_of::<Cff2HeaderFixedHeader>() <= Cff2Header::MIN_SIZE);
48
49
/// [Compact Font Format (CFF) version 2](https://learn.microsoft.com/en-us/typography/opentype/spec/cff2) table header
50
#[derive(Clone)]
51
pub struct Cff2Header<'a> {
52
    data: TableDataRef<'a, Cff2HeaderFixedHeader>,
53
}
54
55
#[allow(clippy::needless_lifetimes)]
56
impl<'a> Cff2Header<'a> {
57
    pub const MIN_SIZE: usize =
58
        (u8::RAW_BYTE_LEN + u8::RAW_BYTE_LEN + u8::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
59
    basic_table_impls!(impl_the_methods);
60
61
    /// Format major version (set to 2).
62
0
    pub fn major_version(&self) -> u8 {
63
0
        self.data.header().major_version
64
0
    }
65
66
    /// Format minor version (set to 0).
67
0
    pub fn minor_version(&self) -> u8 {
68
0
        self.data.header().minor_version
69
0
    }
70
71
    /// Header size (bytes).
72
0
    pub fn header_size(&self) -> u8 {
73
0
        self.data.header().header_size
74
0
    }
75
76
    /// Length of Top DICT structure in bytes.
77
0
    pub fn top_dict_length(&self) -> u16 {
78
0
        self.data.header().top_dict_length.get()
79
0
    }
80
81
    /// Padding bytes before the start of the Top DICT.
82
0
    pub fn _padding(&self) -> &'a [u8] {
83
0
        let range = self._padding_byte_range();
84
0
        self.offset_data()
85
0
            .read_array(range)
86
0
            .ok()
87
0
            .unwrap_or_default()
88
0
    }
89
90
    /// Data containing the Top DICT.
91
0
    pub fn top_dict_data(&self) -> &'a [u8] {
92
0
        let range = self.top_dict_data_byte_range();
93
0
        self.offset_data()
94
0
            .read_array(range)
95
0
            .ok()
96
0
            .unwrap_or_default()
97
0
    }
98
99
    /// Remaining table data.
100
0
    pub fn trailing_data(&self) -> &'a [u8] {
101
0
        let range = self.trailing_data_byte_range();
102
0
        self.offset_data()
103
0
            .read_array(range)
104
0
            .ok()
105
0
            .unwrap_or_default()
106
0
    }
107
108
0
    pub fn major_version_byte_range(&self) -> Range<usize> {
109
0
        let start = 0usize;
110
0
        let end = start.saturating_add(u8::RAW_BYTE_LEN);
111
0
        start..end
112
0
    }
113
114
0
    pub fn minor_version_byte_range(&self) -> Range<usize> {
115
0
        let start = self.major_version_byte_range().end;
116
0
        let end = start.saturating_add(u8::RAW_BYTE_LEN);
117
0
        start..end
118
0
    }
119
120
0
    pub fn header_size_byte_range(&self) -> Range<usize> {
121
0
        let start = self.minor_version_byte_range().end;
122
0
        let end = start.saturating_add(u8::RAW_BYTE_LEN);
123
0
        start..end
124
0
    }
125
126
0
    pub fn top_dict_length_byte_range(&self) -> Range<usize> {
127
0
        let start = self.header_size_byte_range().end;
128
0
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
129
0
        start..end
130
0
    }
131
132
0
    pub fn _padding_byte_range(&self) -> Range<usize> {
133
0
        let header_size = self.header_size();
134
0
        let start = self.top_dict_length_byte_range().end;
135
0
        let end = start.saturating_add(
136
0
            (transforms::subtract(header_size, 5_usize)).saturating_mul(u8::RAW_BYTE_LEN),
137
        );
138
0
        start..end
139
0
    }
140
141
0
    pub fn top_dict_data_byte_range(&self) -> Range<usize> {
142
0
        let top_dict_length = self.top_dict_length();
143
0
        let start = self._padding_byte_range().end;
144
0
        let end = start.saturating_add(
145
0
            (transforms::to_usize(top_dict_length)).saturating_mul(u8::RAW_BYTE_LEN),
146
        );
147
0
        start..end
148
0
    }
149
150
0
    pub fn trailing_data_byte_range(&self) -> Range<usize> {
151
0
        let start = self.top_dict_data_byte_range().end;
152
0
        let end = start.saturating_add(
153
0
            self.offset_data().len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN,
154
        );
155
0
        start..end
156
0
    }
157
}
158
159
const _: () = assert!(FontData::default_data_long_enough(Cff2Header::MIN_SIZE));
160
161
impl Default for Cff2Header<'_> {
162
0
    fn default() -> Self {
163
        Self {
164
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
165
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
166
        }
167
0
    }
168
}
169
170
impl<'a> MinByteRange<'a> for Index<'a> {
171
0
    fn min_byte_range(&self) -> Range<usize> {
172
0
        0..self.data_byte_range().end
173
0
    }
174
0
    fn min_table_bytes(&self) -> &'a [u8] {
175
0
        let range = self.min_byte_range();
176
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
177
0
    }
178
}
179
180
impl ReadArgs for Index<'_> {
181
    type Args = ();
182
}
183
184
impl<'a> FontRead<'a> for Index<'a> {
185
0
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
186
        #[allow(clippy::absurd_extreme_comparisons)]
187
0
        if data.len() < Self::MIN_SIZE {
188
0
            return Err(ReadError::OutOfBounds);
189
0
        }
190
0
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
191
0
            return Err(ReadError::OutOfBounds);
192
        };
193
0
        Ok(Self { data })
194
0
    }
195
}
196
197
/// The fields at the start of the table, which are read directly.
198
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
199
#[repr(C, packed)]
200
#[doc(hidden)]
201
pub struct IndexFixedHeader {
202
    pub count: BigEndian<u32>,
203
    pub off_size: u8,
204
}
205
206
#[allow(clippy::absurd_extreme_comparisons)]
207
const _: () = assert!(std::mem::size_of::<IndexFixedHeader>() <= Index::MIN_SIZE);
208
209
/// An array of variable-sized objects in a `CFF2` table.
210
#[derive(Clone)]
211
pub struct Index<'a> {
212
    data: TableDataRef<'a, IndexFixedHeader>,
213
}
214
215
#[allow(clippy::needless_lifetimes)]
216
impl<'a> Index<'a> {
217
    pub const MIN_SIZE: usize = (u32::RAW_BYTE_LEN + u8::RAW_BYTE_LEN);
218
    basic_table_impls!(impl_the_methods);
219
220
    /// Number of objects stored in INDEX.
221
0
    pub fn count(&self) -> u32 {
222
0
        self.data.header().count.get()
223
0
    }
224
225
    /// Object array element size.
226
0
    pub fn off_size(&self) -> u8 {
227
0
        self.data.header().off_size
228
0
    }
229
230
    /// Bytes containing `count + 1` offsets each of `off_size`.
231
0
    pub fn offsets(&self) -> &'a [u8] {
232
0
        let range = self.offsets_byte_range();
233
0
        self.offset_data()
234
0
            .read_array(range)
235
0
            .ok()
236
0
            .unwrap_or_default()
237
0
    }
238
239
    /// Array containing the object data.
240
0
    pub fn data(&self) -> &'a [u8] {
241
0
        let range = self.data_byte_range();
242
0
        self.offset_data()
243
0
            .read_array(range)
244
0
            .ok()
245
0
            .unwrap_or_default()
246
0
    }
247
248
0
    pub fn count_byte_range(&self) -> Range<usize> {
249
0
        let start = 0usize;
250
0
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
251
0
        start..end
252
0
    }
253
254
0
    pub fn off_size_byte_range(&self) -> Range<usize> {
255
0
        let start = self.count_byte_range().end;
256
0
        let end = start.saturating_add(u8::RAW_BYTE_LEN);
257
0
        start..end
258
0
    }
259
260
0
    pub fn offsets_byte_range(&self) -> Range<usize> {
261
0
        let count = self.count();
262
0
        let off_size = self.off_size();
263
0
        let start = self.off_size_byte_range().end;
264
0
        let end = start.saturating_add(
265
0
            (transforms::add_multiply(count, 1_usize, off_size)).saturating_mul(u8::RAW_BYTE_LEN),
266
        );
267
0
        start..end
268
0
    }
269
270
0
    pub fn data_byte_range(&self) -> Range<usize> {
271
0
        let start = self.offsets_byte_range().end;
272
0
        let end = start.saturating_add(
273
0
            self.offset_data().len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN,
274
        );
275
0
        start..end
276
0
    }
277
}
278
279
const _: () = assert!(FontData::default_data_long_enough(Index::MIN_SIZE));
280
281
impl Default for Index<'_> {
282
0
    fn default() -> Self {
283
        Self {
284
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
285
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
286
        }
287
0
    }
288
}