diff --git a/reaction_systems_gui/Cargo.toml b/reaction_systems_gui/Cargo.toml index 7527355..902ad08 100644 --- a/reaction_systems_gui/Cargo.toml +++ b/reaction_systems_gui/Cargo.toml @@ -14,7 +14,7 @@ serde = { version = "1", optional = true } colored = "*" lalrpop-util = "*" petgraph = ">=0.8" -nsvg = "0.5.1" +resvg = "*" dyn-clone = "*" petgraph-graphml = "*" egui_node_graph2 = { path = "../egui_node_graph2" } diff --git a/reaction_systems_gui/src/app_logic.rs b/reaction_systems_gui/src/app_logic.rs index 94f34de..fae3b1b 100644 --- a/reaction_systems_gui/src/app_logic.rs +++ b/reaction_systems_gui/src/app_logic.rs @@ -2532,8 +2532,7 @@ fn process_template( Ok(svg) => svg, Err(e) => anyhow::bail!(e), }; - let raw = svg.into_raw(); - *to_ret = Some(BasicValue::SaveBytes { path, value: raw }); + *to_ret = Some(BasicValue::SaveBytes { path, value: svg }); }, | (BasicValue::Path { .. }, _) => { anyhow::bail!("Not an svg"); diff --git a/reaction_systems_gui/src/svg.rs b/reaction_systems_gui/src/svg.rs index 983feef..28d3571 100644 --- a/reaction_systems_gui/src/svg.rs +++ b/reaction_systems_gui/src/svg.rs @@ -41,19 +41,20 @@ impl Svg { ); let content = svg.finalize(); - let svg = match nsvg::parse_str(&content, nsvg::Units::Pixel, 96.0) { + let svg_tree = match resvg::usvg::Tree::from_str(&content, &resvg::usvg::Options::default()) { Ok(svg) => svg, - Err(nsvg_err) => return Err(format!("{}", nsvg_err)), + Err(err) => return Err(format!("{}", err)), }; - let svg_size = egui::vec2(svg.width(), svg.height()); + let svg_size = egui::vec2(svg_tree.size().width(), svg_tree.size().height()); - let (w, h, data) = match svg.rasterize_to_raw_rgba(1.) { - Ok(o) => o, - Err(e) => return Err(format!("{}", e)), - }; + let mut pixmap = resvg::tiny_skia::Pixmap::new(svg_size.x as _, svg_size.y as _) + .expect("Could not allocate svg"); + let pixmap_mut = &mut pixmap.as_mut(); + resvg::render(&svg_tree, Default::default(), pixmap_mut); + let pixmap = pixmap_mut.to_owned(); - let image = egui::ColorImage::from_rgba_unmultiplied([w as _, h as _], &data); + let image = egui::ColorImage::from_rgba_unmultiplied([pixmap.width() as _, pixmap.height() as _], pixmap.data()); let svg = Svg { image, original: content, @@ -75,17 +76,21 @@ impl Svg { } } - pub(crate) fn rasterize(&self) -> Result, Vec>, String> { - let svg = match nsvg::parse_str(&self.original, nsvg::Units::Pixel, 96.0) { + pub(crate) fn rasterize(&self) -> Result, String> { + let svg_tree = match resvg::usvg::Tree::from_str(&self.original, &resvg::usvg::Options::default()) { Ok(svg) => svg, - Err(nsvg_err) => return Err(format!("{}", nsvg_err)), - }; - let data = match svg.rasterize(1.) { - Ok(o) => o, - Err(e) => return Err(format!("{}", e)), + Err(err) => return Err(format!("{}", err)), }; + let mut pixmap = resvg::tiny_skia::Pixmap::new(self.svg_size.x as _, self.svg_size.y as _) + .expect("Could not allocate svg"); + let pixmap_mut = &mut pixmap.as_mut(); + resvg::render(&svg_tree, Default::default(), pixmap_mut); + let pixmap = pixmap_mut.to_owned(); - Ok(data) + match pixmap.encode_png() { + Ok(png) => Ok(png), + Err(e) => Err(format!("{}", e)), + } } }