Skip to content

Commit

Permalink
Merge pull request #347 from thecoolwinter/fix/font-char-width
Browse files Browse the repository at this point in the history
Use W Hack For macOS Cell Width
  • Loading branch information
migueldeicaza authored Aug 7, 2024
2 parents 0f1cd24 + f54defd commit e9cfa44
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Sources/SwiftTerm/Apple/AppleTerminalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<NSSize>.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
Expand Down

0 comments on commit e9cfa44

Please sign in to comment.