Skip to main content

easyofd_core/graphics2d/
ofd_page_graphics2d.rs

1//! OFD 页面 2D 图形上下文。
2//!
3//! 对应 Java: org.ofdrw.graphics2d.OFDPageGraphics2D
4//!
5//! Java 版继承 `java.awt.Graphics2D`,提供 2D 绘图 API。
6//! Rust 版提供简化结构,保留页面级绘图上下文状态。
7
8use super::OfdGraphics2DDrawParam;
9
10/// OFD 页面 2D 图形上下文。
11///
12/// 对应 Java: org.ofdrw.graphics2d.OFDPageGraphics2D
13///
14/// 绑定到单个 OFD 页面的绘图上下文,持有当前绘制参数和页面尺寸。
15/// Java 版提供完整的 `Graphics2D` API(drawLine / fillRect / drawString 等);
16/// Rust 版保留上下文状态,具体绘制由上层引擎实现。
17#[derive(Debug, Clone)]
18pub struct OfdPageGraphics2D {
19    /// 页面宽度(mm)。
20    pub page_width: f64,
21    /// 页面高度(mm)。
22    pub page_height: f64,
23    /// 当前绘制参数。
24    pub draw_param: OfdGraphics2DDrawParam,
25    /// 已绘制的对象计数。
26    pub object_count: u32,
27}
28
29impl OfdPageGraphics2D {
30    /// 创建新的页面图形上下文。
31    #[must_use]
32    pub fn new(page_width: f64, page_height: f64) -> Self {
33        Self {
34            page_width,
35            page_height,
36            draw_param: OfdGraphics2DDrawParam::new(),
37            object_count: 0,
38        }
39    }
40
41    /// 设置绘制参数。
42    #[must_use]
43    pub fn draw_param(mut self, param: OfdGraphics2DDrawParam) -> Self {
44        self.draw_param = param;
45        self
46    }
47
48    /// 获取页面宽度。
49    #[must_use]
50    pub fn page_width(&self) -> f64 {
51        self.page_width
52    }
53
54    /// 获取页面高度。
55    #[must_use]
56    pub fn page_height(&self) -> f64 {
57        self.page_height
58    }
59
60    /// 获取已绘制对象数。
61    #[must_use]
62    pub fn object_count(&self) -> u32 {
63        self.object_count
64    }
65
66    /// 增加对象计数(模拟绘制操作)。
67    pub fn increment_object_count(&mut self) {
68        self.object_count += 1;
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn test_new() {
78        let g = OfdPageGraphics2D::new(210.0, 297.0);
79        assert!((g.page_width() - 210.0).abs() < f64::EPSILON);
80        assert!((g.page_height() - 297.0).abs() < f64::EPSILON);
81        assert_eq!(g.object_count(), 0);
82    }
83
84    #[test]
85    fn test_draw_param() {
86        let param = OfdGraphics2DDrawParam::new().line_width(3.0);
87        let g = OfdPageGraphics2D::new(100.0, 100.0).draw_param(param);
88        assert!((g.draw_param.line_width - 3.0).abs() < f64::EPSILON);
89    }
90
91    #[test]
92    fn test_increment_object_count() {
93        let mut g = OfdPageGraphics2D::new(100.0, 100.0);
94        g.increment_object_count();
95        g.increment_object_count();
96        assert_eq!(g.object_count(), 2);
97    }
98
99    #[test]
100    fn test_clone_debug() {
101        let g = OfdPageGraphics2D::new(100.0, 200.0);
102        let g2 = g.clone();
103        assert!((g2.page_width - 100.0).abs() < f64::EPSILON);
104        assert!(format!("{g:?}").contains("OfdPageGraphics2D"));
105    }
106}