PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Affinity import: pie shapes with huge angles no longer hang the reader (#4054)
.CooCoo003f9b4a1867
diff --git a/contributors/people.toml b/contributors/people.tomlindex fadd13984..9a8bf4b4c 100644--- a/contributors/people.toml+++ b/contributors/people.toml@@ -136,3 +136,7 @@ sync_github_name = true[people.Hevarh1]sync_github_name = true++[people.dotCooCoo]+real_name = "Robert Lee"+sync_github_name = truediff --git a/crates/affinity/src/shapes.rs b/crates/affinity/src/shapes.rsindex 5e825fa85..f3bff62a3 100644--- a/crates/affinity/src/shapes.rs+++ b/crates/affinity/src/shapes.rs@@ -115,13 +115,21 @@ pub(crate) fn shape(r: &mut Reader, shape: ObjId, b: [f64; 4], world: Affine) ->}))?}b"ShPi" => {- let start = num(r, shape, b"AngS").unwrap_or(0.0);- let mut end = num(r, shape, b"AngE").unwrap_or(TAU);- while end < start {- end += TAU;- }+ let raw_start = num(r, shape, b"AngS").unwrap_or(0.0);+ let raw_end = num(r, shape, b"AngE").unwrap_or(TAU);+ // Normalize in constant time. Repeatedly adding TAU can hang on finite values+ // large enough that floating-point addition no longer changes the endpoint.+ let start = raw_start.rem_euclid(TAU);+ let delta = raw_end - raw_start;+ let sweep = if raw_end >= raw_start {+ delta.min(TAU)+ } else if delta.is_finite() {+ delta.rem_euclid(TAU)+ } else {+ (raw_end.rem_euclid(TAU) - start).rem_euclid(TAU)+ };let inner = num(r, shape, b"IRad").unwrap_or(0.0).clamp(0.0, 1.0);- pie(cx, cy, rx, ry, start, end.min(start + TAU), inner)+ pie(cx, cy, rx, ry, start, start + sweep, inner)}b"ShpT" => {let p = num(r, shape, b"Pos ").unwrap_or(0.5);diff --git a/crates/affinity/src/tests.rs b/crates/affinity/src/tests.rsindex f70648b42..7f1472b1a 100644--- a/crates/affinity/src/tests.rs+++ b/crates/affinity/src/tests.rs@@ -349,3 +349,68 @@ mod archive {}}}++mod shapes {+ use crate::container::Limits;+ use crate::model::{Document, Kind, Path};+ use crate::stream::Tag;+ use crate::synth::{self, F, Method, tag};++ fn shape(id: u32, class: &[u8; 4], parameters: Vec<(Tag, F)>) -> F {+ F::Def(+ id,+ vec![tag(b"ShpN")],+ vec![(tag(b"ShpB"), F::F64s(vec![0.0, 0.0, 10.0, 20.0])), (tag(b"Shpe"), F::Def(id + 10_000, vec![tag(class)], parameters))],+ )+ }++ /// Reads a document whose one page holds the object `top` from `definitions`, on a thread of+ /// its own, and fails the test when reading takes more than 20 s.+ fn read(definitions: Vec<F>, top: u32) -> Document {+ let data = synth::stream(&[+ (tag(b"Defs"), F::Shared(definitions)),+ (+ tag(b"DocR"),+ F::Def(+ 1,+ vec![tag(b"DocN")],+ vec![(+ tag(b"Chld"),+ F::Shared(vec![F::Def(+ 2,+ vec![tag(b"Sprd")],+ vec![(tag(b"SprB"), F::F64s(vec![0.0, 0.0, 400.0, 300.0])), (tag(b"Chld"), F::Shared(vec![F::Ref(top)]))],+ )]),+ )],+ ),+ ),+ ]);+ let bytes = synth::container(&[("doc.dat", &data, Method::Zlib)], None);+ let (tx, rx) = std::sync::mpsc::channel();+ std::thread::spawn(move || tx.send(crate::model::read(&bytes, Limits::default())).ok());+ rx.recv_timeout(std::time::Duration::from_secs(20)).unwrap_or_else(|_| panic!("reading the document did not finish")).unwrap()+ }++ fn path(document: &Document) -> &Path {+ match &document.spreads[0].nodes[0].kind {+ Kind::Shape { path, .. } => path,+ other => panic!("expected shape, got {other:?}"),+ }+ }++ #[test]+ fn pie_angles_with_huge_finite_values_do_not_loop_or_emit_nonfinite_points() {+ let document = read(vec![shape(100, b"ShPi", vec![(tag(b"AngS"), F::F64(1.0e308)), (tag(b"AngE"), F::F64(-1.0e308))])], 100);+ for subpath in &path(&document).subpaths {+ assert!(subpath.start.x.is_finite() && subpath.start.y.is_finite());+ assert!(subpath.segments.len() <= 20);+ assert!(subpath.segments.iter().flatten().all(|point| point.x.is_finite() && point.y.is_finite()));+ }+ }++ #[test]+ fn wrapped_pie_angles_keep_the_same_geometry_as_the_positive_span() {+ let make = |end| read(vec![shape(100, b"ShPi", vec![(tag(b"AngS"), F::F64(0.0)), (tag(b"AngE"), F::F64(end))])], 100);+ assert_eq!(path(&make(-std::f64::consts::FRAC_PI_2)), path(&make(3.0 * std::f64::consts::FRAC_PI_2)));+ }+}