From 9a3c81f336b8e29c64471026860f3c9d8b56348c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 31 Oct 2021 16:24:31 +0700 Subject: Introduce first-class `svg` module in `iced_native` --- native/src/svg.rs | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 native/src/svg.rs (limited to 'native/src/svg.rs') diff --git a/native/src/svg.rs b/native/src/svg.rs new file mode 100644 index 00000000..229738ee --- /dev/null +++ b/native/src/svg.rs @@ -0,0 +1,90 @@ +use crate::{Hasher, Rectangle}; + +use std::hash::{Hash, Hasher as _}; +use std::path::PathBuf; +use std::sync::Arc; + +/// An [`Svg`] handle. +#[derive(Debug, Clone)] +pub struct Handle { + id: u64, + data: Arc, +} + +impl Handle { + /// Creates an SVG [`Handle`] pointing to the vector image of the given + /// path. + pub fn from_path(path: impl Into) -> Handle { + Self::from_data(Data::Path(path.into())) + } + + /// Creates an SVG [`Handle`] from raw bytes containing either an SVG string + /// or gzip compressed data. + /// + /// 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())) + } + + fn from_data(data: Data) -> Handle { + let mut hasher = Hasher::default(); + data.hash(&mut hasher); + + Handle { + id: hasher.finish(), + data: Arc::new(data), + } + } + + /// Returns the unique identifier of the [`Handle`]. + pub fn id(&self) -> u64 { + self.id + } + + /// Returns a reference to the SVG [`Data`]. + pub fn data(&self) -> &Data { + &self.data + } +} + +impl Hash for Handle { + fn hash(&self, state: &mut H) { + self.id.hash(state); + } +} + +/// The data of an [`Svg`]. +#[derive(Clone, Hash)] +pub enum Data { + /// File data + Path(PathBuf), + + /// In-memory data + /// + /// Can contain an SVG string or a gzip compressed data. + Bytes(Vec), +} + +impl std::fmt::Debug for Data { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Data::Path(path) => write!(f, "Path({:?})", path), + Data::Bytes(_) => write!(f, "Bytes(...)"), + } + } +} + +/// The renderer of an [`Svg`]. +/// +/// Your [renderer] will need to implement this trait before being able to use +/// an [`Svg`] in your user interface. +/// +/// [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); + + // Draws an [`Svg`]. + fn draw(&mut self, handle: Handle, bounds: Rectangle); +} -- cgit From faaa17c3a5332a74147b6d5c937e3e9bc0952ecb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 4 Nov 2021 19:24:40 +0700 Subject: Write documentation for `iced_native::svg` --- native/src/svg.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'native/src/svg.rs') diff --git a/native/src/svg.rs b/native/src/svg.rs index 229738ee..90eff87e 100644 --- a/native/src/svg.rs +++ b/native/src/svg.rs @@ -1,3 +1,4 @@ +//! Load and draw vector graphics. use crate::{Hasher, Rectangle}; use std::hash::{Hash, Hasher as _}; @@ -75,16 +76,13 @@ impl std::fmt::Debug for Data { } } -/// The renderer of an [`Svg`]. -/// -/// Your [renderer] will need to implement this trait before being able to use -/// an [`Svg`] in your user interface. +/// A [`Renderer`] that can render vector graphics. /// /// [renderer]: crate::renderer pub trait Renderer: crate::Renderer { - /// Returns the default dimensions of an [`Svg`] for the given [`Handle`]. + /// Returns the default dimensions of an SVG for the given [`Handle`]. fn dimensions(&self, handle: &Handle) -> (u32, u32); - // Draws an [`Svg`]. + /// Draws an SVG with the given [`Handle`] and inside the provided `bounds`. fn draw(&mut self, handle: Handle, bounds: Rectangle); } -- cgit