Skip to main content

easyofd_core/graphics2d/
ofd_page_graphics_configuration.rs

1//! OFD 页面图形配置。
2//!
3//! 对应 Java: org.ofdrw.graphics2d.OFDPageGraphicsConfiguration
4//!
5//! Java 版继承 `java.awt.GraphicsConfiguration`,描述输出设备的
6//! 显示能力(分辨率、色彩模型等)。Rust 版提供简化结构。
7
8/// OFD 页面图形配置。
9///
10/// 对应 Java: org.ofdrw.graphics2d.OFDPageGraphicsConfiguration
11///
12/// 描述 OFD 页面的渲染配置:DPI、色彩模式等。
13/// Java 版依赖 AWT GraphicsConfiguration;Rust 版保留核心配置字段。
14#[derive(Debug, Clone)]
15pub struct OfdPageGraphicsConfiguration {
16    /// 水平 DPI(默认 96)。
17    pub dpi_x: u32,
18    /// 垂直 DPI(默认 96)。
19    pub dpi_y: u32,
20    /// 颜色位深度(默认 24)。
21    pub color_depth: u32,
22}
23
24impl OfdPageGraphicsConfiguration {
25    /// 创建默认配置(96 DPI,24 位色深)。
26    #[must_use]
27    pub fn new() -> Self {
28        Self {
29            dpi_x: 96,
30            dpi_y: 96,
31            color_depth: 24,
32        }
33    }
34
35    /// 设置 DPI(水平和垂直)。
36    #[must_use]
37    pub fn dpi(mut self, dpi: u32) -> Self {
38        self.dpi_x = dpi;
39        self.dpi_y = dpi;
40        self
41    }
42
43    /// 设置水平 DPI。
44    #[must_use]
45    pub fn dpi_x(mut self, dpi: u32) -> Self {
46        self.dpi_x = dpi;
47        self
48    }
49
50    /// 设置垂直 DPI。
51    #[must_use]
52    pub fn dpi_y(mut self, dpi: u32) -> Self {
53        self.dpi_y = dpi;
54        self
55    }
56
57    /// 设置颜色位深度。
58    #[must_use]
59    pub fn color_depth(mut self, depth: u32) -> Self {
60        self.color_depth = depth;
61        self
62    }
63}
64
65impl Default for OfdPageGraphicsConfiguration {
66    fn default() -> Self {
67        Self::new()
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn test_new_defaults() {
77        let c = OfdPageGraphicsConfiguration::new();
78        assert_eq!(c.dpi_x, 96);
79        assert_eq!(c.dpi_y, 96);
80        assert_eq!(c.color_depth, 24);
81    }
82
83    #[test]
84    fn test_builder() {
85        let c = OfdPageGraphicsConfiguration::new().dpi(300).color_depth(32);
86        assert_eq!(c.dpi_x, 300);
87        assert_eq!(c.dpi_y, 300);
88        assert_eq!(c.color_depth, 32);
89    }
90
91    #[test]
92    fn test_separate_dpi() {
93        let c = OfdPageGraphicsConfiguration::new().dpi_x(150).dpi_y(200);
94        assert_eq!(c.dpi_x, 150);
95        assert_eq!(c.dpi_y, 200);
96    }
97
98    #[test]
99    fn test_default() {
100        let c = OfdPageGraphicsConfiguration::default();
101        assert_eq!(c.dpi_x, 96);
102    }
103}