Truly independent web browser
LibWeb: Anchor an SVG text chunk by what its characters cover
sideshowbarker5aa05561d56b
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rsindex 36dc0575ad9..d9d758a6387 100644--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs@@ -720,14 +720,18 @@ impl SvgTextLengthAdjustment {#[derive(Clone, Copy, Debug, PartialEq)]struct SvgTextChunkMeasurement {- advance: f32,+ /// The least and the greatest position along the x axis that the characters of the chunk cover, counted from where+ /// the chunk starts.+ start: f32,+ end: f32,anchor: u8,}impl Default for SvgTextChunkMeasurement {fn default() -> Self {Self {- advance: 0.0,+ start: 0.0,+ end: 0.0,anchor: text_anchor::START,}}@@ -2008,7 +2012,7 @@ impl<'pass> SvgFormattingContext<'pass> {advance}- /// Measures the total advance of the text chunk that starts at the given box, and determines+ /// Measures what the characters of the text chunk that starts at the given box cover, and determines/// the 'text-anchor' value that applies to the chunk. The chunk extends in document order/// through the subtree of the containing <text> element until the next box that starts a chunk/// of its own, so it takes in the text that follows the box in the elements the box is in.@@ -2024,7 +2028,7 @@ impl<'pass> SvgFormattingContext<'pass> {let mut found_chunk_start = false;let mut found_first_rendered_text = false;let mut found_first_rendered_text_node = false;- let mut chunk_ends_the_text = false;+ let mut position = 0.0f32;let mut current = subtree_root;'walk: loop {let kind = self.node_kind(current);@@ -2041,7 +2045,7 @@ impl<'pass> SvgFormattingContext<'pass> {// 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);+ position += self.svg_text_dx_before(current);}found_first_rendered_text_node = true;}@@ -2059,7 +2063,12 @@ impl<'pass> SvgFormattingContext<'pass> {measurement.anchor = self.style(text_box).inherited_svg().text_anchor;found_first_rendered_text = true;}- measurement.advance += self.svg_text_run_advance(text_box, &text);+ // NB: The space 'textLength' adds after each character is no part of what the character covers.+ let run_start = position;+ position += self.svg_text_run_advance(text_box, &text);+ let run_end = position - self.text_length_adjustment.spacing();+ measurement.start = measurement.start.min(run_start).min(run_end);+ measurement.end = measurement.end.max(run_start).max(run_end);}// AD-HOC: Text on a path is laid out independently; see svg_text_path_box_path().if kind == NodeKind::SVGTextBox {@@ -2071,7 +2080,6 @@ impl<'pass> SvgFormattingContext<'pass> {}loop {if current == subtree_root {- chunk_ends_the_text = true;break 'walk;}let sibling = self.next_sibling(current);@@ -2082,10 +2090,6 @@ impl<'pass> SvgFormattingContext<'pass> {current = self.parent(current);}}- // NB: 'textLength' adds no space after the last character of the text.- if chunk_ends_the_text && found_first_rendered_text {- measurement.advance -= self.text_length_adjustment.spacing();- }measurement}@@ -2121,23 +2125,26 @@ impl<'pass> SvgFormattingContext<'pass> {// The 'text-anchor' property is applied to each individual text chunk within a given// 'text' element.// AD-HOC: The spec applies 'text-anchor' as a shift of the chunk's rendered glyphs- // after layout; shifting the chunk's starting position up front by the chunk's- // total advance is equivalent for horizontal text - since every run in the- // chunk is laid out sequentially from this position.+ // after layout; shifting the chunk's starting position up front by what the+ // chunk's characters cover is equivalent for horizontal text - since every run+ // in the chunk is laid out sequentially from this position.+ // NB: The start and the end of the rendered text are the least and the greatest position its characters+ // cover. Those aren't where the first character starts and the last one ends, where a negative 'dx'+ // takes a character back past the ones before it.let chunk = self.measure_svg_text_chunk(text_box);match chunk.anchor {// The rendered characters are aligned such that the start of the resulting// rendered text is at the initial current text position.- text_anchor::START => {}+ text_anchor::START => self.current_text_position.x -= chunk.start,// The rendered characters are shifted such that the geometric middle of the// resulting rendered text (determined from the initial and final current text// position before applying the 'text-anchor' property) is at the initial current// text position.- text_anchor::MIDDLE => self.current_text_position.x -= chunk.advance / 2.0,+ text_anchor::MIDDLE => self.current_text_position.x -= (chunk.start + chunk.end) / 2.0,// The rendered characters are shifted such that the end of the resulting rendered// text (final current text position before applying the 'text-anchor' property) is// at the initial current text position.- text_anchor::END => self.current_text_position.x -= chunk.advance,+ text_anchor::END => self.current_text_position.x -= chunk.end,_ => unreachable!("invalid text-anchor value"),}}@@ -2390,7 +2397,9 @@ impl<'pass> SvgFormattingContext<'pass> {/// properties letter-spacing and word-spacing and adjustments due to attributes 'dx' and 'dy' on this 'text' or/// 'tspan' element or any descendants. This value is used to calibrate the user agent's own calculations with/// that of the author.- /// NB: A 'dx' on the text's first character moves where the text starts, so it's no part of the length.+ /// NB: A 'dx' on the text's first character moves where the text starts, so it's no part of the length. And the+ /// length is what the characters cover: from the least position one of them starts at to the greatest one+ /// ends at, which is past where the last one ends where a negative 'dx' takes a character back./// AD-HOC: Text on a path isn't adjusted, since it's laid out independently.fn svg_text_length_adjustment(&self, text_box: Node, attributes: FfiSvgAttributeFacts) -> SvgTextLengthAdjustment {if attributes.text_length.kind == SVG_LENGTH_KIND_NONE {@@ -2408,13 +2417,14 @@ impl<'pass> SvgFormattingContext<'pass> {} else {f32::MIN_POSITIVE};- let mut natural_length = 0.0f32;+ let mut position = 0.0f32;+ let (mut start, mut end) = (0.0f32, 0.0f32);let mut typographic_character_count = 0usize;let mut found_first_rendered_text_node = false;self.for_each_svg_text_node_in(text_box, false, |node| {if !self.processed_svg_text(node).is_empty() {if found_first_rendered_text_node {- natural_length += self.svg_text_dx_before(node);+ position += self.svg_text_dx_before(node);}found_first_rendered_text_node = true;}@@ -2427,13 +2437,17 @@ impl<'pass> SvgFormattingContext<'pass> {FfiFloatPoint::default(),spacing_while_measuring,);- natural_length += shaped.advance;+ let run_start = position;+ position += shaped.advance;+ start = start.min(run_start).min(position);+ end = end.max(run_start).max(position);typographic_character_count += shaped.character_cells.iter().filter(|cell| cell.starts_typographic_character).count();});+ let natural_length = end - start;if attributes.text_length_adjusts_glyphs {if natural_length <= 0.0 {return SvgTextLengthAdjustment::None;diff --git a/Tests/LibWeb/Text/expected/SVG/text-anchor-with-characters-moved-back.txt b/Tests/LibWeb/Text/expected/SVG/text-anchor-with-characters-moved-back.txtnew file mode 100644index 00000000000..39878fea01b--- /dev/null+++ b/Tests/LibWeb/Text/expected/SVG/text-anchor-with-characters-moved-back.txt@@ -0,0 +1,7 @@+anchored at the start: 260,30 280,30 200,30+anchored in the middle: 210,30 230,30 150,30+anchored at the end: 160,30 180,30 100,30+anchored in the middle, moved back onto the character before: 180,30 200,30 190,30+anchored at the end, moved back onto the character before: 160,30 180,30 170,30+anchored at the end, the last character moved back past the first: 180,30 100,30+anchored at the end, in a second chunk: 30,30 260,30 280,30 200,30diff --git a/Tests/LibWeb/Text/expected/SVG/text-length.txt b/Tests/LibWeb/Text/expected/SVG/text-length.txtindex 9dd86fa3c84..4f1c50b1aeb 100644--- a/Tests/LibWeb/Text/expected/SVG/text-length.txt+++ b/Tests/LibWeb/Text/expected/SVG/text-length.txt@@ -27,6 +27,9 @@ with a dx on the first character: 30+20 90+20 150+20 210+20; length 80; box 30,1with a dx on the first character, glyphs too: 30+50 80+50 130+50 180+50; length 200; box 30,14 200x20with a dx at the start of a chunk, glyphs too: 10+40 50+40 320+40 360+40; length 160; box 10,14 390x20with a dx in nested elements, glyphs too: 10+40 50+40 130+40 170+40; length 160; box 10,14 200x20+with a dx that takes a character back past the first: 100+20 120+20; length 40; box 100,14 40x20+with a dx that takes a character back past the first, glyphs too: 220+40 100+40; length 80; box 100,14 160x20+with a dx that takes a character back onto the one before, glyphs too: 132+64 196+64 100+64; length 192; box 100,14 160x20on a <tspan>: 10+20 30+20 50+20 70+20 90+20 110+20; length 120; box 10,14 120x20with text on a path: 10+20 70+20 0+20 20+20; length 80; box 10,14 130x40textLength set: 10+20 70+20 130+20 190+20; length 80; box 10,14 200x20diff --git a/Tests/LibWeb/Text/input/SVG/text-anchor-with-characters-moved-back.html b/Tests/LibWeb/Text/input/SVG/text-anchor-with-characters-moved-back.htmlnew file mode 100644index 00000000000..f54f1c7553b--- /dev/null+++ b/Tests/LibWeb/Text/input/SVG/text-anchor-with-characters-moved-back.html@@ -0,0 +1,50 @@+<!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.+ // A negative dx takes a character back past the ones before it, so the chunk doesn't end where its last+ // character does.+ const cases = [+ ["anchored at the start", `<text x="200" y="30">AB<tspan dx="-100">C</tspan></text>`],+ ["anchored in the middle", `<text x="200" y="30" text-anchor="middle">AB<tspan dx="-100">C</tspan></text>`],+ ["anchored at the end", `<text x="200" y="30" text-anchor="end">AB<tspan dx="-100">C</tspan></text>`],+ ["anchored in the middle, moved back onto the character before", `<text x="200" y="30" text-anchor="middle">AB<tspan dx="-30">C</tspan></text>`],+ ["anchored at the end, moved back onto the character before", `<text x="200" y="30" text-anchor="end">AB<tspan dx="-30">C</tspan></text>`],+ ["anchored at the end, the last character moved back past the first", `<text x="200" y="30" text-anchor="end">A<tspan dx="-100">B</tspan></text>`],+ ["anchored at the end, in a second chunk", `<text x="50" y="30" text-anchor="end">A<tspan x="300">BC<tspan dx="-100">D</tspan></tspan></text>`],+ ];+ for (const [name, markup] of cases) {+ svg.innerHTML = markup;+ println(`${name}: ${describe(svg.querySelector("text"))}`);+ }+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/SVG/text-length.html b/Tests/LibWeb/Text/input/SVG/text-length.htmlindex 03ae683abc9..f1d4da19687 100644--- a/Tests/LibWeb/Text/input/SVG/text-length.html+++ b/Tests/LibWeb/Text/input/SVG/text-length.html@@ -61,6 +61,9 @@["with a dx on the first character, glyphs too", `<text x="10" y="30" textLength="200" lengthAdjust="spacingAndGlyphs"><tspan dx="20">AB</tspan>CD</text>`],["with a dx at the start of a chunk, glyphs too", `<text x="10" y="30" textLength="200" lengthAdjust="spacingAndGlyphs">AB<tspan x="300" dx="20">CD</tspan></text>`],["with a dx in nested elements, glyphs too", `<text x="10" y="30" textLength="200" lengthAdjust="spacingAndGlyphs">AB<tspan dx="100"><tspan dx="20">CD</tspan></tspan></text>`],+ ["with a dx that takes a character back past the first", `<text x="100" y="30" textLength="160">A<tspan dx="-80">B</tspan></text>`],+ ["with a dx that takes a character back past the first, glyphs too", `<text x="100" y="30" textLength="160" lengthAdjust="spacingAndGlyphs">A<tspan dx="-80">B</tspan></text>`],+ ["with a dx that takes a character back onto the one before, glyphs too", `<text x="100" y="30" textLength="160" lengthAdjust="spacingAndGlyphs">AB<tspan dx="-50">C</tspan></text>`],["on a <tspan>", `<text x="10" y="30">AB<tspan textLength="100">CD</tspan>EF</text>`],["with text on a path", `<path id="path" d="M100 50H500" /><text x="10" y="30" textLength="80">AB<textPath href="#path">CD</textPath></text>`],];