mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-23 16:58:09 +02:00
CoreText: When matching fonts prefer non-expanded/condensed variants
This commit is contained in:
@@ -92,10 +92,10 @@ font_descriptor_to_python(CTFontDescriptorRef descriptor) {
|
|||||||
NSString *style = (NSString *) CTFontDescriptorCopyAttribute(descriptor, kCTFontStyleNameAttribute);
|
NSString *style = (NSString *) CTFontDescriptorCopyAttribute(descriptor, kCTFontStyleNameAttribute);
|
||||||
NSDictionary *traits = (NSDictionary *) CTFontDescriptorCopyAttribute(descriptor, kCTFontTraitsAttribute);
|
NSDictionary *traits = (NSDictionary *) CTFontDescriptorCopyAttribute(descriptor, kCTFontTraitsAttribute);
|
||||||
unsigned int straits = [traits[(id)kCTFontSymbolicTrait] unsignedIntValue];
|
unsigned int straits = [traits[(id)kCTFontSymbolicTrait] unsignedIntValue];
|
||||||
NSNumber *weightVal = traits[(id)kCTFontWeightTrait];
|
float *weightVal = [traits[(id)kCTFontWeightTrait] floatValue];
|
||||||
NSNumber *widthVal = traits[(id)kCTFontWidthTrait];
|
float widthVal = [traits[(id)kCTFontWidthTrait] floatValue];
|
||||||
|
|
||||||
PyObject *ans = Py_BuildValue("{ssssssss sOsOsO sfsfsI}",
|
PyObject *ans = Py_BuildValue("{ssssssss sOsOsOsOsOsO sfsfsI}",
|
||||||
"path", [[url path] UTF8String],
|
"path", [[url path] UTF8String],
|
||||||
"postscript_name", [psName UTF8String],
|
"postscript_name", [psName UTF8String],
|
||||||
"family", [family UTF8String],
|
"family", [family UTF8String],
|
||||||
@@ -104,9 +104,12 @@ font_descriptor_to_python(CTFontDescriptorRef descriptor) {
|
|||||||
"bold", (straits & kCTFontBoldTrait) != 0 ? Py_True : Py_False,
|
"bold", (straits & kCTFontBoldTrait) != 0 ? Py_True : Py_False,
|
||||||
"italic", (straits & kCTFontItalicTrait) != 0 ? Py_True : Py_False,
|
"italic", (straits & kCTFontItalicTrait) != 0 ? Py_True : Py_False,
|
||||||
"monospace", (straits & kCTFontMonoSpaceTrait) != 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],
|
"weight", weightVal,
|
||||||
"width", [widthVal floatValue],
|
"width", widthVal,
|
||||||
"traits", straits
|
"traits", straits
|
||||||
);
|
);
|
||||||
[url release];
|
[url release];
|
||||||
|
|||||||
@@ -438,6 +438,9 @@ class CoreTextFont(TypedDict):
|
|||||||
style: str
|
style: str
|
||||||
bold: bool
|
bold: bool
|
||||||
italic: bool
|
italic: bool
|
||||||
|
expanded: bool
|
||||||
|
condensed: bool
|
||||||
|
color_glyphs: bool
|
||||||
monospace: bool
|
monospace: bool
|
||||||
weight: float
|
weight: float
|
||||||
width: float
|
width: float
|
||||||
|
|||||||
@@ -54,12 +54,13 @@ def find_best_match(family: str, bold: bool = False, italic: bool = False) -> Co
|
|||||||
q = re.sub(r'\s+', ' ', family.lower())
|
q = re.sub(r'\s+', ' ', family.lower())
|
||||||
font_map = all_fonts_map()
|
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[
|
style_match = 1 if candidate['bold'] == bold and candidate[
|
||||||
'italic'
|
'italic'
|
||||||
] == italic else 0
|
] == italic else 0
|
||||||
monospace_match = 1 if candidate['monospace'] 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
|
# First look for an exact match
|
||||||
for selector in ('ps_map', 'full_map'):
|
for selector in ('ps_map', 'full_map'):
|
||||||
|
|||||||
Reference in New Issue
Block a user