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, Property, Token};
12
13pub type ColorRef = u16;
16pub type ColorTable = HashMap<ColorRef, Color>;
17
18pub type FontRef = u16;
21pub type FontTable = HashMap<FontRef, Font>;
22
23pub type StyleRef = u16;
26pub type StyleSheet = HashMap<StyleRef, Style>;
27
28#[derive(Hash, Default, Debug, Clone, PartialEq, Deserialize, Serialize)]
30#[non_exhaustive]
31pub struct Style {
32 pub name: String,
33 pub painter: Painter,
34 pub paragraph: Paragraph,
35}
36
37#[derive(Default, Debug, Clone, PartialEq, Deserialize, Serialize)]
39#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
40#[non_exhaustive]
41pub struct RtfHeader {
42 pub character_set: CharacterSet,
43 pub font_table: FontTable,
44 pub color_table: ColorTable,
45 pub stylesheet: StyleSheet,
46}
47
48#[derive(Hash, Default, Clone, Debug, PartialEq, Deserialize, Serialize)]
49#[cfg_attr(feature = "jsbindings", wasm_bindgen(getter_with_clone))]
50#[non_exhaustive]
51pub struct Font {
52 pub name: String,
53 pub character_set: u8,
54 pub font_family: FontFamily,
55}
56
57#[derive(Hash, Default, Clone, Debug, PartialEq, Deserialize, Serialize)]
58#[cfg_attr(feature = "jsbindings", wasm_bindgen)]
59#[non_exhaustive]
60pub struct Color {
61 pub red: u8,
62 pub green: u8,
63 pub blue: u8,
64}
65
66#[allow(dead_code)]
67#[derive(Debug, PartialEq, Default, Clone, Hash, Deserialize, Serialize)]
68#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
69#[non_exhaustive]
70pub enum CharacterSet {
71 #[default]
72 Ansi,
73 Mac,
74 Pc,
75 Pca,
76 Ansicpg(u16),
77}
78
79impl CharacterSet {
80 pub fn from(token: &Token) -> Option<Self> {
81 match token {
82 Token::ControlSymbol((ControlWord::Ansi, _)) => Some(Self::Ansi),
83 Token::ControlSymbol((ControlWord::Mac, _)) => Some(Self::Mac),
84 Token::ControlSymbol((ControlWord::Pc, _)) => Some(Self::Pc),
85 Token::ControlSymbol((ControlWord::Pca, _)) => Some(Self::Pca),
86 Token::ControlSymbol((ControlWord::AnsiCodePage, Property::Value(code_page))) => u16::try_from(*code_page).ok().map(Self::Ansicpg),
87 _ => None,
88 }
89 }
90}
91
92#[allow(dead_code)]
93#[derive(Debug, PartialEq, Hash, Clone, Default, Deserialize, Serialize)]
94#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
95#[non_exhaustive]
96pub enum FontFamily {
97 #[default]
98 Nil,
99 Roman,
100 Swiss,
101 Modern,
102 Script,
103 Decor,
104 Tech,
105 Bidi,
106}
107
108impl FontFamily {
109 pub fn from(string: &str) -> Option<Self> {
110 match string {
111 r"\fnil" => Some(Self::Nil),
112 r"\froman" => Some(Self::Roman),
113 r"\fswiss" => Some(Self::Swiss),
114 r"\fmodern" => Some(Self::Modern),
115 r"\fscript" => Some(Self::Script),
116 r"\fdecor" => Some(Self::Decor),
117 r"\ftech" => Some(Self::Tech),
118 r"\fbidi" => Some(Self::Bidi),
119 _ => None,
120 }
121 }
122}