Skip to content

Truly independent web browser

LibWeb: Do not map the last point of a path twice in arcTo()

Andreas Kling781d79c930b6
diff --git a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/path.rs b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/path.rs
index 985e634ba0c..6e7eed6cde5 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/path.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/path.rs
@@ -80,8 +80,7 @@ impl<K: CanvasKind> RenderingContext2D<K> {
y2: f64,
radius: f64,
) -> WebIdlResult<()> {
- let transform = self.drawing_state().transform;
- self.build_path(|path| canvas_path::arc_to(path, transform, x1, y1, x2, y2, radius))?;
+ self.build_path(|path| canvas_path::arc_to(path, x1, y1, x2, y2, radius))?;
Ok(())
}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_path.rs b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_path.rs
index 2e98711ff4d..82d29f350d0 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_path.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_path.rs
@@ -11,7 +11,7 @@
//! quadraticCurveTo(), arc() and ellipse() are floats, and those of the rest are doubles that become floats where
//! they become points.
-use libgfx_rust::{AffineTransform, FloatPoint, FloatSize};
+use libgfx_rust::{FloatPoint, FloatSize};
use crate::ak_format::ak_formatted;
@@ -427,11 +427,8 @@ pub(crate) fn ellipse(
}
/// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-arcto
-///
-/// The transform is the current transformation matrix of a context, and the identity for a Path2D.
pub(crate) fn arc_to(
path: &mut impl PathSink,
- transform: AffineTransform,
x1: f64,
y1: f64,
x2: f64,
@@ -458,8 +455,10 @@ pub(crate) fn arc_to(
// 4. Let the point (x0, y0) be the last point in the subpath, transformed by the inverse of the current
// transformation matrix (so that it is in the same coordinate system as the points passed to the method).
+ // NB: A Path2D has no transformation matrix, and a context keeps its current default path in the coordinates of
+ // its current transformation matrix, so the last point is in that coordinate system already.
// Point (x0, y0)
- let p0 = transform.inverse().unwrap_or_default().map_point(path.last_point());
+ let p0 = path.last_point();
// 5. If the point (x0, y0) is equal to the point (x1, y1), or if the point (x1, y1) is equal to the point
// (x2, y2), or if radius is zero, then add the point (x1, y1) to the subpath, and connect that point to the
@@ -721,7 +720,7 @@ mod tests {
CanvasPathError, PathSink, RoundRectRadius, arc, arc_to, close_path, ellipse, line_to, move_to, rect,
round_rect,
};
- use libgfx_rust::{AffineTransform, FloatPoint, FloatSize};
+ use libgfx_rust::{FloatPoint, FloatSize};
#[derive(Debug, PartialEq)]
enum Op {
@@ -864,7 +863,7 @@ mod tests {
Err(CanvasPathError::NegativeMinorAxisRadius(-2.0))
);
assert_eq!(
- arc_to(&mut path, AffineTransform::identity(), 1.0, 1.0, 2.0, 2.0, -0.5),
+ arc_to(&mut path, 1.0, 1.0, 2.0, 2.0, -0.5),
Err(CanvasPathError::NegativeArcToRadius(-0.5))
);
// NB: arcTo() ensures there is a subpath before it checks the radius.
@@ -911,14 +910,8 @@ mod tests {
fn arc_to_with_collinear_points_is_a_line() {
let mut path = RecordingPath::default();
move_to(&mut path, 0.0, 0.0);
- assert_eq!(
- arc_to(&mut path, AffineTransform::identity(), 1.0, 0.0, 2.0, 0.0, 1.0),
- Ok(())
- );
- assert_eq!(
- arc_to(&mut path, AffineTransform::identity(), 3.0, 3.0, 4.0, 3.0, 0.0),
- Ok(())
- );
+ assert_eq!(arc_to(&mut path, 1.0, 0.0, 2.0, 0.0, 1.0), Ok(()));
+ assert_eq!(arc_to(&mut path, 3.0, 3.0, 4.0, 3.0, 0.0), Ok(()));
assert_eq!(
path.ops,
[Op::MoveTo(0.0, 0.0), Op::LineTo(1.0, 0.0), Op::LineTo(3.0, 3.0)]
@@ -929,10 +922,7 @@ mod tests {
fn arc_to_a_corner_is_a_line_and_an_arc_between_the_tangent_points() {
let mut path = RecordingPath::default();
move_to(&mut path, 0.0, 0.0);
- assert_eq!(
- arc_to(&mut path, AffineTransform::identity(), 10.0, 0.0, 10.0, 10.0, 5.0),
- Ok(())
- );
+ assert_eq!(arc_to(&mut path, 10.0, 0.0, 10.0, 10.0, 5.0), Ok(()));
let [_, Op::LineTo(x3, y3), Op::ArcTo { x, y, radius, sweep }] = path.ops.as_slice() else {
panic!("the corner is a line and an arc: {:?}", path.ops);
};
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/path2d.rs b/Libraries/LibWeb/Bindings/Rust/src/html/path2d.rs
index 2d91987b339..4b7002910de 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/path2d.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/path2d.rs
@@ -9,7 +9,6 @@
use core::cell::RefCell;
use ak::Utf16String;
-use libgfx_rust::AffineTransform;
use libgfx_rust::path::{OwnedPath, PathBuilder, PathMut};
use libjs_rust::api::Trace;
@@ -195,7 +194,7 @@ impl Path2D {
radius: f64,
) -> WebIdlResult<()> {
// NB: A Path2D has no transformation matrix, so its points are in the coordinates that it was given.
- self.build(|path| canvas_path::arc_to(path, AffineTransform::identity(), x1, y1, x2, y2, radius))?;
+ self.build(|path| canvas_path::arc_to(path, x1, y1, x2, y2, radius))?;
Ok(())
}
diff --git a/Tests/LibWeb/Text/expected/canvas/arc-to-under-transform.txt b/Tests/LibWeb/Text/expected/canvas/arc-to-under-transform.txt
new file mode 100644
index 00000000000..57b85bf72b7
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/canvas/arc-to-under-transform.txt
@@ -0,0 +1,4 @@
+Inside the square: 255
+Above the top edge: 0
+Outside the rounded corner: 0
+Inside the rounded corner: 255
diff --git a/Tests/LibWeb/Text/input/canvas/arc-to-under-transform.html b/Tests/LibWeb/Text/input/canvas/arc-to-under-transform.html
new file mode 100644
index 00000000000..ee623eca4ce
--- /dev/null
+++ b/Tests/LibWeb/Text/input/canvas/arc-to-under-transform.html
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // arcTo() takes the last point of the current default path in the coordinates of the current transformation
+ // matrix, which the path is kept in. The shape is a square from (10, 10) to (50, 50) whose top right corner is
+ // rounded with a radius of 20, drawn under a translation of (0, 20).
+ test(() => {
+ const canvas = document.createElement("canvas");
+ canvas.width = 100;
+ canvas.height = 100;
+ const context = canvas.getContext("2d");
+ context.translate(0, 20);
+ context.moveTo(10, 10);
+ context.arcTo(50, 10, 50, 50, 20);
+ context.lineTo(50, 50);
+ context.lineTo(10, 50);
+ context.closePath();
+ context.fill();
+
+ const alphaAt = (x, y) => context.getImageData(x, y + 20, 1, 1).data[3];
+ println(`Inside the square: ${alphaAt(20, 30)}`);
+ println(`Above the top edge: ${alphaAt(30, 7)}`);
+ println(`Outside the rounded corner: ${alphaAt(45, 13)}`);
+ println(`Inside the rounded corner: ${alphaAt(40, 20)}`);
+ });
+</script>