use super::{layout, legacy_font_advances, validated_replay_positions, TextStyle};
use std::borrow::Cow;
fn has_positive_source_tracking(style: &TextStyle) -> bool {
style.letter_spacing > 0.0
|| style
.ascii_punct_latin
.as_ref()
.is_some_and(|latin| latin.letter_spacing > 0.0)
}
fn intrinsic_style(style: &TextStyle) -> Cow<'_, TextStyle> {
if !has_positive_source_tracking(style) {
return Cow::Borrowed(style);
}
let mut intrinsic = style.clone();
if intrinsic.letter_spacing > 0.0 {
intrinsic.letter_spacing = 0.0;
}
if let Some(latin) = intrinsic.ascii_punct_latin.as_mut() {
if latin.letter_spacing > 0.0 {
latin.letter_spacing = 0.0;
}
}
Cow::Owned(intrinsic)
}
pub(crate) fn glyph_fit_positions<'a>(
replay_text: &str,
style: &TextStyle,
layout_positions: Option<&[f64]>,
char_positions: &'a [f64],
) -> std::borrow::Cow<'a, [f64]> {
let positive_source_tracking = has_positive_source_tracking(style);
let intrinsic = intrinsic_style(style);
let style = intrinsic.as_ref();
if legacy_font_advances::has_fixed_ascii_bold_tracking(style) {
let mut fit_style = style.clone();
fit_style.extra_char_spacing = fit_style.extra_char_spacing.max(0.0);
let decisions = layout::trace_char_width_decisions(replay_text, &fit_style);
let mut positions = Vec::with_capacity(decisions.len() + 1);
let mut x = 0.0;
positions.push(x);
for decision in decisions {
x += decision.final_width_px - decision.bold_tracking_px;
positions.push(x);
}
return std::borrow::Cow::Owned(positions);
}
if layout::has_hft_bold_tracking(style) {
{
let mut fit_style = style.clone();
fit_style.extra_char_spacing = fit_style.extra_char_spacing.max(0.0);
std::borrow::Cow::Owned(layout::compute_glyph_fit_positions(replay_text, &fit_style))
}
} else if (style.kerning && validated_replay_positions(replay_text, layout_positions).is_some())
|| positive_source_tracking
|| style.extra_char_spacing < 0.0
|| legacy_font_advances::has_bold_tracking(style)
{
let mut fit_style = style.clone();
fit_style.extra_char_spacing = fit_style.extra_char_spacing.max(0.0);
if legacy_font_advances::has_bold_tracking(style) {
fit_style.metric_font_family = None;
}
std::borrow::Cow::Owned(layout::compute_char_positions(replay_text, &fit_style))
} else {
std::borrow::Cow::Borrowed(char_positions)
}
}
pub(crate) fn terminal_underline_positions(
run: &super::render_tree::TextRunNode,
) -> Option<Vec<f64>> {
if run.is_vertical
|| !(run.is_para_end || run.is_line_break_end)
|| !has_positive_source_tracking(&run.style)
{
return None;
}
let text = super::composer::one_to_one_pua_replay_text(run.display_or_text());
if text.chars().last()?.is_whitespace() {
return None;
}
let mut positions = run.replay_positions_for(&text).into_owned();
validated_replay_positions(&text, Some(&positions))?;
let (start, cluster) = layout::split_into_clusters(&text).pop()?;
let end = start + cluster.chars().count();
let fit = glyph_fit_positions(
&text,
&run.style,
run.layout_positions.as_deref(),
&positions,
);
let natural = run.style.glyph_fit_advance(fit[end] - fit[start])?;
let visible_end = positions[start] + natural;
if !visible_end.is_finite() || visible_end < positions[end - 1] {
return None;
}
drop(fit);
positions[end] = visible_end;
Some(positions)
}