Truly independent web browser
LibWeb: Apply letter-spacing and word-spacing to SVG text
sideshowbarker13a59365c4e5
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rsindex cd6f8ffbe1b..f9652efc698 100644--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs@@ -1500,6 +1500,8 @@ impl<'pass> SvgFormattingContext<'pass> {glyph_cells: SvgCssPixelRect::default(),character_cells: Vec::with_capacity(text.len()),};+ let letter_spacing = style.letter_spacing().to_double() as f32;+ let word_spacing = style.word_spacing().to_double() as f32;Self::for_each_svg_font_run(style, text, |font, run_text| {let run_start_x = baseline_start.x + shaped_text.advance;let shaped = libgfx_rust::text_layout::shape_text(@@ -1507,8 +1509,8 @@ impl<'pass> SvgFormattingContext<'pass> {run_text,libgfx_rust::text_layout::TextType::Common,run_start_x,- 0.0,- 0.0,+ letter_spacing,+ word_spacing,);// https://svgwg.org/svg2-draft/coords.html#BoundingBoxes// The full glyph cell must have width equal to the horizontal advance and height equal to the EM box for@@ -1550,14 +1552,17 @@ impl<'pass> SvgFormattingContext<'pass> {/// The advance of the text run rendered by the given box; that is, of its direct child text.fn svg_text_run_advance(&self, text_box: Node, text: &[u16]) -> f32 {let mut advance = 0.0f32;- Self::for_each_svg_font_run(self.style(text_box), text, |font, run_text| {+ let style = self.style(text_box);+ let letter_spacing = style.letter_spacing().to_double() as f32;+ let word_spacing = style.word_spacing().to_double() as f32;+ Self::for_each_svg_font_run(style, text, |font, run_text| {advance += libgfx_rust::text_layout::shape_text(font,run_text,libgfx_rust::text_layout::TextType::Common,0.0,- 0.0,- 0.0,+ letter_spacing,+ word_spacing,).width();});diff --git a/Libraries/LibWeb/SVG/SVGTextContentElement.cpp b/Libraries/LibWeb/SVG/SVGTextContentElement.cppindex 9f9063ff5ad..839749b87c3 100644--- a/Libraries/LibWeb/SVG/SVGTextContentElement.cpp+++ b/Libraries/LibWeb/SVG/SVGTextContentElement.cpp@@ -79,8 +79,8 @@ static float sub_string_length(ReadonlySpan<Layout::RustFFI::FfiSvgTextCharacter// 1. Add the advance of the typographic character to length, adjusted for any font kerning in effect.// 2. If the letter-spacing or word-spacing properties contributed space just after the typographic// character, then add that space to length.- // NB: A cell's width is its typographic character's advance.- // FIXME: Layout applies neither letter-spacing nor word-spacing to SVG text, so there's no such space to add.+ // NB: A cell's width is its typographic character's advance, plus any letter-spacing and word-spacing layout+ // put just after it.length += cells[index].width;}diff --git a/Tests/LibWeb/Text/expected/SVG/text-letter-and-word-spacing.txt b/Tests/LibWeb/Text/expected/SVG/text-letter-and-word-spacing.txtnew file mode 100644index 00000000000..e7c46e4a13f--- /dev/null+++ b/Tests/LibWeb/Text/expected/SVG/text-letter-and-word-spacing.txt@@ -0,0 +1,7 @@+plain: length +0.0, first two characters +0.0+letter: length +25.0, first two characters +10.0+negative-letter: length +-10.0, first two characters +-4.0+word: length +10.0, first two characters +0.0+both: length +22.0, first two characters +6.0+anchored: length +25.0, first two characters +10.0+anchored: ends at 300diff --git a/Tests/LibWeb/Text/input/SVG/text-letter-and-word-spacing.html b/Tests/LibWeb/Text/input/SVG/text-letter-and-word-spacing.htmlnew file mode 100644index 00000000000..e54c2618365--- /dev/null+++ b/Tests/LibWeb/Text/input/SVG/text-letter-and-word-spacing.html@@ -0,0 +1,24 @@+<!DOCTYPE html>+<svg width="600" height="300" font-family="sans-serif" font-size="20">+ <text id="plain" x="0" y="20">ab cd</text>+ <text id="letter" x="0" y="50" letter-spacing="5">ab cd</text>+ <text id="negative-letter" x="0" y="80" letter-spacing="-2">ab cd</text>+ <text id="word" x="0" y="110" word-spacing="10">ab cd</text>+ <text id="both" x="0" y="140" style="letter-spacing: 3px; word-spacing: 7px">ab cd</text>+ <text id="anchored" x="300" y="170" text-anchor="end" letter-spacing="5">ab cd</text>+</svg>+<script src="../include.js"></script>+<script>+ test(() => {+ const plain = document.getElementById("plain");+ const plainLength = plain.getComputedTextLength();+ for (const text of document.querySelectorAll("text")) {+ const extra = text.getComputedTextLength() - plainLength;+ const firstTwo = text.getSubStringLength(0, 2) - plain.getSubStringLength(0, 2);+ println(`${text.id}: length +${extra.toFixed(1)}, first two characters +${firstTwo.toFixed(1)}`);+ }+ const anchored = document.getElementById("anchored");+ const box = anchored.getBBox();+ println(`anchored: ends at ${(box.x + box.width).toFixed(0)}`);+ });+</script>