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        let end = start + u16::RAW_BYTE_LEN;
721        start..end
722    }
723
724    pub fn x_avg_char_width_byte_range(&self) -> Range<usize> {
725        let start = self.version_byte_range().end;
726        let end = start + i16::RAW_BYTE_LEN;
727        start..end
728    }
729
730    pub fn us_weight_class_byte_range(&self) -> Range<usize> {
731        let start = self.x_avg_char_width_byte_range().end;
732        let end = start + u16::RAW_BYTE_LEN;
733        start..end
734    }
735
736    pub fn us_width_class_byte_range(&self) -> Range<usize> {
737        let start = self.us_weight_class_byte_range().end;
738        let end = start + u16::RAW_BYTE_LEN;
739        start..end
740    }
741
742    pub fn fs_type_byte_range(&self) -> Range<usize> {
743        let start = self.us_width_class_byte_range().end;
744        let end = start + u16::RAW_BYTE_LEN;
745        start..end
746    }
747
748    pub fn y_subscript_x_size_byte_range(&self) -> Range<usize> {
749        let start = self.fs_type_byte_range().end;
750        let end = start + i16::RAW_BYTE_LEN;
751        start..end
752    }
753
754    pub fn y_subscript_y_size_byte_range(&self) -> Range<usize> {
755        let start = self.y_subscript_x_size_byte_range().end;
756        let end = start + i16::RAW_BYTE_LEN;
757        start..end
758    }
759
760    pub fn y_subscript_x_offset_byte_range(&self) -> Range<usize> {
761        let start = self.y_subscript_y_size_byte_range().end;
762        let end = start + i16::RAW_BYTE_LEN;
763        start..end
764    }
765
766    pub fn y_subscript_y_offset_byte_range(&self) -> Range<usize> {
767        let start = self.y_subscript_x_offset_byte_range().end;
768        let end = start + i16::RAW_BYTE_LEN;
769        start..end
770    }
771
772    pub fn y_superscript_x_size_byte_range(&self) -> Range<usize> {
773        let start = self.y_subscript_y_offset_byte_range().end;
774        let end = start + i16::RAW_BYTE_LEN;
775        start..end
776    }
777
778    pub fn y_superscript_y_size_byte_range(&self) -> Range<usize> {
779        let start = self.y_superscript_x_size_byte_range().end;
780        let end = start + i16::RAW_BYTE_LEN;
781        start..end
782    }
783
784    pub fn y_superscript_x_offset_byte_range(&self) -> Range<usize> {
785        let start = self.y_superscript_y_size_byte_range().end;
786        let end = start + i16::RAW_BYTE_LEN;
787        start..end
788    }
789
790    pub fn y_superscript_y_offset_byte_range(&self) -> Range<usize> {
791        let start = self.y_superscript_x_offset_byte_range().end;
792        let end = start + i16::RAW_BYTE_LEN;
793        start..end
794    }
795
796    pub fn y_strikeout_size_byte_range(&self) -> Range<usize> {
797        let start = self.y_superscript_y_offset_byte_range().end;
798        let end = start + i16::RAW_BYTE_LEN;
799        start..end
800    }
801
802    pub fn y_strikeout_position_byte_range(&self) -> Range<usize> {
803        let start = self.y_strikeout_size_byte_range().end;
804        let end = start + i16::RAW_BYTE_LEN;
805        start..end
806    }
807
808    pub fn s_family_class_byte_range(&self) -> Range<usize> {
809        let start = self.y_strikeout_position_byte_range().end;
810        let end = start + i16::RAW_BYTE_LEN;
811        start..end
812    }
813
814    pub fn panose_10_byte_range(&self) -> Range<usize> {
815        let start = self.s_family_class_byte_range().end;
816        let end = start + (10_usize).saturating_mul(u8::RAW_BYTE_LEN);
817        start..end
818    }
819
820    pub fn ul_unicode_range_1_byte_range(&self) -> Range<usize> {
821        let start = self.panose_10_byte_range().end;
822        let end = start + u32::RAW_BYTE_LEN;
823        start..end
824    }
825
826    pub fn ul_unicode_range_2_byte_range(&self) -> Range<usize> {
827        let start = self.ul_unicode_range_1_byte_range().end;
828        let end = start + u32::RAW_BYTE_LEN;
829        start..end
830    }
831
832    pub fn ul_unicode_range_3_byte_range(&self) -> Range<usize> {
833        let start = self.ul_unicode_range_2_byte_range().end;
834        let end = start + u32::RAW_BYTE_LEN;
835        start..end
836    }
837
838    pub fn ul_unicode_range_4_byte_range(&self) -> Range<usize> {
839        let start = self.ul_unicode_range_3_byte_range().end;
840        let end = start + u32::RAW_BYTE_LEN;
841        start..end
842    }
843
844    pub fn ach_vend_id_byte_range(&self) -> Range<usize> {
845        let start = self.ul_unicode_range_4_byte_range().end;
846        let end = start + Tag::RAW_BYTE_LEN;
847        start..end
848    }
849
850    pub fn fs_selection_byte_range(&self) -> Range<usize> {
851        let start = self.ach_vend_id_byte_range().end;
852        let end = start + SelectionFlags::RAW_BYTE_LEN;
853        start..end
854    }
855
856    pub fn us_first_char_index_byte_range(&self) -> Range<usize> {
857        let start = self.fs_selection_byte_range().end;
858        let end = start + u16::RAW_BYTE_LEN;
859        start..end
860    }
861
862    pub fn us_last_char_index_byte_range(&self) -> Range<usize> {
863        let start = self.us_first_char_index_byte_range().end;
864        let end = start + u16::RAW_BYTE_LEN;
865        start..end
866    }
867
868    pub fn s_typo_ascender_byte_range(&self) -> Range<usize> {
869        let start = self.us_last_char_index_byte_range().end;
870        let end = start + i16::RAW_BYTE_LEN;
871        start..end
872    }
873
874    pub fn s_typo_descender_byte_range(&self) -> Range<usize> {
875        let start = self.s_typo_ascender_byte_range().end;
876        let end = start + i16::RAW_BYTE_LEN;
877        start..end
878    }
879
880    pub fn s_typo_line_gap_byte_range(&self) -> Range<usize> {
881        let start = self.s_typo_descender_byte_range().end;
882        let end = start + i16::RAW_BYTE_LEN;
883        start..end
884    }
885
886    pub fn us_win_ascent_byte_range(&self) -> Range<usize> {
887        let start = self.s_typo_line_gap_byte_range().end;
888        let end = start + u16::RAW_BYTE_LEN;
889        start..end
890    }
891
892    pub fn us_win_descent_byte_range(&self) -> Range<usize> {
893        let start = self.us_win_ascent_byte_range().end;
894        let end = start + u16::RAW_BYTE_LEN;
895        start..end
896    }
897
898    pub fn ul_code_page_range_1_byte_range(&self) -> Range<usize> {
899        let start = self.us_win_descent_byte_range().end;
900        let end = if self.version().compatible(1u16) {
901            start + u32::RAW_BYTE_LEN
902        } else {
903            start
904        };
905        start..end
906    }
907
908    pub fn ul_code_page_range_2_byte_range(&self) -> Range<usize> {
909        let start = self.ul_code_page_range_1_byte_range().end;
910        let end = if self.version().compatible(1u16) {
911            start + u32::RAW_BYTE_LEN
912        } else {
913            start
914        };
915        start..end
916    }
917
918    pub fn sx_height_byte_range(&self) -> Range<usize> {
919        let start = self.ul_code_page_range_2_byte_range().end;
920        let end = if self.version().compatible(2u16) {
921            start + i16::RAW_BYTE_LEN
922        } else {
923            start
924        };
925        start..end
926    }
927
928    pub fn s_cap_height_byte_range(&self) -> Range<usize> {
929        let start = self.sx_height_byte_range().end;
930        let end = if self.version().compatible(2u16) {
931            start + i16::RAW_BYTE_LEN
932        } else {
933            start
934        };
935        start..end
936    }
937
938    pub fn us_default_char_byte_range(&self) -> Range<usize> {
939        let start = self.s_cap_height_byte_range().end;
940        let end = if self.version().compatible(2u16) {
941            start + u16::RAW_BYTE_LEN
942        } else {
943            start
944        };
945        start..end
946    }
947
948    pub fn us_break_char_byte_range(&self) -> Range<usize> {
949        let start = self.us_default_char_byte_range().end;
950        let end = if self.version().compatible(2u16) {
951            start + u16::RAW_BYTE_LEN
952        } else {
953            start
954        };
955        start..end
956    }
957
958    pub fn us_max_context_byte_range(&self) -> Range<usize> {
959        let start = self.us_break_char_byte_range().end;
960        let end = if self.version().compatible(2u16) {
961            start + u16::RAW_BYTE_LEN
962        } else {
963            start
964        };
965        start..end
966    }
967
968    pub fn us_lower_optical_point_size_byte_range(&self) -> Range<usize> {
969        let start = self.us_max_context_byte_range().end;
970        let end = if self.version().compatible(5u16) {
971            start + u16::RAW_BYTE_LEN
972        } else {
973            start
974        };
975        start..end
976    }
977
978    pub fn us_upper_optical_point_size_byte_range(&self) -> Range<usize> {
979        let start = self.us_lower_optical_point_size_byte_range().end;
980        let end = if self.version().compatible(5u16) {
981            start + u16::RAW_BYTE_LEN
982        } else {
983            start
984        };
985        start..end
986    }
987}
988
989const _: () = assert!(FontData::default_data_long_enough(Os2::MIN_SIZE));
990
991impl Default for Os2<'_> {
992    fn default() -> Self {
993        Self {
994            data: FontData::default_table_data(),
995        }
996    }
997}
998
999#[cfg(feature = "experimental_traverse")]
1000impl<'a> SomeTable<'a> for Os2<'a> {
1001    fn type_name(&self) -> &str {
1002        "Os2"
1003    }
1004    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
1005        match idx {
1006            0usize => Some(Field::new("version", self.version())),
1007            1usize => Some(Field::new("x_avg_char_width", self.x_avg_char_width())),
1008            2usize => Some(Field::new("us_weight_class", self.us_weight_class())),
1009            3usize => Some(Field::new("us_width_class", self.us_width_class())),
1010            4usize => Some(Field::new("fs_type", self.fs_type())),
1011            5usize => Some(Field::new("y_subscript_x_size", self.y_subscript_x_size())),
1012            6usize => Some(Field::new("y_subscript_y_size", self.y_subscript_y_size())),
1013            7usize => Some(Field::new(
1014                "y_subscript_x_offset",
1015                self.y_subscript_x_offset(),
1016            )),
1017            8usize => Some(Field::new(
1018                "y_subscript_y_offset",
1019                self.y_subscript_y_offset(),
1020            )),
1021            9usize => Some(Field::new(
1022                "y_superscript_x_size",
1023                self.y_superscript_x_size(),
1024            )),
1025            10usize => Some(Field::new(
1026                "y_superscript_y_size",
1027                self.y_superscript_y_size(),
1028            )),
1029            11usize => Some(Field::new(
1030                "y_superscript_x_offset",
1031                self.y_superscript_x_offset(),
1032            )),
1033            12usize => Some(Field::new(
1034                "y_superscript_y_offset",
1035                self.y_superscript_y_offset(),
1036            )),
1037            13usize => Some(Field::new("y_strikeout_size", self.y_strikeout_size())),
1038            14usize => Some(Field::new(
1039                "y_strikeout_position",
1040                self.y_strikeout_position(),
1041            )),
1042            15usize => Some(Field::new("s_family_class", self.s_family_class())),
1043            16usize => Some(Field::new("panose_10", self.panose_10())),
1044            17usize => Some(Field::new("ul_unicode_range_1", self.ul_unicode_range_1())),
1045            18usize => Some(Field::new("ul_unicode_range_2", self.ul_unicode_range_2())),
1046            19usize => Some(Field::new("ul_unicode_range_3", self.ul_unicode_range_3())),
1047            20usize => Some(Field::new("ul_unicode_range_4", self.ul_unicode_range_4())),
1048            21usize => Some(Field::new("ach_vend_id", self.ach_vend_id())),
1049            22usize => Some(Field::new("fs_selection", self.fs_selection())),
1050            23usize => Some(Field::new(
1051                "us_first_char_index",
1052                self.us_first_char_index(),
1053            )),
1054            24usize => Some(Field::new("us_last_char_index", self.us_last_char_index())),
1055            25usize => Some(Field::new("s_typo_ascender", self.s_typo_ascender())),
1056            26usize => Some(Field::new("s_typo_descender", self.s_typo_descender())),
1057            27usize => Some(Field::new("s_typo_line_gap", self.s_typo_line_gap())),
1058            28usize => Some(Field::new("us_win_ascent", self.us_win_ascent())),
1059            29usize => Some(Field::new("us_win_descent", self.us_win_descent())),
1060            30usize if self.version().compatible(1u16) => Some(Field::new(
1061                "ul_code_page_range_1",
1062                self.ul_code_page_range_1().unwrap(),
1063            )),
1064            31usize if self.version().compatible(1u16) => Some(Field::new(
1065                "ul_code_page_range_2",
1066                self.ul_code_page_range_2().unwrap(),
1067            )),
1068            32usize if self.version().compatible(2u16) => {
1069                Some(Field::new("sx_height", self.sx_height().unwrap()))
1070            }
1071            33usize if self.version().compatible(2u16) => {
1072                Some(Field::new("s_cap_height", self.s_cap_height().unwrap()))
1073            }
1074            34usize if self.version().compatible(2u16) => Some(Field::new(
1075                "us_default_char",
1076                self.us_default_char().unwrap(),
1077            )),
1078            35usize if self.version().compatible(2u16) => {
1079                Some(Field::new("us_break_char", self.us_break_char().unwrap()))
1080            }
1081            36usize if self.version().compatible(2u16) => {
1082                Some(Field::new("us_max_context", self.us_max_context().unwrap()))
1083            }
1084            37usize if self.version().compatible(5u16) => Some(Field::new(
1085                "us_lower_optical_point_size",
1086                self.us_lower_optical_point_size().unwrap(),
1087            )),
1088            38usize if self.version().compatible(5u16) => Some(Field::new(
1089                "us_upper_optical_point_size",
1090                self.us_upper_optical_point_size().unwrap(),
1091            )),
1092            _ => None,
1093        }
1094    }
1095}
1096
1097#[cfg(feature = "experimental_traverse")]
1098#[allow(clippy::needless_lifetimes)]
1099impl<'a> std::fmt::Debug for Os2<'a> {
1100    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1101        (self as &dyn SomeTable<'a>).fmt(f)
1102    }
1103}