Skip to main content

read_fonts/generated/
generated_kerx.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 Kerx<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.subtables_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 TopLevelTable for Kerx<'_> {
19    /// `kerx`
20    const TAG: Tag = Tag::new(b"kerx");
21}
22
23impl ReadArgs for Kerx<'_> {
24    type Args = ();
25}
26
27impl<'a> FontRead<'a> for Kerx<'a> {
28    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
29        #[allow(clippy::absurd_extreme_comparisons)]
30        if data.len() < Self::MIN_SIZE {
31            return Err(ReadError::OutOfBounds);
32        }
33        let Some(data) = TableDataRef::new(data.as_bytes()) else {
34            return Err(ReadError::OutOfBounds);
35        };
36        Ok(Self { data })
37    }
38}
39
40/// The fields at the start of the table, which are read directly.
41#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
42#[repr(C, packed)]
43#[doc(hidden)]
44pub struct KerxFixedHeader {
45    pub version: BigEndian<u16>,
46    pub padding: BigEndian<u16>,
47    pub n_tables: BigEndian<u32>,
48}
49
50#[allow(clippy::absurd_extreme_comparisons)]
51const _: () = assert!(std::mem::size_of::<KerxFixedHeader>() <= Kerx::MIN_SIZE);
52
53/// The [kerx (Extended Kerning)](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6morx.html) table.
54#[derive(Clone)]
55pub struct Kerx<'a> {
56    data: TableDataRef<'a, KerxFixedHeader>,
57}
58
59#[allow(clippy::needless_lifetimes)]
60impl<'a> Kerx<'a> {
61    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
62    basic_table_impls!(impl_the_methods);
63
64    /// The version number of the extended kerning table (currently 2, 3, or 4)
65    pub fn version(&self) -> u16 {
66        self.data.header().version.get()
67    }
68
69    /// The number of subtables included in the extended kerning table.
70    pub fn n_tables(&self) -> u32 {
71        self.data.header().n_tables.get()
72    }
73
74    pub fn subtables(&self) -> VarLenArray<'a, Subtable<'a>> {
75        let range = self.subtables_byte_range();
76        self.offset_data()
77            .split_off(range.start)
78            .and_then(|d| VarLenArray::read(d).ok())
79            .unwrap_or_default()
80    }
81
82    pub fn version_byte_range(&self) -> Range<usize> {
83        let start = 0;
84        let end = start + u16::RAW_BYTE_LEN;
85        start..end
86    }
87
88    pub fn padding_byte_range(&self) -> Range<usize> {
89        let start = self.version_byte_range().end;
90        let end = start + u16::RAW_BYTE_LEN;
91        start..end
92    }
93
94    pub fn n_tables_byte_range(&self) -> Range<usize> {
95        let start = self.padding_byte_range().end;
96        let end = start + u32::RAW_BYTE_LEN;
97        start..end
98    }
99
100    pub fn subtables_byte_range(&self) -> Range<usize> {
101        let n_tables = self.n_tables();
102        let start = self.n_tables_byte_range().end;
103        let end = start + {
104            let data = self.offset_data().split_off(start).unwrap_or_default();
105            <Subtable as VarSize>::total_len_for_count(data, transforms::to_usize(n_tables))
106                .unwrap_or(0)
107        };
108        start..end
109    }
110}
111
112const _: () = assert!(FontData::default_data_long_enough(Kerx::MIN_SIZE));
113
114impl Default for Kerx<'_> {
115    fn default() -> Self {
116        Self {
117            data: TableDataRef::new(FontData::default_table_data().as_bytes())
118                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
119        }
120    }
121}
122
123impl<'a> MinByteRange<'a> for Subtable<'a> {
124    fn min_byte_range(&self) -> Range<usize> {
125        0..self.data_byte_range().end
126    }
127    fn min_table_bytes(&self) -> &'a [u8] {
128        let range = self.min_byte_range();
129        self.offset_data().as_bytes().get(range).unwrap_or_default()
130    }
131}
132
133impl ReadArgs for Subtable<'_> {
134    type Args = ();
135}
136
137impl<'a> FontRead<'a> for Subtable<'a> {
138    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
139        #[allow(clippy::absurd_extreme_comparisons)]
140        if data.len() < Self::MIN_SIZE {
141            return Err(ReadError::OutOfBounds);
142        }
143        let Some(data) = TableDataRef::new(data.as_bytes()) else {
144            return Err(ReadError::OutOfBounds);
145        };
146        Ok(Self { data })
147    }
148}
149
150/// The fields at the start of the table, which are read directly.
151#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
152#[repr(C, packed)]
153#[doc(hidden)]
154pub struct SubtableFixedHeader {
155    pub length: BigEndian<u32>,
156    pub coverage: BigEndian<u32>,
157    pub tuple_count: BigEndian<u32>,
158}
159
160#[allow(clippy::absurd_extreme_comparisons)]
161const _: () = assert!(std::mem::size_of::<SubtableFixedHeader>() <= Subtable::MIN_SIZE);
162
163/// A subtable in a `kerx` table.
164#[derive(Clone)]
165pub struct Subtable<'a> {
166    data: TableDataRef<'a, SubtableFixedHeader>,
167}
168
169#[allow(clippy::needless_lifetimes)]
170impl<'a> Subtable<'a> {
171    pub const MIN_SIZE: usize = (u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
172    basic_table_impls!(impl_the_methods);
173
174    /// The length of this subtable in bytes, including this header.
175    pub fn length(&self) -> u32 {
176        self.data.header().length.get()
177    }
178
179    /// Circumstances under which this table is used.
180    pub fn coverage(&self) -> u32 {
181        self.data.header().coverage.get()
182    }
183
184    /// The tuple count. This value is only used with variation fonts and should be 0 for all other fonts. The subtable's tupleCount will be ignored if the 'kerx' table version is less than 4.
185    pub fn tuple_count(&self) -> u32 {
186        self.data.header().tuple_count.get()
187    }
188
189    /// Subtable specific data.
190    pub fn data(&self) -> &'a [u8] {
191        let range = self.data_byte_range();
192        self.offset_data()
193            .read_array(range)
194            .ok()
195            .unwrap_or_default()
196    }
197
198    pub fn length_byte_range(&self) -> Range<usize> {
199        let start = 0;
200        let end = start + u32::RAW_BYTE_LEN;
201        start..end
202    }
203
204    pub fn coverage_byte_range(&self) -> Range<usize> {
205        let start = self.length_byte_range().end;
206        let end = start + u32::RAW_BYTE_LEN;
207        start..end
208    }
209
210    pub fn tuple_count_byte_range(&self) -> Range<usize> {
211        let start = self.coverage_byte_range().end;
212        let end = start + u32::RAW_BYTE_LEN;
213        start..end
214    }
215
216    pub fn data_byte_range(&self) -> Range<usize> {
217        let start = self.tuple_count_byte_range().end;
218        let end = start
219            + self.offset_data().len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN;
220        start..end
221    }
222}
223
224const _: () = assert!(FontData::default_data_long_enough(Subtable::MIN_SIZE));
225
226impl Default for Subtable<'_> {
227    fn default() -> Self {
228        Self {
229            data: TableDataRef::new(FontData::default_table_data().as_bytes())
230                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
231        }
232    }
233}
234
235impl<'a> MinByteRange<'a> for Subtable0<'a> {
236    fn min_byte_range(&self) -> Range<usize> {
237        0..self.pairs_byte_range().end
238    }
239    fn min_table_bytes(&self) -> &'a [u8] {
240        let range = self.min_byte_range();
241        self.offset_data().as_bytes().get(range).unwrap_or_default()
242    }
243}
244
245impl ReadArgs for Subtable0<'_> {
246    type Args = ();
247}
248
249impl<'a> FontRead<'a> for Subtable0<'a> {
250    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
251        #[allow(clippy::absurd_extreme_comparisons)]
252        if data.len() < Self::MIN_SIZE {
253            return Err(ReadError::OutOfBounds);
254        }
255        let Some(data) = TableDataRef::new(data.as_bytes()) else {
256            return Err(ReadError::OutOfBounds);
257        };
258        Ok(Self { data })
259    }
260}
261
262/// The fields at the start of the table, which are read directly.
263#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
264#[repr(C, packed)]
265#[doc(hidden)]
266pub struct Subtable0FixedHeader {
267    pub n_pairs: BigEndian<u32>,
268    pub search_range: BigEndian<u32>,
269    pub entry_selector: BigEndian<u32>,
270    pub range_shift: BigEndian<u32>,
271}
272
273#[allow(clippy::absurd_extreme_comparisons)]
274const _: () = assert!(std::mem::size_of::<Subtable0FixedHeader>() <= Subtable0::MIN_SIZE);
275
276/// The type 0 `kerx` subtable.
277#[derive(Clone)]
278pub struct Subtable0<'a> {
279    data: TableDataRef<'a, Subtable0FixedHeader>,
280}
281
282#[allow(clippy::needless_lifetimes)]
283impl<'a> Subtable0<'a> {
284    pub const MIN_SIZE: usize =
285        (u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
286    basic_table_impls!(impl_the_methods);
287
288    /// The number of kerning pairs in this subtable.
289    pub fn n_pairs(&self) -> u32 {
290        self.data.header().n_pairs.get()
291    }
292
293    /// 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.
294    pub fn search_range(&self) -> u32 {
295        self.data.header().search_range.get()
296    }
297
298    /// 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.
299    pub fn entry_selector(&self) -> u32 {
300        self.data.header().entry_selector.get()
301    }
302
303    /// 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.
304    pub fn range_shift(&self) -> u32 {
305        self.data.header().range_shift.get()
306    }
307
308    /// Kerning records.
309    pub fn pairs(&self) -> &'a [Subtable0Pair] {
310        let range = self.pairs_byte_range();
311        self.offset_data()
312            .read_array(range)
313            .ok()
314            .unwrap_or_default()
315    }
316
317    pub fn n_pairs_byte_range(&self) -> Range<usize> {
318        let start = 0;
319        let end = start + u32::RAW_BYTE_LEN;
320        start..end
321    }
322
323    pub fn search_range_byte_range(&self) -> Range<usize> {
324        let start = self.n_pairs_byte_range().end;
325        let end = start + u32::RAW_BYTE_LEN;
326        start..end
327    }
328
329    pub fn entry_selector_byte_range(&self) -> Range<usize> {
330        let start = self.search_range_byte_range().end;
331        let end = start + u32::RAW_BYTE_LEN;
332        start..end
333    }
334
335    pub fn range_shift_byte_range(&self) -> Range<usize> {
336        let start = self.entry_selector_byte_range().end;
337        let end = start + u32::RAW_BYTE_LEN;
338        start..end
339    }
340
341    pub fn pairs_byte_range(&self) -> Range<usize> {
342        let n_pairs = self.n_pairs();
343        let start = self.range_shift_byte_range().end;
344        let end =
345            start + (transforms::to_usize(n_pairs)).saturating_mul(Subtable0Pair::RAW_BYTE_LEN);
346        start..end
347    }
348}
349
350const _: () = assert!(FontData::default_data_long_enough(Subtable0::MIN_SIZE));
351
352impl Default for Subtable0<'_> {
353    fn default() -> Self {
354        Self {
355            data: TableDataRef::new(FontData::default_table_data().as_bytes())
356                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
357        }
358    }
359}
360
361/// The type 0 `kerx` subtable kerning record.
362#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
363#[repr(C)]
364#[repr(packed)]
365pub struct Subtable0Pair {
366    /// The glyph index for the lefthand glyph in the kerning pair.
367    pub left: BigEndian<GlyphId16>,
368    /// The glyph index for the righthand glyph in the kerning pair.
369    pub right: BigEndian<GlyphId16>,
370    /// Kerning value.
371    pub value: BigEndian<i16>,
372}
373
374impl Subtable0Pair {
375    /// The glyph index for the lefthand glyph in the kerning pair.
376    pub fn left(&self) -> GlyphId16 {
377        self.left.get()
378    }
379
380    /// The glyph index for the righthand glyph in the kerning pair.
381    pub fn right(&self) -> GlyphId16 {
382        self.right.get()
383    }
384
385    /// Kerning value.
386    pub fn value(&self) -> i16 {
387        self.value.get()
388    }
389}
390
391impl FixedSize for Subtable0Pair {
392    const RAW_BYTE_LEN: usize =
393        GlyphId16::RAW_BYTE_LEN + GlyphId16::RAW_BYTE_LEN + i16::RAW_BYTE_LEN;
394}