easyofd_core/graphics2d/
ofd_graphics2d_draw_param.rs1#[derive(Debug, Clone)]
15pub struct OfdGraphics2DDrawParam {
16 pub stroke_color: Option<u32>,
18 pub fill_color: Option<u32>,
20 pub line_width: f64,
22 pub opacity: f64,
24 pub font_size: f64,
26 pub font_name: Option<String>,
28}
29
30impl OfdGraphics2DDrawParam {
31 #[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 #[must_use]
46 pub fn stroke_color(mut self, color: u32) -> Self {
47 self.stroke_color = Some(color);
48 self
49 }
50
51 #[must_use]
53 pub fn fill_color(mut self, color: u32) -> Self {
54 self.fill_color = Some(color);
55 self
56 }
57
58 #[must_use]
60 pub fn line_width(mut self, width: f64) -> Self {
61 self.line_width = width;
62 self
63 }
64
65 #[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 #[must_use]
74 pub fn font_size(mut self, size: f64) -> Self {
75 self.font_size = size;
76 self
77 }
78
79 #[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}