use super::{
effective_text_for_metrics, estimate_text_width_exact, LeadingSpacePlan, ResolvedStyleSet,
TextStyle,
};
use unicode_segmentation::UnicodeSegmentation;
pub(super) struct LineMeasure {
pub trailing_spaces: usize,
pub trailing_width: f64,
pub slack: f64,
}
pub(super) fn measure(
plan: &LeadingSpacePlan<'_>,
styles: &ResolvedStyleSet,
total_width: f64,
available: f64,
tab_width: f64,
trailing_limit: usize,
terminal_tracking: f64,
) -> LineMeasure {
let trailing_spaces = plan
.spacing_chars()
.iter()
.rev()
.take_while(|(ch, _)| *ch == ' ')
.count()
.min(trailing_limit);
let trailing_width = plan.line.runs.last().map_or(0.0, |run| {
let mut style = run.text_style(styles);
style.default_tab_width = tab_width;
estimate_text_width_exact(&" ".repeat(trailing_spaces), &style)
});
LineMeasure {
trailing_spaces,
trailing_width,
slack: available - (total_width - trailing_width - terminal_tracking),
}
}
pub(super) fn compress(
plan: &LeadingSpacePlan<'_>,
styles: &ResolvedStyleSet,
trailing_spaces: usize,
tab_width: f64,
slack: f64,
terminal_ink_overhang: f64,
) -> (f64, f64) {
let count = plan.spacing_chars().len();
let end = count.saturating_sub(trailing_spaces);
let mut position = 0;
let mut runs = Vec::new();
let mut smallest_space = f64::INFINITY;
let mut spaces = 0;
let mut glyphs = 0;
let mut last = None;
for (index, run) in plan.line.runs.iter().enumerate() {
let field = plan.body_field_runs.get(index).copied().unwrap_or(false);
let display = effective_text_for_metrics(run);
let length = if field { 1 } else { display.chars().count() };
let take = end.saturating_sub(position).min(length);
let text: String = if field && take > 0 {
display.to_owned()
} else {
display.chars().take(take).collect()
};
position += length;
let distributed = plan.spacing(index, 1.0) != 0.0;
let mut style = run.text_style(styles);
style.default_tab_width = tab_width;
if distributed && text.contains(' ') {
let mut untracked = style.clone();
untracked.letter_spacing = 0.0;
if let Some(latin) = untracked.ascii_punct_latin.as_mut() {
latin.letter_spacing = 0.0;
}
smallest_space = smallest_space.min(estimate_text_width_exact(" ", &untracked));
spaces += text.chars().filter(|&ch| ch == ' ').count();
}
for glyph in text.graphemes(true) {
let eligible = distributed
&& !glyph
.chars()
.any(|ch| ch.is_whitespace() || ch == '\u{fffc}')
&& estimate_text_width_exact(glyph, &style) > 0.0;
glyphs += usize::from(eligible);
last = Some((glyph.to_owned(), style.clone(), eligible));
}
runs.push((text, style, distributed));
}
if spaces == 0 || !smallest_space.is_finite() {
return (0.0, 0.0);
}
let word_delta = (slack / spaces as f64).max(-smallest_space * 0.2);
let leftover = slack - word_delta * spaces as f64;
let terminal = last.filter(|(_, _, eligible)| *eligible && !plan.terminal_inline_object);
let slots = glyphs.saturating_sub(usize::from(terminal.is_some()));
if leftover >= 0.0 || slots == 0 {
return (word_delta, 0.0);
}
let measure = |word: f64, character: f64| -> f64 {
runs.iter()
.map(|(text, style, distributed)| {
let mut measured = style.clone();
if *distributed {
measured.extra_word_spacing = word;
measured.extra_char_spacing = character;
}
estimate_text_width_exact(text, &measured)
})
.sum::<f64>()
};
let natural_width = measure(0.0, 0.0);
let terminal_recovery = |character: f64| -> f64 {
let Some((glyph, style, _)) = &terminal else {
return 0.0;
};
let mut measured: TextStyle = style.clone();
measured.extra_char_spacing = character;
let actual_delta =
estimate_text_width_exact(glyph, &measured) - estimate_text_width_exact(glyph, style);
(terminal_ink_overhang - actual_delta).max(0.0) - terminal_ink_overhang
};
let mut character = leftover / slots as f64;
for _ in 0..8 {
let word = word_delta - character;
let consumed = measure(word, character) - natural_width + terminal_recovery(character);
let remainder = slack - consumed;
if remainder.abs() <= 1.0 / 75.0 {
break;
}
let next = (character + remainder / slots as f64).min(0.0);
if (next - character).abs() <= f64::EPSILON {
break;
}
character = next;
}
(word_delta - character, character)
}