File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- use crate :: module:: ModuleMessage ;
1+ use std:: { fs, path:: PathBuf } ;
2+
23use iced:: widget;
34
5+ use resvg;
6+
7+ use crate :: module:: ModuleMessage ;
8+
49const PADDING : f32 = 4.0 ;
10+ const SVG_HEIGHT : usize = 64 ;
11+ const SVG_WIDTH : usize = 64 ;
12+
13+ pub async fn svg_path_to_handle ( path : PathBuf ) -> Result < iced:: widget:: image:: Handle , String > {
14+ let contents = fs:: read_to_string ( path) . map_err ( |_e| "couldnt read path to string" ) ?;
15+ let tree = resvg:: usvg:: Tree :: from_str ( & contents, & resvg:: usvg:: Options :: default ( ) )
16+ . map_err ( |_e| "Could not turn contents to tree" ) ?;
17+
18+ let mut data: Box < [ u8 ] > = vec ! [ 0 ; SVG_HEIGHT * SVG_WIDTH * 4 ] . into_boxed_slice ( ) ;
19+
20+ let mut pixmap = resvg:: tiny_skia:: PixmapMut :: from_bytes (
21+ & mut data,
22+ SVG_WIDTH . try_into ( ) . expect ( "Cant fail" ) ,
23+ SVG_HEIGHT . try_into ( ) . expect ( "Cant fail" ) ,
24+ )
25+ . ok_or ( "Can create pixmap" ) ?;
26+
27+ let transform = resvg:: tiny_skia:: Transform :: identity ( ) ;
28+ resvg:: render ( & tree, transform, & mut pixmap) ;
29+
30+ Ok ( iced:: widget:: image:: Handle :: from_rgba (
31+ SVG_WIDTH . try_into ( ) . expect ( "cant fail" ) ,
32+ SVG_HEIGHT . try_into ( ) . expect ( "cant fail" ) ,
33+ data,
34+ ) )
35+ }
536
637#[ allow( dead_code) ]
738pub enum HeadingLevel {
You can’t perform that action at this time.
0 commit comments