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

@ -9,10 +9,11 @@ documentation = "https://docs.rs/cosmic-text/latest/cosmic_text/"
repository = "https://github.com/pop-os/cosmic-text"
[dependencies]
fontdb = { version = "0.13.0", default-features = false }
fontdb = { version = "0.10.0", default-features = false }
libm = "0.2.6"
log = "0.4.17"
rustybuzz = { version = "0.7.0", default-features = false, features = ["libm"] }
ouroboros = "0.15.5"
rustybuzz = { version = "0.6.0", default-features = false, features = ["libm"] }
swash = { version = "0.1.6", optional = true }
syntect = { version = "5.0.0", optional = true }
sys-locale = { version = "0.2.3", optional = true }

View file

@ -112,7 +112,6 @@ allow = [
"Apache-2.0",
"Unicode-DFS-2016",
"BSD-2-Clause",
"Zlib",
#"Apache-2.0 WITH LLVM-exception",
]
# List of explicitly disallowed licenses
@ -164,8 +163,8 @@ exceptions = [
# and the crate will be checked normally, which may produce warnings or errors
# depending on the rest of your configuration
#license-files = [
# Each entry is a crate relative path, and the (opaque) hash of its contents
#{ path = "LICENSE", hash = 0xbd0eed23 }
# Each entry is a crate relative path, and the (opaque) hash of its contents
#{ path = "LICENSE", hash = 0xbd0eed23 }
#]
[licenses.private]

View file

@ -1,12 +1,10 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use crate::FontKey;
/// Key for building a glyph cache
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct CacheKey {
/// Font key
pub font_key: FontKey,
/// Font ID
pub font_id: fontdb::ID,
/// Glyph ID
pub glyph_id: u16,
/// `f32` bits of font size
@ -19,7 +17,7 @@ pub struct CacheKey {
impl CacheKey {
pub fn new(
font_key: FontKey,
font_id: fontdb::ID,
glyph_id: u16,
font_size: f32,
pos: (f32, f32),
@ -28,7 +26,7 @@ impl CacheKey {
let (y, y_bin) = SubpixelBin::new(pos.1);
(
Self {
font_key,
font_id,
glyph_id,
font_size_bits: font_size.to_bits(),
x_bin,

View file

@ -1,10 +1,11 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use alloc::sync::Arc;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use unicode_script::Script;
use crate::{Font, FontKey};
use crate::Font;
use self::platform::*;
@ -25,8 +26,7 @@ mod platform;
mod platform;
pub struct FontFallbackIter<'a> {
db: &'a fontdb::Database,
font_keys: &'a [FontKey],
fonts: &'a [Arc<Font<'a>>],
default_families: &'a [&'a str],
default_i: usize,
scripts: Vec<Script>,
@ -39,15 +39,13 @@ pub struct FontFallbackIter<'a> {
impl<'a> FontFallbackIter<'a> {
pub fn new(
db: &'a fontdb::Database,
font_keys: &'a [FontKey],
fonts: &'a [Arc<Font<'a>>],
default_families: &'a [&'a str],
scripts: Vec<Script>,
locale: &'a str,
) -> Self {
Self {
db,
font_keys,
fonts,
default_families,
default_i: 0,
scripts,
@ -68,13 +66,12 @@ impl<'a> FontFallbackIter<'a> {
word
);
} else if self.other_i > 0 {
let font_key = self.font_keys[self.other_i - 1];
let font = Font::from_key(self.db, font_key).expect("invalid font key");
let font = &self.fonts[self.other_i - 1];
log::debug!(
"Failed to find preset fallback for {:?} locale '{}', used '{}': '{}'",
self.scripts,
self.locale,
font.name(),
font.info.family,
word
);
} else if !self.scripts.is_empty() && self.common_i > 0 {
@ -91,15 +88,14 @@ impl<'a> FontFallbackIter<'a> {
}
impl<'a> Iterator for FontFallbackIter<'a> {
type Item = Font<'a>;
type Item = &'a Arc<Font<'a>>;
fn next(&mut self) -> Option<Self::Item> {
while self.default_i < self.default_families.len() {
let default_family = self.default_families[self.default_i];
self.default_i += 1;
for font_key in self.font_keys.iter().copied() {
let font = Font::from_key(self.db, font_key).expect("invalid font key");
if font.contains_family(default_family) {
for font in self.fonts.iter() {
if font.info.family == default_family {
return Some(font);
}
}
@ -112,9 +108,8 @@ impl<'a> Iterator for FontFallbackIter<'a> {
while self.script_i.1 < script_families.len() {
let script_family = script_families[self.script_i.1];
self.script_i.1 += 1;
for font_key in self.font_keys.iter().copied() {
let font = Font::from_key(self.db, font_key).expect("invalid font key");
if font.contains_family(script_family) {
for font in self.fonts.iter() {
if font.info.family == script_family {
return Some(font);
}
}
@ -134,9 +129,8 @@ impl<'a> Iterator for FontFallbackIter<'a> {
while self.common_i < common_families.len() {
let common_family = common_families[self.common_i];
self.common_i += 1;
for font_key in self.font_keys.iter().copied() {
let font = Font::from_key(self.db, font_key).expect("invalid font key");
if font.contains_family(common_family) {
for font in self.fonts.iter() {
if font.info.family == common_family {
return Some(font);
}
}
@ -146,14 +140,10 @@ impl<'a> Iterator for FontFallbackIter<'a> {
//TODO: do we need to do this?
//TODO: do not evaluate fonts more than once!
let forbidden_families = forbidden_fallback();
while self.other_i < self.font_keys.len() {
let font_key = self.font_keys[self.other_i];
let font = Font::from_key(self.db, font_key).expect("invalid font key");
while self.other_i < self.fonts.len() {
let font = &self.fonts[self.other_i];
self.other_i += 1;
if forbidden_families
.iter()
.all(|family| !font.contains_family(family))
{
if !forbidden_families.contains(&font.info.family.as_str()) {
return Some(font);
}
}

14
src/font/matches.rs Normal file
View file

@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use alloc::sync::Arc;
#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};
use crate::Font;
/// Fonts that match a pattern
pub struct FontMatches<'a> {
pub locale: &'a str,
pub default_family: String,
pub fonts: Vec<Arc<Font<'a>>>,
}

View file

@ -4,18 +4,12 @@ use core::ops::Deref;
pub(crate) mod fallback;
pub use self::matches::*;
mod matches;
pub use self::system::*;
mod system;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(not(feature = "swash"), repr(transparent))]
/// Identifies a [`Font`] in a [`FontSystem`]
pub struct FontKey {
pub id: fontdb::ID,
#[cfg(feature = "swash")]
pub swash: (u32, swash::CacheKey),
}
/// A font
pub struct Font<'a> {
pub info: &'a fontdb::FaceInfo,
@ -50,48 +44,6 @@ impl<'a> Font<'a> {
})
}
pub fn from_key(db: &'a fontdb::Database, key: FontKey) -> Option<Self> {
let info = db.face(key.id)?;
let data = match &info.source {
fontdb::Source::Binary(data) => data.deref().as_ref(),
#[cfg(feature = "std")]
fontdb::Source::File(path) => {
log::warn!("Unsupported fontdb Source::File('{}')", path.display());
return None;
}
#[cfg(feature = "std")]
fontdb::Source::SharedFile(_path, data) => data.deref().as_ref(),
};
Some(Self {
info,
data,
rustybuzz: rustybuzz::Face::from_slice(data, info.index)?,
#[cfg(feature = "swash")]
swash: key.swash,
})
}
pub fn key(&self) -> FontKey {
FontKey {
id: self.info.id,
#[cfg(feature = "swash")]
swash: self.swash,
}
}
pub fn name(&self) -> &str {
if let Some((name, _)) = self.info.families.first() {
name
} else {
&self.info.post_script_name
}
}
pub fn contains_family(&self, family: &str) -> bool {
self.info.families.iter().any(|(name, _)| name == family)
}
#[cfg(feature = "swash")]
pub fn as_swash(&self) -> swash::FontRef {
swash::FontRef {

View file

@ -6,7 +6,7 @@ use alloc::{
vec::Vec,
};
use crate::{Attrs, Font, FontKey};
use crate::{Attrs, Font, FontMatches};
/// Access system fonts
pub struct FontSystem {
@ -45,38 +45,33 @@ impl FontSystem {
// 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),
pub fn get_font<'a>(&'a self, id: fontdb::ID) -> Option<Arc<Font<'a>>> {
let face = self.db.face(id)?;
match Font::new(face) {
Some(font) => Some(Arc::new(font)),
None => {
let face = self.db.face(key.id)?;
log::warn!("failed to load font '{}'", face.post_script_name);
None
}
}
}
pub fn get_font_key(&self, id: fontdb::ID) -> Option<FontKey> {
Some(Font::new(self.db.face(id)?)?.key())
}
pub fn get_font_matches(&self, attrs: Attrs) -> Arc<Vec<FontKey>> {
let mut font_keys = Vec::new();
pub fn get_font_matches<'a>(&'a self, attrs: Attrs) -> Arc<FontMatches<'a>> {
let mut fonts = Vec::new();
for face in self.db.faces() {
if !attrs.matches(face) {
continue;
}
let font_key = match self.get_font_key(face.id) {
Some(font_key) => font_key,
None => continue,
};
if self.get_font(font_key).is_some() {
font_keys.push(font_key);
if let Some(font) = self.get_font(face.id) {
fonts.push(font);
}
}
Arc::new(font_keys)
Arc::new(FontMatches {
locale: &self.locale,
default_family: self.db.family_name(&attrs.family).to_string(),
fonts,
})
}
}

View file

@ -6,7 +6,7 @@ use alloc::{
vec::Vec,
};
use crate::{Attrs, Font, FontKey};
use crate::{Attrs, Font, FontMatches};
/// Access system fonts
pub struct FontSystem {
@ -44,7 +44,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);
}
@ -70,38 +71,33 @@ impl FontSystem {
// 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),
pub fn get_font<'a>(&'a self, id: fontdb::ID) -> Option<Arc<Font<'a>>> {
let face = self.db.face(id)?;
match Font::new(face) {
Some(font) => Some(Arc::new(font)),
None => {
let face = self.db.face(key.id)?;
log::warn!("failed to load font '{}'", face.post_script_name);
None
}
}
}
pub fn get_font_key(&self, id: fontdb::ID) -> Option<FontKey> {
Some(Font::new(self.db.face(id)?)?.key())
}
pub fn get_font_matches<'a>(&'a self, attrs: Attrs) -> Arc<Vec<FontKey>> {
let mut font_keys = Vec::new();
pub fn get_font_matches<'a>(&'a self, attrs: Attrs) -> Arc<FontMatches<'a>> {
let mut fonts = Vec::new();
for face in self.db.faces() {
if !attrs.matches(face) {
continue;
}
let font_key = match self.get_font_key(face.id) {
Some(font_key) => font_key,
None => continue,
};
if self.get_font(font_key).is_some() {
font_keys.push(font_key);
if let Some(font) = self.get_font(face.id) {
fonts.push(font);
}
}
Arc::new(font_keys)
Arc::new(FontMatches {
locale: &self.locale,
default_family: self.db.family_name(&attrs.family).to_string(),
fonts,
})
}
}

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()
}

View file

@ -9,9 +9,7 @@ use unicode_script::{Script, UnicodeScript};
use unicode_segmentation::UnicodeSegmentation;
use crate::fallback::FontFallbackIter;
use crate::{
Align, AttrsList, CacheKey, Color, Font, FontKey, FontSystem, LayoutGlyph, LayoutLine, Wrap,
};
use crate::{Align, AttrsList, CacheKey, Color, Font, FontSystem, LayoutGlyph, LayoutLine, Wrap};
fn shape_fallback(
font: &Font,
@ -64,7 +62,7 @@ fn shape_fallback(
y_advance,
x_offset,
y_offset,
font_key: font.key(),
font_id: font.info.id,
glyph_id: info.glyph_id.try_into().expect("failed to cast glyph ID"),
//TODO: color should not be related to shaping
color_opt: attrs.color_opt,
@ -127,21 +125,22 @@ fn shape_run(
let font_matches = font_system.get_font_matches(attrs);
let db = font_system.db();
let default_families = [db.family_name(&attrs.family)];
let default_families = [font_matches.default_family.as_str()];
let mut font_iter = FontFallbackIter::new(
db,
&font_matches,
&font_matches.fonts,
&default_families,
scripts,
font_system.locale(),
font_matches.locale,
);
let font = font_iter.next().expect("no default font found");
let (mut glyphs, mut missing) =
shape_fallback(&font, line, attrs_list, start_run, end_run, span_rtl);
let (mut glyphs, mut missing) = shape_fallback(
font_iter.next().expect("no default font found"),
line,
attrs_list,
start_run,
end_run,
span_rtl,
);
//TODO: improve performance!
while !missing.is_empty() {
@ -150,9 +149,9 @@ fn shape_run(
None => break,
};
log::trace!("Evaluating fallback with font '{}'", font.name());
log::trace!("Evaluating fallback with font '{}'", font.info.family);
let (mut fb_glyphs, fb_missing) =
shape_fallback(&font, line, attrs_list, start_run, end_run, span_rtl);
shape_fallback(font, line, attrs_list, start_run, end_run, span_rtl);
// Insert all matching glyphs
let mut fb_i = 0;
@ -229,7 +228,7 @@ pub struct ShapeGlyph {
pub y_advance: f32,
pub x_offset: f32,
pub y_offset: f32,
pub font_key: FontKey,
pub font_id: fontdb::ID,
pub glyph_id: u16,
pub color_opt: Option<Color>,
pub metadata: usize,
@ -248,7 +247,7 @@ impl ShapeGlyph {
let y_offset = font_size * self.y_offset;
let (cache_key, x_int, y_int) = CacheKey::new(
self.font_key,
self.font_id,
self.glyph_id,
font_size,
(x + x_offset, y - y_offset),

View file

@ -20,10 +20,10 @@ fn swash_image(
context: &mut ScaleContext,
cache_key: CacheKey,
) -> Option<SwashImage> {
let font = match font_system.get_font(cache_key.font_key) {
let font = match font_system.get_font(cache_key.font_id) {
Some(some) => some,
None => {
log::warn!("did not find font {:?}", cache_key.font_key.id);
log::warn!("did not find font {:?}", cache_key.font_id);
return None;
}
};
@ -63,10 +63,10 @@ fn swash_outline_commands(
) -> Option<Vec<swash::zeno::Command>> {
use swash::zeno::PathData as _;
let font = match font_system.get_font(cache_key.font_key) {
let font = match font_system.get_font(cache_key.font_id) {
Some(some) => some,
None => {
log::warn!("did not find font {:?}", cache_key.font_key.id);
log::warn!("did not find font {:?}", cache_key.font_id);
return None;
}
};