From e5da9ca41dfe01824e5e1f71e2d859b8c6d9e49c Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Tue, 20 May 2025 13:19:50 +0100 Subject: [PATCH] Merge SourceCache into Collection --- fontique/src/collection/mod.rs | 19 +++++++++++++++++-- fontique/src/collection/query.rs | 4 ++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/fontique/src/collection/mod.rs b/fontique/src/collection/mod.rs index 6d61042b..465c4b99 100644 --- a/fontique/src/collection/mod.rs +++ b/fontique/src/collection/mod.rs @@ -48,6 +48,15 @@ pub struct CollectionOptions { /// /// The default value is `true`. pub system_fonts: bool, + #[cfg(feature = "std")] + /// If true, the source cache will use a secondary shared cache + /// guaranteeing that all clones will use the same backing store. + /// + /// This is useful for ensuring that only one copy of font data is + /// loaded into memory in multi-threaded scenarios. + /// + /// The default value is `false`. + pub shared_source_cache: bool, } impl Default for CollectionOptions { @@ -55,6 +64,8 @@ impl Default for CollectionOptions { Self { shared: false, system_fonts: true, + #[cfg(feature = "std")] + shared_source_cache: false, } } } @@ -64,6 +75,7 @@ impl Default for CollectionOptions { pub struct Collection { inner: Inner, query_state: query::QueryState, + source_cache: SourceCache, } impl Collection { @@ -80,6 +92,9 @@ impl Collection { Self { inner: Inner::new(options), query_state: Default::default(), + source_cache: SourceCache::new(crate::SourceCacheOptions { + shared: options.shared_source_cache, + }), } } @@ -168,8 +183,8 @@ impl Collection { } /// Returns an object for selecting fonts from this collection. - pub fn query<'a>(&'a mut self, source_cache: &'a mut SourceCache) -> Query<'a> { - Query::new(self, source_cache) + pub fn query<'a>(&'a mut self) -> Query<'a> { + Query::new(self) } /// Registers all fonts that exist in the given data. diff --git a/fontique/src/collection/query.rs b/fontique/src/collection/query.rs index 960366be..c787d418 100644 --- a/fontique/src/collection/query.rs +++ b/fontique/src/collection/query.rs @@ -37,12 +37,12 @@ pub struct Query<'a> { } impl<'a> Query<'a> { - pub(super) fn new(collection: &'a mut Collection, source_cache: &'a mut SourceCache) -> Self { + pub(super) fn new(collection: &'a mut Collection) -> Self { collection.query_state.clear(); Self { collection: &mut collection.inner, state: &mut collection.query_state, - source_cache, + source_cache: &mut collection.source_cache, attributes: Attributes::default(), fallbacks: None, }