1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//! GPU [`Output`] for `Views`
use lyon::path::builder::{NoAttributes, Transformed};
use lyon::path::{BuilderImpl as Builder, Path};
use lyon::tessellation::{
    FillGeometryBuilder, FillOptions, FillTessellator, FillVertex, GeometryBuilder,
    GeometryBuilderError, VertexId,
};

use crate::views::Transform;

///  
pub struct Output {
    storage: Storage,
    options: FillOptions,
    builder: NoAttributes<Transformed<Builder, Transform>>,
}

impl Output {
    /// Creates an indexed-triangle data `Output`.
    pub fn new(rounding: f32) -> Self {
        let builder = Self::builder();
        let storage = Storage::default();

        let options = FillOptions::non_zero().with_tolerance(if rounding > 0.0 {
            rounding
        } else {
            FillOptions::DEFAULT_TOLERANCE
        });

        Self {
            storage,
            options,
            builder,
        }
    }

    /// Consumes the `Output` and returns the constructed indexed-triangle data.
    /// - vertices are stored as (x, y, rgba) tuples
    /// - indices are stored as 32-bit offsets
    ///
    /// ## Example
    /// A WGSL shader that consumes this `Output`’s output:
    ///
    /// ```wgsl
    #[doc = include_str!("../../../examples/winit/wgpu/shader.wgsl")]
    /// ```
    #[allow(clippy::type_complexity)]
    pub fn into_inner(mut self) -> (Vec<(i16, i16, [u8; 4])>, Vec<u32>) {
        self.tessellate();
        self.storage.into_inner()
    }

    #[inline(never)]
    fn tessellate(&mut self) {
        let builder = std::mem::replace(&mut self.builder, Self::builder());

        let path = builder.build();
        let mut tessellator = FillTessellator::default();
        tessellator
            .tessellate_path(&path, &self.options, &mut self.storage)
            .expect("tessellate_path");
    }

    fn builder() -> NoAttributes<Transformed<Builder, Transform>> {
        Path::builder().transformed(Default::default())
    }
}

impl super::Output for Output {
    #[inline]
    fn begin(&mut self, x: f32, y: f32, rgba: [u8; 4], transform: &Transform) {
        if rgba != self.storage.rgba {
            self.tessellate();
        }

        self.storage.rgba = rgba;
        self.builder.inner_mut().set_transform(*transform);

        self.builder.begin((x, y).into());
    }

    #[inline]
    fn line_to(&mut self, x: f32, y: f32) {
        self.builder.line_to((x, y).into());
    }

    #[inline]
    fn quadratic_bezier_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
        self.builder
            .quadratic_bezier_to((x1, y1).into(), (x, y).into());
    }

    #[inline]
    fn cubic_bezier_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
        self.builder
            .cubic_bezier_to((x1, y1).into(), (x2, y2).into(), (x, y).into());
    }

    #[inline]
    fn close(&mut self) {
        self.builder.close();
    }
}

///
#[derive(Default)]
struct Storage {
    vertices: Vec<(i16, i16, [u8; 4])>,
    indices: Vec<u32>,
    rgba: [u8; 4],
}

impl Storage {
    #[allow(clippy::type_complexity)]
    pub fn into_inner(self) -> (Vec<(i16, i16, [u8; 4])>, Vec<u32>) {
        eprintln!("{} vertices", self.vertices.len());
        (self.vertices, self.indices)
    }
}

#[doc(hidden)]
impl FillGeometryBuilder for Storage {
    #[inline]
    fn add_fill_vertex(&mut self, vertex: FillVertex) -> Result<VertexId, GeometryBuilderError> {
        let id = self.vertices.len() as u32;
        let (x, y) = vertex.position().into();

        self.vertices.push((x as i16, y as i16, self.rgba));
        Ok(id.into())
    }
}

#[doc(hidden)]
impl GeometryBuilder for Storage {
    #[inline]
    fn add_triangle(&mut self, a: VertexId, b: VertexId, c: VertexId) {
        let triangle: [u32; 3] = [a, b, c].map(|id| id.into());
        self.indices.extend_from_slice(&triangle);
    }
}