use std::sync::OnceLock;
use glam::Vec2;
use super::tests::{frame, idle};
use super::*;
use crate::draw_list::{DrawCommand, DrawList};
use crate::font::fixtures::cbdt;
use crate::font::{Font, FontMetrics};
use crate::style::Declaration;
const SIZE: f32 = 20.0;
const NARROW: f32 = SIZE / 2.0;
const WIDE: f32 = SIZE;
fn primary() -> &'static Font {
static PRIMARY: OnceLock<&'static Font> = OnceLock::new();
PRIMARY.get_or_init(|| {
Font::fixed_pitch(
FontMetrics {
units_per_em: 1000,
ascent: 800.0,
descent: -200.0,
line_gap: 0.0,
},
500.0,
)
})
}
fn tree() -> Ui {
let mut ui = Ui::new();
ui.register_font("primary", primary())
.expect("not reserved");
ui.set_fallback_fonts([cbdt()]);
ui.add_stylesheet(
"fallback.css",
&format!(".t {{ font-family: primary; font-size: {SIZE}px; }}"),
);
ui
}
fn runs(list: &DrawList) -> Vec<(Font, String, Vec<Vec2>)> {
list.commands()
.iter()
.filter_map(|command| match command {
DrawCommand::Glyphs {
origin,
font,
glyphs,
text,
..
} => Some((
font.clone(),
text.as_deref().unwrap_or_default().to_owned(),
glyphs.iter().map(|glyph| *origin + glyph.offset).collect(),
)),
_ => None,
})
.collect()
}
#[test]
fn a_span_measures_and_draws_each_char_in_its_fallback_face() {
let mut ui = tree();
let text = "a\u{1F600}\u{FE0F} b";
let mut key = None;
frame(&mut ui, idle(), |ui| {
key = Some(ui.span(".t", text, &[]).key);
});
let (min, max) = ui.rect(key.expect("built")).expect("laid out");
assert_eq!(max.x - min.x, NARROW + WIDE + NARROW + NARROW);
let mut list = DrawList::new();
ui.emit(&mut list);
let drawn = runs(&list);
let fonts: Vec<&Font> = drawn.iter().map(|(font, _, _)| font).collect();
assert_eq!(fonts, [primary(), &cbdt(), primary()]);
let texts: Vec<&str> = drawn.iter().map(|(_, text, _)| text.as_str()).collect();
assert_eq!(texts, ["a", "\u{1F600}\u{FE0F} ", "b"]);
assert_eq!(texts.concat(), text);
let pens: Vec<f32> = drawn
.iter()
.flat_map(|(_, _, pens)| pens.iter().map(|pen| pen.x - min.x))
.collect();
assert_eq!(pens, [0.0, NARROW, NARROW + WIDE + NARROW]);
let smile = cbdt().glyph_id('\u{1F600}');
let DrawCommand::Glyphs { glyphs, .. } = &list.commands()[1] else {
panic!("the emoji is the second command");
};
assert_eq!(glyphs[0].glyph, smile);
}
#[test]
fn a_paragraph_falls_back_and_hits_on_the_fallback_advance() {
let mut ui = tree();
let mut keys = Vec::new();
frame(&mut ui, idle(), |ui| {
ui.paragraph(".t", &[], |p| {
keys.push(p.run("", "x\u{1F600}", &[]).key);
keys.push(p.run("", "y", &[]).key);
});
});
let (min, max) = ui.rect(keys[0]).expect("laid out");
assert_eq!(max.x - min.x, NARROW + WIDE);
let (next, _) = ui.rect(keys[1]).expect("laid out");
assert_eq!(next.x - min.x, NARROW + WIDE);
let mut list = DrawList::new();
ui.emit(&mut list);
let drawn = runs(&list);
let summary: Vec<(bool, &str)> = drawn
.iter()
.map(|(font, text, _)| (font == primary(), text.as_str()))
.collect();
assert_eq!(summary, [(true, "x"), (false, "\u{1F600}"), (true, "y")]);
}
#[test]
fn changing_the_fallback_list_measures_again() {
let mut ui = tree();
let width = |ui: &mut Ui| {
let mut key = None;
frame(ui, idle(), |ui| {
key = Some(
ui.span(".t", "a\u{1F600}", &[Declaration::FontSize(SIZE)])
.key,
);
});
let (min, max) = ui.rect(key.expect("built")).expect("laid out");
max.x - min.x
};
assert_eq!(width(&mut ui), NARROW + WIDE);
ui.set_fallback_fonts(Vec::<Font>::new());
assert!(ui.fallback_fonts().is_empty());
assert_eq!(width(&mut ui), NARROW);
ui.set_fallback_fonts([cbdt()]);
assert_eq!(width(&mut ui), NARROW + WIDE);
}
#[test]
fn an_emoji_in_a_span_resolves_to_a_colour_quad() {
use crate::font::color::ColorGlyphCache;
use crate::image::ImageAtlas;
let mut ui = tree();
let mut key = None;
frame(&mut ui, idle(), |ui| {
key = Some(ui.span(".t", "ok \u{1F600}", &[]).key);
});
let (min, _) = ui.rect(key.expect("built")).expect("laid out");
let mut list = DrawList::new();
ui.emit(&mut list);
let (mut cache, mut images) = (ColorGlyphCache::new(), ImageAtlas::new());
cache.begin_frame(&mut images);
list.resolve_color_glyphs(&mut cache, &mut images, 1.0);
let quads: Vec<(Vec2, Vec2)> = list
.commands()
.iter()
.filter_map(|command| match command {
DrawCommand::Image { min, max, .. } => Some((*min, *max)),
_ => None,
})
.collect();
let baseline = runs(&list)[0].2[0].y;
assert_eq!(quads.len(), 1);
let (quad_min, quad_max) = quads[0];
assert_eq!(quad_min.x, (min.x + 3.0 * NARROW).round());
assert_eq!(quad_min.y, baseline.round() - (WIDE * 0.75).round());
assert_eq!(quad_max - quad_min, Vec2::splat(WIDE));
}