ソースを参照

optimize font loading

kevin 2 年 前
コミット
192bc83eb8
1 ファイル変更33 行追加25 行削除
  1. 33 25
      appbuilder-ios/NexilisLite/NexilisLite/Source/Nexilis.swift

+ 33 - 25
appbuilder-ios/NexilisLite/NexilisLite/Source/Nexilis.swift

@@ -1503,19 +1503,19 @@ extension UIFont {
     static var isOverrided: Bool = false
     static var isOverrided: Bool = false
     static let FONT_SELECT = 0
     static let FONT_SELECT = 0
 
 
-    @objc class func libSystemFont(ofSize size: CGFloat) -> UIFont {
+    @objc class func libSystemFont(ofSize size: CGFloat) -> UIFont? {
         jbs_registerFont(withFilenameString: LibFontName.regular)
         jbs_registerFont(withFilenameString: LibFontName.regular)
-        return UIFont(name: LibFontName.regular, size: size)!
+        return UIFont(name: LibFontName.regular, size: size)
     }
     }
 
 
-    @objc class func libBoldSystemFont(ofSize size: CGFloat) -> UIFont {
+    @objc class func libBoldSystemFont(ofSize size: CGFloat) -> UIFont? {
         jbs_registerFont(withFilenameString: LibFontName.bold)
         jbs_registerFont(withFilenameString: LibFontName.bold)
-        return UIFont(name: LibFontName.bold, size: size)!
+        return UIFont(name: LibFontName.bold, size: size)
     }
     }
 
 
-    @objc class func libItalicSystemFont(ofSize size: CGFloat) -> UIFont {
+    @objc class func libItalicSystemFont(ofSize size: CGFloat) -> UIFont? {
         jbs_registerFont(withFilenameString: LibFontName.italic)
         jbs_registerFont(withFilenameString: LibFontName.italic)
-        return UIFont(name: LibFontName.italic, size: size)!
+        return UIFont(name: LibFontName.italic, size: size)
     }
     }
 
 
     @objc convenience init(myCoder aDecoder: NSCoder) {
     @objc convenience init(myCoder aDecoder: NSCoder) {
@@ -1568,29 +1568,37 @@ extension UIFont {
     
     
     class func jbs_registerFont(withFilenameString filenameString: String) {
     class func jbs_registerFont(withFilenameString filenameString: String) {
 
 
-        guard let pathForResourceString = Bundle.resourceBundle(for: Nexilis.self).path(forResource: filenameString, ofType: "otf") else { //resourcesMediaBundle
+//        guard let pathForResourceString = Bundle.resourceBundle(for: Nexilis.self).path(forResource: filenameString, ofType: "otf") else { //resourcesMediaBundle
+//            //print("UIFont+:  Failed to register font - path for resource not found.")
+//            return
+//        }
+        
+        guard let pathForResourceURL = Bundle.resourceBundle(for: Nexilis.self).url(forResource: filenameString, withExtension: "otf") else { //resourcesMediaBundle
             //print("UIFont+:  Failed to register font - path for resource not found.")
             //print("UIFont+:  Failed to register font - path for resource not found.")
             return
             return
         }
         }
-
-        guard let fontData = NSData(contentsOfFile: pathForResourceString) else {
-            //print("UIFont+:  Failed to register font - font data could not be loaded.")
-            return
-        }
-
-        guard let dataProvider = CGDataProvider(data: fontData) else {
-            //print("UIFont+:  Failed to register font - data provider could not be loaded.")
-            return
-        }
-
-        guard let font = CGFont(dataProvider) else {
-            //print("UIFont+:  Failed to register font - font could not be loaded.")
-            return
-        }
-
+        
         var errorRef: Unmanaged<CFError>? = nil
         var errorRef: Unmanaged<CFError>? = nil
-        if (CTFontManagerRegisterGraphicsFont(font, &errorRef) == false) {
-        }
+        CTFontManagerRegisterFontsForURL(pathForResourceURL as CFURL, .process, &errorRef)
+
+//        guard let fontData = NSData(contentsOfFile: pathForResourceString) else {
+//            //print("UIFont+:  Failed to register font - font data could not be loaded.")
+//            return
+//        }
+//
+//        guard let dataProvider = CGDataProvider(data: fontData) else {
+//            //print("UIFont+:  Failed to register font - data provider could not be loaded.")
+//            return
+//        }
+//
+//        guard let font = CGFont(dataProvider) else {
+//            //print("UIFont+:  Failed to register font - font could not be loaded.")
+//            return
+//        }
+//
+//        var errorRef: Unmanaged<CFError>? = nil
+//        if (CTFontManagerRegisterGraphicsFont(font, &errorRef) == false) {
+//        }
     }
     }
 }
 }