summaryrefslogtreecommitdiffstats
path: root/wgpu/src/layer
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2023-05-11 16:45:08 +0200
committerLibravatar GitHub <noreply@github.com>2023-05-11 16:45:08 +0200
commit669f7cc74b2e7918e86a8197916f503f2d3d9b93 (patch)
treeacb365358235be6ce115b50db9404d890b6e77a6 /wgpu/src/layer
parentbc62013b6cde52174bf4c4286939cf170bfa7760 (diff)
parent63d3fc6996b848e10e77e6924bfebdf6ba82852e (diff)
downloadiced-669f7cc74b2e7918e86a8197916f503f2d3d9b93.tar.gz
iced-669f7cc74b2e7918e86a8197916f503f2d3d9b93.tar.bz2
iced-669f7cc74b2e7918e86a8197916f503f2d3d9b93.zip
Merge pull request #1830 from iced-rs/advanced-text
Advanced text
Diffstat (limited to 'wgpu/src/layer')
-rw-r--r--wgpu/src/layer/image.rs27
-rw-r--r--wgpu/src/layer/mesh.rs93
-rw-r--r--wgpu/src/layer/quad.rs30
-rw-r--r--wgpu/src/layer/text.rs34
4 files changed, 184 insertions, 0 deletions
diff --git a/wgpu/src/layer/image.rs b/wgpu/src/layer/image.rs
new file mode 100644
index 00000000..0de589f8
--- /dev/null
+++ b/wgpu/src/layer/image.rs
@@ -0,0 +1,27 @@
+use crate::core::image;
+use crate::core::svg;
+use crate::core::{Color, Rectangle};
+
+/// 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,
+ },
+}
diff --git a/wgpu/src/layer/mesh.rs b/wgpu/src/layer/mesh.rs
new file mode 100644
index 00000000..9dd14391
--- /dev/null
+++ b/wgpu/src/layer/mesh.rs
@@ -0,0 +1,93 @@
+//! A collection of triangle primitives.
+use crate::core::{Gradient, Point, Rectangle};
+use crate::graphics::primitive;
+
+/// 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 primitive::Mesh2D<primitive::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 primitive::Mesh2D<primitive::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
+ })
+}
diff --git a/wgpu/src/layer/quad.rs b/wgpu/src/layer/quad.rs
new file mode 100644
index 00000000..0d8bde9d
--- /dev/null
+++ b/wgpu/src/layer/quad.rs
@@ -0,0 +1,30 @@
+/// 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 {}
diff --git a/wgpu/src/layer/text.rs b/wgpu/src/layer/text.rs
new file mode 100644
index 00000000..ba1bdca8
--- /dev/null
+++ b/wgpu/src/layer/text.rs
@@ -0,0 +1,34 @@
+use crate::core::alignment;
+use crate::core::text;
+use crate::core::{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`] in logical pixels.
+ pub size: f32,
+
+ /// The line height of the [`Text`].
+ pub line_height: text::LineHeight,
+
+ /// 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,
+
+ /// The shaping strategy of the text.
+ pub shaping: text::Shaping,
+}