nitai/projects
noxstrap / src / crates / nox-text / examples / textbench.rs
49 lines · 1.7 KB Raw
1use nox_gfx::canvas::Canvas;
2use nox_gfx::color::hex;
3use nox_text::{draw_text, measure, shape, Font, GlyphCache, TextStyle};
4use std::time::Instant;
5
6const LINE: &str = "Handgloves Roblox 0123 the quick brown fox jumps over the lazy dog";
7
8fn main() {
9 let b = std::fs::read("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf").unwrap();
10 let font = Font::parse(&b).unwrap();
11 let mut c = Canvas::new(1200, 800);
12 let st = TextStyle::new(14.0);
13
14 let mut cache = GlyphCache::new();
15 let t0 = Instant::now();
16 let mut y = 14.0;
17 for _ in 0..50 {
18 draw_text(&mut c, &mut cache, &font, LINE, 10.0, y, st, hex(0xffffff));
19 y += 15.0;
20 if y > 790.0 { y = 14.0; }
21 }
22 println!("cold cache, 50 lines: {:?} ({} glyphs cached)", t0.elapsed(), cache.len());
23
24 let n = 200;
25 let t0 = Instant::now();
26 for i in 0..n {
27 let mut y = 14.0;
28 for _ in 0..50 {
29 draw_text(&mut c, &mut cache, &font, LINE, 10.0 + (i % 3) as f32 * 0.25, y, st, hex(0xffffff));
30 y += 15.0;
31 if y > 790.0 { y = 14.0; }
32 }
33 }
34 let el = t0.elapsed();
35 let glyphs = LINE.chars().count() * 50 * n;
36 println!(
37 "warm: {:.3} ms per 50-line page ({:.1} ns/glyph, {} glyphs total)",
38 el.as_secs_f64() * 1000.0 / n as f64,
39 el.as_nanos() as f64 / glyphs as f64,
40 glyphs
41 );
42
43 let t0 = Instant::now();
44 for _ in 0..2000 { let _ = shape(&font, LINE, st); }
45 println!("shape only: {:.1} us/line", t0.elapsed().as_secs_f64() * 1e6 / 2000.0);
46 let t0 = Instant::now();
47 for _ in 0..2000 { let _ = measure(&font, LINE, st); }
48 println!("measure only: {:.1} us/line", t0.elapsed().as_secs_f64() * 1e6 / 2000.0);
49}