Skip to content

Commit

Permalink
[Fix] Improve font fallback
Browse files Browse the repository at this point in the history
fix #917, #915
  • Loading branch information
LEOYoon-Tsaw committed May 31, 2024
1 parent 6f1a6b4 commit 111e615
Showing 1 changed file with 55 additions and 27 deletions.
82 changes: 55 additions & 27 deletions sources/SquirrelTheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,41 @@ final class SquirrelTheme {
private var fonts = [NSFont]()
private var labelFonts = [NSFont]()
private var commentFonts = [NSFont]()
private var fontSize: CGFloat?
private var labelFontSize: CGFloat?
private var commentFontSize: CGFloat?

private var candidateTemplate = "[label]. [candidate] [comment]"
var statusMessageType: StatusMessageType = .mix

private var defaultFont: NSFont {
if let size = fontSize {
Self.defaultFont.withSize(size)
} else {
Self.defaultFont
}
}

var font: NSFont {
return combineFonts(fonts) ?? Self.defaultFont
return combineFonts(fonts, size: fontSize) ?? defaultFont
}
var labelFont: NSFont? {
return combineFonts(labelFonts)
var labelFont: NSFont {
if let font = combineFonts(labelFonts, size: labelFontSize ?? fontSize) {
return font
} else if let size = labelFontSize {
return self.font.withSize(size)
} else {
return self.font
}
}
var commentFont: NSFont? {
return combineFonts(commentFonts)
var commentFont: NSFont {
if let font = combineFonts(commentFonts, size: commentFontSize ?? fontSize) {
return font
} else if let size = commentFontSize {
return self.font.withSize(size)
} else {
return self.font
}
}
var attrs: [NSAttributedString.Key: Any] {
[.foregroundColor: candidateTextColor,
Expand All @@ -93,23 +116,23 @@ final class SquirrelTheme {
}
var labelAttrs: [NSAttributedString.Key: Any] {
return [.foregroundColor: candidateLabelColor ?? blendColor(foregroundColor: self.candidateTextColor, backgroundColor: self.backgroundColor),
.font: labelFont ?? font,
.baselineOffset: baseOffset + (labelFont != nil && !vertical ? (font.pointSize - labelFont!.pointSize) / 2 : 0)]
.font: labelFont,
.baselineOffset: baseOffset + (!vertical ? (font.pointSize - labelFont.pointSize) / 2 : 0)]
}
var labelHighlightedAttrs: [NSAttributedString.Key: Any] {
return [.foregroundColor: highlightedCandidateLabelColor ?? blendColor(foregroundColor: highlightedCandidateTextColor, backgroundColor: highlightedBackColor),
.font: labelFont ?? font,
.baselineOffset: baseOffset + (labelFont != nil && !vertical ? (font.pointSize - labelFont!.pointSize) / 2 : 0)]
.font: labelFont,
.baselineOffset: baseOffset + (!vertical ? (font.pointSize - labelFont.pointSize) / 2 : 0)]
}
var commentAttrs: [NSAttributedString.Key: Any] {
return [.foregroundColor: commentTextColor ?? candidateTextColor,
.font: commentFont ?? font,
.baselineOffset: baseOffset + (commentFont != nil && !vertical ? (font.pointSize - commentFont!.pointSize) / 2 : 0)]
.font: commentFont,
.baselineOffset: baseOffset + (!vertical ? (font.pointSize - commentFont.pointSize) / 2 : 0)]
}
var commentHighlightedAttrs: [NSAttributedString.Key: Any] {
return [.foregroundColor: highlightedCommentTextColor ?? highlightedCandidateTextColor,
.font: commentFont ?? font,
.baselineOffset: baseOffset + (commentFont != nil && !vertical ? (font.pointSize - commentFont!.pointSize) / 2 : 0)]
.font: commentFont,
.baselineOffset: baseOffset + (!vertical ? (font.pointSize - commentFont.pointSize) / 2 : 0)]
}
var preeditAttrs: [NSAttributedString.Key: Any] {
[.foregroundColor: textColor,
Expand Down Expand Up @@ -246,28 +269,33 @@ final class SquirrelTheme {
} else {
native = true
}
if let name = fontName {
fonts = decodeFonts(from: name, size: fontSize)
}
if let name = labelFontName ?? fontName {
labelFonts = decodeFonts(from: name, size: labelFontSize ?? fontSize)
}
if let name = commentFontName ?? fontName {
commentFonts = decodeFonts(from: name, size: commentFontSize ?? fontSize)
}

fonts = decodeFonts(from: fontName)
self.fontSize = fontSize
labelFonts = decodeFonts(from: labelFontName ?? fontName)
self.labelFontSize = labelFontSize
commentFonts = decodeFonts(from: commentFontName ?? fontName)
self.commentFontSize = commentFontSize
}
}

private extension SquirrelTheme {
func combineFonts(_ fonts: [NSFont]) -> NSFont? {
func combineFonts(_ fonts: [NSFont], size: CGFloat?) -> NSFont? {
if fonts.count == 0 { return nil }
if fonts.count == 1 { return fonts[0] }
if fonts.count == 1 {
if let size = size {
return fonts[0].withSize(size)
} else {
return fonts[0]
}
}
let attribute = [NSFontDescriptor.AttributeName.cascadeList: fonts[1...].map { $0.fontDescriptor } ]
let fontDescriptor = fonts[0].fontDescriptor.addingAttributes(attribute)
return NSFont.init(descriptor: fontDescriptor, size: fonts[0].pointSize)
return NSFont.init(descriptor: fontDescriptor, size: size ?? fonts[0].pointSize)
}

func decodeFonts(from fontString: String, size: CGFloat?) -> [NSFont] {
func decodeFonts(from fontString: String?) -> [NSFont] {
guard let fontString = fontString else { return [] }
var seenFontFamilies = Set<String>()
let fontStrings = fontString.split(separator: ",")
var fonts = [NSFont]()
Expand All @@ -280,7 +308,7 @@ private extension SquirrelTheme {
}
if !seenFontFamilies.contains(familyName) {
seenFontFamilies.insert(familyName)
if let validFont = NSFont(name: trimedString, size: size ?? Self.defaultFontSize) {
if let validFont = NSFont(name: trimedString, size: Self.defaultFontSize) {
fonts.append(validFont)
}
}
Expand Down

0 comments on commit 111e615

Please sign in to comment.