From 89eb794751561e30e45002e35ce6d641a3a95da2 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Tue, 16 Apr 2024 08:53:17 -0700 Subject: [PATCH] Fabric: Add font variant support (#44112) Summary: Fixes https://github.com/facebook/react-native/issues/44064 ## Changelog: [IOS] [FIXED] - [iOS] Fabric: Add font variant support Pull Request resolved: https://github.com/facebook/react-native/pull/44112 Test Plan: Demo in https://github.com/facebook/react-native/issues/44064 . Reviewed By: cortinico Differential Revision: D56189145 Pulled By: cipolleschi fbshipit-source-id: a814cf4959b0160e36f79eaf6c7dcbffe5ef60c5 --- .../textlayoutmanager/RCTFontUtils.mm | 49 ++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTFontUtils.mm b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTFontUtils.mm index 4f8d5aa0703293..1ff8c1f231a533 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTFontUtils.mm +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTFontUtils.mm @@ -6,10 +6,12 @@ */ #import "RCTFontUtils.h" +#import #import #import #import +#import #import static RCTFontProperties RCTDefaultFontProperties() @@ -62,8 +64,51 @@ static RCTFontStyle RCTGetFontStyle(UIFont *font) static NSArray *RCTFontFeatures(RCTFontVariant fontVariant) { - // FIXME: - return @[]; + NSMutableArray *fontFeatures = [NSMutableArray array]; + static std::map mapping; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + mapping = { + {RCTFontVariantSmallCaps, @{ + UIFontFeatureTypeIdentifierKey : @(kLowerCaseType), + UIFontFeatureSelectorIdentifierKey : @(kLowerCaseSmallCapsSelector), + }}, + {RCTFontVariantOldstyleNums, @{ + UIFontFeatureTypeIdentifierKey : @(kNumberCaseType), + UIFontFeatureSelectorIdentifierKey : @(kLowerCaseNumbersSelector), + }}, + {RCTFontVariantLiningNums, @{ + UIFontFeatureTypeIdentifierKey : @(kNumberCaseType), + UIFontFeatureSelectorIdentifierKey : @(kUpperCaseNumbersSelector), + }}, + {RCTFontVariantTabularNums, @{ + UIFontFeatureTypeIdentifierKey : @(kNumberSpacingType), + UIFontFeatureSelectorIdentifierKey : @(kMonospacedNumbersSelector), + }}, + {RCTFontVariantProportionalNums, @{ + UIFontFeatureTypeIdentifierKey : @(kNumberSpacingType), + UIFontFeatureSelectorIdentifierKey : @(kProportionalNumbersSelector), + }}, + }; + }); + + if (fontVariant & RCTFontVariantSmallCaps) { + [fontFeatures addObject:mapping[RCTFontVariantSmallCaps]]; + } + if (fontVariant & RCTFontVariantOldstyleNums) { + [fontFeatures addObject:mapping[RCTFontVariantOldstyleNums]]; + } + if (fontVariant & RCTFontVariantLiningNums) { + [fontFeatures addObject:mapping[RCTFontVariantLiningNums]]; + } + if (fontVariant & RCTFontVariantTabularNums) { + [fontFeatures addObject:mapping[RCTFontVariantTabularNums]]; + } + if (fontVariant & RCTFontVariantProportionalNums) { + [fontFeatures addObject:mapping[RCTFontVariantProportionalNums]]; + } + + return fontFeatures; } static UIFont *RCTDefaultFontWithFontProperties(RCTFontProperties fontProperties)