Add font infrastructure

This commit is contained in:
Guille Gonzalez
2025-12-31 17:22:20 +01:00
parent 5e8e93b53e
commit fbfc1d0ecf
9 changed files with 674 additions and 4 deletions

View File

@@ -0,0 +1,35 @@
@preconcurrency import CoreGraphics
@preconcurrency import CoreText
import Foundation
extension Math {
final class PlatformFont: Sendable {
let font: Font
let cgFont: CGFont
let ctFont: CTFont
let metrics: FontMetrics
init?(font: Font) {
guard
let cgFont = FontRegistry.shared.graphicsFont(named: font.name),
let ctFont = FontRegistry.shared.font(named: font.name, size: font.size),
let table = FontRegistry.shared.table(named: font.name)
else {
return nil
}
self.font = font
self.cgFont = cgFont
self.ctFont = ctFont
self.metrics = FontMetrics(
font: font,
unitsPerEm: UInt(CTFontGetUnitsPerEm(ctFont)),
table: table
)
}
func withSize(_ size: CGFloat) -> PlatformFont {
PlatformFont(font: .init(name: font.name, size: size))!
}
}
}