mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 17:05:55 +01:00
Flare: shape.editFlare refuses an end point that is not finite
On a flare scaled far down, a large pathLength gave an end point that overflowed when taken back to where the flare was drawn. The settings then stored the end point as null, and the flare was drawn again with the default path. shape.flare and shape.editFlare now return an error naming x2 or y2 instead.
This commit is contained in:
1 parent
f026c91159
commit
22f68ebb8d
2 files changed
+24
-2
No files matched your search
@@ -36,6 +36,15 @@ fn count_or(p: &Value, key: &str, default: u64, cmd: &str) -> Result<u64> {
|
||||
}
|
||||
}
|
||||
|
||||
/// `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<Point> {
|
||||
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<Value> {
|
||||
(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<Value> {
|
||||
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,
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in new issue
Block a user