Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ version = "0.12"
optional = true

[dependencies.font-kit]
version = "0.8"
version = "0.10"
optional = true

[package.metadata.docs.rs]
Expand Down
9 changes: 6 additions & 3 deletions wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT AND OFL-1.1"
repository = "https://github.com/hecrj/iced"

[features]
svg = ["resvg"]
svg = ["resvg", "usvg"]
canvas = ["iced_graphics/canvas"]
qr_code = ["iced_graphics/qr_code"]
default_system_font = ["iced_graphics/font-source"]
Expand Down Expand Up @@ -40,8 +40,11 @@ version = "0.23"
optional = true

[dependencies.resvg]
version = "0.9"
features = ["raqote-backend"]
version = "0.12"
optional = true

[dependencies.usvg]
version = "0.12"
optional = true

[package.metadata.docs.rs]
Expand Down
45 changes: 29 additions & 16 deletions wgpu/src/image/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use iced_native::svg;
use std::collections::{HashMap, HashSet};

pub enum Svg {
Loaded(resvg::usvg::Tree),
Loaded(usvg::Tree),
NotFound,
}

Expand Down Expand Up @@ -43,17 +43,15 @@ impl Cache {
return self.svgs.get(&handle.id()).unwrap();
}

let opt = resvg::Options::default();

let svg = match handle.data() {
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),
Err(_) => Svg::NotFound,
}
}
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),
Err(_) => Svg::NotFound,
}
Expand Down Expand Up @@ -101,23 +99,38 @@ impl Cache {
// 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.
let screen_size =
resvg::ScreenSize::new(width, height).unwrap();
let img = resvg::render(
tree,
if width > height {
usvg::FitTo::Width(width)
} else {
usvg::FitTo::Height(height)
},
None,
)?;
let width = img.width();
let height = img.height();

let mut canvas =
resvg::raqote::DrawTarget::new(width as i32, height as i32);
let mut rgba = img.take().into_iter();

resvg::backend_raqote::render_to_canvas(
tree,
&resvg::Options::default(),
screen_size,
&mut canvas,
);
// 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(
width,
height,
bytemuck::cast_slice(canvas.get_data()),
bytemuck::cast_slice(bgra.as_slice()),
device,
encoder,
)?;
Expand Down