Truly independent web browser
LibWeb: Count dx in the text chunk that text-anchor shifts
sideshowbarker207ade5a11a0
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rsindex df6a3ce9a11..d4954bc0d23 100644--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs@@ -1578,6 +1578,14 @@ impl<'pass> SvgFormattingContext<'pass> {None}+ /// How far a 'dx' shifts the first character of the given text node along the x axis.+ fn svg_text_dx_before(&self, text_node: Node) -> f32 {+ self.svg_text_shift_owner(text_node, |attributes| attributes.text_dx)+ .map_or(0.0, |element| {+ self.resolve_svg_text_length(element, self.svg_attributes(element).text_dx, self.viewport_width)+ })+ }+fn apply_svg_text_positioning(&mut self, text_box: Node, attributes: FfiSvgAttributeFacts) {let viewport_width = self.viewport_width;let viewport_height = self.viewport_height;@@ -1774,6 +1782,7 @@ impl<'pass> SvgFormattingContext<'pass> {let mut measurement = SvgTextChunkMeasurement::default();let mut found_chunk_start = false;let mut found_first_rendered_text = false;+ let mut found_first_rendered_text_node = false;let mut current = subtree_root;'walk: loop {let kind = self.node_kind(current);@@ -1784,6 +1793,17 @@ impl<'pass> SvgFormattingContext<'pass> {break 'walk;}} else if found_chunk_start+ && node_facts::kind_is_text(kind)+ && !self.processed_svg_text(current).is_empty()+ {+ // NB: A 'dx' on the chunk's first character moves where the chunk starts. Any other one moves the+ // characters after it apart from those before it, so it's part of what the chunk measures.+ if found_first_rendered_text_node {+ measurement.advance += self.svg_text_dx_before(current);+ }+ found_first_rendered_text_node = true;+ }+ if found_chunk_start&& node_facts::kind_is_text(kind)&& let Some(text) = self.svg_text_run_starting_at(current)&& !text.is_empty()diff --git a/Tests/LibWeb/Text/expected/SVG/text-anchor-with-dx.txt b/Tests/LibWeb/Text/expected/SVG/text-anchor-with-dx.txtnew file mode 100644index 00000000000..842bd78c6a7--- /dev/null+++ b/Tests/LibWeb/Text/expected/SVG/text-anchor-with-dx.txt@@ -0,0 +1,5 @@+anchored in the middle, dx inside: 160,30 220,30+anchored at the end, dx inside: 100,30 160,30 180,30+anchored at the end, negative dx inside: 150,30 160,30 180,30+anchored in the middle, dx on the first character: 220,30 240,30+anchored in the middle, dx in a second chunk: 180,30 200,30 310,30 350,30diff --git a/Tests/LibWeb/Text/input/SVG/text-anchor-with-dx.html b/Tests/LibWeb/Text/input/SVG/text-anchor-with-dx.htmlnew file mode 100644index 00000000000..17da6e63b12--- /dev/null+++ b/Tests/LibWeb/Text/input/SVG/text-anchor-with-dx.html@@ -0,0 +1,46 @@+<!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 = [+ ["anchored in the middle, dx inside", `<text x="200" y="30" text-anchor="middle">A<tspan dx="40">B</tspan></text>`],+ ["anchored at the end, dx inside", `<text x="200" y="30" text-anchor="end">A<tspan dx="40">B</tspan>C</text>`],+ ["anchored at the end, negative dx inside", `<text x="200" y="30" text-anchor="end">A<tspan dx="-10">B</tspan>C</text>`],+ ["anchored in the middle, dx on the first character", `<text x="200" y="30" text-anchor="middle"><tspan dx="40">A</tspan>B</text>`],+ ["anchored in the middle, dx in a second chunk", `<text x="200" y="30" text-anchor="middle">AB<tspan x="300" dx="40">C</tspan><tspan dx="20">D</tspan></text>`],+ ];+ for (const [name, markup] of cases) {+ svg.innerHTML = markup;+ println(`${name}: ${describe(svg.querySelector("text"))}`);+ }+ done();+ });+</script>