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 Feb 4, 2024
1 parent 1fb6611 commit e089856
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 @@ -34,6 +34,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 @@ -496,10 +497,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 @@ -650,6 +654,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 @@ -750,16 +756,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 @@ -292,6 +292,7 @@ impl TextBox {
}
}

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

/// Render selected text
pub fn render_selection(
&self,
text_system: &mut TextSystem,
Expand Down Expand Up @@ -484,6 +486,7 @@ impl TextBox {
}
}

/// Represents a slice of text
#[derive(Clone)]
struct ThinLine {
range: Range<usize>,
Expand All @@ -494,6 +497,8 @@ struct ThinLine {
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 @@ -502,6 +507,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 @@ -516,7 +522,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 @@ -531,6 +539,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 @@ -565,6 +578,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 @@ -589,6 +606,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 @@ -600,6 +624,7 @@ impl Text {
content: line,
font,
color,
bg_color,
index,
})
.collect()
Expand All @@ -618,6 +643,7 @@ pub struct SectionKey<'a> {
content: &'a str,
font: Font<'a>,
color: Color,
bg_color: Color,
index: usize,
}

Expand Down

0 comments on commit e089856

Please sign in to comment.