Skip to content

Truly independent web browser

LibWeb: Implement CanvasDrawPath.isPointInStroke()

Andreas Kling6e7d900efb33
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_draw_path.rs b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_draw_path.rs
index f5937595e6c..631a2fee8e0 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_draw_path.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_draw_path.rs
@@ -6,13 +6,14 @@
//! https://html.spec.whatwg.org/multipage/canvas.html#canvasdrawpath
-use libgfx_rust::path::Path;
+use libgfx_rust::path::{Path, StrokeStyle};
use libgfx_rust::{AffineTransform, FloatPoint, WindingRule};
use super::path2d::Path2D;
use crate::bindings::interface::Ref;
use crate::bindings::{CallContext, WebIdlResult};
use crate::canvas_2d::context::{CanvasKind, RenderingContext2D};
+use crate::canvas_2d::drawing_state::DrawingState;
use crate::canvas_2d::float;
/// https://html.spec.whatwg.org/multipage/canvas.html#canvasfillrule
@@ -56,6 +57,29 @@ fn is_point_in_path_internal(
path.contains(point.x, point.y, WindingRule::from(fill_rule))
}
+/// https://html.spec.whatwg.org/multipage/canvas.html#is-point-in-stroke-steps
+fn is_point_in_stroke_internal(path: &Path, state: &DrawingState, x: f64, y: f64) -> bool {
+ // 1. If x or y are infinite or NaN, then return false.
+ if !x.is_finite() || !y.is_finite() {
+ return false;
+ }
+
+ // 2. If the point given by the x and y coordinates, when treated as coordinates in the canvas coordinate space
+ // unaffected by the current transformation, is inside the path that results from tracing the intended path for
+ // path, using the nonzero winding rule, and using context's line styles as set by its CanvasPathDrawingStyles
+ // mixin, then return true. Points on the resulting path must be considered to be inside the path.
+ // 3. Return false.
+ let stroke = path.stroke_to_fill(&StrokeStyle {
+ thickness: state.line_width,
+ cap_style: state.cap_style(),
+ join_style: state.join_style(),
+ miter_limit: state.miter_limit,
+ dash_array: state.dash_array(),
+ dash_offset: state.line_dash_offset,
+ });
+ is_point_in_path_internal(&stroke, state.transform, x, y, CanvasFillRule::Nonzero)
+}
+
impl<K: CanvasKind> RenderingContext2D<K> {
/// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-beginpath
pub(crate) fn begin_path(&self, _: &mut CallContext) -> WebIdlResult<()> {
@@ -136,4 +160,24 @@ impl<K: CanvasKind> RenderingContext2D<K> {
let transform = self.drawing_state().transform;
Ok(is_point_in_path_internal(path_of(&path), transform, x, y, fill_rule))
}
+
+ /// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-ispointinstroke
+ pub(crate) fn is_point_in_stroke(&self, _: &mut CallContext, x: f64, y: f64) -> WebIdlResult<bool> {
+ // The isPointInStroke(x, y) method steps are to return the result of the is point in stroke steps given this,
+ // null, x, and y.
+ Ok(is_point_in_stroke_internal(&self.path(), &self.drawing_state(), x, y))
+ }
+
+ /// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-ispointinstroke-path
+ pub(crate) fn is_point_in_stroke_path2d(
+ &self,
+ _: &mut CallContext,
+ path: Ref<Path2D>,
+ x: f64,
+ y: f64,
+ ) -> WebIdlResult<bool> {
+ // The isPointInStroke(path, x, y) method steps are to return the result of the is point in stroke steps given
+ // this, path, x, and y.
+ Ok(is_point_in_stroke_internal(path_of(&path), &self.drawing_state(), x, y))
+ }
}
diff --git a/Libraries/LibWeb/HTML/Canvas/CanvasDrawPath.idl b/Libraries/LibWeb/HTML/Canvas/CanvasDrawPath.idl
index 9eccb25f3c6..4e34a2dade1 100644
--- a/Libraries/LibWeb/HTML/Canvas/CanvasDrawPath.idl
+++ b/Libraries/LibWeb/HTML/Canvas/CanvasDrawPath.idl
@@ -13,6 +13,6 @@ interface mixin CanvasDrawPath {
[ImplementedAs=clip_path2d] undefined clip(Path2D path, optional CanvasFillRule fillRule = "nonzero");
[ImplementedAs=is_point_in_path] boolean isPointInPath(unrestricted double x, unrestricted double y, optional CanvasFillRule fillRule = "nonzero");
[ImplementedAs=is_point_in_path2d] boolean isPointInPath(Path2D path, unrestricted double x, unrestricted double y, optional CanvasFillRule fillRule = "nonzero");
- [FIXME] boolean isPointInStroke(unrestricted double x, unrestricted double y);
- [FIXME] boolean isPointInStroke(Path2D path, unrestricted double x, unrestricted double y);
+ [ImplementedAs=is_point_in_stroke] boolean isPointInStroke(unrestricted double x, unrestricted double y);
+ [ImplementedAs=is_point_in_stroke_path2d] boolean isPointInStroke(Path2D path, unrestricted double x, unrestricted double y);
};
diff --git a/Tests/LibWeb/Text/expected/canvas/2d-attribute-setters.txt b/Tests/LibWeb/Text/expected/canvas/2d-attribute-setters.txt
index f4ba5bfd7f9..b05112ed48d 100644
--- a/Tests/LibWeb/Text/expected/canvas/2d-attribute-setters.txt
+++ b/Tests/LibWeb/Text/expected/canvas/2d-attribute-setters.txt
@@ -84,7 +84,7 @@ A canvas, after reset():
letterSpacing: 0px
getLineDash(): []
getTransform(): 1,0,0,1,0,0
-isPointInStroke: undefined, drawFocusIfNeeded: undefined
+isPointInStroke: function, drawFocusIfNeeded: undefined
canvas: true
An OffscreenCanvas, the initial state:
globalAlpha: 1
@@ -172,5 +172,5 @@ An OffscreenCanvas, after reset():
letterSpacing: 0px
getLineDash(): []
getTransform(): 1,0,0,1,0,0
-isPointInStroke: undefined, drawFocusIfNeeded: undefined
+isPointInStroke: function, drawFocusIfNeeded: undefined
canvas: true
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/path-objects/2d.path.isPointInStroke.scaleddashes.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/path-objects/2d.path.isPointInStroke.scaleddashes.txt
index dfe99d37f14..f30b128982a 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/path-objects/2d.path.isPointInStroke.scaleddashes.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/path-objects/2d.path.isPointInStroke.scaleddashes.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail isPointInStroke() should return correct results on dashed paths at high scale factors
\ No newline at end of file
+1 Pass
+Pass isPointInStroke() should return correct results on dashed paths at high scale factors
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.isPointInStroke.basic.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.isPointInStroke.basic.txt
index f46cda2d2f4..115969d0f61 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.isPointInStroke.basic.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.isPointInStroke.basic.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail detects whether point is in the area contained by the stroke of the path
\ No newline at end of file
+1 Pass
+Pass detects whether point is in the area contained by the stroke of the path
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.isPointInStroke.basic.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.isPointInStroke.basic.worker.txt
index f46cda2d2f4..115969d0f61 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.isPointInStroke.basic.worker.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.isPointInStroke.basic.worker.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail detects whether point is in the area contained by the stroke of the path
\ No newline at end of file
+1 Pass
+Pass detects whether point is in the area contained by the stroke of the path
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.isPointInStroke.scaleddashes.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.isPointInStroke.scaleddashes.txt
index dfe99d37f14..f30b128982a 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.isPointInStroke.scaleddashes.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.isPointInStroke.scaleddashes.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail isPointInStroke() should return correct results on dashed paths at high scale factors
\ No newline at end of file
+1 Pass
+Pass isPointInStroke() should return correct results on dashed paths at high scale factors
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.isPointInStroke.scaleddashes.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.isPointInStroke.scaleddashes.worker.txt
index dfe99d37f14..f30b128982a 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.isPointInStroke.scaleddashes.worker.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.isPointInStroke.scaleddashes.worker.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail isPointInStroke() should return correct results on dashed paths at high scale factors
\ No newline at end of file
+1 Pass
+Pass isPointInStroke() should return correct results on dashed paths at high scale factors
\ No newline at end of file