diff --git a/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm b/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm index 3d6b44e5d5b..937edfd35ed 100644 --- a/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm +++ b/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm @@ -18,21 +18,25 @@ using namespace facebook::react; std::weak_ptr _weakEventEmitter; } -- (void)setEventEmitter:(SharedEventEmitter)eventEmitter { +- (void)setEventEmitter:(SharedEventEmitter)eventEmitter +{ _weakEventEmitter = eventEmitter; } -- (SharedEventEmitter)eventEmitter { +- (SharedEventEmitter)eventEmitter +{ return _weakEventEmitter.lock(); } -- (void)dealloc { +- (void)dealloc +{ _weakEventEmitter.reset(); } @end -inline static UIFontWeight RCTUIFontWeightFromInteger(NSInteger fontWeight) { +inline static UIFontWeight RCTUIFontWeightFromInteger(NSInteger fontWeight) +{ assert(fontWeight > 50); assert(fontWeight < 950); @@ -49,11 +53,9 @@ inline static UIFontWeight RCTUIFontWeightFromInteger(NSInteger fontWeight) { return weights[(fontWeight + 50) / 100 - 1]; } -inline static UIFont *RCTEffectiveFontFromTextAttributes( - const TextAttributes &textAttributes) { - NSString *fontFamily = - [NSString stringWithCString:textAttributes.fontFamily.c_str() - encoding:NSUTF8StringEncoding]; +inline static UIFont *RCTEffectiveFontFromTextAttributes(const TextAttributes &textAttributes) +{ + NSString *fontFamily = [NSString stringWithCString:textAttributes.fontFamily.c_str() encoding:NSUTF8StringEncoding]; RCTFontProperties fontProperties; fontProperties.family = fontFamily; @@ -72,49 +74,41 @@ inline static UIFont *RCTEffectiveFontFromTextAttributes( return RCTFontWithFontProperties(fontProperties); } -inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes( - const TextAttributes &textAttributes) { - return textAttributes.allowFontScaling.value_or(true) && - !isnan(textAttributes.fontSizeMultiplier) +inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes(const TextAttributes &textAttributes) +{ + return textAttributes.allowFontScaling.value_or(true) && !isnan(textAttributes.fontSizeMultiplier) ? textAttributes.fontSizeMultiplier : 1.0; } -inline static UIColor *RCTEffectiveForegroundColorFromTextAttributes( - const TextAttributes &textAttributes) { - UIColor *effectiveForegroundColor = - RCTUIColorFromSharedColor(textAttributes.foregroundColor) - ?: [UIColor blackColor]; +inline static UIColor *RCTEffectiveForegroundColorFromTextAttributes(const TextAttributes &textAttributes) +{ + UIColor *effectiveForegroundColor = RCTUIColorFromSharedColor(textAttributes.foregroundColor) ?: [UIColor blackColor]; if (!isnan(textAttributes.opacity)) { effectiveForegroundColor = [effectiveForegroundColor - colorWithAlphaComponent:CGColorGetAlpha( - effectiveForegroundColor.CGColor) * - textAttributes.opacity]; + colorWithAlphaComponent:CGColorGetAlpha(effectiveForegroundColor.CGColor) * textAttributes.opacity]; } return effectiveForegroundColor; } -inline static UIColor *RCTEffectiveBackgroundColorFromTextAttributes( - const TextAttributes &textAttributes) { - UIColor *effectiveBackgroundColor = - RCTUIColorFromSharedColor(textAttributes.backgroundColor); +inline static UIColor *RCTEffectiveBackgroundColorFromTextAttributes(const TextAttributes &textAttributes) +{ + UIColor *effectiveBackgroundColor = RCTUIColorFromSharedColor(textAttributes.backgroundColor); if (effectiveBackgroundColor && !isnan(textAttributes.opacity)) { effectiveBackgroundColor = [effectiveBackgroundColor - colorWithAlphaComponent:CGColorGetAlpha( - effectiveBackgroundColor.CGColor) * - textAttributes.opacity]; + colorWithAlphaComponent:CGColorGetAlpha(effectiveBackgroundColor.CGColor) * textAttributes.opacity]; } return effectiveBackgroundColor ?: [UIColor clearColor]; } -static NSDictionary * -RCTNSTextAttributesFromTextAttributes(const TextAttributes &textAttributes) { - NSMutableDictionary *attributes = - [NSMutableDictionary dictionaryWithCapacity:10]; +static NSDictionary *RCTNSTextAttributesFromTextAttributes( + const TextAttributes &textAttributes) +{ + NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithCapacity:10]; // Font UIFont *font = RCTEffectiveFontFromTextAttributes(textAttributes); @@ -123,16 +117,14 @@ RCTNSTextAttributesFromTextAttributes(const TextAttributes &textAttributes) { } // Colors - UIColor *effectiveForegroundColor = - RCTEffectiveForegroundColorFromTextAttributes(textAttributes); + UIColor *effectiveForegroundColor = RCTEffectiveForegroundColorFromTextAttributes(textAttributes); if (textAttributes.foregroundColor || !isnan(textAttributes.opacity)) { attributes[NSForegroundColorAttributeName] = effectiveForegroundColor; } if (textAttributes.backgroundColor || !isnan(textAttributes.opacity)) { - attributes[NSBackgroundColorAttributeName] = - RCTEffectiveBackgroundColorFromTextAttributes(textAttributes); + attributes[NSBackgroundColorAttributeName] = RCTEffectiveBackgroundColorFromTextAttributes(textAttributes); } // Kerning @@ -144,10 +136,8 @@ RCTNSTextAttributesFromTextAttributes(const TextAttributes &textAttributes) { NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new]; BOOL isParagraphStyleUsed = NO; if (textAttributes.alignment.hasValue()) { - TextAlignment textAlignment = - textAttributes.alignment.value_or(TextAlignment::Natural); - if (textAttributes.layoutDirection.value_or(LayoutDirection::LeftToRight) == - LayoutDirection::RightToLeft) { + TextAlignment textAlignment = textAttributes.alignment.value_or(TextAlignment::Natural); + if (textAttributes.layoutDirection.value_or(LayoutDirection::LeftToRight) == LayoutDirection::RightToLeft) { if (textAlignment == TextAlignment::Right) { textAlignment = TextAlignment::Left; } else if (textAlignment == TextAlignment::Left) { @@ -155,21 +145,18 @@ RCTNSTextAttributesFromTextAttributes(const TextAttributes &textAttributes) { } } - paragraphStyle.alignment = - RCTNSTextAlignmentFromTextAlignment(textAlignment); + paragraphStyle.alignment = RCTNSTextAlignmentFromTextAlignment(textAlignment); isParagraphStyleUsed = YES; } if (textAttributes.baseWritingDirection.hasValue()) { paragraphStyle.baseWritingDirection = - RCTNSWritingDirectionFromWritingDirection( - textAttributes.baseWritingDirection.value()); + RCTNSWritingDirectionFromWritingDirection(textAttributes.baseWritingDirection.value()); isParagraphStyleUsed = YES; } if (!isnan(textAttributes.lineHeight)) { - CGFloat lineHeight = textAttributes.lineHeight * - RCTEffectiveFontSizeMultiplierFromTextAttributes(textAttributes); + CGFloat lineHeight = textAttributes.lineHeight * RCTEffectiveFontSizeMultiplierFromTextAttributes(textAttributes); paragraphStyle.minimumLineHeight = lineHeight; paragraphStyle.maximumLineHeight = lineHeight; isParagraphStyleUsed = YES; @@ -180,23 +167,18 @@ RCTNSTextAttributesFromTextAttributes(const TextAttributes &textAttributes) { } // Decoration - if (textAttributes.textDecorationLineType.value_or( - TextDecorationLineType::None) != TextDecorationLineType::None) { + if (textAttributes.textDecorationLineType.value_or(TextDecorationLineType::None) != TextDecorationLineType::None) { auto textDecorationLineType = textAttributes.textDecorationLineType.value(); NSUnderlineStyle style = RCTNSUnderlineStyleFromStyleAndPattern( - textAttributes.textDecorationLineStyle.value_or( - TextDecorationLineStyle::Single), - textAttributes.textDecorationLinePattern.value_or( - TextDecorationLinePattern::Solid)); + textAttributes.textDecorationLineStyle.value_or(TextDecorationLineStyle::Single), + textAttributes.textDecorationLinePattern.value_or(TextDecorationLinePattern::Solid)); - UIColor *textDecorationColor = - RCTUIColorFromSharedColor(textAttributes.textDecorationColor); + UIColor *textDecorationColor = RCTUIColorFromSharedColor(textAttributes.textDecorationColor); // Underline if (textDecorationLineType == TextDecorationLineType::Underline || - textDecorationLineType == - TextDecorationLineType::UnderlineStrikethrough) { + textDecorationLineType == TextDecorationLineType::UnderlineStrikethrough) { attributes[NSUnderlineStyleAttributeName] = @(style); if (textDecorationColor) { @@ -206,8 +188,7 @@ RCTNSTextAttributesFromTextAttributes(const TextAttributes &textAttributes) { // Strikethrough if (textDecorationLineType == TextDecorationLineType::Strikethrough || - textDecorationLineType == - TextDecorationLineType::UnderlineStrikethrough) { + textDecorationLineType == TextDecorationLineType::UnderlineStrikethrough) { attributes[NSStrikethroughStyleAttributeName] = @(style); if (textDecorationColor) { @@ -220,11 +201,9 @@ RCTNSTextAttributesFromTextAttributes(const TextAttributes &textAttributes) { if (textAttributes.textShadowOffset.hasValue()) { auto textShadowOffset = textAttributes.textShadowOffset.value(); NSShadow *shadow = [NSShadow new]; - shadow.shadowOffset = - CGSize{textShadowOffset.width, textShadowOffset.height}; + shadow.shadowOffset = CGSize{textShadowOffset.width, textShadowOffset.height}; shadow.shadowBlurRadius = textAttributes.textShadowRadius; - shadow.shadowColor = - RCTUIColorFromSharedColor(textAttributes.textShadowColor); + shadow.shadowColor = RCTUIColorFromSharedColor(textAttributes.textShadowColor); attributes[NSShadowAttributeName] = shadow; } @@ -236,10 +215,9 @@ RCTNSTextAttributesFromTextAttributes(const TextAttributes &textAttributes) { return [attributes copy]; } -NSAttributedString *RCTNSAttributedStringFromAttributedString( - const AttributedString &attributedString) { - NSMutableAttributedString *nsAttributedString = - [[NSMutableAttributedString alloc] init]; +NSAttributedString *RCTNSAttributedStringFromAttributedString(const AttributedString &attributedString) +{ + NSMutableAttributedString *nsAttributedString = [[NSMutableAttributedString alloc] init]; [nsAttributedString beginEditing]; @@ -249,46 +227,36 @@ NSAttributedString *RCTNSAttributedStringFromAttributedString( auto layoutMetrics = fragment.shadowView.layoutMetrics; if (layoutMetrics != EmptyLayoutMetrics) { - CGRect bounds = {.origin = {.x = layoutMetrics.frame.origin.x, - .y = layoutMetrics.frame.origin.y}, - .size = {.width = layoutMetrics.frame.size.width, - .height = layoutMetrics.frame.size.height}}; + CGRect bounds = {.origin = {.x = layoutMetrics.frame.origin.x, .y = layoutMetrics.frame.origin.y}, + .size = {.width = layoutMetrics.frame.size.width, .height = layoutMetrics.frame.size.height}}; NSTextAttachment *attachment = [NSTextAttachment new]; attachment.bounds = bounds; - nsAttributedStringFragment = - [NSAttributedString attributedStringWithAttachment:attachment]; + nsAttributedStringFragment = [NSAttributedString attributedStringWithAttachment:attachment]; } else { - NSString *string = [NSString stringWithCString:fragment.string.c_str() - encoding:NSUTF8StringEncoding]; + NSString *string = [NSString stringWithCString:fragment.string.c_str() encoding:NSUTF8StringEncoding]; - nsAttributedStringFragment = [[NSAttributedString alloc] - initWithString:string - attributes:RCTNSTextAttributesFromTextAttributes( - fragment.textAttributes)]; + nsAttributedStringFragment = + [[NSAttributedString alloc] initWithString:string + attributes:RCTNSTextAttributesFromTextAttributes(fragment.textAttributes)]; } NSMutableAttributedString *nsMutableAttributedStringFragment = - [[NSMutableAttributedString alloc] - initWithAttributedString:nsAttributedStringFragment]; + [[NSMutableAttributedString alloc] initWithAttributedString:nsAttributedStringFragment]; if (fragment.parentShadowView.componentHandle) { - RCTWeakEventEmitterWrapper *eventEmitterWrapper = - [RCTWeakEventEmitterWrapper new]; + RCTWeakEventEmitterWrapper *eventEmitterWrapper = [RCTWeakEventEmitterWrapper new]; eventEmitterWrapper.eventEmitter = fragment.parentShadowView.eventEmitter; NSDictionary *additionalTextAttributes = @{RCTAttributedStringEventEmitterKey : eventEmitterWrapper}; - [nsMutableAttributedStringFragment - addAttributes:additionalTextAttributes - range:NSMakeRange( - 0, nsMutableAttributedStringFragment.length)]; + [nsMutableAttributedStringFragment addAttributes:additionalTextAttributes + range:NSMakeRange(0, nsMutableAttributedStringFragment.length)]; } - [nsAttributedString - appendAttributedString:nsMutableAttributedStringFragment]; + [nsAttributedString appendAttributedString:nsMutableAttributedStringFragment]; } [nsAttributedString endEditing]; diff --git a/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTFontUtils.mm b/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTFontUtils.mm index f6347573b30..fd0bd3c8e4d 100644 --- a/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTFontUtils.mm +++ b/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTFontUtils.mm @@ -7,18 +7,18 @@ #import "RCTFontUtils.h" -#import -#import #import +#import #import +#import -static RCTFontProperties RCTDefaultFontProperties() { +static RCTFontProperties RCTDefaultFontProperties() +{ static RCTFontProperties defaultFontProperties; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ - defaultFontProperties.family = - [UIFont systemFontOfSize:defaultFontProperties.size].familyName; + defaultFontProperties.family = [UIFont systemFontOfSize:defaultFontProperties.size].familyName; defaultFontProperties.size = 14; defaultFontProperties.weight = UIFontWeightRegular; defaultFontProperties.style = RCTFontStyleNormal; @@ -30,36 +30,29 @@ static RCTFontProperties RCTDefaultFontProperties() { } static RCTFontProperties RCTResolveFontProperties( - RCTFontProperties fontProperties, RCTFontProperties baseFontProperties) { - fontProperties.family = fontProperties.family.length - ? fontProperties.family - : baseFontProperties.family; - fontProperties.size = !isnan(fontProperties.size) - ? fontProperties.size - : baseFontProperties.size; - fontProperties.weight = !isnan(fontProperties.weight) - ? fontProperties.weight - : baseFontProperties.weight; - fontProperties.style = fontProperties.style != RCTFontStyleUndefined - ? fontProperties.style - : baseFontProperties.style; - fontProperties.variant = fontProperties.variant != RCTFontVariantUndefined - ? fontProperties.variant - : baseFontProperties.variant; + RCTFontProperties fontProperties, + RCTFontProperties baseFontProperties) +{ + fontProperties.family = fontProperties.family.length ? fontProperties.family : baseFontProperties.family; + fontProperties.size = !isnan(fontProperties.size) ? fontProperties.size : baseFontProperties.size; + fontProperties.weight = !isnan(fontProperties.weight) ? fontProperties.weight : baseFontProperties.weight; + fontProperties.style = + fontProperties.style != RCTFontStyleUndefined ? fontProperties.style : baseFontProperties.style; + fontProperties.variant = + fontProperties.variant != RCTFontVariantUndefined ? fontProperties.variant : baseFontProperties.variant; return fontProperties; } -static UIFontWeight RCTGetFontWeight(UIFont *font) { - NSDictionary *traits = - [font.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute]; +static UIFontWeight RCTGetFontWeight(UIFont *font) +{ + NSDictionary *traits = [font.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute]; return [traits[UIFontWeightTrait] doubleValue]; } -static RCTFontStyle RCTGetFontStyle(UIFont *font) { - NSDictionary *traits = - [font.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute]; - UIFontDescriptorSymbolicTraits symbolicTraits = - [traits[UIFontSymbolicTrait] unsignedIntValue]; +static RCTFontStyle RCTGetFontStyle(UIFont *font) +{ + NSDictionary *traits = [font.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute]; + UIFontDescriptorSymbolicTraits symbolicTraits = [traits[UIFontSymbolicTrait] unsignedIntValue]; if (symbolicTraits & UIFontDescriptorTraitItalic) { return RCTFontStyleItalic; } @@ -67,19 +60,18 @@ static RCTFontStyle RCTGetFontStyle(UIFont *font) { return RCTFontStyleNormal; } -static NSArray *RCTFontFeatures(RCTFontVariant fontVariant) { +static NSArray *RCTFontFeatures(RCTFontVariant fontVariant) +{ // FIXME: return @[]; } -static UIFont *RCTDefaultFontWithFontProperties( - RCTFontProperties fontProperties) { +static UIFont *RCTDefaultFontWithFontProperties(RCTFontProperties fontProperties) +{ static NSCache *fontCache; static std::mutex fontCacheMutex; - NSString *cacheKey = [NSString stringWithFormat:@"%.1f/%.2f", - fontProperties.size, - fontProperties.weight]; + NSString *cacheKey = [NSString stringWithFormat:@"%.1f/%.2f", fontProperties.size, fontProperties.weight]; UIFont *font; { @@ -91,21 +83,16 @@ static UIFont *RCTDefaultFontWithFontProperties( } if (!font) { - font = [UIFont - systemFontOfSize:fontProperties.size - weight:fontProperties.weight]; + font = [UIFont systemFontOfSize:fontProperties.size weight:fontProperties.weight]; if (fontProperties.variant == RCTFontStyleItalic) { UIFontDescriptor *fontDescriptor = [font fontDescriptor]; - UIFontDescriptorSymbolicTraits symbolicTraits = - fontDescriptor.symbolicTraits; + UIFontDescriptorSymbolicTraits symbolicTraits = fontDescriptor.symbolicTraits; symbolicTraits |= UIFontDescriptorTraitItalic; - fontDescriptor = - [fontDescriptor fontDescriptorWithSymbolicTraits:symbolicTraits]; - font = [UIFont fontWithDescriptor:fontDescriptor - size:fontProperties.size]; + fontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:symbolicTraits]; + font = [UIFont fontWithDescriptor:fontDescriptor size:fontProperties.size]; } { @@ -117,21 +104,20 @@ static UIFont *RCTDefaultFontWithFontProperties( return font; } -UIFont *RCTFontWithFontProperties(RCTFontProperties fontProperties) { +UIFont *RCTFontWithFontProperties(RCTFontProperties fontProperties) +{ RCTFontProperties defaultFontProperties = RCTDefaultFontProperties(); fontProperties = RCTResolveFontProperties(fontProperties, defaultFontProperties); assert(!isnan(fontProperties.sizeMultiplier)); - CGFloat effectiveFontSize = - fontProperties.sizeMultiplier * fontProperties.size; + CGFloat effectiveFontSize = fontProperties.sizeMultiplier * fontProperties.size; UIFont *font; if ([fontProperties.family isEqualToString:defaultFontProperties.family]) { // 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 { - NSArray *fontNames = - [UIFont fontNamesForFamilyName:fontProperties.family]; + NSArray *fontNames = [UIFont fontNamesForFamilyName:fontProperties.family]; if (fontNames.count == 0) { // Gracefully handle being given a font name rather than font family, for @@ -140,9 +126,7 @@ UIFont *RCTFontWithFontProperties(RCTFontProperties fontProperties) { if (!font) { // Failback to system font. - font = [UIFont - systemFontOfSize:effectiveFontSize - weight:fontProperties.weight]; + font = [UIFont systemFontOfSize:effectiveFontSize weight:fontProperties.weight]; } } else { // Get the closest font that matches the given weight for the fontFamily @@ -155,8 +139,7 @@ UIFont *RCTFontWithFontProperties(RCTFontProperties fontProperties) { } CGFloat testWeight = RCTGetFontWeight(fontMatch); - if (ABS(testWeight - fontProperties.weight) < - ABS(closestWeight - fontProperties.weight)) { + if (ABS(testWeight - fontProperties.weight) < ABS(closestWeight - fontProperties.weight)) { font = fontMatch; closestWeight = testWeight; } @@ -174,10 +157,8 @@ UIFont *RCTFontWithFontProperties(RCTFontProperties fontProperties) { // Apply font variants to font object. if (fontProperties.variant != RCTFontVariantDefault) { NSArray *fontFeatures = RCTFontFeatures(fontProperties.variant); - UIFontDescriptor *fontDescriptor = - [font.fontDescriptor fontDescriptorByAddingAttributes:@{ - UIFontDescriptorFeatureSettingsAttribute : fontFeatures - }]; + UIFontDescriptor *fontDescriptor = [font.fontDescriptor + fontDescriptorByAddingAttributes:@{UIFontDescriptorFeatureSettingsAttribute : fontFeatures}]; font = [UIFont fontWithDescriptor:fontDescriptor size:effectiveFontSize]; }