Implement Canvas support for iced_tiny_skia

This commit is contained in:
Héctor Ramón Jiménez 2023-03-01 21:34:26 +01:00
parent 3f6e28fa9b
commit 5fd5d1cdf8
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
65 changed files with 1354 additions and 570 deletions

View file

@ -10,6 +10,8 @@ use std::borrow::Cow;
///
/// [`Renderer`]: crate::Renderer
pub trait Backend {
type Geometry: Into<crate::Primitive>;
/// Trims the measurements cache.
///
/// This method is currently necessary to properly trim the text cache in

View file

@ -1,117 +0,0 @@
//! For creating a Gradient.
pub mod linear;
pub use linear::Linear;
use crate::{Color, Point, Size};
#[derive(Debug, Clone, PartialEq)]
/// A fill which transitions colors progressively along a direction, either linearly, radially (TBD),
/// or conically (TBD).
pub enum Gradient {
/// A linear gradient interpolates colors along a direction from its `start` to its `end`
/// point.
Linear(Linear),
}
impl Gradient {
/// Creates a new linear [`linear::Builder`].
pub fn linear(position: impl Into<Position>) -> linear::Builder {
linear::Builder::new(position.into())
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
/// A point along the gradient vector where the specified [`color`] is unmixed.
///
/// [`color`]: Self::color
pub struct ColorStop {
/// Offset along the gradient vector.
pub offset: f32,
/// The color of the gradient at the specified [`offset`].
///
/// [`offset`]: Self::offset
pub color: Color,
}
#[derive(Debug)]
/// The position of the gradient within its bounds.
pub enum Position {
/// The gradient will be positioned with respect to two points.
Absolute {
/// The starting point of the gradient.
start: Point,
/// The ending point of the gradient.
end: Point,
},
/// The gradient will be positioned relative to the provided bounds.
Relative {
/// The top left position of the bounds.
top_left: Point,
/// The width & height of the bounds.
size: Size,
/// The start [Location] of the gradient.
start: Location,
/// The end [Location] of the gradient.
end: Location,
},
}
impl From<(Point, Point)> for Position {
fn from((start, end): (Point, Point)) -> Self {
Self::Absolute { start, end }
}
}
#[derive(Debug, Clone, Copy)]
/// The location of a relatively-positioned gradient.
pub enum Location {
/// Top left.
TopLeft,
/// Top.
Top,
/// Top right.
TopRight,
/// Right.
Right,
/// Bottom right.
BottomRight,
/// Bottom.
Bottom,
/// Bottom left.
BottomLeft,
/// Left.
Left,
}
impl Location {
fn to_absolute(self, top_left: Point, size: Size) -> Point {
match self {
Location::TopLeft => top_left,
Location::Top => {
Point::new(top_left.x + size.width / 2.0, top_left.y)
}
Location::TopRight => {
Point::new(top_left.x + size.width, top_left.y)
}
Location::Right => Point::new(
top_left.x + size.width,
top_left.y + size.height / 2.0,
),
Location::BottomRight => {
Point::new(top_left.x + size.width, top_left.y + size.height)
}
Location::Bottom => Point::new(
top_left.x + size.width / 2.0,
top_left.y + size.height,
),
Location::BottomLeft => {
Point::new(top_left.x, top_left.y + size.height)
}
Location::Left => {
Point::new(top_left.x, top_left.y + size.height / 2.0)
}
}
}
}

View file

@ -1,112 +0,0 @@
//! Linear gradient builder & definition.
use crate::gradient::{ColorStop, Gradient, Position};
use crate::{Color, Point};
/// A linear gradient that can be used in the style of [`Fill`] or [`Stroke`].
///
/// [`Fill`]: crate::widget::canvas::Fill
/// [`Stroke`]: crate::widget::canvas::Stroke
#[derive(Debug, Clone, PartialEq)]
pub struct Linear {
/// The point where the linear gradient begins.
pub start: Point,
/// The point where the linear gradient ends.
pub end: Point,
/// [`ColorStop`]s along the linear gradient path.
pub color_stops: Vec<ColorStop>,
}
/// A [`Linear`] builder.
#[derive(Debug)]
pub struct Builder {
start: Point,
end: Point,
stops: Vec<ColorStop>,
error: Option<BuilderError>,
}
impl Builder {
/// Creates a new [`Builder`].
pub fn new(position: Position) -> Self {
let (start, end) = match position {
Position::Absolute { start, end } => (start, end),
Position::Relative {
top_left,
size,
start,
end,
} => (
start.to_absolute(top_left, size),
end.to_absolute(top_left, size),
),
};
Self {
start,
end,
stops: vec![],
error: None,
}
}
/// Adds a new stop, defined by an offset and a color, to the gradient.
///
/// `offset` must be between `0.0` and `1.0` or the gradient cannot be built.
///
/// Note: when using the [`glow`] backend, any color stop added after the 16th
/// will not be displayed.
///
/// On the [`wgpu`] backend this limitation does not exist (technical limit is 524,288 stops).
///
/// [`glow`]: https://docs.rs/iced_glow
/// [`wgpu`]: https://docs.rs/iced_wgpu
pub fn add_stop(mut self, offset: f32, color: Color) -> Self {
if offset.is_finite() && (0.0..=1.0).contains(&offset) {
match self.stops.binary_search_by(|stop| {
stop.offset.partial_cmp(&offset).unwrap()
}) {
Ok(_) => {
self.error = Some(BuilderError::DuplicateOffset(offset))
}
Err(index) => {
self.stops.insert(index, ColorStop { offset, color });
}
}
} else {
self.error = Some(BuilderError::InvalidOffset(offset))
};
self
}
/// Builds the linear [`Gradient`] of this [`Builder`].
///
/// Returns `BuilderError` if gradient in invalid.
pub fn build(self) -> Result<Gradient, BuilderError> {
if self.stops.is_empty() {
Err(BuilderError::MissingColorStop)
} else if let Some(error) = self.error {
Err(error)
} else {
Ok(Gradient::Linear(Linear {
start: self.start,
end: self.end,
color_stops: self.stops,
}))
}
}
}
/// An error that happened when building a [`Linear`] gradient.
#[derive(Debug, thiserror::Error)]
pub enum BuilderError {
#[error("Gradients must contain at least one color stop.")]
/// Gradients must contain at least one color stop.
MissingColorStop,
#[error("Offset {0} must be a unique, finite number.")]
/// Offsets in a gradient must all be unique & finite.
DuplicateOffset(f32),
#[error("Offset {0} must be between 0.0..=1.0.")]
/// Offsets in a gradient must be between 0.0..=1.0.
InvalidOffset(f32),
}

View file

@ -1,270 +0,0 @@
//! Organize rendering primitives into a flattened list of layers.
mod image;
mod quad;
mod text;
pub mod mesh;
pub use image::Image;
pub use mesh::Mesh;
pub use quad::Quad;
pub use text::Text;
use crate::alignment;
use crate::{
Background, Color, Font, Point, Primitive, Rectangle, Size, Vector,
Viewport,
};
/// A group of primitives that should be clipped together.
#[derive(Debug)]
pub struct Layer<'a> {
/// The clipping bounds of the [`Layer`].
pub bounds: Rectangle,
/// The quads of the [`Layer`].
pub quads: Vec<Quad>,
/// The triangle meshes of the [`Layer`].
pub meshes: Vec<Mesh<'a>>,
/// The text of the [`Layer`].
pub text: Vec<Text<'a>>,
/// The images of the [`Layer`].
pub images: Vec<Image>,
}
impl<'a> Layer<'a> {
/// Creates a new [`Layer`] with the given clipping bounds.
pub fn new(bounds: Rectangle) -> Self {
Self {
bounds,
quads: Vec::new(),
meshes: Vec::new(),
text: Vec::new(),
images: Vec::new(),
}
}
/// Creates a new [`Layer`] for the provided overlay text.
///
/// This can be useful for displaying debug information.
pub fn overlay(lines: &'a [impl AsRef<str>], viewport: &Viewport) -> Self {
let mut overlay =
Layer::new(Rectangle::with_size(viewport.logical_size()));
for (i, line) in lines.iter().enumerate() {
let text = Text {
content: line.as_ref(),
bounds: Rectangle::new(
Point::new(11.0, 11.0 + 25.0 * i as f32),
Size::INFINITY,
),
color: Color::new(0.9, 0.9, 0.9, 1.0),
size: 20.0,
font: Font::Monospace,
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
};
overlay.text.push(text);
overlay.text.push(Text {
bounds: text.bounds + Vector::new(-1.0, -1.0),
color: Color::BLACK,
..text
});
}
overlay
}
/// Distributes the given [`Primitive`] and generates a list of layers based
/// on its contents.
pub fn generate(
primitives: &'a [Primitive],
viewport: &Viewport,
) -> Vec<Self> {
let first_layer =
Layer::new(Rectangle::with_size(viewport.logical_size()));
let mut layers = vec![first_layer];
for primitive in primitives {
Self::process_primitive(
&mut layers,
Vector::new(0.0, 0.0),
primitive,
0,
);
}
layers
}
fn process_primitive(
layers: &mut Vec<Self>,
translation: Vector,
primitive: &'a Primitive,
current_layer: usize,
) {
match primitive {
Primitive::None => {}
Primitive::Group { primitives } => {
// TODO: Inspect a bit and regroup (?)
for primitive in primitives {
Self::process_primitive(
layers,
translation,
primitive,
current_layer,
)
}
}
Primitive::Text {
content,
bounds,
size,
color,
font,
horizontal_alignment,
vertical_alignment,
} => {
let layer = &mut layers[current_layer];
layer.text.push(Text {
content,
bounds: *bounds + translation,
size: *size,
color: *color,
font: *font,
horizontal_alignment: *horizontal_alignment,
vertical_alignment: *vertical_alignment,
});
}
Primitive::Quad {
bounds,
background,
border_radius,
border_width,
border_color,
} => {
let layer = &mut layers[current_layer];
// TODO: Move some of these computations to the GPU (?)
layer.quads.push(Quad {
position: [
bounds.x + translation.x,
bounds.y + translation.y,
],
size: [bounds.width, bounds.height],
color: match background {
Background::Color(color) => color.into_linear(),
},
border_radius: *border_radius,
border_width: *border_width,
border_color: border_color.into_linear(),
});
}
Primitive::SolidMesh { buffers, size } => {
let layer = &mut layers[current_layer];
let bounds = Rectangle::new(
Point::new(translation.x, translation.y),
*size,
);
// Only draw visible content
if let Some(clip_bounds) = layer.bounds.intersection(&bounds) {
layer.meshes.push(Mesh::Solid {
origin: Point::new(translation.x, translation.y),
buffers,
clip_bounds,
});
}
}
Primitive::GradientMesh {
buffers,
size,
gradient,
} => {
let layer = &mut layers[current_layer];
let bounds = Rectangle::new(
Point::new(translation.x, translation.y),
*size,
);
// Only draw visible content
if let Some(clip_bounds) = layer.bounds.intersection(&bounds) {
layer.meshes.push(Mesh::Gradient {
origin: Point::new(translation.x, translation.y),
buffers,
clip_bounds,
gradient,
});
}
}
Primitive::Clip { bounds, content } => {
let layer = &mut layers[current_layer];
let translated_bounds = *bounds + translation;
// Only draw visible content
if let Some(clip_bounds) =
layer.bounds.intersection(&translated_bounds)
{
let clip_layer = Layer::new(clip_bounds);
layers.push(clip_layer);
Self::process_primitive(
layers,
translation,
content,
layers.len() - 1,
);
}
}
Primitive::Translate {
translation: new_translation,
content,
} => {
Self::process_primitive(
layers,
translation + *new_translation,
content,
current_layer,
);
}
Primitive::Cached { cache } => {
Self::process_primitive(
layers,
translation,
cache,
current_layer,
);
}
Primitive::Image { handle, bounds } => {
let layer = &mut layers[current_layer];
layer.images.push(Image::Raster {
handle: handle.clone(),
bounds: *bounds + translation,
});
}
Primitive::Svg {
handle,
color,
bounds,
} => {
let layer = &mut layers[current_layer];
layer.images.push(Image::Vector {
handle: handle.clone(),
color: *color,
bounds: *bounds + translation,
});
}
}
}
}

View file

@ -1,27 +0,0 @@
use crate::{Color, Rectangle};
use iced_native::{image, svg};
/// A raster or vector image.
#[derive(Debug, Clone)]
pub enum Image {
/// A raster image.
Raster {
/// The handle of a raster image.
handle: image::Handle,
/// The bounds of the image.
bounds: Rectangle,
},
/// A vector image.
Vector {
/// The handle of a vector image.
handle: svg::Handle,
/// The [`Color`] filter
color: Option<Color>,
/// The bounds of the image.
bounds: Rectangle,
},
}

View file

@ -1,93 +0,0 @@
//! A collection of triangle primitives.
use crate::triangle;
use crate::{Gradient, Point, Rectangle};
/// A mesh of triangles.
#[derive(Debug, Clone, Copy)]
pub enum Mesh<'a> {
/// A mesh of triangles with a solid color.
Solid {
/// The origin of the vertices of the [`Mesh`].
origin: Point,
/// The vertex and index buffers of the [`Mesh`].
buffers: &'a triangle::Mesh2D<triangle::ColoredVertex2D>,
/// The clipping bounds of the [`Mesh`].
clip_bounds: Rectangle<f32>,
},
/// A mesh of triangles with a gradient color.
Gradient {
/// The origin of the vertices of the [`Mesh`].
origin: Point,
/// The vertex and index buffers of the [`Mesh`].
buffers: &'a triangle::Mesh2D<triangle::Vertex2D>,
/// The clipping bounds of the [`Mesh`].
clip_bounds: Rectangle<f32>,
/// The gradient to apply to the [`Mesh`].
gradient: &'a Gradient,
},
}
impl Mesh<'_> {
/// Returns the origin of the [`Mesh`].
pub fn origin(&self) -> Point {
match self {
Self::Solid { origin, .. } | Self::Gradient { origin, .. } => {
*origin
}
}
}
/// Returns the indices of the [`Mesh`].
pub fn indices(&self) -> &[u32] {
match self {
Self::Solid { buffers, .. } => &buffers.indices,
Self::Gradient { buffers, .. } => &buffers.indices,
}
}
/// Returns the clip bounds of the [`Mesh`].
pub fn clip_bounds(&self) -> Rectangle<f32> {
match self {
Self::Solid { clip_bounds, .. }
| Self::Gradient { clip_bounds, .. } => *clip_bounds,
}
}
}
/// The result of counting the attributes of a set of meshes.
#[derive(Debug, Clone, Copy, Default)]
pub struct AttributeCount {
/// The total amount of solid vertices.
pub solid_vertices: usize,
/// The total amount of gradient vertices.
pub gradient_vertices: usize,
/// The total amount of indices.
pub indices: usize,
}
/// Returns the number of total vertices & total indices of all [`Mesh`]es.
pub fn attribute_count_of<'a>(meshes: &'a [Mesh<'a>]) -> AttributeCount {
meshes
.iter()
.fold(AttributeCount::default(), |mut count, mesh| {
match mesh {
Mesh::Solid { buffers, .. } => {
count.solid_vertices += buffers.vertices.len();
count.indices += buffers.indices.len();
}
Mesh::Gradient { buffers, .. } => {
count.gradient_vertices += buffers.vertices.len();
count.indices += buffers.indices.len();
}
}
count
})
}

View file

@ -1,30 +0,0 @@
/// A colored rectangle with a border.
///
/// This type can be directly uploaded to GPU memory.
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct Quad {
/// The position of the [`Quad`].
pub position: [f32; 2],
/// The size of the [`Quad`].
pub size: [f32; 2],
/// The color of the [`Quad`], in __linear RGB__.
pub color: [f32; 4],
/// The border color of the [`Quad`], in __linear RGB__.
pub border_color: [f32; 4],
/// The border radius of the [`Quad`].
pub border_radius: [f32; 4],
/// The border width of the [`Quad`].
pub border_width: f32,
}
#[allow(unsafe_code)]
unsafe impl bytemuck::Zeroable for Quad {}
#[allow(unsafe_code)]
unsafe impl bytemuck::Pod for Quad {}

View file

@ -1,26 +0,0 @@
use crate::{alignment, Color, Font, Rectangle};
/// A paragraph of text.
#[derive(Debug, Clone, Copy)]
pub struct Text<'a> {
/// The content of the [`Text`].
pub content: &'a str,
/// The layout bounds of the [`Text`].
pub bounds: Rectangle,
/// The color of the [`Text`], in __linear RGB_.
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: alignment::Horizontal,
/// The vertical alignment of the [`Text`].
pub vertical_alignment: alignment::Vertical,
}

View file

@ -9,7 +9,7 @@
)]
#![deny(
missing_debug_implementations,
missing_docs,
//missing_docs,
unsafe_code,
unused_results,
clippy::extra_unused_lifetimes,
@ -23,25 +23,19 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
mod antialiasing;
mod error;
mod primitive;
mod transformation;
mod viewport;
pub mod backend;
pub mod gradient;
pub mod image;
pub mod layer;
pub mod overlay;
pub mod primitive;
pub mod renderer;
pub mod triangle;
pub mod widget;
pub mod window;
pub use antialiasing::Antialiasing;
pub use backend::Backend;
pub use error::Error;
pub use gradient::Gradient;
pub use layer::Layer;
pub use primitive::Primitive;
pub use renderer::Renderer;
pub use transformation::Transformation;
@ -50,5 +44,6 @@ pub use viewport::Viewport;
pub use iced_native::alignment;
pub use iced_native::text;
pub use iced_native::{
Alignment, Background, Color, Font, Point, Rectangle, Size, Vector,
Alignment, Background, Color, Font, Gradient, Point, Rectangle, Size,
Vector,
};

View file

@ -1,23 +1,15 @@
use crate::alignment;
use iced_native::image;
use iced_native::svg;
use iced_native::{Background, Color, Font, Rectangle, Size, Vector};
use crate::alignment;
use crate::gradient::Gradient;
use crate::triangle;
use iced_native::{Background, Color, Font, Gradient, Rectangle, Size, Vector};
use bytemuck::{Pod, Zeroable};
use std::sync::Arc;
/// A rendering primitive.
#[derive(Debug, Clone)]
pub enum Primitive {
/// An empty primitive
None,
/// A group of primitives
Group {
/// The primitives of the group
primitives: Vec<Primitive>,
},
/// A text primitive
Text {
/// The contents of the text
@ -66,6 +58,50 @@ pub enum Primitive {
/// The bounds of the viewport
bounds: Rectangle,
},
/// A low-level primitive to render a mesh of triangles with a solid color.
///
/// It can be used to render many kinds of geometry freely.
SolidMesh {
/// The vertices and indices of the mesh.
buffers: Mesh2D<ColoredVertex2D>,
/// The size of the drawable region of the mesh.
///
/// Any geometry that falls out of this region will be clipped.
size: Size,
},
/// A low-level primitive to render a mesh of triangles with a gradient.
///
/// It can be used to render many kinds of geometry freely.
GradientMesh {
/// The vertices and indices of the mesh.
buffers: Mesh2D<Vertex2D>,
/// The size of the drawable region of the mesh.
///
/// Any geometry that falls out of this region will be clipped.
size: Size,
/// The [`Gradient`] to apply to the mesh.
gradient: Gradient,
},
Fill {
path: tiny_skia::Path,
paint: tiny_skia::Paint<'static>,
rule: tiny_skia::FillRule,
transform: tiny_skia::Transform,
},
Stroke {
path: tiny_skia::Path,
paint: tiny_skia::Paint<'static>,
stroke: tiny_skia::Stroke,
transform: tiny_skia::Transform,
},
/// A group of primitives
Group {
/// The primitives of the group
primitives: Vec<Primitive>,
},
/// A clip primitive
Clip {
/// The bounds of the clip
@ -81,45 +117,69 @@ pub enum Primitive {
/// The primitive to translate
content: Box<Primitive>,
},
/// A low-level primitive to render a mesh of triangles with a solid color.
///
/// It can be used to render many kinds of geometry freely.
SolidMesh {
/// The vertices and indices of the mesh.
buffers: triangle::Mesh2D<triangle::ColoredVertex2D>,
/// The size of the drawable region of the mesh.
///
/// Any geometry that falls out of this region will be clipped.
size: Size,
},
/// A low-level primitive to render a mesh of triangles with a gradient.
///
/// It can be used to render many kinds of geometry freely.
GradientMesh {
/// The vertices and indices of the mesh.
buffers: triangle::Mesh2D<triangle::Vertex2D>,
/// The size of the drawable region of the mesh.
///
/// Any geometry that falls out of this region will be clipped.
size: Size,
/// The [`Gradient`] to apply to the mesh.
gradient: Gradient,
},
/// A cached primitive.
///
/// This can be useful if you are implementing a widget where primitive
/// generation is expensive.
Cached {
Cache {
/// The cached primitive
cache: Arc<Primitive>,
content: Arc<Primitive>,
},
}
impl Default for Primitive {
fn default() -> Primitive {
Primitive::None
impl Primitive {
pub fn group(primitives: Vec<Self>) -> Self {
Self::Group { primitives }
}
pub fn clip(self, bounds: Rectangle) -> Self {
Self::Clip {
bounds,
content: Box::new(self),
}
}
pub fn translate(self, translation: Vector) -> Self {
Self::Translate {
translation,
content: Box::new(self),
}
}
}
/// A set of [`Vertex2D`] and indices representing a list of triangles.
#[derive(Clone, Debug)]
pub struct Mesh2D<T> {
/// The vertices of the mesh
pub vertices: Vec<T>,
/// The list of vertex indices that defines the triangles of the mesh.
///
/// Therefore, this list should always have a length that is a multiple of 3.
pub indices: Vec<u32>,
}
/// A two-dimensional vertex.
#[derive(Copy, Clone, Debug, Zeroable, Pod)]
#[repr(C)]
pub struct Vertex2D {
/// The vertex position in 2D space.
pub position: [f32; 2],
}
/// A two-dimensional vertex with a color.
#[derive(Copy, Clone, Debug, Zeroable, Pod)]
#[repr(C)]
pub struct ColoredVertex2D {
/// The vertex position in 2D space.
pub position: [f32; 2],
/// The color of the vertex in __linear__ RGBA.
pub color: [f32; 4],
}
impl From<()> for Primitive {
fn from(_: ()) -> Self {
Self::Group { primitives: vec![] }
}
}

View file

@ -1,6 +1,7 @@
//! Create a renderer from a [`Backend`].
use crate::backend::{self, Backend};
use crate::{Primitive, Vector};
use iced_native::image;
use iced_native::layout;
use iced_native::renderer;
@ -70,19 +71,13 @@ where
}
fn with_layer(&mut self, bounds: Rectangle, f: impl FnOnce(&mut Self)) {
let current_primitives = std::mem::take(&mut self.primitives);
let current = std::mem::take(&mut self.primitives);
f(self);
let layer_primitives =
std::mem::replace(&mut self.primitives, current_primitives);
let layer = std::mem::replace(&mut self.primitives, current);
self.primitives.push(Primitive::Clip {
bounds,
content: Box::new(Primitive::Group {
primitives: layer_primitives,
}),
});
self.primitives.push(Primitive::group(layer).clip(bounds));
}
fn with_translation(
@ -90,19 +85,14 @@ where
translation: Vector,
f: impl FnOnce(&mut Self),
) {
let current_primitives = std::mem::take(&mut self.primitives);
let current = std::mem::take(&mut self.primitives);
f(self);
let layer_primitives =
std::mem::replace(&mut self.primitives, current_primitives);
let layer = std::mem::replace(&mut self.primitives, current);
self.primitives.push(Primitive::Translate {
translation,
content: Box::new(Primitive::Group {
primitives: layer_primitives,
}),
});
self.primitives
.push(Primitive::group(layer).translate(translation));
}
fn fill_quad(
@ -199,7 +189,7 @@ where
}
fn draw(&mut self, handle: image::Handle, bounds: Rectangle) {
self.draw_primitive(Primitive::Image { handle, bounds })
self.primitives.push(Primitive::Image { handle, bounds })
}
}
@ -217,10 +207,23 @@ where
color: Option<Color>,
bounds: Rectangle,
) {
self.draw_primitive(Primitive::Svg {
self.primitives.push(Primitive::Svg {
handle,
color,
bounds,
})
}
}
#[cfg(feature = "canvas")]
impl<B, T> iced_native::widget::canvas::Renderer for Renderer<B, T>
where
B: Backend,
{
type Geometry = B::Geometry;
fn draw(&mut self, layers: Vec<Self::Geometry>) {
self.primitives
.extend(layers.into_iter().map(B::Geometry::into));
}
}

View file

@ -1,33 +1 @@
//! Draw geometry using meshes of triangles.
use bytemuck::{Pod, Zeroable};
/// A set of [`Vertex2D`] and indices representing a list of triangles.
#[derive(Clone, Debug)]
pub struct Mesh2D<T> {
/// The vertices of the mesh
pub vertices: Vec<T>,
/// The list of vertex indices that defines the triangles of the mesh.
///
/// Therefore, this list should always have a length that is a multiple of 3.
pub indices: Vec<u32>,
}
/// A two-dimensional vertex.
#[derive(Copy, Clone, Debug, Zeroable, Pod)]
#[repr(C)]
pub struct Vertex2D {
/// The vertex position in 2D space.
pub position: [f32; 2],
}
/// A two-dimensional vertex with a color.
#[derive(Copy, Clone, Debug, Zeroable, Pod)]
#[repr(C)]
pub struct ColoredVertex2D {
/// The vertex position in 2D space.
pub position: [f32; 2],
/// The color of the vertex in __linear__ RGBA.
pub color: [f32; 4],
}

View file

@ -1,16 +0,0 @@
//! Use the graphical widgets supported out-of-the-box.
#[cfg(feature = "canvas")]
#[cfg_attr(docsrs, doc(cfg(feature = "canvas")))]
pub mod canvas;
#[cfg(feature = "canvas")]
#[doc(no_inline)]
pub use canvas::Canvas;
#[cfg(feature = "qr_code")]
#[cfg_attr(docsrs, doc(cfg(feature = "qr_code")))]
pub mod qr_code;
#[cfg(feature = "qr_code")]
#[doc(no_inline)]
pub use qr_code::QRCode;

View file

@ -1,268 +0,0 @@
//! Draw 2D graphics for your users.
//!
//! A [`Canvas`] widget can be used to draw different kinds of 2D shapes in a
//! [`Frame`]. It can be used for animation, data visualization, game graphics,
//! and more!
pub mod event;
pub mod fill;
pub mod path;
pub mod stroke;
mod cache;
mod cursor;
mod frame;
mod geometry;
mod program;
mod style;
mod text;
pub use crate::gradient::{self, Gradient};
pub use cache::Cache;
pub use cursor::Cursor;
pub use event::Event;
pub use fill::{Fill, FillRule};
pub use frame::Frame;
pub use geometry::Geometry;
pub use path::Path;
pub use program::Program;
pub use stroke::{LineCap, LineDash, LineJoin, Stroke};
pub use style::Style;
pub use text::Text;
use crate::{Backend, Primitive, Renderer};
use iced_native::layout::{self, Layout};
use iced_native::mouse;
use iced_native::renderer;
use iced_native::widget::tree::{self, Tree};
use iced_native::{
Clipboard, Element, Length, Point, Rectangle, Shell, Size, Vector, Widget,
};
use std::marker::PhantomData;
/// A widget capable of drawing 2D graphics.
///
/// ## Drawing a simple circle
/// If you want to get a quick overview, here's how we can draw a simple circle:
///
/// ```no_run
/// # mod iced {
/// # pub mod widget {
/// # pub use iced_graphics::widget::canvas;
/// # }
/// # pub use iced_native::{Color, Rectangle, Theme};
/// # }
/// use iced::widget::canvas::{self, Canvas, Cursor, Fill, Frame, Geometry, Path, Program};
/// use iced::{Color, Rectangle, Theme};
///
/// // First, we define the data we need for drawing
/// #[derive(Debug)]
/// struct Circle {
/// radius: f32,
/// }
///
/// // Then, we implement the `Program` trait
/// impl Program<()> for Circle {
/// type State = ();
///
/// fn draw(&self, _state: &(), _theme: &Theme, bounds: Rectangle, _cursor: Cursor) -> Vec<Geometry>{
/// // We prepare a new `Frame`
/// let mut frame = Frame::new(bounds.size());
///
/// // We create a `Path` representing a simple circle
/// let circle = Path::circle(frame.center(), self.radius);
///
/// // And fill it with some color
/// frame.fill(&circle, Color::BLACK);
///
/// // Finally, we produce the geometry
/// vec![frame.into_geometry()]
/// }
/// }
///
/// // Finally, we simply use our `Circle` to create the `Canvas`!
/// let canvas = Canvas::new(Circle { radius: 50.0 });
/// ```
#[derive(Debug)]
pub struct Canvas<Message, Theme, P>
where
P: Program<Message, Theme>,
{
width: Length,
height: Length,
program: P,
message_: PhantomData<Message>,
theme_: PhantomData<Theme>,
}
impl<Message, Theme, P> Canvas<Message, Theme, P>
where
P: Program<Message, Theme>,
{
const DEFAULT_SIZE: f32 = 100.0;
/// Creates a new [`Canvas`].
pub fn new(program: P) -> Self {
Canvas {
width: Length::Fixed(Self::DEFAULT_SIZE),
height: Length::Fixed(Self::DEFAULT_SIZE),
program,
message_: PhantomData,
theme_: PhantomData,
}
}
/// Sets the width of the [`Canvas`].
pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width.into();
self
}
/// Sets the height of the [`Canvas`].
pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height.into();
self
}
}
impl<Message, P, B, T> Widget<Message, Renderer<B, T>> for Canvas<Message, T, P>
where
P: Program<Message, T>,
B: Backend,
{
fn tag(&self) -> tree::Tag {
struct Tag<T>(T);
tree::Tag::of::<Tag<P::State>>()
}
fn state(&self) -> tree::State {
tree::State::new(P::State::default())
}
fn width(&self) -> Length {
self.width
}
fn height(&self) -> Length {
self.height
}
fn layout(
&self,
_renderer: &Renderer<B, T>,
limits: &layout::Limits,
) -> layout::Node {
let limits = limits.width(self.width).height(self.height);
let size = limits.resolve(Size::ZERO);
layout::Node::new(size)
}
fn on_event(
&mut self,
tree: &mut Tree,
event: iced_native::Event,
layout: Layout<'_>,
cursor_position: Point,
_renderer: &Renderer<B, T>,
_clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
) -> event::Status {
let bounds = layout.bounds();
let canvas_event = match event {
iced_native::Event::Mouse(mouse_event) => {
Some(Event::Mouse(mouse_event))
}
iced_native::Event::Touch(touch_event) => {
Some(Event::Touch(touch_event))
}
iced_native::Event::Keyboard(keyboard_event) => {
Some(Event::Keyboard(keyboard_event))
}
_ => None,
};
let cursor = Cursor::from_window_position(cursor_position);
if let Some(canvas_event) = canvas_event {
let state = tree.state.downcast_mut::<P::State>();
let (event_status, message) =
self.program.update(state, canvas_event, bounds, cursor);
if let Some(message) = message {
shell.publish(message);
}
return event_status;
}
event::Status::Ignored
}
fn mouse_interaction(
&self,
tree: &Tree,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
_renderer: &Renderer<B, T>,
) -> mouse::Interaction {
let bounds = layout.bounds();
let cursor = Cursor::from_window_position(cursor_position);
let state = tree.state.downcast_ref::<P::State>();
self.program.mouse_interaction(state, bounds, cursor)
}
fn draw(
&self,
tree: &Tree,
renderer: &mut Renderer<B, T>,
theme: &T,
_style: &renderer::Style,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
) {
use iced_native::Renderer as _;
let bounds = layout.bounds();
if bounds.width < 1.0 || bounds.height < 1.0 {
return;
}
let translation = Vector::new(bounds.x, bounds.y);
let cursor = Cursor::from_window_position(cursor_position);
let state = tree.state.downcast_ref::<P::State>();
renderer.with_translation(translation, |renderer| {
renderer.draw_primitive(Primitive::Group {
primitives: self
.program
.draw(state, theme, bounds, cursor)
.into_iter()
.map(Geometry::into_primitive)
.collect(),
});
});
}
}
impl<'a, Message, P, B, T> From<Canvas<Message, T, P>>
for Element<'a, Message, Renderer<B, T>>
where
Message: 'a,
P: Program<Message, T> + 'a,
B: Backend,
T: 'a,
{
fn from(
canvas: Canvas<Message, T, P>,
) -> Element<'a, Message, Renderer<B, T>> {
Element::new(canvas)
}
}

View file

@ -1,100 +0,0 @@
use crate::widget::canvas::{Frame, Geometry};
use crate::Primitive;
use iced_native::Size;
use std::{cell::RefCell, sync::Arc};
enum State {
Empty,
Filled {
bounds: Size,
primitive: Arc<Primitive>,
},
}
impl Default for State {
fn default() -> Self {
State::Empty
}
}
/// A simple cache that stores generated [`Geometry`] to avoid recomputation.
///
/// A [`Cache`] will not redraw its geometry unless the dimensions of its layer
/// change or it is explicitly cleared.
#[derive(Debug, Default)]
pub struct Cache {
state: RefCell<State>,
}
impl Cache {
/// Creates a new empty [`Cache`].
pub fn new() -> Self {
Cache {
state: Default::default(),
}
}
/// Clears the [`Cache`], forcing a redraw the next time it is used.
pub fn clear(&self) {
*self.state.borrow_mut() = State::Empty;
}
/// Draws [`Geometry`] using the provided closure and stores it in the
/// [`Cache`].
///
/// The closure will only be called when
/// - the bounds have changed since the previous draw call.
/// - the [`Cache`] is empty or has been explicitly cleared.
///
/// Otherwise, the previously stored [`Geometry`] will be returned. The
/// [`Cache`] is not cleared in this case. In other words, it will keep
/// returning the stored [`Geometry`] if needed.
pub fn draw(
&self,
bounds: Size,
draw_fn: impl FnOnce(&mut Frame),
) -> Geometry {
use std::ops::Deref;
if let State::Filled {
bounds: cached_bounds,
primitive,
} = self.state.borrow().deref()
{
if *cached_bounds == bounds {
return Geometry::from_primitive(Primitive::Cached {
cache: primitive.clone(),
});
}
}
let mut frame = Frame::new(bounds);
draw_fn(&mut frame);
let primitive = {
let geometry = frame.into_geometry();
Arc::new(geometry.into_primitive())
};
*self.state.borrow_mut() = State::Filled {
bounds,
primitive: primitive.clone(),
};
Geometry::from_primitive(Primitive::Cached { cache: primitive })
}
}
impl std::fmt::Debug for State {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
State::Empty => write!(f, "Empty"),
State::Filled { primitive, bounds } => f
.debug_struct("Filled")
.field("primitive", primitive)
.field("bounds", bounds)
.finish(),
}
}
}

View file

@ -1,64 +0,0 @@
use iced_native::{Point, Rectangle};
/// The mouse cursor state.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Cursor {
/// The cursor has a defined position.
Available(Point),
/// The cursor is currently unavailable (i.e. out of bounds or busy).
Unavailable,
}
impl Cursor {
// TODO: Remove this once this type is used in `iced_native` to encode
// proper cursor availability
pub(crate) fn from_window_position(position: Point) -> Self {
if position.x < 0.0 || position.y < 0.0 {
Cursor::Unavailable
} else {
Cursor::Available(position)
}
}
/// Returns the absolute position of the [`Cursor`], if available.
pub fn position(&self) -> Option<Point> {
match self {
Cursor::Available(position) => Some(*position),
Cursor::Unavailable => None,
}
}
/// Returns the relative position of the [`Cursor`] inside the given bounds,
/// if available.
///
/// If the [`Cursor`] is not over the provided bounds, this method will
/// return `None`.
pub fn position_in(&self, bounds: &Rectangle) -> Option<Point> {
if self.is_over(bounds) {
self.position_from(bounds.position())
} else {
None
}
}
/// Returns the relative position of the [`Cursor`] from the given origin,
/// if available.
pub fn position_from(&self, origin: Point) -> Option<Point> {
match self {
Cursor::Available(position) => {
Some(Point::new(position.x - origin.x, position.y - origin.y))
}
Cursor::Unavailable => None,
}
}
/// Returns whether the [`Cursor`] is currently over the provided bounds
/// or not.
pub fn is_over(&self, bounds: &Rectangle) -> bool {
match self {
Cursor::Available(position) => bounds.contains(*position),
Cursor::Unavailable => false,
}
}
}

View file

@ -1,21 +0,0 @@
//! Handle events of a canvas.
use iced_native::keyboard;
use iced_native::mouse;
use iced_native::touch;
pub use iced_native::event::Status;
/// A [`Canvas`] event.
///
/// [`Canvas`]: crate::widget::Canvas
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Event {
/// A mouse event.
Mouse(mouse::Event),
/// A touch event.
Touch(touch::Event),
/// A keyboard event.
Keyboard(keyboard::Event),
}

View file

@ -1,72 +0,0 @@
//! Fill [crate::widget::canvas::Geometry] with a certain style.
use crate::{Color, Gradient};
pub use crate::widget::canvas::Style;
/// The style used to fill geometry.
#[derive(Debug, Clone)]
pub struct Fill {
/// The color or gradient of the fill.
///
/// By default, it is set to [`Style::Solid`] with [`Color::BLACK`].
pub style: Style,
/// The fill rule defines how to determine what is inside and what is
/// outside of a shape.
///
/// See the [SVG specification][1] for more details.
///
/// By default, it is set to `NonZero`.
///
/// [1]: https://www.w3.org/TR/SVG/painting.html#FillRuleProperty
pub rule: FillRule,
}
impl Default for Fill {
fn default() -> Self {
Self {
style: Style::Solid(Color::BLACK),
rule: FillRule::NonZero,
}
}
}
impl From<Color> for Fill {
fn from(color: Color) -> Fill {
Fill {
style: Style::Solid(color),
..Fill::default()
}
}
}
impl From<Gradient> for Fill {
fn from(gradient: Gradient) -> Self {
Fill {
style: Style::Gradient(gradient),
..Default::default()
}
}
}
/// The fill rule defines how to determine what is inside and what is outside of
/// a shape.
///
/// See the [SVG specification][1].
///
/// [1]: https://www.w3.org/TR/SVG/painting.html#FillRuleProperty
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum FillRule {
NonZero,
EvenOdd,
}
impl From<FillRule> for lyon::tessellation::FillRule {
fn from(rule: FillRule) -> lyon::tessellation::FillRule {
match rule {
FillRule::NonZero => lyon::tessellation::FillRule::NonZero,
FillRule::EvenOdd => lyon::tessellation::FillRule::EvenOdd,
}
}
}

View file

@ -1,530 +0,0 @@
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};
use lyon::geom::euclid;
use lyon::tessellation;
use std::borrow::Cow;
/// The frame of a [`Canvas`].
///
/// [`Canvas`]: crate::widget::Canvas
#[allow(missing_debug_implementations)]
pub struct Frame {
size: Size,
buffers: BufferStack,
primitives: Vec<Primitive>,
transforms: Transforms,
fill_tessellator: tessellation::FillTessellator,
stroke_tessellator: tessellation::StrokeTessellator,
}
enum Buffer {
Solid(tessellation::VertexBuffers<triangle::ColoredVertex2D, u32>),
Gradient(
tessellation::VertexBuffers<triangle::Vertex2D, u32>,
Gradient,
),
}
struct BufferStack {
stack: Vec<Buffer>,
}
impl BufferStack {
fn new() -> Self {
Self { stack: Vec::new() }
}
fn get_mut(&mut self, style: &Style) -> &mut Buffer {
match style {
Style::Solid(_) => match self.stack.last() {
Some(Buffer::Solid(_)) => {}
_ => {
self.stack.push(Buffer::Solid(
tessellation::VertexBuffers::new(),
));
}
},
Style::Gradient(gradient) => match self.stack.last() {
Some(Buffer::Gradient(_, last)) if gradient == last => {}
_ => {
self.stack.push(Buffer::Gradient(
tessellation::VertexBuffers::new(),
gradient.clone(),
));
}
},
}
self.stack.last_mut().unwrap()
}
fn get_fill<'a>(
&'a mut self,
style: &Style,
) -> Box<dyn tessellation::FillGeometryBuilder + 'a> {
match (style, self.get_mut(style)) {
(Style::Solid(color), Buffer::Solid(buffer)) => {
Box::new(tessellation::BuffersBuilder::new(
buffer,
TriangleVertex2DBuilder(color.into_linear()),
))
}
(Style::Gradient(_), Buffer::Gradient(buffer, _)) => Box::new(
tessellation::BuffersBuilder::new(buffer, Vertex2DBuilder),
),
_ => unreachable!(),
}
}
fn get_stroke<'a>(
&'a mut self,
style: &Style,
) -> Box<dyn tessellation::StrokeGeometryBuilder + 'a> {
match (style, self.get_mut(style)) {
(Style::Solid(color), Buffer::Solid(buffer)) => {
Box::new(tessellation::BuffersBuilder::new(
buffer,
TriangleVertex2DBuilder(color.into_linear()),
))
}
(Style::Gradient(_), Buffer::Gradient(buffer, _)) => Box::new(
tessellation::BuffersBuilder::new(buffer, Vertex2DBuilder),
),
_ => unreachable!(),
}
}
}
#[derive(Debug)]
struct Transforms {
previous: Vec<Transform>,
current: Transform,
}
#[derive(Debug, Clone, Copy)]
struct Transform {
raw: lyon::math::Transform,
is_identity: bool,
}
impl Transform {
/// Transforms the given [Point] by the transformation matrix.
fn transform_point(&self, point: &mut Point) {
let transformed = self
.raw
.transform_point(euclid::Point2D::new(point.x, point.y));
point.x = transformed.x;
point.y = transformed.y;
}
fn transform_style(&self, style: Style) -> Style {
match style {
Style::Solid(color) => Style::Solid(color),
Style::Gradient(gradient) => {
Style::Gradient(self.transform_gradient(gradient))
}
}
}
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);
gradient
}
}
impl Frame {
/// Creates a new empty [`Frame`] with the given dimensions.
///
/// The default coordinate system of a [`Frame`] has its origin at the
/// top-left corner of its bounds.
pub fn new(size: Size) -> Frame {
Frame {
size,
buffers: BufferStack::new(),
primitives: Vec::new(),
transforms: Transforms {
previous: Vec::new(),
current: Transform {
raw: lyon::math::Transform::identity(),
is_identity: true,
},
},
fill_tessellator: tessellation::FillTessellator::new(),
stroke_tessellator: tessellation::StrokeTessellator::new(),
}
}
/// Returns the width of the [`Frame`].
#[inline]
pub fn width(&self) -> f32 {
self.size.width
}
/// Returns the height of the [`Frame`].
#[inline]
pub fn height(&self) -> f32 {
self.size.height
}
/// Returns the dimensions of the [`Frame`].
#[inline]
pub fn size(&self) -> Size {
self.size
}
/// Returns the coordinate of the center of the [`Frame`].
#[inline]
pub fn center(&self) -> Point {
Point::new(self.size.width / 2.0, self.size.height / 2.0)
}
/// Draws the given [`Path`] on the [`Frame`] by filling it with the
/// provided style.
pub fn fill(&mut self, path: &Path, fill: impl Into<Fill>) {
let Fill { style, rule } = fill.into();
let mut buffer = self
.buffers
.get_fill(&self.transforms.current.transform_style(style));
let options =
tessellation::FillOptions::default().with_fill_rule(rule.into());
if self.transforms.current.is_identity {
self.fill_tessellator.tessellate_path(
path.raw(),
&options,
buffer.as_mut(),
)
} else {
let path = path.transformed(&self.transforms.current.raw);
self.fill_tessellator.tessellate_path(
path.raw(),
&options,
buffer.as_mut(),
)
}
.expect("Tessellate path.");
}
/// 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<Fill>,
) {
let Fill { style, rule } = fill.into();
let mut buffer = self
.buffers
.get_fill(&self.transforms.current.transform_style(style));
let top_left =
self.transforms.current.raw.transform_point(
lyon::math::Point::new(top_left.x, top_left.y),
);
let size =
self.transforms.current.raw.transform_vector(
lyon::math::Vector::new(size.width, size.height),
);
let options =
tessellation::FillOptions::default().with_fill_rule(rule.into());
self.fill_tessellator
.tessellate_rectangle(
&lyon::math::Box2D::new(top_left, top_left + size),
&options,
buffer.as_mut(),
)
.expect("Fill rectangle");
}
/// 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<Stroke<'a>>) {
let stroke = stroke.into();
let mut buffer = self
.buffers
.get_stroke(&self.transforms.current.transform_style(stroke.style));
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();
let path = if stroke.line_dash.segments.is_empty() {
Cow::Borrowed(path)
} else {
Cow::Owned(path::dashed(path, stroke.line_dash))
};
if self.transforms.current.is_identity {
self.stroke_tessellator.tessellate_path(
path.raw(),
&options,
buffer.as_mut(),
)
} else {
let path = path.transformed(&self.transforms.current.raw);
self.stroke_tessellator.tessellate_path(
path.raw(),
&options,
buffer.as_mut(),
)
}
.expect("Stroke path");
}
/// 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<Text>) {
let text = text.into();
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: position.x,
y: 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
/// 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)) {
self.transforms.previous.push(self.transforms.current);
f(self);
self.transforms.current = self.transforms.previous.pop().unwrap();
}
/// 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 = Frame::new(region.size());
f(&mut frame);
let primitives = frame.into_primitives();
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 {
translation,
content: Box::new(Primitive::Group { primitives: meshes }),
},
Primitive::Translate {
translation,
content: Box::new(Primitive::Clip {
bounds: Rectangle::with_size(region.size()),
content: Box::new(Primitive::Group {
primitives: text,
}),
}),
},
],
});
}
/// Applies a translation to the current transform of the [`Frame`].
#[inline]
pub fn translate(&mut self, translation: Vector) {
self.transforms.current.raw = self
.transforms
.current
.raw
.pre_translate(lyon::math::Vector::new(
translation.x,
translation.y,
));
self.transforms.current.is_identity = false;
}
/// Applies a rotation in radians to the current transform of the [`Frame`].
#[inline]
pub fn rotate(&mut self, angle: f32) {
self.transforms.current.raw = self
.transforms
.current
.raw
.pre_rotate(lyon::math::Angle::radians(angle));
self.transforms.current.is_identity = false;
}
/// Applies a scaling to the current transform of the [`Frame`].
#[inline]
pub fn scale(&mut self, scale: f32) {
self.transforms.current.raw =
self.transforms.current.raw.pre_scale(scale, scale);
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 {
primitives: self.into_primitives(),
})
}
fn into_primitives(mut self) -> Vec<Primitive> {
for buffer in self.buffers.stack {
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,
})
}
}
Buffer::Gradient(buffer, gradient) => {
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
}
}
struct Vertex2DBuilder;
impl tessellation::FillVertexConstructor<triangle::Vertex2D>
for Vertex2DBuilder
{
fn new_vertex(
&mut self,
vertex: tessellation::FillVertex<'_>,
) -> triangle::Vertex2D {
let position = vertex.position();
triangle::Vertex2D {
position: [position.x, position.y],
}
}
}
impl tessellation::StrokeVertexConstructor<triangle::Vertex2D>
for Vertex2DBuilder
{
fn new_vertex(
&mut self,
vertex: tessellation::StrokeVertex<'_, '_>,
) -> triangle::Vertex2D {
let position = vertex.position();
triangle::Vertex2D {
position: [position.x, position.y],
}
}
}
struct TriangleVertex2DBuilder([f32; 4]);
impl tessellation::FillVertexConstructor<triangle::ColoredVertex2D>
for TriangleVertex2DBuilder
{
fn new_vertex(
&mut self,
vertex: tessellation::FillVertex<'_>,
) -> triangle::ColoredVertex2D {
let position = vertex.position();
triangle::ColoredVertex2D {
position: [position.x, position.y],
color: self.0,
}
}
}
impl tessellation::StrokeVertexConstructor<triangle::ColoredVertex2D>
for TriangleVertex2DBuilder
{
fn new_vertex(
&mut self,
vertex: tessellation::StrokeVertex<'_, '_>,
) -> triangle::ColoredVertex2D {
let position = vertex.position();
triangle::ColoredVertex2D {
position: [position.x, position.y],
color: self.0,
}
}
}

View file

@ -1,24 +0,0 @@
use crate::Primitive;
/// A bunch of shapes that can be drawn.
///
/// [`Geometry`] can be easily generated with a [`Frame`] or stored in a
/// [`Cache`].
///
/// [`Frame`]: crate::widget::canvas::Frame
/// [`Cache`]: crate::widget::canvas::Cache
#[derive(Debug, Clone)]
pub struct Geometry(Primitive);
impl Geometry {
pub(crate) fn from_primitive(primitive: Primitive) -> Self {
Self(primitive)
}
/// Turns the [`Geometry`] into a [`Primitive`].
///
/// This can be useful if you are building a custom widget.
pub fn into_primitive(self) -> Primitive {
self.0
}
}

View file

@ -1,109 +0,0 @@
//! Build different kinds of 2D shapes.
pub mod arc;
mod builder;
#[doc(no_inline)]
pub use arc::Arc;
pub use builder::Builder;
use crate::widget::canvas::LineDash;
use iced_native::{Point, Size};
use lyon::algorithms::walk::{walk_along_path, RepeatedPattern, WalkerEvent};
use lyon::path::iterator::PathIterator;
/// An immutable set of points that may or may not be connected.
///
/// A single [`Path`] can represent different kinds of 2D shapes!
#[derive(Debug, Clone)]
pub struct Path {
raw: lyon::path::Path,
}
impl Path {
/// Creates a new [`Path`] with the provided closure.
///
/// Use the [`Builder`] to configure your [`Path`].
pub fn new(f: impl FnOnce(&mut Builder)) -> Self {
let mut builder = Builder::new();
// TODO: Make it pure instead of side-effect-based (?)
f(&mut builder);
builder.build()
}
/// Creates a new [`Path`] representing a line segment given its starting
/// and end points.
pub fn line(from: Point, to: Point) -> Self {
Self::new(|p| {
p.move_to(from);
p.line_to(to);
})
}
/// Creates a new [`Path`] representing a rectangle given its top-left
/// corner coordinate and its `Size`.
pub fn rectangle(top_left: Point, size: Size) -> Self {
Self::new(|p| p.rectangle(top_left, size))
}
/// Creates a new [`Path`] representing a circle given its center
/// coordinate and its radius.
pub fn circle(center: Point, radius: f32) -> Self {
Self::new(|p| p.circle(center, radius))
}
#[inline]
pub(crate) fn raw(&self) -> &lyon::path::Path {
&self.raw
}
#[inline]
pub(crate) fn transformed(
&self,
transform: &lyon::math::Transform,
) -> Path {
Path {
raw: self.raw.clone().transformed(transform),
}
}
}
pub(super) fn dashed(path: &Path, line_dash: LineDash<'_>) -> Path {
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),
},
);
})
}

View file

@ -1,42 +0,0 @@
//! Build and draw curves.
use iced_native::{Point, Vector};
/// A segment of a differentiable curve.
#[derive(Debug, Clone, Copy)]
pub struct Arc {
/// The center of the arc.
pub center: Point,
/// The radius of the arc.
pub radius: f32,
/// The start of the segment's angle, clockwise rotation.
pub start_angle: f32,
/// The end of the segment's angle, clockwise rotation.
pub end_angle: f32,
}
/// An elliptical [`Arc`].
#[derive(Debug, Clone, Copy)]
pub struct Elliptical {
/// The center of the arc.
pub center: Point,
/// The radii of the arc's ellipse, defining its axes.
pub radii: Vector,
/// The rotation of the arc's ellipse.
pub rotation: f32,
/// The start of the segment's angle, clockwise rotation.
pub start_angle: f32,
/// The end of the segment's angle, clockwise rotation.
pub end_angle: f32,
}
impl From<Arc> for Elliptical {
fn from(arc: Arc) -> Elliptical {
Elliptical {
center: arc.center,
radii: Vector::new(arc.radius, arc.radius),
rotation: 0.0,
start_angle: arc.start_angle,
end_angle: arc.end_angle,
}
}
}

View file

@ -1,198 +0,0 @@
use crate::widget::canvas::path::{arc, Arc, Path};
use iced_native::{Point, Size};
use lyon::path::builder::SvgPathBuilder;
/// A [`Path`] builder.
///
/// Once a [`Path`] is built, it can no longer be mutated.
#[allow(missing_debug_implementations)]
pub struct Builder {
raw: lyon::path::builder::WithSvg<lyon::path::path::BuilderImpl>,
}
impl Builder {
/// Creates a new [`Builder`].
pub fn new() -> Builder {
Builder {
raw: lyon::path::Path::builder().with_svg(),
}
}
/// Moves the starting point of a new sub-path to the given `Point`.
#[inline]
pub fn move_to(&mut self, point: Point) {
let _ = self.raw.move_to(lyon::math::Point::new(point.x, point.y));
}
/// Connects the last point in the [`Path`] to the given `Point` with a
/// straight line.
#[inline]
pub fn line_to(&mut self, point: Point) {
let _ = self.raw.line_to(lyon::math::Point::new(point.x, point.y));
}
/// Adds an [`Arc`] to the [`Path`] from `start_angle` to `end_angle` in
/// a clockwise direction.
#[inline]
pub fn arc(&mut self, arc: Arc) {
self.ellipse(arc.into());
}
/// Adds a circular arc to the [`Path`] with the given control points and
/// radius.
///
/// This essentially draws a straight line segment from the current
/// position to `a`, but fits a circular arc of `radius` tangent to that
/// segment and tangent to the line between `a` and `b`.
///
/// With another `.line_to(b)`, the result will be a path connecting the
/// starting point and `b` with straight line segments towards `a` and a
/// circular arc smoothing out the corner at `a`.
///
/// See [the HTML5 specification of `arcTo`](https://html.spec.whatwg.org/multipage/canvas.html#building-paths:dom-context-2d-arcto)
/// for more details and examples.
pub fn arc_to(&mut self, a: Point, b: Point, radius: f32) {
use lyon::{math, path};
let start = self.raw.current_position();
let mid = math::Point::new(a.x, a.y);
let end = math::Point::new(b.x, b.y);
if start == mid || mid == end || radius == 0.0 {
let _ = self.raw.line_to(mid);
return;
}
let double_area = start.x * (mid.y - end.y)
+ mid.x * (end.y - start.y)
+ end.x * (start.y - mid.y);
if double_area == 0.0 {
let _ = self.raw.line_to(mid);
return;
}
let to_start = (start - mid).normalize();
let to_end = (end - mid).normalize();
let inner_angle = to_start.dot(to_end).acos();
let origin_angle = inner_angle / 2.0;
let origin_adjacent = radius / origin_angle.tan();
let arc_start = mid + to_start * origin_adjacent;
let arc_end = mid + to_end * origin_adjacent;
let sweep = to_start.cross(to_end) < 0.0;
let _ = self.raw.line_to(arc_start);
self.raw.arc_to(
math::Vector::new(radius, radius),
math::Angle::radians(0.0),
path::ArcFlags {
large_arc: false,
sweep,
},
arc_end,
);
}
/// Adds an ellipse to the [`Path`] using a clockwise direction.
pub fn ellipse(&mut self, arc: arc::Elliptical) {
use lyon::{geom, math};
let arc = geom::Arc {
center: math::Point::new(arc.center.x, arc.center.y),
radii: math::Vector::new(arc.radii.x, arc.radii.y),
x_rotation: math::Angle::radians(arc.rotation),
start_angle: math::Angle::radians(arc.start_angle),
sweep_angle: math::Angle::radians(arc.end_angle - arc.start_angle),
};
let _ = self.raw.move_to(arc.sample(0.0));
arc.for_each_quadratic_bezier(&mut |curve| {
let _ = self.raw.quadratic_bezier_to(curve.ctrl, curve.to);
});
}
/// Adds a cubic Bézier curve to the [`Path`] given its two control points
/// and its end point.
#[inline]
pub fn bezier_curve_to(
&mut self,
control_a: Point,
control_b: Point,
to: Point,
) {
use lyon::math;
let _ = self.raw.cubic_bezier_to(
math::Point::new(control_a.x, control_a.y),
math::Point::new(control_b.x, control_b.y),
math::Point::new(to.x, to.y),
);
}
/// Adds a quadratic Bézier curve to the [`Path`] given its control point
/// and its end point.
#[inline]
pub fn quadratic_curve_to(&mut self, control: Point, to: Point) {
use lyon::math;
let _ = self.raw.quadratic_bezier_to(
math::Point::new(control.x, control.y),
math::Point::new(to.x, to.y),
);
}
/// Adds a rectangle to the [`Path`] given its top-left corner coordinate
/// and its `Size`.
#[inline]
pub fn rectangle(&mut self, top_left: Point, size: Size) {
self.move_to(top_left);
self.line_to(Point::new(top_left.x + size.width, top_left.y));
self.line_to(Point::new(
top_left.x + size.width,
top_left.y + size.height,
));
self.line_to(Point::new(top_left.x, top_left.y + size.height));
self.close();
}
/// Adds a circle to the [`Path`] given its center coordinate and its
/// radius.
#[inline]
pub fn circle(&mut self, center: Point, radius: f32) {
self.arc(Arc {
center,
radius,
start_angle: 0.0,
end_angle: 2.0 * std::f32::consts::PI,
});
}
/// Closes the current sub-path in the [`Path`] with a straight line to
/// the starting point.
#[inline]
pub fn close(&mut self) {
self.raw.close()
}
/// Builds the [`Path`] of this [`Builder`].
#[inline]
pub fn build(self) -> Path {
Path {
raw: self.raw.build(),
}
}
}
impl Default for Builder {
fn default() -> Self {
Self::new()
}
}

View file

@ -1,102 +0,0 @@
use crate::widget::canvas::event::{self, Event};
use crate::widget::canvas::mouse;
use crate::widget::canvas::{Cursor, Geometry};
use crate::Rectangle;
/// The state and logic of a [`Canvas`].
///
/// A [`Program`] can mutate internal state and produce messages for an
/// application.
///
/// [`Canvas`]: crate::widget::Canvas
pub trait Program<Message, Theme = iced_native::Theme> {
/// The internal state mutated by the [`Program`].
type State: Default + 'static;
/// Updates the [`State`](Self::State) of the [`Program`].
///
/// When a [`Program`] is used in a [`Canvas`], the runtime will call this
/// method for each [`Event`].
///
/// This method can optionally return a `Message` to notify an application
/// of any meaningful interactions.
///
/// By default, this method does and returns nothing.
///
/// [`Canvas`]: crate::widget::Canvas
fn update(
&self,
_state: &mut Self::State,
_event: Event,
_bounds: Rectangle,
_cursor: Cursor,
) -> (event::Status, Option<Message>) {
(event::Status::Ignored, None)
}
/// Draws the state of the [`Program`], producing a bunch of [`Geometry`].
///
/// [`Geometry`] can be easily generated with a [`Frame`] or stored in a
/// [`Cache`].
///
/// [`Frame`]: crate::widget::canvas::Frame
/// [`Cache`]: crate::widget::canvas::Cache
fn draw(
&self,
state: &Self::State,
theme: &Theme,
bounds: Rectangle,
cursor: Cursor,
) -> Vec<Geometry>;
/// Returns the current mouse interaction of the [`Program`].
///
/// The interaction returned will be in effect even if the cursor position
/// is out of bounds of the program's [`Canvas`].
///
/// [`Canvas`]: crate::widget::Canvas
fn mouse_interaction(
&self,
_state: &Self::State,
_bounds: Rectangle,
_cursor: Cursor,
) -> mouse::Interaction {
mouse::Interaction::default()
}
}
impl<Message, Theme, T> Program<Message, Theme> for &T
where
T: Program<Message, Theme>,
{
type State = T::State;
fn update(
&self,
state: &mut Self::State,
event: Event,
bounds: Rectangle,
cursor: Cursor,
) -> (event::Status, Option<Message>) {
T::update(self, state, event, bounds, cursor)
}
fn draw(
&self,
state: &Self::State,
theme: &Theme,
bounds: Rectangle,
cursor: Cursor,
) -> Vec<Geometry> {
T::draw(self, state, theme, bounds, cursor)
}
fn mouse_interaction(
&self,
state: &Self::State,
bounds: Rectangle,
cursor: Cursor,
) -> mouse::Interaction {
T::mouse_interaction(self, state, bounds, cursor)
}
}

View file

@ -1,126 +0,0 @@
//! Create lines from a [crate::widget::canvas::Path] and assigns them various attributes/styles.
pub use crate::widget::canvas::Style;
use iced_native::Color;
/// The style of a stroke.
#[derive(Debug, Clone)]
pub struct Stroke<'a> {
/// The color or gradient of the stroke.
///
/// By default, it is set to a [`Style::Solid`] with [`Color::BLACK`].
pub style: Style,
/// The distance between the two edges of the stroke.
pub width: f32,
/// The shape to be used at the end of open subpaths when they are stroked.
pub line_cap: LineCap,
/// The shape to be used at the corners of paths or basic shapes when they
/// are stroked.
pub line_join: LineJoin,
/// The dash pattern used when stroking the line.
pub line_dash: LineDash<'a>,
}
impl<'a> Stroke<'a> {
/// Sets the color of the [`Stroke`].
pub fn with_color(self, color: Color) -> Self {
Stroke {
style: Style::Solid(color),
..self
}
}
/// Sets the width of the [`Stroke`].
pub fn with_width(self, width: f32) -> Self {
Stroke { width, ..self }
}
/// Sets the [`LineCap`] of the [`Stroke`].
pub fn with_line_cap(self, line_cap: LineCap) -> Self {
Stroke { line_cap, ..self }
}
/// Sets the [`LineJoin`] of the [`Stroke`].
pub fn with_line_join(self, line_join: LineJoin) -> Self {
Stroke { line_join, ..self }
}
}
impl<'a> Default for Stroke<'a> {
fn default() -> Self {
Stroke {
style: Style::Solid(Color::BLACK),
width: 1.0,
line_cap: LineCap::default(),
line_join: LineJoin::default(),
line_dash: LineDash::default(),
}
}
}
/// The shape used at the end of open subpaths when they are stroked.
#[derive(Debug, Clone, Copy)]
pub enum LineCap {
/// The stroke for each sub-path does not extend beyond its two endpoints.
Butt,
/// At the end of each sub-path, the shape representing the stroke will be
/// extended by a square.
Square,
/// At the end of each sub-path, the shape representing the stroke will be
/// extended by a semicircle.
Round,
}
impl Default for LineCap {
fn default() -> LineCap {
LineCap::Butt
}
}
impl From<LineCap> for lyon::tessellation::LineCap {
fn from(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,
}
}
}
/// The shape used at the corners of paths or basic shapes when they are
/// stroked.
#[derive(Debug, Clone, Copy)]
pub enum LineJoin {
/// A sharp corner.
Miter,
/// A round corner.
Round,
/// A bevelled corner.
Bevel,
}
impl Default for LineJoin {
fn default() -> LineJoin {
LineJoin::Miter
}
}
impl From<LineJoin> for lyon::tessellation::LineJoin {
fn from(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,
}
}
}
/// The dash pattern used when stroking the line.
#[derive(Debug, Clone, Copy, Default)]
pub struct LineDash<'a> {
/// The alternating lengths of lines and gaps which describe the pattern.
pub segments: &'a [f32],
/// The offset of [`LineDash::segments`] to start the pattern.
pub offset: usize,
}

View file

@ -1,23 +0,0 @@
use crate::{Color, Gradient};
/// The coloring style of some drawing.
#[derive(Debug, Clone, PartialEq)]
pub enum Style {
/// A solid [`Color`].
Solid(Color),
/// A [`Gradient`] color.
Gradient(Gradient),
}
impl From<Color> for Style {
fn from(color: Color) -> Self {
Self::Solid(color)
}
}
impl From<Gradient> for Style {
fn from(gradient: Gradient) -> Self {
Self::Gradient(gradient)
}
}

View file

@ -1,57 +0,0 @@
use crate::alignment;
use crate::{Color, Font, Point};
/// 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 position of the text relative to the alignment properties.
/// By default, this position will be relative to the top-left corner coordinate meaning that
/// if the horizontal and vertical alignments are unchanged, this property will tell where the
/// top-left corner of the text should be placed.
/// By changing the horizontal_alignment and vertical_alignment properties, you are are able to
/// change what part of text is placed at this positions.
/// For example, when the horizontal_alignment and vertical_alignment are set to Center, the
/// center of the text will be placed at the given position NOT the top-left coordinate.
pub position: Point,
/// 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: alignment::Horizontal,
/// The vertical alignment of the text
pub vertical_alignment: alignment::Vertical,
}
impl Default for Text {
fn default() -> Text {
Text {
content: String::new(),
position: Point::ORIGIN,
color: Color::BLACK,
size: 16.0,
font: Font::SansSerif,
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
}
}
}
impl From<String> for Text {
fn from(content: String) -> Text {
Text {
content,
..Default::default()
}
}
}
impl From<&str> for Text {
fn from(content: &str) -> Text {
String::from(content).into()
}
}

View file

@ -1,301 +0,0 @@
//! Encode and display information in a QR code.
use crate::renderer::{self, Renderer};
use crate::widget::canvas;
use crate::Backend;
use iced_native::layout;
use iced_native::widget::Tree;
use iced_native::{
Color, Element, Layout, Length, Point, Rectangle, Size, Vector, Widget,
};
use thiserror::Error;
const DEFAULT_CELL_SIZE: u16 = 4;
const QUIET_ZONE: usize = 2;
/// A type of matrix barcode consisting of squares arranged in a grid which
/// can be read by an imaging device, such as a camera.
#[derive(Debug)]
pub struct QRCode<'a> {
state: &'a State,
dark: Color,
light: Color,
cell_size: u16,
}
impl<'a> QRCode<'a> {
/// Creates a new [`QRCode`] with the provided [`State`].
pub fn new(state: &'a State) -> Self {
Self {
cell_size: DEFAULT_CELL_SIZE,
dark: Color::BLACK,
light: Color::WHITE,
state,
}
}
/// Sets both the dark and light [`Color`]s of the [`QRCode`].
pub fn color(mut self, dark: Color, light: Color) -> Self {
self.dark = dark;
self.light = light;
self
}
/// Sets the size of the squares of the grid cell of the [`QRCode`].
pub fn cell_size(mut self, cell_size: u16) -> Self {
self.cell_size = cell_size;
self
}
}
impl<'a, Message, B, T> Widget<Message, Renderer<B, T>> for QRCode<'a>
where
B: Backend,
{
fn width(&self) -> Length {
Length::Shrink
}
fn height(&self) -> Length {
Length::Shrink
}
fn layout(
&self,
_renderer: &Renderer<B, T>,
_limits: &layout::Limits,
) -> layout::Node {
let side_length = (self.state.width + 2 * QUIET_ZONE) as f32
* f32::from(self.cell_size);
layout::Node::new(Size::new(side_length, side_length))
}
fn draw(
&self,
_state: &Tree,
renderer: &mut Renderer<B, T>,
_theme: &T,
_style: &renderer::Style,
layout: Layout<'_>,
_cursor_position: Point,
_viewport: &Rectangle,
) {
use iced_native::Renderer as _;
let bounds = layout.bounds();
let side_length = self.state.width + 2 * QUIET_ZONE;
// Reuse cache if possible
let geometry = self.state.cache.draw(bounds.size(), |frame| {
// Scale units to cell size
frame.scale(f32::from(self.cell_size));
// Draw background
frame.fill_rectangle(
Point::ORIGIN,
Size::new(side_length as f32, side_length as f32),
self.light,
);
// Avoid drawing on the quiet zone
frame.translate(Vector::new(QUIET_ZONE as f32, QUIET_ZONE as f32));
// Draw contents
self.state
.contents
.iter()
.enumerate()
.filter(|(_, value)| **value == qrcode::Color::Dark)
.for_each(|(index, _)| {
let row = index / self.state.width;
let column = index % self.state.width;
frame.fill_rectangle(
Point::new(column as f32, row as f32),
Size::UNIT,
self.dark,
);
});
});
let translation = Vector::new(bounds.x, bounds.y);
renderer.with_translation(translation, |renderer| {
renderer.draw_primitive(geometry.into_primitive());
});
}
}
impl<'a, Message, B, T> From<QRCode<'a>>
for Element<'a, Message, Renderer<B, T>>
where
B: Backend,
{
fn from(qr_code: QRCode<'a>) -> Self {
Self::new(qr_code)
}
}
/// The state of a [`QRCode`].
///
/// It stores the data that will be displayed.
#[derive(Debug)]
pub struct State {
contents: Vec<qrcode::Color>,
width: usize,
cache: canvas::Cache,
}
impl State {
/// Creates a new [`State`] with the provided data.
///
/// This method uses an [`ErrorCorrection::Medium`] and chooses the smallest
/// size to display the data.
pub fn new(data: impl AsRef<[u8]>) -> Result<Self, Error> {
let encoded = qrcode::QrCode::new(data)?;
Ok(Self::build(encoded))
}
/// Creates a new [`State`] with the provided [`ErrorCorrection`].
pub fn with_error_correction(
data: impl AsRef<[u8]>,
error_correction: ErrorCorrection,
) -> Result<Self, Error> {
let encoded = qrcode::QrCode::with_error_correction_level(
data,
error_correction.into(),
)?;
Ok(Self::build(encoded))
}
/// Creates a new [`State`] with the provided [`Version`] and
/// [`ErrorCorrection`].
pub fn with_version(
data: impl AsRef<[u8]>,
version: Version,
error_correction: ErrorCorrection,
) -> Result<Self, Error> {
let encoded = qrcode::QrCode::with_version(
data,
version.into(),
error_correction.into(),
)?;
Ok(Self::build(encoded))
}
fn build(encoded: qrcode::QrCode) -> Self {
let width = encoded.width();
let contents = encoded.into_colors();
Self {
contents,
width,
cache: canvas::Cache::new(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// The size of a [`QRCode`].
///
/// The higher the version the larger the grid of cells, and therefore the more
/// information the [`QRCode`] can carry.
pub enum Version {
/// A normal QR code version. It should be between 1 and 40.
Normal(u8),
/// A micro QR code version. It should be between 1 and 4.
Micro(u8),
}
impl From<Version> for qrcode::Version {
fn from(version: Version) -> Self {
match version {
Version::Normal(v) => qrcode::Version::Normal(i16::from(v)),
Version::Micro(v) => qrcode::Version::Micro(i16::from(v)),
}
}
}
/// The error correction level.
///
/// It controls the amount of data that can be damaged while still being able
/// to recover the original information.
///
/// A higher error correction level allows for more corrupted data.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorCorrection {
/// Low error correction. 7% of the data can be restored.
Low,
/// Medium error correction. 15% of the data can be restored.
Medium,
/// Quartile error correction. 25% of the data can be restored.
Quartile,
/// High error correction. 30% of the data can be restored.
High,
}
impl From<ErrorCorrection> for qrcode::EcLevel {
fn from(ec_level: ErrorCorrection) -> Self {
match ec_level {
ErrorCorrection::Low => qrcode::EcLevel::L,
ErrorCorrection::Medium => qrcode::EcLevel::M,
ErrorCorrection::Quartile => qrcode::EcLevel::Q,
ErrorCorrection::High => qrcode::EcLevel::H,
}
}
}
/// An error that occurred when building a [`State`] for a [`QRCode`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
pub enum Error {
/// The data is too long to encode in a QR code for the chosen [`Version`].
#[error(
"The data is too long to encode in a QR code for the chosen version"
)]
DataTooLong,
/// The chosen [`Version`] and [`ErrorCorrection`] combination is invalid.
#[error(
"The chosen version and error correction level combination is invalid."
)]
InvalidVersion,
/// One or more characters in the provided data are not supported by the
/// chosen [`Version`].
#[error(
"One or more characters in the provided data are not supported by the \
chosen version"
)]
UnsupportedCharacterSet,
/// The chosen ECI designator is invalid. A valid designator should be
/// between 0 and 999999.
#[error(
"The chosen ECI designator is invalid. A valid designator should be \
between 0 and 999999."
)]
InvalidEciDesignator,
/// A character that does not belong to the character set was found.
#[error("A character that does not belong to the character set was found")]
InvalidCharacter,
}
impl From<qrcode::types::QrError> for Error {
fn from(error: qrcode::types::QrError) -> Self {
use qrcode::types::QrError;
match error {
QrError::DataTooLong => Error::DataTooLong,
QrError::InvalidVersion => Error::InvalidVersion,
QrError::UnsupportedCharacterSet => Error::UnsupportedCharacterSet,
QrError::InvalidEciDesignator => Error::InvalidEciDesignator,
QrError::InvalidCharacter => Error::InvalidCharacter,
}
}
}