font_types/
fword.rs

1//! 16-bit signed and unsigned font-units
2
3use super::Fixed;
4
5/// 16-bit signed quantity in font design units.
6#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8#[cfg_attr(feature = "bytemuck", derive(bytemuck::AnyBitPattern))]
9#[repr(transparent)]
10pub struct FWord(i16);
11
12/// 16-bit unsigned quantity in font design units.
13#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
14#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
15#[cfg_attr(feature = "bytemuck", derive(bytemuck::AnyBitPattern))]
16#[repr(transparent)]
17pub struct UfWord(u16);
18
19impl FWord {
20    pub const fn new(raw: i16) -> Self {
21        Self(raw)
22    }
23
24    pub const fn to_i16(self) -> i16 {
25        self.0
26    }
27
28    /// Converts this number to a 16.16 fixed point value.
29    pub const fn to_fixed(self) -> Fixed {
30        Fixed::from_i32(self.0 as i32)
31    }
32
33    /// The representation of this number as a big-endian byte array.
34    pub const fn to_be_bytes(self) -> [u8; 2] {
35        self.0.to_be_bytes()
36    }
37}
38
39impl UfWord {
40    pub const fn new(raw: u16) -> Self {
41        Self(raw)
42    }
43
44    pub const fn to_u16(self) -> u16 {
45        self.0
46    }
47
48    /// Converts this number to a 16.16 fixed point value.
49    pub const fn to_fixed(self) -> Fixed {
50        Fixed::from_i32(self.0 as i32)
51    }
52
53    /// The representation of this number as a big-endian byte array.
54    pub const fn to_be_bytes(self) -> [u8; 2] {
55        self.0.to_be_bytes()
56    }
57}
58
59impl std::fmt::Display for FWord {
60    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
61        self.0.fmt(f)
62    }
63}
64
65impl std::fmt::Display for UfWord {
66    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
67        self.0.fmt(f)
68    }
69}
70
71impl From<u16> for UfWord {
72    fn from(src: u16) -> Self {
73        UfWord(src)
74    }
75}
76
77impl From<i16> for FWord {
78    fn from(src: i16) -> Self {
79        FWord(src)
80    }
81}
82
83impl From<FWord> for i16 {
84    fn from(src: FWord) -> Self {
85        src.0
86    }
87}
88
89impl From<UfWord> for u16 {
90    fn from(src: UfWord) -> Self {
91        src.0
92    }
93}
94
95crate::newtype_scalar!(FWord, [u8; 2]);
96crate::newtype_scalar!(UfWord, [u8; 2]);
97//TODO: we can add addition/etc as needed