From 7e8c1c916f6cd30de619d738a8d463f5f82524c3 Mon Sep 17 00:00:00 2001 From: James Brumond Date: Sun, 22 Jun 2025 13:42:26 -0700 Subject: [PATCH] add additional settings to control the fonts used for markdown rendering --- widget/src/markdown.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/widget/src/markdown.rs b/widget/src/markdown.rs index 3d6cd056..535733c5 100644 --- a/widget/src/markdown.rs +++ b/widget/src/markdown.rs @@ -313,13 +313,14 @@ impl Span { let span = span(text.clone()).strikethrough(*strikethrough); let span = if *code { - span.font(Font::MONOSPACE) + span.font(style.inline_code_font) .color(style.inline_code_color) .background(style.inline_code_highlight.background) .border(style.inline_code_highlight.border) .padding(style.inline_code_padding) } else if *strong || *emphasis { span.font(Font { + family: style.text_font_family, weight: if *strong { font::Weight::Bold } else { @@ -333,7 +334,10 @@ impl Span { ..Font::default() }) } else { - span + span.font(Font { + family: style.text_font_family, + ..Font::default() + }) }; if let Some(link) = link.as_ref() { @@ -1046,12 +1050,18 @@ impl From for Settings { /// The text styling of some Markdown rendering in [`view`]. #[derive(Debug, Clone, Copy, PartialEq)] pub struct Style { + /// The [`font::Family`] to be applied to non-code text. + pub text_font_family: font::Family, /// The [`Highlight`] to be applied to the background of inline code. pub inline_code_highlight: Highlight, /// The [`Padding`] to be applied to the background of inline code. pub inline_code_padding: Padding, /// The [`Color`] to be applied to inline code. pub inline_code_color: Color, + /// The [`Font`] to be applied to inline code. + pub inline_code_font: Font, + /// The [`Font`] to be applied to code blocks. + pub code_block_font: Font, /// The [`Color`] to be applied to links. pub link_color: Color, } @@ -1060,12 +1070,15 @@ impl Style { /// Creates a new [`Style`] from the given [`theme::Palette`]. pub fn from_palette(palette: theme::Palette) -> Self { Self { + text_font_family: font::Family::default(), inline_code_padding: padding::left(1).right(1), inline_code_highlight: Highlight { background: color!(0x111111).into(), border: border::rounded(4), }, inline_code_color: Color::WHITE, + inline_code_font: Font::MONOSPACE, + code_block_font: Font::MONOSPACE, link_color: palette.primary, } } @@ -1351,7 +1364,7 @@ where container(column(lines.iter().map(|line| { rich_text(line.spans(settings.style)) .on_link_click(on_link_click.clone()) - .font(Font::MONOSPACE) + .font(settings.style.code_block_font) .size(settings.code_size) .into() })))