[PATCH] Fix for vertically misaligned text
By thecloudexpa... on September 28, 2010 01:08 (imported from Google Code)
The method of calculating the baseline when drawing characters is incorrect in both the original iTerm and iTerm2. It only took the font's descender into account, rather than the descender AND the leading. This results in text that is vertically misaligned (i.e. drawn too low on the line). The bug is especially noticeable when using non-default fonts like Inconsolata. It might also be the root cause of the issue previously reported with East Asian fonts.
Here is a diff against the current svn trunk which fixes the issue.
Index: PTYTextView.m
--- PTYTextView.m (revision 188)
+++ PTYTextView.m (working copy)
@@ -474,6 +475,7 @@
verticalSpacing_ = verticalSpacing;
charWidth = ceil(charWidthWithoutSpacing * horizontalSpacing);
lineHeight = ceil(charHeightWithoutSpacing * verticalSpacing);
-
baselineOffset = floorf([aFont leading]) - floorf([aFont descender] + 0.5);
[font release];
[aFont retain];
font=aFont;
@@ -2953,6 +2955,10 @@
if (oldFont != theFont) {
[attrib setObject:theFont forKey: NSFontAttributeName];
} -
Y += lineHeight - baselineOffset;
-
Y = (int)Y;
-
X = (int)X;
if (code < 128) {
char ascii_char = (char)code;
@@ -2975,9 +2981,6 @@
CGContextSetTextDrawingMode(ctx, kCGTextFill);
CGContextSetShouldAntialias(ctx, [self antiAlias]);
- Y += lineHeight + [theFont descender];
- Y = (int)Y;
- X = (int)X;
CGContextShowTextAtPoint(ctx, X, Y, &ascii_char, 1);
CGContextFillPath(ctx);
if (renderBold) {
@@ -2985,9 +2988,6 @@
CGContextFillPath(ctx);
}
} else {
- Y += lineHeight + [theFont descender];
- Y = (int)Y;
- X = (int)X;
NSString* charToDraw = [NSString stringWithCharacters:&code length:1];
[charToDraw drawWithRect:NSMakeRect(X,Y, 0, 0) options:0 attributes:attrib];
Index: Headers/iTerm/PTYTextView.h
--- Headers/iTerm/PTYTextView.h (revision 188)
+++ Headers/iTerm/PTYTextView.h (working copy)
@@ -77,6 +77,7 @@
float lineWidth;
float charWidth;
float charWidthWithoutSpacing, charHeightWithoutSpacing;
- float baselineOffset;
float horizontalSpacing_;
float verticalSpacing_;