Skip to main content

easyofd_core/doc/
res.rs

1//! 资源文件。
2//!
3//! 对应 Java: org.ofdrw.core.basicStructure.res.Res
4//! 资源是绘制图元时所需数据的集合
5
6use crate::basic_type::ST_Loc;
7
8/// 资源文件。
9///
10/// 资源是绘制图元时所需数据(如绘制参数、颜色空间、字形、图像、音视频等)的集合。
11/// 对应 GB/T 33190-2016 第 7.9 节。
12///
13/// 对应 Java: org.ofdrw.core.basicStructure.res.Res
14#[derive(Debug, Clone, Default)]
15pub struct Res {
16    /// 此资源文件的通用数据存储路径。
17    pub base_loc: Option<ST_Loc>,
18    /// 资源列表(XML 片段)。
19    pub resources: Vec<String>,
20}
21
22impl Res {
23    /// 创建新的资源文件。
24    #[must_use]
25    pub fn new() -> Self {
26        Self::default()
27    }
28
29    /// 设置 BaseLoc。
30    #[must_use]
31    pub fn base_loc(mut self, loc: ST_Loc) -> Self {
32        self.base_loc = Some(loc);
33        self
34    }
35
36    /// 添加资源。
37    pub fn add_resource(&mut self, resource: impl Into<String>) {
38        self.resources.push(resource.into());
39    }
40
41    /// 获取资源数量。
42    #[must_use]
43    pub fn resource_count(&self) -> usize {
44        self.resources.len()
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn res_new() {
54        let r = Res::new();
55        assert!(r.base_loc.is_none());
56        assert_eq!(r.resource_count(), 0);
57    }
58
59    #[test]
60    fn res_builder() {
61        let r = Res::new().base_loc(ST_Loc::new("./Res"));
62        assert!(r.base_loc.is_some());
63    }
64
65    #[test]
66    fn res_add_resource() {
67        let mut r = Res::new();
68        r.add_resource("<ofd:Font/>");
69        r.add_resource("<ofd:ColorSpace/>");
70        assert_eq!(r.resource_count(), 2);
71    }
72}