Skip to content

Truly independent web browser

LibWeb: Find no point in a path under a matrix that is not invertible

Andreas Kling3bf7491fff29
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 fbc8bbfc909..53819484ff3 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_draw_path.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_draw_path.rs
@@ -45,13 +45,14 @@ fn is_point_in_path_internal(
y: f64,
fill_rule: CanvasFillRule,
) -> bool {
- let mut point = FloatPoint {
+ // NB: A transform that is not invertible maps every path onto a line or a point, which has no inside.
+ let Some(inverse_transform) = transform.inverse() else {
+ return false;
+ };
+ let point = inverse_transform.map_point(FloatPoint {
x: float(x),
y: float(y),
- };
- if let Some(inverse_transform) = transform.inverse() {
- point = inverse_transform.map_point(point);
- }
+ });
path.contains(point.x, point.y, WindingRule::from(fill_rule) as i32)
}
diff --git a/Tests/LibWeb/Text/expected/canvas/2d-is-point-in-path.txt b/Tests/LibWeb/Text/expected/canvas/2d-is-point-in-path.txt
index 2667de06ad3..21c97ae6f98 100644
--- a/Tests/LibWeb/Text/expected/canvas/2d-is-point-in-path.txt
+++ b/Tests/LibWeb/Text/expected/canvas/2d-is-point-in-path.txt
@@ -3,7 +3,7 @@ A canvas:
two rectangles, evenodd: 1100000
a translated rectangle: 0111001
after a later scale: 0111001
- under a singular transform: 1010000
+ under a singular transform: 0000000
a Path2D circle: 0101000
a Path2D circle under a rotation: 0000000
an empty Path2D: 0000000
@@ -31,7 +31,7 @@ An OffscreenCanvas:
two rectangles, evenodd: 1100000
a translated rectangle: 0111001
after a later scale: 0111001
- under a singular transform: 1010000
+ under a singular transform: 0000000
a Path2D circle: 0101000
a Path2D circle under a rotation: 0000000
an empty Path2D: 0000000
diff --git a/Tests/LibWeb/Text/expected/canvas/is-point-in-path-non-invertible-transform.txt b/Tests/LibWeb/Text/expected/canvas/is-point-in-path-non-invertible-transform.txt
new file mode 100644
index 00000000000..e5cf50012c0
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/canvas/is-point-in-path-non-invertible-transform.txt
@@ -0,0 +1,4 @@
+Path2D: true
+Path2D under scale(0, 0): false
+Path2D under setTransform(0, 0, 0, 0, 0, 0): false
+Path added under setTransform(0, 0, 0, 0, 0, 0): false
diff --git a/Tests/LibWeb/Text/input/canvas/is-point-in-path-non-invertible-transform.html b/Tests/LibWeb/Text/input/canvas/is-point-in-path-non-invertible-transform.html
new file mode 100644
index 00000000000..7bfa7b11d2d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/canvas/is-point-in-path-non-invertible-transform.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // Under a transformation matrix that is not invertible, no point is in a path: a Path2D is transformed by the
+ // matrix onto a line or a point, and so are the points that the current default path gets.
+ test(() => {
+ const context = document.createElement("canvas").getContext("2d");
+ const path = new Path2D();
+ path.rect(0, 0, 10, 10);
+ println(`Path2D: ${context.isPointInPath(path, 5, 5)}`);
+ context.scale(0, 0);
+ println(`Path2D under scale(0, 0): ${context.isPointInPath(path, 5, 5)}`);
+ context.setTransform(0, 0, 0, 0, 0, 0);
+ println(`Path2D under setTransform(0, 0, 0, 0, 0, 0): ${context.isPointInPath(path, 0, 0)}`);
+
+ context.rect(0, 0, 10, 10);
+ println(`Path added under setTransform(0, 0, 0, 0, 0, 0): ${context.isPointInPath(5, 5)}`);
+ });
+</script>