use super::*;
use crate::font::{SANS_TTF, fixtures};
const SIZE: f32 = 16.0;
const LINE: f32 = 20.0;
const TEXT: &str = "The quick brown fox jumps";
fn harfrust(piece: &str) -> Vec<(u32, i32)> {
let font = harfrust::Font::new(SANS_TTF, 0).expect("reads");
let mut buffer = harfrust::Buffer::new();
buffer.push_str(piece);
buffer.guess_segment_properties();
harfrust::shape(
&harfrust::ShaperFont::new(&font),
&mut buffer,
harfrust::ShapeOptions::new(),
)
.expect("shapes");
buffer
.glyph_infos()
.iter()
.zip(buffer.glyph_positions())
.map(|(info, at)| (info.glyph_id, at.x_advance))
.collect()
}
fn width(text: &str) -> f32 {
let scale = Font::sans().metrics().scale(SIZE);
let mut pen = 0.0;
let mut piece = String::new();
let mut spaces = false;
for c in text.chars().chain(['\n']) {
if c == '\n' || (c == ' ') != spaces {
for (_, advance) in harfrust(&piece) {
pen += advance as f32 * scale;
}
piece.clear();
spaces = c == ' ';
}
piece.push(c);
}
pen
}
fn assert_lines(layout: &TextLayout, want: &[&str], what: &str) {
let font = Font::sans();
let got: Vec<Vec<GlyphId>> = layout
.lines()
.iter()
.map(|line| {
layout.glyphs()[line.glyphs.clone()]
.iter()
.map(|g| g.glyph)
.collect()
})
.collect();
let expected: Vec<Vec<GlyphId>> = want
.iter()
.map(|line| {
line.chars()
.filter(|&c| c != ' ')
.map(|c| font.glyph_id(c))
.collect()
})
.collect();
assert_eq!(got, expected, "{what}: lines are not {want:?}");
for (line, text) in layout.lines().iter().zip(want) {
assert!(
(line.width - width(text.trim_end())).abs() < 1e-4,
"{what}: {text:?} measured {} rather than {}",
line.width,
width(text.trim_end())
);
}
}
#[test]
fn lines_break_at_the_exact_places_for_fixed_widths() {
let font = Font::sans();
assert!(width("The quick brown fox jumps") > 188.0 && width(TEXT) < 189.0);
assert!(width("The quick brown fox") > 141.0 && width("The quick brown fox") < 142.0);
assert!(width("brown fox") < 68.0 && width("fox jumps") > 68.0);
let cases: [(Option<f32>, &[&str]); 11] = [
(None, &["The quick brown fox jumps"]),
(Some(189.0), &["The quick brown fox jumps"]),
(Some(188.0), &["The quick brown fox", "jumps"]),
(Some(142.0), &["The quick brown fox", "jumps"]),
(Some(141.9), &["The quick brown", "fox jumps"]),
(Some(116.0), &["The quick brown", "fox jumps"]),
(Some(69.0), &["The quick", "brown fox", "jumps"]),
(Some(68.0), &["The", "quick", "brown fox", "jumps"]),
(Some(67.0), &["The", "quick", "brown", "fox", "jumps"]),
(Some(0.0), &["The", "quick", "brown", "fox", "jumps"]),
(Some(20.0), &["The", "quick", "brown", "fox", "jumps"]),
];
for (wrap, want) in cases {
let layout = TextLayout::new(font, TEXT, SIZE, LINE, wrap);
assert_lines(&layout, want, &format!("wrap {wrap:?}"));
}
}
#[test]
fn newlines_and_spaces_follow_the_rules() {
let font = Font::sans();
let layout = TextLayout::new(font, "ab\n\ncd", SIZE, LINE, None);
assert_lines(&layout, &["ab", "", "cd"], "blank line");
assert_eq!(layout.height(), 3.0 * LINE);
let empty = TextLayout::new(font, "", SIZE, LINE, None);
assert_eq!(empty.lines().len(), 1);
assert_eq!(empty.measure(), Vec2::new(0.0, LINE));
let spaced = TextLayout::new(font, " The quick \nfox ", SIZE, LINE, None);
assert_lines(&spaced, &[" The quick", "fox"], "spaces");
assert!(spaced.lines()[0].width > width("The quick"));
let broken = TextLayout::new(font, "The quick", SIZE, LINE, Some(40.0));
assert_lines(&broken, &["The", "quick"], "break in a run of spaces");
assert_eq!(
broken.glyphs()[broken.lines()[1].glyphs.start].offset.x,
0.0
);
}
#[test]
fn advances_equal_harfrusts_for_a_known_string() {
let font = Font::sans();
let scale = font.metrics().scale(SIZE);
let word = "WAVY Toyota";
let layout = TextLayout::new(font, word, SIZE, LINE, None);
let mut want = Vec::new();
let mut pen = 0.0;
for (index, piece) in ["WAVY", " ", "Toyota"].into_iter().enumerate() {
for (glyph, advance) in harfrust(piece) {
if index != 1 {
want.push((GlyphId(glyph), pen));
}
pen += advance as f32 * scale;
}
}
let got: Vec<(GlyphId, f32)> = layout
.glyphs()
.iter()
.map(|glyph| (glyph.glyph, glyph.offset.x))
.collect();
assert_eq!(got, want);
assert_eq!(layout.width(), pen);
}
#[test]
fn kerning_applies_within_a_line_and_not_across_a_break() {
let font = Font::sans();
let scale = font.metrics().scale(SIZE);
let a = font.glyph_id('A');
let layout = TextLayout::new(font, "AV", SIZE, LINE, None);
let step = layout.glyphs()[1].offset.x - layout.glyphs()[0].offset.x;
assert_eq!(step, harfrust("AV")[0].1 as f32 * scale);
assert!(step < font.advance(a) * scale, "AV is not kerned");
let hearts = fixtures::shaped();
let x = |layout: &TextLayout| -> Vec<f32> {
layout.glyphs().iter().map(|glyph| glyph.offset.x).collect()
};
let word = TextLayout::new(&hearts, "\u{2764}\u{2764}", SIZE, LINE, None);
assert_eq!(x(&word), [0.0, 13.0]);
let spaced = TextLayout::new(&hearts, "\u{2764} \u{2764}", SIZE, LINE, None);
assert_eq!(x(&spaced), [0.0, 32.0], "a heart kerned against a space");
let broken = TextLayout::new(&hearts, "\u{2764} \u{2764}", SIZE, LINE, Some(20.0));
let widths: Vec<f32> = broken.lines().iter().map(|line| line.width).collect();
assert_eq!(widths, [16.0, 16.0], "kerning reached across the break");
assert_eq!(x(&broken), [0.0, 0.0]);
}
#[test]
fn baselines_are_a_line_height_apart_with_the_box_centred() {
let font = Font::sans();
let metrics = font.metrics();
let scale = metrics.scale(SIZE);
let layout = TextLayout::new(font, TEXT, SIZE, LINE, Some(0.0));
let first = (LINE - (metrics.ascent - metrics.descent) * scale) * 0.5 + metrics.ascent * scale;
for (index, line) in layout.lines().iter().enumerate() {
assert_eq!(line.baseline, index as f32 * LINE + first);
for glyph in &layout.glyphs()[line.glyphs.clone()] {
assert_eq!(glyph.offset.y, line.baseline);
}
}
}
#[test]
fn a_mark_is_drawn_at_the_shapers_offset() {
let font = Font::sans();
let layout = TextLayout::new(font, "xh\u{301}", SIZE, LINE, None);
let mut shaped = Vec::new();
shape(font, SIZE, "xh\u{301}", 0..4, &mut shaped);
assert_eq!(layout.glyphs().len(), 3);
let baseline = layout.lines()[0].baseline;
let pen = shaped[0].advance + shaped[1].advance;
let mark = layout.glyphs()[2];
assert_eq!(mark.offset, Vec2::new(pen, baseline) + shaped[2].offset);
assert!(shaped[2].offset.y < 0.0, "the mark is not raised");
assert_eq!(layout.width(), pen, "the mark took width");
}
#[test]
fn a_cluster_falls_back_whole_and_ligates() {
let font = Font::sans().with_fallback(&[fixtures::cbdt(), fixtures::shaped()]);
let text = "a\u{2764}\u{200D}\u{1F525}b";
let layout = TextLayout::new(&font, text, SIZE, LINE, None);
assert_eq!(layout.faces(), [0, 2, 0]);
let runs = layout.face_runs();
let spelled: Vec<&str> = runs.iter().map(|run| &text[run.text.clone()]).collect();
assert_eq!(spelled, ["a", "\u{2764}\u{200D}\u{1F525}", "b"]);
assert_eq!(runs[1].font, &fixtures::shaped());
}
#[test]
fn alignment_places_each_line_in_its_box() {
let font = Font::sans();
let base = TextLayout::new(font, "The quick\nfox", SIZE, LINE, None);
for (align, share) in [
(TextAlign::Left, 0.0),
(TextAlign::Center, 0.5),
(TextAlign::Right, 1.0),
] {
let mut layout = base.clone();
layout.align(100.0, align);
layout.align(100.0, align);
for (line, before) in layout.lines().iter().zip(base.lines()) {
let want = (100.0 - line.width) * share;
assert_eq!(line.offset, want, "{align:?}");
let first = line.glyphs.start;
assert_eq!(
layout.glyphs()[first].offset.x,
base.glyphs()[first].offset.x + want,
"{align:?}"
);
assert_eq!(line.width, before.width);
}
}
let mut wide = TextLayout::new(font, TEXT, SIZE, LINE, None);
wide.align(50.0, TextAlign::Center);
assert_eq!(wide.lines()[0].offset, 0.0);
}
#[test]
fn the_wrap_width_is_whole_pixels() {
assert_eq!(wrap_width(100.0), 100.0);
assert_eq!(wrap_width(99.999_99), 100.0);
assert_eq!(wrap_width(100.000_01), 100.0);
assert_eq!(wrap_width(100.9), 100.0);
let measured = TextLayout::new(Font::sans(), TEXT, SIZE, LINE, None).measure();
let again = TextLayout::new(Font::sans(), TEXT, SIZE, LINE, Some(measured.x));
assert_eq!(
again.lines().len(),
1,
"a measured width does not fit its own line"
);
}
#[test]
fn caret_stops_sit_on_cluster_pens() {
let font = Font::sans();
let mut stops = Vec::new();
caret_stops(font, "Ta h\u{301}", SIZE, &mut stops);
let layout = TextLayout::new(font, "Ta h\u{301}", SIZE, LINE, None);
let x: Vec<f32> = layout.glyphs().iter().map(|glyph| glyph.offset.x).collect();
assert_eq!(stops.len(), 6);
assert_eq!(stops[..2], [x[0], x[1]]);
assert_eq!(stops[3], x[2], "the stop before h is where h is drawn");
assert_eq!(stops[4], stops[3], "the mark takes its base's stop");
assert_eq!(stops[5], layout.width());
let flags = Font::sans().with_fallback(&[fixtures::shaped()]);
let text = "\u{1F1FA}\u{1F1F8}\u{1F1FA}\u{1F1F8}";
caret_stops(&flags, text, SIZE, &mut stops);
assert_eq!(stops, [0.0, 0.0, SIZE, SIZE, 2.0 * SIZE]);
caret_stops(&flags, "\u{2764}\u{1F525}", SIZE, &mut stops);
assert_eq!(stops, [0.0, SIZE / 2.0, SIZE]);
}