mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 17:46:59 +00:00
Type: the font menu's Show Similar Fonts lists the families that look like a family first
A ≈ button on a family's row in the font menus orders the listed families by
how alike they look to it: that family first, then the families that set the
same script (Japanese, Thai or neither), then the others, each group nearest
first. The Recent Fonts section is left out while the list is ordered this
way. A ≈ chip under the filters names the family and clears the order, with
that family still highlighted; closing the menu clears it too.
The measure is VectorCraft's own. Each face records its OS/2 PANOSE digits,
weight and width classes, x-height, cap height and average character width,
and its post italic angle; a named instance of a variable font takes its wght
and slnt settings. The system font scan reads the head table and the post
table's header besides the tables it read before, so installed families are
compared by the same numbers without loading them. A family is compared by its
face nearest an upright Regular, a number either face does not give is left
out, and a family of another kind counts as further away.
text.similarFonts {family, limit?} returns the same order, with errors for a
missing or non-string family, a family that is not available or not listed in
the font menus, and a limit that is not a whole number from 1 to 100000.
This commit is contained in:
@@ -53,6 +53,15 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
always,
|
||||
font_list
|
||||
),
|
||||
cmd!(
|
||||
query "text.similarFonts",
|
||||
"Show Similar Fonts",
|
||||
[],
|
||||
None,
|
||||
"{family (any of its names), limit?: whole number 1..100000 (default: all)} → {family: the family's own name, families: [names]}: the families `text.fontList` lists, ordered as a font menu's Show Similar Fonts orders them: the family itself first, then those that set the same script (Japanese, Thai or neither), then the others, each group nearest first by VectorCraft's own measure of the faces nearest an upright Regular (OS/2 PANOSE digits, weight and width classes, x-height, cap height and average character width, the italic angle, and the kind); an error when the family isn't available or the font menus don't list it, or `limit` is not a whole number from 1 to 100000",
|
||||
always,
|
||||
similar_fonts
|
||||
),
|
||||
cmd!(
|
||||
"text.rescanFonts",
|
||||
"Refresh Font List",
|
||||
@@ -1137,6 +1146,36 @@ fn font_list(_: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The most families `text.similarFonts` takes as its `limit`.
|
||||
const MAX_SIMILAR: u64 = 100_000;
|
||||
|
||||
fn similar_fonts(_: &mut Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "text.similarFonts";
|
||||
let db = vectorcraft_text::FontDb::global();
|
||||
let family = match p.get("family") {
|
||||
Some(Value::String(f)) if !f.trim().is_empty() => f.as_str(),
|
||||
Some(Value::String(_)) | None => return Err(bad(C, "`family` is required")),
|
||||
Some(_) => return Err(bad(C, "`family` must be a string")),
|
||||
};
|
||||
let limit = match p.get("limit") {
|
||||
None => None,
|
||||
Some(v) => Some(
|
||||
v.as_u64()
|
||||
.filter(|n| (1..=MAX_SIMILAR).contains(n))
|
||||
.ok_or_else(|| bad(C, format!("`limit` must be a whole number from 1 to {MAX_SIMILAR}")))?,
|
||||
),
|
||||
};
|
||||
// Any of the family's names, spelled as the font menus list it; a family they don't list is not available.
|
||||
let menu = db.menu_family_list();
|
||||
let listed = db.has_family(family).then(|| db.canonical(family, "").0).and_then(|c| menu.iter().find(|f| f.eq_ignore_ascii_case(&c)).cloned());
|
||||
let Some(family) = listed else { return Err(bad(C, format!("font family `{family}` is not available"))) };
|
||||
let mut families = db.similar_families(&family, &menu);
|
||||
if let Some(n) = limit.and_then(|n| usize::try_from(n).ok()) {
|
||||
families.truncate(n);
|
||||
}
|
||||
Ok(json!({ "family": family, "families": families }))
|
||||
}
|
||||
|
||||
pub(crate) fn rescan(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
let db = vectorcraft_text::FontDb::global();
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
|
||||
@@ -34,6 +34,39 @@ fn the_font_list_names_every_available_family_with_its_styles() {
|
||||
assert!(s.execute("text.fontList", &json!({"family": "No Such Font"})).is_err());
|
||||
}
|
||||
|
||||
/// Show Similar Fonts (`text.similarFonts`): every listed family, the asked one first, in the
|
||||
/// order the font menus show; bad params are refused.
|
||||
#[test]
|
||||
fn similar_fonts_lists_every_family_by_likeness() {
|
||||
let mut s = Session::new();
|
||||
let db = FontDb::global();
|
||||
let r = s.execute("text.similarFonts", &json!({"family": "source sans 3"})).unwrap();
|
||||
assert_eq!(r["family"], "Source Sans 3");
|
||||
let order = names(&r["families"]);
|
||||
assert_eq!(order.first().map(String::as_str), Some("Source Sans 3"));
|
||||
let mut sorted = order.clone();
|
||||
sorted.sort();
|
||||
let mut listed = db.menu_family_list().to_vec();
|
||||
listed.sort();
|
||||
// Other tests running at the same time add and remove composite fonts; the installed families are compared.
|
||||
let installed_only = |list: &[String]| list.iter().filter(|f| db.has_installed_family(f)).cloned().collect::<Vec<_>>();
|
||||
assert_eq!(installed_only(&sorted), installed_only(&listed), "every family once");
|
||||
let two = s.execute("text.similarFonts", &json!({"family": "Source Sans 3", "limit": 2})).unwrap();
|
||||
assert_eq!(names(&two["families"]), order[..2]);
|
||||
for (params, error) in [
|
||||
(json!({}), "`family` is required"),
|
||||
(json!({"family": " "}), "`family` is required"),
|
||||
(json!({"family": 3}), "`family` must be a string"),
|
||||
(json!({"family": "No Such Font"}), "font family `No Such Font` is not available"),
|
||||
(json!({"family": "Inter", "limit": 0}), "`limit` must be a whole number from 1 to 100000"),
|
||||
(json!({"family": "Inter", "limit": 2.5}), "`limit` must be a whole number from 1 to 100000"),
|
||||
(json!({"family": "Inter", "limit": "2"}), "`limit` must be a whole number from 1 to 100000"),
|
||||
] {
|
||||
let e = s.execute("text.similarFonts", ¶ms).unwrap_err().to_string();
|
||||
assert!(e.contains(error), "{params}: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refreshing_the_font_list_redraws_type_without_editing_the_document() {
|
||||
let mut s = Session::new();
|
||||
|
||||
Reference in New Issue
Block a user