1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-23 19:21:53 +03:00

Merge remote-tracking branch 'origin/pr/805' into 805

- Did not change AttributesRunDrawer since it breaks the vertical
  positioning with linespacing = 1.0.

 Conflicts:
	NvimView/Sources/NvimView/AttributesRunDrawer.swift
	NvimView/Sources/NvimView/FontUtils.swift
This commit is contained in:
Tae Won Ha 2021-11-06 13:34:33 +01:00
commit 15ebbd4ee2
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44

View File

@ -21,7 +21,26 @@ private struct SizedFontTrait: Hashable {
extension FontTrait: Hashable {}
enum FontUtils {
final class FontUtils {
static func cellHeight(of font: NSFont) -> CGFloat {
let ascent = CTFontGetAscent(font)
let descent = CTFontGetDescent(font)
let leading = CTFontGetLeading(font)
return ceil(ascent + descent + leading)
}
static func cellWidth(of font: NSFont) -> CGFloat {
let capitalM = [UniChar(0x004D)]
var glyph = [CGGlyph(0)]
var advancement = CGSize.zero
CTFontGetGlyphsForCharacters(font, capitalM, &glyph, 1)
CTFontGetAdvancesForGlyphs(font, .horizontal, glyph, &advancement, 1)
return advancement.width
}
static func cellSize(of font: NSFont, linespacing: CGFloat, characterspacing: CGFloat) -> CGSize {
if let cached = cellSizeWithDefaultLinespacingCache.valueForKey(font) {
return CGSize(
@ -30,21 +49,11 @@ enum FontUtils {
)
}
let capitalM = [UniChar(0x004D)]
var glyph = [CGGlyph(0)]
var advancement = CGSize.zero
CTFontGetGlyphsForCharacters(font, capitalM, &glyph, 1)
CTFontGetAdvancesForGlyphs(font, .horizontal, glyph, &advancement, 1)
let ascent = CTFontGetAscent(font)
let descent = CTFontGetDescent(font)
let leading = CTFontGetLeading(font)
let cellSizeToCache = CGSize(width: advancement.width, height: ceil(ascent + descent + leading))
let cellSizeToCache = CGSize(width: cellWidth(of: font), height: cellHeight(of: font))
cellSizeWithDefaultLinespacingCache.set(cellSizeToCache, forKey: font)
let cellSize = CGSize(
width: characterspacing * advancement.width,
width: characterspacing * cellSizeToCache.width,
height: ceil(linespacing * cellSizeToCache.height)
)