font_types/
version.rs

1/// A legacy 16/16 version encoding
2/// Packed 32-bit value with major and minor version numbers.
3///
4/// This is a legacy type with an unusual representation. See [the spec][spec] for
5/// additional details.
6///
7/// [spec]: https://learn.microsoft.com/en-us/typography/opentype/spec/otff#table-version-numbers
8#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10#[cfg_attr(feature = "bytemuck", derive(bytemuck::AnyBitPattern))]
11#[repr(transparent)]
12pub struct Version16Dot16(u32);
13
14/// A type representing a major, minor version pair.
15///
16/// This is not part of [the spec][spec], but versions in the spec are frequently
17/// represented as a `major_version`, `minor_version` pair. This type encodes
18/// those as a single type, which is useful for some of the generated code that
19/// parses out a version.
20///
21/// [spec]: https://learn.microsoft.com/en-us/typography/opentype/spec/otff#table-version-numbers
22#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
23#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
24#[cfg_attr(feature = "bytemuck", derive(bytemuck::AnyBitPattern))]
25#[repr(C)]
26pub struct MajorMinor {
27    /// The major version number
28    pub major: u16,
29    /// The minor version number
30    pub minor: u16,
31}
32
33/// A trait for determining whether versions are compatible.
34pub trait Compatible<Rhs = Self>: Sized {
35    /// return `true` if this version is field-compatible with `other`.
36    ///
37    /// This is kind of poorly defined, but basically means 'same major version,
38    /// greater than or equal minor version'.
39    fn compatible(&self, other: Rhs) -> bool;
40}
41
42impl Version16Dot16 {
43    /// Version 0.5
44    pub const VERSION_0_5: Version16Dot16 = Version16Dot16::new(0, 5);
45    /// Version 1.0
46    pub const VERSION_1_0: Version16Dot16 = Version16Dot16::new(1, 0);
47    /// Version 1.1
48    pub const VERSION_1_1: Version16Dot16 = Version16Dot16::new(1, 1);
49    /// Version 2.0
50    pub const VERSION_2_0: Version16Dot16 = Version16Dot16::new(2, 0);
51    /// Version 2.5
52    pub const VERSION_2_5: Version16Dot16 = Version16Dot16::new(2, 5);
53    /// Version 3.0
54    pub const VERSION_3_0: Version16Dot16 = Version16Dot16::new(3, 0);
55
56    /// Create a new version with the provided major and minor parts.
57    ///
58    /// The minor version must be in the range 0..=9.
59    ///
60    /// # Panics
61    ///
62    /// Panics if `minor > 9`.
63    pub const fn new(major: u16, minor: u16) -> Self {
64        assert!(minor < 10, "minor version must be in the range [0, 9]");
65        let version = ((major as u32) << 16) | ((minor as u32) << 12);
66        Version16Dot16(version)
67    }
68
69    /// Return the separate major & minor version numbers.
70    pub const fn to_major_minor(self) -> (u16, u16) {
71        let major = (self.0 >> 16) as u16;
72        let minor = ((self.0 & 0xFFFF) >> 12) as u16;
73        (major, minor)
74    }
75
76    /// The representation of this version as a big-endian byte array.
77    #[inline]
78    pub const fn to_be_bytes(self) -> [u8; 4] {
79        self.0.to_be_bytes()
80    }
81}
82
83crate::newtype_scalar!(Version16Dot16, [u8; 4]);
84
85impl MajorMinor {
86    /// Version 1.0
87    pub const VERSION_1_0: MajorMinor = MajorMinor::new(1, 0);
88    /// Version 1.1
89    pub const VERSION_1_1: MajorMinor = MajorMinor::new(1, 1);
90    /// Version 1.2
91    pub const VERSION_1_2: MajorMinor = MajorMinor::new(1, 2);
92    /// Version 1.3
93    pub const VERSION_1_3: MajorMinor = MajorMinor::new(1, 3);
94    /// Version 2.0
95    pub const VERSION_2_0: MajorMinor = MajorMinor::new(2, 0);
96
97    /// Create a new version with major and minor parts.
98    #[inline]
99    pub const fn new(major: u16, minor: u16) -> Self {
100        MajorMinor { major, minor }
101    }
102
103    /// The representation of this version as a big-endian byte array.
104    #[inline]
105    pub const fn to_be_bytes(self) -> [u8; 4] {
106        let [a, b] = self.major.to_be_bytes();
107        let [c, d] = self.minor.to_be_bytes();
108        [a, b, c, d]
109    }
110}
111
112impl crate::Scalar for MajorMinor {
113    type Raw = [u8; 4];
114
115    fn from_raw(raw: Self::Raw) -> Self {
116        let major = u16::from_be_bytes([raw[0], raw[1]]);
117        let minor = u16::from_be_bytes([raw[2], raw[3]]);
118        Self { major, minor }
119    }
120
121    fn to_raw(self) -> Self::Raw {
122        self.to_be_bytes()
123    }
124}
125
126impl Compatible for Version16Dot16 {
127    #[inline]
128    fn compatible(&self, other: Self) -> bool {
129        let (self_major, self_minor) = self.to_major_minor();
130        let (other_major, other_minor) = other.to_major_minor();
131        self_major == other_major && self_minor >= other_minor
132    }
133}
134
135impl Compatible<(u16, u16)> for Version16Dot16 {
136    fn compatible(&self, other: (u16, u16)) -> bool {
137        self.compatible(Version16Dot16::new(other.0, other.1))
138    }
139}
140
141impl Compatible for MajorMinor {
142    #[inline]
143    fn compatible(&self, other: Self) -> bool {
144        self.major == other.major && self.minor >= other.minor
145    }
146}
147
148impl Compatible<(u16, u16)> for MajorMinor {
149    fn compatible(&self, other: (u16, u16)) -> bool {
150        self.compatible(MajorMinor::new(other.0, other.1))
151    }
152}
153
154impl Compatible for u16 {
155    #[inline]
156    fn compatible(&self, other: Self) -> bool {
157        *self >= other
158    }
159}
160
161impl std::fmt::Debug for Version16Dot16 {
162    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
163        write!(f, "Version16Dot16({:08x})", self.0)
164    }
165}
166
167impl std::fmt::Display for Version16Dot16 {
168    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
169        let (major, minor) = self.to_major_minor();
170        write!(f, "{major}.{minor}")
171    }
172}
173
174impl std::fmt::Display for MajorMinor {
175    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
176        let MajorMinor { major, minor } = self;
177        write!(f, "{major}.{minor}")
178    }
179}
180
181#[cfg(test)]
182mod tests {
183    use super::*;
184    #[test]
185    fn version_smoke_test() {
186        assert_eq!(Version16Dot16(0x00005000).to_major_minor(), (0, 5));
187        assert_eq!(Version16Dot16(0x00011000).to_major_minor(), (1, 1));
188        assert_eq!(Version16Dot16::new(0, 5).0, 0x00005000);
189        assert_eq!(Version16Dot16::new(1, 1).0, 0x00011000);
190    }
191
192    #[test]
193    #[should_panic]
194    fn minor_version_out_of_range_test() {
195        Version16Dot16::new(1, 10);
196    }
197}