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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//! Glyph Identifiers.
//!
//! Although these are treated as u16s in the spec, we choose to represent them
//! as a distinct type.

/// A 16-bit glyph identifier.
#[derive(Clone, Copy, Debug, 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 GlyphId16(u16);

impl GlyphId16 {
    /// The identifier reserved for unknown glyphs
    pub const NOTDEF: GlyphId16 = GlyphId16(0);

    /// Construct a new `GlyphId16`.
    pub const fn new(raw: u16) -> Self {
        GlyphId16(raw)
    }

    /// The identifier as a u16.
    pub const fn to_u16(self) -> u16 {
        self.0
    }

    /// The identifier as a u32.
    pub const fn to_u32(self) -> u32 {
        self.0 as u32
    }

    pub const fn to_be_bytes(self) -> [u8; 2] {
        self.0.to_be_bytes()
    }
}

impl Default for GlyphId16 {
    fn default() -> Self {
        GlyphId16::NOTDEF
    }
}

impl From<u16> for GlyphId16 {
    fn from(value: u16) -> Self {
        Self(value)
    }
}

impl std::fmt::Display for GlyphId16 {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "GID_{}", self.0)
    }
}

impl From<GlyphId16> for u32 {
    fn from(value: GlyphId16) -> u32 {
        value.to_u32()
    }
}

crate::newtype_scalar!(GlyphId16, [u8; 2]);

/// A 32-bit glyph identifier.
#[derive(Clone, Copy, Debug, 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 GlyphId(u32);

impl GlyphId {
    /// The identifier reserved for unknown glyphs.
    pub const NOTDEF: GlyphId = GlyphId(0);

    /// Construct a new `GlyphId`.
    pub const fn new(raw: u32) -> Self {
        Self(raw)
    }

    /// The identifier as a u32.
    pub const fn to_u32(self) -> u32 {
        self.0
    }
}

impl Default for GlyphId {
    fn default() -> Self {
        GlyphId::NOTDEF
    }
}

impl From<u16> for GlyphId {
    fn from(value: u16) -> Self {
        Self(value as u32)
    }
}

impl From<u32> for GlyphId {
    fn from(value: u32) -> Self {
        Self(value)
    }
}

impl std::fmt::Display for GlyphId {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "GID_{}", self.0)
    }
}

impl From<GlyphId> for u32 {
    fn from(value: GlyphId) -> u32 {
        value.to_u32()
    }
}

impl From<GlyphId16> for GlyphId {
    fn from(value: GlyphId16) -> GlyphId {
        Self(value.to_u32())
    }
}

impl PartialEq<GlyphId16> for GlyphId {
    fn eq(&self, other: &GlyphId16) -> bool {
        self.0 == other.0 as u32
    }
}

impl PartialOrd<GlyphId16> for GlyphId {
    fn partial_cmp(&self, other: &GlyphId16) -> Option<core::cmp::Ordering> {
        Some(self.0.cmp(&(other.0 as u32)))
    }
}

impl PartialEq<GlyphId> for GlyphId16 {
    fn eq(&self, other: &GlyphId) -> bool {
        self.0 as u32 == other.0
    }
}

impl PartialOrd<GlyphId> for GlyphId16 {
    fn partial_cmp(&self, other: &GlyphId) -> Option<core::cmp::Ordering> {
        Some((self.0 as u32).cmp(&other.0))
    }
}

impl TryFrom<GlyphId> for GlyphId16 {
    type Error = TryFromGlyphIdError;

    fn try_from(value: GlyphId) -> Result<Self, Self::Error> {
        Ok(Self(
            value
                .0
                .try_into()
                .map_err(|_| TryFromGlyphIdError(value.0))?,
        ))
    }
}

/// The error type returned when a glyph identifier conversion fails.
#[derive(Debug)]
pub struct TryFromGlyphIdError(u32);

impl core::fmt::Display for TryFromGlyphIdError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "glyph identifier {} too large for conversion", self.0)
    }
}

#[cfg(feature = "std")]
impl std::error::Error for TryFromGlyphIdError {}