rtf_parser/
paragraph.rs

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