macvim-dev / macvim

Vim - the text editor - for macOS
https://macvim.org
Vim License
7.55k stars 682 forks source link

Support native macOS monospace font (SF Mono) #1463

Closed ychin closed 11 months ago

ychin commented 11 months ago

Can now set guifont to -monospace- to use the system default monospace font, which is SF Mono on recent macOS versions (but could be updated to other fonts in future macOS releases). The reason why this is necessary instead of specifying the actual font name is that Apple does not expose the SF Mono font for the user and instead only exposes an AppKit API monospacedSystemFontOfSize:weight: to access it. The actual font name (.AppleSystemUIFontMonospaced in macOS 14) is internal and subject to change in different OS versions.

In older macOS versions, setting -monospace- will just use Menlo-Regular just like the default font.

Also allow specifying the font weight for the font, e.g. -monospace-Semibold / -monospace-Light. The list of weights follows the NSFontWeight enum, but not all values yield unique fonts. E.g. "UltraLight", "Thin", "Light" will all use the "Light" version of SF Mono. The list of all font weights can be tab-completed but only if the user has already filled in -monospace- in :set guifont=. This helps prevents showing too many options when the user does tab completion just to see the list of all fonts.

Note that SF Mono is currently available to be downloaded from Apple's website as a standalone for testing. That font is mostly the same but seems to have slightly different line spacing behavior, and when using bold it uses the "Bold" font variant, whereas the system monospace font uses "Semibold" variant instead.

Also make font panel not show misc formatting options like underline as they aren't used by MacVim. Keep the background/foreground option just so the font preview colors in the panel can be adjusted.

Also fix an existing potential buffer overflow issue in the Core Text renderer in that changeFont: (when setting a new font using font panel or using font size up/down) isn't setting wideLen which could cause an unsafe memory access in Vim side.

Notes:

etiennebarrie commented 10 months ago

Hey! This is great, but I'm seeing weird results in terms of line height/line spacing. Opening up Terminal.app using SF Mono side-by-side with MacVim, I need to change the line spacing to 0.88 to get the same results:

image

Put another way, there's a bit too much line spacing on MacVim, when I change from SF\ Mono\ Regular (that I get with Homebrew) to -monospace-, I can see fewer lines.

ychin commented 10 months ago

I have noticed something like this before as well, and I don't really know why. In particular, at lower font sizes (11 pts or less) they seem to have the same line heights, but at higher font sizes, the -monospace- font seems to use a line spacing.

Another discrepancy is that -monospace- seems to use a Medium weight for bolded font, but the downloaded "SF Mono" font uses the Bold weight for bolded font.

I think Apple may have just configured the default -monospace- font differently from the downloadable SF Mono font but I do think this is kind of annoying. I may have to ask on Apple Developers forum but I don't think MacVim doing anything wrong per se.