Skip to content
Snippets Groups Projects
Commit 2c73a1d6 authored by George Nachman's avatar George Nachman
Browse files

Add an advanced pref to use the "correct" algorithm for computing line height,...

Add an advanced pref to use the "correct" algorithm for computing line height, which involves magic incantations. It produces better results for Input Mono Condensed and seems to match what Terminal does. Issue 6159.

Also removed some unneeded baseline offset plumbing.
parent fe761422
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -36,7 +36,10 @@
}
 
@property (nonatomic, assign) NSSize size;
@property (nonatomic, assign) double baseline;
// Returns a text container and layout manager that are ready to measure a capital W.
+ (NSTextContainer *)newTextContainer;
+ (NSLayoutManager *)newLayoutManagerForFont:(NSFont *)aFont textContainer:(NSTextContainer *)textContainer;
 
+ (instancetype)fontSizeEstimatorForFont:(NSFont *)aFont;
 
Loading
Loading
Loading
Loading
@@ -29,11 +29,11 @@
#import "FontSizeEstimator.h"
 
#import "DebugLogging.h"
#import "iTermAdvancedSettingsModel.h"
 
@implementation FontSizeEstimator
 
@synthesize size;
@synthesize baseline;
 
- (instancetype)initWithSize:(NSSize)s baseline:(double)b {
self = [super init];
Loading
Loading
@@ -44,6 +44,25 @@
return self;
}
 
+ (NSLayoutManager *)newLayoutManagerForFont:(NSFont *)aFont textContainer:(NSTextContainer *)textContainer {
NSString *myString = @"W";
NSTextStorage *textStorage = [[[NSTextStorage alloc] initWithString:myString] autorelease];
NSLayoutManager *layoutManager = [[[NSLayoutManager alloc] init] autorelease];
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
[textStorage addAttribute:NSFontAttributeName
value:aFont
range:NSMakeRange(0, [textStorage length])];
[textContainer setLineFragmentPadding:0.0];
[layoutManager glyphRangeForTextContainer:textContainer];
return layoutManager;
}
+ (NSTextContainer *)newTextContainer {
return [[[NSTextContainer alloc] initWithContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)] autorelease];
}
+ (id)fontSizeEstimatorForFont:(NSFont *)aFont
{
FontSizeEstimator* fse = [[[FontSizeEstimator alloc] init] autorelease];
Loading
Loading
@@ -82,10 +101,16 @@
size.width = MAX(1, size.width);
size.height = MAX(1, size.height);
 
double baseline = -(floorf([aFont leading]) - floorf([aFont descender]));
if ([iTermAdvancedSettingsModel useExperimentalFontMetrics]) {
NSTextContainer *textContainer = [self newTextContainer];
NSLayoutManager *layoutManager = [self newLayoutManagerForFont:aFont
textContainer:textContainer];
NSRect usedRect = [layoutManager usedRectForTextContainer:textContainer];
 
fse.size = size;
fse.baseline = baseline;
fse.size = usedRect.size;
} else {
fse.size = size;
}
}
return fse;
}
Loading
Loading
Loading
Loading
@@ -9,6 +9,8 @@
#import "PTYFontInfo.h"
 
#import "DebugLogging.h"
#import "FontSizeEstimator.h"
#import "iTermAdvancedSettingsModel.h"
 
@implementation NSFont(PTYFontInfo)
 
Loading
Loading
@@ -106,7 +108,15 @@
}
 
- (CGFloat)computedBaselineOffset {
return -(floorf(font_.leading) - floorf(self.descender));
if ([iTermAdvancedSettingsModel useExperimentalFontMetrics]) {
NSTextContainer *textContainer = [FontSizeEstimator newTextContainer];
NSLayoutManager *layoutManager = [FontSizeEstimator newLayoutManagerForFont:font_ textContainer:textContainer];
CGFloat lineHeight = [layoutManager usedRectForTextContainer:textContainer].size.height;
CGFloat baselineOffsetFromTop = [layoutManager defaultBaselineOffsetForFont:font_];
return -floorf(lineHeight - baselineOffsetFromTop);
} else {
return -(floorf(font_.leading) - floorf(self.descender));
}
}
 
// From https://github.com/DrawKit/DrawKit/blob/master/framework/Code/NSBezierPath%2BText.m#L648
Loading
Loading
Loading
Loading
@@ -653,33 +653,22 @@ static const int kDragThreshold = 3;
 
+ (NSSize)charSizeForFont:(NSFont *)aFont
horizontalSpacing:(double)hspace
verticalSpacing:(double)vspace
baseline:(double *)baseline {
verticalSpacing:(double)vspace {
FontSizeEstimator* fse = [FontSizeEstimator fontSizeEstimatorForFont:aFont];
NSSize size = [fse size];
size.width = ceil(size.width * hspace);
size.height = ceil(vspace * ceil(size.height + [aFont leading]));
if (baseline) {
*baseline = [fse baseline];
}
return size;
}
 
+ (NSSize)charSizeForFont:(NSFont*)aFont horizontalSpacing:(double)hspace verticalSpacing:(double)vspace
{
return [PTYTextView charSizeForFont:aFont horizontalSpacing:hspace verticalSpacing:vspace baseline:nil];
}
- (void)setFont:(NSFont*)aFont
nonAsciiFont:(NSFont *)nonAsciiFont
horizontalSpacing:(double)horizontalSpacing
verticalSpacing:(double)verticalSpacing
{
double baseline;
NSSize sz = [PTYTextView charSizeForFont:aFont
horizontalSpacing:1.0
verticalSpacing:1.0
baseline:&baseline];
verticalSpacing:1.0];
 
_charWidthWithoutSpacing = sz.width;
_charHeightWithoutSpacing = sz.height;
Loading
Loading
Loading
Loading
@@ -195,5 +195,6 @@
+ (BOOL)statusBarIcon;
+ (BOOL)sensitiveScrollWheel;
+ (BOOL)disableCustomBoxDrawing;
+ (BOOL)useExperimentalFontMetrics;
 
@end
Loading
Loading
@@ -285,4 +285,6 @@ DEFINE_BOOL(killSessionsOnLogout, NO, @"Experimental Features: Kill sessions on
// This causes problems like issue 6052, where repeats cause the IME to swallow subsequent keypresses.
DEFINE_BOOL(experimentalKeyHandling, NO, @"General: Improved support for input method editors like AquaSKK.");
 
DEFINE_BOOL(useExperimentalFontMetrics, NO, @"Experimental Features: Use a more theoretically correct technique to measure line height.\nYou must restart iTerm2 or adjust a session's font size for this change to take effect.");
@end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment