Skip to content

Fix issue with font line spacing support and add option for it #1153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion runtime/doc/gui_mac.txt
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ is sent back to the server application.

Some settings are global to the MacVim application and would not make sense as
Vim options. These settings are stored in the user defaults database and can
be accessed via the "MacVim.Preferences..." menu item.
be accessed via the "MacVim.Preferences" menu item.

*macvim-user-defaults*
Not all entries in the user defaults database are exposed via the preference
Expand All @@ -265,6 +265,7 @@ KEY VALUE ~
*MMNativeFullScreen* use native full screen mode [bool]
*MMNoFontSubstitution* disable automatic font substitution [bool]
(Deprecated: Non-CoreText renderer only)
*MMFontPreserveLineSpacing* use the line-spacing as specified by font [bool]
*MMNoTitleBarWindow* hide title bar [bool]
*MMTitlebarAppearsTransparent* enable a transparent titlebar [bool]
*MMAppearanceModeSelection* dark mode selection (|macvim-dark-mode|)[bool]
Expand Down
1 change: 1 addition & 0 deletions runtime/doc/tags
Original file line number Diff line number Diff line change
Expand Up @@ -5015,6 +5015,7 @@ MMAppearanceModeSelection gui_mac.txt /*MMAppearanceModeSelection*
MMCellWidthMultiplier gui_mac.txt /*MMCellWidthMultiplier*
MMDialogsTrackPwd gui_mac.txt /*MMDialogsTrackPwd*
MMDisableLaunchAnimation gui_mac.txt /*MMDisableLaunchAnimation*
MMFontPreserveLineSpacing gui_mac.txt /*MMFontPreserveLineSpacing*
MMFullScreenFadeTime gui_mac.txt /*MMFullScreenFadeTime*
MMLoginShellArgument gui_mac.txt /*MMLoginShellArgument*
MMLoginShellCommand gui_mac.txt /*MMLoginShellCommand*
Expand Down
75 changes: 49 additions & 26 deletions src/MacVim/Base.lproj/Preferences.xib

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/MacVim/MMAppController.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
- (BOOL)openFiles:(NSArray *)filenames withArguments:(NSDictionary *)args;

- (void)refreshAllAppearances;
- (void)refreshAllFonts;

- (IBAction)newWindow:(id)sender;
- (IBAction)newWindowAndActivate:(id)sender;
Expand Down
10 changes: 10 additions & 0 deletions src/MacVim/MMAppController.m
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ + (void)initialize
[NSNumber numberWithBool:YES], MMTranslateCtrlClickKey,
[NSNumber numberWithInt:0], MMOpenInCurrentWindowKey,
[NSNumber numberWithBool:NO], MMNoFontSubstitutionKey,
[NSNumber numberWithBool:YES], MMFontPreserveLineSpacingKey,
[NSNumber numberWithBool:YES], MMLoginShellKey,
[NSNumber numberWithInt:MMRendererCoreText],
MMRendererKey,
Expand Down Expand Up @@ -1109,6 +1110,15 @@ - (void)refreshAllAppearances
}
}

- (void)refreshAllFonts
{
unsigned count = [vimControllers count];
for (unsigned i = 0; i < count; ++i) {
MMVimController *vc = [vimControllers objectAtIndex:i];
[vc.windowController refreshFonts];
}
}

- (IBAction)newWindow:(id)sender
{
ASLogDebug(@"Open new window");
Expand Down
2 changes: 2 additions & 0 deletions src/MacVim/MMCoreTextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
BOOL ligatures;
BOOL thinStrokes;

BOOL forceRefreshFont; // when true, don't early out of setFont calls.

MMTextViewHelper *helper;

NSMutableDictionary<NSNumber *, NSFont *> *fontVariants;
Expand Down
64 changes: 56 additions & 8 deletions src/MacVim/MMCoreTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ - (id)initWithFrame:(NSRect)frame
{
if (!(self = [super initWithFrame:frame]))
return nil;

forceRefreshFont = NO;

self.wantsLayer = YES;

Expand Down Expand Up @@ -374,24 +376,57 @@ - (NSRect)rectForColumnsInRange:(NSRange)range

- (void)setFont:(NSFont *)newFont
{
if (!newFont || [font isEqual:newFont])
if (!newFont) {
return;
}
if (!forceRefreshFont) {
if ([font isEqual:newFont])
return;
}
forceRefreshFont = NO;

double em = round(defaultAdvanceForFont(newFont));
const double em = round(defaultAdvanceForFont(newFont));

float cellWidthMultiplier = [[NSUserDefaults standardUserDefaults]
const float cellWidthMultiplier = [[NSUserDefaults standardUserDefaults]
floatForKey:MMCellWidthMultiplierKey];

// Some fonts have non-standard line heights, and historically MacVim has
// chosen to ignore it. Provide the option for the user to choose whether to
// use the font's line height. If not preserving, will create a new font
// from scratch with just name and pt size, which will disard the line
// height information.
//
// Defaults to the new behavior (preserveLineHeight==true) because it's
// simpler and respects the font's design more.
//
// Note: this behavior is somewhat inconsistent across editors and
// terminals. Xcode, for example, seems to be equivalent to
// (preserveLineHeight==true), but other editors/terminals behave
// differently. Xcode respecting the line height is partially the motivation
// for setting that as the default.
const BOOL preserveLineHeight = [[NSUserDefaults standardUserDefaults]
boolForKey:MMFontPreserveLineSpacingKey];

[font release];
if (!preserveLineHeight) {
double pt = round([newFont pointSize]);

CTFontDescriptorRef desc = CTFontDescriptorCreateWithNameAndSize((CFStringRef)[newFont fontName], pt);
CTFontRef fontRef = CTFontCreateWithFontDescriptor(desc, pt, NULL);
CFRelease(desc);

font = (NSFont*)fontRef;
} else {
font = [newFont retain];
}
fontDescent = ceil(CTFontGetDescent((CTFontRef)font));

// NOTE! Even though NSFontFixedAdvanceAttribute is a float, it will
// only render at integer sizes. Hence, we restrict the cell width to
// an integer here, otherwise the window width and the actual text
// width will not match.
cellSize.width = columnspace + ceil(em * cellWidthMultiplier);
cellSize.height = linespace + defaultLineHeightForFont(newFont);

[font release];
font = [newFont retain];
fontDescent = ceil(CTFontGetDescent((CTFontRef)font));
cellSize.height = linespace + defaultLineHeightForFont(font);

[self clearAll];
[fontVariants removeAllObjects];
Expand All @@ -414,7 +449,17 @@ - (void)setWideFont:(NSFont *)newFont
[fontVariants removeAllObjects];
[characterStrings removeAllObjects];
[characterLines removeAllObjects];
}

- (void)refreshFonts
{
// Mark force refresh, so that we won't try to use the cached font later.
forceRefreshFont = YES;

// Go through the standard path of updating fonts by passing the current
// font in. This lets Vim itself knows about the font change and initiates
// the resizing (depends on guioption-k) and redraws.
[self changeFont:NSFontManager.sharedFontManager];
}

- (NSFont *)font
Expand Down Expand Up @@ -893,6 +938,9 @@ - (NSSize)minSize
MMMinRows * cellSize.height + insetSize.height + bot);
}

// Called when font panel selection has been made. Send the selected font to
// MMBackend so it would set guifont which will send a message back to MacVim to
// call MMWindowController::setFont.
- (void)changeFont:(id)sender
{
NSFont *newFont = [sender convertFont:font];
Expand Down
6 changes: 6 additions & 0 deletions src/MacVim/MMPreferenceController.m
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,10 @@ - (IBAction)appearanceChanged:(id)sender
[[MMAppController sharedInstance] refreshAllAppearances];
}

- (IBAction)fontPropertiesChanged:(id)sender
{
// Refresh all windows' fonts.
[[MMAppController sharedInstance] refreshAllFonts];
}

@end
1 change: 1 addition & 0 deletions src/MacVim/MMTextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
- (void)setFont:(NSFont *)newFont;
- (NSFont *)fontWide;
- (void)setWideFont:(NSFont *)newFont;
- (void)refreshFonts;
- (NSSize)cellSize;
- (void)setLinespace:(float)newLinespace;
- (void)setColumnspace:(float)newColumnspace;
Expand Down
5 changes: 5 additions & 0 deletions src/MacVim/MMTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ - (void)setWideFont:(NSFont *)newFont
[(MMTextStorage*)[self textStorage] setWideFont:newFont];
}

- (void)refreshFonts
{
// Doesn't do anything. CoreText renderer only.
}

- (NSSize)cellSize
{
return [(MMTextStorage*)[self textStorage] cellSize];
Expand Down
1 change: 1 addition & 0 deletions src/MacVim/MMWindowController.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
- (void)setDefaultColorsBackground:(NSColor *)back foreground:(NSColor *)fore;
- (void)setFont:(NSFont *)font;
- (void)setWideFont:(NSFont *)font;
- (void)refreshFonts;
- (void)processInputQueueDidFinish;
- (void)showTabBar:(BOOL)on;
- (void)showToolbar:(BOOL)on size:(int)size mode:(int)mode;
Expand Down
5 changes: 5 additions & 0 deletions src/MacVim/MMWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,11 @@ - (void)setWideFont:(NSFont *)font
[[vimView textView] setWideFont:font];
}

- (void)refreshFonts
{
[[vimView textView] refreshFonts];
}

- (void)processInputQueueDidFinish
{
// NOTE: Resizing is delayed until after all commands have been processed
Expand Down
1 change: 1 addition & 0 deletions src/MacVim/Miscellaneous.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ extern NSString *MMTranslateCtrlClickKey;
extern NSString *MMTopLeftPointKey;
extern NSString *MMOpenInCurrentWindowKey;
extern NSString *MMNoFontSubstitutionKey; // Deprecated: Non-CoreText renderer
extern NSString *MMFontPreserveLineSpacingKey;
extern NSString *MMAppearanceModeSelectionKey;
extern NSString *MMNoTitleBarWindowKey;
extern NSString *MMTitlebarAppearsTransparentKey;
Expand Down
1 change: 1 addition & 0 deletions src/MacVim/Miscellaneous.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
NSString *MMTopLeftPointKey = @"MMTopLeftPoint";
NSString *MMOpenInCurrentWindowKey = @"MMOpenInCurrentWindow";
NSString *MMNoFontSubstitutionKey = @"MMNoFontSubstitution";
NSString *MMFontPreserveLineSpacingKey = @"MMFontPreserveLineSpacing";
NSString *MMAppearanceModeSelectionKey = @"MMAppearanceModeSelection";
NSString *MMNoTitleBarWindowKey = @"MMNoTitleBarWindow";
NSString *MMTitlebarAppearsTransparentKey = @"MMTitlebarAppearsTransparent";
Expand Down