diff --git a/crates/engine/src/cmd/flare.rs b/crates/engine/src/cmd/flare.rs index 187496f137..05b25421eb 100644 --- a/crates/engine/src/cmd/flare.rs +++ b/crates/engine/src/cmd/flare.rs @@ -36,6 +36,15 @@ fn count_or(p: &Value, key: &str, default: u64, cmd: &str) -> Result { } } +/// `end`, the rings' end point, or an error naming `x2` or `y2` when that coordinate is not a +/// finite number (a `pathLength` so large that the end point overflows). +fn finite_end(end: Point, cmd: &str) -> Result { + match [("x2", end.x), ("y2", end.y)].into_iter().find(|(_, v)| !v.is_finite()) { + None => Ok(end), + Some((key, _)) => Err(bad(cmd, format!("{key} must be a finite coordinate"))), + } +} + /// `shape.flare`'s params `p` with every setting given: the defaults filled in, the counts at most /// 50 and the rings' end point as `x2`, `y2` (from `pathLength` and `direction` when `p` gives no /// end point), with `ringSeed` when `p` gives one. @@ -60,7 +69,7 @@ fn settings_of(p: &Value, cmd: &str) -> Result { (Some(x), Some(y)) => Point::new(x, y), _ => { let a = f64_or(p, "direction", 45.0).to_radians(); - c + Vec2::new(a.cos(), -a.sin()) * f64_or(p, "pathLength", 300.0) + finite_end(c + Vec2::new(a.cos(), -a.sin()) * f64_or(p, "pathLength", 300.0), cmd)? } }; let mut v = json!({ "cx": c.x, "cy": c.y, "x2": end.x, "y2": end.y, "rays": rays, "rings": rings, "seed": seed }); @@ -173,7 +182,7 @@ fn edited(old: &Value, p: &Value, xf: Affine) -> Result { let (c, e) = (xf * centre, xf * old_end); let d = e - c; let a = number_or(p, "direction", (-d.y).atan2(d.x).to_degrees(), C)?.to_radians(); - inv * (c + Vec2::new(a.cos(), -a.sin()) * number_or(p, "pathLength", d.hypot(), C)?) + finite_end(inv * (c + Vec2::new(a.cos(), -a.sin()) * number_or(p, "pathLength", d.hypot(), C)?), C)? } // Moving the center leaves the rings' end point where it is. None => old_end, diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 10c7069ce0..09b78d8a89 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -3360,6 +3360,19 @@ fn flare_counts_are_whole_numbers() { assert!(s.execute("shape.flare", &json!({"cx": 100, "cy": 100, "rays": 2.5})).is_err()); } +/// `shape.editFlare` on a flare scaled far down: a `pathLength` whose end point, taken back to +/// where the flare was drawn, is not a finite number is an error, and the flare keeps its settings. +#[test] +fn edit_flare_end_points_that_are_not_finite_are_errors() { + let mut s = session(); + let id = NodeId(s.execute("shape.flare", &json!({"cx": 100, "cy": 100, "rings": 2})).unwrap()["id"].as_u64().unwrap()); + s.execute("object.scale", &json!({"sx": 1e-4, "sy": 1e-4})).unwrap(); + let before = flare_settings(&s.doc().unwrap().doc, id).expect("a flare"); + let err = s.execute("shape.editFlare", &json!({"pathLength": 1e305})).unwrap_err().to_string(); + assert!(err.ends_with("x2 must be a finite coordinate"), "{err}"); + assert_eq!(flare_settings(&s.doc().unwrap().doc, id).expect("still a flare"), before); +} + /// A flare drawn again keeps its parts' pixel-aligned property, whatever the document's Align New /// Objects to Pixel Grid is now, and a pixel-aligned flare that was moved is still a flare. #[test]