From bb9f9a95cc6e8780b1fef0fb9d684536d8b2230a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 00:12:52 +0000 Subject: [PATCH 1/4] Add neutral XFL shape and media schema views --- crates/xfl/src/lib.rs | 4 + crates/xfl/src/media.rs | 190 +++++++++++++++ crates/xfl/src/shape.rs | 500 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 694 insertions(+) create mode 100644 crates/xfl/src/media.rs create mode 100644 crates/xfl/src/shape.rs diff --git a/crates/xfl/src/lib.rs b/crates/xfl/src/lib.rs index 4a957111f..9dd43fd5d 100644 --- a/crates/xfl/src/lib.rs +++ b/crates/xfl/src/lib.rs @@ -27,6 +27,10 @@ use std::collections::HashMap; pub mod document; /// XFL's edge-attribute grammar, independent of any consuming application's geometry types. pub mod edges; +/// Borrowed views of native XFL media library records. +pub mod media; +/// Borrowed views of native XFL shape and style elements. +pub mod shape; /// Largest single entry inflated (bytes). pub const MAX_ENTRY: usize = 256 * 1024 * 1024; diff --git a/crates/xfl/src/media.rs b/crates/xfl/src/media.rs new file mode 100644 index 000000000..a501e7888 --- /dev/null +++ b/crates/xfl/src/media.rs @@ -0,0 +1,190 @@ +//! Borrowed views of XFL's bitmap, sound, and video library-item records. +//! +//! These views expose native XFL metadata and archive member names. Decoding those bytes and +//! mapping the records into an application's media model remain consumer responsibilities. + +use crate::shape::XflElement; + +/// Archive member named by a library item's native `href` attribute. +pub fn library_member(href: &str) -> String { + format!("LIBRARY/{href}") +} + +/// Archive member named by an XFL item's native `*DataHRef` attribute. +pub fn data_member(href: &str) -> String { + format!("bin/{href}") +} + +/// A packaged `./...` external video source, normalized to ZIP's slash separators. +pub fn packaged_external_member(path: &str) -> Option { + path.strip_prefix("./").map(|path| path.replace('\\', "/")) +} + +/// Media library records in an XFL document, in source order. +#[derive(Clone, Copy, Debug)] +pub struct Catalog<'a, E: XflElement> { + media: Option<&'a E>, +} + +impl<'a, E: XflElement> Catalog<'a, E> { + pub fn new(document: &'a E) -> Self { + Self { media: document.child("media") } + } + + pub fn bitmaps(&self) -> impl Iterator> + 'a { + let media = self.media; + media.into_iter().flat_map(|media| media.elements_named("DOMBitmapItem")).map(|element| Bitmap { element }) + } + + pub fn sounds(&self) -> impl Iterator> + 'a { + let media = self.media; + media.into_iter().flat_map(|media| media.elements_named("DOMSoundItem")).map(|element| Sound { element }) + } + + pub fn videos(&self) -> impl Iterator> + 'a { + let media = self.media; + media.into_iter().flat_map(|media| media.elements_named("DOMVideoItem")).map(|element| Video { element }) + } +} + +/// Native bitmap library metadata. +#[derive(Clone, Copy, Debug)] +pub struct Bitmap<'a, E: XflElement> { + element: &'a E, +} + +impl<'a, E: XflElement> Bitmap<'a, E> { + pub fn name(&self) -> Option<&'a str> { + self.element.attr("name") + } + + pub fn href(&self) -> Option<&'a str> { + self.element.attr("href") + } + + pub fn data_href(&self) -> Option<&'a str> { + self.element.attr("bitmapDataHRef") + } + + pub fn allow_smoothing(&self) -> Option { + self.element.attr("allowSmoothing").map(|value| value.trim() == "true") + } +} + +/// Native sound library metadata. +#[derive(Clone, Copy, Debug)] +pub struct Sound<'a, E: XflElement> { + element: &'a E, +} + +impl<'a, E: XflElement> Sound<'a, E> { + pub fn name(&self) -> Option<&'a str> { + self.element.attr("name") + } + + pub fn href(&self) -> Option<&'a str> { + self.element.attr("href") + } + + pub fn data_href(&self) -> Option<&'a str> { + self.element.attr("soundDataHRef") + } +} + +/// Native video library metadata. +#[derive(Clone, Copy, Debug)] +pub struct Video<'a, E: XflElement> { + element: &'a E, +} + +impl<'a, E: XflElement> Video<'a, E> { + pub fn name(&self) -> Option<&'a str> { + self.element.attr("name") + } + + pub fn href(&self) -> Option<&'a str> { + self.element.attr("href") + } + + pub fn external_path(&self) -> Option<&'a str> { + self.element.attr("sourceExternalFilepath") + } + + pub fn data_href(&self) -> Option<&'a str> { + self.element.attr("videoDataHRef") + } + + pub fn length(&self) -> Option { + number(self.element, "length") + } + + pub fn frame_count(&self) -> Option { + number(self.element, "frameCount") + } + + pub fn frames_per_second(&self) -> Option { + number(self.element, "fps") + } +} + +fn number(element: &impl XflElement, name: &str) -> Option { + element.attr(name)?.trim().parse::().ok().filter(|value| value.is_finite()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn parse(source: &[u8]) -> craft_xml::El { + craft_xml::Options::default().parse(source).unwrap() + } + + #[test] + fn reads_native_media_metadata_in_order_and_ignores_other_records() { + let root = parse( + br#" + + + + + + "#, + ); + let catalog = Catalog::new(&root); + let bitmaps: Vec<_> = catalog.bitmaps().collect(); + assert_eq!(bitmaps.len(), 2); + assert_eq!(bitmaps[0].name(), Some("folder/a")); + assert_eq!(bitmaps[0].href(), Some("a.png")); + assert_eq!(bitmaps[0].data_href(), Some("Bitmap 1")); + assert_eq!(bitmaps[0].allow_smoothing(), Some(false)); + assert_eq!( + catalog.sounds().next().map(|item| (item.name(), item.href(), item.data_href())), + Some((Some("s"), Some("s.wav"), Some("Sound 1"))) + ); + let video = catalog.videos().next().expect("video record"); + assert_eq!(video.external_path(), Some("./LIBRARY/v.flv")); + assert_eq!(video.data_href(), Some("Video 1")); + assert_eq!(video.length(), Some(3.5)); + assert_eq!(video.frame_count(), Some(70.0)); + assert_eq!(video.frames_per_second(), Some(20.0)); + } + + #[test] + fn missing_or_invalid_media_metadata_stays_optional() { + let root = parse(br#""#); + let video = Catalog::new(&root).videos().next().expect("video record"); + assert_eq!(video.name(), Some("v")); + assert_eq!(video.length(), None); + assert_eq!(video.frames_per_second(), None); + let empty = parse(b""); + assert_eq!(Catalog::new(&empty).bitmaps().count(), 0); + } + + #[test] + fn native_media_references_resolve_to_the_legacy_archive_members() { + assert_eq!(library_member("folder/a.png"), "LIBRARY/folder/a.png"); + assert_eq!(data_member("Bitmap 1"), "bin/Bitmap 1"); + assert_eq!(packaged_external_member("./LIBRARY\\v.flv").as_deref(), Some("LIBRARY/v.flv")); + assert_eq!(packaged_external_member("/outside/v.flv"), None); + } +} diff --git a/crates/xfl/src/shape.rs b/crates/xfl/src/shape.rs new file mode 100644 index 000000000..ec7c0d8fc --- /dev/null +++ b/crates/xfl/src/shape.rs @@ -0,0 +1,500 @@ +//! Borrowed views of native XFL shape and style elements. +//! +//! These views recognize XFL's element names and attribute grammar without depending on an +//! application's geometry, paint, or animation types. They work with `craft_xml::El` and with +//! compatible borrowed element adapters implemented by consumers. + +use crate::edges::{self, Segment}; + +/// An element source used by the borrowed XFL schema views. +/// +/// Consumers with an existing XML facade can implement this small interface and use the same +/// XFL parser without converting or reparsing their tree. Attribute lookup must return the last +/// occurrence when an XML document repeats an attribute, matching the native XFL reader's +/// historical behavior. +pub trait XflElement { + /// Iterator type for direct child elements. + type Elements<'a>: Iterator + where + Self: 'a; + + /// The element's local name, with any namespace prefix removed. + fn local_name(&self) -> &str; + + /// The last attribute with this exact name. + fn attr(&self, name: &str) -> Option<&str>; + + /// Direct child elements in document order. + fn elements(&self) -> Self::Elements<'_>; + + /// The first direct child whose local name matches. + fn child(&self, name: &str) -> Option<&Self> { + self.elements().find(|element| element.local_name() == name) + } + + /// Direct children whose local name matches, in document order. + fn elements_named<'a>(&'a self, name: &'a str) -> impl Iterator + 'a { + self.elements().filter(move |element| element.local_name() == name) + } +} + +/// Direct child elements of `craft_xml::El`, used by its `XflElement` implementation. +pub struct XmlElements<'a> { + nodes: std::slice::Iter<'a, craft_xml::Node>, +} + +impl<'a> Iterator for XmlElements<'a> { + type Item = &'a craft_xml::El; + + fn next(&mut self) -> Option { + self.nodes.find_map(|node| match node { + craft_xml::Node::El(element) => Some(element), + _ => None, + }) + } +} + +impl XflElement for craft_xml::El { + type Elements<'a> = XmlElements<'a>; + + fn local_name(&self) -> &str { + self.name.rsplit(':').next().unwrap_or(&self.name) + } + + fn attr(&self, name: &str) -> Option<&str> { + self.attrs.iter().rev().find(|(key, _)| key == name).map(|(_, value)| value.as_str()) + } + + fn elements(&self) -> Self::Elements<'_> { + XmlElements { nodes: self.children.iter() } + } +} + +/// XFL matrix coefficients, in the native units stored in XML. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Matrix { + pub a: f32, + pub b: f32, + pub c: f32, + pub d: f32, + pub tx: f32, + pub ty: f32, +} + +/// A color encoded by an XFL color attribute and its optional alpha attribute. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Color { + pub rgb: u32, + pub alpha: f32, +} + +impl Matrix { + /// The matrix used when an XFL element omits its transform. + pub const IDENTITY: Matrix = Matrix { a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 0.0, ty: 0.0 }; + + fn finite(self) -> bool { + [self.a, self.b, self.c, self.d, self.tx, self.ty].into_iter().all(f32::is_finite) + } +} + +fn number(element: &impl XflElement, name: &str) -> Option { + element.attr(name)?.trim().parse::().ok().filter(|value| value.is_finite()) +} + +fn integer(element: &impl XflElement, name: &str) -> Option { + element.attr(name)?.trim().parse::().ok() +} + +fn boolean(element: &impl XflElement, name: &str) -> Option { + element.attr(name).map(|value| value.trim() == "true") +} + +/// Parses XFL's hexadecimal color and alpha attributes. Missing or malformed colors use black; +/// missing, malformed, or non-finite alpha uses full opacity, matching the legacy importer. +pub fn color(element: &impl XflElement, color_attribute: &str, alpha_attribute: &str) -> Color { + let rgb = element.attr(color_attribute).and_then(|value| u32::from_str_radix(value.trim().trim_start_matches('#'), 16).ok()).unwrap_or(0); + let alpha = number(element, alpha_attribute).unwrap_or(1.0).clamp(0.0, 1.0); + Color { rgb, alpha } +} + +/// Reads the native `` transform, using XFL's identity defaults. +/// A non-finite coefficient makes the entire transform identity, matching the previous reader. +pub fn matrix(element: &impl XflElement) -> Matrix { + let Some(matrix) = element.child("matrix").and_then(|wrapper| wrapper.child("Matrix")) else { + return Matrix::IDENTITY; + }; + let parsed = Matrix { + a: number(matrix, "a").unwrap_or(1.0), + b: number(matrix, "b").unwrap_or(0.0), + c: number(matrix, "c").unwrap_or(0.0), + d: number(matrix, "d").unwrap_or(1.0), + tx: number(matrix, "tx").unwrap_or(0.0), + ty: number(matrix, "ty").unwrap_or(0.0), + }; + if parsed.finite() { parsed } else { Matrix::IDENTITY } +} + +/// A native `DOMShape` or drawing shape and its XFL style tables. +#[derive(Clone, Copy, Debug)] +pub struct Shape<'a, E: XflElement> { + element: &'a E, +} + +impl<'a, E: XflElement> Shape<'a, E> { + /// Views an element as an XFL shape when its local name is `DOMShape`. + pub fn new(element: &'a E) -> Option { + (element.local_name() == "DOMShape").then_some(Self { element }) + } + + /// Native shape transform. + pub fn matrix(&self) -> Matrix { + matrix(self.element) + } + + /// Whether Animate stores the shape as a drawing object. + pub fn is_drawing_object(&self) -> Option { + boolean(self.element, "isDrawingObject") + } + + /// Fill styles, preserving their native order and index values. + pub fn fills(&self) -> impl Iterator> + '_ { + self.element.child("fills").into_iter().flat_map(|table| table.elements_named("FillStyle")).map(|element| FillStyle { element }) + } + + /// Stroke styles, preserving their native order and index values. + pub fn strokes(&self) -> impl Iterator> + '_ { + self.element.child("strokes").into_iter().flat_map(|table| table.elements_named("StrokeStyle")).map(|element| StrokeStyle { element }) + } + + /// Native edge records, preserving malformed and unknown data for consumer policy. + pub fn edges(&self) -> impl Iterator> + '_ { + self.element.child("edges").into_iter().flat_map(|table| table.elements_named("Edge")).map(|element| Edge { element }) + } +} + +/// A native `FillStyle` entry. +#[derive(Clone, Copy, Debug)] +pub struct FillStyle<'a, E: XflElement> { + element: &'a E, +} + +impl<'a, E: XflElement> FillStyle<'a, E> { + pub fn index(&self) -> Option { + integer(self.element, "index") + } + + /// The first recognized fill definition inside this table entry. + pub fn definition(&self) -> Option> { + self.element.elements().next().map(|element| Fill { element }) + } +} + +/// Native fill definitions understood by XFL. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum FillKind { + SolidColor, + LinearGradient, + RadialGradient, + Bitmap, + Other, +} + +/// A native XFL fill definition. +#[derive(Clone, Copy, Debug)] +pub struct Fill<'a, E: XflElement> { + element: &'a E, +} + +impl<'a, E: XflElement> Fill<'a, E> { + /// Views any native fill-definition element, including fills nested in primitives or strokes. + pub fn new(element: &'a E) -> Self { + Self { element } + } + + /// Underlying native definition element for consumers that map it into application types. + pub fn element(&self) -> &'a E { + self.element + } + + pub fn kind(&self) -> FillKind { + match self.element.local_name() { + "SolidColor" => FillKind::SolidColor, + "LinearGradient" => FillKind::LinearGradient, + "RadialGradient" => FillKind::RadialGradient, + "BitmapFill" => FillKind::Bitmap, + _ => FillKind::Other, + } + } + + pub fn color(&self) -> Option<&str> { + self.element.attr("color") + } + + pub fn color_value(&self) -> Color { + color(self.element, "color", "alpha") + } + + pub fn alpha(&self) -> Option { + number(self.element, "alpha") + } + + pub fn matrix(&self) -> Matrix { + matrix(self.element) + } + + pub fn spread_method(&self) -> Option<&str> { + self.element.attr("spreadMethod") + } + + pub fn interpolation_method(&self) -> Option<&str> { + self.element.attr("interpolationMethod") + } + + pub fn focal_point_ratio(&self) -> Option { + number(self.element, "focalPointRatio") + } + + pub fn bitmap_path(&self) -> Option<&str> { + self.element.attr("bitmapPath") + } + + pub fn bitmap_is_clipped(&self) -> Option { + boolean(self.element, "bitmapIsClipped") + } + + /// Gradient entries, preserving source order and raw native ratios. + pub fn gradient_entries(&self) -> impl Iterator> + '_ { + self.element.elements_named("GradientEntry").map(|element| GradientEntry { element }) + } +} + +/// A native gradient ramp entry. +#[derive(Clone, Copy, Debug)] +pub struct GradientEntry<'a, E: XflElement> { + element: &'a E, +} + +impl GradientEntry<'_, E> { + pub fn ratio(&self) -> Option { + number(self.element, "ratio") + } + + pub fn color(&self) -> Option<&str> { + self.element.attr("color") + } + + pub fn color_value(&self) -> Color { + color(self.element, "color", "alpha") + } + + pub fn alpha(&self) -> Option { + number(self.element, "alpha") + } +} + +/// Native stroke definitions understood by XFL. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum StrokeKind { + Solid, + Dashed, + Dotted, + Ragged, + Stipple, + Hatched, + Other, +} + +/// A native `StrokeStyle` table entry. +#[derive(Clone, Copy, Debug)] +pub struct StrokeStyle<'a, E: XflElement> { + element: &'a E, +} + +impl<'a, E: XflElement> StrokeStyle<'a, E> { + pub fn index(&self) -> Option { + integer(self.element, "index") + } + + /// The first stroke definition inside this table entry. + pub fn definition(&self) -> Option> { + self.element.elements().next().map(|element| Stroke { element }) + } +} + +/// A native XFL stroke definition. +#[derive(Clone, Copy, Debug)] +pub struct Stroke<'a, E: XflElement> { + element: &'a E, +} + +impl<'a, E: XflElement> Stroke<'a, E> { + /// Views a native stroke-definition element. + pub fn new(element: &'a E) -> Self { + Self { element } + } + + /// Underlying native definition element for consumers that map it into application types. + pub fn element(&self) -> &'a E { + self.element + } + + pub fn kind(&self) -> StrokeKind { + match self.element.local_name() { + "SolidStroke" => StrokeKind::Solid, + "DashedStroke" => StrokeKind::Dashed, + "DottedStroke" => StrokeKind::Dotted, + "RaggedStroke" => StrokeKind::Ragged, + "StippleStroke" => StrokeKind::Stipple, + "HatchedStroke" => StrokeKind::Hatched, + _ => StrokeKind::Other, + } + } + + pub fn weight(&self) -> Option { + number(self.element, "weight") + } + + pub fn caps(&self) -> Option<&str> { + self.element.attr("caps") + } + + pub fn joints(&self) -> Option<&str> { + self.element.attr("joints") + } + + pub fn miter_limit(&self) -> Option { + number(self.element, "miterLimit") + } + + pub fn dash1(&self) -> Option { + number(self.element, "dash1") + } + + pub fn dash2(&self) -> Option { + number(self.element, "dash2") + } + + pub fn dot_space(&self) -> Option { + number(self.element, "dotSpace") + } + + /// The first fill definition nested in the stroke. + pub fn fill(&self) -> Option> { + self.element.child("fill")?.elements().next().map(|element| Fill { element }) + } +} + +/// A native `` record and its style indices. +#[derive(Clone, Copy, Debug)] +pub struct Edge<'a, E: XflElement> { + element: &'a E, +} + +impl Edge<'_, E> { + pub fn fill_style0(&self) -> Option { + integer(self.element, "fillStyle0") + } + + pub fn fill_style1(&self) -> Option { + integer(self.element, "fillStyle1") + } + + pub fn stroke_style(&self) -> Option { + integer(self.element, "strokeStyle") + } + + pub fn encoded_segments(&self) -> Option<&str> { + self.element.attr("edges") + } + + /// Parsed XFL path segments, using the shared bounded edge grammar. + pub fn segments(&self) -> Vec { + self.encoded_segments().map(edges::parse_edge_segments).unwrap_or_default() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn parse(source: &[u8]) -> craft_xml::El { + craft_xml::Options::default().parse(source).unwrap() + } + + #[test] + fn reads_prefixed_shape_fill_stroke_and_edge_schema() { + let root = parse(br##" + + + + + + + "##); + + let shape = Shape::new(&root).unwrap(); + assert_eq!(shape.matrix(), Matrix { a: 2.0, b: 0.0, c: 0.0, d: 1.0, tx: 12.0, ty: 0.0 }); + assert_eq!(shape.is_drawing_object(), Some(false)); + let fills: Vec<_> = shape.fills().collect(); + assert_eq!(fills.iter().map(FillStyle::index).collect::>(), [Some(1), Some(2)]); + let gradient = fills[0].definition().unwrap(); + assert_eq!(gradient.kind(), FillKind::LinearGradient); + assert_eq!(gradient.spread_method(), Some("reflect")); + assert_eq!(gradient.interpolation_method(), Some("linearRGB")); + assert_eq!(gradient.gradient_entries().map(|entry| entry.ratio()).collect::>(), [Some(0.0), Some(255.0)]); + let bitmap = fills[1].definition().unwrap(); + assert_eq!(bitmap.kind(), FillKind::Bitmap); + assert_eq!(bitmap.bitmap_path(), Some("Library/a.png")); + assert_eq!(bitmap.bitmap_is_clipped(), Some(true)); + + let stroke_style = shape.strokes().next().unwrap(); + let stroke = stroke_style.definition().unwrap(); + assert_eq!(stroke.kind(), StrokeKind::Dashed); + assert_eq!(stroke.weight(), Some(2.0)); + assert_eq!((stroke.dash1(), stroke.dash2()), (Some(8.0), Some(3.0))); + assert_eq!(stroke.fill().unwrap().color(), Some("#abcdef")); + let color = stroke.fill().unwrap().color_value(); + assert_eq!(color, Color { rgb: 0xabcdef, alpha: 1.0 }); + + let edge = shape.edges().next().unwrap(); + assert_eq!((edge.fill_style0(), edge.fill_style1(), edge.stroke_style()), (Some(2), Some(1), Some(3))); + assert_eq!(edge.segments().len(), 1); + } + + #[test] + fn duplicate_attributes_use_last_value_and_invalid_numbers_are_absent() { + let root = parse(b""); + let shape = Shape::new(&root).unwrap(); + assert_eq!(shape.is_drawing_object(), Some(true)); + assert_eq!(shape.matrix(), Matrix::IDENTITY); + let fill = shape.fills().next().unwrap(); + assert_eq!(fill.index(), Some(4)); + let fill = fill.definition().unwrap(); + assert_eq!(fill.alpha(), None); + assert_eq!(fill.color_value(), Color { rgb: 0, alpha: 1.0 }); + } + + #[test] + fn an_application_element_adapter_can_use_the_same_views() { + #[derive(Debug)] + struct AppElement { + name: String, + attrs: Vec<(String, String)>, + children: Vec, + } + impl XflElement for AppElement { + type Elements<'a> = std::slice::Iter<'a, AppElement>; + fn local_name(&self) -> &str { + self.name.rsplit(':').next().unwrap_or(&self.name) + } + fn attr(&self, name: &str) -> Option<&str> { + self.attrs.iter().rev().find(|(key, _)| key == name).map(|(_, value)| value.as_str()) + } + fn elements(&self) -> Self::Elements<'_> { + self.children.iter() + } + } + let app = AppElement { name: "x:DOMShape".into(), attrs: vec![("isDrawingObject".into(), "true".into())], children: Vec::new() }; + assert_eq!(Shape::new(&app).unwrap().is_drawing_object(), Some(true)); + } +} From 352a410332c1c1d53bbb358b47cfd53ce18579b4 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 00:18:46 +0000 Subject: [PATCH 2/4] Expose native XFL drawing primitives --- crates/xfl/src/shape.rs | 80 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/crates/xfl/src/shape.rs b/crates/xfl/src/shape.rs index ec7c0d8fc..8b0667a1a 100644 --- a/crates/xfl/src/shape.rs +++ b/crates/xfl/src/shape.rs @@ -140,6 +140,68 @@ pub struct Shape<'a, E: XflElement> { element: &'a E, } +/// Kind of native drawable primitive stored in XFL. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum PrimitiveKind { + Rectangle, + Oval, +} + +/// Native rectangle or oval object, including its native style definitions. +#[derive(Clone, Copy, Debug)] +pub struct Primitive<'a, E: XflElement> { + element: &'a E, + kind: PrimitiveKind, +} + +impl<'a, E: XflElement> Primitive<'a, E> { + /// Views a supported native primitive by its local element name. + pub fn new(element: &'a E) -> Option { + let kind = match element.local_name() { + "DOMRectangleObject" => PrimitiveKind::Rectangle, + "DOMOvalObject" => PrimitiveKind::Oval, + _ => return None, + }; + Some(Self { element, kind }) + } + + pub fn kind(&self) -> PrimitiveKind { + self.kind + } + + pub fn x(&self) -> Option { + number(self.element, "x") + } + + pub fn y(&self) -> Option { + number(self.element, "y") + } + + pub fn width(&self) -> Option { + number(self.element, "objectWidth") + } + + pub fn height(&self) -> Option { + number(self.element, "objectHeight") + } + + pub fn top_left_radius(&self) -> Option { + number(self.element, "topLeftRadius") + } + + pub fn matrix(&self) -> Matrix { + matrix(self.element) + } + + pub fn fill(&self) -> Option> { + self.element.child("fill")?.elements().next().map(|element| Fill { element }) + } + + pub fn stroke(&self) -> Option> { + self.element.child("stroke")?.elements().next().map(|element| Stroke { element }) + } +} + impl<'a, E: XflElement> Shape<'a, E> { /// Views an element as an XFL shape when its local name is `DOMShape`. pub fn new(element: &'a E) -> Option { @@ -497,4 +559,22 @@ mod tests { let app = AppElement { name: "x:DOMShape".into(), attrs: vec![("isDrawingObject".into(), "true".into())], children: Vec::new() }; assert_eq!(Shape::new(&app).unwrap().is_drawing_object(), Some(true)); } + + #[test] + fn reads_native_rectangle_and_oval_geometry_and_styles() { + let root = parse(br##""##); + let primitive = Primitive::new(&root).unwrap(); + assert_eq!(primitive.kind(), PrimitiveKind::Rectangle); + assert_eq!( + (primitive.x(), primitive.y(), primitive.width(), primitive.height(), primitive.top_left_radius()), + (Some(-2.0), Some(3.0), Some(20.0), Some(10.0), Some(4.0)) + ); + assert_eq!(primitive.matrix().tx, 7.0); + assert_eq!(primitive.fill().unwrap().kind(), FillKind::SolidColor); + assert_eq!(primitive.stroke().unwrap().kind(), StrokeKind::Dashed); + + let oval = parse(b""); + assert_eq!(Primitive::new(&oval).unwrap().kind(), PrimitiveKind::Oval); + assert!(Primitive::new(&parse(b"")).is_none()); + } } From e4eddc5fe55a2cced3854a14f1ef326a20ad449b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 00:23:42 +0000 Subject: [PATCH 3/4] Preserve legacy XFL style table permissiveness --- crates/xfl/src/shape.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/crates/xfl/src/shape.rs b/crates/xfl/src/shape.rs index 8b0667a1a..24074e165 100644 --- a/crates/xfl/src/shape.rs +++ b/crates/xfl/src/shape.rs @@ -220,12 +220,12 @@ impl<'a, E: XflElement> Shape<'a, E> { /// Fill styles, preserving their native order and index values. pub fn fills(&self) -> impl Iterator> + '_ { - self.element.child("fills").into_iter().flat_map(|table| table.elements_named("FillStyle")).map(|element| FillStyle { element }) + self.element.child("fills").into_iter().flat_map(|table| table.elements()).map(|element| FillStyle { element }) } /// Stroke styles, preserving their native order and index values. pub fn strokes(&self) -> impl Iterator> + '_ { - self.element.child("strokes").into_iter().flat_map(|table| table.elements_named("StrokeStyle")).map(|element| StrokeStyle { element }) + self.element.child("strokes").into_iter().flat_map(|table| table.elements()).map(|element| StrokeStyle { element }) } /// Native edge records, preserving malformed and unknown data for consumer policy. @@ -245,7 +245,7 @@ impl<'a, E: XflElement> FillStyle<'a, E> { integer(self.element, "index") } - /// The first recognized fill definition inside this table entry. + /// The first native definition element inside this table entry. pub fn definition(&self) -> Option> { self.element.elements().next().map(|element| Fill { element }) } @@ -377,7 +377,7 @@ impl<'a, E: XflElement> StrokeStyle<'a, E> { integer(self.element, "index") } - /// The first stroke definition inside this table entry. + /// The first native definition element inside this table entry. pub fn definition(&self) -> Option> { self.element.elements().next().map(|element| Stroke { element }) } @@ -536,6 +536,18 @@ mod tests { assert_eq!(fill.color_value(), Color { rgb: 0, alpha: 1.0 }); } + #[test] + fn style_table_views_keep_the_legacy_first_child_and_index_behavior() { + let root = parse(b""); + let shape = Shape::new(&root).unwrap(); + let fill_style = shape.fills().next().unwrap(); + assert_eq!(fill_style.index(), Some(5)); + assert_eq!(fill_style.definition().map(|fill| fill.kind()), Some(FillKind::SolidColor)); + let stroke_style = shape.strokes().next().unwrap(); + assert_eq!(stroke_style.index(), Some(6)); + assert_eq!(stroke_style.definition().map(|stroke| stroke.kind()), Some(StrokeKind::Solid)); + } + #[test] fn an_application_element_adapter_can_use_the_same_views() { #[derive(Debug)] From d5a77b7dc1f8b6445f26581c0a4ea63de7d25186 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 00:33:05 +0000 Subject: [PATCH 4/4] Preserve legacy XFL matrix coefficient defaults --- crates/xfl/src/shape.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/crates/xfl/src/shape.rs b/crates/xfl/src/shape.rs index 24074e165..bcd3e9373 100644 --- a/crates/xfl/src/shape.rs +++ b/crates/xfl/src/shape.rs @@ -91,10 +91,6 @@ pub struct Color { impl Matrix { /// The matrix used when an XFL element omits its transform. pub const IDENTITY: Matrix = Matrix { a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 0.0, ty: 0.0 }; - - fn finite(self) -> bool { - [self.a, self.b, self.c, self.d, self.tx, self.ty].into_iter().all(f32::is_finite) - } } fn number(element: &impl XflElement, name: &str) -> Option { @@ -117,21 +113,23 @@ pub fn color(element: &impl XflElement, color_attribute: &str, alpha_attribute: Color { rgb, alpha } } -/// Reads the native `` transform, using XFL's identity defaults. -/// A non-finite coefficient makes the entire transform identity, matching the previous reader. +/// Reads the native `` transform. +/// +/// Each absent, malformed, or non-finite coefficient uses its identity default independently. +/// This preserves the prior AniCraft reader's `Element::f32` behavior, including retaining other +/// valid coefficients when one coefficient is invalid. pub fn matrix(element: &impl XflElement) -> Matrix { let Some(matrix) = element.child("matrix").and_then(|wrapper| wrapper.child("Matrix")) else { return Matrix::IDENTITY; }; - let parsed = Matrix { + Matrix { a: number(matrix, "a").unwrap_or(1.0), b: number(matrix, "b").unwrap_or(0.0), c: number(matrix, "c").unwrap_or(0.0), d: number(matrix, "d").unwrap_or(1.0), tx: number(matrix, "tx").unwrap_or(0.0), ty: number(matrix, "ty").unwrap_or(0.0), - }; - if parsed.finite() { parsed } else { Matrix::IDENTITY } + } } /// A native `DOMShape` or drawing shape and its XFL style tables. @@ -525,10 +523,10 @@ mod tests { #[test] fn duplicate_attributes_use_last_value_and_invalid_numbers_are_absent() { - let root = parse(b""); + let root = parse(b""); let shape = Shape::new(&root).unwrap(); assert_eq!(shape.is_drawing_object(), Some(true)); - assert_eq!(shape.matrix(), Matrix::IDENTITY); + assert_eq!(shape.matrix(), Matrix { tx: 12.0, ..Matrix::IDENTITY }); let fill = shape.fills().next().unwrap(); assert_eq!(fill.index(), Some(4)); let fill = fill.definition().unwrap();