Skip to content

Commit 64b30a9

Browse files
javachefacebook-github-bot
authored andcommitted
Fix newarch ignoring defaultfonthandler (#53755)
Summary: Pull Request resolved: #53755 Changelog: [iOS][Fixed] Make `RCTSetDefaultFontHandler` compatible with the new arch, and add a more powerful version as `RCTSetDefaultFontResolver` Reviewed By: fkgozali Differential Revision: D82207676 fbshipit-source-id: eeeaf708491de9156ef4f1e045864e4322213902
1 parent 07da2ff commit 64b30a9

5 files changed

Lines changed: 66 additions & 10 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <UIKit/UIKit.h>
9+
10+
#import <React/RCTDefines.h>
11+
12+
NS_ASSUME_NONNULL_BEGIN
13+
14+
RCT_EXTERN UIFont *__nullable RCTGetLegacyDefaultFont(CGFloat fontSize, UIFontWeight fontWeight);
15+
16+
NS_ASSUME_NONNULL_END

packages/react-native/React/Views/RCTFont.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ typedef CGFloat RCTFontWeight;
1717
* provide a different base font, use this override. The font weight supplied to your
1818
* handler will be one of "ultralight", "thin", "light", "regular", "medium",
1919
* "semibold", "extrabold", "bold", "heavy", or "black".
20+
*
21+
* @deprecated Use RCTSetDefaultFontResolver
2022
*/
21-
RCT_EXTERN void RCTSetDefaultFontHandler(RCTFontHandler handler);
23+
RCT_EXTERN void RCTSetDefaultFontHandler(RCTFontHandler handler) __attribute__((deprecated));
2224
RCT_EXTERN BOOL RCTHasFontHandlerSet(void);
2325
RCT_EXTERN RCTFontWeight RCTGetFontWeight(UIFont *font);
2426

packages/react-native/React/Views/RCTFont.mm

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77

88
#import "RCTFont.h"
9+
#import "RCTFont+Private.h"
10+
911
#import "RCTAssert.h"
1012
#import "RCTLog.h"
1113

@@ -122,6 +124,15 @@ static inline BOOL CompareFontWeights(UIFontWeight firstWeight, UIFontWeight sec
122124
return @"regular";
123125
}
124126

127+
UIFont *RCTGetLegacyDefaultFont(CGFloat size, UIFontWeight fontWeight)
128+
{
129+
if (defaultFontHandler != nil) {
130+
return defaultFontHandler(size, FontWeightDescriptionFromUIFontWeight(fontWeight));
131+
} else {
132+
return nil;
133+
}
134+
}
135+
125136
static UIFont *cachedSystemFont(CGFloat size, RCTFontWeight weight)
126137
{
127138
static NSCache<NSValue *, UIFont *> *fontCache = [NSCache new];
@@ -135,8 +146,8 @@ struct __attribute__((__packed__)) CacheKey {
135146
NSValue *cacheKey = [[NSValue alloc] initWithBytes:&key objCType:@encode(CacheKey)];
136147
UIFont *font = [fontCache objectForKey:cacheKey];
137148

138-
if (!font) {
139-
if (defaultFontHandler) {
149+
if (font == nil) {
150+
if (defaultFontHandler != nil) {
140151
NSString *fontWeightDescription = FontWeightDescriptionFromUIFontWeight(weight);
141152
font = defaultFontHandler(size, fontWeightDescription);
142153
} else {

packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTFontUtils.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
#import <React/RCTDefines.h>
89
#import <UIKit/UIKit.h>
9-
1010
#import <react/renderer/textlayoutmanager/RCTFontProperties.h>
1111

1212
NS_ASSUME_NONNULL_BEGIN
1313

14+
using RCTDefaultFontResolver = UIFont *__nullable (^)(const RCTFontProperties &);
15+
16+
/**
17+
* React Native will use the System font for rendering by default. If you want to
18+
* provide a different base font, use this override.
19+
*/
20+
RCT_EXTERN void RCTSetDefaultFontResolver(RCTDefaultFontResolver handler);
21+
1422
/**
1523
* Returns UIFont instance corresponded to given font properties.
1624
*/
17-
UIFont *RCTFontWithFontProperties(RCTFontProperties fontProperties);
25+
RCT_EXTERN UIFont *RCTFontWithFontProperties(RCTFontProperties fontProperties);
1826

1927
NS_ASSUME_NONNULL_END

packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTFontUtils.mm

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
*/
77

88
#import "RCTFontUtils.h"
9+
910
#import <CoreText/CoreText.h>
11+
#import <React/RCTFont+Private.h>
1012
#import <React/RCTFont.h>
1113

1214
#import <algorithm>
@@ -246,7 +248,14 @@ static RCTFontStyle RCTGetFontStyle(UIFont *font)
246248
return fontFeatures;
247249
}
248250

249-
static UIFont *RCTDefaultFontWithFontProperties(RCTFontProperties fontProperties)
251+
static RCTDefaultFontResolver defaultFontResolver;
252+
253+
void RCTSetDefaultFontResolver(RCTDefaultFontResolver handler)
254+
{
255+
defaultFontResolver = handler;
256+
}
257+
258+
static UIFont *RCTDefaultFontWithFontProperties(const RCTFontProperties &fontProperties)
250259
{
251260
static NSCache *fontCache;
252261
static std::mutex fontCacheMutex;
@@ -261,14 +270,24 @@ static RCTFontStyle RCTGetFontStyle(UIFont *font)
261270

262271
{
263272
std::lock_guard<std::mutex> lock(fontCacheMutex);
264-
if (fontCache == nullptr) {
273+
if (fontCache == nil) {
265274
fontCache = [NSCache new];
266275
}
267276
font = [fontCache objectForKey:cacheKey];
268277
}
269278

270-
if (font == nullptr) {
271-
font = [UIFont systemFontOfSize:effectiveFontSize weight:fontProperties.weight];
279+
if (font == nil) {
280+
if (defaultFontResolver != nil) {
281+
font = defaultFontResolver(fontProperties);
282+
}
283+
284+
if (font == nil) {
285+
font = RCTGetLegacyDefaultFont(effectiveFontSize, fontProperties.weight);
286+
}
287+
288+
if (font == nil) {
289+
font = [UIFont systemFontOfSize:effectiveFontSize weight:fontProperties.weight];
290+
}
272291

273292
BOOL isItalicFont = fontProperties.style == RCTFontStyleItalic;
274293
BOOL isCondensedFont = [fontProperties.family isEqualToString:@"SystemCondensed"];
@@ -366,7 +385,7 @@ static UIFontDescriptorSystemDesign RCTGetFontDescriptorSystemDesign(NSString *f
366385
}
367386
}
368387

369-
if (font == nullptr) {
388+
if (font == nil) {
370389
// If we still don't have a match at least return the first font in the
371390
// fontFamily This is to support built-in font Zapfino and other custom
372391
// single font families like Impact

0 commit comments

Comments
 (0)