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 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#[test]
90fn test_tight_bounds() {
91 let mut path = Path::new();
92 path.add_rect(Rect::from_point_and_size((10.0, 10.0), (10.0, 10.0)), None);
93 path.add_rect(Rect::from_point_and_size((15.0, 15.0), (10.0, 10.0)), None);
94 let tight_bounds: Rect = Rect::from_point_and_size((10.0, 10.0), (15.0, 15.0));
95 assert_eq!(path.compute_tight_bounds(), tight_bounds);
96}
97
98#[test]
99fn test_union() {
100 let mut path = Path::new();
101 path.add_rect(Rect::from_point_and_size((10.0, 10.0), (10.0, 10.0)), None);
102 let mut path2 = Path::new();
103 path2.add_rect(Rect::from_point_and_size((15.0, 15.0), (10.0, 10.0)), None);
104 let union = path.op(&path2, PathOp::Union).unwrap();
105 let expected: Rect = Rect::from_point_and_size((10.0, 10.0), (15.0, 15.0));
106 assert_eq!(union.compute_tight_bounds(), expected);
107}
108
109#[test]
110fn test_intersect() {
111 let mut path = Path::new();
112 path.add_rect(Rect::from_point_and_size((10.0, 10.0), (10.0, 10.0)), None);
113 let mut path2 = Path::new();
114 path2.add_rect(Rect::from_point_and_size((15.0, 15.0), (10.0, 10.0)), None);
115 let intersected = path.op(&path2, PathOp::Intersect).unwrap();
116 let expected: Rect = Rect::from_point_and_size((15.0, 15.0), (5.0, 5.0));
117 assert_eq!(intersected.compute_tight_bounds(), expected);
118}