summaryrefslogtreecommitdiffstats
path: root/wgpu/src/geometry.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--wgpu/src/geometry.rs (renamed from graphics/src/widget/canvas/frame.rs)261
1 files changed, 181 insertions, 80 deletions
diff --git a/graphics/src/widget/canvas/frame.rs b/wgpu/src/geometry.rs
index d68548ae..e421e0b0 100644
--- a/graphics/src/widget/canvas/frame.rs
+++ b/wgpu/src/geometry.rs
@@ -1,17 +1,19 @@
-use crate::gradient::Gradient;
-use crate::triangle;
-use crate::widget::canvas::{path, Fill, Geometry, Path, Stroke, Style, Text};
-use crate::Primitive;
-
-use iced_native::{Point, Rectangle, Size, Vector};
+//! Build and draw geometry.
+use crate::core::{Point, Rectangle, Size, Vector};
+use crate::graphics::color;
+use crate::graphics::geometry::fill::{self, Fill};
+use crate::graphics::geometry::{
+ LineCap, LineDash, LineJoin, Path, Stroke, Style, Text,
+};
+use crate::graphics::gradient::{self, Gradient};
+use crate::graphics::mesh::{self, Mesh};
+use crate::primitive::{self, Primitive};
use lyon::geom::euclid;
use lyon::tessellation;
use std::borrow::Cow;
-/// The frame of a [`Canvas`].
-///
-/// [`Canvas`]: crate::widget::Canvas
+/// A frame for drawing some geometry.
#[allow(missing_debug_implementations)]
pub struct Frame {
size: Size,
@@ -23,11 +25,8 @@ pub struct Frame {
}
enum Buffer {
- Solid(tessellation::VertexBuffers<triangle::ColoredVertex2D, u32>),
- Gradient(
- tessellation::VertexBuffers<triangle::Vertex2D, u32>,
- Gradient,
- ),
+ Solid(tessellation::VertexBuffers<mesh::SolidVertex2D, u32>),
+ Gradient(tessellation::VertexBuffers<mesh::GradientVertex2D, u32>),
}
struct BufferStack {
@@ -49,12 +48,11 @@ impl BufferStack {
));
}
},
- Style::Gradient(gradient) => match self.stack.last() {
- Some(Buffer::Gradient(_, last)) if gradient == last => {}
+ Style::Gradient(_) => match self.stack.last() {
+ Some(Buffer::Gradient(_)) => {}
_ => {
self.stack.push(Buffer::Gradient(
tessellation::VertexBuffers::new(),
- gradient.clone(),
));
}
},
@@ -71,12 +69,17 @@ impl BufferStack {
(Style::Solid(color), Buffer::Solid(buffer)) => {
Box::new(tessellation::BuffersBuilder::new(
buffer,
- TriangleVertex2DBuilder(color.into_linear()),
+ TriangleVertex2DBuilder(color::pack(*color)),
+ ))
+ }
+ (Style::Gradient(gradient), Buffer::Gradient(buffer)) => {
+ Box::new(tessellation::BuffersBuilder::new(
+ buffer,
+ GradientVertex2DBuilder {
+ gradient: gradient.pack(),
+ },
))
}
- (Style::Gradient(_), Buffer::Gradient(buffer, _)) => Box::new(
- tessellation::BuffersBuilder::new(buffer, Vertex2DBuilder),
- ),
_ => unreachable!(),
}
}
@@ -89,12 +92,17 @@ impl BufferStack {
(Style::Solid(color), Buffer::Solid(buffer)) => {
Box::new(tessellation::BuffersBuilder::new(
buffer,
- TriangleVertex2DBuilder(color.into_linear()),
+ TriangleVertex2DBuilder(color::pack(*color)),
+ ))
+ }
+ (Style::Gradient(gradient), Buffer::Gradient(buffer)) => {
+ Box::new(tessellation::BuffersBuilder::new(
+ buffer,
+ GradientVertex2DBuilder {
+ gradient: gradient.pack(),
+ },
))
}
- (Style::Gradient(_), Buffer::Gradient(buffer, _)) => Box::new(
- tessellation::BuffersBuilder::new(buffer, Vertex2DBuilder),
- ),
_ => unreachable!(),
}
}
@@ -132,11 +140,13 @@ impl Transform {
}
fn transform_gradient(&self, mut gradient: Gradient) -> Gradient {
- let (start, end) = match &mut gradient {
- Gradient::Linear(linear) => (&mut linear.start, &mut linear.end),
- };
- self.transform_point(start);
- self.transform_point(end);
+ match &mut gradient {
+ Gradient::Linear(linear) => {
+ self.transform_point(&mut linear.start);
+ self.transform_point(&mut linear.end);
+ }
+ }
+
gradient
}
}
@@ -196,8 +206,8 @@ impl Frame {
.buffers
.get_fill(&self.transforms.current.transform_style(style));
- let options =
- tessellation::FillOptions::default().with_fill_rule(rule.into());
+ let options = tessellation::FillOptions::default()
+ .with_fill_rule(into_fill_rule(rule));
if self.transforms.current.is_identity {
self.fill_tessellator.tessellate_path(
@@ -206,7 +216,7 @@ impl Frame {
buffer.as_mut(),
)
} else {
- let path = path.transformed(&self.transforms.current.raw);
+ let path = path.transform(&self.transforms.current.raw);
self.fill_tessellator.tessellate_path(
path.raw(),
@@ -241,8 +251,8 @@ impl Frame {
lyon::math::Vector::new(size.width, size.height),
);
- let options =
- tessellation::FillOptions::default().with_fill_rule(rule.into());
+ let options = tessellation::FillOptions::default()
+ .with_fill_rule(into_fill_rule(rule));
self.fill_tessellator
.tessellate_rectangle(
@@ -264,14 +274,14 @@ impl Frame {
let mut options = tessellation::StrokeOptions::default();
options.line_width = stroke.width;
- options.start_cap = stroke.line_cap.into();
- options.end_cap = stroke.line_cap.into();
- options.line_join = stroke.line_join.into();
+ options.start_cap = into_line_cap(stroke.line_cap);
+ options.end_cap = into_line_cap(stroke.line_cap);
+ options.line_join = into_line_join(stroke.line_join);
let path = if stroke.line_dash.segments.is_empty() {
Cow::Borrowed(path)
} else {
- Cow::Owned(path::dashed(path, stroke.line_dash))
+ Cow::Owned(dashed(path, stroke.line_dash))
};
if self.transforms.current.is_identity {
@@ -281,7 +291,7 @@ impl Frame {
buffer.as_mut(),
)
} else {
- let path = path.transformed(&self.transforms.current.raw);
+ let path = path.transform(&self.transforms.current.raw);
self.stroke_tessellator.tessellate_path(
path.raw(),
@@ -331,9 +341,11 @@ impl Frame {
},
color: text.color,
size: text.size,
+ line_height: text.line_height,
font: text.font,
horizontal_alignment: text.horizontal_alignment,
vertical_alignment: text.vertical_alignment,
+ shaping: text.shaping,
});
}
@@ -344,10 +356,20 @@ impl Frame {
/// operations in different coordinate systems.
#[inline]
pub fn with_save(&mut self, f: impl FnOnce(&mut Frame)) {
- self.transforms.previous.push(self.transforms.current);
+ self.push_transform();
f(self);
+ self.pop_transform();
+ }
+
+ /// Pushes the current transform in the transform stack.
+ pub fn push_transform(&mut self) {
+ self.transforms.previous.push(self.transforms.current);
+ }
+
+ /// Pops a transform from the transform stack and sets it as the current transform.
+ pub fn pop_transform(&mut self) {
self.transforms.current = self.transforms.previous.pop().unwrap();
}
@@ -363,14 +385,21 @@ impl Frame {
f(&mut frame);
+ let origin = Point::new(region.x, region.y);
+
+ self.clip(frame, origin);
+ }
+
+ /// Draws the clipped contents of the given [`Frame`] with origin at the given [`Point`].
+ pub fn clip(&mut self, frame: Frame, at: Point) {
+ let size = frame.size();
let primitives = frame.into_primitives();
+ let translation = Vector::new(at.x, at.y);
let (text, meshes) = primitives
.into_iter()
.partition(|primitive| matches!(primitive, Primitive::Text { .. }));
- let translation = Vector::new(region.x, region.y);
-
self.primitives.push(Primitive::Group {
primitives: vec![
Primitive::Translate {
@@ -380,7 +409,7 @@ impl Frame {
Primitive::Translate {
translation,
content: Box::new(Primitive::Clip {
- bounds: Rectangle::with_size(region.size()),
+ bounds: Rectangle::with_size(size),
content: Box::new(Primitive::Group {
primitives: text,
}),
@@ -423,11 +452,11 @@ impl Frame {
self.transforms.current.is_identity = false;
}
- /// Produces the [`Geometry`] representing everything drawn on the [`Frame`].
- pub fn into_geometry(self) -> Geometry {
- Geometry::from_primitive(Primitive::Group {
+ /// Produces the [`Primitive`] representing everything drawn on the [`Frame`].
+ pub fn into_primitive(self) -> Primitive {
+ Primitive::Group {
primitives: self.into_primitives(),
- })
+ }
}
fn into_primitives(mut self) -> Vec<Primitive> {
@@ -435,25 +464,28 @@ impl Frame {
match buffer {
Buffer::Solid(buffer) => {
if !buffer.indices.is_empty() {
- self.primitives.push(Primitive::SolidMesh {
- buffers: triangle::Mesh2D {
- vertices: buffer.vertices,
- indices: buffer.indices,
- },
- size: self.size,
- })
+ self.primitives.push(Primitive::Custom(
+ primitive::Custom::Mesh(Mesh::Solid {
+ buffers: mesh::Indexed {
+ vertices: buffer.vertices,
+ indices: buffer.indices,
+ },
+ size: self.size,
+ }),
+ ))
}
}
- Buffer::Gradient(buffer, gradient) => {
+ Buffer::Gradient(buffer) => {
if !buffer.indices.is_empty() {
- self.primitives.push(Primitive::GradientMesh {
- buffers: triangle::Mesh2D {
- vertices: buffer.vertices,
- indices: buffer.indices,
- },
- size: self.size,
- gradient,
- })
+ self.primitives.push(Primitive::Custom(
+ primitive::Custom::Mesh(Mesh::Gradient {
+ buffers: mesh::Indexed {
+ vertices: buffer.vertices,
+ indices: buffer.indices,
+ },
+ size: self.size,
+ }),
+ ))
}
}
}
@@ -463,68 +495,137 @@ impl Frame {
}
}
-struct Vertex2DBuilder;
+struct GradientVertex2DBuilder {
+ gradient: gradient::Packed,
+}
-impl tessellation::FillVertexConstructor<triangle::Vertex2D>
- for Vertex2DBuilder
+impl tessellation::FillVertexConstructor<mesh::GradientVertex2D>
+ for GradientVertex2DBuilder
{
fn new_vertex(
&mut self,
vertex: tessellation::FillVertex<'_>,
- ) -> triangle::Vertex2D {
+ ) -> mesh::GradientVertex2D {
let position = vertex.position();
- triangle::Vertex2D {
+ mesh::GradientVertex2D {
position: [position.x, position.y],
+ gradient: self.gradient,
}
}
}
-impl tessellation::StrokeVertexConstructor<triangle::Vertex2D>
- for Vertex2DBuilder
+impl tessellation::StrokeVertexConstructor<mesh::GradientVertex2D>
+ for GradientVertex2DBuilder
{
fn new_vertex(
&mut self,
vertex: tessellation::StrokeVertex<'_, '_>,
- ) -> triangle::Vertex2D {
+ ) -> mesh::GradientVertex2D {
let position = vertex.position();
- triangle::Vertex2D {
+ mesh::GradientVertex2D {
position: [position.x, position.y],
+ gradient: self.gradient,
}
}
}
-struct TriangleVertex2DBuilder([f32; 4]);
+struct TriangleVertex2DBuilder(color::Packed);
-impl tessellation::FillVertexConstructor<triangle::ColoredVertex2D>
+impl tessellation::FillVertexConstructor<mesh::SolidVertex2D>
for TriangleVertex2DBuilder
{
fn new_vertex(
&mut self,
vertex: tessellation::FillVertex<'_>,
- ) -> triangle::ColoredVertex2D {
+ ) -> mesh::SolidVertex2D {
let position = vertex.position();
- triangle::ColoredVertex2D {
+ mesh::SolidVertex2D {
position: [position.x, position.y],
color: self.0,
}
}
}
-impl tessellation::StrokeVertexConstructor<triangle::ColoredVertex2D>
+impl tessellation::StrokeVertexConstructor<mesh::SolidVertex2D>
for TriangleVertex2DBuilder
{
fn new_vertex(
&mut self,
vertex: tessellation::StrokeVertex<'_, '_>,
- ) -> triangle::ColoredVertex2D {
+ ) -> mesh::SolidVertex2D {
let position = vertex.position();
- triangle::ColoredVertex2D {
+ mesh::SolidVertex2D {
position: [position.x, position.y],
color: self.0,
}
}
}
+
+fn into_line_join(line_join: LineJoin) -> lyon::tessellation::LineJoin {
+ match line_join {
+ LineJoin::Miter => lyon::tessellation::LineJoin::Miter,
+ LineJoin::Round => lyon::tessellation::LineJoin::Round,
+ LineJoin::Bevel => lyon::tessellation::LineJoin::Bevel,
+ }
+}
+
+fn into_line_cap(line_cap: LineCap) -> lyon::tessellation::LineCap {
+ match line_cap {
+ LineCap::Butt => lyon::tessellation::LineCap::Butt,
+ LineCap::Square => lyon::tessellation::LineCap::Square,
+ LineCap::Round => lyon::tessellation::LineCap::Round,
+ }
+}
+
+fn into_fill_rule(rule: fill::Rule) -> lyon::tessellation::FillRule {
+ match rule {
+ fill::Rule::NonZero => lyon::tessellation::FillRule::NonZero,
+ fill::Rule::EvenOdd => lyon::tessellation::FillRule::EvenOdd,
+ }
+}
+
+pub(super) fn dashed(path: &Path, line_dash: LineDash<'_>) -> Path {
+ use lyon::algorithms::walk::{
+ walk_along_path, RepeatedPattern, WalkerEvent,
+ };
+ use lyon::path::iterator::PathIterator;
+
+ Path::new(|builder| {
+ let segments_odd = (line_dash.segments.len() % 2 == 1)
+ .then(|| [line_dash.segments, line_dash.segments].concat());
+
+ let mut draw_line = false;
+
+ walk_along_path(
+ path.raw().iter().flattened(0.01),
+ 0.0,
+ lyon::tessellation::StrokeOptions::DEFAULT_TOLERANCE,
+ &mut RepeatedPattern {
+ callback: |event: WalkerEvent<'_>| {
+ let point = Point {
+ x: event.position.x,
+ y: event.position.y,
+ };
+
+ if draw_line {
+ builder.line_to(point);
+ } else {
+ builder.move_to(point);
+ }
+
+ draw_line = !draw_line;
+
+ true
+ },
+ index: line_dash.offset,
+ intervals: segments_odd
+ .as_deref()
+ .unwrap_or(line_dash.segments),
+ },
+ );
+ })
+}