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/generated_kern.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 OtKern<'a> {
9
0
    fn min_byte_range(&self) -> Range<usize> {
10
0
        0..self.subtable_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 OtKern<'_> {
19
    type Args = ();
20
}
21
22
impl<'a> FontRead<'a> for OtKern<'a> {
23
1.69k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
24
        #[allow(clippy::absurd_extreme_comparisons)]
25
1.69k
        if data.len() < Self::MIN_SIZE {
26
1
            return Err(ReadError::OutOfBounds);
27
1.69k
        }
28
1.69k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
29
0
            return Err(ReadError::OutOfBounds);
30
        };
31
1.69k
        Ok(Self { data })
32
1.69k
    }
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 OtKernFixedHeader {
40
    pub version: BigEndian<u16>,
41
    pub n_tables: BigEndian<u16>,
42
}
43
44
#[allow(clippy::absurd_extreme_comparisons)]
45
const _: () = assert!(std::mem::size_of::<OtKernFixedHeader>() <= OtKern::MIN_SIZE);
46
47
/// The OpenType [kerning](https://learn.microsoft.com/en-us/typography/opentype/spec/kern) table.
48
#[derive(Clone)]
49
pub struct OtKern<'a> {
50
    data: TableDataRef<'a, OtKernFixedHeader>,
51
}
52
53
#[allow(clippy::needless_lifetimes)]
54
impl<'a> OtKern<'a> {
55
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
56
    basic_table_impls!(impl_the_methods);
57
58
    /// Table version number—set to 0.
59
0
    pub fn version(&self) -> u16 {
60
0
        self.data.header().version.get()
61
0
    }
62
63
    /// Number of subtables in the kerning table.
64
9.58k
    pub fn n_tables(&self) -> u16 {
65
9.58k
        self.data.header().n_tables.get()
66
9.58k
    }
67
68
    /// Data for subtables, immediately following the header.
69
7.89k
    pub fn subtable_data(&self) -> &'a [u8] {
70
7.89k
        let range = self.subtable_data_byte_range();
71
7.89k
        self.offset_data()
72
7.89k
            .read_array(range)
73
7.89k
            .ok()
74
7.89k
            .unwrap_or_default()
75
7.89k
    }
76
77
7.89k
    pub fn version_byte_range(&self) -> Range<usize> {
78
7.89k
        let start = 0usize;
79
7.89k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
80
7.89k
        start..end
81
7.89k
    }
82
83
7.89k
    pub fn n_tables_byte_range(&self) -> Range<usize> {
84
7.89k
        let start = self.version_byte_range().end;
85
7.89k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
86
7.89k
        start..end
87
7.89k
    }
88
89
7.89k
    pub fn subtable_data_byte_range(&self) -> Range<usize> {
90
7.89k
        let start = self.n_tables_byte_range().end;
91
7.89k
        let end = start.saturating_add(
92
7.89k
            self.offset_data().len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN,
93
        );
94
7.89k
        start..end
95
7.89k
    }
96
}
97
98
const _: () = assert!(FontData::default_data_long_enough(OtKern::MIN_SIZE));
99
100
impl Default for OtKern<'_> {
101
0
    fn default() -> Self {
102
        Self {
103
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
104
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
105
        }
106
0
    }
107
}
108
109
impl<'a> MinByteRange<'a> for AatKern<'a> {
110
0
    fn min_byte_range(&self) -> Range<usize> {
111
0
        0..self.subtable_data_byte_range().end
112
0
    }
113
0
    fn min_table_bytes(&self) -> &'a [u8] {
114
0
        let range = self.min_byte_range();
115
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
116
0
    }
117
}
118
119
impl ReadArgs for AatKern<'_> {
120
    type Args = ();
121
}
122
123
impl<'a> FontRead<'a> for AatKern<'a> {
124
172
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
125
        #[allow(clippy::absurd_extreme_comparisons)]
126
172
        if data.len() < Self::MIN_SIZE {
127
1
            return Err(ReadError::OutOfBounds);
128
171
        }
129
171
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
130
0
            return Err(ReadError::OutOfBounds);
131
        };
132
171
        Ok(Self { data })
133
172
    }
134
}
135
136
/// The fields at the start of the table, which are read directly.
137
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
138
#[repr(C, packed)]
139
#[doc(hidden)]
140
pub struct AatKernFixedHeader {
141
    pub version: BigEndian<MajorMinor>,
142
    pub n_tables: BigEndian<u32>,
143
}
144
145
#[allow(clippy::absurd_extreme_comparisons)]
146
const _: () = assert!(std::mem::size_of::<AatKernFixedHeader>() <= AatKern::MIN_SIZE);
147
148
/// The Apple Advanced Typography [kerning](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6kern.html) table.
149
#[derive(Clone)]
150
pub struct AatKern<'a> {
151
    data: TableDataRef<'a, AatKernFixedHeader>,
152
}
153
154
#[allow(clippy::needless_lifetimes)]
155
impl<'a> AatKern<'a> {
156
    pub const MIN_SIZE: usize = (MajorMinor::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
157
    basic_table_impls!(impl_the_methods);
158
159
    /// The version number of the kerning table (0x00010000 for the current version).
160
0
    pub fn version(&self) -> MajorMinor {
161
0
        self.data.header().version.get()
162
0
    }
163
164
    /// The number of subtables included in the kerning table.
165
854
    pub fn n_tables(&self) -> u32 {
166
854
        self.data.header().n_tables.get()
167
854
    }
168
169
    /// Data for subtables, immediately following the header.    
170
683
    pub fn subtable_data(&self) -> &'a [u8] {
171
683
        let range = self.subtable_data_byte_range();
172
683
        self.offset_data()
173
683
            .read_array(range)
174
683
            .ok()
175
683
            .unwrap_or_default()
176
683
    }
177
178
683
    pub fn version_byte_range(&self) -> Range<usize> {
179
683
        let start = 0usize;
180
683
        let end = start.saturating_add(MajorMinor::RAW_BYTE_LEN);
181
683
        start..end
182
683
    }
183
184
683
    pub fn n_tables_byte_range(&self) -> Range<usize> {
185
683
        let start = self.version_byte_range().end;
186
683
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
187
683
        start..end
188
683
    }
189
190
683
    pub fn subtable_data_byte_range(&self) -> Range<usize> {
191
683
        let start = self.n_tables_byte_range().end;
192
683
        let end = start.saturating_add(
193
683
            self.offset_data().len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN,
194
        );
195
683
        start..end
196
683
    }
197
}
198
199
const _: () = assert!(FontData::default_data_long_enough(AatKern::MIN_SIZE));
200
201
impl Default for AatKern<'_> {
202
0
    fn default() -> Self {
203
        Self {
204
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
205
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
206
        }
207
0
    }
208
}
209
210
impl<'a> MinByteRange<'a> for OtSubtable<'a> {
211
0
    fn min_byte_range(&self) -> Range<usize> {
212
0
        0..self.data_byte_range().end
213
0
    }
214
0
    fn min_table_bytes(&self) -> &'a [u8] {
215
0
        let range = self.min_byte_range();
216
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
217
0
    }
218
}
219
220
impl ReadArgs for OtSubtable<'_> {
221
    type Args = ();
222
}
223
224
impl<'a> FontRead<'a> for OtSubtable<'a> {
225
17.5k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
226
        #[allow(clippy::absurd_extreme_comparisons)]
227
17.5k
        if data.len() < Self::MIN_SIZE {
228
1.42k
            return Err(ReadError::OutOfBounds);
229
16.1k
        }
230
16.1k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
231
0
            return Err(ReadError::OutOfBounds);
232
        };
233
16.1k
        Ok(Self { data })
234
17.5k
    }
235
}
236
237
/// The fields at the start of the table, which are read directly.
238
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
239
#[repr(C, packed)]
240
#[doc(hidden)]
241
pub struct OtSubtableFixedHeader {
242
    pub version: BigEndian<u16>,
243
    pub length: BigEndian<u16>,
244
    pub coverage: BigEndian<u16>,
245
}
246
247
#[allow(clippy::absurd_extreme_comparisons)]
248
const _: () = assert!(std::mem::size_of::<OtSubtableFixedHeader>() <= OtSubtable::MIN_SIZE);
249
250
/// A subtable in an OT `kern` table.
251
#[derive(Clone)]
252
pub struct OtSubtable<'a> {
253
    data: TableDataRef<'a, OtSubtableFixedHeader>,
254
}
255
256
#[allow(clippy::needless_lifetimes)]
257
impl<'a> OtSubtable<'a> {
258
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
259
    basic_table_impls!(impl_the_methods);
260
261
    /// Kern subtable version number-- set to 0.
262
0
    pub fn version(&self) -> u16 {
263
0
        self.data.header().version.get()
264
0
    }
265
266
    /// The length of this subtable in bytes, including this header.
267
0
    pub fn length(&self) -> u16 {
268
0
        self.data.header().length.get()
269
0
    }
270
271
    /// Circumstances under which this table is used.
272
25.3k
    pub fn coverage(&self) -> u16 {
273
25.3k
        self.data.header().coverage.get()
274
25.3k
    }
275
276
    /// Subtable specific data.
277
10.2k
    pub fn data(&self) -> &'a [u8] {
278
10.2k
        let range = self.data_byte_range();
279
10.2k
        self.offset_data()
280
10.2k
            .read_array(range)
281
10.2k
            .ok()
282
10.2k
            .unwrap_or_default()
283
10.2k
    }
284
285
10.2k
    pub fn version_byte_range(&self) -> Range<usize> {
286
10.2k
        let start = 0usize;
287
10.2k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
288
10.2k
        start..end
289
10.2k
    }
290
291
10.2k
    pub fn length_byte_range(&self) -> Range<usize> {
292
10.2k
        let start = self.version_byte_range().end;
293
10.2k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
294
10.2k
        start..end
295
10.2k
    }
296
297
10.2k
    pub fn coverage_byte_range(&self) -> Range<usize> {
298
10.2k
        let start = self.length_byte_range().end;
299
10.2k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
300
10.2k
        start..end
301
10.2k
    }
302
303
10.2k
    pub fn data_byte_range(&self) -> Range<usize> {
304
10.2k
        let start = self.coverage_byte_range().end;
305
10.2k
        let end = start.saturating_add(
306
10.2k
            self.offset_data().len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN,
307
        );
308
10.2k
        start..end
309
10.2k
    }
310
}
311
312
const _: () = assert!(FontData::default_data_long_enough(OtSubtable::MIN_SIZE));
313
314
impl Default for OtSubtable<'_> {
315
0
    fn default() -> Self {
316
        Self {
317
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
318
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
319
        }
320
0
    }
321
}
322
323
impl<'a> MinByteRange<'a> for AatSubtable<'a> {
324
0
    fn min_byte_range(&self) -> Range<usize> {
325
0
        0..self.data_byte_range().end
326
0
    }
327
0
    fn min_table_bytes(&self) -> &'a [u8] {
328
0
        let range = self.min_byte_range();
329
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
330
0
    }
331
}
332
333
impl ReadArgs for AatSubtable<'_> {
334
    type Args = ();
335
}
336
337
impl<'a> FontRead<'a> for AatSubtable<'a> {
338
1.01k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
339
        #[allow(clippy::absurd_extreme_comparisons)]
340
1.01k
        if data.len() < Self::MIN_SIZE {
341
112
            return Err(ReadError::OutOfBounds);
342
903
        }
343
903
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
344
0
            return Err(ReadError::OutOfBounds);
345
        };
346
903
        Ok(Self { data })
347
1.01k
    }
348
}
349
350
/// The fields at the start of the table, which are read directly.
351
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
352
#[repr(C, packed)]
353
#[doc(hidden)]
354
pub struct AatSubtableFixedHeader {
355
    pub length: BigEndian<u32>,
356
    pub coverage: BigEndian<u16>,
357
    pub tuple_index: BigEndian<u16>,
358
}
359
360
#[allow(clippy::absurd_extreme_comparisons)]
361
const _: () = assert!(std::mem::size_of::<AatSubtableFixedHeader>() <= AatSubtable::MIN_SIZE);
362
363
/// A subtable in an AAT `kern` table.
364
#[derive(Clone)]
365
pub struct AatSubtable<'a> {
366
    data: TableDataRef<'a, AatSubtableFixedHeader>,
367
}
368
369
#[allow(clippy::needless_lifetimes)]
370
impl<'a> AatSubtable<'a> {
371
    pub const MIN_SIZE: usize = (u32::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
372
    basic_table_impls!(impl_the_methods);
373
374
    /// The length of this subtable in bytes, including this header.
375
0
    pub fn length(&self) -> u32 {
376
0
        self.data.header().length.get()
377
0
    }
378
379
    /// Circumstances under which this table is used.
380
1.72k
    pub fn coverage(&self) -> u16 {
381
1.72k
        self.data.header().coverage.get()
382
1.72k
    }
383
384
    /// The tuple index (used for variations fonts). This value specifies which tuple this subtable covers.
385
0
    pub fn tuple_index(&self) -> u16 {
386
0
        self.data.header().tuple_index.get()
387
0
    }
388
389
    /// Subtable specific data.
390
616
    pub fn data(&self) -> &'a [u8] {
391
616
        let range = self.data_byte_range();
392
616
        self.offset_data()
393
616
            .read_array(range)
394
616
            .ok()
395
616
            .unwrap_or_default()
396
616
    }
397
398
616
    pub fn length_byte_range(&self) -> Range<usize> {
399
616
        let start = 0usize;
400
616
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
401
616
        start..end
402
616
    }
403
404
616
    pub fn coverage_byte_range(&self) -> Range<usize> {
405
616
        let start = self.length_byte_range().end;
406
616
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
407
616
        start..end
408
616
    }
409
410
616
    pub fn tuple_index_byte_range(&self) -> Range<usize> {
411
616
        let start = self.coverage_byte_range().end;
412
616
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
413
616
        start..end
414
616
    }
415
416
616
    pub fn data_byte_range(&self) -> Range<usize> {
417
616
        let start = self.tuple_index_byte_range().end;
418
616
        let end = start.saturating_add(
419
616
            self.offset_data().len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN,
420
        );
421
616
        start..end
422
616
    }
423
}
424
425
const _: () = assert!(FontData::default_data_long_enough(AatSubtable::MIN_SIZE));
426
427
impl Default for AatSubtable<'_> {
428
0
    fn default() -> Self {
429
        Self {
430
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
431
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
432
        }
433
0
    }
434
}
435
436
impl<'a> MinByteRange<'a> for Subtable0<'a> {
437
0
    fn min_byte_range(&self) -> Range<usize> {
438
0
        0..self.pairs_byte_range().end
439
0
    }
440
0
    fn min_table_bytes(&self) -> &'a [u8] {
441
0
        let range = self.min_byte_range();
442
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
443
0
    }
444
}
445
446
impl ReadArgs for Subtable0<'_> {
447
    type Args = ();
448
}
449
450
impl<'a> FontRead<'a> for Subtable0<'a> {
451
1.69k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
452
        #[allow(clippy::absurd_extreme_comparisons)]
453
1.69k
        if data.len() < Self::MIN_SIZE {
454
569
            return Err(ReadError::OutOfBounds);
455
1.12k
        }
456
1.12k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
457
0
            return Err(ReadError::OutOfBounds);
458
        };
459
1.12k
        Ok(Self { data })
460
1.69k
    }
461
}
462
463
/// The fields at the start of the table, which are read directly.
464
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
465
#[repr(C, packed)]
466
#[doc(hidden)]
467
pub struct Subtable0FixedHeader {
468
    pub n_pairs: BigEndian<u16>,
469
    pub search_range: BigEndian<u16>,
470
    pub entry_selector: BigEndian<u16>,
471
    pub range_shift: BigEndian<u16>,
472
}
473
474
#[allow(clippy::absurd_extreme_comparisons)]
475
const _: () = assert!(std::mem::size_of::<Subtable0FixedHeader>() <= Subtable0::MIN_SIZE);
476
477
/// The type 0 `kern` subtable.
478
#[derive(Clone)]
479
pub struct Subtable0<'a> {
480
    data: TableDataRef<'a, Subtable0FixedHeader>,
481
}
482
483
#[allow(clippy::needless_lifetimes)]
484
impl<'a> Subtable0<'a> {
485
    pub const MIN_SIZE: usize =
486
        (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
487
    basic_table_impls!(impl_the_methods);
488
489
    /// The number of kerning pairs in this subtable.
490
877k
    pub fn n_pairs(&self) -> u16 {
491
877k
        self.data.header().n_pairs.get()
492
877k
    }
493
494
    /// The largest power of two less than or equal to the value of nPairs, multiplied by the size in bytes of an entry in the subtable.
495
0
    pub fn search_range(&self) -> u16 {
496
0
        self.data.header().search_range.get()
497
0
    }
498
499
    /// This is calculated as log2 of the largest power of two less than or equal to the value of nPairs. This value indicates how many iterations of the search loop have to be made. For example, in a list of eight items, there would be three iterations of the loop.
500
0
    pub fn entry_selector(&self) -> u16 {
501
0
        self.data.header().entry_selector.get()
502
0
    }
503
504
    /// The value of nPairs minus the largest power of two less than or equal to nPairs. This is multiplied by the size in bytes of an entry in the table.
505
0
    pub fn range_shift(&self) -> u16 {
506
0
        self.data.header().range_shift.get()
507
0
    }
508
509
    /// Kerning records.
510
877k
    pub fn pairs(&self) -> &'a [Subtable0Pair] {
511
877k
        let range = self.pairs_byte_range();
512
877k
        self.offset_data()
513
877k
            .read_array(range)
514
877k
            .ok()
515
877k
            .unwrap_or_default()
516
877k
    }
517
518
877k
    pub fn n_pairs_byte_range(&self) -> Range<usize> {
519
877k
        let start = 0usize;
520
877k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
521
877k
        start..end
522
877k
    }
523
524
877k
    pub fn search_range_byte_range(&self) -> Range<usize> {
525
877k
        let start = self.n_pairs_byte_range().end;
526
877k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
527
877k
        start..end
528
877k
    }
529
530
877k
    pub fn entry_selector_byte_range(&self) -> Range<usize> {
531
877k
        let start = self.search_range_byte_range().end;
532
877k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
533
877k
        start..end
534
877k
    }
535
536
877k
    pub fn range_shift_byte_range(&self) -> Range<usize> {
537
877k
        let start = self.entry_selector_byte_range().end;
538
877k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
539
877k
        start..end
540
877k
    }
541
542
877k
    pub fn pairs_byte_range(&self) -> Range<usize> {
543
877k
        let n_pairs = self.n_pairs();
544
877k
        let start = self.range_shift_byte_range().end;
545
877k
        let end = start.saturating_add(
546
877k
            (transforms::to_usize(n_pairs)).saturating_mul(Subtable0Pair::RAW_BYTE_LEN),
547
        );
548
877k
        start..end
549
877k
    }
550
}
551
552
const _: () = assert!(FontData::default_data_long_enough(Subtable0::MIN_SIZE));
553
554
impl Default for Subtable0<'_> {
555
0
    fn default() -> Self {
556
        Self {
557
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
558
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
559
        }
560
0
    }
561
}
562
563
impl<'a> MinByteRange<'a> for Subtable2ClassTable<'a> {
564
0
    fn min_byte_range(&self) -> Range<usize> {
565
0
        0..self.offsets_byte_range().end
566
0
    }
567
0
    fn min_table_bytes(&self) -> &'a [u8] {
568
0
        let range = self.min_byte_range();
569
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
570
0
    }
571
}
572
573
impl ReadArgs for Subtable2ClassTable<'_> {
574
    type Args = ();
575
}
576
577
impl<'a> FontRead<'a> for Subtable2ClassTable<'a> {
578
392
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
579
        #[allow(clippy::absurd_extreme_comparisons)]
580
392
        if data.len() < Self::MIN_SIZE {
581
2
            return Err(ReadError::OutOfBounds);
582
390
        }
583
390
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
584
0
            return Err(ReadError::OutOfBounds);
585
        };
586
390
        Ok(Self { data })
587
392
    }
588
}
589
590
/// The fields at the start of the table, which are read directly.
591
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
592
#[repr(C, packed)]
593
#[doc(hidden)]
594
pub struct Subtable2ClassTableFixedHeader {
595
    pub first_glyph: BigEndian<GlyphId16>,
596
    pub n_glyphs: BigEndian<u16>,
597
}
598
599
#[allow(clippy::absurd_extreme_comparisons)]
600
const _: () =
601
    assert!(std::mem::size_of::<Subtable2ClassTableFixedHeader>() <= Subtable2ClassTable::MIN_SIZE);
602
603
/// Class table for the type 2 `kern` subtable.
604
#[derive(Clone)]
605
pub struct Subtable2ClassTable<'a> {
606
    data: TableDataRef<'a, Subtable2ClassTableFixedHeader>,
607
}
608
609
#[allow(clippy::needless_lifetimes)]
610
impl<'a> Subtable2ClassTable<'a> {
611
    pub const MIN_SIZE: usize = (GlyphId16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
612
    basic_table_impls!(impl_the_methods);
613
614
    /// First glyph in class range.
615
247k
    pub fn first_glyph(&self) -> GlyphId16 {
616
247k
        self.data.header().first_glyph.get()
617
247k
    }
618
619
    /// Number of glyph in class range.
620
247k
    pub fn n_glyphs(&self) -> u16 {
621
247k
        self.data.header().n_glyphs.get()
622
247k
    }
623
624
    /// The offsets array for all of the glyphs in the range.
625
247k
    pub fn offsets(&self) -> &'a [BigEndian<u16>] {
626
247k
        let range = self.offsets_byte_range();
627
247k
        self.offset_data()
628
247k
            .read_array(range)
629
247k
            .ok()
630
247k
            .unwrap_or_default()
631
247k
    }
632
633
247k
    pub fn first_glyph_byte_range(&self) -> Range<usize> {
634
247k
        let start = 0usize;
635
247k
        let end = start.saturating_add(GlyphId16::RAW_BYTE_LEN);
636
247k
        start..end
637
247k
    }
638
639
247k
    pub fn n_glyphs_byte_range(&self) -> Range<usize> {
640
247k
        let start = self.first_glyph_byte_range().end;
641
247k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
642
247k
        start..end
643
247k
    }
644
645
247k
    pub fn offsets_byte_range(&self) -> Range<usize> {
646
247k
        let n_glyphs = self.n_glyphs();
647
247k
        let start = self.n_glyphs_byte_range().end;
648
247k
        let end = start
649
247k
            .saturating_add((transforms::to_usize(n_glyphs)).saturating_mul(u16::RAW_BYTE_LEN));
650
247k
        start..end
651
247k
    }
652
}
653
654
const _: () = assert!(FontData::default_data_long_enough(
655
    Subtable2ClassTable::MIN_SIZE
656
));
657
658
impl Default for Subtable2ClassTable<'_> {
659
0
    fn default() -> Self {
660
        Self {
661
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
662
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
663
        }
664
0
    }
665
}
666
667
impl<'a> MinByteRange<'a> for Subtable3<'a> {
668
0
    fn min_byte_range(&self) -> Range<usize> {
669
0
        0..self.kern_index_byte_range().end
670
0
    }
671
0
    fn min_table_bytes(&self) -> &'a [u8] {
672
0
        let range = self.min_byte_range();
673
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
674
0
    }
675
}
676
677
impl ReadArgs for Subtable3<'_> {
678
    type Args = ();
679
}
680
681
impl<'a> FontRead<'a> for Subtable3<'a> {
682
568
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
683
        #[allow(clippy::absurd_extreme_comparisons)]
684
568
        if data.len() < Self::MIN_SIZE {
685
5
            return Err(ReadError::OutOfBounds);
686
563
        }
687
563
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
688
0
            return Err(ReadError::OutOfBounds);
689
        };
690
563
        Ok(Self { data })
691
568
    }
692
}
693
694
/// The fields at the start of the table, which are read directly.
695
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
696
#[repr(C, packed)]
697
#[doc(hidden)]
698
pub struct Subtable3FixedHeader {
699
    pub glyph_count: BigEndian<u16>,
700
    pub kern_value_count: u8,
701
    pub left_class_count: u8,
702
    pub right_class_count: u8,
703
    pub flags: u8,
704
}
705
706
#[allow(clippy::absurd_extreme_comparisons)]
707
const _: () = assert!(std::mem::size_of::<Subtable3FixedHeader>() <= Subtable3::MIN_SIZE);
708
709
/// The type 3 'kern' subtable.
710
#[derive(Clone)]
711
pub struct Subtable3<'a> {
712
    data: TableDataRef<'a, Subtable3FixedHeader>,
713
}
714
715
#[allow(clippy::needless_lifetimes)]
716
impl<'a> Subtable3<'a> {
717
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
718
        + u8::RAW_BYTE_LEN
719
        + u8::RAW_BYTE_LEN
720
        + u8::RAW_BYTE_LEN
721
        + u8::RAW_BYTE_LEN);
722
    basic_table_impls!(impl_the_methods);
723
724
    /// The number of glyphs in this font.
725
1.48M
    pub fn glyph_count(&self) -> u16 {
726
1.48M
        self.data.header().glyph_count.get()
727
1.48M
    }
728
729
    /// The number of kerning values.
730
1.46M
    pub fn kern_value_count(&self) -> u8 {
731
1.46M
        self.data.header().kern_value_count
732
1.46M
    }
733
734
    /// The number of left-hand classes.
735
15.4k
    pub fn left_class_count(&self) -> u8 {
736
15.4k
        self.data.header().left_class_count
737
15.4k
    }
738
739
    /// The number of right-hand classes.
740
30.8k
    pub fn right_class_count(&self) -> u8 {
741
30.8k
        self.data.header().right_class_count
742
30.8k
    }
743
744
    /// Set to zero (reserved for future use).
745
0
    pub fn flags(&self) -> u8 {
746
0
        self.data.header().flags
747
0
    }
748
749
    /// The kerning values.
750
12.9k
    pub fn kern_value(&self) -> &'a [BigEndian<i16>] {
751
12.9k
        let range = self.kern_value_byte_range();
752
12.9k
        self.offset_data()
753
12.9k
            .read_array(range)
754
12.9k
            .ok()
755
12.9k
            .unwrap_or_default()
756
12.9k
    }
757
758
    /// The left-hand classes.
759
1.40M
    pub fn left_class(&self) -> &'a [u8] {
760
1.40M
        let range = self.left_class_byte_range();
761
1.40M
        self.offset_data()
762
1.40M
            .read_array(range)
763
1.40M
            .ok()
764
1.40M
            .unwrap_or_default()
765
1.40M
    }
766
767
    /// The right-hand classes.
768
23.9k
    pub fn right_class(&self) -> &'a [u8] {
769
23.9k
        let range = self.right_class_byte_range();
770
23.9k
        self.offset_data()
771
23.9k
            .read_array(range)
772
23.9k
            .ok()
773
23.9k
            .unwrap_or_default()
774
23.9k
    }
775
776
    /// The indices into the kernValue array.
777
15.4k
    pub fn kern_index(&self) -> &'a [u8] {
778
15.4k
        let range = self.kern_index_byte_range();
779
15.4k
        self.offset_data()
780
15.4k
            .read_array(range)
781
15.4k
            .ok()
782
15.4k
            .unwrap_or_default()
783
15.4k
    }
784
785
1.46M
    pub fn glyph_count_byte_range(&self) -> Range<usize> {
786
1.46M
        let start = 0usize;
787
1.46M
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
788
1.46M
        start..end
789
1.46M
    }
790
791
1.46M
    pub fn kern_value_count_byte_range(&self) -> Range<usize> {
792
1.46M
        let start = self.glyph_count_byte_range().end;
793
1.46M
        let end = start.saturating_add(u8::RAW_BYTE_LEN);
794
1.46M
        start..end
795
1.46M
    }
796
797
1.46M
    pub fn left_class_count_byte_range(&self) -> Range<usize> {
798
1.46M
        let start = self.kern_value_count_byte_range().end;
799
1.46M
        let end = start.saturating_add(u8::RAW_BYTE_LEN);
800
1.46M
        start..end
801
1.46M
    }
802
803
1.46M
    pub fn right_class_count_byte_range(&self) -> Range<usize> {
804
1.46M
        let start = self.left_class_count_byte_range().end;
805
1.46M
        let end = start.saturating_add(u8::RAW_BYTE_LEN);
806
1.46M
        start..end
807
1.46M
    }
808
809
1.46M
    pub fn flags_byte_range(&self) -> Range<usize> {
810
1.46M
        let start = self.right_class_count_byte_range().end;
811
1.46M
        let end = start.saturating_add(u8::RAW_BYTE_LEN);
812
1.46M
        start..end
813
1.46M
    }
814
815
1.46M
    pub fn kern_value_byte_range(&self) -> Range<usize> {
816
1.46M
        let kern_value_count = self.kern_value_count();
817
1.46M
        let start = self.flags_byte_range().end;
818
1.46M
        let end = start.saturating_add(
819
1.46M
            (transforms::to_usize(kern_value_count)).saturating_mul(i16::RAW_BYTE_LEN),
820
        );
821
1.46M
        start..end
822
1.46M
    }
823
824
1.44M
    pub fn left_class_byte_range(&self) -> Range<usize> {
825
1.44M
        let glyph_count = self.glyph_count();
826
1.44M
        let start = self.kern_value_byte_range().end;
827
1.44M
        let end = start
828
1.44M
            .saturating_add((transforms::to_usize(glyph_count)).saturating_mul(u8::RAW_BYTE_LEN));
829
1.44M
        start..end
830
1.44M
    }
831
832
39.3k
    pub fn right_class_byte_range(&self) -> Range<usize> {
833
39.3k
        let glyph_count = self.glyph_count();
834
39.3k
        let start = self.left_class_byte_range().end;
835
39.3k
        let end = start
836
39.3k
            .saturating_add((transforms::to_usize(glyph_count)).saturating_mul(u8::RAW_BYTE_LEN));
837
39.3k
        start..end
838
39.3k
    }
839
840
15.4k
    pub fn kern_index_byte_range(&self) -> Range<usize> {
841
15.4k
        let left_class_count = self.left_class_count();
842
15.4k
        let right_class_count = self.right_class_count();
843
15.4k
        let start = self.right_class_byte_range().end;
844
15.4k
        let end = start.saturating_add(
845
15.4k
            (transforms::add_multiply(left_class_count, 0_usize, right_class_count))
846
15.4k
                .saturating_mul(u8::RAW_BYTE_LEN),
847
        );
848
15.4k
        start..end
849
15.4k
    }
850
}
851
852
const _: () = assert!(FontData::default_data_long_enough(Subtable3::MIN_SIZE));
853
854
impl Default for Subtable3<'_> {
855
0
    fn default() -> Self {
856
        Self {
857
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
858
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
859
        }
860
0
    }
861
}