Skip to content

Truly independent web browser

LibWeb: Measure tab stops in spaces of the block container’s font

sideshowbarker37b746f82236
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
index b2815fc7a50..cc9fd6c91c8 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
@@ -1076,8 +1076,19 @@ impl<'pass> InlineContentPreparer<'pass> {
let mut tab_stop = None;
if chunk.has_breaking_tab {
let style = text_context.style;
+ // https://drafts.csswg.org/css-text-3/#tab-size-property
+ // "Represents the measure as a multiple of the advance width of the space character (U+0020) of the
+ // nearest block container ancestor of the preserved tab, including its associated letter-spacing and
+ // word-spacing."
+ // NB: That's the space of the block container's first available font, whatever font the tab itself would
+ // be drawn with. The zeros that set the least distance to a tab stop are of that font too.
+ let text_font = self
+ .style(self.block_container)
+ .font()
+ .first_available_font()
+ .expect("layout read a font group that never received a frozen font list");
let tab_inline_size = if style.tab_size_is_number() {
- let space = font.glyph_width(b' ' as u32);
+ let space = text_font.glyph_width(b' ' as u32);
CssPixels::nearest_value_for(
style.tab_size_number() * (space + text_context.word_spacing + text_context.letter_spacing) as f64,
)
@@ -1093,7 +1104,7 @@ impl<'pass> InlineContentPreparer<'pass> {
tab_stop = Some(PreparedTabStop {
tab_count: u32::try_from(tab_count).expect("tab counts fit in 32 bits"),
tab_inline_size,
- zero_advance: font.glyph_width(b'0' as u32),
+ zero_advance: text_font.glyph_width(b'0' as u32),
upright_advance: None,
});
}
diff --git a/Tests/LibWeb/Text/expected/css/tab-size-measured-in-spaces-of-text-font.txt b/Tests/LibWeb/Text/expected/css/tab-size-measured-in-spaces-of-text-font.txt
new file mode 100644
index 00000000000..bbf8f4d3ee1
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/tab-size-measured-in-spaces-of-text-font.txt
@@ -0,0 +1,4 @@
+tab-size 8 reaches 8 spaces: true
+tab-size 4 reaches 4 spaces: true
+a tab in an inline box uses the block container's spaces: true
+a tab stop of 8 spaces is 80px: 80px
diff --git a/Tests/LibWeb/Text/expected/intrinsic-inline-content-sizing.txt b/Tests/LibWeb/Text/expected/intrinsic-inline-content-sizing.txt
index 162362d0da7..749850fbbdb 100644
--- a/Tests/LibWeb/Text/expected/intrinsic-inline-content-sizing.txt
+++ b/Tests/LibWeb/Text/expected/intrinsic-inline-content-sizing.txt
@@ -161,9 +161,9 @@ vertical alignment, fit-content: [100,70,20,5,40,20]
mixed line heights, min-content: [120,90,20,10,80,40]
mixed line heights, max-content: [140,60,20,10,80,40]
mixed line heights, fit-content: [140,60,20,10,80,40]
-tabs, min-content: [80,60]
-tabs, max-content: [160,30]
-tabs, fit-content: [160,30]
+tabs, min-content: [160,60]
+tabs, max-content: [240,30]
+tabs, fit-content: [240,30]
decorated inline, min-content: [87,60,20,-2,67,34,0,28,27,34]
decorated inline, max-content: [154,30,20,-2,94,34]
decorated inline, fit-content: [154,30,20,-2,94,34]
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 cede2126139..32b0ab21d30 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: 213.03125
+Width: 83.90625
diff --git a/Tests/LibWeb/Text/input/css/tab-size-measured-in-spaces-of-text-font.html b/Tests/LibWeb/Text/input/css/tab-size-measured-in-spaces-of-text-font.html
new file mode 100644
index 00000000000..e6e5523c423
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/tab-size-measured-in-spaces-of-text-font.html
@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+@font-face {
+ font-family: Ahem;
+ src: url("../../../Ref/data/fonts/Ahem.ttf");
+}
+
+pre {
+ font: 10px Ahem;
+ margin: 0;
+}
+</style>
+<pre> <span id="after-tab">x</span></pre>
+<pre><span> </span><span id="after-spaces">x</span></pre>
+<pre style="tab-size: 4"> <span id="after-tab-4">x</span></pre>
+<pre><span> </span><span id="after-spaces-4">x</span></pre>
+<pre style="font-family: monospace"> <span id="after-tab-in-ahem-span" style="font-family: Ahem">x</span></pre>
+<pre style="font-family: monospace"><span id="ahem-span" style="font-family: Ahem"> x</span></pre>
+<pre style="font-family: monospace"><span> </span><span id="after-monospace-spaces">x</span></pre>
+<script>
+ // A tab stop is measured in spaces of the text's own font, whichever font the tab itself would be drawn with.
+ asyncTest(async done => {
+ await document.fonts.ready;
+ const left = id => document.getElementById(id).getBoundingClientRect().left;
+ println(`tab-size 8 reaches 8 spaces: ${left("after-tab") === left("after-spaces")}`);
+ println(`tab-size 4 reaches 4 spaces: ${left("after-tab-4") === left("after-spaces-4")}`);
+ // The spaces are those of the nearest block container, not of an inline box around the tab.
+ const range = document.createRange();
+ const ahemText = document.getElementById("ahem-span").firstChild;
+ range.setStart(ahemText, 1);
+ range.setEnd(ahemText, 2);
+ println(`a tab in an inline box uses the block container's spaces: ${range.getBoundingClientRect().left === left("after-monospace-spaces")}`);
+ println(`a tab stop of 8 spaces is 80px: ${left("after-tab") - document.getElementById("after-tab").parentElement.getBoundingClientRect().left}px`);
+ done();
+ });
+</script>