@@ -3,22 +3,19 @@ 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 ;
87#[ cfg( target_family = "wasm" ) ]
98use graphene_core:: math:: bbox:: Bbox ;
109use graphene_core:: raster:: image:: Image ;
1110use graphene_core:: raster_types:: { CPU , Raster } ;
1211use graphene_core:: table:: Table ;
13- #[ cfg( target_family = "wasm" ) ]
1412use graphene_core:: transform:: Footprint ;
15- #[ cfg( target_family = "wasm" ) ]
1613use graphene_core:: vector:: Vector ;
1714use graphene_core:: { Color , Ctx } ;
18- #[ cfg( target_family = "wasm" ) ]
1915use graphene_core:: { Graphic , WasmNotSend } ;
16+ use graphene_svg_renderer:: Render ;
2017#[ cfg( target_family = "wasm" ) ]
21- use graphene_svg_renderer:: { Render , RenderParams , RenderSvgSegmentList , SvgRender } ;
18+ use graphene_svg_renderer:: { RenderParams , RenderSvgSegmentList , SvgRender } ;
2219use std:: sync:: Arc ;
2320#[ cfg( target_family = "wasm" ) ]
2421use wasm_bindgen:: JsCast ;
@@ -209,3 +206,71 @@ where
209206..Default :: default ( )
210207} )
211208}
209+
210+ #[ cfg( not( target_family = "wasm" ) ) ]
211+ #[ node_macro:: node( category( "" ) ) ]
212+ async fn rasterize < ' a : ' n , T : WasmNotSend + ' n > (
213+ _: impl Ctx ,
214+ #[ implementations(
215+ Table <Vector >,
216+ Table <Raster <CPU >>,
217+ Table <Graphic >,
218+ Table <Color >,
219+ Table <GradientStops >,
220+ ) ]
221+ mut data : Table < T > ,
222+ footprint : Footprint ,
223+ wgpu_executor : & ' a wgpu_executor:: WgpuExecutor ,
224+ ) -> Table < Raster < CPU > >
225+ where
226+ Table < T > : graphene_svg_renderer:: Render ,
227+ {
228+ use graphene_core:: math:: bbox:: Bbox ;
229+ use graphene_core:: table:: TableRow ;
230+ use wgpu_executor:: RenderContext ;
231+
232+ if footprint. transform . matrix2 . determinant ( ) == 0. {
233+ log:: trace!( "Invalid footprint received for rasterization" ) ;
234+ return Table :: new ( ) ;
235+ }
236+
237+ let aabb = Bbox :: from_transform ( footprint. transform ) . to_axis_aligned_bbox ( ) ;
238+ let resolution = footprint. resolution ;
239+
240+ // Adjust data transforms to account for bounding box offset
241+ for row in data. iter_mut ( ) {
242+ * row. transform = glam:: DAffine2 :: from_translation ( -aabb. start ) * * row. transform ;
243+ }
244+
245+ // Create Vello scene and render context
246+ let mut scene = vello:: Scene :: new ( ) ;
247+ let mut context = RenderContext :: default ( ) ;
248+
249+ // Render data to Vello scene
250+ let render_params = graphene_svg_renderer:: RenderParams {
251+ footprint : Footprint :: default ( ) ,
252+ for_export : true ,
253+ ..Default :: default ( )
254+ } ;
255+ data. render_to_vello ( & mut scene, Default :: default ( ) , & mut context, & render_params) ;
256+
257+ // Render scene to texture
258+ let background = graphene_core:: Color :: TRANSPARENT ;
259+ let wgpu_texture = wgpu_executor
260+ . render_vello_scene_to_texture ( & scene, resolution, & context, background)
261+ . await
262+ . expect ( "Failed to render Vello scene to texture" ) ;
263+
264+ // Wrap the texture in a Raster<GPU>
265+ let gpu_raster = Raster :: new_gpu ( wgpu_texture) ;
266+
267+ // Convert GPU raster to CPU raster using Convert trait
268+ use graphene_core:: ops:: Convert ;
269+ let cpu_raster = gpu_raster. convert ( Footprint :: default ( ) , wgpu_executor) . await ;
270+
271+ Table :: new_from_row ( TableRow {
272+ element : cpu_raster,
273+ transform : footprint. transform ,
274+ ..Default :: default ( )
275+ } )
276+ }
0 commit comments