Skip to content

Truly independent web browser

LibWeb: Keep the stroke outline of an SVG path with the committed path

Aliaksandr Kalenik107ab9b5ba62
diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
index 300f59acf0b..fa55dad714c 100644
--- a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
@@ -128,10 +128,67 @@ pub(crate) struct GridPayload {
pub(crate) struct SvgPayload {
pub(crate) facts: CommittedSvgFacts,
pub(crate) geometry: svg_formatting_context::SvgGeometry,
- pub(crate) path: Option<std::sync::Arc<libgfx_rust::path::Path>>,
+ pub(crate) path: Option<std::sync::Arc<CommittedSvgPath>>,
pub(crate) text_character_cells: Option<std::sync::Arc<Vec<svg_formatting_context::FfiSvgTextCharacterCell>>>,
}
+pub(crate) struct CommittedSvgPath {
+ pub(crate) path: libgfx_rust::path::Path,
+ latest_stroke_outline: std::sync::Mutex<Option<StrokeOutline>>,
+}
+
+struct StrokeOutline {
+ style_it_was_built_for: libgfx_rust::path::StrokeStyle,
+ outline: libgfx_rust::path::Path,
+}
+
+impl CommittedSvgPath {
+ pub(crate) fn new(path: libgfx_rust::path::Path) -> Self {
+ Self {
+ path,
+ latest_stroke_outline: std::sync::Mutex::new(None),
+ }
+ }
+
+ pub(crate) fn stroke_with_style_contains(&self, style: &libgfx_rust::path::StrokeStyle, x: f32, y: f32) -> bool {
+ let mut latest_stroke_outline = self
+ .latest_stroke_outline
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ let stroke_outline = match latest_stroke_outline.take() {
+ Some(stroke_outline) if stroke_outline.style_it_was_built_for == *style => stroke_outline,
+ _ => StrokeOutline {
+ style_it_was_built_for: style.clone(),
+ outline: self.path.stroke_to_fill(style),
+ },
+ };
+ latest_stroke_outline
+ .insert(stroke_outline)
+ .outline
+ .contains(x, y, libgfx_rust::WindingRule::Nonzero)
+ }
+}
+
+impl std::ops::Deref for CommittedSvgPath {
+ type Target = libgfx_rust::path::Path;
+
+ fn deref(&self) -> &libgfx_rust::path::Path {
+ &self.path
+ }
+}
+
+impl PartialEq for CommittedSvgPath {
+ fn eq(&self, other: &Self) -> bool {
+ self.path == other.path
+ }
+}
+
+impl std::fmt::Debug for CommittedSvgPath {
+ fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ self.path.fmt(formatter)
+ }
+}
+
/// Compares payloads as painting needs: the layout data painting reads through its allocation as the same allocations,
/// and the facts it copies as equal values.
impl PartialEq for FcPayload {
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index f9652efc698..350e378b559 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -2358,7 +2358,7 @@ impl<'pass> SvgFormattingContext<'pass> {
}
self.update_svg_payload_of(graphics_box, |svg| {
- svg.path = Some(std::sync::Arc::new(path));
+ svg.path = Some(std::sync::Arc::new(fragment_tree::CommittedSvgPath::new(path)));
svg.text_character_cells = character_cells.map(std::sync::Arc::new);
});
let object_bounding_box = if is_text_content_box {
diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/items.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/items.rs
index c35a27d47fc..c55dacda0e8 100644
--- a/Libraries/LibWeb/Rust/src/painting/hit_test/items.rs
+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/items.rs
@@ -494,7 +494,7 @@ impl<'a, R: PaintRead, S: HitTestItemSink> HitTestItems<'a, R, S> {
self.sink.append(item);
}
- fn svg_stroke_hit_region(&self, paintable: NodeSlotId) -> Option<std::sync::Arc<SvgStrokeHitRegion>> {
+ fn svg_stroke_hit_region(&self, paintable: NodeSlotId) -> Option<SvgStrokeHitRegion> {
let style = self.rows.node_style_if_live(paintable)?;
let stroke = crate::painting::record::paint::svg::svg_stroke_style(self.rows, paintable, &style);
let mut thickness = stroke.width;
@@ -516,16 +516,14 @@ impl<'a, R: PaintRead, S: HitTestItemSink> HitTestItems<'a, R, S> {
if thickness.is_nan() || thickness <= 0.0 {
return None;
}
- Some(std::sync::Arc::new(SvgStrokeHitRegion::new(
- libgfx_rust::path::StrokeStyle {
- thickness,
- cap_style: stroke.cap_style,
- join_style: stroke.join_style,
- miter_limit: stroke.miter_limit,
- dash_array,
- dash_offset,
- },
- )))
+ Some(SvgStrokeHitRegion::new(libgfx_rust::path::StrokeStyle {
+ thickness,
+ cap_style: stroke.cap_style,
+ join_style: stroke.join_style,
+ miter_limit: stroke.miter_limit,
+ dash_array,
+ dash_offset,
+ }))
}
fn empty_editable_item(&mut self, paintable: NodeSlotId) {
diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/mod.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/mod.rs
index 46e0bd9f9d6..71f555a798d 100644
--- a/Libraries/LibWeb/Rust/src/painting/hit_test/mod.rs
+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/mod.rs
@@ -45,12 +45,12 @@ pub struct HitTestItem {
pub rect: CssPixelRect,
pub context: ContextRef,
pub border_radii: BorderRadii,
- pub path: Option<Arc<libgfx_rust::path::Path>>,
+ pub path: Option<Arc<crate::layout::fragment_tree::CommittedSvgPath>>,
pub winding_rule: libgfx_rust::WindingRule,
/// For an SVG path, the `svg_hit_area` flags naming which of its areas pointer events can target.
pub svg_hit_areas: u8,
/// For an SVG path whose stroke pointer events can target, the shape of that stroke.
- pub svg_stroke: Option<Arc<SvgStrokeHitRegion>>,
+ pub svg_stroke: Option<SvgStrokeHitRegion>,
}
/// The areas of an SVG graphics element that pointer events can target.
@@ -61,19 +61,15 @@ pub mod svg_hit_area {
}
/// The stroke of an SVG path as hit testing reads it. The outline of the stroke is only built when a point first
-/// reaches it, and the copies of an item that later recordings reuse share it.
-#[derive(Debug)]
+/// reaches it, and the committed path keeps it for as long as the stroke keeps its style.
+#[derive(Clone, Debug, PartialEq)]
pub struct SvgStrokeHitRegion {
style: libgfx_rust::path::StrokeStyle,
- outline: std::sync::OnceLock<libgfx_rust::path::Path>,
}
impl SvgStrokeHitRegion {
pub fn new(style: libgfx_rust::path::StrokeStyle) -> Self {
- Self {
- style,
- outline: std::sync::OnceLock::new(),
- }
+ Self { style }
}
/// How far past the path its stroke can reach.
@@ -88,16 +84,8 @@ impl SvgStrokeHitRegion {
self.style.thickness / 2.0 * factor
}
- pub fn contains(&self, path: &libgfx_rust::path::Path, x: f32, y: f32) -> bool {
- self.outline
- .get_or_init(|| path.stroke_to_fill(&self.style))
- .contains(x, y, libgfx_rust::WindingRule::Nonzero)
- }
-}
-
-impl PartialEq for SvgStrokeHitRegion {
- fn eq(&self, other: &Self) -> bool {
- self.style == other.style
+ pub fn contains(&self, path: &crate::layout::fragment_tree::CommittedSvgPath, x: f32, y: f32) -> bool {
+ path.stroke_with_style_contains(&self.style, x, y)
}
}
diff --git a/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rs b/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rs
index 93cb7ddb65e..85f9386d121 100644
--- a/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rs
+++ b/Libraries/LibWeb/Rust/src/painting/paintable_geometry.rs
@@ -146,7 +146,7 @@ pub(crate) fn committed_collapsed_table_borders(
pub(crate) fn committed_svg_path(
arena: &impl GeometryRead,
slot: NodeSlotId,
-) -> Option<std::sync::Arc<libgfx_rust::path::Path>> {
+) -> Option<std::sync::Arc<crate::layout::fragment_tree::CommittedSvgPath>> {
if !arena.node_kind_if_live(slot).is_some_and(node_painting::is_svg_path) {
return None;
}
diff --git a/Tests/LibWeb/Text/expected/hit_testing/queries-repeated-between-changes.txt b/Tests/LibWeb/Text/expected/hit_testing/queries-repeated-between-changes.txt
new file mode 100644
index 00000000000..c73eda09fbf
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/hit_testing/queries-repeated-between-changes.txt
@@ -0,0 +1,34 @@
+a cell: cell-2-2 [cell-2-2 grid html]
+a cell across two cells of 128px: cell-6-6 [cell-6-6 grid html]
+a cell past 128px: cell-7-7 [cell-7-7 grid html]
+between cells: grid [grid html]
+a cell that takes no pointer events: grid [grid html]
+the cell taking pointer events again: cell-2-2 [cell-2-2 grid html]
+a hidden cell: grid [grid html]
+where a moved cell was: grid [grid html]
+where the moved cell is: cell-2-2 [cell-2-2 html]
+where a translated cell was: grid [grid html]
+where the translated cell is: cell-2-2 [cell-2-2 html]
+where a removed cell was: grid [grid html]
+a cell added late: late [late grid html]
+the corner of a cell that became round: grid [grid html]
+the middle of the round cell: late [late grid html]
+the corner of the round cell with a background: grid [grid html]
+the middle of the round cell with a background: late [late grid html]
+two cells that overlap: cell-5-6 [cell-5-6 cell-5-5 grid html]
+the earlier cell raised above the later one: cell-5-5 [cell-5-5 cell-5-6 grid html]
+a box in a rotated box: rotated-0 [rotated-0 rotated html]
+another box in the rotated box: rotated-37 [rotated-37 rotated html]
+the same point once that box takes no pointer events: rotated [rotated html]
+a box in a scroller: scrolled-0 [scrolled-0 scroller html]
+the same point after scrolling: scrolled-20 [scrolled-20 scroller html]
+below a box that grew: scrolled-21 [scrolled-21 scroller html]
+the stroke of a circle: circle-0 [circle-0 shapes html]
+inside the circle: shapes [shapes html]
+inside the circle after its stroke grew: circle-0 [circle-0 shapes html]
+where the circle had a stroke: shapes [shapes html]
+inside a circle that takes all pointer events: circle-11 [circle-11 shapes html]
+text in a span: span [span text html]
+the text in another color: span [span text html]
+the text once the span takes no pointer events: text [text html]
+the text once the span is hidden: text [text html]
diff --git a/Tests/LibWeb/Text/input/hit_testing/queries-repeated-between-changes.html b/Tests/LibWeb/Text/input/hit_testing/queries-repeated-between-changes.html
new file mode 100644
index 00000000000..73524928d44
--- /dev/null
+++ b/Tests/LibWeb/Text/input/hit_testing/queries-repeated-between-changes.html
@@ -0,0 +1,148 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body { margin: 0; }
+ #grid { position: absolute; left: 0; top: 0; width: 400px; height: 300px; }
+ #grid > div { position: absolute; width: 18px; height: 18px; }
+ #rotated { position: absolute; left: 420px; top: 0; width: 200px; height: 200px; transform: rotate(180deg); }
+ #rotated > div { float: left; width: 20px; height: 20px; }
+ #scroller { position: absolute; left: 420px; top: 220px; width: 200px; height: 100px; overflow: auto; }
+ #scroller > div { height: 10px; }
+ #shapes { position: absolute; left: 0; top: 320px; }
+ #text { position: absolute; left: 420px; top: 340px; margin: 0; font: 20px/20px monospace; }
+</style>
+<div id="grid"></div>
+<div id="rotated"></div>
+<div id="scroller"></div>
+<svg id="shapes" width="400" height="200"></svg>
+<p id="text">plain <span data-name="span">span</span> plain</p>
+<script>
+ function describe(element) {
+ return element ? element.id || element.dataset.name || element.localName : "null";
+ }
+ const answersByLabel = [];
+ function probe(label, x, y) {
+ const answers = [];
+ for (let repetition = 0; repetition < 3; ++repetition) {
+ const all = document.elementsFromPoint(x, y).map(describe).join(" ");
+ answers.push(`${describe(document.elementFromPoint(x, y))} [${all}]`);
+ }
+ const agree = answers.every(answer => answer === answers[0]);
+ answersByLabel.push(`${label}: ${agree ? answers[0] : `repeated queries disagree: ${answers.join(" | ")}`}`);
+ }
+ function pointInside(element) {
+ const rect = element.getBoundingClientRect();
+ return [rect.left + 5, rect.top + 5];
+ }
+ function named(name) {
+ return document.querySelector(`[data-name="${name}"]`);
+ }
+ function appendNamed(parent, name, localName = "div") {
+ const element = document.createElement(localName);
+ element.dataset.name = name;
+ parent.appendChild(element);
+ return element;
+ }
+ test(() => {
+ const grid = document.getElementById("grid");
+ for (let row = 0; row < 15; ++row) {
+ for (let column = 0; column < 20; ++column) {
+ const cell = appendNamed(grid, `cell-${row}-${column}`);
+ cell.style.left = `${column * 20}px`;
+ cell.style.top = `${row * 20}px`;
+ }
+ }
+ const rotated = document.getElementById("rotated");
+ for (let index = 0; index < 100; ++index)
+ appendNamed(rotated, `rotated-${index}`);
+ const scroller = document.getElementById("scroller");
+ for (let index = 0; index < 60; ++index)
+ appendNamed(scroller, `scrolled-${index}`);
+ const shapes = document.getElementById("shapes");
+ for (let index = 0; index < 40; ++index) {
+ const circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
+ circle.dataset.name = `circle-${index}`;
+ circle.setAttribute("cx", 20 + (index % 10) * 40);
+ circle.setAttribute("cy", 20 + Math.floor(index / 10) * 40);
+ circle.setAttribute("r", 10);
+ circle.setAttribute("fill", "none");
+ circle.setAttribute("stroke", "black");
+ circle.setAttribute("stroke-width", 8);
+ shapes.appendChild(circle);
+ }
+
+ probe("a cell", 50, 50);
+ probe("a cell across two cells of 128px", 127, 127);
+ probe("a cell past 128px", 141, 141);
+ probe("between cells", 119, 50);
+
+ const cell = named("cell-2-2");
+ cell.style.pointerEvents = "none";
+ probe("a cell that takes no pointer events", 50, 50);
+ cell.style.pointerEvents = "";
+ probe("the cell taking pointer events again", 50, 50);
+ cell.style.visibility = "hidden";
+ probe("a hidden cell", 50, 50);
+ cell.style.visibility = "";
+ cell.style.left = "401px";
+ probe("where a moved cell was", 50, 50);
+ probe("where the moved cell is", 410, 50);
+ cell.style.left = "40px";
+ cell.style.transform = "translate(361px, 20px)";
+ probe("where a translated cell was", 50, 50);
+ probe("where the translated cell is", 410, 70);
+ cell.remove();
+ probe("where a removed cell was", 50, 50);
+ const late = appendNamed(grid, "late");
+ late.style.left = "40px";
+ late.style.top = "40px";
+ probe("a cell added late", 50, 50);
+ late.style.borderRadius = "9px";
+ probe("the corner of a cell that became round", 41, 41);
+ probe("the middle of the round cell", 49, 49);
+ late.style.background = "green";
+ probe("the corner of the round cell with a background", 41, 41);
+ probe("the middle of the round cell with a background", 49, 49);
+
+ const lower = named("cell-5-5");
+ lower.style.left = "110px";
+ probe("two cells that overlap", 124, 105);
+ lower.style.zIndex = "1";
+ probe("the earlier cell raised above the later one", 124, 105);
+
+ probe("a box in a rotated box", ...pointInside(named("rotated-0")));
+ const pointInsideAnotherRotatedBox = pointInside(named("rotated-37"));
+ probe("another box in the rotated box", ...pointInsideAnotherRotatedBox);
+ named("rotated-37").style.pointerEvents = "none";
+ probe("the same point once that box takes no pointer events", ...pointInsideAnotherRotatedBox);
+
+ probe("a box in a scroller", 430, 225);
+ scroller.scrollTop = 200;
+ probe("the same point after scrolling", 430, 225);
+ named("scrolled-20").style.height = "40px";
+ probe("below a box that grew", 430, 265);
+ scroller.scrollTop = 0;
+
+ probe("the stroke of a circle", 30, 340);
+ probe("inside the circle", 20, 340);
+ named("circle-0").setAttribute("stroke-width", 30);
+ probe("inside the circle after its stroke grew", 20, 340);
+ named("circle-0").setAttribute("stroke", "none");
+ probe("where the circle had a stroke", 30, 340);
+ named("circle-11").style.pointerEvents = "all";
+ probe("inside a circle that takes all pointer events", 60, 380);
+
+ const span = named("span");
+ const pointInsideSpan = pointInside(span);
+ probe("text in a span", ...pointInsideSpan);
+ span.style.color = "green";
+ probe("the text in another color", ...pointInsideSpan);
+ span.style.pointerEvents = "none";
+ probe("the text once the span takes no pointer events", ...pointInsideSpan);
+ span.style.pointerEvents = "";
+ span.style.visibility = "hidden";
+ probe("the text once the span is hidden", ...pointInsideSpan);
+
+ answersByLabel.forEach(println);
+ });
+</script>