From 4c46a0437e4434c78a2f5703009b4745a2da25c7 Mon Sep 17 00:00:00 2001 From: Valentin271 Date: Thu, 25 Jan 2024 16:35:20 +0100 Subject: [PATCH] feat: support bg color in code blocks --- src/debug_impls.rs | 8 ++++++++ src/interpreter/mod.rs | 23 ++++++++++++----------- src/text.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/debug_impls.rs b/src/debug_impls.rs index aeaf7009..c93f293a 100644 --- a/src/debug_impls.rs +++ b/src/debug_impls.rs @@ -127,6 +127,7 @@ pub fn text(text: &Text, f: &mut fmt::Formatter<'_>) -> fmt::Result { let Text { text, color, + bg_color, link, is_bold, is_italic, @@ -136,6 +137,7 @@ pub fn text(text: &Text, f: &mut fmt::Formatter<'_>) -> fmt::Result { // Globally consistent so avoid displaying as noise hidpi_scale: _, default_color, + default_bg_color, } = text; let mut debug = f.debug_struct("Text"); @@ -153,6 +155,12 @@ pub fn text(text: &Text, f: &mut fmt::Formatter<'_>) -> fmt::Result { let color = color.map(DebugF32Color); debug.field("color", &DebugInline(&color)); } + if bg_color.is_none() { + debug.field("default_bg_color", &DebugF32Color(*default_bg_color)); + } else { + let bg = bg_color.map(DebugF32Color); + debug.field("bg_color", &DebugInline(&bg)); + } let style = StyleWrapper { is_bold: *is_bold, is_italic: *is_italic, diff --git a/src/interpreter/mod.rs b/src/interpreter/mod.rs index e435a836..06199650 100644 --- a/src/interpreter/mod.rs +++ b/src/interpreter/mod.rs @@ -33,6 +33,7 @@ struct State { element_stack: Vec, text_options: html::TextOptions, span_color: [f32; 4], + span_bg: [f32; 4], span_weight: FontWeight, span_style: FontStyle, span_decor: TextDecoration, @@ -492,10 +493,13 @@ impl TokenSink for HtmlInterpreter { self.state.span_color = native_color(color, &self.surface_format) } + Style::BackgroundColor(color) => { + self.state.span_bg = + native_color(color, &self.surface_format) + } Style::FontWeight(weight) => self.state.span_weight = weight, Style::FontStyle(style) => self.state.span_style = style, Style::TextDecoration(decor) => self.state.span_decor = decor, - _ => {} } } } @@ -648,6 +652,8 @@ impl TokenSink for HtmlInterpreter { "span" => { self.state.span_color = native_color(self.theme.code_color, &self.surface_format); + self.state.span_bg = + native_color(self.theme.code_color, &self.surface_format); self.state.span_weight = FontWeight::default(); self.state.span_style = FontStyle::default(); self.state.span_decor = TextDecoration::default(); @@ -748,16 +754,11 @@ impl TokenSink for HtmlInterpreter { if self.state.text_options.code >= 1 { text = text .with_color(self.state.span_color) - .with_family(FamilyOwned::Monospace); - if self.state.span_weight == FontWeight::Bold { - text = text.make_bold(true); - } - if self.state.span_style == FontStyle::Italic { - text = text.make_italic(true); - } - if self.state.span_decor == TextDecoration::Underline { - text = text.make_underlined(true); - } + .with_bg_color(self.state.span_bg) + .with_family(FamilyOwned::Monospace) + .make_bold(self.state.span_weight == FontWeight::Bold) + .make_italic(self.state.span_style == FontStyle::Italic) + .make_underlined(self.state.span_decor == TextDecoration::Underline); //.with_size(18.) } for elem in self.state.element_stack.iter().rev() { diff --git a/src/text.rs b/src/text.rs index 1caf0e9a..f2964869 100644 --- a/src/text.rs +++ b/src/text.rs @@ -290,6 +290,7 @@ impl TextBox { } } + /// Render underlines and strikethrough pub fn render_lines( &self, text_system: &mut TextSystem, @@ -370,6 +371,7 @@ impl TextBox { lines } + /// Render selected text pub fn render_selection( &self, text_system: &mut TextSystem, @@ -455,10 +457,13 @@ impl TextBox { } } +/// Represents a slice of text #[derive(Clone)] pub struct Text { pub text: String, pub color: Option<[f32; 4]>, + /// Background color of this slice of text + pub bg_color: Option<[f32; 4]>, pub link: Option, pub is_bold: bool, pub is_italic: bool, @@ -467,6 +472,7 @@ pub struct Text { pub font_family: FamilyOwned, pub hidpi_scale: f32, pub default_color: [f32; 4], + pub default_bg_color: [f32; 4], } impl fmt::Debug for Text { @@ -481,7 +487,9 @@ impl Text { text, hidpi_scale, default_color: default_text_color, + default_bg_color: [0., 0., 0., 1.], color: None, + bg_color: None, link: None, is_bold: false, is_italic: false, @@ -496,6 +504,11 @@ impl Text { self } + pub fn with_bg_color(mut self, color: [f32; 4]) -> Self { + self.bg_color = Some(color); + self + } + pub fn with_link(mut self, link: String) -> Self { self.link = Some(link); self @@ -530,6 +543,10 @@ impl Text { self.color.unwrap_or(self.default_color) } + fn bg_color(&self) -> [f32; 4] { + self.bg_color.unwrap_or(self.default_bg_color) + } + fn style(&self) -> Style { if self.is_italic { Style::Italic @@ -554,6 +571,13 @@ impl Text { (color[2] * 255.) as u8, (color[3] * 255.) as u8, ); + let bg_color = self.bg_color(); + let bg_color = Color::rgba( + (bg_color[0] * 255.) as u8, + (bg_color[1] * 255.) as u8, + (bg_color[2] * 255.) as u8, + (bg_color[3] * 255.) as u8, + ); let font = Font { family: self.font_family.as_family(), weight: self.weight(), @@ -565,6 +589,7 @@ impl Text { content: line, font, color, + bg_color, index, }) .collect() @@ -583,6 +608,7 @@ pub struct SectionKey<'a> { content: &'a str, font: Font<'a>, color: Color, + bg_color: Color, index: usize, }