Skip to content

Truly independent web browser

LibWeb: Handle negative widths and heights in canvas roundRect()

Andreas Kling6bffbe0e35a9
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_path.rs b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_path.rs
index ed26cd01ed2..fe2fe0c89c4 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_path.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_path.rs
@@ -630,8 +630,11 @@ pub(crate) fn round_rect(
// 11.5. Let scale be the minimum value of the ratios w / top, h / right, w / bottom, h / left.
// NB: As AK::min(), which the C++ took it with, this keeps the first of two ratios unless the second is less.
+ // INTEROP: The ratios are taken with the magnitudes of w and h, so that a rectangle with a negative width or
+ // height keeps its radii.
let min = |a: f64, b: f64| if b < a { b } else { a };
- let scale = min(min(w / top, h / right), min(w / bottom, h / left));
+ let (width, height) = (w.abs(), h.abs());
+ let scale = min(min(width / top, height / right), min(width / bottom, height / left));
// 11.6. If scale is less than 1, then set the x and y members of upperLeft, upperRight, lowerLeft, and
// lowerRight to their current values multiplied by scale.
@@ -643,8 +646,13 @@ pub(crate) fn round_rect(
}
// 12. Create a new subpath:
+ // INTEROP: When w or h is negative, the rectangle extends to the left of x or above y. The corner radii are then
+ // measured into the rectangle from its corners, and the arcs turn the other way when just one of them
+ // is negative.
+ let x_direction = if w < 0.0 { -1.0 } else { 1.0 };
+ let y_direction = if h < 0.0 { -1.0 } else { 1.0 };
let large_arc = false;
- let sweep = true;
+ let sweep = x_direction * y_direction > 0.0;
#[allow(
clippy::cast_possible_truncation,
reason = "the radii of an arc of a Gfx::Path are floats"
@@ -655,14 +663,14 @@ pub(crate) fn round_rect(
};
// 12.1. Move to the point (x + upperLeft["x"], y).
- path.move_to(point_from_doubles(x + upper_left.x, y));
+ path.move_to(point_from_doubles(x + x_direction * upper_left.x, y));
// 12.2. Draw a straight line to the point (x + w − upperRight["x"], y).
- path.line_to(point_from_doubles(x + w - upper_right.x, y));
+ path.line_to(point_from_doubles(x + w - x_direction * upper_right.x, y));
// 12.3. Draw an arc to the point (x + w, y + upperRight["y"]).
path.elliptical_arc_to(
- point_from_doubles(x + w, y + upper_right.y),
+ point_from_doubles(x + w, y + y_direction * upper_right.y),
radii_of(upper_right),
0.0,
large_arc,
@@ -670,11 +678,11 @@ pub(crate) fn round_rect(
);
// 12.4. Draw a straight line to the point (x + w, y + h − lowerRight["y"]).
- path.line_to(point_from_doubles(x + w, y + h - lower_right.y));
+ path.line_to(point_from_doubles(x + w, y + h - y_direction * lower_right.y));
// 12.5. Draw an arc to the point (x + w − lowerRight["x"], y + h).
path.elliptical_arc_to(
- point_from_doubles(x + w - lower_right.x, y + h),
+ point_from_doubles(x + w - x_direction * lower_right.x, y + h),
radii_of(lower_right),
0.0,
large_arc,
@@ -682,11 +690,11 @@ pub(crate) fn round_rect(
);
// 12.6. Draw a straight line to the point (x + lowerLeft["x"], y + h).
- path.line_to(point_from_doubles(x + lower_left.x, y + h));
+ path.line_to(point_from_doubles(x + x_direction * lower_left.x, y + h));
// 12.7. Draw an arc to the point (x, y + h − lowerLeft["y"]).
path.elliptical_arc_to(
- point_from_doubles(x, y + h - lower_left.y),
+ point_from_doubles(x, y + h - y_direction * lower_left.y),
radii_of(lower_left),
0.0,
large_arc,
@@ -694,11 +702,11 @@ pub(crate) fn round_rect(
);
// 12.8. Draw a straight line to the point (x, y + upperLeft["y"]).
- path.line_to(point_from_doubles(x, y + upper_left.y));
+ path.line_to(point_from_doubles(x, y + y_direction * upper_left.y));
// 12.9. Draw an arc to the point (x + upperLeft["x"], y).
path.elliptical_arc_to(
- point_from_doubles(x + upper_left.x, y),
+ point_from_doubles(x + x_direction * upper_left.x, y),
radii_of(upper_left),
0.0,
large_arc,
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/path-objects/2d.path.roundrect.negative.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/path-objects/2d.path.roundrect.negative.txt
index 07ed5fe0fb1..6412280f87f 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/path-objects/2d.path.roundrect.negative.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/path-objects/2d.path.roundrect.negative.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail Canvas test: 2d.path.roundrect.negative
\ No newline at end of file
+1 Pass
+Pass Canvas test: 2d.path.roundrect.negative
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.roundrect.negative.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.roundrect.negative.txt
index c152b6ea31c..587a0e8d780 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.roundrect.negative.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.roundrect.negative.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail OffscreenCanvas test: 2d.path.roundrect.negative
\ No newline at end of file
+1 Pass
+Pass OffscreenCanvas test: 2d.path.roundrect.negative
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.roundrect.negative.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.roundrect.negative.worker.txt
index cd924677233..cf5877d439e 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.roundrect.negative.worker.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.roundrect.negative.worker.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail 2d
\ No newline at end of file
+1 Pass
+Pass 2d
\ No newline at end of file