diff --git a/tiny_skia/src/vector.rs b/tiny_skia/src/vector.rs index 8a15f47f95..ea7de2150b 100644 --- a/tiny_skia/src/vector.rs +++ b/tiny_skia/src/vector.rs @@ -8,6 +8,7 @@ use tiny_skia::Transform; use std::cell::RefCell; use std::collections::hash_map; use std::fs; +use std::sync::Arc; #[derive(Debug)] pub struct Pipeline { @@ -68,6 +69,7 @@ struct Cache { tree_hits: FxHashSet, rasters: FxHashMap, raster_hits: FxHashSet, + fontdb: Option>, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -81,23 +83,32 @@ 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(); + + self.fontdb = Some(Arc::new(fontdb)); + } + + let options = usvg::Options { + fontdb: self + .fontdb + .as_ref() + .expect("fontdb must be initialized") + .clone(), + ..usvg::Options::default() + }; + if let hash_map::Entry::Vacant(entry) = self.trees.entry(id) { let svg = match handle.data() { Data::Path(path) => { fs::read_to_string(path).ok().and_then(|contents| { - usvg::Tree::from_str( - &contents, - &usvg::Options::default(), // TODO: Set usvg::Options::fontdb - ) - .ok() + usvg::Tree::from_str(&contents, &options).ok() }) } Data::Bytes(bytes) => { - usvg::Tree::from_data( - bytes, - &usvg::Options::default(), // TODO: Set usvg::Options::fontdb - ) - .ok() + usvg::Tree::from_data(bytes, &options).ok() } }; diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index 74e9924dd6..e55ade388c 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -6,6 +6,7 @@ use resvg::tiny_skia; use resvg::usvg; use rustc_hash::{FxHashMap, FxHashSet}; use std::fs; +use std::sync::Arc; /// Entry in cache corresponding to an svg handle pub enum Svg { @@ -37,6 +38,7 @@ pub struct Cache { svg_hits: FxHashSet, rasterized_hits: FxHashSet<(u64, u32, u32, ColorFilter)>, should_trim: bool, + fontdb: Option>, } type ColorFilter = Option<[u8; 4]>; @@ -48,23 +50,33 @@ impl Cache { return self.svgs.get(&handle.id()).unwrap(); } + // TODO: Reuse `cosmic-text` font database + if self.fontdb.is_none() { + let mut fontdb = usvg::fontdb::Database::new(); + fontdb.load_system_fonts(); + + self.fontdb = Some(Arc::new(fontdb)); + } + + let options = usvg::Options { + fontdb: self + .fontdb + .as_ref() + .expect("fontdb must be initialized") + .clone(), + ..usvg::Options::default() + }; + let svg = match handle.data() { svg::Data::Path(path) => fs::read_to_string(path) .ok() .and_then(|contents| { - usvg::Tree::from_str( - &contents, - &usvg::Options::default(), // TODO: Set usvg::Options::fontdb - ) - .ok() + usvg::Tree::from_str(&contents, &options).ok() }) .map(Svg::Loaded) .unwrap_or(Svg::NotFound), svg::Data::Bytes(bytes) => { - match usvg::Tree::from_data( - bytes, - &usvg::Options::default(), // TODO: Set usvg::Options::fontdb - ) { + match usvg::Tree::from_data(bytes, &options) { Ok(tree) => Svg::Loaded(tree), Err(_) => Svg::NotFound, }