Skip to content

Truly independent web browser

LibWeb: Position an SVG character by its innermost dx and dy

sideshowbarker311487da54e7
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index e5e12275e2d..df6a3ce9a11 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -1526,6 +1526,58 @@ impl<'pass> SvgFormattingContext<'pass> {
renders_characters
}
+ /// The first text node below `root` that renders a character, if there is one.
+ fn first_rendered_svg_text_node(&self, root: Node) -> Option<Node> {
+ let mut first = None;
+ self.for_each_svg_text_node(root, |node| {
+ if first.is_none() && !self.processed_svg_text(node).is_empty() {
+ first = Some(node);
+ }
+ });
+ first
+ }
+
+ /// The text content element whose 'dx' or 'dy' (the attribute `shift_of` picks) shifts the first character of the
+ /// given text node, if any element's does.
+ /// https://svgwg.org/svg2-draft/text.html#TextLayoutAlgorithm
+ /// NB: An element's attribute positions the element's first character. Where that's the first character of an
+ /// element inside it too, which has the attribute as well, the inner element's attribute is the one that counts.
+ fn svg_text_shift_owner(
+ &self,
+ text_node: Node,
+ shift_of: fn(&FfiSvgAttributeFacts) -> FfiSvgLengthValue,
+ ) -> Option<Node> {
+ // OPTIMIZATION: Most text has no such attribute anywhere above it, which is cheap to find out.
+ let mut any_element_shifts = false;
+ let mut element = self.parent(text_node);
+ while self.node_kind(element) == NodeKind::SVGTextBox {
+ let attributes = self.svg_attributes(element);
+ any_element_shifts = any_element_shifts || shift_of(&attributes).kind != SVG_LENGTH_KIND_NONE;
+ if attributes.is_text_element {
+ break;
+ }
+ element = self.parent(element);
+ }
+ if !any_element_shifts {
+ return None;
+ }
+
+ let mut element = self.parent(text_node);
+ while self.node_kind(element) == NodeKind::SVGTextBox
+ && self.first_rendered_svg_text_node(element) == Some(text_node)
+ {
+ let attributes = self.svg_attributes(element);
+ if shift_of(&attributes).kind != SVG_LENGTH_KIND_NONE {
+ return Some(element);
+ }
+ if attributes.is_text_element {
+ break;
+ }
+ element = self.parent(element);
+ }
+ None
+ }
+
fn apply_svg_text_positioning(&mut self, text_box: Node, attributes: FfiSvgAttributeFacts) {
let viewport_width = self.viewport_width;
let viewport_height = self.viewport_height;
@@ -1535,8 +1587,23 @@ impl<'pass> SvgFormattingContext<'pass> {
if attributes.text_y.kind != SVG_LENGTH_KIND_NONE {
self.current_text_position.y = self.resolve_svg_text_length(text_box, attributes.text_y, viewport_height);
}
- self.current_text_position.x += self.resolve_svg_text_length(text_box, attributes.text_dx, viewport_width);
- self.current_text_position.y += self.resolve_svg_text_length(text_box, attributes.text_dy, viewport_height);
+ let has_dx = attributes.text_dx.kind != SVG_LENGTH_KIND_NONE;
+ let has_dy = attributes.text_dy.kind != SVG_LENGTH_KIND_NONE;
+ if !has_dx && !has_dy {
+ return;
+ }
+ let first_text_node = self.first_rendered_svg_text_node(text_box);
+ let shifts = |shift_of: fn(&FfiSvgAttributeFacts) -> FfiSvgLengthValue| {
+ first_text_node.is_none_or(|node| self.svg_text_shift_owner(node, shift_of) == Some(text_box))
+ };
+ let shifts_along_x = has_dx && shifts(|attributes| attributes.text_dx);
+ let shifts_along_y = has_dy && shifts(|attributes| attributes.text_dy);
+ if shifts_along_x {
+ self.current_text_position.x += self.resolve_svg_text_length(text_box, attributes.text_dx, viewport_width);
+ }
+ if shifts_along_y {
+ self.current_text_position.y += self.resolve_svg_text_length(text_box, attributes.text_dy, viewport_height);
+ }
}
/// https://drafts.csswg.org/css-inline/#dominant-baseline-property
diff --git a/Tests/LibWeb/Text/expected/SVG/text-dx-and-dy-in-nested-elements.txt b/Tests/LibWeb/Text/expected/SVG/text-dx-and-dy-in-nested-elements.txt
new file mode 100644
index 00000000000..b30fb3dc1e9
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/text-dx-and-dy-in-nested-elements.txt
@@ -0,0 +1,6 @@
+dx on an element and on its first child: 100,30 130,30 150,30
+dy on an element and on its first child: 100,30 120,40 140,40
+dx on an element, dy on its first child: 100,30 160,40 180,40
+dx on an element and on a later child: 100,30 160,30 190,30
+dx on a <text> and on its first child: 110,30 130,30
+dx on an element whose first child renders nothing: 100,30 160,30
diff --git a/Tests/LibWeb/Text/input/SVG/text-dx-and-dy-in-nested-elements.html b/Tests/LibWeb/Text/input/SVG/text-dx-and-dy-in-nested-elements.html
new file mode 100644
index 00000000000..331b6cc3520
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/text-dx-and-dy-in-nested-elements.html
@@ -0,0 +1,47 @@
+<!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: 20px Ahem;
+ }
+</style>
+<svg width="400" height="100"></svg>
+<script>
+ const round = value => Math.round(value * 100) / 100;
+ const describe = text => {
+ const positions = [];
+ for (let index = 0; index < text.getNumberOfChars(); ++index) {
+ const position = text.getStartPositionOfChar(index);
+ positions.push(`${round(position.x)},${round(position.y)}`);
+ }
+ return positions.join(" ");
+ };
+
+ asyncTest(async done => {
+ await document.fonts.load("20px Ahem");
+ const svg = document.querySelector("svg");
+
+ // Each glyph of Ahem is 20px wide here.
+ const cases = [
+ ["dx on an element and on its first child", `<text x="100" y="30">A<tspan dx="40"><tspan dx="10">B</tspan></tspan>C</text>`],
+ ["dy on an element and on its first child", `<text x="100" y="30">A<tspan dy="40"><tspan dy="10">B</tspan></tspan>C</text>`],
+ ["dx on an element, dy on its first child", `<text x="100" y="30">A<tspan dx="40"><tspan dy="10">B</tspan></tspan>C</text>`],
+ ["dx on an element and on a later child", `<text x="100" y="30">A<tspan dx="40">B<tspan dx="10">C</tspan></tspan></text>`],
+ ["dx on a <text> and on its first child", `<text x="100" y="30" dx="40"><tspan dx="10">A</tspan>B</text>`],
+ ["dx on an element whose first child renders nothing", `<text x="100" y="30">A<tspan dx="40"><tspan dx="10"></tspan>B</tspan></text>`],
+ ];
+ for (const [name, markup] of cases) {
+ svg.innerHTML = markup;
+ println(`${name}: ${describe(svg.querySelector("text"))}`);
+ }
+ done();
+ });
+</script>