rtf_parser_tt/
header.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4#[cfg(feature = "jsbindings")]
5use tsify::Tsify;
6#[cfg(feature = "jsbindings")]
7use wasm_bindgen::prelude::wasm_bindgen;
8
9use crate::paragraph::Paragraph;
10use crate::parser::Painter;
11use crate::tokens::{ControlWord, Token};
12
13/// The ColorRef represent the index of the color in the ColorTable
14/// It's use in the document's body to reference a specific color with the \cfN or \cbN control words
15pub type ColorRef = u16;
16pub type ColorTable = HashMap<ColorRef, Color>;
17
18/// The FontRef represent the index of the color in the FontTable
19/// It's use in the document's body to reference a specific font with the \fN control word
20pub type FontRef = u16;
21pub type FontTable = HashMap<FontRef, Font>;
22
23/// The StyleRef represent the index of the style in the StyleSheet
24/// It's use in the document's body to reference a specific style with the \sN control word
25pub type StyleRef = u16;
26pub type StyleSheet = HashMap<StyleRef, Style>;
27
28/// Style for the StyleSheet
29#[derive(Hash, Default, Debug, Clone, PartialEq, Deserialize, Serialize)]
30pub struct Style {
31    /// The style attributes
32    painter: Painter,
33    /// The layout attributes
34    paragraph: Paragraph,
35}
36
37/// Information about the document, including references to fonts & styles
38#[derive(Default, Debug, Clone, PartialEq, Deserialize, Serialize)]
39#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
40pub struct RtfHeader {
41    pub character_set: CharacterSet,
42    pub font_table: FontTable,
43    pub color_table: ColorTable,
44    pub stylesheet: StyleSheet,
45}
46
47#[derive(Hash, Default, Clone, Debug, PartialEq, Deserialize, Serialize)]
48#[cfg_attr(feature = "jsbindings", wasm_bindgen(getter_with_clone))]
49pub struct Font {
50    pub name: String,
51    pub character_set: u8,
52    pub font_family: FontFamily,
53}
54
55#[derive(Hash, Default, Clone, Debug, PartialEq, Deserialize, Serialize)]
56#[cfg_attr(feature = "jsbindings", wasm_bindgen)]
57pub struct Color {
58    pub red: u8,
59    pub green: u8,
60    pub blue: u8,
61}
62
63#[allow(dead_code)]
64#[derive(Debug, PartialEq, Default, Clone, Hash, Deserialize, Serialize)]
65#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
66pub enum CharacterSet {
67    #[default]
68    Ansi,
69    Mac,
70    Pc,
71    Pca,
72    Ansicpg(u16),
73}
74
75impl CharacterSet {
76    pub fn from(token: &Token) -> Option<Self> {
77        match token {
78            Token::ControlSymbol((ControlWord::Ansi, _)) => Some(Self::Ansi),
79            // TODO: implement the rest
80            _ => None,
81        }
82    }
83}
84
85#[allow(dead_code)]
86#[derive(Debug, PartialEq, Hash, Clone, Default, Deserialize, Serialize)]
87#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
88pub enum FontFamily {
89    #[default]
90    Nil,
91    Roman,
92    Swiss,
93    Modern,
94    Script,
95    Decor,
96    Tech,
97    Bidi,
98}
99
100impl FontFamily {
101    pub fn from(string: &str) -> Option<Self> {
102        match string {
103            r"\fnil" => Some(Self::Nil),
104            r"\froman" => Some(Self::Roman),
105            r"\fswiss" => Some(Self::Swiss),
106            r"\fmodern" => Some(Self::Modern),
107            r"\fscript" => Some(Self::Script),
108            r"\fdecor" => Some(Self::Decor),
109            r"\ftech" => Some(Self::Tech),
110            r"\fbidi" => Some(Self::Bidi),
111            _ => None,
112        }
113    }
114}