Update resvg to 0.12

This commit is contained in:
Greg V 2020-11-25 22:25:15 +03:00 committed by Héctor Ramón Jiménez
parent 86361f003c
commit 81f37123ad
2 changed files with 36 additions and 20 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"] svg = ["resvg", "usvg"]
canvas = ["iced_graphics/canvas"] canvas = ["iced_graphics/canvas"]
qr_code = ["iced_graphics/qr_code"] qr_code = ["iced_graphics/qr_code"]
default_system_font = ["iced_graphics/font-source"] default_system_font = ["iced_graphics/font-source"]
@ -40,8 +40,11 @@ version = "0.23"
optional = true optional = true
[dependencies.resvg] [dependencies.resvg]
version = "0.9" version = "0.12"
features = ["raqote-backend"] optional = true
[dependencies.usvg]
version = "0.12"
optional = true optional = true
[package.metadata.docs.rs] [package.metadata.docs.rs]

View File

@ -3,7 +3,7 @@ use iced_native::svg;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
pub enum Svg { pub enum Svg {
Loaded(resvg::usvg::Tree), Loaded(usvg::Tree),
NotFound, NotFound,
} }
@ -43,17 +43,15 @@ impl Cache {
return self.svgs.get(&handle.id()).unwrap(); return self.svgs.get(&handle.id()).unwrap();
} }
let opt = resvg::Options::default();
let svg = match handle.data() { let svg = match handle.data() {
svg::Data::Path(path) => { svg::Data::Path(path) => {
match resvg::usvg::Tree::from_file(path, &opt.usvg) { match usvg::Tree::from_file(path, &Default::default()) {
Ok(tree) => Svg::Loaded(tree), Ok(tree) => Svg::Loaded(tree),
Err(_) => Svg::NotFound, Err(_) => Svg::NotFound,
} }
} }
svg::Data::Bytes(bytes) => { svg::Data::Bytes(bytes) => {
match resvg::usvg::Tree::from_data(&bytes, &opt.usvg) { match usvg::Tree::from_data(&bytes, &Default::default()) {
Ok(tree) => Svg::Loaded(tree), Ok(tree) => Svg::Loaded(tree),
Err(_) => Svg::NotFound, Err(_) => Svg::NotFound,
} }
@ -101,23 +99,38 @@ 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 screen_size = let img = resvg::render(
resvg::ScreenSize::new(width, height).unwrap();
let mut canvas =
resvg::raqote::DrawTarget::new(width as i32, height as i32);
resvg::backend_raqote::render_to_canvas(
tree, tree,
&resvg::Options::default(), if width > height {
screen_size, usvg::FitTo::Width(width)
&mut canvas, } else {
); usvg::FitTo::Height(height)
},
None,
)?;
let width = img.width();
let height = img.height();
let mut rgba = img.take().into_iter();
// TODO: Perform conversion in the GPU
let bgra: Vec<u8> = std::iter::from_fn(move || {
use std::iter::once;
let r = rgba.next()?;
let g = rgba.next()?;
let b = rgba.next()?;
let a = rgba.next()?;
Some(once(b).chain(once(g)).chain(once(r)).chain(once(a)))
})
.flatten()
.collect();
let allocation = texture_atlas.upload( let allocation = texture_atlas.upload(
width, width,
height, height,
bytemuck::cast_slice(canvas.get_data()), bytemuck::cast_slice(bgra.as_slice()),
device, device,
encoder, encoder,
)?; )?;