agg-gui 0.3.0

Immediate-mode Rust GUI library with AGG rendering, Y-up layout, widgets, text, SVG, and native/WASM adapters
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use super::*;

const FONT_BYTES: &[u8] = include_bytes!("../../../demo/assets/CascadiaCode.ttf");
const FA_BYTES: &[u8] = include_bytes!("../../../demo/assets/fa.ttf");
/// Noto Sans has very broad Unicode coverage (Latin, Greek, Cyrillic,
/// punctuation, currency, math) but deliberately no CJK ideographs — an
/// ideal fixture for asserting `Font::characters()` reports real coverage.
const NOTO_BYTES: &[u8] = include_bytes!("../../assets/fonts/NotoSans-Regular.ttf");

fn test_font() -> Arc<Font> {
    Arc::new(Font::from_slice(FONT_BYTES).expect("font ok"))
}

/// Font-Awesome codepoint U+F109 ("fa-laptop") — used by the demo's
/// backend-panel button label.  The primary font (CascadiaCode) does not
/// cover the FA range, so the fallback chain must carry it.
const FA_LAPTOP: &str = "\u{F109}";

/// A `shape_text` call for a codepoint absent from the primary font must
/// walk the fallback chain and produce the real glyph outline — not the
/// primary font's `.notdef` (the tofu box the top screenshot shows).
#[test]
fn test_shape_text_renders_fa_icon_via_fallback() {
    let fa = Font::from_slice(FA_BYTES).expect("parse fa.ttf");
    let font = Arc::new(
        Font::from_slice(FONT_BYTES)
            .expect("cc")
            .with_fallback(Arc::new(fa)),
    );

    // shape_glyphs must agree the glyph was resolved via fallback.
    let shaped = shape_glyphs(&font, FA_LAPTOP, 16.0);
    assert_eq!(shaped.len(), 1);
    assert!(
        shaped[0].fallback_font.is_some(),
        "FA codepoint must resolve via fallback font"
    );

    // shape_text must return a non-empty path for that glyph.
    let (paths, _adv) = shape_text(&font, FA_LAPTOP, 16.0, 0.0, 0.0);
    assert_eq!(
        paths.len(),
        1,
        "fallback outline must yield exactly one PathStorage for FA_LAPTOP"
    );
}

/// The outline returned by `shape_text` for a codepoint missing from the
/// primary font must match the fallback font's outline — not the primary
/// font's `.notdef`.  Compare flattened bounding boxes.
#[test]
fn test_shape_text_fa_outline_matches_fallback_font() {
    use agg_rust::basics::{is_stop, VertexSource};
    use agg_rust::conv_curve::ConvCurve;

    let fa_arc = Arc::new(Font::from_slice(FA_BYTES).expect("fa"));
    let font = Arc::new(
        Font::from_slice(FONT_BYTES)
            .expect("cc")
            .with_fallback(Arc::clone(&fa_arc)),
    );

    // Outline via the fallback-aware shape_text.
    let (mut paths, _) = shape_text(&font, FA_LAPTOP, 48.0, 0.0, 0.0);
    assert_eq!(paths.len(), 1);
    let mut curves = ConvCurve::new(&mut paths[0]);
    curves.rewind(0);

    let (mut xmin, mut xmax) = (f64::INFINITY, f64::NEG_INFINITY);
    loop {
        let (mut cx, mut cy) = (0.0, 0.0);
        let cmd = curves.vertex(&mut cx, &mut cy);
        if is_stop(cmd) {
            break;
        }
        if cx < xmin {
            xmin = cx;
        }
        if cx > xmax {
            xmax = cx;
        }
        let _ = cy;
    }
    let width = xmax - xmin;

    // FA's "laptop" glyph is full-width at 48 px; the CascadiaCode .notdef
    // (tofu) is closer to advance-width (~24 px).  A width over 32 px at
    // size 48 proves we took the fallback outline, not .notdef.
    assert!(
        width > 32.0,
        "FA glyph outline width at 48 px was {width:.1} — too narrow, \
         likely still rendering CascadiaCode .notdef instead of FA fallback"
    );
}

/// Verify that shape_and_flatten_text produces a sane number of
/// contour points at typical UI font sizes.
///
/// Before the fix, subdivide_quad tested flatness in font units
/// (~2048 upm), producing ~1000 sub-divisions per Bézier segment
/// instead of ~4 — this test would time-out or produce millions of
/// points under the broken implementation.
#[test]
fn test_flatten_point_count_is_sane() {
    let font = test_font();
    let sizes: &[f64] = &[10.0, 13.0, 14.0, 24.0, 34.0];
    let texts: &[&str] = &[
        "Hello",
        "The quick brown fox",
        "Caption — 10px  The quick brown fox",
        "agg-gui",
        "Aa",
    ];

    for &size in sizes {
        for &text in texts {
            let contours = shape_and_flatten_text(&font, text, size, 0.0, 0.0, 0.5);

            let total_pts: usize = contours.iter().map(|c| c.len()).sum();
            let char_count = text.chars().count().max(1);
            let pts_per_char = total_pts / char_count;

            // A well-formed glyph at any typical size should produce
            // between 4 and 300 points per character.  Anything above
            // ~500 means over-subdivision is happening again.
            assert!(
                pts_per_char <= 500,
                "size={size} text={text:?}: {pts_per_char} pts/char \
                 (total {total_pts}) — too many, subdivision loop likely"
            );
            assert!(
                total_pts > 0 || text.trim().is_empty(),
                "size={size} text={text:?}: zero points produced"
            );
        }
    }
}

/// Print raw contour coordinates for a single character.
#[test]
fn test_dump_single_char_coords() {
    use crate::gl_renderer::tessellate_fill;
    let font = test_font();
    for ch in ['W', 'i', 'd', 'g', 'e', 't', 's'] {
        let s = ch.to_string();
        let contours = shape_and_flatten_text(&font, &s, 13.0, 10.0, 50.0, 0.5);
        let total: usize = contours.iter().map(|c| c.len()).sum();
        eprintln!("{:?}: {} contours, {} pts", ch, contours.len(), total);
        // Print bounding box of each contour
        for (ci, c) in contours.iter().enumerate() {
            if c.is_empty() {
                continue;
            }
            let xs: Vec<f32> = c.iter().map(|p| p[0]).collect();
            let ys: Vec<f32> = c.iter().map(|p| p[1]).collect();
            let xmin = xs.iter().cloned().fold(f32::INFINITY, f32::min);
            let xmax = xs.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
            let ymin = ys.iter().cloned().fold(f32::INFINITY, f32::min);
            let ymax = ys.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
            eprintln!(
                "  contour {ci}: {}/{} pts  x:[{xmin:.1},{xmax:.1}] y:[{ymin:.1},{ymax:.1}]",
                c.len(),
                c.len()
            );
        }
        let result = tessellate_fill(&contours);
        eprintln!(
            "  tess: {:?}",
            result.as_ref().map(|(v, i)| (v.len() / 2, i.len() / 3))
        );
    }
}

/// Simulate the text draw calls that happen on the very first WASM
/// render frame (Basics tab + window visible) and assert the full
/// pipeline (shape → flatten → tessellate) completes in < 200 ms.
///
/// This test catches both infinite-subdivision loops and algorithmic
/// slowness that would cause a tab-kill dialog in the browser.
/// WASM is ~5× slower than native, so 200 ms native ≈ 1 s WASM — fine.
#[test]
fn test_first_frame_text_pipeline_is_fast() {
    use crate::gl_renderer::tessellate_fill;
    use std::time::Instant;

    let font = test_font();
    let t0 = Instant::now();

    // All fill_text calls expected on the first rendered frame:
    //   tab bar (TabView), window title + label (Window),
    //   button labels (Button), text field placeholders (TextField).
    let calls: &[(&str, f64)] = &[
        // tab bar labels (13 pt)
        ("Basics", 13.0),
        ("Widgets", 13.0),
        ("Text", 13.0),
        ("Layout", 13.0),
        ("Tree", 13.0),
        // floating window
        ("3D Demo", 16.0),
        ("WebGL2 — rotating cube", 11.0),
        // Basics tab buttons
        ("Primary Action", 14.0),
        ("Secondary", 14.0),
        ("Destructive", 14.0),
        // text field placeholders
        ("Type something\u{2026}", 14.0),
        ("Another field", 14.0),
    ];

    let mut total_pts = 0usize;
    let mut total_tris = 0usize;

    for &(text, size) in calls {
        let contours = shape_and_flatten_text(&font, text, size, 10.0, 50.0, 0.5);
        total_pts += contours.iter().map(|c| c.len()).sum::<usize>();

        if let Some((verts, idx)) = tessellate_fill(&contours) {
            total_tris += idx.len() / 3;
            let _ = verts;
        }
    }

    let elapsed = t0.elapsed();

    // Sanity: we should have produced some geometry.
    assert!(total_pts > 0, "no contour points produced");
    assert!(total_tris > 0, "no triangles tessellated");

    // Performance gate: must finish in under 200 ms natively.
    assert!(
        elapsed.as_millis() < 200,
        "first-frame text pipeline took {}ms (pts={total_pts} tris={total_tris}) — \
         too slow, would hang browser (WASM is ~5× slower)",
        elapsed.as_millis()
    );

    eprintln!(
        "first-frame text: {total_pts} pts, {total_tris} tris in {}ms",
        elapsed.as_millis()
    );
}

/// Verify shape_glyphs returns the right number of glyphs with positive advances.
#[test]
fn test_shape_glyphs_basic() {
    let font = test_font();
    let glyphs = shape_glyphs(&font, "Hi", 14.0);
    assert_eq!(glyphs.len(), 2, "two glyphs for 'Hi'");
    assert!(glyphs[0].x_advance > 0.0, "H has positive advance");
    assert!(glyphs[1].x_advance > 0.0, "i has positive advance");
}

/// flatten_glyph_at_origin must produce coords in glyph-local pixel space
/// (roughly 0..size range), not in font units (hundreds–thousands).
#[test]
fn test_flatten_glyph_at_origin_local_coords() {
    let font = test_font();
    let size = 16.0_f64;
    let glyphs = shape_glyphs(&font, "H", size);
    assert!(!glyphs.is_empty());
    let gid = glyphs[0].glyph_id;

    let contours = flatten_glyph_at_origin(&font, gid, size).expect("'H' must have an outline");
    assert!(!contours.is_empty(), "should produce at least one contour");

    for contour in &contours {
        for &[x, y] in contour {
            assert!(
                x >= -2.0 && x <= size as f32 + 4.0,
                "x={x} should be in glyph-local pixels for size={size}"
            );
            assert!(
                y >= -size as f32 * 0.3 && y <= size as f32 * 1.2,
                "y={y} should be in glyph-local pixels for size={size}"
            );
        }
    }
}

/// Space has no outline; flatten_glyph_at_origin should return None.
#[test]
fn test_flatten_glyph_at_origin_space_returns_none() {
    let font = test_font();
    let glyphs = shape_glyphs(&font, " ", 14.0);
    assert_eq!(glyphs.len(), 1);
    let result = flatten_glyph_at_origin(&font, glyphs[0].glyph_id, 14.0);
    assert!(
        result.is_none(),
        "space glyph should have no outline, got {:?}",
        result.as_ref().map(|c| c.len())
    );
}

/// `glyph_visual_bounds` must return the actual glyph extent, not the
/// font's worst-case ascender/descender. For Font Awesome's "crosshairs"
/// glyph at 14 pt the real height should be noticeably less than the
/// font ascender — that's what makes the per-glyph bbox useful for
/// vertically-centring icons in buttons. If this test stops finding a
/// gap, the centring fix in `Button::paint_icon` will silently regress
/// back to "icon floats to the top".
#[test]
fn glyph_visual_bounds_is_tighter_than_font_metric_for_fa_icon() {
    let fa = Font::from_slice(FA_BYTES).expect("parse fa.ttf");
    let size = 14.0;
    let (y_min, y_max) = fa
        .glyph_visual_bounds('\u{F05B}', size)
        .expect("FA crosshairs glyph must have an outline");
    let glyph_height = y_max - y_min;
    let font_height = fa.ascender_px(size) + fa.descender_px(size);
    assert!(glyph_height > 0.0, "glyph height should be positive");
    assert!(
        glyph_height < font_height,
        "glyph height ({glyph_height}) must be tighter than font ascender+descender ({font_height})"
    );
    // Glyph extent shouldn't exceed the em-size by more than a hair
    // (FA glyphs live inside the design space).
    assert!(
        glyph_height <= size * 1.05,
        "glyph height should fit inside the em-box, got {glyph_height} at size {size}"
    );
}

/// Glyphs absent from the font (or with no outline, like a space) must
/// return `None` so callers can fall back to a font-metric estimate
/// without panicking.
#[test]
fn glyph_visual_bounds_returns_none_for_outlineless_glyph() {
    let font = test_font();
    // ASCII space — has an advance but no outline.
    assert!(font.glyph_visual_bounds(' ', 14.0).is_none());
}

/// `Font::characters()` must enumerate the real `cmap` coverage: common
/// letters/digits and a spread of symbols are present, an ideograph the
/// Latin font lacks is absent, and the count is plausibly large (not the
/// old hard-coded ~80).
#[test]
fn characters_reports_real_cmap_coverage() {
    let font = Font::from_slice(NOTO_BYTES).expect("parse NotoSans");
    let chars = font.characters();

    // Letters, digits.
    for ch in ['A', 'z', '0', '9'] {
        assert!(chars.contains(&ch), "expected {ch:?} in Noto Sans");
    }
    // Known symbols from Latin-1 Supplement / Greek / currency.
    for ch in ['©', '±', '×', '', 'α'] {
        assert!(chars.contains(&ch), "expected symbol {ch:?} in Noto Sans");
    }

    // A CJK ideograph is NOT in the Latin Noto Sans, and neither is the
    // unassigned max code point — proves we report actual coverage, not a
    // superset.
    assert!(
        !chars.contains(&''),
        "Latin Noto Sans must not claim CJK coverage"
    );
    assert!(
        !chars.contains(&'\u{10FFFF}'),
        "unmapped code point must be absent"
    );

    // Plausible count — Noto Sans carries thousands of glyphs. The exact
    // number is intentionally not asserted so a font update can't break it;
    // only that it dwarfs the old 80-glyph hard-coded set.
    assert!(
        chars.len() > 1000 && chars.len() < 100_000,
        "glyph count {} is implausible",
        chars.len()
    );
}

/// The fallback chain contributes its glyphs to `characters()`, mirroring how
/// the text renderer resolves missing code points through the fallback.
#[test]
fn characters_includes_fallback_glyphs() {
    let cascadia_only = Font::from_slice(FONT_BYTES).expect("cc");
    // The FA laptop code point lives only in fa.ttf, not CascadiaCode.
    let fa_laptop = FA_LAPTOP.chars().next().unwrap();
    assert!(
        !cascadia_only.characters().contains(&fa_laptop),
        "primary font alone must not cover the FA code point"
    );

    let fa = Font::from_slice(FA_BYTES).expect("fa");
    let with_fallback = Font::from_slice(FONT_BYTES)
        .expect("cc")
        .with_fallback(Arc::new(fa));
    assert!(
        with_fallback.characters().contains(&fa_laptop),
        "fallback glyph must appear once the fallback is chained"
    );
}

/// Verify that all contour points are in screen-pixel range for the
/// given font size (not left in raw font units).
#[test]
fn test_flatten_output_is_in_screen_space() {
    let font = test_font();
    // Place text at (100, 200) at size 16.
    let contours = shape_and_flatten_text(&font, "Hello", 16.0, 100.0, 200.0, 0.5);

    assert!(!contours.is_empty(), "should produce contours for 'Hello'");

    for (ci, contour) in contours.iter().enumerate() {
        for &[x, y] in contour {
            // Screen-space points should be near (100±50, 200±30) at 16pt.
            // Font-unit coordinates would be in the hundreds–thousands.
            assert!(
                x > 50.0 && x < 300.0,
                "contour {ci}: x={x} looks like font units, not screen px"
            );
            assert!(
                y > 150.0 && y < 280.0,
                "contour {ci}: y={y} looks like font units, not screen px"
            );
        }
    }
}