From 1bc7e4438005f1fe508019a8a8a68ae40cc33174 Mon Sep 17 00:00:00 2001 From: Dawid Lentka <46752179+PingPongun@users.noreply.github.com> Date: Wed, 29 Nov 2023 21:25:58 +0100 Subject: [PATCH] Update to resvg 0.36 --- Cargo.lock | 111 ++++++++++--------- crates/egui/src/load.rs | 2 - crates/egui_extras/Cargo.toml | 2 +- crates/egui_extras/src/image.rs | 58 +++++----- crates/egui_extras/src/loaders/svg_loader.rs | 12 +- deny.toml | 4 - 6 files changed, 84 insertions(+), 105 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f9151f10..cefbef46 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -479,12 +479,6 @@ dependencies = [ "rustc-demangle", ] -[[package]] -name = "base64" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" - [[package]] name = "base64" version = "0.21.4" @@ -1064,9 +1058,9 @@ dependencies = [ [[package]] name = "data-url" -version = "0.2.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d7439c3735f405729d52c3fbbe4de140eaf938a1fe47d227c27f8254d4302a5" +checksum = "5c297a1c74b71ae29df00c3e22dd9534821d60eb9af5a0192823fa2acea70c2a" [[package]] name = "deranged" @@ -2119,9 +2113,9 @@ dependencies = [ [[package]] name = "imagesize" -version = "0.10.1" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df19da1e92fbfec043ca97d622955381b1f3ee72a180ec999912df31b1ccd951" +checksum = "029d73f573d8e8d63e6d5020011d3255b28c3ba85d6cf870a07184ed23de9284" [[package]] name = "indexmap" @@ -2262,9 +2256,9 @@ checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc" [[package]] name = "kurbo" -version = "0.8.3" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a53776d271cfb873b17c618af0298445c88afc52837f3e948fa3fafd131f449" +checksum = "bd85a5776cd9500c2e2059c8c76c3b01528566b7fcbaf8098b55a33fc298849b" dependencies = [ "arrayvec", ] @@ -2825,7 +2819,7 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a4a0cfc5fb21a09dc6af4bf834cf10d4a32fccd9e2ea468c4b1751a097487aa" dependencies = [ - "base64 0.21.4", + "base64", "indexmap 1.9.3", "line-wrap", "quick-xml", @@ -3105,15 +3099,15 @@ checksum = "216080ab382b992234dda86873c18d4c48358f5cfcb70fd693d7f6f2131b628b" [[package]] name = "resvg" -version = "0.28.0" +version = "0.36.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c115863f2d3621999cf187e318bc92b16402dfeff6a48c74df700d77381394c1" +checksum = "cc7980f653f9a7db31acff916a262c3b78c562919263edea29bf41a056e20497" dependencies = [ "log", "pico-args", "rgb", "svgtypes", - "tiny-skia 0.8.4", + "tiny-skia", "usvg", ] @@ -3172,7 +3166,7 @@ version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b91f7eff05f748767f183df4320a63d6936e9c6107d97c9e6bdd9784f4289c94" dependencies = [ - "base64 0.21.4", + "base64", "bitflags 2.4.0", "serde", "serde_derive", @@ -3180,9 +3174,9 @@ dependencies = [ [[package]] name = "roxmltree" -version = "0.15.1" +version = "0.18.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b9de9831a129b122e7e61f242db509fa9d0838008bf0b29bb0624669edfe48a" +checksum = "862340e351ce1b271a378ec53f304a5558f7db87f3769dc655a8f6ecbb68b302" dependencies = [ "xmlparser", ] @@ -3321,7 +3315,7 @@ dependencies = [ "log", "memmap2", "smithay-client-toolkit", - "tiny-skia 0.11.2", + "tiny-skia", ] [[package]] @@ -3551,10 +3545,11 @@ dependencies = [ [[package]] name = "svgtypes" -version = "0.8.2" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22975e8a2bac6a76bb54f898a6b18764633b00e780330f0b689f65afb3975564" +checksum = "d71499ff2d42f59d26edb21369a308ede691421f79ebc0f001e2b1fd3a7c9e52" dependencies = [ + "kurbo", "siphasher", ] @@ -3704,20 +3699,6 @@ dependencies = [ "time-core", ] -[[package]] -name = "tiny-skia" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df8493a203431061e901613751931f047d1971337153f96d0e5e363d6dbf6a67" -dependencies = [ - "arrayref", - "arrayvec", - "bytemuck", - "cfg-if", - "png", - "tiny-skia-path 0.8.4", -] - [[package]] name = "tiny-skia" version = "0.11.2" @@ -3729,18 +3710,8 @@ dependencies = [ "bytemuck", "cfg-if", "log", - "tiny-skia-path 0.11.2", -] - -[[package]] -name = "tiny-skia-path" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adbfb5d3f3dd57a0e11d12f4f13d4ebbbc1b5c15b7ab0a156d030b21da5f677c" -dependencies = [ - "arrayref", - "bytemuck", - "strict-num", + "png", + "tiny-skia-path", ] [[package]] @@ -3942,7 +3913,7 @@ version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f5ccd538d4a604753ebc2f17cd9946e89b77bf87f6a8e2309667c6f2e87855e3" dependencies = [ - "base64 0.21.4", + "base64", "flate2", "log", "once_cell", @@ -3973,22 +3944,46 @@ dependencies = [ [[package]] name = "usvg" -version = "0.28.0" +version = "0.36.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b5b7c2b30845b3348c067ca3d09e20cc6e327c288f0ca4c48698712abf432e9" +checksum = "c51daa774fe9ee5efcf7b4fec13019b8119cda764d9a8b5b06df02bb1445c656" +dependencies = [ + "base64", + "log", + "pico-args", + "usvg-parser", + "usvg-tree", + "xmlwriter", +] + +[[package]] +name = "usvg-parser" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45c88a5ffaa338f0e978ecf3d4e00d8f9f493e29bed0752e1a808a1db16afc40" dependencies = [ - "base64 0.13.1", "data-url", "flate2", "imagesize", "kurbo", "log", - "rctree", "roxmltree", "simplecss", "siphasher", + "svgtypes", + "usvg-tree", +] + +[[package]] +name = "usvg-tree" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6cacb0c5edeaf3e80e5afcf5b0d4004cc1d36318befc9a7c6606507e5d0f4062" +dependencies = [ + "rctree", "strict-num", "svgtypes", + "tiny-skia-path", ] [[package]] @@ -4726,9 +4721,15 @@ checksum = "0fcb9cbac069e033553e8bb871be2fbdffcab578eb25bd0f7c508cedc6dcd75a" [[package]] name = "xmlparser" -version = "0.13.5" +version = "0.13.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66fee0b777b0f5ac1c69bb06d361268faafa61cd4682ae064a171c16c433e9e4" + +[[package]] +name = "xmlwriter" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d25c75bf9ea12c4040a97f829154768bbbce366287e2dc044af160cd79a13fd" +checksum = "ec7a2a501ed189703dba8b08142f057e887dfc4b2cc4db2d343ac6376ba3e0b9" [[package]] name = "yaml-rust" diff --git a/crates/egui/src/load.rs b/crates/egui/src/load.rs index f1c61a64..26e8af23 100644 --- a/crates/egui/src/load.rs +++ b/crates/egui/src/load.rs @@ -127,8 +127,6 @@ pub type Result = std::result::Result; /// Used mostly for rendering SVG:s to a good size. /// /// All variants will preserve the original aspect ratio. -/// -/// Similar to `usvg::FitTo`. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum SizeHint { /// Scale original size by some factor. diff --git a/crates/egui_extras/Cargo.toml b/crates/egui_extras/Cargo.toml index d48549b7..927d3d30 100644 --- a/crates/egui_extras/Cargo.toml +++ b/crates/egui_extras/Cargo.toml @@ -92,7 +92,7 @@ syntect = { version = "5", optional = true, default-features = false, features = ] } # svg feature -resvg = { version = "0.28", optional = true, default-features = false } +resvg = { version = "0.36", optional = true, default-features = false } # http feature ehttp = { version = "0.3.1", optional = true, default-features = false } diff --git a/crates/egui_extras/src/image.rs b/crates/egui_extras/src/image.rs index 3aeb737b..996c0db6 100644 --- a/crates/egui_extras/src/image.rs +++ b/crates/egui_extras/src/image.rs @@ -3,10 +3,7 @@ use egui::{mutex::Mutex, TextureFilter, TextureOptions}; #[cfg(feature = "svg")] -use resvg::{tiny_skia, usvg}; - -#[cfg(feature = "svg")] -pub use usvg::FitTo; +use egui::SizeHint; /// An image to be shown in egui. /// @@ -67,7 +64,7 @@ impl RetainedImage { /// On invalid image #[cfg(feature = "svg")] pub fn from_svg_bytes(debug_name: impl Into, svg_bytes: &[u8]) -> Result { - Self::from_svg_bytes_with_size(debug_name, svg_bytes, FitTo::Original) + Self::from_svg_bytes_with_size(debug_name, svg_bytes, None) } /// Pass in the str of an SVG that you've loaded. @@ -88,11 +85,11 @@ impl RetainedImage { pub fn from_svg_bytes_with_size( debug_name: impl Into, svg_bytes: &[u8], - size: FitTo, + size_hint: Option, ) -> Result { Ok(Self::from_color_image( debug_name, - load_svg_bytes_with_size(svg_bytes, size)?, + load_svg_bytes_with_size(svg_bytes, size_hint)?, )) } @@ -231,7 +228,7 @@ pub fn load_image_bytes(image_bytes: &[u8]) -> Result /// On invalid image #[cfg(feature = "svg")] pub fn load_svg_bytes(svg_bytes: &[u8]) -> Result { - load_svg_bytes_with_size(svg_bytes, FitTo::Original) + load_svg_bytes_with_size(svg_bytes, None) } /// Load an SVG and rasterize it into an egui image with a scaling parameter. @@ -243,36 +240,31 @@ pub fn load_svg_bytes(svg_bytes: &[u8]) -> Result { #[cfg(feature = "svg")] pub fn load_svg_bytes_with_size( svg_bytes: &[u8], - fit_to: FitTo, + size_hint: Option, ) -> Result { + use resvg::tiny_skia::{IntSize, Pixmap}; + use resvg::usvg::{Options, Tree, TreeParsing}; + crate::profile_function!(); - let opt = usvg::Options::default(); - - let rtree = usvg::Tree::from_data(svg_bytes, &opt).map_err(|err| err.to_string())?; - - let pixmap_size = rtree.size.to_screen_size(); - let [w, h] = match fit_to { - FitTo::Original => [pixmap_size.width(), pixmap_size.height()], - FitTo::Size(w, h) => [w, h], - FitTo::Height(h) => [ - (pixmap_size.width() as f32 * (h as f32 / pixmap_size.height() as f32)) as u32, - h, - ], - FitTo::Width(w) => [ - w, - (pixmap_size.height() as f32 * (w as f32 / pixmap_size.width() as f32)) as u32, - ], - FitTo::Zoom(z) => [ - (pixmap_size.width() as f32 * z) as u32, - (pixmap_size.height() as f32 * z) as u32, - ], + let opt = Options::default(); + + let mut rtree = Tree::from_data(svg_bytes, &opt).map_err(|err| err.to_string())?; + + let mut size = rtree.size.to_int_size(); + match size_hint { + None => (), + Some(SizeHint::Size(w, h)) => size = size.scale_to(IntSize::from_wh(w, h).unwrap()), + Some(SizeHint::Height(h)) => size = size.scale_to_height(h).unwrap(), + Some(SizeHint::Width(w)) => size = size.scale_to_width(w).unwrap(), + Some(SizeHint::Scale(z)) => size = size.scale_by(z.into_inner()).unwrap(), }; + let (w, h) = (size.width(), size.height()); - let mut pixmap = tiny_skia::Pixmap::new(w, h) - .ok_or_else(|| format!("Failed to create SVG Pixmap of size {w}x{h}"))?; + let mut pixmap = + Pixmap::new(w, h).ok_or_else(|| format!("Failed to create SVG Pixmap of size {w}x{h}"))?; - resvg::render(&rtree, fit_to, Default::default(), pixmap.as_mut()) - .ok_or_else(|| "Failed to render SVG".to_owned())?; + rtree.size = size.to_size(); + resvg::Tree::from_usvg(&rtree).render(Default::default(), &mut pixmap.as_mut()); let image = egui::ColorImage::from_rgba_unmultiplied([w as _, h as _], pixmap.data()); diff --git a/crates/egui_extras/src/loaders/svg_loader.rs b/crates/egui_extras/src/loaders/svg_loader.rs index 00a3bdfe..1794a872 100644 --- a/crates/egui_extras/src/loaders/svg_loader.rs +++ b/crates/egui_extras/src/loaders/svg_loader.rs @@ -7,8 +7,6 @@ use egui::{ ColorImage, }; -use resvg::usvg; - type Entry = Result, String>; #[derive(Default)] @@ -51,14 +49,8 @@ impl ImageLoader for SvgLoader { match ctx.try_load_bytes(&uri) { Ok(BytesPoll::Ready { bytes, .. }) => { log::trace!("started loading {uri:?}"); - let fit_to = match size_hint { - SizeHint::Scale(factor) => usvg::FitTo::Zoom(factor.into_inner()), - SizeHint::Width(w) => usvg::FitTo::Width(w), - SizeHint::Height(h) => usvg::FitTo::Height(h), - SizeHint::Size(w, h) => usvg::FitTo::Size(w, h), - }; - let result = - crate::image::load_svg_bytes_with_size(&bytes, fit_to).map(Arc::new); + let result = crate::image::load_svg_bytes_with_size(&bytes, Some(size_hint)) + .map(Arc::new); log::trace!("finished loading {uri:?}"); cache.insert((uri, size_hint), result.clone()); match result { diff --git a/deny.toml b/deny.toml index e0ce6331..d50851ce 100644 --- a/deny.toml +++ b/deny.toml @@ -34,8 +34,6 @@ deny = [ ] skip = [ - { name = "arrayvec" }, # old version via tiny-skiaz - { name = "base64" }, # small crate, old version from usvg { name = "glow" }, # TODO(@wumpf): Old version use for glow backend right now, newer for wgpu. Updating this trickles out to updating winit. { name = "glutin_wgl_sys" }, # TODO(@wumpf): Old version use for glow backend right now, newer for wgpu. Updating this trickles out to updating winit. { name = "libloading" }, # wgpu-hal itself depends on 0.8 while some of its dependencies, like ash and d3d12, depend on 0.7 @@ -43,7 +41,6 @@ skip = [ { name = "redox_syscall" }, # old version via directories-next { name = "spin" }, # old version via ring through rusttls and other libraries, newer for wgpu. { name = "time" }, # old version pulled in by unmaintianed crate 'chrono' - { name = "ttf-parser" }, # different versions pulled in by ab_glyph and usvg { name = "windows" }, # old version via accesskit_windows ] skip-tree = [ @@ -51,7 +48,6 @@ skip-tree = [ { name = "foreign-types" }, # small crate. Old version via cocoa and core-graphics (winit). { name = "objc2" }, # old version via accesskit_macos { name = "rfd" }, # example dependency - { name = "tiny-skia" }, # old version via old resvg in egui_extras - see https://github.com/emilk/egui/issues/3652 ] -- 2.43.0.windows.1