use glam::Vec2;
use super::tests::{frame, idle};
use super::*;
use crate::draw_list::{DrawCommand, DrawList};
use crate::font::face::{SYNTHETIC_BOLD_EM, Synthesis};
use crate::font::{Font, FontMetrics, GlyphId, ReservedFamilyName};
use crate::style::Declaration;
fn fixed_pitch(advance: f32) -> &'static Font {
Font::fixed_pitch(
FontMetrics {
units_per_em: 1000,
ascent: 800.0,
descent: -200.0,
line_gap: 0.0,
},
advance,
)
}
const TEXT: &str = "WIDE";
const SIZE: f32 = 20.0;
fn span_in(ui: &mut Ui, css: &str, class: &str) -> (Vec2, DrawList) {
ui.add_stylesheet("fonts.css", css);
let mut key = None;
frame(ui, idle(), |ui| {
key = Some(ui.span(class, TEXT, &[Declaration::FontSize(SIZE)]).key);
});
let (min, max) = ui.rect(key.expect("built")).expect("laid out");
let mut list = DrawList::new();
ui.emit(&mut list);
(max - min, list)
}
fn glyph_run(list: &DrawList) -> (Font, Vec<crate::font::layout::PositionedGlyph>) {
let runs: Vec<_> = list
.commands()
.iter()
.filter_map(|command| match command {
DrawCommand::Glyphs { font, glyphs, .. } => Some((font.clone(), glyphs.clone())),
_ => None,
})
.collect();
assert_eq!(runs.len(), 1, "not one glyph run: {}", runs.len());
runs.into_iter().next().expect("one run")
}
#[test]
fn a_font_read_at_run_time_draws_and_is_let_go_with_the_tree() {
let bytes: std::sync::Arc<[u8]> = std::sync::Arc::from(crate::font::SANS_TTF);
let font = Font::parse(std::sync::Arc::clone(&bytes)).expect("parses");
let id = font.id();
let mut ui = Ui::new();
ui.register_font("found on disk", font)
.expect("not reserved");
let (_, list) = span_in(
&mut ui,
".a { font-family: \"found on disk\", bitmap; }",
".a",
);
let (drawn, _) = glyph_run(&list);
assert_eq!(drawn.id(), id, "the run is not in the registered font");
assert_eq!(
std::sync::Arc::strong_count(&bytes),
2,
"the font holds the bytes it was given, not a copy"
);
drop((drawn, list, ui));
assert_eq!(
std::sync::Arc::strong_count(&bytes),
1,
"the tree and its draw list let the font go"
);
}
#[test]
fn a_font_registered_by_reference_is_the_same_font() {
let font = Font::parse(crate::font::SANS_TTF).expect("parses");
let mut ui = Ui::new();
ui.register_font("by reference", &font)
.expect("not reserved");
let (_, list) = span_in(
&mut ui,
".a { font-family: \"by reference\", bitmap; }",
".a",
);
assert_eq!(glyph_run(&list).0.id(), font.id());
}
#[test]
fn a_registered_font_measures_the_spans_that_name_it() {
let font = fixed_pitch(500.0);
let css = r#"
.quoted { font-family: "ROBOTO", sans-serif; }
.bare { font-family: roboto, sans-serif; }
.alone { font-family: Roboto; }
.sans { font-family: sans-serif; }
"#;
let mut sans_ui = Ui::new();
let (sans, _) = span_in(&mut sans_ui, css, ".sans");
let want = Vec2::new(4.0 * 0.5 * SIZE, SIZE);
assert_ne!(sans, want, "the fixture measures like sans-serif");
for class in [".quoted", ".bare", ".alone"] {
let mut ui = Ui::new();
ui.register_font("Roboto", font).expect("not reserved");
let (size, _) = span_in(&mut ui, css, class);
assert_eq!(size, want, "{class} did not measure in the registered font");
}
}
#[test]
fn a_registered_font_is_the_font_its_spans_glyph_run_draws_in() {
let font = fixed_pitch(500.0);
let mut ui = Ui::new();
ui.register_font("roboto", font).expect("not reserved");
let (_, list) = span_in(&mut ui, ".a { font-family: roboto, bitmap; }", ".a");
let (drawn, glyphs) = glyph_run(&list);
assert_eq!(
drawn.id(),
font.id(),
"the run is not in the registered font"
);
assert_eq!(glyphs.len(), TEXT.len());
for (at, glyph) in glyphs.iter().enumerate() {
assert_eq!(glyph.glyph, GlyphId(1));
assert_eq!(glyph.offset.x, at as f32 * 0.5 * SIZE);
}
assert!(
!list
.commands()
.iter()
.any(|command| matches!(command, DrawCommand::Text { .. })),
"the span also drew in the bitmap font"
);
}
#[test]
fn an_unregistered_family_draws_in_what_the_list_falls_back_to() {
let other = fixed_pitch(250.0);
let reference = |css: &str| span_in(&mut Ui::new(), css, ".a");
let (sans, _) = reference(".a { font-family: sans-serif; }");
let (bitmap, _) = reference(".a { font-family: bitmap; }");
assert_ne!(sans, bitmap);
let cases = [
(".a { font-family: roboto, sans-serif; }", sans),
(".a { font-family: roboto, bitmap; }", bitmap),
(
".a { font-family: sans-serif; } .a { font-family: roboto; }",
sans,
),
(".a { font-family: roboto; }", bitmap),
];
for (css, want) in cases {
let mut ui = Ui::new();
ui.register_font("inter", other).expect("not reserved");
let (size, list) = span_in(&mut ui, css, ".a");
assert_eq!(size, want, "{css}");
let drew_sans = list
.commands()
.iter()
.any(|command| matches!(command, DrawCommand::Glyphs { .. }));
assert_eq!(drew_sans, want == sans, "{css} drew in the wrong font");
}
let mut ui = Ui::new();
ui.add_stylesheet(
"fonts.css",
".column { font-family: sans-serif; } .a { font-family: roboto; }",
);
let mut key = None;
frame(&mut ui, idle(), |ui| {
ui.block(".column", &[], |ui| {
key = Some(ui.span(".a", TEXT, &[Declaration::FontSize(SIZE)]).key);
});
});
let (min, max) = ui.rect(key.expect("built")).expect("laid out");
assert_eq!(
max - min,
sans,
"an unregistered name dropped the inherited family"
);
}
#[test]
fn registering_a_name_again_replaces_its_font_and_reserved_names_are_refused() {
let narrow = fixed_pitch(250.0);
let wide = fixed_pitch(750.0);
let css = ".a { font-family: roboto, sans-serif; }";
let mut ui = Ui::new();
ui.register_font("roboto", narrow).expect("not reserved");
let (first, _) = span_in(&mut ui, css, ".a");
ui.register_font("ROBOTO", wide).expect("not reserved");
let (second, list) = span_in(&mut ui, css, ".a");
assert_eq!(first.x, 4.0 * 0.25 * SIZE);
assert_eq!(
second.x,
4.0 * 0.75 * SIZE,
"the replaced font's width stuck"
);
assert_eq!(glyph_run(&list).0.id(), wide.id());
for name in [
"",
"bitmap",
"Atkinson Hyperlegible",
"Sans-Serif",
"serif",
"inherit",
] {
assert_eq!(
ui.register_font(name, wide),
Err(ReservedFamilyName(name.to_owned())),
"`{name}` was registered"
);
}
}
#[test]
fn a_tree_glyph_run_scales_its_origin_size_and_pen_offsets() {
let css = ".s { font-family: sans-serif; }";
let mut ui = Ui::new();
let (_, logical) = span_in(&mut ui, css, ".s");
let mut scaled = DrawList::new();
scaled.set_scale(1.5);
ui.emit(&mut scaled);
let run = |list: &DrawList| {
list.commands()
.iter()
.find_map(|command| match command {
DrawCommand::Glyphs {
origin,
size,
glyphs,
text,
..
} => Some((*origin, *size, glyphs.clone(), text.clone())),
_ => None,
})
.expect("the span draws a glyph run")
};
let (origin, size, glyphs, text) = run(&logical);
let (scaled_origin, scaled_size, scaled_glyphs, scaled_text) = run(&scaled);
assert_eq!(scaled_origin, origin * 1.5);
assert_eq!(scaled_size, size * 1.5);
assert_eq!(scaled_text, text);
assert_eq!(scaled_glyphs.len(), glyphs.len());
for (scaled, glyph) in scaled_glyphs.iter().zip(&glyphs) {
assert_eq!(scaled.glyph, glyph.glyph);
assert_eq!(scaled.offset, glyph.offset * 1.5);
}
assert!(
glyphs.iter().any(|glyph| glyph.offset.x > 0.0),
"every pen at zero would pass a missing offset scale"
);
}
fn face(advance: f32, weight: f32, italic: bool) -> &'static Font {
Font::fixed_pitch_face(
FontMetrics {
units_per_em: 1000,
ascent: 800.0,
descent: -200.0,
line_gap: 0.0,
},
advance,
weight,
italic,
)
}
#[test]
fn a_span_draws_in_the_face_its_weight_and_style_select() {
let regular = face(400.0, 400.0, false);
let bold = face(600.0, 700.0, false);
let italic = Synthesis {
bold: false,
italic: true,
};
let css = r"
.a { font-family: family; }
.bold { font-weight: bold; }
.semibold { font-weight: 600; }
.light { font-weight: 300; }
.italic { font-style: italic; }
";
let cases = [
(".a", regular.clone(), 0.4),
(".a.bold", bold.clone(), 0.6),
(".a.semibold", bold.clone(), 0.6),
(".a.light", regular.clone(), 0.4),
(".a.italic", regular.synthesized(italic), 0.4),
(".a.bold.italic", bold.synthesized(italic), 0.6),
];
for (class, want, em) in cases {
let mut ui = Ui::new();
ui.register_font("family", regular).expect("not reserved");
ui.register_font("FAMILY", bold).expect("not reserved");
let (size, list) = span_in(&mut ui, css, class);
assert_eq!(glyph_run(&list).0, want, "{class} drew in the wrong face");
assert_eq!(
size.x,
4.0 * em * SIZE,
"{class} measured in the wrong face"
);
}
}
#[test]
fn a_family_without_the_face_is_synthesised_and_the_bitmap_font_ignores_it() {
let mut ui = Ui::new();
ui.add_stylesheet(
"fonts.css",
".column { font-weight: 700; font-style: italic; } .sans { font-family: sans-serif; } \
.bitmap { font-family: bitmap; }",
);
let mut keys = None;
frame(&mut ui, idle(), |ui| {
let plain = ui.span(".sans", TEXT, &[Declaration::FontSize(SIZE)]).key;
let (mut sans, mut bitmap) = (plain, plain);
ui.block(".column", &[], |ui| {
sans = ui.span(".sans", TEXT, &[Declaration::FontSize(SIZE)]).key;
bitmap = ui.span(".bitmap", TEXT, &[Declaration::FontSize(SIZE)]).key;
});
let flat = ui.span(".bitmap", TEXT, &[Declaration::FontSize(SIZE)]).key;
keys = Some((plain, sans, bitmap, flat));
});
let (plain, sans, bitmap, flat) = keys.expect("built");
let width = |key| {
let (min, max) = ui.rect(key).expect("laid out");
(max - min).x
};
let mut list = DrawList::new();
ui.emit(&mut list);
let fonts: Vec<Font> = list
.commands()
.iter()
.filter_map(|command| match command {
DrawCommand::Glyphs { font, .. } => Some(font.clone()),
_ => None,
})
.collect();
let both = Synthesis {
bold: true,
italic: true,
};
assert_eq!(
fonts,
[Font::sans().clone(), Font::sans().synthesized(both)],
"the inherited weight and style did not synthesise the committed face"
);
let units = f32::from(Font::sans().metrics().units_per_em);
let widened =
TEXT.len() as f32 * units * SYNTHETIC_BOLD_EM * Font::sans().metrics().scale(SIZE);
assert!(
(width(sans) - width(plain) - widened).abs() <= 1.0,
"the bold measured {} wider rather than {widened}",
width(sans) - width(plain)
);
assert_eq!(
width(bitmap),
width(flat),
"the bitmap font took the weight"
);
let texts = list
.commands()
.iter()
.filter(|command| matches!(command, DrawCommand::Text { .. }))
.count();
assert_eq!(texts, 2, "a bitmap span stopped drawing as bitmap text");
}