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 =
45 physical_size.clamp(GLYPH_MIN_PHYSICAL_PX, GLYPH_ANALYTIC_MAX_PHYSICAL_PX);
46 let size_tenths: u16 = (clamped * 10.0 + 0.5) as u16;
47 Self::new(codepoint, size_tenths, weight.is_bold())
48 }
49
50 /// Builds a quantized `GlyphKey` capturing size, weight, monospace family, and italic slant from `style`.
51 ///
52 /// # Arguments
53 ///
54 /// - `char` - Character to rasterize.
55 /// - `f32` - Effective physical font size in pixels.
56 /// - `TextStyle` - Active typography style.
57 ///
58 /// # Returns
59 ///
60 /// - `Self` - Quantized cache key distinguishing sans, monospace, bold, and italic faces.
61 #[must_use]
62 pub fn from_text_style(codepoint: char, physical_size: f32, style: TextStyle) -> Self {
63 let clamped: f32 =
64 physical_size.clamp(GLYPH_MIN_PHYSICAL_PX, GLYPH_ANALYTIC_MAX_PHYSICAL_PX);
65 let size_tenths: u16 = (clamped * 10.0 + 0.5) as u16;
66 Self {
67 codepoint,
68 size_tenths,
69 bold: style.get_weight().is_bold(),
70 monospace: style.get_family() == FontFamily::Monospace,
71 italic: style.get_italic(),
72 }
73 }
74
75 /// Returns the decoded physical font size in pixels represented by `size_tenths`.
76 ///
77 /// # Arguments
78 ///
79 /// - `&Self` - Quantized glyph lookup key.
80 ///
81 /// # Returns
82 ///
83 /// - `f32` - Physical glyph size in pixels.
84 #[must_use]
85 pub fn physical_size_px(&self) -> f32 {
86 (self.get_size_tenths() as f32) * 0.1
87 }
88
89 /// Returns `true` when the requested physical size exceeds `GLYPH_DIRECT_RASTER_MAX_PX` (`512px`) and routes through analytic SDF reconstruction.
90 ///
91 /// # Arguments
92 ///
93 /// - `&Self` - Quantized glyph lookup key.
94 ///
95 /// # Returns
96 ///
97 /// - `bool` - `true` when gigantic-scale analytic SDF reconstruction applies.
98 #[must_use]
99 pub fn uses_analytic_sdf_fallback(&self) -> bool {
100 self.physical_size_px() > GLYPH_DIRECT_RASTER_MAX_PX
101 }
102
103 /// Returns a cloned key re-sized to `target_physical_px` while preserving codepoint, weight, monospace, and italic flags.
104 ///
105 /// # Arguments
106 ///
107 /// - `&Self` - Source glyph lookup key.
108 /// - `f32` - Replacement physical pixel size.
109 ///
110 /// # Returns
111 ///
112 /// - `Self` - Resized glyph key.
113 #[must_use]
114 pub fn with_physical_size(&self, target_physical_px: f32) -> Self {
115 let clamped: f32 =
116 target_physical_px.clamp(GLYPH_MIN_PHYSICAL_PX, GLYPH_ANALYTIC_MAX_PHYSICAL_PX);
117 let size_tenths: u16 = (clamped * 10.0 + 0.5) as u16;
118 Self {
119 codepoint: self.get_codepoint(),
120 size_tenths,
121 bold: self.get_bold(),
122 monospace: self.get_monospace(),
123 italic: self.get_italic(),
124 }
125 }
126}
127
128/// Single-register 64-bit equality comparison for `GlyphKey`.
129impl PartialEq for GlyphKey {
130 /// Compares two glyph keys via their bijective 64-bit packed integer representation.
131 ///
132 /// # Arguments
133 ///
134 /// - `&Self` - Left glyph key.
135 /// - `&Self` - Right glyph key.
136 ///
137 /// # Returns
138 ///
139 /// - `bool` - `true` when all glyph key fields match.
140 #[inline(always)]
141 fn eq(&self, other: &Self) -> bool {
142 self.pack_u64() == other.pack_u64()
143 }
144}
145
146/// Single-register 64-bit partial ordering for `GlyphKey`.
147impl PartialOrd for GlyphKey {
148 /// Compares two glyph keys via their order-preserving packed 64-bit integers.
149 ///
150 /// # Arguments
151 ///
152 /// - `&Self` - Left glyph key.
153 /// - `&Self` - Right glyph key.
154 ///
155 /// # Returns
156 ///
157 /// - `Option<core::cmp::Ordering>` - Total lexicographical ordering of the two keys.
158 #[inline(always)]
159 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
160 Some(self.cmp(other))
161 }
162}
163
164/// Single-register 64-bit total ordering for `GlyphKey`.
165impl Ord for GlyphKey {
166 /// Compares two glyph keys via their order-preserving packed 64-bit integers.
167 ///
168 /// # Arguments
169 ///
170 /// - `&Self` - Left glyph key.
171 /// - `&Self` - Right glyph key.
172 ///
173 /// # Returns
174 ///
175 /// - `core::cmp::Ordering` - Lexicographical ordering across `(codepoint, size_tenths, bold, monospace, italic)`.
176 #[inline(always)]
177 fn cmp(&self, other: &Self) -> core::cmp::Ordering {
178 self.pack_u64().cmp(&other.pack_u64())
179 }
180}
181
182/// Single-word 64-bit hashing for `GlyphKey`.
183impl Hash for GlyphKey {
184 /// Feeds the single bijective 64-bit packed glyph key directly to `state`.
185 ///
186 /// # Arguments
187 ///
188 /// - `&Self` - Quantized glyph lookup key.
189 /// - `&mut HasherType` - Target hasher state.
190 #[inline(always)]
191 fn hash<HasherType>(&self, state: &mut HasherType)
192 where
193 HasherType: Hasher,
194 {
195 state.write_u64(self.pack_u64());
196 }
197}