2022-10-31 13:37:56 -07:00
|
|
|
//! Vector image loading and caching
|
2021-09-27 14:23:22 +07:00
|
|
|
|
2019-12-15 06:19:07 +01:00
|
|
|
use iced_native::svg;
|
|
|
|
|
|
2021-09-27 14:23:22 +07:00
|
|
|
use std::collections::{HashMap, HashSet};
|
|
|
|
|
use std::fs;
|
2021-05-11 22:41:55 +01:00
|
|
|
|
2022-10-31 13:37:56 -07:00
|
|
|
use super::TextureStore;
|
|
|
|
|
|
|
|
|
|
/// Entry in cache corresponding to an svg handle
|
2019-12-15 07:03:54 +01:00
|
|
|
pub enum Svg {
|
2022-10-31 13:37:56 -07:00
|
|
|
/// Parsed svg
|
2020-11-25 22:25:15 +03:00
|
|
|
Loaded(usvg::Tree),
|
2022-10-31 13:37:56 -07:00
|
|
|
/// Svg not found or failed to parse
|
2019-12-15 07:03:54 +01:00
|
|
|
NotFound,
|
2019-12-15 06:19:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Svg {
|
2022-10-31 13:37:56 -07:00
|
|
|
/// Viewport width and height
|
2019-12-15 06:19:07 +01:00
|
|
|
pub fn viewport_dimensions(&self) -> (u32, u32) {
|
2019-12-15 07:03:54 +01:00
|
|
|
match self {
|
2020-01-11 21:50:42 +01:00
|
|
|
Svg::Loaded(tree) => {
|
2019-12-15 07:03:54 +01:00
|
|
|
let size = tree.svg_node().size;
|
2019-12-15 06:19:07 +01:00
|
|
|
|
2019-12-15 07:03:54 +01:00
|
|
|
(size.width() as u32, size.height() as u32)
|
|
|
|
|
}
|
|
|
|
|
Svg::NotFound => (1, 1),
|
|
|
|
|
}
|
2019-12-15 06:19:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-31 13:37:56 -07:00
|
|
|
/// Caches svg vector and raster data
|
2020-02-26 20:47:37 +01:00
|
|
|
#[derive(Debug)]
|
2022-10-31 13:37:56 -07:00
|
|
|
pub struct Cache<T: TextureStore> {
|
2019-12-15 06:19:07 +01:00
|
|
|
svgs: HashMap<u64, Svg>,
|
2022-10-31 13:37:56 -07:00
|
|
|
rasterized: HashMap<(u64, u32, u32), T::Entry>,
|
2019-12-15 06:19:07 +01:00
|
|
|
svg_hits: HashSet<u64>,
|
|
|
|
|
rasterized_hits: HashSet<(u64, u32, u32)>,
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-31 13:37:56 -07:00
|
|
|
impl<T: TextureStore> Cache<T> {
|
|
|
|
|
/// Load svg
|
2019-12-15 07:03:54 +01:00
|
|
|
pub fn load(&mut self, handle: &svg::Handle) -> &Svg {
|
2019-12-15 06:19:07 +01:00
|
|
|
if self.svgs.contains_key(&handle.id()) {
|
2019-12-15 07:03:54 +01:00
|
|
|
return self.svgs.get(&handle.id()).unwrap();
|
2019-12-15 06:19:07 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-31 00:39:18 +02:00
|
|
|
let svg = match handle.data() {
|
|
|
|
|
svg::Data::Path(path) => {
|
2021-09-27 14:23:22 +07:00
|
|
|
let tree = fs::read_to_string(path).ok().and_then(|contents| {
|
|
|
|
|
usvg::Tree::from_str(
|
|
|
|
|
&contents,
|
|
|
|
|
&usvg::Options::default().to_ref(),
|
|
|
|
|
)
|
|
|
|
|
.ok()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tree.map(Svg::Loaded).unwrap_or(Svg::NotFound)
|
2020-03-31 00:39:18 +02:00
|
|
|
}
|
|
|
|
|
svg::Data::Bytes(bytes) => {
|
2021-09-27 14:23:22 +07:00
|
|
|
match usvg::Tree::from_data(
|
2022-07-04 01:17:29 +02:00
|
|
|
bytes,
|
2021-09-27 14:23:22 +07:00
|
|
|
&usvg::Options::default().to_ref(),
|
|
|
|
|
) {
|
2020-03-31 00:39:18 +02:00
|
|
|
Ok(tree) => Svg::Loaded(tree),
|
|
|
|
|
Err(_) => Svg::NotFound,
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-15 06:19:07 +01:00
|
|
|
};
|
|
|
|
|
|
2019-12-15 07:03:54 +01:00
|
|
|
let _ = self.svgs.insert(handle.id(), svg);
|
|
|
|
|
self.svgs.get(&handle.id()).unwrap()
|
2019-12-15 06:19:07 +01:00
|
|
|
}
|
|
|
|
|
|
2022-10-31 13:37:56 -07:00
|
|
|
/// Load svg and upload raster data
|
2019-12-15 06:19:07 +01:00
|
|
|
pub fn upload(
|
|
|
|
|
&mut self,
|
|
|
|
|
handle: &svg::Handle,
|
|
|
|
|
[width, height]: [f32; 2],
|
|
|
|
|
scale: f32,
|
2022-10-31 13:37:56 -07:00
|
|
|
state: &mut T::State<'_>,
|
|
|
|
|
texture_store: &mut T,
|
|
|
|
|
) -> Option<&T::Entry> {
|
2019-12-15 06:19:07 +01:00
|
|
|
let id = handle.id();
|
|
|
|
|
|
|
|
|
|
let (width, height) = (
|
2021-07-13 22:27:48 -04:00
|
|
|
(scale * width).ceil() as u32,
|
|
|
|
|
(scale * height).ceil() as u32,
|
2019-12-15 06:19:07 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// TODO: Optimize!
|
|
|
|
|
// We currently rerasterize the SVG when its size changes. This is slow
|
|
|
|
|
// as heck. A GPU rasterizer like `pathfinder` may perform better.
|
2019-12-15 06:28:55 +01:00
|
|
|
// It would be cool to be able to smooth resize the `svg` example.
|
2020-01-17 20:15:20 +01:00
|
|
|
if self.rasterized.contains_key(&(id, width, height)) {
|
2019-12-15 06:19:07 +01:00
|
|
|
let _ = self.svg_hits.insert(id);
|
|
|
|
|
let _ = self.rasterized_hits.insert((id, width, height));
|
|
|
|
|
|
2020-01-10 14:39:29 +01:00
|
|
|
return self.rasterized.get(&(id, width, height));
|
2019-12-15 06:19:07 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-17 20:15:20 +01:00
|
|
|
match self.load(handle) {
|
2020-01-11 21:50:42 +01:00
|
|
|
Svg::Loaded(tree) => {
|
2019-12-21 08:16:36 +01:00
|
|
|
if width == 0 || height == 0 {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-10 14:39:29 +01:00
|
|
|
// TODO: Optimize!
|
|
|
|
|
// We currently rerasterize the SVG when its size changes. This is slow
|
|
|
|
|
// as heck. A GPU rasterizer like `pathfinder` may perform better.
|
|
|
|
|
// It would be cool to be able to smooth resize the `svg` example.
|
2021-09-27 14:23:22 +07:00
|
|
|
let mut img = tiny_skia::Pixmap::new(width, height)?;
|
|
|
|
|
|
2022-07-04 01:17:29 +02:00
|
|
|
resvg::render(
|
2020-11-25 22:25:15 +03:00
|
|
|
tree,
|
|
|
|
|
if width > height {
|
|
|
|
|
usvg::FitTo::Width(width)
|
|
|
|
|
} else {
|
|
|
|
|
usvg::FitTo::Height(height)
|
|
|
|
|
},
|
2021-09-27 14:23:22 +07:00
|
|
|
img.as_mut(),
|
2020-11-25 22:25:15 +03:00
|
|
|
)?;
|
2020-01-13 15:33:12 +01:00
|
|
|
|
2021-05-11 22:41:55 +01:00
|
|
|
let mut rgba = img.take();
|
|
|
|
|
rgba.chunks_exact_mut(4).for_each(|rgba| rgba.swap(0, 2));
|
2020-01-13 15:33:12 +01:00
|
|
|
|
2022-10-31 13:37:56 -07:00
|
|
|
let allocation = texture_store.upload(
|
2020-02-26 12:34:34 +01:00
|
|
|
width,
|
|
|
|
|
height,
|
2021-05-11 22:41:55 +01:00
|
|
|
bytemuck::cast_slice(rgba.as_slice()),
|
2022-10-31 13:37:56 -07:00
|
|
|
state,
|
2020-02-26 12:34:34 +01:00
|
|
|
)?;
|
2021-07-13 22:27:48 -04:00
|
|
|
log::debug!("allocating {} {}x{}", id, width, height);
|
2020-01-13 15:33:12 +01:00
|
|
|
|
2019-12-15 06:19:07 +01:00
|
|
|
let _ = self.svg_hits.insert(id);
|
|
|
|
|
let _ = self.rasterized_hits.insert((id, width, height));
|
2020-02-26 12:34:34 +01:00
|
|
|
let _ = self.rasterized.insert((id, width, height), allocation);
|
2019-12-15 06:19:07 +01:00
|
|
|
|
2020-01-10 14:39:29 +01:00
|
|
|
self.rasterized.get(&(id, width, height))
|
2019-12-15 06:19:07 +01:00
|
|
|
}
|
2020-02-26 12:34:34 +01:00
|
|
|
Svg::NotFound => None,
|
2019-12-15 06:19:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-31 13:37:56 -07:00
|
|
|
/// Load svg and upload raster data
|
|
|
|
|
pub fn trim(&mut self, texture_store: &mut T, state: &mut T::State<'_>) {
|
2019-12-15 06:19:07 +01:00
|
|
|
let svg_hits = &self.svg_hits;
|
|
|
|
|
let rasterized_hits = &self.rasterized_hits;
|
|
|
|
|
|
|
|
|
|
self.svgs.retain(|k, _| svg_hits.contains(k));
|
2020-02-26 20:10:19 +01:00
|
|
|
self.rasterized.retain(|k, entry| {
|
|
|
|
|
let retain = rasterized_hits.contains(k);
|
|
|
|
|
|
|
|
|
|
if !retain {
|
2022-10-31 13:37:56 -07:00
|
|
|
texture_store.remove(entry, state);
|
2020-02-26 20:10:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
retain
|
|
|
|
|
});
|
2019-12-15 06:19:07 +01:00
|
|
|
self.svg_hits.clear();
|
|
|
|
|
self.rasterized_hits.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-17 20:48:49 +01:00
|
|
|
|
2022-10-31 13:37:56 -07:00
|
|
|
impl<T: TextureStore> Default for Cache<T> {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
svgs: HashMap::new(),
|
|
|
|
|
rasterized: HashMap::new(),
|
|
|
|
|
svg_hits: HashSet::new(),
|
|
|
|
|
rasterized_hits: HashSet::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-26 20:47:37 +01:00
|
|
|
impl std::fmt::Debug for Svg {
|
2020-01-17 20:48:49 +01:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2020-02-26 20:47:37 +01:00
|
|
|
match self {
|
|
|
|
|
Svg::Loaded(_) => write!(f, "Svg::Loaded"),
|
|
|
|
|
Svg::NotFound => write!(f, "Svg::NotFound"),
|
|
|
|
|
}
|
2020-01-17 20:48:49 +01:00
|
|
|
}
|
2020-02-26 12:34:34 +01:00
|
|
|
}
|