11mod quad;
22mod rect;
3- use dyn_any:: DynAny ;
43pub use quad:: Quad ;
54pub use rect:: Rect ;
65
@@ -13,6 +12,7 @@ use crate::Raster;
1312use crate :: { vector:: VectorData , Artboard , Color , GraphicElement , GraphicGroup } ;
1413
1514use bezier_rs:: Subpath ;
15+ use dyn_any:: { DynAny , StaticType } ;
1616
1717use base64:: Engine ;
1818use glam:: { DAffine2 , DVec2 } ;
@@ -270,7 +270,6 @@ pub fn to_transform(transform: DAffine2) -> usvg::Transform {
270270 usvg:: Transform :: from_row ( cols[ 0 ] as f32 , cols[ 1 ] as f32 , cols[ 2 ] as f32 , cols[ 3 ] as f32 , cols[ 4 ] as f32 , cols[ 5 ] as f32 )
271271}
272272
273- use dyn_any:: StaticType ;
274273#[ derive( Debug , Clone , PartialEq , DynAny ) ]
275274#[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
276275pub struct RenderMetadata {
@@ -281,19 +280,25 @@ pub struct RenderMetadata {
281280
282281pub trait GraphicElementRendered {
283282 fn render_svg ( & self , render : & mut SvgRender , render_params : & RenderParams ) ;
283+
284284 fn bounding_box ( & self , transform : DAffine2 ) -> Option < [ DVec2 ; 2 ] > ;
285+
285286 // The upstream click targets for each layer are collected during the render so that they do not have to be calculated for each click detection
286287 fn add_upstream_click_targets ( & self , _click_targets : & mut Vec < ClickTarget > ) { }
288+
287289 // TODO: Store all click targets in a vec which contains the AABB, click target, and path
288290 // fn add_click_targets(&self, click_targets: &mut Vec<([DVec2; 2], ClickTarget, Vec<NodeId>)>, current_path: Option<NodeId>) {}
291+
289292 // Recursively iterate over data in the render (including groups upstream from vector data in the case of a boolean operation) to collect the footprints, click targets, and vector modify
290293 fn collect_metadata ( & self , _metadata : & mut RenderMetadata , _footprint : Footprint , _element_id : Option < NodeId > ) { }
294+
291295 #[ cfg( feature = "vello" ) ]
292296 fn to_vello_scene ( & self , transform : DAffine2 , context : & mut RenderContext ) -> Scene {
293297 let mut scene = vello:: Scene :: new ( ) ;
294298 self . render_to_vello ( & mut scene, transform, context) ;
295299 scene
296300 }
301+
297302 #[ cfg( feature = "vello" ) ]
298303 fn render_to_vello ( & self , _scene : & mut Scene , _transform : DAffine2 , _render_condext : & mut RenderContext ) { }
299304
@@ -334,11 +339,13 @@ impl GraphicElementRendered for GraphicGroup {
334339
335340 fn collect_metadata ( & self , metadata : & mut RenderMetadata , mut footprint : Footprint , element_id : Option < NodeId > ) {
336341 footprint. transform *= self . transform ;
342+
337343 for ( element, element_id) in self . elements . iter ( ) {
338344 if let Some ( element_id) = element_id {
339345 element. collect_metadata ( metadata, footprint, Some ( * element_id) ) ;
340346 }
341347 }
348+
342349 if let Some ( graphic_group_id) = element_id {
343350 let mut all_upstream_click_targets = Vec :: new ( ) ;
344351 self . add_upstream_click_targets ( & mut all_upstream_click_targets) ;
@@ -349,10 +356,13 @@ impl GraphicElementRendered for GraphicGroup {
349356 fn add_upstream_click_targets ( & self , click_targets : & mut Vec < ClickTarget > ) {
350357 for ( element, _) in self . elements . iter ( ) {
351358 let mut new_click_targets = Vec :: new ( ) ;
359+
352360 element. add_upstream_click_targets ( & mut new_click_targets) ;
361+
353362 for click_target in new_click_targets. iter_mut ( ) {
354363 click_target. apply_transform ( element. transform ( ) )
355364 }
365+
356366 click_targets. extend ( new_click_targets) ;
357367 }
358368 }
@@ -374,9 +384,11 @@ impl GraphicElementRendered for GraphicGroup {
374384 & vello:: kurbo:: Rect :: new ( bounds[ 0 ] . x , bounds[ 0 ] . y , bounds[ 1 ] . x , bounds[ 1 ] . y ) ,
375385 ) ;
376386 }
387+
377388 for ( element, _) in self . iter ( ) {
378389 element. render_to_vello ( scene, child_transform, context) ;
379390 }
391+
380392 if layer {
381393 scene. pop_layer ( ) ;
382394 }
@@ -447,6 +459,7 @@ impl GraphicElementRendered for VectorData {
447459 . insert ( element_id, self . stroke_bezier_paths ( ) . map ( fill) . map ( |subpath| ClickTarget :: new ( subpath, stroke_width) ) . collect ( ) ) ;
448460 metadata. vector_data . insert ( element_id, self . clone ( ) ) ;
449461 }
462+
450463 if let Some ( upstream_graphic_group) = & self . upstream_graphic_group {
451464 footprint. transform *= self . transform ;
452465 upstream_graphic_group. collect_metadata ( metadata, footprint, None ) ;
@@ -615,7 +628,7 @@ impl GraphicElementRendered for Artboard {
615628
616629 write ! (
617630 & mut attributes. 0 . svg_defs,
618- r##"<clipPath id="{id}"><rect x="0" y="0" width="{}" height="{}"/></clipPath>"## ,
631+ r##"<clipPath id="{id}"><rect x="0" y="0" width="{}" height="{}" /></clipPath>"## ,
619632 self . dimensions. x, self . dimensions. y
620633 )
621634 . unwrap ( ) ;
@@ -644,11 +657,13 @@ impl GraphicElementRendered for Artboard {
644657 metadata. click_targets . insert ( element_id, vec ! [ ClickTarget :: new( subpath, 0. ) ] ) ;
645658 metadata. footprints . insert ( element_id, ( footprint, DAffine2 :: from_translation ( self . location . as_dvec2 ( ) ) ) ) ;
646659 }
660+
647661 self . graphic_group . collect_metadata ( metadata, footprint, None ) ;
648662 }
649663
650664 fn add_upstream_click_targets ( & self , click_targets : & mut Vec < ClickTarget > ) {
651665 let mut subpath = Subpath :: new_rect ( DVec2 :: ZERO , self . dimensions . as_dvec2 ( ) ) ;
666+
652667 if self . graphic_group . transform . matrix2 . determinant ( ) != 0. {
653668 subpath. apply_transform ( self . graphic_group . transform . inverse ( ) ) ;
654669 click_targets. push ( ClickTarget :: new ( subpath, 0. ) ) ;
@@ -690,6 +705,7 @@ impl GraphicElementRendered for crate::ArtboardGroup {
690705 artboard. render_svg ( render, render_params) ;
691706 }
692707 }
708+
693709 fn bounding_box ( & self , transform : DAffine2 ) -> Option < [ DVec2 ; 2 ] > {
694710 self . artboards . iter ( ) . filter_map ( |( element, _) | element. bounding_box ( transform) ) . reduce ( Quad :: combine_bounds)
695711 }
@@ -760,11 +776,11 @@ impl GraphicElementRendered for ImageFrame<Color> {
760776 }
761777
762778 fn collect_metadata ( & self , metadata : & mut RenderMetadata , footprint : Footprint , element_id : Option < NodeId > ) {
763- if let Some ( element_id) = element_id {
764- let subpath = Subpath :: new_rect ( DVec2 :: ZERO , DVec2 :: ONE ) ;
765- metadata . click_targets . insert ( element_id , vec ! [ ClickTarget :: new ( subpath , 0. ) ] ) ;
766- metadata. footprints . insert ( element_id, ( footprint , self . transform ) ) ;
767- }
779+ let Some ( element_id) = element_id else { return } ;
780+ let subpath = Subpath :: new_rect ( DVec2 :: ZERO , DVec2 :: ONE ) ;
781+
782+ metadata. click_targets . insert ( element_id, vec ! [ ClickTarget :: new ( subpath , 0. ) ] ) ;
783+ metadata . footprints . insert ( element_id , ( footprint , self . transform ) ) ;
768784 }
769785
770786 fn add_upstream_click_targets ( & self , click_targets : & mut Vec < ClickTarget > ) {
@@ -838,11 +854,11 @@ impl GraphicElementRendered for Raster {
838854 }
839855
840856 fn collect_metadata ( & self , metadata : & mut RenderMetadata , footprint : Footprint , element_id : Option < NodeId > ) {
841- if let Some ( element_id) = element_id {
842- let subpath = Subpath :: new_rect ( DVec2 :: ZERO , DVec2 :: ONE ) ;
843- metadata . click_targets . insert ( element_id , vec ! [ ClickTarget :: new ( subpath , 0. ) ] ) ;
844- metadata. footprints . insert ( element_id, ( footprint , self . transform ( ) ) ) ;
845- }
857+ let Some ( element_id) = element_id else { return } ;
858+
859+ let subpath = Subpath :: new_rect ( DVec2 :: ZERO , DVec2 :: ONE ) ;
860+ metadata. click_targets . insert ( element_id, vec ! [ ClickTarget :: new ( subpath , 0. ) ] ) ;
861+ metadata . footprints . insert ( element_id , ( footprint , self . transform ( ) ) ) ;
846862 }
847863
848864 fn add_upstream_click_targets ( & self , click_targets : & mut Vec < ClickTarget > ) {
@@ -911,6 +927,7 @@ impl GraphicElementRendered for GraphicElement {
911927 if let Some ( element_id) = element_id {
912928 metadata. footprints . insert ( element_id, ( footprint, self . transform ( ) ) ) ;
913929 }
930+
914931 match self {
915932 GraphicElement :: VectorData ( vector_data) => vector_data. collect_metadata ( metadata, footprint, element_id) ,
916933 GraphicElement :: Raster ( raster) => raster. collect_metadata ( metadata, footprint, element_id) ,
0 commit comments