Skip to main content

easyofd_core/graphics2d/
ofd_graphics2d_draw_param.rs

1//! OFD 2D 图形绘制参数。
2//!
3//! 对应 Java: org.ofdrw.graphics2d.OFDGraphics2DDrawParam
4//!
5//! Java 版中封装 `java.awt.Graphics2D` 的绘制状态(颜色、字体、变换等)。
6//! Rust 版提供简化结构,保留核心绘制参数。
7
8/// OFD 2D 图形绘制参数。
9///
10/// 对应 Java: org.ofdrw.graphics2d.OFDGraphics2DDrawParam
11///
12/// 描述绘制上下文的当前状态:描边颜色、填充颜色、线宽、
13/// 透明度等。Java 版依赖 AWT Color/Font;Rust 版用基础类型。
14#[derive(Debug, Clone)]
15pub struct OfdGraphics2DDrawParam {
16    /// 描边颜色(RGB,如 0xFF0000 = 红色)。
17    pub stroke_color: Option<u32>,
18    /// 填充颜色(RGB)。
19    pub fill_color: Option<u32>,
20    /// 线宽(mm)。
21    pub line_width: f64,
22    /// 全局透明度(0.0 = 全透明,1.0 = 不透明)。
23    pub opacity: f64,
24    /// 字体大小(pt)。
25    pub font_size: f64,
26    /// 字体名称。
27    pub font_name: Option<String>,
28}
29
30impl OfdGraphics2DDrawParam {
31    /// 创建默认绘制参数。
32    #[must_use]
33    pub fn new() -> Self {
34        Self {
35            stroke_color: None,
36            fill_color: None,
37            line_width: 1.0,
38            opacity: 1.0,
39            font_size: 12.0,
40            font_name: None,
41        }
42    }
43
44    /// 设置描边颜色。
45    #[must_use]
46    pub fn stroke_color(mut self, color: u32) -> Self {
47        self.stroke_color = Some(color);
48        self
49    }
50
51    /// 设置填充颜色。
52    #[must_use]
53    pub fn fill_color(mut self, color: u32) -> Self {
54        self.fill_color = Some(color);
55        self
56    }
57
58    /// 设置线宽。
59    #[must_use]
60    pub fn line_width(mut self, width: f64) -> Self {
61        self.line_width = width;
62        self
63    }
64
65    /// 设置透明度。
66    #[must_use]
67    pub fn opacity(mut self, opacity: f64) -> Self {
68        self.opacity = opacity.clamp(0.0, 1.0);
69        self
70    }
71
72    /// 设置字体大小。
73    #[must_use]
74    pub fn font_size(mut self, size: f64) -> Self {
75        self.font_size = size;
76        self
77    }
78
79    /// 设置字体名称。
80    #[must_use]
81    pub fn font_name(mut self, name: impl Into<String>) -> Self {
82        self.font_name = Some(name.into());
83        self
84    }
85}
86
87impl Default for OfdGraphics2DDrawParam {
88    fn default() -> Self {
89        Self::new()
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96
97    #[test]
98    fn test_new_defaults() {
99        let p = OfdGraphics2DDrawParam::new();
100        assert!(p.stroke_color.is_none());
101        assert!(p.fill_color.is_none());
102        assert!((p.line_width - 1.0).abs() < f64::EPSILON);
103        assert!((p.opacity - 1.0).abs() < f64::EPSILON);
104        assert!((p.font_size - 12.0).abs() < f64::EPSILON);
105        assert!(p.font_name.is_none());
106    }
107
108    #[test]
109    fn test_builder() {
110        let p = OfdGraphics2DDrawParam::new()
111            .stroke_color(0xFF_0000)
112            .fill_color(0x00_FF00)
113            .line_width(2.5)
114            .opacity(0.8)
115            .font_size(14.0)
116            .font_name("SimSun");
117        assert_eq!(p.stroke_color, Some(0xFF_0000));
118        assert_eq!(p.fill_color, Some(0x00_FF00));
119        assert!((p.line_width - 2.5).abs() < f64::EPSILON);
120        assert!((p.opacity - 0.8).abs() < f64::EPSILON);
121        assert_eq!(p.font_name.as_deref(), Some("SimSun"));
122    }
123
124    #[test]
125    fn test_opacity_clamp() {
126        let p = OfdGraphics2DDrawParam::new().opacity(1.5);
127        assert!((p.opacity - 1.0).abs() < f64::EPSILON);
128        let p2 = OfdGraphics2DDrawParam::new().opacity(-0.5);
129        assert!((p2.opacity - 0.0).abs() < f64::EPSILON);
130    }
131
132    #[test]
133    fn test_default() {
134        let p = OfdGraphics2DDrawParam::default();
135        assert!((p.line_width - 1.0).abs() < f64::EPSILON);
136    }
137}