Coverage Report

Created: 2026-11-11 07:51

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