rtf_parser/
header.rs

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