From 1bb85556915bb00057ef2ee66a596592c292b15b Mon Sep 17 00:00:00 2001 From: Artur Sapek Date: Thu, 5 Mar 2020 22:05:05 -0700 Subject: implement text support in canvas widget --- wgpu/src/widget/canvas.rs | 7 ++--- wgpu/src/widget/canvas/frame.rs | 52 +++++++++++++++++++++++++++++------ wgpu/src/widget/canvas/layer.rs | 7 ++--- wgpu/src/widget/canvas/layer/cache.rs | 25 +++++++---------- wgpu/src/widget/canvas/text.rs | 20 ++++++++++++++ 5 files changed, 80 insertions(+), 31 deletions(-) create mode 100644 wgpu/src/widget/canvas/text.rs (limited to 'wgpu/src/widget') diff --git a/wgpu/src/widget/canvas.rs b/wgpu/src/widget/canvas.rs index 38c1ce62..7d0c5bf3 100644 --- a/wgpu/src/widget/canvas.rs +++ b/wgpu/src/widget/canvas.rs @@ -20,6 +20,7 @@ mod drawable; mod fill; mod frame; mod stroke; +mod text; pub use drawable::Drawable; pub use fill::Fill; @@ -27,6 +28,7 @@ pub use frame::Frame; pub use layer::Layer; pub use path::Path; pub use stroke::{LineCap, LineJoin, Stroke}; +pub use text::TextNode; /// A widget capable of drawing 2D graphics. /// @@ -121,10 +123,7 @@ impl<'a, Message> Widget for Canvas<'a> { primitives: self .layers .iter() - .map(|layer| Primitive::Mesh2D { - origin, - buffers: layer.draw(size), - }) + .map(|layer| layer.draw(origin, size)) .collect(), }, MouseCursor::Idle, diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs index fa6d8c0a..cdb9d0e1 100644 --- a/wgpu/src/widget/canvas/frame.rs +++ b/wgpu/src/widget/canvas/frame.rs @@ -1,10 +1,12 @@ use iced_native::{Point, Size, Vector}; use crate::{ - canvas::{Fill, Path, Stroke}, - triangle, + canvas::{Fill, Path, Stroke, TextNode}, + triangle, Primitive, }; +use std::sync::Arc; + /// The frame of a [`Canvas`]. /// /// [`Canvas`]: struct.Canvas.html @@ -14,6 +16,7 @@ pub struct Frame { height: f32, buffers: lyon::tessellation::VertexBuffers, transforms: Transforms, + texts: Vec, } #[derive(Debug)] @@ -40,6 +43,7 @@ impl Frame { width, height, buffers: lyon::tessellation::VertexBuffers::new(), + texts: Vec::new(), transforms: Transforms { previous: Vec::new(), current: Transform { @@ -154,6 +158,14 @@ impl Frame { let _ = result.expect("Stroke path"); } + /// Draws the text of the given [`TextNode`] on the [`Frame`] + /// + /// [`TextNode`]: struct.TextNode.html + /// [`Frame`]: struct.Frame.html + pub fn text(&mut self, text: TextNode) { + self.texts.push(text); + } + /// Stores the current transform of the [`Frame`] and executes the given /// drawing operations, restoring the transform afterwards. /// @@ -209,14 +221,38 @@ impl Frame { self.transforms.current.is_identity = false; } - /// Produces the geometry that has been drawn on the [`Frame`]. + /// Produces the primitive representing everything drawn on the [`Frame`]. /// /// [`Frame`]: struct.Frame.html - pub fn into_mesh(self) -> triangle::Mesh2D { - triangle::Mesh2D { - vertices: self.buffers.vertices, - indices: self.buffers.indices, - } + pub fn into_primitive(self, origin: Point) -> Primitive { + let mut primitives: Vec = self + .texts + .into_iter() + .map(|mut t| { + t.bounds.x += origin.x; + t.bounds.y += origin.y; + + Primitive::Text { + content: t.content, + bounds: t.bounds, + color: t.color, + size: t.size, + font: t.font, + horizontal_alignment: t.horizontal_alignment, + vertical_alignment: t.vertical_alignment, + } + }) + .collect(); + + primitives.push(Primitive::Mesh2D { + origin, + buffers: Arc::new(triangle::Mesh2D { + vertices: self.buffers.vertices, + indices: self.buffers.indices, + }), + }); + + Primitive::Group { primitives } } } diff --git a/wgpu/src/widget/canvas/layer.rs b/wgpu/src/widget/canvas/layer.rs index 82d647bb..95e2d0ee 100644 --- a/wgpu/src/widget/canvas/layer.rs +++ b/wgpu/src/widget/canvas/layer.rs @@ -3,10 +3,9 @@ mod cache; pub use cache::Cache; -use crate::triangle; +use crate::Primitive; -use iced_native::Size; -use std::sync::Arc; +use iced_native::{Point, Size}; /// A layer that can be presented at a [`Canvas`]. /// @@ -21,5 +20,5 @@ pub trait Layer: std::fmt::Debug { /// /// [`Layer`]: trait.Layer.html /// [`Mesh2D`]: ../../../triangle/struct.Mesh2D.html - fn draw(&self, bounds: Size) -> Arc; + fn draw(&self, origin: Point, bounds: Size) -> Primitive; } diff --git a/wgpu/src/widget/canvas/layer/cache.rs b/wgpu/src/widget/canvas/layer/cache.rs index 3071cce0..f05028da 100644 --- a/wgpu/src/widget/canvas/layer/cache.rs +++ b/wgpu/src/widget/canvas/layer/cache.rs @@ -1,12 +1,10 @@ use crate::{ canvas::{Drawable, Frame, Layer}, - triangle, + Primitive, }; -use iced_native::Size; -use std::cell::RefCell; -use std::marker::PhantomData; -use std::sync::Arc; +use iced_native::{Point, Size}; +use std::{cell::RefCell, marker::PhantomData}; /// A simple cache that stores generated geometry to avoid recomputation. /// @@ -24,10 +22,7 @@ pub struct Cache { #[derive(Debug)] enum State { Empty, - Filled { - mesh: Arc, - bounds: Size, - }, + Filled { bounds: Size, primitive: Primitive }, } impl Cache @@ -75,27 +70,27 @@ impl<'a, T> Layer for Bind<'a, T> where T: Drawable + std::fmt::Debug, { - fn draw(&self, current_bounds: Size) -> Arc { + fn draw(&self, origin: Point, current_bounds: Size) -> Primitive { use std::ops::Deref; - if let State::Filled { mesh, bounds } = + if let State::Filled { bounds, primitive } = self.cache.state.borrow().deref() { if *bounds == current_bounds { - return mesh.clone(); + return primitive.clone(); } } let mut frame = Frame::new(current_bounds.width, current_bounds.height); self.input.draw(&mut frame); - let mesh = Arc::new(frame.into_mesh()); + let primitive = frame.into_primitive(origin); *self.cache.state.borrow_mut() = State::Filled { - mesh: mesh.clone(), bounds: current_bounds, + primitive: primitive.clone(), }; - mesh + primitive } } diff --git a/wgpu/src/widget/canvas/text.rs b/wgpu/src/widget/canvas/text.rs new file mode 100644 index 00000000..5f6abe58 --- /dev/null +++ b/wgpu/src/widget/canvas/text.rs @@ -0,0 +1,20 @@ +use iced_native::{Color, Font, HorizontalAlignment, Rectangle, VerticalAlignment}; + +/// A text node to be drawn to a canvas +#[derive(Debug, Clone)] +pub struct TextNode { + /// The contents of the text + pub content: String, + /// The bounds of the text + pub bounds: Rectangle, + /// The color of the text + pub color: Color, + /// The size of the text + pub size: f32, + /// The font of the text + pub font: Font, + /// The horizontal alignment of the text + pub horizontal_alignment: HorizontalAlignment, + /// The vertical alignment of the text + pub vertical_alignment: VerticalAlignment, +} -- cgit From f35c9f25f03976e058e892662454b1143fd172cd Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 7 Mar 2020 22:27:02 +0100 Subject: Rename `canvas::TextNode` to `canvas::Text` --- wgpu/src/widget/canvas.rs | 2 +- wgpu/src/widget/canvas/frame.rs | 10 +++++----- wgpu/src/widget/canvas/text.rs | 8 +++++--- 3 files changed, 11 insertions(+), 9 deletions(-) (limited to 'wgpu/src/widget') diff --git a/wgpu/src/widget/canvas.rs b/wgpu/src/widget/canvas.rs index 7d0c5bf3..9741ee9d 100644 --- a/wgpu/src/widget/canvas.rs +++ b/wgpu/src/widget/canvas.rs @@ -28,7 +28,7 @@ pub use frame::Frame; pub use layer::Layer; pub use path::Path; pub use stroke::{LineCap, LineJoin, Stroke}; -pub use text::TextNode; +pub use text::Text; /// A widget capable of drawing 2D graphics. /// diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs index cdb9d0e1..44a8d9aa 100644 --- a/wgpu/src/widget/canvas/frame.rs +++ b/wgpu/src/widget/canvas/frame.rs @@ -1,7 +1,7 @@ use iced_native::{Point, Size, Vector}; use crate::{ - canvas::{Fill, Path, Stroke, TextNode}, + canvas::{Fill, Path, Stroke, Text}, triangle, Primitive, }; @@ -16,7 +16,7 @@ pub struct Frame { height: f32, buffers: lyon::tessellation::VertexBuffers, transforms: Transforms, - texts: Vec, + texts: Vec, } #[derive(Debug)] @@ -158,11 +158,11 @@ impl Frame { let _ = result.expect("Stroke path"); } - /// Draws the text of the given [`TextNode`] on the [`Frame`] + /// Draws the text of the given [`Text`] on the [`Frame`] /// - /// [`TextNode`]: struct.TextNode.html + /// [`Text`]: struct.Text.html /// [`Frame`]: struct.Frame.html - pub fn text(&mut self, text: TextNode) { + pub fn text(&mut self, text: Text) { self.texts.push(text); } diff --git a/wgpu/src/widget/canvas/text.rs b/wgpu/src/widget/canvas/text.rs index 5f6abe58..241f8503 100644 --- a/wgpu/src/widget/canvas/text.rs +++ b/wgpu/src/widget/canvas/text.rs @@ -1,8 +1,10 @@ -use iced_native::{Color, Font, HorizontalAlignment, Rectangle, VerticalAlignment}; +use iced_native::{ + Color, Font, HorizontalAlignment, Rectangle, VerticalAlignment, +}; -/// A text node to be drawn to a canvas +/// A bunch of text that can be drawn to a canvas #[derive(Debug, Clone)] -pub struct TextNode { +pub struct Text { /// The contents of the text pub content: String, /// The bounds of the text -- cgit From 37f0d97159d81dbd4801e287a06f4e243e483269 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 7 Mar 2020 22:28:57 +0100 Subject: Rename `Frame::text` to `Frame::fill_text` This keeps the API similar to the Web Canvas API. --- wgpu/src/widget/canvas/frame.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'wgpu/src/widget') diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs index 44a8d9aa..5b82c951 100644 --- a/wgpu/src/widget/canvas/frame.rs +++ b/wgpu/src/widget/canvas/frame.rs @@ -158,11 +158,13 @@ impl Frame { let _ = result.expect("Stroke path"); } - /// Draws the text of the given [`Text`] on the [`Frame`] + /// Draws the characters of the given [`Text`] on the [`Frame`], filling + /// them with the given color. /// /// [`Text`]: struct.Text.html /// [`Frame`]: struct.Frame.html - pub fn text(&mut self, text: Text) { + #[inline] + pub fn fill_text(&mut self, text: Text) { self.texts.push(text); } -- cgit From b74e7e7353d69ffb54cf0c0f0574ea7abf0f3a68 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 7 Mar 2020 23:45:54 +0100 Subject: Implement `Primitive::Cached` --- wgpu/src/widget/canvas.rs | 5 ++- wgpu/src/widget/canvas/frame.rs | 59 +++++++++++++++++------------------ wgpu/src/widget/canvas/layer.rs | 13 ++++---- wgpu/src/widget/canvas/layer/cache.rs | 13 +++++--- wgpu/src/widget/canvas/text.rs | 8 ++--- 5 files changed, 50 insertions(+), 48 deletions(-) (limited to 'wgpu/src/widget') diff --git a/wgpu/src/widget/canvas.rs b/wgpu/src/widget/canvas.rs index 9741ee9d..3a9605c9 100644 --- a/wgpu/src/widget/canvas.rs +++ b/wgpu/src/widget/canvas.rs @@ -123,7 +123,10 @@ impl<'a, Message> Widget for Canvas<'a> { primitives: self .layers .iter() - .map(|layer| layer.draw(origin, size)) + .map(|layer| Primitive::Cached { + origin, + cache: layer.draw(size), + }) .collect(), }, MouseCursor::Idle, diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs index 5b82c951..0bf58320 100644 --- a/wgpu/src/widget/canvas/frame.rs +++ b/wgpu/src/widget/canvas/frame.rs @@ -1,12 +1,10 @@ -use iced_native::{Point, Size, Vector}; +use iced_native::{Point, Rectangle, Size, Vector}; use crate::{ canvas::{Fill, Path, Stroke, Text}, triangle, Primitive, }; -use std::sync::Arc; - /// The frame of a [`Canvas`]. /// /// [`Canvas`]: struct.Canvas.html @@ -15,8 +13,8 @@ pub struct Frame { width: f32, height: f32, buffers: lyon::tessellation::VertexBuffers, + primitives: Vec, transforms: Transforms, - texts: Vec, } #[derive(Debug)] @@ -43,7 +41,7 @@ impl Frame { width, height, buffers: lyon::tessellation::VertexBuffers::new(), - texts: Vec::new(), + primitives: Vec::new(), transforms: Transforms { previous: Vec::new(), current: Transform { @@ -165,7 +163,23 @@ impl Frame { /// [`Frame`]: struct.Frame.html #[inline] pub fn fill_text(&mut self, text: Text) { - self.texts.push(text); + use std::f32; + + // TODO: Use vectorial text instead of primitive + self.primitives.push(Primitive::Text { + content: text.content, + bounds: Rectangle { + x: text.position.x, + y: text.position.y, + width: f32::INFINITY, + height: f32::INFINITY, + }, + color: text.color, + size: text.size, + font: text.font, + horizontal_alignment: text.horizontal_alignment, + vertical_alignment: text.vertical_alignment, + }); } /// Stores the current transform of the [`Frame`] and executes the given @@ -226,35 +240,18 @@ impl Frame { /// Produces the primitive representing everything drawn on the [`Frame`]. /// /// [`Frame`]: struct.Frame.html - pub fn into_primitive(self, origin: Point) -> Primitive { - let mut primitives: Vec = self - .texts - .into_iter() - .map(|mut t| { - t.bounds.x += origin.x; - t.bounds.y += origin.y; - - Primitive::Text { - content: t.content, - bounds: t.bounds, - color: t.color, - size: t.size, - font: t.font, - horizontal_alignment: t.horizontal_alignment, - vertical_alignment: t.vertical_alignment, - } - }) - .collect(); - - primitives.push(Primitive::Mesh2D { - origin, - buffers: Arc::new(triangle::Mesh2D { + pub fn into_primitive(mut self) -> Primitive { + self.primitives.push(Primitive::Mesh2D { + origin: Point::ORIGIN, + buffers: triangle::Mesh2D { vertices: self.buffers.vertices, indices: self.buffers.indices, - }), + }, }); - Primitive::Group { primitives } + Primitive::Group { + primitives: self.primitives, + } } } diff --git a/wgpu/src/widget/canvas/layer.rs b/wgpu/src/widget/canvas/layer.rs index 95e2d0ee..a46b7fb1 100644 --- a/wgpu/src/widget/canvas/layer.rs +++ b/wgpu/src/widget/canvas/layer.rs @@ -4,21 +4,22 @@ mod cache; pub use cache::Cache; use crate::Primitive; +use iced_native::Size; -use iced_native::{Point, Size}; +use std::sync::Arc; /// A layer that can be presented at a [`Canvas`]. /// /// [`Canvas`]: ../struct.Canvas.html pub trait Layer: std::fmt::Debug { - /// Draws the [`Layer`] in the given bounds and produces [`Mesh2D`] as a - /// result. + /// Draws the [`Layer`] in the given bounds and produces a [`Primitive`] as + /// a result. /// - /// The [`Layer`] may choose to store the produced [`Mesh2D`] locally and + /// The [`Layer`] may choose to store the produced [`Primitive`] locally and /// only recompute it when the bounds change, its contents change, or is /// otherwise explicitly cleared by other means. /// /// [`Layer`]: trait.Layer.html - /// [`Mesh2D`]: ../../../triangle/struct.Mesh2D.html - fn draw(&self, origin: Point, bounds: Size) -> Primitive; + /// [`Primitive`]: ../../../enum.Primitive.html + fn draw(&self, bounds: Size) -> Arc; } diff --git a/wgpu/src/widget/canvas/layer/cache.rs b/wgpu/src/widget/canvas/layer/cache.rs index f05028da..6b69f01e 100644 --- a/wgpu/src/widget/canvas/layer/cache.rs +++ b/wgpu/src/widget/canvas/layer/cache.rs @@ -3,8 +3,8 @@ use crate::{ Primitive, }; -use iced_native::{Point, Size}; -use std::{cell::RefCell, marker::PhantomData}; +use iced_native::Size; +use std::{cell::RefCell, marker::PhantomData, sync::Arc}; /// A simple cache that stores generated geometry to avoid recomputation. /// @@ -22,7 +22,10 @@ pub struct Cache { #[derive(Debug)] enum State { Empty, - Filled { bounds: Size, primitive: Primitive }, + Filled { + bounds: Size, + primitive: Arc, + }, } impl Cache @@ -70,7 +73,7 @@ impl<'a, T> Layer for Bind<'a, T> where T: Drawable + std::fmt::Debug, { - fn draw(&self, origin: Point, current_bounds: Size) -> Primitive { + fn draw(&self, current_bounds: Size) -> Arc { use std::ops::Deref; if let State::Filled { bounds, primitive } = @@ -84,7 +87,7 @@ where let mut frame = Frame::new(current_bounds.width, current_bounds.height); self.input.draw(&mut frame); - let primitive = frame.into_primitive(origin); + let primitive = Arc::new(frame.into_primitive()); *self.cache.state.borrow_mut() = State::Filled { bounds: current_bounds, diff --git a/wgpu/src/widget/canvas/text.rs b/wgpu/src/widget/canvas/text.rs index 241f8503..1f9bdc19 100644 --- a/wgpu/src/widget/canvas/text.rs +++ b/wgpu/src/widget/canvas/text.rs @@ -1,14 +1,12 @@ -use iced_native::{ - Color, Font, HorizontalAlignment, Rectangle, VerticalAlignment, -}; +use iced_native::{Color, Font, HorizontalAlignment, Point, VerticalAlignment}; /// A bunch of text that can be drawn to a canvas #[derive(Debug, Clone)] pub struct Text { /// The contents of the text pub content: String, - /// The bounds of the text - pub bounds: Rectangle, + /// The position where to begin drawing the text (top-left corner coordinates) + pub position: Point, /// The color of the text pub color: Color, /// The size of the text -- cgit From e9194cbf4a95ad743e16864f949716701d984a0d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 8 Mar 2020 00:06:04 +0100 Subject: Transform text position in `Frame::fill_text` Also add a warning explaining its current limitations. --- wgpu/src/widget/canvas/frame.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'wgpu/src/widget') diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs index 0bf58320..7d7ce06a 100644 --- a/wgpu/src/widget/canvas/frame.rs +++ b/wgpu/src/widget/canvas/frame.rs @@ -159,18 +159,38 @@ impl Frame { /// 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. + /// /// [`Text`]: struct.Text.html /// [`Frame`]: struct.Frame.html - #[inline] pub fn fill_text(&mut self, text: Text) { use std::f32; + let position = if self.transforms.current.is_identity { + text.position + } else { + let transformed = self.transforms.current.raw.transform_point( + lyon::math::Point::new(text.position.x, text.position.y), + ); + + Point::new(transformed.x, transformed.y) + }; + // TODO: Use vectorial text instead of primitive self.primitives.push(Primitive::Text { content: text.content, bounds: Rectangle { - x: text.position.x, - y: text.position.y, + x: position.x, + y: position.y, width: f32::INFINITY, height: f32::INFINITY, }, -- cgit From b4f970ee7317297615848cd12f422c0cd2889f60 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 8 Mar 2020 00:06:48 +0100 Subject: Implement `Default` for `canvas::Text` --- wgpu/src/widget/canvas/text.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'wgpu/src/widget') diff --git a/wgpu/src/widget/canvas/text.rs b/wgpu/src/widget/canvas/text.rs index 1f9bdc19..d1cf1a0f 100644 --- a/wgpu/src/widget/canvas/text.rs +++ b/wgpu/src/widget/canvas/text.rs @@ -18,3 +18,17 @@ pub struct Text { /// The vertical alignment of the text pub vertical_alignment: VerticalAlignment, } + +impl Default for Text { + fn default() -> Text { + Text { + content: String::new(), + position: Point::ORIGIN, + color: Color::BLACK, + size: 16.0, + font: Font::Default, + horizontal_alignment: HorizontalAlignment::Left, + vertical_alignment: VerticalAlignment::Top, + } + } +} -- cgit