1use crate::path::Path;
4use crate::pathkit;
5
6#[derive(Debug, Clone, Copy, PartialEq)]
8pub enum StrokeStyle {
9 Hairline,
11 Fill,
13 Stroke { width: f32, stroke_and_fill: bool },
15 StrokeAndFill { width: f32 },
17}
18
19pub struct StrokeRec {
25 inner: pathkit::SkStrokeRec,
26}
27
28impl StrokeRec {
29 pub fn new_fill() -> Self {
31 Self {
32 inner: unsafe {
33 pathkit::SkStrokeRec::new(pathkit::SkStrokeRec_InitStyle::kFill_InitStyle)
34 },
35 }
36 }
37
38 pub fn new_hairline() -> Self {
40 Self {
41 inner: unsafe {
42 pathkit::SkStrokeRec::new(pathkit::SkStrokeRec_InitStyle::kHairline_InitStyle)
43 },
44 }
45 }
46
47 pub fn new_stroke(width: f32, stroke_and_fill: bool) -> Self {
49 let mut rec = Self::new_hairline();
50 unsafe {
51 rec.inner.setStrokeStyle(width, stroke_and_fill);
52 }
53 rec
54 }
55
56 pub fn set_fill(&mut self) {
58 unsafe {
59 self.inner.setFillStyle();
60 }
61 }
62
63 pub fn set_hairline(&mut self) {
65 unsafe {
66 self.inner.setHairlineStyle();
67 }
68 }
69
70 pub fn set_stroke_style(&mut self, width: f32, stroke_and_fill: bool) {
72 unsafe {
73 self.inner.setStrokeStyle(width, stroke_and_fill);
74 }
75 }
76
77 pub fn style(&self) -> StrokeStyle {
79 let raw = unsafe { self.inner.getStyle() };
80 match raw {
81 pathkit::SkStrokeRec_Style::kHairline_Style => StrokeStyle::Hairline,
82 pathkit::SkStrokeRec_Style::kFill_Style => StrokeStyle::Fill,
83 pathkit::SkStrokeRec_Style::kStroke_Style => StrokeStyle::Stroke {
84 width: self.inner.fWidth,
85 stroke_and_fill: false,
86 },
87 pathkit::SkStrokeRec_Style::kStrokeAndFill_Style => StrokeStyle::StrokeAndFill {
88 width: self.inner.fWidth,
89 },
90 _ => StrokeStyle::Fill,
91 }
92 }
93
94 pub fn width(&self) -> f32 {
96 self.inner.fWidth
97 }
98
99 pub fn inflation_radius(&self) -> f32 {
101 unsafe { self.inner.getInflationRadius() }
102 }
103
104 pub fn apply_to_path(&self, path: &Path) -> Option<Path> {
110 let mut dst = Path::new();
111 let ok = unsafe {
112 pathkit::SkStrokeRec_applyToPath(
113 &self.inner as *const _,
114 dst.as_raw_mut() as *mut _,
115 path.as_raw() as *const _,
116 )
117 };
118 if ok {
119 Some(dst)
120 } else {
121 None
122 }
123 }
124
125 #[allow(dead_code)]
127 pub(crate) fn as_raw(&self) -> &pathkit::SkStrokeRec {
128 &self.inner
129 }
130
131 #[allow(dead_code)]
133 pub(crate) fn as_raw_mut(&mut self) -> &mut pathkit::SkStrokeRec {
134 &mut self.inner
135 }
136}
137
138impl Default for StrokeRec {
139 fn default() -> Self {
140 Self::new_fill()
141 }
142}