Skip to main content

read_fonts/generated/
generated_os2.rs

1// THIS FILE IS AUTOGENERATED.
2// Any changes to this file will be overwritten.
3// For more information about how codegen works, see font-codegen/README.md
4
5#[allow(unused_imports)]
6use crate::codegen_prelude::*;
7
8/// OS/2 [selection flags](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#fsselection)
9#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck :: AnyBitPattern)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11#[repr(transparent)]
12pub struct SelectionFlags {
13    bits: u16,
14}
15
16impl SelectionFlags {
17    /// Bit 0: Font contains italic or oblique glyphs, otherwise they are
18    /// upright.
19    pub const ITALIC: Self = Self { bits: 0x0001 };
20
21    /// Bit 1: Glyphs are underscored.
22    pub const UNDERSCORE: Self = Self { bits: 0x0002 };
23
24    /// Bit 2: Glyphs have their foreground and background reversed.
25    pub const NEGATIVE: Self = Self { bits: 0x0004 };
26
27    /// Bit 3: Outline (hollow) glyphs, otherwise they are solid.
28    pub const OUTLINED: Self = Self { bits: 0x0008 };
29
30    /// Bit 4: Glyphs are overstruck.
31    pub const STRIKEOUT: Self = Self { bits: 0x0010 };
32
33    /// Bit 5: Glyphs are emboldened.
34    pub const BOLD: Self = Self { bits: 0x0020 };
35
36    /// Bit 6: Glyphs are in the standard weight/style for the font.
37    pub const REGULAR: Self = Self { bits: 0x0040 };
38
39    /// Bit 7: If set, it is strongly recommended that applications use
40    /// OS/2.sTypoAscender - OS/2.sTypoDescender + OS/2.sTypoLineGap as
41    /// the default line spacing for this font.
42    pub const USE_TYPO_METRICS: Self = Self { bits: 0x0080 };
43
44    /// Bit 8: The font has 'name' table strings consistent with a
45    /// weight/width/slope family without requiring use of name IDs 21 and 22.
46    pub const WWS: Self = Self { bits: 0x0100 };
47
48    /// Bit 9: Font contains oblique glyphs.
49    pub const OBLIQUE: Self = Self { bits: 0x0200 };
50}
51
52impl SelectionFlags {
53    ///  Returns an empty set of flags.
54    #[inline]
55    pub const fn empty() -> Self {
56        Self { bits: 0 }
57    }
58
59    /// Returns the set containing all flags.
60    #[inline]
61    pub const fn all() -> Self {
62        Self {
63            bits: Self::ITALIC.bits
64                | Self::UNDERSCORE.bits
65                | Self::NEGATIVE.bits
66                | Self::OUTLINED.bits
67                | Self::STRIKEOUT.bits
68                | Self::BOLD.bits
69                | Self::REGULAR.bits
70                | Self::USE_TYPO_METRICS.bits
71                | Self::WWS.bits
72                | Self::OBLIQUE.bits,
73        }
74    }
75
76    /// Returns the raw value of the flags currently stored.
77    #[inline]
78    pub const fn bits(&self) -> u16 {
79        self.bits
80    }
81
82    /// Convert from underlying bit representation, unless that
83    /// representation contains bits that do not correspond to a flag.
84    #[inline]
85    pub const fn from_bits(bits: u16) -> Option<Self> {
86        if (bits & !Self::all().bits()) == 0 {
87            Some(Self { bits })
88        } else {
89            None
90        }
91    }
92
93    /// Convert from underlying bit representation, dropping any bits
94    /// that do not correspond to flags.
95    #[inline]
96    pub const fn from_bits_truncate(bits: u16) -> Self {
97        Self {
98            bits: bits & Self::all().bits,
99        }
100    }
101
102    /// Returns `true` if no flags are currently stored.
103    #[inline]
104    pub const fn is_empty(&self) -> bool {
105        self.bits() == Self::empty().bits()
106    }
107
108    /// Returns `true` if there are flags common to both `self` and `other`.
109    #[inline]
110    pub const fn intersects(&self, other: Self) -> bool {
111        !(Self {
112            bits: self.bits & other.bits,
113        })
114        .is_empty()
115    }
116
117    /// Returns `true` if all of the flags in `other` are contained within `self`.
118    #[inline]
119    pub const fn contains(&self, other: Self) -> bool {
120        (self.bits & other.bits) == other.bits
121    }
122
123    /// Inserts the specified flags in-place.
124    #[inline]
125    pub fn insert(&mut self, other: Self) {
126        self.bits |= other.bits;
127    }
128
129    /// Removes the specified flags in-place.
130    #[inline]
131    pub fn remove(&mut self, other: Self) {
132        self.bits &= !other.bits;
133    }
134
135    /// Toggles the specified flags in-place.
136    #[inline]
137    pub fn toggle(&mut self, other: Self) {
138        self.bits ^= other.bits;
139    }
140
141    /// Returns the intersection between the flags in `self` and
142    /// `other`.
143    ///
144    /// Specifically, the returned set contains only the flags which are
145    /// present in *both* `self` *and* `other`.
146    ///
147    /// This is equivalent to using the `&` operator (e.g.
148    /// [`ops::BitAnd`]), as in `flags & other`.
149    ///
150    /// [`ops::BitAnd`]: https://doc.rust-lang.org/std/ops/trait.BitAnd.html
151    #[inline]
152    #[must_use]
153    pub const fn intersection(self, other: Self) -> Self {
154        Self {
155            bits: self.bits & other.bits,
156        }
157    }
158
159    /// Returns the union of between the flags in `self` and `other`.
160    ///
161    /// Specifically, the returned set contains all flags which are
162    /// present in *either* `self` *or* `other`, including any which are
163    /// present in both.
164    ///
165    /// This is equivalent to using the `|` operator (e.g.
166    /// [`ops::BitOr`]), as in `flags | other`.
167    ///
168    /// [`ops::BitOr`]: https://doc.rust-lang.org/std/ops/trait.BitOr.html
169    #[inline]
170    #[must_use]
171    pub const fn union(self, other: Self) -> Self {
172        Self {
173            bits: self.bits | other.bits,
174        }
175    }
176
177    /// Returns the difference between the flags in `self` and `other`.
178    ///
179    /// Specifically, the returned set contains all flags present in
180    /// `self`, except for the ones present in `other`.
181    ///
182    /// It is also conceptually equivalent to the "bit-clear" operation:
183    /// `flags & !other` (and this syntax is also supported).
184    ///
185    /// This is equivalent to using the `-` operator (e.g.
186    /// [`ops::Sub`]), as in `flags - other`.
187    ///
188    /// [`ops::Sub`]: https://doc.rust-lang.org/std/ops/trait.Sub.html
189    #[inline]
190    #[must_use]
191    pub const fn difference(self, other: Self) -> Self {
192        Self {
193            bits: self.bits & !other.bits,
194        }
195    }
196}
197
198impl std::ops::BitOr for SelectionFlags {
199    type Output = Self;
200
201    /// Returns the union of the two sets of flags.
202    #[inline]
203    fn bitor(self, other: SelectionFlags) -> Self {
204        Self {
205            bits: self.bits | other.bits,
206        }
207    }
208}
209
210impl std::ops::BitOrAssign for SelectionFlags {
211    /// Adds the set of flags.
212    #[inline]
213    fn bitor_assign(&mut self, other: Self) {
214        self.bits |= other.bits;
215    }
216}
217
218impl std::ops::BitXor for SelectionFlags {
219    type Output = Self;
220
221    /// Returns the left flags, but with all the right flags toggled.
222    #[inline]
223    fn bitxor(self, other: Self) -> Self {
224        Self {
225            bits: self.bits ^ other.bits,
226        }
227    }
228}
229
230impl std::ops::BitXorAssign for SelectionFlags {
231    /// Toggles the set of flags.
232    #[inline]
233    fn bitxor_assign(&mut self, other: Self) {
234        self.bits ^= other.bits;
235    }
236}
237
238impl std::ops::BitAnd for SelectionFlags {
239    type Output = Self;
240
241    /// Returns the intersection between the two sets of flags.
242    #[inline]
243    fn bitand(self, other: Self) -> Self {
244        Self {
245            bits: self.bits & other.bits,
246        }
247    }
248}
249
250impl std::ops::BitAndAssign for SelectionFlags {
251    /// Disables all flags disabled in the set.
252    #[inline]
253    fn bitand_assign(&mut self, other: Self) {
254        self.bits &= other.bits;
255    }
256}
257
258impl std::ops::Sub for SelectionFlags {
259    type Output = Self;
260
261    /// Returns the set difference of the two sets of flags.
262    #[inline]
263    fn sub(self, other: Self) -> Self {
264        Self {
265            bits: self.bits & !other.bits,
266        }
267    }
268}
269
270impl std::ops::SubAssign for SelectionFlags {
271    /// Disables all flags enabled in the set.
272    #[inline]
273    fn sub_assign(&mut self, other: Self) {
274        self.bits &= !other.bits;
275    }
276}
277
278impl std::ops::Not for SelectionFlags {
279    type Output = Self;
280
281    /// Returns the complement of this set of flags.
282    #[inline]
283    fn not(self) -> Self {
284        Self { bits: !self.bits } & Self::all()
285    }
286}
287
288impl std::fmt::Debug for SelectionFlags {
289    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
290        let members: &[(&str, Self)] = &[
291            ("ITALIC", Self::ITALIC),
292            ("UNDERSCORE", Self::UNDERSCORE),
293            ("NEGATIVE", Self::NEGATIVE),
294            ("OUTLINED", Self::OUTLINED),
295            ("STRIKEOUT", Self::STRIKEOUT),
296            ("BOLD", Self::BOLD),
297            ("REGULAR", Self::REGULAR),
298            ("USE_TYPO_METRICS", Self::USE_TYPO_METRICS),
299            ("WWS", Self::WWS),
300            ("OBLIQUE", Self::OBLIQUE),
301        ];
302        let mut first = true;
303        for (name, value) in members {
304            if self.contains(*value) {
305                if !first {
306                    f.write_str(" | ")?;
307                }
308                first = false;
309                f.write_str(name)?;
310            }
311        }
312        if first {
313            f.write_str("(empty)")?;
314        }
315        Ok(())
316    }
317}
318
319impl std::fmt::Binary for SelectionFlags {
320    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
321        std::fmt::Binary::fmt(&self.bits, f)
322    }
323}
324
325impl std::fmt::Octal for SelectionFlags {
326    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
327        std::fmt::Octal::fmt(&self.bits, f)
328    }
329}
330
331impl std::fmt::LowerHex for SelectionFlags {
332    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
333        std::fmt::LowerHex::fmt(&self.bits, f)
334    }
335}
336
337impl std::fmt::UpperHex for SelectionFlags {
338    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
339        std::fmt::UpperHex::fmt(&self.bits, f)
340    }
341}
342
343impl font_types::Scalar for SelectionFlags {
344    type Raw = <u16 as font_types::Scalar>::Raw;
345    fn to_raw(self) -> Self::Raw {
346        self.bits().to_raw()
347    }
348    fn from_raw(raw: Self::Raw) -> Self {
349        let t = <u16>::from_raw(raw);
350        Self::from_bits_truncate(t)
351    }
352}
353
354#[cfg(feature = "experimental_traverse")]
355impl<'a> From<SelectionFlags> for FieldType<'a> {
356    fn from(src: SelectionFlags) -> FieldType<'a> {
357        src.bits().into()
358    }
359}
360
361impl<'a> MinByteRange<'a> for Os2<'a> {
362    fn min_byte_range(&self) -> Range<usize> {
363        0..self.us_win_descent_byte_range().end
364    }
365    fn min_table_bytes(&self) -> &'a [u8] {
366        let range = self.min_byte_range();
367        self.data.as_bytes().get(range).unwrap_or_default()
368    }
369}
370
371impl TopLevelTable for Os2<'_> {
372    /// `OS/2`
373    const TAG: Tag = Tag::new(b"OS/2");
374}
375
376impl<'a> FontRead<'a> for Os2<'a> {
377    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
378        #[allow(clippy::absurd_extreme_comparisons)]
379        if data.len() < Self::MIN_SIZE {
380            return Err(ReadError::OutOfBounds);
381        }
382        Ok(Self { data })
383    }
384}
385
386/// [`OS/2`](https://docs.microsoft.com/en-us/typography/opentype/spec/os2)
387#[derive(Clone)]
388pub struct Os2<'a> {
389    data: FontData<'a>,
390}
391
392#[allow(clippy::needless_lifetimes)]
393impl<'a> Os2<'a> {
394    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
395        + i16::RAW_BYTE_LEN
396        + u16::RAW_BYTE_LEN
397        + u16::RAW_BYTE_LEN
398        + u16::RAW_BYTE_LEN
399        + i16::RAW_BYTE_LEN
400        + i16::RAW_BYTE_LEN
401        + i16::RAW_BYTE_LEN
402        + i16::RAW_BYTE_LEN
403        + i16::RAW_BYTE_LEN
404        + i16::RAW_BYTE_LEN
405        + i16::RAW_BYTE_LEN
406        + i16::RAW_BYTE_LEN
407        + i16::RAW_BYTE_LEN
408        + i16::RAW_BYTE_LEN
409        + i16::RAW_BYTE_LEN
410        + u8::RAW_BYTE_LEN * 10_usize
411        + u32::RAW_BYTE_LEN
412        + u32::RAW_BYTE_LEN
413        + u32::RAW_BYTE_LEN
414        + u32::RAW_BYTE_LEN
415        + Tag::RAW_BYTE_LEN
416        + SelectionFlags::RAW_BYTE_LEN
417        + u16::RAW_BYTE_LEN
418        + u16::RAW_BYTE_LEN
419        + i16::RAW_BYTE_LEN
420        + i16::RAW_BYTE_LEN
421        + i16::RAW_BYTE_LEN
422        + u16::RAW_BYTE_LEN
423        + u16::RAW_BYTE_LEN);
424    basic_table_impls!(impl_the_methods);
425
426    pub fn version(&self) -> u16 {
427        let range = self.version_byte_range();
428        self.data.read_at(range.start).ok().unwrap()
429    }
430
431    /// [Average weighted escapement](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#xavgcharwidth).
432    ///
433    /// The Average Character Width parameter specifies the arithmetic average
434    /// of the escapement (width) of all non-zero width glyphs in the font.
435    pub fn x_avg_char_width(&self) -> i16 {
436        let range = self.x_avg_char_width_byte_range();
437        self.data.read_at(range.start).ok().unwrap()
438    }
439
440    /// [Weight class](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#usweightclass).
441    ///
442    /// Indicates the visual weight (degree of blackness or thickness of
443    /// strokes) of the characters in the font. Values from 1 to 1000 are valid.
444    pub fn us_weight_class(&self) -> u16 {
445        let range = self.us_weight_class_byte_range();
446        self.data.read_at(range.start).ok().unwrap()
447    }
448
449    /// [Width class](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#uswidthclass).
450    ///
451    /// Indicates a relative change from the normal aspect ratio (width to height
452    /// ratio) as specified by a font designer for the glyphs in a font.
453    pub fn us_width_class(&self) -> u16 {
454        let range = self.us_width_class_byte_range();
455        self.data.read_at(range.start).ok().unwrap()
456    }
457
458    /// [Type flags](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#fstype).
459    ///
460    /// Indicates font embedding licensing rights for the font.
461    pub fn fs_type(&self) -> u16 {
462        let range = self.fs_type_byte_range();
463        self.data.read_at(range.start).ok().unwrap()
464    }
465
466    /// The recommended horizontal size in font design units for subscripts for
467    /// this font.
468    pub fn y_subscript_x_size(&self) -> i16 {
469        let range = self.y_subscript_x_size_byte_range();
470        self.data.read_at(range.start).ok().unwrap()
471    }
472
473    /// The recommended vertical size in font design units for subscripts for
474    /// this font.
475    pub fn y_subscript_y_size(&self) -> i16 {
476        let range = self.y_subscript_y_size_byte_range();
477        self.data.read_at(range.start).ok().unwrap()
478    }
479
480    /// The recommended horizontal offset in font design units for subscripts
481    /// for this font.
482    pub fn y_subscript_x_offset(&self) -> i16 {
483        let range = self.y_subscript_x_offset_byte_range();
484        self.data.read_at(range.start).ok().unwrap()
485    }
486
487    /// The recommended vertical offset in font design units for subscripts
488    /// for this font.
489    pub fn y_subscript_y_offset(&self) -> i16 {
490        let range = self.y_subscript_y_offset_byte_range();
491        self.data.read_at(range.start).ok().unwrap()
492    }
493
494    /// The recommended horizontal size in font design units for superscripts
495    /// for this font.
496    pub fn y_superscript_x_size(&self) -> i16 {
497        let range = self.y_superscript_x_size_byte_range();
498        self.data.read_at(range.start).ok().unwrap()
499    }
500
501    /// The recommended vertical size in font design units for superscripts
502    /// for this font.
503    pub fn y_superscript_y_size(&self) -> i16 {
504        let range = self.y_superscript_y_size_byte_range();
505        self.data.read_at(range.start).ok().unwrap()
506    }
507
508    /// The recommended horizontal offset in font design units for superscripts
509    /// for this font.
510    pub fn y_superscript_x_offset(&self) -> i16 {
511        let range = self.y_superscript_x_offset_byte_range();
512        self.data.read_at(range.start).ok().unwrap()
513    }
514
515    /// The recommended vertical offset in font design units for superscripts
516    /// for this font.
517    pub fn y_superscript_y_offset(&self) -> i16 {
518        let range = self.y_superscript_y_offset_byte_range();
519        self.data.read_at(range.start).ok().unwrap()
520    }
521
522    /// Thickness of the strikeout stroke in font design units.
523    pub fn y_strikeout_size(&self) -> i16 {
524        let range = self.y_strikeout_size_byte_range();
525        self.data.read_at(range.start).ok().unwrap()
526    }
527
528    /// The position of the top of the strikeout stroke relative to the
529    /// baseline in font design units.
530    pub fn y_strikeout_position(&self) -> i16 {
531        let range = self.y_strikeout_position_byte_range();
532        self.data.read_at(range.start).ok().unwrap()
533    }
534
535    /// [Font-family class and subclass](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#sfamilyclass).
536    /// This parameter is a classification of font-family design.
537    pub fn s_family_class(&self) -> i16 {
538        let range = self.s_family_class_byte_range();
539        self.data.read_at(range.start).ok().unwrap()
540    }
541
542    /// [PANOSE classification number](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#panose).
543    ///
544    /// Additional specifications are required for PANOSE to classify non-Latin
545    /// character sets.
546    pub fn panose_10(&self) -> &'a [u8] {
547        let range = self.panose_10_byte_range();
548        self.data.read_array(range).ok().unwrap()
549    }
550
551    /// [Unicode Character Range](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#ulunicoderange1-bits-031ulunicoderange2-bits-3263ulunicoderange3-bits-6495ulunicoderange4-bits-96127).
552    ///
553    /// Unicode Character Range (bits 0-31).
554    pub fn ul_unicode_range_1(&self) -> u32 {
555        let range = self.ul_unicode_range_1_byte_range();
556        self.data.read_at(range.start).ok().unwrap()
557    }
558
559    /// Unicode Character Range (bits 32-63).
560    pub fn ul_unicode_range_2(&self) -> u32 {
561        let range = self.ul_unicode_range_2_byte_range();
562        self.data.read_at(range.start).ok().unwrap()
563    }
564
565    /// Unicode Character Range (bits 64-95).
566    pub fn ul_unicode_range_3(&self) -> u32 {
567        let range = self.ul_unicode_range_3_byte_range();
568        self.data.read_at(range.start).ok().unwrap()
569    }
570
571    /// Unicode Character Range (bits 96-127).
572    pub fn ul_unicode_range_4(&self) -> u32 {
573        let range = self.ul_unicode_range_4_byte_range();
574        self.data.read_at(range.start).ok().unwrap()
575    }
576
577    /// [Font Vendor Identification](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#achvendid).
578    ///
579    /// The four-character identifier for the vendor of the given type face.
580    pub fn ach_vend_id(&self) -> Tag {
581        let range = self.ach_vend_id_byte_range();
582        self.data.read_at(range.start).ok().unwrap()
583    }
584
585    /// [Font selection flags](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#fsselection).
586    ///
587    /// Contains information concerning the nature of the font patterns.
588    pub fn fs_selection(&self) -> SelectionFlags {
589        let range = self.fs_selection_byte_range();
590        self.data.read_at(range.start).ok().unwrap()
591    }
592
593    /// The minimum Unicode index (character code) in this font.
594    pub fn us_first_char_index(&self) -> u16 {
595        let range = self.us_first_char_index_byte_range();
596        self.data.read_at(range.start).ok().unwrap()
597    }
598
599    /// The maximum Unicode index (character code) in this font.
600    pub fn us_last_char_index(&self) -> u16 {
601        let range = self.us_last_char_index_byte_range();
602        self.data.read_at(range.start).ok().unwrap()
603    }
604
605    /// The typographic ascender for this font.
606    pub fn s_typo_ascender(&self) -> i16 {
607        let range = self.s_typo_ascender_byte_range();
608        self.data.read_at(range.start).ok().unwrap()
609    }
610
611    /// The typographic descender for this font.
612    pub fn s_typo_descender(&self) -> i16 {
613        let range = self.s_typo_descender_byte_range();
614        self.data.read_at(range.start).ok().unwrap()
615    }
616
617    /// The typographic line gap for this font.
618    pub fn s_typo_line_gap(&self) -> i16 {
619        let range = self.s_typo_line_gap_byte_range();
620        self.data.read_at(range.start).ok().unwrap()
621    }
622
623    /// The “Windows ascender” metric.
624    ///
625    /// This should be used to specify the height above the baseline for a
626    /// clipping region.
627    pub fn us_win_ascent(&self) -> u16 {
628        let range = self.us_win_ascent_byte_range();
629        self.data.read_at(range.start).ok().unwrap()
630    }
631
632    /// The “Windows descender” metric.
633    ///
634    /// This should be used to specify the vertical extent below the baseline
635    /// for a clipping region.
636    pub fn us_win_descent(&self) -> u16 {
637        let range = self.us_win_descent_byte_range();
638        self.data.read_at(range.start).ok().unwrap()
639    }
640
641    /// Code page character range bits 0-31.
642    pub fn ul_code_page_range_1(&self) -> Option<u32> {
643        let range = self.ul_code_page_range_1_byte_range();
644        (!range.is_empty())
645            .then(|| self.data.read_at(range.start).ok())
646            .flatten()
647    }
648
649    /// Code page character range bits 32-63.
650    pub fn ul_code_page_range_2(&self) -> Option<u32> {
651        let range = self.ul_code_page_range_2_byte_range();
652        (!range.is_empty())
653            .then(|| self.data.read_at(range.start).ok())
654            .flatten()
655    }
656
657    /// This metric specifies the distance between the baseline and the
658    /// approximate height of non-ascending lowercase letters measured in
659    /// FUnits.
660    pub fn sx_height(&self) -> Option<i16> {
661        let range = self.sx_height_byte_range();
662        (!range.is_empty())
663            .then(|| self.data.read_at(range.start).ok())
664            .flatten()
665    }
666
667    /// This metric specifies the distance between the baseline and the
668    /// approximate height of uppercase letters measured in FUnits.
669    pub fn s_cap_height(&self) -> Option<i16> {
670        let range = self.s_cap_height_byte_range();
671        (!range.is_empty())
672            .then(|| self.data.read_at(range.start).ok())
673            .flatten()
674    }
675
676    /// This is the Unicode codepoint, in UTF-16 encoding, of a character that
677    /// can be used for a default glyph.
678    pub fn us_default_char(&self) -> Option<u16> {
679        let range = self.us_default_char_byte_range();
680        (!range.is_empty())
681            .then(|| self.data.read_at(range.start).ok())
682            .flatten()
683    }
684
685    /// This is the Unicode codepoint, in UTF-16 encoding, of a character that
686    /// can be used as a default break character.
687    pub fn us_break_char(&self) -> Option<u16> {
688        let range = self.us_break_char_byte_range();
689        (!range.is_empty())
690            .then(|| self.data.read_at(range.start).ok())
691            .flatten()
692    }
693
694    /// This field is used for fonts with multiple optical styles.
695    pub fn us_max_context(&self) -> Option<u16> {
696        let range = self.us_max_context_byte_range();
697        (!range.is_empty())
698            .then(|| self.data.read_at(range.start).ok())
699            .flatten()
700    }
701
702    /// This field is used for fonts with multiple optical styles.
703    pub fn us_lower_optical_point_size(&self) -> Option<u16> {
704        let range = self.us_lower_optical_point_size_byte_range();
705        (!range.is_empty())
706            .then(|| self.data.read_at(range.start).ok())
707            .flatten()
708    }
709
710    /// This field is used for fonts with multiple optical styles.
711    pub fn us_upper_optical_point_size(&self) -> Option<u16> {
712        let range = self.us_upper_optical_point_size_byte_range();
713        (!range.is_empty())
714            .then(|| self.data.read_at(range.start).ok())
715            .flatten()
716    }
717
718    pub fn version_byte_range(&self) -> Range<usize> {
719        let start = 0;
720        start..start + u16::RAW_BYTE_LEN
721    }
722
723    pub fn x_avg_char_width_byte_range(&self) -> Range<usize> {
724        let start = self.version_byte_range().end;
725        start..start + i16::RAW_BYTE_LEN
726    }
727
728    pub fn us_weight_class_byte_range(&self) -> Range<usize> {
729        let start = self.x_avg_char_width_byte_range().end;
730        start..start + u16::RAW_BYTE_LEN
731    }
732
733    pub fn us_width_class_byte_range(&self) -> Range<usize> {
734        let start = self.us_weight_class_byte_range().end;
735        start..start + u16::RAW_BYTE_LEN
736    }
737
738    pub fn fs_type_byte_range(&self) -> Range<usize> {
739        let start = self.us_width_class_byte_range().end;
740        start..start + u16::RAW_BYTE_LEN
741    }
742
743    pub fn y_subscript_x_size_byte_range(&self) -> Range<usize> {
744        let start = self.fs_type_byte_range().end;
745        start..start + i16::RAW_BYTE_LEN
746    }
747
748    pub fn y_subscript_y_size_byte_range(&self) -> Range<usize> {
749        let start = self.y_subscript_x_size_byte_range().end;
750        start..start + i16::RAW_BYTE_LEN
751    }
752
753    pub fn y_subscript_x_offset_byte_range(&self) -> Range<usize> {
754        let start = self.y_subscript_y_size_byte_range().end;
755        start..start + i16::RAW_BYTE_LEN
756    }
757
758    pub fn y_subscript_y_offset_byte_range(&self) -> Range<usize> {
759        let start = self.y_subscript_x_offset_byte_range().end;
760        start..start + i16::RAW_BYTE_LEN
761    }
762
763    pub fn y_superscript_x_size_byte_range(&self) -> Range<usize> {
764        let start = self.y_subscript_y_offset_byte_range().end;
765        start..start + i16::RAW_BYTE_LEN
766    }
767
768    pub fn y_superscript_y_size_byte_range(&self) -> Range<usize> {
769        let start = self.y_superscript_x_size_byte_range().end;
770        start..start + i16::RAW_BYTE_LEN
771    }
772
773    pub fn y_superscript_x_offset_byte_range(&self) -> Range<usize> {
774        let start = self.y_superscript_y_size_byte_range().end;
775        start..start + i16::RAW_BYTE_LEN
776    }
777
778    pub fn y_superscript_y_offset_byte_range(&self) -> Range<usize> {
779        let start = self.y_superscript_x_offset_byte_range().end;
780        start..start + i16::RAW_BYTE_LEN
781    }
782
783    pub fn y_strikeout_size_byte_range(&self) -> Range<usize> {
784        let start = self.y_superscript_y_offset_byte_range().end;
785        start..start + i16::RAW_BYTE_LEN
786    }
787
788    pub fn y_strikeout_position_byte_range(&self) -> Range<usize> {
789        let start = self.y_strikeout_size_byte_range().end;
790        start..start + i16::RAW_BYTE_LEN
791    }
792
793    pub fn s_family_class_byte_range(&self) -> Range<usize> {
794        let start = self.y_strikeout_position_byte_range().end;
795        start..start + i16::RAW_BYTE_LEN
796    }
797
798    pub fn panose_10_byte_range(&self) -> Range<usize> {
799        let start = self.s_family_class_byte_range().end;
800        start..start + (10_usize).saturating_mul(u8::RAW_BYTE_LEN)
801    }
802
803    pub fn ul_unicode_range_1_byte_range(&self) -> Range<usize> {
804        let start = self.panose_10_byte_range().end;
805        start..start + u32::RAW_BYTE_LEN
806    }
807
808    pub fn ul_unicode_range_2_byte_range(&self) -> Range<usize> {
809        let start = self.ul_unicode_range_1_byte_range().end;
810        start..start + u32::RAW_BYTE_LEN
811    }
812
813    pub fn ul_unicode_range_3_byte_range(&self) -> Range<usize> {
814        let start = self.ul_unicode_range_2_byte_range().end;
815        start..start + u32::RAW_BYTE_LEN
816    }
817
818    pub fn ul_unicode_range_4_byte_range(&self) -> Range<usize> {
819        let start = self.ul_unicode_range_3_byte_range().end;
820        start..start + u32::RAW_BYTE_LEN
821    }
822
823    pub fn ach_vend_id_byte_range(&self) -> Range<usize> {
824        let start = self.ul_unicode_range_4_byte_range().end;
825        start..start + Tag::RAW_BYTE_LEN
826    }
827
828    pub fn fs_selection_byte_range(&self) -> Range<usize> {
829        let start = self.ach_vend_id_byte_range().end;
830        start..start + SelectionFlags::RAW_BYTE_LEN
831    }
832
833    pub fn us_first_char_index_byte_range(&self) -> Range<usize> {
834        let start = self.fs_selection_byte_range().end;
835        start..start + u16::RAW_BYTE_LEN
836    }
837
838    pub fn us_last_char_index_byte_range(&self) -> Range<usize> {
839        let start = self.us_first_char_index_byte_range().end;
840        start..start + u16::RAW_BYTE_LEN
841    }
842
843    pub fn s_typo_ascender_byte_range(&self) -> Range<usize> {
844        let start = self.us_last_char_index_byte_range().end;
845        start..start + i16::RAW_BYTE_LEN
846    }
847
848    pub fn s_typo_descender_byte_range(&self) -> Range<usize> {
849        let start = self.s_typo_ascender_byte_range().end;
850        start..start + i16::RAW_BYTE_LEN
851    }
852
853    pub fn s_typo_line_gap_byte_range(&self) -> Range<usize> {
854        let start = self.s_typo_descender_byte_range().end;
855        start..start + i16::RAW_BYTE_LEN
856    }
857
858    pub fn us_win_ascent_byte_range(&self) -> Range<usize> {
859        let start = self.s_typo_line_gap_byte_range().end;
860        start..start + u16::RAW_BYTE_LEN
861    }
862
863    pub fn us_win_descent_byte_range(&self) -> Range<usize> {
864        let start = self.us_win_ascent_byte_range().end;
865        start..start + u16::RAW_BYTE_LEN
866    }
867
868    pub fn ul_code_page_range_1_byte_range(&self) -> Range<usize> {
869        let start = self.us_win_descent_byte_range().end;
870        start
871            ..(self.version().compatible(1u16))
872                .then(|| start + u32::RAW_BYTE_LEN)
873                .unwrap_or(start)
874    }
875
876    pub fn ul_code_page_range_2_byte_range(&self) -> Range<usize> {
877        let start = self.ul_code_page_range_1_byte_range().end;
878        start
879            ..(self.version().compatible(1u16))
880                .then(|| start + u32::RAW_BYTE_LEN)
881                .unwrap_or(start)
882    }
883
884    pub fn sx_height_byte_range(&self) -> Range<usize> {
885        let start = self.ul_code_page_range_2_byte_range().end;
886        start
887            ..(self.version().compatible(2u16))
888                .then(|| start + i16::RAW_BYTE_LEN)
889                .unwrap_or(start)
890    }
891
892    pub fn s_cap_height_byte_range(&self) -> Range<usize> {
893        let start = self.sx_height_byte_range().end;
894        start
895            ..(self.version().compatible(2u16))
896                .then(|| start + i16::RAW_BYTE_LEN)
897                .unwrap_or(start)
898    }
899
900    pub fn us_default_char_byte_range(&self) -> Range<usize> {
901        let start = self.s_cap_height_byte_range().end;
902        start
903            ..(self.version().compatible(2u16))
904                .then(|| start + u16::RAW_BYTE_LEN)
905                .unwrap_or(start)
906    }
907
908    pub fn us_break_char_byte_range(&self) -> Range<usize> {
909        let start = self.us_default_char_byte_range().end;
910        start
911            ..(self.version().compatible(2u16))
912                .then(|| start + u16::RAW_BYTE_LEN)
913                .unwrap_or(start)
914    }
915
916    pub fn us_max_context_byte_range(&self) -> Range<usize> {
917        let start = self.us_break_char_byte_range().end;
918        start
919            ..(self.version().compatible(2u16))
920                .then(|| start + u16::RAW_BYTE_LEN)
921                .unwrap_or(start)
922    }
923
924    pub fn us_lower_optical_point_size_byte_range(&self) -> Range<usize> {
925        let start = self.us_max_context_byte_range().end;
926        start
927            ..(self.version().compatible(5u16))
928                .then(|| start + u16::RAW_BYTE_LEN)
929                .unwrap_or(start)
930    }
931
932    pub fn us_upper_optical_point_size_byte_range(&self) -> Range<usize> {
933        let start = self.us_lower_optical_point_size_byte_range().end;
934        start
935            ..(self.version().compatible(5u16))
936                .then(|| start + u16::RAW_BYTE_LEN)
937                .unwrap_or(start)
938    }
939}
940
941#[cfg(feature = "experimental_traverse")]
942impl<'a> SomeTable<'a> for Os2<'a> {
943    fn type_name(&self) -> &str {
944        "Os2"
945    }
946    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
947        match idx {
948            0usize => Some(Field::new("version", self.version())),
949            1usize => Some(Field::new("x_avg_char_width", self.x_avg_char_width())),
950            2usize => Some(Field::new("us_weight_class", self.us_weight_class())),
951            3usize => Some(Field::new("us_width_class", self.us_width_class())),
952            4usize => Some(Field::new("fs_type", self.fs_type())),
953            5usize => Some(Field::new("y_subscript_x_size", self.y_subscript_x_size())),
954            6usize => Some(Field::new("y_subscript_y_size", self.y_subscript_y_size())),
955            7usize => Some(Field::new(
956                "y_subscript_x_offset",
957                self.y_subscript_x_offset(),
958            )),
959            8usize => Some(Field::new(
960                "y_subscript_y_offset",
961                self.y_subscript_y_offset(),
962            )),
963            9usize => Some(Field::new(
964                "y_superscript_x_size",
965                self.y_superscript_x_size(),
966            )),
967            10usize => Some(Field::new(
968                "y_superscript_y_size",
969                self.y_superscript_y_size(),
970            )),
971            11usize => Some(Field::new(
972                "y_superscript_x_offset",
973                self.y_superscript_x_offset(),
974            )),
975            12usize => Some(Field::new(
976                "y_superscript_y_offset",
977                self.y_superscript_y_offset(),
978            )),
979            13usize => Some(Field::new("y_strikeout_size", self.y_strikeout_size())),
980            14usize => Some(Field::new(
981                "y_strikeout_position",
982                self.y_strikeout_position(),
983            )),
984            15usize => Some(Field::new("s_family_class", self.s_family_class())),
985            16usize => Some(Field::new("panose_10", self.panose_10())),
986            17usize => Some(Field::new("ul_unicode_range_1", self.ul_unicode_range_1())),
987            18usize => Some(Field::new("ul_unicode_range_2", self.ul_unicode_range_2())),
988            19usize => Some(Field::new("ul_unicode_range_3", self.ul_unicode_range_3())),
989            20usize => Some(Field::new("ul_unicode_range_4", self.ul_unicode_range_4())),
990            21usize => Some(Field::new("ach_vend_id", self.ach_vend_id())),
991            22usize => Some(Field::new("fs_selection", self.fs_selection())),
992            23usize => Some(Field::new(
993                "us_first_char_index",
994                self.us_first_char_index(),
995            )),
996            24usize => Some(Field::new("us_last_char_index", self.us_last_char_index())),
997            25usize => Some(Field::new("s_typo_ascender", self.s_typo_ascender())),
998            26usize => Some(Field::new("s_typo_descender", self.s_typo_descender())),
999            27usize => Some(Field::new("s_typo_line_gap", self.s_typo_line_gap())),
1000            28usize => Some(Field::new("us_win_ascent", self.us_win_ascent())),
1001            29usize => Some(Field::new("us_win_descent", self.us_win_descent())),
1002            30usize if self.version().compatible(1u16) => Some(Field::new(
1003                "ul_code_page_range_1",
1004                self.ul_code_page_range_1().unwrap(),
1005            )),
1006            31usize if self.version().compatible(1u16) => Some(Field::new(
1007                "ul_code_page_range_2",
1008                self.ul_code_page_range_2().unwrap(),
1009            )),
1010            32usize if self.version().compatible(2u16) => {
1011                Some(Field::new("sx_height", self.sx_height().unwrap()))
1012            }
1013            33usize if self.version().compatible(2u16) => {
1014                Some(Field::new("s_cap_height", self.s_cap_height().unwrap()))
1015            }
1016            34usize if self.version().compatible(2u16) => Some(Field::new(
1017                "us_default_char",
1018                self.us_default_char().unwrap(),
1019            )),
1020            35usize if self.version().compatible(2u16) => {
1021                Some(Field::new("us_break_char", self.us_break_char().unwrap()))
1022            }
1023            36usize if self.version().compatible(2u16) => {
1024                Some(Field::new("us_max_context", self.us_max_context().unwrap()))
1025            }
1026            37usize if self.version().compatible(5u16) => Some(Field::new(
1027                "us_lower_optical_point_size",
1028                self.us_lower_optical_point_size().unwrap(),
1029            )),
1030            38usize if self.version().compatible(5u16) => Some(Field::new(
1031                "us_upper_optical_point_size",
1032                self.us_upper_optical_point_size().unwrap(),
1033            )),
1034            _ => None,
1035        }
1036    }
1037}
1038
1039#[cfg(feature = "experimental_traverse")]
1040#[allow(clippy::needless_lifetimes)]
1041impl<'a> std::fmt::Debug for Os2<'a> {
1042    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1043        (self as &dyn SomeTable<'a>).fmt(f)
1044    }
1045}