From 352a410332c1c1d53bbb358b47cfd53ce18579b4 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 00:18:46 +0000 Subject: [PATCH] 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()); + } }