Truly independent web browser
LibWeb: Hit-test SVG text by its character cells
sideshowbarker53938da46772
diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rsindex 1130556d857..2c1ed050c31 100644--- a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs+++ b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs@@ -161,6 +161,9 @@ pub(crate) struct SvgPayload {/// after that child. None where all of the element's text comes before its child elements.pub(crate) text_segments: Option<std::sync::Arc<Vec<SvgTextSegment>>>,pub(crate) text_character_cells: Option<std::sync::Arc<Vec<svg_formatting_context::FfiSvgTextCharacterCell>>>,+ /// The cells of the characters of a <text> or <tspan>'s own text nodes, without those of the elements inside it,+ /// which it's hit-tested by.+ pub(crate) text_own_character_cells: Option<std::sync::Arc<Vec<svg_formatting_context::FfiSvgTextCharacterCell>>>,}pub(crate) struct CommittedSvgPath {@@ -240,6 +243,10 @@ impl PartialEq for FcPayload {&& same_allocation(left.path.as_ref(), right.path.as_ref())&& same_allocation(left.text_segments.as_ref(), right.text_segments.as_ref())&& same_allocation(left.text_character_cells.as_ref(), right.text_character_cells.as_ref())+ && same_allocation(+ left.text_own_character_cells.as_ref(),+ right.text_own_character_cells.as_ref(),+ )}_ => false,}@@ -263,6 +270,7 @@ impl FcPayload {&& left.path == right.path&& left.text_segments == right.text_segments&& left.text_character_cells == right.text_character_cells+ && left.text_own_character_cells == right.text_own_character_cells}_ => false,}@@ -277,6 +285,7 @@ impl FcPayload {&& svg.path.is_none()&& svg.text_segments.is_none()&& svg.text_character_cells.is_none()+ && svg.text_own_character_cells.is_none()}Self::Table(_) | Self::Flex(_) | Self::Multicol(_) | Self::Math(_) => false,}diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rsindex af852fa5a37..bfb598d2914 100644--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs@@ -612,6 +612,8 @@ struct SvgTextBoxContents {glyph_cells: SvgCssPixelRect,/// The glyph cell of each character in the element's subtree, in document order.character_cells: Vec<FfiSvgTextCharacterCell>,+ /// The glyph cell of each character of the element's own text nodes, in document order.+ own_character_cells: Vec<FfiSvgTextCharacterCell>,/// The union of the boxes of the element's text content child elements.children_bounding_box: SvgCssPixelRect,}@@ -2424,7 +2426,11 @@ impl<'pass> SvgFormattingContext<'pass> {paint_parts.add_decoration_lines(after_child, lines_over);contents.glyph_cells.unite(shaped.glyph_cells);+ let first_own_cell = contents.character_cells.len();self.append_svg_run_dom_cells(child, shaped.character_cells, &mut contents.character_cells);+ contents+ .own_character_cells+ .extend_from_slice(&contents.character_cells[first_own_cell..]);} else if matches!(kind, NodeKind::SVGTextBox | NodeKind::SVGTextPathBox) {// <text> and <tspan> elements can contain more text elements, whose glyph cells count toward this// box too.@@ -3324,12 +3330,14 @@ impl<'pass> SvgFormattingContext<'pass> {let kind = self.node_kind(graphics_box);let mut text_segments = None;let mut text_children_bounding_box = SvgCssPixelRect::default();+ 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 => {let contents = self.layout_svg_text_box_contents(run, graphics_box);text_segments = contents.segments;text_children_bounding_box = contents.children_bounding_box;+ own_character_cells = Some(std::sync::Arc::new(contents.own_character_cells));(contents.path,Some(contents.glyph_cells),@@ -3370,6 +3378,7 @@ impl<'pass> SvgFormattingContext<'pass> {svg.path = Some(std::sync::Arc::new(fragment_tree::CommittedSvgPath::new(path)));svg.text_segments = text_segments.map(std::sync::Arc::new);svg.text_character_cells = character_cells;+ svg.text_own_character_cells = own_character_cells;});let object_bounding_box = if is_text_content_box {bounding_box.to_float_rect()diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/items.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/items.rsindex 8be1742a1a1..4f3b7b853ba 100644--- a/Libraries/LibWeb/Rust/src/painting/hit_test/items.rs+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/items.rs@@ -22,7 +22,7 @@ use crate::painting::paintable_geometry;use crate::painting::record::PaintPhase;use crate::painting::style_queries;use crate::painting::text_fragment;-use libgfx_rust::WindingRule;+use libgfx_rust::{FloatPoint, WindingRule};/// A fragment of a row's inline content, read through whatever holds the row's committed side/// data.@@ -451,13 +451,23 @@ impl<'a, R: PaintRead, S: HitTestItemSink> HitTestItems<'a, R, S> {if phase != PaintPhase::Foreground {return;}- let Some(path) = paintable_geometry::committed_svg_path(self.rows, paintable) else {+ let Some(mut path) = paintable_geometry::committed_svg_path(self.rows, paintable) else {return;};- if !self.sink.hit_test_facts(paintable).visible_for_hit_testing {+ // OPTIMIZATION: The facts are computed afresh from the box's style each time they're asked for, so they're asked+ // for once.+ let facts = self.sink.hit_test_facts(paintable);+ if !facts.visible_for_hit_testing {return;}- let facts = self.sink.hit_test_facts(paintable);+ // https://svgwg.org/svg2-draft/interact.html#PointerEventsProperty+ // For text elements, hit-testing is performed on a character cell basis+ let mut winding_rule = facts.svg_path_winding_rule;+ if let Some(cells_path) = self.svg_text_character_cells_path(paintable) {+ path = std::sync::Arc::new(crate::layout::fragment_tree::CommittedSvgPath::new(cells_path));+ // NB: Every point of a cell is in it, even where cells overlap, whatever the text's fill-rule.+ winding_rule = WindingRule::Nonzero;+ }if facts.svg_hit_areas == 0 {return;}@@ -503,7 +513,7 @@ impl<'a, R: PaintRead, S: HitTestItemSink> HitTestItems<'a, R, S> {let item = HitTestItem {rect: bounding_box,path: Some(path),- winding_rule: facts.svg_path_winding_rule,+ winding_rule,svg_hit_areas: facts.svg_hit_areas,svg_stroke: stroke,..self.base_hit_test_item(HitTestItemKind::SvgPath, paintable, context)@@ -511,6 +521,37 @@ impl<'a, R: PaintRead, S: HitTestItemSink> HitTestItems<'a, R, S> {self.sink.append(item);}+ /// 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.+ 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+ .rows+ .children(paintable)+ .any(|child| self.rows.node_kind_if_live(child) == Some(NodeKind::SVGTextPathBox))+ {+ return None;+ }+ // 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)?;+ let mut path = libgfx_rust::path::Path::new();+ for cell in cells.iter().filter(|cell| cell.starts_typographic_character) {+ if cell.width <= 0.0 || cell.height <= 0.0 {+ continue;+ }+ let (left, top) = (cell.x, cell.y);+ let (right, bottom) = (cell.x + cell.width, cell.y + cell.height);+ path.move_to(FloatPoint { x: left, y: top });+ path.line_to(FloatPoint { x: right, y: top });+ path.line_to(FloatPoint { x: right, y: bottom });+ path.line_to(FloatPoint { x: left, y: bottom });+ path.close();+ }+ (!path.is_empty()).then_some(path)+ }+fn svg_box_items(&mut self, paintable: NodeSlotId, phase: PaintPhase) {if phase != PaintPhase::Foreground || !self.sink.hit_test_facts(paintable).visible_for_hit_testing {return;diff --git a/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rs b/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rsindex c670594a964..f36f02d5199 100644--- a/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rs+++ b/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rs@@ -184,6 +184,20 @@ pub(crate) fn committed_svg_text_character_cells(})}+/// The cells of the characters of a <text> or <tspan>'s own text nodes, as its box committed them; None for any other box,+/// and for a text box laid along a path or not committed.+pub(crate) fn committed_svg_text_own_character_cells(+ arena: &impl GeometryRead,+ slot: NodeSlotId,+) -> Option<std::sync::Arc<Vec<svg_formatting_context::FfiSvgTextCharacterCell>>> {+ if arena.node_kind_if_live(slot) != Some(crate::layout::node_data::NodeKind::SVGTextBox) {+ return None;+ }+ arena.with_committed_fragment_link(slot, |link| {+ link.and_then(|link| link.fragment.svg()?.text_own_character_cells.clone())+ })+}+pub(crate) fn committed_svg_viewport_transform(arena: &impl GeometryRead,slot: NodeSlotId,diff --git a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-lane-drag-selection-matches-the-host.txt b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-lane-drag-selection-matches-the-host.txtindex 767b005eef7..3befeca9fb2 100644--- a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-lane-drag-selection-matches-the-host.txt+++ b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-lane-drag-selection-matches-the-host.txt@@ -112,7 +112,7 @@ same: ""same: ""same: ""same: ""-same: "<BODY> | <svg> | <text> | <P> | text of a vector | the very"+same: "<text> | <P> | text of a vector | the very"same: "<P> | <B> | <I> | <U> | <P> | <P> | <SPAN> | <P> | <P> | <P> | <SPAN> | <P> | <SPAN> | <P> | <P> | <IMG> | <P> | <P> | <TABLE> | <TBODY> | <TR> | <TD> | <TD> | <P> | <DIV> | <P> | <P> | <P> | <P> | <P> | <SPAN> | <P> | <DIV> | <P> | <P> | <P> | <SPAN> | <P> | <A> | <IMG> | <P> (rgb(255, 0, 0) on rgb(255, 255, 0)) | <P> | <A> | <A> | <IFRAME> | <svg> | <text> | <P> | first line of plain text | some | bold | and | italic | underlined | text | text | around | comments | words | in a block | before | after | kept | kept again | around | all of this | the element | outside | kept in here | again outside | outer | inside contents | outer again | words | more words | light text | left cell | right cell | שלום עולם | scrolled line one | scrolled line two | scrolled line three | scrolled line four | floated text | text around the float | text moved by a transform | placed out of the flow | text after generated content | one line | another line | words | in a block | more words | words | around | boxes | end | text with a selection style (rgb(255, 0, 0) on rgb(255, 255, 0)) | before | the link text | after | a link to drag | text of a vector | the"same: " very last line"same: "<P> | <SPAN> | <P> | <P> | <P> | <SPAN> | <P> | <SPAN> | <P> | <P> | <IMG> | <P> | <P> | <TABLE> | <TBODY> | <TR> | <TD> | <TD> | <P> | <DIV> | <P> | <P> | <P> | <P> | <P> | <SPAN> | <P> | <DIV> | <P> | <P> | <P> | <SPAN> | <P> | <A> | <IMG> | <P> (rgb(255, 0, 0) on rgb(255, 255, 0)) | <P> | <A> | <A> | <IFRAME> | <svg> | <text> | <P> | text | around | comments | words | in a block | before | after | kept | kept again | around | all of this | the element | outside | kept in here | again outside | outer | inside contents | outer again | words | more words | light text | left cell | right cell | שלום עולם | scrolled line one | scrolled line two | scrolled line three | scrolled line four | floated text | text around the float | text moved by a transform | placed out of the flow | text after generated content | one line | another line | words | in a block | more words | words | around | boxes | end | text with a selection style (rgb(255, 0, 0) on rgb(255, 255, 0)) | before | the link text | after | a link to drag | text of a vector | the"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.txtnew file mode 100644index 00000000000..ba1eb41f910--- /dev/null+++ b/Tests/LibWeb/Text/expected/SVG/text-hit-test-by-character-cells.txt@@ -0,0 +1,7 @@+middle of the X: text+blank top of the p's cell: text+between two cells: text+above the cells: svg+past the last cell: svg+over the text: text+over a hidden tspan in the text: svgdiff --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.htmlnew file mode 100644index 00000000000..da072c62567--- /dev/null+++ b/Tests/LibWeb/Text/input/SVG/text-hit-test-by-character-cells.html@@ -0,0 +1,39 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ @font-face {+ font-family: Ahem;+ src: url("../../../Ref/data/fonts/Ahem.ttf");+ }+ body {+ margin: 0;+ }+</style>+<svg width="300" height="100" style="font: 40px Ahem">+ <a id="link" href="#target"><text id="text" x="10" y="50">XÉp</text></a>+</svg>+<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>+<script>+ asyncTest(async done => {+ await document.fonts.ready;+ // Ahem's X fills its whole cell, but É and p leave parts of their cells blank: above the É's accent and below+ // the p's descender. Those blank parts of the cells still hit the text.+ const text = document.getElementById("text");+ const cells = text.getBoundingClientRect();+ const cell = 40;+ const hit = (x, y) => document.elementFromPoint(x, y)?.localName;+ println(`middle of the X: ${hit(cells.left + cell / 2, cells.top + cell / 2)}`);+ println(`blank top of the p's cell: ${hit(cells.left + cell * 2.75, cells.top + 2)}`);+ println(`between two cells: ${hit(cells.left + cell, cells.top + cell / 2)}`);+ println(`above the cells: ${hit(cells.left + cell / 2, cells.top - 5)}`);+ println(`past the last cell: ${hit(cells.left + cell * 3 + 5, cells.top + cell / 2)}`);++ // An element takes hits in the cells of its own characters, not in those of a hidden element inside it.+ const hiddenBox = document.getElementById("hidden-svg").getBoundingClientRect();+ 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)}`);+ done();+ });+</script>