use super::super::*;
use crate::css::ComputedLengthPercentage;
use crate::units::{LayoutLength, SemanticLengthExt, layout_pt};
pub(in crate::text) struct RenderedRunTabContext<'a> {
pub(in crate::text) style: &'a ComputedStyle,
pub(in crate::text) metric_style: &'a ComputedStyle,
}
impl FontSystem {
pub(in crate::text) fn apply_css_tab_stops(
&mut self,
runs: &mut [ShapedGlyphRun],
contexts: &[RenderedRunTabContext<'_>],
tab_origin: f32,
letter_spacing: ShapingLetterSpacing,
) {
if !runs.iter().any(|run| run.text.contains('\t')) {
return;
}
let mut logical_line_cursor = 0.0;
let mut accumulated_tab_correction = 0.0;
for (run_index, run) in runs.iter_mut().enumerate() {
let starts_with_tab = run
.text
.chars()
.find(|character| !character_is_bidi_format_control(*character))
.filter(|character| *character == '\t')
.is_some();
let provisional_start = run.x_offset + accumulated_tab_correction;
let run_start = if starts_with_tab {
logical_line_cursor
} else {
provisional_start.max(logical_line_cursor)
};
run.x_offset = run_start;
let mut run_cursor = run_start;
for glyph in &mut run.glyphs {
if glyph.unicode == "\t" {
let Some(context) = contexts.get(run_index) else {
run_cursor += glyph.x_advance;
continue;
};
let space_advance =
self.css_tab_stop_space_advance(context.metric_style, letter_spacing);
let tab_period = context
.style
.tab_size
.used_tab_stop_advance(space_advance.points())
.points();
let mut unspaced_tab_style = context.style.clone();
unspaced_tab_style.letter_spacing = ComputedLengthPercentage::ZERO;
unspaced_tab_style.word_spacing = ComputedLengthPercentage::ZERO;
let minimum_advance = self.ch_advance(&unspaced_tab_style).points() * 0.5;
let old_advance = glyph.x_advance;
let used_advance =
tab_stop_advance(tab_period, tab_origin + run_cursor, minimum_advance)
.points();
glyph.x_advance = used_advance;
glyph.nominal_x_advance = space_advance.points();
glyph.x_offset = 0.0;
glyph.y_offset = 0.0;
accumulated_tab_correction += used_advance - old_advance;
}
run_cursor += glyph.x_advance;
}
logical_line_cursor = run_cursor;
}
}
fn css_tab_stop_space_advance(
&mut self,
style: &ComputedStyle,
letter_spacing: ShapingLetterSpacing,
) -> LayoutLength {
let mut unspaced_style = style.clone();
unspaced_style.letter_spacing = ComputedLengthPercentage::ZERO;
unspaced_style.word_spacing = ComputedLengthPercentage::ZERO;
layout_pt(
self.shape_untracked_inline_line(" ", &unspaced_style, unspaced_style.line_height)
.and_then(|matched| {
matched
.runs
.iter()
.filter_map(|run| run.font_id)
.any(|font_id| self.document_fonts.font_has_character(font_id, ' '))
.then(|| matched.advance_width())
})
.or_else(|| {
self.character_font_match(style, ' ').and_then(|matched| {
let font_size = self
.font_size_adjusted_size_for_font_id(style, matched.font_id)
.unwrap_or(style.font_size);
let font = self.document_fonts.get(matched.font_id)?;
shape_text_with_document_font(font, " ", font_size, 0.0, 0.0)
.map(|glyphs| glyphs.into_iter().map(|glyph| glyph.x_advance).sum())
})
})
.filter(|advance| *advance > 0.0 && advance.is_finite())
.unwrap_or(style.font_size * 0.25)
+ letter_spacing.requested_for(style)
+ style.used_word_spacing().points(),
)
}
}
pub(in crate::text) fn synthesized_tab_glyph(provisional_advance: f32) -> RenderedGlyph {
RenderedGlyph {
kind: RenderedGlyphKind::AdvanceOnly,
x_advance: provisional_advance,
nominal_x_advance: provisional_advance,
x_offset: 0.0,
y_offset: 0.0,
unicode: "\t".to_string(),
}
}
pub(in crate::text) fn tab_stop_advance(
period: f32,
current_x: f32,
minimum_advance: f32,
) -> LayoutLength {
if period <= 0.0 || !period.is_finite() || !current_x.is_finite() {
return layout_pt(0.0);
}
let next_stop = (current_x / period).floor().mul_add(period, period);
let advance = (next_stop - current_x).max(0.0);
if minimum_advance.is_finite() && advance < minimum_advance.max(0.0) {
layout_pt(advance + period)
} else {
layout_pt(advance)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tab_stop_uses_the_following_stop_only_below_half_ch() {
assert_eq!(tab_stop_advance(8.0, 7.6, 0.5), layout_pt(8.4));
assert_eq!(tab_stop_advance(8.0, 7.5, 0.5), layout_pt(0.5));
assert_eq!(tab_stop_advance(0.0, 0.0, 0.5), layout_pt(0.0));
}
}