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_aat.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
/// Lookup tables provide a way of looking up information about a glyph index.
9
/// The different cmap subtable formats.
10
#[derive(Clone)]
11
pub enum Lookup<'a> {
12
    Format0(Lookup0<'a>),
13
    Format2(Lookup2<'a>),
14
    Format4(Lookup4<'a>),
15
    Format6(Lookup6<'a>),
16
    Format8(Lookup8<'a>),
17
    Format10(Lookup10<'a>),
18
}
19
20
impl Default for Lookup<'_> {
21
0
    fn default() -> Self {
22
0
        Self::Format0(Default::default())
23
0
    }
24
}
25
26
impl<'a> Lookup<'a> {
27
    ///Return the `FontData` used to resolve offsets for this table.
28
0
    pub fn offset_data(&self) -> FontData<'a> {
29
0
        match self {
30
0
            Self::Format0(item) => item.offset_data(),
31
0
            Self::Format2(item) => item.offset_data(),
32
0
            Self::Format4(item) => item.offset_data(),
33
0
            Self::Format6(item) => item.offset_data(),
34
0
            Self::Format8(item) => item.offset_data(),
35
0
            Self::Format10(item) => item.offset_data(),
36
        }
37
0
    }
38
39
    /// Format number is set to 0.
40
0
    pub fn format(&self) -> u16 {
41
0
        match self {
42
0
            Self::Format0(item) => item.format(),
43
0
            Self::Format2(item) => item.format(),
44
0
            Self::Format4(item) => item.format(),
45
0
            Self::Format6(item) => item.format(),
46
0
            Self::Format8(item) => item.format(),
47
0
            Self::Format10(item) => item.format(),
48
        }
49
0
    }
50
}
51
52
impl ReadArgs for Lookup<'_> {
53
    type Args = ();
54
}
55
56
impl<'a> FontRead<'a> for Lookup<'a> {
57
    #[inline]
58
757k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
59
757k
        let format: u16 = data.read_at(0usize)?;
60
710k
        match format {
61
89.4k
            Lookup0::FORMAT => Ok(Self::Format0(FontRead::read(data)?)),
62
123k
            Lookup2::FORMAT => Ok(Self::Format2(FontRead::read(data)?)),
63
207k
            Lookup4::FORMAT => Ok(Self::Format4(FontRead::read(data)?)),
64
93.2k
            Lookup6::FORMAT => Ok(Self::Format6(FontRead::read(data)?)),
65
89.1k
            Lookup8::FORMAT => Ok(Self::Format8(FontRead::read(data)?)),
66
62.2k
            Lookup10::FORMAT => Ok(Self::Format10(FontRead::read(data)?)),
67
46.5k
            other => Err(ReadError::InvalidFormat(other.into())),
68
        }
69
757k
    }
70
}
71
72
impl<'a> MinByteRange<'a> for Lookup<'a> {
73
0
    fn min_byte_range(&self) -> Range<usize> {
74
0
        match self {
75
0
            Self::Format0(item) => item.min_byte_range(),
76
0
            Self::Format2(item) => item.min_byte_range(),
77
0
            Self::Format4(item) => item.min_byte_range(),
78
0
            Self::Format6(item) => item.min_byte_range(),
79
0
            Self::Format8(item) => item.min_byte_range(),
80
0
            Self::Format10(item) => item.min_byte_range(),
81
        }
82
0
    }
83
0
    fn min_table_bytes(&self) -> &'a [u8] {
84
0
        match self {
85
0
            Self::Format0(item) => item.min_table_bytes(),
86
0
            Self::Format2(item) => item.min_table_bytes(),
87
0
            Self::Format4(item) => item.min_table_bytes(),
88
0
            Self::Format6(item) => item.min_table_bytes(),
89
0
            Self::Format8(item) => item.min_table_bytes(),
90
0
            Self::Format10(item) => item.min_table_bytes(),
91
        }
92
0
    }
93
}
94
95
impl Format<u16> for Lookup0<'_> {
96
    const FORMAT: u16 = 0;
97
}
98
99
impl<'a> MinByteRange<'a> for Lookup0<'a> {
100
0
    fn min_byte_range(&self) -> Range<usize> {
101
0
        0..self.values_data_byte_range().end
102
0
    }
103
0
    fn min_table_bytes(&self) -> &'a [u8] {
104
0
        let range = self.min_byte_range();
105
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
106
0
    }
107
}
108
109
impl ReadArgs for Lookup0<'_> {
110
    type Args = ();
111
}
112
113
impl<'a> FontRead<'a> for Lookup0<'a> {
114
89.4k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
115
        #[allow(clippy::absurd_extreme_comparisons)]
116
89.4k
        if data.len() < Self::MIN_SIZE {
117
0
            return Err(ReadError::OutOfBounds);
118
89.4k
        }
119
89.4k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
120
0
            return Err(ReadError::OutOfBounds);
121
        };
122
89.4k
        Ok(Self { data })
123
89.4k
    }
124
}
125
126
/// The fields at the start of the table, which are read directly.
127
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
128
#[repr(C, packed)]
129
#[doc(hidden)]
130
pub struct Lookup0FixedHeader {
131
    pub format: BigEndian<u16>,
132
}
133
134
#[allow(clippy::absurd_extreme_comparisons)]
135
const _: () = assert!(std::mem::size_of::<Lookup0FixedHeader>() <= Lookup0::MIN_SIZE);
136
137
/// Simple array format. The lookup data is an array of lookup values, indexed
138
/// by glyph index.
139
#[derive(Clone)]
140
pub struct Lookup0<'a> {
141
    data: TableDataRef<'a, Lookup0FixedHeader>,
142
}
143
144
#[allow(clippy::needless_lifetimes)]
145
impl<'a> Lookup0<'a> {
146
    pub const MIN_SIZE: usize = u16::RAW_BYTE_LEN;
147
    basic_table_impls!(impl_the_methods);
148
149
    /// Format number is set to 0.
150
0
    pub fn format(&self) -> u16 {
151
0
        self.data.header().format.get()
152
0
    }
153
154
    /// Values, indexed by glyph index.
155
114k
    pub fn values_data(&self) -> &'a [u8] {
156
114k
        let range = self.values_data_byte_range();
157
114k
        self.offset_data()
158
114k
            .read_array(range)
159
114k
            .ok()
160
114k
            .unwrap_or_default()
161
114k
    }
162
163
114k
    pub fn format_byte_range(&self) -> Range<usize> {
164
114k
        let start = 0usize;
165
114k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
166
114k
        start..end
167
114k
    }
168
169
114k
    pub fn values_data_byte_range(&self) -> Range<usize> {
170
114k
        let start = self.format_byte_range().end;
171
114k
        let end = start.saturating_add(
172
114k
            self.offset_data().len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN,
173
        );
174
114k
        start..end
175
114k
    }
176
}
177
178
const _: () = assert!(FontData::default_data_long_enough(Lookup0::MIN_SIZE));
179
180
impl Default for Lookup0<'_> {
181
0
    fn default() -> Self {
182
        Self {
183
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
184
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
185
        }
186
0
    }
187
}
188
189
impl Format<u16> for Lookup2<'_> {
190
    const FORMAT: u16 = 2;
191
}
192
193
impl<'a> MinByteRange<'a> for Lookup2<'a> {
194
0
    fn min_byte_range(&self) -> Range<usize> {
195
0
        0..self.segments_data_byte_range().end
196
0
    }
197
0
    fn min_table_bytes(&self) -> &'a [u8] {
198
0
        let range = self.min_byte_range();
199
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
200
0
    }
201
}
202
203
impl ReadArgs for Lookup2<'_> {
204
    type Args = ();
205
}
206
207
impl<'a> FontRead<'a> for Lookup2<'a> {
208
123k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
209
        #[allow(clippy::absurd_extreme_comparisons)]
210
123k
        if data.len() < Self::MIN_SIZE {
211
99
            return Err(ReadError::OutOfBounds);
212
122k
        }
213
122k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
214
0
            return Err(ReadError::OutOfBounds);
215
        };
216
122k
        Ok(Self { data })
217
123k
    }
218
}
219
220
/// The fields at the start of the table, which are read directly.
221
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
222
#[repr(C, packed)]
223
#[doc(hidden)]
224
pub struct Lookup2FixedHeader {
225
    pub format: BigEndian<u16>,
226
    pub unit_size: BigEndian<u16>,
227
    pub n_units: BigEndian<u16>,
228
    pub search_range: BigEndian<u16>,
229
    pub entry_selector: BigEndian<u16>,
230
    pub range_shift: BigEndian<u16>,
231
}
232
233
#[allow(clippy::absurd_extreme_comparisons)]
234
const _: () = assert!(std::mem::size_of::<Lookup2FixedHeader>() <= Lookup2::MIN_SIZE);
235
236
/// Segment single format. Each non-overlapping segment has a single lookup
237
/// value that applies to all glyphs in the segment. A segment is defined as
238
/// a contiguous range of glyph indexes.
239
#[derive(Clone)]
240
pub struct Lookup2<'a> {
241
    data: TableDataRef<'a, Lookup2FixedHeader>,
242
}
243
244
#[allow(clippy::needless_lifetimes)]
245
impl<'a> Lookup2<'a> {
246
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
247
        + u16::RAW_BYTE_LEN
248
        + u16::RAW_BYTE_LEN
249
        + u16::RAW_BYTE_LEN
250
        + u16::RAW_BYTE_LEN
251
        + u16::RAW_BYTE_LEN);
252
    basic_table_impls!(impl_the_methods);
253
254
    /// Format number is set to 2.
255
0
    pub fn format(&self) -> u16 {
256
0
        self.data.header().format.get()
257
0
    }
258
259
    /// Size of a lookup unit for this search in bytes.
260
130k
    pub fn unit_size(&self) -> u16 {
261
130k
        self.data.header().unit_size.get()
262
130k
    }
263
264
    /// Number of units of the preceding size to be searched.
265
260k
    pub fn n_units(&self) -> u16 {
266
260k
        self.data.header().n_units.get()
267
260k
    }
268
269
    /// The value of unitSize times the largest power of 2 that is less than or equal to the value of nUnits.
270
0
    pub fn search_range(&self) -> u16 {
271
0
        self.data.header().search_range.get()
272
0
    }
273
274
    /// The log base 2 of the largest power of 2 less than or equal to the value of nUnits.
275
0
    pub fn entry_selector(&self) -> u16 {
276
0
        self.data.header().entry_selector.get()
277
0
    }
278
279
    /// The value of unitSize times the difference of the value of nUnits minus the largest power of 2 less than or equal to the value of nUnits.
280
0
    pub fn range_shift(&self) -> u16 {
281
0
        self.data.header().range_shift.get()
282
0
    }
283
284
    /// Segments.
285
130k
    pub fn segments_data(&self) -> &'a [u8] {
286
130k
        let range = self.segments_data_byte_range();
287
130k
        self.offset_data()
288
130k
            .read_array(range)
289
130k
            .ok()
290
130k
            .unwrap_or_default()
291
130k
    }
292
293
130k
    pub fn format_byte_range(&self) -> Range<usize> {
294
130k
        let start = 0usize;
295
130k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
296
130k
        start..end
297
130k
    }
298
299
130k
    pub fn unit_size_byte_range(&self) -> Range<usize> {
300
130k
        let start = self.format_byte_range().end;
301
130k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
302
130k
        start..end
303
130k
    }
304
305
130k
    pub fn n_units_byte_range(&self) -> Range<usize> {
306
130k
        let start = self.unit_size_byte_range().end;
307
130k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
308
130k
        start..end
309
130k
    }
310
311
130k
    pub fn search_range_byte_range(&self) -> Range<usize> {
312
130k
        let start = self.n_units_byte_range().end;
313
130k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
314
130k
        start..end
315
130k
    }
316
317
130k
    pub fn entry_selector_byte_range(&self) -> Range<usize> {
318
130k
        let start = self.search_range_byte_range().end;
319
130k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
320
130k
        start..end
321
130k
    }
322
323
130k
    pub fn range_shift_byte_range(&self) -> Range<usize> {
324
130k
        let start = self.entry_selector_byte_range().end;
325
130k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
326
130k
        start..end
327
130k
    }
328
329
130k
    pub fn segments_data_byte_range(&self) -> Range<usize> {
330
130k
        let unit_size = self.unit_size();
331
130k
        let n_units = self.n_units();
332
130k
        let start = self.range_shift_byte_range().end;
333
130k
        let end = start.saturating_add(
334
130k
            (transforms::add_multiply(unit_size, 0_usize, n_units))
335
130k
                .saturating_mul(u8::RAW_BYTE_LEN),
336
        );
337
130k
        start..end
338
130k
    }
339
}
340
341
impl Format<u16> for Lookup4<'_> {
342
    const FORMAT: u16 = 4;
343
}
344
345
impl<'a> MinByteRange<'a> for Lookup4<'a> {
346
0
    fn min_byte_range(&self) -> Range<usize> {
347
0
        0..self.segments_byte_range().end
348
0
    }
349
0
    fn min_table_bytes(&self) -> &'a [u8] {
350
0
        let range = self.min_byte_range();
351
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
352
0
    }
353
}
354
355
impl ReadArgs for Lookup4<'_> {
356
    type Args = ();
357
}
358
359
impl<'a> FontRead<'a> for Lookup4<'a> {
360
207k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
361
        #[allow(clippy::absurd_extreme_comparisons)]
362
207k
        if data.len() < Self::MIN_SIZE {
363
103
            return Err(ReadError::OutOfBounds);
364
206k
        }
365
206k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
366
0
            return Err(ReadError::OutOfBounds);
367
        };
368
206k
        Ok(Self { data })
369
207k
    }
370
}
371
372
/// The fields at the start of the table, which are read directly.
373
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
374
#[repr(C, packed)]
375
#[doc(hidden)]
376
pub struct Lookup4FixedHeader {
377
    pub format: BigEndian<u16>,
378
    pub unit_size: BigEndian<u16>,
379
    pub n_units: BigEndian<u16>,
380
    pub search_range: BigEndian<u16>,
381
    pub entry_selector: BigEndian<u16>,
382
    pub range_shift: BigEndian<u16>,
383
}
384
385
#[allow(clippy::absurd_extreme_comparisons)]
386
const _: () = assert!(std::mem::size_of::<Lookup4FixedHeader>() <= Lookup4::MIN_SIZE);
387
388
/// Segment array format. A segment mapping is performed (as with Format 2),
389
/// but instead of a single lookup value for all the glyphs in the segment,
390
/// each glyph in the segment gets its own separate lookup value.
391
#[derive(Clone)]
392
pub struct Lookup4<'a> {
393
    data: TableDataRef<'a, Lookup4FixedHeader>,
394
}
395
396
#[allow(clippy::needless_lifetimes)]
397
impl<'a> Lookup4<'a> {
398
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
399
        + u16::RAW_BYTE_LEN
400
        + u16::RAW_BYTE_LEN
401
        + u16::RAW_BYTE_LEN
402
        + u16::RAW_BYTE_LEN
403
        + u16::RAW_BYTE_LEN);
404
    basic_table_impls!(impl_the_methods);
405
406
    /// Format number is set to 4.
407
0
    pub fn format(&self) -> u16 {
408
0
        self.data.header().format.get()
409
0
    }
410
411
    /// Size of a lookup unit for this search in bytes.
412
0
    pub fn unit_size(&self) -> u16 {
413
0
        self.data.header().unit_size.get()
414
0
    }
415
416
    /// Number of units of the preceding size to be searched.
417
4.59M
    pub fn n_units(&self) -> u16 {
418
4.59M
        self.data.header().n_units.get()
419
4.59M
    }
420
421
    /// The value of unitSize times the largest power of 2 that is less than or equal to the value of nUnits.
422
0
    pub fn search_range(&self) -> u16 {
423
0
        self.data.header().search_range.get()
424
0
    }
425
426
    /// The log base 2 of the largest power of 2 less than or equal to the value of nUnits.
427
0
    pub fn entry_selector(&self) -> u16 {
428
0
        self.data.header().entry_selector.get()
429
0
    }
430
431
    /// The value of unitSize times the difference of the value of nUnits minus the largest power of 2 less than or equal to the value of nUnits.
432
0
    pub fn range_shift(&self) -> u16 {
433
0
        self.data.header().range_shift.get()
434
0
    }
435
436
    /// Segments.
437
4.59M
    pub fn segments(&self) -> &'a [LookupSegment4] {
438
4.59M
        let range = self.segments_byte_range();
439
4.59M
        self.offset_data()
440
4.59M
            .read_array(range)
441
4.59M
            .ok()
442
4.59M
            .unwrap_or_default()
443
4.59M
    }
444
445
4.59M
    pub fn format_byte_range(&self) -> Range<usize> {
446
4.59M
        let start = 0usize;
447
4.59M
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
448
4.59M
        start..end
449
4.59M
    }
450
451
4.59M
    pub fn unit_size_byte_range(&self) -> Range<usize> {
452
4.59M
        let start = self.format_byte_range().end;
453
4.59M
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
454
4.59M
        start..end
455
4.59M
    }
456
457
4.59M
    pub fn n_units_byte_range(&self) -> Range<usize> {
458
4.59M
        let start = self.unit_size_byte_range().end;
459
4.59M
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
460
4.59M
        start..end
461
4.59M
    }
462
463
4.59M
    pub fn search_range_byte_range(&self) -> Range<usize> {
464
4.59M
        let start = self.n_units_byte_range().end;
465
4.59M
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
466
4.59M
        start..end
467
4.59M
    }
468
469
4.59M
    pub fn entry_selector_byte_range(&self) -> Range<usize> {
470
4.59M
        let start = self.search_range_byte_range().end;
471
4.59M
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
472
4.59M
        start..end
473
4.59M
    }
474
475
4.59M
    pub fn range_shift_byte_range(&self) -> Range<usize> {
476
4.59M
        let start = self.entry_selector_byte_range().end;
477
4.59M
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
478
4.59M
        start..end
479
4.59M
    }
480
481
4.59M
    pub fn segments_byte_range(&self) -> Range<usize> {
482
4.59M
        let n_units = self.n_units();
483
4.59M
        let start = self.range_shift_byte_range().end;
484
4.59M
        let end = start.saturating_add(
485
4.59M
            (transforms::to_usize(n_units)).saturating_mul(LookupSegment4::RAW_BYTE_LEN),
486
        );
487
4.59M
        start..end
488
4.59M
    }
489
}
490
491
/// Lookup segment for format 4.
492
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
493
#[repr(C)]
494
#[repr(packed)]
495
pub struct LookupSegment4 {
496
    /// Last glyph index in this segment.
497
    pub last_glyph: BigEndian<u16>,
498
    /// First glyph index in this segment.
499
    pub first_glyph: BigEndian<u16>,
500
    /// A 16-bit offset from the start of the table to the data.
501
    pub value_offset: BigEndian<u16>,
502
}
503
504
impl LookupSegment4 {
505
    /// Last glyph index in this segment.
506
0
    pub fn last_glyph(&self) -> u16 {
507
0
        self.last_glyph.get()
508
0
    }
509
510
    /// First glyph index in this segment.
511
4.43M
    pub fn first_glyph(&self) -> u16 {
512
4.43M
        self.first_glyph.get()
513
4.43M
    }
514
515
    /// A 16-bit offset from the start of the table to the data.
516
4.43M
    pub fn value_offset(&self) -> u16 {
517
4.43M
        self.value_offset.get()
518
4.43M
    }
519
}
520
521
impl FixedSize for LookupSegment4 {
522
    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
523
}
524
525
impl Format<u16> for Lookup6<'_> {
526
    const FORMAT: u16 = 6;
527
}
528
529
impl<'a> MinByteRange<'a> for Lookup6<'a> {
530
0
    fn min_byte_range(&self) -> Range<usize> {
531
0
        0..self.entries_data_byte_range().end
532
0
    }
533
0
    fn min_table_bytes(&self) -> &'a [u8] {
534
0
        let range = self.min_byte_range();
535
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
536
0
    }
537
}
538
539
impl ReadArgs for Lookup6<'_> {
540
    type Args = ();
541
}
542
543
impl<'a> FontRead<'a> for Lookup6<'a> {
544
93.2k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
545
        #[allow(clippy::absurd_extreme_comparisons)]
546
93.2k
        if data.len() < Self::MIN_SIZE {
547
46.4k
            return Err(ReadError::OutOfBounds);
548
46.8k
        }
549
46.8k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
550
0
            return Err(ReadError::OutOfBounds);
551
        };
552
46.8k
        Ok(Self { data })
553
93.2k
    }
554
}
555
556
/// The fields at the start of the table, which are read directly.
557
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
558
#[repr(C, packed)]
559
#[doc(hidden)]
560
pub struct Lookup6FixedHeader {
561
    pub format: BigEndian<u16>,
562
    pub unit_size: BigEndian<u16>,
563
    pub n_units: BigEndian<u16>,
564
    pub search_range: BigEndian<u16>,
565
    pub entry_selector: BigEndian<u16>,
566
    pub range_shift: BigEndian<u16>,
567
}
568
569
#[allow(clippy::absurd_extreme_comparisons)]
570
const _: () = assert!(std::mem::size_of::<Lookup6FixedHeader>() <= Lookup6::MIN_SIZE);
571
572
/// Single table format. The lookup data is a sorted list of
573
/// <glyph index,lookup value> pairs.
574
#[derive(Clone)]
575
pub struct Lookup6<'a> {
576
    data: TableDataRef<'a, Lookup6FixedHeader>,
577
}
578
579
#[allow(clippy::needless_lifetimes)]
580
impl<'a> Lookup6<'a> {
581
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
582
        + u16::RAW_BYTE_LEN
583
        + u16::RAW_BYTE_LEN
584
        + u16::RAW_BYTE_LEN
585
        + u16::RAW_BYTE_LEN
586
        + u16::RAW_BYTE_LEN);
587
    basic_table_impls!(impl_the_methods);
588
589
    /// Format number is set to 6.
590
0
    pub fn format(&self) -> u16 {
591
0
        self.data.header().format.get()
592
0
    }
593
594
    /// Size of a lookup unit for this search in bytes.
595
52.7k
    pub fn unit_size(&self) -> u16 {
596
52.7k
        self.data.header().unit_size.get()
597
52.7k
    }
598
599
    /// Number of units of the preceding size to be searched.
600
105k
    pub fn n_units(&self) -> u16 {
601
105k
        self.data.header().n_units.get()
602
105k
    }
603
604
    /// The value of unitSize times the largest power of 2 that is less than or equal to the value of nUnits.
605
0
    pub fn search_range(&self) -> u16 {
606
0
        self.data.header().search_range.get()
607
0
    }
608
609
    /// The log base 2 of the largest power of 2 less than or equal to the value of nUnits.
610
0
    pub fn entry_selector(&self) -> u16 {
611
0
        self.data.header().entry_selector.get()
612
0
    }
613
614
    /// The value of unitSize times the difference of the value of nUnits minus the largest power of 2 less than or equal to the value of nUnits.
615
0
    pub fn range_shift(&self) -> u16 {
616
0
        self.data.header().range_shift.get()
617
0
    }
618
619
    /// Values, indexed by glyph index.
620
52.7k
    pub fn entries_data(&self) -> &'a [u8] {
621
52.7k
        let range = self.entries_data_byte_range();
622
52.7k
        self.offset_data()
623
52.7k
            .read_array(range)
624
52.7k
            .ok()
625
52.7k
            .unwrap_or_default()
626
52.7k
    }
627
628
52.7k
    pub fn format_byte_range(&self) -> Range<usize> {
629
52.7k
        let start = 0usize;
630
52.7k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
631
52.7k
        start..end
632
52.7k
    }
633
634
52.7k
    pub fn unit_size_byte_range(&self) -> Range<usize> {
635
52.7k
        let start = self.format_byte_range().end;
636
52.7k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
637
52.7k
        start..end
638
52.7k
    }
639
640
52.7k
    pub fn n_units_byte_range(&self) -> Range<usize> {
641
52.7k
        let start = self.unit_size_byte_range().end;
642
52.7k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
643
52.7k
        start..end
644
52.7k
    }
645
646
52.7k
    pub fn search_range_byte_range(&self) -> Range<usize> {
647
52.7k
        let start = self.n_units_byte_range().end;
648
52.7k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
649
52.7k
        start..end
650
52.7k
    }
651
652
52.7k
    pub fn entry_selector_byte_range(&self) -> Range<usize> {
653
52.7k
        let start = self.search_range_byte_range().end;
654
52.7k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
655
52.7k
        start..end
656
52.7k
    }
657
658
52.7k
    pub fn range_shift_byte_range(&self) -> Range<usize> {
659
52.7k
        let start = self.entry_selector_byte_range().end;
660
52.7k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
661
52.7k
        start..end
662
52.7k
    }
663
664
52.7k
    pub fn entries_data_byte_range(&self) -> Range<usize> {
665
52.7k
        let unit_size = self.unit_size();
666
52.7k
        let n_units = self.n_units();
667
52.7k
        let start = self.range_shift_byte_range().end;
668
52.7k
        let end = start.saturating_add(
669
52.7k
            (transforms::add_multiply(unit_size, 0_usize, n_units))
670
52.7k
                .saturating_mul(u8::RAW_BYTE_LEN),
671
        );
672
52.7k
        start..end
673
52.7k
    }
674
}
675
676
impl Format<u16> for Lookup8<'_> {
677
    const FORMAT: u16 = 8;
678
}
679
680
impl<'a> MinByteRange<'a> for Lookup8<'a> {
681
0
    fn min_byte_range(&self) -> Range<usize> {
682
0
        0..self.value_array_byte_range().end
683
0
    }
684
0
    fn min_table_bytes(&self) -> &'a [u8] {
685
0
        let range = self.min_byte_range();
686
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
687
0
    }
688
}
689
690
impl ReadArgs for Lookup8<'_> {
691
    type Args = ();
692
}
693
694
impl<'a> FontRead<'a> for Lookup8<'a> {
695
89.1k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
696
        #[allow(clippy::absurd_extreme_comparisons)]
697
89.1k
        if data.len() < Self::MIN_SIZE {
698
31.3k
            return Err(ReadError::OutOfBounds);
699
57.8k
        }
700
57.8k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
701
0
            return Err(ReadError::OutOfBounds);
702
        };
703
57.8k
        Ok(Self { data })
704
89.1k
    }
705
}
706
707
/// The fields at the start of the table, which are read directly.
708
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
709
#[repr(C, packed)]
710
#[doc(hidden)]
711
pub struct Lookup8FixedHeader {
712
    pub format: BigEndian<u16>,
713
    pub first_glyph: BigEndian<u16>,
714
    pub glyph_count: BigEndian<u16>,
715
}
716
717
#[allow(clippy::absurd_extreme_comparisons)]
718
const _: () = assert!(std::mem::size_of::<Lookup8FixedHeader>() <= Lookup8::MIN_SIZE);
719
720
/// Trimmed array format. The lookup data is a simple trimmed array
721
/// indexed by glyph index.
722
#[derive(Clone)]
723
pub struct Lookup8<'a> {
724
    data: TableDataRef<'a, Lookup8FixedHeader>,
725
}
726
727
#[allow(clippy::needless_lifetimes)]
728
impl<'a> Lookup8<'a> {
729
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
730
    basic_table_impls!(impl_the_methods);
731
732
    /// Format number is set to 8.
733
0
    pub fn format(&self) -> u16 {
734
0
        self.data.header().format.get()
735
0
    }
736
737
    /// First glyph index included in the trimmed array.
738
615k
    pub fn first_glyph(&self) -> u16 {
739
615k
        self.data.header().first_glyph.get()
740
615k
    }
741
742
    /// Total number of glyphs (equivalent to the last glyph minus the value
743
    /// of firstGlyph plus 1).
744
561k
    pub fn glyph_count(&self) -> u16 {
745
561k
        self.data.header().glyph_count.get()
746
561k
    }
747
748
    /// The lookup values (indexed by the glyph index minus the value of
749
    /// firstGlyph). Entries in the value array must be two bytes.
750
561k
    pub fn value_array(&self) -> &'a [BigEndian<u16>] {
751
561k
        let range = self.value_array_byte_range();
752
561k
        self.offset_data()
753
561k
            .read_array(range)
754
561k
            .ok()
755
561k
            .unwrap_or_default()
756
561k
    }
757
758
561k
    pub fn format_byte_range(&self) -> Range<usize> {
759
561k
        let start = 0usize;
760
561k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
761
561k
        start..end
762
561k
    }
763
764
561k
    pub fn first_glyph_byte_range(&self) -> Range<usize> {
765
561k
        let start = self.format_byte_range().end;
766
561k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
767
561k
        start..end
768
561k
    }
769
770
561k
    pub fn glyph_count_byte_range(&self) -> Range<usize> {
771
561k
        let start = self.first_glyph_byte_range().end;
772
561k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
773
561k
        start..end
774
561k
    }
775
776
561k
    pub fn value_array_byte_range(&self) -> Range<usize> {
777
561k
        let glyph_count = self.glyph_count();
778
561k
        let start = self.glyph_count_byte_range().end;
779
561k
        let end = start
780
561k
            .saturating_add((transforms::to_usize(glyph_count)).saturating_mul(u16::RAW_BYTE_LEN));
781
561k
        start..end
782
561k
    }
783
}
784
785
impl Format<u16> for Lookup10<'_> {
786
    const FORMAT: u16 = 10;
787
}
788
789
impl<'a> MinByteRange<'a> for Lookup10<'a> {
790
0
    fn min_byte_range(&self) -> Range<usize> {
791
0
        0..self.values_data_byte_range().end
792
0
    }
793
0
    fn min_table_bytes(&self) -> &'a [u8] {
794
0
        let range = self.min_byte_range();
795
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
796
0
    }
797
}
798
799
impl ReadArgs for Lookup10<'_> {
800
    type Args = ();
801
}
802
803
impl<'a> FontRead<'a> for Lookup10<'a> {
804
62.2k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
805
        #[allow(clippy::absurd_extreme_comparisons)]
806
62.2k
        if data.len() < Self::MIN_SIZE {
807
95
            return Err(ReadError::OutOfBounds);
808
62.1k
        }
809
62.1k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
810
0
            return Err(ReadError::OutOfBounds);
811
        };
812
62.1k
        Ok(Self { data })
813
62.2k
    }
814
}
815
816
/// The fields at the start of the table, which are read directly.
817
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
818
#[repr(C, packed)]
819
#[doc(hidden)]
820
pub struct Lookup10FixedHeader {
821
    pub format: BigEndian<u16>,
822
    pub unit_size: BigEndian<u16>,
823
    pub first_glyph: BigEndian<u16>,
824
    pub glyph_count: BigEndian<u16>,
825
}
826
827
#[allow(clippy::absurd_extreme_comparisons)]
828
const _: () = assert!(std::mem::size_of::<Lookup10FixedHeader>() <= Lookup10::MIN_SIZE);
829
830
/// Trimmed array format. The lookup data is a simple trimmed array
831
/// indexed by glyph index.
832
#[derive(Clone)]
833
pub struct Lookup10<'a> {
834
    data: TableDataRef<'a, Lookup10FixedHeader>,
835
}
836
837
#[allow(clippy::needless_lifetimes)]
838
impl<'a> Lookup10<'a> {
839
    pub const MIN_SIZE: usize =
840
        (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
841
    basic_table_impls!(impl_the_methods);
842
843
    /// Format number is set to 10.
844
0
    pub fn format(&self) -> u16 {
845
0
        self.data.header().format.get()
846
0
    }
847
848
    /// Size of a lookup unit for this lookup table in bytes. Allowed values
849
    /// are 1, 2, 4, and 8.
850
3.26M
    pub fn unit_size(&self) -> u16 {
851
3.26M
        self.data.header().unit_size.get()
852
3.26M
    }
853
854
    /// First glyph index included in the trimmed array.
855
1.79M
    pub fn first_glyph(&self) -> u16 {
856
1.79M
        self.data.header().first_glyph.get()
857
1.79M
    }
858
859
    /// Total number of glyphs (equivalent to the last glyph minus the value
860
    /// of firstGlyph plus 1).
861
1.63M
    pub fn glyph_count(&self) -> u16 {
862
1.63M
        self.data.header().glyph_count.get()
863
1.63M
    }
864
865
    /// The lookup values (indexed by the glyph index minus the value of
866
    /// firstGlyph).
867
1.63M
    pub fn values_data(&self) -> &'a [u8] {
868
1.63M
        let range = self.values_data_byte_range();
869
1.63M
        self.offset_data()
870
1.63M
            .read_array(range)
871
1.63M
            .ok()
872
1.63M
            .unwrap_or_default()
873
1.63M
    }
874
875
1.63M
    pub fn format_byte_range(&self) -> Range<usize> {
876
1.63M
        let start = 0usize;
877
1.63M
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
878
1.63M
        start..end
879
1.63M
    }
880
881
1.63M
    pub fn unit_size_byte_range(&self) -> Range<usize> {
882
1.63M
        let start = self.format_byte_range().end;
883
1.63M
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
884
1.63M
        start..end
885
1.63M
    }
886
887
1.63M
    pub fn first_glyph_byte_range(&self) -> Range<usize> {
888
1.63M
        let start = self.unit_size_byte_range().end;
889
1.63M
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
890
1.63M
        start..end
891
1.63M
    }
892
893
1.63M
    pub fn glyph_count_byte_range(&self) -> Range<usize> {
894
1.63M
        let start = self.first_glyph_byte_range().end;
895
1.63M
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
896
1.63M
        start..end
897
1.63M
    }
898
899
1.63M
    pub fn values_data_byte_range(&self) -> Range<usize> {
900
1.63M
        let glyph_count = self.glyph_count();
901
1.63M
        let unit_size = self.unit_size();
902
1.63M
        let start = self.glyph_count_byte_range().end;
903
1.63M
        let end = start.saturating_add(
904
1.63M
            (transforms::add_multiply(glyph_count, 0_usize, unit_size))
905
1.63M
                .saturating_mul(u8::RAW_BYTE_LEN),
906
        );
907
1.63M
        start..end
908
1.63M
    }
909
}
910
911
impl<'a> MinByteRange<'a> for StateHeader<'a> {
912
0
    fn min_byte_range(&self) -> Range<usize> {
913
0
        0..self.entry_table_offset_byte_range().end
914
0
    }
915
0
    fn min_table_bytes(&self) -> &'a [u8] {
916
0
        let range = self.min_byte_range();
917
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
918
0
    }
919
}
920
921
impl ReadArgs for StateHeader<'_> {
922
    type Args = ();
923
}
924
925
impl<'a> FontRead<'a> for StateHeader<'a> {
926
6.79k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
927
        #[allow(clippy::absurd_extreme_comparisons)]
928
6.79k
        if data.len() < Self::MIN_SIZE {
929
141
            return Err(ReadError::OutOfBounds);
930
6.65k
        }
931
6.65k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
932
0
            return Err(ReadError::OutOfBounds);
933
        };
934
6.65k
        Ok(Self { data })
935
6.79k
    }
936
}
937
938
/// The fields at the start of the table, which are read directly.
939
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
940
#[repr(C, packed)]
941
#[doc(hidden)]
942
pub struct StateHeaderFixedHeader {
943
    pub state_size: BigEndian<u16>,
944
    pub class_table_offset: BigEndian<Offset16>,
945
    pub state_array_offset: BigEndian<Offset16>,
946
    pub entry_table_offset: BigEndian<Offset16>,
947
}
948
949
#[allow(clippy::absurd_extreme_comparisons)]
950
const _: () = assert!(std::mem::size_of::<StateHeaderFixedHeader>() <= StateHeader::MIN_SIZE);
951
952
/// Header for a state table.
953
#[derive(Clone)]
954
pub struct StateHeader<'a> {
955
    data: TableDataRef<'a, StateHeaderFixedHeader>,
956
}
957
958
#[allow(clippy::needless_lifetimes)]
959
impl<'a> StateHeader<'a> {
960
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
961
        + Offset16::RAW_BYTE_LEN
962
        + Offset16::RAW_BYTE_LEN
963
        + Offset16::RAW_BYTE_LEN);
964
    basic_table_impls!(impl_the_methods);
965
966
    /// Size of a state, in bytes. The size is limited to 8 bits, although the
967
    /// field is 16 bits for alignment.
968
7.43k
    pub fn state_size(&self) -> u16 {
969
7.43k
        self.data.header().state_size.get()
970
7.43k
    }
971
972
    /// Byte offset from the beginning of the state table to the class subtable.
973
4.11k
    pub fn class_table_offset(&self) -> Offset16 {
974
4.11k
        self.data.header().class_table_offset.get()
975
4.11k
    }
976
977
    /// Attempt to resolve [`class_table_offset`][Self::class_table_offset].
978
4.11k
    pub fn class_table(&self) -> Result<ClassSubtable<'a>, ReadError> {
979
4.11k
        let data = self.offset_data();
980
4.11k
        self.class_table_offset().resolve(data)
981
4.11k
    }
982
983
    /// Byte offset from the beginning of the state table to the state array.
984
337M
    pub fn state_array_offset(&self) -> Offset16 {
985
337M
        self.data.header().state_array_offset.get()
986
337M
    }
987
988
    /// Attempt to resolve [`state_array_offset`][Self::state_array_offset].
989
5.66k
    pub fn state_array(&self) -> Result<RawBytes<'a>, ReadError> {
990
5.66k
        let data = self.offset_data();
991
5.66k
        self.state_array_offset().resolve(data)
992
5.66k
    }
993
994
    /// Byte offset from the beginning of the state table to the entry subtable.
995
2.78k
    pub fn entry_table_offset(&self) -> Offset16 {
996
2.78k
        self.data.header().entry_table_offset.get()
997
2.78k
    }
998
999
    /// Attempt to resolve [`entry_table_offset`][Self::entry_table_offset].
1000
2.78k
    pub fn entry_table(&self) -> Result<RawBytes<'a>, ReadError> {
1001
2.78k
        let data = self.offset_data();
1002
2.78k
        self.entry_table_offset().resolve(data)
1003
2.78k
    }
1004
1005
0
    pub fn state_size_byte_range(&self) -> Range<usize> {
1006
0
        let start = 0usize;
1007
0
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1008
0
        start..end
1009
0
    }
1010
1011
0
    pub fn class_table_offset_byte_range(&self) -> Range<usize> {
1012
0
        let start = self.state_size_byte_range().end;
1013
0
        let end = start.saturating_add(Offset16::RAW_BYTE_LEN);
1014
0
        start..end
1015
0
    }
1016
1017
0
    pub fn state_array_offset_byte_range(&self) -> Range<usize> {
1018
0
        let start = self.class_table_offset_byte_range().end;
1019
0
        let end = start.saturating_add(Offset16::RAW_BYTE_LEN);
1020
0
        start..end
1021
0
    }
1022
1023
0
    pub fn entry_table_offset_byte_range(&self) -> Range<usize> {
1024
0
        let start = self.state_array_offset_byte_range().end;
1025
0
        let end = start.saturating_add(Offset16::RAW_BYTE_LEN);
1026
0
        start..end
1027
0
    }
1028
}
1029
1030
const _: () = assert!(FontData::default_data_long_enough(StateHeader::MIN_SIZE));
1031
1032
impl Default for StateHeader<'_> {
1033
0
    fn default() -> Self {
1034
        Self {
1035
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
1036
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
1037
        }
1038
0
    }
1039
}
1040
1041
impl<'a> MinByteRange<'a> for ClassSubtable<'a> {
1042
0
    fn min_byte_range(&self) -> Range<usize> {
1043
0
        0..self.class_array_byte_range().end
1044
0
    }
1045
0
    fn min_table_bytes(&self) -> &'a [u8] {
1046
0
        let range = self.min_byte_range();
1047
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
1048
0
    }
1049
}
1050
1051
impl ReadArgs for ClassSubtable<'_> {
1052
    type Args = ();
1053
}
1054
1055
impl<'a> FontRead<'a> for ClassSubtable<'a> {
1056
3.77k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
1057
        #[allow(clippy::absurd_extreme_comparisons)]
1058
3.77k
        if data.len() < Self::MIN_SIZE {
1059
28
            return Err(ReadError::OutOfBounds);
1060
3.74k
        }
1061
3.74k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
1062
0
            return Err(ReadError::OutOfBounds);
1063
        };
1064
3.74k
        Ok(Self { data })
1065
3.77k
    }
1066
}
1067
1068
/// The fields at the start of the table, which are read directly.
1069
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1070
#[repr(C, packed)]
1071
#[doc(hidden)]
1072
pub struct ClassSubtableFixedHeader {
1073
    pub first_glyph: BigEndian<u16>,
1074
    pub n_glyphs: BigEndian<u16>,
1075
}
1076
1077
#[allow(clippy::absurd_extreme_comparisons)]
1078
const _: () = assert!(std::mem::size_of::<ClassSubtableFixedHeader>() <= ClassSubtable::MIN_SIZE);
1079
1080
/// Maps the glyph indexes of your font into classes.
1081
#[derive(Clone)]
1082
pub struct ClassSubtable<'a> {
1083
    data: TableDataRef<'a, ClassSubtableFixedHeader>,
1084
}
1085
1086
#[allow(clippy::needless_lifetimes)]
1087
impl<'a> ClassSubtable<'a> {
1088
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
1089
    basic_table_impls!(impl_the_methods);
1090
1091
    /// Glyph index of the first glyph in the class table.
1092
3.74k
    pub fn first_glyph(&self) -> u16 {
1093
3.74k
        self.data.header().first_glyph.get()
1094
3.74k
    }
1095
1096
    /// Number of glyphs in class table.
1097
3.74k
    pub fn n_glyphs(&self) -> u16 {
1098
3.74k
        self.data.header().n_glyphs.get()
1099
3.74k
    }
1100
1101
    /// The class codes (indexed by glyph index minus firstGlyph). Class codes
1102
    /// range from 0 to the value of stateSize minus 1.
1103
3.74k
    pub fn class_array(&self) -> &'a [u8] {
1104
3.74k
        let range = self.class_array_byte_range();
1105
3.74k
        self.offset_data()
1106
3.74k
            .read_array(range)
1107
3.74k
            .ok()
1108
3.74k
            .unwrap_or_default()
1109
3.74k
    }
1110
1111
3.74k
    pub fn first_glyph_byte_range(&self) -> Range<usize> {
1112
3.74k
        let start = 0usize;
1113
3.74k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1114
3.74k
        start..end
1115
3.74k
    }
1116
1117
3.74k
    pub fn n_glyphs_byte_range(&self) -> Range<usize> {
1118
3.74k
        let start = self.first_glyph_byte_range().end;
1119
3.74k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1120
3.74k
        start..end
1121
3.74k
    }
1122
1123
3.74k
    pub fn class_array_byte_range(&self) -> Range<usize> {
1124
3.74k
        let n_glyphs = self.n_glyphs();
1125
3.74k
        let start = self.n_glyphs_byte_range().end;
1126
3.74k
        let end =
1127
3.74k
            start.saturating_add((transforms::to_usize(n_glyphs)).saturating_mul(u8::RAW_BYTE_LEN));
1128
3.74k
        start..end
1129
3.74k
    }
1130
}
1131
1132
const _: () = assert!(FontData::default_data_long_enough(ClassSubtable::MIN_SIZE));
1133
1134
impl Default for ClassSubtable<'_> {
1135
0
    fn default() -> Self {
1136
        Self {
1137
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
1138
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
1139
        }
1140
0
    }
1141
}
1142
1143
impl<'a> MinByteRange<'a> for RawBytes<'a> {
1144
0
    fn min_byte_range(&self) -> Range<usize> {
1145
0
        0..self.data_byte_range().end
1146
0
    }
1147
0
    fn min_table_bytes(&self) -> &'a [u8] {
1148
0
        let range = self.min_byte_range();
1149
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
1150
0
    }
1151
}
1152
1153
impl ReadArgs for RawBytes<'_> {
1154
    type Args = ();
1155
}
1156
1157
impl<'a> FontRead<'a> for RawBytes<'a> {
1158
12.8k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
1159
        #[allow(clippy::absurd_extreme_comparisons)]
1160
12.8k
        if data.len() < Self::MIN_SIZE {
1161
0
            return Err(ReadError::OutOfBounds);
1162
12.8k
        }
1163
12.8k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
1164
0
            return Err(ReadError::OutOfBounds);
1165
        };
1166
12.8k
        Ok(Self { data })
1167
12.8k
    }
1168
}
1169
1170
/// The fields at the start of the table, which are read directly.
1171
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1172
#[repr(C, packed)]
1173
#[doc(hidden)]
1174
pub struct RawBytesFixedHeader {}
1175
1176
#[allow(clippy::absurd_extreme_comparisons)]
1177
const _: () = assert!(std::mem::size_of::<RawBytesFixedHeader>() <= RawBytes::MIN_SIZE);
1178
1179
/// Used for the `state_array` and `entry_table` fields in [`StateHeader`].
1180
#[derive(Clone)]
1181
pub struct RawBytes<'a> {
1182
    data: TableDataRef<'a, RawBytesFixedHeader>,
1183
}
1184
1185
#[allow(clippy::needless_lifetimes)]
1186
impl<'a> RawBytes<'a> {
1187
    pub const MIN_SIZE: usize = 0;
1188
    basic_table_impls!(impl_the_methods);
1189
1190
12.8k
    pub fn data(&self) -> &'a [u8] {
1191
12.8k
        let range = self.data_byte_range();
1192
12.8k
        self.offset_data()
1193
12.8k
            .read_array(range)
1194
12.8k
            .ok()
1195
12.8k
            .unwrap_or_default()
1196
12.8k
    }
1197
1198
12.8k
    pub fn data_byte_range(&self) -> Range<usize> {
1199
12.8k
        let start = 0usize;
1200
12.8k
        let end = start.saturating_add(
1201
12.8k
            self.offset_data().len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN,
1202
        );
1203
12.8k
        start..end
1204
12.8k
    }
1205
}
1206
1207
#[allow(clippy::absurd_extreme_comparisons)]
1208
const _: () = assert!(FontData::default_data_long_enough(RawBytes::MIN_SIZE));
1209
1210
impl Default for RawBytes<'_> {
1211
0
    fn default() -> Self {
1212
        Self {
1213
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
1214
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
1215
        }
1216
0
    }
1217
}
1218
1219
impl<'a> MinByteRange<'a> for StxHeader<'a> {
1220
0
    fn min_byte_range(&self) -> Range<usize> {
1221
0
        0..self.entry_table_offset_byte_range().end
1222
0
    }
1223
0
    fn min_table_bytes(&self) -> &'a [u8] {
1224
0
        let range = self.min_byte_range();
1225
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
1226
0
    }
1227
}
1228
1229
impl ReadArgs for StxHeader<'_> {
1230
    type Args = ();
1231
}
1232
1233
impl<'a> FontRead<'a> for StxHeader<'a> {
1234
18.6k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
1235
        #[allow(clippy::absurd_extreme_comparisons)]
1236
18.6k
        if data.len() < Self::MIN_SIZE {
1237
37
            return Err(ReadError::OutOfBounds);
1238
18.6k
        }
1239
18.6k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
1240
0
            return Err(ReadError::OutOfBounds);
1241
        };
1242
18.6k
        Ok(Self { data })
1243
18.6k
    }
1244
}
1245
1246
/// The fields at the start of the table, which are read directly.
1247
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1248
#[repr(C, packed)]
1249
#[doc(hidden)]
1250
pub struct StxHeaderFixedHeader {
1251
    pub n_classes: BigEndian<u32>,
1252
    pub class_table_offset: BigEndian<Offset32>,
1253
    pub state_array_offset: BigEndian<Offset32>,
1254
    pub entry_table_offset: BigEndian<Offset32>,
1255
}
1256
1257
#[allow(clippy::absurd_extreme_comparisons)]
1258
const _: () = assert!(std::mem::size_of::<StxHeaderFixedHeader>() <= StxHeader::MIN_SIZE);
1259
1260
/// Header for an extended state table.
1261
#[derive(Clone)]
1262
pub struct StxHeader<'a> {
1263
    data: TableDataRef<'a, StxHeaderFixedHeader>,
1264
}
1265
1266
#[allow(clippy::needless_lifetimes)]
1267
impl<'a> StxHeader<'a> {
1268
    pub const MIN_SIZE: usize = (u32::RAW_BYTE_LEN
1269
        + Offset32::RAW_BYTE_LEN
1270
        + Offset32::RAW_BYTE_LEN
1271
        + Offset32::RAW_BYTE_LEN);
1272
    basic_table_impls!(impl_the_methods);
1273
1274
    /// Number of classes, which is the number of 16-bit entry indices in a single line in the state array.
1275
18.6k
    pub fn n_classes(&self) -> u32 {
1276
18.6k
        self.data.header().n_classes.get()
1277
18.6k
    }
1278
1279
    /// Byte offset from the beginning of the state table to the class subtable.
1280
18.6k
    pub fn class_table_offset(&self) -> Offset32 {
1281
18.6k
        self.data.header().class_table_offset.get()
1282
18.6k
    }
1283
1284
    /// Attempt to resolve [`class_table_offset`][Self::class_table_offset].
1285
5.09k
    pub fn class_table(&self) -> Result<LookupU16<'a>, ReadError> {
1286
5.09k
        let data = self.offset_data();
1287
5.09k
        self.class_table_offset().resolve(data)
1288
5.09k
    }
1289
1290
    /// Byte offset from the beginning of the state table to the state array.
1291
18.3k
    pub fn state_array_offset(&self) -> Offset32 {
1292
18.3k
        self.data.header().state_array_offset.get()
1293
18.3k
    }
1294
1295
    /// Attempt to resolve [`state_array_offset`][Self::state_array_offset].
1296
4.82k
    pub fn state_array(&self) -> Result<RawWords<'a>, ReadError> {
1297
4.82k
        let data = self.offset_data();
1298
4.82k
        self.state_array_offset().resolve(data)
1299
4.82k
    }
1300
1301
    /// Byte offset from the beginning of the state table to the entry subtable.
1302
18.2k
    pub fn entry_table_offset(&self) -> Offset32 {
1303
18.2k
        self.data.header().entry_table_offset.get()
1304
18.2k
    }
1305
1306
    /// Attempt to resolve [`entry_table_offset`][Self::entry_table_offset].
1307
4.71k
    pub fn entry_table(&self) -> Result<RawBytes<'a>, ReadError> {
1308
4.71k
        let data = self.offset_data();
1309
4.71k
        self.entry_table_offset().resolve(data)
1310
4.71k
    }
1311
1312
0
    pub fn n_classes_byte_range(&self) -> Range<usize> {
1313
0
        let start = 0usize;
1314
0
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1315
0
        start..end
1316
0
    }
1317
1318
0
    pub fn class_table_offset_byte_range(&self) -> Range<usize> {
1319
0
        let start = self.n_classes_byte_range().end;
1320
0
        let end = start.saturating_add(Offset32::RAW_BYTE_LEN);
1321
0
        start..end
1322
0
    }
1323
1324
0
    pub fn state_array_offset_byte_range(&self) -> Range<usize> {
1325
0
        let start = self.class_table_offset_byte_range().end;
1326
0
        let end = start.saturating_add(Offset32::RAW_BYTE_LEN);
1327
0
        start..end
1328
0
    }
1329
1330
0
    pub fn entry_table_offset_byte_range(&self) -> Range<usize> {
1331
0
        let start = self.state_array_offset_byte_range().end;
1332
0
        let end = start.saturating_add(Offset32::RAW_BYTE_LEN);
1333
0
        start..end
1334
0
    }
1335
}
1336
1337
const _: () = assert!(FontData::default_data_long_enough(StxHeader::MIN_SIZE));
1338
1339
impl Default for StxHeader<'_> {
1340
0
    fn default() -> Self {
1341
        Self {
1342
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
1343
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
1344
        }
1345
0
    }
1346
}
1347
1348
impl<'a> MinByteRange<'a> for RawWords<'a> {
1349
0
    fn min_byte_range(&self) -> Range<usize> {
1350
0
        0..self.data_byte_range().end
1351
0
    }
1352
0
    fn min_table_bytes(&self) -> &'a [u8] {
1353
0
        let range = self.min_byte_range();
1354
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
1355
0
    }
1356
}
1357
1358
impl ReadArgs for RawWords<'_> {
1359
    type Args = ();
1360
}
1361
1362
impl<'a> FontRead<'a> for RawWords<'a> {
1363
4.71k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
1364
        #[allow(clippy::absurd_extreme_comparisons)]
1365
4.71k
        if data.len() < Self::MIN_SIZE {
1366
0
            return Err(ReadError::OutOfBounds);
1367
4.71k
        }
1368
4.71k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
1369
0
            return Err(ReadError::OutOfBounds);
1370
        };
1371
4.71k
        Ok(Self { data })
1372
4.71k
    }
1373
}
1374
1375
/// The fields at the start of the table, which are read directly.
1376
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1377
#[repr(C, packed)]
1378
#[doc(hidden)]
1379
pub struct RawWordsFixedHeader {}
1380
1381
#[allow(clippy::absurd_extreme_comparisons)]
1382
const _: () = assert!(std::mem::size_of::<RawWordsFixedHeader>() <= RawWords::MIN_SIZE);
1383
1384
/// Used for the `state_array` in [`StxHeader`].
1385
#[derive(Clone)]
1386
pub struct RawWords<'a> {
1387
    data: TableDataRef<'a, RawWordsFixedHeader>,
1388
}
1389
1390
#[allow(clippy::needless_lifetimes)]
1391
impl<'a> RawWords<'a> {
1392
    pub const MIN_SIZE: usize = 0;
1393
    basic_table_impls!(impl_the_methods);
1394
1395
4.71k
    pub fn data(&self) -> &'a [BigEndian<u16>] {
1396
4.71k
        let range = self.data_byte_range();
1397
4.71k
        self.offset_data()
1398
4.71k
            .read_array(range)
1399
4.71k
            .ok()
1400
4.71k
            .unwrap_or_default()
1401
4.71k
    }
1402
1403
4.71k
    pub fn data_byte_range(&self) -> Range<usize> {
1404
4.71k
        let start = 0usize;
1405
4.71k
        let end = start.saturating_add(
1406
4.71k
            self.offset_data().len().saturating_sub(start) / u16::RAW_BYTE_LEN * u16::RAW_BYTE_LEN,
1407
        );
1408
4.71k
        start..end
1409
4.71k
    }
1410
}
1411
1412
#[allow(clippy::absurd_extreme_comparisons)]
1413
const _: () = assert!(FontData::default_data_long_enough(RawWords::MIN_SIZE));
1414
1415
impl Default for RawWords<'_> {
1416
0
    fn default() -> Self {
1417
        Self {
1418
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
1419
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
1420
        }
1421
0
    }
1422
}