diff --git a/crates/lottie/src/path.rs b/crates/lottie/src/path.rs index a8301ccba..f404b3b6b 100644 --- a/crates/lottie/src/path.rs +++ b/crates/lottie/src/path.rs @@ -40,7 +40,29 @@ impl Bezier { /// The path as `{c, v, i, o}`, coordinates rounded to six decimals. pub fn to_json(&self) -> Json { - let pts = |v: &[[f64; 2]]| v.iter().map(|[x, y]| json!([round6(*x), round6(*y)])).collect::>(); + self.to_json_with(|v| json!(round6(v))) + } + + /// Writes the path with a caller-owned numeric representation (for example, + /// three-decimal compact exports). Geometry and tangent lengths are unchanged. + /// The callback is applied to every coordinate; callers own finite-value policy. + pub fn to_json_with(&self, number: impl Fn(f64) -> Json) -> Json { + let pts = |v: &[[f64; 2]]| v.iter().map(|[x, y]| Json::Array(vec![number(*x), number(*y)])).collect::>(); json!({"c": self.closed, "v": pts(&self.vertices), "i": pts(&self.in_tangents), "o": pts(&self.out_tangents)}) } } + +#[cfg(test)] +mod policy_tests { + use super::*; + + #[test] + fn caller_number_policy_is_applied_without_prior_rounding() { + let p = Bezier { closed: true, vertices: vec![[1.23456, 1e303]], in_tangents: vec![[0.0, -2.0]], out_tangents: vec![] }; + let raw = p.to_json_with(|v| json!(v)); + assert_eq!(raw, json!({"c": true, "v": [[1.23456, 1e303]], "i": [[0.0, -2.0]], "o": []})); + let zero = p.to_json_with(|_| json!(0)); + assert_eq!(zero, json!({"c": true, "v": [[0, 0]], "i": [[0, 0]], "o": []})); + assert_eq!(Bezier::default().to_json(), json!({"c": false, "v": [], "i": [], "o": []})); + } +}