From 6cc48b5c62bac287b8f9f1c79c1fb7486c51b18f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 3 Mar 2023 04:57:55 +0100 Subject: Move `Canvas` and `QRCode` to `iced` crate Rename `canvas` modules to `geometry` in graphics subcrates --- renderer/src/geometry.rs | 168 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 renderer/src/geometry.rs (limited to 'renderer/src/geometry.rs') diff --git a/renderer/src/geometry.rs b/renderer/src/geometry.rs new file mode 100644 index 00000000..e491ea73 --- /dev/null +++ b/renderer/src/geometry.rs @@ -0,0 +1,168 @@ +mod cache; + +pub use cache::Cache; + +pub use iced_graphics::geometry::*; + +use crate::{Backend, Point, Rectangle, Size, Vector}; + +pub enum Frame { + Wgpu(iced_wgpu::geometry::Frame), + TinySkia(iced_tiny_skia::geometry::Frame), +} + +macro_rules! delegate { + ($frame:expr, $name:ident => $body:expr) => { + match $frame { + Self::Wgpu($name) => $body, + Self::TinySkia($name) => $body, + } + }; +} + +impl Frame { + pub fn new(renderer: &crate::Renderer, size: Size) -> Self { + match renderer.backend() { + Backend::Wgpu(_) => { + Frame::Wgpu(iced_wgpu::geometry::Frame::new(size)) + } + Backend::TinySkia(_) => { + Frame::TinySkia(iced_tiny_skia::geometry::Frame::new(size)) + } + } + } + + /// Returns the width of the [`Frame`]. + #[inline] + pub fn width(&self) -> f32 { + delegate!(self, frame => frame.width()) + } + + /// Returns the height of the [`Frame`]. + #[inline] + pub fn height(&self) -> f32 { + delegate!(self, frame => frame.height()) + } + + /// Returns the dimensions of the [`Frame`]. + #[inline] + pub fn size(&self) -> Size { + delegate!(self, frame => frame.size()) + } + + /// Returns the coordinate of the center of the [`Frame`]. + #[inline] + pub fn center(&self) -> Point { + delegate!(self, frame => frame.center()) + } + + /// Draws the given [`Path`] on the [`Frame`] by filling it with the + /// provided style. + pub fn fill(&mut self, path: &Path, fill: impl Into) { + delegate!(self, frame => frame.fill(path, fill)); + } + + /// Draws an axis-aligned rectangle given its top-left corner coordinate and + /// its `Size` on the [`Frame`] by filling it with the provided style. + pub fn fill_rectangle( + &mut self, + top_left: Point, + size: Size, + fill: impl Into, + ) { + delegate!(self, frame => frame.fill_rectangle(top_left, size, fill)); + } + + /// Draws the stroke of the given [`Path`] on the [`Frame`] with the + /// provided style. + pub fn stroke<'a>(&mut self, path: &Path, stroke: impl Into>) { + delegate!(self, frame => frame.stroke(path, stroke)); + } + + /// Draws the characters of the given [`Text`] on the [`Frame`], filling + /// them with the given color. + /// + /// __Warning:__ Text currently does not work well with rotations and scale + /// transforms! The position will be correctly transformed, but the + /// resulting glyphs will not be rotated or scaled properly. + /// + /// Additionally, all text will be rendered on top of all the layers of + /// a [`Canvas`]. Therefore, it is currently only meant to be used for + /// overlays, which is the most common use case. + /// + /// Support for vectorial text is planned, and should address all these + /// limitations. + /// + /// [`Canvas`]: crate::widget::Canvas + pub fn fill_text(&mut self, text: impl Into) { + delegate!(self, frame => frame.fill_text(text)); + } + + /// Stores the current transform of the [`Frame`] and executes the given + /// drawing operations, restoring the transform afterwards. + /// + /// This method is useful to compose transforms and perform drawing + /// operations in different coordinate systems. + #[inline] + pub fn with_save(&mut self, f: impl FnOnce(&mut Frame)) { + delegate!(self, frame => frame.push_transform()); + + f(self); + + delegate!(self, frame => frame.pop_transform()); + } + + /// Executes the given drawing operations within a [`Rectangle`] region, + /// clipping any geometry that overflows its bounds. Any transformations + /// performed are local to the provided closure. + /// + /// This method is useful to perform drawing operations that need to be + /// clipped. + #[inline] + pub fn with_clip(&mut self, region: Rectangle, f: impl FnOnce(&mut Frame)) { + let mut frame = match self { + Self::Wgpu(_) => { + Self::Wgpu(iced_wgpu::geometry::Frame::new(region.size())) + } + Self::TinySkia(_) => Self::TinySkia( + iced_tiny_skia::geometry::Frame::new(region.size()), + ), + }; + + f(&mut frame); + + let translation = Vector::new(region.x, region.y); + + match (self, frame) { + (Self::Wgpu(target), Self::Wgpu(frame)) => { + target.clip(frame, translation); + } + (Self::TinySkia(target), Self::TinySkia(frame)) => { + target.clip(frame, translation); + } + _ => unreachable!(), + }; + } + + /// Applies a translation to the current transform of the [`Frame`]. + #[inline] + pub fn translate(&mut self, translation: Vector) { + delegate!(self, frame => frame.translate(translation)); + } + + /// Applies a rotation in radians to the current transform of the [`Frame`]. + #[inline] + pub fn rotate(&mut self, angle: f32) { + delegate!(self, frame => frame.rotate(angle)); + } + + /// Applies a scaling to the current transform of the [`Frame`]. + #[inline] + pub fn scale(&mut self, scale: f32) { + delegate!(self, frame => frame.scale(scale)); + } + + pub fn into_geometry(self) -> Geometry { + Geometry(delegate!(self, frame => frame.into_primitive())) + } +} -- cgit From 3a0d34c0240f4421737a6a08761f99d6f8140d02 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 4 Mar 2023 05:37:11 +0100 Subject: Create `iced_widget` subcrate and re-organize the whole codebase --- renderer/src/geometry.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'renderer/src/geometry.rs') diff --git a/renderer/src/geometry.rs b/renderer/src/geometry.rs index e491ea73..361fc86b 100644 --- a/renderer/src/geometry.rs +++ b/renderer/src/geometry.rs @@ -2,9 +2,9 @@ mod cache; pub use cache::Cache; -pub use iced_graphics::geometry::*; - -use crate::{Backend, Point, Rectangle, Size, Vector}; +use crate::core::{Point, Rectangle, Size, Vector}; +use crate::graphics::geometry::{Fill, Geometry, Path, Stroke, Text}; +use crate::Backend; pub enum Frame { Wgpu(iced_wgpu::geometry::Frame), -- cgit From 9b4bcd287a7f4822314e158990d1dc023d5aab51 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 6 Mar 2023 22:10:13 +0100 Subject: Introduce backend feature flags in `iced_renderer` --- renderer/src/geometry.rs | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) (limited to 'renderer/src/geometry.rs') diff --git a/renderer/src/geometry.rs b/renderer/src/geometry.rs index 361fc86b..21ef2c06 100644 --- a/renderer/src/geometry.rs +++ b/renderer/src/geometry.rs @@ -7,14 +7,18 @@ use crate::graphics::geometry::{Fill, Geometry, Path, Stroke, Text}; use crate::Backend; pub enum Frame { + #[cfg(feature = "wgpu")] Wgpu(iced_wgpu::geometry::Frame), + #[cfg(feature = "tiny-skia")] TinySkia(iced_tiny_skia::geometry::Frame), } macro_rules! delegate { - ($frame:expr, $name:ident => $body:expr) => { + ($frame:expr, $name:ident, $body:expr) => { match $frame { + #[cfg(feature = "wgpu")] Self::Wgpu($name) => $body, + #[cfg(feature = "tiny-skia")] Self::TinySkia($name) => $body, } }; @@ -23,9 +27,11 @@ macro_rules! delegate { impl Frame { pub fn new(renderer: &crate::Renderer, size: Size) -> Self { match renderer.backend() { + #[cfg(feature = "wgpu")] Backend::Wgpu(_) => { Frame::Wgpu(iced_wgpu::geometry::Frame::new(size)) } + #[cfg(feature = "tiny-skia")] Backend::TinySkia(_) => { Frame::TinySkia(iced_tiny_skia::geometry::Frame::new(size)) } @@ -35,31 +41,31 @@ impl Frame { /// Returns the width of the [`Frame`]. #[inline] pub fn width(&self) -> f32 { - delegate!(self, frame => frame.width()) + delegate!(self, frame, frame.width()) } /// Returns the height of the [`Frame`]. #[inline] pub fn height(&self) -> f32 { - delegate!(self, frame => frame.height()) + delegate!(self, frame, frame.height()) } /// Returns the dimensions of the [`Frame`]. #[inline] pub fn size(&self) -> Size { - delegate!(self, frame => frame.size()) + delegate!(self, frame, frame.size()) } /// Returns the coordinate of the center of the [`Frame`]. #[inline] pub fn center(&self) -> Point { - delegate!(self, frame => frame.center()) + delegate!(self, frame, frame.center()) } /// Draws the given [`Path`] on the [`Frame`] by filling it with the /// provided style. pub fn fill(&mut self, path: &Path, fill: impl Into) { - delegate!(self, frame => frame.fill(path, fill)); + delegate!(self, frame, frame.fill(path, fill)); } /// Draws an axis-aligned rectangle given its top-left corner coordinate and @@ -70,13 +76,13 @@ impl Frame { size: Size, fill: impl Into, ) { - delegate!(self, frame => frame.fill_rectangle(top_left, size, fill)); + delegate!(self, frame, frame.fill_rectangle(top_left, size, fill)); } /// Draws the stroke of the given [`Path`] on the [`Frame`] with the /// provided style. pub fn stroke<'a>(&mut self, path: &Path, stroke: impl Into>) { - delegate!(self, frame => frame.stroke(path, stroke)); + delegate!(self, frame, frame.stroke(path, stroke)); } /// Draws the characters of the given [`Text`] on the [`Frame`], filling @@ -95,7 +101,7 @@ impl Frame { /// /// [`Canvas`]: crate::widget::Canvas pub fn fill_text(&mut self, text: impl Into) { - delegate!(self, frame => frame.fill_text(text)); + delegate!(self, frame, frame.fill_text(text)); } /// Stores the current transform of the [`Frame`] and executes the given @@ -105,11 +111,11 @@ impl Frame { /// operations in different coordinate systems. #[inline] pub fn with_save(&mut self, f: impl FnOnce(&mut Frame)) { - delegate!(self, frame => frame.push_transform()); + delegate!(self, frame, frame.push_transform()); f(self); - delegate!(self, frame => frame.pop_transform()); + delegate!(self, frame, frame.pop_transform()); } /// Executes the given drawing operations within a [`Rectangle`] region, @@ -121,9 +127,11 @@ impl Frame { #[inline] pub fn with_clip(&mut self, region: Rectangle, f: impl FnOnce(&mut Frame)) { let mut frame = match self { + #[cfg(feature = "wgpu")] Self::Wgpu(_) => { Self::Wgpu(iced_wgpu::geometry::Frame::new(region.size())) } + #[cfg(feature = "tiny-skia")] Self::TinySkia(_) => Self::TinySkia( iced_tiny_skia::geometry::Frame::new(region.size()), ), @@ -134,12 +142,15 @@ impl Frame { let translation = Vector::new(region.x, region.y); match (self, frame) { + #[cfg(feature = "wgpu")] (Self::Wgpu(target), Self::Wgpu(frame)) => { target.clip(frame, translation); } + #[cfg(feature = "tiny-skia")] (Self::TinySkia(target), Self::TinySkia(frame)) => { target.clip(frame, translation); } + #[allow(unreachable_patterns)] _ => unreachable!(), }; } @@ -147,22 +158,22 @@ impl Frame { /// Applies a translation to the current transform of the [`Frame`]. #[inline] pub fn translate(&mut self, translation: Vector) { - delegate!(self, frame => frame.translate(translation)); + delegate!(self, frame, frame.translate(translation)); } /// Applies a rotation in radians to the current transform of the [`Frame`]. #[inline] pub fn rotate(&mut self, angle: f32) { - delegate!(self, frame => frame.rotate(angle)); + delegate!(self, frame, frame.rotate(angle)); } /// Applies a scaling to the current transform of the [`Frame`]. #[inline] pub fn scale(&mut self, scale: f32) { - delegate!(self, frame => frame.scale(scale)); + delegate!(self, frame, frame.scale(scale)); } pub fn into_geometry(self) -> Geometry { - Geometry(delegate!(self, frame => frame.into_primitive())) + Geometry(delegate!(self, frame, frame.into_primitive())) } } -- cgit From dd04c0b070b60b15293892e2a7c284787d3d63b1 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 10 May 2023 22:21:31 +0200 Subject: Bundle `tiny-skia` backend together with `iced_renderer` --- renderer/src/geometry.rs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) (limited to 'renderer/src/geometry.rs') diff --git a/renderer/src/geometry.rs b/renderer/src/geometry.rs index 21ef2c06..a20e9d35 100644 --- a/renderer/src/geometry.rs +++ b/renderer/src/geometry.rs @@ -7,19 +7,17 @@ use crate::graphics::geometry::{Fill, Geometry, Path, Stroke, Text}; use crate::Backend; pub enum Frame { + TinySkia(iced_tiny_skia::geometry::Frame), #[cfg(feature = "wgpu")] Wgpu(iced_wgpu::geometry::Frame), - #[cfg(feature = "tiny-skia")] - TinySkia(iced_tiny_skia::geometry::Frame), } macro_rules! delegate { ($frame:expr, $name:ident, $body:expr) => { match $frame { + Self::TinySkia($name) => $body, #[cfg(feature = "wgpu")] Self::Wgpu($name) => $body, - #[cfg(feature = "tiny-skia")] - Self::TinySkia($name) => $body, } }; } @@ -27,14 +25,13 @@ macro_rules! delegate { impl Frame { pub fn new(renderer: &crate::Renderer, size: Size) -> Self { match renderer.backend() { + Backend::TinySkia(_) => { + Frame::TinySkia(iced_tiny_skia::geometry::Frame::new(size)) + } #[cfg(feature = "wgpu")] Backend::Wgpu(_) => { Frame::Wgpu(iced_wgpu::geometry::Frame::new(size)) } - #[cfg(feature = "tiny-skia")] - Backend::TinySkia(_) => { - Frame::TinySkia(iced_tiny_skia::geometry::Frame::new(size)) - } } } @@ -127,14 +124,13 @@ impl Frame { #[inline] pub fn with_clip(&mut self, region: Rectangle, f: impl FnOnce(&mut Frame)) { let mut frame = match self { + Self::TinySkia(_) => Self::TinySkia( + iced_tiny_skia::geometry::Frame::new(region.size()), + ), #[cfg(feature = "wgpu")] Self::Wgpu(_) => { Self::Wgpu(iced_wgpu::geometry::Frame::new(region.size())) } - #[cfg(feature = "tiny-skia")] - Self::TinySkia(_) => Self::TinySkia( - iced_tiny_skia::geometry::Frame::new(region.size()), - ), }; f(&mut frame); @@ -142,12 +138,11 @@ impl Frame { let translation = Vector::new(region.x, region.y); match (self, frame) { - #[cfg(feature = "wgpu")] - (Self::Wgpu(target), Self::Wgpu(frame)) => { + (Self::TinySkia(target), Self::TinySkia(frame)) => { target.clip(frame, translation); } - #[cfg(feature = "tiny-skia")] - (Self::TinySkia(target), Self::TinySkia(frame)) => { + #[cfg(feature = "wgpu")] + (Self::Wgpu(target), Self::Wgpu(frame)) => { target.clip(frame, translation); } #[allow(unreachable_patterns)] -- cgit From de638f44a5c62459008a5c024b39c2443b72bf25 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 11 May 2023 15:37:56 +0200 Subject: Write missing documentation in `iced_wgpu` --- renderer/src/geometry.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'renderer/src/geometry.rs') diff --git a/renderer/src/geometry.rs b/renderer/src/geometry.rs index a20e9d35..26e2fed0 100644 --- a/renderer/src/geometry.rs +++ b/renderer/src/geometry.rs @@ -135,15 +135,15 @@ impl Frame { f(&mut frame); - let translation = Vector::new(region.x, region.y); + let origin = Point::new(region.x, region.y); match (self, frame) { (Self::TinySkia(target), Self::TinySkia(frame)) => { - target.clip(frame, translation); + target.clip(frame, origin); } #[cfg(feature = "wgpu")] (Self::Wgpu(target), Self::Wgpu(frame)) => { - target.clip(frame, translation); + target.clip(frame, origin); } #[allow(unreachable_patterns)] _ => unreachable!(), -- cgit