Skip to main content

easyofd_core/graphics2d/
ofd_page_graphics_device.rs

1//! OFD 页面图形设备。
2//!
3//! 对应 Java: org.ofdrw.graphics2d.OFDPageGraphicsDevice
4//!
5//! Java 版继承 `java.awt.GraphicsDevice`,代表物理/逻辑显示设备。
6//! Rust 版提供简化结构,保留设备标识与类型。
7
8use super::OfdPageGraphicsConfiguration;
9
10/// 图形设备类型。
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum GraphicsDeviceType {
13    /// 屏幕显示。
14    Screen,
15    /// 打印机。
16    Printer,
17    /// 图像缓冲区。
18    Image,
19}
20
21/// OFD 页面图形设备。
22///
23/// 对应 Java: org.ofdrw.graphics2d.OFDPageGraphicsDevice
24///
25/// 描述 OFD 输出的目标设备。Java 版依赖 AWT GraphicsDevice;
26/// Rust 版保留设备类型和默认配置。
27#[derive(Debug, Clone)]
28pub struct OfdPageGraphicsDevice {
29    /// 设备类型。
30    pub device_type: GraphicsDeviceType,
31    /// 设备名称。
32    pub name: Option<String>,
33    /// 默认图形配置。
34    pub configuration: OfdPageGraphicsConfiguration,
35}
36
37impl OfdPageGraphicsDevice {
38    /// 创建默认图像缓冲设备。
39    #[must_use]
40    pub fn new() -> Self {
41        Self {
42            device_type: GraphicsDeviceType::Image,
43            name: None,
44            configuration: OfdPageGraphicsConfiguration::new(),
45        }
46    }
47
48    /// 创建指定类型的设备。
49    #[must_use]
50    pub fn with_type(device_type: GraphicsDeviceType) -> Self {
51        Self {
52            device_type,
53            name: None,
54            configuration: OfdPageGraphicsConfiguration::new(),
55        }
56    }
57
58    /// 设置设备名称。
59    #[must_use]
60    pub fn name(mut self, name: impl Into<String>) -> Self {
61        self.name = Some(name.into());
62        self
63    }
64
65    /// 设置图形配置。
66    #[must_use]
67    pub fn configuration(mut self, config: OfdPageGraphicsConfiguration) -> Self {
68        self.configuration = config;
69        self
70    }
71
72    /// 获取设备类型。
73    #[must_use]
74    pub fn device_type(&self) -> GraphicsDeviceType {
75        self.device_type
76    }
77}
78
79impl Default for OfdPageGraphicsDevice {
80    fn default() -> Self {
81        Self::new()
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    #[test]
90    fn test_new_default() {
91        let d = OfdPageGraphicsDevice::new();
92        assert_eq!(d.device_type(), GraphicsDeviceType::Image);
93        assert!(d.name.is_none());
94    }
95
96    #[test]
97    fn test_with_type() {
98        let d = OfdPageGraphicsDevice::with_type(GraphicsDeviceType::Screen);
99        assert_eq!(d.device_type(), GraphicsDeviceType::Screen);
100    }
101
102    #[test]
103    fn test_builder() {
104        let config = OfdPageGraphicsConfiguration::new().dpi(300);
105        let d = OfdPageGraphicsDevice::new()
106            .name("Printer1")
107            .configuration(config);
108        assert_eq!(d.name.as_deref(), Some("Printer1"));
109        assert_eq!(d.configuration.dpi_x, 300);
110    }
111
112    #[test]
113    fn test_device_type_variants() {
114        assert_ne!(GraphicsDeviceType::Screen, GraphicsDeviceType::Printer);
115        assert_ne!(GraphicsDeviceType::Printer, GraphicsDeviceType::Image);
116    }
117
118    #[test]
119    fn test_default() {
120        let d = OfdPageGraphicsDevice::default();
121        assert_eq!(d.device_type(), GraphicsDeviceType::Image);
122    }
123}