1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
//! Apple Advanced Typography common tables.
//!
//! See <https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6Tables.html>

include!("../../generated/generated_aat.rs");

/// Predefined classes.
///
/// See <https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6Tables.html>
pub mod class {
    pub const END_OF_TEXT: u8 = 0;
    pub const OUT_OF_BOUNDS: u8 = 1;
    pub const DELETED_GLYPH: u8 = 2;
}

impl<'a> Lookup0<'a> {
    pub fn value<T: LookupValue>(&self, index: u16) -> Result<T, ReadError> {
        let data = self.values_data();
        let data_len = data.len();
        let n_elems = data_len / T::RAW_BYTE_LEN;
        let len_in_bytes = n_elems * T::RAW_BYTE_LEN;
        FontData::new(&data[..len_in_bytes])
            .cursor()
            .read_array::<BigEndian<T>>(n_elems)?
            .get(index as usize)
            .map(|val| val.get())
            .ok_or(ReadError::OutOfBounds)
    }
}

/// Lookup segment for format 2.
#[derive(Copy, Clone, bytemuck::AnyBitPattern)]
#[repr(packed)]
pub struct LookupSegment2<T>
where
    T: LookupValue,
{
    /// Last glyph index in this segment.
    pub last_glyph: BigEndian<u16>,
    /// First glyph index in this segment.
    pub first_glyph: BigEndian<u16>,
    /// The lookup value.
    pub value: BigEndian<T>,
}

/// Note: this requires `LookupSegment2` to be `repr(packed)`.
impl<T: LookupValue> FixedSize for LookupSegment2<T> {
    const RAW_BYTE_LEN: usize = std::mem::size_of::<Self>();
}

impl<'a> Lookup2<'a> {
    pub fn value<T: LookupValue>(&self, index: u16) -> Result<T, ReadError> {
        let segments = self.segments::<T>()?;
        let ix = match segments.binary_search_by(|segment| segment.first_glyph.get().cmp(&index)) {
            Ok(ix) => ix,
            Err(ix) => ix.saturating_sub(1),
        };
        let segment = segments.get(ix).ok_or(ReadError::OutOfBounds)?;
        if (segment.first_glyph.get()..=segment.last_glyph.get()).contains(&index) {
            let value = segment.value;
            return Ok(value.get());
        }
        Err(ReadError::OutOfBounds)
    }

    fn segments<T: LookupValue>(&self) -> Result<&[LookupSegment2<T>], ReadError> {
        FontData::new(self.segments_data())
            .cursor()
            .read_array(self.n_units() as usize)
    }
}

impl<'a> Lookup4<'a> {
    pub fn value<T: LookupValue>(&self, index: u16) -> Result<T, ReadError> {
        let segments = self.segments();
        let ix = match segments.binary_search_by(|segment| segment.first_glyph.get().cmp(&index)) {
            Ok(ix) => ix,
            Err(ix) => ix.saturating_sub(1),
        };
        let segment = segments.get(ix).ok_or(ReadError::OutOfBounds)?;
        if (segment.first_glyph.get()..=segment.last_glyph.get()).contains(&index) {
            let base_offset = segment.value_offset() as usize;
            let offset = base_offset
                + index
                    .checked_sub(segment.first_glyph())
                    .ok_or(ReadError::OutOfBounds)? as usize
                    * T::RAW_BYTE_LEN;
            return self.offset_data().read_at(offset);
        }
        Err(ReadError::OutOfBounds)
    }
}

/// Lookup single record for format 6.
#[derive(Copy, Clone, bytemuck::AnyBitPattern)]
#[repr(packed)]
pub struct LookupSingle<T>
where
    T: LookupValue,
{
    /// The glyph index.
    pub glyph: BigEndian<u16>,
    /// The lookup value.
    pub value: BigEndian<T>,
}

/// Note: this requires `LookupSingle` to be `repr(packed)`.
impl<T: LookupValue> FixedSize for LookupSingle<T> {
    const RAW_BYTE_LEN: usize = std::mem::size_of::<Self>();
}

impl<'a> Lookup6<'a> {
    pub fn value<T: LookupValue>(&self, index: u16) -> Result<T, ReadError> {
        let entries = self.entries::<T>()?;
        if let Ok(ix) = entries.binary_search_by_key(&index, |entry| entry.glyph.get()) {
            let entry = &entries[ix];
            let value = entry.value;
            return Ok(value.get());
        }
        Err(ReadError::OutOfBounds)
    }

    fn entries<T: LookupValue>(&self) -> Result<&[LookupSingle<T>], ReadError> {
        FontData::new(self.entries_data())
            .cursor()
            .read_array(self.n_units() as usize)
    }
}

impl<'a> Lookup8<'a> {
    pub fn value<T: LookupValue>(&self, index: u16) -> Result<T, ReadError> {
        index
            .checked_sub(self.first_glyph())
            .and_then(|ix| {
                self.value_array()
                    .get(ix as usize)
                    .map(|val| T::from_u16(val.get()))
            })
            .ok_or(ReadError::OutOfBounds)
    }
}

impl<'a> Lookup10<'a> {
    pub fn value<T: LookupValue>(&self, index: u16) -> Result<T, ReadError> {
        let ix = index
            .checked_sub(self.first_glyph())
            .ok_or(ReadError::OutOfBounds)? as usize;
        let unit_size = self.unit_size() as usize;
        let offset = ix * unit_size;
        let mut cursor = FontData::new(self.values_data()).cursor();
        cursor.advance_by(offset);
        let val = match unit_size {
            1 => cursor.read::<u8>()? as u32,
            2 => cursor.read::<u16>()? as u32,
            4 => cursor.read::<u32>()?,
            _ => {
                return Err(ReadError::MalformedData(
                    "invalid unit_size in format 10 AAT lookup table",
                ))
            }
        };
        Ok(T::from_u32(val))
    }
}

impl<'a> Lookup<'a> {
    pub fn value<T: LookupValue>(&self, index: u16) -> Result<T, ReadError> {
        match self {
            Lookup::Format0(lookup) => lookup.value::<T>(index),
            Lookup::Format2(lookup) => lookup.value::<T>(index),
            Lookup::Format4(lookup) => lookup.value::<T>(index),
            Lookup::Format6(lookup) => lookup.value::<T>(index),
            Lookup::Format8(lookup) => lookup.value::<T>(index),
            Lookup::Format10(lookup) => lookup.value::<T>(index),
        }
    }
}

pub struct TypedLookup<'a, T> {
    lookup: Lookup<'a>,
    _marker: std::marker::PhantomData<fn() -> T>,
}

impl<'a, T: LookupValue> TypedLookup<'a, T> {
    /// Returns the value associated with the given index.
    pub fn value(&self, index: u16) -> Result<T, ReadError> {
        self.lookup.value::<T>(index)
    }
}

impl<'a, T> FontRead<'a> for TypedLookup<'a, T> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        Ok(Self {
            lookup: Lookup::read(data)?,
            _marker: std::marker::PhantomData,
        })
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a, T> SomeTable<'a> for TypedLookup<'a, T> {
    fn type_name(&self) -> &str {
        "TypedLookup"
    }

    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        self.lookup.get_field(idx)
    }
}

/// Trait for values that can be read from lookup tables.
pub trait LookupValue: Copy + Scalar + bytemuck::AnyBitPattern {
    fn from_u16(v: u16) -> Self;
    fn from_u32(v: u32) -> Self;
}

impl LookupValue for u16 {
    fn from_u16(v: u16) -> Self {
        v
    }

    fn from_u32(v: u32) -> Self {
        // intentionally truncates
        v as _
    }
}

impl LookupValue for u32 {
    fn from_u16(v: u16) -> Self {
        v as _
    }

    fn from_u32(v: u32) -> Self {
        v
    }
}

impl LookupValue for GlyphId16 {
    fn from_u16(v: u16) -> Self {
        GlyphId16::from(v)
    }

    fn from_u32(v: u32) -> Self {
        // intentionally truncates
        GlyphId16::from(v as u16)
    }
}

pub type LookupU16<'a> = TypedLookup<'a, u16>;
pub type LookupU32<'a> = TypedLookup<'a, u32>;
pub type LookupGlyphId<'a> = TypedLookup<'a, GlyphId16>;

/// Empty data type for a state table entry with no payload.
///
/// Note: this type is only intended for use as the type parameter for
/// `StateEntry`. The inner field is private and this type cannot be
/// constructed outside of this module.
#[derive(Copy, Clone, bytemuck::AnyBitPattern)]
pub struct NoPayload(());

impl FixedSize for NoPayload {
    const RAW_BYTE_LEN: usize = 0;
}

/// Entry in an (extended) state table.
pub struct StateEntry<T = NoPayload> {
    /// Index of the next state.
    pub new_state: u16,
    /// Flag values are table specific.
    pub flags: u16,
    /// Payload is table specific.
    pub payload: T,
}

impl<'a, T: bytemuck::AnyBitPattern + FixedSize> FontRead<'a> for StateEntry<T> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        let mut cursor = data.cursor();
        let new_state = cursor.read()?;
        let flags = cursor.read()?;
        let remaining = cursor.remaining().ok_or(ReadError::OutOfBounds)?;
        let payload = *remaining.read_ref_at(0)?;
        Ok(Self {
            new_state,
            flags,
            payload,
        })
    }
}

impl<T> FixedSize for StateEntry<T>
where
    T: FixedSize,
{
    // Two u16 fields + payload
    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + T::RAW_BYTE_LEN;
}

pub struct StateTable<'a> {
    header: StateHeader<'a>,
}

impl<'a> StateTable<'a> {
    /// Returns the class table entry for the given glyph identifier.
    pub fn class(&self, glyph_id: GlyphId16) -> Result<u8, ReadError> {
        let glyph_id = glyph_id.to_u16();
        if glyph_id == 0xFFFF {
            return Ok(class::DELETED_GLYPH);
        }
        let class_table = self.header.class_table()?;
        glyph_id
            .checked_sub(class_table.first_glyph())
            .and_then(|ix| class_table.class_array().get(ix as usize).copied())
            .ok_or(ReadError::OutOfBounds)
    }

    /// Returns the entry for the given state and class.
    pub fn entry(&self, state: u16, class: u8) -> Result<StateEntry, ReadError> {
        // Each state has a 1-byte entry per class so state_size == n_classes
        let n_classes = self.header.state_size() as usize;
        if n_classes == 0 {
            // Avoid potential divide by zero below
            return Err(ReadError::MalformedData("empty AAT state table"));
        }
        let mut class = class as usize;
        if class >= n_classes {
            class = class::OUT_OF_BOUNDS as usize;
        }
        let state_array = self.header.state_array()?.data();
        let entry_ix = state_array
            .get(
                (state as usize)
                    .checked_mul(n_classes)
                    .ok_or(ReadError::OutOfBounds)?
                    + class,
            )
            .copied()
            .ok_or(ReadError::OutOfBounds)? as usize;
        let entry_offset = entry_ix * 4;
        let entry_data = self
            .header
            .entry_table()?
            .data()
            .get(entry_offset..)
            .ok_or(ReadError::OutOfBounds)?;
        let mut entry = StateEntry::read(FontData::new(entry_data))?;
        // For legacy state tables, the newState is a byte offset into
        // the state array. Convert this to an index for consistency.
        let new_state = (entry.new_state as i32)
            .checked_sub(self.header.state_array_offset().to_u32() as i32)
            .ok_or(ReadError::OutOfBounds)?
            / n_classes as i32;
        entry.new_state = new_state.try_into().map_err(|_| ReadError::OutOfBounds)?;
        Ok(entry)
    }
}

impl<'a> FontRead<'a> for StateTable<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        Ok(Self {
            header: StateHeader::read(data)?,
        })
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for StateTable<'a> {
    fn type_name(&self) -> &str {
        "StateTable"
    }

    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        self.header.get_field(idx)
    }
}

pub struct ExtendedStateTable<'a, T = ()> {
    header: StxHeader<'a>,
    _marker: std::marker::PhantomData<fn() -> T>,
}

impl<'a, T: bytemuck::AnyBitPattern + FixedSize> ExtendedStateTable<'a, T> {
    /// Returns the class table entry for the given glyph identifier.
    pub fn class(&self, glyph_id: GlyphId16) -> Result<u16, ReadError> {
        let glyph_id = glyph_id.to_u16();
        if glyph_id == 0xFFFF {
            return Ok(class::DELETED_GLYPH as u16);
        }
        self.header.class_table()?.value(glyph_id)
    }

    /// Returns the entry for the given state and class.
    pub fn entry(&self, state: u16, class: u16) -> Result<StateEntry<T>, ReadError> {
        let n_classes = self.header.n_classes() as usize;
        let mut class = class as usize;
        if class >= n_classes {
            class = class::OUT_OF_BOUNDS as usize;
        }
        let state_array = self.header.state_array()?.data();
        let state_ix = state as usize * n_classes + class;
        let entry_ix = state_array
            .get(state_ix)
            .copied()
            .ok_or(ReadError::OutOfBounds)?
            .get() as usize;
        let entry_offset = entry_ix * StateEntry::<T>::RAW_BYTE_LEN;
        let entry_data = self
            .header
            .entry_table()?
            .data()
            .get(entry_offset..)
            .ok_or(ReadError::OutOfBounds)?;
        StateEntry::read(FontData::new(entry_data))
    }
}

impl<'a, T> FontRead<'a> for ExtendedStateTable<'a, T> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        Ok(Self {
            header: StxHeader::read(data)?,
            _marker: std::marker::PhantomData,
        })
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a, T> SomeTable<'a> for ExtendedStateTable<'a, T> {
    fn type_name(&self) -> &str {
        "ExtendedStateTable"
    }

    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        self.header.get_field(idx)
    }
}

pub type ExtendedStateTableU16<'a> = ExtendedStateTable<'a, u16>;

#[cfg(test)]
mod tests {
    use crate::test_helpers::BeBuffer;

    use super::*;

    #[test]
    fn lookup_format_0() {
        #[rustfmt::skip]
        let words = [
            0_u16, // format
            0, 2, 4, 6, 8, 10, 12, 14, 16, // maps all glyphs to gid * 2
        ];
        let mut buf = BeBuffer::new();
        buf = buf.extend(words);
        let lookup = LookupU16::read(buf.font_data()).unwrap();
        for gid in 0..=8 {
            assert_eq!(lookup.value(gid).unwrap(), gid * 2);
        }
        assert!(lookup.value(9).is_err());
    }

    // Taken from example 2 at https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6morx.html
    #[test]
    fn lookup_format_2() {
        #[rustfmt::skip]
        let words = [
            2_u16, // format
            6,     // unit size (6 bytes)
            3,     // number of units
            12,    // search range
            1,     // entry selector
            6,     // range shift
            22, 20, 4, // First segment, mapping glyphs 20 through 22 to class 4
            24, 23, 5, // Second segment, mapping glyph 23 and 24 to class 5
            28, 25, 6, // Third segment, mapping glyphs 25 through 28 to class 6
        ];
        let mut buf = BeBuffer::new();
        buf = buf.extend(words);
        let lookup = LookupU16::read(buf.font_data()).unwrap();
        let expected = [(20..=22, 4), (23..=24, 5), (25..=28, 6)];
        for (range, class) in expected {
            for gid in range {
                assert_eq!(lookup.value(gid).unwrap(), class);
            }
        }
        for fail in [0, 10, 19, 29, 0xFFFF] {
            assert!(lookup.value(fail).is_err());
        }
    }

    #[test]
    fn lookup_format_4() {
        #[rustfmt::skip]
        let words = [
            4_u16, // format
            6,     // unit size (6 bytes)
            3,     // number of units
            12,    // search range
            1,     // entry selector
            6,     // range shift
            22, 20, 30, // First segment, mapping glyphs 20 through 22 to mapped data at offset 30
            24, 23, 36, // Second segment, mapping glyph 23 and 24 to mapped data at offset 36
            28, 25, 40, // Third segment, mapping glyphs 25 through 28 to mapped data at offset 40
            // mapped data
            3, 2, 1,
            100, 150,
            8, 6, 7, 9
        ];
        let mut buf = BeBuffer::new();
        buf = buf.extend(words);
        let lookup = LookupU16::read(buf.font_data()).unwrap();
        let expected = [
            (20, 3),
            (21, 2),
            (22, 1),
            (23, 100),
            (24, 150),
            (25, 8),
            (26, 6),
            (27, 7),
            (28, 9),
        ];
        for (in_glyph, out_glyph) in expected {
            assert_eq!(lookup.value(in_glyph).unwrap(), out_glyph);
        }
        for fail in [0, 10, 19, 29, 0xFFFF] {
            assert!(lookup.value(fail).is_err());
        }
    }

    // Taken from example 1 at https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6morx.html
    #[test]
    fn lookup_format_6() {
        #[rustfmt::skip]
        let words = [
            6_u16, // format
            4,     // unit size (4 bytes)
            4,     // number of units
            16,    // search range
            2,     // entry selector
            0,     // range shift
            50, 600, // Input glyph 50 maps to glyph 600
            51, 601, // Input glyph 51 maps to glyph 601
            201, 602, // Input glyph 201 maps to glyph 602
            202, 900, // Input glyph 202 maps to glyph 900
        ];
        let mut buf = BeBuffer::new();
        buf = buf.extend(words);
        let lookup = LookupU16::read(buf.font_data()).unwrap();
        let expected = [(50, 600), (51, 601), (201, 602), (202, 900)];
        for (in_glyph, out_glyph) in expected {
            assert_eq!(lookup.value(in_glyph).unwrap(), out_glyph);
        }
        for fail in [0, 10, 49, 52, 203, 0xFFFF] {
            assert!(lookup.value(fail).is_err());
        }
    }

    #[test]
    fn lookup_format_8() {
        #[rustfmt::skip]
        let words = [
            8_u16, // format
            201,   // first glyph
            8,     // glyph count
            3, 8, 2, 9, 1, 200, 60, // glyphs 201..209 mapped to these values
        ];
        let mut buf = BeBuffer::new();
        buf = buf.extend(words);
        let lookup = LookupU16::read(buf.font_data()).unwrap();
        let expected = &words[3..];
        for (gid, expected) in (201..209).zip(expected) {
            assert_eq!(lookup.value(gid).unwrap(), *expected);
        }
        for fail in [0, 10, 200, 210, 0xFFFF] {
            assert!(lookup.value(fail).is_err());
        }
    }

    #[test]
    fn lookup_format_10() {
        #[rustfmt::skip]
        let words = [
            10_u16, // format
            4,      // unit size, use 4 byte values
            201,   // first glyph
            8,     // glyph count
        ];
        // glyphs 201..209 mapped to these values
        let mapped = [3_u32, 8, 2902384, 9, 1, u32::MAX, 60];
        let mut buf = BeBuffer::new();
        buf = buf.extend(words).extend(mapped);
        let lookup = LookupU32::read(buf.font_data()).unwrap();
        for (gid, expected) in (201..209).zip(mapped) {
            assert_eq!(lookup.value(gid).unwrap(), expected);
        }
        for fail in [0, 10, 200, 210, 0xFFFF] {
            assert!(lookup.value(fail).is_err());
        }
    }

    #[test]
    fn extended_state_table() {
        #[rustfmt::skip]
        let header = [
            6_u32, // number of classes
            20, // byte offset to class table
            56, // byte offset to state array
            92, // byte offset to entry array
            0, // padding
        ];
        #[rustfmt::skip]
        let class_table = [
            6_u16, // format
            4,     // unit size (4 bytes)
            5,     // number of units
            16,    // search range
            2,     // entry selector
            0,     // range shift
            50, 4, // Input glyph 50 maps to class 4
            51, 4, // Input glyph 51 maps to class 4
            80, 5, // Input glyph 80 maps to class 5
            201, 4, // Input glyph 201 maps to class 4
            202, 4, // Input glyph 202 maps to class 4
            !0, !0
        ];
        #[rustfmt::skip]
        let state_array: [u16; 18] = [
            0, 0, 0, 0, 0, 1,
            0, 0, 0, 0, 0, 1,
            0, 0, 0, 0, 2, 1,
        ];
        #[rustfmt::skip]
        let entry_table: [u16; 12] = [
            0, 0, u16::MAX, u16::MAX,
            2, 0, u16::MAX, u16::MAX,
            0, 0, u16::MAX, 0,
        ];
        let buf = BeBuffer::new()
            .extend(header)
            .extend(class_table)
            .extend(state_array)
            .extend(entry_table);
        let table = ExtendedStateTable::<ContextualData>::read(buf.font_data()).unwrap();
        // check class lookups
        let [class_50, class_80, class_201] =
            [50, 80, 201].map(|gid| table.class(GlyphId16::from(gid)).unwrap());
        assert_eq!(class_50, 4);
        assert_eq!(class_80, 5);
        assert_eq!(class_201, 4);
        // initial state
        let entry = table.entry(0, 4).unwrap();
        assert_eq!(entry.new_state, 0);
        assert_eq!(entry.payload.current_index, !0);
        // entry (state 0, class 5) should transition to state 2
        let entry = table.entry(0, 5).unwrap();
        assert_eq!(entry.new_state, 2);
        // from state 2, we transition back to state 0 when class is not 5
        // this also enables an action (payload.current_index != -1)
        let entry = table.entry(2, 4).unwrap();
        assert_eq!(entry.new_state, 0);
        assert_eq!(entry.payload.current_index, 0);
    }

    #[derive(Copy, Clone, Debug, bytemuck::AnyBitPattern)]
    #[repr(packed)]
    struct ContextualData {
        _mark_index: BigEndian<u16>,
        current_index: BigEndian<u16>,
    }

    impl FixedSize for ContextualData {
        const RAW_BYTE_LEN: usize = 4;
    }

    // Take from example at <https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6kern.html>
    // with class table trimmed to 4 glyphs
    #[test]
    fn state_table() {
        #[rustfmt::skip]
        let header = [
            7_u16, // number of classes
            10, // byte offset to class table
            18, // byte offset to state array
            40, // byte offset to entry array
            64, // byte offset to value array (unused here)
        ];
        #[rustfmt::skip]
        let class_table = [
            3_u16, // first glyph
            4, // number of glyphs
        ];
        let classes = [1u8, 2, 3, 4];
        #[rustfmt::skip]
        let state_array: [u8; 22] = [
            2, 0, 0, 2, 1, 0, 0,
            2, 0, 0, 2, 1, 0, 0,
            2, 3, 3, 2, 3, 4, 5,
            0, // padding
        ];
        #[rustfmt::skip]
        let entry_table: [u16; 10] = [
            // The first column are offsets from the beginning of the state
            // table to some position in the state array
            18, 0x8112,
            32, 0x8112,
            18, 0x0000,
            32, 0x8114,
            18, 0x8116,
        ];
        let buf = BeBuffer::new()
            .extend(header)
            .extend(class_table)
            .extend(classes)
            .extend(state_array)
            .extend(entry_table);
        let table = StateTable::read(buf.font_data()).unwrap();
        // check class lookups
        for i in 0..4u8 {
            assert_eq!(table.class(GlyphId16::from(i as u16 + 3)).unwrap(), i + 1);
        }
        // (state, class) -> (new_state, flags)
        let cases = [
            ((0, 4), (2, 0x8112)),
            ((2, 1), (2, 0x8114)),
            ((1, 3), (0, 0x0000)),
            ((2, 5), (0, 0x8116)),
        ];
        for ((state, class), (new_state, flags)) in cases {
            let entry = table.entry(state, class).unwrap();
            assert_eq!(
                entry.new_state, new_state,
                "state {state}, class {class} should map to new state {new_state} (got {})",
                entry.new_state
            );
            assert_eq!(
                entry.flags, flags,
                "state {state}, class {class} should map to flags 0x{flags:X} (got 0x{:X})",
                entry.flags
            );
        }
    }
}