image_renderer/
paint.rs

1//! 绘制样式定义
2//!
3//! 提供画笔、填充样式等绘制属性
4
5use crate::color::Color;
6
7/// 绘制样式枚举
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum PaintStyle {
10    /// 填充
11    Fill,
12    /// 描边
13    Stroke,
14    /// 填充并描边
15    FillAndStroke,
16}
17
18/// 虚线样式
19#[derive(Debug, Clone, PartialEq)]
20pub enum DashStyle {
21    /// 实线(默认)
22    Solid,
23    /// 短虚线 [10, 5]
24    ShortDash,
25    /// 长虚线 [20, 10]
26    LongDash,
27    /// 点状 [3, 5]
28    Dot,
29    /// 点划线 [15, 5, 3, 5]
30    DashDot,
31    /// 双点划线 [15, 5, 3, 5, 3, 5]
32    DashDotDot,
33    /// 自定义模式 [dash1, gap1, dash2, gap2, ...]
34    Custom(Vec<f32>),
35}
36
37impl DashStyle {
38    /// 获取虚线模式数组
39    pub fn pattern(&self) -> Option<&[f32]> {
40        match self {
41            DashStyle::Solid => None,
42            DashStyle::ShortDash => Some(&[10.0, 5.0]),
43            DashStyle::LongDash => Some(&[20.0, 10.0]),
44            DashStyle::Dot => Some(&[3.0, 5.0]),
45            DashStyle::DashDot => Some(&[15.0, 5.0, 3.0, 5.0]),
46            DashStyle::DashDotDot => Some(&[15.0, 5.0, 3.0, 5.0, 3.0, 5.0]),
47            DashStyle::Custom(pattern) => Some(pattern.as_slice()),
48        }
49    }
50
51    /// 判断是否为实线
52    pub fn is_solid(&self) -> bool {
53        matches!(self, DashStyle::Solid)
54    }
55}
56
57impl Default for DashStyle {
58    fn default() -> Self {
59        DashStyle::Solid
60    }
61}
62
63/// 画笔样式
64#[derive(Debug, Clone)]
65pub struct Paint {
66    /// 颜色
67    color: Color,
68    /// 线宽(用于描边)
69    stroke_width: f32,
70    /// 绘制模式
71    style: PaintStyle,
72    /// 抗锯齿
73    anti_alias: bool,
74    /// 虚线样式
75    dash_style: DashStyle,
76}
77
78impl Paint {
79    /// 创建新的画笔
80    pub fn new() -> Self {
81        Self {
82            color: Color::BLACK,
83            stroke_width: 1.0,
84            style: PaintStyle::Fill,
85            anti_alias: true,
86            dash_style: DashStyle::Solid,
87        }
88    }
89
90    /// 设置颜色
91    pub fn set_color(&mut self, color: Color) {
92        self.color = color;
93    }
94
95    /// 获取颜色
96    pub fn color(&self) -> Color {
97        self.color
98    }
99
100    /// 设置线宽
101    pub fn set_stroke_width(&mut self, width: f32) {
102        self.stroke_width = width.max(0.0);
103    }
104
105    /// 获取线宽
106    pub fn stroke_width(&self) -> f32 {
107        self.stroke_width
108    }
109
110    /// 设置绘制样式
111    pub fn set_style(&mut self, style: PaintStyle) {
112        self.style = style;
113    }
114
115    /// 获取绘制样式
116    pub fn style(&self) -> PaintStyle {
117        self.style
118    }
119
120    /// 设置抗锯齿
121    pub fn set_anti_alias(&mut self, anti_alias: bool) {
122        self.anti_alias = anti_alias;
123    }
124
125    /// 是否启用抗锯齿
126    pub fn anti_alias(&self) -> bool {
127        self.anti_alias
128    }
129
130    /// 创建填充样式的画笔
131    pub fn fill(color: Color) -> Self {
132        let mut paint = Self::new();
133        paint.set_color(color);
134        paint.set_style(PaintStyle::Fill);
135        paint
136    }
137
138    /// 创建描边样式的画笔
139    pub fn stroke(color: Color, width: f32) -> Self {
140        let mut paint = Self::new();
141        paint.set_color(color);
142        paint.set_stroke_width(width);
143        paint.set_style(PaintStyle::Stroke);
144        paint
145    }
146
147    /// 设置虚线样式
148    pub fn set_dash_style(&mut self, dash_style: DashStyle) {
149        self.dash_style = dash_style;
150    }
151
152    /// 获取虚线样式
153    pub fn dash_style(&self) -> &DashStyle {
154        &self.dash_style
155    }
156
157    /// 设置自定义虚线模式
158    pub fn set_dash_pattern(&mut self, pattern: Vec<f32>) {
159        self.dash_style = DashStyle::Custom(pattern);
160    }
161
162    /// 获取虚线模式数组
163    pub fn dash_pattern(&self) -> Option<&[f32]> {
164        self.dash_style.pattern()
165    }
166
167    /// 创建带虚线样式的描边画笔
168    pub fn dashed_stroke(color: Color, width: f32, dash_style: DashStyle) -> Self {
169        let mut paint = Self::stroke(color, width);
170        paint.set_dash_style(dash_style);
171        paint
172    }
173}
174
175impl Default for Paint {
176    fn default() -> Self {
177        Self::new()
178    }
179}
180
181/// 线帽样式
182#[derive(Debug, Clone, Copy, PartialEq, Eq)]
183pub enum StrokeCap {
184    /// 平头
185    Butt,
186    /// 圆头
187    Round,
188    /// 方头
189    Square,
190}
191
192/// 线连接样式
193#[derive(Debug, Clone, Copy, PartialEq, Eq)]
194pub enum StrokeJoin {
195    /// 尖角
196    Miter,
197    /// 圆角
198    Round,
199    /// 斜角
200    Bevel,
201}
202
203/// 滤镜模式
204///
205/// 用于图片缩放时的插值算法选择
206#[derive(Debug, Clone, Copy, PartialEq, Eq)]
207pub enum FilterMode {
208    /// 最近邻插值(速度快,质量低)
209    Nearest,
210    /// 线性插值(速度和质量平衡)
211    Linear,
212}
213
214impl Default for FilterMode {
215    fn default() -> Self {
216        FilterMode::Linear
217    }
218}