From fd58508c4bad2fb2abaeb547d77823fbbaabe27d Mon Sep 17 00:00:00 2001 From: Marcin Krzyzanowski Date: Mon, 13 Apr 2020 12:29:51 +0200 Subject: [PATCH 1/3] Use line metrics (not normal font metrics) to adjust text position --- SwiftTerm/Sources/SwiftTerm/Mac/MacTerminalView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SwiftTerm/Sources/SwiftTerm/Mac/MacTerminalView.swift b/SwiftTerm/Sources/SwiftTerm/Mac/MacTerminalView.swift index 41595d0..f4e273c 100644 --- a/SwiftTerm/Sources/SwiftTerm/Mac/MacTerminalView.swift +++ b/SwiftTerm/Sources/SwiftTerm/Mac/MacTerminalView.swift @@ -669,7 +669,7 @@ public class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations } // The code above is CTLineDraw() in disguise - let baseLineAdj = font.normal.descender + font.normal.leading + let baseLineAdj = -(lineDescent + lineLeading) context.textPosition = CGPoint (x: 0, y: lineOrigin.y - baseLineAdj) CTLineDraw (line, context) From b645fda692e012d215230934a49ee30c4ed9a445 Mon Sep 17 00:00:00 2001 From: Marcin Krzyzanowski Date: Mon, 13 Apr 2020 12:34:08 +0200 Subject: [PATCH 2/3] rename defaultLineHeight -> estimatedLineHeight --- .../SwiftTerm/Mac/MacSelectionView.swift | 2 +- .../SwiftTerm/Mac/MacTerminalView.swift | 20 +++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/SwiftTerm/Sources/SwiftTerm/Mac/MacSelectionView.swift b/SwiftTerm/Sources/SwiftTerm/Mac/MacSelectionView.swift index 13956bc..c2b5021 100644 --- a/SwiftTerm/Sources/SwiftTerm/Mac/MacSelectionView.swift +++ b/SwiftTerm/Sources/SwiftTerm/Mac/MacSelectionView.swift @@ -22,7 +22,7 @@ class SelectionView: NSView { terminalView.selection } private var defaultLineHeight: CGFloat { - terminalView.defaultLineHeight + terminalView.estimatedLineHeight } public init(terminalView: TerminalView, frame: CGRect) diff --git a/SwiftTerm/Sources/SwiftTerm/Mac/MacTerminalView.swift b/SwiftTerm/Sources/SwiftTerm/Mac/MacTerminalView.swift index f4e273c..8cf246e 100644 --- a/SwiftTerm/Sources/SwiftTerm/Mac/MacTerminalView.swift +++ b/SwiftTerm/Sources/SwiftTerm/Mac/MacTerminalView.swift @@ -79,7 +79,7 @@ public class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations var accessibility: AccessibilityService = AccessibilityService() var search: SearchService! /// Precalculated line height - var defaultLineHeight: CGFloat! + var estimatedLineHeight: CGFloat! var selectionView: SelectionView! var selection: SelectionService! var scroller: NSScroller! @@ -129,9 +129,9 @@ public class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations // Calculation assume that all glyphs in the font have the same advancement. // Get the ascent + descent + leading from the font, already scaled for the font's size - self.defaultLineHeight = CTFontGetAscent (font.normal) + CTFontGetDescent (font.normal) + CTFontGetLeading (font.normal) + self.estimatedLineHeight = CTFontGetAscent (font.normal) + CTFontGetDescent (font.normal) + CTFontGetLeading (font.normal) let options = TerminalOptions(cols: Int(bounds.width / font.normal.maximumAdvancement.width), - rows: Int(bounds.height / defaultLineHeight)) + rows: Int(bounds.height / estimatedLineHeight)) terminal = Terminal(delegate: self, options: options) fullBufferUpdate() @@ -146,7 +146,7 @@ public class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations addSubview(selectionView) // Install carret view - caretView = CaretView(frame: CGRect(origin: .zero, size: CGSize(width: font.normal.maximumAdvancement.width, height: defaultLineHeight))) + caretView = CaretView(frame: CGRect(origin: .zero, size: CGSize(width: font.normal.maximumAdvancement.width, height: estimatedLineHeight))) addSubview(caretView) search = SearchService (terminal: terminal) @@ -218,7 +218,7 @@ public class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations */ public func getOptimalFrameSize () -> NSRect { - return NSRect (x: 0, y: 0, width: font.normal.maximumAdvancement.width * CGFloat(terminal.cols), height: defaultLineHeight * CGFloat(terminal.rows)) + return NSRect (x: 0, y: 0, width: font.normal.maximumAdvancement.width * CGFloat(terminal.cols), height: estimatedLineHeight * CGFloat(terminal.rows)) } public func scrolled(source terminal: Terminal, yDisp: Int) { @@ -527,9 +527,9 @@ public class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations // BROKEN: let baseLine = frame.height let region = CGRect (x: 0, - y: baseLine - (defaultLineHeight + CGFloat(rowEnd) * defaultLineHeight), + y: baseLine - (estimatedLineHeight + CGFloat(rowEnd) * estimatedLineHeight), width: frame.width, - height: CGFloat(rowEnd-rowStart + 1) * defaultLineHeight) + height: CGFloat(rowEnd-rowStart + 1) * estimatedLineHeight) //print ("Region: \(region)") setNeedsDisplay(region) @@ -687,7 +687,7 @@ public class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations func getCaretPos(_ col: Int, _ row: Int) -> CGPoint { let x = self.characterOffset (atRow: row, col: col) - let y = frame.height - (defaultLineHeight + (CGFloat (row) * defaultLineHeight)) + let y = frame.height - (estimatedLineHeight + (CGFloat (row) * estimatedLineHeight)) return CGPoint (x: x, y: y) } @@ -751,7 +751,7 @@ public class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations set(newValue) { super.frame = newValue - let newRows = Int (newValue.height / defaultLineHeight) + let newRows = Int (newValue.height / estimatedLineHeight) let newCols = Int (newValue.width / font.normal.maximumAdvancement.width) if newCols != terminal.cols || newRows != terminal.rows { @@ -1243,7 +1243,7 @@ public class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations func calculateMouseHit (with event: NSEvent) -> Position { let point = convert(event.locationInWindow, from: nil) - let row = Int((frame.height - point.y) / defaultLineHeight) + let row = Int((frame.height - point.y) / estimatedLineHeight) if row < 0 { return Position(col: 0, row: 0) } From e258cd7dce6b93a6ab5c12dd893b16dbd7b97f88 Mon Sep 17 00:00:00 2001 From: Marcin Krzyzanowski Date: Mon, 13 Apr 2020 13:23:24 +0200 Subject: [PATCH 3/3] Update full display because dirty region calculations are flawed --- .../SwiftTerm/Mac/MacTerminalView.swift | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/SwiftTerm/Sources/SwiftTerm/Mac/MacTerminalView.swift b/SwiftTerm/Sources/SwiftTerm/Mac/MacTerminalView.swift index 8cf246e..459e4b2 100644 --- a/SwiftTerm/Sources/SwiftTerm/Mac/MacTerminalView.swift +++ b/SwiftTerm/Sources/SwiftTerm/Mac/MacTerminalView.swift @@ -523,16 +523,21 @@ public class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations attrStrBuffer [row + tb.yDisp] = buildAttributedString (line: line, cols: cols, prefix: "") } - //print ("Dirty is \(rowStart) to \(rowEnd)") - // BROKEN: - let baseLine = frame.height - let region = CGRect (x: 0, - y: baseLine - (estimatedLineHeight + CGFloat(rowEnd) * estimatedLineHeight), - width: frame.width, - height: CGFloat(rowEnd-rowStart + 1) * estimatedLineHeight) - - //print ("Region: \(region)") - setNeedsDisplay(region) + #if false + // FIXME: Calculations are broken because based on estimatedLineHeight. + // See https://github.com/migueldeicaza/SwiftTerm/issues/71 for example + let baseLine = frame.height + let region = CGRect (x: 0, + y: baseLine - (estimatedLineHeight + CGFloat(rowEnd) * estimatedLineHeight), + width: frame.width, + height: CGFloat(rowEnd-rowStart + 1) * estimatedLineHeight) + + //print ("Region: \(region)") + setNeedsDisplay(region) + #else + needsDisplay = true + #endif + pendingDisplay = false debug?.update()