From 58227cdc2f95370ffc366f03d6319f582c39867d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E7=94=B5=E8=8A=BD=E8=A1=A3?= Date: Mon, 12 Oct 2026 12:45:21 -0400 Subject: [PATCH] documents: measuring and export load gpui's bundled faces and map generic families as it does --- src/documents/diagram.rs | 43 ++++++++++++++++++++++++++++++++++-- src/typography/math/tests.rs | 3 ++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/documents/diagram.rs b/src/documents/diagram.rs index 115b890..5ef0606 100644 --- a/src/documents/diagram.rs +++ b/src/documents/diagram.rs @@ -182,14 +182,53 @@ fn texts(group: &usvg::Group, kept: &mut usize, lost: &mut usize) { } } -/// The system's fonts, read once. +/// The system's fonts and gpui's bundled ones, read once. fn fonts() -> Arc { static FONTS: OnceLock> = OnceLock::new(); FONTS .get_or_init(|| { + use gpui::AssetSource as _; let mut fonts = usvg::fontdb::Database::new(); fonts.load_system_fonts(); - log::info!("diagram: {} system font faces for export", fonts.len()); + // The faces gpui's SVG renderer adds, from the same assets. + for path in [ + "fonts/ibm-plex-sans/IBMPlexSans-Regular.ttf", + "fonts/lilex/Lilex-Regular.ttf", + ] { + match crate::Assets.load(path) { + Ok(Some(data)) => fonts.load_font_data(data.into_owned()), + Ok(None) => log::debug!("diagram: no bundled {path}"), + Err(error) => log::error!("diagram: bundled {path} did not load: {error}"), + } + } + // As gpui: unanswered generic families take bundled faces. + use usvg::fontdb::{Family, Query}; + for (family, bundled) in [ + (Family::SansSerif, "IBM Plex Sans"), + (Family::Serif, "IBM Plex Sans"), + (Family::Monospace, "Lilex"), + (Family::Cursive, "IBM Plex Sans"), + (Family::Fantasy, "IBM Plex Sans"), + ] { + let query = Query { + families: &[family], + ..Default::default() + }; + if fonts.query(&query).is_some() { + continue; + } + match family { + Family::SansSerif => fonts.set_sans_serif_family(bundled), + Family::Serif => fonts.set_serif_family(bundled), + Family::Monospace => fonts.set_monospace_family(bundled), + Family::Cursive => fonts.set_cursive_family(bundled), + _ => fonts.set_fantasy_family(bundled), + } + } + log::info!( + "diagram: {} font faces for measuring and export", + fonts.len() + ); Arc::new(fonts) }) .clone() diff --git a/src/typography/math/tests.rs b/src/typography/math/tests.rs index 2f4add6..74612ba 100644 --- a/src/typography/math/tests.rs +++ b/src/typography/math/tests.rs @@ -224,7 +224,8 @@ fn drawn_fallback_text_stays_inside_the_frame() { let pixels = image.as_bytes(0).expect("pixels"); pixels.chunks_exact(4).filter(|pixel| pixel[3] > 16).count() }; - for source in [r"\text{中文}", r"\text{한글}"] { + // The last is in gpui's bundled IBM Plex Sans alone. + for source in [r"\text{中文}", r"\text{한글}", "\\text{\u{ECE0}}"] { let (svg, _) = svg(&formula(source, false)).expect("typesets"); let tree = usvg::roxmltree::Document::parse(&svg).expect("xml"); let view: Vec = tree