Skip to content

Truly independent web browser

LibWeb: Keep canvas arcs of a whole turn whole

Andreas Kling4000ec49f66d
diff --git a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/path.rs b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/path.rs
index 228071df896..daaae501e6c 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/path.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/path.rs
@@ -123,8 +123,8 @@ impl<K: CanvasKind> RenderingContext2D<K> {
float(x),
float(y),
float(radius),
- float(start_angle),
- float(end_angle),
+ start_angle,
+ end_angle,
counterclockwise,
)
})?;
@@ -153,8 +153,8 @@ impl<K: CanvasKind> RenderingContext2D<K> {
float(radius_x),
float(radius_y),
float(rotation),
- float(start_angle),
- float(end_angle),
+ start_angle,
+ end_angle,
counterclockwise,
)
})?;
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_path.rs b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_path.rs
index fe2fe0c89c4..596643677d3 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_path.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_path.rs
@@ -282,8 +282,8 @@ pub(crate) fn arc(
x: f32,
y: f32,
radius: f32,
- start_angle: f32,
- end_angle: f32,
+ start_angle: f64,
+ end_angle: f64,
counterclockwise: bool,
) -> Result<(), CanvasPathError> {
if radius < 0.0 && radius.is_finite() {
@@ -311,14 +311,16 @@ pub(crate) fn ellipse(
radius_x: f32,
radius_y: f32,
rotation: f32,
- mut start_angle: f32,
- mut end_angle: f32,
- mut counterclockwise: bool,
+ start_angle: f64,
+ end_angle: f64,
+ counterclockwise: bool,
) -> Result<(), CanvasPathError> {
// 1. If any of the arguments are infinite or NaN, then return.
- if [x, y, radius_x, radius_y, rotation, start_angle, end_angle]
+ if [x, y, radius_x, radius_y, rotation]
.iter()
.any(|argument| !argument.is_finite())
+ || !start_angle.is_finite()
+ || !end_angle.is_finite()
{
return Ok(());
}
@@ -340,20 +342,17 @@ pub(crate) fn ellipse(
// is true and startAngle − endAngle is greater than or equal to 2π, then the arc is the whole circumference of
// this ellipse"
// Also draw the full ellipse if making a non-zero whole number of turns.
+ // NB: The angles are compared in double precision, as they were given, so that an arc of exactly 2π stays whole.
+ let is_whole_circumference = (!counterclockwise && (end_angle - start_angle) >= core::f64::consts::TAU)
+ || (counterclockwise && (start_angle - end_angle) >= core::f64::consts::TAU)
+ || (start_angle != end_angle && (start_angle - end_angle) % core::f64::consts::TAU == 0.0);
+
let tau = core::f32::consts::TAU;
- if (!counterclockwise && (end_angle - start_angle) >= tau)
- || (counterclockwise && (start_angle - end_angle) >= tau)
- || (start_angle != end_angle && (start_angle - end_angle) % tau == 0.0)
- {
- start_angle = 0.0;
- // FIXME: elliptical_arc_to() incorrectly handles the case where the start/end points are very close.
- // So we slightly fudge the numbers here to correct for that.
- end_angle = tau * 0.9999;
- counterclockwise = false;
- } else {
- start_angle %= tau;
- end_angle %= tau;
- }
+ #[allow(
+ clippy::cast_possible_truncation,
+ reason = "the angles of an arc of a Gfx::Path are floats"
+ )]
+ let (start_angle, end_angle) = ((start_angle as f32) % tau, (end_angle as f32) % tau);
// Then, figure out where the ends of the arc are.
// To do so, we can pretend that the center of this ellipse is at (0, 0), and the whole coordinate system is
@@ -410,13 +409,28 @@ pub(crate) fn ellipse(
// start point of the arc.
add_line_to_start_point(path, start_point);
+ let radii = FloatSize {
+ width: radius_x,
+ height: radius_y,
+ };
+
+ // NB: The whole circumference starts and ends at the start point, so it is added as two halves.
+ if is_whole_circumference {
+ let half_turn = if counterclockwise {
+ -core::f32::consts::PI
+ } else {
+ core::f32::consts::PI
+ };
+ let opposite_point = resolve_point_with_angle(start_angle + half_turn);
+ path.elliptical_arc_to(opposite_point, radii, rotation, false, !counterclockwise);
+ path.elliptical_arc_to(start_point, radii, rotation, false, !counterclockwise);
+ return Ok(());
+ }
+
// 4. Add the start and end points of the arc to the subpath, and connect them with an arc.
path.elliptical_arc_to(
end_point,
- FloatSize {
- width: radius_x,
- height: radius_y,
- },
+ radii,
rotation,
delta_theta > core::f32::consts::PI,
!counterclockwise,
@@ -893,24 +907,34 @@ mod tests {
fn a_whole_turn_is_the_whole_ellipse() {
let mut path = RecordingPath::default();
assert_eq!(
- arc(&mut path, 0.0, 0.0, 10.0, 0.0, core::f32::consts::TAU, false),
+ arc(&mut path, 0.0, 0.0, 10.0, 0.0, core::f64::consts::TAU, false),
Ok(())
);
let [
Op::MoveTo(start_x, start_y),
Op::EllipticalArcTo {
- large_arc,
- sweep,
+ x: opposite_x,
+ large_arc: first_large_arc,
+ sweep: first_sweep,
radii,
..
},
+ Op::EllipticalArcTo {
+ x: end_x,
+ y: end_y,
+ large_arc: second_large_arc,
+ sweep: second_sweep,
+ ..
+ },
] = path.ops.as_slice()
else {
- panic!("the arc is one elliptical arc from its start: {:?}", path.ops);
+ panic!("the arc is two elliptical arcs from its start: {:?}", path.ops);
};
assert_eq!((*start_x, *start_y), (10.0, 0.0));
+ assert_eq!(*opposite_x, -10.0);
+ assert_eq!((*end_x, *end_y), (10.0, 0.0));
assert_eq!(*radii, (10.0, 10.0));
- assert!(*large_arc && *sweep);
+ assert!(!*first_large_arc && *first_sweep && !*second_large_arc && *second_sweep);
}
#[test]
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/path2d.rs b/Libraries/LibWeb/Bindings/Rust/src/html/path2d.rs
index 9eb63676be6..fdc8f940911 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/path2d.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/path2d.rs
@@ -234,8 +234,8 @@ impl Path2D {
float(x),
float(y),
float(radius),
- float(start_angle),
- float(end_angle),
+ start_angle,
+ end_angle,
counterclockwise,
)
})?;
@@ -263,8 +263,8 @@ impl Path2D {
float(radius_x),
float(radius_y),
float(rotation),
- float(start_angle),
- float(end_angle),
+ start_angle,
+ end_angle,
counterclockwise,
)
})?;
diff --git a/Tests/LibWeb/Screenshot/expected/canvas-arcs-and-ellipses.png b/Tests/LibWeb/Screenshot/expected/canvas-arcs-and-ellipses.png
index 5e93a3431cd..a43f0c6ceac 100644
Binary files a/Tests/LibWeb/Screenshot/expected/canvas-arcs-and-ellipses.png and b/Tests/LibWeb/Screenshot/expected/canvas-arcs-and-ellipses.png differ
diff --git a/Tests/LibWeb/Screenshot/expected/canvas-filters.png b/Tests/LibWeb/Screenshot/expected/canvas-filters.png
index 68891348767..a835baa666f 100644
Binary files a/Tests/LibWeb/Screenshot/expected/canvas-filters.png and b/Tests/LibWeb/Screenshot/expected/canvas-filters.png differ
diff --git a/Tests/LibWeb/Screenshot/expected/canvas-implict-moves-and-lines.png b/Tests/LibWeb/Screenshot/expected/canvas-implict-moves-and-lines.png
index 9c52431c8d2..be9e065fbad 100644
Binary files a/Tests/LibWeb/Screenshot/expected/canvas-implict-moves-and-lines.png and b/Tests/LibWeb/Screenshot/expected/canvas-implict-moves-and-lines.png differ
diff --git a/Tests/LibWeb/Screenshot/expected/canvas-stroke-styles.png b/Tests/LibWeb/Screenshot/expected/canvas-stroke-styles.png
index 42c0bd5713b..6b0787d6cbf 100644
Binary files a/Tests/LibWeb/Screenshot/expected/canvas-stroke-styles.png and b/Tests/LibWeb/Screenshot/expected/canvas-stroke-styles.png differ
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/path-objects/2d.path.arc.twopie.5.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/path-objects/2d.path.arc.twopie.5.txt
index 6291d061312..14d6d707ca3 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/path-objects/2d.path.arc.twopie.5.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/path-objects/2d.path.arc.twopie.5.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail arc() draws correctly when start = 2 and end = start + 2pi+e and clockwise
\ No newline at end of file
+1 Pass
+Pass arc() draws correctly when start = 2 and end = start + 2pi+e and clockwise
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.arc.twopie.5.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.arc.twopie.5.txt
index 6291d061312..14d6d707ca3 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.arc.twopie.5.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.arc.twopie.5.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail arc() draws correctly when start = 2 and end = start + 2pi+e and clockwise
\ No newline at end of file
+1 Pass
+Pass arc() draws correctly when start = 2 and end = start + 2pi+e and clockwise
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.arc.twopie.5.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.arc.twopie.5.worker.txt
index 6291d061312..14d6d707ca3 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.arc.twopie.5.worker.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/path-objects/2d.path.arc.twopie.5.worker.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail arc() draws correctly when start = 2 and end = start + 2pi+e and clockwise
\ No newline at end of file
+1 Pass
+Pass arc() draws correctly when start = 2 and end = start + 2pi+e and clockwise
\ No newline at end of file