LiveUI / Awesome

FontAwesome 6 Swift & SwiftUI implementation for iOS, tvOS & macOS
MIT License
97 stars 27 forks source link

Integration AwesomePro 5 : Crash #36

Closed Droppix closed 5 years ago

Droppix commented 5 years ago

Hi,

I try to integrated my fontawesome pro, but I have a crash : Error Code 105

  AwesomePro.loadFonts(from: Bundle.main, only: [.regular])
  let image = AwesomePro.Regular.user.asImage(size: 40) // Crash here

Your code in Fonts.swift

  if CTFontManagerRegisterGraphicsFont(font!, &error) == false {
            let errorDescription: CFString = CFErrorCopyDescription(error!.takeUnretainedValue())
            let nsError = error!.takeUnretainedValue() as AnyObject as! NSError
            NSException(name: NSExceptionName.internalInconsistencyException, reason: errorDescription as String, userInfo: [NSUnderlyingErrorKey: nsError]).raise()
        }

Any idea?

Thanks

Droppix commented 5 years ago

Here are the solutions tested with all PRO fonts

1/ I used the last version of FontAwesome Pro 5.71 2/ You have 2 problems in your file Fonts.swift

public extension AwesomePro {

    public enum Font: String, AwesomeFont {
        case brand = "fa-brands-400"
        case regular = "fa-regular-400"
        case solid = "fa-solid-900"
        case light = "fa-light-300"

        public var file: String {
            return rawValue
        }

        public var description: String {
            switch self {
                case .brand:
                    return "Font Awesome 5 Brands"
                case .regular:
                    return "Font Awesome 5 Pro"
                case .solid:
                    return "Font Awesome 5 Pro Solid"
                case .light:
                    return "Font Awesome 5 Pro Light"
            }
        }

        public var memberName: String {
            switch self {
                case .brand:
                    return "FontAwesome5Brands-Regular"
                case .regular:
                    return "FontAwesome5Pro-Regular"
                case .solid:
                    return "FontAwesome5Pro-Solid"
                case .light:
                    return "FontAwesome5Pro-Light"
            }
        }
    }
class Fonts {
         ...

        let data = try! Data(contentsOf: url as URL)
        let provider = CGDataProvider(data: data as CFData)
        let font = CGFont(provider!)

        let familyNames = Font.familyNames
        if familyNames.contains(where: { $0 == type.description }) == false {

            var error: Unmanaged<CFError>?

            if CTFontManagerRegisterGraphicsFont(font!, &error) == false {
                let errorDescription: CFString = CFErrorCopyDescription(error!.takeUnretainedValue())
                let nsError = error!.takeUnretainedValue() as AnyObject as! NSError
                NSException(name: NSExceptionName.internalInconsistencyException, reason: errorDescription as String, userInfo: [NSUnderlyingErrorKey: nsError]).raise()
            }
        }
}
        AwesomePro.loadFonts(from: Bundle.main, only: [.regular, .light, .solid, .brand])

        let temp1 = AwesomePro.Brand.adobe.asImage(size: 40)
        let temp2 = AwesomePro.Light.user.asImage(size: 40)
        let temp3 = AwesomePro.Regular.user.asImage(size: 40)
        let temp4 = AwesomePro.Solid.abacus.asImage(size: 40)
ghowen commented 5 years ago

If I am not mistaken, AwesomeEnum currently has not updated to FA 5.7.x yet. This could also be the reason for the crash.

Droppix commented 5 years ago

The crash is not due to the version of "FA 5.7.x", but the fact that you don't check if the font is already registered. (see CTFontManagerRegisterGraphicsFont)

ghowen commented 5 years ago

@Droppix Thanks! I can ACK the problem now and will look into your solution. I think we do check if the font is loaded with the guard statement here, but I need to investigate if this is effective.

class Fonts {

    static func load(type: AwesomeFont, from bundle: Bundle? = nil) {
        guard !Font.fontNames(forFamilyName: type.description).contains(type.memberName) else {
            return
        }

But there seem to be some changes in the font descriptions and member names in 5.7.x

ghowen commented 5 years ago

Fixed with https://github.com/LiveUI/Awesome/pull/39. Only renaming the member names was necessary (free and pro fonts).

ghowen commented 5 years ago

2.1.0 was just released in Cocoapods. This should fix the issue. Make sure to clean your build folder after updating the pods and properly add the FA5Pro 5.7.x fonts to your project and target as otherwise your app might still crash.