mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 13:02:50 -05:00
Merge pull request #10772 from dotCooCoo/fix/font-catalog-bundled-lazy
Fonts: add the bundled fonts to the font catalog on its first read
This commit is contained in:
5 files changed
+75
-35
No files matched your search
@@ -100,8 +100,11 @@ pub struct FontDb {
|
||||
colours: Mutex<GlyphColours>,
|
||||
/// The installed fonts: the system font scan's faces, found by any of their names
|
||||
/// ([`FontDb::catalog`]). Lookups by family name wait for its first scan, so what they find
|
||||
/// never depends on what ran before them.
|
||||
/// never depends on what ran before them. The bundled faces join it on the first call of
|
||||
/// [`FontDb::catalog`], so its faces are read through that call.
|
||||
catalog: Arc<Catalog>,
|
||||
/// The bundled faces were added to [`Self::catalog`].
|
||||
bundled_cataloged: std::sync::Once,
|
||||
/// Fonts files carried for their type ([`vectorcraft_doc::FileFonts`]): apart from the faces,
|
||||
/// so no family name, font menu, fallback or PostScript name lookup finds them.
|
||||
pub(super) file_faces: RwLock<FileFaces>,
|
||||
@@ -387,6 +390,28 @@ mod race_tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, feature = "bundled-fonts"))]
|
||||
mod bundled_catalog_tests {
|
||||
use super::*;
|
||||
|
||||
fn bundled_in_catalog(catalog: &Catalog) -> usize {
|
||||
catalog.faces().iter().filter(|f| f.source == craft::font_catalog::Source::Bundled).count()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_bundled_fonts_join_the_catalog_when_it_is_first_read() {
|
||||
let db = FontDb::with_font_dirs(Vec::new());
|
||||
assert!(!db.read_faces().is_empty(), "the database draws with its bundled faces from the start");
|
||||
assert_eq!(bundled_in_catalog(&db.catalog), 0, "making the database copies no bundled font into the catalog");
|
||||
// A lookup that only the catalog answers finds a bundled face.
|
||||
assert_eq!(db.by_postscript_name("SourceSans3-Bold"), Some(("Source Sans 3".to_string(), "Bold".to_string())));
|
||||
let added = bundled_in_catalog(&db.catalog);
|
||||
assert!(added > 0, "the first read of the catalog adds the bundled fonts");
|
||||
assert!(db.catalog().families().iter().any(|f| f == "Inter"), "font pickers list a bundled family");
|
||||
assert_eq!(bundled_in_catalog(db.catalog()), added, "later reads add them no second time");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod image_cache_tests {
|
||||
use super::*;
|
||||
|
||||
@@ -31,7 +31,7 @@ impl FontDb {
|
||||
/// is dated by the file the link leads to. Fonts loaded from bytes and the bundled fonts are
|
||||
/// never listed. Empty on wasm, which has no installed fonts.
|
||||
pub fn recently_added(&self, now: SystemTime) -> RecentlyAdded {
|
||||
let faces = self.catalog.faces();
|
||||
let faces = self.catalog().faces();
|
||||
// Each file's creation time once for all its faces (a collection or a variable font's
|
||||
// named instances share one).
|
||||
let mut created: HashMap<&Path, Option<SystemTime>> = HashMap::new();
|
||||
|
||||
@@ -20,6 +20,14 @@ static NEXT_ID: AtomicU64 = AtomicU64::new(0);
|
||||
/// [`FontDb::global`].
|
||||
static GLOBAL: std::sync::OnceLock<FontDb> = std::sync::OnceLock::new();
|
||||
|
||||
/// The fonts a database starts with: the bundled fonts first, then explicitly supplied craft-fonts
|
||||
/// faces: Japanese (Mincho first), Arabic, Thai, then remaining faces in manifest order. Existing
|
||||
/// script fallback priorities stay ahead of the additional families users can now pick by name.
|
||||
fn bundled_fonts() -> impl Iterator<Item = &'static [u8]> {
|
||||
let craft = crate::craft_fonts::document_fonts().into_iter().map(|f| f.bytes);
|
||||
BUNDLED.iter().copied().chain(craft)
|
||||
}
|
||||
|
||||
impl FontDb {
|
||||
/// A database holding the bundled fonts, whose system font scan reads `font_dirs` (folders,
|
||||
/// read with their subfolders, and font files) and nothing else.
|
||||
@@ -44,15 +52,7 @@ impl FontDb {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
catalog.set_confined_dirs(confined_font_dirs());
|
||||
let mut faces = Vec::new();
|
||||
// Bundled fonts first, then explicitly supplied craft-fonts faces: Japanese (Mincho
|
||||
// first), Arabic, Thai, then remaining faces in manifest order. Existing script fallback
|
||||
// priorities stay ahead of the additional families users can now pick by name.
|
||||
let craft = crate::craft_fonts::document_fonts().into_iter().map(|f| f.bytes);
|
||||
for data in BUNDLED.iter().copied().chain(craft) {
|
||||
// Font pickers list the catalog's families: the fonts the app carries are in it as bundled faces.
|
||||
if let Err(e) = catalog.add_bytes(data.to_vec(), Source::Bundled, &Limits::default()) {
|
||||
log::warn!("a bundled font is not in the font catalog: {e}");
|
||||
}
|
||||
for data in bundled_fonts() {
|
||||
for (i, spec) in enumerate_faces(data) {
|
||||
if let Some(f) = make_face(FontBytes::Static(data), i, spec, None) {
|
||||
faces.push(Arc::new(f));
|
||||
@@ -65,6 +65,7 @@ impl FontDb {
|
||||
images: Mutex::new(GlyphCache::default()),
|
||||
colours: Mutex::new(GlyphCache::default()),
|
||||
catalog: Arc::new(catalog),
|
||||
bundled_cataloged: std::sync::Once::new(),
|
||||
file_faces: RwLock::new(super::FileFaces::default()),
|
||||
canonical_cache: Mutex::new(CanonicalNames::new()),
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
@@ -119,7 +120,20 @@ impl FontDb {
|
||||
|
||||
/// The installed fonts this database finds by name, shared with the framework's font menus and
|
||||
/// pickers. Its lookups wait for its first scan ([`FontDb::scan_in_background`]).
|
||||
///
|
||||
/// The first call adds the bundled fonts to it as [`Source::Bundled`] faces, so font pickers
|
||||
/// list them. A run that never looks a font up in the catalog, such as exporting a file without
|
||||
/// type, does not hold the catalog's copies of their bytes.
|
||||
// The catalog keeps its own copy of the bytes it is given. Adding the bundled fonts on first use
|
||||
// can go once the catalog takes static bytes without copying them (craft-libs#4888).
|
||||
pub fn catalog(&self) -> &Arc<Catalog> {
|
||||
self.bundled_cataloged.call_once(|| {
|
||||
for data in bundled_fonts() {
|
||||
if let Err(e) = self.catalog.add_bytes(data.to_vec(), Source::Bundled, &Limits::default()) {
|
||||
log::warn!("a bundled font is not in the font catalog: {e}");
|
||||
}
|
||||
}
|
||||
});
|
||||
&self.catalog
|
||||
}
|
||||
|
||||
@@ -164,7 +178,7 @@ impl FontDb {
|
||||
/// [`FontDb::families`], shared: cheap to call every frame.
|
||||
pub fn family_list(&self) -> Arc<[String]> {
|
||||
// The catalog's list is the same `Arc` until it rescans (here or in its watch).
|
||||
let installed = self.catalog.families();
|
||||
let installed = self.catalog().families();
|
||||
let mut cache = self.family_cache.lock().unwrap_or_else(|e| e.into_inner());
|
||||
if let Some((from, list)) = cache.as_ref()
|
||||
&& Arc::ptr_eq(from, &installed)
|
||||
@@ -238,7 +252,7 @@ impl FontDb {
|
||||
}
|
||||
// An installed family's, from its names in other languages, as a loaded face's is chosen
|
||||
// (every face of a family has the same names: one is enough).
|
||||
let native = self.catalog.resolve(&family, "").and_then(|(f, _)| {
|
||||
let native = self.catalog().resolve(&family, "").and_then(|(f, _)| {
|
||||
let names: Vec<(Option<String>, String)> = f.localized_families.into_iter().map(|n| (n.language, n.name)).collect();
|
||||
pick_native_label(&f.family, &names)
|
||||
});
|
||||
@@ -256,7 +270,7 @@ impl FontDb {
|
||||
v.push((f.italic, f.weight, f.style.clone()));
|
||||
}
|
||||
}
|
||||
for f in self.catalog.styles(family) {
|
||||
for f in self.catalog().styles(family) {
|
||||
if !v.iter().any(|(_, _, s)| s.eq_ignore_ascii_case(&f.style)) {
|
||||
v.push((f.italic, f.weight, f.style));
|
||||
}
|
||||
@@ -282,7 +296,7 @@ impl FontDb {
|
||||
for f in self.read_faces().iter().filter(|f| f.family.eq_ignore_ascii_case(family)) {
|
||||
consider(&mut best, f.weight, f.italic, &f.style);
|
||||
}
|
||||
for f in self.catalog.styles(family) {
|
||||
for f in self.catalog().styles(family) {
|
||||
consider(&mut best, f.weight, f.italic, &f.style);
|
||||
}
|
||||
best.map(|(_, style)| style)
|
||||
@@ -347,7 +361,7 @@ impl FontDb {
|
||||
/// folders, any other waits for that scan to finish, as every catalog lookup does.
|
||||
#[cfg(any(test, feature = "test-fonts"))]
|
||||
pub(crate) fn ensure_catalog(&self) {
|
||||
let _ = self.catalog.families();
|
||||
let _ = self.catalog().families();
|
||||
}
|
||||
|
||||
/// Catalog the installed fonts on a background thread, so the first lookup by family name
|
||||
@@ -370,14 +384,14 @@ impl FontDb {
|
||||
let _change = self.catalog.is_ready().then(crate::test_fonts::font_change);
|
||||
// The first scan, or a rescan once it is done (never both at once). The count is of the installed faces: the
|
||||
// catalog's less the bundled faces no installed file also has.
|
||||
let cataloged = self.catalog.rescan();
|
||||
let bundled_only = self.catalog.faces().iter().filter(|f| f.source == Source::Bundled && self.catalog.versions(f).len() == 1).count();
|
||||
let catalog = self.catalog();
|
||||
let cataloged = catalog.rescan();
|
||||
let bundled_only = catalog.faces().iter().filter(|f| f.source == Source::Bundled && catalog.versions(f).len() == 1).count();
|
||||
let n = cataloged.saturating_sub(bundled_only);
|
||||
// Files may have been installed since: other versions are looked for again, in the catalog
|
||||
// the rescan published. A version lookup made during the scan read the previous one.
|
||||
self.version_scanned.write().unwrap_or_else(|e| e.into_inner()).clear();
|
||||
let installed: std::collections::HashSet<PathBuf> =
|
||||
self.catalog.faces().iter().flat_map(|f| self.catalog.versions(f)).filter_map(|f| f.path).collect();
|
||||
let installed: std::collections::HashSet<PathBuf> = catalog.faces().iter().flat_map(|f| catalog.versions(f)).filter_map(|f| f.path).collect();
|
||||
// Faces loaded from a file this scan did not catalog, as a face or a version of one (the font was removed, or its folder
|
||||
// is no longer read) are dropped with their outlines. A family with no face left and no
|
||||
// cataloged file leaves the lists, and type set in it resolves to the fallback font. A face
|
||||
|
||||
@@ -30,13 +30,12 @@ impl FontDb {
|
||||
// The catalog first: the first scan takes the faces' lock, which a guard held here would block.
|
||||
// The bundled faces have no file to read. The catalog lists a bundled face ahead of an
|
||||
// installed file of the same face, so each face's file is its first installed version.
|
||||
let cataloged: Vec<(String, String, PathBuf)> = self
|
||||
.catalog
|
||||
let catalog = self.catalog();
|
||||
let cataloged: Vec<(String, String, PathBuf)> = catalog
|
||||
.faces()
|
||||
.iter()
|
||||
.filter_map(|f| {
|
||||
let path =
|
||||
self.catalog.versions(f).into_iter().filter(|v| v.source != craft::font_catalog::Source::Bundled).find_map(|v| v.path)?;
|
||||
let path = catalog.versions(f).into_iter().filter(|v| v.source != craft::font_catalog::Source::Bundled).find_map(|v| v.path)?;
|
||||
Some((f.family.clone(), f.style.clone(), path))
|
||||
})
|
||||
.collect();
|
||||
|
||||
@@ -73,7 +73,7 @@ impl FontDb {
|
||||
}
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
let faces = self.catalog.styles(family);
|
||||
let faces = self.catalog().styles(family);
|
||||
let off = |f: &craft::font_catalog::FaceInfo| (f.italic, (f.weight - 400.0).abs());
|
||||
let nearest = faces.iter().min_by(|a, b| {
|
||||
let ((ai, aw), (bi, bw)) = (off(a), off(b));
|
||||
@@ -89,7 +89,7 @@ impl FontDb {
|
||||
|
||||
/// Is `family` an installed family's own name (any ASCII case)?
|
||||
fn is_installed(&self, family: &str) -> bool {
|
||||
self.catalog.family(family).is_some_and(|f| f.eq_ignore_ascii_case(family))
|
||||
self.catalog().family(family).is_some_and(|f| f.eq_ignore_ascii_case(family))
|
||||
}
|
||||
|
||||
/// The family's own name and the style for a name given in a document: any of a family's
|
||||
@@ -124,7 +124,7 @@ impl FontDb {
|
||||
self.read_faces().iter().flat_map(|f| Alias::of(&f.family, &f.style, &f.keys)).filter(|(k, _)| *k == key).map(|(_, a)| a).collect();
|
||||
// The installed face the name gives: by a family name in its family, by a legacy family
|
||||
// name with its legacy style, by a full or PostScript name whatever the style.
|
||||
let installed = self.catalog.resolve(family, style);
|
||||
let installed = self.catalog().resolve(family, style);
|
||||
let ns = norm(style);
|
||||
// A family and style split from a PostScript name by an importer that didn't have the font
|
||||
// (`Honoka-Mincho` → Honoka, Mincho): once it is installed, the face of that PostScript
|
||||
@@ -147,7 +147,7 @@ impl FontDb {
|
||||
if let Some(a) = found.iter().find(|a| a.style.is_none()) {
|
||||
return (a.family.clone(), style.to_string());
|
||||
}
|
||||
if let Some(installed_family) = self.catalog.family(family) {
|
||||
if let Some(installed_family) = self.catalog().family(family) {
|
||||
return (installed_family, style.to_string());
|
||||
}
|
||||
if let Some(a) = found.iter().find(|a| a.paired.as_ref().is_some_and(|p| *p == ns) || a.paired.is_none()) {
|
||||
@@ -278,7 +278,7 @@ impl FontDb {
|
||||
let key = norm(name);
|
||||
let mut families: Vec<String> = vec![first.family.clone()];
|
||||
let named = |f: &&FaceInfo| f.postscript_name.as_deref().is_some_and(|p| norm(p) == key);
|
||||
for f in self.catalog.faces().iter().filter(named) {
|
||||
for f in self.catalog().faces().iter().filter(named) {
|
||||
if families.len() < MAX_POSTSCRIPT_FACES && !families.iter().any(|known| known.eq_ignore_ascii_case(&f.family)) {
|
||||
families.push(f.family.clone());
|
||||
}
|
||||
@@ -303,13 +303,14 @@ impl FontDb {
|
||||
/// The cataloged face of PostScript name `name`: an installed face before a bundled face of another family with the
|
||||
/// same name, as the installed faces alone answered before the bundled ones were cataloged.
|
||||
fn cataloged_postscript(&self, name: &str) -> Option<FaceInfo> {
|
||||
let face = self.catalog.by_postscript_name(name)?;
|
||||
let catalog = self.catalog();
|
||||
let face = catalog.by_postscript_name(name)?;
|
||||
if face.source != Source::Bundled {
|
||||
return Some(face);
|
||||
}
|
||||
let key = norm(name);
|
||||
let named = |f: &&FaceInfo| f.source != Source::Bundled && f.postscript_name.as_deref().is_some_and(|p| norm(p) == key);
|
||||
Some(self.catalog.faces().iter().find(named).cloned().unwrap_or(face))
|
||||
Some(catalog.faces().iter().find(named).cloned().unwrap_or(face))
|
||||
}
|
||||
|
||||
/// Load the files of the installed `family`, every cataloged version of its faces included,
|
||||
@@ -317,8 +318,9 @@ impl FontDb {
|
||||
/// thread meanwhile.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn load_cataloged(&self, family: &str) -> bool {
|
||||
let faces = self.catalog.styles(family);
|
||||
let mut paths: Vec<PathBuf> = faces.iter().flat_map(|f| self.catalog.versions(f)).filter_map(|f| f.path).collect();
|
||||
let catalog = self.catalog();
|
||||
let faces = catalog.styles(family);
|
||||
let mut paths: Vec<PathBuf> = faces.iter().flat_map(|f| catalog.versions(f)).filter_map(|f| f.path).collect();
|
||||
paths.sort();
|
||||
paths.dedup();
|
||||
let files = paths.into_iter().flat_map(|p| font_files(&p).into_iter().map(move |data| (data, Some(p.clone()))));
|
||||
@@ -403,7 +405,7 @@ impl FontDb {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn is_cataloged(&self, family: &str, style: &str) -> bool {
|
||||
let ns = norm(style);
|
||||
self.catalog.styles(family).iter().any(|f| norm(&f.style) == ns)
|
||||
self.catalog().styles(family).iter().any(|f| norm(&f.style) == ns)
|
||||
}
|
||||
|
||||
/// The loaded face with [`FontFace::id`] `id` (the face a laid-out glyph came from), an
|
||||
@@ -680,7 +682,7 @@ impl FontDb {
|
||||
}
|
||||
sys.file_searches += 1;
|
||||
}
|
||||
let mut paths: Vec<PathBuf> = self.catalog.faces().iter().filter_map(|f| f.path.clone()).collect();
|
||||
let mut paths: Vec<PathBuf> = self.catalog().faces().iter().filter_map(|f| f.path.clone()).collect();
|
||||
paths.sort();
|
||||
paths.dedup();
|
||||
for p in paths {
|
||||
|
||||
Reference in new issue
Block a user