CoreText: Prefer less bold variants over heavier variants when auto-selecting the bold face

This commit is contained in:
Kovid Goyal 2020-03-24 19:27:14 +05:30
parent 0547102bdd
commit bf7c26d357
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -54,13 +54,16 @@ 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, int]:
def score(candidate: CoreTextFont) -> Tuple[int, int, int, float]:
style_match = 1 if candidate['bold'] == bold and candidate[
'italic'
] == italic else 0
monospace_match = 1 if candidate['monospace'] else 0
is_regular_width = not candidate['expanded'] and not candidate['condensed']
return style_match, monospace_match, 1 if is_regular_width else 0
# prefer demi-bold to bold to heavy, less bold means less chance of
# overflow
weight_distance_from_medium = abs(candidate['weight'])
return style_match, monospace_match, 1 if is_regular_width else 0, 1 - weight_distance_from_medium
# First look for an exact match
for selector in ('ps_map', 'full_map'):