Skip to main content

ferroui_vello/
line_geometry_impl.rs

1use crate::geometry_impl::{impl_geometry_impl, register, FillPath, GeometryImpl, GeometryImplBase, VelloPath};
2use crate::vello_extensions::to_kurbo_point;
3use ferroui_base::media::FillRule;
4use ferroui_base::{Point, Rect};
5use kurbo::BezPath;
6use std::sync::Arc;
7
8/// A kurbo implementation of a line geometry.
9pub struct LineGeometryImpl {
10    base: GeometryImplBase,
11    bounds: Rect,
12    stroke_path: VelloPath,
13}
14
15impl LineGeometryImpl {
16    /// Creates the geometry of the line from `p1` to `p2`.
17    pub fn new(p1: Point, p2: Point) -> Arc<Self> {
18        let mut path = BezPath::new();
19        path.move_to(to_kurbo_point(p1));
20        path.line_to(to_kurbo_point(p2));
21
22        let bounds = Rect::from_points(
23            Point::new(p1.x.min(p2.x), p1.y.min(p2.y)),
24            Point::new(p1.x.max(p2.x), p1.y.max(p2.y)),
25        );
26
27        register(Self {
28            base: GeometryImplBase::new(),
29            bounds,
30            stroke_path: VelloPath::new(path, FillRule::NonZero),
31        })
32    }
33
34    fn geometry_bounds(&self) -> Rect {
35        self.bounds
36    }
37}
38
39impl GeometryImpl for LineGeometryImpl {
40    fn base(&self) -> &GeometryImplBase {
41        &self.base
42    }
43
44    fn stroke_path(&self) -> Option<VelloPath> {
45        Some(self.stroke_path.clone())
46    }
47
48    fn fill(&self) -> FillPath {
49        FillPath::None
50    }
51}
52
53impl_geometry_impl!(LineGeometryImpl);