use std::sync::Arc;
use super::AttrBinding;
use super::BlockPatch;
use super::BlockRule;
use super::Border;
use super::Chrome;
use super::Decorations;
use super::EmbedRule;
use super::EmbedStyle;
use super::FontSize;
use super::LeafStyle;
use super::MarkRule;
use super::MarkerKind;
use super::MarkerStyle;
use super::Motion;
use super::TableRole;
use super::TextPatch;
use super::TextStyle;
use super::Theme;
use super::ThemeBuilder;
use crate::Color;
use crate::FontSpec;
use crate::Insets;
pub const SANS: &str = "Inter, ui-sans-serif, system-ui, sans-serif";
pub const MONO: &str =
"\"JetBrains Mono\", SFMono-Regular, Menlo, Consolas, \"Liberation Mono\", monospace";
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ColorScheme {
#[default]
Light,
Dark,
}
struct Palette {
page: Color,
ink: Color,
accent: Color,
accent_wash: Color,
rule: Color,
quote_bar: Color,
highlight: Color,
code_page: Color,
code_ink: Color,
selection: Color,
callouts: [(Color, Color); 4],
table_header: Color,
comment: Color,
conditional: Color,
ink_paper: Color,
}
const LIGHT: Palette = Palette {
page: Color::rgb(0xff, 0xff, 0xff),
ink: Color::rgb(0x2e, 0x2b, 0x29),
accent: Color::rgb(0x6a, 0x00, 0xf5),
accent_wash: Color::rgb(0xee, 0xee, 0xff),
rule: Color::rgb(0xe5, 0xe3, 0xe1),
quote_bar: Color::rgb(0xd6, 0xd3, 0xd1),
highlight: Color::rgb(0xfa, 0xf5, 0x94),
code_page: Color::rgb(0x2e, 0x2b, 0x29),
code_ink: Color::rgb(0xff, 0xff, 0xff),
selection: Color::rgba(0x6a, 0x00, 0xf5, 0x33),
callouts: [
(Color::rgb(0xee, 0xee, 0xff), Color::rgb(0x6a, 0x00, 0xf5)),
(Color::rgb(0xe6, 0xf6, 0xea), Color::rgb(0x2b, 0x8a, 0x3e)),
(Color::rgb(0xff, 0xf4, 0xdb), Color::rgb(0xe8, 0x59, 0x0c)),
(Color::rgb(0xff, 0xe9, 0xe9), Color::rgb(0xc9, 0x2a, 0x2a)),
],
table_header: Color::rgb(0xf7, 0xf6, 0xf5),
comment: Color::rgba(0xe8, 0x59, 0x0c, 0x1f),
conditional: Color::rgba(0x1c, 0x7e, 0xd6, 0x1f),
ink_paper: Color::rgb(0xfa, 0xf9, 0xf7),
};
const DARK: Palette = Palette {
page: Color::rgb(0x1a, 0x18, 0x17),
ink: Color::rgb(0xe9, 0xe6, 0xe3),
accent: Color::rgb(0xb8, 0x96, 0xff),
accent_wash: Color::rgb(0x33, 0x2a, 0x4d),
rule: Color::rgb(0x3d, 0x39, 0x36),
quote_bar: Color::rgb(0x57, 0x52, 0x4e),
highlight: Color::rgb(0x5c, 0x55, 0x1a),
code_page: Color::rgb(0x0d, 0x0c, 0x0b),
code_ink: Color::rgb(0xe9, 0xe6, 0xe3),
selection: Color::rgba(0xb8, 0x96, 0xff, 0x4d),
callouts: [
(Color::rgb(0x33, 0x2a, 0x4d), Color::rgb(0xb8, 0x96, 0xff)),
(Color::rgb(0x1f, 0x3a, 0x29), Color::rgb(0x69, 0xdb, 0x7c)),
(Color::rgb(0x45, 0x34, 0x18), Color::rgb(0xff, 0xa9, 0x4d)),
(Color::rgb(0x4a, 0x21, 0x21), Color::rgb(0xff, 0x87, 0x87)),
],
table_header: Color::rgb(0x24, 0x21, 0x1f),
comment: Color::rgba(0xff, 0xa9, 0x4d, 0x2e),
conditional: Color::rgba(0x74, 0xc0, 0xfc, 0x2e),
ink_paper: Color::rgb(0x21, 0x1f, 0x1d),
};
#[derive(Clone, Debug, PartialEq)]
pub struct StarterKitOptions {
pub scheme: ColorScheme,
pub body_family: Arc<str>,
pub code_family: Arc<str>,
pub size: f32,
pub line_height: f32,
pub letter_spacing: f32,
}
impl Default for StarterKitOptions {
fn default() -> Self {
Self {
scheme: ColorScheme::Light,
body_family: Arc::from(SANS),
code_family: Arc::from(MONO),
size: REM,
line_height: BODY_LINE_HEIGHT,
letter_spacing: 0.0,
}
}
}
const REM: f32 = 16.0;
const BODY_LINE_HEIGHT: f32 = 1.5;
const BOLD: u16 = 700;
const SEMIBOLD: u16 = 600;
const HEADING_LINE_HEIGHT: f32 = 1.1;
const MARKER_GAP: f32 = 0.4 * REM;
const CHECKBOX_SIZE: f32 = 0.875 * REM;
const PARAGRAPH_SPACE: f32 = 0.75 * REM;
const FIGURE_SPACE: f32 = 1.5 * REM;
const LIST_SPACE: f32 = 1.25 * REM;
const LIST_INDENT: f32 = 1.4 * REM;
const ITEM_SPACE: f32 = 0.25 * REM;
const CODE_BLOCK_SIZE: f32 = 0.8 * REM;
const CODE_BLOCK_PADDING: f32 = 0.75 * REM;
const INLINE_CODE_SCALE: f32 = 0.85;
const SCRIPT_SCALE: f32 = 0.75;
const SUBSCRIPT_SHIFT: f32 = -0.25;
const SUPERSCRIPT_SHIFT: f32 = 0.5;
const IMAGE_HEIGHT: f32 = 12.5 * REM;
const AUDIO_HEIGHT: f32 = 3.5 * REM;
const FILE_HEIGHT: f32 = 4.0 * REM;
const MATH_HEIGHT: f32 = 3.0 * REM;
struct HeadingStyle {
level: i64,
size: f32,
space_before: f32,
space_after: f32,
}
impl HeadingStyle {
fn rule(&self) -> BlockRule {
BlockRule::new(
"heading",
BlockPatch::new()
.text(TextPatch::new().size(FontSize::Px(self.size * REM)))
.space(self.space_before * REM, self.space_after * REM)
.tag(&format!("h{}", self.level)),
)
.when("level", self.level)
}
}
const HEADINGS: [HeadingStyle; 6] = [
HeadingStyle {
level: 1,
size: 1.4,
space_before: 3.5,
space_after: 1.5,
},
HeadingStyle {
level: 2,
size: 1.2,
space_before: 3.5,
space_after: 1.5,
},
HeadingStyle {
level: 3,
size: 1.1,
space_before: 2.5,
space_after: 0.75,
},
HeadingStyle {
level: 4,
size: 1.0,
space_before: 2.5,
space_after: 0.75,
},
HeadingStyle {
level: 5,
size: 1.0,
space_before: 2.5,
space_after: 0.75,
},
HeadingStyle {
level: 6,
size: 1.0,
space_before: 2.5,
space_after: 0.75,
},
];
impl Theme {
#[must_use]
pub fn starter_kit() -> Self {
Self::starter_kit_with(&StarterKitOptions::default())
}
#[must_use]
pub fn starter_kit_with(options: &StarterKitOptions) -> Self {
let palette = match options.scheme {
ColorScheme::Light => &LIGHT,
ColorScheme::Dark => &DARK,
};
let builder = Theme::builder(base_style(options, palette))
.chrome(Chrome {
background: palette.page,
selection: palette.selection,
caret: palette.ink,
caret_width: 2.0,
})
.markers(MarkerStyle {
gap: MARKER_GAP,
bullets: vec![Arc::from("•"), Arc::from("◦"), Arc::from("▪")],
checkbox: CHECKBOX_SIZE,
color: None,
});
let builder = block_rules(options, palette)
.into_iter()
.fold(builder, ThemeBuilder::block);
let builder = mark_rules(options, palette)
.into_iter()
.fold(builder, ThemeBuilder::mark);
embed_rules(palette)
.into_iter()
.fold(builder, ThemeBuilder::embed)
.build()
}
}
fn base_style(options: &StarterKitOptions, palette: &Palette) -> TextStyle {
TextStyle {
font: FontSpec {
family: options.body_family.clone(),
size: options.size,
weight: 400,
italic: false,
},
line_height: options.line_height,
color: palette.ink,
background: None,
decorations: Decorations::default(),
letter_spacing: options.letter_spacing,
baseline_shift: 0.0,
motion: Motion::None,
}
}
fn block_rules(options: &StarterKitOptions, palette: &Palette) -> Vec<BlockRule> {
let mut rules = text_block_rules();
rules.extend(container_rules(palette));
rules.extend(code_and_leaf_rules(options, palette));
rules.extend(nested_list_rules());
if cfg!(feature = "theme-extensions") {
rules.extend(media_rules());
rules.extend(details_rules());
rules.extend(callout_rules(palette));
}
if cfg!(feature = "tables") {
rules.extend(table_rules(palette));
}
if cfg!(feature = "ink") {
rules.push(BlockRule::new(
"ink",
BlockPatch::new()
.leaf(LeafStyle::Ink {
height: INK_HEIGHT,
stroke: INK_STROKE,
})
.background(palette.ink_paper, 0.5 * REM)
.space(FIGURE_SPACE, FIGURE_SPACE)
.tag("figure"),
));
}
rules
}
fn media_rules() -> [BlockRule; 5] {
[
media_rule("youtube", IMAGE_HEIGHT),
media_rule("twitch", IMAGE_HEIGHT),
media_rule("audio", AUDIO_HEIGHT),
media_rule("file", FILE_HEIGHT),
media_rule("blockMath", MATH_HEIGHT),
]
}
const INK_HEIGHT: f32 = 400.0;
const INK_STROKE: f32 = 4.0;
fn table_rules(palette: &Palette) -> Vec<BlockRule> {
let cell = || {
BlockPatch::new()
.table(TableRole::Cell)
.padding(Insets::symmetric(0.375 * REM, 0.5 * REM))
};
let rule = Border {
width: 1.0,
color: palette.rule,
};
vec![
BlockRule::new(
"table",
BlockPatch::new()
.table(TableRole::Table { rule })
.space(FIGURE_SPACE, FIGURE_SPACE)
.tag("table"),
),
BlockRule::new(
"tableRow",
BlockPatch::new().table(TableRole::Row).tag("tr"),
),
BlockRule::new("tableCell", cell().tag("td")),
BlockRule::new(
"tableHeader",
cell()
.text(TextPatch::new().weight(BOLD))
.background(palette.table_header, 0.0)
.tag("th"),
),
BlockRule::new("paragraph", BlockPatch::new().space(ITEM_SPACE, ITEM_SPACE))
.within("tableCell"),
BlockRule::new("paragraph", BlockPatch::new().space(ITEM_SPACE, ITEM_SPACE))
.within("tableHeader"),
]
}
fn details_rules() -> Vec<BlockRule> {
vec![
BlockRule::new(
"details",
BlockPatch::new()
.padding(Insets::new(0.0, 0.0, 0.0, LIST_INDENT))
.space(PARAGRAPH_SPACE, PARAGRAPH_SPACE)
.marker(MarkerKind::Disclosure)
.tag("div"),
),
BlockRule::new(
"detailsSummary",
BlockPatch::new()
.text(TextPatch::new().weight(SEMIBOLD))
.tag("div"),
),
BlockRule::new("detailsContent", BlockPatch::new().tag("div")),
]
}
fn callout_rules(palette: &Palette) -> Vec<BlockRule> {
let callout = |(background, bar): (Color, Color)| {
BlockPatch::new()
.background(background, 0.5 * REM)
.border_left(4.0, bar)
.padding(Insets::symmetric(0.75 * REM, REM))
.space(FIGURE_SPACE, FIGURE_SPACE)
.tag("div")
};
let [info, success, warning, error] = palette.callouts;
vec![
BlockRule::new("callout", callout(info)),
BlockRule::new("callout", callout(success)).when("type", "success"),
BlockRule::new("callout", callout(warning)).when("type", "warning"),
BlockRule::new("callout", callout(error)).when("type", "error"),
BlockRule::new("callout", BlockPatch::new().border_left(0.0, palette.rule))
.when("type", "blank"),
BlockRule::new("paragraph", BlockPatch::new().space(ITEM_SPACE, ITEM_SPACE))
.within("callout"),
]
}
fn text_block_rules() -> Vec<BlockRule> {
let heading_text = TextPatch::new()
.weight(BOLD)
.line_height(HEADING_LINE_HEIGHT);
let mut rules = vec![
BlockRule::new(
"paragraph",
BlockPatch::new()
.space(PARAGRAPH_SPACE, PARAGRAPH_SPACE)
.tag("p"),
),
BlockRule::new("heading", BlockPatch::new().text(heading_text).tag("h1")),
];
rules.extend(HEADINGS.iter().map(HeadingStyle::rule));
rules
}
fn container_rules(palette: &Palette) -> Vec<BlockRule> {
let list = |marker| {
BlockPatch::new()
.padding(Insets::new(0.0, REM, 0.0, LIST_INDENT))
.space(LIST_SPACE, LIST_SPACE)
.marker(marker)
};
let item = || BlockPatch::new().space(ITEM_SPACE, ITEM_SPACE);
vec![
BlockRule::new(
"blockquote",
BlockPatch::new()
.border_left(3.0, palette.quote_bar)
.padding(Insets::new(0.0, 0.0, 0.0, REM))
.space(FIGURE_SPACE, FIGURE_SPACE)
.tag("blockquote"),
),
BlockRule::new("bulletList", list(MarkerKind::Bullet).tag("ul")),
BlockRule::new("orderedList", list(MarkerKind::Ordered).tag("ol")),
BlockRule::new("taskList", list(MarkerKind::Task).tag("ul")),
BlockRule::new("listItem", item().tag("li")),
BlockRule::new("taskItem", item().tag("li")),
BlockRule::new("paragraph", item()).within("listItem"),
BlockRule::new("paragraph", item()).within("taskItem"),
]
}
fn code_and_leaf_rules(options: &StarterKitOptions, palette: &Palette) -> Vec<BlockRule> {
let code_text = TextPatch::new()
.family(&options.code_family)
.size(FontSize::Px(CODE_BLOCK_SIZE))
.color(palette.code_ink);
vec![
BlockRule::new(
"codeBlock",
BlockPatch::new()
.text(code_text)
.padding(Insets::symmetric(CODE_BLOCK_PADDING, REM))
.background(palette.code_page, 0.5 * REM)
.space(FIGURE_SPACE, FIGURE_SPACE)
.tag("pre"),
),
BlockRule::new(
"horizontalRule",
BlockPatch::new()
.leaf(LeafStyle::Rule {
thickness: 1.0,
color: palette.rule,
})
.space(2.0 * REM, 2.0 * REM)
.tag("hr"),
),
BlockRule::new(
"image",
BlockPatch::new()
.leaf(LeafStyle::Slot {
height: IMAGE_HEIGHT,
})
.space(REM, REM)
.tag("div"),
),
]
}
fn media_rule(name: &str, height: f32) -> BlockRule {
BlockRule::new(
name,
BlockPatch::new()
.leaf(LeafStyle::Slot { height })
.space(REM, REM)
.tag("div"),
)
}
fn nested_list_rules() -> Vec<BlockRule> {
let mut rules = Vec::new();
for list in ["bulletList", "orderedList", "taskList"] {
for item in ["listItem", "taskItem"] {
rules.push(
BlockRule::new(list, BlockPatch::new().space(ITEM_SPACE, ITEM_SPACE)).within(item),
);
}
}
rules
}
fn mark_rules(options: &StarterKitOptions, palette: &Palette) -> Vec<MarkRule> {
let mut rules = vec![
MarkRule::new("bold", "strong", TextPatch::new().weight(BOLD)),
MarkRule::new("italic", "em", TextPatch::new().italic(true)),
MarkRule::new("underline", "u", TextPatch::new().underline(true)),
MarkRule::new("strike", "s", TextPatch::new().strike(true)),
MarkRule::new(
"code",
"code",
TextPatch::new()
.family(&options.code_family)
.size(FontSize::Em(INLINE_CODE_SCALE))
.background(palette.accent_wash),
),
MarkRule::new(
"link",
"a",
TextPatch::new().color(palette.accent).underline(true),
),
MarkRule::new(
"highlight",
"mark",
TextPatch::new().background(palette.highlight),
)
.bind(AttrBinding::Background(Arc::from("color"))),
MarkRule::new("textStyle", "span", TextPatch::new())
.bind(AttrBinding::Color(Arc::from("color")))
.bind(AttrBinding::Background(Arc::from("backgroundColor")))
.bind(AttrBinding::FontFamily(Arc::from("fontFamily")))
.bind(AttrBinding::FontSize(Arc::from("fontSize")))
.bind(AttrBinding::LineHeight(Arc::from("lineHeight"))),
];
if cfg!(feature = "theme-extensions") {
rules.extend([
MarkRule::new(
"comment",
"span",
TextPatch::new().background(palette.comment).underline(true),
),
MarkRule::new(
"conditional",
"span",
TextPatch::new().background(palette.conditional),
),
MarkRule::new(
"subscript",
"sub",
TextPatch::new()
.size(FontSize::Em(SCRIPT_SCALE))
.baseline_shift(SUBSCRIPT_SHIFT),
),
MarkRule::new(
"superscript",
"sup",
TextPatch::new()
.size(FontSize::Em(SCRIPT_SCALE))
.baseline_shift(SUPERSCRIPT_SHIFT),
),
]);
}
if cfg!(feature = "kinetic") {
rules.push(
MarkRule::new("kinetic", "span", TextPatch::new())
.bind(AttrBinding::Motion(Arc::from("effect"))),
);
}
rules
}
fn embed_rules(palette: &Palette) -> Vec<EmbedRule> {
let mut rules = vec![
EmbedRule::new("hardBreak", EmbedStyle::LineBreak),
EmbedRule::new(
"inlineImage",
EmbedStyle::Box {
width: REM,
height: REM,
},
),
];
if cfg!(feature = "theme-extensions") {
rules.extend([
EmbedRule::new(
"mention",
EmbedStyle::Label {
attr: Arc::from("label"),
prefix: Arc::from("@"),
patch: TextPatch::new()
.color(palette.accent)
.background(palette.accent_wash),
fallback: Some(Arc::from("id")),
},
),
EmbedRule::new(
"inlineMath",
EmbedStyle::Label {
attr: Arc::from("latex"),
prefix: Arc::from(""),
patch: TextPatch::new().background(palette.accent_wash),
fallback: None,
},
),
EmbedRule::new(
"emoji",
EmbedStyle::Label {
attr: Arc::from("emoji"),
prefix: Arc::from(""),
patch: TextPatch::new(),
fallback: Some(Arc::from("name")),
},
),
]);
}
rules
}