Skip to content

Truly independent web browser

LibWeb: Shape text on a path in the style of the element it’s in

sideshowbarker0a69ee7bd5a5
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index e9207174f04..ca61755ffca 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -1659,14 +1659,6 @@ impl<'pass> SvgFormattingContext<'pass> {
Some(std::sync::Arc::from(run))
}
- /// The character data text on a path renders: the text of every text node in its subtree that renders, rather
- /// than of its direct children alone.
- fn rendered_svg_text_contents(&self, root: Node) -> Vec<u16> {
- let mut text: Vec<u16> = Vec::new();
- self.for_each_svg_text_node(root, |node| text.extend_from_slice(&self.processed_svg_text(node)));
- text
- }
-
fn append_svg_source_text(&self, text_node: Node, out: &mut Vec<u16>) {
let content = self.run.pass.arena().text_content(text_node);
debug_assert!(content.is_some(), "SVG text child was not synced before layout");
@@ -2436,8 +2428,8 @@ impl<'pass> SvgFormattingContext<'pass> {
}
/// The glyph outlines a <textPath> renders: its whole subtree's text, shaped from the origin
- /// and then laid along the shape its `href` names. Also returns the glyph cell of each of its
- /// characters.
+ /// and then laid along the shape its `href` names. Each run of text is shaped in the style of
+ /// the element it's in. Also returns the glyph cell of each of its characters.
/// 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.
@@ -2448,9 +2440,25 @@ impl<'pass> SvgFormattingContext<'pass> {
};
let style = self.style(text_path_box);
- let text = self.rendered_svg_text_contents(text_path_box);
- // AD-HOC: Text on a path is laid out independently, so the 'textLength' of the <text> it's in doesn't adjust it.
- let shaped = self.shape_svg_text(style, &text, FfiFloatPoint::default(), 0.0);
+ let mut runs = Vec::new();
+ let mut character_cells = Vec::new();
+ let mut advance = 0.0f32;
+ self.for_each_svg_text_node(text_path_box, |node| {
+ let Some(text) = self.svg_text_run_starting_at(node) else {
+ return;
+ };
+ // AD-HOC: Text on a path is laid out independently, so the 'textLength' of the <text> it's in doesn't
+ // adjust it.
+ let shaped = self.shape_svg_text(
+ self.style(self.parent(node)),
+ &text,
+ FfiFloatPoint { x: advance, y: 0.0 },
+ 0.0,
+ );
+ advance += shaped.advance;
+ runs.extend(shaped.runs);
+ character_cells.extend(shaped.character_cells);
+ });
// https://svgwg.org/svg2-draft/text.html#TextPathElementStartOffsetAttribute
let mut start_offset = self
@@ -2460,15 +2468,12 @@ impl<'pass> SvgFormattingContext<'pass> {
// FIXME: Take writing mode and text direction into account.
match style.inherited_svg().text_anchor {
text_anchor::START => {}
- text_anchor::MIDDLE => start_offset -= shaped.advance / 2.0,
- text_anchor::END => start_offset -= shaped.advance,
+ text_anchor::MIDDLE => start_offset -= advance / 2.0,
+ text_anchor::END => start_offset -= advance,
_ => unreachable!("invalid text-anchor value"),
}
- (
- shape_path.place_glyph_runs_along(&shaped.runs, start_offset),
- shaped.character_cells,
- )
+ (shape_path.place_glyph_runs_along(&runs, start_offset), character_cells)
}
/// The geometry of the shape element an SVG reference names, read from what that element
diff --git a/Tests/LibWeb/Text/expected/SVG/text-on-a-path-in-the-styles-of-its-elements.txt b/Tests/LibWeb/Text/expected/SVG/text-on-a-path-in-the-styles-of-its-elements.txt
new file mode 100644
index 00000000000..7cd166f8401
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/text-on-a-path-in-the-styles-of-its-elements.txt
@@ -0,0 +1,4 @@
+text: length 120; "BC" is 80 long
+text-path: length 120; "BC" is 80 long
+text: box 10,28 120x40
+anchored at the end: box 90,28 120x40
diff --git a/Tests/LibWeb/Text/input/SVG/text-on-a-path-in-the-styles-of-its-elements.html b/Tests/LibWeb/Text/input/SVG/text-on-a-path-in-the-styles-of-its-elements.html
new file mode 100644
index 00000000000..4864deb90f1
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/text-on-a-path-in-the-styles-of-its-elements.html
@@ -0,0 +1,37 @@
+<!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">
+ <path id="path" d="M10 60H390" fill="none" />
+ <text id="text"><textPath id="text-path" href="#path">A<tspan font-size="40">BC</tspan>D</textPath></text>
+ <text id="anchored"><textPath href="#path" startOffset="200" text-anchor="end">A<tspan font-size="40">BC</tspan>D</textPath></text>
+</svg>
+<script>
+ const round = value => Math.round(value * 100) / 100;
+
+ asyncTest(async done => {
+ await document.fonts.load("20px Ahem");
+ // Each glyph of Ahem is as wide as its font size: 20px in the <textPath>, and 40px in the <tspan>.
+ for (const id of ["text", "text-path"]) {
+ const element = document.getElementById(id);
+ println(`${id}: length ${round(element.getComputedTextLength())}; "BC" is ${round(element.getSubStringLength(1, 2))} long`);
+ }
+ const box = document.getElementById("text").getBBox();
+ println(`text: box ${round(box.x)},${round(box.y)} ${round(box.width)}x${round(box.height)}`);
+ const anchoredBox = document.getElementById("anchored").getBBox();
+ println(`anchored at the end: box ${round(anchoredBox.x)},${round(anchoredBox.y)} ${round(anchoredBox.width)}x${round(anchoredBox.height)}`);
+ done();
+ });
+</script>