use super::super::composer::ComposedLine;
use super::super::style_resolver::ResolvedStyleSet;
use super::super::TextStyle;
use super::text_measurement::estimate_text_width_unrounded;
pub(crate) fn glyph_letter_spacing_advance(text: &str, style: &TextStyle) -> f64 {
if style.letter_spacing == 0.0
&& style
.ascii_punct_latin
.as_ref()
.is_none_or(|latin| latin.letter_spacing == 0.0)
{
return 0.0;
}
let display = super::super::composer::one_to_one_pua_replay_text(text);
let tracked = estimate_text_width_unrounded(&display, style);
let mut natural = style.clone();
natural.letter_spacing = 0.0;
if let Some(latin) = natural.ascii_punct_latin.as_mut() {
latin.letter_spacing = 0.0;
}
tracked - estimate_text_width_unrounded(&display, &natural)
}
pub(super) fn terminal_tracking_for_alignment(
line: &ComposedLine,
styles: &ResolvedStyleSet,
tac_offsets: &[(usize, f64, usize)],
trim_trailing_spaces: bool,
) -> f64 {
if line.runs.iter().any(|run| {
run.char_overlap.is_some()
|| run.display_text.as_ref().is_some_and(|display| {
super::super::composer::one_to_one_pua_replay_text(&run.text).as_ref()
!= display
})
|| run.inserted_control_text
}) {
return 0.0;
}
let mut text_end = line.char_start
+ line
.runs
.iter()
.map(|run| run.source_scalar_count())
.sum::<usize>();
for run in line.runs.iter().rev() {
let text = if trim_trailing_spaces {
run.text.trim_end_matches(' ')
} else {
run.text.as_str()
};
text_end -= run.text.chars().count() - text.chars().count();
if text.is_empty() {
continue;
}
if tac_offsets.iter().any(|(pos, _, _)| *pos >= text_end) {
return 0.0;
}
let Some(last) =
unicode_segmentation::UnicodeSegmentation::graphemes(text, true).next_back()
else {
return 0.0;
};
if last.chars().any(char::is_whitespace) {
return 0.0;
}
let style = run.text_style(styles);
if style.kerning {
return 0.0;
}
if super::text_measurement::has_hft_bold_tracking(&style) {
let mut intrinsic = style.clone();
intrinsic.letter_spacing = 0.0;
if let Some(latin) = intrinsic.ascii_punct_latin.as_mut() {
latin.letter_spacing = 0.0;
}
let intrinsic_width =
super::text_measurement::compute_glyph_fit_positions(last, &intrinsic)
.last()
.copied()
.unwrap_or(0.0);
return estimate_text_width_unrounded(last, &style) - intrinsic_width;
}
return glyph_letter_spacing_advance(last, &style);
}
0.0
}