pi-remote-ios/Sources/UI/Terminal/TerminalFont.swift

67 lines
2.4 KiB
Swift

// TerminalFont.swift
// Represents a named monospace font that can be applied to the terminal view.
//
// T-2.3 note: JetBrains Mono font file is NOT bundled yet that is deferred
// to T-2.12. Until then, `jetBrainsMono` falls back to the system monospaced
// font so the struct is usable without crashing.
import UIKit
public struct TerminalFont: Identifiable, Sendable {
// MARK: Properties
public let id: String
public let displayName: String
/// The `UIFont` at whatever point size was requested when the static
/// preset was created. `FontStore` applies its own `size` override via
/// `uiFont.withSize(_:)` before passing the font to `TerminalView`.
public let uiFont: UIFont
// MARK: Init
public init(id: String, displayName: String, uiFont: UIFont) {
self.id = id
self.displayName = displayName
self.uiFont = uiFont
}
}
// MARK: - Static presets
public extension TerminalFont {
// MARK: JetBrains Mono
// Falls back to the system monospaced font when the font file is absent
// (i.e. before T-2.12 bundles it). Never crashes safe for all builds.
static let jetBrainsMono: TerminalFont = {
let baseSize: CGFloat = 13
// Attempt to load the named font; fall back to the system mono font.
let font = UIFont(name: "JetBrainsMono-Regular", size: baseSize)
?? UIFont.monospacedSystemFont(ofSize: baseSize, weight: .regular)
return TerminalFont(id: "jetbrains-mono", displayName: "JetBrains Mono", uiFont: font)
}()
// MARK: Menlo system monospaced, always available
static let menlo: TerminalFont = {
let baseSize: CGFloat = 13
// Menlo ships with iOS/macOS; fall back to the system mono if absent.
let font = UIFont(name: "Menlo-Regular", size: baseSize)
?? UIFont.monospacedSystemFont(ofSize: baseSize, weight: .regular)
return TerminalFont(id: "menlo", displayName: "Menlo", uiFont: font)
}()
// MARK: SF Mono always available on iOS 13+
static let sfMono: TerminalFont = {
let baseSize: CGFloat = 13
// "SFMono-Regular" is available as a named font on iOS 13+.
let font = UIFont(name: "SFMono-Regular", size: baseSize)
?? UIFont.monospacedSystemFont(ofSize: baseSize, weight: .regular)
return TerminalFont(id: "sf-mono", displayName: "SF Mono", uiFont: font)
}()
}