Fixed bugs for svg

This commit is contained in:
elvis
2025-11-02 17:45:54 +01:00
parent 920b9c7280
commit 7c73fd6ed3
4 changed files with 82 additions and 17 deletions

View File

@ -13,6 +13,8 @@ pub(crate) struct Svg {
/// original size of the svg
svg_size: egui::Vec2,
original: String,
#[cfg_attr(feature = "persistence", serde(skip))]
svg_texture: Arc<Mutex<Option<egui::TextureHandle>>>,
}
@ -53,7 +55,10 @@ impl Svg {
let image = egui::ColorImage::from_rgba_unmultiplied([w as _, h as _], &data);
let svg = Svg { image, svg_size, svg_texture: Arc::new(Mutex::new(None)) };
let svg = Svg { image,
original: content,
svg_size,
svg_texture: Arc::new(Mutex::new(None)) };
Ok(svg)
}
@ -69,6 +74,19 @@ impl Svg {
svg_texture
}
}
pub(crate) fn rasterize(&self) -> Result<nsvg::image::ImageBuffer<nsvg::image::Rgba<u8>, Vec<u8>>, String> {
let svg = match nsvg::parse_str(&self.original, nsvg::Units::Pixel, 96.0) {
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)),
};
Ok(data)
}
}
impl Debug for Svg {
@ -105,6 +123,7 @@ impl Hash for Svg {
hash_float!(self.svg_size.y);
self.image.pixels.hash(state);
self.image.size.hash(state);
self.original.hash(state);
hash_float!(self.image.source_size.x);
hash_float!(self.image.source_size.y);
self.svg_texture.lock().expect("Poisoned").hash(state);