CoreText: When matching fonts prefer non-expanded/condensed variants

This commit is contained in:
Kovid Goyal 2020-03-24 17:55:38 +05:30
parent 76a6bba643
commit 6dbdf72f40
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 14 additions and 7 deletions

View File

@ -92,10 +92,10 @@ font_descriptor_to_python(CTFontDescriptorRef descriptor) {
NSString *style = (NSString *) CTFontDescriptorCopyAttribute(descriptor, kCTFontStyleNameAttribute);
NSDictionary *traits = (NSDictionary *) CTFontDescriptorCopyAttribute(descriptor, kCTFontTraitsAttribute);
unsigned int straits = [traits[(id)kCTFontSymbolicTrait] unsignedIntValue];
NSNumber *weightVal = traits[(id)kCTFontWeightTrait];
NSNumber *widthVal = traits[(id)kCTFontWidthTrait];
float *weightVal = [traits[(id)kCTFontWeightTrait] floatValue];
float widthVal = [traits[(id)kCTFontWidthTrait] floatValue];
PyObject *ans = Py_BuildValue("{ssssssss sOsOsO sfsfsI}",
PyObject *ans = Py_BuildValue("{ssssssss sOsOsOsOsOsO sfsfsI}",
"path", [[url path] UTF8String],
"postscript_name", [psName UTF8String],
"family", [family UTF8String],
@ -104,9 +104,12 @@ font_descriptor_to_python(CTFontDescriptorRef descriptor) {
"bold", (straits & kCTFontBoldTrait) != 0 ? Py_True : Py_False,
"italic", (straits & kCTFontItalicTrait) != 0 ? Py_True : Py_False,
"monospace", (straits & kCTFontMonoSpaceTrait) != 0 ? Py_True : Py_False,
"expanded", (straits & kCTFontExpandedTrait) != 0 ? Py_True : Py_False,
"condensed", (straits & kCTFontCondensedTrait) != 0 ? Py_True : Py_False,
"color_glyphs", (straits & kCTFontColorGlyphsTrait) != 0 ? Py_True : Py_False,
"weight", [weightVal floatValue],
"width", [widthVal floatValue],
"weight", weightVal,
"width", widthVal,
"traits", straits
);
[url release];

View File

@ -438,6 +438,9 @@ class CoreTextFont(TypedDict):
style: str
bold: bool
italic: bool
expanded: bool
condensed: bool
color_glyphs: bool
monospace: bool
weight: float
width: float

View File

@ -54,12 +54,13 @@ def find_best_match(family: str, bold: bool = False, italic: bool = False) -> Co
q = re.sub(r'\s+', ' ', family.lower())
font_map = all_fonts_map()
def score(candidate: CoreTextFont) -> Tuple[int, int]:
def score(candidate: CoreTextFont) -> Tuple[int, int, int]:
style_match = 1 if candidate['bold'] == bold and candidate[
'italic'
] == italic else 0
monospace_match = 1 if candidate['monospace'] else 0
return style_match, monospace_match
is_regular_width = not candidate['expanded'] and not candidate['condensed']
return style_match, monospace_match, 1 if is_regular_width else 0
# First look for an exact match
for selector in ('ps_map', 'full_map'):