Skip to content

Commit 64aeb11

Browse files
committed
add svg_path_to_handle function
1 parent 6483179 commit 64aeb11

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

src/widglets.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
1-
use crate::module::ModuleMessage;
1+
use std::{fs, path::PathBuf};
2+
23
use iced::widget;
34

5+
use resvg;
6+
7+
use crate::module::ModuleMessage;
8+
49
const 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)]
738
pub enum HeadingLevel {

0 commit comments

Comments
 (0)