tawasuyu/llimphi
Sovereign retained-mode GPU UI framework in Rust (wgpu+vello+taffy+parley), Elm loop, 40+ widgets.
1// Copyright 2024 the Velato Authors
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4use kurbo::{PathEl, Point};
5
6/// Helper trait for converting cubic splines to paths.
7pub trait SplineToPath {
8 fn get(&self, index: usize) -> Point;
9 fn len(&self) -> usize;
10
11 fn to_path(&self, is_closed: bool, path: &mut Vec<PathEl>) -> Option<()> {
12 use PathEl::*;
13 if self.len() == 0 {
14 return None;
15 }
16 path.push(MoveTo(self.get(0)));
17 let n_vertices = self.len() / 3;
18 let mut add_element = |from_vertex, to_vertex| {
19 let from_index = 3 * from_vertex;
20 let to_index = 3 * to_vertex;
21 let p0: Point = self.get(from_index);
22 let p1: Point = self.get(to_index);
23 let mut c0: Point = self.get(from_index + 2);
24 c0.x += p0.x;
25 c0.y += p0.y;
26 let mut c1: Point = self.get(to_index + 1);
27 c1.x += p1.x;
28 c1.y += p1.y;
29 if c0 == p0 && c1 == p1 {
30 path.push(LineTo(p1));
31 } else {
32 path.push(CurveTo(c0, c1, p1));
33 }
34 };
35 for i in 1..n_vertices {
36 add_element(i - 1, i);
37 }
38 if is_closed && n_vertices != 0 {
39 add_element(n_vertices - 1, 0);
40 path.push(ClosePath);
41 }
42 Some(())
43 }
44}
45
46/// Converts a static spline to a path.
47impl SplineToPath for &'_ [Point] {
48 fn len(&self) -> usize {
49 self.as_ref().len()
50 }
51
52 fn get(&self, index: usize) -> Point {
53 self[index]
54 }
55}
56
57/// Produces a path by lerping between two sets of points.
58impl SplineToPath for (&'_ [Point], &'_ [Point], f64) {
59 fn len(&self) -> usize {
60 self.0.len().min(self.1.len())
61 }
62
63 fn get(&self, index: usize) -> Point {
64 // TODO: This definitely shouldn't be a lerp
65 self.0[index].lerp(self.1[index], self.2)
66 }
67}