Skip to main content

path_kit/
paint.rs

1//! 绘图参数。Paint parameters for path stroking/filling.
2
3use crate::bridge::ffi;
4use crate::path::Path;
5use crate::stroke_rec::{StrokeCap, StrokeJoin};
6use cxx::UniquePtr;
7
8pub use crate::bridge::ffi::PaintStyle;
9
10impl Default for PaintStyle {
11    fn default() -> Self {
12        PaintStyle::Fill
13    }
14}
15
16/// 绘图参数,控制路径的填充/描边及描边样式。
17/// Paint - controls fill/stroke style and stroke parameters for path rendering.
18///
19/// 用于 `get_fill_path` 将路径转换为描边后的填充路径(类似 StrokeRec,但包含 Style)。
20/// Used with `get_fill_path` to convert path to stroked fill path (like StrokeRec, but includes Style).
21pub struct Paint {
22    inner: UniquePtr<ffi::PaintHolder>,
23}
24
25impl Paint {
26    /// 创建默认绘图参数(填充样式)。Creates default paint (fill style).
27    pub fn new() -> Self {
28        Self {
29            inner: ffi::paint_new(),
30        }
31    }
32
33    /// 设置为填充样式。Sets to fill style.
34    pub fn set_fill(&mut self) {
35        ffi::paint_set_fill(self.inner.pin_mut());
36    }
37
38    /// 设置为描边样式。Sets to stroke style.
39    pub fn set_stroke(&mut self, enable: bool) {
40        ffi::paint_set_stroke(self.inner.pin_mut(), enable);
41    }
42
43    /// 设置绘图样式。Sets paint style.
44    pub fn set_style(&mut self, style: PaintStyle) {
45        ffi::paint_set_style(self.inner.pin_mut(), style);
46    }
47
48    /// 设置描边宽度。Sets stroke width (0 = hairline).
49    pub fn set_stroke_width(&mut self, width: f32) {
50        ffi::paint_set_stroke_width(self.inner.pin_mut(), width);
51    }
52
53    /// 设置 Miter 限制。Sets miter limit for sharp corners.
54    pub fn set_stroke_miter(&mut self, miter: f32) {
55        ffi::paint_set_stroke_miter(self.inner.pin_mut(), miter);
56    }
57
58    /// 设置线端样式。Sets line cap.
59    pub fn set_stroke_cap(&mut self, cap: StrokeCap) {
60        ffi::paint_set_stroke_cap(self.inner.pin_mut(), cap);
61    }
62
63    /// 设置转角连接样式。Sets line join.
64    pub fn set_stroke_join(&mut self, join: StrokeJoin) {
65        ffi::paint_set_stroke_join(self.inner.pin_mut(), join);
66    }
67
68    /// 将路径转为填充等效路径。
69    /// Converts path to filled equivalent (applies stroke/style).
70    ///
71    /// Fill 样式返回原路径副本;Stroke 样式返回描边后的填充路径。
72    /// Fill style returns copy of path; stroke style returns stroked fill path.
73    pub fn get_fill_path(&self, path: &Path) -> Option<Path> {
74        let mut dst = Path::new();
75        let ok = ffi::paint_get_fill_path(
76            self.inner.as_ref().expect("Paint"),
77            path.as_raw(),
78            dst.as_raw_pin_mut(),
79        );
80        if ok {
81            Some(dst)
82        } else {
83            None
84        }
85    }
86
87    /// 内部引用(仅 crate 内使用)。Internal use only.
88    pub(crate) fn as_holder_ref(&self) -> &ffi::PaintHolder {
89        self.inner.as_ref().expect("Paint")
90    }
91}
92
93impl Default for Paint {
94    fn default() -> Self {
95        Self::new()
96    }
97}
98
99impl Clone for Paint {
100    fn clone(&self) -> Self {
101        Self {
102            inner: ffi::paint_clone(self.as_holder_ref()),
103        }
104    }
105}