diff --git a/crates/ui-egui/src/text/served_fonts.rs b/crates/ui-egui/src/text/served_fonts.rs index e61d66e0c6..001b361fba 100644 --- a/crates/ui-egui/src/text/served_fonts.rs +++ b/crates/ui-egui/src/text/served_fonts.rs @@ -18,6 +18,25 @@ use crate::PhotocraftApp; /// Layers whose document was busy with a background job when their font arrived. static PENDING: Mutex> = Mutex::new(Vec::new()); +/// The served catalog is process-wide, and every family added while a font menu is open adds a +/// row that egui styles on its next pass, so `Harness::run` takes one more pass per family. Tests +/// that add families hold this for writing ([`changing_catalog`]), and tests that wait for an +/// open font menu to settle hold it for reading ([`steady_catalog`]). +#[cfg(test)] +static CATALOG: std::sync::RwLock<()> = std::sync::RwLock::new(()); + +/// Held by a test while it adds families to the served catalog. +#[cfg(test)] +pub(crate) fn changing_catalog() -> std::sync::RwLockWriteGuard<'static, ()> { + CATALOG.write().unwrap_or_else(PoisonError::into_inner) +} + +/// Held by a test while it waits for an open font menu to settle: no family is added meanwhile. +#[cfg(test)] +pub(crate) fn steady_catalog() -> std::sync::RwLockReadGuard<'static, ()> { + CATALOG.read().unwrap_or_else(PoisonError::into_inner) +} + /// Served script fallbacks in the UI's font stack, by egui font name, unscaled: kept so the stack /// `theme::install_fonts` rebuilds on a language change includes them again. static UI_FACES: Mutex)>> = Mutex::new(Vec::new()); @@ -204,7 +223,10 @@ mod tests { fn a_layer_in_a_script_redraws_when_that_scripts_fallback_arrives() { // Thai: other tests use other scripts (the served catalog is process-wide). let thai = "Served Test Thai"; - photocraft_text::served::add_fonts(&parse_manifest(&format!("{thai} | Regular | fonts/t/T.ttf | Thai\n")).0); + { + let _catalog = changing_catalog(); + photocraft_text::served::add_fonts(&parse_manifest(&format!("{thai} | Regular | fonts/t/T.ttf | Thai\n")).0); + } let layer = |text: &str| TextLayer { text: text.into(), font_family: "Inter".into(), ..Default::default() }; assert!(redraws_for(&layer("สวัสดี"), &[thai.into()]), "Thai text set in Inter falls back to it"); assert!(!redraws_for(&layer("hello"), &[thai.into()])); @@ -276,7 +298,12 @@ mod tests { return; } let (jpan, hans) = ("Served Test Jpan", "Served Test Hans"); - photocraft_text::served::add_fonts(&parse_manifest(&format!("{jpan} | Regular | fonts/j/J.ttf | Jpan\n{hans} | Regular | fonts/h/H.ttf | Hans\n")).0); + { + let _catalog = changing_catalog(); + photocraft_text::served::add_fonts( + &parse_manifest(&format!("{jpan} | Regular | fonts/j/J.ttf | Jpan\n{hans} | Regular | fonts/h/H.ttf | Hans\n")).0, + ); + } crate::i18n::set_current(crate::i18n::Lang::from_code("zh-hans").expect("Simplified Chinese")); let ctx = egui::Context::default(); crate::theme::install_fonts(&ctx); diff --git a/crates/ui-egui/src/tools/type_font_preview.rs b/crates/ui-egui/src/tools/type_font_preview.rs index a334642c99..7f7c4ccfbb 100644 --- a/crates/ui-egui/src/tools/type_font_preview.rs +++ b/crates/ui-egui/src/tools/type_font_preview.rs @@ -198,7 +198,10 @@ mod tests { // Families the host serves but hasn't sent (own names: the served catalog is process-wide). let (a, b) = ("Served Hover Rest A", "Served Hover Rest B"); let (fonts, _) = photocraft_text::served::parse_manifest(&format!("{a} | Regular | fonts/a/A.ttf\n{b} | Regular | fonts/b/B.ttf\n")); - photocraft_text::served::add_fonts(&fonts); + { + let _catalog = crate::served_fonts::changing_catalog(); + photocraft_text::served::add_fonts(&fonts); + } let mut app = app_with_type_layer(); let ctx = egui::Context::default(); diff --git a/crates/ui-egui/src/tools/type_tool/canvas_tests.rs b/crates/ui-egui/src/tools/type_tool/canvas_tests.rs index 96c0551955..d6f9ac41f4 100644 --- a/crates/ui-egui/src/tools/type_tool/canvas_tests.rs +++ b/crates/ui-egui/src/tools/type_tool/canvas_tests.rs @@ -1905,7 +1905,10 @@ fn variable_font_axes_become_sliders_with_the_drawn_value() { /// Families the host serves are in the font menu before they are fetched. #[test] fn the_font_menu_lists_served_families() { - photocraft_text::served::add_families(["Served Menu Test Serif".to_string()]); + { + let _catalog = crate::served_fonts::changing_catalog(); + photocraft_text::served::add_families(["Served Menu Test Serif".to_string()]); + } assert!(super::families().iter().any(|f| f == "Served Menu Test Serif")); } @@ -2186,6 +2189,7 @@ mod multi_layer_formatting { #[test] fn options_font_family_preserves_peer_faces_and_batches_layer_flags() { + let _catalog = crate::served_fonts::steady_catalog(); let (app, ids) = fixture(); let before = ids.map(|id| style(&app, id, 0)); let mut h = controls(app, false); @@ -2284,6 +2288,7 @@ mod multi_layer_formatting { /// Here both happen on every pass. #[test] fn the_font_menu_settles_while_other_engines_register_fonts() { + let _catalog = crate::served_fonts::changing_catalog(); let (app, _) = fixture(); let mut pass = 0u32; let mut h = Harness::builder().with_size(vec2(1100.0, 800.0)).build_ui_state( @@ -2310,6 +2315,7 @@ mod multi_layer_formatting { /// layers selected, sets them and is kept for new type, as the options bar's menu does. #[test] fn a_style_picked_in_the_character_sections_font_menu_is_kept_for_new_type() { + let _catalog = crate::served_fonts::steady_catalog(); let (app, ids) = fixture(); // The first style row under the family (so it is in view), other than the defaults'. let pick = super::super::styles("Inter").into_iter().find(|s| s != "Regular" && s != "Bold Italic").expect("Inter has several styles");