Skip to main content

euv_chart/geom/tess/
struct.rs

1use super::*;
2
3/// A contour: one flattened sub-path of a [`Path`].
4///
5/// A contour is the unit of both filling and stroking. Filling
6/// triangulates the set of contours together (so that holes are punched
7/// correctly); stroking expands each contour on its own.
8#[derive(Clone, Debug, Default, PartialEq)]
9pub struct Contour {
10    /// The flattened vertices.
11    ///
12    /// A closed contour repeats neither its first nor its last point; the
13    /// closing edge is implicit.
14    pub points: Vec<Point>,
15    /// Whether the contour is explicitly closed.
16    pub closed: bool,
17}
18
19/// A path flattened into contours.
20#[derive(Clone, Debug, Default, PartialEq)]
21pub struct FlatPath {
22    /// The contours, in the order they appear in the source path.
23    pub contours: Vec<Contour>,
24}
25
26/// Turns paths into GPU-ready triangle meshes.
27///
28/// The tessellator owns the scratch buffers it needs, so filling a chart
29/// with many series does not allocate once per shape: the buffers are
30/// reused across calls and only grow when a larger shape shows up.
31///
32/// A tessellator is not thread-safe; each renderer owns one.
33#[derive(Clone, Debug, lombok_macros::Data)]
34pub struct Tessellator {
35    /// The tolerance used when flattening curves.
36    #[get(pub(crate), type(copy))]
37    #[get_mut(pub(crate))]
38    #[set(pub(crate))]
39    pub(crate) tolerance: f64,
40    /// Scratch storage for the flattened path.
41    #[get(pub(crate))]
42    #[get_mut(pub(crate))]
43    #[set(pub(crate))]
44    pub(crate) flat: FlatPath,
45    /// Scratch storage for the working polygon during triangulation.
46    #[get(pub(crate))]
47    #[get_mut(pub(crate))]
48    #[set(pub(crate))]
49    pub(crate) working: Vec<Point>,
50    /// Scratch storage for the output triangle indices.
51    #[get(pub(crate))]
52    #[get_mut(pub(crate))]
53    #[set(pub(crate))]
54    pub(crate) indices: Vec<usize>,
55}
56
57/// A vertex of a solid-colour mesh.
58#[derive(Clone, Copy, Debug, Default, PartialEq)]
59pub struct SolidVertex {
60    /// The horizontal position, in canvas pixels.
61    pub x: f32,
62    /// The vertical position, in canvas pixels.
63    pub y: f32,
64    /// The packed colour, as `0xRRGGBBAA`.
65    pub color: u32,
66}
67
68/// A solid-colour triangle mesh.
69///
70/// The mesh is uploaded to the GPU verbatim: the vertex buffer is the
71/// `vertices` array reinterpreted as `SolidVertex`, and the index buffer
72/// is the `indices` array. Keeping the two in a single struct lets the
73/// backends share one upload path and one pipeline layout.
74#[derive(Clone, Debug, Default, lombok_macros::Data, PartialEq)]
75pub struct SolidMesh {
76    /// The vertices.
77    pub vertices: Vec<SolidVertex>,
78    /// The triangle indices.
79    pub indices: Vec<u32>,
80}
81
82/// A textured vertex, used for glyph-atlas text.
83#[derive(Clone, Copy, Debug, Default, PartialEq)]
84pub struct TextVertex {
85    /// The horizontal position of the glyph quad, in canvas pixels.
86    pub x: f32,
87    /// The vertical position of the glyph quad, in canvas pixels.
88    pub y: f32,
89    /// The horizontal texture coordinate of the glyph's top-left corner.
90    pub u: f32,
91    /// The vertical texture coordinate of the glyph's top-left corner.
92    pub v: f32,
93    /// The packed colour, as `0xRRGGBBAA`.
94    pub color: u32,
95}
96
97/// A textured triangle mesh.
98#[derive(Clone, Debug, Default, lombok_macros::Data, PartialEq)]
99pub struct TextMesh {
100    /// The vertices.
101    pub vertices: Vec<TextVertex>,
102    /// The triangle indices.
103    pub indices: Vec<u32>,
104}