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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
// THIS FILE IS AUTOGENERATED.
// Any changes to this file will be overwritten.
// For more information about how codegen works, see font-codegen/README.md

#[allow(unused_imports)]
use crate::codegen_prelude::*;

/// The [BASE](https://learn.microsoft.com/en-us/typography/opentype/spec/base) (Baseline) table
#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct BaseMarker {
    item_var_store_offset_byte_start: Option<usize>,
}

impl BaseMarker {
    fn version_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + MajorMinor::RAW_BYTE_LEN
    }
    fn horiz_axis_offset_byte_range(&self) -> Range<usize> {
        let start = self.version_byte_range().end;
        start..start + Offset16::RAW_BYTE_LEN
    }
    fn vert_axis_offset_byte_range(&self) -> Range<usize> {
        let start = self.horiz_axis_offset_byte_range().end;
        start..start + Offset16::RAW_BYTE_LEN
    }
    fn item_var_store_offset_byte_range(&self) -> Option<Range<usize>> {
        let start = self.item_var_store_offset_byte_start?;
        Some(start..start + Offset32::RAW_BYTE_LEN)
    }
}

impl TopLevelTable for Base<'_> {
    /// `BASE`
    const TAG: Tag = Tag::new(b"BASE");
}

impl<'a> FontRead<'a> for Base<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        let mut cursor = data.cursor();
        let version: MajorMinor = cursor.read()?;
        cursor.advance::<Offset16>();
        cursor.advance::<Offset16>();
        let item_var_store_offset_byte_start = version
            .compatible((1u16, 1u16))
            .then(|| cursor.position())
            .transpose()?;
        version
            .compatible((1u16, 1u16))
            .then(|| cursor.advance::<Offset32>());
        cursor.finish(BaseMarker {
            item_var_store_offset_byte_start,
        })
    }
}

/// The [BASE](https://learn.microsoft.com/en-us/typography/opentype/spec/base) (Baseline) table
pub type Base<'a> = TableRef<'a, BaseMarker>;

impl<'a> Base<'a> {
    /// (major, minor) Version for the BASE table (1,0) or (1,1)
    pub fn version(&self) -> MajorMinor {
        let range = self.shape.version_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Offset to horizontal Axis table, from beginning of BASE table (may be NULL)
    pub fn horiz_axis_offset(&self) -> Nullable<Offset16> {
        let range = self.shape.horiz_axis_offset_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Attempt to resolve [`horiz_axis_offset`][Self::horiz_axis_offset].
    pub fn horiz_axis(&self) -> Option<Result<Axis<'a>, ReadError>> {
        let data = self.data;
        self.horiz_axis_offset().resolve(data)
    }

    /// Offset to vertical Axis table, from beginning of BASE table (may be NULL)
    pub fn vert_axis_offset(&self) -> Nullable<Offset16> {
        let range = self.shape.vert_axis_offset_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Attempt to resolve [`vert_axis_offset`][Self::vert_axis_offset].
    pub fn vert_axis(&self) -> Option<Result<Axis<'a>, ReadError>> {
        let data = self.data;
        self.vert_axis_offset().resolve(data)
    }

    /// Offset to Item Variation Store table, from beginning of BASE table (may be null)
    pub fn item_var_store_offset(&self) -> Option<Nullable<Offset32>> {
        let range = self.shape.item_var_store_offset_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// Attempt to resolve [`item_var_store_offset`][Self::item_var_store_offset].
    pub fn item_var_store(&self) -> Option<Result<ItemVariationStore<'a>, ReadError>> {
        let data = self.data;
        self.item_var_store_offset().map(|x| x.resolve(data))?
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for Base<'a> {
    fn type_name(&self) -> &str {
        "Base"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        let version = self.version();
        match idx {
            0usize => Some(Field::new("version", self.version())),
            1usize => Some(Field::new(
                "horiz_axis_offset",
                FieldType::offset(self.horiz_axis_offset(), self.horiz_axis()),
            )),
            2usize => Some(Field::new(
                "vert_axis_offset",
                FieldType::offset(self.vert_axis_offset(), self.vert_axis()),
            )),
            3usize if version.compatible((1u16, 1u16)) => Some(Field::new(
                "item_var_store_offset",
                FieldType::offset(self.item_var_store_offset().unwrap(), self.item_var_store()),
            )),
            _ => None,
        }
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> std::fmt::Debug for Base<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}

/// [Axis Table](https://learn.microsoft.com/en-us/typography/opentype/spec/base#axis-tables-horizaxis-and-vertaxis)
#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct AxisMarker {}

impl AxisMarker {
    fn base_tag_list_offset_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + Offset16::RAW_BYTE_LEN
    }
    fn base_script_list_offset_byte_range(&self) -> Range<usize> {
        let start = self.base_tag_list_offset_byte_range().end;
        start..start + Offset16::RAW_BYTE_LEN
    }
}

impl<'a> FontRead<'a> for Axis<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        let mut cursor = data.cursor();
        cursor.advance::<Offset16>();
        cursor.advance::<Offset16>();
        cursor.finish(AxisMarker {})
    }
}

/// [Axis Table](https://learn.microsoft.com/en-us/typography/opentype/spec/base#axis-tables-horizaxis-and-vertaxis)
pub type Axis<'a> = TableRef<'a, AxisMarker>;

impl<'a> Axis<'a> {
    /// Offset to BaseTagList table, from beginning of Axis table (may
    /// be NULL)
    pub fn base_tag_list_offset(&self) -> Nullable<Offset16> {
        let range = self.shape.base_tag_list_offset_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Attempt to resolve [`base_tag_list_offset`][Self::base_tag_list_offset].
    pub fn base_tag_list(&self) -> Option<Result<BaseTagList<'a>, ReadError>> {
        let data = self.data;
        self.base_tag_list_offset().resolve(data)
    }

    /// Offset to BaseScriptList table, from beginning of Axis table
    pub fn base_script_list_offset(&self) -> Offset16 {
        let range = self.shape.base_script_list_offset_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Attempt to resolve [`base_script_list_offset`][Self::base_script_list_offset].
    pub fn base_script_list(&self) -> Result<BaseScriptList<'a>, ReadError> {
        let data = self.data;
        self.base_script_list_offset().resolve(data)
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for Axis<'a> {
    fn type_name(&self) -> &str {
        "Axis"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new(
                "base_tag_list_offset",
                FieldType::offset(self.base_tag_list_offset(), self.base_tag_list()),
            )),
            1usize => Some(Field::new(
                "base_script_list_offset",
                FieldType::offset(self.base_script_list_offset(), self.base_script_list()),
            )),
            _ => None,
        }
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> std::fmt::Debug for Axis<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}

/// [BaseTagList Table](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basetaglist-table)
#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct BaseTagListMarker {
    baseline_tags_byte_len: usize,
}

impl BaseTagListMarker {
    fn base_tag_count_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + u16::RAW_BYTE_LEN
    }
    fn baseline_tags_byte_range(&self) -> Range<usize> {
        let start = self.base_tag_count_byte_range().end;
        start..start + self.baseline_tags_byte_len
    }
}

impl<'a> FontRead<'a> for BaseTagList<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        let mut cursor = data.cursor();
        let base_tag_count: u16 = cursor.read()?;
        let baseline_tags_byte_len = (base_tag_count as usize)
            .checked_mul(Tag::RAW_BYTE_LEN)
            .ok_or(ReadError::OutOfBounds)?;
        cursor.advance_by(baseline_tags_byte_len);
        cursor.finish(BaseTagListMarker {
            baseline_tags_byte_len,
        })
    }
}

/// [BaseTagList Table](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basetaglist-table)
pub type BaseTagList<'a> = TableRef<'a, BaseTagListMarker>;

impl<'a> BaseTagList<'a> {
    /// Number of baseline identification tags in this text direction
    /// — may be zero (0)
    pub fn base_tag_count(&self) -> u16 {
        let range = self.shape.base_tag_count_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Array of 4-byte baseline identification tags — must be in
    /// alphabetical order
    pub fn baseline_tags(&self) -> &'a [BigEndian<Tag>] {
        let range = self.shape.baseline_tags_byte_range();
        self.data.read_array(range).unwrap()
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for BaseTagList<'a> {
    fn type_name(&self) -> &str {
        "BaseTagList"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new("base_tag_count", self.base_tag_count())),
            1usize => Some(Field::new("baseline_tags", self.baseline_tags())),
            _ => None,
        }
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> std::fmt::Debug for BaseTagList<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}

/// [BaseScriptList Table](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basescriptlist-table)
#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct BaseScriptListMarker {
    base_script_records_byte_len: usize,
}

impl BaseScriptListMarker {
    fn base_script_count_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + u16::RAW_BYTE_LEN
    }
    fn base_script_records_byte_range(&self) -> Range<usize> {
        let start = self.base_script_count_byte_range().end;
        start..start + self.base_script_records_byte_len
    }
}

impl<'a> FontRead<'a> for BaseScriptList<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        let mut cursor = data.cursor();
        let base_script_count: u16 = cursor.read()?;
        let base_script_records_byte_len = (base_script_count as usize)
            .checked_mul(BaseScriptRecord::RAW_BYTE_LEN)
            .ok_or(ReadError::OutOfBounds)?;
        cursor.advance_by(base_script_records_byte_len);
        cursor.finish(BaseScriptListMarker {
            base_script_records_byte_len,
        })
    }
}

/// [BaseScriptList Table](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basescriptlist-table)
pub type BaseScriptList<'a> = TableRef<'a, BaseScriptListMarker>;

impl<'a> BaseScriptList<'a> {
    /// Number of BaseScriptRecords defined
    pub fn base_script_count(&self) -> u16 {
        let range = self.shape.base_script_count_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Array of BaseScriptRecords, in alphabetical order by
    /// baseScriptTag
    pub fn base_script_records(&self) -> &'a [BaseScriptRecord] {
        let range = self.shape.base_script_records_byte_range();
        self.data.read_array(range).unwrap()
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for BaseScriptList<'a> {
    fn type_name(&self) -> &str {
        "BaseScriptList"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new("base_script_count", self.base_script_count())),
            1usize => Some(Field::new(
                "base_script_records",
                traversal::FieldType::array_of_records(
                    stringify!(BaseScriptRecord),
                    self.base_script_records(),
                    self.offset_data(),
                ),
            )),
            _ => None,
        }
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> std::fmt::Debug for BaseScriptList<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}

/// [BaseScriptRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basescriptrecord)
#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
#[repr(C)]
#[repr(packed)]
pub struct BaseScriptRecord {
    /// 4-byte script identification tag
    pub base_script_tag: BigEndian<Tag>,
    /// Offset to BaseScript table, from beginning of BaseScriptList
    pub base_script_offset: BigEndian<Offset16>,
}

impl BaseScriptRecord {
    /// 4-byte script identification tag
    pub fn base_script_tag(&self) -> Tag {
        self.base_script_tag.get()
    }

    /// Offset to BaseScript table, from beginning of BaseScriptList
    pub fn base_script_offset(&self) -> Offset16 {
        self.base_script_offset.get()
    }

    /// Offset to BaseScript table, from beginning of BaseScriptList
    ///
    /// The `data` argument should be retrieved from the parent table
    /// By calling its `offset_data` method.
    pub fn base_script<'a>(&self, data: FontData<'a>) -> Result<BaseScript<'a>, ReadError> {
        self.base_script_offset().resolve(data)
    }
}

impl FixedSize for BaseScriptRecord {
    const RAW_BYTE_LEN: usize = Tag::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN;
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeRecord<'a> for BaseScriptRecord {
    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
        RecordResolver {
            name: "BaseScriptRecord",
            get_field: Box::new(move |idx, _data| match idx {
                0usize => Some(Field::new("base_script_tag", self.base_script_tag())),
                1usize => Some(Field::new(
                    "base_script_offset",
                    FieldType::offset(self.base_script_offset(), self.base_script(_data)),
                )),
                _ => None,
            }),
            data,
        }
    }
}

/// [BaseScript Table](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basescript-table)
#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct BaseScriptMarker {
    base_lang_sys_records_byte_len: usize,
}

impl BaseScriptMarker {
    fn base_values_offset_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + Offset16::RAW_BYTE_LEN
    }
    fn default_min_max_offset_byte_range(&self) -> Range<usize> {
        let start = self.base_values_offset_byte_range().end;
        start..start + Offset16::RAW_BYTE_LEN
    }
    fn base_lang_sys_count_byte_range(&self) -> Range<usize> {
        let start = self.default_min_max_offset_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn base_lang_sys_records_byte_range(&self) -> Range<usize> {
        let start = self.base_lang_sys_count_byte_range().end;
        start..start + self.base_lang_sys_records_byte_len
    }
}

impl<'a> FontRead<'a> for BaseScript<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        let mut cursor = data.cursor();
        cursor.advance::<Offset16>();
        cursor.advance::<Offset16>();
        let base_lang_sys_count: u16 = cursor.read()?;
        let base_lang_sys_records_byte_len = (base_lang_sys_count as usize)
            .checked_mul(BaseLangSysRecord::RAW_BYTE_LEN)
            .ok_or(ReadError::OutOfBounds)?;
        cursor.advance_by(base_lang_sys_records_byte_len);
        cursor.finish(BaseScriptMarker {
            base_lang_sys_records_byte_len,
        })
    }
}

/// [BaseScript Table](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basescript-table)
pub type BaseScript<'a> = TableRef<'a, BaseScriptMarker>;

impl<'a> BaseScript<'a> {
    /// Offset to BaseValues table, from beginning of BaseScript table (may be NULL)
    pub fn base_values_offset(&self) -> Nullable<Offset16> {
        let range = self.shape.base_values_offset_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Attempt to resolve [`base_values_offset`][Self::base_values_offset].
    pub fn base_values(&self) -> Option<Result<BaseValues<'a>, ReadError>> {
        let data = self.data;
        self.base_values_offset().resolve(data)
    }

    /// Offset to MinMax table, from beginning of BaseScript table (may be NULL)
    pub fn default_min_max_offset(&self) -> Nullable<Offset16> {
        let range = self.shape.default_min_max_offset_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Attempt to resolve [`default_min_max_offset`][Self::default_min_max_offset].
    pub fn default_min_max(&self) -> Option<Result<MinMax<'a>, ReadError>> {
        let data = self.data;
        self.default_min_max_offset().resolve(data)
    }

    /// Number of BaseLangSysRecords defined — may be zero (0)
    pub fn base_lang_sys_count(&self) -> u16 {
        let range = self.shape.base_lang_sys_count_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Array of BaseLangSysRecords, in alphabetical order by
    /// BaseLangSysTag
    pub fn base_lang_sys_records(&self) -> &'a [BaseLangSysRecord] {
        let range = self.shape.base_lang_sys_records_byte_range();
        self.data.read_array(range).unwrap()
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for BaseScript<'a> {
    fn type_name(&self) -> &str {
        "BaseScript"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new(
                "base_values_offset",
                FieldType::offset(self.base_values_offset(), self.base_values()),
            )),
            1usize => Some(Field::new(
                "default_min_max_offset",
                FieldType::offset(self.default_min_max_offset(), self.default_min_max()),
            )),
            2usize => Some(Field::new(
                "base_lang_sys_count",
                self.base_lang_sys_count(),
            )),
            3usize => Some(Field::new(
                "base_lang_sys_records",
                traversal::FieldType::array_of_records(
                    stringify!(BaseLangSysRecord),
                    self.base_lang_sys_records(),
                    self.offset_data(),
                ),
            )),
            _ => None,
        }
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> std::fmt::Debug for BaseScript<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}

/// [BaseLangSysRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/base#baselangsysrecord)
#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
#[repr(C)]
#[repr(packed)]
pub struct BaseLangSysRecord {
    /// 4-byte language system identification tag
    pub base_lang_sys_tag: BigEndian<Tag>,
    /// Offset to MinMax table, from beginning of BaseScript table
    pub min_max_offset: BigEndian<Offset16>,
}

impl BaseLangSysRecord {
    /// 4-byte language system identification tag
    pub fn base_lang_sys_tag(&self) -> Tag {
        self.base_lang_sys_tag.get()
    }

    /// Offset to MinMax table, from beginning of BaseScript table
    pub fn min_max_offset(&self) -> Offset16 {
        self.min_max_offset.get()
    }

    /// Offset to MinMax table, from beginning of BaseScript table
    ///
    /// The `data` argument should be retrieved from the parent table
    /// By calling its `offset_data` method.
    pub fn min_max<'a>(&self, data: FontData<'a>) -> Result<MinMax<'a>, ReadError> {
        self.min_max_offset().resolve(data)
    }
}

impl FixedSize for BaseLangSysRecord {
    const RAW_BYTE_LEN: usize = Tag::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN;
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeRecord<'a> for BaseLangSysRecord {
    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
        RecordResolver {
            name: "BaseLangSysRecord",
            get_field: Box::new(move |idx, _data| match idx {
                0usize => Some(Field::new("base_lang_sys_tag", self.base_lang_sys_tag())),
                1usize => Some(Field::new(
                    "min_max_offset",
                    FieldType::offset(self.min_max_offset(), self.min_max(_data)),
                )),
                _ => None,
            }),
            data,
        }
    }
}

/// [BaseValues](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basevalues-table) table
#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct BaseValuesMarker {
    base_coord_offsets_byte_len: usize,
}

impl BaseValuesMarker {
    fn default_baseline_index_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + u16::RAW_BYTE_LEN
    }
    fn base_coord_count_byte_range(&self) -> Range<usize> {
        let start = self.default_baseline_index_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn base_coord_offsets_byte_range(&self) -> Range<usize> {
        let start = self.base_coord_count_byte_range().end;
        start..start + self.base_coord_offsets_byte_len
    }
}

impl<'a> FontRead<'a> for BaseValues<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        let mut cursor = data.cursor();
        cursor.advance::<u16>();
        let base_coord_count: u16 = cursor.read()?;
        let base_coord_offsets_byte_len = (base_coord_count as usize)
            .checked_mul(Offset16::RAW_BYTE_LEN)
            .ok_or(ReadError::OutOfBounds)?;
        cursor.advance_by(base_coord_offsets_byte_len);
        cursor.finish(BaseValuesMarker {
            base_coord_offsets_byte_len,
        })
    }
}

/// [BaseValues](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basevalues-table) table
pub type BaseValues<'a> = TableRef<'a, BaseValuesMarker>;

impl<'a> BaseValues<'a> {
    /// Index number of default baseline for this script — equals
    /// index position of baseline tag in baselineTags array of the
    /// BaseTagList
    pub fn default_baseline_index(&self) -> u16 {
        let range = self.shape.default_baseline_index_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Number of BaseCoord tables defined — should equal
    /// baseTagCount in the BaseTagList
    pub fn base_coord_count(&self) -> u16 {
        let range = self.shape.base_coord_count_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Array of offsets to BaseCoord tables, from beginning of
    /// BaseValues table — order matches baselineTags array in the
    /// BaseTagList
    pub fn base_coord_offsets(&self) -> &'a [BigEndian<Offset16>] {
        let range = self.shape.base_coord_offsets_byte_range();
        self.data.read_array(range).unwrap()
    }

    /// A dynamically resolving wrapper for [`base_coord_offsets`][Self::base_coord_offsets].
    pub fn base_coords(&self) -> ArrayOfOffsets<'a, BaseCoord<'a>, Offset16> {
        let data = self.data;
        let offsets = self.base_coord_offsets();
        ArrayOfOffsets::new(offsets, data, ())
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for BaseValues<'a> {
    fn type_name(&self) -> &str {
        "BaseValues"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new(
                "default_baseline_index",
                self.default_baseline_index(),
            )),
            1usize => Some(Field::new("base_coord_count", self.base_coord_count())),
            2usize => Some({
                let data = self.data;
                Field::new(
                    "base_coord_offsets",
                    FieldType::array_of_offsets(
                        better_type_name::<BaseCoord>(),
                        self.base_coord_offsets(),
                        move |off| {
                            let target = off.get().resolve::<BaseCoord>(data);
                            FieldType::offset(off.get(), target)
                        },
                    ),
                )
            }),
            _ => None,
        }
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> std::fmt::Debug for BaseValues<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}

/// [MinMax](https://learn.microsoft.com/en-us/typography/opentype/spec/base#minmax-table) table
#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct MinMaxMarker {
    feat_min_max_records_byte_len: usize,
}

impl MinMaxMarker {
    fn min_coord_offset_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + Offset16::RAW_BYTE_LEN
    }
    fn max_coord_offset_byte_range(&self) -> Range<usize> {
        let start = self.min_coord_offset_byte_range().end;
        start..start + Offset16::RAW_BYTE_LEN
    }
    fn feat_min_max_count_byte_range(&self) -> Range<usize> {
        let start = self.max_coord_offset_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn feat_min_max_records_byte_range(&self) -> Range<usize> {
        let start = self.feat_min_max_count_byte_range().end;
        start..start + self.feat_min_max_records_byte_len
    }
}

impl<'a> FontRead<'a> for MinMax<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        let mut cursor = data.cursor();
        cursor.advance::<Offset16>();
        cursor.advance::<Offset16>();
        let feat_min_max_count: u16 = cursor.read()?;
        let feat_min_max_records_byte_len = (feat_min_max_count as usize)
            .checked_mul(FeatMinMaxRecord::RAW_BYTE_LEN)
            .ok_or(ReadError::OutOfBounds)?;
        cursor.advance_by(feat_min_max_records_byte_len);
        cursor.finish(MinMaxMarker {
            feat_min_max_records_byte_len,
        })
    }
}

/// [MinMax](https://learn.microsoft.com/en-us/typography/opentype/spec/base#minmax-table) table
pub type MinMax<'a> = TableRef<'a, MinMaxMarker>;

impl<'a> MinMax<'a> {
    /// Offset to BaseCoord table that defines the minimum extent
    /// value, from the beginning of MinMax table (may be NULL)
    pub fn min_coord_offset(&self) -> Nullable<Offset16> {
        let range = self.shape.min_coord_offset_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Attempt to resolve [`min_coord_offset`][Self::min_coord_offset].
    pub fn min_coord(&self) -> Option<Result<BaseCoord<'a>, ReadError>> {
        let data = self.data;
        self.min_coord_offset().resolve(data)
    }

    /// Offset to BaseCoord table that defines maximum extent value,
    /// from the beginning of MinMax table (may be NULL)
    pub fn max_coord_offset(&self) -> Nullable<Offset16> {
        let range = self.shape.max_coord_offset_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Attempt to resolve [`max_coord_offset`][Self::max_coord_offset].
    pub fn max_coord(&self) -> Option<Result<BaseCoord<'a>, ReadError>> {
        let data = self.data;
        self.max_coord_offset().resolve(data)
    }

    /// Number of FeatMinMaxRecords — may be zero (0)
    pub fn feat_min_max_count(&self) -> u16 {
        let range = self.shape.feat_min_max_count_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Array of FeatMinMaxRecords, in alphabetical order by
    /// featureTableTag
    pub fn feat_min_max_records(&self) -> &'a [FeatMinMaxRecord] {
        let range = self.shape.feat_min_max_records_byte_range();
        self.data.read_array(range).unwrap()
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for MinMax<'a> {
    fn type_name(&self) -> &str {
        "MinMax"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new(
                "min_coord_offset",
                FieldType::offset(self.min_coord_offset(), self.min_coord()),
            )),
            1usize => Some(Field::new(
                "max_coord_offset",
                FieldType::offset(self.max_coord_offset(), self.max_coord()),
            )),
            2usize => Some(Field::new("feat_min_max_count", self.feat_min_max_count())),
            3usize => Some(Field::new(
                "feat_min_max_records",
                traversal::FieldType::array_of_records(
                    stringify!(FeatMinMaxRecord),
                    self.feat_min_max_records(),
                    self.offset_data(),
                ),
            )),
            _ => None,
        }
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> std::fmt::Debug for MinMax<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}

/// [FeatMinMaxRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/base#baselangsysrecord)
#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
#[repr(C)]
#[repr(packed)]
pub struct FeatMinMaxRecord {
    /// 4-byte feature identification tag — must match feature tag in
    /// FeatureList
    pub feature_table_tag: BigEndian<Tag>,
    /// Offset to BaseCoord table that defines the minimum extent
    /// value, from beginning of MinMax table (may be NULL)
    pub min_coord_offset: BigEndian<Nullable<Offset16>>,
    /// Offset to BaseCoord table that defines the maximum extent
    /// value, from beginning of MinMax table (may be NULL)
    pub max_coord_offset: BigEndian<Nullable<Offset16>>,
}

impl FeatMinMaxRecord {
    /// 4-byte feature identification tag — must match feature tag in
    /// FeatureList
    pub fn feature_table_tag(&self) -> Tag {
        self.feature_table_tag.get()
    }

    /// Offset to BaseCoord table that defines the minimum extent
    /// value, from beginning of MinMax table (may be NULL)
    pub fn min_coord_offset(&self) -> Nullable<Offset16> {
        self.min_coord_offset.get()
    }

    /// Offset to BaseCoord table that defines the minimum extent
    /// value, from beginning of MinMax table (may be NULL)
    ///
    /// The `data` argument should be retrieved from the parent table
    /// By calling its `offset_data` method.
    pub fn min_coord<'a>(&self, data: FontData<'a>) -> Option<Result<MinMax<'a>, ReadError>> {
        self.min_coord_offset().resolve(data)
    }

    /// Offset to BaseCoord table that defines the maximum extent
    /// value, from beginning of MinMax table (may be NULL)
    pub fn max_coord_offset(&self) -> Nullable<Offset16> {
        self.max_coord_offset.get()
    }

    /// Offset to BaseCoord table that defines the maximum extent
    /// value, from beginning of MinMax table (may be NULL)
    ///
    /// The `data` argument should be retrieved from the parent table
    /// By calling its `offset_data` method.
    pub fn max_coord<'a>(&self, data: FontData<'a>) -> Option<Result<MinMax<'a>, ReadError>> {
        self.max_coord_offset().resolve(data)
    }
}

impl FixedSize for FeatMinMaxRecord {
    const RAW_BYTE_LEN: usize = Tag::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN;
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeRecord<'a> for FeatMinMaxRecord {
    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
        RecordResolver {
            name: "FeatMinMaxRecord",
            get_field: Box::new(move |idx, _data| match idx {
                0usize => Some(Field::new("feature_table_tag", self.feature_table_tag())),
                1usize => Some(Field::new(
                    "min_coord_offset",
                    FieldType::offset(self.min_coord_offset(), self.min_coord(_data)),
                )),
                2usize => Some(Field::new(
                    "max_coord_offset",
                    FieldType::offset(self.max_coord_offset(), self.max_coord(_data)),
                )),
                _ => None,
            }),
            data,
        }
    }
}

#[derive(Clone)]
pub enum BaseCoord<'a> {
    Format1(BaseCoordFormat1<'a>),
    Format2(BaseCoordFormat2<'a>),
    Format3(BaseCoordFormat3<'a>),
}

impl<'a> BaseCoord<'a> {
    ///Return the `FontData` used to resolve offsets for this table.
    pub fn offset_data(&self) -> FontData<'a> {
        match self {
            Self::Format1(item) => item.offset_data(),
            Self::Format2(item) => item.offset_data(),
            Self::Format3(item) => item.offset_data(),
        }
    }

    /// Format identifier — format = 1
    pub fn base_coord_format(&self) -> u16 {
        match self {
            Self::Format1(item) => item.base_coord_format(),
            Self::Format2(item) => item.base_coord_format(),
            Self::Format3(item) => item.base_coord_format(),
        }
    }

    /// X or Y value, in design units
    pub fn coordinate(&self) -> i16 {
        match self {
            Self::Format1(item) => item.coordinate(),
            Self::Format2(item) => item.coordinate(),
            Self::Format3(item) => item.coordinate(),
        }
    }
}

impl<'a> FontRead<'a> for BaseCoord<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        let format: u16 = data.read_at(0usize)?;
        match format {
            BaseCoordFormat1Marker::FORMAT => Ok(Self::Format1(FontRead::read(data)?)),
            BaseCoordFormat2Marker::FORMAT => Ok(Self::Format2(FontRead::read(data)?)),
            BaseCoordFormat3Marker::FORMAT => Ok(Self::Format3(FontRead::read(data)?)),
            other => Err(ReadError::InvalidFormat(other.into())),
        }
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> BaseCoord<'a> {
    fn dyn_inner<'b>(&'b self) -> &'b dyn SomeTable<'a> {
        match self {
            Self::Format1(table) => table,
            Self::Format2(table) => table,
            Self::Format3(table) => table,
        }
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> std::fmt::Debug for BaseCoord<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.dyn_inner().fmt(f)
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for BaseCoord<'a> {
    fn type_name(&self) -> &str {
        self.dyn_inner().type_name()
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        self.dyn_inner().get_field(idx)
    }
}

impl Format<u16> for BaseCoordFormat1Marker {
    const FORMAT: u16 = 1;
}

/// [BaseCoordFormat1](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basecoord-format-1)
#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct BaseCoordFormat1Marker {}

impl BaseCoordFormat1Marker {
    fn base_coord_format_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + u16::RAW_BYTE_LEN
    }
    fn coordinate_byte_range(&self) -> Range<usize> {
        let start = self.base_coord_format_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
}

impl<'a> FontRead<'a> for BaseCoordFormat1<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        let mut cursor = data.cursor();
        cursor.advance::<u16>();
        cursor.advance::<i16>();
        cursor.finish(BaseCoordFormat1Marker {})
    }
}

/// [BaseCoordFormat1](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basecoord-format-1)
pub type BaseCoordFormat1<'a> = TableRef<'a, BaseCoordFormat1Marker>;

impl<'a> BaseCoordFormat1<'a> {
    /// Format identifier — format = 1
    pub fn base_coord_format(&self) -> u16 {
        let range = self.shape.base_coord_format_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// X or Y value, in design units
    pub fn coordinate(&self) -> i16 {
        let range = self.shape.coordinate_byte_range();
        self.data.read_at(range.start).unwrap()
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for BaseCoordFormat1<'a> {
    fn type_name(&self) -> &str {
        "BaseCoordFormat1"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new("base_coord_format", self.base_coord_format())),
            1usize => Some(Field::new("coordinate", self.coordinate())),
            _ => None,
        }
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> std::fmt::Debug for BaseCoordFormat1<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}

impl Format<u16> for BaseCoordFormat2Marker {
    const FORMAT: u16 = 2;
}

/// [BaseCoordFormat2](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basecoord-format-2)
#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct BaseCoordFormat2Marker {}

impl BaseCoordFormat2Marker {
    fn base_coord_format_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + u16::RAW_BYTE_LEN
    }
    fn coordinate_byte_range(&self) -> Range<usize> {
        let start = self.base_coord_format_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn reference_glyph_byte_range(&self) -> Range<usize> {
        let start = self.coordinate_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn base_coord_point_byte_range(&self) -> Range<usize> {
        let start = self.reference_glyph_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
}

impl<'a> FontRead<'a> for BaseCoordFormat2<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        let mut cursor = data.cursor();
        cursor.advance::<u16>();
        cursor.advance::<i16>();
        cursor.advance::<u16>();
        cursor.advance::<u16>();
        cursor.finish(BaseCoordFormat2Marker {})
    }
}

/// [BaseCoordFormat2](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basecoord-format-2)
pub type BaseCoordFormat2<'a> = TableRef<'a, BaseCoordFormat2Marker>;

impl<'a> BaseCoordFormat2<'a> {
    /// Format identifier — format = 2
    pub fn base_coord_format(&self) -> u16 {
        let range = self.shape.base_coord_format_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// X or Y value, in design units
    pub fn coordinate(&self) -> i16 {
        let range = self.shape.coordinate_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Glyph ID of control glyph
    pub fn reference_glyph(&self) -> u16 {
        let range = self.shape.reference_glyph_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Index of contour point on the reference glyph
    pub fn base_coord_point(&self) -> u16 {
        let range = self.shape.base_coord_point_byte_range();
        self.data.read_at(range.start).unwrap()
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for BaseCoordFormat2<'a> {
    fn type_name(&self) -> &str {
        "BaseCoordFormat2"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new("base_coord_format", self.base_coord_format())),
            1usize => Some(Field::new("coordinate", self.coordinate())),
            2usize => Some(Field::new("reference_glyph", self.reference_glyph())),
            3usize => Some(Field::new("base_coord_point", self.base_coord_point())),
            _ => None,
        }
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> std::fmt::Debug for BaseCoordFormat2<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}

impl Format<u16> for BaseCoordFormat3Marker {
    const FORMAT: u16 = 3;
}

/// [BaseCoordFormat3](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basecoord-format-3)
#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct BaseCoordFormat3Marker {}

impl BaseCoordFormat3Marker {
    fn base_coord_format_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + u16::RAW_BYTE_LEN
    }
    fn coordinate_byte_range(&self) -> Range<usize> {
        let start = self.base_coord_format_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn device_offset_byte_range(&self) -> Range<usize> {
        let start = self.coordinate_byte_range().end;
        start..start + Offset16::RAW_BYTE_LEN
    }
}

impl<'a> FontRead<'a> for BaseCoordFormat3<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        let mut cursor = data.cursor();
        cursor.advance::<u16>();
        cursor.advance::<i16>();
        cursor.advance::<Offset16>();
        cursor.finish(BaseCoordFormat3Marker {})
    }
}

/// [BaseCoordFormat3](https://learn.microsoft.com/en-us/typography/opentype/spec/base#basecoord-format-3)
pub type BaseCoordFormat3<'a> = TableRef<'a, BaseCoordFormat3Marker>;

impl<'a> BaseCoordFormat3<'a> {
    /// Format identifier — format = 3
    pub fn base_coord_format(&self) -> u16 {
        let range = self.shape.base_coord_format_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// X or Y value, in design units
    pub fn coordinate(&self) -> i16 {
        let range = self.shape.coordinate_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Offset to Device table (non-variable font) / Variation Index
    /// table (variable font) for X or Y value, from beginning of
    /// BaseCoord table (may be NULL).
    pub fn device_offset(&self) -> Nullable<Offset16> {
        let range = self.shape.device_offset_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Attempt to resolve [`device_offset`][Self::device_offset].
    pub fn device(&self) -> Option<Result<DeviceOrVariationIndex<'a>, ReadError>> {
        let data = self.data;
        self.device_offset().resolve(data)
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for BaseCoordFormat3<'a> {
    fn type_name(&self) -> &str {
        "BaseCoordFormat3"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new("base_coord_format", self.base_coord_format())),
            1usize => Some(Field::new("coordinate", self.coordinate())),
            2usize => Some(Field::new(
                "device_offset",
                FieldType::offset(self.device_offset(), self.device()),
            )),
            _ => None,
        }
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> std::fmt::Debug for BaseCoordFormat3<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}