font_types/
fword.rs

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//! 16-bit signed and unsigned font-units

use super::Fixed;

/// 16-bit signed quantity in font design units.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bytemuck", derive(bytemuck::AnyBitPattern))]
#[repr(transparent)]
pub struct FWord(i16);

/// 16-bit unsigned quantity in font design units.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bytemuck", derive(bytemuck::AnyBitPattern))]
#[repr(transparent)]
pub struct UfWord(u16);

impl FWord {
    pub const fn new(raw: i16) -> Self {
        Self(raw)
    }

    pub const fn to_i16(self) -> i16 {
        self.0
    }

    /// Converts this number to a 16.16 fixed point value.
    pub const fn to_fixed(self) -> Fixed {
        Fixed::from_i32(self.0 as i32)
    }

    /// The representation of this number as a big-endian byte array.
    pub const fn to_be_bytes(self) -> [u8; 2] {
        self.0.to_be_bytes()
    }
}

impl UfWord {
    pub const fn new(raw: u16) -> Self {
        Self(raw)
    }

    pub const fn to_u16(self) -> u16 {
        self.0
    }

    /// Converts this number to a 16.16 fixed point value.
    pub const fn to_fixed(self) -> Fixed {
        Fixed::from_i32(self.0 as i32)
    }

    /// The representation of this number as a big-endian byte array.
    pub const fn to_be_bytes(self) -> [u8; 2] {
        self.0.to_be_bytes()
    }
}

impl std::fmt::Display for FWord {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl std::fmt::Display for UfWord {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl From<u16> for UfWord {
    fn from(src: u16) -> Self {
        UfWord(src)
    }
}

impl From<i16> for FWord {
    fn from(src: i16) -> Self {
        FWord(src)
    }
}

impl From<FWord> for i16 {
    fn from(src: FWord) -> Self {
        src.0
    }
}

impl From<UfWord> for u16 {
    fn from(src: UfWord) -> Self {
        src.0
    }
}

crate::newtype_scalar!(FWord, [u8; 2]);
crate::newtype_scalar!(UfWord, [u8; 2]);
//TODO: we can add addition/etc as needed