different svg renderer
This commit is contained in:
@ -14,7 +14,7 @@ serde = { version = "1", optional = true }
|
|||||||
colored = "*"
|
colored = "*"
|
||||||
lalrpop-util = "*"
|
lalrpop-util = "*"
|
||||||
petgraph = ">=0.8"
|
petgraph = ">=0.8"
|
||||||
nsvg = "0.5.1"
|
resvg = "*"
|
||||||
dyn-clone = "*"
|
dyn-clone = "*"
|
||||||
petgraph-graphml = "*"
|
petgraph-graphml = "*"
|
||||||
egui_node_graph2 = { path = "../egui_node_graph2" }
|
egui_node_graph2 = { path = "../egui_node_graph2" }
|
||||||
|
|||||||
@ -2532,8 +2532,7 @@ fn process_template(
|
|||||||
Ok(svg) => svg,
|
Ok(svg) => svg,
|
||||||
Err(e) => anyhow::bail!(e),
|
Err(e) => anyhow::bail!(e),
|
||||||
};
|
};
|
||||||
let raw = svg.into_raw();
|
*to_ret = Some(BasicValue::SaveBytes { path, value: svg });
|
||||||
*to_ret = Some(BasicValue::SaveBytes { path, value: raw });
|
|
||||||
},
|
},
|
||||||
| (BasicValue::Path { .. }, _) => {
|
| (BasicValue::Path { .. }, _) => {
|
||||||
anyhow::bail!("Not an svg");
|
anyhow::bail!("Not an svg");
|
||||||
|
|||||||
@ -41,19 +41,20 @@ impl Svg {
|
|||||||
);
|
);
|
||||||
let content = svg.finalize();
|
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,
|
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.) {
|
let mut pixmap = resvg::tiny_skia::Pixmap::new(svg_size.x as _, svg_size.y as _)
|
||||||
Ok(o) => o,
|
.expect("Could not allocate svg");
|
||||||
Err(e) => return Err(format!("{}", e)),
|
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,
|
let svg = Svg { image,
|
||||||
original: content,
|
original: content,
|
||||||
@ -75,17 +76,21 @@ impl Svg {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn rasterize(&self) -> Result<nsvg::image::ImageBuffer<nsvg::image::Rgba<u8>, Vec<u8>>, String> {
|
pub(crate) fn rasterize(&self) -> Result<Vec<u8>, String> {
|
||||||
let svg = match nsvg::parse_str(&self.original, nsvg::Units::Pixel, 96.0) {
|
let svg_tree = match resvg::usvg::Tree::from_str(&self.original, &resvg::usvg::Options::default()) {
|
||||||
Ok(svg) => svg,
|
Ok(svg) => svg,
|
||||||
Err(nsvg_err) => return Err(format!("{}", nsvg_err)),
|
Err(err) => return Err(format!("{}", err)),
|
||||||
};
|
|
||||||
let data = match svg.rasterize(1.) {
|
|
||||||
Ok(o) => o,
|
|
||||||
Err(e) => return Err(format!("{}", e)),
|
|
||||||
};
|
};
|
||||||
|
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)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user