mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 13:33:51 +00:00
UI fonts: a font search that cannot start keeps its characters
When the thread for a fallback font search could not be created, the characters taken for that search were lost. Characters no font covered were already removed from the retry list, and characters met that frame were already marked seen, so neither was looked for again. Now the characters no font covered go back to the retry list, and the ones met that frame are looked for when they are painted again. A web test also checks that a face fetched again after a failure is not fetched a second time at once, whether the interface asks for its script again or the retry timer fires.
This commit is contained in:
@@ -110,6 +110,21 @@ mod tests {
|
||||
assert!(d.start(&[1], 1e12).is_empty());
|
||||
}
|
||||
|
||||
/// While a face is fetched again after a failure, neither the interface asking for its script
|
||||
/// again nor the retry timer starts a second fetch of it.
|
||||
#[test]
|
||||
fn a_face_being_fetched_again_is_not_fetched_twice_at_once() {
|
||||
let mut d = Demand::default();
|
||||
d.start(&[0], 0.0);
|
||||
assert_eq!(d.finished(0, false, 0.0), Some(RETRY_MS));
|
||||
let retry = f64::from(RETRY_MS);
|
||||
assert_eq!(d.start(&[0, 0], retry), [0], "one fetch for a face wanted twice");
|
||||
assert!(d.start(&[0], retry + 1.0).is_empty(), "the interface asking again");
|
||||
assert!(d.start(&[0], retry + 1_000.0).is_empty(), "the retry timer");
|
||||
assert_eq!(d.finished(0, true, retry + 2_000.0), None);
|
||||
assert!(d.start(&[0], 1e12).is_empty(), "loaded");
|
||||
}
|
||||
|
||||
/// A clock that reads earlier than the failure (one set back meanwhile) does not hold the
|
||||
/// retry back: the timer that asks for it has waited already.
|
||||
#[test]
|
||||
|
||||
@@ -71,6 +71,9 @@ pub(crate) struct UiFonts {
|
||||
/// The faces covering the characters being looked for (native: looked for on a thread).
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pending: Option<std::sync::mpsc::Receiver<Found>>,
|
||||
/// The search thread can't be created (tests).
|
||||
#[cfg(all(test, not(target_arch = "wasm32")))]
|
||||
spawn_fails: bool,
|
||||
}
|
||||
|
||||
impl UiFonts {
|
||||
@@ -93,14 +96,18 @@ impl UiFonts {
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let idle = true;
|
||||
// A font added since (a web face fetched for them) may cover them now.
|
||||
if idle && !self.uncovered.is_empty() && db.generation() != self.searched {
|
||||
missing.extend(self.uncovered.drain());
|
||||
let retry: Vec<char> =
|
||||
if idle && !self.uncovered.is_empty() && db.generation() != self.searched { self.uncovered.drain().collect() } else { vec![] };
|
||||
if !retry.is_empty() {
|
||||
missing.extend_from_slice(&retry);
|
||||
missing.sort_unstable();
|
||||
missing.dedup();
|
||||
}
|
||||
if missing.is_empty() {
|
||||
return;
|
||||
}
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let looked_for = missing.clone();
|
||||
let find = move || search(db, missing, FontDb::face_covering);
|
||||
// Native: the installed fonts are read off the UI thread. Characters met meanwhile wait for
|
||||
// the next search (they aren't marked seen until then).
|
||||
@@ -108,13 +115,20 @@ impl UiFonts {
|
||||
{
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let repaint = ctx.clone();
|
||||
let spawned = std::thread::Builder::new().name("ui-font-fallback".into()).spawn(move || {
|
||||
let spawned = self.spawn_search(move || {
|
||||
// The receiver is gone only when the app is.
|
||||
let _ = tx.send(find());
|
||||
repaint.request_repaint();
|
||||
});
|
||||
if spawned.is_ok() {
|
||||
self.pending = Some(rx);
|
||||
} else {
|
||||
// Not looked for: the characters met this frame are looked for when painted again,
|
||||
// and the ones no font covered wait for the fonts to change again.
|
||||
for c in looked_for.iter().filter(|c| !retry.contains(c)) {
|
||||
self.seen.remove(c);
|
||||
}
|
||||
self.uncovered.extend(retry);
|
||||
}
|
||||
}
|
||||
// The web has only the loaded fonts to look through: quick, on this thread.
|
||||
@@ -122,6 +136,16 @@ impl UiFonts {
|
||||
self.found(ctx, find(), report);
|
||||
}
|
||||
|
||||
/// Run `search` on a thread of its own.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn spawn_search(&self, search: impl FnOnce() + Send + 'static) -> std::io::Result<()> {
|
||||
#[cfg(test)]
|
||||
if self.spawn_fails {
|
||||
return Err(std::io::Error::other("no thread for the font search"));
|
||||
}
|
||||
std::thread::Builder::new().name("ui-font-fallback".into()).spawn(search).map(|_| ())
|
||||
}
|
||||
|
||||
/// Add the faces a search found, and keep (and report) the characters it found no face for.
|
||||
fn found(&mut self, ctx: &egui::Context, found: Found, report: fn(&[char])) {
|
||||
self.install(ctx, &found.faces);
|
||||
@@ -543,4 +567,32 @@ mod tests {
|
||||
assert!(has_glyph(&ctx, PRIVATE_USE_CHAR), "the added face draws the character");
|
||||
assert!(fonts.uncovered.is_empty());
|
||||
}
|
||||
|
||||
/// A search whose thread can't be created leaves its characters to be looked for later: a
|
||||
/// character painted for the first time is looked for when it is painted again, and one no font
|
||||
/// covered stays waiting for the fonts to change.
|
||||
#[test]
|
||||
fn a_search_that_cannot_start_keeps_its_characters() {
|
||||
use vectorcraft_text::test_fonts::{PRIVATE_USE_CHAR, private_use_font};
|
||||
let db: &'static FontDb = Box::leak(Box::new(FontDb::with_font_dirs(vec![])));
|
||||
db.families();
|
||||
let ctx = egui::Context::default();
|
||||
crate::theme::install_fonts(&ctx);
|
||||
let mut fonts = UiFonts { spawn_fails: true, ..UiFonts::default() };
|
||||
let text = format!("Layer {PRIVATE_USE_CHAR}");
|
||||
frame_in(&ctx, &mut fonts, &text, db);
|
||||
assert!(fonts.pending.is_none() && !fonts.seen.contains(&PRIVATE_USE_CHAR), "not looked for yet");
|
||||
fonts.spawn_fails = false;
|
||||
frame_in(&ctx, &mut fonts, &text, db);
|
||||
assert!(fonts.uncovered.contains(&PRIVATE_USE_CHAR), "looked for when painted again");
|
||||
db.add_font(private_use_font().expect("the test face builds"));
|
||||
fonts.spawn_fails = true;
|
||||
frame_in(&ctx, &mut fonts, &text, db);
|
||||
assert!(fonts.uncovered.contains(&PRIVATE_USE_CHAR), "still waiting to be looked for again");
|
||||
fonts.spawn_fails = false;
|
||||
frame_in(&ctx, &mut fonts, &text, db);
|
||||
frame_in(&ctx, &mut fonts, &text, db);
|
||||
assert!(has_glyph(&ctx, PRIVATE_USE_CHAR), "the added face draws the character");
|
||||
assert!(fonts.uncovered.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user