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/frame.rs | 52 ++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 8 deletions(-) (limited to 'wgpu/src/widget/canvas/frame.rs') 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 } } } -- 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/frame.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'wgpu/src/widget/canvas/frame.rs') 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); } -- 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/canvas/frame.rs') 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/frame.rs | 59 +++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 31 deletions(-) (limited to 'wgpu/src/widget/canvas/frame.rs') 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, + } } } -- 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/canvas/frame.rs') 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