1 | //! Wrapper for pathops/SkPathOps.h |
2 | use crate::{prelude::*, Path, Rect}; |
3 | use skia_bindings::{self as sb, SkOpBuilder}; |
4 | use std::fmt; |
5 | |
6 | pub use skia_bindings::SkPathOp as PathOp; |
7 | variant_name!(PathOp::XOR); |
8 | |
9 | // TODO: I am not so sure if we should export these global functions. |
10 | |
11 | pub fn op(one: &Path, two: &Path, op: PathOp) -> Option<Path> { |
12 | let mut result: Handle = Path::default(); |
13 | unsafe { sb::Op(one.native(), two.native(), op, result.native_mut()) }.if_true_some(result) |
14 | } |
15 | |
16 | pub fn simplify(path: &Path) -> Option<Path> { |
17 | let mut result: Handle = 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 | )] |
25 | pub fn tight_bounds(path: &Path) -> Option<Rect> { |
26 | let rect: Rect = path.compute_tight_bounds(); |
27 | rect.is_finite().if_true_some(rect) |
28 | } |
29 | |
30 | pub fn as_winding(path: &Path) -> Option<Path> { |
31 | let mut result: Handle = Path::default(); |
32 | unsafe { sb::AsWinding(path.native(), result.native_mut()) }.if_true_some(result) |
33 | } |
34 | |
35 | pub type OpBuilder = Handle<SkOpBuilder>; |
36 | unsafe_send_sync!(OpBuilder); |
37 | |
38 | impl NativeDrop for SkOpBuilder { |
39 | fn drop(&mut self) { |
40 | unsafe { sb::C_SkOpBuilder_destruct(self) } |
41 | } |
42 | } |
43 | |
44 | impl Default for Handle<SkOpBuilder> { |
45 | fn default() -> Self { |
46 | Self::construct(|opb: *mut SkOpBuilder| unsafe { sb::C_SkOpBuilder_Construct(uninitialized:opb) }) |
47 | } |
48 | } |
49 | |
50 | impl fmt::Debug for OpBuilder { |
51 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
52 | f.debug_struct(name:"OpBuilder" ).finish() |
53 | } |
54 | } |
55 | |
56 | impl 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: Handle = Path::default(); |
66 | unsafe { self.native_mut().resolve(result:path.native_mut()) }.if_true_some(path) |
67 | } |
68 | } |
69 | |
70 | impl Path { |
71 | pub fn op(&self, path: &Path, path_op: PathOp) -> Option<Self> { |
72 | op(self, two: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 ] |
94 | fn 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 ] |
103 | fn 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 ] |
114 | fn 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 | } |
123 | |