Something went wrong. Try again.
Local-first knowledge base made for Typst and the Atmosphere. typbase.at
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426use std::fmt;
use serde::{Deserialize, Serialize};use wasm_bindgen::prelude::*;
/// Colors handed to Typst as the `theme` dictionary. The slots mirror the/// renderer tokens of the app's resolved `ThemePaletteTokens` (plus derived/// `on-*` text colors), so a single token set styles both the chrome and/// rendered pages. The dict keys in [`fmt::Display`] are the kebab-case token/// names Typst code reads. `backdrop` and `overlay` stay chrome-only: the first/// is the plane behind the page and the second has alpha, which Rgb cannot.#[allow(clippy::unsafe_derive_deserialize)]#[derive(Debug, Clone, Copy, Hash, Serialize, Deserialize)]#[wasm_bindgen]pub struct ThemeColors { surface: Rgb, surface2: Rgb, surface3: Rgb,
border: Rgb, border_strong: Rgb,
text: Rgb, text_secondary: Rgb,
accent: Rgb, accent_soft: Rgb, on_accent: Rgb,
ok: Rgb, ok_soft: Rgb,
warning: Rgb, warning_soft: Rgb,
danger: Rgb, danger_soft: Rgb, on_danger: Rgb,
info: Rgb, info_soft: Rgb,
red: Rgb, orange: Rgb, yellow: Rgb, green: Rgb, cyan: Rgb, blue: Rgb, violet: Rgb,
code: Rgb,}
impl Default for ThemeColors { fn default() -> Self { Self { surface: Rgb::WHITE, surface2: Rgb(243, 244, 246), surface3: Rgb(233, 235, 238),
border: Rgb(229, 231, 235), border_strong: Rgb(209, 213, 219),
text: Rgb(31, 35, 40), text_secondary: Rgb(107, 114, 128),
accent: Rgb(30, 90, 160), accent_soft: Rgb(228, 235, 244), on_accent: Rgb::WHITE,
ok: Rgb(47, 111, 79), ok_soft: Rgb(230, 238, 234),
warning: Rgb(150, 102, 15), warning_soft: Rgb(242, 237, 226),
danger: Rgb(180, 40, 40), danger_soft: Rgb(246, 229, 229), on_danger: Rgb::WHITE,
info: Rgb(14, 116, 144), info_soft: Rgb(226, 238, 242),
red: Rgb(180, 40, 40), orange: Rgb(180, 83, 9), yellow: Rgb(150, 102, 15), green: Rgb(47, 111, 79), cyan: Rgb(14, 116, 144), blue: Rgb(30, 90, 160), violet: Rgb(109, 79, 160),
code: Rgb(246, 248, 250), } }}
#[wasm_bindgen]impl ThemeColors { #[must_use] #[allow(clippy::missing_const_for_fn, clippy::too_many_arguments)] #[wasm_bindgen(constructor)] pub fn new( surface: Rgb, surface2: Rgb, surface3: Rgb,
border: Rgb, border_strong: Rgb,
text: Rgb, text_secondary: Rgb,
accent: Rgb, accent_soft: Rgb, on_accent: Rgb,
ok: Rgb, ok_soft: Rgb,
warning: Rgb, warning_soft: Rgb,
danger: Rgb, danger_soft: Rgb, on_danger: Rgb,
info: Rgb, info_soft: Rgb,
red: Rgb, orange: Rgb, yellow: Rgb, green: Rgb, cyan: Rgb, blue: Rgb, violet: Rgb,
code: Rgb, ) -> Self { Self { surface, surface2, surface3,
border, border_strong,
text, text_secondary,
accent, accent_soft, on_accent,
ok, ok_soft,
warning, warning_soft,
danger, danger_soft, on_danger,
info, info_soft,
red, orange, yellow, green, cyan, blue, violet,
code, } }
/// Generated tmTheme text, for writing into exported project bundles. #[must_use] #[wasm_bindgen(js_name = "tmTheme")] pub fn tm_theme_js(&self) -> String { self.tm_theme() }
/// Virtual path of `tmTheme`, with no leading slash. #[must_use] #[wasm_bindgen(js_name = "tmThemePath")] pub fn tm_theme_path_js(&self) -> String { self.syntax_theme_path() }}
impl fmt::Display for ThemeColors { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, "(surface:{},surface-2:{},surface-3:{},border:{},border-strong:{},text:{},text-secondary:{},accent:{},accent-soft:{},on-accent:{},ok:{},ok-soft:{},warning:{},warning-soft:{},danger:{},danger-soft:{},on-danger:{},info:{},info-soft:{},red:{},orange:{},yellow:{},green:{},cyan:{},blue:{},violet:{},code:{})", self.surface, self.surface2, self.surface3, self.border, self.border_strong, self.text, self.text_secondary, self.accent, self.accent_soft, self.on_accent, self.ok, self.ok_soft, self.warning, self.warning_soft, self.danger, self.danger_soft, self.on_danger, self.info, self.info_soft, self.red, self.orange, self.yellow, self.green, self.cyan, self.blue, self.violet, self.code, ) }}
impl ThemeColors { /// Every channel in slot order, for a stable path hash. fn channels(&self) -> [u8; 81] { let slots = [ self.surface, self.surface2, self.surface3, self.border, self.border_strong, self.text, self.text_secondary, self.accent, self.accent_soft, self.on_accent, self.ok, self.ok_soft, self.warning, self.warning_soft, self.danger, self.danger_soft, self.on_danger, self.info, self.info_soft, self.red, self.orange, self.yellow, self.green, self.cyan, self.blue, self.violet, self.code, ]; let mut out = [0u8; 81]; for (index, color) in slots.iter().enumerate() { out[index * 3] = color.0; out[index * 3 + 1] = color.1; out[index * 3 + 2] = color.2; }
out }
/// FNV-1a over the palette, so the same theme always maps to the same /// virtual file path and different themes never collide. #[must_use] pub fn hash(&self) -> u64 { let mut hash: u64 = 0xcbf2_9ce4_8422_2325; for byte in self.channels() { hash ^= u64::from(byte); hash = hash.wrapping_mul(0x0000_0100_0000_01b3); }
hash }
/// Virtual path (project root, no leading slash) of the generated tmTheme. #[must_use] pub fn syntax_theme_path(&self) -> String { format!("typbase/syntax-{:016x}.tmTheme", self.hash()) }
/// A Sublime `.tmTheme` that colors code blocks with this palette. /// Scope names mirror Typst's default theme so every highlightable token /// keeps its scope, and only the colors change. #[must_use] pub fn tm_theme(&self) -> String { let foreground = self.text; let items = [ ("comment", Some(self.text_secondary), None), ("constant.character.escape", Some(self.ok), None), ("markup.bold", None, Some("bold")), ("markup.italic", None, Some("italic")), ("markup.underline", None, Some("underline")), ("markup.raw", Some(self.text_secondary), None), ("string.other.math.typst", None, None), ("punctuation.definition.math", Some(self.ok), None), ( "keyword.operator.math, punctuation.math.typst", Some(self.accent), None, ), ("markup.heading, entity.name.section", None, Some("bold")), ("markup.heading.typst", None, Some("bold underline")), ("punctuation.definition.list", Some(self.accent), None), ("markup.list.term", None, Some("bold")), ( "entity.name.label, markup.other.reference", Some(self.accent), None, ), ( "keyword, constant.language, variable.language", Some(self.accent), None, ), ("storage.type, storage.modifier", Some(self.accent), None), ("constant", Some(self.warning), None), ("string", Some(self.warning), None), ( "entity.name, variable.function, support", Some(self.accent), None, ), ("support.macro", Some(self.accent), None), ("meta.annotation", Some(self.warning), None), ("entity.other, meta.interpolation", Some(self.accent), None), ("meta.diff.range", Some(self.accent), None), ( "markup.inserted, meta.diff.header.to-file", Some(self.ok), None, ), ( "markup.deleted, meta.diff.header.from-file", Some(self.danger), None, ), ( "meta.mapping.key.json string.quoted.double.json", Some(self.accent), None, ), ( "meta.mapping.value.json string.quoted.double.json", Some(self.warning), None, ), ];
let mut settings = String::new(); settings.push_str(&format!( " <dict><key>settings</key><dict><key>foreground</key><string>{}</string><key>background</key><string>{}</string></dict></dict>\n", foreground.hex(), self.code.hex(), )); for (scope, color, style) in items { settings.push_str(" <dict><key>scope</key><string>"); settings.push_str(scope); settings.push_str("</string><key>settings</key><dict>"); if let Some(color) = color { settings.push_str(&format!( "<key>foreground</key><string>{}</string>", color.hex() )); } if let Some(style) = style { settings.push_str(&format!("<key>fontStyle</key><string>{style}</string>")); } settings.push_str("</dict></dict>\n"); }
format!( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n <key>name</key><string>typbase</string>\n <key>settings</key>\n <array>\n{settings} </array>\n</dict>\n</plist>\n" ) }}
impl Rgb { /// `#rrggbb` for tmTheme files. fn hex(&self) -> String { format!("#{:02x}{:02x}{:02x}", self.0, self.1, self.2) }}
#[allow(clippy::unsafe_derive_deserialize)]#[derive(Default, Debug, Clone, Copy, Hash, Serialize, Deserialize)]#[wasm_bindgen]pub struct Rgb(u8, u8, u8);
impl Rgb { pub const BLACK: Self = Self(0, 0, 0); pub const WHITE: Self = Self(255, 255, 255);}
#[wasm_bindgen]impl Rgb { #[must_use] #[allow(clippy::missing_const_for_fn)] #[wasm_bindgen(constructor)] pub fn new(r: u8, g: u8, b: u8) -> Self { Self(r, g, b) }
#[must_use] #[wasm_bindgen(js_name = toString)] pub fn to_js_string(&self) -> String { format!("rgb({},{},{})", self.0, self.1, self.2) }}
impl fmt::Display for Rgb { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "rgb({},{},{})", self.0, self.1, self.2) }}