@@ -3,7 +3,7 @@ use iced_native::svg;
33use std:: collections:: { HashMap , HashSet } ;
44
55pub enum Svg {
6- Loaded ( resvg :: usvg:: Tree ) ,
6+ Loaded ( usvg:: Tree ) ,
77 NotFound ,
88}
99
@@ -43,17 +43,15 @@ impl Cache {
4343 return self . svgs . get ( & handle. id ( ) ) . unwrap ( ) ;
4444 }
4545
46- let opt = resvg:: Options :: default ( ) ;
47-
4846 let svg = match handle. data ( ) {
4947 svg:: Data :: Path ( path) => {
50- match resvg :: usvg:: Tree :: from_file ( path, & opt . usvg ) {
48+ match usvg:: Tree :: from_file ( path, & Default :: default ( ) ) {
5149 Ok ( tree) => Svg :: Loaded ( tree) ,
5250 Err ( _) => Svg :: NotFound ,
5351 }
5452 }
5553 svg:: Data :: Bytes ( bytes) => {
56- match resvg :: usvg:: Tree :: from_data ( & bytes, & opt . usvg ) {
54+ match usvg:: Tree :: from_data ( & bytes, & Default :: default ( ) ) {
5755 Ok ( tree) => Svg :: Loaded ( tree) ,
5856 Err ( _) => Svg :: NotFound ,
5957 }
@@ -101,23 +99,38 @@ impl Cache {
10199 // We currently rerasterize the SVG when its size changes. This is slow
102100 // as heck. A GPU rasterizer like `pathfinder` may perform better.
103101 // It would be cool to be able to smooth resize the `svg` example.
104- let screen_size =
105- resvg:: ScreenSize :: new ( width, height) . unwrap ( ) ;
102+ let img = resvg:: render (
103+ tree,
104+ if width > height {
105+ usvg:: FitTo :: Width ( width)
106+ } else {
107+ usvg:: FitTo :: Height ( height)
108+ } ,
109+ None ,
110+ ) ?;
111+ let width = img. width ( ) ;
112+ let height = img. height ( ) ;
106113
107- let mut canvas =
108- resvg:: raqote:: DrawTarget :: new ( width as i32 , height as i32 ) ;
114+ let mut rgba = img. take ( ) . into_iter ( ) ;
109115
110- resvg:: backend_raqote:: render_to_canvas (
111- tree,
112- & resvg:: Options :: default ( ) ,
113- screen_size,
114- & mut canvas,
115- ) ;
116+ // TODO: Perform conversion in the GPU
117+ let bgra: Vec < u8 > = std:: iter:: from_fn ( move || {
118+ use std:: iter:: once;
119+
120+ let r = rgba. next ( ) ?;
121+ let g = rgba. next ( ) ?;
122+ let b = rgba. next ( ) ?;
123+ let a = rgba. next ( ) ?;
124+
125+ Some ( once ( b) . chain ( once ( g) ) . chain ( once ( r) ) . chain ( once ( a) ) )
126+ } )
127+ . flatten ( )
128+ . collect ( ) ;
116129
117130 let allocation = texture_atlas. upload (
118131 width,
119132 height,
120- bytemuck:: cast_slice ( canvas . get_data ( ) ) ,
133+ bytemuck:: cast_slice ( bgra . as_slice ( ) ) ,
121134 device,
122135 encoder,
123136 ) ?;
0 commit comments