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 pub struct GlyphId(u16); 10 11 impl GlyphId { 12 /// The identifier reserved for unknown glyphs 13 pub const NOTDEF: GlyphId = GlyphId(0); 14 15 /// Construct a new `GlyphId`. new(raw: u16) -> Self16 pub const fn new(raw: u16) -> Self { 17 GlyphId(raw) 18 } 19 20 /// The identifier as a u16. to_u16(self) -> u1621 pub const fn to_u16(self) -> u16 { 22 self.0 23 } 24 25 /// The identifier as a u32. to_u32(self) -> u3226 pub const fn to_u32(self) -> u32 { 27 self.0 as u32 28 } 29 to_be_bytes(self) -> [u8; 2]30 pub const fn to_be_bytes(self) -> [u8; 2] { 31 self.0.to_be_bytes() 32 } 33 } 34 35 impl Default for GlyphId { default() -> Self36 fn default() -> Self { 37 GlyphId::NOTDEF 38 } 39 } 40 41 impl std::fmt::Display for GlyphId { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result42 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 43 write!(f, "GID_{}", self.0) 44 } 45 } 46 47 crate::newtype_scalar!(GlyphId, [u8; 2]); 48