From 6ce12fc0c60adbd169676ee985e8529831633a76 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Mon, 3 Oct 2022 15:53:39 -0700 Subject: Use `Cow<'static, [u8]>` in image/svg, add constructors taking &[u8]` This should resolve https://github.com/iced-rs/iced/issues/580 by providing a way to use an image included with `include_bytes!` without needing to copy it to a `Vec` to create an image handle. It would be nice if these methods could also be `const`, but that isn't possible due to the hashing being done. This is technically a breaking change since `Handle::data()` is public. But if that is used, it's most likely in used somewhere that only relies on the type derefing to `&[u8]`. --- native/src/svg.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'native/src/svg.rs') diff --git a/native/src/svg.rs b/native/src/svg.rs index f86fec5b..c89eed3f 100644 --- a/native/src/svg.rs +++ b/native/src/svg.rs @@ -1,6 +1,7 @@ //! Load and draw vector graphics. use crate::{Hasher, Rectangle}; +use std::borrow::Cow; use std::hash::{Hash, Hasher as _}; use std::path::PathBuf; use std::sync::Arc; @@ -25,7 +26,14 @@ impl Handle { /// This is useful if you already have your SVG data in-memory, maybe /// because you downloaded or generated it procedurally. pub fn from_memory(bytes: impl Into>) -> Handle { - Self::from_data(Data::Bytes(bytes.into())) + Self::from_data(Data::Bytes(Cow::Owned(bytes.into()))) + } + + /// Like [`Handle::from_memory`], but from static image data. + /// + /// Useful for images included in binary, for instance with [`include_bytes!`]. + pub fn from_static_memory(bytes: &'static [u8]) -> Handle { + Self::from_data(Data::Bytes(Cow::Borrowed(bytes))) } fn from_data(data: Data) -> Handle { @@ -64,7 +72,7 @@ pub enum Data { /// In-memory data /// /// Can contain an SVG string or a gzip compressed data. - Bytes(Vec), + Bytes(Cow<'static, [u8]>), } impl std::fmt::Debug for Data { -- cgit From bc5986c7c69efd206b900e8d923d3df3a225f6cc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 5 Nov 2022 01:53:24 +0100 Subject: Unify methods by leveraging `Into` in `image` and `svg` --- native/src/svg.rs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) (limited to 'native/src/svg.rs') diff --git a/native/src/svg.rs b/native/src/svg.rs index c89eed3f..d4d20182 100644 --- a/native/src/svg.rs +++ b/native/src/svg.rs @@ -25,15 +25,8 @@ impl Handle { /// /// This is useful if you already have your SVG data in-memory, maybe /// because you downloaded or generated it procedurally. - pub fn from_memory(bytes: impl Into>) -> Handle { - Self::from_data(Data::Bytes(Cow::Owned(bytes.into()))) - } - - /// Like [`Handle::from_memory`], but from static image data. - /// - /// Useful for images included in binary, for instance with [`include_bytes!`]. - pub fn from_static_memory(bytes: &'static [u8]) -> Handle { - Self::from_data(Data::Bytes(Cow::Borrowed(bytes))) + pub fn from_memory(bytes: impl Into>) -> Handle { + Self::from_data(Data::Bytes(bytes.into())) } fn from_data(data: Data) -> Handle { -- cgit From 8ce8d374b1e8d1d394a42a5ee2bca8af790f0b71 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 5 Nov 2022 03:13:04 +0100 Subject: Refactor some `image` traits a bit - Use `Size` were applicable. - Rename `TextureStore` to `image::Storage`. - Rename `TextureStoreEntry` to `image::storage::Entry`. - Wire up `viewport_dimensions` to `iced_glow` for `Svg`. --- native/src/svg.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'native/src/svg.rs') diff --git a/native/src/svg.rs b/native/src/svg.rs index d4d20182..a8e481d2 100644 --- a/native/src/svg.rs +++ b/native/src/svg.rs @@ -1,5 +1,5 @@ //! Load and draw vector graphics. -use crate::{Hasher, Rectangle}; +use crate::{Hasher, Rectangle, Size}; use std::borrow::Cow; use std::hash::{Hash, Hasher as _}; @@ -82,7 +82,7 @@ impl std::fmt::Debug for Data { /// [renderer]: crate::renderer pub trait Renderer: crate::Renderer { /// Returns the default dimensions of an SVG for the given [`Handle`]. - fn dimensions(&self, handle: &Handle) -> (u32, u32); + fn dimensions(&self, handle: &Handle) -> Size; /// Draws an SVG with the given [`Handle`] and inside the provided `bounds`. fn draw(&mut self, handle: Handle, bounds: Rectangle); -- cgit From 75ae0de9bdd3376b6e537bf59030059c926114ee Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Wed, 16 Nov 2022 17:42:41 +0100 Subject: feat: SVG styling with icon fill color --- native/src/svg.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'native/src/svg.rs') diff --git a/native/src/svg.rs b/native/src/svg.rs index a8e481d2..08b0984a 100644 --- a/native/src/svg.rs +++ b/native/src/svg.rs @@ -6,11 +6,14 @@ use std::hash::{Hash, Hasher as _}; use std::path::PathBuf; use std::sync::Arc; +pub use iced_style::svg::{Appearance, StyleSheet}; + /// A handle of Svg data. #[derive(Debug, Clone)] pub struct Handle { id: u64, data: Arc, + appearance: Appearance, } impl Handle { @@ -36,6 +39,7 @@ impl Handle { Handle { id: hasher.finish(), data: Arc::new(data), + appearance: Appearance::default(), } } @@ -48,6 +52,16 @@ impl Handle { pub fn data(&self) -> &Data { &self.data } + + /// Returns the styling [`Appearance`] for the SVG. + pub fn appearance(&self) -> Appearance { + self.appearance + } + + /// Set the [`Appearance`] for the SVG. + pub fn set_appearance(&mut self, appearance: Appearance) { + self.appearance = appearance; + } } impl Hash for Handle { -- cgit From b205a663471a8170d7b30cc59894425c09bea563 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 6 Dec 2022 04:34:00 +0100 Subject: Remove `appearance` from `Handle` ... and pass it directly to `Renderer::draw` instead. --- native/src/svg.rs | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) (limited to 'native/src/svg.rs') diff --git a/native/src/svg.rs b/native/src/svg.rs index 08b0984a..2168e409 100644 --- a/native/src/svg.rs +++ b/native/src/svg.rs @@ -1,19 +1,16 @@ //! Load and draw vector graphics. -use crate::{Hasher, Rectangle, Size}; +use crate::{Color, Hasher, Rectangle, Size}; use std::borrow::Cow; use std::hash::{Hash, Hasher as _}; use std::path::PathBuf; use std::sync::Arc; -pub use iced_style::svg::{Appearance, StyleSheet}; - /// A handle of Svg data. #[derive(Debug, Clone)] pub struct Handle { id: u64, data: Arc, - appearance: Appearance, } impl Handle { @@ -39,7 +36,6 @@ impl Handle { Handle { id: hasher.finish(), data: Arc::new(data), - appearance: Appearance::default(), } } @@ -52,16 +48,6 @@ impl Handle { pub fn data(&self) -> &Data { &self.data } - - /// Returns the styling [`Appearance`] for the SVG. - pub fn appearance(&self) -> Appearance { - self.appearance - } - - /// Set the [`Appearance`] for the SVG. - pub fn set_appearance(&mut self, appearance: Appearance) { - self.appearance = appearance; - } } impl Hash for Handle { @@ -98,6 +84,6 @@ pub trait Renderer: crate::Renderer { /// Returns the default dimensions of an SVG for the given [`Handle`]. fn dimensions(&self, handle: &Handle) -> Size; - /// Draws an SVG with the given [`Handle`] and inside the provided `bounds`. - fn draw(&mut self, handle: Handle, bounds: Rectangle); + /// Draws an SVG with the given [`Handle`], an optional [`Color`] filter, and inside the provided `bounds`. + fn draw(&mut self, handle: Handle, color: Option, bounds: Rectangle); } -- cgit