use std::collections::HashMap;
use serde::{Deserialize, Serialize};
#[cfg(feature = "jsbindings")]
use tsify::Tsify;
#[cfg(feature = "jsbindings")]
use wasm_bindgen::prelude::wasm_bindgen;
use crate::paragraph::Paragraph;
use crate::parser::Painter;
use crate::tokens::{ControlWord, Property, Token};
pub type ColorRef = u16;
pub type ColorTable = HashMap<ColorRef, Color>;
pub type FontRef = u16;
pub type FontTable = HashMap<FontRef, Font>;
pub type StyleRef = u16;
pub type StyleSheet = HashMap<StyleRef, Style>;
#[derive(Hash, Default, Debug, Clone, PartialEq, Deserialize, Serialize)]
#[non_exhaustive]
pub struct Style {
pub name: String,
pub painter: Painter,
pub paragraph: Paragraph,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize, Serialize)]
#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
#[non_exhaustive]
pub struct RtfHeader {
pub character_set: CharacterSet,
pub font_table: FontTable,
pub color_table: ColorTable,
pub stylesheet: StyleSheet,
}
#[derive(Hash, Default, Clone, Debug, PartialEq, Deserialize, Serialize)]
#[cfg_attr(feature = "jsbindings", wasm_bindgen(getter_with_clone))]
#[non_exhaustive]
pub struct Font {
pub name: String,
pub character_set: u8,
pub font_family: FontFamily,
}
#[derive(Hash, Default, Clone, Debug, PartialEq, Deserialize, Serialize)]
#[cfg_attr(feature = "jsbindings", wasm_bindgen)]
#[non_exhaustive]
pub struct Color {
pub red: u8,
pub green: u8,
pub blue: u8,
}
#[allow(dead_code)]
#[derive(Debug, PartialEq, Default, Clone, Hash, Deserialize, Serialize)]
#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
#[non_exhaustive]
pub enum CharacterSet {
#[default]
Ansi,
Mac,
Pc,
Pca,
Ansicpg(u16),
}
impl CharacterSet {
pub fn from(token: &Token) -> Option<Self> {
match token {
Token::ControlSymbol((ControlWord::Ansi, _)) => Some(Self::Ansi),
Token::ControlSymbol((ControlWord::Mac, _)) => Some(Self::Mac),
Token::ControlSymbol((ControlWord::Pc, _)) => Some(Self::Pc),
Token::ControlSymbol((ControlWord::Pca, _)) => Some(Self::Pca),
Token::ControlSymbol((ControlWord::AnsiCodePage, Property::Value(code_page))) => u16::try_from(*code_page).ok().map(Self::Ansicpg),
_ => None,
}
}
}
#[allow(dead_code)]
#[derive(Debug, PartialEq, Hash, Clone, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "jsbindings", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
#[non_exhaustive]
pub enum FontFamily {
#[default]
Nil,
Roman,
Swiss,
Modern,
Script,
Decor,
Tech,
Bidi,
}
impl FontFamily {
pub fn from(string: &str) -> Option<Self> {
match string {
r"\fnil" => Some(Self::Nil),
r"\froman" => Some(Self::Roman),
r"\fswiss" => Some(Self::Swiss),
r"\fmodern" => Some(Self::Modern),
r"\fscript" => Some(Self::Script),
r"\fdecor" => Some(Self::Decor),
r"\ftech" => Some(Self::Tech),
r"\fbidi" => Some(Self::Bidi),
_ => None,
}
}
}