improv: reuse loaded fonts by cosmic-text for svg renderer

This commit is contained in:
Hojjat 2026-05-29 17:16:30 -06:00 committed by Michael Murphy
parent ebb3b125bf
commit 5013f63956
2 changed files with 18 additions and 5 deletions

View file

@ -161,6 +161,14 @@ impl FontSystem {
&mut self.raw
}
/// Returns a reference to the underlying [`fontdb::Database`].
///
/// Unlike going through [`raw`](Self::raw) and `db_mut`, this does not
/// clear `cosmic-text`'s internal font-match cache.
pub fn db(&self) -> &cosmic_text::fontdb::Database {
self.raw.db()
}
/// Loads a font from its bytes.
pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) {
if let Cow::Borrowed(bytes) = bytes {

View file

@ -1,5 +1,6 @@
use crate::core::svg::{Data, Handle};
use crate::core::{Color, Rectangle, Size};
use crate::graphics::text;
use resvg::usvg;
use rustc_hash::{FxHashMap, FxHashSet};
@ -74,6 +75,7 @@ struct Cache {
rasters: FxHashMap<RasterKey, tiny_skia::Pixmap>,
raster_hits: FxHashSet<RasterKey>,
fontdb: Option<Arc<usvg::fontdb::Database>>,
fontdb_version: Option<text::Version>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@ -87,12 +89,15 @@ impl Cache {
fn load(&mut self, handle: &Handle) -> Option<&usvg::Tree> {
let id = handle.id();
// TODO: Reuse `cosmic-text` font database
if self.fontdb.is_none() {
let mut fontdb = usvg::fontdb::Database::new();
fontdb.load_system_fonts();
// Reuse the font database already loaded by the `cosmic-text` font
// system. We clone it once and refresh only when a font is
// loaded (i.e. when the font system's version changes).
let font_system = text::font_system().read().expect("Read font system");
let version = font_system.version();
self.fontdb = Some(Arc::new(fontdb));
if self.fontdb_version != Some(version) {
self.fontdb = Some(Arc::new(font_system.db().clone()));
self.fontdb_version = Some(version);
}
let options = usvg::Options {