Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,33 @@ static UIFontDescriptorSystemDesign RCTGetFontDescriptorSystemDesign(NSString *f
return systemDesigns[family];
}

// Picking a face instantiates every face in the family, so resolved fonts are cached.
static NSCache<NSString *, UIFont *> *RCTCustomFontCache(void)
{
static NSCache<NSString *, UIFont *> *cache;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
cache = [NSCache new];
[NSNotificationCenter.defaultCenter
addObserverForName:(NSNotificationName)kCTFontManagerRegisteredFontsChangedNotification
object:nil
queue:nil
usingBlock:^(NSNotification *) {
[cache removeAllObjects];
}];
});
return cache;
}

static NSString *RCTCustomFontCacheKey(const RCTFontProperties &fontProperties, CGFloat effectiveFontSize)
{
return [NSString stringWithFormat:@"%@/%.1f/%.2f/%ld",
fontProperties.family,
effectiveFontSize,
fontProperties.weight,
(long)fontProperties.style];
}

UIFont *RCTFontWithFontProperties(RCTFontProperties fontProperties)
{
RCTFontProperties defaultFontProperties = RCTDefaultFontProperties();
Expand All @@ -406,6 +433,9 @@ static UIFontDescriptorSystemDesign RCTGetFontDescriptorSystemDesign(NSString *f
// Handle system font as special case. This ensures that we preserve
// the specific metrics of the standard system font as closely as possible.
font = RCTDefaultFontWithFontProperties(fontProperties);
} else if (
(font = [RCTCustomFontCache() objectForKey:RCTCustomFontCacheKey(fontProperties, effectiveFontSize)]) != nil) {
// Resolved before; variants and variation settings are applied below.
} else {
NSArray<NSString *> *fontNames = [UIFont fontNamesForFamilyName:fontProperties.family];
UIFontWeight fontWeight = fontProperties.weight;
Expand Down Expand Up @@ -447,6 +477,10 @@ static UIFontDescriptorSystemDesign RCTGetFontDescriptorSystemDesign(NSString *f
font = [UIFont fontWithName:fontNames[0] size:effectiveFontSize];
}
}

if (font != nil) {
[RCTCustomFontCache() setObject:font forKey:RCTCustomFontCacheKey(fontProperties, effectiveFontSize)];
}
}

// Apply font variants to font object.
Expand Down
Loading