| 1 | import CoreText |
| 2 | import Foundation |
| 3 | import Synchronization |
| 4 | import SwiftUI |
| 5 | |
| 6 | /// Base's faces (P8). Timeless Sans is one variable file with two cuts on |
| 7 | /// its STYL axis: Grotesk (STYL 0) for headings and the wordmark, Sans |
| 8 | /// (STYL 100) for everything else, as the web sets it (`h1, h2, .wordmark` |
| 9 | /// at STYL 0; the body at 100). The file is fetched, never committed |
| 10 | /// (`make fonts`); without it the app is set in DM Sans, as the web is. |
| 11 | /// |
| 12 | /// `register()` runs at launch, before the first frame, so nothing ever |
| 13 | /// paints in a fallback face while the file is there (no FOUT). |
| 14 | public enum BaseFonts { |
| 15 | public enum Face: Sendable, Equatable { case timeless, dmSans, system } |
| 16 | |
| 17 | /// The bundled faces: BaseKit's Resources/Fonts, filled by |
| 18 | /// scripts/fonts-into-bundle.sh. |
| 19 | public static var bundled: URL? { Bundle.module.url(forResource: "Fonts", withExtension: nil) } |
| 20 | |
| 21 | private static let state = Mutex<(face: Face, registered: Set<String>)>((.system, [])) |
| 22 | |
| 23 | /// The face in use: Timeless Sans once registered, DM Sans without it, |
| 24 | /// the system's before anything is registered. |
| 25 | public static var face: Face { state.withLock { $0.face } } |
| 26 | |
| 27 | /// Registers Timeless Sans from `directory`, or DM Sans when it is not |
| 28 | /// there, for this process. Idempotent; never throws or traps: a |
| 29 | /// missing or unreadable file falls back, and with neither file the app |
| 30 | /// is set in the system face. |
| 31 | @discardableResult |
| 32 | public static func register(from directory: URL? = bundled) -> Face { |
| 33 | let candidates: [(Face, String)] = [(.timeless, "TimelessSansVF.woff2"), (.dmSans, "DMSans-VF.woff2")] |
| 34 | for (face, file) in candidates { |
| 35 | guard let url = directory?.appendingPathComponent(file), FileManager.default.fileExists(atPath: url.path) else { continue } |
| 36 | if registerFile(url) { |
| 37 | state.withLock { $0.face = face } |
| 38 | return face |
| 39 | } |
| 40 | } |
| 41 | state.withLock { $0.face = .system } |
| 42 | return .system |
| 43 | } |
| 44 | |
| 45 | private static func registerFile(_ url: URL) -> Bool { |
| 46 | if state.withLock({ $0.registered.contains(url.path) }) { return true } |
| 47 | var error: Unmanaged<CFError>? |
| 48 | let ok = CTFontManagerRegisterFontsForURL(url as CFURL, .process, &error) |
| 49 | // Already registered by an earlier call in this process: usable. |
| 50 | let already = (error?.takeRetainedValue()).map { CFErrorGetCode($0) == CTFontManagerError.alreadyRegistered.rawValue } ?? false |
| 51 | guard ok || already else { return false } |
| 52 | state.withLock { _ = $0.registered.insert(url.path) } |
| 53 | return true |
| 54 | } |
| 55 | |
| 56 | /// The PostScript name of a named instance: Timeless Sans's cuts and |
| 57 | /// weights, or DM Sans's. |
| 58 | static func postScriptName(grotesk: Bool, weight: BaseFont.Weight, face: Face, italic: Bool = false) -> String? { |
| 59 | switch face { |
| 60 | // Timeless Sans's italics are named instances on its ital axis |
| 61 | // (TimelessSans-SansRegularItalic and its siblings); none carries |
| 62 | // the italic trait, so SwiftUI's own italic never finds them. |
| 63 | case .timeless: return "TimelessSans-\(grotesk ? "Grotesk" : "Sans")\(weight.timeless)\(italic ? "Italic" : "")" |
| 64 | case .dmSans: return "DMSans-9ptRegular_\(weight.dmSans)" |
| 65 | case .system: return nil |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /// The type scale. Sizes are the web's in points; each scales with Dynamic |
| 71 | /// Type from its text style, as the system's own fonts do. |
| 72 | public enum BaseFont { |
| 73 | public enum Weight: Sendable, CaseIterable { |
| 74 | case regular, medium, semibold, bold |
| 75 | var timeless: String { |
| 76 | switch self { case .regular: "Regular"; case .medium: "Medium"; case .semibold: "Semibold"; case .bold: "Bold" } |
| 77 | } |
| 78 | var dmSans: String { |
| 79 | switch self { case .regular: "Regular"; case .medium: "Medium"; case .semibold: "SemiBold"; case .bold: "Bold" } |
| 80 | } |
| 81 | var system: Font.Weight { |
| 82 | switch self { case .regular: .regular; case .medium: .medium; case .semibold: .semibold; case .bold: .bold } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /// The Grotesk cut: headings and the wordmark. |
| 87 | public static func heading(_ size: CGFloat, _ weight: Weight = .semibold, relativeTo style: Font.TextStyle = .title3) -> Font { |
| 88 | font(size, weight, grotesk: true, style) |
| 89 | } |
| 90 | |
| 91 | /// The Sans cut: everything else. `italic`: Timeless Sans's own |
| 92 | /// italic instance; DM Sans, which has none bundled, and the system |
| 93 | /// face slant as SwiftUI's italic does. |
| 94 | public static func body(_ size: CGFloat, _ weight: Weight = .regular, italic: Bool = false, relativeTo style: Font.TextStyle = .body) -> Font { |
| 95 | font(size, weight, grotesk: false, style, italic: italic) |
| 96 | } |
| 97 | |
| 98 | static func font(_ size: CGFloat, _ weight: Weight, grotesk: Bool, _ style: Font.TextStyle, italic: Bool = false) -> Font { |
| 99 | let face = BaseFonts.face |
| 100 | guard let name = BaseFonts.postScriptName(grotesk: grotesk, weight: weight, face: face, italic: italic) else { |
| 101 | let system = Font.system(size: size, weight: weight.system) |
| 102 | return italic ? system.italic() : system |
| 103 | } |
| 104 | let font = Font.custom(name, size: size, relativeTo: style) |
| 105 | return italic && face != .timeless ? font.italic() : font |
| 106 | } |
| 107 | } |
| 108 | |