lottie: caller-owned numeric precision for Bezier path writing (#1979)

This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-16 22:22:32 -05:00
1 parent c153d95e55
commit bee6842e8a
1 file changed
+23 -1
+23 -1
View File
@@ -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::<Vec<_>>();
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::<Vec<_>>();
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": []}));
}
}