mirror of
https://github.com/storytold/figcraft.git
synced 2026-12-05 19:51:38 -05:00
fix(fonts): weight bindings keep each layer's slant, records match in any case, auto-save warns (#1025)
From review: the binding cache key left out whether the style is italic, so two layers sharing a weight binding but slanted differently resolved to the same style and one of them went unrecorded. A record spelt with other non-ASCII capitals than its text wasn't found and was dropped on save. An auto-save never said when the font record may be incomplete; it now shows the same notice a Save does.
This commit is contained in:
@@ -30,8 +30,10 @@ impl FontRecord {
|
||||
Self { family: family.into(), style: style.into(), ..Self::default() }
|
||||
}
|
||||
|
||||
/// Whether this records `family` and `style` (names compare ignoring case and outer spaces).
|
||||
/// Whether this records `family` and `style` (names compare ignoring outer spaces and case, in
|
||||
/// any script, as the fonts a document uses are told apart).
|
||||
pub fn is(&self, family: &str, style: &str) -> bool {
|
||||
self.family.trim().eq_ignore_ascii_case(family.trim()) && self.style.trim().eq_ignore_ascii_case(style.trim())
|
||||
let same = |a: &str, b: &str| a.trim().to_lowercase() == b.trim().to_lowercase();
|
||||
same(&self.family, family) && same(&self.style, style)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,14 +54,15 @@ fn each_font(doc: &Document, mut f: impl FnMut(&str, Option<&str>, Option<NodeId
|
||||
let bound_families =
|
||||
|bound: &BTreeMap<String, VariableId>| bound.get("fontFamily").map(|var| string_values(&doc.variables, var)).unwrap_or_default();
|
||||
// Layers usually share their bindings: each distinct set is resolved once.
|
||||
let mut resolved: BTreeMap<(Bindings, String, String), (Fonts, bool)> = BTreeMap::new();
|
||||
// The key holds all a binding reads of the style: a weight keeps it italic or not (#1025).
|
||||
let mut resolved: BTreeMap<(Bindings, String, String, bool), (Fonts, bool)> = BTreeMap::new();
|
||||
let mut complete = true;
|
||||
let mut moded = |style: &TextStyle, bound: &BTreeMap<String, VariableId>| -> Fonts {
|
||||
let fonts = font_bindings(bound);
|
||||
if fonts.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
let key = (fonts, style.font_family.clone(), style.font_style.clone());
|
||||
let key = (fonts, style.font_family.clone(), style.font_style.clone(), style.italic());
|
||||
let (pairs, all) = resolved.entry(key).or_insert_with_key(|(fonts, ..)| in_each_mode(&doc.variables, style, fonts));
|
||||
complete &= *all;
|
||||
pairs.clone()
|
||||
|
||||
@@ -176,3 +176,36 @@ fn too_many_mode_combinations_say_so_and_keep_the_files_other_records() {
|
||||
assert!(saved_fonts(&s).contains(&kept), "a record that may still be used survives the save");
|
||||
assert!(save_warnings(&mut s, "too-many-modes").as_array().is_some_and(|w| !w.is_empty()), "the save says its record may be incomplete");
|
||||
}
|
||||
|
||||
/// Two layers in one family and style name bound to the same weight variable, one slanted by its
|
||||
/// `ital` axis, resolve to different styles: the slanted one is set in the italic (#1025 review).
|
||||
#[test]
|
||||
fn a_weight_binding_keeps_each_layers_slant() {
|
||||
let mut s = Session::new();
|
||||
text(&mut s, "FigCraft Slant Sans", "Regular");
|
||||
text(&mut s, "FigCraft Slant Sans", "Regular");
|
||||
s.execute("variable.createCollection", &json!({"name": "Type", "modes": ["Base"]})).unwrap();
|
||||
s.execute("variable.create", &json!({"collection": "Type", "name": "Weight", "type": "number", "value": 700})).unwrap();
|
||||
let var = s.doc().unwrap().variables.get("Weight").unwrap().id.clone();
|
||||
let layers: Vec<NodeId> = s.doc().unwrap().nodes().filter(|n| matches!(n.kind, NodeKind::Text(_))).map(|n| n.id).collect();
|
||||
for (i, id) in layers.iter().enumerate() {
|
||||
let n = s.doc_mut().unwrap().get_mut(*id).unwrap();
|
||||
n.bound.insert("fontWeight".into(), var.clone());
|
||||
if let (1, NodeKind::Text(t)) = (i, &mut n.kind) {
|
||||
t.style.variations.insert("ital".into(), 1.0);
|
||||
}
|
||||
}
|
||||
let styles: Vec<String> = used_fonts(&mut s).iter().map(|f| f["style"].as_str().unwrap().to_string()).collect();
|
||||
assert!(styles.iter().any(|st| st.contains("Italic")) && styles.iter().any(|st| !st.contains("Italic") && st != "Regular"), "{styles:?}");
|
||||
}
|
||||
|
||||
/// A record names its font in any case: one spelt with other capitals, in any script, is kept.
|
||||
#[test]
|
||||
fn a_record_spelt_in_other_capitals_is_kept() {
|
||||
let mut s = Session::new();
|
||||
text(&mut s, "école Sans FigCraft", "Regular");
|
||||
let recorded = FontRecord { version: Some("Version 2".into()), ..FontRecord::named("École Sans FigCraft", "REGULAR") };
|
||||
s.doc_mut().unwrap().fonts = vec![recorded];
|
||||
let fonts = saved_fonts(&s);
|
||||
assert_eq!(fonts.iter().find(|r| r.is("école Sans FigCraft", "Regular")).and_then(|r| r.version.as_deref()), Some("Version 2"), "{fonts:?}");
|
||||
}
|
||||
|
||||
@@ -362,10 +362,19 @@ impl FigApp {
|
||||
return None;
|
||||
}
|
||||
let mut failed = None;
|
||||
// A written file whose font record may be incomplete says so, as a Save does (#1025).
|
||||
let mut incomplete = None;
|
||||
let done: Vec<_> = self.ui.auto_saving.extract_if(.., |(_, h)| h.is_finished()).collect();
|
||||
for (_, h) in done {
|
||||
let saved = match h.join() {
|
||||
Ok((snapshot, Ok(bytes))) => snapshot.save(&mut self.session, &bytes).map(|_| ()).map_err(|e| e.to_string()),
|
||||
Ok((snapshot, Ok(bytes))) => {
|
||||
let result = snapshot.with_font_warning(json!({}));
|
||||
let saved = snapshot.save(&mut self.session, &bytes).map_err(|e| e.to_string());
|
||||
if saved == Ok(true) && result.get("warnings").is_some() {
|
||||
incomplete = Some(result);
|
||||
}
|
||||
saved.map(|_| ())
|
||||
}
|
||||
Ok((_, Err(e))) => Err(e.to_string()),
|
||||
Err(_) => Err("the file couldn't be encoded".to_string()),
|
||||
};
|
||||
@@ -373,6 +382,9 @@ impl FigApp {
|
||||
failed.get_or_insert(e);
|
||||
}
|
||||
}
|
||||
if let Some(result) = incomplete {
|
||||
self.toast_saved(&result);
|
||||
}
|
||||
failed
|
||||
}
|
||||
}
|
||||
|
||||
@@ -592,3 +592,37 @@ fn a_rename_started_while_minimized_waits_for_the_layers_panel_quietly() {
|
||||
}
|
||||
assert!(h.app.ui.rename.is_some_and(|r| !r.start), "the field opened once the interface expanded");
|
||||
}
|
||||
|
||||
/// An auto-save whose file may not record every font its text uses says so, as a Save does (#1025).
|
||||
#[test]
|
||||
fn auto_save_says_when_the_font_record_may_be_incomplete() {
|
||||
let dir = std::env::temp_dir().join(format!("figcraft-auto-save-fonts-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("fonts.figcraft").to_string_lossy().to_string();
|
||||
let mut s = Session::new();
|
||||
let layer = figcraft_engine::params::id_of(&s.execute("text.create", &json!({"x": 0, "y": 0, "text": "Bound"})).unwrap()["id"]).unwrap();
|
||||
// Two collections of 65 modes each: more combinations than a save checks.
|
||||
for (collection, var, key) in [("Families", "Family", "fontFamily"), ("Styles", "Style", "fontStyle")] {
|
||||
let modes: Vec<String> = (0..65).map(|i| format!("M{i}")).collect();
|
||||
s.execute("variable.createCollection", &json!({"name": collection, "modes": modes})).unwrap();
|
||||
s.execute("variable.create", &json!({"collection": collection, "name": var, "type": "string", "value": var})).unwrap();
|
||||
let id = s.doc().unwrap().variables.get(var).unwrap().id.clone();
|
||||
s.doc_mut().unwrap().get_mut(layer).unwrap().bound.insert(key.into(), id);
|
||||
}
|
||||
let mut h = Harness::new(s);
|
||||
h.run("file.save", json!({"path": path}));
|
||||
h.run("pref.autoSave", json!({"on": true}));
|
||||
h.app.ui.toast = None;
|
||||
h.run("shape.rectangle", json!({"x": 500, "y": 300, "width": 40, "height": 40}));
|
||||
h.frame(vec![]);
|
||||
h.time += crate::AUTO_SAVE_SECS + 1.0;
|
||||
let start = Instant::now();
|
||||
while h.app.session.ds().unwrap().is_dirty() {
|
||||
assert!(start.elapsed() < Duration::from_secs(20), "saved");
|
||||
h.frame(vec![]);
|
||||
std::thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
let toast = h.app.ui.toast.as_ref().map(|(t, _)| t.clone()).unwrap_or_default();
|
||||
assert!(toast.contains("font record"), "{toast:?}");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user