Skip to main content

webizen_render/
ui_msdf_text.rs

1//! GPU Multi-Channel Signed Distance Field (MSDF) Font Atlas & Zero-Heap UTF-8 Text Shaper (`ui_msdf_text.rs`)
2//!
3//! Re-exports the canonical zero-heap MSDF font atlas and text shaper from `etalon_render::ui_canvas::msdf_text`.
4
5pub use etalon_render::ui_canvas::msdf_text::*;
6
7#[cfg(test)]
8mod tests {
9    use super::*;
10    use bytemuck::Zeroable;
11
12    #[test]
13    fn msdf_shaper_handles_utf8_10d_symbols_kerning_and_word_wrap() {
14        let mut quads = [MsdfTextQuad::zeroed(); 128];
15        let text = "AV α=0.95 μ=1 σ=2.4\n10D Holosonic HUD";
16        let metrics = shape_text_zero_heap(
17            text,
18            10.0,
19            20.0,
20            16.0,
21            1.25,
22            120.0,
23            [1.0, 1.0, 1.0, 1.0],
24            &mut quads,
25        );
26
27        assert!(metrics.quad_count > 10);
28        assert!(
29            metrics.line_count >= 3,
30            "Expected word-wrap + newline to produce >= 3 lines"
31        );
32        assert!(metrics.width_px <= 120.0 + 1e-3);
33        assert!(MSDF_TEXT_WGSL.contains("max(min(r, g), min(max(r, g), b))"));
34    }
35
36    #[test]
37    fn procedural_msdf_atlas_generates_valid_distance_fields() {
38        let mut atlas = vec![0u8; MSDF_ATLAS_WIDTH * MSDF_ATLAS_HEIGHT * 4];
39        let written = generate_procedural_msdf_atlas_into(&mut atlas);
40        assert_eq!(written, MSDF_ATLAS_WIDTH * MSDF_ATLAS_HEIGHT * 4);
41
42        // Verify 'A' (slot 33) has both inside (>128) and outside (<128) MSDF samples
43        let slot_a = glyph_slot_for_char('A');
44        let col = slot_a % MSDF_ATLAS_COLS;
45        let row = slot_a / MSDF_ATLAS_COLS;
46        let mut has_inside = false;
47        let mut has_outside = false;
48        for py in 0..MSDF_CELL_PX {
49            for px in 0..MSDF_CELL_PX {
50                let idx =
51                    ((row * MSDF_CELL_PX + py) * MSDF_ATLAS_WIDTH + (col * MSDF_CELL_PX + px)) * 4;
52                let r = atlas[idx] as f32 / 255.0;
53                let g = atlas[idx + 1] as f32 / 255.0;
54                let b = atlas[idx + 2] as f32 / 255.0;
55                let med = msdf_median_of_three(r, g, b);
56                if med > 0.55 {
57                    has_inside = true;
58                }
59                if med < 0.45 {
60                    has_outside = true;
61                }
62            }
63        }
64        assert!(
65            has_inside && has_outside,
66            "Glyph 'A' should contain both inside and outside SDF regions"
67        );
68    }
69}