Prioritize family name matches over postscript name matches

This commit is contained in:
Thomas Goyne 2015-12-13 11:45:27 -08:00
parent 278b7621cf
commit b9a56cb870

View file

@ -21,80 +21,49 @@
namespace { namespace {
struct FontMatch { struct FontMatch {
NSURL *url; NSURL *url = nil;
NSCharacterSet *codepoints; NSCharacterSet *codepoints = nil;
int weight;
int width;
bool italic;
};
void process_descriptor(NSFontDescriptor *font, std::vector<FontMatch>& out) {
// For whatever reason there is no NSFontURLAttribute
NSURL *url = [font objectForKey:(__bridge NSString *)kCTFontURLAttribute];
if (!url)
return;
NSFont *nsfont = [NSFont fontWithDescriptor:font size:10];
int weight = 400; int weight = 400;
int width = 5; int width = 5;
bool italic = false;
bool family_match = false;
};
auto traits = CTFontGetSymbolicTraits((__bridge CTFontRef)nsfont); FontMatch process_descriptor(NSFontDescriptor *desc, NSString *name) {
bool italic = !!(traits & kCTFontItalicTrait); FontMatch ret;
// CoreText sometimes reports a slant of zero (or a very small slant) // For whatever reason there is no NSFontURLAttribute
// despite the macStyle field indicating that the font is italic, so ret.url = [desc objectForKey:(__bridge NSString *)kCTFontURLAttribute];
// double check by reading that field from the ttf if (!ret.url)
if (!italic) { return ret;
auto data = (__bridge_transfer NSData *)CTFontCopyTable((__bridge CTFontRef)nsfont, kCTFontTableHead, 0);
NSFont *font = [NSFont fontWithDescriptor:desc size:10];
// Ask CoreText if the font is italic, but if it says no double-check
// by reading the macStyle field of the 'head' table as CT doesn't honor
// that
auto traits = CTFontGetSymbolicTraits((__bridge CTFontRef)font);
ret.italic = !!(traits & kCTFontItalicTrait);
if (!ret.italic) {
auto data = (__bridge_transfer NSData *)CTFontCopyTable((__bridge CTFontRef)font, kCTFontTableHead, 0);
if (data.length > 45) { if (data.length > 45) {
italic = static_cast<const char *>(data.bytes)[45] & 2; ret.italic = static_cast<const char *>(data.bytes)[45] & 2;
} }
} }
auto data = (__bridge_transfer NSData *)CTFontCopyTable((__bridge CTFontRef)nsfont, kCTFontTableOS2, 0); // Get the weight from the OS/2 table because the weights reported by CT
// are often uncorrelated with the OS/2 weight, which is what GDI uses
auto data = (__bridge_transfer NSData *)CTFontCopyTable((__bridge CTFontRef)font, kCTFontTableOS2, 0);
if (data.length > 7) { if (data.length > 7) {
auto bytes = static_cast<const uint8_t *>(data.bytes); auto bytes = static_cast<const uint8_t *>(data.bytes);
weight = (bytes[4] << 8) + bytes[5]; // These values are big-endian in the file, so byteswap them
width = (bytes[6] << 8) + bytes[7]; ret.weight = (bytes[4] << 8) + bytes[5];
ret.width = (bytes[6] << 8) + bytes[7];
} }
out.push_back({url, [font objectForKey:NSFontCharacterSetAttribute], weight, width, italic}); ret.family_match = [font.familyName isEqualToString:name];
} ret.codepoints = [desc objectForKey:NSFontCharacterSetAttribute];
return ret;
void select_best(CollectionResult& ret, std::vector<FontMatch>& options, int bold,
bool italic, std::vector<int> const& characters) {
if (options.empty())
return;
// Prioritize fonts with the width closest to normal
sort(begin(options), end(options), [](FontMatch const& lft, FontMatch const& rgt) {
return std::abs(lft.width - 100) < std::abs(rgt.width - 100);
});
const FontMatch *match = nullptr;
for (auto const& m : options) {
if (italic == m.italic && (!match || std::abs(match->weight - bold + 1) > std::abs(m.weight - bold + 1)))
match = &m;
}
if (!match) {
ret.fake_italic = italic;
for (auto const& m : options) {
if (!match || std::abs(match->weight - bold + 1) > std::abs(m.weight - bold + 1))
match = &m;
}
}
ret.fake_bold = bold > 400 && match->weight < bold;
auto& best = *match;
ret.paths.push_back(best.url.fileSystemRepresentation);
for (int chr : characters) {
if (![best.codepoints longCharacterIsMember:chr]) {
ret.missing += chr;
}
}
} }
} }
@ -108,7 +77,38 @@ CollectionResult CoreTextFontFileLister::GetFontPaths(std::string const& facenam
else if (bold == 1) else if (bold == 1)
bold = 700; bold = 700;
std::vector<FontMatch> fonts; FontMatch best;
bool have_match = false;
auto match_is_better = [&](FontMatch const& m) {
// Prioritize family name matches over postscript name matches
if (m.family_match != best.family_match) {
return m.family_match > best.family_match;
}
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);
};
auto compare_match = [&](FontMatch match) {
if (!match.url)
return;
if (!have_match) {
best = match;
have_match = true;
}
else if (match_is_better(match))
best = match;
};
@autoreleasepool { @autoreleasepool {
NSString *name = @(facename.c_str() + (facename[0] == '@')); NSString *name = @(facename.c_str() + (facename[0] == '@'));
@ -118,11 +118,25 @@ CollectionResult CoreTextFontFileLister::GetFontPaths(std::string const& facenam
[NSFontDescriptor fontDescriptorWithFontAttributes:@{NSFontNameAttribute: name}] [NSFontDescriptor fontDescriptorWithFontAttributes:@{NSFontNameAttribute: name}]
]; ];
auto font_collection = [NSFontCollection fontCollectionWithDescriptors:attrs]; auto collection = [NSFontCollection fontCollectionWithDescriptors:attrs];
for (NSFontDescriptor *desc in font_collection.matchingDescriptors) { if (NSArray *matching = collection.matchingDescriptors) {
process_descriptor(desc, fonts); for (NSFontDescriptor *desc in matching) {
compare_match(process_descriptor(desc, name));
}
}
if (!have_match)
return ret;
ret.fake_italic = italic && !best.italic;
ret.fake_bold = bold > 400 && best.weight < bold;
ret.paths.push_back(best.url.fileSystemRepresentation);
for (int chr : characters) {
if (![best.codepoints longCharacterIsMember:chr]) {
ret.missing += chr;
}
} }
select_best(ret, fonts, bold, italic, characters);
} }
return ret; return ret;