1// Copyright 2015-present 650 Industries. All rights reserved. 2 3#import <EXFont/EXFontLoaderProcessor.h> 4#import <EXFont/EXFontLoader.h> 5#import <EXFont/EXFont.h> 6#import <EXFont/EXFontManager.h> 7#import <objc/runtime.h> 8 9@interface EXFontLoaderProcessor () 10 11@property (nonatomic, copy) NSString *fontFamilyPrefix; 12@property (nonatomic, strong) EXFontManager *manager; 13 14@end 15 16@implementation EXFontLoaderProcessor 17 18- (instancetype)initWithFontFamilyPrefix:(NSString *)prefix 19 manager:(EXFontManager *)manager 20{ 21 if (self = [super init]) { 22 _fontFamilyPrefix = prefix; 23 _manager = manager; 24 } 25 return self; 26} 27 28- (instancetype)initWithManager:(EXFontManager *)manager 29{ 30 return [self initWithFontFamilyPrefix:nil manager:manager]; 31} 32 33- (UIFont *)updateFont:(UIFont *)uiFont 34 withFamily:(NSString *)family 35 size:(NSNumber *)size 36 weight:(NSString *)weight 37 style:(NSString *)style 38 variant:(NSArray<NSDictionary *> *)variant 39 scaleMultiplier:(CGFloat)scaleMultiplier 40{ 41 const CGFloat defaultFontSize = 14; 42 EXFont *exFont = nil; 43 44 // Did we get a new family, and if so, is it associated with an EXFont? 45 if (_fontFamilyPrefix && [family hasPrefix:_fontFamilyPrefix]) { 46 NSString *suffix = [family substringFromIndex:_fontFamilyPrefix.length]; 47 exFont = [_manager fontForName:suffix]; 48 } else if (!_fontFamilyPrefix) { 49 exFont = [_manager fontForName:family]; 50 } 51 52 // Did the passed-in UIFont come from an EXFont? 53 if (!exFont && uiFont) { 54 exFont = objc_getAssociatedObject(uiFont, EXFontAssocKey); 55 } 56 57 // If it's an EXFont, generate the corresponding UIFont, else fallback to React Native's built-in method 58 if (exFont) { 59 CGFloat computedSize = [size doubleValue] ?: uiFont.pointSize ?: defaultFontSize; 60 if (scaleMultiplier > 0.0 && scaleMultiplier != 1.0) { 61 computedSize = round(computedSize * scaleMultiplier); 62 } 63 return [exFont UIFontWithSize:computedSize]; 64 } 65 66 return nil; 67} 68 69@end 70