Branchwell

Inkspring / app · inkspring.ai / native-mac / Sources / Inkspring / InkspringTypography.swift

1import CoreText
2import SwiftUI
3
4enum InkspringTypography {
5 private enum Family: String {
6 case newsreader = "Newsreader"
7 case geist = "Geist"
8
9 var postScriptName: String {
10 switch self {
11 case .newsreader: "Newsreader16pt16pt-Regular"
12 case .geist: "Geist-Regular"
13 }
14 }
15 }
16
17 private static let registeredFonts = Set([Family.newsreader, .geist].filter { family in
18 guard let url = Bundle.main.url(forResource: family.rawValue, withExtension: "ttf", subdirectory: "Fonts") else {
19 return false
20 }
21 return CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil)
22 })
23
24 static func registerFonts() {
25 _ = registeredFonts
26 }
27
28 static func editorial(_ size: CGFloat, relativeTo style: Font.TextStyle = .body) -> Font {
29 registeredFonts.contains(.newsreader)
30 ? .custom(Family.newsreader.postScriptName, size: size, relativeTo: style)
31 : .system(size: size, design: .serif)
32 }
33
34 static func editorialCapHeight(_ size: CGFloat) -> CGFloat {
35 let name = registeredFonts.contains(.newsreader) ? Family.newsreader.postScriptName : "Times-Roman"
36 return CTFontGetCapHeight(CTFontCreateWithName(name as CFString, size, nil))
37 }
38
39 static func interface(_ size: CGFloat) -> Font {
40 registeredFonts.contains(.geist)
41 ? .custom(Family.geist.postScriptName, size: size, relativeTo: .body)
42 : .system(size: size)
43 }
44}