euv_core/font/shaping/struct.rs
1use super::*;
2
3/// DPI- and wrap-width-independent lookup key identifying shaped glyph advances for a text run.
4#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, PartialEq)]
5pub struct TextShapingKey {
6 /// Primary 64-bit hash of the source UTF-8 text bytes.
7 #[get(type(copy))]
8 pub(crate) text_hash_a: u64,
9 /// Secondary 64-bit hash of the source UTF-8 text bytes.
10 #[get(type(copy))]
11 pub(crate) text_hash_b: u64,
12 /// Byte length of the source text string.
13 #[get(type(copy))]
14 pub(crate) text_len: u32,
15 /// IEEE-754 bits of the logical font size.
16 #[get(type(copy))]
17 pub(crate) font_size_bits: u32,
18 /// Logical font family classification.
19 #[get(type(copy))]
20 pub(crate) family: FontFamily,
21 /// Logical font weight classification.
22 #[get(type(copy))]
23 pub(crate) weight: FontWeight,
24 /// Whether italic styling is enabled.
25 #[get(type(copy))]
26 pub(crate) italic: bool,
27 /// IEEE-754 bits of additional logical letter spacing.
28 #[get(type(copy))]
29 pub(crate) letter_spacing_bits: u32,
30 /// Hash of the active BCP-47 locale string.
31 #[get(type(copy))]
32 pub(crate) locale_hash: u64,
33 /// Whether right-to-left base direction is active.
34 #[get(type(copy))]
35 pub(crate) rtl: bool,
36 /// Active font fallback chain revision invalidating shaping when system fonts change.
37 #[get(type(copy))]
38 pub(crate) font_chain_revision: u64,
39}
40
41/// DPI- and color-independent lookup key identifying wrapped lines derived from a shaped run.
42#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, PartialEq)]
43pub struct LineBreakKey {
44 /// Primary 64-bit shaping key hash.
45 #[get(type(copy))]
46 pub(crate) shaping_hash_a: u64,
47 /// Secondary 64-bit shaping key hash.
48 #[get(type(copy))]
49 pub(crate) shaping_hash_b: u64,
50 /// IEEE-754 bits of optional wrap width (`u32::MAX` when unbounded).
51 #[get(type(copy))]
52 pub(crate) max_width_bits: u32,
53 /// IEEE-754 bits of line-height multiplier.
54 #[get(type(copy))]
55 pub(crate) line_height_bits: u32,
56 /// Overflow policy (`Wrap`, `Ellipsis`, `Clip`).
57 #[get(type(copy))]
58 pub(crate) overflow: TextOverflow,
59 /// Active font fallback chain revision matching the underlying shaped run.
60 #[get(type(copy))]
61 pub(crate) font_chain_revision: u64,
62}
63
64/// Cached logical glyph-advance shaping result independent of DPI scale and raster bitmap atlas.
65#[derive(Clone, Data, Debug, Default, PartialEq)]
66pub struct ShapedGlyphRun {
67 /// Logical advance width for each shaped character cluster.
68 pub(crate) cluster_advances: Vec<f32>,
69 /// Total unwrapped logical advance width of the run.
70 #[get(type(copy))]
71 pub(crate) total_width: f32,
72 /// Number of shaped character clusters.
73 #[get(type(copy))]
74 pub(crate) cluster_count: usize,
75 /// Tracked resident byte footprint of this entry.
76 #[get(type(copy))]
77 pub(crate) byte_size: usize,
78 /// Monotonic LRU access tick.
79 #[get(type(copy))]
80 pub(crate) last_used_tick: u64,
81}
82
83/// Typed intrusive LRU slot storing a cached `ShapedGlyphRun` and its exact key.
84#[derive(Clone, Debug, Default, PartialEq)]
85pub(crate) struct ShapingRunSlot {
86 /// Whether this slot currently holds a live entry (`false` when on `shape_free_slots`).
87 pub(crate) occupied: bool,
88 /// Precomputed 64-bit FxHash of `key` for O(1) eviction and rehash.
89 pub(crate) hash: u64,
90 /// Exact shaping lookup key verified after 31-bit tag match.
91 pub(crate) key: TextShapingKey,
92 /// Cached shaped glyph run payload.
93 pub(crate) run: ShapedGlyphRun,
94 /// Tagged handle of the more-recently-used neighbor (`SHAPING_NODE_NONE` at head).
95 pub(crate) prev: u32,
96 /// Tagged handle of the less-recently-used neighbor (`SHAPING_NODE_NONE` at tail).
97 pub(crate) next: u32,
98}
99
100/// Typed intrusive LRU slot storing a cached wrapped `TextLayoutResult` and its exact key.
101#[derive(Clone, Debug, Default, PartialEq)]
102pub(crate) struct LineBreakSlot {
103 /// Whether this slot currently holds a live entry (`false` when on `line_break_free_slots`).
104 pub(crate) occupied: bool,
105 /// Precomputed 64-bit FxHash of `key` for O(1) eviction and rehash.
106 pub(crate) hash: u64,
107 /// Tracked resident byte footprint of this wrapped layout entry.
108 pub(crate) byte_size: usize,
109 /// Exact line-break lookup key verified after 31-bit tag match.
110 pub(crate) key: LineBreakKey,
111 /// Cached wrapped text layout result.
112 pub(crate) layout: TextLayoutResult,
113 /// Tagged handle of the more-recently-used neighbor (`SHAPING_NODE_NONE` at head).
114 pub(crate) prev: u32,
115 /// Tagged handle of the less-recently-used neighbor (`SHAPING_NODE_NONE` at tail).
116 pub(crate) next: u32,
117}
118
119/// Bounded DPI-independent single-probe FxHash text shaping and line-break cache with O(1) intrusive LRU eviction.
120#[derive(Clone, Data, Debug)]
121pub struct TextShapingCache {
122 /// Configured aggregate memory budget in bytes across shaping runs and wrapped line layouts.
123 #[get(type(copy))]
124 pub(crate) budget_bytes: usize,
125 /// Current resident bytes tracked across shaping runs and wrapped line layouts.
126 #[get(type(copy))]
127 #[get_mut(skip)]
128 #[set(skip)]
129 pub(crate) resident_bytes: usize,
130 /// Active font fallback chain revision; bumping it invalidates shaping without touching unrelated caches.
131 #[get(type(copy))]
132 #[get_mut(skip)]
133 #[set(skip)]
134 pub(crate) font_chain_revision: u64,
135 /// Monotonic logical LRU access clock.
136 #[get(skip)]
137 #[get_mut(skip)]
138 #[set(skip)]
139 pub(crate) lru_clock: u64,
140 /// Open-addressed power-of-two shaping lookup table packing `(hash_tag_31 << 32) | slot_idx`.
141 #[get(skip)]
142 #[get_mut(skip)]
143 #[set(skip)]
144 pub(crate) shape_index: Vec<u64>,
145 /// Open-addressed power-of-two line-break lookup table packing `(hash_tag_31 << 32) | slot_idx`.
146 #[get(skip)]
147 #[get_mut(skip)]
148 #[set(skip)]
149 pub(crate) line_break_index: Vec<u64>,
150 /// Contiguous flat arena of shaped glyph run slots.
151 #[get(skip)]
152 #[get_mut(skip)]
153 #[set(skip)]
154 pub(crate) shape_slots: Vec<ShapingRunSlot>,
155 /// Contiguous flat arena of wrapped line-break slots.
156 #[get(skip)]
157 #[get_mut(skip)]
158 #[set(skip)]
159 pub(crate) line_break_slots: Vec<LineBreakSlot>,
160 /// Recycled shaping slot indices for zero-allocation O(1) reuse.
161 #[get(skip)]
162 #[get_mut(skip)]
163 #[set(skip)]
164 pub(crate) shape_free_slots: Vec<u32>,
165 /// Recycled line-break slot indices for zero-allocation O(1) reuse.
166 #[get(skip)]
167 #[get_mut(skip)]
168 #[set(skip)]
169 pub(crate) line_break_free_slots: Vec<u32>,
170 /// Tagged handle of the most-recently-used entry (`SHAPING_NODE_NONE` when empty).
171 #[get(skip)]
172 #[get_mut(skip)]
173 #[set(skip)]
174 pub(crate) lru_head: u32,
175 /// Tagged handle of the least-recently-used eviction victim (`SHAPING_NODE_NONE` when empty).
176 #[get(skip)]
177 #[get_mut(skip)]
178 #[set(skip)]
179 pub(crate) lru_tail: u32,
180 /// Active live entry count in `shape_slots`.
181 #[get(skip)]
182 #[get_mut(skip)]
183 #[set(skip)]
184 pub(crate) shape_count: usize,
185 /// Active live entry count in `line_break_slots`.
186 #[get(skip)]
187 #[get_mut(skip)]
188 #[set(skip)]
189 pub(crate) line_break_count: usize,
190 /// Live plus tombstone slot count in `shape_index` governing load-factor rehash triggers.
191 #[get(skip)]
192 #[get_mut(skip)]
193 #[set(skip)]
194 pub(crate) shape_occupied: usize,
195 /// Live plus tombstone slot count in `line_break_index` governing load-factor rehash triggers.
196 #[get(skip)]
197 #[get_mut(skip)]
198 #[set(skip)]
199 pub(crate) line_break_occupied: usize,
200 /// Cumulative shaping cache hits.
201 #[get(type(copy))]
202 #[get_mut(skip)]
203 #[set(skip)]
204 pub(crate) shaping_hits: u64,
205 /// Cumulative shaping cache misses.
206 #[get(type(copy))]
207 #[get_mut(skip)]
208 #[set(skip)]
209 pub(crate) shaping_misses: u64,
210 /// Cumulative line-break cache hits.
211 #[get(type(copy))]
212 #[get_mut(skip)]
213 #[set(skip)]
214 pub(crate) line_break_hits: u64,
215 /// Cumulative line-break cache misses.
216 #[get(type(copy))]
217 #[get_mut(skip)]
218 #[set(skip)]
219 pub(crate) line_break_misses: u64,
220 /// Cumulative cold entries evicted to stay within `budget_bytes`.
221 #[get(type(copy))]
222 #[get_mut(skip)]
223 #[set(skip)]
224 pub(crate) evictions: u64,
225}