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
// 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::*;

/// [`OS/2`](https://docs.microsoft.com/en-us/typography/opentype/spec/os2)
#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct Os2Marker {
    panose_10_byte_len: usize,
    ul_code_page_range_1_byte_start: Option<usize>,
    ul_code_page_range_2_byte_start: Option<usize>,
    sx_height_byte_start: Option<usize>,
    s_cap_height_byte_start: Option<usize>,
    us_default_char_byte_start: Option<usize>,
    us_break_char_byte_start: Option<usize>,
    us_max_context_byte_start: Option<usize>,
    us_lower_optical_point_size_byte_start: Option<usize>,
    us_upper_optical_point_size_byte_start: Option<usize>,
}

impl Os2Marker {
    fn version_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + u16::RAW_BYTE_LEN
    }
    fn x_avg_char_width_byte_range(&self) -> Range<usize> {
        let start = self.version_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn us_weight_class_byte_range(&self) -> Range<usize> {
        let start = self.x_avg_char_width_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn us_width_class_byte_range(&self) -> Range<usize> {
        let start = self.us_weight_class_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn fs_type_byte_range(&self) -> Range<usize> {
        let start = self.us_width_class_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn y_subscript_x_size_byte_range(&self) -> Range<usize> {
        let start = self.fs_type_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn y_subscript_y_size_byte_range(&self) -> Range<usize> {
        let start = self.y_subscript_x_size_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn y_subscript_x_offset_byte_range(&self) -> Range<usize> {
        let start = self.y_subscript_y_size_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn y_subscript_y_offset_byte_range(&self) -> Range<usize> {
        let start = self.y_subscript_x_offset_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn y_superscript_x_size_byte_range(&self) -> Range<usize> {
        let start = self.y_subscript_y_offset_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn y_superscript_y_size_byte_range(&self) -> Range<usize> {
        let start = self.y_superscript_x_size_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn y_superscript_x_offset_byte_range(&self) -> Range<usize> {
        let start = self.y_superscript_y_size_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn y_superscript_y_offset_byte_range(&self) -> Range<usize> {
        let start = self.y_superscript_x_offset_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn y_strikeout_size_byte_range(&self) -> Range<usize> {
        let start = self.y_superscript_y_offset_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn y_strikeout_position_byte_range(&self) -> Range<usize> {
        let start = self.y_strikeout_size_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn s_family_class_byte_range(&self) -> Range<usize> {
        let start = self.y_strikeout_position_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn panose_10_byte_range(&self) -> Range<usize> {
        let start = self.s_family_class_byte_range().end;
        start..start + self.panose_10_byte_len
    }
    fn ul_unicode_range_1_byte_range(&self) -> Range<usize> {
        let start = self.panose_10_byte_range().end;
        start..start + u32::RAW_BYTE_LEN
    }
    fn ul_unicode_range_2_byte_range(&self) -> Range<usize> {
        let start = self.ul_unicode_range_1_byte_range().end;
        start..start + u32::RAW_BYTE_LEN
    }
    fn ul_unicode_range_3_byte_range(&self) -> Range<usize> {
        let start = self.ul_unicode_range_2_byte_range().end;
        start..start + u32::RAW_BYTE_LEN
    }
    fn ul_unicode_range_4_byte_range(&self) -> Range<usize> {
        let start = self.ul_unicode_range_3_byte_range().end;
        start..start + u32::RAW_BYTE_LEN
    }
    fn ach_vend_id_byte_range(&self) -> Range<usize> {
        let start = self.ul_unicode_range_4_byte_range().end;
        start..start + Tag::RAW_BYTE_LEN
    }
    fn fs_selection_byte_range(&self) -> Range<usize> {
        let start = self.ach_vend_id_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn us_first_char_index_byte_range(&self) -> Range<usize> {
        let start = self.fs_selection_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn us_last_char_index_byte_range(&self) -> Range<usize> {
        let start = self.us_first_char_index_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn s_typo_ascender_byte_range(&self) -> Range<usize> {
        let start = self.us_last_char_index_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn s_typo_descender_byte_range(&self) -> Range<usize> {
        let start = self.s_typo_ascender_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn s_typo_line_gap_byte_range(&self) -> Range<usize> {
        let start = self.s_typo_descender_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn us_win_ascent_byte_range(&self) -> Range<usize> {
        let start = self.s_typo_line_gap_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn us_win_descent_byte_range(&self) -> Range<usize> {
        let start = self.us_win_ascent_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn ul_code_page_range_1_byte_range(&self) -> Option<Range<usize>> {
        let start = self.ul_code_page_range_1_byte_start?;
        Some(start..start + u32::RAW_BYTE_LEN)
    }
    fn ul_code_page_range_2_byte_range(&self) -> Option<Range<usize>> {
        let start = self.ul_code_page_range_2_byte_start?;
        Some(start..start + u32::RAW_BYTE_LEN)
    }
    fn sx_height_byte_range(&self) -> Option<Range<usize>> {
        let start = self.sx_height_byte_start?;
        Some(start..start + i16::RAW_BYTE_LEN)
    }
    fn s_cap_height_byte_range(&self) -> Option<Range<usize>> {
        let start = self.s_cap_height_byte_start?;
        Some(start..start + i16::RAW_BYTE_LEN)
    }
    fn us_default_char_byte_range(&self) -> Option<Range<usize>> {
        let start = self.us_default_char_byte_start?;
        Some(start..start + u16::RAW_BYTE_LEN)
    }
    fn us_break_char_byte_range(&self) -> Option<Range<usize>> {
        let start = self.us_break_char_byte_start?;
        Some(start..start + u16::RAW_BYTE_LEN)
    }
    fn us_max_context_byte_range(&self) -> Option<Range<usize>> {
        let start = self.us_max_context_byte_start?;
        Some(start..start + u16::RAW_BYTE_LEN)
    }
    fn us_lower_optical_point_size_byte_range(&self) -> Option<Range<usize>> {
        let start = self.us_lower_optical_point_size_byte_start?;
        Some(start..start + u16::RAW_BYTE_LEN)
    }
    fn us_upper_optical_point_size_byte_range(&self) -> Option<Range<usize>> {
        let start = self.us_upper_optical_point_size_byte_start?;
        Some(start..start + u16::RAW_BYTE_LEN)
    }
}

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

impl<'a> FontRead<'a> for Os2<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        let mut cursor = data.cursor();
        let version: u16 = cursor.read()?;
        cursor.advance::<i16>();
        cursor.advance::<u16>();
        cursor.advance::<u16>();
        cursor.advance::<u16>();
        cursor.advance::<i16>();
        cursor.advance::<i16>();
        cursor.advance::<i16>();
        cursor.advance::<i16>();
        cursor.advance::<i16>();
        cursor.advance::<i16>();
        cursor.advance::<i16>();
        cursor.advance::<i16>();
        cursor.advance::<i16>();
        cursor.advance::<i16>();
        cursor.advance::<i16>();
        let panose_10_byte_len = 10_usize * u8::RAW_BYTE_LEN;
        cursor.advance_by(panose_10_byte_len);
        cursor.advance::<u32>();
        cursor.advance::<u32>();
        cursor.advance::<u32>();
        cursor.advance::<u32>();
        cursor.advance::<Tag>();
        cursor.advance::<u16>();
        cursor.advance::<u16>();
        cursor.advance::<u16>();
        cursor.advance::<i16>();
        cursor.advance::<i16>();
        cursor.advance::<i16>();
        cursor.advance::<u16>();
        cursor.advance::<u16>();
        let ul_code_page_range_1_byte_start = version
            .compatible(1)
            .then(|| cursor.position())
            .transpose()?;
        version.compatible(1).then(|| cursor.advance::<u32>());
        let ul_code_page_range_2_byte_start = version
            .compatible(1)
            .then(|| cursor.position())
            .transpose()?;
        version.compatible(1).then(|| cursor.advance::<u32>());
        let sx_height_byte_start = version
            .compatible(2)
            .then(|| cursor.position())
            .transpose()?;
        version.compatible(2).then(|| cursor.advance::<i16>());
        let s_cap_height_byte_start = version
            .compatible(2)
            .then(|| cursor.position())
            .transpose()?;
        version.compatible(2).then(|| cursor.advance::<i16>());
        let us_default_char_byte_start = version
            .compatible(2)
            .then(|| cursor.position())
            .transpose()?;
        version.compatible(2).then(|| cursor.advance::<u16>());
        let us_break_char_byte_start = version
            .compatible(2)
            .then(|| cursor.position())
            .transpose()?;
        version.compatible(2).then(|| cursor.advance::<u16>());
        let us_max_context_byte_start = version
            .compatible(2)
            .then(|| cursor.position())
            .transpose()?;
        version.compatible(2).then(|| cursor.advance::<u16>());
        let us_lower_optical_point_size_byte_start = version
            .compatible(5)
            .then(|| cursor.position())
            .transpose()?;
        version.compatible(5).then(|| cursor.advance::<u16>());
        let us_upper_optical_point_size_byte_start = version
            .compatible(5)
            .then(|| cursor.position())
            .transpose()?;
        version.compatible(5).then(|| cursor.advance::<u16>());
        cursor.finish(Os2Marker {
            panose_10_byte_len,
            ul_code_page_range_1_byte_start,
            ul_code_page_range_2_byte_start,
            sx_height_byte_start,
            s_cap_height_byte_start,
            us_default_char_byte_start,
            us_break_char_byte_start,
            us_max_context_byte_start,
            us_lower_optical_point_size_byte_start,
            us_upper_optical_point_size_byte_start,
        })
    }
}

/// [`OS/2`](https://docs.microsoft.com/en-us/typography/opentype/spec/os2)
pub type Os2<'a> = TableRef<'a, Os2Marker>;

impl<'a> Os2<'a> {
    pub fn version(&self) -> u16 {
        let range = self.shape.version_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// [Average weighted escapement](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#xavgcharwidth).
    ///
    /// The Average Character Width parameter specifies the arithmetic average
    /// of the escapement (width) of all non-zero width glyphs in the font.
    pub fn x_avg_char_width(&self) -> i16 {
        let range = self.shape.x_avg_char_width_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// [Weight class](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#usweightclass).
    ///
    /// Indicates the visual weight (degree of blackness or thickness of
    /// strokes) of the characters in the font. Values from 1 to 1000 are valid.
    pub fn us_weight_class(&self) -> u16 {
        let range = self.shape.us_weight_class_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// [Width class](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#uswidthclass).
    ///
    /// Indicates a relative change from the normal aspect ratio (width to height
    /// ratio) as specified by a font designer for the glyphs in a font.
    pub fn us_width_class(&self) -> u16 {
        let range = self.shape.us_width_class_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// [Type flags](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#fstype).
    ///
    /// Indicates font embedding licensing rights for the font.
    pub fn fs_type(&self) -> u16 {
        let range = self.shape.fs_type_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The recommended horizontal size in font design units for subscripts for
    /// this font.
    pub fn y_subscript_x_size(&self) -> i16 {
        let range = self.shape.y_subscript_x_size_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The recommended vertical size in font design units for subscripts for
    /// this font.
    pub fn y_subscript_y_size(&self) -> i16 {
        let range = self.shape.y_subscript_y_size_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The recommended horizontal offset in font design units for subscripts
    /// for this font.
    pub fn y_subscript_x_offset(&self) -> i16 {
        let range = self.shape.y_subscript_x_offset_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The recommended vertical offset in font design units for subscripts
    /// for this font.
    pub fn y_subscript_y_offset(&self) -> i16 {
        let range = self.shape.y_subscript_y_offset_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The recommended horizontal size in font design units for superscripts
    /// for this font.
    pub fn y_superscript_x_size(&self) -> i16 {
        let range = self.shape.y_superscript_x_size_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The recommended vertical size in font design units for superscripts
    /// for this font.
    pub fn y_superscript_y_size(&self) -> i16 {
        let range = self.shape.y_superscript_y_size_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The recommended horizontal offset in font design units for superscripts
    /// for this font.
    pub fn y_superscript_x_offset(&self) -> i16 {
        let range = self.shape.y_superscript_x_offset_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The recommended vertical offset in font design units for superscripts
    /// for this font.
    pub fn y_superscript_y_offset(&self) -> i16 {
        let range = self.shape.y_superscript_y_offset_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Thickness of the strikeout stroke in font design units.
    pub fn y_strikeout_size(&self) -> i16 {
        let range = self.shape.y_strikeout_size_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The position of the top of the strikeout stroke relative to the
    /// baseline in font design units.
    pub fn y_strikeout_position(&self) -> i16 {
        let range = self.shape.y_strikeout_position_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// [Font-family class and subclass](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#sfamilyclass).
    /// This parameter is a classification of font-family design.
    pub fn s_family_class(&self) -> i16 {
        let range = self.shape.s_family_class_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// [PANOSE classification number](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#panose).
    ///
    /// Additional specifications are required for PANOSE to classify non-Latin
    /// character sets.
    pub fn panose_10(&self) -> &'a [u8] {
        let range = self.shape.panose_10_byte_range();
        self.data.read_array(range).unwrap()
    }

    /// [Unicode Character Range](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#ulunicoderange1-bits-031ulunicoderange2-bits-3263ulunicoderange3-bits-6495ulunicoderange4-bits-96127).
    ///
    /// Unicode Character Range (bits 0-31).
    pub fn ul_unicode_range_1(&self) -> u32 {
        let range = self.shape.ul_unicode_range_1_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Unicode Character Range (bits 32-63).
    pub fn ul_unicode_range_2(&self) -> u32 {
        let range = self.shape.ul_unicode_range_2_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Unicode Character Range (bits 64-95).
    pub fn ul_unicode_range_3(&self) -> u32 {
        let range = self.shape.ul_unicode_range_3_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Unicode Character Range (bits 96-127).
    pub fn ul_unicode_range_4(&self) -> u32 {
        let range = self.shape.ul_unicode_range_4_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// [Font Vendor Identification](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#achvendid).
    ///
    /// The four-character identifier for the vendor of the given type face.
    pub fn ach_vend_id(&self) -> Tag {
        let range = self.shape.ach_vend_id_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// [Font selection flags](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#fsselection).
    ///
    /// Contains information concerning the nature of the font patterns.
    pub fn fs_selection(&self) -> u16 {
        let range = self.shape.fs_selection_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The minimum Unicode index (character code) in this font.
    pub fn us_first_char_index(&self) -> u16 {
        let range = self.shape.us_first_char_index_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The maximum Unicode index (character code) in this font.
    pub fn us_last_char_index(&self) -> u16 {
        let range = self.shape.us_last_char_index_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The typographic ascender for this font.
    pub fn s_typo_ascender(&self) -> i16 {
        let range = self.shape.s_typo_ascender_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The typographic descender for this font.
    pub fn s_typo_descender(&self) -> i16 {
        let range = self.shape.s_typo_descender_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The typographic line gap for this font.
    pub fn s_typo_line_gap(&self) -> i16 {
        let range = self.shape.s_typo_line_gap_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The “Windows ascender” metric.
    ///
    /// This should be used to specify the height above the baseline for a
    /// clipping region.
    pub fn us_win_ascent(&self) -> u16 {
        let range = self.shape.us_win_ascent_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The “Windows descender” metric.
    ///
    /// This should be used to specify the vertical extent below the baseline
    /// for a clipping region.
    pub fn us_win_descent(&self) -> u16 {
        let range = self.shape.us_win_descent_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Code page character range bits 0-31.
    pub fn ul_code_page_range_1(&self) -> Option<u32> {
        let range = self.shape.ul_code_page_range_1_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// Code page character range bits 32-63.
    pub fn ul_code_page_range_2(&self) -> Option<u32> {
        let range = self.shape.ul_code_page_range_2_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// This metric specifies the distance between the baseline and the
    /// approximate height of non-ascending lowercase letters measured in
    /// FUnits.
    pub fn sx_height(&self) -> Option<i16> {
        let range = self.shape.sx_height_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// This metric specifies the distance between the baseline and the
    /// approximate height of uppercase letters measured in FUnits.
    pub fn s_cap_height(&self) -> Option<i16> {
        let range = self.shape.s_cap_height_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// This is the Unicode code point, in UTF-16 encoding, of a character that
    /// can be used for a default glyph.
    pub fn us_default_char(&self) -> Option<u16> {
        let range = self.shape.us_default_char_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// his is the Unicode code point, in UTF-16 encoding, of a character that
    /// can be used as a default break character.
    pub fn us_break_char(&self) -> Option<u16> {
        let range = self.shape.us_break_char_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// This field is used for fonts with multiple optical styles.
    pub fn us_max_context(&self) -> Option<u16> {
        let range = self.shape.us_max_context_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// This field is used for fonts with multiple optical styles.
    pub fn us_lower_optical_point_size(&self) -> Option<u16> {
        let range = self.shape.us_lower_optical_point_size_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// This field is used for fonts with multiple optical styles.
    pub fn us_upper_optical_point_size(&self) -> Option<u16> {
        let range = self.shape.us_upper_optical_point_size_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }
}

#[cfg(feature = "traversal")]
impl<'a> SomeTable<'a> for Os2<'a> {
    fn type_name(&self) -> &str {
        "Os2"
    }
    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("x_avg_char_width", self.x_avg_char_width())),
            2usize => Some(Field::new("us_weight_class", self.us_weight_class())),
            3usize => Some(Field::new("us_width_class", self.us_width_class())),
            4usize => Some(Field::new("fs_type", self.fs_type())),
            5usize => Some(Field::new("y_subscript_x_size", self.y_subscript_x_size())),
            6usize => Some(Field::new("y_subscript_y_size", self.y_subscript_y_size())),
            7usize => Some(Field::new(
                "y_subscript_x_offset",
                self.y_subscript_x_offset(),
            )),
            8usize => Some(Field::new(
                "y_subscript_y_offset",
                self.y_subscript_y_offset(),
            )),
            9usize => Some(Field::new(
                "y_superscript_x_size",
                self.y_superscript_x_size(),
            )),
            10usize => Some(Field::new(
                "y_superscript_y_size",
                self.y_superscript_y_size(),
            )),
            11usize => Some(Field::new(
                "y_superscript_x_offset",
                self.y_superscript_x_offset(),
            )),
            12usize => Some(Field::new(
                "y_superscript_y_offset",
                self.y_superscript_y_offset(),
            )),
            13usize => Some(Field::new("y_strikeout_size", self.y_strikeout_size())),
            14usize => Some(Field::new(
                "y_strikeout_position",
                self.y_strikeout_position(),
            )),
            15usize => Some(Field::new("s_family_class", self.s_family_class())),
            16usize => Some(Field::new("panose_10", self.panose_10())),
            17usize => Some(Field::new("ul_unicode_range_1", self.ul_unicode_range_1())),
            18usize => Some(Field::new("ul_unicode_range_2", self.ul_unicode_range_2())),
            19usize => Some(Field::new("ul_unicode_range_3", self.ul_unicode_range_3())),
            20usize => Some(Field::new("ul_unicode_range_4", self.ul_unicode_range_4())),
            21usize => Some(Field::new("ach_vend_id", self.ach_vend_id())),
            22usize => Some(Field::new("fs_selection", self.fs_selection())),
            23usize => Some(Field::new(
                "us_first_char_index",
                self.us_first_char_index(),
            )),
            24usize => Some(Field::new("us_last_char_index", self.us_last_char_index())),
            25usize => Some(Field::new("s_typo_ascender", self.s_typo_ascender())),
            26usize => Some(Field::new("s_typo_descender", self.s_typo_descender())),
            27usize => Some(Field::new("s_typo_line_gap", self.s_typo_line_gap())),
            28usize => Some(Field::new("us_win_ascent", self.us_win_ascent())),
            29usize => Some(Field::new("us_win_descent", self.us_win_descent())),
            30usize if version.compatible(1) => Some(Field::new(
                "ul_code_page_range_1",
                self.ul_code_page_range_1().unwrap(),
            )),
            31usize if version.compatible(1) => Some(Field::new(
                "ul_code_page_range_2",
                self.ul_code_page_range_2().unwrap(),
            )),
            32usize if version.compatible(2) => {
                Some(Field::new("sx_height", self.sx_height().unwrap()))
            }
            33usize if version.compatible(2) => {
                Some(Field::new("s_cap_height", self.s_cap_height().unwrap()))
            }
            34usize if version.compatible(2) => Some(Field::new(
                "us_default_char",
                self.us_default_char().unwrap(),
            )),
            35usize if version.compatible(2) => {
                Some(Field::new("us_break_char", self.us_break_char().unwrap()))
            }
            36usize if version.compatible(2) => {
                Some(Field::new("us_max_context", self.us_max_context().unwrap()))
            }
            37usize if version.compatible(5) => Some(Field::new(
                "us_lower_optical_point_size",
                self.us_lower_optical_point_size().unwrap(),
            )),
            38usize if version.compatible(5) => Some(Field::new(
                "us_upper_optical_point_size",
                self.us_upper_optical_point_size().unwrap(),
            )),
            _ => None,
        }
    }
}

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