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
//! The [os2](https://docs.microsoft.com/en-us/typography/opentype/spec/os2) table

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

impl Os2 {
    fn compute_version(&self) -> u16 {
        if self.us_lower_optical_point_size.is_some() || self.us_upper_optical_point_size.is_some()
        {
            5
        } else if self.sx_height.or(self.s_cap_height).is_some()
            || self
                .us_default_char
                .or(self.us_break_char)
                .or(self.us_max_context)
                .is_some()
        {
            // https://learn.microsoft.com/en-us/typography/opentype/spec/os2
            // "All versions are supported, but use of version 4 or later is strongly recommended"
            // Fields for v2, v3, and v4 are the same. Choice of 4 matches behavior of python tooling.
            4
        } else {
            u16::from(
                self.ul_code_page_range_1
                    .or(self.ul_code_page_range_2)
                    .is_some(),
            )
        }
    }
}

fn convert_panose(raw: &[u8]) -> [u8; 10] {
    raw.try_into().unwrap_or_default()
}