Update resvg to 0.18 in iced_wgpu

This commit is contained in:
Héctor Ramón Jiménez 2021-09-27 14:23:22 +07:00
parent 35c4ad6dd9
commit 73f2881568
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
2 changed files with 29 additions and 15 deletions

View file

@ -8,7 +8,7 @@ license = "MIT AND OFL-1.1"
repository = "https://github.com/hecrj/iced" repository = "https://github.com/hecrj/iced"
[features] [features]
svg = ["resvg", "usvg"] svg = ["resvg", "usvg", "tiny-skia"]
image = ["png", "jpeg", "jpeg_rayon", "gif", "webp", "bmp"] image = ["png", "jpeg", "jpeg_rayon", "gif", "webp", "bmp"]
png = ["image_rs/png"] png = ["image_rs/png"]
jpeg = ["image_rs/jpeg"] jpeg = ["image_rs/jpeg"]
@ -55,11 +55,15 @@ default-features = false
optional = true optional = true
[dependencies.resvg] [dependencies.resvg]
version = "0.12" version = "0.18"
optional = true optional = true
[dependencies.usvg] [dependencies.usvg]
version = "0.12" version = "0.18"
optional = true
[dependencies.tiny-skia]
version = "0.6"
optional = true optional = true
[package.metadata.docs.rs] [package.metadata.docs.rs]

View file

@ -1,8 +1,10 @@
use iced_native::svg;
use std::collections::{HashMap, HashSet};
use crate::image::atlas::{self, Atlas}; use crate::image::atlas::{self, Atlas};
use iced_native::svg;
use std::collections::{HashMap, HashSet};
use std::fs;
pub enum Svg { pub enum Svg {
Loaded(usvg::Tree), Loaded(usvg::Tree),
NotFound, NotFound,
@ -46,13 +48,21 @@ impl Cache {
let svg = match handle.data() { let svg = match handle.data() {
svg::Data::Path(path) => { svg::Data::Path(path) => {
match usvg::Tree::from_file(path, &Default::default()) { let tree = fs::read_to_string(path).ok().and_then(|contents| {
Ok(tree) => Svg::Loaded(tree), usvg::Tree::from_str(
Err(_) => Svg::NotFound, &contents,
} &usvg::Options::default().to_ref(),
)
.ok()
});
tree.map(Svg::Loaded).unwrap_or(Svg::NotFound)
} }
svg::Data::Bytes(bytes) => { svg::Data::Bytes(bytes) => {
match usvg::Tree::from_data(&bytes, &Default::default()) { match usvg::Tree::from_data(
&bytes,
&usvg::Options::default().to_ref(),
) {
Ok(tree) => Svg::Loaded(tree), Ok(tree) => Svg::Loaded(tree),
Err(_) => Svg::NotFound, Err(_) => Svg::NotFound,
} }
@ -100,17 +110,17 @@ impl Cache {
// We currently rerasterize the SVG when its size changes. This is slow // We currently rerasterize the SVG when its size changes. This is slow
// as heck. A GPU rasterizer like `pathfinder` may perform better. // as heck. A GPU rasterizer like `pathfinder` may perform better.
// It would be cool to be able to smooth resize the `svg` example. // It would be cool to be able to smooth resize the `svg` example.
let img = resvg::render( let mut img = tiny_skia::Pixmap::new(width, height)?;
let _ = resvg::render(
tree, tree,
if width > height { if width > height {
usvg::FitTo::Width(width) usvg::FitTo::Width(width)
} else { } else {
usvg::FitTo::Height(height) usvg::FitTo::Height(height)
}, },
None, img.as_mut(),
)?; )?;
let width = img.width();
let height = img.height();
let mut rgba = img.take(); let mut rgba = img.take();
rgba.chunks_exact_mut(4).for_each(|rgba| rgba.swap(0, 2)); rgba.chunks_exact_mut(4).for_each(|rgba| rgba.swap(0, 2));