Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Open: a file without type never waits for the font scan (#7332)

Higgs32584f6ed751a27ff
diff --git a/crates/engine/src/jobs.rs b/crates/engine/src/jobs.rs
index 0ee057317..086772d09 100644
--- a/crates/engine/src/jobs.rs
+++ b/crates/engine/src/jobs.rs
@@ -532,6 +532,15 @@ impl Session {
})?;
ctx.progress(0.95, "Preparing layers");
warm_layer_bounds(&imported.document, || ctx.check())?;
+ // Type layers need the shared text engine, whose first use waits for the
+ // installed-font scan started at launch (minutes on some machines). Wait here,
+ // so the shell's missing-font check after the open finds it ready instead of
+ // freezing the window (#7332).
+ if crate::type_extra_cmds::has_type_layers(&imported.document) {
+ ctx.progress(0.97, "Loading fonts");
+ let _ = photocraft_text::shared();
+ ctx.check()?;
+ }
Ok(imported)
},
move |s, mut r: photocraft_io::ImportResult| {
diff --git a/crates/engine/src/type_extra_cmds.rs b/crates/engine/src/type_extra_cmds.rs
index 91d75f033..c431a3081 100644
--- a/crates/engine/src/type_extra_cmds.rs
+++ b/crates/engine/src/type_extra_cmds.rs
@@ -527,16 +527,39 @@ fn style_font_missing(fonts: &mut photocraft_text::FontDb, style: &photocraft_do
true
}
-/// Font families used by type layers that the font database cannot supply.
+/// The document's type layers, inside groups too.
+fn type_layers(doc: &Document) -> Vec<&TextLayer> {
+ doc.walk()
+ .into_iter()
+ .filter_map(|(_, _, l)| match &l.content {
+ LayerContent::Text(t) => Some(t),
+ _ => None,
+ })
+ .collect()
+}
+
+/// Whether `doc` has a type layer. Only then does checking its fonts need the shared text engine,
+/// whose first use waits for the installed-font scan started at launch: minutes on some machines
+/// (#7332).
+pub fn has_type_layers(doc: &Document) -> bool {
+ doc.walk().iter().any(|(_, _, l)| matches!(l.content, LayerContent::Text(_)))
+}
+
+/// Font families used by type layers that the font database cannot supply. A document without
+/// type returns at once, without touching the shared text engine: the shell asks this on the UI
+/// thread for every file it opens, and waiting there for the font scan froze the window on the
+/// first open after launch (#7332).
pub fn missing_fonts(doc: &Document) -> Vec<String> {
+ let layers = type_layers(doc);
+ if layers.is_empty() {
+ return Vec::new();
+ }
let mut engine = photocraft_text::shared().lock().unwrap_or_else(|e| e.into_inner());
let mut out: Vec<String> = Vec::new();
- for (_, _, l) in doc.walk() {
- if let LayerContent::Text(t) = &l.content {
- for r in t.char_runs() {
- if style_font_missing(&mut engine.fonts, &r.style) && !out.contains(&r.style.font_family) {
- out.push(r.style.font_family);
- }
+ for t in layers {
+ for r in t.char_runs() {
+ if style_font_missing(&mut engine.fonts, &r.style) && !out.contains(&r.style.font_family) {
+ out.push(r.style.font_family);
}
}
}
diff --git a/crates/engine/src/type_extra_cmds/tests.rs b/crates/engine/src/type_extra_cmds/tests.rs
index 687a56471..ae913555f 100644
--- a/crates/engine/src/type_extra_cmds/tests.rs
+++ b/crates/engine/src/type_extra_cmds/tests.rs
@@ -438,3 +438,24 @@ fn camel_case_and_underscore_postscript_names_resolve_to_their_installed_faces()
assert!(!style_font_missing(&mut fonts, &style), "{ps} stored as {guess:?} is not missing");
}
}
+
+/// #7332: the shell asks for an opened file's missing fonts on the UI thread. A document without
+/// type must answer without the shared text engine, whose first use waits for the installed-font
+/// scan (minutes on some machines). The engine is held locked meanwhile, which used to block it.
+#[test]
+fn a_document_without_type_never_waits_for_the_text_engine() {
+ let s = session(8);
+ let doc = (*s.active().unwrap().doc).clone();
+ assert!(!has_type_layers(&doc));
+ let held = photocraft_text::shared().lock().unwrap_or_else(|e| e.into_inner());
+ let (tx, rx) = std::sync::mpsc::channel();
+ std::thread::spawn(move || {
+ let _ = tx.send(missing_font_warnings(&doc));
+ });
+ let got = rx.recv_timeout(std::time::Duration::from_secs(10));
+ drop(held);
+ assert_eq!(got, Ok(Vec::<String>::new()), "a document without type waited for the text engine");
+ let mut s = session(8);
+ text_layer(&mut s, "type");
+ assert!(has_type_layers(&s.active().unwrap().doc));
+}