Skip to main content

rtf_parser_tt/
paragraph.rs

1/// Define the paragraph related structs and enums
2use serde::{Deserialize, Serialize};
3
4#[cfg(feature = "jsbindings")]
5use tsify::Tsify;
6#[cfg(feature = "jsbindings")]
7use wasm_bindgen::prelude::wasm_bindgen;
8
9use crate::tokens::ControlWord;
10
11#[derive(Debug, Default, Clone, Copy, PartialEq, Hash, Deserialize, Serialize)]
12#[cfg_attr(feature = "jsbindings", wasm_bindgen)]
13#[non_exhaustive]
14pub struct Paragraph {
15    pub alignment: Alignment,
16    pub spacing: Spacing,
17    pub indent: Indentation,
18    pub tab_width: i32,
19}
20
21/// Alignement of a paragraph (left, right, center, justify)
22#[derive(Debug, Default, Clone, Copy, PartialEq, Hash, Deserialize, Serialize)]
23#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
24#[non_exhaustive]
25pub enum Alignment {
26    #[default]
27    LeftAligned, // \ql
28    RightAligned, // \qr
29    Center,       // \qc
30    Justify,      // \qj
31}
32
33impl From<&ControlWord<'_>> for Alignment {
34    fn from(cw: &ControlWord) -> Self {
35        match cw {
36            ControlWord::LeftAligned  => Alignment::LeftAligned,
37            ControlWord::RightAligned => Alignment::RightAligned,
38            ControlWord::Center       => Alignment::Center,
39            ControlWord::Justify      => Alignment::Justify,
40            _  /* default */          => Alignment::LeftAligned,
41        }
42    }
43}
44
45/// The vertical margin before / after a block of text
46#[derive(Debug, Default, Clone, Copy, PartialEq, Hash, Deserialize, Serialize)]
47#[cfg_attr(feature = "jsbindings", wasm_bindgen)]
48#[non_exhaustive]
49pub struct Spacing {
50    pub before: i32,
51    pub after: i32,
52    pub between_line: SpaceBetweenLine,
53    pub line_multiplier: i32,
54}
55
56#[derive(Default, Debug, Clone, Copy, PartialEq, Hash, Deserialize, Serialize)]
57#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
58#[non_exhaustive]
59pub enum SpaceBetweenLine {
60    Value(i32),
61    #[default]
62    Auto,
63    Invalid,
64}
65
66/// Space between lines.
67// If this control word is missing or if \sl1000 is used, the line spacing is automatically determined by the tallest character in the line;
68// if N is a positive value, this size is used only if it is taller than the tallest character (otherwise, the tallest character is used);
69// if N is a negative value, the absolute value of N is used, even if it is shorter than the tallest character.
70impl From<i32> for SpaceBetweenLine {
71    fn from(value: i32) -> Self {
72        match value {
73            1000 => SpaceBetweenLine::Auto,
74            val if val < 0 => SpaceBetweenLine::Value(val.abs()),
75            val => SpaceBetweenLine::Value(val),
76        }
77    }
78}
79
80// This struct can not be an enum because left-indent and right-ident can both be defined at the same time
81#[derive(Default, Debug, Clone, Copy, PartialEq, Hash, Deserialize, Serialize)]
82#[cfg_attr(feature = "jsbindings", wasm_bindgen)]
83#[non_exhaustive]
84pub struct Indentation {
85    pub left: i32,
86    pub right: i32,
87    pub first_line: i32,
88}