Skip to content

Commit

Permalink
feat: support bg color in code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin271 committed Jan 25, 2024
1 parent b4b0a36 commit 4c46a04
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/debug_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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");
Expand All @@ -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,
Expand Down
23 changes: 12 additions & 11 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct State {
element_stack: Vec<html::Element>,
text_options: html::TextOptions,
span_color: [f32; 4],
span_bg: [f32; 4],
span_weight: FontWeight,
span_style: FontStyle,
span_decor: TextDecoration,
Expand Down Expand Up @@ -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,
_ => {}
}
}
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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() {
Expand Down
26 changes: 26 additions & 0 deletions src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ impl TextBox {
}
}

/// Render underlines and strikethrough
pub fn render_lines(
&self,
text_system: &mut TextSystem,
Expand Down Expand Up @@ -370,6 +371,7 @@ impl TextBox {
lines
}

/// Render selected text
pub fn render_selection(
&self,
text_system: &mut TextSystem,
Expand Down Expand Up @@ -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<String>,
pub is_bold: bool,
pub is_italic: bool,
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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(),
Expand All @@ -565,6 +589,7 @@ impl Text {
content: line,
font,
color,
bg_color,
index,
})
.collect()
Expand All @@ -583,6 +608,7 @@ pub struct SectionKey<'a> {
content: &'a str,
font: Font<'a>,
color: Color,
bg_color: Color,
index: usize,
}

Expand Down

0 comments on commit 4c46a04

Please sign in to comment.