Skip to main content

easydoc_core/style/
paragraph.rs

1use crate::types::HorizontalAlignment;
2
3/// 段落级格式化。
4///
5/// 对应 Java: `com.alibaba.excel.write.metadata.style.WriteCellStyle` 中的段落属性
6#[derive(Debug, Clone, Default)]
7pub struct ParagraphStyle {
8    /// Horizontal text alignment.
9    pub alignment: Option<HorizontalAlignment>,
10    /// First-line indent in twips.
11    pub first_line_indent: Option<i32>,
12    /// Left indent in twips.
13    pub left_indent: Option<i32>,
14    /// Right indent in twips.
15    pub right_indent: Option<i32>,
16    /// Space before paragraph in twips.
17    pub space_before: Option<u32>,
18    /// Space after paragraph in twips.
19    pub space_after: Option<u32>,
20    /// Line spacing (e.g. 240 = single, 360 = 1.5, 480 = double).
21    pub line_spacing: Option<u32>,
22}
23
24impl ParagraphStyle {
25    /// Creates a new paragraph style with default values.
26    #[must_use]
27    pub fn new() -> Self {
28        Self::default()
29    }
30
31    /// Sets horizontal alignment.
32    #[must_use]
33    pub fn alignment(mut self, alignment: HorizontalAlignment) -> Self {
34        self.alignment = Some(alignment);
35        self
36    }
37
38    /// Sets first-line indent.
39    #[must_use]
40    pub fn first_line_indent(mut self, indent: i32) -> Self {
41        self.first_line_indent = Some(indent);
42        self
43    }
44
45    /// Sets spacing after the paragraph.
46    #[must_use]
47    pub fn space_after(mut self, space: u32) -> Self {
48        self.space_after = Some(space);
49        self
50    }
51
52    /// Sets line spacing in twips (240 = single, 360 = 1.5, 480 = double).
53    #[must_use]
54    pub fn line_spacing(mut self, spacing: u32) -> Self {
55        self.line_spacing = Some(spacing);
56        self
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn default_paragraph_style() {
66        let s = ParagraphStyle::default();
67        assert!(s.alignment.is_none());
68        assert!(s.first_line_indent.is_none());
69        assert!(s.space_after.is_none());
70        assert!(s.line_spacing.is_none());
71    }
72
73    #[test]
74    fn new_equals_default() {
75        let s = ParagraphStyle::new();
76        assert!(s.alignment.is_none());
77    }
78
79    #[test]
80    fn builder_chain() {
81        let s = ParagraphStyle::new()
82            .alignment(HorizontalAlignment::Center)
83            .first_line_indent(480)
84            .space_after(200)
85            .line_spacing(360);
86        assert_eq!(s.alignment, Some(HorizontalAlignment::Center));
87        assert_eq!(s.first_line_indent, Some(480));
88        assert_eq!(s.space_after, Some(200));
89        assert_eq!(s.line_spacing, Some(360));
90    }
91}