Introduce a `Path` struct

Antonio Scandurra and Max Brunsfeld created

Co-Authored-By: Max Brunsfeld <max@zed.dev>

Change summary

gpui/src/geometry.rs | 94 +++++++++++++++++++++++++++++++++++++++++++++
gpui/src/lib.rs      |  2 
2 files changed, 95 insertions(+), 1 deletion(-)

Detailed changes

gpui/src/geometry.rs 🔗

@@ -0,0 +1,94 @@
+pub use pathfinder_geometry::*;
+
+use vector::{vec2f, Vector2F};
+
+pub(crate) struct Vertex {
+    xy_position: Vector2F,
+    st_position: Vector2F,
+}
+
+pub struct Path {
+    vertices: Vec<Vertex>,
+    start: Vector2F,
+    current: Vector2F,
+    countours_len: usize,
+}
+
+enum Kind {
+    Solid,
+    Quadratic,
+}
+
+impl Path {
+    fn new() -> Self {
+        Self {
+            vertices: Vec::new(),
+            start: vec2f(0., 0.),
+            current: vec2f(0., 0.),
+            countours_len: 0,
+        }
+    }
+
+    pub fn reset(&mut self, point: Vector2F) {
+        self.vertices.clear();
+        self.start = point;
+        self.current = point;
+        self.countours_len = 0;
+    }
+
+    pub fn line_to(&mut self, point: Vector2F) {
+        self.countours_len += 1;
+        if self.countours_len > 1 {
+            self.push_triangle(self.start, self.current, point, Kind::Solid);
+        }
+
+        self.current = point;
+    }
+
+    pub fn curve_to(&mut self, point: Vector2F, ctrl: Vector2F) {
+        self.countours_len += 1;
+        if self.countours_len > 1 {
+            self.push_triangle(self.start, self.current, point, Kind::Solid);
+        }
+
+        self.push_triangle(self.current, ctrl, point, Kind::Quadratic);
+        self.current = point;
+    }
+
+    pub(crate) fn close(self) -> Vec<Vertex> {
+        self.vertices
+    }
+
+    fn push_triangle(&mut self, a: Vector2F, b: Vector2F, c: Vector2F, kind: Kind) {
+        match kind {
+            Kind::Solid => {
+                self.vertices.push(Vertex {
+                    xy_position: a,
+                    st_position: vec2f(0., 1.),
+                });
+                self.vertices.push(Vertex {
+                    xy_position: b,
+                    st_position: vec2f(0., 1.),
+                });
+                self.vertices.push(Vertex {
+                    xy_position: c,
+                    st_position: vec2f(0., 1.),
+                });
+            }
+            Kind::Quadratic => {
+                self.vertices.push(Vertex {
+                    xy_position: a,
+                    st_position: vec2f(0., 0.),
+                });
+                self.vertices.push(Vertex {
+                    xy_position: b,
+                    st_position: vec2f(0.5, 0.),
+                });
+                self.vertices.push(Vertex {
+                    xy_position: c,
+                    st_position: vec2f(1., 1.),
+                });
+            }
+        }
+    }
+}

gpui/src/lib.rs 🔗

@@ -6,6 +6,7 @@ pub mod elements;
 pub mod font_cache;
 pub use font_cache::FontCache;
 pub mod fonts;
+pub mod geometry;
 mod presenter;
 mod scene;
 pub use scene::{Border, Quad, Scene};
@@ -17,7 +18,6 @@ pub mod executor;
 pub mod keymap;
 pub mod platform;
 pub use pathfinder_color as color;
-pub use pathfinder_geometry as geometry;
 pub use platform::Event;
 pub use presenter::{
     AfterLayoutContext, Axis, EventContext, LayoutContext, PaintContext, SizeConstraint,