Skip to content

Commit

Permalink
[common] Load fonts ourselves at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
kirb committed Apr 19, 2020
1 parent f124b81 commit e5bcb70
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
5 changes: 4 additions & 1 deletion Common/Controllers/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ public enum KeyboardButtonStyle: Int {
override init() {
super.init()

// TODO: Preferences really shouldn’t be responsible for loading fonts!
FontMetrics.loadFonts()

let defaultFontName: String
if #available(iOS 13.0, macOS 10.15, *) {
defaultFontName = "SF Mono"
} else {
defaultFontName = "Fira Code"
defaultFontName = "Menlo"
}
let defaults: [String: Any] = [
"fontName": defaultFontName,
Expand Down
18 changes: 18 additions & 0 deletions Common/VT100/FontMetrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Foundation
import CoreGraphics
import CoreText

@objc public class FontMetrics: NSObject {
Expand All @@ -19,6 +20,23 @@ import CoreText

@objc public let boundingBox: CGSize

class func loadFonts() {
// Runtime load all fonts we’re interested in.
// TODO: This should only load the fonts the user wants.
guard let listing = try? FileManager.default.contentsOfDirectory(at: Bundle.main.resourceURL!, includingPropertiesForKeys: nil, options: [ .skipsSubdirectoryDescendants, .skipsHiddenFiles ]) else {
return
}
let fonts = listing.filter { item in item.pathExtension == "ttf" || item.pathExtension == "otf" }
if fonts.count > 0 {
var cfErrorsWrapper: Unmanaged<CFArray>? = nil
CTFontManagerRegisterFontsForURLs(fonts as CFArray, .process, &cfErrorsWrapper)
if let cfErrors = cfErrorsWrapper?.takeUnretainedValue(),
let errors = cfErrors as? [NSError] {
NSLog("%li error(s) loading fonts: %@", errors.count, errors)
}
}
}

init(regularFont: Font, boldFont: Font) {
self.regularFont = regularFont
self.boldFont = boldFont
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ SUBPROJECTS = prefs

include $(THEOS_MAKE_PATH)/aggregate.mk
endif

all package install::
# TODO: This should be possible natively in Theos!
+$(MAKE) -C Fonts $@ THEOS_PROJECT_DIR=$(THEOS_PROJECT_DIR)/Fonts
21 changes: 0 additions & 21 deletions iOS/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,6 @@
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIAppFonts</key>
<array>
<string>AnonymousPro-Bold.ttf</string>
<string>AnonymousPro-BoldItalic.ttf</string>
<string>AnonymousPro-Italic.ttf</string>
<string>AnonymousPro-Regular.ttf</string>
<string>FiraCode-Bold.otf</string>
<string>FiraCode-Retina.otf</string>
<string>Hack-Bold.ttf</string>
<string>Hack-Regular.ttf</string>
<string>JetBrainsMono-Bold.ttf</string>
<string>JetBrainsMono-BoldItalic.ttf</string>
<string>JetBrainsMono-Italic.ttf</string>
<string>JetBrainsMono-Regular.ttf</string>
<string>SourceCodePro-Bold.otf</string>
<string>SourceCodePro-Regular.otf</string>
<string>UbuntuMono-Bold.ttf</string>
<string>UbuntuMono-BoldItalic.ttf</string>
<string>UbuntuMono-Italic.ttf</string>
<string>UbuntuMono-Regular.ttf</string>
</array>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
Expand Down

0 comments on commit e5bcb70

Please sign in to comment.