use nox_gfx::canvas::Canvas; use nox_gfx::color::hex; use nox_text::{draw_text, measure, shape, Font, GlyphCache, TextStyle}; use std::time::Instant; const LINE: &str = "Handgloves Roblox 0123 the quick brown fox jumps over the lazy dog"; fn main() { let b = std::fs::read("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf").unwrap(); let font = Font::parse(&b).unwrap(); let mut c = Canvas::new(1200, 800); let st = TextStyle::new(14.0); let mut cache = GlyphCache::new(); let t0 = Instant::now(); let mut y = 14.0; for _ in 0..50 { draw_text(&mut c, &mut cache, &font, LINE, 10.0, y, st, hex(0xffffff)); y += 15.0; if y > 790.0 { y = 14.0; } } println!("cold cache, 50 lines: {:?} ({} glyphs cached)", t0.elapsed(), cache.len()); let n = 200; let t0 = Instant::now(); for i in 0..n { let mut y = 14.0; for _ in 0..50 { draw_text(&mut c, &mut cache, &font, LINE, 10.0 + (i % 3) as f32 * 0.25, y, st, hex(0xffffff)); y += 15.0; if y > 790.0 { y = 14.0; } } } let el = t0.elapsed(); let glyphs = LINE.chars().count() * 50 * n; println!( "warm: {:.3} ms per 50-line page ({:.1} ns/glyph, {} glyphs total)", el.as_secs_f64() * 1000.0 / n as f64, el.as_nanos() as f64 / glyphs as f64, glyphs ); let t0 = Instant::now(); for _ in 0..2000 { let _ = shape(&font, LINE, st); } println!("shape only: {:.1} us/line", t0.elapsed().as_secs_f64() * 1e6 / 2000.0); let t0 = Instant::now(); for _ in 0..2000 { let _ = measure(&font, LINE, st); } println!("measure only: {:.1} us/line", t0.elapsed().as_secs_f64() * 1e6 / 2000.0); }