use std::ops::Range;
use alpha2_model::TextStyle;
use alpha2_text::{TextRasterError, TextRasterizer};
use crate::{CanvasRect, PictureSize, TextAt};
#[derive(Clone, Debug, PartialEq)]
pub struct TextLayoutReport {
pub font_size: f32,
pub layout: CanvasRect,
pub lines: Vec<LineReport>,
pub words: Vec<WordReport>,
pub clusters: Vec<ClusterReport>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct LineReport {
pub clusters: Range<usize>,
pub line_box: CanvasRect,
pub em: CanvasRect,
pub baseline: f32,
}
#[derive(Clone, Debug, PartialEq)]
pub struct WordReport {
pub clusters: Range<usize>,
pub boxes: Vec<CanvasRect>,
pub ink: Option<CanvasRect>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ClusterReport {
pub bytes: Range<usize>,
pub run: Option<usize>,
pub word: Option<u32>,
pub line: u32,
pub space: bool,
pub ellipsis: bool,
pub font_size: f32,
pub baseline: f32,
pub cluster_box: CanvasRect,
pub ink: Option<CanvasRect>,
}
pub fn text_layout(
rasterizer: &TextRasterizer,
text: &TextAt<'_>,
style: &TextStyle,
size: PictureSize,
) -> Result<TextLayoutReport, TextRasterError> {
let placed = crate::text::place(rasterizer, text, style, size)?;
let layout = rasterizer.layout_with_ink(&placed.request)?;
let unit = size.unit();
let rect = |bounds: [f32; 4]| placed.rect(bounds);
let baseline = |pixels: f32| (placed.origin[1] + pixels) / placed.canvas[1];
let em = |baseline: f32, size: f32| [baseline - 0.8 * size, baseline + 0.2 * size];
let run_ends = text
.content
.runs()
.iter()
.scan(0, |end, run| {
*end += run.text.len();
Some(*end)
})
.collect::<Vec<_>>();
let clusters = layout
.clusters
.iter()
.map(|cluster| {
let [top, bottom] = em(cluster.baseline, cluster.font_size);
ClusterReport {
bytes: cluster.bytes.clone(),
run: (!run_ends.is_empty()).then(|| {
run_ends
.partition_point(|end| *end <= cluster.bytes.start)
.min(run_ends.len() - 1)
}),
word: cluster.word,
line: cluster.line,
space: cluster.is_space,
ellipsis: cluster.ellipsis,
font_size: cluster.font_size / unit,
baseline: baseline(cluster.baseline),
cluster_box: rect([cluster.x, top, cluster.x + cluster.advance, bottom]),
ink: cluster.ink.map(rect),
}
})
.collect::<Vec<_>>();
let lines = layout
.lines
.iter()
.map(|line| {
let [em_top, em_bottom] = em(line.baseline, line.size);
let (left, right) = (line.left, line.left + line.width);
LineReport {
clusters: line.clusters.clone(),
line_box: rect([
left,
line.box_top,
right,
line.box_top + placed.request.line_height * line.size,
]),
em: rect([left, em_top, right, em_bottom]),
baseline: baseline(line.baseline),
}
})
.collect();
let words = words_of(&layout, &clusters, &rect);
Ok(TextLayoutReport {
font_size: placed.font_size,
layout: placed.layout,
lines,
words,
clusters,
})
}
fn words_of(
layout: &alpha2_text::TextLayout,
clusters: &[ClusterReport],
rect: &impl Fn([f32; 4]) -> CanvasRect,
) -> Vec<WordReport> {
let em = |baseline: f32, size: f32| [baseline - 0.8 * size, baseline + 0.2 * size];
let mut words = Vec::<WordReport>::new();
for (index, cluster) in layout.clusters.iter().enumerate() {
let Some(word) = cluster.word else {
continue;
};
let word = usize::try_from(word).unwrap_or(usize::MAX);
while words.len() <= word {
words.push(WordReport {
clusters: index..index,
boxes: Vec::new(),
ink: None,
});
}
let entry = &mut words[word];
if entry.clusters.is_empty() {
entry.clusters.start = index;
}
entry.clusters.end = index + 1;
}
for word in &mut words {
let members = &layout.clusters[word.clusters.clone()];
let mut line_of = None;
let mut fragment: Option<[f32; 4]> = None;
let mut fragments = Vec::new();
for cluster in members.iter().filter(|cluster| cluster.word.is_some()) {
let [top, bottom] = em(cluster.baseline, cluster.font_size);
let piece = [cluster.x, top, cluster.x + cluster.advance, bottom];
if line_of != Some(cluster.line) {
fragments.extend(fragment.take());
line_of = Some(cluster.line);
}
fragment = Some(fragment.map_or(piece, |held| {
[
held[0].min(piece[0]),
held[1].min(piece[1]),
held[2].max(piece[2]),
held[3].max(piece[3]),
]
}));
}
fragments.extend(fragment);
word.boxes = fragments.into_iter().map(rect).collect();
word.ink = clusters[word.clusters.clone()]
.iter()
.filter_map(|cluster| cluster.ink)
.reduce(|held, ink| CanvasRect {
left: held.left.min(ink.left),
top: held.top.min(ink.top),
right: held.right.max(ink.right),
bottom: held.bottom.max(ink.bottom),
});
}
words
}