forked from 99ff00/react-native-dynamic-fonts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDynamicFonts.m
121 lines (93 loc) · 3.71 KB
/
DynamicFonts.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Copyright (c) 2017-present, Wyatt Greenway. All rights reserved.
*
* This source code is licensed under the MIT license found in the LICENSE file in the root
* directory of this source tree.
*/
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <CoreText/CoreText.h>
#import "DynamicFonts.h"
@implementation DynamicFonts
RCT_EXPORT_MODULE();
- (void) loadFontWithData:(CGDataProviderRef) fontDataProvider callback:(RCTResponseSenderBlock)callback
{
CGFontRef newFont = CGFontCreateWithDataProvider(fontDataProvider);
NSString *newFontName = (__bridge_transfer NSString *)CGFontCopyPostScriptName(newFont);
UIFont* font = [UIFont fontWithName:newFontName size:16];
if (font != nil) {
CGDataProviderRelease(fontDataProvider);
callback(@[[NSNull null], newFontName]);
return;
}
CFErrorRef error;
if (! CTFontManagerRegisterGraphicsFont(newFont, &error)) {
CFStringRef errorDescription = CFErrorCopyDescription(error);
NSLog(@"Failed to register font: %@", errorDescription);
callback(@[@"Failed to register font: %@", (__bridge_transfer NSString *)errorDescription]);
CFRelease(errorDescription);
CGFontRelease(newFont);
CGDataProviderRelease(fontDataProvider);
return;
}
CGFontRelease(newFont);
CGDataProviderRelease(fontDataProvider);
callback(@[[NSNull null], newFontName]);
}
RCT_EXPORT_METHOD(loadFont:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback)
{
NSString *name = [options valueForKey:@"name"];
NSString *data = [options valueForKey:@"data"];
NSString *type = NULL;
if ([name isEqual:[NSNull null]]) {
callback(@[@"Name property is empty"]);
return;
}
if ([data isEqual:[NSNull null]]) {
callback(@[@"Data property is empty"]);
return;
}
if ([[[data substringWithRange:NSMakeRange(0, 5)] lowercaseString] isEqualToString:@"data:"]) {
NSArray *parts = [data componentsSeparatedByString:@","];
NSString *mimeType = [parts objectAtIndex:0];
data = [parts objectAtIndex:1];
if (![mimeType isEqual:[NSNull null]]) {
mimeType = [[[mimeType substringFromIndex:5] componentsSeparatedByString:@";"] objectAtIndex:0];
if ([mimeType isEqualToString:@"application/x-font-ttf"] ||
[mimeType isEqualToString:@"application/x-font-truetype"] ||
[mimeType isEqualToString:@"font/ttf"]) {
type = @"ttf";
} else if ( [mimeType isEqualToString:@"application/x-font-opentype"] ||
[mimeType isEqualToString:@"font/opentype"]) {
type = @"otf";
}
}
}
if ([type isEqual:[NSNull null]])
type = [options valueForKey:@"type"];
if ([type isEqual:[NSNull null]])
type = @"ttf";
NSData *decodedData = [[NSData alloc]initWithBase64EncodedString:data options:NSDataBase64DecodingIgnoreUnknownCharacters];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)decodedData);
[self loadFontWithData:fontDataProvider callback:callback];
}
RCT_EXPORT_METHOD(loadFontFromFile:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback)
{
NSString *name = [options valueForKey:@"name"];
NSString *filePath = [options valueForKey:@"filePath"];
NSString *type = NULL;
if ([name isEqual:[NSNull null]]) {
callback(@[@"Name property is empty"]);
return;
}
if ([filePath isEqual:[NSNull null]]) {
callback(@[@"FilePath property is empty"]);
return;
}
if ([type isEqual:[NSNull null]])
type = [options valueForKey:@"type"];
if ([type isEqual:[NSNull null]]) type = @"ttf";
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename(filePath.fileSystemRepresentation);
[self loadFontWithData:fontDataProvider callback:callback];
}
@end