From 4253f17c6cc8b4380fb045b4018682d954b5ea75 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 16 Nov 2026 09:45:35 -0800 Subject: [PATCH] Font menu: the Document filter looks up each run's family with its style A run whose family and style were split from a PostScript name, such as Honoka and Mincho, now counts under the family of the installed font of that name, as the font lookup finds it with both values. The wheel's order over the closed field stays as it was listed at the first notch until the pointer leaves the field or the selection changes, the Document filter's families included. --- crates/ui-egui/src/font_menu.rs | 32 +++++++++++++++++++-------- crates/ui-egui/src/font_menu/tests.rs | 21 ++++++++++++++++++ 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/crates/ui-egui/src/font_menu.rs b/crates/ui-egui/src/font_menu.rs index f7dacfce7d..911d183215 100644 --- a/crates/ui-egui/src/font_menu.rs +++ b/crates/ui-egui/src/font_menu.rs @@ -407,7 +407,10 @@ struct MenuState { filters: f32, /// The families the wheel steps through, listed at its first notch over the closed field and /// kept for that text selection while the pointer stays on it: each step makes its family a recent one, which would - /// otherwise move it in the order. + /// otherwise move it in the order. The Document filter's families are kept the same way: a + /// step that leaves a family unused keeps it in the order, so turning the wheel back returns + /// to it. They are listed again at the first notch after the pointer leaves the field or the + /// selection changes. wheel_order: Option>, wheel_target: Option, } @@ -447,16 +450,26 @@ impl Filters { /// The families the type in `doc` is set in, lowercase and by the name the menu lists them under /// ([`vectorcraft_text::FontDb::canonical`]): the runs of every text object in the layers, symbols, /// patterns and graph designs, hidden, locked and masked ones included, with a composite font named -/// as such. A pattern being edited is read from its editing layer only. Styles no type uses are left -/// out. +/// as such. A pattern being edited is read from its editing layer only. Each run's family is looked +/// up with its style, so a family and style split from a PostScript name (`Honoka`, `Mincho`) find +/// the installed font of that name. Styles no type uses are left out. fn document_families(db: &vectorcraft_text::FontDb, doc: &vectorcraft_doc::Document) -> HashSet { - // Each name once, so a document with many runs in a few families looks each family up once. - let mut named: HashSet = HashSet::new(); + // Each family and style once, so a document with many runs in a few fonts looks each one up + // once. + let mut named: HashMap> = HashMap::new(); let mut add = |n: &vectorcraft_doc::Node| { if let vectorcraft_doc::NodeKind::Text(t) = &n.kind { for r in &t.runs { - if !named.contains(&r.style.font_family) { - named.insert(r.style.font_family.clone()); + let (family, style) = (&r.style.font_family, &r.style.font_style); + match named.get_mut(family) { + Some(styles) => { + if !styles.contains(style) { + styles.insert(style.clone()); + } + } + None => { + named.insert(family.clone(), HashSet::from([style.clone()])); + } } } } @@ -467,7 +480,7 @@ fn document_families(db: &vectorcraft_text::FontDb, doc: &vectorcraft_doc::Docum for n in doc.layers.iter().map(|l| &**l).chain(doc.symbols.iter().map(|s| &*s.art)).chain(patterns).chain(designs) { vectorcraft_doc::swatches::walk_with_masks(n, &mut add); } - named.into_iter().map(|f| db.canonical(&f, "").0.to_lowercase()).collect() + named.iter().flat_map(|(f, styles)| styles.iter().map(move |s| db.canonical(f, s).0.to_lowercase())).collect() } /// The families the Document filter lists, when it is on and a document is open: those of @@ -613,7 +626,8 @@ fn listed(db: &vectorcraft_text::FontDb, f: &str, query: &str, st: &MenuState, l } /// The families the closed field's wheel steps through, in the open menu's order with no search: -/// the recent families first, then the others, under the menu's filters. +/// the recent families first, then the others, under the menu's filters. Listed once per wheel +/// gesture ([`MenuState`]'s `wheel_order`). fn wheel_order(st: &mut MenuState, look: MenuLook) -> Vec { let db = vectorcraft_text::FontDb::global(); let used = used_families(db, st, look); diff --git a/crates/ui-egui/src/font_menu/tests.rs b/crates/ui-egui/src/font_menu/tests.rs index b1394f77f3..60c0dcea16 100644 --- a/crates/ui-egui/src/font_menu/tests.rs +++ b/crates/ui-egui/src/font_menu/tests.rs @@ -1221,6 +1221,27 @@ fn the_document_filter_counts_type_in_graph_designs() { assert_eq!(names, ["inter", "jetbrains mono"]); } +/// A run whose family and style were split from a PostScript name (as an importer without the font +/// splits one) counts under the family of the installed font of that name, found with its style. +#[test] +fn the_document_filter_finds_a_postscript_name_split_into_family_and_style() { + let mut app = app_with_type(&[]); + let id = app.run("text.create", serde_json::json!({"x": 20, "y": 90, "text": "Split", "font": "Inter"})).unwrap()["id"].as_u64().unwrap(); + let mut art = app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).cloned().expect("the new type"); + app.run("edit.undo", serde_json::json!({})).unwrap(); + let vectorcraft_doc::NodeKind::Text(t) = &mut art.kind else { panic!("type") }; + for r in &mut t.runs { + // SourceSans3-Semibold, split after "Semi". + r.style.font_family = "SourceSans3Semi".into(); + r.style.font_style = "bold".into(); + } + let mut doc = (*app.session.active().unwrap().doc).clone(); + doc.graph_designs.push(vectorcraft_doc::GraphDesign { name: "Split".into(), art: std::sync::Arc::new(art) }); + let db = vectorcraft_text::FontDb::global(); + let names: Vec<_> = super::document_families(db, &doc).into_iter().collect(); + assert_eq!(names, ["source sans 3"]); +} + /// The Document filter combines with the others and with Recent Fonts, follows edits and undo /// without the menu reopening, and steers the closed field's wheel. #[test]