LibWeb: Start implementation of rendering tabs according to tab-size

This commit is contained in:
Kostya Farber 2024-10-15 18:21:49 +01:00 committed by Alexander Kalenik
commit 323370dfa3
Notes: github-actions[bot] 2024-10-17 13:01:57 +00:00
8 changed files with 205 additions and 11 deletions

View file

@ -241,7 +241,55 @@ Optional<InlineLevelIterator::Item> InlineLevelIterator::next_without_lookahead(
};
}
auto glyph_run = Gfx::shape_text({ 0, 0 }, chunk.view, chunk.font, text_type);
auto x = 0.0f;
if (chunk.has_breaking_tab) {
CSSPixels accumulated_width;
// make sure to account for any fragments that take up a portion of the measured tab stop distance
auto fragments = m_containing_block_used_values.line_boxes.last().fragments();
for (auto const& frag : fragments) {
accumulated_width += frag.width();
}
// https://drafts.csswg.org/css-text/#tab-size-property
auto tab_size = text_node.computed_values().tab_size();
CSSPixels tab_width;
tab_width = tab_size.visit(
[&](CSS::LengthOrCalculated const& t) -> CSSPixels {
auto resolution_context = CSS::Length::ResolutionContext::for_layout_node(text_node);
auto value = t.resolved(resolution_context);
return value.to_px(text_node);
},
[&](CSS::NumberOrCalculated const& n) -> CSSPixels {
auto number = n.resolved(text_node);
return CSSPixels::nearest_value_for(number * chunk.font->glyph_width(' '));
});
// https://drafts.csswg.org/css-text/#white-space-phase-2
// if fragments have added to the width, calculate the net distance to the next tab stop, otherwise the shift will just be the tab width
auto tab_stop_dist = accumulated_width > 0 ? (ceil((accumulated_width / tab_width)) * tab_width) - accumulated_width : tab_width;
auto ch_width = chunk.font->glyph_width('0');
// If this distance is less than 0.5ch, then the subsequent tab stop is used instead
if (tab_stop_dist < ch_width * 0.5)
tab_stop_dist += tab_width;
// account for consecutive tabs
auto num_of_tabs = 0;
for (auto code_point : chunk.view) {
if (code_point != '\t')
break;
num_of_tabs++;
}
tab_stop_dist = tab_stop_dist * num_of_tabs;
x = tab_stop_dist.to_float();
}
auto glyph_run = Gfx::shape_text({ x, 0 }, chunk.view, chunk.font, text_type);
CSSPixels chunk_width = CSSPixels::nearest_value_for(glyph_run->width());
// NOTE: We never consider `content: ""` to be collapsible whitespace.