Turn on strict memory safety for the macOS app

This commit is contained in:
Ian Gulliver
2026-10-17 09:57:28 -07:00
parent 3541abb1b1
commit 7452bd5936
9 changed files with 33 additions and 32 deletions
+12 -15
View File
@@ -1,4 +1,5 @@
import CoreText
import Synchronization
import SwiftUI
enum Fonts {
@@ -14,7 +15,7 @@ enum Fonts {
}()
private static func file(_ subset: String, _ italic: Bool, _ weight: CGFloat) -> CTFontDescriptor {
let d = descriptors["atkinson-hyperlegible-mono-\(subset)-wght-\(italic ? "italic" : "normal").woff2"]!
let d = unsafe descriptors["atkinson-hyperlegible-mono-\(subset)-wght-\(italic ? "italic" : "normal").woff2"]!
return CTFontDescriptorCreateCopyWithVariation(d, 0x7767_6874 as CFNumber, weight)
}
@@ -24,29 +25,25 @@ enum Fonts {
return CTFontCreateWithFontDescriptor(d, size, nil)
}
nonisolated(unsafe) private static var fonts: [String: (font: Font, ns: NSFont)] = [:]
private static let lock = NSLock()
private static let fonts = Mutex<[String: CTFont]>([:])
private static func cached(_ size: CGFloat, _ weight: CGFloat, _ italic: Bool) -> (font: Font, ns: NSFont) {
private static func cached(_ size: CGFloat, _ weight: CGFloat, _ italic: Bool) -> CTFont {
let key = "\(size) \(weight) \(italic)"
lock.lock()
defer {
lock.unlock()
}
if let f = fonts[key] {
return fonts.withLock { fonts in
if let f = fonts[key] {
return f
}
let f = ct(size, weight: weight, italic: italic)
fonts[key] = f
return f
}
let c = ct(size, weight: weight, italic: italic)
let f = (Font(c), c as NSFont)
fonts[key] = f
return f
}
static func font(_ size: CGFloat, weight: CGFloat = 400, italic: Bool = false) -> Font {
cached(size, weight, italic).font
Font(cached(size, weight, italic))
}
static func ns(_ size: CGFloat, weight: CGFloat = 400, italic: Bool = false) -> NSFont {
cached(size, weight, italic).ns
cached(size, weight, italic) as NSFont
}
}