From f54defdbd1335bf06b426e971def16d71c9541f1 Mon Sep 17 00:00:00 2001 From: Khan Winter <35942988+thecoolwinter@users.noreply.github.com> Date: Wed, 7 Aug 2024 18:07:54 -0500 Subject: [PATCH] Use W Hack For macOS Cell Width --- Sources/SwiftTerm/Apple/AppleTerminalView.swift | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftTerm/Apple/AppleTerminalView.swift b/Sources/SwiftTerm/Apple/AppleTerminalView.swift index 35ce9ce..23407a7 100644 --- a/Sources/SwiftTerm/Apple/AppleTerminalView.swift +++ b/Sources/SwiftTerm/Apple/AppleTerminalView.swift @@ -146,7 +146,19 @@ extension TerminalView { let lineLeading = CTFontGetLeading (fontSet.normal) let cellHeight = ceil(lineAscent + lineDescent + lineLeading) #if os(macOS) - let cellWidth = fontSet.normal.maximumAdvancement.width + // The following is a more robust way of getting the largest ascii character width, but comes with a performance hit. + // See: https://github.com/migueldeicaza/SwiftTerm/issues/286 + // var sizes = UnsafeMutablePointer.allocate(capacity: 95) + // let ctFont = (font as CTFont) + // var glyphs = (32..<127).map { CTFontGetGlyphWithName(ctFont, String(Unicode.Scalar($0)) as CFString) } + // withUnsafePointer(to: glyphs[0]) { glyphsPtr in + // fontSet.normal.getAdvancements(NSSizeArray(sizes), forCGGlyphs: glyphsPtr, count: 95) + // } + // let cellWidth = (0..<95).reduce(into: 0) { partialResult, idx in + // partialResult = max(partialResult, sizes[idx].width) + // } + let glyph = fontSet.normal.glyph(withName: "W") + let cellWidth = fontSet.normal.advancement(forGlyph: glyph).width #else let fontAttributes = [NSAttributedString.Key.font: fontSet.normal] let cellWidth = "W".size(withAttributes: fontAttributes).width