Skip to content

Truly independent web browser

LibWeb: Keep the path in place when a context replaces its transform

Andreas Kling3afa64657186
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_state.rs b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_state.rs
index aac27821b19..a210f8a8561 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_state.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_state.rs
@@ -23,9 +23,12 @@ impl<K: CanvasKind> RenderingContext2D<K> {
pub(crate) fn restore(&self, _: &mut CallContext) -> WebIdlResult<()> {
// The restore() method steps are to pop the top entry in the drawing state stack, and reset the drawing state
// it describes. If there is no saved state, then the method must do nothing.
+ let original_transform = self.drawing_state().transform;
if !self.pop_drawing_state() {
return Ok(());
}
+ // NB: The current default path is not part of the drawing state.
+ self.keep_path_in_place(original_transform);
self.record_if_ready(CanvasRecorder::restore);
Ok(())
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_transform.rs b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_transform.rs
index 3f27eb21518..df9cb3f6660 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_transform.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_transform.rs
@@ -138,10 +138,14 @@ impl<K: CanvasKind> RenderingContext2D<K> {
// a c e
// b d f
// 0 0 1
- // NB: That is the identity matrix multiplied with the matrix.
- self.drawing_state_mut().transform = AffineTransform::identity();
- self.flush_transform();
- self.multiply_transform(a, b, c, d, e, f);
+ self.set_current_transform(AffineTransform::new(
+ float(a),
+ float(b),
+ float(c),
+ float(d),
+ float(e),
+ float(f),
+ ));
Ok(())
}
@@ -157,14 +161,7 @@ impl<K: CanvasKind> RenderingContext2D<K> {
}
// 3. Reset the current transformation matrix to matrix.
- let original_transform = {
- let mut state = self.drawing_state_mut();
- core::mem::replace(&mut state.transform, matrix.to_affine_transform())
- };
-
- self.transform_path(original_transform);
-
- self.flush_transform();
+ self.set_current_transform(matrix.to_affine_transform());
Ok(())
}
@@ -172,8 +169,33 @@ impl<K: CanvasKind> RenderingContext2D<K> {
pub(crate) fn reset_transform(&self, _: &mut CallContext) -> WebIdlResult<()> {
// The resetTransform() method, when invoked, must reset the current transformation matrix to the identity
// matrix.
- self.drawing_state_mut().transform = AffineTransform::identity();
- self.flush_transform();
+ self.set_current_transform(AffineTransform::identity());
Ok(())
}
+
+ /// Replaces the current transformation matrix, which leaves the current default path where it is on the output
+ /// bitmap.
+ fn set_current_transform(&self, transform: AffineTransform) {
+ let original_transform = core::mem::replace(&mut self.drawing_state_mut().transform, transform);
+ self.keep_path_in_place(original_transform);
+ self.flush_transform();
+ }
+
+ /// Moves the current default path from the coordinates of the original transformation matrix to those of the
+ /// current one, after a change of the matrix that is not a multiplication with another, so that the points of
+ /// the path stay where they were on the output bitmap when they were added.
+ pub(crate) fn keep_path_in_place(&self, original_transform: AffineTransform) {
+ let transform = self.drawing_state().transform;
+ if transform.values == original_transform.values {
+ return;
+ }
+ // NB: A path cannot be kept in the coordinates of a matrix that is not invertible, so it stays as it is when it
+ // changes to or from one, as it does for transform().
+ if original_transform.inverse().is_none() {
+ return;
+ }
+ if let Some(inverse) = transform.inverse() {
+ self.transform_path(multiply_transforms(inverse, original_transform));
+ }
+ }
}
diff --git a/Tests/LibWeb/Text/expected/canvas/path-across-transform-change.txt b/Tests/LibWeb/Text/expected/canvas/path-across-transform-change.txt
new file mode 100644
index 00000000000..3dfe5cf466e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/canvas/path-across-transform-change.txt
@@ -0,0 +1,5 @@
+No change: 255 255 0 0
+resetTransform(): 255 255 0 0
+setTransform(1, 0, 0, 1, 30, 0): 255 255 0 0
+setTransform({ e: 30 }): 255 255 0 0
+restore(): 255 255 0 0
diff --git a/Tests/LibWeb/Text/input/canvas/path-across-transform-change.html b/Tests/LibWeb/Text/input/canvas/path-across-transform-change.html
new file mode 100644
index 00000000000..9097f879f93
--- /dev/null
+++ b/Tests/LibWeb/Text/input/canvas/path-across-transform-change.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // The points of the current default path stay where they were on the bitmap when they were added, whatever
+ // resetTransform(), setTransform() and restore() do to the current transformation matrix later.
+ function run(label, change) {
+ const canvas = document.createElement("canvas");
+ canvas.width = 60;
+ canvas.height = 60;
+ const context = canvas.getContext("2d");
+ context.save();
+ context.scale(2, 2);
+ context.rect(0, 0, 10, 10);
+ change(context);
+ context.fill();
+ const alphaAt = (x, y) => context.getImageData(x, y, 1, 1).data[3];
+ println(`${label}: ${[alphaAt(5, 5), alphaAt(15, 15), alphaAt(25, 25), alphaAt(35, 5)].join(" ")}`);
+ }
+
+ test(() => {
+ run("No change", () => {});
+ run("resetTransform()", context => context.resetTransform());
+ run("setTransform(1, 0, 0, 1, 30, 0)", context => context.setTransform(1, 0, 0, 1, 30, 0));
+ run("setTransform({ e: 30 })", context => context.setTransform({ e: 30 }));
+ run("restore()", context => context.restore());
+ });
+</script>