Skip to content

Commit afeb743

Browse files
authored
Merge pull request #1153 from ychin/fix-line-spacing-provide-options
Fix issue with font line spacing support and add option for it
2 parents 5ba08b4 + 5f95572 commit afeb743

14 files changed

+141
-35
lines changed

runtime/doc/gui_mac.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ is sent back to the server application.
241241

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

246246
*macvim-user-defaults*
247247
Not all entries in the user defaults database are exposed via the preference
@@ -265,6 +265,7 @@ KEY VALUE ~
265265
*MMNativeFullScreen* use native full screen mode [bool]
266266
*MMNoFontSubstitution* disable automatic font substitution [bool]
267267
(Deprecated: Non-CoreText renderer only)
268+
*MMFontPreserveLineSpacing* use the line-spacing as specified by font [bool]
268269
*MMNoTitleBarWindow* hide title bar [bool]
269270
*MMTitlebarAppearsTransparent* enable a transparent titlebar [bool]
270271
*MMAppearanceModeSelection* dark mode selection (|macvim-dark-mode|)[bool]

runtime/doc/tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5015,6 +5015,7 @@ MMAppearanceModeSelection gui_mac.txt /*MMAppearanceModeSelection*
50155015
MMCellWidthMultiplier gui_mac.txt /*MMCellWidthMultiplier*
50165016
MMDialogsTrackPwd gui_mac.txt /*MMDialogsTrackPwd*
50175017
MMDisableLaunchAnimation gui_mac.txt /*MMDisableLaunchAnimation*
5018+
MMFontPreserveLineSpacing gui_mac.txt /*MMFontPreserveLineSpacing*
50185019
MMFullScreenFadeTime gui_mac.txt /*MMFullScreenFadeTime*
50195020
MMLoginShellArgument gui_mac.txt /*MMLoginShellArgument*
50205021
MMLoginShellCommand gui_mac.txt /*MMLoginShellCommand*

src/MacVim/Base.lproj/Preferences.xib

Lines changed: 49 additions & 26 deletions
Large diffs are not rendered by default.

src/MacVim/MMAppController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
- (BOOL)openFiles:(NSArray *)filenames withArguments:(NSDictionary *)args;
6060

6161
- (void)refreshAllAppearances;
62+
- (void)refreshAllFonts;
6263

6364
- (IBAction)newWindow:(id)sender;
6465
- (IBAction)newWindowAndActivate:(id)sender;

src/MacVim/MMAppController.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ + (void)initialize
229229
[NSNumber numberWithBool:YES], MMTranslateCtrlClickKey,
230230
[NSNumber numberWithInt:0], MMOpenInCurrentWindowKey,
231231
[NSNumber numberWithBool:NO], MMNoFontSubstitutionKey,
232+
[NSNumber numberWithBool:YES], MMFontPreserveLineSpacingKey,
232233
[NSNumber numberWithBool:YES], MMLoginShellKey,
233234
[NSNumber numberWithInt:MMRendererCoreText],
234235
MMRendererKey,
@@ -1109,6 +1110,15 @@ - (void)refreshAllAppearances
11091110
}
11101111
}
11111112

1113+
- (void)refreshAllFonts
1114+
{
1115+
unsigned count = [vimControllers count];
1116+
for (unsigned i = 0; i < count; ++i) {
1117+
MMVimController *vc = [vimControllers objectAtIndex:i];
1118+
[vc.windowController refreshFonts];
1119+
}
1120+
}
1121+
11121122
- (IBAction)newWindow:(id)sender
11131123
{
11141124
ASLogDebug(@"Open new window");

src/MacVim/MMCoreTextView.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
BOOL ligatures;
3333
BOOL thinStrokes;
3434

35+
BOOL forceRefreshFont; // when true, don't early out of setFont calls.
36+
3537
MMTextViewHelper *helper;
3638

3739
NSMutableDictionary<NSNumber *, NSFont *> *fontVariants;

src/MacVim/MMCoreTextView.m

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ - (id)initWithFrame:(NSRect)frame
221221
{
222222
if (!(self = [super initWithFrame:frame]))
223223
return nil;
224+
225+
forceRefreshFont = NO;
224226

225227
self.wantsLayer = YES;
226228

@@ -374,24 +376,57 @@ - (NSRect)rectForColumnsInRange:(NSRange)range
374376

375377
- (void)setFont:(NSFont *)newFont
376378
{
377-
if (!newFont || [font isEqual:newFont])
379+
if (!newFont) {
378380
return;
381+
}
382+
if (!forceRefreshFont) {
383+
if ([font isEqual:newFont])
384+
return;
385+
}
386+
forceRefreshFont = NO;
379387

380-
double em = round(defaultAdvanceForFont(newFont));
388+
const double em = round(defaultAdvanceForFont(newFont));
381389

382-
float cellWidthMultiplier = [[NSUserDefaults standardUserDefaults]
390+
const float cellWidthMultiplier = [[NSUserDefaults standardUserDefaults]
383391
floatForKey:MMCellWidthMultiplierKey];
384392

393+
// Some fonts have non-standard line heights, and historically MacVim has
394+
// chosen to ignore it. Provide the option for the user to choose whether to
395+
// use the font's line height. If not preserving, will create a new font
396+
// from scratch with just name and pt size, which will disard the line
397+
// height information.
398+
//
399+
// Defaults to the new behavior (preserveLineHeight==true) because it's
400+
// simpler and respects the font's design more.
401+
//
402+
// Note: this behavior is somewhat inconsistent across editors and
403+
// terminals. Xcode, for example, seems to be equivalent to
404+
// (preserveLineHeight==true), but other editors/terminals behave
405+
// differently. Xcode respecting the line height is partially the motivation
406+
// for setting that as the default.
407+
const BOOL preserveLineHeight = [[NSUserDefaults standardUserDefaults]
408+
boolForKey:MMFontPreserveLineSpacingKey];
409+
410+
[font release];
411+
if (!preserveLineHeight) {
412+
double pt = round([newFont pointSize]);
413+
414+
CTFontDescriptorRef desc = CTFontDescriptorCreateWithNameAndSize((CFStringRef)[newFont fontName], pt);
415+
CTFontRef fontRef = CTFontCreateWithFontDescriptor(desc, pt, NULL);
416+
CFRelease(desc);
417+
418+
font = (NSFont*)fontRef;
419+
} else {
420+
font = [newFont retain];
421+
}
422+
fontDescent = ceil(CTFontGetDescent((CTFontRef)font));
423+
385424
// NOTE! Even though NSFontFixedAdvanceAttribute is a float, it will
386425
// only render at integer sizes. Hence, we restrict the cell width to
387426
// an integer here, otherwise the window width and the actual text
388427
// width will not match.
389428
cellSize.width = columnspace + ceil(em * cellWidthMultiplier);
390-
cellSize.height = linespace + defaultLineHeightForFont(newFont);
391-
392-
[font release];
393-
font = [newFont retain];
394-
fontDescent = ceil(CTFontGetDescent((CTFontRef)font));
429+
cellSize.height = linespace + defaultLineHeightForFont(font);
395430

396431
[self clearAll];
397432
[fontVariants removeAllObjects];
@@ -414,7 +449,17 @@ - (void)setWideFont:(NSFont *)newFont
414449
[fontVariants removeAllObjects];
415450
[characterStrings removeAllObjects];
416451
[characterLines removeAllObjects];
452+
}
453+
454+
- (void)refreshFonts
455+
{
456+
// Mark force refresh, so that we won't try to use the cached font later.
457+
forceRefreshFont = YES;
417458

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

420465
- (NSFont *)font
@@ -893,6 +938,9 @@ - (NSSize)minSize
893938
MMMinRows * cellSize.height + insetSize.height + bot);
894939
}
895940

941+
// Called when font panel selection has been made. Send the selected font to
942+
// MMBackend so it would set guifont which will send a message back to MacVim to
943+
// call MMWindowController::setFont.
896944
- (void)changeFont:(id)sender
897945
{
898946
NSFont *newFont = [sender convertFont:font];

src/MacVim/MMPreferenceController.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,10 @@ - (IBAction)appearanceChanged:(id)sender
127127
[[MMAppController sharedInstance] refreshAllAppearances];
128128
}
129129

130+
- (IBAction)fontPropertiesChanged:(id)sender
131+
{
132+
// Refresh all windows' fonts.
133+
[[MMAppController sharedInstance] refreshAllFonts];
134+
}
135+
130136
@end

src/MacVim/MMTextView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
- (void)setFont:(NSFont *)newFont;
4848
- (NSFont *)fontWide;
4949
- (void)setWideFont:(NSFont *)newFont;
50+
- (void)refreshFonts;
5051
- (NSSize)cellSize;
5152
- (void)setLinespace:(float)newLinespace;
5253
- (void)setColumnspace:(float)newColumnspace;

src/MacVim/MMTextView.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ - (void)setWideFont:(NSFont *)newFont
352352
[(MMTextStorage*)[self textStorage] setWideFont:newFont];
353353
}
354354

355+
- (void)refreshFonts
356+
{
357+
// Doesn't do anything. CoreText renderer only.
358+
}
359+
355360
- (NSSize)cellSize
356361
{
357362
return [(MMTextStorage*)[self textStorage] cellSize];

src/MacVim/MMWindowController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
- (void)setDefaultColorsBackground:(NSColor *)back foreground:(NSColor *)fore;
8383
- (void)setFont:(NSFont *)font;
8484
- (void)setWideFont:(NSFont *)font;
85+
- (void)refreshFonts;
8586
- (void)processInputQueueDidFinish;
8687
- (void)showTabBar:(BOOL)on;
8788
- (void)showToolbar:(BOOL)on size:(int)size mode:(int)mode;

src/MacVim/MMWindowController.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,11 @@ - (void)setWideFont:(NSFont *)font
721721
[[vimView textView] setWideFont:font];
722722
}
723723

724+
- (void)refreshFonts
725+
{
726+
[[vimView textView] refreshFonts];
727+
}
728+
724729
- (void)processInputQueueDidFinish
725730
{
726731
// NOTE: Resizing is delayed until after all commands have been processed

src/MacVim/Miscellaneous.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extern NSString *MMTranslateCtrlClickKey;
3333
extern NSString *MMTopLeftPointKey;
3434
extern NSString *MMOpenInCurrentWindowKey;
3535
extern NSString *MMNoFontSubstitutionKey; // Deprecated: Non-CoreText renderer
36+
extern NSString *MMFontPreserveLineSpacingKey;
3637
extern NSString *MMAppearanceModeSelectionKey;
3738
extern NSString *MMNoTitleBarWindowKey;
3839
extern NSString *MMTitlebarAppearsTransparentKey;

src/MacVim/Miscellaneous.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
NSString *MMTopLeftPointKey = @"MMTopLeftPoint";
3030
NSString *MMOpenInCurrentWindowKey = @"MMOpenInCurrentWindow";
3131
NSString *MMNoFontSubstitutionKey = @"MMNoFontSubstitution";
32+
NSString *MMFontPreserveLineSpacingKey = @"MMFontPreserveLineSpacing";
3233
NSString *MMAppearanceModeSelectionKey = @"MMAppearanceModeSelection";
3334
NSString *MMNoTitleBarWindowKey = @"MMNoTitleBarWindow";
3435
NSString *MMTitlebarAppearsTransparentKey = @"MMTitlebarAppearsTransparent";

0 commit comments

Comments
 (0)