|
| 1 | +// swiftlint:disable all |
| 2 | +// Generated using SwiftGen — https://github.com/SwiftGen/SwiftGen |
| 3 | + |
| 4 | +#if os(macOS) |
| 5 | + import AppKit.NSFont |
| 6 | +#elseif os(iOS) || os(tvOS) || os(watchOS) |
| 7 | + import UIKit.UIFont |
| 8 | +#endif |
| 9 | +#if canImport(SwiftUI) |
| 10 | + import SwiftUI |
| 11 | +#endif |
| 12 | + |
| 13 | +// Deprecated typealiases |
| 14 | +@available(*, deprecated, renamed: "FontConvertible.Font", message: "This typealias will be removed in SwiftGen 7.0") |
| 15 | +internal typealias Font = FontConvertible.Font |
| 16 | + |
| 17 | +// swiftlint:disable superfluous_disable_command file_length implicit_return |
| 18 | + |
| 19 | +// MARK: - Fonts |
| 20 | + |
| 21 | +// swiftlint:disable identifier_name line_length type_body_length |
| 22 | +internal enum FontFamily { |
| 23 | + internal enum FONTSPRINGDEMOProximaNova { |
| 24 | + internal static let bold = FontConvertible(name: "FONTSPRINGDEMO-ProximaNovaBold", family: "FONTSPRING DEMO - Proxima Nova", path: "proximanova_bold.otf") |
| 25 | + internal static let regular = FontConvertible(name: "FONTSPRINGDEMO-ProximaNovaRegular", family: "FONTSPRING DEMO - Proxima Nova", path: "proximanova_regular.otf") |
| 26 | + internal static let all: [FontConvertible] = [bold, regular] |
| 27 | + } |
| 28 | + internal enum FONTSPRINGDEMOProximaNovaLight { |
| 29 | + internal static let regular = FontConvertible(name: "FONTSPRINGDEMO-ProximaNovaLightRegular", family: "FONTSPRING DEMO - Proxima Nova Light", path: "proximanova_light.otf") |
| 30 | + internal static let all: [FontConvertible] = [regular] |
| 31 | + } |
| 32 | + internal enum FONTSPRINGDEMOProximaNovaSemibold { |
| 33 | + internal static let regular = FontConvertible(name: "FONTSPRINGDEMO-ProximaNovaSemiboldRegular", family: "FONTSPRING DEMO - Proxima Nova Semibold", path: "proximanova_semibold.otf") |
| 34 | + internal static let all: [FontConvertible] = [regular] |
| 35 | + } |
| 36 | + internal enum Lato { |
| 37 | + internal static let bold = FontConvertible(name: "Lato-Bold", family: "Lato", path: "Lato-Bold.ttf") |
| 38 | + internal static let medium = FontConvertible(name: "Lato-Medium", family: "Lato", path: "Lato-Medium.ttf") |
| 39 | + internal static let regular = FontConvertible(name: "Lato-Regular", family: "Lato", path: "Lato-Regular.ttf") |
| 40 | + internal static let all: [FontConvertible] = [bold, medium, regular] |
| 41 | + } |
| 42 | + internal static let allCustomFonts: [FontConvertible] = [FONTSPRINGDEMOProximaNova.all, FONTSPRINGDEMOProximaNovaLight.all, FONTSPRINGDEMOProximaNovaSemibold.all, Lato.all].flatMap { $0 } |
| 43 | + internal static func registerAllCustomFonts() { |
| 44 | + allCustomFonts.forEach { $0.register() } |
| 45 | + } |
| 46 | +} |
| 47 | +// swiftlint:enable identifier_name line_length type_body_length |
| 48 | + |
| 49 | +// MARK: - Implementation Details |
| 50 | + |
| 51 | +internal struct FontConvertible { |
| 52 | + internal let name: String |
| 53 | + internal let family: String |
| 54 | + internal let path: String |
| 55 | + |
| 56 | + #if os(macOS) |
| 57 | + internal typealias Font = NSFont |
| 58 | + #elseif os(iOS) || os(tvOS) || os(watchOS) |
| 59 | + internal typealias Font = UIFont |
| 60 | + #endif |
| 61 | + |
| 62 | + internal func font(size: CGFloat) -> Font { |
| 63 | + guard let font = Font(font: self, size: size) else { |
| 64 | + fatalError("Unable to initialize font '\(name)' (\(family))") |
| 65 | + } |
| 66 | + return font |
| 67 | + } |
| 68 | + |
| 69 | + #if canImport(SwiftUI) |
| 70 | + @available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *) |
| 71 | + internal func swiftUIFont(size: CGFloat) -> SwiftUI.Font { |
| 72 | + return SwiftUI.Font.custom(self, size: size) |
| 73 | + } |
| 74 | + |
| 75 | + @available(iOS 14.0, tvOS 14.0, watchOS 7.0, macOS 11.0, *) |
| 76 | + internal func swiftUIFont(fixedSize: CGFloat) -> SwiftUI.Font { |
| 77 | + return SwiftUI.Font.custom(self, fixedSize: fixedSize) |
| 78 | + } |
| 79 | + |
| 80 | + @available(iOS 14.0, tvOS 14.0, watchOS 7.0, macOS 11.0, *) |
| 81 | + internal func swiftUIFont(size: CGFloat, relativeTo textStyle: SwiftUI.Font.TextStyle) -> SwiftUI.Font { |
| 82 | + return SwiftUI.Font.custom(self, size: size, relativeTo: textStyle) |
| 83 | + } |
| 84 | + #endif |
| 85 | + |
| 86 | + internal func register() { |
| 87 | + // swiftlint:disable:next conditional_returns_on_newline |
| 88 | + guard let url = url else { return } |
| 89 | + CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil) |
| 90 | + } |
| 91 | + |
| 92 | + fileprivate func registerIfNeeded() { |
| 93 | + #if os(iOS) || os(tvOS) || os(watchOS) |
| 94 | + if !UIFont.fontNames(forFamilyName: family).contains(name) { |
| 95 | + register() |
| 96 | + } |
| 97 | + #elseif os(macOS) |
| 98 | + if let url = url, CTFontManagerGetScopeForURL(url as CFURL) == .none { |
| 99 | + register() |
| 100 | + } |
| 101 | + #endif |
| 102 | + } |
| 103 | + |
| 104 | + fileprivate var url: URL? { |
| 105 | + // swiftlint:disable:next implicit_return |
| 106 | + return BundleToken.bundle.url(forResource: path, withExtension: nil) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +internal extension FontConvertible.Font { |
| 111 | + convenience init?(font: FontConvertible, size: CGFloat) { |
| 112 | + font.registerIfNeeded() |
| 113 | + self.init(name: font.name, size: size) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +#if canImport(SwiftUI) |
| 118 | +@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *) |
| 119 | +internal extension SwiftUI.Font { |
| 120 | + static func custom(_ font: FontConvertible, size: CGFloat) -> SwiftUI.Font { |
| 121 | + font.registerIfNeeded() |
| 122 | + return custom(font.name, size: size) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +@available(iOS 14.0, tvOS 14.0, watchOS 7.0, macOS 11.0, *) |
| 127 | +internal extension SwiftUI.Font { |
| 128 | + static func custom(_ font: FontConvertible, fixedSize: CGFloat) -> SwiftUI.Font { |
| 129 | + font.registerIfNeeded() |
| 130 | + return custom(font.name, fixedSize: fixedSize) |
| 131 | + } |
| 132 | + |
| 133 | + static func custom( |
| 134 | + _ font: FontConvertible, |
| 135 | + size: CGFloat, |
| 136 | + relativeTo textStyle: SwiftUI.Font.TextStyle |
| 137 | + ) -> SwiftUI.Font { |
| 138 | + font.registerIfNeeded() |
| 139 | + return custom(font.name, size: size, relativeTo: textStyle) |
| 140 | + } |
| 141 | +} |
| 142 | +#endif |
| 143 | + |
| 144 | +// swiftlint:disable convenience_type |
| 145 | +private final class BundleToken { |
| 146 | + static let bundle: Bundle = { |
| 147 | + #if SWIFT_PACKAGE |
| 148 | + return Bundle.module |
| 149 | + #else |
| 150 | + return Bundle(for: BundleToken.self) |
| 151 | + #endif |
| 152 | + }() |
| 153 | +} |
| 154 | +// swiftlint:enable convenience_type |
0 commit comments