Skip to main content

easyofd_core/model/
ofd_page.rs

1//! OFD 页面定义。
2
3use crate::model::content_object::ContentObject;
4use crate::model::image_object::ImageObject;
5use crate::model::path_object::PathObject;
6use crate::model::text_object::TextObject;
7
8/// OFD 文档中的单个页面。
9#[derive(Debug, Clone)]
10pub struct OfdPage {
11    /// 页面宽度(mm)。
12    pub width: f64,
13    /// 页面高度(mm)。
14    pub height: f64,
15    /// 此页面上的内容块。
16    pub content: Vec<ContentObject>,
17    /// 原始页面路径(相对文档目录,如 `"Pages/Page_Insert_55_2/Content.xml"`)。
18    ///
19    /// 读取 OFD 文件时保留,写入器优先使用该路径而非自动命名
20    /// (`Pages/Page_N/Content.xml`),从而在 roundtrip 时保持页面路径一致。
21    pub base_path: Option<String>,
22}
23
24impl OfdPage {
25    /// 使用给定尺寸创建新页面。
26    #[must_use]
27    pub fn new(width: f64, height: f64) -> Self {
28        Self {
29            width,
30            height,
31            content: Vec::new(),
32            base_path: None,
33        }
34    }
35
36    /// 设置原始页面路径(roundtrip 保留页面路径时使用)。
37    #[must_use]
38    pub fn with_base_path(mut self, path: impl Into<String>) -> Self {
39        self.base_path = Some(path.into());
40        self
41    }
42
43    /// 向此页面添加文本对象。
44    pub fn add_text(&mut self, text: TextObject) {
45        self.content.push(ContentObject::Text(text));
46    }
47
48    /// 向此页面添加图片对象。
49    pub fn add_image(&mut self, image: ImageObject) {
50        self.content.push(ContentObject::Image(image));
51    }
52
53    /// 向此页面添加路径对象。
54    pub fn add_path(&mut self, path: PathObject) {
55        self.content.push(ContentObject::Path(path));
56    }
57}