Skip to content

Truly independent web browser

LibWeb: Give a tab the font a space in its place would take

sideshowbarker89fd88861d14
diff --git a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
index c7f0714b39c..ca5ff532c31 100644
--- a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
+++ b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs
@@ -1138,6 +1138,8 @@ struct TextChunker<'text> {
last_non_whitespace_font: Option<u16>,
first_font_and_ascii_code_points_it_resolves: Option<(&'text FontHandle, u128)>,
font_for_every_space: Option<&'text FontHandle>,
+ /// Where the look-ahead from a run of white space to the text after it last started, and where it ended.
+ space_look_ahead: std::cell::Cell<(usize, usize)>,
}
impl<'text> TextChunker<'text> {
@@ -1183,6 +1185,7 @@ impl<'text> TextChunker<'text> {
last_non_whitespace_font: None,
first_font_and_ascii_code_points_it_resolves,
font_for_every_space,
+ space_look_ahead: std::cell::Cell::new((usize::MAX, usize::MAX)),
}
}
@@ -1292,10 +1295,19 @@ impl<'text> TextChunker<'text> {
}
// 2. Look ahead to the next non-space to infer the base font of this run.
- let mut i = at_index;
+ // NB: Each space or tab of a run looks ahead to the same text, so start from where the look-ahead from an
+ // earlier one in the run ended.
+ let (scanned_from, scanned_to) = self.space_look_ahead.get();
+ let mut i = if (scanned_from..=scanned_to).contains(&at_index) {
+ scanned_to
+ } else {
+ at_index
+ };
+ let scan_start = if i == at_index { at_index } else { scanned_from };
while i < self.text.len() {
let code_point = code_point_at(self.text, i);
if !is_interword_space(code_point) && code_point != '\t' as u32 && code_point != '\n' as u32 {
+ self.space_look_ahead.set((scan_start, i));
let font = self.font_for_code_point_at(i, code_point);
if !font.is_emoji_font() && has_glyph(&font) {
return ChunkFont::FromCascade(font);
@@ -1309,6 +1321,7 @@ impl<'text> TextChunker<'text> {
.unwrap_or(self.text.len());
}
+ self.space_look_ahead.set((scan_start, i));
// 3. No text around (leading/trailing/all spaces) — pick a font with the glyph from the cascade.
ChunkFont::FromCascade(self.frozen_font_list.font_for_code_point_borrowed(
space_code_point,
@@ -1322,6 +1335,10 @@ impl<'text> TextChunker<'text> {
fn expected_font_for(&self, code_point: u32) -> ChunkFont<'text> {
if is_interword_space(code_point) {
self.font_for_space(self.current_index, code_point)
+ } else if code_point == '\t' as u32 {
+ // NB: A tab is drawn as a shift to a tab stop rather than as a glyph, so it takes the font that a space in
+ // its place would take, not a fallback font that happens to have a glyph for it.
+ self.font_for_space(self.current_index, ' ' as u32)
} else {
ChunkFont::FromCascade(self.font_for_code_point_at(self.current_index, code_point))
}
diff --git a/Tests/LibWeb/Text/expected/css/consecutive-tabs-advance-to-successive-tab-stops.txt b/Tests/LibWeb/Text/expected/css/consecutive-tabs-advance-to-successive-tab-stops.txt
index 5d4c27768ea..8e899d6115a 100644
--- a/Tests/LibWeb/Text/expected/css/consecutive-tabs-advance-to-successive-tab-stops.txt
+++ b/Tests/LibWeb/Text/expected/css/consecutive-tabs-advance-to-successive-tab-stops.txt
@@ -7,12 +7,12 @@ text-then-three-tabs: 24.00 spaces
tab-size-4-two-tabs: 8.00 spaces
tab-word-tab: 16.00 spaces
tabs-in-two-nodes: 16.00 spaces
-one-tab characters: 0.00..8.00 8.00..8.72
-two-tabs characters: 0.00..8.00 8.00..16.00 16.00..16.72
-three-tabs characters: 0.00..8.00 8.00..16.00 16.00..24.00 24.00..24.72
-text-then-two-tabs characters: 0.00..1.00 1.00..8.00 8.00..16.00 16.00..16.72
-longer-text-then-two-tabs characters: 0.00..1.00 1.00..2.00 2.00..3.00 3.00..4.00 4.00..5.00 5.00..6.00 6.00..7.00 7.00..8.00 8.00..16.00 16.00..16.72
-text-then-three-tabs characters: 0.00..1.00 1.00..2.00 2.00..3.00 3.00..8.00 8.00..16.00 16.00..24.00 24.00..24.72
-tab-size-4-two-tabs characters: 0.00..1.00 1.00..2.00 2.00..4.00 4.00..8.00 8.00..8.72
-tab-word-tab characters: 0.00..1.00 1.00..8.00 8.00..8.59 8.59..16.00 16.00..16.72
+one-tab characters: 0.00..8.00 8.00..9.00
+two-tabs characters: 0.00..8.00 8.00..16.00 16.00..17.00
+three-tabs characters: 0.00..8.00 8.00..16.00 16.00..24.00 24.00..25.00
+text-then-two-tabs characters: 0.00..1.00 1.00..8.00 8.00..16.00 16.00..17.00
+longer-text-then-two-tabs characters: 0.00..1.00 1.00..2.00 2.00..3.00 3.00..4.00 4.00..5.00 5.00..6.00 6.00..7.00 7.00..8.00 8.00..16.00 16.00..17.00
+text-then-three-tabs characters: 0.00..1.00 1.00..2.00 2.00..3.00 3.00..8.00 8.00..16.00 16.00..24.00 24.00..25.00
+tab-size-4-two-tabs characters: 0.00..1.00 1.00..2.00 2.00..4.00 4.00..8.00 8.00..9.00
+tab-word-tab characters: 0.00..1.00 1.00..8.00 8.00..9.00 9.00..16.00 16.00..17.00
tabs-in-two-nodes characters: 0.00..1.00 1.00..8.00
diff --git a/Tests/LibWeb/Text/expected/intrinsic-inline-content-sizing.txt b/Tests/LibWeb/Text/expected/intrinsic-inline-content-sizing.txt
index 1a206cb800a..5a8152ad2e6 100644
--- a/Tests/LibWeb/Text/expected/intrinsic-inline-content-sizing.txt
+++ b/Tests/LibWeb/Text/expected/intrinsic-inline-content-sizing.txt
@@ -5,7 +5,7 @@ adjacent inline edges: 114x20, 64x40, 114x20, 114x20
inline edges before break: 50x40, 50x40, 50x40, 50x40
inline edges before absolute: 56x20, 56x20, 56x20, 56x20
nowrap: 160x20, 160x20, 160x20, 160x20
-pre: 135x40, 135x40, 135x40, 135x40
+pre: 140x40, 140x40, 140x40, 140x40
pre-wrap: 140x40, 50x100, 140x40, 120x60
pre-line: 100x40, 50x60, 100x40, 100x40
break-spaces: 130x40, 60x60, 130x40, 120x40
diff --git a/Tests/LibWeb/Text/expected/preformatted-space-after-tab-indented-text-font.txt b/Tests/LibWeb/Text/expected/preformatted-space-after-tab-indented-text-font.txt
index 32b0ab21d30..4a35ce5dcbd 100644
--- a/Tests/LibWeb/Text/expected/preformatted-space-after-tab-indented-text-font.txt
+++ b/Tests/LibWeb/Text/expected/preformatted-space-after-tab-indented-text-font.txt
@@ -1 +1 @@
-Width: 83.90625
+Width: 61.96875