font_types/
glyph_id.rs

1//! Glyph Identifiers.
2//!
3//! Although these are treated as u16s in the spec, we choose to represent them
4//! as a distinct type.
5
6/// A 16-bit glyph identifier.
7#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9#[cfg_attr(feature = "bytemuck", derive(bytemuck::AnyBitPattern))]
10#[repr(transparent)]
11pub struct GlyphId16(u16);
12
13impl GlyphId16 {
14    /// The identifier reserved for unknown glyphs
15    pub const NOTDEF: GlyphId16 = GlyphId16(0);
16
17    /// Construct a new `GlyphId16`.
18    pub const fn new(raw: u16) -> Self {
19        GlyphId16(raw)
20    }
21
22    /// The identifier as a u16.
23    pub const fn to_u16(self) -> u16 {
24        self.0
25    }
26
27    /// The identifier as a u32.
28    pub const fn to_u32(self) -> u32 {
29        self.0 as u32
30    }
31
32    pub const fn to_be_bytes(self) -> [u8; 2] {
33        self.0.to_be_bytes()
34    }
35}
36
37impl Default for GlyphId16 {
38    fn default() -> Self {
39        GlyphId16::NOTDEF
40    }
41}
42
43impl From<u16> for GlyphId16 {
44    fn from(value: u16) -> Self {
45        Self(value)
46    }
47}
48
49impl From<GlyphId16> for usize {
50    fn from(value: GlyphId16) -> Self {
51        value.0 as usize
52    }
53}
54
55impl std::fmt::Display for GlyphId16 {
56    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
57        write!(f, "GID_{}", self.0)
58    }
59}
60
61impl From<GlyphId16> for u32 {
62    fn from(value: GlyphId16) -> u32 {
63        value.to_u32()
64    }
65}
66
67crate::newtype_scalar!(GlyphId16, [u8; 2]);
68
69/// A 32-bit glyph identifier.
70#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
71#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
72#[cfg_attr(feature = "bytemuck", derive(bytemuck::AnyBitPattern))]
73#[repr(transparent)]
74pub struct GlyphId(u32);
75
76impl GlyphId {
77    /// The identifier reserved for unknown glyphs.
78    pub const NOTDEF: GlyphId = GlyphId(0);
79
80    /// Construct a new `GlyphId`.
81    pub const fn new(raw: u32) -> Self {
82        Self(raw)
83    }
84
85    /// The identifier as a u32.
86    pub const fn to_u32(self) -> u32 {
87        self.0
88    }
89}
90
91impl Default for GlyphId {
92    fn default() -> Self {
93        GlyphId::NOTDEF
94    }
95}
96
97impl From<u16> for GlyphId {
98    fn from(value: u16) -> Self {
99        Self(value as u32)
100    }
101}
102
103impl From<u32> for GlyphId {
104    fn from(value: u32) -> Self {
105        Self(value)
106    }
107}
108
109impl std::fmt::Display for GlyphId {
110    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
111        write!(f, "GID_{}", self.0)
112    }
113}
114
115impl From<GlyphId> for u32 {
116    fn from(value: GlyphId) -> u32 {
117        value.to_u32()
118    }
119}
120
121impl From<GlyphId16> for GlyphId {
122    fn from(value: GlyphId16) -> GlyphId {
123        Self(value.to_u32())
124    }
125}
126
127impl PartialEq<GlyphId16> for GlyphId {
128    fn eq(&self, other: &GlyphId16) -> bool {
129        self.0 == other.0 as u32
130    }
131}
132
133impl PartialOrd<GlyphId16> for GlyphId {
134    fn partial_cmp(&self, other: &GlyphId16) -> Option<core::cmp::Ordering> {
135        Some(self.0.cmp(&(other.0 as u32)))
136    }
137}
138
139impl PartialEq<GlyphId> for GlyphId16 {
140    fn eq(&self, other: &GlyphId) -> bool {
141        self.0 as u32 == other.0
142    }
143}
144
145impl PartialOrd<GlyphId> for GlyphId16 {
146    fn partial_cmp(&self, other: &GlyphId) -> Option<core::cmp::Ordering> {
147        Some((self.0 as u32).cmp(&other.0))
148    }
149}
150
151impl TryFrom<GlyphId> for GlyphId16 {
152    type Error = TryFromGlyphIdError;
153
154    fn try_from(value: GlyphId) -> Result<Self, Self::Error> {
155        Ok(Self(
156            value
157                .0
158                .try_into()
159                .map_err(|_| TryFromGlyphIdError(value.0))?,
160        ))
161    }
162}
163
164/// The error type returned when a glyph identifier conversion fails.
165#[derive(Debug)]
166pub struct TryFromGlyphIdError(u32);
167
168impl core::fmt::Display for TryFromGlyphIdError {
169    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
170        write!(f, "glyph identifier {} too large for conversion", self.0)
171    }
172}
173
174#[cfg(feature = "std")]
175impl std::error::Error for TryFromGlyphIdError {}