Skip to content

Truly independent web browser

LibWeb: Paint each <tspan> on a <textPath> in its own style

sideshowbarker982ef594293b
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index b481e1f2707..f4ac96fe7a0 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -915,6 +915,10 @@ pub(super) struct SvgFormattingContext<'pass> {
/// Where text-transform changed the characters of a text node in `processed_svg_text`, how its characters map to
/// those it had before the transform.
svg_text_transform_edits: RefCell<std::collections::HashMap<Node, Vec<super::rendered_text::RenderedTextEdit>>>,
+ /// The glyph outlines, laid along the path, and the character cells of each text content element inside the
+ /// <textPath> laid out last, which that element paints in its own style.
+ text_on_path_glyphs:
+ RefCell<std::collections::HashMap<Node, (libgfx_rust::path::Path, Vec<FfiSvgTextCharacterCell>)>>,
/// The glyph cells of the text content element laid out last: those of its whole subtree, in document order.
last_text_subtree_cells: Option<std::sync::Arc<Vec<FfiSvgTextCharacterCell>>>,
/// The stretches of text in the subtree of the <text>, <tspan>, or <a> laid out last, in document order.
@@ -959,6 +963,7 @@ impl<'pass> SvgFormattingContext<'pass> {
text_length_adjustment: SvgTextLengthAdjustment::None,
processed_svg_text: RefCell::new(std::collections::HashMap::new()),
svg_text_transform_edits: RefCell::new(std::collections::HashMap::new()),
+ text_on_path_glyphs: RefCell::new(std::collections::HashMap::new()),
last_text_subtree_cells: None,
last_text_subtree_spans: Vec::new(),
root_payload: RefCell::new(fragment_tree::PendingPayload::default()),
@@ -2616,15 +2621,31 @@ impl<'pass> SvgFormattingContext<'pass> {
/// https://svgwg.org/svg2-draft/text.html#TextPathElement
/// FIXME: The character cells are those of the text shaped from the origin; they aren't moved
/// onto the path or rotated along it, so only their advances are meaningful.
- fn svg_text_path_box_path(&self, text_path_box: Node) -> (libgfx_rust::path::Path, Vec<FfiSvgTextCharacterCell>) {
+ /// Where some of its own text follows a child element, also returns the parts its path paints in.
+ fn svg_text_path_box_path(
+ &self,
+ text_path_box: Node,
+ ) -> (
+ libgfx_rust::path::Path,
+ Vec<FfiSvgTextCharacterCell>,
+ Option<Vec<fragment_tree::SvgTextSegment>>,
+ ) {
let empty = || libgfx_rust::path::Path::new();
let Some(shape_path) = self.svg_referenced_shape_path(text_path_box) else {
- return (empty(), Vec::new());
+ self.text_on_path_glyphs.borrow_mut().clear();
+ return (empty(), Vec::new(), None);
};
let style = self.style(text_path_box);
- let mut runs = Vec::new();
+ // The runs of the <textPath>'s own text, each with the child element it follows, or the invalid node for the
+ // text that comes before every child element: Text that follows a child paints after it.
+ let mut runs: Vec<(Node, Vec<libgfx_rust::path::GlyphRun>)> = Vec::new();
+ let mut after_child = Node::INVALID;
let mut character_cells = Vec::new();
+ // NB: Each text content element inside the <textPath> paints its own text, in its own style.
+ let mut runs_of_elements: Vec<(Node, Vec<libgfx_rust::path::GlyphRun>)> = Vec::new();
+ let mut cells_of_elements: std::collections::HashMap<Node, Vec<FfiSvgTextCharacterCell>> =
+ std::collections::HashMap::new();
let mut advance = 0.0f32;
// https://svgwg.org/svg2-draft/text.html#TextLayoutPath
// Align the glyph vertically relative to the midpoint-on-the-path based on property alignment-baseline and any
@@ -2683,8 +2704,34 @@ impl<'pass> SvgFormattingContext<'pass> {
extent_start = extent_start.min(run_start).min(advance);
extent_end = extent_end.max(run_start).max(advance);
}
- runs.extend(shaped.runs);
+ let first_cell = character_cells.len();
self.append_svg_run_dom_cells(node, shaped.character_cells, &mut character_cells);
+ let owner = self.parent(node);
+ if owner == text_path_box || self.node_kind(owner) != NodeKind::SVGTextBox {
+ match runs.last_mut() {
+ Some((child, own_runs)) if *child == after_child => own_runs.extend(shaped.runs),
+ _ => runs.push((after_child, shaped.runs)),
+ }
+ return;
+ }
+ let mut child = owner;
+ while self.parent(child) != text_path_box {
+ child = self.parent(child);
+ }
+ after_child = child;
+ match runs_of_elements.iter_mut().find(|(element, _)| *element == owner) {
+ Some((_, element_runs)) => element_runs.extend(shaped.runs),
+ None => runs_of_elements.push((owner, shaped.runs)),
+ }
+ // The cells of an element's characters count toward each element it's in, as they do in a <text>.
+ let mut element = owner;
+ while element != text_path_box && self.node_kind(element) == NodeKind::SVGTextBox {
+ cells_of_elements
+ .entry(element)
+ .or_default()
+ .extend_from_slice(&character_cells[first_cell..]);
+ element = self.parent(element);
+ }
});
// https://svgwg.org/svg2-draft/text.html#TextPathElementStartOffsetAttribute
@@ -2703,7 +2750,33 @@ impl<'pass> SvgFormattingContext<'pass> {
}
}
- (shape_path.place_glyph_runs_along(&runs, start_offset), character_cells)
+ let mut text_on_path_glyphs = self.text_on_path_glyphs.borrow_mut();
+ text_on_path_glyphs.clear();
+ for (element, element_runs) in runs_of_elements {
+ let cells = cells_of_elements.remove(&element).unwrap_or_default();
+ text_on_path_glyphs.insert(
+ element,
+ (shape_path.place_glyph_runs_along(&element_runs, start_offset), cells),
+ );
+ }
+ for (element, cells) in cells_of_elements {
+ text_on_path_glyphs.insert(element, (libgfx_rust::path::Path::new(), cells));
+ }
+ let mut path = empty();
+ let mut segments = Vec::new();
+ let paints_as_one_path = runs.iter().all(|(after_child, _)| after_child.is_invalid());
+ for (after_child, own_runs) in runs {
+ let part = shape_path.place_glyph_runs_along(&own_runs, start_offset);
+ path.append_path(&part);
+ if !paints_as_one_path {
+ segments.push(fragment_tree::SvgTextSegment {
+ after_child,
+ path: part,
+ color_glyph_runs: Vec::new(),
+ });
+ }
+ }
+ (path, character_cells, (!paints_as_one_path).then_some(segments))
}
/// The 'dx' and 'dy' that shift the first character of a text node in a <textPath>: those of the innermost element
@@ -3339,6 +3412,15 @@ impl<'pass> SvgFormattingContext<'pass> {
let mut own_character_cells = None;
let (path, glyph_cells, character_cells) = match kind {
NodeKind::SVGGeometryBox => (self.svg_geometry_path(graphics_box), None, None),
+ NodeKind::SVGTextBox if self.text_on_path_glyphs.borrow().contains_key(&graphics_box) => {
+ let (path, cells) = self
+ .text_on_path_glyphs
+ .borrow_mut()
+ .remove(&graphics_box)
+ .expect("the element's glyphs on the path");
+ text_children_bounding_box = self.layout_text_content_children_on_path(run, graphics_box);
+ (path, None, Some(cells))
+ }
NodeKind::SVGTextBox => {
let contents = self.layout_svg_text_box_contents(run, graphics_box);
text_segments = contents.segments;
@@ -3351,7 +3433,9 @@ impl<'pass> SvgFormattingContext<'pass> {
)
}
NodeKind::SVGTextPathBox => {
- let (path, character_cells) = self.svg_text_path_box_path(graphics_box);
+ let (path, character_cells, segments) = self.svg_text_path_box_path(graphics_box);
+ text_segments = segments;
+ text_children_bounding_box = self.layout_text_content_children_on_path(run, graphics_box);
(path, None, Some(character_cells))
}
_ => (libgfx_rust::path::Path::new(), None, None),
@@ -3394,6 +3478,31 @@ impl<'pass> SvgFormattingContext<'pass> {
self.set_content_rect(graphics_box, cells, bounding_box, object_bounding_box)
}
+ /// Lays out the text content elements inside a <textPath>, or inside one of those, each with the glyphs its own text
+ /// has on the path, and returns the box they cover.
+ fn layout_text_content_children_on_path(
+ &mut self,
+ run: &FormattingContextRun<'pass>,
+ element: Node,
+ ) -> SvgCssPixelRect {
+ let mut bounding_box = SvgCssPixelRect::default();
+ let children: Vec<Node> = self.run.pass.children(element).collect();
+ for child in children {
+ if self.node_kind(child) != NodeKind::SVGTextBox {
+ continue;
+ }
+ // NB: An element with no text on the path, such as one in a <textPath> whose path doesn't resolve, renders
+ // nothing, rather than its text laid out as if it weren't on a path.
+ self.text_on_path_glyphs
+ .borrow_mut()
+ .entry(child)
+ .or_insert_with(|| (libgfx_rust::path::Path::new(), Vec::new()));
+ let geometry = self.layout_graphics_element(run, child);
+ bounding_box.unite(float_rect_to_css_pixels(geometry.stroke_bounding_box));
+ }
+ bounding_box
+ }
+
// https://w3c.github.io/svgwg/svg2-draft/embedded.html#Placement
// Computation of automatically-sized values follows the Default Sizing Algorithm defined for
// replaced elements in CSS layout [css-images-3]. In particular, when the referenced resource
diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/items.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/items.rs
index 4f3b7b853ba..6c004869859 100644
--- a/Libraries/LibWeb/Rust/src/painting/hit_test/items.rs
+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/items.rs
@@ -522,8 +522,8 @@ impl<'a, R: PaintRead, S: HitTestItemSink> HitTestItems<'a, R, S> {
}
/// The character cells of a text element, as one path, which it's hit-tested by rather than by its glyphs.
- /// FIXME: The cells of text on a path aren't laid along the path, so text on a path, and a <text> with a <textPath>
- /// in it, are still hit-tested by their glyphs.
+ /// FIXME: The cells of text on a path aren't laid along the path, so text on a path, a <text> with a <textPath> in
+ /// it, and a <tspan> in a <textPath> are still hit-tested by their glyphs.
fn svg_text_character_cells_path(&self, paintable: NodeSlotId) -> Option<libgfx_rust::path::Path> {
if self.rows.node_kind_if_live(paintable) != Some(NodeKind::SVGTextBox)
|| self
@@ -533,6 +533,14 @@ impl<'a, R: PaintRead, S: HitTestItemSink> HitTestItems<'a, R, S> {
{
return None;
}
+ let mut ancestor = self.rows.node_parent_if_live(paintable);
+ while let Some(element) = ancestor {
+ match self.rows.node_kind_if_live(element) {
+ Some(NodeKind::SVGTextPathBox) => return None,
+ Some(NodeKind::SVGTextBox) => ancestor = self.rows.node_parent_if_live(element),
+ _ => break,
+ }
+ }
// NB: Each element is hit-tested by its own characters, so that the elements inside it keep their own visibility and
// pointer-events.
let cells = paintable_geometry::committed_svg_text_own_character_cells(self.rows, paintable)?;
diff --git a/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rs b/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rs
index f36f02d5199..b97f228850a 100644
--- a/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rs
+++ b/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rs
@@ -159,7 +159,10 @@ pub(crate) fn committed_svg_text_segments(
arena: &impl GeometryRead,
slot: NodeSlotId,
) -> Option<std::sync::Arc<Vec<crate::layout::fragment_tree::SvgTextSegment>>> {
- if arena.node_kind_if_live(slot) != Some(crate::layout::node_data::NodeKind::SVGTextBox) {
+ if !matches!(
+ arena.node_kind_if_live(slot),
+ Some(crate::layout::node_data::NodeKind::SVGTextBox | crate::layout::node_data::NodeKind::SVGTextPathBox)
+ ) {
return None;
}
arena.with_committed_fragment_link(slot, |link| {
diff --git a/Libraries/LibWeb/Rust/src/painting/record/svg_resources.rs b/Libraries/LibWeb/Rust/src/painting/record/svg_resources.rs
index 67dd8a15986..2a5598943d3 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/svg_resources.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/svg_resources.rs
@@ -386,7 +386,10 @@ impl<O: Observer> PaintRecorder<'_, O> {
self.recorder
.set_ambient_inline_transform(Some(descendants_to_enclosing_space));
let mut child = crate::painting::paint_order::first_paint_child(self.source, svg_box);
- let mut next_text_part = if self.source.node_kind_if_live(svg_box) == Some(NodeKind::SVGTextBox) {
+ let mut next_text_part = if matches!(
+ self.source.node_kind_if_live(svg_box),
+ Some(NodeKind::SVGTextBox | NodeKind::SVGTextPathBox)
+ ) {
crate::painting::record::paint::svg::text_parts_before_children(self, svg_box)
} else {
None
diff --git a/Libraries/LibWeb/Rust/src/painting/record/traversal.rs b/Libraries/LibWeb/Rust/src/painting/record/traversal.rs
index cc6acc377b5..9c0e0e81f78 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/traversal.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/traversal.rs
@@ -515,8 +515,10 @@ impl<O: Observer> PaintRecorder<'_, O> {
// A text content element paints in document order: its text that follows a child element paints after that
// child.
// FIXME: Hit testing still tests all of the element's own text after its child elements.
- let is_text_box =
- self.source.node_kind_if_live(paintable) == Some(crate::layout::node_data::NodeKind::SVGTextBox);
+ let is_text_box = matches!(
+ self.source.node_kind_if_live(paintable),
+ Some(crate::layout::node_data::NodeKind::SVGTextBox | crate::layout::node_data::NodeKind::SVGTextPathBox)
+ );
let mut next_text_part = if is_text_box {
crate::painting::record::paint::svg::text_parts_before_children(self, paintable)
} else {
diff --git a/Tests/LibWeb/Layout/expected/tree-builder/svg-text-tspan-textpath.txt b/Tests/LibWeb/Layout/expected/tree-builder/svg-text-tspan-textpath.txt
index fc061b2f4f9..157677bbf8d 100644
--- a/Tests/LibWeb/Layout/expected/tree-builder/svg-text-tspan-textpath.txt
+++ b/Tests/LibWeb/Layout/expected/tree-builder/svg-text-tspan-textpath.txt
@@ -14,7 +14,7 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
SVGTextBox <text> at [10.921875,67.6875] [0+0+0 79.453125 0+0+0] [0+0+0 13.0625 0+0+0] children: not-inline
SVGTextPathBox <textPath> at [10.921875,67.6875] [0+0+0 79.453125 0+0+0] [0+0+0 13.0625 0+0+0] children: inline
TextNode <#text> (not painted)
- SVGTextBox <tspan> (not painted) children: inline
+ SVGTextBox <tspan> at [57.4375,68.015625] [0+0+0 32.9375 0+0+0] [0+0+0 12.515625 0+0+0] children: inline
TextNode <#text> (not painted)
TextNode <#text> (not painted)
diff --git a/Tests/LibWeb/Layout/expected/tree-builder/whitespace-in-svg-text-and-textpath.txt b/Tests/LibWeb/Layout/expected/tree-builder/whitespace-in-svg-text-and-textpath.txt
index 34469443e6b..9621f256449 100644
--- a/Tests/LibWeb/Layout/expected/tree-builder/whitespace-in-svg-text-and-textpath.txt
+++ b/Tests/LibWeb/Layout/expected/tree-builder/whitespace-in-svg-text-and-textpath.txt
@@ -16,7 +16,7 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
SVGTextBox <text> at [10.8125,72.125] [0+0+0 7 0+0+0] [0+0+0 7.28125 0+0+0] children: not-inline
SVGTextPathBox <textPath> at [10.8125,72.125] [0+0+0 7 0+0+0] [0+0+0 7.28125 0+0+0] children: inline
TextNode <#text> (not painted)
- SVGTextBox <tspan> (not painted) children: inline
+ SVGTextBox <tspan> at [10.8125,72.125] [0+0+0 7 0+0+0] [0+0+0 7.28125 0+0+0] children: inline
TextNode <#text> (not painted)
TextNode <#text> (not painted)
TextNode <#text> (not painted)
diff --git a/Tests/LibWeb/Ref/expected/svg-text-on-path-tspan-fill-ref.html b/Tests/LibWeb/Ref/expected/svg-text-on-path-tspan-fill-ref.html
new file mode 100644
index 00000000000..fca40c553cc
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/svg-text-on-path-tspan-fill-ref.html
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<style>
+ @font-face {
+ font-family: Ahem;
+ src: url("../data/fonts/Ahem.ttf");
+ }
+ body {
+ margin: 0;
+ }
+ svg {
+ font: 20px Ahem;
+ }
+</style>
+<svg width="400" height="150">
+ <text x="10" y="50" fill="black">AB<tspan fill="blue">CD<tspan fill="green">E</tspan>F</tspan>G</text>
+ <text x="10" y="100" fill="black">A<tspan fill="blue" dy="10">BC</tspan>D</text>
+ <text x="200" y="130" fill="red" stroke="red" stroke-width="8"><tspan fill="blue" stroke="blue">X</tspan>X</text>
+</svg>
diff --git a/Tests/LibWeb/Ref/input/svg-text-on-path-tspan-fill.html b/Tests/LibWeb/Ref/input/svg-text-on-path-tspan-fill.html
new file mode 100644
index 00000000000..7d275ecb67d
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/svg-text-on-path-tspan-fill.html
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/svg-text-on-path-tspan-fill-ref.html" />
+<style>
+ @font-face {
+ font-family: Ahem;
+ src: url("../data/fonts/Ahem.ttf");
+ }
+ body {
+ margin: 0;
+ }
+ svg {
+ font: 20px Ahem;
+ }
+</style>
+<svg width="400" height="150">
+ <defs>
+ <path id="line1" d="M 10 50 H 390" />
+ <path id="line2" d="M 10 100 H 390" />
+ <path id="line3" d="M 200 130 H 390" />
+ </defs>
+ <!-- Each <tspan> on the path paints its text in its own fill, a <tspan> inside another one too. -->
+ <text fill="black"><textPath href="#line1">AB<tspan fill="blue">CD<tspan fill="green">E</tspan>F</tspan>G</textPath></text>
+ <!-- A <tspan> moved off the path by its 'dy' still paints in its own fill. -->
+ <text fill="black"><textPath href="#line2">A<tspan fill="blue" dy="10">BC</tspan>D</textPath></text>
+ <!-- The text that follows a <tspan> paints after it, over the stroke of the <tspan>'s text. -->
+ <text fill="red" stroke="red" stroke-width="8"><textPath href="#line3"><tspan fill="blue" stroke="blue">X</tspan>X</textPath></text>
+ <!-- A <textPath> whose path doesn't resolve renders nothing, its <tspan>s included. -->
+ <text x="10" y="140" fill="red"><textPath href="#missing">A<tspan>BC</tspan></textPath></text>
+</svg>
diff --git a/Tests/LibWeb/Text/expected/SVG/text-hit-test-by-character-cells.txt b/Tests/LibWeb/Text/expected/SVG/text-hit-test-by-character-cells.txt
index 22c2ace02ca..9f7af3881cd 100644
--- a/Tests/LibWeb/Text/expected/SVG/text-hit-test-by-character-cells.txt
+++ b/Tests/LibWeb/Text/expected/SVG/text-hit-test-by-character-cells.txt
@@ -5,5 +5,7 @@ above the cells: svg
past the last cell: svg
over the text: text
over a hidden tspan in the text: svg
+on a glyph of a tspan on a path: tspan
+where the tspan would be off the path: svg
SVG link: text-decoration-line none, cursor pointer
HTML link: text-decoration-line underline, cursor pointer
diff --git a/Tests/LibWeb/Text/input/SVG/text-hit-test-by-character-cells.html b/Tests/LibWeb/Text/input/SVG/text-hit-test-by-character-cells.html
index 33c291e4601..6ee8bab18d4 100644
--- a/Tests/LibWeb/Text/input/SVG/text-hit-test-by-character-cells.html
+++ b/Tests/LibWeb/Text/input/SVG/text-hit-test-by-character-cells.html
@@ -15,6 +15,10 @@
<svg id="hidden-svg" width="300" height="100" style="font: 40px Ahem">
<text x="10" y="50">X<tspan visibility="hidden">XXX</tspan></text>
</svg>
+<svg id="on-path-svg" width="300" height="100" style="font: 40px Ahem">
+ <path id="path" d="M 100 80 H 300" />
+ <text><textPath href="#path"><tspan id="on-path">XX</tspan></textPath></text>
+</svg>
<p><a id="html-link" href="#target">HTML link</a></p>
<script>
asyncTest(async done => {
@@ -36,6 +40,11 @@
println(`over the text: ${hit(hiddenBox.left + 30, hiddenBox.top + 30)}`);
println(`over a hidden tspan in the text: ${hit(hiddenBox.left + 100, hiddenBox.top + 30)}`);
+ // A <tspan> on a path takes hits where its glyphs are drawn, not where it would be off the path.
+ const pathBox = document.getElementById("on-path-svg").getBoundingClientRect();
+ println(`on a glyph of a tspan on a path: ${hit(pathBox.left + 120, pathBox.top + 70)}`);
+ println(`where the tspan would be off the path: ${hit(pathBox.left + 20, pathBox.top + 2)}`);
+
// The link rules of the HTML rendering section are for HTML elements only.
const svgLink = getComputedStyle(document.getElementById("link"));
const htmlLink = getComputedStyle(document.getElementById("html-link"));