@@ -3,21 +3,15 @@ use base64::Engine;
33pub use graph_craft:: document:: value:: RenderOutputType ;
44pub use graph_craft:: wasm_application_io:: * ;
55use graphene_application_io:: ApplicationIo ;
6- #[ cfg( target_family = "wasm" ) ]
76use graphene_core:: gradient:: GradientStops ;
8- #[ cfg( target_family = "wasm" ) ]
97use graphene_core:: math:: bbox:: Bbox ;
108use graphene_core:: raster:: image:: Image ;
119use graphene_core:: raster_types:: { CPU , Raster } ;
1210use graphene_core:: table:: Table ;
13- #[ cfg( target_family = "wasm" ) ]
1411use graphene_core:: transform:: Footprint ;
15- #[ cfg( target_family = "wasm" ) ]
1612use graphene_core:: vector:: Vector ;
1713use graphene_core:: { Color , Ctx } ;
18- #[ cfg( target_family = "wasm" ) ]
1914use graphene_core:: { Graphic , WasmNotSend } ;
20- #[ cfg( target_family = "wasm" ) ]
2115use graphene_svg_renderer:: { Render , RenderParams , RenderSvgSegmentList , SvgRender } ;
2216use std:: sync:: Arc ;
2317#[ cfg( target_family = "wasm" ) ]
@@ -209,3 +203,72 @@ where
209203..Default :: default ( )
210204} )
211205}
206+
207+ #[ cfg( not( target_family = "wasm" ) ) ]
208+ #[ node_macro:: node( category( "" ) ) ]
209+ async fn rasterize < ' a : ' n , T : WasmNotSend + ' n > (
210+ _: impl Ctx ,
211+ #[ implementations(
212+ Table <Vector >,
213+ Table <Raster <CPU >>,
214+ Table <Graphic >,
215+ Table <Color >,
216+ Table <GradientStops >,
217+ ) ]
218+ mut data : Table < T > ,
219+ footprint : Footprint ,
220+ wgpu_executor : & ' a wgpu_executor:: WgpuExecutor ,
221+ ) -> Table < Raster < CPU > >
222+ where
223+ Table < T > : graphene_svg_renderer:: Render ,
224+ {
225+ use graphene_core:: math:: bbox:: Bbox ;
226+ use graphene_core:: table:: TableRow ;
227+ use wgpu_executor:: RenderContext ;
228+
229+ if footprint. transform . matrix2 . determinant ( ) == 0. {
230+ log:: trace!( "Invalid footprint received for rasterization" ) ;
231+ return Table :: new ( ) ;
232+ }
233+
234+ let aabb = Bbox :: from_transform ( footprint. transform ) . to_axis_aligned_bbox ( ) ;
235+ let resolution = footprint. resolution ;
236+
237+ // Adjust data transforms to account for bounding box offset
238+ for row in data. iter_mut ( ) {
239+ * row. transform = glam:: DAffine2 :: from_translation ( -aabb. start ) * * row. transform ;
240+ }
241+
242+ // Create Vello scene and render context
243+ let mut scene = vello:: Scene :: new ( ) ;
244+ let mut context = RenderContext :: default ( ) ;
245+
246+ // Render data to Vello scene
247+ let render_params = graphene_svg_renderer:: RenderParams {
248+ footprint : Footprint :: default ( ) ,
249+ for_export : true ,
250+ ..Default :: default ( )
251+ } ;
252+ data. render_to_vello ( & mut scene, Default :: default ( ) , & mut context, & render_params) ;
253+
254+ // Render scene to texture
255+ let background = graphene_core:: Color :: TRANSPARENT ;
256+ let wgpu_texture = wgpu_executor
257+ . render_vello_scene_to_texture ( & scene, resolution, & context, background)
258+ . await
259+ . expect ( "Failed to render Vello scene to texture" ) ;
260+
261+ // Wrap the texture in a Raster<GPU>
262+ use graphene_core:: raster_types:: GPU ;
263+ let gpu_raster = Raster :: new_gpu ( wgpu_texture) ;
264+
265+ // Convert GPU raster to CPU raster using Convert trait
266+ use graphene_core:: ops:: Convert ;
267+ let cpu_raster = gpu_raster. convert ( Footprint :: default ( ) , wgpu_executor) . await ;
268+
269+ Table :: new_from_row ( TableRow {
270+ element : cpu_raster,
271+ transform : footprint. transform ,
272+ ..Default :: default ( )
273+ } )
274+ }
0 commit comments