import CoreText import Foundation import SwiftUI import UIKit struct DisplayPreferences { var hideAvatars = false var handlesOnly = false func name(_ author: Post.Author) -> String { name(displayName: author.name, handle: author.handle) } func name(displayName: String, handle: String) -> String { handlesOnly ? "@\(handle)" : displayName } } enum AppFont: String, CaseIterable, Identifiable { case system case atkinson case iosevka case paperMono var id: String { rawValue } var title: String { switch self { case .system: "System" case .atkinson: "Atkinson Hyperlegible Next" case .iosevka: "Iosevka" case .paperMono: "Paper Mono" } } var fontName: String? { switch self { case .system: nil case .atkinson: "AtkinsonHyperlegibleNext-Regular" case .iosevka: "Iosevka" case .paperMono: "PaperMono-Regular" } } static func registerBundledFonts() { guard let urls = Bundle.main.urls(forResourcesWithExtension: "ttf", subdirectory: "Fonts") else { return } for url in urls { CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil) } } func font(_ style: Font.TextStyle, weight: Font.Weight? = nil) -> Font { guard let fontName else { let font = Font.system(style) return weight.map { font.weight($0) } ?? font } let size = UIFont.preferredFont( forTextStyle: style.uiStyle, compatibleWith: UITraitCollection(preferredContentSizeCategory: .large) ).pointSize let font = Font.custom(fontName, size: size, relativeTo: style) if let weight { return font.weight(weight) } return style == .headline ? font.weight(.semibold) : font } func fixedFont(size: CGFloat) -> Font { guard let fontName else { return .system(size: size) } return .custom(fontName, fixedSize: size) } func uiFont(for style: UIFont.TextStyle, traits: UITraitCollection) -> UIFont { let system = UIFont.preferredFont(forTextStyle: style, compatibleWith: traits) guard let fontName, let custom = UIFont(name: fontName, size: system.pointSize) else { return system } return custom } } extension Font.TextStyle { fileprivate var uiStyle: UIFont.TextStyle { switch self { case .largeTitle: .largeTitle case .title: .title1 case .title2: .title2 case .title3: .title3 case .headline: .headline case .subheadline: .subheadline case .callout: .callout case .footnote: .footnote case .caption: .caption1 case .caption2: .caption2 default: .body } } } private struct AppDefaultFontModifier: ViewModifier { @AppStorage("appFont") private var selection = AppFont.system.rawValue func body(content: Content) -> some View { let font = AppFont(rawValue: selection) ?? .system content.font(font == .system ? nil : font.font(.body)) } } private struct AppFontModifier: ViewModifier { @AppStorage("appFont") private var selection = AppFont.system.rawValue let style: Font.TextStyle var weight: Font.Weight? func body(content: Content) -> some View { content.font((AppFont(rawValue: selection) ?? .system).font(style, weight: weight)) } } extension View { @ViewBuilder func appFontIfSet(_ style: Font.TextStyle?) -> some View { if let style { appFont(style) } else { self } } func appDefaultFont() -> some View { modifier(AppDefaultFontModifier()) } func appFont(_ style: Font.TextStyle, weight: Font.Weight? = nil) -> some View { modifier(AppFontModifier(style: style, weight: weight)) } }