Files
react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/RCTTextPrimitivesConversions.h
T
Valentin Shergin 5b93c49e76 Fabric: Using pre-cached UIColor objects for black, white, and clear colors
Summary:
This change maps the three most used colors (black, white, clear) to corresponding predefined values in UIColor. This should meaningfully reduce the overall amount of allocated UIColor/CGColor objects. In my non-scientific measures, it reduces the number of CGColor objects from ~1500 to ~1000. And... it no much at least in terms of kilobytes. However, I still think it's a good idea to implement this because I hope that can remove some work from memory allocation infra and maybe enable some optimizations that UIKit hopefully does for black and white colors. (I tend to believe that this optimization exists because UIKit even has a classes called UIDeviceWhiteColor and UICachedDeviceWhiteColor.)

Changelog: [Internal] Fabric-specific internal change.

Reviewed By: JoshuaGross

Differential Revision: D23753506

fbshipit-source-id: 46e58dc7e6b0dcab3c83d29c7257c90ffbd95246
2020-09-17 11:12:29 -07:00

120 lines
3.5 KiB
Objective-C

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <UIKit/UIKit.h>
#include <react/renderer/textlayoutmanager/RCTFontProperties.h>
#include <react/renderer/textlayoutmanager/RCTFontUtils.h>
using namespace facebook::react;
inline static NSTextAlignment RCTNSTextAlignmentFromTextAlignment(TextAlignment textAlignment)
{
switch (textAlignment) {
case TextAlignment::Natural:
return NSTextAlignmentNatural;
case TextAlignment::Left:
return NSTextAlignmentLeft;
case TextAlignment::Right:
return NSTextAlignmentRight;
case TextAlignment::Center:
return NSTextAlignmentCenter;
case TextAlignment::Justified:
return NSTextAlignmentJustified;
}
}
inline static NSWritingDirection RCTNSWritingDirectionFromWritingDirection(WritingDirection writingDirection)
{
switch (writingDirection) {
case WritingDirection::Natural:
return NSWritingDirectionNatural;
case WritingDirection::LeftToRight:
return NSWritingDirectionLeftToRight;
case WritingDirection::RightToLeft:
return NSWritingDirectionRightToLeft;
}
}
inline static RCTFontStyle RCTFontStyleFromFontStyle(FontStyle fontStyle)
{
switch (fontStyle) {
case FontStyle::Normal:
return RCTFontStyleNormal;
case FontStyle::Italic:
return RCTFontStyleItalic;
case FontStyle::Oblique:
return RCTFontStyleOblique;
}
}
inline static RCTFontVariant RCTFontVariantFromFontVariant(FontVariant fontVariant)
{
return (RCTFontVariant)fontVariant;
}
inline static NSUnderlineStyle RCTNSUnderlineStyleFromStyleAndPattern(
TextDecorationLineStyle textDecorationLineStyle,
TextDecorationLinePattern textDecorationLinePattern)
{
NSUnderlineStyle style = NSUnderlineStyleNone;
switch (textDecorationLineStyle) {
case TextDecorationLineStyle::Single:
style = NSUnderlineStyle(style | NSUnderlineStyleSingle);
break;
case TextDecorationLineStyle::Thick:
style = NSUnderlineStyle(style | NSUnderlineStyleThick);
break;
case TextDecorationLineStyle::Double:
style = NSUnderlineStyle(style | NSUnderlineStyleDouble);
break;
}
switch (textDecorationLinePattern) {
case TextDecorationLinePattern::Solid:
style = NSUnderlineStyle(style | NSUnderlinePatternSolid);
break;
case TextDecorationLinePattern::Dash:
style = NSUnderlineStyle(style | NSUnderlinePatternDash);
break;
case TextDecorationLinePattern::Dot:
style = NSUnderlineStyle(style | NSUnderlinePatternDot);
break;
case TextDecorationLinePattern::DashDot:
style = NSUnderlineStyle(style | NSUnderlinePatternDashDot);
break;
case TextDecorationLinePattern::DashDotDot:
style = NSUnderlineStyle(style | NSUnderlinePatternDashDotDot);
break;
}
return style;
}
inline static UIColor *RCTUIColorFromSharedColor(const SharedColor &sharedColor)
{
if (!sharedColor) {
return nil;
}
if (*facebook::react::clearColor() == *sharedColor) {
return [UIColor clearColor];
}
if (*facebook::react::blackColor() == *sharedColor) {
return [UIColor blackColor];
}
if (*facebook::react::whiteColor() == *sharedColor) {
return [UIColor whiteColor];
}
auto components = facebook::react::colorComponentsFromColor(sharedColor);
return [UIColor colorWithRed:components.red green:components.green blue:components.blue alpha:components.alpha];
}