Skip to content

Truly independent web browser

LibWeb: Align SVG text to its text-before-edge and text-after-edge

sideshowbarkera02fb57c672e
diff --git a/Libraries/LibWeb/CSS/Keywords.json b/Libraries/LibWeb/CSS/Keywords.json
index bfda33b2ff8..52c2793ccb1 100644
--- a/Libraries/LibWeb/CSS/Keywords.json
+++ b/Libraries/LibWeb/CSS/Keywords.json
@@ -764,6 +764,8 @@
"telugu-alpha",
"telugu",
"text",
+ "text-after-edge",
+ "text-before-edge",
"text-bottom",
"text-top",
"textarea",
diff --git a/Libraries/LibWeb/CSS/Properties.json b/Libraries/LibWeb/CSS/Properties.json
index 587c60f8f7b..82f5d766608 100644
--- a/Libraries/LibWeb/CSS/Properties.json
+++ b/Libraries/LibWeb/CSS/Properties.json
@@ -2372,7 +2372,9 @@
"baseline-metric"
],
"valid-identifiers": [
- "auto"
+ "auto",
+ "text-after-edge>text-bottom",
+ "text-before-edge>text-top"
]
},
"empty-cells": {
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index ed30248b938..e5e12275e2d 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -1568,7 +1568,12 @@ impl<'pass> SvgFormattingContext<'pass> {
baseline_metric::IDEOGRAPHIC => -font.descent,
// FIXME: Read the math baseline from the font's BASE table.
baseline_metric::MATHEMATICAL => font.ascent * 0.5,
- // FIXME: Support text-top and text-bottom.
+ // https://drafts.csswg.org/css-inline/#text-over-baseline
+ // Corresponds to the metric used as the line-over edge of an inline's content box per [CSS2].
+ baseline_metric::TEXT_TOP => font.ascent,
+ // https://drafts.csswg.org/css-inline/#text-under-baseline
+ // Corresponds to the metric used as the line-under edge of an inline's content box per [CSS2].
+ baseline_metric::TEXT_BOTTOM => -font.descent,
_ => 0.0,
}
}
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/svg/text/reftests/green-100x100.svg b/Tests/LibWeb/Ref/expected/wpt-import/svg/text/reftests/green-100x100.svg
new file mode 100644
index 00000000000..120941444a4
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/svg/text/reftests/green-100x100.svg
@@ -0,0 +1,3 @@
+<svg xmlns="http://www.w3.org/2000/svg">
+ <rect width="100" height="100" fill="green"/>
+</svg>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/svg/text/reftests/dominant-baseline-text-after-edge.svg b/Tests/LibWeb/Ref/input/wpt-import/svg/text/reftests/dominant-baseline-text-after-edge.svg
new file mode 100644
index 00000000000..f836ee2a2f7
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/svg/text/reftests/dominant-baseline-text-after-edge.svg
@@ -0,0 +1,10 @@
+<svg xmlns="http://www.w3.org/2000/svg" xmlns:h="http://www.w3.org/1999/xhtml">
+ <metadata>
+ <title>'dominant-baseline: text-after-edge'</title>
+ <h:link rel="help" href="https://w3c.github.io/svgwg/svg2-draft/text.html#DominantBaselineProperty"/>
+ <h:link rel="match" href="../../../../../expected/wpt-import/svg/text/reftests/../../embedded/reference/green-rect-100x100.svg"/>
+ <h:link rel="stylesheet" type="text/css" href="../../../fonts/ahem.css"/>
+ </metadata>
+ <rect width="100" height="100" fill="red"/>
+ <text dominant-baseline="text-after-edge" y="100" fill="green" font-size="100" font-family="Ahem">X</text>
+</svg>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/svg/text/reftests/dominant-baseline-text-before-edge.svg b/Tests/LibWeb/Ref/input/wpt-import/svg/text/reftests/dominant-baseline-text-before-edge.svg
new file mode 100644
index 00000000000..775eda7c920
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/svg/text/reftests/dominant-baseline-text-before-edge.svg
@@ -0,0 +1,10 @@
+<svg xmlns="http://www.w3.org/2000/svg" xmlns:h="http://www.w3.org/1999/xhtml">
+ <metadata>
+ <title>'dominant-baseline: text-before-edge'</title>
+ <h:link rel="help" href="https://w3c.github.io/svgwg/svg2-draft/text.html#DominantBaselineProperty"/>
+ <h:link rel="match" href="../../../../../expected/wpt-import/svg/text/reftests/../../embedded/reference/green-rect-100x100.svg"/>
+ <h:link rel="stylesheet" type="text/css" href="../../../fonts/ahem.css"/>
+ </metadata>
+ <rect width="100" height="100" fill="red"/>
+ <text dominant-baseline="text-before-edge" y="0" fill="green" font-size="100" font-family="Ahem">X</text>
+</svg>
diff --git a/Tests/LibWeb/Text/expected/SVG/text-dominant-baseline.txt b/Tests/LibWeb/Text/expected/SVG/text-dominant-baseline.txt
new file mode 100644
index 00000000000..7d4eea18ec9
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/text-dominant-baseline.txt
@@ -0,0 +1,16 @@
+auto: computed auto, box 0,120 100x100
+alphabetic: computed alphabetic, box 0,120 100x100
+text-before-edge: computed text-top, box 0,200 100x100
+text-after-edge: computed text-bottom, box 0,100 100x100
+text-top: computed text-top, box 0,200 100x100
+text-bottom: computed text-bottom, box 0,100 100x100
+central: computed central, box 0,150 100x100
+middle: computed middle, box 0,160 100x100
+hanging: computed hanging, box 0,184 100x100
+ideographic: computed ideographic, box 0,100 100x100
+mathematical: computed mathematical, box 0,160 100x100
+bogus: computed auto, box 0,120 100x100
+In a style: box 0,200 100x100
+text-before-edge, inherited by a smaller <tspan>: computed text-top, box 100,200 50x50
+text-after-edge, inherited by a smaller <tspan>: computed text-bottom, box 100,150 50x50
+On a <tspan> alone: <tspan> box 100,200 100x100, <text> box 0,120 300x180
diff --git a/Tests/LibWeb/Text/input/SVG/text-dominant-baseline.html b/Tests/LibWeb/Text/input/SVG/text-dominant-baseline.html
new file mode 100644
index 00000000000..571244ddb6e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/text-dominant-baseline.html
@@ -0,0 +1,52 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ @font-face {
+ font-family: Ahem;
+ src: url("../../../Ref/data/fonts/Ahem.ttf");
+ }
+ body {
+ margin: 0;
+ }
+ svg {
+ display: block;
+ font: 100px Ahem;
+ }
+</style>
+<svg width="800" height="400"></svg>
+<script>
+ const round = value => Math.round(value * 100) / 100;
+ const box = element => {
+ const rect = element.getBBox();
+ return `${round(rect.x)},${round(rect.y)} ${round(rect.width)}x${round(rect.height)}`;
+ };
+
+ asyncTest(async done => {
+ await document.fonts.load("100px Ahem");
+ const svg = document.querySelector("svg");
+
+ // Ahem's ascent is 0.8em and its descent 0.2em, so a glyph cell is 100px tall, and sits 80px above the baseline.
+ const values = [
+ "auto", "alphabetic", "text-before-edge", "text-after-edge", "text-top", "text-bottom", "central", "middle",
+ "hanging", "ideographic", "mathematical", "bogus",
+ ];
+ for (const value of values) {
+ svg.innerHTML = `<text x="0" y="200" dominant-baseline="${value}">X</text>`;
+ const text = svg.firstChild;
+ println(`${value}: computed ${getComputedStyle(text).dominantBaseline}, box ${box(text)}`);
+ }
+
+ svg.innerHTML = `<text x="0" y="200" style="dominant-baseline: text-before-edge">X</text>`;
+ println(`In a style: box ${box(svg.firstChild)}`);
+
+ // Each element is aligned with its own font.
+ for (const value of ["text-before-edge", "text-after-edge"]) {
+ svg.innerHTML = `<text x="0" y="200" dominant-baseline="${value}">X<tspan font-size="50">X</tspan></text>`;
+ const tspan = svg.querySelector("tspan");
+ println(`${value}, inherited by a smaller <tspan>: computed ${getComputedStyle(tspan).dominantBaseline}, box ${box(tspan)}`);
+ }
+ svg.innerHTML = `<text x="0" y="200">X<tspan dominant-baseline="text-before-edge">X</tspan>X</text>`;
+ println(`On a <tspan> alone: <tspan> box ${box(svg.querySelector("tspan"))}, <text> box ${box(svg.firstChild)}`);
+ done();
+ });
+</script>