Create Font struct to track custom fonts

This commit is contained in:
Gabriel Wong 2017-10-20 15:53:11 +07:00
parent 6fe3906319
commit 01fb7af7aa
5 changed files with 50 additions and 28 deletions

View file

@ -39,6 +39,7 @@
C040E5141F86108900901EE4 /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C040E5131F86108900901EE4 /* MainViewController.swift */; };
C04514881F85D7F500D88416 /* KeyboardViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04514871F85D7F500D88416 /* KeyboardViewController.swift */; };
C045148A1F85DF9100D88416 /* InputViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04514891F85DF9000D88416 /* InputViewController.swift */; };
C055E6EB1F99ED090035C2DD /* Font.swift in Sources */ = {isa = PBXBuildFile; fileRef = C055E6EA1F99ED090035C2DD /* Font.swift */; };
C06085B41F9485E40057E5B9 /* UIButton+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = C06085B31F9485E40057E5B9 /* UIButton+Helpers.swift */; };
C06D372F1F81F4E100F61AE0 /* KeymanEngine.h in Headers */ = {isa = PBXBuildFile; fileRef = C06D372D1F81F4E100F61AE0 /* KeymanEngine.h */; settings = {ATTRIBUTES = (Public, ); }; };
C06D37331F81F5C300F61AE0 /* Reachability.m in Sources */ = {isa = PBXBuildFile; fileRef = CECB38941F2199BC0098882F /* Reachability.m */; };
@ -158,6 +159,7 @@
C04514871F85D7F500D88416 /* KeyboardViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyboardViewController.swift; sourceTree = "<group>"; };
C04514891F85DF9000D88416 /* InputViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InputViewController.swift; sourceTree = "<group>"; };
C04C2A6C1F6B7D9A00BA42B6 /* LanguageViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LanguageViewController.swift; sourceTree = "<group>"; };
C055E6EA1F99ED090035C2DD /* Font.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Font.swift; sourceTree = "<group>"; };
C06085B31F9485E40057E5B9 /* UIButton+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIButton+Helpers.swift"; sourceTree = "<group>"; };
C06D372B1F81F4E100F61AE0 /* KeymanEngine.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = KeymanEngine.framework; sourceTree = BUILT_PRODUCTS_DIR; };
C06D372D1F81F4E100F61AE0 /* KeymanEngine.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KeymanEngine.h; sourceTree = "<group>"; };
@ -364,6 +366,14 @@
path = Extension;
sourceTree = "<group>";
};
C055E6E91F99EA740035C2DD /* Model */ = {
isa = PBXGroup;
children = (
C055E6EA1F99ED090035C2DD /* Font.swift */,
);
path = Model;
sourceTree = "<group>";
};
C06D372C1F81F4E100F61AE0 /* KeymanEngine */ = {
isa = PBXGroup;
children = (
@ -461,6 +471,7 @@
isa = PBXGroup;
children = (
C055E6E81F99EA320035C2DD /* Extension */,
C055E6E91F99EA740035C2DD /* Model */,
C0A93A531F8B21240079948B /* Manager.swift */,
CE25CCBB1F1DA72A005AA2BC /* HTTPRequest */,
987F00041AB8FCB700998116 /* KeyboardMenuView */,
@ -730,6 +741,7 @@
C0EF3E7B1F95B65300CE9BD4 /* KeymanWebViewDelegate.swift in Sources */,
C06D37411F81F5C400F61AE0 /* LanguageViewController.swift in Sources */,
C06D37421F81F5C400F61AE0 /* KeyboardPickerButton.swift in Sources */,
C055E6EB1F99ED090035C2DD /* Font.swift in Sources */,
C0324B931F87689B00AF3785 /* KeymanURLProtocol.swift in Sources */,
C06D37431F81F5C400F61AE0 /* KeyboardPickerBarButtonItem.swift in Sources */,
C082CE151F90AFD400860F02 /* Collection+SafeAccess.swift in Sources */,

View file

@ -32,8 +32,6 @@ public struct Key {
public static let fontFiles = "files"
// Font filename is deprecated
public static let fontFilename = "filename"
public static let fontName = "fontname"
public static let fontRegistered = "fontregistered"
public static let keyboardFilename = "filename"
public static let keyboardModified = "lastModified"
public static let keyboardRTL = "rtl"

View file

@ -107,8 +107,8 @@ UIGestureRecognizerDelegate {
/// The key format is $languageID_$keyboardID. For example, "eng_european2" returns the English EuroLatin2 keyboard
public private(set) var keyboardsDictionary: [String: [String: String]] = [:]
/// Dictionary of available Keyman keyboard fonts
public private(set) var keymanFonts: [String: [String: Any]] = [:]
/// Dictionary of available Keyman keyboard fonts keyed by font filename
public private(set) var keymanFonts: [String: Font] = [:]
/// Keyman system-wide keyboard
public let isSystemKeyboard: Bool
@ -788,7 +788,7 @@ UIGestureRecognizerDelegate {
if let kb = userKeyboards.first(where: { isCurrentKeyboard($0) }) {
let font = kb[Key.font]
if let fontFilename = self.fontFilename(fromJSONFont: font!) {
return keymanFonts[fontFilename]?[Key.fontName] as? String
return keymanFonts[fontFilename]?.name
}
return nil
}
@ -797,7 +797,7 @@ UIGestureRecognizerDelegate {
if let kb = keyboardsDictionary["\(languageID)_\(keyboardID)"] {
let font = kb[Key.font]
if let fontFilename = self.fontFilename(fromJSONFont: font!) {
return keymanFonts[fontFilename]?[Key.fontName] as? String
return keymanFonts[fontFilename]?.name
}
return nil
}
@ -816,7 +816,7 @@ UIGestureRecognizerDelegate {
if let kb = userKeyboards.first(where: { isCurrentKeyboard($0) }) {
if let font = kb[Key.oskFont] {
let fontFilename = self.fontFilename(fromJSONFont: font)
return keymanFonts[fontFilename!]?[Key.fontName] as? String
return keymanFonts[fontFilename!]?.name
}
}
}
@ -824,7 +824,7 @@ UIGestureRecognizerDelegate {
if let kb = keyboardsDictionary["\(languageID)_\(keyboardID)"] {
if let font = kb[Key.oskFont] {
let fontFilename = self.fontFilename(fromJSONFont: font)
return keymanFonts[fontFilename!]?[Key.fontName] as? String
return keymanFonts[fontFilename!]?.name
}
}
@ -1647,15 +1647,13 @@ UIGestureRecognizerDelegate {
for fontFilename in directoryContents where fontFilename.hasFontExtension {
if let fontInfo = keymanFonts[fontFilename] {
if fontInfo[Key.fontRegistered] as? Int == 0 {
if !fontInfo.isRegistered {
if let newFontInfo = registerFont(withFilename: fontFilename) {
keymanFonts[fontFilename] = newFontInfo
}
}
} else {
if let fontInfo = registerFont(withFilename: fontFilename) {
keymanFonts[fontFilename] = fontInfo
}
} else if let fontInfo = registerFont(withFilename: fontFilename) {
keymanFonts[fontFilename] = fontInfo
}
}
}
@ -1671,16 +1669,16 @@ UIGestureRecognizerDelegate {
}
for fontFilename in directoryContents where fontFilename.hasFontExtension {
if var fontInfo = keymanFonts[fontFilename], fontInfo[Key.fontRegistered] as? Int != 0 {
if var fontInfo = keymanFonts[fontFilename], fontInfo.isRegistered {
if unregisterFont(withFilename: fontFilename) {
fontInfo[Key.fontRegistered] = 0
fontInfo.isRegistered = false
keymanFonts[fontFilename] = fontInfo
}
}
}
}
private func registerFont(withFilename fontFilename: String) -> [String: Any]? {
private func registerFont(withFilename fontFilename: String) -> Font? {
guard let fontURL = activeFontDirectory()?.appendingPathComponent(fontFilename),
FileManager.default.fileExists(atPath: fontURL.path) else {
return nil
@ -1690,13 +1688,14 @@ UIGestureRecognizerDelegate {
kmLog("Failed to open \(fontURL)", checkDebugPrinting: false)
return nil
}
guard let font = CGFont(provider) else {
guard let font = CGFont(provider),
let cfFontName = font.postScriptName else {
kmLog("Failed to read font at \(fontURL)", checkDebugPrinting: false)
return nil
}
var didRegister: Bool = false
let fontName = font.postScriptName! as String
var didRegister = false
let fontName = cfFontName as String
if !fontExists(fontName) {
var errorRef: Unmanaged<CFError>?
didRegister = CTFontManagerRegisterFontsForURL(fontURL as CFURL, .none, &errorRef)
@ -1708,11 +1707,7 @@ UIGestureRecognizerDelegate {
kmLog("Registered font: \(fontURL)", checkDebugPrinting: true)
}
}
return [
Key.fontName: fontName,
// TODO: Check if didRegister should be true if font exists
Key.fontRegistered: didRegister ? 1 : 0
]
return Font(name: fontName, isRegistered: didRegister)
}
private func unregisterFont(withFilename fontFilename: String) -> Bool {

View file

@ -0,0 +1,17 @@
//
// Font.swift
// KeymanEngine
//
// Created by Gabriel Wong on 2017-10-20.
// Copyright © 2017 SIL International. All rights reserved.
//
public struct Font {
public let name: String
public var isRegistered: Bool
public init(name: String, isRegistered: Bool) {
self.name = name
self.isRegistered = isRegistered
}
}

View file

@ -125,10 +125,10 @@ class MainViewController: UIViewController, TextViewDelegate, UIActionSheetDeleg
// Check for configuration profiles/fonts to install
let kmFonts = Manager.shared.keymanFonts
var profilesByFontName = [String: String](minimumCapacity: kmFonts.count - 1)
for (key, value) in kmFonts where key != "keymanweb-osk.ttf" {
let fontName = value[Key.fontName] as! String
let type = key[key.range(of: ".", options: .backwards)!.lowerBound...]
profilesByFontName[fontName] = key.replacingOccurrences(of: type, with: ".mobileconfig")
for (filename, font) in kmFonts where filename != "keymanweb-osk.ttf" {
let fontName = font.name
let type = filename[filename.range(of: ".", options: .backwards)!.lowerBound...]
profilesByFontName[fontName] = filename.replacingOccurrences(of: type, with: ".mobileconfig")
}
let customFonts = UIFont.familyNames.filter { !systemFonts.contains($0) && !($0 == "KeymanwebOsk") }