mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-16 07:12:46 +00:00
M8: a whole ellipse starting anywhere stays whole through a .fig file (#1851)
A .fig file (and the REST JSON) holds an ellipse's start and end angles as 32-bit floats, and FigCraft writes them that way too. For a whole turn that starts anywhere but zero, end - start comes out a hair short of 2π as often as not (start 0.3: 6.283185184), which reads as an arc: the ellipse came back as a pie slice whose centred stroke draws a line from the centre to the edge. On import, a sweep within the angles' precision of a turn is now a turn. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
dea8d8eb3a
commit
fa8faf216d
2 files changed
+25
-1
No files matched your search
@@ -1592,3 +1592,22 @@ fn a_name_used_with_two_types_in_a_set_writes_both_definitions() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A whole ellipse that starts anywhere round comes back whole: the file holds its start and end
|
||||
/// angles as 32-bit floats, and their difference falls a hair short of a turn as often as not.
|
||||
#[test]
|
||||
fn a_whole_ellipse_starting_anywhere_comes_back_whole() {
|
||||
let tau = std::f64::consts::TAU;
|
||||
for start in [0.3, 2.0, -std::f64::consts::FRAC_PI_2, std::f64::consts::PI, 100.25] {
|
||||
for sweep in [tau, -tau] {
|
||||
let mut doc = Document::new("whole");
|
||||
let page = doc.first_page().unwrap();
|
||||
doc.create(page, NodeKind::Ellipse { start, sweep, inner: 0.0 }, "Ellipse", 0.0, 0.0, 100.0, 100.0).unwrap();
|
||||
let (bytes, _) = export(&doc, None).unwrap();
|
||||
let (back, _) = import(&bytes, "x").unwrap();
|
||||
let kinds: Vec<NodeKind> =
|
||||
back.ids().filter_map(|id| back.node(id)).map(|n| n.kind.clone()).filter(|k| matches!(k, NodeKind::Ellipse { .. })).collect();
|
||||
assert!(matches!(kinds.as_slice(), [NodeKind::Ellipse { sweep: s, .. }] if *s == sweep), "start {start}, sweep {sweep}: {kinds:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -629,7 +629,12 @@ impl<'a> Ctx<'a> {
|
||||
let start = arc("startingAngle").unwrap_or(0.0).clamp(-1e3, 1e3);
|
||||
let end = arc("endingAngle").map_or(start + std::f64::consts::TAU, |e| e.clamp(-1e3, 1e3));
|
||||
let tau = std::f64::consts::TAU;
|
||||
NodeKind::Ellipse { start, sweep: (end - start).clamp(-tau, tau), inner: arc("innerRadius").unwrap_or(0.0).clamp(0.0, 1.0) }
|
||||
// The angles are 32-bit floats: a whole turn from anywhere but zero comes out a
|
||||
// hair short of 2π as often as not. Within their precision of a turn, it is one.
|
||||
let precision = (start.abs() + end.abs() + tau) * f64::from(f32::EPSILON);
|
||||
let sweep = end - start;
|
||||
let sweep = if sweep.abs() >= tau - precision { tau.copysign(sweep) } else { sweep };
|
||||
NodeKind::Ellipse { start, sweep: sweep.clamp(-tau, tau), inner: arc("innerRadius").unwrap_or(0.0).clamp(0.0, 1.0) }
|
||||
}
|
||||
// Point counts aren't part of the documented JSON: the outline is exact when present.
|
||||
"REGULAR_POLYGON" | "STAR" if point_count().is_none() && has_geometry => NodeKind::Vector { network: VectorNetwork::default() },
|
||||
|
||||
Reference in new issue
Block a user