Skip to main content

path_kit/
paint.rs

1//! 绘图参数。Paint parameters for path stroking/filling.
2
3use crate::path::Path;
4use crate::stroke_rec::{StrokeCap, StrokeJoin};
5use crate::pathkit;
6
7/// 绘图样式。Paint style - fill, stroke, or both.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
9pub enum PaintStyle {
10    /// 填充 / Fill
11    #[default]
12    Fill = 0,
13    /// 描边 / Stroke
14    Stroke = 1,
15    /// 描边并填充 / Stroke and fill
16    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
29/// 绘图参数,控制路径的填充/描边及描边样式。
30/// Paint - controls fill/stroke style and stroke parameters for path rendering.
31///
32/// 用于 `get_fill_path` 将路径转换为描边后的填充路径(类似 StrokeRec,但包含 Style)。
33/// Used with `get_fill_path` to convert path to stroked fill path (like StrokeRec, but includes Style).
34pub struct Paint {
35    inner: pathkit::SkPaint,
36}
37
38impl Paint {
39    /// 创建默认绘图参数(填充样式)。Creates default paint (fill style).
40    pub fn new() -> Self {
41        Self {
42            inner: unsafe { pathkit::SkPaint::new() },
43        }
44    }
45
46    /// 设置为填充样式。Sets to fill style.
47    pub fn set_fill(&mut self) {
48        unsafe {
49            self.inner.setStyle(pathkit::SkPaint_Style::kFill_Style);
50        }
51    }
52
53    /// 设置为描边样式。Sets to stroke style.
54    pub fn set_stroke(&mut self, enable: bool) {
55        unsafe {
56            self.inner.setStroke(enable);
57        }
58    }
59
60    /// 设置绘图样式。Sets paint style.
61    pub fn set_style(&mut self, style: PaintStyle) {
62        unsafe {
63            self.inner.setStyle(style.into());
64        }
65    }
66
67    /// 设置描边宽度。Sets stroke width (0 = hairline).
68    pub fn set_stroke_width(&mut self, width: f32) {
69        unsafe {
70            self.inner.setStrokeWidth(width);
71        }
72    }
73
74    /// 设置 Miter 限制。Sets miter limit for sharp corners.
75    pub fn set_stroke_miter(&mut self, miter: f32) {
76        unsafe {
77            self.inner.setStrokeMiter(miter);
78        }
79    }
80
81    /// 设置线端样式。Sets line cap.
82    pub fn set_stroke_cap(&mut self, cap: StrokeCap) {
83        unsafe {
84            self.inner.setStrokeCap(cap.into());
85        }
86    }
87
88    /// 设置转角连接样式。Sets line join.
89    pub fn set_stroke_join(&mut self, join: StrokeJoin) {
90        unsafe {
91            self.inner.setStrokeJoin(join.into());
92        }
93    }
94
95    /// 将路径转为填充等效路径。
96    /// Converts path to filled equivalent (applies stroke/style).
97    ///
98    /// Fill 样式返回原路径副本;Stroke 样式返回描边后的填充路径。
99    /// Fill style returns copy of path; stroke style returns stroked fill path.
100    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    /// 内部 SkPaint 引用(仅 crate 内使用)。Internal use only.
118    pub(crate) fn as_raw(&self) -> &pathkit::SkPaint {
119        &self.inner
120    }
121
122    /// 内部 SkPaint 可变引用(仅 crate 内使用)。Internal use only.
123    #[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}