use std::collections::{BTreeMap, HashMap};
use serde::Deserialize;
#[derive(Debug, Clone, Copy, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ColorRgb {
pub r: u8,
pub g: u8,
pub b: u8,
}
impl ColorRgb {
pub const fn new(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b }
}
}
impl From<ColorRgb> for krilla::color::rgb::Color {
fn from(c: ColorRgb) -> Self {
krilla::color::rgb::Color::new(c.r, c.g, c.b)
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct HeadingStyle {
pub font_size: f32,
pub font_weight: f32,
pub space_before: f32,
pub space_after: f32,
pub color: Option<ColorRgb>,
pub font_families: Vec<String>,
}
impl Default for HeadingStyle {
fn default() -> Self {
Self {
font_families: Vec::new(),
font_size: 14.0,
font_weight: 700.0,
space_before: 12.0,
space_after: 6.0,
color: None,
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct HeadingStyles {
pub h1: HeadingStyle,
pub h2: HeadingStyle,
pub h3: HeadingStyle,
pub h4: HeadingStyle,
pub h5: HeadingStyle,
pub h6: HeadingStyle,
}
impl Default for HeadingStyles {
fn default() -> Self {
Self {
h1: HeadingStyle {
font_size: 26.0,
font_weight: 700.0,
space_before: 18.0,
space_after: 12.0,
color: None,
font_families: Vec::new(),
},
h2: HeadingStyle {
font_size: 21.0,
font_weight: 700.0,
space_before: 16.0,
space_after: 10.0,
color: None,
font_families: Vec::new(),
},
h3: HeadingStyle {
font_size: 17.0,
font_weight: 700.0,
space_before: 14.0,
space_after: 8.0,
color: None,
font_families: Vec::new(),
},
h4: HeadingStyle {
font_size: 14.0,
font_weight: 700.0,
space_before: 12.0,
space_after: 6.0,
color: None,
font_families: Vec::new(),
},
h5: HeadingStyle {
font_size: 12.0,
font_weight: 700.0,
space_before: 10.0,
space_after: 6.0,
color: None,
font_families: Vec::new(),
},
h6: HeadingStyle {
font_size: 11.0,
font_weight: 700.0,
space_before: 10.0,
space_after: 4.0,
color: None,
font_families: Vec::new(),
},
}
}
}
impl HeadingStyles {
pub fn for_level(&self, level: u8) -> &HeadingStyle {
match level {
1 => &self.h1,
2 => &self.h2,
3 => &self.h3,
4 => &self.h4,
5 => &self.h5,
_ => &self.h6,
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct HeadingNumbering {
pub enabled: bool,
pub max_depth: u8,
pub separator: String,
}
impl Default for HeadingNumbering {
fn default() -> Self {
Self {
enabled: false,
max_depth: 3,
separator: " ".to_string(),
}
}
}
#[derive(Debug, Clone, Copy, Default, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum CalloutDecoration {
#[default]
Box,
Rules,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct CalloutStyle {
pub background: ColorRgb,
pub border: ColorRgb,
pub accent: ColorRgb,
pub decoration: CalloutDecoration,
pub label: String,
pub label_color: Option<ColorRgb>,
pub label_centered: bool,
pub icon: String,
pub font_families: Vec<String>,
}
impl Default for CalloutStyle {
fn default() -> Self {
Self {
font_families: Vec::new(),
background: ColorRgb::new(247, 248, 250),
border: ColorRgb::new(220, 225, 230),
accent: ColorRgb::new(120, 130, 145),
decoration: CalloutDecoration::Box,
label: String::new(),
label_color: None,
label_centered: false,
icon: String::new(),
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct LinkStyle {
pub color: ColorRgb,
pub italic: bool,
pub bold: bool,
pub underline: bool,
pub underline_thickness: f32,
}
impl Default for LinkStyle {
fn default() -> Self {
Self {
color: ColorRgb::new(20, 95, 175),
italic: false,
bold: false,
underline: false,
underline_thickness: 0.6,
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct CodeHighlightStyle {
pub keyword: ColorRgb,
pub string: ColorRgb,
pub comment: ColorRgb,
pub number: ColorRgb,
}
impl Default for CodeHighlightStyle {
fn default() -> Self {
Self {
keyword: ColorRgb::new(170, 50, 130),
string: ColorRgb::new(70, 120, 50),
comment: ColorRgb::new(120, 130, 140),
number: ColorRgb::new(190, 110, 30),
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct CalloutStyles {
pub note: CalloutStyle,
pub info: CalloutStyle,
pub warning: CalloutStyle,
pub caution: CalloutStyle,
pub danger: CalloutStyle,
pub success: CalloutStyle,
pub notice: CalloutStyle,
}
impl Default for CalloutStyles {
fn default() -> Self {
Self {
note: CalloutStyle {
background: ColorRgb::new(247, 248, 250),
border: ColorRgb::new(220, 225, 230),
accent: ColorRgb::new(120, 130, 145),
..CalloutStyle::default()
},
info: CalloutStyle {
background: ColorRgb::new(232, 244, 253),
border: ColorRgb::new(180, 213, 240),
accent: ColorRgb::new(54, 130, 200),
..CalloutStyle::default()
},
warning: CalloutStyle {
background: ColorRgb::new(255, 247, 230),
border: ColorRgb::new(252, 211, 166),
accent: ColorRgb::new(217, 119, 6),
..CalloutStyle::default()
},
caution: CalloutStyle {
background: ColorRgb::new(255, 247, 230),
border: ColorRgb::new(252, 211, 166),
accent: ColorRgb::new(217, 119, 6),
..CalloutStyle::default()
},
danger: CalloutStyle {
background: ColorRgb::new(254, 232, 232),
border: ColorRgb::new(248, 187, 187),
accent: ColorRgb::new(204, 51, 51),
..CalloutStyle::default()
},
success: CalloutStyle {
background: ColorRgb::new(232, 250, 240),
border: ColorRgb::new(168, 220, 188),
accent: ColorRgb::new(46, 160, 100),
..CalloutStyle::default()
},
notice: CalloutStyle {
background: ColorRgb::new(245, 240, 255),
border: ColorRgb::new(214, 198, 240),
accent: ColorRgb::new(120, 80, 200),
..CalloutStyle::default()
},
}
}
}
impl CalloutStyles {
pub fn for_kind(&self, kind: &str) -> &CalloutStyle {
match kind {
"info" => &self.info,
"warning" => &self.warning,
"caution" => &self.caution,
"danger" => &self.danger,
"success" => &self.success,
"notice" => &self.notice,
_ => &self.note,
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct PageLayoutStyle {
pub columns: u8,
pub gap: f32,
pub span_heading_through: u8,
}
impl Default for PageLayoutStyle {
fn default() -> Self {
Self {
columns: 1,
gap: 20.0,
span_heading_through: 1,
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct Style {
pub schema_version: u32,
pub page_width: f32,
pub page_height: f32,
pub margin_x: f32,
pub margin_y: f32,
pub page_layout: PageLayoutStyle,
pub pad_to_even: bool,
pub pad_to_multiple: u32,
pub body_font_size: f32,
pub body_line_height: f32,
pub paragraph_space_after: f32,
pub text_color: ColorRgb,
pub text_align: TextAlign,
pub link: LinkStyle,
pub heading: HeadingStyles,
pub heading_numbering: HeadingNumbering,
pub list_indent: f32,
pub list_item_space_after: f32,
pub list_marker_gap: f32,
pub list_marker: ListMarkerStyle,
pub blockquote_indent: f32,
pub blockquote_bar_width: f32,
pub blockquote_bar_color: ColorRgb,
pub blockquote_text_color: ColorRgb,
pub code_font_family: String,
pub code_font_size: f32,
pub code_padding: f32,
pub code_background: ColorRgb,
pub code_text_color: ColorRgb,
pub code_highlight: CodeHighlightStyle,
pub callout_padding: f32,
pub callout_accent_width: f32,
pub callout_styles: CalloutStyles,
pub callout_space_after: f32,
pub callout_label_size: f32,
pub callout_icon_size: f32,
pub callout_icon_gap: f32,
pub callout_rule_thickness: f32,
pub rule_color: ColorRgb,
pub rule_thickness: f32,
pub rule_space_around: f32,
pub toc: TocStyle,
pub lof: ListSectionStyle,
pub lot: ListSectionStyle,
pub pdf_export: PdfExportProfile,
pub page_decoration: PageDecorationStyle,
pub caption_position: CaptionPosition,
pub caption_color: Option<ColorRgb>,
pub figure_caption_prefix: String,
pub table_caption_prefix: String,
pub caption_separator: String,
pub image_title_fallback: bool,
pub table_column_sizing: TableColumnSizing,
pub table_column_weights: Vec<f32>,
pub table_borders: TableBorders,
pub table_cell_padding: f32,
pub table_border_color: ColorRgb,
pub table_border_thickness: f32,
pub table_edge_color: Option<ColorRgb>,
pub table_edge_thickness: Option<f32>,
pub table_header_background: ColorRgb,
pub table_header_text_color: ColorRgb,
pub table_stripe_color: Option<ColorRgb>,
pub table_header_column: bool,
pub table_space_after: f32,
pub footnote: FootnoteStyle,
pub watermark: Option<Watermark>,
pub font_paths: Vec<String>,
pub body_font_families: Vec<String>,
pub hyphenation: HyphenationStyle,
pub coverpage: CoverPageStyle,
pub frontmatter_tables: BTreeMap<String, FrontmatterTable>,
}
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FrontmatterTable {
pub key: String,
#[serde(default)]
pub heading: Option<String>,
pub columns: Vec<FrontmatterColumn>,
}
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FrontmatterColumn {
pub field: String,
pub header: String,
}
impl Default for Style {
fn default() -> Self {
Self {
schema_version: 1,
page_width: 595.0,
page_height: 842.0, margin_x: 72.0,
margin_y: 72.0,
page_layout: PageLayoutStyle::default(),
pad_to_even: false,
pad_to_multiple: 0,
text_align: TextAlign::default(),
body_font_size: 11.0,
body_line_height: 1.5,
paragraph_space_after: 8.0,
text_color: ColorRgb::new(20, 20, 20),
link: LinkStyle::default(),
heading: HeadingStyles::default(),
heading_numbering: HeadingNumbering::default(),
list_indent: 24.0,
list_item_space_after: 4.0,
list_marker_gap: 8.0,
list_marker: ListMarkerStyle::default(),
blockquote_indent: 24.0,
blockquote_bar_width: 3.0,
blockquote_bar_color: ColorRgb::new(200, 205, 215),
blockquote_text_color: ColorRgb::new(80, 90, 100),
code_font_family: "Noto Sans Mono".into(),
code_font_size: 10.0,
code_padding: 12.0,
code_background: ColorRgb::new(245, 246, 248),
code_text_color: ColorRgb::new(40, 50, 70),
code_highlight: CodeHighlightStyle::default(),
callout_padding: 12.0,
callout_accent_width: 4.0,
callout_styles: CalloutStyles::default(),
callout_space_after: 12.0,
callout_label_size: 11.0,
callout_icon_size: 20.0,
callout_icon_gap: 10.0,
callout_rule_thickness: 0.7,
rule_color: ColorRgb::new(200, 205, 215),
rule_thickness: 0.75,
rule_space_around: 12.0,
toc: TocStyle::default(),
lof: ListSectionStyle::default(),
lot: ListSectionStyle::default(),
pdf_export: PdfExportProfile::default(),
page_decoration: PageDecorationStyle::default(),
caption_position: CaptionPosition::Above,
caption_color: None,
figure_caption_prefix: "Figure".to_string(),
table_caption_prefix: "Table".to_string(),
caption_separator: ":".to_string(),
image_title_fallback: false,
table_column_sizing: TableColumnSizing::Auto,
table_column_weights: Vec::new(),
table_borders: TableBorders::Grid,
table_cell_padding: 6.0,
table_border_color: ColorRgb::new(210, 215, 225),
table_border_thickness: 0.5,
table_edge_color: None,
table_edge_thickness: None,
table_header_background: ColorRgb::new(240, 242, 246),
table_header_text_color: ColorRgb::new(20, 30, 50),
table_stripe_color: None,
table_header_column: false,
table_space_after: 12.0,
footnote: FootnoteStyle::default(),
watermark: None,
font_paths: Vec::new(),
body_font_families: Vec::new(),
hyphenation: HyphenationStyle::default(),
coverpage: CoverPageStyle::default(),
frontmatter_tables: BTreeMap::new(),
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct Watermark {
pub kind: WatermarkKind,
pub opacity: f32,
pub skip_first_page: bool,
}
impl Default for Watermark {
fn default() -> Self {
Self {
kind: WatermarkKind::Text(WatermarkText::default()),
opacity: 0.15,
skip_first_page: false,
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum WatermarkKind {
Image(WatermarkImage),
Text(WatermarkText),
}
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
pub struct WatermarkImage {
pub src: String,
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct WatermarkText {
pub text: String,
pub font_size: f32,
pub color: ColorRgb,
pub rotation_deg: f32,
pub font_families: Vec<String>,
}
impl Default for WatermarkText {
fn default() -> Self {
Self {
font_families: Vec::new(),
text: "DRAFT".to_string(),
font_size: 96.0,
color: ColorRgb::new(180, 180, 180),
rotation_deg: -30.0,
}
}
}
#[derive(Debug, Clone, Copy, Default, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum LogoPosition {
#[default]
Above,
BelowTitle,
}
#[derive(Debug, Clone, Copy, Default, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum CoverAlign {
#[default]
Center,
Left,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TextAlign {
#[default]
Left,
Justify,
Center,
Right,
}
impl TextAlign {
pub fn to_parley(self) -> parley::layout::Alignment {
use parley::layout::Alignment;
match self {
TextAlign::Left => Alignment::Start,
TextAlign::Justify => Alignment::Justify,
TextAlign::Center => Alignment::Center,
TextAlign::Right => Alignment::End,
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct CoverPageStyle {
pub enabled: bool,
pub margin_x: Option<f32>,
pub margin_y: Option<f32>,
pub logo: Option<LogoSpec>,
pub logo_position: LogoPosition,
pub hero: Option<LogoSpec>,
pub hero_gap: f32,
pub top_margin: f32,
pub logo_to_title_gap: f32,
pub title_font_size: f32,
pub title_accent: String,
pub title_accent_color: Option<ColorRgb>,
pub title_to_subtitle_gap: f32,
pub subtitle: String,
pub subtitle_font_size: f32,
pub subtitle_to_authors_gap: f32,
pub show_authors: bool,
pub authors_font_size: f32,
pub authors_to_date_gap: f32,
pub show_date: bool,
pub date_font_size: f32,
pub text_color: ColorRgb,
pub align: CoverAlign,
pub detail_lines: Vec<String>,
pub detail_font_size: f32,
pub title_to_detail_gap: f32,
pub detail_line_gap: f32,
pub detail_color: Option<ColorRgb>,
pub blank_page_after: bool,
pub font_families: Vec<String>,
}
impl Default for CoverPageStyle {
fn default() -> Self {
Self {
font_families: Vec::new(),
enabled: false,
margin_x: None,
margin_y: None,
logo: None,
logo_position: LogoPosition::Above,
hero: None,
hero_gap: 40.0,
top_margin: 200.0,
logo_to_title_gap: 32.0,
title_font_size: 32.0,
title_accent: String::new(),
title_accent_color: None,
title_to_subtitle_gap: 12.0,
subtitle: String::new(),
subtitle_font_size: 14.0,
subtitle_to_authors_gap: 24.0,
show_authors: true,
authors_font_size: 12.0,
authors_to_date_gap: 8.0,
show_date: true,
date_font_size: 11.0,
text_color: ColorRgb::new(20, 20, 20),
align: CoverAlign::Center,
detail_lines: Vec::new(),
detail_font_size: 11.0,
title_to_detail_gap: 14.0,
detail_line_gap: 3.0,
detail_color: None,
blank_page_after: false,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum MarkerSequence {
Decimal,
LowerAlpha,
UpperAlpha,
LowerRoman,
UpperRoman,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct ListMarkerStyle {
pub ordered_sequences: Vec<MarkerSequence>,
pub badge: bool,
pub badge_fill: ColorRgb,
pub badge_text_color: Option<ColorRgb>,
pub badge_scale: f32,
pub font_families: Vec<String>,
}
impl Default for ListMarkerStyle {
fn default() -> Self {
Self {
font_families: Vec::new(),
ordered_sequences: Vec::new(),
badge: false,
badge_fill: ColorRgb::new(223, 227, 232),
badge_text_color: None,
badge_scale: 1.7,
}
}
}
impl ListMarkerStyle {
pub fn sequence_for_depth(&self, depth: usize) -> MarkerSequence {
if self.ordered_sequences.is_empty() {
MarkerSequence::Decimal
} else {
self.ordered_sequences[depth % self.ordered_sequences.len()]
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct HyphenationStyle {
pub enabled: bool,
pub language: String,
pub min_word_chars: u8,
pub dictionary_path: Option<String>,
}
impl Default for HyphenationStyle {
fn default() -> Self {
Self {
enabled: false,
language: "en-us".to_string(),
min_word_chars: 5,
dictionary_path: None,
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct FootnoteStyle {
pub font_size: f32,
pub line_height: f32,
pub entry_space_after: f32,
pub gap_above: f32,
pub gap_below_rule: f32,
pub rule_width_frac: f32,
pub rule_thickness: f32,
pub rule_color: ColorRgb,
pub text_color: ColorRgb,
pub font_families: Vec<String>,
}
impl Default for FootnoteStyle {
fn default() -> Self {
Self {
font_families: Vec::new(),
font_size: 9.0,
line_height: 1.35,
entry_space_after: 3.0,
gap_above: 12.0,
gap_below_rule: 6.0,
rule_width_frac: 0.3,
rule_thickness: 0.5,
rule_color: ColorRgb::new(150, 155, 165),
text_color: ColorRgb::new(70, 80, 95),
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct TocStyle {
pub enabled: bool,
pub position: TocPosition,
pub title: String,
pub title_font_size: f32,
pub entry_font_size: f32,
pub entry_space_after: f32,
pub entry_indent_per_level: f32,
pub max_depth: u8,
pub font_families: Vec<String>,
}
impl Default for TocStyle {
fn default() -> Self {
Self {
font_families: Vec::new(),
enabled: false,
position: TocPosition::Start,
title: "Table of Contents".to_string(),
title_font_size: 24.0,
entry_font_size: 11.0,
entry_space_after: 4.0,
entry_indent_per_level: 16.0,
max_depth: 3,
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TocPosition {
#[default]
Start,
End,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CaptionPosition {
#[default]
Above,
Below,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TableColumnSizing {
#[default]
Auto,
Equal,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TableBorders {
#[default]
Grid,
Horizontal,
None,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct ListSectionStyle {
pub enabled: bool,
pub position: TocPosition,
pub title: Option<String>,
#[serde(default = "default_list_title_size")]
pub title_font_size: f32,
#[serde(default = "default_list_entry_size")]
pub entry_font_size: f32,
#[serde(default = "default_list_entry_space_after")]
pub entry_space_after: f32,
pub font_families: Vec<String>,
}
fn default_list_title_size() -> f32 {
24.0
}
fn default_list_entry_size() -> f32 {
11.0
}
fn default_list_entry_space_after() -> f32 {
4.0
}
impl ListSectionStyle {
pub fn resolved_title<'a>(&'a self, fallback: &'a str) -> &'a str {
self.title.as_deref().unwrap_or(fallback)
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PdfExportProfile {
#[default]
None,
A1B,
A2B,
A3B,
A4,
UA1,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct PageDecorationStyle {
pub header: Option<HeaderFooterStyle>,
pub footer: Option<HeaderFooterStyle>,
pub banner: Option<NoticeBanner>,
pub skip_first_page: bool,
pub last_page_qr: Option<LastPageQr>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct LastPageQr {
pub value: String,
pub label: String,
pub size: f32,
pub ecl: String,
pub align: String,
pub margin_left: f32,
pub margin_right: f32,
pub margin_bottom: f32,
pub color: ColorRgb,
pub label_font_size: f32,
pub include_padding_page: bool,
pub font_families: Vec<String>,
}
impl Default for LastPageQr {
fn default() -> Self {
Self {
font_families: Vec::new(),
value: "{documentNumber}".to_string(),
label: "{documentNumber}".to_string(),
size: 54.0,
ecl: "medium".to_string(),
align: "right".to_string(),
margin_left: 40.0,
margin_right: 40.0,
margin_bottom: 48.0,
color: ColorRgb::new(0, 0, 0),
label_font_size: 8.0,
include_padding_page: false,
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct NoticeBanner {
pub height: f32,
pub logo: Option<LogoSpec>,
pub logo_subtitle: String,
pub logo_subtitle_color: ColorRgb,
pub logo_subtitle_font_size: f32,
pub disclaimer: String,
pub disclaimer_color: ColorRgb,
pub disclaimer_font_size: f32,
pub disclaimer_max_lines: u8,
pub icon: Option<LogoSpec>,
pub label: String,
pub label_color: ColorRgb,
pub label_font_size: f32,
pub note: String,
pub note_color: ColorRgb,
pub note_font_size: f32,
pub rule: bool,
pub rule_color: ColorRgb,
pub rule_thickness: f32,
pub font_families: Vec<String>,
}
impl Default for NoticeBanner {
fn default() -> Self {
Self {
font_families: Vec::new(),
height: 100.0,
logo: None,
logo_subtitle: String::new(),
logo_subtitle_color: ColorRgb::new(140, 145, 150),
logo_subtitle_font_size: 6.0,
disclaimer: String::new(),
disclaimer_color: ColorRgb::new(150, 155, 160),
disclaimer_font_size: 8.0,
disclaimer_max_lines: 3,
icon: None,
label: String::new(),
label_color: ColorRgb::new(20, 20, 20),
label_font_size: 11.0,
note: String::new(),
note_color: ColorRgb::new(60, 60, 60),
note_font_size: 8.5,
rule: true,
rule_color: ColorRgb::new(180, 185, 190),
rule_thickness: 0.6,
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct HeaderFooterStyle {
pub left: String,
pub center: String,
pub right: String,
pub font_size: f32,
pub color: ColorRgb,
pub margin_from_edge: f32,
pub rule: bool,
pub rule_color: ColorRgb,
pub rule_thickness: f32,
pub rule_gap: f32,
pub max_lines: u8,
pub logo_left: Option<LogoSpec>,
pub logo_center: Option<LogoSpec>,
pub logo_right: Option<LogoSpec>,
pub even: Option<HeaderFooterSlots>,
pub per_chapter: HashMap<String, HeaderFooterSlots>,
pub font_families: Vec<String>,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct HeaderFooterSlots {
pub left: String,
pub center: String,
pub right: String,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct LogoSpec {
pub id: String,
pub src: String,
pub width: f32,
pub height: f32,
pub gap: f32,
}
impl Default for LogoSpec {
fn default() -> Self {
Self {
id: String::new(),
src: String::new(),
width: 0.0,
height: 0.0,
gap: 6.0,
}
}
}
impl Default for HeaderFooterStyle {
fn default() -> Self {
Self {
font_families: Vec::new(),
left: String::new(),
center: String::new(),
right: String::new(),
font_size: 9.0,
color: ColorRgb::new(110, 120, 130),
margin_from_edge: 36.0,
rule: false,
rule_color: ColorRgb::new(220, 225, 230),
rule_thickness: 0.5,
rule_gap: 4.0,
max_lines: 1,
logo_left: None,
logo_center: None,
logo_right: None,
even: None,
per_chapter: HashMap::new(),
}
}
}
impl HeaderFooterStyle {
pub fn resolved_slots(&self, page_number: usize, chapter: &str) -> (String, String, String) {
let mut left = self.left.clone();
let mut center = self.center.clone();
let mut right = self.right.clone();
if page_number.is_multiple_of(2)
&& let Some(e) = &self.even
{
if !e.left.is_empty() {
left = e.left.clone();
}
if !e.center.is_empty() {
center = e.center.clone();
}
if !e.right.is_empty() {
right = e.right.clone();
}
}
if !chapter.is_empty()
&& let Some(c) = self.per_chapter.get(chapter)
{
if !c.left.is_empty() {
left = c.left.clone();
}
if !c.center.is_empty() {
center = c.center.clone();
}
if !c.right.is_empty() {
right = c.right.clone();
}
}
(left, center, right)
}
}
impl HeaderFooterStyle {
pub fn reserved_height(&self) -> f32 {
let lines = self.max_lines.max(1) as f32;
let text = self.font_size * 1.2 * lines;
let rule = if self.rule {
self.rule_gap + self.rule_thickness
} else {
0.0
};
text + rule
}
}
impl Style {
pub fn from_toml_str(s: &str) -> Result<Self, toml::de::Error> {
toml::from_str(s)
}
pub fn from_toml_file(path: impl AsRef<std::path::Path>) -> Result<Self, StyleLoadError> {
let text = std::fs::read_to_string(path).map_err(StyleLoadError::Io)?;
Self::from_toml_str(&text).map_err(StyleLoadError::Toml)
}
pub fn padding_multiple(&self) -> usize {
if self.pad_to_multiple > 1 {
self.pad_to_multiple as usize
} else if self.pad_to_even {
2
} else {
0
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum StyleLoadError {
#[error("failed to read style file: {0}")]
Io(std::io::Error),
#[error("failed to parse style TOML: {0}")]
Toml(toml::de::Error),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_loads() {
let s = Style::default();
assert_eq!(s.page_width, 595.0);
assert_eq!(s.heading.h1.font_size, 26.0);
}
#[test]
fn frontmatter_tables_are_none_unless_a_style_names_them() {
assert!(Style::default().frontmatter_tables.is_empty());
assert!(
Style::from_toml_str("page_width = 612.0\n")
.unwrap()
.frontmatter_tables
.is_empty()
);
}
#[test]
fn frontmatter_tables_load_by_tag_name() {
let s = Style::from_toml_str(
r#"
[frontmatter_tables.changes]
key = "changeLog"
heading = "Changes"
columns = [{ field = "when", header = "When" }, { field = "what", header = "What" }]
[frontmatter_tables.people]
key = "contributors"
columns = [{ field = "name", header = "Name" }]
"#,
)
.unwrap();
assert_eq!(
s.frontmatter_tables["changes"],
FrontmatterTable {
key: "changeLog".into(),
heading: Some("Changes".into()),
columns: vec![
FrontmatterColumn {
field: "when".into(),
header: "When".into(),
},
FrontmatterColumn {
field: "what".into(),
header: "What".into(),
},
],
}
);
assert_eq!(s.frontmatter_tables["people"].heading, None);
assert_eq!(s.frontmatter_tables.len(), 2);
}
#[test]
fn a_frontmatter_table_needs_its_key_and_columns_and_nothing_else() {
for toml in [
"[frontmatter_tables.t]\ncolumns = []\n",
"[frontmatter_tables.t]\nkey = \"k\"\n",
"[frontmatter_tables.t]\nkey = \"k\"\ncolumns = []\nlevel = 3\n",
"[frontmatter_tables.t]\nkey = \"k\"\ncolumns = [{ field = \"f\" }]\n",
] {
assert!(Style::from_toml_str(toml).is_err(), "{toml}");
}
}
#[test]
fn partial_toml_overrides_only_specified_fields() {
let toml = r#"
page_width = 612.0
page_height = 792.0
margin_x = 54.0
"#;
let s = Style::from_toml_str(toml).unwrap();
assert_eq!(s.page_width, 612.0);
assert_eq!(s.page_height, 792.0);
assert_eq!(s.margin_x, 54.0);
assert_eq!(s.body_font_size, 11.0);
assert_eq!(s.heading.h1.font_size, 26.0);
assert_eq!(
s.callout_styles.warning.background,
ColorRgb::new(255, 247, 230)
);
}
#[test]
fn nested_overrides_work() {
let toml = r#"
[heading.h1]
font_size = 32.0
"#;
let s = Style::from_toml_str(toml).unwrap();
assert_eq!(s.heading.h1.font_size, 32.0);
assert_eq!(s.heading.h2.font_size, 21.0);
}
#[test]
fn table_borders_default_and_override() {
assert_eq!(Style::default().table_borders, TableBorders::Grid);
let horizontal = Style::from_toml_str("table_borders = \"horizontal\"").unwrap();
assert_eq!(horizontal.table_borders, TableBorders::Horizontal);
let none = Style::from_toml_str("table_borders = \"none\"").unwrap();
assert_eq!(none.table_borders, TableBorders::None);
}
#[test]
fn text_align_defaults_left_and_parses() {
assert_eq!(Style::default().text_align, TextAlign::Left);
assert_eq!(
Style::from_toml_str("text_align = \"justify\"")
.unwrap()
.text_align,
TextAlign::Justify
);
assert_eq!(
TextAlign::Justify.to_parley(),
parley::layout::Alignment::Justify
);
}
#[test]
fn table_edge_color_optional() {
let d = Style::default();
assert!(d.table_edge_color.is_none());
assert!(d.table_edge_thickness.is_none());
let s = Style::from_toml_str("table_edge_color = [0, 0, 0]\ntable_edge_thickness = 1.0")
.unwrap();
assert_eq!(s.table_edge_color, Some(ColorRgb::new(0, 0, 0)));
assert_eq!(s.table_edge_thickness, Some(1.0));
}
#[test]
fn list_marker_badge_and_sequences() {
let d = Style::default().list_marker;
assert!(!d.badge);
assert_eq!(d.sequence_for_depth(0), MarkerSequence::Decimal);
assert_eq!(d.sequence_for_depth(3), MarkerSequence::Decimal);
let toml = r#"
[list_marker]
badge = true
ordered_sequences = ["decimal", "lower-alpha", "lower-roman"]
badge_fill = [223, 227, 232]
"#;
let lm = Style::from_toml_str(toml).unwrap().list_marker;
assert!(lm.badge);
assert_eq!(lm.badge_fill, ColorRgb::new(223, 227, 232));
assert_eq!(lm.sequence_for_depth(0), MarkerSequence::Decimal);
assert_eq!(lm.sequence_for_depth(1), MarkerSequence::LowerAlpha);
assert_eq!(lm.sequence_for_depth(2), MarkerSequence::LowerRoman);
assert_eq!(lm.sequence_for_depth(3), MarkerSequence::Decimal);
}
#[test]
fn callout_kind_lookup() {
let s = Style::default();
assert_eq!(
s.callout_styles.for_kind("warning").background,
ColorRgb::new(255, 247, 230)
);
assert_eq!(
s.callout_styles.for_kind("info").background,
ColorRgb::new(232, 244, 253)
);
assert_eq!(
s.callout_styles.for_kind("unknown").background,
s.callout_styles.note.background
);
}
#[test]
fn coverpage_hero_deserializes_without_explicit_size() {
let toml = r#"
[coverpage.hero]
id = "{coverImage}"
src = "{title}.png"
"#;
let s = Style::from_toml_str(toml).unwrap();
let hero = s.coverpage.hero.expect("hero slot");
assert_eq!(hero.id, "{coverImage}");
assert_eq!(hero.src, "{title}.png");
assert_eq!(hero.width, 0.0);
assert_eq!(hero.height, 0.0);
}
}