LibWeb: Reset Painter when resetting canvas to its initial state

This commit is contained in:
Tim Ledbetter 2025-10-21 17:49:12 +01:00 committed by Jelle Raaijmakers
parent 017e8a5b8d
commit 1c00279488
Notes: github-actions[bot] 2025-10-23 16:53:42 +00:00
7 changed files with 119 additions and 1 deletions

View file

@ -634,8 +634,10 @@ void CanvasRenderingContext2D::reset_to_default_state()
// 4. Reset everything that drawing state consists of to their initial values.
reset_drawing_state();
if (surface)
if (surface) {
painter()->reset();
did_draw(surface->rect().to_type<float>());
}
}
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-measuretext

View file

@ -0,0 +1,15 @@
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<meta charset="UTF-8">
<title>Canvas test: 2d.reset.state.clip</title>
<h1>2d.reset.state.clip</h1>
<p class="desc">check that the clip is reset</p>
<canvas id="canvas" width="200" height="200">
<p class="fallback">FAIL (fallback content)</p>
</canvas>
<script>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext('2d');
ctx.fillRect(0, 0, 200, 200);
</script>

View file

@ -0,0 +1,24 @@
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<meta charset="UTF-8">
<link rel="match" href="../../../../../../expected/wpt-import/html/canvas/element/reset/2d.reset.state.clip-expected.html">
<title>Canvas test: 2d.reset.state.clip</title>
<h1>2d.reset.state.clip</h1>
<p class="desc">check that the clip is reset</p>
<canvas id="canvas" width="200" height="200">
<p class="fallback">FAIL (fallback content)</p>
</canvas>
<script>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.rect(0, 0, 100, 100);
ctx.clip();
ctx.fillRect(0, 0, 200, 200);
ctx.reset();
ctx.fillRect(0, 0, 200, 200);
</script>

View file

@ -0,0 +1,6 @@
Harness status: OK
Found 1 tests
1 Pass
Pass Resetting the canvas state resets the current clip region

View file

@ -0,0 +1,6 @@
Harness status: OK
Found 1 tests
1 Pass
Pass Resetting the canvas state resets the current transformation matrix

View file

@ -0,0 +1,33 @@
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<meta charset="UTF-8">
<title>Canvas test: 2d.canvas.host.initial.reset.clip</title>
<script src="../../../../resources/testharness.js"></script>
<script src="../../../../resources/testharnessreport.js"></script>
<script src="../../../../html/canvas/resources/canvas-tests.js"></script>
<link rel="stylesheet" href="../../../../html/canvas/resources/canvas-tests.css">
<body class="show_output">
<h1>2d.canvas.host.initial.reset.clip</h1>
<p class="desc">Resetting the canvas state resets the current clip region</p>
<p class="output">Actual output:</p>
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
<ul id="d"></ul>
<script>
var t = async_test("Resetting the canvas state resets the current clip region");
_addTest(function(canvas, ctx) {
canvas.width = 100;
ctx.rect(0, 0, 1, 1);
ctx.clip();
canvas.width = 100;
ctx.fillStyle = '#0f0';
ctx.fillRect(0, 0, 100, 50);
_assertPixel(canvas, 20,20, 0,255,0,255);
});
</script>

View file

@ -0,0 +1,32 @@
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
<meta charset="UTF-8">
<title>Canvas test: 2d.canvas.host.initial.reset.transform</title>
<script src="../../../../resources/testharness.js"></script>
<script src="../../../../resources/testharnessreport.js"></script>
<script src="../../../../html/canvas/resources/canvas-tests.js"></script>
<link rel="stylesheet" href="../../../../html/canvas/resources/canvas-tests.css">
<body class="show_output">
<h1>2d.canvas.host.initial.reset.transform</h1>
<p class="desc">Resetting the canvas state resets the current transformation matrix</p>
<p class="output">Actual output:</p>
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
<ul id="d"></ul>
<script>
var t = async_test("Resetting the canvas state resets the current transformation matrix");
_addTest(function(canvas, ctx) {
canvas.width = 100;
ctx.scale(0.1, 0.1);
canvas.width = 100;
ctx.fillStyle = '#0f0';
ctx.fillRect(0, 0, 100, 50);
_assertPixel(canvas, 20,20, 0,255,0,255);
});
</script>