Revert "Make FontSystem not self-referencing and update fontdb and rustybuzz"

This commit is contained in:
Jeremy Soller 2023-03-02 18:16:57 -07:00 committed by GitHub
parent b6398a2d57
commit eca804c732
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 179 additions and 225 deletions

View file

@ -1,23 +1,27 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
#[cfg(feature = "swash")]
use std::collections::hash_map::Entry;
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
use crate::{Attrs, AttrsOwned, Font, FontKey};
use crate::{Attrs, AttrsOwned, Font, FontMatches};
/// Access system fonts
pub struct FontSystem {
#[ouroboros::self_referencing]
struct FontSystemInner {
locale: String,
db: fontdb::Database,
font_matches_cache: Mutex<HashMap<AttrsOwned, Arc<Vec<FontKey>>>>,
#[cfg(feature = "swash")]
font_key_cache: Mutex<HashMap<fontdb::ID, Option<FontKey>>>,
#[borrows(db)]
#[not_covariant]
font_cache: Mutex<HashMap<fontdb::ID, Option<Arc<Font<'this>>>>>,
#[borrows(locale, db)]
#[not_covariant]
font_matches_cache: Mutex<HashMap<AttrsOwned, Arc<FontMatches<'this>>>>,
}
/// Access system fonts
pub struct FontSystem(FontSystemInner);
impl FontSystem {
/// Create a new [`FontSystem`], that allows access to any installed system fonts
///
@ -71,7 +75,8 @@ impl FontSystem {
let now = std::time::Instant::now();
//TODO only do this on demand!
for id in db.faces().map(|face| face.id).collect::<Vec<_>>() {
for i in 0..db.faces().len() {
let id = db.faces()[i].id;
unsafe {
db.make_shared_face_data(id);
}
@ -85,92 +90,97 @@ impl FontSystem {
);
}
Self {
locale,
db,
font_matches_cache: Mutex::new(HashMap::new()),
#[cfg(feature = "swash")]
font_key_cache: Mutex::new(HashMap::new()),
}
Self(
FontSystemInnerBuilder {
locale,
db,
font_cache_builder: |_| Mutex::new(HashMap::new()),
font_matches_cache_builder: |_, _| Mutex::new(HashMap::new()),
}
.build(),
)
}
pub fn locale(&self) -> &str {
&self.locale
self.0.borrow_locale()
}
pub fn db(&self) -> &fontdb::Database {
&self.db
self.0.borrow_db()
}
pub fn into_locale_and_db(self) -> (String, fontdb::Database) {
(self.locale, self.db)
let heads = self.0.into_heads();
(heads.locale, heads.db)
}
// Clippy false positive
#[allow(clippy::needless_lifetimes)]
pub fn get_font<'a>(&'a self, key: FontKey) -> Option<Font<'a>> {
match Font::from_key(&self.db, key) {
Some(font) => Some(font),
None => {
let face = self.db.face(key.id)?;
log::warn!("failed to load font '{}'", face.post_script_name);
None
}
}
pub fn get_font<'a>(&'a self, id: fontdb::ID) -> Option<Arc<Font<'a>>> {
self.0.with(|fields| get_font(&fields, id))
}
#[cfg(feature = "swash")]
pub fn get_font_key(&self, id: fontdb::ID) -> Option<FontKey> {
let mut font_key_cache = self
.font_key_cache
.lock()
.expect("failed to lock font matches cache");
match font_key_cache.entry(id) {
Entry::Occupied(entry) => *entry.get(),
Entry::Vacant(entry) => {
let key = self.db.face(id).and_then(Font::new).as_ref().map(Font::key);
entry.insert(key);
key
}
}
}
pub fn get_font_matches<'a>(&'a self, attrs: Attrs) -> Arc<FontMatches<'a>> {
self.0.with(|fields| {
let mut font_matches_cache = fields
.font_matches_cache
.lock()
.expect("failed to lock font matches cache");
//TODO: do not create AttrsOwned unless entry does not already exist
font_matches_cache
.entry(AttrsOwned::new(attrs))
.or_insert_with(|| {
#[cfg(not(target_arch = "wasm32"))]
let now = std::time::Instant::now();
#[cfg(not(feature = "swash"))]
pub fn get_font_key(&self, id: fontdb::ID) -> Option<FontKey> {
Some(Font::new(self.db.face(id)?)?.key())
}
let mut fonts = Vec::new();
for face in fields.db.faces() {
if !attrs.matches(face) {
continue;
}
pub fn get_font_matches(&self, attrs: Attrs) -> Arc<Vec<FontKey>> {
let mut font_matches_cache = self
.font_matches_cache
.lock()
.expect("failed to lock font matches cache");
//TODO: do not create AttrsOwned unless entry does not already exist
font_matches_cache
.entry(AttrsOwned::new(attrs))
.or_insert_with(|| {
#[cfg(not(target_arch = "wasm32"))]
let now = std::time::Instant::now();
let mut font_keys = Vec::new();
for face in self.db.faces() {
if !attrs.matches(face) {
continue;
if let Some(font) = get_font(&fields, face.id) {
fonts.push(font);
}
}
if let Some(key) = self.get_font_key(face.id) {
font_keys.push(key);
let font_matches = Arc::new(FontMatches {
locale: fields.locale,
default_family: fields.db.family_name(&attrs.family).to_string(),
fonts,
});
#[cfg(not(target_arch = "wasm32"))]
{
let elapsed = now.elapsed();
log::debug!("font matches for {:?} in {:?}", attrs, elapsed);
}
}
#[cfg(not(target_arch = "wasm32"))]
{
let elapsed = now.elapsed();
log::debug!("font matches for {:?} in {:?}", attrs, elapsed);
}
Arc::new(font_keys)
})
.clone()
font_matches
})
.clone()
})
}
}
fn get_font<'b>(
fields: &ouroboros_impl_font_system_inner::BorrowedFields<'_, 'b>,
id: fontdb::ID,
) -> Option<Arc<Font<'b>>> {
fields
.font_cache
.lock()
.expect("failed to lock font cache")
.entry(id)
.or_insert_with(|| {
let face = fields.db.face(id)?;
match Font::new(face) {
Some(font) => Some(Arc::new(font)),
None => {
log::warn!("failed to load font '{}'", face.post_script_name);
None
}
}
})
.clone()
}