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

GH-405 Vertically center-align text when linespacing > 1

This commit is contained in:
Tae Won Ha 2021-11-06 14:05:25 +01:00
parent 15ebbd4ee2
commit f503b9e47e
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
3 changed files with 13 additions and 5 deletions

View File

@ -181,7 +181,9 @@ final class AttributesRunDrawer {
self.cellSize = FontUtils.cellSize(
of: self.font, linespacing: self.linespacing, characterspacing: self.characterspacing
)
self.baselineOffset = self.cellSize.height - CTFontGetAscent(self.font)
self
.baselineOffset = (self.cellSize.height + FontUtils.fontHeight(of: self.font)) / 2 -
CTFontGetAscent(self.font)
self.descent = CTFontGetDescent(self.font)
self.underlinePosition = CTFontGetUnderlinePosition(self.font)
self.underlineThickness = CTFontGetUnderlineThickness(self.font)

View File

@ -23,15 +23,19 @@ extension FontTrait: Hashable {}
final class FontUtils {
static func cellHeight(of font: NSFont) -> CGFloat {
static func fontHeight(of font: NSFont) -> CGFloat {
if let cached = fontHeightCache.valueForKey(font) { return cached }
let ascent = CTFontGetAscent(font)
let descent = CTFontGetDescent(font)
let leading = CTFontGetLeading(font)
let height = ceil(ascent + descent + leading)
return ceil(ascent + descent + leading)
fontHeightCache.set(height, forKey: font)
return height
}
static func cellWidth(of font: NSFont) -> CGFloat {
static func fontWidth(of font: NSFont) -> CGFloat {
let capitalM = [UniChar(0x004D)]
var glyph = [CGGlyph(0)]
var advancement = CGSize.zero
@ -49,7 +53,7 @@ final class FontUtils {
)
}
let cellSizeToCache = CGSize(width: cellWidth(of: font), height: cellHeight(of: font))
let cellSizeToCache = CGSize(width: fontWidth(of: font), height: fontHeight(of: font))
cellSizeWithDefaultLinespacingCache.set(cellSizeToCache, forKey: font)
let cellSize = CGSize(
@ -112,6 +116,7 @@ final class FontUtils {
}
private let fontCache = FifoCache<SizedFontTrait, NSFont>(count: 100, queueQos: .userInteractive)
private let fontHeightCache = FifoCache<NSFont, CGFloat>(count: 100, queueQos: .userInteractive)
private let cellSizeWithDefaultLinespacingCache = FifoCache<NSFont, CGSize>(
count: 100,
queueQos: .userInteractive

View File

@ -1,6 +1,7 @@
# Next
* GH-874: Make Chinese input possible (and simplify input method handling in general), thank you very much, @SolarWing!
* GH-805 (GH-415): Vertically center-align text. Thank you @apaleslimghost!
* ...
## 0.35.0-20211105.213803