Skip to main content

easyofd_core/action/
goto.rs

1//! 文档内跳转动作。
2//!
3//! 对应 Java: org.ofdrw.core.action.actionType.Goto
4
5use super::{CTDest, OfdAction};
6
7/// 文档内跳转动作。
8///
9/// 跳转到当前文档的指定页面和位置,对应 GB/T 33190 第 15 章的 Goto 动作。
10///
11/// 对应 Java: org.ofdrw.core.action.actionType.Goto
12#[derive(Debug, Clone)]
13pub struct Goto {
14    /// 跳转目标位置。
15    ///
16    /// 对应 Java: Goto.dest (CT_Dest)
17    pub dest: CTDest,
18}
19
20impl Goto {
21    /// 创建一个新的文档内跳转动作。
22    ///
23    /// 对应 Java: new Goto(CT_Dest dest)
24    #[must_use]
25    pub fn new(dest: CTDest) -> Self {
26        Self { dest }
27    }
28}
29
30impl OfdAction for Goto {
31    fn to_xml_string(&self) -> String {
32        format!("<ofd:Goto>{}</ofd:Goto>", self.dest.to_xml_string())
33    }
34
35    fn clone_box(&self) -> Box<dyn OfdAction> {
36        Box::new(self.clone())
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::super::DestType;
43    use super::*;
44
45    #[test]
46    fn test_goto_new() {
47        let dest = CTDest::new(3).dest_type(DestType::XYZ).left(10.0).top(20.0);
48        let goto = Goto::new(dest);
49        assert_eq!(goto.dest.page, 3);
50    }
51
52    #[test]
53    fn test_goto_to_xml() {
54        let dest = CTDest::new(1).dest_type(DestType::Fit);
55        let goto = Goto::new(dest);
56        let xml = goto.to_xml_string();
57        assert!(xml.contains("<ofd:Goto>"));
58        assert!(xml.contains("PageID=\"1\""));
59        assert!(xml.contains("Type=\"Fit\""));
60        assert!(xml.contains("</ofd:Goto>"));
61    }
62
63    #[test]
64    fn test_goto_clone_debug() {
65        let dest = CTDest::new(5);
66        let goto = Goto::new(dest);
67        let goto2 = goto.clone();
68        assert_eq!(goto2.dest.page, 5);
69        let dbg = format!("{goto:?}");
70        assert!(dbg.contains("Goto"));
71    }
72}