Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support bg color in code blocks #223

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
29 changes: 28 additions & 1 deletion src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::borrow::Cow;
use std::sync::{Arc, Mutex};

use crate::color::{native_color, Theme};
use crate::debug_impls::DebugF32Color;
use crate::fonts::get_fonts;
use crate::image::ImageRenderer;
use crate::opts::FontOptions;
Expand Down Expand Up @@ -270,7 +271,33 @@ impl Renderer {
min.0 -= (nest - 1) as f32 * DEFAULT_MARGIN / 2.;
}
if min.0 < screen_size.0 - DEFAULT_MARGIN - centering {
self.draw_rectangle(Rect::from_min_max(min, max), color)?;
let rect = Rect::from_min_max(min, max);
let line_height = text_box.line_height(self.zoom);
self.draw_rectangle(rect.clone(), color)?;

tracing::debug!("Inner texts: {:#?}", &text_box.texts);
let mut line = 0;
for text in &text_box.texts {
if text.text == "\n" {
line += 1;
}
if let Some(bg_color) = text.bg_color {
tracing::debug!(
"Rendering BG {:#?} for line {line}",
DebugF32Color(bg_color)
);
self.draw_rectangle(
Rect {
pos: (
rect.pos.0,
rect.pos.1 + (line_height * line as f32),
),
size: (rect.size.0, line_height),
},
bg_color,
)?;
}
}
}
}
if let Some(nest) = text_box.is_quote_block {
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