pub(super) fn glyph_band_horizontal(
run: &crate::renderer::render_tree::TextRunNode,
x: f64,
width: f64,
_em: f64,
) -> (f64, f64) {
let text = run.display_or_text();
let is_space = |c: char| matches!(c, ' ' | '\u{3000}');
let start = text.chars().take_while(|&c| is_space(c)).count();
let end = text.chars().count() - text.chars().rev().take_while(|&c| is_space(c)).count();
let positive_tracking =
run.style.extra_char_spacing.is_finite() && run.style.extra_char_spacing > 0.0;
if start >= end || (!positive_tracking && start == 0 && end == text.chars().count()) {
return (x, width);
}
let positions = run.replay_positions_for(text);
let Some((&left, &right)) = positions.get(start).zip(positions.get(end)) else {
return (x, width);
};
if !left.is_finite() || !right.is_finite() || left < 0.0 || right <= left || left >= width {
return (x, width);
}
let left = if start == 0 { 0.0 } else { left };
let mut right = if end == text.chars().count() {
width
} else {
right.min(width)
};
if let Some(ink_end) = tracked_glyph_end(run, text, &positions, width) {
right = right.min(ink_end);
}
if right <= left {
return (x, width);
}
(x + left, right - left)
}
fn tracked_glyph_end(
run: &crate::renderer::render_tree::TextRunNode,
text: &str,
positions: &[f64],
width: f64,
) -> Option<f64> {
let style = &run.style;
if !style.extra_char_spacing.is_finite()
|| style.extra_char_spacing <= 0.0
|| !style.letter_spacing.is_finite()
|| !width.is_finite()
{
return None;
}
let advance = *positions.last()?;
if (advance - width).abs() > 1e-6 * width.abs().max(1.0) {
return None;
}
let fits = crate::renderer::glyph_fit_positions(
text,
style,
run.layout_positions.as_deref(),
positions,
);
let mut right: f64 = 0.0;
for (start, cluster) in crate::renderer::layout::split_into_clusters(text) {
if cluster.chars().all(char::is_whitespace) {
continue;
}
let count = cluster.chars().count();
let fit_advance = fits.get(start + count)? - fits.get(start)?;
let glyph_width =
style.glyph_fit_advance(fit_advance)? + (-style.letter_spacing).max(0.0) * count as f64;
let end = positions.get(start)? + glyph_width;
if !end.is_finite() {
return None;
}
right = right.max(end);
}
(right > 0.0).then_some(right)
}