Skip to content

Commit 494fcc4

Browse files
tcl3gmta
authored andcommitted
LibWeb: Account for transforms in isPointInPath()
1 parent d17f666 commit 494fcc4

File tree

9 files changed

+165
-4
lines changed

9 files changed

+165
-4
lines changed

Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -774,19 +774,22 @@ void CanvasRenderingContext2D::clip(Path2D& path, StringView fill_rule)
774774
clip_internal(path.path(), parse_fill_rule(fill_rule));
775775
}
776776

777-
static bool is_point_in_path_internal(Gfx::Path path, double x, double y, StringView fill_rule)
777+
static bool is_point_in_path_internal(Gfx::Path path, Gfx::AffineTransform const& transform, double x, double y, StringView fill_rule)
778778
{
779-
return path.contains(Gfx::FloatPoint(x, y), parse_fill_rule(fill_rule));
779+
auto point = Gfx::FloatPoint(x, y);
780+
if (auto inverse_transform = transform.inverse(); inverse_transform.has_value())
781+
point = inverse_transform->map(point);
782+
return path.contains(point, parse_fill_rule(fill_rule));
780783
}
781784

782785
bool CanvasRenderingContext2D::is_point_in_path(double x, double y, StringView fill_rule)
783786
{
784-
return is_point_in_path_internal(path(), x, y, fill_rule);
787+
return is_point_in_path_internal(path(), drawing_state().transform, x, y, fill_rule);
785788
}
786789

787790
bool CanvasRenderingContext2D::is_point_in_path(Path2D const& path, double x, double y, StringView fill_rule)
788791
{
789-
return is_point_in_path_internal(path.path(), x, y, fill_rule);
792+
return is_point_in_path_internal(path.path(), drawing_state().transform, x, y, fill_rule);
790793
}
791794

792795
// https://html.spec.whatwg.org/multipage/canvas.html#check-the-usability-of-the-image-argument
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Harness status: OK
2+
3+
Found 1 tests
4+
5+
1 Pass
6+
Pass isPointInPath() handles transformations correctly
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Harness status: OK
2+
3+
Found 1 tests
4+
5+
1 Pass
6+
Pass isPointInPath() handles transformations correctly
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Harness status: OK
2+
3+
Found 1 tests
4+
5+
1 Pass
6+
Pass isPointInPath() handles transformations correctly
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Harness status: OK
2+
3+
Found 1 tests
4+
5+
1 Pass
6+
Pass isPointInPath() handles transformations correctly
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
3+
<meta charset="UTF-8">
4+
<title>Canvas test: 2d.path.isPointInPath.transform.1</title>
5+
<script src="../../../../resources/testharness.js"></script>
6+
<script src="../../../../resources/testharnessreport.js"></script>
7+
<script src="../../../../html/canvas/resources/canvas-tests.js"></script>
8+
<link rel="stylesheet" href="../../../../html/canvas/resources/canvas-tests.css">
9+
<body class="show_output">
10+
11+
<h1>2d.path.isPointInPath.transform.1</h1>
12+
<p class="desc">isPointInPath() handles transformations correctly</p>
13+
14+
15+
<p class="output">Actual output:</p>
16+
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
17+
18+
<ul id="d"></ul>
19+
<script>
20+
var t = async_test("isPointInPath() handles transformations correctly");
21+
_addTest(function(canvas, ctx) {
22+
23+
ctx.translate(50, 0);
24+
ctx.rect(0, 0, 20, 20);
25+
_assertSame(ctx.isPointInPath(-40, 10), false, "ctx.isPointInPath(-40, 10)", "false");
26+
_assertSame(ctx.isPointInPath(10, 10), false, "ctx.isPointInPath(10, 10)", "false");
27+
_assertSame(ctx.isPointInPath(49, 10), false, "ctx.isPointInPath(49, 10)", "false");
28+
_assertSame(ctx.isPointInPath(51, 10), true, "ctx.isPointInPath(51, 10)", "true");
29+
_assertSame(ctx.isPointInPath(69, 10), true, "ctx.isPointInPath(69, 10)", "true");
30+
_assertSame(ctx.isPointInPath(71, 10), false, "ctx.isPointInPath(71, 10)", "false");
31+
32+
});
33+
</script>
34+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
3+
<meta charset="UTF-8">
4+
<title>Canvas test: 2d.path.isPointInPath.transform.2</title>
5+
<script src="../../../../resources/testharness.js"></script>
6+
<script src="../../../../resources/testharnessreport.js"></script>
7+
<script src="../../../../html/canvas/resources/canvas-tests.js"></script>
8+
<link rel="stylesheet" href="../../../../html/canvas/resources/canvas-tests.css">
9+
<body class="show_output">
10+
11+
<h1>2d.path.isPointInPath.transform.2</h1>
12+
<p class="desc">isPointInPath() handles transformations correctly</p>
13+
14+
15+
<p class="output">Actual output:</p>
16+
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
17+
18+
<ul id="d"></ul>
19+
<script>
20+
var t = async_test("isPointInPath() handles transformations correctly");
21+
_addTest(function(canvas, ctx) {
22+
23+
ctx.rect(50, 0, 20, 20);
24+
ctx.translate(50, 0);
25+
_assertSame(ctx.isPointInPath(-40, 10), false, "ctx.isPointInPath(-40, 10)", "false");
26+
_assertSame(ctx.isPointInPath(10, 10), false, "ctx.isPointInPath(10, 10)", "false");
27+
_assertSame(ctx.isPointInPath(49, 10), false, "ctx.isPointInPath(49, 10)", "false");
28+
_assertSame(ctx.isPointInPath(51, 10), true, "ctx.isPointInPath(51, 10)", "true");
29+
_assertSame(ctx.isPointInPath(69, 10), true, "ctx.isPointInPath(69, 10)", "true");
30+
_assertSame(ctx.isPointInPath(71, 10), false, "ctx.isPointInPath(71, 10)", "false");
31+
32+
});
33+
</script>
34+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
3+
<meta charset="UTF-8">
4+
<title>Canvas test: 2d.path.isPointInPath.transform.3</title>
5+
<script src="../../../../resources/testharness.js"></script>
6+
<script src="../../../../resources/testharnessreport.js"></script>
7+
<script src="../../../../html/canvas/resources/canvas-tests.js"></script>
8+
<link rel="stylesheet" href="../../../../html/canvas/resources/canvas-tests.css">
9+
<body class="show_output">
10+
11+
<h1>2d.path.isPointInPath.transform.3</h1>
12+
<p class="desc">isPointInPath() handles transformations correctly</p>
13+
14+
15+
<p class="output">Actual output:</p>
16+
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
17+
18+
<ul id="d"></ul>
19+
<script>
20+
var t = async_test("isPointInPath() handles transformations correctly");
21+
_addTest(function(canvas, ctx) {
22+
23+
ctx.scale(-1, 1);
24+
ctx.rect(-70, 0, 20, 20);
25+
_assertSame(ctx.isPointInPath(-40, 10), false, "ctx.isPointInPath(-40, 10)", "false");
26+
_assertSame(ctx.isPointInPath(10, 10), false, "ctx.isPointInPath(10, 10)", "false");
27+
_assertSame(ctx.isPointInPath(49, 10), false, "ctx.isPointInPath(49, 10)", "false");
28+
_assertSame(ctx.isPointInPath(51, 10), true, "ctx.isPointInPath(51, 10)", "true");
29+
_assertSame(ctx.isPointInPath(69, 10), true, "ctx.isPointInPath(69, 10)", "true");
30+
_assertSame(ctx.isPointInPath(71, 10), false, "ctx.isPointInPath(71, 10)", "false");
31+
32+
});
33+
</script>
34+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
3+
<meta charset="UTF-8">
4+
<title>Canvas test: 2d.path.isPointInPath.transform.4</title>
5+
<script src="../../../../resources/testharness.js"></script>
6+
<script src="../../../../resources/testharnessreport.js"></script>
7+
<script src="../../../../html/canvas/resources/canvas-tests.js"></script>
8+
<link rel="stylesheet" href="../../../../html/canvas/resources/canvas-tests.css">
9+
<body class="show_output">
10+
11+
<h1>2d.path.isPointInPath.transform.4</h1>
12+
<p class="desc">isPointInPath() handles transformations correctly</p>
13+
14+
15+
<p class="output">Actual output:</p>
16+
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
17+
18+
<ul id="d"></ul>
19+
<script>
20+
var t = async_test("isPointInPath() handles transformations correctly");
21+
_addTest(function(canvas, ctx) {
22+
23+
ctx.translate(50, 0);
24+
ctx.rect(50, 0, 20, 20);
25+
ctx.translate(0, 50);
26+
_assertSame(ctx.isPointInPath(60, 10), false, "ctx.isPointInPath(60, 10)", "false");
27+
_assertSame(ctx.isPointInPath(110, 10), true, "ctx.isPointInPath(110, 10)", "true");
28+
_assertSame(ctx.isPointInPath(110, 60), false, "ctx.isPointInPath(110, 60)", "false");
29+
30+
});
31+
</script>
32+

0 commit comments

Comments
 (0)