Skip to content

Truly independent web browser

LibWeb: Implement the operations of a canvas path in Rust

Andreas Klinga3657a7e4958
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_path.rs b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_path.rs
new file mode 100644
index 00000000000..62f0bac38c0
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_path.rs
@@ -0,0 +1,1041 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! The operations of the CanvasPath mixin, https://html.spec.whatwg.org/multipage/canvas.html#canvaspath, which
+//! Path2D and the 2D contexts share. They build on a Gfx::Path, with its rules for a path without a current point.
+//!
+//! The arithmetic is that of the C++ it replaces, in the same precision: the arguments of moveTo(), lineTo(),
+//! quadraticCurveTo(), arc() and ellipse() are floats, and those of the rest are doubles that become floats where
+//! they become points.
+
+use core::fmt::LowerExp;
+
+use libgfx_rust::{AffineTransform, FloatPoint, FloatSize};
+
+/// What the operations build on, which is a Gfx::Path, or what the crate tests record.
+pub(crate) trait PathSink {
+ /// Whether the path has no current point.
+ fn is_empty(&self) -> bool;
+ fn last_point(&self) -> FloatPoint;
+ fn move_to(&mut self, point: FloatPoint);
+ fn line_to(&mut self, point: FloatPoint);
+ fn quadratic_bezier_curve_to(&mut self, through: FloatPoint, point: FloatPoint);
+ fn cubic_bezier_curve_to(&mut self, c1: FloatPoint, c2: FloatPoint, point: FloatPoint);
+ fn elliptical_arc_to(
+ &mut self,
+ point: FloatPoint,
+ radii: FloatSize,
+ x_axis_rotation: f32,
+ large_arc: bool,
+ sweep: bool,
+ );
+ fn arc_to(&mut self, point: FloatPoint, radius: f32, large_arc: bool, sweep: bool);
+ fn close(&mut self);
+}
+
+#[cfg(not(test))]
+impl PathSink for libgfx_rust::path::PathMut<'_> {
+ fn is_empty(&self) -> bool {
+ self.is_empty()
+ }
+
+ fn last_point(&self) -> FloatPoint {
+ self.last_point()
+ }
+
+ fn move_to(&mut self, point: FloatPoint) {
+ self.move_to(point);
+ }
+
+ fn line_to(&mut self, point: FloatPoint) {
+ self.line_to(point);
+ }
+
+ fn quadratic_bezier_curve_to(&mut self, through: FloatPoint, point: FloatPoint) {
+ self.quadratic_bezier_curve_to(through, point);
+ }
+
+ fn cubic_bezier_curve_to(&mut self, c1: FloatPoint, c2: FloatPoint, point: FloatPoint) {
+ self.cubic_bezier_curve_to(c1, c2, point);
+ }
+
+ fn elliptical_arc_to(
+ &mut self,
+ point: FloatPoint,
+ radii: FloatSize,
+ x_axis_rotation: f32,
+ large_arc: bool,
+ sweep: bool,
+ ) {
+ self.elliptical_arc_to(point, radii, x_axis_rotation, large_arc, sweep);
+ }
+
+ fn arc_to(&mut self, point: FloatPoint, radius: f32, large_arc: bool, sweep: bool) {
+ self.arc_to(point, radius, large_arc, sweep);
+ }
+
+ fn close(&mut self) {
+ self.close();
+ }
+}
+
+/// What an operation throws.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub(crate) enum CanvasPathError {
+ /// The radius of arc(), an IndexSizeError.
+ NegativeArcRadius(f32),
+ /// The radius of arcTo(), an IndexSizeError.
+ NegativeArcToRadius(f64),
+ /// The radiusX of ellipse(), an IndexSizeError.
+ NegativeMajorAxisRadius(f32),
+ /// The radiusY of ellipse(), an IndexSizeError.
+ NegativeMinorAxisRadius(f32),
+ /// More than four radii, or none, for roundRect(), a RangeError.
+ RoundRectRadiusCount,
+ /// A negative radius for roundRect(), a RangeError.
+ NegativeRoundRectRadius,
+}
+
+impl CanvasPathError {
+ /// Whether the exception is a RangeError rather than an "IndexSizeError" DOMException.
+ pub(crate) fn is_range_error(self) -> bool {
+ matches!(self, Self::RoundRectRadiusCount | Self::NegativeRoundRectRadius)
+ }
+
+ pub(crate) fn message(self) -> String {
+ match self {
+ Self::NegativeArcRadius(radius) => format!("The radius provided ({}) is negative.", ak_formatted(radius)),
+ Self::NegativeArcToRadius(radius) => format!("The radius provided ({}) is negative.", ak_formatted(radius)),
+ Self::NegativeMajorAxisRadius(radius) => {
+ format!("The major-axis radius provided ({}) is negative.", ak_formatted(radius))
+ }
+ Self::NegativeMinorAxisRadius(radius) => {
+ format!("The minor-axis radius provided ({}) is negative.", ak_formatted(radius))
+ }
+ Self::RoundRectRadiusCount => "roundRect: Can have between 1 and 4 radii".to_owned(),
+ Self::NegativeRoundRectRadius => "roundRect: Radius can't be negative".to_owned(),
+ }
+ }
+}
+
+#[cfg(not(test))]
+impl From<CanvasPathError> for crate::bindings::Exception {
+ fn from(error: CanvasPathError) -> Self {
+ let message = ak::Utf16String::from_utf8(&error.message());
+ if error.is_range_error() {
+ Self::RangeError(message)
+ } else {
+ Self::index_size_error(message)
+ }
+ }
+}
+
+/// The number as AK's formatter writes it for "{}", which the messages of the C++ had: the shortest digits that
+/// read back as the number, in the notation of Number::toString() of ECMAScript.
+fn ak_formatted(number: impl LowerExp) -> String {
+ let scientific = format!("{number:e}");
+ let (sign, scientific) = match scientific.strip_prefix('-') {
+ Some(magnitude) => ("-", magnitude),
+ None => ("", scientific.as_str()),
+ };
+ let (mantissa, exponent) = scientific
+ .split_once('e')
+ .expect("a number in scientific notation has an exponent");
+ let exponent: i32 = exponent.parse().expect("the exponent is an integer");
+ let digits: String = mantissa.chars().filter(|character| *character != '.').collect();
+ let digit_count = i32::try_from(digits.len()).expect("a number has few digits");
+ let zeros = |count: i32| "0".repeat(usize::try_from(count).expect("the count is not negative"));
+
+ let unsigned = if !(-6..=20).contains(&exponent) {
+ let (first, rest) = digits.split_at(1);
+ let fraction = if rest.is_empty() {
+ String::new()
+ } else {
+ format!(".{rest}")
+ };
+ let exponent_sign = if exponent < 0 { '-' } else { '+' };
+ format!("{first}{fraction}e{exponent_sign}{}", exponent.unsigned_abs())
+ } else if exponent < 0 {
+ format!("0.{}{digits}", zeros(-exponent - 1))
+ } else if digit_count <= exponent + 1 {
+ format!("{digits}{}", zeros(exponent + 1 - digit_count))
+ } else {
+ let (integer, fraction) = digits.split_at(usize::try_from(exponent + 1).expect("the exponent is not negative"));
+ format!("{integer}.{fraction}")
+ };
+ format!("{sign}{unsigned}")
+}
+
+fn point(x: f32, y: f32) -> FloatPoint {
+ FloatPoint { x, y }
+}
+
+#[allow(
+ clippy::cast_possible_truncation,
+ reason = "a point of a Gfx::Path is in floats, as the C++ made it"
+)]
+fn point_from_doubles(x: f64, y: f64) -> FloatPoint {
+ point(x as f32, y as f32)
+}
+
+fn ensure_subpath(path: &mut impl PathSink, x: f32, y: f32) {
+ if path.is_empty() {
+ path.move_to(point(x, y));
+ }
+}
+
+fn add_line_to_start_point(path: &mut impl PathSink, start_point: FloatPoint) {
+ if path.is_empty() {
+ path.move_to(start_point);
+ } else {
+ path.line_to(start_point);
+ }
+}
+
+/// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-closepath
+pub(crate) fn close_path(path: &mut impl PathSink) {
+ // The closePath() method, when invoked, must do nothing if the object's path has no subpaths. Otherwise, it must
+ // mark the last subpath as closed, create a new subpath whose first point is the same as the previous subpath's
+ // first point, and finally add this new subpath to the path.
+ if path.is_empty() {
+ return;
+ }
+ path.close();
+}
+
+/// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-moveto
+pub(crate) fn move_to(path: &mut impl PathSink, x: f32, y: f32) {
+ // 1. If either of the arguments are infinite or NaN, then return.
+ if !x.is_finite() || !y.is_finite() {
+ return;
+ }
+
+ // 2. Create a new subpath with the specified point as its first (and only) point.
+ path.move_to(point(x, y));
+}
+
+/// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-lineto
+pub(crate) fn line_to(path: &mut impl PathSink, x: f32, y: f32) {
+ // 1. If either of the arguments are infinite or NaN, then return.
+ if !x.is_finite() || !y.is_finite() {
+ return;
+ }
+
+ if path.is_empty() {
+ // 2. If the object's path has no subpaths, then ensure there is a subpath for (x, y).
+ ensure_subpath(path, x, y);
+ } else {
+ // 3. Otherwise, connect the last point in the subpath to the given point (x, y) using a straight line, and
+ // then add the given point (x, y) to the subpath.
+ path.line_to(point(x, y));
+ }
+}
+
+/// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-quadraticcurveto
+pub(crate) fn quadratic_curve_to(path: &mut impl PathSink, cpx: f32, cpy: f32, x: f32, y: f32) {
+ // 1. If any of the arguments are infinite or NaN, then return.
+ if !cpx.is_finite() || !cpy.is_finite() || !x.is_finite() || !y.is_finite() {
+ return;
+ }
+
+ // 2. Ensure there is a subpath for (cpx, cpy).
+ ensure_subpath(path, cpx, cpy);
+
+ // 3. Connect the last point in the subpath to the given point (x, y) using a quadratic Bézier curve with control
+ // point (cpx, cpy).
+ // 4. Add the given point (x, y) to the subpath.
+ path.quadratic_bezier_curve_to(point(cpx, cpy), point(x, y));
+}
+
+/// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-beziercurveto
+pub(crate) fn bezier_curve_to(path: &mut impl PathSink, cp1x: f64, cp1y: f64, cp2x: f64, cp2y: f64, x: f64, y: f64) {
+ // 1. If any of the arguments are infinite or NaN, then return.
+ if [cp1x, cp1y, cp2x, cp2y, x, y]
+ .iter()
+ .any(|argument| !argument.is_finite())
+ {
+ return;
+ }
+
+ // 2. Ensure there is a subpath for (cp1x, cp1y).
+ let control_point_1 = point_from_doubles(cp1x, cp1y);
+ ensure_subpath(path, control_point_1.x, control_point_1.y);
+
+ // 3. Connect the last point in the subpath to the given point (x, y) using a cubic Bézier curve with control
+ // points (cp1x, cp1y) and (cp2x, cp2y).
+ // 4. Add the point (x, y) to the subpath.
+ path.cubic_bezier_curve_to(
+ control_point_1,
+ point_from_doubles(cp2x, cp2y),
+ point_from_doubles(x, y),
+ );
+}
+
+/// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-arc
+#[allow(clippy::too_many_arguments)]
+pub(crate) fn arc(
+ path: &mut impl PathSink,
+ x: f32,
+ y: f32,
+ radius: f32,
+ start_angle: f32,
+ end_angle: f32,
+ counterclockwise: bool,
+) -> Result<(), CanvasPathError> {
+ if radius < 0.0 && radius.is_finite() {
+ return Err(CanvasPathError::NegativeArcRadius(radius));
+ }
+ ellipse(
+ path,
+ x,
+ y,
+ radius,
+ radius,
+ 0.0,
+ start_angle,
+ end_angle,
+ counterclockwise,
+ )
+}
+
+/// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-ellipse
+#[allow(clippy::too_many_arguments)]
+pub(crate) fn ellipse(
+ path: &mut impl PathSink,
+ x: f32,
+ y: f32,
+ radius_x: f32,
+ radius_y: f32,
+ rotation: f32,
+ mut start_angle: f32,
+ mut end_angle: f32,
+ mut 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]
+ .iter()
+ .any(|argument| !argument.is_finite())
+ {
+ return Ok(());
+ }
+
+ // 2. If either radiusX or radiusY are negative, then throw an "IndexSizeError" DOMException.
+ if radius_x < 0.0 {
+ return Err(CanvasPathError::NegativeMajorAxisRadius(radius_x));
+ }
+ if radius_y < 0.0 {
+ return Err(CanvasPathError::NegativeMinorAxisRadius(radius_y));
+ }
+
+ if radius_x == 0.0 || radius_y == 0.0 {
+ add_line_to_start_point(path, point(x, y));
+ return Ok(());
+ }
+
+ // "If counterclockwise is false and endAngle − startAngle is greater than or equal to 2π, or, if counterclockwise
+ // 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.
+ 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;
+ }
+
+ // 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
+ // rotated `rotation` radians around the x axis, centered on `center`. The sign of the resulting relative
+ // positions is just whether our angle is on one of the left quadrants.
+ // NB: The C++ took the sine and the cosine of the rotation with the x87 instruction, in extended precision. Taken
+ // in double precision, they round to the same floats but in rare cases.
+ let (sin_rotation, cos_rotation) = f64::from(rotation).sin_cos();
+ #[allow(
+ clippy::cast_possible_truncation,
+ reason = "the C++ kept the sine and the cosine in floats"
+ )]
+ let (sin_rotation, cos_rotation) = (sin_rotation as f32, cos_rotation as f32);
+
+ let resolve_point_with_angle = |angle: f32| {
+ let tan_relative = angle.tan();
+ let tan2 = tan_relative * tan_relative;
+
+ let ab = radius_x * radius_y;
+ let a2 = radius_x * radius_x;
+ let b2 = radius_y * radius_y;
+ let sqrt = (b2 + a2 * tan2).sqrt();
+
+ let mut relative_x_position = ab / sqrt;
+ let mut relative_y_position = ab * tan_relative / sqrt;
+
+ // Make sure to set the correct sign
+ // -1 if 0 ≤ θ < 90° or 270°< θ ≤ 360°
+ // 1 if 90° < θ< 270°
+ let sn = if angle.cos() >= 0.0 { 1.0 } else { -1.0 };
+ relative_x_position *= sn;
+ relative_y_position *= sn;
+
+ // Now rotate it (back) around the center point by 'rotation' radians, then move it back to our actual origin.
+ let relative_rotated_x_position = relative_x_position * cos_rotation - relative_y_position * sin_rotation;
+ let relative_rotated_y_position = relative_x_position * sin_rotation + relative_y_position * cos_rotation;
+ point(relative_rotated_x_position + x, relative_rotated_y_position + y)
+ };
+
+ let start_point = resolve_point_with_angle(start_angle);
+ let end_point = resolve_point_with_angle(end_angle);
+
+ let mut delta_theta = if counterclockwise {
+ start_angle - end_angle
+ } else {
+ end_angle - start_angle
+ };
+
+ if delta_theta < 0.0 {
+ delta_theta += core::f32::consts::PI * 2.0;
+ }
+
+ // 3. If canvasPath's path has any subpaths, then add a straight line from the last point in the subpath to the
+ // start point of the arc.
+ add_line_to_start_point(path, start_point);
+
+ // 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,
+ },
+ rotation,
+ delta_theta > core::f32::consts::PI,
+ !counterclockwise,
+ );
+
+ Ok(())
+}
+
+/// 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,
+ y2: f64,
+ radius: f64,
+) -> Result<(), CanvasPathError> {
+ // 1. If any of the arguments are infinite or NaN, then return.
+ if [x1, y1, x2, y2, radius].iter().any(|argument| !argument.is_finite()) {
+ return Ok(());
+ }
+
+ // Point (x1, y1)
+ let p1 = point_from_doubles(x1, y1);
+ // Point (x2, y2)
+ let p2 = point_from_doubles(x2, y2);
+
+ // 2. Ensure there is a subpath for (x1, y1).
+ ensure_subpath(path, p1.x, p1.y);
+
+ // 3. If radius is negative, then throw an "IndexSizeError" DOMException.
+ if radius < 0.0 {
+ return Err(CanvasPathError::NegativeArcToRadius(radius));
+ }
+
+ // 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).
+ // Point (x0, y0)
+ let p0 = transform.inverse().unwrap_or_default().map_point(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
+ // previous point (x0, y0) by a straight line.
+ if p0 == p1 || p1 == p2 || radius == 0.0 {
+ path.line_to(p1);
+ return Ok(());
+ }
+
+ let v1 = (p0.x - p1.x, p0.y - p1.y);
+ let v2 = (p2.x - p1.x, p2.y - p1.y);
+ let length = |vector: (f32, f32)| (vector.0 * vector.0 + vector.1 * vector.1).sqrt();
+ let cos_theta = (v1.0 * v2.0 + v1.1 * v2.1) / (length(v1) * length(v2));
+ // 6. Otherwise, if the points (x0, y0), (x1, y1), and (x2, y2) all lie on a single straight line, then add the
+ // point (x1, y1) to the subpath, and connect that point to the previous point (x0, y0) by a straight line.
+ if cos_theta == -1.0 || cos_theta == 1.0 {
+ path.line_to(p1);
+ return Ok(());
+ }
+
+ // 7. Otherwise, let The Arc be the shortest arc given by circumference of the circle that has radius radius, and
+ // that has one point tangent to the half-infinite line that crosses the point (x0, y0) and ends at the point
+ // (x1, y1), and that has a different point tangent to the half-infinite line that ends at the point (x1, y1)
+ // and crosses the point (x2, y2). The points at which this circle touches these two lines are called the start
+ // and end tangent points respectively.
+ let adjacent = radius / f64::from((cos_theta.acos() / 2.0).tan());
+ let factor1 = adjacent / f64::from(length(v1));
+ let x3 = f64::from(p1.x) + factor1 * f64::from(p0.x - p1.x);
+ let y3 = f64::from(p1.y) + factor1 * f64::from(p0.y - p1.y);
+ let start_tangent = point_from_doubles(x3, y3);
+
+ let factor2 = adjacent / f64::from(length(v2));
+ let x4 = f64::from(p1.x) + factor2 * f64::from(p2.x - p1.x);
+ let y4 = f64::from(p1.y) + factor2 * f64::from(p2.y - p1.y);
+ let end_tangent = point_from_doubles(x4, y4);
+
+ // Connect the point (x0, y0) to the start tangent point by a straight line, adding the start tangent point to the
+ // subpath.
+ path.line_to(start_tangent);
+
+ // NB: The arc is always small, since the tangent points are its end points and the lines meet at (x1, y1).
+ let large_arc = false;
+ let cross_product = v1.0 * v2.1 - v1.1 * v2.0;
+ // NB: By the right-hand rule, true means clockwise.
+ let sweep = cross_product < 0.0;
+
+ // and then connect the start tangent point to the end tangent point by The Arc, adding the end tangent point to
+ // the subpath.
+ #[allow(
+ clippy::cast_possible_truncation,
+ reason = "the radius of an arc of a Gfx::Path is a float"
+ )]
+ path.arc_to(end_tangent, radius as f32, large_arc, sweep);
+ Ok(())
+}
+
+/// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-rect
+pub(crate) fn rect(path: &mut impl PathSink, x: f64, y: f64, w: f64, h: f64) {
+ // 1. If any of the arguments are infinite or NaN, then return.
+ if [x, y, w, h].iter().any(|argument| !argument.is_finite()) {
+ return;
+ }
+
+ // 2. Create a new subpath containing just the four points (x, y), (x+w, y), (x+w, y+h), (x, y+h), in that order,
+ // with those four points connected by straight lines.
+ path.move_to(point_from_doubles(x, y));
+ path.line_to(point_from_doubles(x + w, y));
+ path.line_to(point_from_doubles(x + w, y + h));
+ path.line_to(point_from_doubles(x, y + h));
+
+ // 3. Mark the subpath as closed.
+ path.close();
+
+ // 4. Create a new subpath with the point (x, y) as the only point in the subpath.
+ path.move_to(point_from_doubles(x, y));
+}
+
+/// A radius of roundRect(): both of a DOMPointInit, or a number twice. `CanvasRoundRectRadius` of
+/// Libraries/LibWeb/HTML/Canvas/CanvasPath.h mirrors it.
+#[derive(Clone, Copy, Debug, Default, PartialEq)]
+#[repr(C)]
+pub(crate) struct RoundRectRadius {
+ pub(crate) x: f64,
+ pub(crate) y: f64,
+}
+
+/// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-roundrect, with the radii as a list.
+pub(crate) fn round_rect(
+ path: &mut impl PathSink,
+ x: f64,
+ y: f64,
+ w: f64,
+ h: f64,
+ radii: &[RoundRectRadius],
+) -> Result<(), CanvasPathError> {
+ // 1. If any of x, y, w, or h are infinite or NaN, then return.
+ if [x, y, w, h].iter().any(|argument| !argument.is_finite()) {
+ return Ok(());
+ }
+
+ // 2. If radii is an unrestricted double or DOMPointInit, then set radii to « radii ».
+ // NB: The caller passes a list.
+
+ // 3. If radii is not a list of size one, two, three, or four, then throw a RangeError.
+ if radii.is_empty() || radii.len() > 4 {
+ return Err(CanvasPathError::RoundRectRadiusCount);
+ }
+
+ // 4. Let normalizedRadii be an empty list.
+ let mut normalized_radii = Vec::with_capacity(radii.len());
+
+ // 5. For each radius of radii:
+ for radius in radii {
+ // 5.1.1. If radius["x"] or radius["y"] is infinite or NaN, then return.
+ if !radius.x.is_finite() || !radius.y.is_finite() {
+ return Ok(());
+ }
+
+ // 5.1.2. If radius["x"] or radius["y"] is negative, then throw a RangeError.
+ if radius.x < 0.0 || radius.y < 0.0 {
+ return Err(CanvasPathError::NegativeRoundRectRadius);
+ }
+
+ // 5.1.3. Otherwise, append radius to normalizedRadii.
+ normalized_radii.push(*radius);
+
+ // 5.2. If radius is a unrestricted double:
+ // NB: The caller passes a number as a radius with x and y.
+ }
+
+ // 6. Let upperLeft, upperRight, lowerRight, and lowerLeft be null.
+ // 7. If normalizedRadii's size is 4, then set upperLeft to normalizedRadii[0], set upperRight to
+ // normalizedRadii[1], set lowerRight to normalizedRadii[2], and set lowerLeft to normalizedRadii[3].
+ // 8. If normalizedRadii's size is 3, then set upperLeft to normalizedRadii[0], set upperRight and lowerLeft to
+ // normalizedRadii[1], and set lowerRight to normalizedRadii[2].
+ // 9. If normalizedRadii's size is 2, then set upperLeft and lowerRight to normalizedRadii[0] and set upperRight
+ // and lowerLeft to normalizedRadii[1].
+ // 10. If normalizedRadii's size is 1, then set upperLeft, upperRight, lowerRight, and lowerLeft to
+ // normalizedRadii[0].
+ let (mut upper_left, mut upper_right, mut lower_right, mut lower_left) = match *normalized_radii.as_slice() {
+ [upper_left, upper_right, lower_right, lower_left] => (upper_left, upper_right, lower_right, lower_left),
+ [upper_left, upper_right_and_lower_left, lower_right] => (
+ upper_left,
+ upper_right_and_lower_left,
+ lower_right,
+ upper_right_and_lower_left,
+ ),
+ [upper_left_and_lower_right, upper_right_and_lower_left] => (
+ upper_left_and_lower_right,
+ upper_right_and_lower_left,
+ upper_left_and_lower_right,
+ upper_right_and_lower_left,
+ ),
+ [all] => (all, all, all, all),
+ _ => unreachable!("there are one to four radii"),
+ };
+
+ // 11. Corner curves must not overlap. Scale all radii to prevent this:
+ // 11.1. Let top be upperLeft["x"] + upperRight["x"].
+ let top = upper_left.x + upper_right.x;
+
+ // 11.2. Let right be upperRight["y"] + lowerRight["y"].
+ let right = upper_right.y + lower_right.y;
+
+ // 11.3. Let bottom be lowerRight["x"] + lowerLeft["x"].
+ let bottom = lower_right.x + lower_left.x;
+
+ // 11.4. Let left be upperLeft["y"] + lowerLeft["y"].
+ let left = upper_left.y + lower_left.y;
+
+ // 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.
+ 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));
+
+ // 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.
+ if scale < 1.0 {
+ for radius in [&mut upper_left, &mut upper_right, &mut lower_left, &mut lower_right] {
+ radius.x *= scale;
+ radius.y *= scale;
+ }
+ }
+
+ // 12. Create a new subpath:
+ let large_arc = false;
+ let sweep = true;
+ #[allow(
+ clippy::cast_possible_truncation,
+ reason = "the radii of an arc of a Gfx::Path are floats"
+ )]
+ let radii_of = |radius: RoundRectRadius| FloatSize {
+ width: radius.x as f32,
+ height: radius.y as f32,
+ };
+
+ // 12.1. Move to the point (x + upperLeft["x"], y).
+ path.move_to(point_from_doubles(x + 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));
+
+ // 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),
+ radii_of(upper_right),
+ 0.0,
+ large_arc,
+ sweep,
+ );
+
+ // 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));
+
+ // 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),
+ radii_of(lower_right),
+ 0.0,
+ large_arc,
+ sweep,
+ );
+
+ // 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));
+
+ // 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),
+ radii_of(lower_left),
+ 0.0,
+ large_arc,
+ sweep,
+ );
+
+ // 12.8. Draw a straight line to the point (x, y + upperLeft["y"]).
+ path.line_to(point_from_doubles(x, y + 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),
+ radii_of(upper_left),
+ 0.0,
+ large_arc,
+ sweep,
+ );
+
+ // 13. Mark the subpath as closed.
+ path.close();
+
+ // 14. Create a new subpath with the point (x, y) as the only point in the subpath.
+ path.move_to(point_from_doubles(x, y));
+ Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::{
+ CanvasPathError, PathSink, RoundRectRadius, ak_formatted, arc, arc_to, close_path, ellipse, line_to, move_to,
+ rect, round_rect,
+ };
+ use libgfx_rust::{AffineTransform, FloatPoint, FloatSize};
+
+ #[derive(Debug, PartialEq)]
+ enum Op {
+ MoveTo(f32, f32),
+ LineTo(f32, f32),
+ Quadratic,
+ Cubic,
+ EllipticalArcTo {
+ x: f32,
+ y: f32,
+ radii: (f32, f32),
+ large_arc: bool,
+ sweep: bool,
+ },
+ ArcTo {
+ x: f32,
+ y: f32,
+ radius: f32,
+ sweep: bool,
+ },
+ Close,
+ }
+
+ /// The operations that a path was given, with the rules of a Gfx::Path for its current point.
+ #[derive(Default)]
+ struct RecordingPath {
+ ops: Vec<Op>,
+ last_point: Option<FloatPoint>,
+ subpath_start: FloatPoint,
+ }
+
+ impl PathSink for RecordingPath {
+ fn is_empty(&self) -> bool {
+ self.last_point.is_none()
+ }
+
+ fn last_point(&self) -> FloatPoint {
+ self.last_point.unwrap_or_default()
+ }
+
+ fn move_to(&mut self, point: FloatPoint) {
+ self.ops.push(Op::MoveTo(point.x, point.y));
+ self.last_point = Some(point);
+ self.subpath_start = point;
+ }
+
+ fn line_to(&mut self, point: FloatPoint) {
+ if self.is_empty() {
+ return self.move_to(point);
+ }
+ self.ops.push(Op::LineTo(point.x, point.y));
+ self.last_point = Some(point);
+ }
+
+ fn quadratic_bezier_curve_to(&mut self, _: FloatPoint, point: FloatPoint) {
+ self.ops.push(Op::Quadratic);
+ self.last_point = Some(point);
+ }
+
+ fn cubic_bezier_curve_to(&mut self, _: FloatPoint, _: FloatPoint, point: FloatPoint) {
+ self.ops.push(Op::Cubic);
+ self.last_point = Some(point);
+ }
+
+ fn elliptical_arc_to(&mut self, point: FloatPoint, radii: FloatSize, _: f32, large_arc: bool, sweep: bool) {
+ self.ops.push(Op::EllipticalArcTo {
+ x: point.x,
+ y: point.y,
+ radii: (radii.width, radii.height),
+ large_arc,
+ sweep,
+ });
+ self.last_point = Some(point);
+ }
+
+ fn arc_to(&mut self, point: FloatPoint, radius: f32, _: bool, sweep: bool) {
+ self.ops.push(Op::ArcTo {
+ x: point.x,
+ y: point.y,
+ radius,
+ sweep,
+ });
+ self.last_point = Some(point);
+ }
+
+ fn close(&mut self) {
+ if self.is_empty() {
+ return;
+ }
+ self.ops.push(Op::Close);
+ self.last_point = Some(self.subpath_start);
+ }
+ }
+
+ #[test]
+ fn numbers_are_formatted_as_ak_formats_them() {
+ // NB: What AK's "{}" wrote for these numbers, which a C++ program printed.
+ let floats: [(f32, &str); 14] = [
+ (-1.0, "-1"),
+ (-0.5, "-0.5"),
+ (-0.1, "-0.1"),
+ (-1e30, "-1e+30"),
+ (-1e-7, "-1e-7"),
+ (-123_456_789.0, "-123456790"),
+ (-3.402_823_5e38, "-3.4028235e+38"),
+ (-1.175_494_4e-38, "-1.1754944e-38"),
+ (-0.333_333_3, "-0.3333333"),
+ (-16_777_217.0, "-16777216"),
+ (-1e7, "-10000000"),
+ (-1e16, "-10000000000000000"),
+ (-100_000.0, "-100000"),
+ (-0.0001, "-0.0001"),
+ ];
+ for (number, formatted) in floats {
+ assert_eq!(ak_formatted(number), formatted);
+ }
+ let doubles: [(f64, &str); 10] = [
+ (-1.0, "-1"),
+ (-0.1, "-0.1"),
+ (-1e300, "-1e+300"),
+ (-1e-7, "-1e-7"),
+ (-123_456_789.123, "-123456789.123"),
+ (-1e21, "-1e+21"),
+ (-1e22, "-1e+22"),
+ (-0.0001, "-0.0001"),
+ (-1e16, "-10000000000000000"),
+ (-1e17, "-100000000000000000"),
+ ];
+ for (number, formatted) in doubles {
+ assert_eq!(ak_formatted(number), formatted);
+ }
+ }
+
+ #[test]
+ fn points_that_are_not_finite_are_ignored() {
+ let mut path = RecordingPath::default();
+ move_to(&mut path, f32::NAN, 0.0);
+ line_to(&mut path, 0.0, f32::INFINITY);
+ close_path(&mut path);
+ rect(&mut path, 0.0, 0.0, f64::NAN, 1.0);
+ assert!(path.ops.is_empty());
+ }
+
+ #[test]
+ fn a_line_without_a_subpath_starts_one() {
+ let mut path = RecordingPath::default();
+ line_to(&mut path, 1.0, 2.0);
+ line_to(&mut path, 3.0, 4.0);
+ assert_eq!(path.ops, [Op::MoveTo(1.0, 2.0), Op::LineTo(3.0, 4.0)]);
+ }
+
+ #[test]
+ fn a_rect_is_a_closed_subpath_followed_by_a_new_one() {
+ let mut path = RecordingPath::default();
+ rect(&mut path, 1.0, 2.0, 3.0, 4.0);
+ assert_eq!(
+ path.ops,
+ [
+ Op::MoveTo(1.0, 2.0),
+ Op::LineTo(4.0, 2.0),
+ Op::LineTo(4.0, 6.0),
+ Op::LineTo(1.0, 6.0),
+ Op::Close,
+ Op::MoveTo(1.0, 2.0),
+ ]
+ );
+ }
+
+ #[test]
+ fn negative_radii_throw() {
+ let mut path = RecordingPath::default();
+ assert_eq!(
+ arc(&mut path, 0.0, 0.0, -1.0, 0.0, 1.0, false),
+ Err(CanvasPathError::NegativeArcRadius(-1.0))
+ );
+ assert_eq!(arc(&mut path, 0.0, 0.0, f32::NEG_INFINITY, 0.0, 1.0, false), Ok(()));
+ assert_eq!(
+ ellipse(&mut path, 0.0, 0.0, 1.0, -2.0, 0.0, 0.0, 1.0, false),
+ Err(CanvasPathError::NegativeMinorAxisRadius(-2.0))
+ );
+ assert_eq!(
+ arc_to(&mut path, AffineTransform::identity(), 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.
+ assert_eq!(path.ops, [Op::MoveTo(1.0, 1.0)]);
+ assert_eq!(
+ CanvasPathError::NegativeArcToRadius(-0.5).message(),
+ "The radius provided (-0.5) is negative."
+ );
+ }
+
+ #[test]
+ fn an_ellipse_with_a_zero_radius_is_a_line_to_its_center() {
+ let mut path = RecordingPath::default();
+ move_to(&mut path, 1.0, 1.0);
+ assert_eq!(ellipse(&mut path, 5.0, 6.0, 0.0, 2.0, 0.0, 0.0, 1.0, false), Ok(()));
+ assert_eq!(path.ops, [Op::MoveTo(1.0, 1.0), Op::LineTo(5.0, 6.0)]);
+ }
+
+ #[test]
+ 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),
+ Ok(())
+ );
+ let [
+ Op::MoveTo(start_x, start_y),
+ Op::EllipticalArcTo {
+ large_arc,
+ sweep,
+ radii,
+ ..
+ },
+ ] = path.ops.as_slice()
+ else {
+ panic!("the arc is one elliptical arc from its start: {:?}", path.ops);
+ };
+ assert_eq!((*start_x, *start_y), (10.0, 0.0));
+ assert_eq!(*radii, (10.0, 10.0));
+ assert!(*large_arc && *sweep);
+ }
+
+ #[test]
+ 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!(
+ path.ops,
+ [Op::MoveTo(0.0, 0.0), Op::LineTo(1.0, 0.0), Op::LineTo(3.0, 3.0)]
+ );
+ }
+
+ #[test]
+ 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(())
+ );
+ 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);
+ };
+ assert!((x3 - 5.0).abs() < 1e-5 && *y3 == 0.0);
+ assert!((*x - 10.0).abs() < 1e-5 && (y - 5.0).abs() < 1e-5);
+ assert!(*sweep);
+ assert!((radius - 5.0).abs() < 1e-5);
+ }
+
+ #[test]
+ fn round_rect_takes_one_to_four_radii() {
+ let radius = RoundRectRadius { x: 1.0, y: 1.0 };
+ let mut path = RecordingPath::default();
+ assert_eq!(
+ round_rect(&mut path, 0.0, 0.0, 10.0, 10.0, &[]),
+ Err(CanvasPathError::RoundRectRadiusCount)
+ );
+ assert_eq!(
+ round_rect(&mut path, 0.0, 0.0, 10.0, 10.0, &[radius; 5]),
+ Err(CanvasPathError::RoundRectRadiusCount)
+ );
+ assert_eq!(
+ round_rect(
+ &mut path,
+ 0.0,
+ 0.0,
+ 10.0,
+ 10.0,
+ &[radius, RoundRectRadius { x: -1.0, y: 0.0 }]
+ ),
+ Err(CanvasPathError::NegativeRoundRectRadius)
+ );
+ assert_eq!(
+ round_rect(
+ &mut path,
+ 0.0,
+ 0.0,
+ 10.0,
+ 10.0,
+ &[radius, RoundRectRadius { x: f64::NAN, y: -1.0 }]
+ ),
+ Ok(())
+ );
+ assert!(path.ops.is_empty());
+ }
+
+ #[test]
+ fn round_rect_assigns_three_radii_to_the_corners_and_scales_them_down() {
+ let mut path = RecordingPath::default();
+ let radii = [
+ RoundRectRadius { x: 1.0, y: 1.0 },
+ RoundRectRadius { x: 2.0, y: 2.0 },
+ RoundRectRadius { x: 30.0, y: 30.0 },
+ ];
+ assert_eq!(round_rect(&mut path, 0.0, 0.0, 16.0, 16.0, &radii), Ok(()));
+ // NB: The upper right and the lower right corners are 32 tall together, so every radius is halved.
+ let arc_radii: Vec<_> = path
+ .ops
+ .iter()
+ .filter_map(|op| match op {
+ Op::EllipticalArcTo { radii, .. } => Some(*radii),
+ _ => None,
+ })
+ .collect();
+ assert_eq!(arc_radii, [(1.0, 1.0), (15.0, 15.0), (1.0, 1.0), (0.5, 0.5)]);
+ assert_eq!(path.ops.first(), Some(&Op::MoveTo(0.5, 0.0)));
+ assert_eq!(path.ops.last(), Some(&Op::MoveTo(0.0, 0.0)));
+ }
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs b/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs
index bd5eb44521c..7af403f7b18 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs
@@ -10,13 +10,17 @@
use core::ffi::c_void;
+use ak::Utf16String;
use libcompositing_rust::display_list::canvas_2d::Paint;
use libcompositing_rust::display_list::canvas_2d_recorder::FfiCanvas2DPaint;
use libcompositing_rust::display_list::commands::{Canvas2DPaintKind, Canvas2DPatternRepetition};
+use libgfx_rust::AffineTransform;
use libgfx_rust::image_frame::ImageFrameHandle;
+use libgfx_rust::path::PathMut;
use libjs_rust::api::Vm;
use super::canvas_gradient::CanvasGradient;
+use super::canvas_path::{self, CanvasPathError, RoundRectRadius};
use super::canvas_pattern::CanvasPattern;
use super::text_metrics::{TextMeasurements, TextMetrics};
use crate::bindings::interface::{Interface, Ref, cell_from_abi, cell_into_abi};
@@ -209,3 +213,195 @@ pub unsafe extern "C" fn web_canvas_pattern_lend_paint(pattern: *mut c_void, out
out_paint.pattern_transform = transform;
}
}
+
+/// What an operation of a canvas path threw, which `CanvasPathResult` of Libraries/LibWeb/HTML/Canvas/CanvasPath.cpp
+/// mirrors.
+#[derive(Clone, Copy)]
+#[repr(u8)]
+pub enum CanvasPathResultKind {
+ Ok,
+ IndexSizeError,
+ RangeError,
+}
+
+/// The result of an operation of a canvas path, with the message of its exception, an AK::Utf16String that the
+/// caller takes over, unless the operation threw none.
+#[repr(C)]
+pub struct CanvasPathResult {
+ kind: CanvasPathResultKind,
+ message: usize,
+}
+
+impl From<Result<(), CanvasPathError>> for CanvasPathResult {
+ fn from(result: Result<(), CanvasPathError>) -> Self {
+ match result {
+ Ok(()) => Self {
+ kind: CanvasPathResultKind::Ok,
+ message: 0,
+ },
+ Err(error) => Self {
+ kind: if error.is_range_error() {
+ CanvasPathResultKind::RangeError
+ } else {
+ CanvasPathResultKind::IndexSizeError
+ },
+ message: Utf16String::from_utf8(&error.message()).into_raw(),
+ },
+ }
+ }
+}
+
+// NB: `path` of every operation below is a Gfx::Path that the caller lends mutably for the call.
+
+/// # Safety
+///
+/// `path` must be a live Gfx::Path.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_canvas_path_close_path(path: *mut c_void) {
+ // SAFETY: The caller lends a live path.
+ canvas_path::close_path(&mut unsafe { PathMut::from_raw(path) });
+}
+
+/// # Safety
+///
+/// `path` must be a live Gfx::Path.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_canvas_path_move_to(path: *mut c_void, x: f32, y: f32) {
+ // SAFETY: The caller lends a live path.
+ canvas_path::move_to(&mut unsafe { PathMut::from_raw(path) }, x, y);
+}
+
+/// # Safety
+///
+/// `path` must be a live Gfx::Path.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_canvas_path_line_to(path: *mut c_void, x: f32, y: f32) {
+ // SAFETY: The caller lends a live path.
+ canvas_path::line_to(&mut unsafe { PathMut::from_raw(path) }, x, y);
+}
+
+/// # Safety
+///
+/// `path` must be a live Gfx::Path.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_canvas_path_quadratic_curve_to(path: *mut c_void, cpx: f32, cpy: f32, x: f32, y: f32) {
+ // SAFETY: The caller lends a live path.
+ canvas_path::quadratic_curve_to(&mut unsafe { PathMut::from_raw(path) }, cpx, cpy, x, y);
+}
+
+/// # Safety
+///
+/// `path` must be a live Gfx::Path.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_canvas_path_bezier_curve_to(
+ path: *mut c_void,
+ cp1x: f64,
+ cp1y: f64,
+ cp2x: f64,
+ cp2y: f64,
+ x: f64,
+ y: f64,
+) {
+ // SAFETY: The caller lends a live path.
+ canvas_path::bezier_curve_to(&mut unsafe { PathMut::from_raw(path) }, cp1x, cp1y, cp2x, cp2y, x, y);
+}
+
+/// # Safety
+///
+/// `path` must be a live Gfx::Path.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_canvas_path_arc_to(
+ path: *mut c_void,
+ transform: &AffineTransform,
+ x1: f64,
+ y1: f64,
+ x2: f64,
+ y2: f64,
+ radius: f64,
+) -> CanvasPathResult {
+ // SAFETY: The caller lends a live path.
+ canvas_path::arc_to(
+ &mut unsafe { PathMut::from_raw(path) },
+ *transform,
+ x1,
+ y1,
+ x2,
+ y2,
+ radius,
+ )
+ .into()
+}
+
+/// # Safety
+///
+/// `path` must be a live Gfx::Path.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_canvas_path_rect(path: *mut c_void, x: f64, y: f64, w: f64, h: f64) {
+ // SAFETY: The caller lends a live path.
+ canvas_path::rect(&mut unsafe { PathMut::from_raw(path) }, x, y, w, h);
+}
+
+/// # Safety
+///
+/// `path` must be a live Gfx::Path.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_canvas_path_round_rect(
+ path: *mut c_void,
+ x: f64,
+ y: f64,
+ w: f64,
+ h: f64,
+ radii: ak::Span<'_, RoundRectRadius>,
+) -> CanvasPathResult {
+ // SAFETY: The caller lends a live path.
+ canvas_path::round_rect(&mut unsafe { PathMut::from_raw(path) }, x, y, w, h, radii.as_slice()).into()
+}
+
+/// # Safety
+///
+/// `path` must be a live Gfx::Path.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_canvas_path_arc(
+ path: *mut c_void,
+ x: f32,
+ y: f32,
+ radius: f32,
+ start_angle: f32,
+ end_angle: f32,
+ counterclockwise: bool,
+) -> CanvasPathResult {
+ // SAFETY: The caller lends a live path.
+ let mut path = unsafe { PathMut::from_raw(path) };
+ canvas_path::arc(&mut path, x, y, radius, start_angle, end_angle, counterclockwise).into()
+}
+
+/// # Safety
+///
+/// `path` must be a live Gfx::Path.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_canvas_path_ellipse(
+ path: *mut c_void,
+ x: f32,
+ y: f32,
+ radius_x: f32,
+ radius_y: f32,
+ rotation: f32,
+ start_angle: f32,
+ end_angle: f32,
+ counterclockwise: bool,
+) -> CanvasPathResult {
+ // SAFETY: The caller lends a live path.
+ let mut path = unsafe { PathMut::from_raw(path) };
+ canvas_path::ellipse(
+ &mut path,
+ x,
+ y,
+ radius_x,
+ radius_y,
+ rotation,
+ start_angle,
+ end_angle,
+ counterclockwise,
+ )
+ .into()
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/html/mod.rs
index 8f908106ebd..3eeae4b7590 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/mod.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/mod.rs
@@ -7,6 +7,7 @@
//! The objects that the 2D canvas contexts create and draw with. The contexts are still LibWeb's C++, which creates
//! and reads these objects through the C ABI of `exports`.
+pub(crate) mod canvas_path;
pub(crate) mod color_stops;
// What follows runs on the JS runtime, which the tests of the crate do not link.
diff --git a/Libraries/LibWeb/HTML/Canvas/CanvasPath.cpp b/Libraries/LibWeb/HTML/Canvas/CanvasPath.cpp
index e2e8006ad85..fcdbfc738ca 100644
--- a/Libraries/LibWeb/HTML/Canvas/CanvasPath.cpp
+++ b/Libraries/LibWeb/HTML/Canvas/CanvasPath.cpp
@@ -5,11 +5,58 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <LibGfx/Vector2.h>
#include <LibWeb/HTML/Canvas/CanvasPath.h>
namespace Web::HTML {
+namespace {
+
+// `CanvasPathResultKind` and `CanvasPathResult` of Bindings/Rust/src/html/exports.rs.
+enum class CanvasPathResultKind : u8 {
+ Ok,
+ IndexSizeError,
+ RangeError,
+};
+
+struct CanvasPathResult {
+ CanvasPathResultKind kind;
+ // An AK::Utf16String that the caller takes over, unless the kind is Ok.
+ FlatPtr message;
+};
+
+}
+
+// NB: `RoundRectRadius` of Bindings/Rust/src/html/canvas_path.rs is two doubles in this order.
+static_assert(sizeof(CanvasRoundRectRadius) == 2 * sizeof(double));
+static_assert(offsetof(CanvasRoundRectRadius, y) == sizeof(double));
+
+// Defined in Bindings/Rust/src/html/exports.rs. The path is lent mutably for the call.
+extern "C" {
+void web_canvas_path_close_path(Gfx::Path*);
+void web_canvas_path_move_to(Gfx::Path*, float x, float y);
+void web_canvas_path_line_to(Gfx::Path*, float x, float y);
+void web_canvas_path_quadratic_curve_to(Gfx::Path*, float cpx, float cpy, float x, float y);
+void web_canvas_path_bezier_curve_to(Gfx::Path*, double cp1x, double cp1y, double cp2x, double cp2y, double x, double y);
+CanvasPathResult web_canvas_path_arc_to(Gfx::Path*, Gfx::AffineTransform const*, double x1, double y1, double x2, double y2, double radius);
+void web_canvas_path_rect(Gfx::Path*, double x, double y, double w, double h);
+CanvasPathResult web_canvas_path_round_rect(Gfx::Path*, double x, double y, double w, double h, ReadonlySpan<CanvasRoundRectRadius> radii);
+CanvasPathResult web_canvas_path_arc(Gfx::Path*, float x, float y, float radius, float start_angle, float end_angle, bool counter_clockwise);
+CanvasPathResult web_canvas_path_ellipse(Gfx::Path*, float x, float y, float radius_x, float radius_y, float rotation, float start_angle, float end_angle, bool counter_clockwise);
+}
+
+static WebIDL::ExceptionOr<void> to_exception_or(CanvasPathResult result)
+{
+ switch (result.kind) {
+ case CanvasPathResultKind::Ok:
+ return {};
+ case CanvasPathResultKind::IndexSizeError:
+ return WebIDL::IndexSizeError::create(Utf16String::adopt_raw(result.message));
+ case CanvasPathResultKind::RangeError:
+ return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, Utf16String::adopt_raw(result.message) };
+ }
+ VERIFY_NOT_REACHED();
+}
+
Gfx::AffineTransform CanvasPath::active_transform() const
{
if (m_canvas_state.has_value())
@@ -17,282 +64,59 @@ Gfx::AffineTransform CanvasPath::active_transform() const
return {};
}
-void CanvasPath::ensure_subpath(float x, float y)
-{
- if (m_path.is_empty())
- m_path.move_to(Gfx::FloatPoint { x, y });
-}
-
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-closepath
void CanvasPath::close_path()
{
- // The closePath() method, when invoked, must do nothing if the object's path has no subpaths. Otherwise, it must
- // mark the last subpath as closed, create a new subpath whose first point is the same as the previous subpath's
- // first point, and finally add this new subpath to the path.
- if (m_path.is_empty())
- return;
- m_path.close();
+ web_canvas_path_close_path(&m_path);
}
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-moveto
void CanvasPath::move_to(float x, float y)
{
- // 1. If either of the arguments are infinite or NaN, then return.
- if (!isfinite(x) || !isfinite(y))
- return;
-
- // 2. Create a new subpath with the specified point as its first (and only) point.
- m_path.move_to(Gfx::FloatPoint { x, y });
+ web_canvas_path_move_to(&m_path, x, y);
}
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-lineto
void CanvasPath::line_to(float x, float y)
{
- // 1. If either of the arguments are infinite or NaN, then return.
- if (!isfinite(x) || !isfinite(y))
- return;
-
- if (m_path.is_empty()) {
- // 2. If the object's path has no subpaths, then ensure there is a subpath for (x, y).
- ensure_subpath(x, y);
- } else {
- // 3. Otherwise, connect the last point in the subpath to the given point (x, y) using a straight line,
- // and then add the given point (x, y) to the subpath.
- m_path.line_to(Gfx::FloatPoint { x, y });
- }
+ web_canvas_path_line_to(&m_path, x, y);
}
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-quadraticcurveto
void CanvasPath::quadratic_curve_to(float cpx, float cpy, float x, float y)
{
- // 1. If any of the arguments are infinite or NaN, then return.
- if (!isfinite(cpx) || !isfinite(cpy) || !isfinite(x) || !isfinite(y))
- return;
-
- // 2. Ensure there is a subpath for (cpx, cpy).
- ensure_subpath(cpx, cpy);
-
- // 3. Connect the last point in the subpath to the given point (x, y) using a quadratic Bézier curve with control point (cpx, cpy).
- // 4. Add the given point (x, y) to the subpath.
- m_path.quadratic_bezier_curve_to(Gfx::FloatPoint { cpx, cpy }, Gfx::FloatPoint { x, y });
+ web_canvas_path_quadratic_curve_to(&m_path, cpx, cpy, x, y);
}
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-beziercurveto
void CanvasPath::bezier_curve_to(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y)
{
- // 1. If any of the arguments are infinite or NaN, then return.
- if (!isfinite(cp1x) || !isfinite(cp1y) || !isfinite(cp2x) || !isfinite(cp2y) || !isfinite(x) || !isfinite(y))
- return;
-
- // 2. Ensure there is a subpath for (cp1x, cp1y).
- ensure_subpath(cp1x, cp1y);
-
- // 3. Connect the last point in the subpath to the given point (x, y) using a cubic Bézier curve with control points (cp1x, cp1y) and (cp2x, cp2y).
- // 4. Add the point (x, y) to the subpath.
- m_path.cubic_bezier_curve_to(
- Gfx::FloatPoint { cp1x, cp1y }, Gfx::FloatPoint { cp2x, cp2y }, Gfx::FloatPoint { x, y });
+ web_canvas_path_bezier_curve_to(&m_path, cp1x, cp1y, cp2x, cp2y, x, y);
}
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-arc
WebIDL::ExceptionOr<void> CanvasPath::arc(float x, float y, float radius, float start_angle, float end_angle, bool counter_clockwise)
{
- if (radius < 0 && isfinite(radius))
- return WebIDL::IndexSizeError::create(Utf16String::formatted("The radius provided ({}) is negative.", radius));
- return ellipse(x, y, radius, radius, 0, start_angle, end_angle, counter_clockwise);
+ return to_exception_or(web_canvas_path_arc(&m_path, x, y, radius, start_angle, end_angle, counter_clockwise));
}
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-ellipse
WebIDL::ExceptionOr<void> CanvasPath::ellipse(float x, float y, float radius_x, float radius_y, float rotation, float start_angle, float end_angle, bool counter_clockwise)
{
- // 1. If any of the arguments are infinite or NaN, then return.
- if (!isfinite(x) || !isfinite(y) || !isfinite(radius_x) || !isfinite(radius_y) || !isfinite(rotation) || !isfinite(start_angle) || !isfinite(end_angle))
- return {};
-
- // 2. If either radiusX or radiusY are negative, then throw an "IndexSizeError" DOMException.
- if (radius_x < 0)
- return WebIDL::IndexSizeError::create(Utf16String::formatted("The major-axis radius provided ({}) is negative.", radius_x));
- if (radius_y < 0)
- return WebIDL::IndexSizeError::create(Utf16String::formatted("The minor-axis radius provided ({}) is negative.", radius_y));
-
- auto add_line_to_start_point = [this](Gfx::FloatPoint const& start_point) {
- if (!m_path.is_empty())
- m_path.line_to(start_point);
- else
- m_path.move_to(start_point);
- };
-
- if (radius_x == 0 || radius_y == 0) {
- add_line_to_start_point(Gfx::FloatPoint { x, y });
- return {};
- }
-
- // "If counterclockwise is false and endAngle − startAngle is greater than or equal to 2π,
- // or, if counterclockwise 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.
- if (constexpr float tau = M_PI * 2; (!counter_clockwise && (end_angle - start_angle) >= tau)
- || (counter_clockwise && (start_angle - end_angle) >= tau)
- || (start_angle != end_angle && fmodf(start_angle - end_angle, tau) == 0)) {
- start_angle = 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.9999f;
- counter_clockwise = false;
- } else {
- start_angle = fmodf(start_angle, tau);
- end_angle = fmodf(end_angle, 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 rotated `rotation` radians around the x axis, centered on `center`.
- // The sign of the resulting relative positions is just whether our angle is on one of the left quadrants.
- float sin_rotation;
- float cos_rotation;
- AK::sincos(rotation, sin_rotation, cos_rotation);
-
- auto resolve_point_with_angle = [&](float angle) {
- auto tan_relative = tanf(angle);
- auto tan2 = tan_relative * tan_relative;
-
- auto ab = radius_x * radius_y;
- auto a2 = radius_x * radius_x;
- auto b2 = radius_y * radius_y;
- auto sqrt = sqrtf(b2 + a2 * tan2);
-
- auto relative_x_position = ab / sqrt;
- auto relative_y_position = ab * tan_relative / sqrt;
-
- // Make sure to set the correct sign
- // -1 if 0 ≤ θ < 90° or 270°< θ ≤ 360°
- // 1 if 90° < θ< 270°
- float sn = cosf(angle) >= 0 ? 1 : -1;
- relative_x_position *= sn;
- relative_y_position *= sn;
-
- // Now rotate it (back) around the center point by 'rotation' radians, then move it back to our actual origin.
- auto relative_rotated_x_position = relative_x_position * cos_rotation - relative_y_position * sin_rotation;
- auto relative_rotated_y_position = relative_x_position * sin_rotation + relative_y_position * cos_rotation;
- return Gfx::FloatPoint { relative_rotated_x_position + x, relative_rotated_y_position + y };
- };
-
- auto start_point = resolve_point_with_angle(start_angle);
- auto end_point = resolve_point_with_angle(end_angle);
-
- float delta_theta;
- if (counter_clockwise) {
- delta_theta = start_angle - end_angle;
- } else {
- delta_theta = end_angle - start_angle;
- }
-
- if (delta_theta < 0)
- delta_theta += AK::Pi<float> * 2;
-
- // 3. If canvasPath's path has any subpaths, then add a straight line from the last point in the subpath to the start point of the arc.
- add_line_to_start_point(start_point);
-
- // 4. Add the start and end points of the arc to the subpath, and connect them with an arc.
- m_path.elliptical_arc_to(
- Gfx::FloatPoint { end_point },
- Gfx::FloatSize { radius_x, radius_y },
- rotation,
- delta_theta > AK::Pi<float>, !counter_clockwise);
-
- return {};
+ return to_exception_or(web_canvas_path_ellipse(&m_path, x, y, radius_x, radius_y, rotation, start_angle, end_angle, counter_clockwise));
}
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-arcto
WebIDL::ExceptionOr<void> CanvasPath::arc_to(double x1, double y1, double x2, double y2, double radius)
{
- // 1. If any of the arguments are infinite or NaN, then return.
- if (!isfinite(x1) || !isfinite(y1) || !isfinite(x2) || !isfinite(y2) || !isfinite(radius))
- return {};
-
- // 2. Ensure there is a subpath for (x1, y1).
- ensure_subpath(x1, y1);
-
- // 3. If radius is negative, then throw an "IndexSizeError" DOMException.
- if (radius < 0)
- return WebIDL::IndexSizeError::create(Utf16String::formatted("The radius provided ({}) is negative.", radius));
-
auto transform = active_transform();
-
- // 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).
- // Point (x0, y0)
- auto p0 = transform.inverse().value_or(Gfx::AffineTransform()).map(m_path.last_point());
- // Point (x1, y1)
- auto p1 = Gfx::FloatPoint { x1, y1 };
- // Point (x2, y2)
- auto p2 = Gfx::FloatPoint { x2, y2 };
-
- // 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 previous point (x0, y0) by a straight line.
- if (p0 == p1 || p1 == p2 || radius == 0) {
- m_path.line_to(p1);
- return {};
- }
-
- auto v1 = Gfx::FloatVector2 { p0.x() - p1.x(), p0.y() - p1.y() };
- auto v2 = Gfx::FloatVector2 { p2.x() - p1.x(), p2.y() - p1.y() };
- auto cos_theta = v1.dot(v2) / (v1.length() * v2.length());
- // 6. Otherwise, if the points (x0, y0), (x1, y1), and (x2, y2) all lie on a single straight line,
- // then add the point (x1, y1) to the subpath,
- // and connect that point to the previous point (x0, y0) by a straight line.
- if (-1 == cos_theta || 1 == cos_theta) {
- m_path.line_to(p1);
- return {};
- }
-
- // 7. Otherwise, let The Arc be the shortest arc given by circumference of the circle that has radius radius,
- // and that has one point tangent to the half-infinite line that crosses the point (x0, y0) and ends at the point (x1, y1),
- // and that has a different point tangent to the half-infinite line that ends at the point (x1, y1) and crosses the point (x2, y2).
- // The points at which this circle touches these two lines are called the start and end tangent points respectively.
- auto adjacent = radius / static_cast<double>(tan(acos(cos_theta) / 2));
- auto factor1 = adjacent / static_cast<double>(v1.length());
- auto x3 = static_cast<double>(p1.x()) + factor1 * static_cast<double>(p0.x() - p1.x());
- auto y3 = static_cast<double>(p1.y()) + factor1 * static_cast<double>(p0.y() - p1.y());
- auto start_tangent = Gfx::FloatPoint { x3, y3 };
-
- auto factor2 = adjacent / static_cast<double>(v2.length());
- auto x4 = static_cast<double>(p1.x()) + factor2 * static_cast<double>(p2.x() - p1.x());
- auto y4 = static_cast<double>(p1.y()) + factor2 * static_cast<double>(p2.y() - p1.y());
- auto end_tangent = Gfx::FloatPoint { x4, y4 };
-
- // Connect the point (x0, y0) to the start tangent point by a straight line, adding the start tangent point to the subpath.
- m_path.line_to(start_tangent);
-
- bool const large_arc = false; // always small since tangent points define arc endpoints and lines meet at (x1, y1)
- auto cross_product = v1.x() * v2.y() - v1.y() * v2.x();
- bool const sweep = cross_product < 0; // right-hand rule, true means clockwise
-
- // and then connect the start tangent point to the end tangent point by The Arc, adding the end tangent point to the subpath.
- m_path.arc_to(end_tangent, radius, large_arc, sweep);
- return {};
+ return to_exception_or(web_canvas_path_arc_to(&m_path, &transform, x1, y1, x2, y2, radius));
}
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-rect
void CanvasPath::rect(double x, double y, double w, double h)
{
- // 1. If any of the arguments are infinite or NaN, then return.
- if (!isfinite(x) || !isfinite(y) || !isfinite(w) || !isfinite(h))
- return;
-
- // 2. Create a new subpath containing just the four points (x, y), (x+w, y), (x+w, y+h), (x, y+h), in that order, with those four points connected by straight lines.
- m_path.move_to(Gfx::FloatPoint { x, y });
- m_path.line_to(Gfx::FloatPoint { x + w, y });
- m_path.line_to(Gfx::FloatPoint { x + w, y + h });
- m_path.line_to(Gfx::FloatPoint { x, y + h });
-
- // 3. Mark the subpath as closed.
- m_path.close();
-
- // 4. Create a new subpath with the point (x, y) as the only point in the subpath.
- m_path.move_to(Gfx::FloatPoint { x, y });
+ web_canvas_path_rect(&m_path, x, y, w, h);
}
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-roundrect
@@ -327,133 +151,7 @@ WebIDL::ExceptionOr<void> CanvasPath::round_rect(double x, double y, double w, d
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-roundrect
WebIDL::ExceptionOr<void> CanvasPath::round_rect(double x, double y, double w, double h, ReadonlySpan<CanvasRoundRectRadius> radii)
{
- // 1. If any of x, y, w, or h are infinite or NaN, then return.
- if (!isfinite(x) || !isfinite(y) || !isfinite(w) || !isfinite(h))
- return {};
-
- // 2. If radii is an unrestricted double or DOMPointInit, then set radii to « radii ».
- // NB: This is handled by the binding helper.
-
- // 3. If radii is not a list of size one, two, three, or four, then throw a RangeError.
- if (radii.is_empty() || radii.size() > 4)
- return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, "roundRect: Can have between 1 and 4 radii"_utf16 };
-
- // 4. Let normalizedRadii be an empty list.
- Vector<CanvasRoundRectRadius> normalized_radii;
-
- // 5. For each radius of radii:
- for (auto const& radius : radii) {
- // 5.1.1. If radius["x"] or radius["y"] is infinite or NaN, then return.
- if (!isfinite(radius.x) || !isfinite(radius.y))
- return {};
-
- // 5.1.2. If radius["x"] or radius["y"] is negative, then throw a RangeError.
- if (radius.x < 0 || radius.y < 0)
- return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, "roundRect: Radius can't be negative"_utf16 };
-
- // 5.1.3. Otherwise, append radius to normalizedRadii.
- normalized_radii.append(radius);
-
- // 5.2. If radius is a unrestricted double:
- // NB: This is handled by the binding helper.
- }
-
- // 6. Let upperLeft, upperRight, lowerRight, and lowerLeft be null.
- CanvasRoundRectRadius upper_left {};
- CanvasRoundRectRadius upper_right {};
- CanvasRoundRectRadius lower_right {};
- CanvasRoundRectRadius lower_left {};
-
- // 7. If normalizedRadii's size is 4, then set upperLeft to normalizedRadii[0], set upperRight to normalizedRadii[1], set lowerRight to normalizedRadii[2], and set lowerLeft to normalizedRadii[3].
- if (normalized_radii.size() == 4) {
- upper_left = normalized_radii.at(0);
- upper_right = normalized_radii.at(1);
- lower_right = normalized_radii.at(2);
- lower_left = normalized_radii.at(3);
- }
-
- // 8. If normalizedRadii's size is 3, then set upperLeft to normalizedRadii[0], set upperRight and lowerLeft to normalizedRadii[1], and set lowerRight to normalizedRadii[2].
- if (normalized_radii.size() == 3) {
- upper_left = normalized_radii.at(0);
- upper_right = lower_left = normalized_radii.at(1);
- lower_right = normalized_radii.at(2);
- }
-
- // 9. If normalizedRadii's size is 2, then set upperLeft and lowerRight to normalizedRadii[0] and set upperRight and lowerLeft to normalizedRadii[1].
- if (normalized_radii.size() == 2) {
- upper_left = lower_right = normalized_radii.at(0);
- upper_right = lower_left = normalized_radii.at(1);
- }
-
- // 10. If normalizedRadii's size is 1, then set upperLeft, upperRight, lowerRight, and lowerLeft to normalizedRadii[0].
- if (normalized_radii.size() == 1)
- upper_left = upper_right = lower_right = lower_left = normalized_radii.at(0);
-
- // 11. Corner curves must not overlap. Scale all radii to prevent this:
- // 11.1. Let top be upperLeft["x"] + upperRight["x"].
- double top = upper_left.x + upper_right.x;
-
- // 11.2. Let right be upperRight["y"] + lowerRight["y"].
- double right = upper_right.y + lower_right.y;
-
- // 11.3. Let bottom be lowerRight["x"] + lowerLeft["x"].
- double bottom = lower_right.x + lower_left.x;
-
- // 11.4. Let left be upperLeft["y"] + lowerLeft["y"].
- double left = upper_left.y + lower_left.y;
-
- // 11.5. Let scale be the minimum value of the ratios w / top, h / right, w / bottom, h / left.
- double scale = AK::min(AK::min(w / top, h / right), AK::min(w / bottom, h / 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.
- if (scale < 1) {
- upper_left.x *= scale;
- upper_left.y *= scale;
- upper_right.x *= scale;
- upper_right.y *= scale;
- lower_left.x *= scale;
- lower_left.y *= scale;
- lower_right.x *= scale;
- lower_right.y *= scale;
- }
-
- // 12. Create a new subpath:
- bool large_arc = false;
- bool sweep = true;
-
- // 12.1. Move to the point (x + upperLeft["x"], y).
- m_path.move_to(Gfx::FloatPoint { x + upper_left.x, y });
-
- // 12.2. Draw a straight line to the point (x + w − upperRight["x"], y).
- m_path.line_to(Gfx::FloatPoint { x + w - upper_right.x, y });
-
- // 12.3. Draw an arc to the point (x + w, y + upperRight["y"]).
- m_path.elliptical_arc_to(Gfx::FloatPoint { x + w, y + upper_right.y }, { upper_right.x, upper_right.y }, 0, large_arc, sweep);
-
- // 12.4. Draw a straight line to the point (x + w, y + h − lowerRight["y"]).
- m_path.line_to(Gfx::FloatPoint { x + w, y + h - lower_right.y });
-
- // 12.5. Draw an arc to the point (x + w − lowerRight["x"], y + h).
- m_path.elliptical_arc_to(Gfx::FloatPoint { x + w - lower_right.x, y + h }, { lower_right.x, lower_right.y }, 0, large_arc, sweep);
-
- // 12.6. Draw a straight line to the point (x + lowerLeft["x"], y + h).
- m_path.line_to(Gfx::FloatPoint { x + lower_left.x, y + h });
-
- // 12.7. Draw an arc to the point (x, y + h − lowerLeft["y"]).
- m_path.elliptical_arc_to(Gfx::FloatPoint { x, y + h - lower_left.y }, { lower_left.x, lower_left.y }, 0, large_arc, sweep);
-
- // 12.8. Draw a straight line to the point (x, y + upperLeft["y"]).
- m_path.line_to(Gfx::FloatPoint { x, y + upper_left.y });
-
- // 12.9. Draw an arc to the point (x + upperLeft["x"], y).
- m_path.elliptical_arc_to(Gfx::FloatPoint { x + upper_left.x, y }, { upper_left.x, upper_left.y }, 0, large_arc, sweep);
-
- // 13. Mark the subpath as closed.
- m_path.close();
-
- // 14. Create a new subpath with the point (x, y) as the only point in the subpath.
- m_path.move_to(Gfx::FloatPoint { x, y });
- return {};
+ return to_exception_or(web_canvas_path_round_rect(&m_path, x, y, w, h, radii));
}
}
diff --git a/Libraries/LibWeb/HTML/Canvas/CanvasPath.h b/Libraries/LibWeb/HTML/Canvas/CanvasPath.h
index 579337e6088..ac9a714f314 100644
--- a/Libraries/LibWeb/HTML/Canvas/CanvasPath.h
+++ b/Libraries/LibWeb/HTML/Canvas/CanvasPath.h
@@ -17,12 +17,14 @@
namespace Web::HTML {
+// A radius of roundRect(), which `RoundRectRadius` of Bindings/Rust/src/html/canvas_path.rs mirrors.
struct CanvasRoundRectRadius {
double x { 0 };
double y { 0 };
};
// https://html.spec.whatwg.org/multipage/canvas.html#canvaspath
+// The path of a 2D context, which the operations of the mixin in Bindings/Rust/src/html/canvas_path.rs build on.
class CanvasPath {
public:
virtual ~CanvasPath() = default;
@@ -54,8 +56,6 @@ protected:
private:
Gfx::AffineTransform active_transform() const;
- void ensure_subpath(float x, float y);
-
Optional<CanvasState const&> m_canvas_state;
Gfx::Path m_path;
};
diff --git a/Tests/LibWeb/Text/expected/canvas/path-operation-errors.txt b/Tests/LibWeb/Text/expected/canvas/path-operation-errors.txt
new file mode 100644
index 00000000000..a6c9af63537
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/canvas/path-operation-errors.txt
@@ -0,0 +1,18 @@
+arc(-1): DOMException IndexSizeError: The radius provided (-1) is negative.
+arc(-0.1): DOMException IndexSizeError: The radius provided (-0.1) is negative.
+arc(-1e30): DOMException IndexSizeError: The radius provided (-1e+30) is negative.
+arc(-1e-7): DOMException IndexSizeError: The radius provided (-1e-7) is negative.
+arc(-Infinity): no exception
+arc(-1e39): no exception
+ellipse(-2, 1): DOMException IndexSizeError: The major-axis radius provided (-2) is negative.
+ellipse(1, -2.5): DOMException IndexSizeError: The minor-axis radius provided (-2.5) is negative.
+ellipse(NaN, -2): no exception
+arcTo(-0.1): DOMException IndexSizeError: The radius provided (-0.1) is negative.
+arcTo(-1e300): DOMException IndexSizeError: The radius provided (-1e+300) is negative.
+arcTo(NaN point, -1): no exception
+roundRect([]): RangeError: roundRect: Can have between 1 and 4 radii
+roundRect([1, 2, 3, 4, 5]): RangeError: roundRect: Can have between 1 and 4 radii
+roundRect(-1): RangeError: roundRect: Radius can't be negative
+roundRect({ x: 1, y: -1 }): RangeError: roundRect: Radius can't be negative
+roundRect([NaN, -1]): no exception
+roundRect(NaN size, []): no exception
diff --git a/Tests/LibWeb/Text/input/canvas/path-operation-errors.html b/Tests/LibWeb/Text/input/canvas/path-operation-errors.html
new file mode 100644
index 00000000000..1864f99db3d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/canvas/path-operation-errors.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const context = document.createElement("canvas").getContext("2d");
+ function tryOperation(description, operation) {
+ try {
+ operation();
+ println(`${description}: no exception`);
+ } catch (e) {
+ const kind = e instanceof DOMException ? `DOMException ${e.name}` : e.constructor.name;
+ println(`${description}: ${kind}: ${e.message}`);
+ }
+ }
+ tryOperation("arc(-1)", () => context.arc(0, 0, -1, 0, 1));
+ tryOperation("arc(-0.1)", () => context.arc(0, 0, -0.1, 0, 1));
+ tryOperation("arc(-1e30)", () => context.arc(0, 0, -1e30, 0, 1));
+ tryOperation("arc(-1e-7)", () => context.arc(0, 0, -1e-7, 0, 1));
+ tryOperation("arc(-Infinity)", () => context.arc(0, 0, -Infinity, 0, 1));
+ tryOperation("arc(-1e39)", () => context.arc(0, 0, -1e39, 0, 1));
+ tryOperation("ellipse(-2, 1)", () => context.ellipse(0, 0, -2, 1, 0, 0, 1));
+ tryOperation("ellipse(1, -2.5)", () => context.ellipse(0, 0, 1, -2.5, 0, 0, 1));
+ tryOperation("ellipse(NaN, -2)", () => context.ellipse(0, 0, NaN, -2, 0, 0, 1));
+ tryOperation("arcTo(-0.1)", () => context.arcTo(0, 0, 1, 1, -0.1));
+ tryOperation("arcTo(-1e300)", () => context.arcTo(0, 0, 1, 1, -1e300));
+ tryOperation("arcTo(NaN point, -1)", () => context.arcTo(NaN, 0, 1, 1, -1));
+ tryOperation("roundRect([])", () => context.roundRect(0, 0, 10, 10, []));
+ tryOperation("roundRect([1, 2, 3, 4, 5])", () => context.roundRect(0, 0, 10, 10, [1, 2, 3, 4, 5]));
+ tryOperation("roundRect(-1)", () => context.roundRect(0, 0, 10, 10, -1));
+ tryOperation("roundRect({ x: 1, y: -1 })", () => context.roundRect(0, 0, 10, 10, { x: 1, y: -1 }));
+ tryOperation("roundRect([NaN, -1])", () => context.roundRect(0, 0, 10, 10, [NaN, -1]));
+ tryOperation("roundRect(NaN size, [])", () => context.roundRect(NaN, 0, 10, 10, []));
+ });
+</script>