2020-02-11 10:01:39 +02:00
|
|
|
use serde::Serialize;
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
use super::*;
|
2019-11-06 12:17:49 +02:00
|
|
|
use crate::documents::BuildXML;
|
2021-11-25 12:42:06 +02:00
|
|
|
use crate::types::{AlignmentType, SpecialIndentType};
|
2023-10-10 10:00:15 +03:00
|
|
|
use crate::ParagraphBorderPosition;
|
2024-03-18 14:29:16 +02:00
|
|
|
use crate::{xml_builder::*, TextAlignmentType};
|
2019-11-06 12:17:49 +02:00
|
|
|
|
2022-01-02 19:18:04 +02:00
|
|
|
#[derive(Serialize, Debug, Clone, PartialEq, Default)]
|
2020-02-11 10:01:39 +02:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2019-12-06 19:15:21 +02:00
|
|
|
pub struct ParagraphProperty {
|
2020-02-11 10:01:39 +02:00
|
|
|
pub run_property: RunProperty,
|
2022-01-07 18:28:34 +02:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2020-05-27 14:43:25 +03:00
|
|
|
pub style: Option<ParagraphStyle>,
|
2022-01-07 18:28:34 +02:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2020-02-11 10:01:39 +02:00
|
|
|
pub numbering_property: Option<NumberingProperty>,
|
2022-01-07 18:28:34 +02:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2020-02-11 10:01:39 +02:00
|
|
|
pub alignment: Option<Justification>,
|
2022-01-07 18:28:34 +02:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2020-02-11 10:01:39 +02:00
|
|
|
pub indent: Option<Indent>,
|
2022-01-07 18:28:34 +02:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2021-09-29 03:03:39 +03:00
|
|
|
pub line_spacing: Option<LineSpacing>,
|
2022-01-07 18:28:34 +02:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub keep_next: Option<bool>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub keep_lines: Option<bool>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub page_break_before: Option<bool>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2022-01-14 08:03:53 +02:00
|
|
|
pub widow_control: Option<bool>,
|
2022-01-07 18:28:34 +02:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2021-09-10 03:42:08 +03:00
|
|
|
pub outline_lvl: Option<OutlineLvl>,
|
2022-09-09 05:52:53 +03:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub section_property: Option<SectionProperty>,
|
2022-01-02 19:18:04 +02:00
|
|
|
pub tabs: Vec<Tab>,
|
2022-01-07 18:28:34 +02:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub paragraph_property_change: Option<ParagraphPropertyChange>,
|
2023-07-27 15:43:15 +03:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub borders: Option<ParagraphBorders>,
|
2024-03-18 10:28:52 +02:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub frame_property: Option<FrameProperty>,
|
2024-03-18 14:29:16 +02:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub text_alignment: Option<TextAlignment>,
|
2024-03-18 16:27:28 +02:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub adjust_right_ind: Option<AdjustRightInd>,
|
2024-07-17 10:28:29 +03:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub snap_to_grid: Option<bool>,
|
2024-03-18 16:27:28 +02:00
|
|
|
// read only
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub(crate) div_id: Option<String>,
|
2019-11-11 06:05:07 +02:00
|
|
|
}
|
|
|
|
|
2019-11-06 12:17:49 +02:00
|
|
|
// 17.3.1.26
|
|
|
|
// pPr (Paragraph Properties)
|
|
|
|
// This element specifies a set of paragraph properties which shall be applied to the contents of the parent
|
|
|
|
// paragraph after all style/numbering/table properties have been applied to the text. These properties are defined
|
|
|
|
// as direct formatting, since they are directly applied to the paragraph and supersede any formatting from styles.
|
2019-12-06 19:15:21 +02:00
|
|
|
impl ParagraphProperty {
|
|
|
|
pub fn new() -> ParagraphProperty {
|
2019-11-11 06:05:07 +02:00
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
2019-12-06 12:18:48 +02:00
|
|
|
pub fn align(mut self, alignment_type: AlignmentType) -> Self {
|
2019-11-11 06:05:07 +02:00
|
|
|
self.alignment = Some(Justification::new(alignment_type.to_string()));
|
|
|
|
self
|
2019-11-06 12:17:49 +02:00
|
|
|
}
|
2019-11-11 08:36:26 +02:00
|
|
|
|
2019-12-06 12:18:48 +02:00
|
|
|
pub fn style(mut self, style_id: &str) -> Self {
|
2020-05-27 14:43:25 +03:00
|
|
|
self.style = Some(ParagraphStyle::new(Some(style_id)));
|
2019-11-11 08:36:26 +02:00
|
|
|
self
|
|
|
|
}
|
2019-11-11 09:48:28 +02:00
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
pub fn indent(
|
|
|
|
mut self,
|
2020-03-10 04:56:12 +02:00
|
|
|
left: Option<i32>,
|
2020-02-11 10:01:39 +02:00
|
|
|
special_indent: Option<SpecialIndentType>,
|
2020-02-28 12:52:41 +02:00
|
|
|
end: Option<i32>,
|
2020-03-10 04:56:12 +02:00
|
|
|
start_chars: Option<i32>,
|
2020-02-11 10:01:39 +02:00
|
|
|
) -> Self {
|
2020-03-10 04:56:12 +02:00
|
|
|
self.indent = Some(Indent::new(left, special_indent, end, start_chars));
|
2019-11-11 09:48:28 +02:00
|
|
|
self
|
|
|
|
}
|
2019-12-06 12:18:48 +02:00
|
|
|
|
2019-12-06 19:15:21 +02:00
|
|
|
pub fn numbering(mut self, id: NumberingId, level: IndentLevel) -> Self {
|
2020-03-13 10:01:25 +02:00
|
|
|
self.numbering_property = Some(NumberingProperty::new().add_num(id, level));
|
2019-12-06 12:18:48 +02:00
|
|
|
self
|
|
|
|
}
|
2020-10-14 04:16:13 +03:00
|
|
|
|
2022-01-14 08:03:53 +02:00
|
|
|
pub fn numbering_property(mut self, np: NumberingProperty) -> Self {
|
|
|
|
self.numbering_property = Some(np);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-11-25 12:42:06 +02:00
|
|
|
pub fn line_spacing(mut self, spacing: LineSpacing) -> Self {
|
|
|
|
self.line_spacing = Some(spacing);
|
2020-10-14 04:16:13 +03:00
|
|
|
self
|
|
|
|
}
|
2021-04-08 10:53:21 +03:00
|
|
|
|
2023-06-23 13:09:27 +03:00
|
|
|
pub fn character_spacing(mut self, spacing: i32) -> Self {
|
|
|
|
self.run_property.character_spacing = Some(CharacterSpacing::new(spacing));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-07-17 10:28:29 +03:00
|
|
|
pub fn snap_to_grid(mut self, v: bool) -> Self {
|
|
|
|
self.snap_to_grid = Some(v);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-04-14 06:01:38 +03:00
|
|
|
pub fn keep_next(mut self, v: bool) -> Self {
|
2022-01-07 18:28:34 +02:00
|
|
|
self.keep_next = Some(v);
|
2021-04-14 06:01:38 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn keep_lines(mut self, v: bool) -> Self {
|
2022-01-07 18:28:34 +02:00
|
|
|
self.keep_lines = Some(v);
|
2021-04-14 06:01:38 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-09-10 03:42:08 +03:00
|
|
|
pub fn outline_lvl(mut self, v: usize) -> Self {
|
2021-10-20 10:37:51 +03:00
|
|
|
if v >= 10 {
|
2022-12-13 16:05:37 +02:00
|
|
|
// clamped
|
|
|
|
self.outline_lvl = Some(OutlineLvl::new(9));
|
2021-10-20 10:37:51 +03:00
|
|
|
return self;
|
|
|
|
}
|
2021-09-10 03:42:08 +03:00
|
|
|
self.outline_lvl = Some(OutlineLvl::new(v));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-04-14 06:01:38 +03:00
|
|
|
pub fn page_break_before(mut self, v: bool) -> Self {
|
2022-01-07 18:28:34 +02:00
|
|
|
self.page_break_before = Some(v);
|
2021-04-14 06:01:38 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-01-14 08:03:53 +02:00
|
|
|
pub fn widow_control(mut self, v: bool) -> Self {
|
|
|
|
self.widow_control = Some(v);
|
2021-04-14 06:01:38 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-01-02 19:18:04 +02:00
|
|
|
pub fn add_tab(mut self, t: Tab) -> Self {
|
|
|
|
self.tabs.push(t);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-09-09 05:52:53 +03:00
|
|
|
pub fn section_property(mut self, s: SectionProperty) -> Self {
|
|
|
|
self.section_property = Some(s);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-01-07 18:28:34 +02:00
|
|
|
pub fn paragraph_property_change(mut self, p: ParagraphPropertyChange) -> Self {
|
|
|
|
self.paragraph_property_change = Some(p);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-03-18 10:28:52 +02:00
|
|
|
pub fn frame_property(mut self, s: FrameProperty) -> Self {
|
|
|
|
self.frame_property = Some(s);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-03-18 14:29:16 +02:00
|
|
|
pub fn text_alignment(mut self, s: TextAlignmentType) -> Self {
|
|
|
|
self.text_alignment = Some(TextAlignment::new(s));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-03-18 16:27:28 +02:00
|
|
|
pub fn adjust_right_ind(mut self, s: isize) -> Self {
|
|
|
|
self.adjust_right_ind = Some(AdjustRightInd::new(s));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-04-08 10:53:21 +03:00
|
|
|
pub(crate) fn hanging_chars(mut self, chars: i32) -> Self {
|
|
|
|
if let Some(indent) = self.indent {
|
|
|
|
self.indent = Some(indent.hanging_chars(chars));
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn first_line_chars(mut self, chars: i32) -> Self {
|
|
|
|
if let Some(indent) = self.indent {
|
|
|
|
self.indent = Some(indent.first_line_chars(chars));
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
2023-07-27 15:43:15 +03:00
|
|
|
|
|
|
|
pub fn set_borders(mut self, borders: ParagraphBorders) -> Self {
|
|
|
|
self.borders = Some(borders);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_border(mut self, border: ParagraphBorder) -> Self {
|
|
|
|
self.borders = Some(self.borders.unwrap_or_default().set(border));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear_border(mut self, position: ParagraphBorderPosition) -> Self {
|
|
|
|
self.borders = Some(self.borders.unwrap_or_default().clear(position));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear_all_borders(mut self) -> Self {
|
|
|
|
self.borders = Some(self.borders.unwrap_or_default().clear_all());
|
|
|
|
self
|
|
|
|
}
|
2019-11-06 12:17:49 +02:00
|
|
|
}
|
|
|
|
|
2022-01-07 18:28:34 +02:00
|
|
|
fn inner_build(p: &ParagraphProperty) -> Vec<u8> {
|
|
|
|
let mut b = XMLBuilder::new()
|
|
|
|
.open_paragraph_property()
|
|
|
|
.add_child(&p.run_property)
|
|
|
|
.add_optional_child(&p.style)
|
|
|
|
.add_optional_child(&p.numbering_property)
|
2024-03-18 10:28:52 +02:00
|
|
|
.add_optional_child(&p.frame_property)
|
2022-01-07 18:28:34 +02:00
|
|
|
.add_optional_child(&p.alignment)
|
|
|
|
.add_optional_child(&p.indent)
|
|
|
|
.add_optional_child(&p.line_spacing)
|
|
|
|
.add_optional_child(&p.outline_lvl)
|
2023-07-27 15:43:15 +03:00
|
|
|
.add_optional_child(&p.paragraph_property_change)
|
2024-03-18 14:29:16 +02:00
|
|
|
.add_optional_child(&p.borders)
|
2024-03-18 16:27:28 +02:00
|
|
|
.add_optional_child(&p.text_alignment)
|
|
|
|
.add_optional_child(&p.adjust_right_ind);
|
2022-01-07 18:28:34 +02:00
|
|
|
|
2024-07-17 10:28:29 +03:00
|
|
|
if let Some(v) = p.snap_to_grid {
|
2024-07-18 13:11:11 +03:00
|
|
|
b = b.snap_to_grid(v)
|
2024-07-17 10:28:29 +03:00
|
|
|
}
|
|
|
|
|
2022-01-07 18:28:34 +02:00
|
|
|
if let Some(v) = p.keep_next {
|
|
|
|
if v {
|
2021-04-14 06:01:38 +03:00
|
|
|
b = b.keep_next()
|
|
|
|
}
|
2022-01-07 18:28:34 +02:00
|
|
|
}
|
2021-04-14 06:01:38 +03:00
|
|
|
|
2022-01-07 18:28:34 +02:00
|
|
|
if let Some(v) = p.keep_lines {
|
|
|
|
if v {
|
2021-04-14 06:01:38 +03:00
|
|
|
b = b.keep_lines()
|
|
|
|
}
|
2022-01-07 18:28:34 +02:00
|
|
|
}
|
2021-04-14 06:01:38 +03:00
|
|
|
|
2022-01-07 18:28:34 +02:00
|
|
|
if let Some(v) = p.page_break_before {
|
|
|
|
if v {
|
2021-04-14 06:01:38 +03:00
|
|
|
b = b.page_break_before()
|
|
|
|
}
|
2022-01-07 18:28:34 +02:00
|
|
|
}
|
2021-04-14 06:01:38 +03:00
|
|
|
|
2022-01-14 08:03:53 +02:00
|
|
|
if let Some(v) = p.widow_control {
|
2023-06-22 18:20:27 +03:00
|
|
|
b = b.widow_control(if v { "1" } else { "0" })
|
2022-01-07 18:28:34 +02:00
|
|
|
}
|
2021-04-14 06:01:38 +03:00
|
|
|
|
2022-01-07 18:28:34 +02:00
|
|
|
if !p.tabs.is_empty() {
|
|
|
|
b = b.open_tabs();
|
|
|
|
for t in p.tabs.iter() {
|
|
|
|
b = b.tab(t.val, t.leader, t.pos);
|
2022-01-02 19:18:04 +02:00
|
|
|
}
|
2022-01-07 18:28:34 +02:00
|
|
|
b = b.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
b.close().build()
|
|
|
|
}
|
2022-01-02 19:18:04 +02:00
|
|
|
|
2022-01-07 18:28:34 +02:00
|
|
|
impl BuildXML for ParagraphProperty {
|
|
|
|
fn build(&self) -> Vec<u8> {
|
|
|
|
inner_build(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BuildXML for Box<ParagraphProperty> {
|
|
|
|
fn build(&self) -> Vec<u8> {
|
|
|
|
inner_build(self)
|
2019-11-06 12:17:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-11-25 12:42:06 +02:00
|
|
|
use crate::types::LineSpacingType;
|
2019-11-07 06:57:58 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
use pretty_assertions::assert_eq;
|
2019-11-06 12:17:49 +02:00
|
|
|
use std::str;
|
|
|
|
|
|
|
|
#[test]
|
2019-11-11 08:36:26 +02:00
|
|
|
fn test_default() {
|
2019-11-06 12:17:49 +02:00
|
|
|
let c = ParagraphProperty::new();
|
|
|
|
let b = c.build();
|
2020-06-08 07:41:13 +03:00
|
|
|
assert_eq!(str::from_utf8(&b).unwrap(), r#"<w:pPr><w:rPr /></w:pPr>"#);
|
2019-11-06 12:17:49 +02:00
|
|
|
}
|
2019-11-11 08:36:26 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_alignment() {
|
|
|
|
let c = ParagraphProperty::new();
|
|
|
|
let b = c.align(AlignmentType::Right).build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
2020-05-27 14:43:25 +03:00
|
|
|
r#"<w:pPr><w:rPr /><w:jc w:val="right" /></w:pPr>"#
|
2019-11-11 09:48:28 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_indent() {
|
|
|
|
let c = ParagraphProperty::new();
|
2020-03-10 04:56:12 +02:00
|
|
|
let b = c.indent(Some(20), None, None, None).build();
|
2019-11-11 09:48:28 +02:00
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
2020-05-27 14:43:25 +03:00
|
|
|
r#"<w:pPr><w:rPr /><w:ind w:left="20" w:right="0" /></w:pPr>"#
|
2020-02-11 10:01:39 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-14 06:01:38 +03:00
|
|
|
#[test]
|
|
|
|
fn test_keep_next() {
|
|
|
|
let c = ParagraphProperty::new();
|
|
|
|
let b = c.keep_next(true).build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
|
|
|
r#"<w:pPr><w:rPr /><w:keepNext />
|
|
|
|
</w:pPr>"#
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-10 03:42:08 +03:00
|
|
|
#[test]
|
|
|
|
fn test_outline_lvl() {
|
|
|
|
let props = ParagraphProperty::new();
|
|
|
|
let bytes = props.outline_lvl(1).build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&bytes).unwrap(),
|
|
|
|
r#"<w:pPr><w:rPr /><w:outlineLvl w:val="1" /></w:pPr>"#
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
#[test]
|
|
|
|
fn test_indent_json() {
|
|
|
|
let c = ParagraphProperty::new();
|
2020-03-10 04:56:12 +02:00
|
|
|
let b = c.indent(Some(20), Some(SpecialIndentType::FirstLine(10)), None, None);
|
2020-02-11 10:01:39 +02:00
|
|
|
assert_eq!(
|
|
|
|
serde_json::to_string(&b).unwrap(),
|
2022-01-07 18:28:34 +02:00
|
|
|
r#"{"runProperty":{},"indent":{"start":20,"startChars":null,"end":null,"specialIndent":{"type":"firstLine","val":10},"hangingChars":null,"firstLineChars":null},"tabs":[]}"#
|
2019-11-11 08:36:26 +02:00
|
|
|
);
|
|
|
|
}
|
2021-09-29 03:03:39 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_line_spacing() {
|
|
|
|
let props = ParagraphProperty::new();
|
2021-11-25 12:42:06 +02:00
|
|
|
let spacing = LineSpacing::new()
|
|
|
|
.line_rule(LineSpacingType::AtLeast)
|
|
|
|
.line(100);
|
|
|
|
let bytes = props.line_spacing(spacing).build();
|
2021-09-29 03:03:39 +03:00
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&bytes).unwrap(),
|
|
|
|
r#"<w:pPr><w:rPr /><w:spacing w:line="100" w:lineRule="atLeast" /></w:pPr>"#
|
|
|
|
)
|
|
|
|
}
|
2019-11-06 12:17:49 +02:00
|
|
|
}
|