Skip to main content

easyofd_reader/
page_info.rs

1//! 页面信息。
2//!
3//! 对应 Java: org.ofdrw.reader.PageInfo
4
5use easyofd_core::{ST_Box, ST_ID, ST_Loc};
6
7/// 页面信息,描述 OFD 文档中单个页面的元数据。
8///
9/// 对应 Java: `org.ofdrw.reader.PageInfo`
10#[derive(Debug, Clone)]
11pub struct PageInfo {
12    /// 页面的物理大小(毫米)。
13    pub size: ST_Box,
14    /// 页面在 OFD 中的对象 ID。
15    pub id: Option<ST_ID>,
16    /// 页码,从 1 开始。
17    pub index: usize,
18    /// 页面的容器内绝对路径。
19    pub page_abs_loc: Option<ST_Loc>,
20    /// 页码目录文件的序号(Page_N 中的 N)。
21    pub page_n: usize,
22    /// 该页面引用的模板页面 ID 列表。
23    pub template_ids: Vec<String>,
24}
25
26impl PageInfo {
27    /// 创建新的页面信息。
28    #[must_use]
29    pub fn new(index: usize, size: ST_Box) -> Self {
30        Self {
31            size,
32            id: None,
33            index,
34            page_abs_loc: None,
35            page_n: index.saturating_sub(1),
36            template_ids: Vec::new(),
37        }
38    }
39
40    /// 设置页面 ID。
41    #[must_use]
42    pub fn with_id(mut self, id: ST_ID) -> Self {
43        self.id = Some(id);
44        self
45    }
46
47    /// 设置页面绝对路径。
48    #[must_use]
49    pub fn with_abs_loc(mut self, loc: ST_Loc) -> Self {
50        self.page_abs_loc = Some(loc);
51        self
52    }
53
54    /// 设置页码目录序号。
55    #[must_use]
56    pub fn with_page_n(mut self, n: usize) -> Self {
57        self.page_n = n;
58        self
59    }
60
61    /// 添加模板页面 ID。
62    pub fn add_template(&mut self, template_id: String) {
63        self.template_ids.push(template_id);
64    }
65
66    /// 获取页面宽度(毫米)。
67    #[must_use]
68    pub fn width(&self) -> f64 {
69        self.size.width
70    }
71
72    /// 获取页面高度(毫米)。
73    #[must_use]
74    pub fn height(&self) -> f64 {
75        self.size.height
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82
83    #[test]
84    fn test_page_info_new() {
85        let size = ST_Box::new(0.0, 0.0, 210.0, 297.0);
86        let info = PageInfo::new(1, size);
87        assert_eq!(info.index, 1);
88        assert!((info.width() - 210.0).abs() < f64::EPSILON);
89        assert!((info.height() - 297.0).abs() < f64::EPSILON);
90        assert!(info.id.is_none());
91        assert!(info.template_ids.is_empty());
92    }
93
94    #[test]
95    fn test_page_info_with_id() {
96        let size = ST_Box::new(0.0, 0.0, 210.0, 297.0);
97        let info = PageInfo::new(1, size).with_id(ST_ID::new(42).unwrap());
98        assert_eq!(info.id.map(|id| id.get()), Some(42));
99    }
100
101    #[test]
102    fn test_page_info_page_n_default() {
103        let size = ST_Box::new(0.0, 0.0, 210.0, 297.0);
104        let info = PageInfo::new(3, size);
105        assert_eq!(info.page_n, 2);
106    }
107
108    #[test]
109    fn test_page_info_add_template() {
110        let size = ST_Box::new(0.0, 0.0, 210.0, 297.0);
111        let mut info = PageInfo::new(1, size);
112        info.add_template("tpl_0".into());
113        info.add_template("tpl_1".into());
114        assert_eq!(info.template_ids.len(), 2);
115        assert_eq!(info.template_ids[0], "tpl_0");
116    }
117
118    #[test]
119    fn test_page_info_with_page_n() {
120        let size = ST_Box::new(0.0, 0.0, 210.0, 297.0);
121        let info = PageInfo::new(1, size).with_page_n(5);
122        assert_eq!(info.page_n, 5);
123    }
124}