From 1c60bbc738f72447d9d32bda1e1654692498f990 Mon Sep 17 00:00:00 2001 From: LEO Yoon-Tsaw Date: Fri, 31 May 2024 10:49:04 -0400 Subject: [PATCH] [Fix] Better font creator --- sources/SquirrelTheme.swift | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/sources/SquirrelTheme.swift b/sources/SquirrelTheme.swift index f57e32b07..fd657e35b 100644 --- a/sources/SquirrelTheme.swift +++ b/sources/SquirrelTheme.swift @@ -300,18 +300,25 @@ private extension SquirrelTheme { let fontStrings = fontString.split(separator: ",") var fonts = [NSFont]() for string in fontStrings { - let trimedString = string.trimmingCharacters(in: .whitespaces) - let familyName = if let firstPart = trimedString.split(separator: "-").first { - String(firstPart) - } else { - trimedString - } - if !seenFontFamilies.contains(familyName) { - seenFontFamilies.insert(familyName) - if let validFont = NSFont(name: trimedString, size: Self.defaultFontSize) { - fonts.append(validFont) + if let matchedFontName = try? /^\s*(.+)-([^-]+)\s*$/.firstMatch(in: string) { + let family = String(matchedFontName.output.1) + let style = String(matchedFontName.output.2) + if seenFontFamilies.contains(family) { continue } + let fontDescriptor = NSFontDescriptor(fontAttributes: [.family: family, .face: style]) + if let font = NSFont(descriptor: fontDescriptor, size: Self.defaultFontSize) { + fonts.append(font) + seenFontFamilies.insert(family) + continue } } + let fontName = string.trimmingCharacters(in: .whitespaces) + if seenFontFamilies.contains(fontName) { continue } + let fontDescriptor = NSFontDescriptor(fontAttributes: [.name: fontName]) + if let font = NSFont(descriptor: fontDescriptor, size: Self.defaultFontSize) { + fonts.append(font) + seenFontFamilies.insert(fontName) + continue + } } return fonts }