Skip to content

Truly independent web browser

LibWeb: Map a tspan’s paint server onto its whole text’s box

sideshowbarker3a14c079c806
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs
index 81841a7fe7c..cfb13056b78 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/svg.rs
@@ -500,6 +500,37 @@ fn svg_content_force_dark_role(source: &impl PaintRead, paintable: NodeSlotId) -
}
}
+/// The box that a paint server on `paintable`, whose own geometry is `geometry`, maps its object bounding box units onto.
+fn paint_server_object_bounding_box(
+ source: &impl PaintRead,
+ paintable: NodeSlotId,
+ geometry: &crate::layout::svg_formatting_context::SvgGeometry,
+) -> FloatRect {
+ // https://svgwg.org/svg2-draft/text.html#ObjectBoundingBoxUnitsTextObjects
+ // When one of these facilities is applied to text and keyword 'objectBoundingBox' is used (see Object bounding box
+ // units) to specify a graphical effect relative to the "object bounding box", then the object bounding box units
+ // are computed relative to the entire 'text' element in all cases, even when different effects are applied to
+ // different 'tspan' or 'textPath' elements within the same 'text' element.
+ // NB: The layout of a <text> lays out every element inside it again, so a change to the <text>'s box records the
+ // elements that paint with it again too.
+ let mut text = paintable;
+ while let Some(parent) = source.node_parent_if_live(text)
+ && matches!(
+ source.node_kind_if_live(parent),
+ Some(NodeKind::SVGTextBox | NodeKind::SVGTextPathBox)
+ )
+ {
+ text = parent;
+ }
+ if text == paintable {
+ return geometry.object_bounding_box;
+ }
+ crate::painting::paintable_geometry::committed_svg_geometry(source, text)
+ .map_or(geometry.object_bounding_box, |text_geometry| {
+ text_geometry.object_bounding_box
+ })
+}
+
fn paint_path_parts<O: Observer>(
recorder: &mut PaintRecorder<'_, O>,
paintable: NodeSlotId,
@@ -598,13 +629,14 @@ fn paint_path_parts<O: Observer>(
let geometry =
crate::painting::paintable_geometry::committed_svg_geometry(recorder.source, paintable).unwrap_or_default();
+ let object_bounding_box = paint_server_object_bounding_box(recorder.source, paintable, &geometry);
for (path, path_bounding_box, color_glyph_runs) in &parts {
// NB: A paint server is placed from the bounding box of the path it paints, so each part is painted with its
// own.
let paint_context = SvgPaintContext {
viewport: geometry.viewport_user_rect,
path_bounding_box: *path_bounding_box,
- object_bounding_box: geometry.object_bounding_box,
+ object_bounding_box,
paint_transform: affine(paint_transform),
content_scale: recorder.own_accumulated_2d_scale(paintable),
};
diff --git a/Tests/LibWeb/Ref/expected/svg/text-content-paint-server-follows-text-bounding-box-ref.html b/Tests/LibWeb/Ref/expected/svg/text-content-paint-server-follows-text-bounding-box-ref.html
new file mode 100644
index 00000000000..5cb465bcf38
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/svg/text-content-paint-server-follows-text-bounding-box-ref.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<style>
+ body {
+ margin: 0;
+ }
+ svg {
+ display: block;
+ }
+</style>
+<svg width="400" height="80">
+ <rect x="10" y="14" width="80" height="20" fill="green" />
+ <rect x="90" y="14" width="80" height="20" />
+</svg>
diff --git a/Tests/LibWeb/Ref/expected/svg/text-content-paint-server-uses-text-bounding-box-ref.html b/Tests/LibWeb/Ref/expected/svg/text-content-paint-server-uses-text-bounding-box-ref.html
new file mode 100644
index 00000000000..7718cf990ca
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/svg/text-content-paint-server-uses-text-bounding-box-ref.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<style>
+ body {
+ margin: 0;
+ }
+ svg {
+ display: block;
+ }
+</style>
+<svg width="400" height="280">
+ <rect x="10" y="14" width="120" height="20" />
+ <rect x="130" y="14" width="40" height="20" fill="orange" />
+ <rect x="10" y="54" width="120" height="20" />
+ <rect x="130" y="54" width="40" height="20" fill="orange" />
+ <rect x="10" y="94" width="120" height="20" />
+ <rect x="130" y="94" width="40" height="20" fill="orange" />
+ <rect x="10" y="134" width="120" height="20" />
+ <rect x="130" y="134" width="40" height="20" fill="orange" />
+ <rect x="10" y="174" width="120" height="20" />
+ <rect x="130" y="174" width="40" height="20" fill="orange" />
+ <rect x="10" y="214" width="120" height="20" />
+ <rect x="10" y="254" width="40" height="20" fill="green" />
+</svg>
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/svg/pservers/reftests/gradient-on-text-after-font-size-change-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/svg/pservers/reftests/gradient-on-text-after-font-size-change-ref.html
new file mode 100644
index 00000000000..235bd6333ff
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/svg/pservers/reftests/gradient-on-text-after-font-size-change-ref.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html>
+<meta charset="utf-8">
+<link rel="stylesheet" href="../../../fonts/ahem.css">
+<svg width="600" height="80">
+ <linearGradient id="g" gradientUnits="objectBoundingBox">
+ <stop offset="0.5" stop-color="green"/>
+ <stop offset="0.5" stop-color="orange"/>
+ </linearGradient>
+ <text x="10" y="55" font-family="Ahem" font-size="18">
+ <tspan fill="url(#g)">X</tspan>
+ </text>
+</svg>
+</html>
diff --git a/Tests/LibWeb/Ref/input/svg/text-content-paint-server-follows-text-bounding-box.html b/Tests/LibWeb/Ref/input/svg/text-content-paint-server-follows-text-bounding-box.html
new file mode 100644
index 00000000000..93314b8ca20
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/svg/text-content-paint-server-follows-text-bounding-box.html
@@ -0,0 +1,38 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<link rel="match" href="../../expected/svg/text-content-paint-server-follows-text-bounding-box-ref.html" />
+<style>
+ @font-face {
+ font-family: Ahem;
+ src: url("../../data/fonts/Ahem.ttf");
+ }
+ body {
+ margin: 0;
+ }
+ svg {
+ display: block;
+ font: 20px Ahem;
+ }
+</style>
+<svg width="400" height="80">
+ <linearGradient id="split">
+ <stop offset="0.5" stop-color="green" />
+ <stop offset="0.5" stop-color="orange" />
+ </linearGradient>
+ <!-- The <text>'s box grows with its other <tspan>, and the paint server on the first one follows it. -->
+ <text x="10" y="30"><tspan fill="url(#split)">XXXX</tspan><tspan id="grows">XX</tspan></text>
+</svg>
+<script>
+ requestAnimationFrame(() => {
+ requestAnimationFrame(() => {
+ document.getElementById("grows").textContent = "XXXX";
+ requestAnimationFrame(() => {
+ if (window.internals)
+ internals.signalTestIsDone("PASS");
+ else
+ document.documentElement.classList.remove("reftest-wait");
+ });
+ });
+ });
+</script>
+</html>
diff --git a/Tests/LibWeb/Ref/input/svg/text-content-paint-server-uses-text-bounding-box.html b/Tests/LibWeb/Ref/input/svg/text-content-paint-server-uses-text-bounding-box.html
new file mode 100644
index 00000000000..d7a1ad5b835
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/svg/text-content-paint-server-uses-text-bounding-box.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<link rel="match" href="../../expected/svg/text-content-paint-server-uses-text-bounding-box-ref.html" />
+<style>
+ @font-face {
+ font-family: Ahem;
+ src: url("../../data/fonts/Ahem.ttf");
+ }
+ body {
+ margin: 0;
+ }
+ svg {
+ display: block;
+ font: 20px Ahem;
+ }
+</style>
+<svg width="400" height="280">
+ <defs>
+ <linearGradient id="split">
+ <stop offset="0.5" stop-color="green" />
+ <stop offset="0.5" stop-color="orange" />
+ </linearGradient>
+ <pattern id="stripes" width="0.5" height="1" patternContentUnits="objectBoundingBox">
+ <rect width="0.25" height="1" fill="green" />
+ <rect x="0.25" width="0.25" height="1" fill="orange" />
+ </pattern>
+ <path id="path1" d="M 90 150 H 390" />
+ <path id="path2" d="M 130 190 H 390" />
+ </defs>
+ <!-- An objectBoundingBox paint server on a <tspan> maps onto the whole <text>'s box, not the <tspan>'s. -->
+ <text x="10" y="30">XXXXXX<tspan fill="url(#split)">XX</tspan></text>
+ <text x="10" y="70">XXXXXX<tspan fill="url(#stripes)">XX</tspan></text>
+ <!-- A <tspan> inside another one. -->
+ <text x="10" y="110">XX<tspan>XXXX<tspan fill="url(#split)">XX</tspan></tspan></text>
+ <!-- A <tspan> inside a <textPath>, and a <textPath> itself. -->
+ <text x="10" y="150">XXXX<textPath href="#path1">XX<tspan fill="url(#split)">XX</tspan></textPath></text>
+ <text x="10" y="190">XXXXXX<textPath href="#path2" fill="url(#split)">XX</textPath></text>
+ <!-- A <tspan> on a line of its own. -->
+ <text x="10" y="230">XXXXXX<tspan x="10" y="270" fill="url(#split)">XX</tspan></text>
+</svg>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/svg/pservers/reftests/gradient-on-tspan-after-font-size-change.html b/Tests/LibWeb/Ref/input/wpt-import/svg/pservers/reftests/gradient-on-tspan-after-font-size-change.html
new file mode 100644
index 00000000000..843a33f3d92
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/svg/pservers/reftests/gradient-on-tspan-after-font-size-change.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<meta charset="utf-8">
+<link rel="match" href="../../../../../expected/wpt-import/svg/pservers/reftests/gradient-on-text-after-font-size-change-ref.html">
+<link rel="help" href="https://issues.chromium.org/issues/557821366">
+<link rel="help" href="https://svgwg.org/svg2-draft/text.html#ObjectBoundingBoxUnitsTextObjects">
+<link rel="stylesheet" href="../../../fonts/ahem.css">
+<script src="../../../common/reftest-wait.js"></script>
+<script>
+document.documentElement.addEventListener('TestRendered', () => {
+ document.querySelector('text').setAttribute('font-size', '18');
+ takeScreenshot();
+});
+</script>
+<svg width="600" height="80">
+ <linearGradient id="g" gradientUnits="objectBoundingBox">
+ <stop offset="0.5" stop-color="green"/>
+ <stop offset="0.5" stop-color="orange"/>
+ </linearGradient>
+ <text x="10" y="55" font-family="Ahem" font-size="36">
+ <tspan fill="url(#g)">X</tspan>
+ </text>
+</svg>
+</html>