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 #[serde(default)]
55 pub code_page: Option<u16>,
56 pub font_family: FontFamily,
57}
58
59#[derive(Hash, Default, Clone, Debug, PartialEq, Deserialize, Serialize)]
60#[cfg_attr(feature = "jsbindings", wasm_bindgen)]
61#[non_exhaustive]
62pub struct Color {
63 pub red: u8,
64 pub green: u8,
65 pub blue: u8,
66}
67
68#[allow(dead_code)]
69#[derive(Debug, PartialEq, Default, Clone, Hash, Deserialize, Serialize)]
70#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
71#[non_exhaustive]
72pub enum CharacterSet {
73 #[default]
74 Ansi,
75 Mac,
76 Pc,
77 Pca,
78 Ansicpg(u16),
79}
80
81impl CharacterSet {
82 pub fn from(token: &Token) -> Option<Self> {
83 match token {
84 Token::ControlSymbol((ControlWord::Ansi, _)) => Some(Self::Ansi),
85 Token::ControlSymbol((ControlWord::Mac, _)) => Some(Self::Mac),
86 Token::ControlSymbol((ControlWord::Pc, _)) => Some(Self::Pc),
87 Token::ControlSymbol((ControlWord::Pca, _)) => Some(Self::Pca),
88 Token::ControlSymbol((ControlWord::AnsiCodePage, Property::Value(code_page))) => u16::try_from(*code_page).ok().map(Self::Ansicpg),
89 _ => None,
90 }
91 }
92}
93
94#[allow(dead_code)]
95#[derive(Debug, PartialEq, Hash, Clone, Default, Deserialize, Serialize)]
96#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
97#[non_exhaustive]
98pub enum FontFamily {
99 #[default]
100 Nil,
101 Roman,
102 Swiss,
103 Modern,
104 Script,
105 Decor,
106 Tech,
107 Bidi,
108}
109
110impl FontFamily {
111 pub fn from(string: &str) -> Option<Self> {
112 match string {
113 r"\fnil" => Some(Self::Nil),
114 r"\froman" => Some(Self::Roman),
115 r"\fswiss" => Some(Self::Swiss),
116 r"\fmodern" => Some(Self::Modern),
117 r"\fscript" => Some(Self::Script),
118 r"\fdecor" => Some(Self::Decor),
119 r"\ftech" => Some(Self::Tech),
120 r"\fbidi" => Some(Self::Bidi),
121 _ => None,
122 }
123 }
124}