M4: weight steps read number weights after a width too (#1831)

An off-list "Condensed 600" read as 400, since a number word counted as
a weight only at the start of the name: weightUp took Condensed 500 and
weightDown found nothing. A number from 100 to 1000 now counts in any
position, as W numbers do. Smaller numbers (a width grade, an optical
size: "Condensed 2", "Caption 12") are part of the width rather than a
weight, so optical sizes no longer step into each other.
This commit is contained in:
ljkx committed 2026-11-03 05:27:23 +01:00
1 parent 45c89c5a73
commit dd03165d79
2 files changed
+24 -3

No files matched your search

+10 -3
View File
@@ -128,6 +128,13 @@ fn w_number(word: &str) -> Option<u16> {
(1..=9).contains(&n).then_some(n * 100)
}
/// A weight number as some families name styles ("Condensed 600"). Below 100 it is something else
/// (a width grade, an optical size: "Condensed 2", "Caption 12").
fn number_weight(word: &str) -> Option<u16> {
let n = word.parse::<u16>().ok().filter(|_| word.bytes().all(|b| b.is_ascii_digit()))?;
(100..=1000).contains(&n).then_some(n)
}
/// What a style name says besides its weight and slant ("Narrow Bold Italic" → "narrow",
/// "Extra Condensed Bold" → "extra condensed").
fn style_rest(style: &str) -> String {
@@ -162,7 +169,7 @@ fn style_rest(style: &str) -> String {
const GRADES: [&str; 4] = ["extra", "ultra", "semi", "demi"];
let s = style.to_ascii_lowercase().replace('-', " ");
let words: Vec<&str> = s.split_whitespace().collect();
let weight = |w: &str| WEIGHTS.contains(&w) || w_number(w).is_some() || w.chars().all(|c| c.is_ascii_digit());
let weight = |w: &str| WEIGHTS.contains(&w) || w_number(w).or_else(|| number_weight(w)).is_some();
let rest = words.iter().enumerate().filter(|(i, w)| {
let graded = GRADES.contains(w) && words.get(i + 1).is_some_and(|next| weight(next) && !matches!(*next, "italic" | "oblique"));
!weight(w) && !graded
@@ -176,10 +183,10 @@ fn slanted(style: &str) -> bool {
s.contains("italic") || s.contains("oblique")
}
/// The weight a style name says ("W3", "W6 Italic" and "Condensed W7" too).
/// The weight a style name says ("W3", "W6 Italic", "Condensed W7" and "Condensed 600" too).
pub(crate) fn name_weight(style: &str) -> u16 {
let words = style.to_ascii_lowercase().replace('-', " ");
words.split_whitespace().find_map(w_number).unwrap_or_else(|| figcraft_doc::style_weight(style))
words.split_whitespace().find_map(|w| w_number(w).or_else(|| number_weight(w))).unwrap_or_else(|| figcraft_doc::style_weight(style))
}
/// The next heavier (`up`) or lighter style of `styles` (a family's with the weight each is listed
@@ -77,6 +77,20 @@ fn hairline_and_w_numbers_are_weights() {
assert_eq!(next_style(&hiragino, &at("Bold"), false).as_deref(), Some("W6"));
}
#[test]
fn number_weights_count_after_a_width_but_other_numbers_dont() {
// An off-list "Condensed 600" is 600, as "600" alone is: between Condensed 500 and 700.
let condensed = weighed(&[("Condensed 300", 300), ("Condensed 500", 500), ("Condensed 700", 700)]);
assert_eq!(next_style(&condensed, &at("Condensed 600"), true).as_deref(), Some("Condensed 700"));
assert_eq!(next_style(&condensed, &at("Condensed 600"), false).as_deref(), Some("Condensed 500"));
assert_eq!((name_weight("Condensed 600"), name_weight("600 Italic"), name_weight("Condensed 1000")), (600, 600, 1000));
// A number that isn't a weight (a width grade, an optical size) is part of the width.
assert_eq!((name_weight("Condensed 2"), name_weight("Caption 12 Bold"), name_weight("Condensed +600")), (400, 700, 400));
let optical = styles(&["Caption 12 Regular", "Caption 24 Regular", "Caption 12 Bold", "Caption 24 Bold"]);
assert_eq!(walk(&optical, "Caption 12 Regular", true), ["Caption 12 Bold"]);
assert_eq!(walk(&optical, "Caption 24 Bold", false), ["Caption 24 Regular"]);
}
#[test]
fn a_custom_weight_axis_value_steps_from_where_it_is() {
let family = styles(&["Light", "Regular", "Medium", "Bold"]);