Merge pull request #1470 from storytold/codex/xfl-shape-hints

Add shared XFL shape hint and tween schema views
This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-15 00:22:00 -05:00
commit ec2dd163b4
3 files changed
+240

No files matched your search

+4
View File
@@ -33,8 +33,12 @@ pub mod filters;
pub mod media;
/// Borrowed views of native XFL shape and style elements.
pub mod shape;
/// Borrowed views of native XFL shape-hint records.
pub mod shape_hints;
/// Borrowed views of native XFL text-field and text-run schema.
pub mod text;
/// Borrowed views of native XFL tween and custom-ease records.
pub mod tween;
/// Largest single entry inflated (bytes).
pub const MAX_ENTRY: usize = 256 * 1024 * 1024;
+119
View File
@@ -0,0 +1,119 @@
//! Borrowed views of native XFL shape-hint records.
//!
//! These parse the native point encodings and hint ordering. Snapping points onto consumer
//! geometry and pairing start/end keyframes remain application responsibilities.
use crate::shape::{XflElement, integer, number};
/// A native XFL point in document coordinates.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Point {
pub x: f32,
pub y: f32,
}
/// The optional `<shapeHints>` wrapper on a keyframe.
#[derive(Clone, Copy, Debug)]
pub struct ShapeHints<'a, E: XflElement> {
wrapper: Option<&'a E>,
}
impl<'a, E: XflElement> ShapeHints<'a, E> {
pub fn new(keyframe: &'a E) -> Self {
Self { wrapper: keyframe.child("shapeHints") }
}
pub fn is_empty(&self) -> bool {
self.wrapper.is_none_or(|wrapper| wrapper.elements().next().is_none())
}
/// Records in source order; unrecognized child names are retained because the legacy reader
/// interpreted their point and ordering attributes too.
pub fn iter(&self) -> impl Iterator<Item = ShapeHint<'a, E>> + 'a {
let wrapper = self.wrapper;
wrapper.into_iter().flat_map(XflElement::elements).map(|element| ShapeHint { element })
}
}
/// One native shape-hint record.
#[derive(Clone, Copy, Debug)]
pub struct ShapeHint<'a, E: XflElement> {
element: &'a E,
}
impl<'a, E: XflElement> ShapeHint<'a, E> {
/// Native numeric order, or its single-letter `name` fallback (`a` is zero).
pub fn index(&self) -> Option<usize> {
integer(self.element, "index").map(|value| value as usize).or_else(|| self.element.attr("name").and_then(letter_index))
}
/// The keyframe's point, in either joined or separate native coordinate attributes.
pub fn start_point(&self) -> Option<Point> {
read_point(self.element, "startPoint", "startX", "startY").or_else(|| read_point(self.element, "point", "x", "y"))
}
/// The next keyframe's point, when the record contains one.
pub fn end_point(&self) -> Option<Point> {
read_point(self.element, "endPoint", "endX", "endY")
}
}
fn letter_index(name: &str) -> Option<usize> {
let [byte] = name.as_bytes() else { return None };
match byte.to_ascii_lowercase() {
b'a'..=b'z' => Some((byte.to_ascii_lowercase() - b'a') as usize),
_ => None,
}
}
fn read_point(element: &impl XflElement, joined: &str, x: &str, y: &str) -> Option<Point> {
let point = match element.attr(joined) {
Some(value) => {
let mut values = value.split([',', ' ']).filter(|part| !part.trim().is_empty()).map(|part| part.trim().parse::<f32>().ok());
Point { x: values.next()??, y: values.next()?? }
}
None => Point { x: number(element, x)?, y: number(element, y)? },
};
(point.x.is_finite() && point.y.is_finite()).then_some(point)
}
#[cfg(test)]
mod tests {
use super::*;
fn parse(source: &[u8]) -> craft_xml::El {
craft_xml::parse(source).expect("well-formed test XML")
}
#[test]
fn reads_joined_and_split_native_points_in_source_order() {
let keyframe = parse(br#"<DOMFrame><shapeHints><Hint name="B" startPoint="12.5, -2" endX="30" endY="40"/><Hint index="0" startX="1" startY="2" endPoint="3 4"/></shapeHints></DOMFrame>"#);
let hints = ShapeHints::new(&keyframe);
assert!(!hints.is_empty());
let records: Vec<_> = hints.iter().collect();
assert_eq!(records[0].index(), Some(1));
assert_eq!(records[0].start_point(), Some(Point { x: 12.5, y: -2.0 }));
assert_eq!(records[0].end_point(), Some(Point { x: 30.0, y: 40.0 }));
assert_eq!(records[1].index(), Some(0));
assert_eq!(records[1].start_point(), Some(Point { x: 1.0, y: 2.0 }));
assert_eq!(records[1].end_point(), Some(Point { x: 3.0, y: 4.0 }));
}
#[test]
fn malformed_joined_values_fall_back_to_the_other_native_point_field() {
let keyframe = parse(br#"<DOMFrame><shapeHints><Any name="z" startPoint="bad" x="7" y="8" endPoint="nan, 2"/></shapeHints></DOMFrame>"#);
let hint = ShapeHints::new(&keyframe).iter().next().expect("hint record");
assert_eq!(hint.index(), Some(25));
assert_eq!(hint.start_point(), Some(Point { x: 7.0, y: 8.0 }));
assert_eq!(hint.end_point(), None);
}
#[test]
fn missing_lists_and_non_letter_names_remain_optional() {
let keyframe = parse(b"<DOMFrame/>");
assert!(ShapeHints::new(&keyframe).is_empty());
let keyframe = parse(br#"<DOMFrame><shapeHints><Hint name="AB" startX="1" startY="2"/></shapeHints></DOMFrame>"#);
let hint = ShapeHints::new(&keyframe).iter().next().expect("hint record");
assert_eq!(hint.index(), None);
}
}
+117
View File
@@ -0,0 +1,117 @@
//! Borrowed views of native XFL frame-tween and custom-ease records.
//!
//! This parses the native attributes and point lists. Interpreting easing curves and applying
//! them to an application's timeline model remain consumer responsibilities.
use crate::shape::{XflElement, boolean, number};
/// Native motion/shape tween settings on a `DOMFrame`.
#[derive(Clone, Copy, Debug)]
pub struct FrameTween<'a, E: XflElement> {
frame: &'a E,
}
impl<'a, E: XflElement> FrameTween<'a, E> {
pub fn new(frame: &'a E) -> Self {
Self { frame }
}
/// The classic slider value, when present and finite.
pub fn acceleration(&self) -> Option<f32> {
number(self.frame, "acceleration")
}
pub fn has_custom_ease(&self) -> Option<bool> {
boolean(self.frame, "hasCustomEase")
}
/// Animate's default is a single curve unless this is explicitly false.
pub fn use_single_ease_curve(&self) -> Option<bool> {
boolean(self.frame, "useSingleEaseCurve")
}
pub fn orient_to_path(&self) -> Option<bool> {
boolean(self.frame, "motionTweenOrientToPath")
}
/// Raw native rotation setting; interpretation stays in the consuming application.
pub fn rotate(&self) -> Option<&'a str> {
self.frame.attr("motionTweenRotate")
}
/// Custom ease curves in source order; unknown tween entries are skipped.
pub fn custom_eases(&self) -> impl Iterator<Item = CustomEase<'a, E>> + 'a {
self.frame.child("tweens").into_iter().flat_map(|wrapper| wrapper.elements_named("CustomEase")).map(|element| CustomEase { element })
}
}
/// A native custom-ease curve.
#[derive(Clone, Copy, Debug)]
pub struct CustomEase<'a, E: XflElement> {
element: &'a E,
}
impl<'a, E: XflElement> CustomEase<'a, E> {
/// The property-group name, defaulting to `all` in XFL.
pub fn target(&self) -> &'a str {
self.element.attr("target").map(str::trim).unwrap_or("all")
}
pub fn points(&self) -> impl Iterator<Item = EasePoint<'a, E>> + 'a {
self.element.elements_named("Point").map(|element| EasePoint { element })
}
}
/// One native custom-ease point.
#[derive(Clone, Copy, Debug)]
pub struct EasePoint<'a, E: XflElement> {
element: &'a E,
}
impl<E: XflElement> EasePoint<'_, E> {
pub fn x(&self) -> Option<f32> {
number(self.element, "x")
}
pub fn y(&self) -> Option<f32> {
number(self.element, "y")
}
}
#[cfg(test)]
mod tests {
use super::*;
fn parse(source: &[u8]) -> craft_xml::El {
craft_xml::parse(source).expect("well-formed test XML")
}
#[test]
fn reads_frame_tween_settings_and_custom_eases_in_order() {
let frame = parse(
br#"<x:DOMFrame xmlns:x="urn:xfl" acceleration="-45" hasCustomEase="true" useSingleEaseCurve="false" motionTweenOrientToPath="true" motionTweenRotate="clockwise"><x:tweens><x:Other/><x:CustomEase target="position"><x:Point x="0" y="0"/><x:Point x="0.5" y="0.25"/></x:CustomEase><x:CustomEase><x:Point x="1" y="1"/></x:CustomEase></x:tweens></x:DOMFrame>"#,
);
let tween = FrameTween::new(&frame);
assert_eq!(tween.acceleration(), Some(-45.0));
assert_eq!(tween.has_custom_ease(), Some(true));
assert_eq!(tween.use_single_ease_curve(), Some(false));
assert_eq!(tween.orient_to_path(), Some(true));
assert_eq!(tween.rotate(), Some("clockwise"));
let curves: Vec<_> = tween.custom_eases().collect();
assert_eq!(curves.len(), 2);
assert_eq!(curves[0].target(), "position");
assert_eq!(curves[0].points().map(|point| (point.x(), point.y())).collect::<Vec<_>>(), [(Some(0.0), Some(0.0)), (Some(0.5), Some(0.25))]);
assert_eq!(curves[1].target(), "all");
assert_eq!(curves[1].points().next().map(|point| (point.x(), point.y())), Some((Some(1.0), Some(1.0))));
}
#[test]
fn absent_and_malformed_values_remain_optional() {
let frame = parse(br#"<DOMFrame acceleration="NaN" hasCustomEase="bad" useSingleEaseCurve="false"/>"#);
let tween = FrameTween::new(&frame);
assert_eq!(tween.acceleration(), None);
assert_eq!(tween.has_custom_ease(), Some(false));
assert_eq!(tween.use_single_ease_curve(), Some(false));
assert!(tween.custom_eases().next().is_none());
}
}