1use crate::path::Path;
4use crate::stroke_rec::{StrokeCap, StrokeJoin};
5use crate::pathkit;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
9pub enum PaintStyle {
10 #[default]
12 Fill = 0,
13 Stroke = 1,
15 StrokeAndFill = 2,
17}
18
19impl From<PaintStyle> for pathkit::SkPaint_Style::Type {
20 fn from(s: PaintStyle) -> Self {
21 match s {
22 PaintStyle::Fill => pathkit::SkPaint_Style::kFill_Style,
23 PaintStyle::Stroke => pathkit::SkPaint_Style::kStroke_Style,
24 PaintStyle::StrokeAndFill => pathkit::SkPaint_Style::kStrokeAndFill_Style,
25 }
26 }
27}
28
29pub struct Paint {
35 inner: pathkit::SkPaint,
36}
37
38impl Paint {
39 pub fn new() -> Self {
41 Self {
42 inner: unsafe { pathkit::SkPaint::new() },
43 }
44 }
45
46 pub fn set_fill(&mut self) {
48 unsafe {
49 self.inner.setStyle(pathkit::SkPaint_Style::kFill_Style);
50 }
51 }
52
53 pub fn set_stroke(&mut self, enable: bool) {
55 unsafe {
56 self.inner.setStroke(enable);
57 }
58 }
59
60 pub fn set_style(&mut self, style: PaintStyle) {
62 unsafe {
63 self.inner.setStyle(style.into());
64 }
65 }
66
67 pub fn set_stroke_width(&mut self, width: f32) {
69 unsafe {
70 self.inner.setStrokeWidth(width);
71 }
72 }
73
74 pub fn set_stroke_miter(&mut self, miter: f32) {
76 unsafe {
77 self.inner.setStrokeMiter(miter);
78 }
79 }
80
81 pub fn set_stroke_cap(&mut self, cap: StrokeCap) {
83 unsafe {
84 self.inner.setStrokeCap(cap.into());
85 }
86 }
87
88 pub fn set_stroke_join(&mut self, join: StrokeJoin) {
90 unsafe {
91 self.inner.setStrokeJoin(join.into());
92 }
93 }
94
95 pub fn get_fill_path(&self, path: &Path) -> Option<Path> {
101 let mut dst = Path::new();
102 let ok = unsafe {
103 self.inner.getFillPath(
104 path.as_raw() as *const _,
105 dst.as_raw_mut() as *mut _,
106 std::ptr::null(),
107 1.0,
108 )
109 };
110 if ok {
111 Some(dst)
112 } else {
113 None
114 }
115 }
116
117 pub(crate) fn as_raw(&self) -> &pathkit::SkPaint {
119 &self.inner
120 }
121
122 #[allow(dead_code)]
124 pub(crate) fn as_raw_mut(&mut self) -> &mut pathkit::SkPaint {
125 &mut self.inner
126 }
127}
128
129impl Default for Paint {
130 fn default() -> Self {
131 Self::new()
132 }
133}
134
135impl Clone for Paint {
136 fn clone(&self) -> Self {
137 Self {
138 inner: unsafe { pathkit::SkPaint::new1(self.as_raw() as *const _) },
139 }
140 }
141}
142
143impl Drop for Paint {
144 fn drop(&mut self) {
145 unsafe {
146 self.inner.destruct();
147 }
148 }
149}