fix(fonts): discover named instances before listing paragraph styles (#1246)

For a catalog-only variable family, named instances are discovered before the Font Style combo and the Family menu list their styles, so the first opening is complete. Styles are cached once per popup opening. Synthetic fvar regressions run by default.

Related to #1210.
This commit is contained in:
makesomethingshit authored and GitHub committed 2026-11-18 16:30:06 +07:00
1 parent 3930cff80d
commit b1bf440cbb
4 files changed
+481 -27

No files matched your search

+25
View File
@@ -326,3 +326,28 @@ pub fn with_meta(font: &[u8], dlng: Option<&str>, slng: Option<&str>) -> Option<
pub fn installed_face(bytes: Vec<u8>, path: std::path::PathBuf) -> Option<crate::FontFace> {
crate::FontDb::with_font_dirs(vec![]).new_faces(bytes, crate::FontSource::Installed(path)).into_iter().next()
}
/// A synthetic weight-variable font with Regular and SemiBold named instances, using bundled outlines.
/// Only discovery is variable; the outlines do not change with weight.
pub fn variable_font(family: &str) -> Option<Vec<u8>> {
let base =
font_with_names(&[(1, family), (2, "Regular"), (6, "SyntheticVF-Regular"), (256, "Weight"), (257, "Regular"), (258, "SemiBold")], &['X'])?;
// fvar 1.0: one weight axis and two named instances, without PostScript name IDs.
let mut fvar = Vec::new();
for v in [1u16, 0, 16, 2, 1, 20, 2, 8] {
fvar.extend_from_slice(&v.to_be_bytes());
}
fvar.extend_from_slice(b"wght");
for v in [100i32 << 16, 400 << 16, 900 << 16] {
fvar.extend_from_slice(&v.to_be_bytes());
}
for v in [0u16, 256] {
fvar.extend_from_slice(&v.to_be_bytes());
}
for (id, weight) in [(257u16, 400i32), (258, 600)] {
fvar.extend_from_slice(&id.to_be_bytes());
fvar.extend_from_slice(&0u16.to_be_bytes());
fvar.extend_from_slice(&(weight << 16).to_be_bytes());
}
with_tables(&base, vec![(*b"fvar", fvar)])
}
+300 -26
View File
@@ -7693,16 +7693,21 @@ fn char_number(ui: &mut egui::Ui, d: &mut Dialog, cf: &CharFields, key: &str, su
fn char_choice(lang: &str, ui: &mut egui::Ui, d: &mut Dialog, cf: &CharFields, key: &str, opts: &[(Value, &str)]) {
let cur = cf.get(d, key);
let shown = opts.iter().find(|o| o.0 == cur).map_or("", |o| crate::i18n::tr(lang, o.1));
craft_ui::ComboBox::from_id_salt(("cs", key)).selected_text(crate::rtl::widget(ui, shown)).width(140.0).show_ui(ui, |ui| {
if cf.sparse && ui.selectable_label(cur.is_null(), " ").clicked() {
cf.set(d, key, Value::Null);
}
for (v, label) in opts {
if ui.selectable_label(*v == cur, crate::rtl::widget(ui, crate::i18n::tr(lang, label))).clicked() {
cf.set(d, key, v.clone());
craft_ui::ComboBox::from_id_salt(("cs", key))
.selected_text(crate::rtl::widget(ui, shown))
.width(140.0)
.show_ui(ui, |ui| {
if cf.sparse && ui.selectable_label(cur.is_null(), " ").clicked() {
cf.set(d, key, Value::Null);
}
}
});
for (v, label) in opts {
if ui.selectable_label(*v == cur, crate::rtl::widget(ui, crate::i18n::tr(lang, label))).clicked() {
cf.set(d, key, v.clone());
}
}
})
.response
.accessible_name(setting_label(lang, key));
}
/// An on/off attribute; for a character style a third, mixed state leaves it unset (clicks go
@@ -7729,6 +7734,42 @@ fn char_swatch(lang: &str, ui: &mut egui::Ui, d: &mut Dialog, cf: &CharFields, d
char_choice(lang, ui, d, cf, key, &opts);
}
/// Load styles once per popup opening, including when the selected family is missing.
fn font_style_combo(
ui: &mut egui::Ui,
fonts: &designcraft_fonts::ScopedFonts<'_>,
scope: u32,
family: &str,
style: &str,
d: &mut Dialog,
cf: &CharFields,
) -> egui::Response {
let key = ui.make_persistent_id(("pssty_styles", scope));
let frame = ui.ctx().cumulative_frame_nr();
let shown = craft_ui::ComboBox::from_id_salt("pssty").selected_text(style).width(200.0).show_ui(ui, |ui| {
let cached = ui.data(|data| data.get_temp::<(String, u64, Vec<String>)>(key));
let styles = if let Some((_, _, styles)) = cached.filter(|(f, last, _)| f == family && last.saturating_add(1) >= frame) {
styles
} else {
let _ = fonts.face(family, style);
fonts.styles(family)
};
ui.data_mut(|data| data.insert_temp(key, (family.to_string(), frame, styles.clone())));
if cf.sparse && ui.selectable_label(style.is_empty(), " ").clicked() {
cf.set(d, "fontStyle", Value::Null);
}
for s in styles {
if ui.selectable_label(s == style, &s).clicked() {
cf.set(d, "fontStyle", json!(s));
}
}
});
if shown.inner.is_none() {
ui.data_mut(|data| data.remove::<(String, u64, Vec<String>)>(key));
}
shown.response
}
/// Basic Character Formats: font, size, leading, kerning, tracking, case, position and the
/// underline, strikethrough, ligatures and no-break switches.
fn basic_character_formats(app: &mut DesignApp, ui: &mut egui::Ui, d: &mut Dialog, cf: &CharFields) {
@@ -7738,7 +7779,7 @@ fn basic_character_formats(app: &mut DesignApp, ui: &mut egui::Ui, d: &mut Dialo
let menu = crate::panels::font_menu(app);
// Two columns, as InDesign's: the font, then Size | Leading, Kerning | Tracking, Case | Position.
egui::Grid::new("psc").num_columns(2).spacing([8.0, 6.0]).show(ui, |ui| {
crate::rtl::label(ui, crate::i18n::tr(lang, "Font Family:"));
let name = crate::rtl::label(ui, crate::i18n::tr(lang, "Font Family:"));
let fam = cf.get(d, "fontFamily").as_str().unwrap_or("").to_string();
let shown = if fam.is_empty() { "—".to_string() } else { crate::panels::font_label(app, &menu, &fam) };
let picked = craft_ui::ComboBox::from_id_salt("psfam")
@@ -7755,35 +7796,25 @@ fn basic_character_formats(app: &mut DesignApp, ui: &mut egui::Ui, d: &mut Dialo
crate::panels::font_menu_header(app, ui, &menu, &fam, 200.0)
},
crate::panels::font_menu_list,
)
.inner
.flatten();
if let Some(f) = picked {
);
picked.response.labelled_by(name.id);
if let Some(f) = picked.inner.flatten() {
cf.set(d, "fontFamily", json!(f.family));
if let Some(style) = f.style {
cf.set(d, "fontStyle", json!(style));
}
}
ui.end_row();
crate::rtl::label(ui, crate::i18n::tr(lang, "Font Style:"));
let name = crate::rtl::label(ui, crate::i18n::tr(lang, "Font Style:"));
let sty = cf.get(d, "fontStyle").as_str().unwrap_or("").to_string();
if cf.sparse && fam.is_empty() {
// No family to list styles of: type the style ("Italic" in whatever font the text has).
let mut s = sty.clone();
if ui.add(egui::TextEdit::singleline(&mut s).desired_width(200.0)).changed() {
if ui.add(egui::TextEdit::singleline(&mut s).desired_width(200.0)).labelled_by(name.id).changed() {
cf.set(d, "fontStyle", if s.trim().is_empty() { Value::Null } else { json!(s) });
}
} else {
craft_ui::ComboBox::from_id_salt("pssty").selected_text(&sty).width(200.0).show_ui(ui, |ui| {
if cf.sparse && ui.selectable_label(sty.is_empty(), " ").clicked() {
cf.set(d, "fontStyle", Value::Null);
}
for s in fonts.styles(&fam) {
if ui.selectable_label(s == sty, &s).clicked() {
cf.set(d, "fontStyle", json!(s));
}
}
});
font_style_combo(ui, &fonts, crate::panels::font_scope(app), &fam, &sty, d, cf).labelled_by(name.id);
}
ui.end_row();
});
@@ -10317,3 +10348,246 @@ fn picker_field(ui: &mut egui::Ui, d: &mut Dialog, r: egui::Rect, key: &str, nam
d.fields.insert(edit_key, json!(text));
out
}
#[cfg(test)]
mod variable_style_regression {
use super::*;
#[test]
#[ignore = "requires the pinned Bricolage fixture; see docs/testing-variable-fonts.md"]
fn named_instance_style_widget_and_api_agree() {
use egui_kittest::{Harness, kittest::Queryable};
let path = std::env::var_os("DESIGNCRAFT_TEST_VARIABLE_FONT").expect("set DESIGNCRAFT_TEST_VARIABLE_FONT");
let db = designcraft_fonts::FontDb::global();
db.add_font(std::fs::read(path).unwrap());
let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({})).unwrap();
app.run("style.paragraph.create", json!({"name":"Base VF", "chars":{"fontFamily":"Bricolage Grotesque", "fontStyle":"Regular"}})).unwrap();
app.run("style.paragraph.create", json!({"name":"UI VF", "basedOn":"Base VF"})).unwrap();
app.run("style.paragraph.create", json!({"name":"API VF", "basedOn":"Base VF"})).unwrap();
let base = json!({"fontFamily":"Bricolage Grotesque", "fontStyle":"Regular"});
let mut h = Harness::builder()
.with_context_setup(|ctx| {
crate::theme::install_fonts(ctx, "");
})
.with_size(egui::vec2(1000.0, 800.0))
.build_ui_state(
move |ui, state: &mut (DesignApp, Dialog)| {
basic_character_formats(&mut state.0, ui, &mut state.1, &CharFields { base: &base, sparse: false });
},
(app, Dialog::new("paragraphStyleOptions", json!({"name":"UI VF"}))),
);
h.run_steps(3);
{
let (app, d) = h.state_mut();
let before = app.session.doc().unwrap().doc.clone();
let undo = app.session.doc().unwrap().history.undo.len();
d.fields.insert("preview".into(), json!(true));
d.fields.insert("c.fontStyle".into(), json!("SemiBold"));
style_options_preview(app, d);
assert_eq!(app.session.doc().unwrap().doc.styles.resolve_para_style("UI VF").1.font_style, "SemiBold");
assert_eq!(app.session.doc().unwrap().history.undo.len(), undo);
d.fields.insert("preview".into(), json!(false));
style_options_preview(app, d);
assert_eq!(app.session.doc().unwrap().doc, before);
d.fields.insert("preview".into(), json!(true));
style_options_preview(app, d);
app.ui.dialog = Some(d.clone());
cancel(app);
assert_eq!(app.session.doc().unwrap().doc, before);
d.fields.remove(PREVIEWED);
d.fields.insert("preview".into(), json!(true));
style_options_preview(app, d);
app.ui.dialog = Some(d.clone());
confirm(app).unwrap();
assert_eq!(app.session.doc().unwrap().history.undo.len(), undo + 1);
app.run("edit.undo", json!({})).unwrap();
assert_eq!(app.session.doc().unwrap().doc, before);
app.run("edit.redo", json!({})).unwrap();
assert_eq!(app.session.doc().unwrap().doc.styles.resolve_para_style("UI VF").1.font_style, "SemiBold");
app.run("edit.undo", json!({})).unwrap();
d.fields.remove(PREVIEWED);
d.fields.remove("c.fontStyle");
d.fields.insert("preview".into(), json!(false));
}
h.run_steps(3);
let mut current = "Regular";
for selected in ["SemiBold", "Bold", "Regular"] {
h.get_by_value(current).click();
h.run_steps(3);
h.get_by_label(selected).click();
h.run_steps(3);
let (app, d) = h.state_mut();
assert_eq!(d.fields["c.fontStyle"], selected);
assert!(!d.fields.contains_key("c.fontFamily"), "inherited family remains inherited");
confirm_paragraph_style_options(app, d).unwrap();
app.run("style.paragraph.edit", json!({"name":"API VF","chars":{"fontStyle":selected}})).unwrap();
let styles = &app.session.doc().unwrap().doc.styles;
assert_eq!(styles.para("UI VF").unwrap().chars, styles.para("API VF").unwrap().chars);
assert_eq!(styles.resolve_para_style("UI VF").1.font_style, selected);
assert_eq!(styles.para("UI VF").unwrap().based_on.as_deref(), Some("Base VF"));
current = selected;
let native = app.run("file.serialize", json!({})).unwrap();
app.run("file.openBytes", json!({"name":"vf.designcraft","base64":native["base64"]})).unwrap();
assert_eq!(app.session.doc().unwrap().doc.styles.resolve_para_style("UI VF").1.font_style, selected);
}
}
}
#[cfg(test)]
mod variable_cold_style_regression {
use super::*;
#[test]
#[ignore = "requires a fresh process with the pinned Bricolage fixture catalogued; see docs/testing-variable-fonts.md"]
fn cold_style_combo_contains_named_instances() {
use egui_kittest::{Harness, kittest::Queryable};
let fonts = designcraft_fonts::FontDb::global();
assert!(
!fonts.styles("Bricolage Grotesque").contains(&"SemiBold".into()),
"run in a fresh process with the test font catalogued but not loaded"
);
let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({})).unwrap();
let base = json!({"fontFamily":"Bricolage Grotesque", "fontStyle":"Regular"});
let mut h = Harness::builder()
.with_context_setup(|ctx| {
crate::theme::install_fonts(ctx, "");
})
.with_size(egui::vec2(1000.0, 800.0))
.build_ui_state(
move |ui, state: &mut (DesignApp, Dialog)| {
basic_character_formats(&mut state.0, ui, &mut state.1, &CharFields { base: &base, sparse: false });
},
(app, Dialog::new("paragraphStyleOptions", json!({}))),
);
h.run_steps(3);
h.get_by_value("Regular").click();
h.run_steps(3);
assert!(h.query_by_label("SemiBold").is_some(), "the cold Style combo must offer named instances");
h.get_by_label("SemiBold").click();
h.run_steps(3);
assert_eq!(h.state().1.fields["c.fontStyle"], "SemiBold");
}
}
#[cfg(test)]
mod synthetic_variable_style_regression {
use super::*;
use egui_kittest::{Harness, kittest::Queryable};
#[test]
fn paragraph_style_formats_select_a_synthetic_named_instance() {
let family = "Synthetic Paragraph Wiring";
designcraft_fonts::FontDb::global().add_font(designcraft_fonts::testing::variable_font(family).unwrap());
let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({})).unwrap();
app.run("style.paragraph.create", json!({"name":"Synthetic UI", "chars":{"fontFamily":family,"fontStyle":"Regular"}})).unwrap();
let base = json!({"fontFamily":family,"fontStyle":"Regular"});
let mut h = Harness::builder()
.with_context_setup(|ctx| {
crate::theme::install_fonts(ctx, "");
})
.with_size(egui::vec2(1000.0, 800.0))
.build_ui_state(
|ui, state: &mut (DesignApp, Dialog)| {
basic_character_formats(&mut state.0, ui, &mut state.1, &CharFields { base: &base, sparse: false });
},
(app, Dialog::new("paragraphStyleOptions", json!({"name":"Synthetic UI"}))),
);
h.run_steps(3);
h.get_by_value("Regular").click();
h.run_steps(3);
h.get_by_label("SemiBold").click();
h.run_steps(3);
let (app, d) = h.state_mut();
confirm_paragraph_style_options(app, d).unwrap();
assert_eq!(app.session.doc().unwrap().doc.styles.resolve_para_style("Synthetic UI").1.font_style, "SemiBold");
}
#[test]
fn cold_style_popup_discovers_instances_and_selects_them() {
let dir = tempfile::tempdir().unwrap();
let family = "Synthetic Cold Style";
std::fs::write(dir.path().join("variable.ttf"), designcraft_fonts::testing::variable_font(family).unwrap()).unwrap();
let db = designcraft_fonts::FontDb::with_font_dirs(vec![dir.path().to_owned()]);
assert_eq!(db.styles(family), ["Regular"]);
let mut h = Harness::builder()
.with_context_setup(|ctx| {
crate::theme::install_fonts(ctx, "");
})
.build_ui_state(
|ui, d: &mut Dialog| {
let style = d.fields.get("c.fontStyle").and_then(Value::as_str).unwrap_or("Regular").to_string();
font_style_combo(ui, &db.scoped(0), 0, family, &style, d, &CharFields { base: &Value::Null, sparse: false })
.accessible_name("Font Style".to_owned());
},
Dialog::new("paragraphStyleOptions", json!({})),
);
h.run_steps(3);
assert_eq!(db.styles(family), ["Regular"], "closed popup must not preload the font");
h.get_by_value("Regular").click();
h.run_steps(3);
assert_eq!(db.styles(family), ["Regular", "SemiBold"]);
h.get_by_label("SemiBold").click();
h.run_steps(3);
assert_eq!(h.state().fields["c.fontStyle"], "SemiBold");
}
#[test]
fn missing_family_styles_are_cached_until_the_popup_reopens() {
for retry in ["reopen", "hidden", "scope"] {
let dir = tempfile::tempdir().unwrap();
let family = "Synthetic Missing Style";
let path = dir.path().join("variable.ttf");
std::fs::write(&path, designcraft_fonts::testing::variable_font(family).unwrap()).unwrap();
let db = designcraft_fonts::FontDb::with_font_dirs(vec![dir.path().to_owned()]);
assert_eq!(db.styles(family), ["Regular"]);
std::fs::remove_file(&path).unwrap();
let mut h = Harness::builder()
.with_context_setup(|ctx| {
crate::theme::install_fonts(ctx, "");
})
.build_ui_state(
|ui, state: &mut (Dialog, bool, u32)| {
if state.1 {
font_style_combo(
ui,
&db.scoped(state.2),
state.2,
family,
"Regular",
&mut state.0,
&CharFields { base: &Value::Null, sparse: false },
)
.accessible_name("Font Style".to_owned());
}
},
(Dialog::new("paragraphStyleOptions", json!({})), true, 0),
);
h.run_steps(3);
h.get_by_value("Regular").click();
h.run_steps(3);
assert!(h.query_by_label("SemiBold").is_none());
std::fs::write(&path, designcraft_fonts::testing::variable_font(family).unwrap()).unwrap();
h.run_steps(3);
assert_eq!(db.styles(family), ["Regular"], "an open popup must not retry the missing font every frame");
match retry {
"hidden" => {
h.state_mut().1 = false;
h.run_steps(3);
h.state_mut().1 = true;
}
"scope" => h.state_mut().2 = 1,
_ => {
egui::Popup::close_all(&h.ctx);
h.run_steps(3);
h.get_by_value("Regular").click();
}
}
h.run_steps(3);
if h.query_by_label("SemiBold").is_none() {
h.get_by_value("Regular").click();
h.run_steps(3);
}
assert!(h.query_by_label("SemiBold").is_some(), "{retry} retries the now-readable font");
}
}
}
+116 -1
View File
@@ -63,13 +63,15 @@ fn regular(styles: &[String]) -> String {
styles.iter().find(|s| *s == "Regular").or(styles.first()).cloned().unwrap_or_else(|| "Regular".into())
}
fn meta(ui: &Ui, fonts: &designcraft_fonts::ScopedFonts<'static>, scope: u32, family: &str) -> FamilyMeta {
fn meta(ui: &Ui, fonts: &designcraft_fonts::ScopedFonts<'_>, scope: u32, family: &str) -> FamilyMeta {
let key = egui::Id::unique(("font_meta", family, scope));
if let Some(m) = ui.data(|d| d.get_temp::<FamilyMeta>(key)) {
return m;
}
let styles = fonts.styles(family);
let kind = fonts.face(family, &regular(&styles)).kind();
// Loading a catalogued variable font discovers its named instances.
let styles = fonts.styles(family);
let m = FamilyMeta { styles, kind };
ui.data_mut(|d| d.insert_temp(key, m.clone()));
m
@@ -730,3 +732,116 @@ mod preview_cache_tests {
assert!(c.get(key(Previews::CAP + 39)).is_some());
}
}
#[cfg(test)]
mod variable_catalog_regression {
#[test]
#[ignore = "requires the pinned Bricolage fixture; see docs/testing-variable-fonts.md"]
fn cold_family_menu_contains_named_instances() {
let path = std::env::var_os("DESIGNCRAFT_TEST_VARIABLE_FONT").expect("set DESIGNCRAFT_TEST_VARIABLE_FONT");
let path = std::path::PathBuf::from(path);
let db = Box::leak(Box::new(designcraft_fonts::FontDb::with_font_dirs(vec![path.parent().unwrap().to_owned()])));
let fonts = db.scoped(0);
assert!(!fonts.styles("Bricolage Grotesque").contains(&"SemiBold".into()), "the fixture must be catalogued but not loaded");
let ctx = egui::Context::default();
let mut output = ctx.run_ui(egui::RawInput::default(), |root| {
egui::CentralPanel::default().show(root, |ui| {
let first = super::meta(ui, &fonts, 0, "Bricolage Grotesque");
assert!(
first.styles.contains(&"SemiBold".into()),
"cold menu cached {:?}, live styles {:?}",
first.styles,
fonts.styles("Bricolage Grotesque")
);
let second = super::meta(ui, &fonts, 0, "Bricolage Grotesque");
assert_eq!(first.styles, second.styles);
});
});
output.textures_delta.clear();
// Exercise the family menu's actual style-row selection too.
use egui_kittest::{Harness, kittest::Queryable};
designcraft_fonts::FontDb::global().add_font(std::fs::read(&path).unwrap());
let app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
let menu: Vec<_> =
designcraft_fonts::FontDb::global().scoped(0).family_infos().into_iter().filter(|f| f.family == "Bricolage Grotesque").collect();
let mut h = Harness::builder()
.with_context_setup(|ctx| {
crate::theme::install_fonts(ctx, "");
})
.with_size(egui::vec2(1000.0, 800.0))
.build_ui_state(
move |ui, state: &mut (crate::DesignApp, Option<super::FontPick>)| {
let key = egui::Id::unique("font_menu_state");
let mut st: super::MenuState = ui.data(|d| d.get_temp(key)).unwrap_or_default();
st.expanded.insert("Bricolage Grotesque".into());
ui.data_mut(|d| d.insert_temp(key, st));
let m = super::font_menu_header(&mut state.0, ui, &menu, "Bricolage Grotesque", 560.0);
if let Some(pick) = super::font_menu_list(ui, m) {
state.1 = Some(pick);
}
},
(app, None),
);
h.run_steps(3);
h.get_by_label("SemiBold").click();
h.run_steps(3);
let pick = h.state().1.as_ref().expect("a style row was picked");
assert_eq!(pick.family, "Bricolage Grotesque");
assert_eq!(pick.style.as_deref(), Some("SemiBold"));
}
}
#[cfg(test)]
mod synthetic_variable_catalog_regression {
#[test]
fn family_menu_selects_a_synthetic_named_instance() {
use egui_kittest::{Harness, kittest::Queryable};
let family = "Synthetic Menu Wiring";
let db = designcraft_fonts::FontDb::global();
db.add_font(designcraft_fonts::testing::variable_font(family).unwrap());
let app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
let menu: Vec<_> = db.scoped(0).family_infos().into_iter().filter(|f| f.family == family).collect();
let mut h = Harness::builder()
.with_context_setup(|ctx| {
crate::theme::install_fonts(ctx, "");
})
.with_size(egui::vec2(1000.0, 800.0))
.build_ui_state(
|ui, state: &mut (crate::DesignApp, Option<super::FontPick>)| {
let key = egui::Id::unique("font_menu_state");
let mut st: super::MenuState = ui.data(|d| d.get_temp(key)).unwrap_or_default();
st.expanded.insert(family.into());
ui.data_mut(|d| d.insert_temp(key, st));
let m = super::font_menu_header(&mut state.0, ui, &menu, family, 560.0);
if let Some(pick) = super::font_menu_list(ui, m) {
state.1 = Some(pick);
}
},
(app, None),
);
h.run_steps(3);
h.get_by_label("SemiBold").click();
h.run_steps(3);
let pick = h.state().1.as_ref().expect("the real Family menu selected a style");
assert_eq!(pick.family, family);
assert_eq!(pick.style.as_deref(), Some("SemiBold"));
}
#[test]
fn cold_family_menu_discovers_and_caches_named_instances() {
let dir = tempfile::tempdir().unwrap();
let family = "Synthetic Cold Menu";
std::fs::write(dir.path().join("variable.ttf"), designcraft_fonts::testing::variable_font(family).unwrap()).unwrap();
let db = designcraft_fonts::FontDb::with_font_dirs(vec![dir.path().to_owned()]);
let fonts = db.scoped(0);
assert_eq!(fonts.styles(family), ["Regular"]);
let ctx = egui::Context::default();
let mut output = ctx.run_ui(egui::RawInput::default(), |ui| {
let first = super::meta(ui, &fonts, 0, family);
assert_eq!(first.styles, ["Regular", "SemiBold"]);
let second = super::meta(ui, &fonts, 0, family);
assert_eq!(first.styles, second.styles);
});
output.textures_delta.clear();
}
}
+40
View File
@@ -0,0 +1,40 @@
# Variable-font paragraph style regressions (#1210)
Default tests generate a small variable-font name/axis table around the existing bundled OFL outlines. They exercise cold catalog discovery in both the Family menu and Style popup, selection, and retry on reopening a popup after a font file was unavailable. No download or system-font installation is required. Run them with `cargo test -p designcraft-ui-egui --lib synthetic_variable`.
The real-font tests remain explicit opt-in tests: missing fonts fail rather than silently pass.
They use SIL OFL 1.1 Bricolage Grotesque from google/fonts commit
`47d54ca1ebb14fd864695f1d39dc734f39017c33`, path
`ofl/bricolagegrotesque/BricolageGrotesque[opsz,wdth,wght].ttf`.
SHA256: `413e7357809ddd12fd80a96a8a396de0e401638d4acd3cb3e37532f0472ac682`.
Download OFL.txt from the same directory; keep both outside this repository.
Set `DESIGNCRAFT_TEST_VARIABLE_FONT` to the absolute font file path. Then run:
```text
cargo test -p designcraft-ui-egui cold_family_menu_contains_named_instances -- --ignored --nocapture
cargo test -p designcraft-ui-egui named_instance_style_widget_and_api_agree -- --ignored --nocapture
```
On Windows, for the cold Style-combo test, make a temporary directory containing
`Microsoft/Windows/Fonts/` with that font inside. Set `LOCALAPPDATA` **only for the test
process** to this temporary directory, then run the following test alone in a fresh process:
```text
cargo test -p designcraft-ui-egui cold_style_combo_contains_named_instances -- --ignored --nocapture
```
Do not install the fixture globally or preload it through add_font for this cold test.
On other platforms use the platform's test font directory before the process starts.
The test asserts that SemiBold is absent before loading, without depending on the default style name.
It then opens the actual Style combo, selects SemiBold and verifies c.fontStyle.
The menu-cache test creates an isolated database whose font directory holds the fixture.
The warm widget test clicks Regular -> SemiBold -> Bold -> Regular, compares Confirm with
style.paragraph.edit, preserves an inherited family and Based On, serializes/reopens the
native document, and verifies Preview off, Cancel and single-step Confirm/Undo/Redo.
Before the patch, both cold tests fail: the menu caches only `96pt ExtraBold`, and the
Style combo offers no SemiBold. After preloading, selection and persistence already work.
This proves a discovery/availability failure, not a loss of a selected style in the engine.
No font binaries should be committed. A desktop visual/manual check on macOS and Linux
is separate from these headless egui input tests.