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_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
92
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
24
        #[allow(clippy::absurd_extreme_comparisons)]
25
92
        if data.len() < Self::MIN_SIZE {
26
1
            return Err(ReadError::OutOfBounds);
27
91
        }
28
91
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
29
0
            return Err(ReadError::OutOfBounds);
30
        };
31
91
        Ok(Self { data })
32
92
    }
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
159
    pub fn header_size(&self) -> u8 {
73
159
        self.data.header().header_size
74
159
    }
75
76
    /// Length of Top DICT structure in bytes.
77
159
    pub fn top_dict_length(&self) -> u16 {
78
159
        self.data.header().top_dict_length.get()
79
159
    }
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
68
    pub fn top_dict_data(&self) -> &'a [u8] {
92
68
        let range = self.top_dict_data_byte_range();
93
68
        self.offset_data()
94
68
            .read_array(range)
95
68
            .ok()
96
68
            .unwrap_or_default()
97
68
    }
98
99
    /// Remaining table data.
100
91
    pub fn trailing_data(&self) -> &'a [u8] {
101
91
        let range = self.trailing_data_byte_range();
102
91
        self.offset_data()
103
91
            .read_array(range)
104
91
            .ok()
105
91
            .unwrap_or_default()
106
91
    }
107
108
159
    pub fn major_version_byte_range(&self) -> Range<usize> {
109
159
        let start = 0;
110
159
        let end = start + u8::RAW_BYTE_LEN;
111
159
        start..end
112
159
    }
113
114
159
    pub fn minor_version_byte_range(&self) -> Range<usize> {
115
159
        let start = self.major_version_byte_range().end;
116
159
        let end = start + u8::RAW_BYTE_LEN;
117
159
        start..end
118
159
    }
119
120
159
    pub fn header_size_byte_range(&self) -> Range<usize> {
121
159
        let start = self.minor_version_byte_range().end;
122
159
        let end = start + u8::RAW_BYTE_LEN;
123
159
        start..end
124
159
    }
125
126
159
    pub fn top_dict_length_byte_range(&self) -> Range<usize> {
127
159
        let start = self.header_size_byte_range().end;
128
159
        let end = start + u16::RAW_BYTE_LEN;
129
159
        start..end
130
159
    }
131
132
159
    pub fn _padding_byte_range(&self) -> Range<usize> {
133
159
        let header_size = self.header_size();
134
159
        let start = self.top_dict_length_byte_range().end;
135
159
        let end =
136
159
            start + (transforms::subtract(header_size, 5_usize)).saturating_mul(u8::RAW_BYTE_LEN);
137
159
        start..end
138
159
    }
139
140
159
    pub fn top_dict_data_byte_range(&self) -> Range<usize> {
141
159
        let top_dict_length = self.top_dict_length();
142
159
        let start = self._padding_byte_range().end;
143
159
        let end = start + (transforms::to_usize(top_dict_length)).saturating_mul(u8::RAW_BYTE_LEN);
144
159
        start..end
145
159
    }
146
147
91
    pub fn trailing_data_byte_range(&self) -> Range<usize> {
148
91
        let start = self.top_dict_data_byte_range().end;
149
91
        let end = start
150
91
            + self.offset_data().len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN;
151
91
        start..end
152
91
    }
153
}
154
155
const _: () = assert!(FontData::default_data_long_enough(Cff2Header::MIN_SIZE));
156
157
impl Default for Cff2Header<'_> {
158
0
    fn default() -> Self {
159
        Self {
160
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
161
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
162
        }
163
0
    }
164
}
165
166
impl<'a> MinByteRange<'a> for Index<'a> {
167
0
    fn min_byte_range(&self) -> Range<usize> {
168
0
        0..self.data_byte_range().end
169
0
    }
170
0
    fn min_table_bytes(&self) -> &'a [u8] {
171
0
        let range = self.min_byte_range();
172
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
173
0
    }
174
}
175
176
impl ReadArgs for Index<'_> {
177
    type Args = ();
178
}
179
180
impl<'a> FontRead<'a> for Index<'a> {
181
13.6M
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
182
        #[allow(clippy::absurd_extreme_comparisons)]
183
13.6M
        if data.len() < Self::MIN_SIZE {
184
18
            return Err(ReadError::OutOfBounds);
185
13.6M
        }
186
13.6M
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
187
0
            return Err(ReadError::OutOfBounds);
188
        };
189
13.6M
        Ok(Self { data })
190
13.6M
    }
191
}
192
193
/// The fields at the start of the table, which are read directly.
194
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
195
#[repr(C, packed)]
196
#[doc(hidden)]
197
pub struct IndexFixedHeader {
198
    pub count: BigEndian<u32>,
199
    pub off_size: u8,
200
}
201
202
#[allow(clippy::absurd_extreme_comparisons)]
203
const _: () = assert!(std::mem::size_of::<IndexFixedHeader>() <= Index::MIN_SIZE);
204
205
/// An array of variable-sized objects in a `CFF2` table.
206
#[derive(Clone)]
207
pub struct Index<'a> {
208
    data: TableDataRef<'a, IndexFixedHeader>,
209
}
210
211
#[allow(clippy::needless_lifetimes)]
212
impl<'a> Index<'a> {
213
    pub const MIN_SIZE: usize = (u32::RAW_BYTE_LEN + u8::RAW_BYTE_LEN);
214
    basic_table_impls!(impl_the_methods);
215
216
    /// Number of objects stored in INDEX.
217
41.0M
    pub fn count(&self) -> u32 {
218
41.0M
        self.data.header().count.get()
219
41.0M
    }
220
221
    /// Object array element size.
222
41.0M
    pub fn off_size(&self) -> u8 {
223
41.0M
        self.data.header().off_size
224
41.0M
    }
225
226
    /// Bytes containing `count + 1` offsets each of `off_size`.
227
13.6M
    pub fn offsets(&self) -> &'a [u8] {
228
13.6M
        let range = self.offsets_byte_range();
229
13.6M
        self.offset_data()
230
13.6M
            .read_array(range)
231
13.6M
            .ok()
232
13.6M
            .unwrap_or_default()
233
13.6M
    }
234
235
    /// Array containing the object data.
236
13.6M
    pub fn data(&self) -> &'a [u8] {
237
13.6M
        let range = self.data_byte_range();
238
13.6M
        self.offset_data()
239
13.6M
            .read_array(range)
240
13.6M
            .ok()
241
13.6M
            .unwrap_or_default()
242
13.6M
    }
243
244
27.3M
    pub fn count_byte_range(&self) -> Range<usize> {
245
27.3M
        let start = 0;
246
27.3M
        let end = start + u32::RAW_BYTE_LEN;
247
27.3M
        start..end
248
27.3M
    }
249
250
27.3M
    pub fn off_size_byte_range(&self) -> Range<usize> {
251
27.3M
        let start = self.count_byte_range().end;
252
27.3M
        let end = start + u8::RAW_BYTE_LEN;
253
27.3M
        start..end
254
27.3M
    }
255
256
27.3M
    pub fn offsets_byte_range(&self) -> Range<usize> {
257
27.3M
        let count = self.count();
258
27.3M
        let off_size = self.off_size();
259
27.3M
        let start = self.off_size_byte_range().end;
260
27.3M
        let end = start
261
27.3M
            + (transforms::add_multiply(count, 1_usize, off_size)).saturating_mul(u8::RAW_BYTE_LEN);
262
27.3M
        start..end
263
27.3M
    }
264
265
13.6M
    pub fn data_byte_range(&self) -> Range<usize> {
266
13.6M
        let start = self.offsets_byte_range().end;
267
13.6M
        let end = start
268
13.6M
            + self.offset_data().len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN;
269
13.6M
        start..end
270
13.6M
    }
271
}
272
273
const _: () = assert!(FontData::default_data_long_enough(Index::MIN_SIZE));
274
275
impl Default for Index<'_> {
276
0
    fn default() -> Self {
277
        Self {
278
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
279
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
280
        }
281
0
    }
282
}