Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(canvas): correct arc geometry and bound angle normalization

ldm0a05b346c5b79
diff --git a/Cargo.lock b/Cargo.lock
index 70122773a..82903b196 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2868,6 +2868,7 @@ dependencies = [
"http",
"httpdate",
"indexmap",
+ "kurbo",
"markup5ever",
"moli-action-window",
"moli-broadcast-channel",
diff --git a/moli-renderer-v8/Cargo.toml b/moli-renderer-v8/Cargo.toml
index 5adce2400..a65a234ed 100644
--- a/moli-renderer-v8/Cargo.toml
+++ b/moli-renderer-v8/Cargo.toml
@@ -24,6 +24,7 @@ data-url = "0.3.2"
dom = { package = "stylo_dom", version = "0.20" }
encoding_rs = "0.8"
euclid = "0.22"
+kurbo = "0.13.1"
futures-util = "0.3.31"
precomputed-hash = "0.1"
percent-encoding = "2.3"
diff --git a/moli-renderer-v8/src/context_bootstrap/canvas/path.rs b/moli-renderer-v8/src/context_bootstrap/canvas/path.rs
index eb9748af0..dbf84e160 100644
--- a/moli-renderer-v8/src/context_bootstrap/canvas/path.rs
+++ b/moli-renderer-v8/src/context_bootstrap/canvas/path.rs
@@ -87,8 +87,7 @@ impl Canvas2dPathState {
self.just_closed = false;
}
- /// Returns `false` when the path has no subpath, matching the spec: line
- /// and curve commands must do nothing when the path has no subpaths.
+ /// Reopens a closed subpath; callers establish the initial point for an empty path.
fn ensure_open_subpath(&mut self) -> bool {
if !self.has_subpath {
return false;
@@ -106,6 +105,7 @@ impl Canvas2dPathState {
pub(super) fn line_to(&mut self, x: f64, y: f64) {
if !self.ensure_open_subpath() {
+ self.move_to(x, y);
return;
}
self.elements.push(PaintPathElement::LineTo(point(x, y)));
@@ -114,7 +114,7 @@ impl Canvas2dPathState {
pub(super) fn quadratic_curve_to(&mut self, cpx: f64, cpy: f64, x: f64, y: f64) {
if !self.ensure_open_subpath() {
- return;
+ self.move_to(cpx, cpy);
}
self.elements
.push(PaintPathElement::QuadTo(point(cpx, cpy), point(x, y)));
@@ -131,7 +131,7 @@ impl Canvas2dPathState {
y: f64,
) {
if !self.ensure_open_subpath() {
- return;
+ self.move_to(c1x, c1y);
}
self.elements.push(PaintPathElement::CubicTo(
point(c1x, c1y),
@@ -158,8 +158,7 @@ impl Canvas2dPathState {
self.close_path();
}
- /// Appends the path for `ctx.arc(...)`. Returns `false` when the arguments
- /// are non-finite or the radius is negative (caller reports the error).
+ /// Canvas arcs use positive angles clockwise in the screen's y-down space.
pub(super) fn arc(
&mut self,
x: f64,
@@ -169,33 +168,9 @@ impl Canvas2dPathState {
end: f64,
ccw: bool,
) -> bool {
- if !x.is_finite()
- || !y.is_finite()
- || !radius.is_finite()
- || !start.is_finite()
- || !end.is_finite()
- {
- return false;
- }
- if radius < 0.0 {
- return false;
- }
- if radius == 0.0 {
- return true;
- }
- let (start, end) = normalized_arc_angles(start, end, ccw);
- let start_point = (x + radius * start.cos(), y + radius * start.sin());
- if !self.has_subpath {
- self.move_to(start_point.0, start_point.1);
- } else if self.current != start_point {
- self.line_to(start_point.0, start_point.1);
- }
- self.push_arc_cubics((x, y), radius, start, end, ccw);
- self.current = (x + radius * end.cos(), y + radius * end.sin());
- true
+ self.ellipse(x, y, radius, radius, 0.0, start, end, ccw)
}
- /// Appends the path for `ctx.ellipse(...)`.
pub(super) fn ellipse(
&mut self,
x: f64,
@@ -207,181 +182,74 @@ impl Canvas2dPathState {
end: f64,
ccw: bool,
) -> bool {
- if !x.is_finite()
- || !y.is_finite()
- || !radius_x.is_finite()
- || !radius_y.is_finite()
- || !rotation.is_finite()
- || !start.is_finite()
- || !end.is_finite()
+ if ![x, y, radius_x, radius_y, rotation, start, end]
+ .into_iter()
+ .all(f64::is_finite)
+ || radius_x < 0.0
+ || radius_y < 0.0
{
return false;
}
- if radius_x < 0.0 || radius_y < 0.0 {
- return false;
- }
- if radius_x == 0.0 || radius_y == 0.0 {
- return true;
- }
- let (start, end) = normalized_arc_angles(start, end, ccw);
- // Map unit-circle angles through the ellipse's scale + rotation.
- let ellipse_transform = LayoutTransform2D::IDENTITY
- .concatenate(LayoutTransform2D::rotation(rotation))
- .concatenate(LayoutTransform2D::scale(radius_x, radius_y))
- .concatenate(LayoutTransform2D::translation(x as f32, y as f32));
- let start_point = transform_point(ellipse_transform, start.cos(), start.sin());
- if !self.has_subpath {
- self.move_to(start_point.0, start_point.1);
- } else if self.current != start_point {
- self.line_to(start_point.0, start_point.1);
- }
- self.push_ellipse_cubics(ellipse_transform, start, end, ccw);
- let end_point = transform_point(ellipse_transform, end.cos(), end.sin());
- self.current = end_point;
+ let (start, sweep) = arc_angles(start, end, ccw);
+ self.append_arc(kurbo::Arc::new(
+ (x, y),
+ (radius_x, radius_y),
+ start,
+ sweep,
+ rotation.rem_euclid(TWO_PI),
+ ));
true
}
- /// Appends the path for `ctx.arcTo(...)`.
pub(super) fn arc_to(&mut self, x1: f64, y1: f64, x2: f64, y2: f64, radius: f64) -> bool {
- if !x1.is_finite()
- || !y1.is_finite()
- || !x2.is_finite()
- || !y2.is_finite()
- || !radius.is_finite()
- {
- return false;
- }
- if radius < 0.0 {
+ if ![x1, y1, x2, y2, radius].into_iter().all(f64::is_finite) || radius < 0.0 {
return false;
}
if !self.has_subpath {
+ self.move_to(x1, y1);
return true;
}
- let (x0, y0) = self.current;
- let (p1, p2) = ((x1, y1), (x2, y2));
- if (x0, y0) == p1 || p1 == p2 || radius == 0.0 {
+ let p0 = kurbo::Point::from(self.current);
+ let p1 = kurbo::Point::new(x1, y1);
+ let p2 = kurbo::Point::new(x2, y2);
+ let incoming = p0 - p1;
+ let outgoing = p2 - p1;
+ if incoming.hypot() == 0.0 || outgoing.hypot() == 0.0 || radius == 0.0 {
self.line_to(x1, y1);
return true;
}
- let (x01, y01) = (p1.0 - x0, p1.1 - y0);
- let (x12, y12) = (p2.0 - p1.0, p2.1 - p1.1);
- let d01 = x01.hypot(y01);
- let d12 = x12.hypot(y12);
- if d01 <= f64::EPSILON || d12 <= f64::EPSILON {
+ // Both rays originate at the corner. There is no radius clamp: the
+ // tangent points may lie beyond either of the supplied line segments.
+ let u = incoming / incoming.hypot();
+ let v = outgoing / outgoing.hypot();
+ let cross = u.cross(v);
+ if cross == 0.0 || !cross.is_finite() {
self.line_to(x1, y1);
return true;
}
- let cross = x01 * y12 - y01 * x12;
- if cross.abs() <= f64::EPSILON {
- self.line_to(x1, y1);
- return true;
- }
- let (ux01, uy01) = (x01 / d01, y01 / d01);
- let (ux12, uy12) = (x12 / d12, y12 / d12);
- let cos_theta = (x01 * x12 + y01 * y12) / (d01 * d12);
- let cos_theta = cos_theta.clamp(-1.0, 1.0);
- let theta = cos_theta.acos();
- let half = theta / 2.0;
- let tan_half = half.tan();
- if !tan_half.is_finite() || tan_half.abs() <= f64::EPSILON {
- self.line_to(x1, y1);
- return true;
- }
- let max_radius = d01.min(d12) * tan_half;
- let radius = radius.min(max_radius);
- let tangent = radius / tan_half;
- let (ta_x, ta_y) = (p1.0 + ux01 * tangent, p1.1 + uy01 * tangent);
- let (tb_x, tb_y) = (p1.0 + ux12 * tangent, p1.1 + uy12 * tangent);
- let (bx, by) = normalize((ux01 + ux12, uy01 + uy12));
- let sin_half = half.sin();
- if !sin_half.is_finite() || sin_half.abs() <= f64::EPSILON {
- self.line_to(x1, y1);
- return true;
- }
- let center = (
- p1.0 + bx * (radius / sin_half),
- p1.1 + by * (radius / sin_half),
- );
- let start_angle = (ta_y - center.1).atan2(ta_x - center.0);
- let end_angle = (tb_y - center.1).atan2(tb_x - center.0);
- let ccw = cross < 0.0;
- self.push_arc_cubics(center, radius, start_angle, end_angle, ccw);
- self.current = (tb_x, tb_y);
+ let tangent_distance = radius * ((1.0 + u.dot(v).clamp(-1.0, 1.0)) / cross.abs());
+ let tangent = p1 + u * tangent_distance;
+ let center = tangent + kurbo::Vec2::new(-u.y, u.x) * (radius * cross.signum());
+ let end = p1 + v * tangent_distance;
+ let start_angle = (tangent - center).atan2();
+ let end_angle = (end - center).atan2();
+ let (start, sweep) = arc_angles(start_angle, end_angle, cross > 0.0);
+ self.append_arc(kurbo::Arc::new(center, (radius, radius), start, sweep, 0.0));
true
}
- /// Adds a straight connector to the current point when `connect` and the
- /// current point differs from `target`, then appends cubic approximations
- /// of the arc from `start` to `end` around `center` with radius `radius`.
- fn push_arc_cubics(
- &mut self,
- center: (f64, f64),
- radius: f64,
- start: f64,
- end: f64,
- ccw: bool,
- ) {
- let sweep = signed_arc_sweep(start, end, ccw);
- if sweep.abs() <= f64::EPSILON {
- return;
- }
- let segments = (sweep.abs() / (std::f64::consts::FRAC_PI_2)).ceil() as usize;
- let segments = segments.max(1);
- let delta = sweep / segments as f64;
- let mut angle = start;
- for _ in 0..segments {
- let next = angle + delta;
- let (cos_a, sin_a) = angle.sin_cos();
- let (cos_b, sin_b) = next.sin_cos();
- let alpha = (4.0 / 3.0) * (1.0 - (delta / 2.0).cos()) / (delta / 2.0).sin();
- let (p0x, p0y) = (center.0 + radius * cos_a, center.1 + radius * sin_a);
- let (c1x, c1y) = (p0x - alpha * radius * sin_a, p0y + alpha * radius * cos_a);
- let (p3x, p3y) = (center.0 + radius * cos_b, center.1 + radius * sin_b);
- let (c2x, c2y) = (p3x + alpha * radius * sin_b, p3y - alpha * radius * cos_b);
- self.elements.push(PaintPathElement::CubicTo(
- point(c1x, c1y),
- point(c2x, c2y),
- point(p3x, p3y),
- ));
- self.current = (p3x, p3y);
- angle = next;
- }
- }
-
- fn push_ellipse_cubics(
- &mut self,
- transform: LayoutTransform2D,
- start: f64,
- end: f64,
- ccw: bool,
- ) {
- let sweep = signed_arc_sweep(start, end, ccw);
- if sweep.abs() <= f64::EPSILON {
- return;
- }
- let segments = (sweep.abs() / (std::f64::consts::FRAC_PI_2)).ceil() as usize;
- let segments = segments.max(1);
- let delta = sweep / segments as f64;
- let mut angle = start;
- for _ in 0..segments {
- let next = angle + delta;
- let (cos_a, sin_a) = angle.sin_cos();
- let (cos_b, sin_b) = next.sin_cos();
- let alpha = (4.0 / 3.0) * (1.0 - (delta / 2.0).cos()) / (delta / 2.0).sin();
- let (p0x, p0y) = transform_point(transform, cos_a, sin_a);
- let (p3x, p3y) = transform_point(transform, cos_b, sin_b);
- // Tangents on the unit circle, then mapped through the transform.
- let tangent_a = radius_tangent(transform, -sin_a, cos_a);
- let tangent_b = radius_tangent(transform, -sin_b, cos_b);
- let (c1x, c1y) = (p0x + alpha * tangent_a.0, p0y + alpha * tangent_a.1);
- let (c2x, c2y) = (p3x - alpha * tangent_b.0, p3y - alpha * tangent_b.1);
- self.elements.push(PaintPathElement::CubicTo(
- point(c1x, c1y),
- point(c2x, c2y),
- point(p3x, p3y),
- ));
- self.current = (p3x, p3y);
- angle = next;
+ fn append_arc(&mut self, arc: kurbo::Arc) {
+ use kurbo::{PathEl, Shape};
+
+ // A relative floor also bounds subdivision for enormous but finite
+ // radii. It prevents resource usage growing without bound with radius.
+ let tolerance = 0.01_f64.max(arc.radii.x.max(arc.radii.y) / 4096.0);
+ for element in arc.path_elements(tolerance) {
+ match element {
+ PathEl::MoveTo(p) => self.line_to(p.x, p.y),
+ PathEl::CurveTo(a, b, p) => self.bezier_curve_to(a.x, a.y, b.x, b.y, p.x, p.y),
+ _ => unreachable!("kurbo arcs contain only a start point and cubic segments"),
+ }
}
}
@@ -478,51 +346,102 @@ fn expand(min_x: &mut f64, min_y: &mut f64, max_x: &mut f64, max_y: &mut f64, po
*max_y = (*max_y).max(y);
}
-fn normalize(vector: (f64, f64)) -> (f64, f64) {
- let length = vector.0.hypot(vector.1);
- if length <= f64::EPSILON {
- (0.0, 0.0)
+/// Normalize in constant time, including opposite-sign finite angles whose
+/// subtraction overflows. Never repeatedly add/subtract TAU: at large f64
+/// magnitudes that operation does not advance at all.
+fn arc_angles(start: f64, end: f64, ccw: bool) -> (f64, f64) {
+ let difference = end - start;
+ let normalized_start = start.rem_euclid(TWO_PI);
+ let sweep = if !ccw && difference >= TWO_PI {
+ TWO_PI
+ } else if ccw && difference <= -TWO_PI {
+ -TWO_PI
+ } else if difference == 0.0 {
+ 0.0
} else {
- (vector.0 / length, vector.1 / length)
- }
+ let remainder = if difference.is_finite() {
+ difference.rem_euclid(TWO_PI)
+ } else {
+ (end.rem_euclid(TWO_PI) - normalized_start).rem_euclid(TWO_PI)
+ };
+ if ccw {
+ // Like Blink's AdjustEndAngle, preserve a whole turn for opposite-
+ // direction endpoints separated by an exact multiple of TAU.
+ if remainder == 0.0 && difference < 0.0 {
+ 0.0
+ } else {
+ remainder - TWO_PI
+ }
+ } else if remainder == 0.0 && difference < 0.0 {
+ TWO_PI
+ } else {
+ remainder
+ }
+ };
+ (normalized_start, sweep)
}
-fn normalized_arc_angles(start: f64, end: f64, ccw: bool) -> (f64, f64) {
- let mut end = end;
- if ccw {
- while end > start {
- end -= TWO_PI;
- }
- } else {
- while end < start {
- end += TWO_PI;
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use std::f64::consts::FRAC_PI_2;
+
+ #[test]
+ fn arc_angles_preserve_direction_full_turns_and_equal_endpoints() {
+ for (start, end, ccw, expected) in [
+ (0.0, FRAC_PI_2, false, FRAC_PI_2),
+ (0.0, FRAC_PI_2, true, -3.0 * FRAC_PI_2),
+ (0.0, -FRAC_PI_2, true, -FRAC_PI_2),
+ (0.0, -FRAC_PI_2, false, 3.0 * FRAC_PI_2),
+ (0.0, TWO_PI, false, TWO_PI),
+ (0.0, -TWO_PI, true, -TWO_PI),
+ (0.0, TWO_PI, true, -TWO_PI),
+ (0.0, -TWO_PI, false, TWO_PI),
+ (2.0, 2.0, true, 0.0),
+ (2.0, 2.0, false, 0.0),
+ ] {
+ assert!((arc_angles(start, end, ccw).1 - expected).abs() < 1e-12);
}
}
- (start, end)
-}
-/// Signed sweep angle (positive counterclockwise, negative clockwise) in
-/// `(-TAU, TAU]`. An exact full-circle difference is preserved as ±TAU.
-fn signed_arc_sweep(start: f64, end: f64, ccw: bool) -> f64 {
- let difference = end - start;
- if difference.abs() >= TWO_PI {
- return if ccw { TWO_PI } else { -TWO_PI };
- }
- if ccw {
- difference.rem_euclid(TWO_PI)
- } else {
- -((start - end).rem_euclid(TWO_PI))
+ #[test]
+ fn finite_extreme_angles_produce_bounded_path_work() {
+ for start in [0.0, 1e20, -1e20, f64::MAX, -f64::MAX] {
+ for end in [0.0, 1e20, -1e20, f64::MAX, -f64::MAX] {
+ for ccw in [false, true] {
+ let (angle, sweep) = arc_angles(start, end, ccw);
+ assert!(angle.is_finite() && (0.0..TWO_PI).contains(&angle));
+ assert!(sweep.is_finite() && sweep.abs() <= TWO_PI);
+ assert!(if ccw { sweep <= 0.0 } else { sweep >= 0.0 });
+ let mut state = Canvas2dPathState::default();
+ assert!(state.arc(10.0, 10.0, 5.0, start, end, ccw));
+ assert!(state.elements.len() <= 7);
+ }
+ }
+ }
}
-}
-fn transform_point(transform: LayoutTransform2D, x: f64, y: f64) -> (f64, f64) {
- let mapped = transform.map_point(LayoutPoint::new(x as f32, y as f32));
- (f64::from(mapped.x), f64::from(mapped.y))
-}
+ #[test]
+ fn arc_to_has_the_correct_tangent_and_endpoint() {
+ let mut state = Canvas2dPathState::default();
+ state.move_to(0.0, 0.0);
+ assert!(state.arc_to(10.0, 0.0, 10.0, 10.0, 5.0));
+ let PaintPathElement::LineTo(tangent) = state.elements[1] else {
+ panic!("arcTo must connect to its first tangent");
+ };
+ assert!((tangent.x - 5.0).abs() < 1e-5 && tangent.y.abs() < 1e-5);
+ assert!((state.current.0 - 10.0).abs() < 1e-5);
+ assert!((state.current.1 - 5.0).abs() < 1e-5);
+ }
-/// Maps a unit-circle tangent through the ellipse's linear part only
-/// (scale + rotation, excluding translation).
-fn radius_tangent(transform: LayoutTransform2D, x: f64, y: f64) -> (f64, f64) {
- let [a, b, c, d, _, _] = transform.coefficients;
- (a * x + c * y, b * x + d * y)
+ #[test]
+ fn rejected_arc_geometry_does_not_mutate_the_path() {
+ let mut state = Canvas2dPathState::default();
+ for invalid in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY, -1.0] {
+ assert!(!state.arc(0.0, 0.0, invalid, 0.0, 1.0, false));
+ assert!(!state.ellipse(0.0, 0.0, 5.0, invalid, 0.0, 0.0, 1.0, false));
+ assert!(!state.arc_to(0.0, 0.0, 1.0, 1.0, invalid));
+ assert!(state.is_empty());
+ }
+ }
}
diff --git a/moli-renderer-v8/src/script_vm/tests/canvas_paths.rs b/moli-renderer-v8/src/script_vm/tests/canvas_paths.rs
new file mode 100644
index 000000000..108b4cd2c
--- /dev/null
+++ b/moli-renderer-v8/src/script_vm/tests/canvas_paths.rs
@@ -0,0 +1,148 @@
+use super::*;
+
+fn canvas_pixels(script: &str, points: &[(u32, u32)], expected: &[u8]) {
+ for kind in ["html", "offscreen"] {
+ let mut vm = new_storage_test_vm("https://canvas-paths.test/");
+ let result = vm
+ .eval(&format!(
+ r#"(() => {{
+ const canvas = {constructor};
+ canvas.width = canvas.height = 96;
+ const ctx = canvas.getContext('2d');
+ ctx.fillStyle = ctx.strokeStyle = 'red';
+ ctx.lineWidth = 2;
+ {script}
+ return JSON.stringify({points}.map(([x,y]) => ctx.getImageData(x,y,1,1).data[3]));
+ }})()"#,
+ constructor = if kind == "html" {
+ "document.createElement('canvas')"
+ } else {
+ "new OffscreenCanvas(96,96)"
+ },
+ points = serde_json::to_string(points).unwrap(),
+ ))
+ .expect("canvas script should execute");
+ assert_eq!(
+ result,
+ serde_json::to_string(expected).unwrap(),
+ "{kind}: {script}"
+ );
+ }
+}
+
+#[test]
+fn canvas_arc_quadrants_match_screen_coordinate_direction() {
+ // Pixel probes exclude the antialiased boundary; Chromium 145 gives these
+ // same results. Checking only the center misses malformed circular paths.
+ let points = [(40, 40), (24, 40), (24, 24), (40, 24)];
+ canvas_pixels(
+ "ctx.moveTo(32,32);ctx.arc(32,32,20,0,Math.PI/2);ctx.closePath();ctx.fill();",
+ &points,
+ &[255, 0, 0, 0],
+ );
+ canvas_pixels(
+ "ctx.moveTo(32,32);ctx.arc(32,32,20,0,-Math.PI/2,true);ctx.closePath();ctx.fill();",
+ &points,
+ &[0, 0, 0, 255],
+ );
+ canvas_pixels(
+ "ctx.moveTo(32,32);ctx.arc(32,32,20,0,Math.PI/2,true);ctx.closePath();ctx.fill();",
+ &points,
+ &[0, 255, 255, 255],
+ );
+}
+
+#[test]
+fn canvas_full_circle_matches_a_disk_away_from_its_boundary() {
+ let mut points = Vec::new();
+ let mut expected = Vec::new();
+ for y in (5..60).step_by(2) {
+ for x in (5..60).step_by(2) {
+ let distance = (f64::from(x) + 0.5 - 32.0).hypot(f64::from(y) + 0.5 - 32.0);
+ if (distance - 20.0).abs() > 2.0 {
+ points.push((x, y));
+ expected.push(if distance < 20.0 { 255 } else { 0 });
+ }
+ }
+ }
+ for arc in [
+ "0,2*Math.PI",
+ "0,-2*Math.PI,true",
+ "0,4*Math.PI",
+ "1,1+2*Math.PI",
+ ] {
+ canvas_pixels(
+ &format!("ctx.arc(32,32,20,{arc});ctx.fill();"),
+ &points,
+ &expected,
+ );
+ }
+}
+
+#[test]
+fn canvas_ellipse_preserves_center_and_rotation() {
+ canvas_pixels(
+ "ctx.ellipse(32,32,20,10,0,0,2*Math.PI);ctx.fill();",
+ &[(48, 32), (32, 39), (32, 45), (32, 32)],
+ &[255, 255, 0, 255],
+ );
+ canvas_pixels(
+ "ctx.ellipse(32,32,20,10,Math.PI/2,0,2*Math.PI);ctx.fill();",
+ &[(32, 48), (39, 32), (45, 32), (32, 32)],
+ &[255, 255, 0, 255],
+ );
+}
+
+#[test]
+fn canvas_arc_to_connects_to_the_first_tangent() {
+ canvas_pixels(
+ "ctx.moveTo(0,20);ctx.arcTo(40,20,40,60,15);ctx.stroke();",
+ &[(10, 20), (25, 20), (39, 34), (48, 20), (39, 50)],
+ &[255, 255, 255, 0, 0],
+ );
+}
+
+#[test]
+fn canvas_arc_to_does_not_clamp_radius_to_segment_lengths() {
+ canvas_pixels(
+ "ctx.moveTo(10,20);ctx.arcTo(40,20,40,30,25);ctx.stroke();",
+ &[(14, 20), (39, 44), (39, 30), (39, 25)],
+ &[255, 255, 0, 0],
+ );
+}
+
+#[test]
+fn canvas_empty_path_commands_establish_the_required_start_point() {
+ for command in [
+ "ctx.lineTo(10,10);",
+ "ctx.quadraticCurveTo(10,10,10,10);",
+ "ctx.bezierCurveTo(10,10,10,10,10,10);",
+ "ctx.arcTo(10,10,20,20,5);",
+ ] {
+ canvas_pixels(
+ &format!("{command}ctx.lineTo(30,10);ctx.lineTo(30,30);ctx.closePath();ctx.fill();"),
+ &[(25, 15), (15, 25)],
+ &[255, 0],
+ );
+ }
+}
+
+#[test]
+fn canvas_zero_radius_arc_keeps_its_connecting_line() {
+ canvas_pixels(
+ "ctx.moveTo(5,20);ctx.arc(30,20,0,0,1);ctx.stroke();",
+ &[(15, 20), (35, 20)],
+ &[255, 0],
+ );
+}
+
+#[test]
+fn canvas_arc_to_degenerate_segments_remain_lines() {
+ for arguments in ["30,20,50,20,8", "30,20,30,20,8", "30,20,30,40,0"] {
+ canvas_pixels(
+ &format!("ctx.moveTo(5,20);ctx.arcTo({arguments});ctx.stroke();"),
+ &[(15, 20), (35, 20)],
+ &[255, 0],
+ );
+ }
+}
diff --git a/moli-renderer-v8/src/script_vm/tests/mod.rs b/moli-renderer-v8/src/script_vm/tests/mod.rs
index ca6b7f07d..d21c4b3d6 100644
--- a/moli-renderer-v8/src/script_vm/tests/mod.rs
+++ b/moli-renderer-v8/src/script_vm/tests/mod.rs
@@ -15145,6 +15145,7 @@ fn decode_png_dimensions_from_data_url(data_url: &str) -> (u32, u32) {
}
mod browser_api;
+mod canvas_paths;
mod canvas_webgl;
mod dom_elements;
mod dom_xhr;