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