From 09077d6382e89a72c954edf99cfc3f014206ce7a Mon Sep 17 00:00:00 2001 From: Laurenz Date: Fri, 24 Feb 2023 15:08:00 +0100 Subject: [PATCH] Fix more bad font names --- src/font/book.rs | 50 +++++++++++++++++++++++++++++++++++++++------- tests/src/tests.rs | 1 + 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/font/book.rs b/src/font/book.rs index 7e43033e1..a6e41860f 100644 --- a/src/font/book.rs +++ b/src/font/book.rs @@ -199,7 +199,10 @@ impl FontInfo { // up. let family = { let mut family = find_name(ttf, name_id::FAMILY)?; - if family.starts_with("Noto") || family.starts_with("NewComputerModern") { + if family.starts_with("Noto") + || family.starts_with("NewCM") + || family.starts_with("NewComputerModern") + { family = find_name(ttf, name_id::FULL_NAME)?; } typographic_family(&family).to_string() @@ -221,9 +224,18 @@ impl FontInfo { (_, true) => FontStyle::Oblique, }; - let weight = FontWeight::from_number(ttf.weight().to_number()); - let stretch = FontStretch::from_number(ttf.width().to_number()); + let weight = { + let mut number = ttf.weight().to_number(); + if (family.starts_with("NewCM") + || family.starts_with("New Computer Modern")) + && full.contains("book") + { + number += 50; + } + FontWeight::from_number(number) + }; + let stretch = FontStretch::from_number(ttf.width().to_number()); FontVariant { style, weight, stretch } }; @@ -318,6 +330,12 @@ fn typographic_family(mut family: &str) -> &str { "narrow", "condensed", "cond", "cn", "cd", "compressed", "expanded", "exp" ]; + let mut extra = [].as_slice(); + let newcm = family.starts_with("NewCM") || family.starts_with("NewComputerModern"); + if newcm { + extra = &["book"]; + } + // Trim spacing and weird leading dots in Apple fonts. family = family.trim().trim_start_matches('.'); @@ -331,9 +349,16 @@ fn typographic_family(mut family: &str) -> &str { len = trimmed.len(); // Find style suffix. - let Some(mut t) = SUFFIXES.iter().find_map(|s| trimmed.strip_suffix(s)) else { + let mut t = trimmed; + let mut shortened = false; + while let Some(s) = SUFFIXES.iter().chain(extra).find_map(|s| t.strip_suffix(s)) { + shortened = true; + t = s; + } + + if !shortened { break; - }; + } // Strip optional separator. if let Some(s) = t.strip_suffix(SEPARATORS) { @@ -350,10 +375,21 @@ fn typographic_family(mut family: &str) -> &str { } } + // Apply style suffix trimming. + family = &family[..len]; + + if newcm { + family = family.trim_end_matches("10"); + } + // Fix bad names. - match &family[..len] { + match family { + "Noto Sans Symbols2" => "Noto Sans Symbols 2", + "NewComputerModern" => "New Computer Modern", + "NewComputerModernMono" => "New Computer Modern Mono", + "NewComputerModernSans" => "New Computer Modern Sans", "NewComputerModernMath" => "New Computer Modern Math", - "NewComputerModernMath-Book" => "New Computer Modern Math Book", + "NewCMUncial" | "NewComputerModernUncial" => "New Computer Modern Uncial", other => other, } } diff --git a/tests/src/tests.rs b/tests/src/tests.rs index 8895ccdd0..62e6f3f52 100644 --- a/tests/src/tests.rs +++ b/tests/src/tests.rs @@ -220,6 +220,7 @@ impl TestWorld { // Search for fonts. let mut fonts = vec![]; for entry in WalkDir::new(FONT_DIR) + .sort_by_file_name() .into_iter() .filter_map(|e| e.ok()) .filter(|entry| entry.file_type().is_file())