Skip to main content

read_fonts/generated/
generated_kern.rs

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