From 930ab116a48059517216216d27eaa43c1129ccd2 Mon Sep 17 00:00:00 2001 From: rsheeter Date: Wed, 4 Dec 2024 14:21:47 -0800 Subject: [PATCH] Let's not name things Thing Italic Italic --- glyphs-reader/src/font.rs | 43 ++++++++++++++----- .../testdata/glyphs2/ItalicItalic.glyphs | 34 +++++++++++++++ 2 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 resources/testdata/glyphs2/ItalicItalic.glyphs diff --git a/glyphs-reader/src/font.rs b/glyphs-reader/src/font.rs index dc7913e3..c9946028 100644 --- a/glyphs-reader/src/font.rs +++ b/glyphs-reader/src/font.rs @@ -1549,24 +1549,28 @@ impl RawFont { if master.name.is_some() { continue; } - let width_name = master.width.take(); - let weight_name = master.weight.take(); - let custom_name = master.custom.take(); + // Remove Nones, empty strings and redundant occurrences of 'Regular' - let mut names: Vec<_> = [width_name, weight_name, custom_name] - .into_iter() - .flatten() - .filter(|x| !x.is_empty() && x != "Regular") - .collect(); + let mut names = [ + master.width.as_deref(), + master.weight.as_deref(), + master.custom.as_deref(), + ] + .iter() + .flatten() + .flat_map(|n| n.split_ascii_whitespace()) + .filter(|x| *x != "Regular") + .collect::>(); + // append "Italic" if italic angle != 0 if let Some(italic_angle) = master.italic_angle { if italic_angle != 0.0 && (names.is_empty() || !names .iter() - .any(|name| name == "Italic" || name == "Oblique")) + .any(|name| *name == "Italic" || *name == "Oblique")) { - names.push("Italic".into()); + names.push("Italic"); } } // if all are empty, default to "Regular" @@ -3560,6 +3564,7 @@ mod tests { Some(&OrderedFloat(0f64)) ); } + #[test] fn read_font_metrics() { let font = @@ -3590,4 +3595,22 @@ mod tests { .content .contains("name 3 1 0x409 \"Alternate placeholder\"")); } + + // + #[test] + fn one_italic_is_enough() { + let font = Font::load(&glyphs2_dir().join("ItalicItalic.glyphs")).unwrap(); + for master in font.masters { + let mut fragments = master.name.split_ascii_whitespace().collect::>(); + fragments.sort(); + for words in fragments.windows(2) { + assert!( + words[0] != words[1], + "Multiple instances of {} in {}", + words[0], + master.name + ); + } + } + } } diff --git a/resources/testdata/glyphs2/ItalicItalic.glyphs b/resources/testdata/glyphs2/ItalicItalic.glyphs new file mode 100644 index 00000000..5d0e9a61 --- /dev/null +++ b/resources/testdata/glyphs2/ItalicItalic.glyphs @@ -0,0 +1,34 @@ +{ +customParameters = ( +{ +name = Axes; +value = ( +{ +Name = Weight; +Tag = wght; +} +); +} +); +familyName = "Master Names"; +fontMaster = ( +{ +custom = "Thin Italic"; +id = m01; +italicAngle = 10; +} +); +glyphs = ( +{ +glyphname = space; +layers = ( +{ +layerId = m01; +width = 200; +} +); +unicode = 0020; +} +); +unitsPerEm = 1000; +}