From 8f4c2ad520cae9ed8e9f251afbd96325200d8aa5 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Mon, 12 Feb 2024 13:37:40 -0800 Subject: [PATCH] Allow customizing slide title style --- src/processing/builder.rs | 12 +++++++- src/style.rs | 59 +++++++++++++++++++++--------------- src/theme.rs | 12 ++++++++ themes/catppuccin-mocha.yaml | 1 + themes/dark.yaml | 1 + themes/light.yaml | 1 + themes/terminal-dark.yaml | 1 + themes/terminal-light.yaml | 1 + themes/tokyonight-storm.yaml | 1 + 9 files changed, 63 insertions(+), 26 deletions(-) diff --git a/src/processing/builder.rs b/src/processing/builder.rs index 34c5dda6..f174f2b5 100644 --- a/src/processing/builder.rs +++ b/src/processing/builder.rs @@ -443,7 +443,17 @@ impl<'a> PresentationBuilder<'a> { } let style = self.theme.slide_title.clone(); - text.apply_style(&TextStyle::default().bold().colors(style.colors.clone())); + let mut text_style = TextStyle::default().colors(style.colors.clone()); + if style.bold { + text_style = text_style.bold(); + } + if style.italics { + text_style = text_style.italics(); + } + if style.underlined { + text_style = text_style.underlined(); + } + text.apply_style(&text_style); for _ in 0..style.padding_top.unwrap_or(0) { self.push_line_break(); diff --git a/src/style.rs b/src/style.rs index 9fead818..47e17fe5 100644 --- a/src/style.rs +++ b/src/style.rs @@ -16,33 +16,33 @@ pub(crate) struct TextStyle { impl TextStyle { /// Add bold to this style. - pub(crate) fn bold(mut self) -> Self { - self.flags |= TextFormatFlags::Bold as u8; - self + pub(crate) fn bold(self) -> Self { + self.add_flag(TextFormatFlags::Bold) } /// Add italics to this style. - pub(crate) fn italics(mut self) -> Self { - self.flags |= TextFormatFlags::Italics as u8; - self + pub(crate) fn italics(self) -> Self { + self.add_flag(TextFormatFlags::Italics) } /// Indicate this text is a piece of inline code. - pub(crate) fn code(mut self) -> Self { - self.flags |= TextFormatFlags::Code as u8; - self + pub(crate) fn code(self) -> Self { + self.add_flag(TextFormatFlags::Code) } /// Add strikethrough to this style. - pub(crate) fn strikethrough(mut self) -> Self { - self.flags |= TextFormatFlags::Strikethrough as u8; - self + pub(crate) fn strikethrough(self) -> Self { + self.add_flag(TextFormatFlags::Strikethrough) + } + + /// Add underline to this style. + pub(crate) fn underlined(self) -> Self { + self.add_flag(TextFormatFlags::Underlined) } /// Indicate this is a link. - pub(crate) fn link(mut self) -> Self { - self.flags |= TextFormatFlags::Link as u8; - self + pub(crate) fn link(self) -> Self { + self.italics().underlined() } /// Set the colors for this text style. @@ -53,27 +53,27 @@ impl TextStyle { /// Check whether this text style is bold. pub(crate) fn is_bold(&self) -> bool { - self.flags & TextFormatFlags::Bold as u8 != 0 + self.has_flag(TextFormatFlags::Bold) } /// Check whether this text style has italics. pub(crate) fn is_italics(&self) -> bool { - self.flags & TextFormatFlags::Italics as u8 != 0 + self.has_flag(TextFormatFlags::Italics) } /// Check whether this text is code. pub(crate) fn is_code(&self) -> bool { - self.flags & TextFormatFlags::Code as u8 != 0 + self.has_flag(TextFormatFlags::Code) } /// Check whether this text style is strikethrough. pub(crate) fn is_strikethrough(&self) -> bool { - self.flags & TextFormatFlags::Strikethrough as u8 != 0 + self.has_flag(TextFormatFlags::Strikethrough) } - /// Check whether this text is a link. - pub(crate) fn is_link(&self) -> bool { - self.flags & TextFormatFlags::Link as u8 != 0 + /// Check whether this text style is underlined. + pub(crate) fn is_underlined(&self) -> bool { + self.has_flag(TextFormatFlags::Underlined) } /// Merge this style with another one. @@ -96,8 +96,8 @@ impl TextStyle { if self.is_strikethrough() { styled = styled.crossed_out(); } - if self.is_link() { - styled = styled.italic().underlined(); + if self.is_underlined() { + styled = styled.underlined(); } if let Some(color) = self.colors.background { styled = styled.on(color.into()); @@ -107,6 +107,15 @@ impl TextStyle { } styled } + + fn add_flag(mut self, flag: TextFormatFlags) -> Self { + self.flags |= flag as u8; + self + } + + fn has_flag(&self, flag: TextFormatFlags) -> bool { + self.flags & flag as u8 != 0 + } } #[derive(Debug)] @@ -115,7 +124,7 @@ enum TextFormatFlags { Italics = 2, Code = 4, Strikethrough = 8, - Link = 16, + Underlined = 16, } #[derive(Debug, Copy, Clone, PartialEq, Eq, SerializeDisplay, DeserializeFromStr)] diff --git a/src/theme.rs b/src/theme.rs index 03be7e64..e2f3514b 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -163,6 +163,18 @@ pub(crate) struct SlideTitleStyle { /// The colors to be used. #[serde(default)] pub(crate) colors: Colors, + + /// Whether to use bold font for slide titles. + #[serde(default)] + pub(crate) bold: bool, + + /// Whether to use italics font for slide titles. + #[serde(default)] + pub(crate) italics: bool, + + /// Whether to use underlined font for slide titles. + #[serde(default)] + pub(crate) underlined: bool, } /// The style for all headings. diff --git a/themes/catppuccin-mocha.yaml b/themes/catppuccin-mocha.yaml index c9f804b8..5db34466 100644 --- a/themes/catppuccin-mocha.yaml +++ b/themes/catppuccin-mocha.yaml @@ -11,6 +11,7 @@ slide_title: padding_top: 1 colors: foreground: "f9e2af" + bold: true code: alignment: center diff --git a/themes/dark.yaml b/themes/dark.yaml index 86538c68..2c7aba16 100644 --- a/themes/dark.yaml +++ b/themes/dark.yaml @@ -11,6 +11,7 @@ slide_title: padding_top: 1 colors: foreground: "ee9322" + bold: true code: alignment: center diff --git a/themes/light.yaml b/themes/light.yaml index d8e07931..6841dd5a 100644 --- a/themes/light.yaml +++ b/themes/light.yaml @@ -11,6 +11,7 @@ slide_title: padding_top: 1 colors: foreground: "f77f00" + bold: true code: alignment: center diff --git a/themes/terminal-dark.yaml b/themes/terminal-dark.yaml index 5281b0d2..246f561a 100644 --- a/themes/terminal-dark.yaml +++ b/themes/terminal-dark.yaml @@ -11,6 +11,7 @@ slide_title: padding_top: 1 colors: foreground: yellow + bold: true code: alignment: center diff --git a/themes/terminal-light.yaml b/themes/terminal-light.yaml index c08b113e..a27aec6d 100644 --- a/themes/terminal-light.yaml +++ b/themes/terminal-light.yaml @@ -11,6 +11,7 @@ slide_title: padding_top: 1 colors: foreground: dark_yellow + bold: true code: alignment: center diff --git a/themes/tokyonight-storm.yaml b/themes/tokyonight-storm.yaml index 96e95c1a..b92575e1 100644 --- a/themes/tokyonight-storm.yaml +++ b/themes/tokyonight-storm.yaml @@ -11,6 +11,7 @@ slide_title: padding_top: 1 colors: foreground: "e0af68" + bold: true code: alignment: center