@@ -45,7 +45,11 @@ public class GutterView: NSView {
45
45
var textColor : NSColor = . secondaryLabelColor
46
46
47
47
@Invalidating ( . display)
48
- var font : NSFont = . systemFont( ofSize: 13 )
48
+ var font : NSFont = . systemFont( ofSize: 13 ) {
49
+ didSet {
50
+ updateFontLineHeight ( )
51
+ }
52
+ }
49
53
50
54
@Invalidating ( . display)
51
55
var edgeInsets : EdgeInsets = EdgeInsets ( leading: 20 , trailing: 12 )
@@ -74,6 +78,19 @@ public class GutterView: NSView {
74
78
/// The maximum number of digits found for a line number.
75
79
private var maxLineLength : Int = 0
76
80
81
+ private var fontLineHeight = 1.0
82
+
83
+ private func updateFontLineHeight( ) {
84
+ let string = NSAttributedString ( string: " 0 " , attributes: [ . font: font] )
85
+ let typesetter = CTTypesetterCreateWithAttributedString ( string)
86
+ let ctLine = CTTypesetterCreateLine ( typesetter, CFRangeMake ( 0 , 1 ) )
87
+ var ascent : CGFloat = 0
88
+ var descent : CGFloat = 0
89
+ var leading : CGFloat = 0
90
+ CTLineGetTypographicBounds ( ctLine, & ascent, & descent, & leading)
91
+ fontLineHeight = ( ascent + descent + leading)
92
+ }
93
+
77
94
override public var isFlipped : Bool {
78
95
true
79
96
}
@@ -181,7 +198,7 @@ public class GutterView: NSView {
181
198
y: line. yPos,
182
199
width: width,
183
200
height: line. height
184
- )
201
+ ) . pixelAligned
185
202
)
186
203
}
187
204
@@ -217,12 +234,16 @@ public class GutterView: NSView {
217
234
let fragment : LineFragment ? = linePosition. data. lineFragments. first? . data
218
235
var ascent : CGFloat = 0
219
236
let lineNumberWidth = CTLineGetTypographicBounds ( ctLine, & ascent, nil , nil )
237
+ let fontHeightDifference = ( ( fragment? . height ?? 0 ) - fontLineHeight) / 4
220
238
221
- let yPos = linePosition. yPos + ascent + ( fragment? . heightDifference ?? 0 ) / 2
239
+ let yPos = linePosition. yPos + ascent + ( fragment? . heightDifference ?? 0 ) / 2 + fontHeightDifference
222
240
// Leading padding + (width - linewidth)
223
241
let xPos = edgeInsets. leading + ( maxWidth - lineNumberWidth)
224
242
225
- context. textPosition = CGPoint ( x: xPos, y: yPos) . pixelAligned
243
+ ContextSetHiddenSmoothingStyle ( context, 16 )
244
+
245
+ context. textPosition = CGPoint ( x: xPos, y: yPos)
246
+
226
247
CTLineDraw ( ctLine, context)
227
248
}
228
249
context. restoreGState ( )
0 commit comments