Skip to main content

euv_core/font/glyph/
impl.rs

1use super::*;
2
3/// Construction and compact 64-bit packing helpers for `GlyphKey`.
4impl GlyphKey {
5    /// Losslessly packs all `GlyphKey` fields into the low 40 bits of a `u64`, preserving lexicographical `(codepoint, size_tenths, bold, monospace, italic)` order.
6    ///
7    /// # Arguments
8    ///
9    /// - `&Self` - Quantized glyph lookup key.
10    ///
11    /// # Returns
12    ///
13    /// - `u64` - Bijective 40-bit packed key representation (`0..2^40`).
14    #[inline(always)]
15    #[must_use]
16    pub const fn pack_u64(&self) -> u64 {
17        let Self {
18            codepoint,
19            size_tenths,
20            bold,
21            monospace,
22            italic,
23        } = *self;
24        ((codepoint as u64) << 19)
25            | ((size_tenths as u64) << 3)
26            | ((bold as u64) << 2)
27            | ((monospace as u64) << 1)
28            | (italic as u64)
29    }
30
31    /// Builds a quantized `GlyphKey` from a character, pixel size, and font weight.
32    ///
33    /// # Arguments
34    ///
35    /// - `char` - Character to rasterize.
36    /// - `f32` - Effective physical font size in pixels.
37    /// - `FontWeight` - Font weight determining bold synthesis.
38    ///
39    /// # Returns
40    ///
41    /// - `Self` - Quantized cache key.
42    #[must_use]
43    pub fn from_style(codepoint: char, physical_size: f32, weight: FontWeight) -> Self {
44        let clamped: f32 = physical_size.clamp(6.0, 256.0);
45        let size_tenths: u16 = (clamped * 10.0 + 0.5) as u16;
46        Self::new(codepoint, size_tenths, weight.is_bold())
47    }
48
49    /// Builds a quantized `GlyphKey` capturing size, weight, monospace family, and italic slant from `style`.
50    ///
51    /// # Arguments
52    ///
53    /// - `char` - Character to rasterize.
54    /// - `f32` - Effective physical font size in pixels.
55    /// - `TextStyle` - Active typography style.
56    ///
57    /// # Returns
58    ///
59    /// - `Self` - Quantized cache key distinguishing sans, monospace, bold, and italic faces.
60    #[must_use]
61    pub fn from_text_style(codepoint: char, physical_size: f32, style: TextStyle) -> Self {
62        let clamped: f32 = physical_size.clamp(6.0, 256.0);
63        let size_tenths: u16 = (clamped * 10.0 + 0.5) as u16;
64        Self {
65            codepoint,
66            size_tenths,
67            bold: style.get_weight().is_bold(),
68            monospace: style.get_family() == FontFamily::Monospace,
69            italic: style.get_italic(),
70        }
71    }
72}
73
74/// Single-register 64-bit equality comparison for `GlyphKey`.
75impl PartialEq for GlyphKey {
76    /// Compares two glyph keys via their bijective 64-bit packed integer representation.
77    ///
78    /// # Arguments
79    ///
80    /// - `&Self` - Left glyph key.
81    /// - `&Self` - Right glyph key.
82    ///
83    /// # Returns
84    ///
85    /// - `bool` - `true` when all glyph key fields match.
86    #[inline(always)]
87    fn eq(&self, other: &Self) -> bool {
88        self.pack_u64() == other.pack_u64()
89    }
90}
91
92/// Single-register 64-bit partial ordering for `GlyphKey`.
93impl PartialOrd for GlyphKey {
94    /// Compares two glyph keys via their order-preserving packed 64-bit integers.
95    ///
96    /// # Arguments
97    ///
98    /// - `&Self` - Left glyph key.
99    /// - `&Self` - Right glyph key.
100    ///
101    /// # Returns
102    ///
103    /// - `Option<core::cmp::Ordering>` - Total lexicographical ordering of the two keys.
104    #[inline(always)]
105    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
106        Some(self.cmp(other))
107    }
108}
109
110/// Single-register 64-bit total ordering for `GlyphKey`.
111impl Ord for GlyphKey {
112    /// Compares two glyph keys via their order-preserving packed 64-bit integers.
113    ///
114    /// # Arguments
115    ///
116    /// - `&Self` - Left glyph key.
117    /// - `&Self` - Right glyph key.
118    ///
119    /// # Returns
120    ///
121    /// - `core::cmp::Ordering` - Lexicographical ordering across `(codepoint, size_tenths, bold, monospace, italic)`.
122    #[inline(always)]
123    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
124        self.pack_u64().cmp(&other.pack_u64())
125    }
126}
127
128/// Single-word 64-bit hashing for `GlyphKey`.
129impl Hash for GlyphKey {
130    /// Feeds the single bijective 64-bit packed glyph key directly to `state`.
131    ///
132    /// # Arguments
133    ///
134    /// - `&Self` - Quantized glyph lookup key.
135    /// - `&mut H` - Target hasher state.
136    #[inline(always)]
137    fn hash<H>(&self, state: &mut H)
138    where
139        H: Hasher,
140    {
141        state.write_u64(self.pack_u64());
142    }
143}