Skip to main content

wisp_storybook/stories/
s_text_presets.rs

1//! Story: gallery of `TextPreset` styles in one scene (M-TEXT.12 /
2//! AUT-86). Each preset renders its label using its own style so the
3//! gallery reads as the preset names themselves.
4
5use glam::Vec2;
6use wisp::application::Application;
7use wisp::text::{TextPreset, TextTexturePipeline, WispText};
8use wisp::{Sprite, Stage};
9
10use crate::story::Story;
11
12pub fn story() -> Story {
13    Story {
14        id: "text-presets",
15        category: "Text",
16        title: "Text style presets",
17        milestone: "M-TEXT.12",
18        writeup: include_str!("writeups/text_presets.md"),
19        build,
20        tick: None,
21    }
22}
23
24fn build(app: &Application, stage: &mut Stage) {
25    let pipeline = TextTexturePipeline::new(app, wgpu::TextureFormat::Rgba8UnormSrgb);
26    let root = stage.root();
27
28    // 7 presets stacked vertically. Storybook export is 256×256, so
29    // each row gets ≈0.27 NDC of vertical space.
30    let presets = TextPreset::all();
31    let row_count = u8::try_from(presets.len()).unwrap_or(1);
32    let row_height = 2.0 / f32::from(row_count); // NDC ∈ [-1, +1]
33
34    for (i, preset) in presets.iter().enumerate() {
35        let style = preset.style();
36        let text = WispText::new(preset.label()).with_style(style);
37        let rt = pipeline.render(app, &text, 1024, 224);
38
39        // Place this row at center-y for slot i, top→bottom.
40        let idx = u8::try_from(i).unwrap_or(0);
41        let center_y = 1.0 - row_height * (f32::from(idx) + 0.5);
42        let mut sprite = Sprite::from_texture(rt.as_texture()).with_anchor(Vec2::splat(0.5));
43        sprite.container.transform.position = Vec2::new(0.0, center_y);
44        // Each row is 1024×224 ≈ 4.5:1, so width-scale 1.7 fits and the
45        // matching height-scale ≈ 1.7 / 4.5 ≈ 0.38 preserves aspect. We
46        // intentionally squash a bit so all rows fit cleanly.
47        sprite.container.transform.scale = Vec2::new(1.7, -row_height * 0.85);
48        let _ = stage.add_child(root, sprite);
49    }
50}