1use crate::renderer::GpuRenderer;
2use crate::vertex::{InstanceData, Vertex};
3use cvkg_core::Rect;
4use std::sync::Arc;
5
6impl GpuRenderer {
7 pub fn clear_text_cache_impl(&mut self) {
9 self.text.shaped_cache.clear();
10 }
11
12 pub(crate) fn measure_text_impl(&mut self, text: &str, size: f32) -> (f32, f32) {
14 let cache_key = (text.to_string(), (size * 100.0) as u32);
15 if let Some(shaped) = self.text.shaped_cache.get(&cache_key) {
16 return (shaped.width, shaped.height);
17 }
18 let style = cvkg_runic_text::TextStyle::new("Inter", size);
19 let spans = [cvkg_runic_text::TextSpan::new(text, style)];
20 if let Some(shaped) = self.shape_rich_text_impl(
21 &spans,
22 None,
23 cvkg_runic_text::TextAlign::Start,
24 cvkg_runic_text::TextOverflow::Visible,
25 ) {
26 let shaped = std::sync::Arc::new(shaped);
27 let result = (shaped.width, shaped.height);
28 self.text.shaped_cache.put(cache_key, shaped);
29 result
30 } else {
31 (0.0, 0.0)
32 }
33 }
34
35 pub(crate) fn shape_rich_text_impl(
37 &mut self,
38 spans: &[cvkg_runic_text::TextSpan],
39 max_width: Option<f32>,
40 align: cvkg_runic_text::TextAlign,
41 overflow: cvkg_runic_text::TextOverflow,
42 ) -> Option<cvkg_runic_text::ShapedText> {
43 let sf = self.current_scale_factor();
44 let mut scaled_spans = spans.to_vec();
45 for span in &mut scaled_spans {
46 span.style.font_size *= sf;
47 if span.style.fallback_families.is_empty() {
48 span.style.fallback_families = vec![
49 "SF Pro".to_string(),
50 "Inter".to_string(),
51 "Helvetica Neue".to_string(),
52 "Helvetica".to_string(),
53 "Arial".to_string(),
54 "sans-serif".to_string(),
55 ];
56 }
57 }
58 let scaled_max_width = max_width.map(|w| w * sf);
59 self.text
60 .engine
61 .shape_layout(&scaled_spans, scaled_max_width, align, overflow)
62 .ok()
63 }
64
65 pub(crate) fn draw_shaped_text_impl(
67 &mut self,
68 shaped: &cvkg_runic_text::ShapedText,
69 x: f32,
70 y: f32,
71 ) {
72 for glyph in &shaped.glyphs {
73 let byte_idx = shaped
74 .grapheme_boundaries
75 .get(glyph.cluster as usize)
76 .copied()
77 .unwrap_or(0);
78 let mut span_color = [1.0, 1.0, 1.0, 1.0];
79 for span in &shaped.spans {
80 if byte_idx >= span.byte_offset && byte_idx < span.byte_offset + span.text.len() {
81 span_color = [
82 span.style.color[0] as f32 / 255.0,
83 span.style.color[1] as f32 / 255.0,
84 span.style.color[2] as f32 / 255.0,
85 span.style.color[3] as f32 / 255.0,
86 ];
87 break;
88 }
89 }
90 let c = self.apply_opacity(span_color);
91
92 let cache_key = glyph.cache_key;
93 let (uv_rect, w, h, x_off, y_off) = if let Some(info) =
94 self.text.glyph_cache.get(&cache_key)
95 {
96 *info
97 } else {
98 if let Some(image) = self.text.engine.rasterize(cache_key) {
99 let glyph_id = image.glyph_id;
100 let data_len = image.data.len();
101 let gw = image.width;
102 let gh = image.height;
103 let x_offset = image.x_offset;
104 let y_offset = image.y_offset;
105 let (rgba_data, gw, gh) = glyph_image_to_rgba(image);
106 if gw == 0 || gh == 0 {
107 let info = (Rect::zero(), 0.0, 0.0, 0.0, 0.0);
108 self.text.glyph_cache.put(cache_key, info);
109 continue;
110 }
111 if rgba_data.is_empty() {
112 tracing::warn!(
113 "Glyph rasterizer returned unsupported pixel format for glyph {} ({} bytes, {}x{}), skipping",
114 glyph_id,
115 data_len,
116 gw,
117 gh
118 );
119 continue;
120 }
121
122 let pack_res = self.heim_packer.pack(gw, gh);
123 let (nx, ny) = if let Some(pos) = pack_res {
124 pos
125 } else {
126 self.reclaim_vram();
127 match self.heim_packer.pack(gw, gh) {
128 Some(pos) => pos,
129 None => {
130 tracing::error!(
131 "Glyph heim critically full after reclaim: cannot pack {}x{} glyph, skipping",
132 gw,
133 gh
134 );
135 continue;
136 }
137 }
138 };
139
140 self.queue.write_texture(
141 wgpu::TexelCopyTextureInfo {
142 texture: &self.mega_heim_tex,
143 mip_level: 0,
144 origin: wgpu::Origin3d { x: nx, y: ny, z: 0 },
145 aspect: wgpu::TextureAspect::All,
146 },
147 &rgba_data,
148 wgpu::TexelCopyBufferLayout {
149 offset: 0,
150 bytes_per_row: Some(gw * 4),
151 rows_per_image: Some(gh),
152 },
153 wgpu::Extent3d {
154 width: gw,
155 height: gh,
156 depth_or_array_layers: 1,
157 },
158 );
159
160 let tex_w = self.mega_heim_tex.width() as f32;
161 let tex_h = self.mega_heim_tex.height() as f32;
162 let info = (
163 Rect {
164 x: nx as f32 / tex_w,
165 y: ny as f32 / tex_h,
166 width: gw as f32 / tex_w,
167 height: gh as f32 / tex_h,
168 },
169 gw as f32,
170 gh as f32,
171 x_offset,
172 y_offset,
173 );
174 self.text.glyph_cache.put(cache_key, info);
175 info
176 } else {
177 (Rect::zero(), 0.0, 0.0, 0.0, 0.0)
178 }
179 };
180
181 if w > 0.0 {
182 let sf = self.current_scale_factor();
183 let glyph_rect = Rect {
184 x: x + (glyph.x + x_off) / sf,
185 y: y + (glyph.y - y_off) / sf,
186 width: w / sf,
187 height: h / sf,
188 };
189 let tid = self.get_texture_id("__mega_heim");
190 let slice = self
191 .slice_stack
192 .last()
193 .copied()
194 .map(|(a, o)| [a, o, 1.0, 1.0])
195 .unwrap_or([0.0, 0.0, 0.0, 1.0]);
196 self.fill_rect_with_full_params_and_slice(
197 glyph_rect,
198 c,
199 6,
200 tid,
201 0.0,
202 uv_rect,
203 slice,
204 [glyph.glyph_index as f32, glyph.time_offset],
205 );
206 }
207 }
208 }
209
210 pub(crate) fn draw_text_impl(
212 &mut self,
213 text: &str,
214 x: f32,
215 y: f32,
216 size: f32,
217 color: [f32; 4],
218 ) {
219 let cache_key = (text.to_string(), (size * 100.0) as u32);
220 let r = (color[0] * 255.0).clamp(0.0, 255.0) as u8;
221 let g = (color[1] * 255.0).clamp(0.0, 255.0) as u8;
222 let b = (color[2] * 255.0).clamp(0.0, 255.0) as u8;
223 let a = (color[3] * 255.0).clamp(0.0, 255.0) as u8;
224 let cached = self.text.shaped_cache.get(&cache_key).cloned();
225 if let Some(shaped) = cached {
226 let color_matches = shaped
227 .spans
228 .first()
229 .map(|s| s.style.color == [r, g, b, a])
230 .unwrap_or(false);
231 if color_matches {
232 self.draw_shaped_text_impl(&shaped, x, y);
233 return;
234 }
235 let mut shaped = (*shaped).clone();
236 for span in &mut shaped.spans {
237 span.style.color = [r, g, b, a];
238 }
239 self.draw_shaped_text_impl(&shaped, x, y);
240 return;
241 }
242 let mut style = cvkg_runic_text::TextStyle::new("Inter", size);
243 style.color = [r, g, b, a];
244 let spans = [cvkg_runic_text::TextSpan::new(text, style)];
245 if let Some(shaped) = self.shape_rich_text_impl(
246 &spans,
247 None,
248 cvkg_runic_text::TextAlign::Start,
249 cvkg_runic_text::TextOverflow::Visible,
250 ) {
251 let shaped = std::sync::Arc::new(shaped);
252 self.draw_shaped_text_impl(&shaped, x, y);
253 self.text.shaped_cache.put(cache_key, shaped);
254 }
255 }
256}
257
258fn glyph_image_to_rgba(image: cvkg_runic_text::GlyphImage) -> (Vec<u8>, u32, u32) {
259 let width = image.width;
260 let height = image.height;
261 let pixels = width.saturating_mul(height) as usize;
262
263 if pixels == 0 || image.data.is_empty() {
264 return (Vec::new(), width, height);
265 }
266
267 let (bytes_per_pixel, remainder) = (image.data.len() / pixels, image.data.len() % pixels);
268 if remainder != 0 {
269 tracing::warn!(
270 "Glyph rasterizer returned {} bytes for {}x{} glyph; expected whole pixels ({} bytes per pixel)",
271 image.data.len(),
272 width,
273 height,
274 bytes_per_pixel
275 );
276 return (Vec::new(), width, height);
277 }
278
279 let rgba_data = match bytes_per_pixel {
280 1 => {
281 let mut data = Vec::with_capacity(pixels * 4);
282 for alpha in &image.data {
283 data.push(255);
284 data.push(255);
285 data.push(255);
286 data.push(*alpha);
287 }
288 data
289 }
290 3 => {
291 let mut data = Vec::with_capacity(pixels * 4);
292 for rgb in image.data.chunks_exact(3) {
293 let alpha = rgb.iter().copied().max().unwrap_or(0);
294 data.push(255);
295 data.push(255);
296 data.push(255);
297 data.push(alpha);
298 }
299 data
300 }
301 4 => {
302 let mut data = image.data;
303 for chunk in data.chunks_exact_mut(4) {
304 if chunk[3] == 0 && (chunk[0] > 0 || chunk[1] > 0 || chunk[2] > 0) {
305 chunk[3] = chunk[0].max(chunk[1]).max(chunk[2]);
306 }
307 }
308 data
309 }
310 _ => {
311 tracing::warn!(
312 "Glyph rasterizer returned unsupported {} bytes per pixel for {}x{} glyph ({} bytes total)",
313 bytes_per_pixel,
314 width,
315 height,
316 image.data.len()
317 );
318 Vec::new()
319 }
320 };
321
322 (rgba_data, width, height)
323}
324
325#[cfg(test)]
326mod tests {
327 use super::glyph_image_to_rgba;
328
329 #[test]
330 fn glyph_image_to_rgba_keeps_rgba_color_data() {
331 let image = cvkg_runic_text::GlyphImage {
332 glyph_id: 1,
333 width: 2,
334 height: 1,
335 data: vec![1, 2, 3, 4, 5, 6, 7, 8],
336 x_offset: 0.0,
337 y_offset: 0.0,
338 cache_key: 42,
339 };
340
341 assert_eq!(
342 glyph_image_to_rgba(image),
343 (vec![1, 2, 3, 4, 5, 6, 7, 8], 2, 1)
344 );
345 }
346
347 #[test]
348 fn glyph_image_to_rgba_expands_grayscale_alpha() {
349 let image = cvkg_runic_text::GlyphImage {
350 glyph_id: 1,
351 width: 3,
352 height: 1,
353 data: vec![0, 128, 255],
354 x_offset: 0.0,
355 y_offset: 0.0,
356 cache_key: 42,
357 };
358
359 assert_eq!(
360 glyph_image_to_rgba(image),
361 (
362 vec![255, 255, 255, 0, 255, 255, 255, 128, 255, 255, 255, 255],
363 3,
364 1
365 )
366 );
367 }
368
369 #[test]
370 fn glyph_image_to_rgba_collapses_subpixel_rgb_to_alpha() {
371 let image = cvkg_runic_text::GlyphImage {
372 glyph_id: 1,
373 width: 2,
374 height: 1,
375 data: vec![0, 128, 255, 255, 0, 64],
376 x_offset: 0.0,
377 y_offset: 0.0,
378 cache_key: 42,
379 };
380
381 assert_eq!(
382 glyph_image_to_rgba(image),
383 (vec![255, 255, 255, 255, 255, 255, 255, 255], 2, 1)
384 );
385 }
386}