use super::{CaptionFont, CaptionStyle};
#[derive(Clone, Debug, PartialEq)]
pub struct LineBox {
pub text: String,
pub width: f32,
}
#[derive(Clone, Debug, PartialEq)]
pub struct LaidOutText {
pub lines: Vec<LineBox>,
pub width: f32,
pub height: f32,
pub line_advance: f32,
pub first_baseline: f32,
}
impl LaidOutText {
pub fn is_empty(&self) -> bool {
self.lines.is_empty()
}
}
pub fn layout(
font: &mut CaptionFont,
text: &str,
style: &CaptionStyle,
max_width: f32,
) -> LaidOutText {
let size = style.font_size;
let metrics = font.metrics(size);
let line_advance = size * style.line_height;
let mut lines: Vec<LineBox> = Vec::new();
for paragraph in text.split('\n') {
let paragraph = paragraph.trim_end();
if paragraph.is_empty() {
lines.push(LineBox {
text: String::new(),
width: 0.0,
});
continue;
}
wrap_paragraph(font, paragraph, style, max_width, &mut lines);
}
if lines.iter().all(|l| l.text.is_empty()) {
lines.clear();
}
let width = lines.iter().fold(0.0f32, |acc, l| acc.max(l.width));
let height = if lines.is_empty() {
0.0
} else {
metrics.ascent + metrics.descent + line_advance * (lines.len() - 1) as f32
};
LaidOutText {
lines,
width,
height,
line_advance,
first_baseline: metrics.ascent,
}
}
fn wrap_paragraph(
font: &mut CaptionFont,
paragraph: &str,
style: &CaptionStyle,
max_width: f32,
out: &mut Vec<LineBox>,
) {
let size = style.font_size;
let spacing = style.letter_spacing;
let mut current = String::new();
for word in paragraph.split_whitespace() {
let candidate = if current.is_empty() {
word.to_string()
} else {
format!("{current} {word}")
};
let candidate_width = font.measure(&candidate, size, spacing);
if candidate_width <= max_width || current.is_empty() {
current = candidate;
} else {
out.push(LineBox {
width: font.measure(¤t, size, spacing),
text: std::mem::take(&mut current),
});
current = word.to_string();
}
while font.measure(¤t, size, spacing) > max_width && current.chars().count() > 1 {
let split = break_point(font, ¤t, size, spacing, max_width);
let head: String = current.chars().take(split).collect();
let tail: String = current.chars().skip(split).collect();
out.push(LineBox {
width: font.measure(&head, size, spacing),
text: head,
});
current = tail;
}
}
if !current.is_empty() {
out.push(LineBox {
width: font.measure(¤t, size, spacing),
text: current,
});
}
}
fn break_point(
font: &mut CaptionFont,
text: &str,
size: f32,
spacing: f32,
max_width: f32,
) -> usize {
let mut width = 0.0;
let mut count = 0usize;
for c in text.chars() {
let advance = font.advance(c, size) + if count > 0 { spacing } else { 0.0 };
if count > 0 && width + advance > max_width {
break;
}
width += advance;
count += 1;
}
count.max(1)
}
#[cfg(test)]
mod tests {
use super::*;
fn style(size: f32) -> CaptionStyle {
CaptionStyle::default().font_size(size).line_height(1.0)
}
#[test]
fn explicit_newlines_always_break() {
let mut font = CaptionFont::builtin();
let out = layout(&mut font, "one\ntwo", &style(10.0), 10_000.0);
assert_eq!(out.lines.len(), 2);
assert_eq!(out.lines[0].text, "one");
assert_eq!(out.lines[1].text, "two");
}
#[test]
fn greedy_wrap_fits_words_into_the_width() {
let mut font = CaptionFont::builtin();
let out = layout(&mut font, "aaa bbb ccc", &style(10.0), 80.0);
assert_eq!(out.lines.len(), 2);
assert_eq!(out.lines[0].text, "aaa bbb");
assert_eq!(out.lines[1].text, "ccc");
assert_eq!(out.width, 70.0);
}
#[test]
fn over_long_words_are_broken_instead_of_overflowing() {
let mut font = CaptionFont::builtin();
let out = layout(&mut font, "aaaaaaaaaa", &style(10.0), 30.0);
assert!(out.lines.len() >= 4, "{:?}", out.lines);
assert!(out.width <= 30.0, "no line may exceed the wrap width");
let joined: String = out.lines.iter().map(|l| l.text.as_str()).collect();
assert_eq!(joined, "aaaaaaaaaa", "no characters lost");
}
#[test]
fn block_height_counts_ink_plus_one_advance_per_extra_line() {
let mut font = CaptionFont::builtin();
let s = style(16.0); let one = layout(&mut font, "a", &s, 1000.0);
assert_eq!(one.height, 16.0); let two = layout(&mut font, "a\nb", &s, 1000.0);
assert_eq!(two.height, 32.0);
assert_eq!(two.line_advance, 16.0);
assert_eq!(two.first_baseline, 14.0);
}
#[test]
fn whitespace_only_text_lays_out_empty() {
let mut font = CaptionFont::builtin();
let out = layout(&mut font, " \n ", &style(16.0), 1000.0);
assert!(out.is_empty());
assert_eq!(out.height, 0.0);
}
}