Truly independent web browser
LibGfx+LibWeb: Place text on a path by each glyph’s middle
sideshowbarker3295baa8e1a4
diff --git a/Libraries/LibGfx/Rust/src/path.rs b/Libraries/LibGfx/Rust/src/path.rsindex 2ce349645f8..89a092cff8f 100644--- a/Libraries/LibGfx/Rust/src/path.rs+++ b/Libraries/LibGfx/Rust/src/path.rs@@ -280,9 +280,11 @@ impl Path {Self::from_geometry(geometry)}- /// The outlines of the given glyphs, laid along the first subpath of this path that has length. Each glyph has- /// its origin at the distance of its x position plus the offset, and its baseline along the direction of the- /// path there. The glyphs stop at the first one whose middle would go past the end.+ /// The outlines of the given glyphs, laid along the first subpath of this path that has length, as SVG lays out+ /// text on a path: Each glyph is centered on the point at the distance of the middle of its advance (its x position+ /// plus the offset, plus half its advance), turned to the direction of the path there, and moved off the path, at+ /// a right angle to it, by as much as its baseline is shifted. A glyph whose middle is off either end isn't drawn.+ /// https://svgwg.org/svg2-draft/text.html#TextLayoutPathpub fn place_glyph_runs_along(&self, runs: &[GlyphRun], offset: f32) -> Path {let mut geometry = PathGeometry::new();let Some(contour) = measure::ContourMeasure::contours_of(&self.geometry).next() else {@@ -293,21 +295,35 @@ impl Path {if run.font.is_invisible() {continue;}+ let ascent = run.font.facts().ascent;+ let mut glyphs = Vec::with_capacity(run.glyphs.len());let mut transforms = Vec::with_capacity(run.glyphs.len());- for glyph in run.glyphs.placed() {- let glyph_distance = offset + glyph.x;- if glyph_distance + (glyph.glyph.advance / 2.0) > path_length {- break;+ for placed in run.glyphs.placed() {+ let half_advance = placed.glyph.advance / 2.0;+ let midpoint_distance = offset + placed.x + half_advance;+ // "Glyphs whose midpoint-on-the-path are off the path are not rendered."+ if !(0.0..=path_length).contains(&midpoint_distance) {+ continue;}- let (point, tangent) = contour.position_and_tangent(glyph_distance);+ // AD-HOC: The spec turns each glyph to the line through the start and the end of its advance on the+ // path. Like other engines, this turns it to the direction of the path at its midpoint.+ let (midpoint, tangent) = contour.position_and_tangent(midpoint_distance);+ // A glyph's y is the top of its em box, an ascent above its baseline.+ let baseline_shift = placed.y + ascent;+ let normal = (-tangent.y, tangent.x);transforms.push(AffineTransform {- values: [tangent.x, tangent.y, -tangent.y, tangent.x, point.x, point.y],+ values: [+ tangent.x,+ tangent.y,+ normal.0,+ normal.1,+ midpoint.x - tangent.x * half_advance + normal.0 * baseline_shift,+ midpoint.y - tangent.y * half_advance + normal.1 * baseline_shift,+ ],});+ glyphs.push(placed.glyph);}- append_glyph_outlines(&mut geometry, &run.font, &run.glyphs[..transforms.len()], &transforms);- if transforms.len() < run.glyphs.len() {- break;- }+ append_glyph_outlines(&mut geometry, &run.font, &glyphs, &transforms);}geometry.update_state_from_contours();Self::from_geometry(geometry)diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rsindex 994d985a913..af852fa5a37 100644--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs@@ -2614,19 +2614,63 @@ impl<'pass> SvgFormattingContext<'pass> {let mut runs = Vec::new();let mut character_cells = Vec::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+ // specified values for attribute 'dy' on a 'tspan' element.+ // NB: A 'dx' moves the text along the path, and a 'dy' moves it off the path. Each is the shift of the first+ // character of the element that has it, and stays for the characters after it.+ let mut shift_off_path = 0.0f32;+ // NB: A 'dx' on the first character moves the text along the path once it's anchored, so it's no part of the+ // extent the anchoring takes.+ let mut leading_dx = None;+ // The span the characters cover, which text-anchor places, as for ordinary text: A 'dx' can move a character+ // back past the ones before it.+ let (mut extent_start, mut extent_end) = (f32::INFINITY, f32::NEG_INFINITY);+ // OPTIMIZATION: Which text node is the first that the <textPath> and its <text> render is the same for every text+ // node, so it's found once.+ let first_on_path = self.first_rendered_svg_text_node(text_path_box);+ let first_of_text = self.first_rendered_svg_text_node(self.parent(text_path_box));self.for_each_svg_text_node(text_path_box, |node| {+ // NB: A text node right after another one is shaped as part of the run that one starts, so the run takes the+ // shift of its first character: that of the first of its text nodes that has text left once white space+ // is processed.let Some(text) = self.svg_text_run_starting_at(node) else {return;};+ let mut first_rendered = node;+ while self.processed_svg_text(first_rendered).is_empty() {+ let next = self.next_sibling(first_rendered);+ if next.is_invalid() || !node_facts::kind_is_text(self.node_kind(next)) {+ first_rendered = node;+ break;+ }+ first_rendered = next;+ }+ let (dx, dy) = self.svg_text_on_path_shift_at(first_rendered, text_path_box, first_on_path, first_of_text);+ advance += dx;+ shift_off_path += dy;+ if leading_dx.is_none() && !text.is_empty() {+ leading_dx = Some(advance);+ }+ let run_style = self.style(self.parent(node));// 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)),+ run_style,&text,- FfiFloatPoint { x: advance, y: 0.0 },+ FfiFloatPoint {+ x: advance,+ y: self.svg_dominant_baseline_offset(run_style) + shift_off_path,+ },0.0,);+ let run_start = advance;advance += shaped.advance;+ // NB: A run that white space processing left empty, such as a line break before a <tspan>, covers nothing.+ if !text.is_empty() {+ extent_start = extent_start.min(run_start).min(advance);+ extent_end = extent_end.max(run_start).max(advance);+ }runs.extend(shaped.runs);self.append_svg_run_dom_cells(node, shaped.character_cells, &mut character_cells);});@@ -2637,16 +2681,64 @@ impl<'pass> SvgFormattingContext<'pass> {.text_path_start_offset.resolve_relative_to(shape_path.length());// FIXME: Take writing mode and text direction into account.- match style.inherited_svg().text_anchor {- text_anchor::START => {}- text_anchor::MIDDLE => start_offset -= advance / 2.0,- text_anchor::END => start_offset -= advance,- _ => unreachable!("invalid text-anchor value"),+ if extent_start <= extent_end {+ let leading_dx = leading_dx.unwrap_or(0.0);+ match style.inherited_svg().text_anchor {+ text_anchor::START => start_offset -= extent_start - leading_dx,+ text_anchor::MIDDLE => start_offset -= (extent_start + extent_end) / 2.0 - leading_dx,+ text_anchor::END => start_offset -= extent_end - leading_dx,+ _ => unreachable!("invalid text-anchor value"),+ }}(shape_path.place_glyph_runs_along(&runs, start_offset), character_cells)}+ /// The 'dx' and 'dy' that shift the first character of a text node in a <textPath>: those of the innermost element+ /// that has each, of the elements whose first character it is. That takes in the <text>, where the <textPath>'s+ /// characters are the first it renders.+ fn svg_text_on_path_shift_at(+ &self,+ text_node: Node,+ text_path_box: Node,+ first_on_path: Option<Node>,+ first_of_text: Option<Node>,+ ) -> (f32, f32) {+ let (mut dx, mut dy) = (None, None);+ let mut element = self.parent(text_node);+ loop {+ let first = if element == text_path_box {+ first_on_path+ } else if element == self.parent(text_path_box) {+ first_of_text+ } else {+ self.first_rendered_svg_text_node(element)+ };+ if first != Some(text_node) {+ break;+ }+ if element == text_path_box {+ element = self.parent(element);+ continue;+ }+ if self.node_kind(element) != NodeKind::SVGTextBox {+ break;+ }+ let attributes = self.svg_attributes(element);+ if dx.is_none() && attributes.text_dx.kind != SVG_LENGTH_KIND_NONE {+ dx = Some(self.resolve_svg_text_length(element, attributes.text_dx, self.viewport_width));+ }+ if dy.is_none() && attributes.text_dy.kind != SVG_LENGTH_KIND_NONE {+ dy = Some(self.resolve_svg_text_length(element, attributes.text_dy, self.viewport_height));+ }+ if attributes.is_text_element {+ break;+ }+ element = self.parent(element);+ }+ (dx.unwrap_or(0.0), dy.unwrap_or(0.0))+ }+/// The geometry of the shape element an SVG reference names, read from what that element/// published rather than from a box: a `<textPath>`'s target is normally a `<path>` inside a/// `<defs>`, and a `<defs>` builds no box at all.diff --git a/Tests/LibWeb/Ref/expected/svg-text-on-path-glyph-placement-ref.html b/Tests/LibWeb/Ref/expected/svg-text-on-path-glyph-placement-ref.htmlnew file mode 100644index 00000000000..fa845de9d9d--- /dev/null+++ b/Tests/LibWeb/Ref/expected/svg-text-on-path-glyph-placement-ref.html@@ -0,0 +1,29 @@+<!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="500">+ <text x="10" y="60">AB</text>+ <text x="10" y="100" style="dominant-baseline: central">AB</text>+ <text x="15" y="150">BC</text>+ <text x="10" y="200">AB</text>+ <text x="-10" y="0" transform="translate(50 210) rotate(90)">CD</text>+ <text x="10" y="460">AB</text>+ <text x="100" y="320">AB</text>+ <text x="200" y="350">A</text>+ <text x="280" y="400">A</text>+ <text x="240" y="400">B</text>+ <text x="200" y="480">BA</text>+ <text x="270" y="30">X</text>+ <text x="100" y="430">X</text>+ <text x="140" y="260">X</text>+</svg>diff --git a/Tests/LibWeb/Ref/input/svg-text-on-path-glyph-placement.html b/Tests/LibWeb/Ref/input/svg-text-on-path-glyph-placement.htmlnew file mode 100644index 00000000000..4a997c7b33d--- /dev/null+++ b/Tests/LibWeb/Ref/input/svg-text-on-path-glyph-placement.html@@ -0,0 +1,54 @@+<!DOCTYPE html>+<link rel="match" href="../expected/svg-text-on-path-glyph-placement-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="500">+ <defs>+ <path id="line1" d="M 10 50 H 400" />+ <path id="line2" d="M 10 100 H 400" />+ <path id="line3" d="M 10 150 H 400" />+ <path id="corner" d="M 10 200 H 50 V 400" />+ <path id="line4" d="M 10 450 H 400" />+ <path id="line5" d="M 100 300 H 400" />+ <path id="line6" d="M 100 350 H 400" />+ <path id="line7" d="M 100 400 H 400" />+ <path id="line8" d="M 100 480 H 400" />+ <path id="line9" d="M 200 30 H 400" />+ <path id="line10" d="M 100 430 H 400" />+ <path id="line11" d="M 100 260 H 400" />+ </defs>+ <!-- A 'dy' on the <text> moves its text off the path. -->+ <text dy="10"><textPath href="#line1">AB</textPath></text>+ <!-- The dominant baseline is aligned to the path. -->+ <text><textPath href="#line2" style="dominant-baseline: central">AB</textPath></text>+ <!-- A glyph whose middle is before the start of the path isn't drawn. -->+ <text><textPath href="#line3" startOffset="-15">ABC</textPath></text>+ <!-- Each glyph is turned to the direction of the path at its middle. -->+ <text><textPath href="#corner">ABCD</textPath></text>+ <!-- A 'dy' on a <tspan> in the <textPath> moves the text from there on. -->+ <text><textPath href="#line4"><tspan dy="10">AB</tspan></textPath></text>+ <!-- Of a 'dy' on the <text> and one on a <tspan> that starts with the same character, only the inner one counts. -->+ <text dy="10"><textPath href="#line5"><tspan dy="20">AB</tspan></textPath></text>+ <!-- The 'dx' of the first character moves the text along the path once it's anchored. -->+ <text dx="20" text-anchor="end"><textPath href="#line6" startOffset="100">A</textPath></text>+ <!-- text-anchor places the span the characters cover, which a 'dx' can extend back past the first one. -->+ <text text-anchor="end"><textPath href="#line7" startOffset="200">A<tspan dx="-60">B</tspan></textPath></text>+ <text><textPath href="#line8" startOffset="100">A<tspan dx="-40">B</tspan></textPath></text>+ <!-- Text that white space processing leaves empty, like this line break, covers nothing that text-anchor places. -->+ <text><textPath href="#line9" startOffset="50">+<tspan dx="20">X</tspan></textPath></text>+ <!-- A comment leaves two text nodes side by side, which are laid out as one: The space is collapsed away. -->+ <text><textPath href="#line10"> <!---->X</textPath></text>+ <!-- The character after such a collapsed space is the first the <text> renders, so it takes the <text>'s 'dx'. -->+ <text dx="40"><textPath href="#line11"> <!---->X</textPath></text>+</svg>diff --git a/Tests/LibWeb/Screenshot/expected/svg-textPath.png b/Tests/LibWeb/Screenshot/expected/svg-textPath.pngindex 1f76d8dedec..e7dbfc2805d 100644Binary files a/Tests/LibWeb/Screenshot/expected/svg-textPath.png and b/Tests/LibWeb/Screenshot/expected/svg-textPath.png differ