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 &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. /// Loads a font from its bytes.
pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) { pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) {
if let Cow::Borrowed(bytes) = bytes { if let Cow::Borrowed(bytes) = bytes {

View file

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