From e13a39f3027ee18d4397bbf1af9cd3bfa487a101 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Mon, 14 Dec 2015 11:45:29 -0800 Subject: [PATCH] Try to better match GDI's weight penalty --- src/font_file_lister_coretext.mm | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/font_file_lister_coretext.mm b/src/font_file_lister_coretext.mm index f429538c2..596e4949d 100644 --- a/src/font_file_lister_coretext.mm +++ b/src/font_file_lister_coretext.mm @@ -65,6 +65,14 @@ FontMatch process_descriptor(NSFontDescriptor *desc, NSString *name) { ret.codepoints = [desc objectForKey:NSFontCharacterSetAttribute]; return ret; } + +int weight_penalty(int desired, int actual) { + int d = desired - actual; + // If the font is too light but can be emboldened, reduce the penalty + if (d > 150 && actual <= 550) + d = d * 2 / 5; + return std::abs(d); +} } CollectionResult CoreTextFontFileLister::GetFontPaths(std::string const& facename, @@ -86,14 +94,14 @@ CollectionResult CoreTextFontFileLister::GetFontPaths(std::string const& facenam return m.family_match > best.family_match; } + // GDI prefers to match weight over matching italic when it has to choose + if (m.weight != best.weight) { + return weight_penalty(bold, m.weight) < weight_penalty(bold, best.weight); + } if (m.italic != best.italic) { return (m.italic != italic) < (best.italic != italic); } - if (m.weight != best.weight) { - // + 1 to bias in favor of lighter fonts when they're equal distances - // from the correct weight - return std::abs(m.weight - bold + 1) < std::abs(best.weight - bold + 1); - } + // Pick closest to "medium" weight return std::abs(m.width - 5) < std::abs(best.width - 5);