skia_safe/
pathops.rs

1//! Wrapper for pathops/SkPathOps.h
2use crate::{prelude::*, Path, Rect};
3use skia_bindings::{self as sb, SkOpBuilder};
4use std::fmt;
5
6pub type PathOp = skia_bindings::SkPathOp;
7variant_name!(PathOp::XOR);
8
9// TODO: I am not so sure if we should export these global functions.
10
11pub fn op(one: &Path, two: &Path, op: PathOp) -> Option<Path> {
12    Path::try_construct(|p| unsafe { sb::C_SkPathOps_Op(one.native(), two.native(), op, p) })
13}
14
15pub fn simplify(path: &Path) -> Option<Path> {
16    Path::try_construct(|p| unsafe { sb::C_SkPathOps_Simplify(path.native(), p) })
17}
18
19#[deprecated(
20    since = "0.83.0",
21    note = "Use Path::compute_tight_bounds() and test if the resulting Rect::is_finite()"
22)]
23pub fn tight_bounds(path: &Path) -> Option<Rect> {
24    let rect = path.compute_tight_bounds();
25    rect.is_finite().then_some(rect)
26}
27
28pub fn as_winding(path: &Path) -> Option<Path> {
29    Path::try_construct(|p| unsafe { sb::C_SkPathOps_AsWinding(path.native(), p) })
30}
31
32pub type OpBuilder = Handle<SkOpBuilder>;
33unsafe_send_sync!(OpBuilder);
34
35impl NativeDrop for SkOpBuilder {
36    fn drop(&mut self) {
37        unsafe { sb::C_SkOpBuilder_destruct(self) }
38    }
39}
40
41impl Default for Handle<SkOpBuilder> {
42    fn default() -> Self {
43        Self::construct(|opb| unsafe { sb::C_SkOpBuilder_Construct(opb) })
44    }
45}
46
47impl fmt::Debug for OpBuilder {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        f.debug_struct("OpBuilder").finish()
50    }
51}
52
53impl OpBuilder {
54    pub fn add(&mut self, path: &Path, operator: PathOp) -> &mut Self {
55        unsafe {
56            self.native_mut().add(path.native(), operator);
57        }
58        self
59    }
60
61    pub fn resolve(&mut self) -> Option<Path> {
62        Path::try_construct(|p| unsafe { sb::C_SkOpBuilder_resolve(self.native_mut(), p) })
63    }
64}
65
66impl Path {
67    pub fn op(&self, path: &Path, path_op: PathOp) -> Option<Self> {
68        op(self, path, path_op)
69    }
70
71    pub fn simplify(&self) -> Option<Self> {
72        simplify(self)
73    }
74
75    #[deprecated(
76        since = "0.83.0",
77        note = "Use Path::compute_tight_bounds() and test if the resulting Rect::is_finite()"
78    )]
79    pub fn tight_bounds(&self) -> Option<Rect> {
80        #[allow(deprecated)]
81        tight_bounds(self)
82    }
83
84    pub fn as_winding(&self) -> Option<Path> {
85        as_winding(self)
86    }
87}
88
89#[cfg(test)]
90mod tests {
91    use crate::{Path, PathBuilder, PathOp, Rect};
92
93    #[test]
94    fn test_tight_bounds() {
95        let mut builder = PathBuilder::new();
96        builder.add_rect(
97            Rect::from_point_and_size((10.0, 10.0), (10.0, 10.0)),
98            None,
99            None,
100        );
101        builder.add_rect(
102            Rect::from_point_and_size((15.0, 15.0), (10.0, 10.0)),
103            None,
104            None,
105        );
106        let path = builder.detach();
107
108        let tight_bounds: Rect = Rect::from_point_and_size((10.0, 10.0), (15.0, 15.0));
109        assert_eq!(path.compute_tight_bounds(), tight_bounds);
110    }
111
112    #[test]
113    fn test_union() {
114        let path = Path::rect(Rect::from_point_and_size((10.0, 10.0), (10.0, 10.0)), None);
115        let path2 = Path::rect(Rect::from_point_and_size((15.0, 15.0), (10.0, 10.0)), None);
116        let union = path.op(&path2, PathOp::Union).unwrap();
117        let expected: Rect = Rect::from_point_and_size((10.0, 10.0), (15.0, 15.0));
118        assert_eq!(union.compute_tight_bounds(), expected);
119    }
120
121    #[test]
122    fn test_intersect() {
123        let path = Path::rect(Rect::from_point_and_size((10.0, 10.0), (10.0, 10.0)), None);
124        let path2 = Path::rect(Rect::from_point_and_size((15.0, 15.0), (10.0, 10.0)), None);
125        let intersected = path.op(&path2, PathOp::Intersect).unwrap();
126        let expected: Rect = Rect::from_point_and_size((15.0, 15.0), (5.0, 5.0));
127        assert_eq!(intersected.compute_tight_bounds(), expected);
128    }
129}