From e56b57931883f6b7dfca8dbdae1a61ec1064c420 Mon Sep 17 00:00:00 2001 From: shawn Date: Wed, 2 Dec 2026 18:11:11 +0800 Subject: [PATCH 1/2] fix: unify heading font weight --- src/font.rs | 156 +++++++++++++++++--------------------------------- src/render.rs | 2 +- 2 files changed, 54 insertions(+), 104 deletions(-) diff --git a/src/font.rs b/src/font.rs index 0e63372..fc9c0cb 100644 --- a/src/font.rs +++ b/src/font.rs @@ -1,4 +1,3 @@ -use std::cell::OnceCell; use std::collections::HashMap; use std::fs; use std::sync::{Mutex, OnceLock}; @@ -199,14 +198,6 @@ impl FontSet { // ─── Font Loading ─────────────────────────────────────────────────────────── -fn weight_for_level(level: u8) -> Weight { - match level { - 1 | 2 => Weight::BLACK, - 3 => Weight::EXTRA_BOLD, - _ => Weight::BOLD, - } -} - static FONT_DATA_CACHE: OnceLock>> = OnceLock::new(); fn load_from_handle(handle: &Handle) -> Option> { @@ -249,30 +240,19 @@ fn try_load(source: &SystemSource, family: &str, props: &Properties) -> Option Option> { - // For bold weights, try explicit variant family names first - let suffixes: &[&str] = if props.weight.0 >= Weight::BLACK.0 { - &["Black", "Heavy"] - } else if props.weight.0 >= Weight::EXTRA_BOLD.0 { - &["ExtraBold", "Heavy", "Bold"] - } else if props.weight.0 >= Weight::BOLD.0 { - &["Bold", "Demi Bold"] - } else { - &[] - }; - - let normal_props = Properties { - weight: Weight::NORMAL, - ..*props - }; - - for suffix in suffixes { - let variant = format!("{family} {suffix}"); - if let Some(font) = try_load(source, &variant, &normal_props) { - return Some(font); + if props.weight == Weight::BOLD { + let normal_props = Properties { + weight: Weight::NORMAL, + ..*props + }; + for suffix in ["Bold", "Demi Bold"] { + let variant = format!("{family} {suffix}"); + if let Some(font) = try_load(source, &variant, &normal_props) { + return Some(font); + } } } @@ -323,41 +303,13 @@ fn resolve_optional_font( None } -/// Per-level cache of resolved font sets. `Config` is loaded once at startup -/// and is constant for the process lifetime, so a level → FontSet mapping is -/// safe to memoize. `SystemSource::new()` and the family-resolution walk are -/// the dominant cost in heading rasterization (~30–40ms each on macOS), and -/// without this cache they were re-paid for every H1–H3 in the document. -static FONT_SETS: [OnceLock>; 6] = [ - OnceLock::new(), - OnceLock::new(), - OnceLock::new(), - OnceLock::new(), - OnceLock::new(), - OnceLock::new(), -]; - -/// Resolve a Latin + CJK + optional emoji font set for the given heading level. -/// Cached per-level for the process lifetime — the first call's `config` wins; -/// subsequent calls with a different `config` return the originally-resolved -/// fonts. Safe today because `Config` is loaded once at startup, but callers -/// shouldn't rely on per-call config. -pub fn get_fonts(level: u8, config: &Config) -> Option<&'static FontSet> { - let idx = level.saturating_sub(1).min(5) as usize; - FONT_SETS[idx] - .get_or_init(|| resolve_font_set(level, config)) - .as_ref() -} +static HEADING_FONTS: OnceLock> = OnceLock::new(); -// Shared across heading levels within a thread. `SystemSource::new()` walks -// the OS font registry (CoreText / fontconfig / DirectWrite), ~20-30ms per -// call, and we'd otherwise pay it once per heading level. Thread-local -// instead of `static` because on Linux `SystemSource` wraps a raw -// `*mut FcConfig` pointer and is neither `Send` nor `Sync`. The value is -// `Option` because on Linux fontconfig may be unavailable at runtime (see -// `new_system_source`), in which case we render with the bundled font only. -thread_local! { - static SYSTEM_SOURCE: OnceCell> = const { OnceCell::new() }; +/// Resolve the shared heading fonts once using the startup configuration. +pub fn get_fonts(config: &Config) -> Option<&'static FontSet> { + HEADING_FONTS + .get_or_init(|| resolve_font_set(config)) + .as_ref() } /// Build the system font source, returning `None` when it can't be used. @@ -388,43 +340,41 @@ fn fontconfig_available() -> bool { false } -fn resolve_font_set(level: u8, config: &Config) -> Option { - SYSTEM_SOURCE.with(|cell| { - let source = cell.get_or_init(new_system_source).as_ref(); - let props = Properties { - style: Style::Normal, - weight: weight_for_level(level), - stretch: Stretch::NORMAL, - }; - let emoji_props = Properties { - style: Style::Normal, - weight: Weight::NORMAL, - stretch: Stretch::NORMAL, - }; - - let latin = resolve_font( - source, - &props, - config.font.heading.latin.as_deref(), - preferred_latin_families(), - )?; - - let cjk = resolve_font( - source, - &props, - config.font.heading.cjk.as_deref(), - preferred_cjk_families(), - )?; - - let emoji = resolve_optional_font( - source, - &emoji_props, - config.font.heading.emoji.as_deref(), - preferred_emoji_families(), - ); +fn resolve_font_set(config: &Config) -> Option { + let source = new_system_source(); + let props = Properties { + style: Style::Normal, + weight: Weight::BOLD, + stretch: Stretch::NORMAL, + }; + let emoji_props = Properties { + style: Style::Normal, + weight: Weight::NORMAL, + stretch: Stretch::NORMAL, + }; - Some(FontSet { latin, cjk, emoji }) - }) + let latin = resolve_font( + source.as_ref(), + &props, + config.font.heading.latin.as_deref(), + preferred_latin_families(), + )?; + + let cjk = resolve_font( + source.as_ref(), + &props, + config.font.heading.cjk.as_deref(), + preferred_cjk_families(), + )?; + + let emoji = resolve_optional_font( + source.as_ref(), + &emoji_props, + config.font.heading.emoji.as_deref(), + preferred_emoji_families(), + ); + + Some(FontSet { latin, cjk, emoji }) } #[cfg(test)] @@ -451,7 +401,7 @@ mod tests { #[test] fn emoji_font_prefers_renderable_emoji_glyph() { let config = Config::default(); - let fonts = get_fonts(1, &config).expect("fonts should resolve"); + let fonts = get_fonts(&config).expect("fonts should resolve"); let glyph = fonts.for_char('😀'); let raster = glyph.font.glyph_raster_image2(glyph.glyph_id, u16::MAX); diff --git a/src/render.rs b/src/render.rs index be30af6..74c5f99 100644 --- a/src/render.rs +++ b/src/render.rs @@ -331,7 +331,7 @@ pub fn render_heading( } let st = style::heading_style(level, theme); - let fonts = font::get_fonts(level, config)?; + let fonts = font::get_fonts(config)?; let scale = PxScale { x: st.size, y: st.size, From c6185a54ade44cf07e6a87fcd76333f02f759991 Mon Sep 17 00:00:00 2001 From: shawn Date: Wed, 2 Dec 2026 18:28:13 +0800 Subject: [PATCH 2/2] fix: use Black for heading text --- src/font.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/font.rs b/src/font.rs index fc9c0cb..89f6b88 100644 --- a/src/font.rs +++ b/src/font.rs @@ -238,17 +238,15 @@ fn try_load(source: &SystemSource, family: &str, props: &Properties) -> Option Option> { - if props.weight == Weight::BOLD { + if props.weight == Weight::BLACK { let normal_props = Properties { weight: Weight::NORMAL, ..*props }; - for suffix in ["Bold", "Demi Bold"] { + for suffix in ["Black", "Heavy"] { let variant = format!("{family} {suffix}"); if let Some(font) = try_load(source, &variant, &normal_props) { return Some(font); @@ -344,7 +342,7 @@ fn resolve_font_set(config: &Config) -> Option { let source = new_system_source(); let props = Properties { style: Style::Normal, - weight: Weight::BOLD, + weight: Weight::BLACK, stretch: Stretch::NORMAL, }; let emoji_props = Properties {