first commit

This commit is contained in:
2026-03-10 16:18:05 +00:00
commit 11f9c069b5
31635 changed files with 3187747 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <CoreFoundation/CFRunLoop.h>
#include <CoreFoundation/CoreFoundation.h>
#include <react/utils/RunLoopObserver.h>
namespace facebook::react {
/*
* Concrete iOS-specific implementation of `RunLoopObserver` using
* `CFRunLoopObserver` under the hood.
*/
class PlatformRunLoopObserver : public RunLoopObserver {
public:
PlatformRunLoopObserver(RunLoopObserver::Activity activities, RunLoopObserver::WeakOwner owner, CFRunLoopRef runLoop);
~PlatformRunLoopObserver();
virtual bool isOnRunLoopThread() const noexcept override;
private:
void startObserving() const noexcept override;
void stopObserving() const noexcept override;
CFRunLoopRef runLoop_;
CFRunLoopObserverRef mainRunLoopObserver_;
};
/*
* Convenience specialization of `PlatformRunLoopObserver` observing the main
* run loop.
*/
class MainRunLoopObserver final : public PlatformRunLoopObserver {
public:
MainRunLoopObserver(RunLoopObserver::Activity activities, RunLoopObserver::WeakOwner owner)
: PlatformRunLoopObserver(activities, std::move(owner), CFRunLoopGetMain())
{
}
};
} // namespace facebook::react

View File

@@ -0,0 +1,92 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "PlatformRunLoopObserver.h"
#import <mutex>
namespace facebook::react {
static CFRunLoopActivity toCFRunLoopActivity(RunLoopObserver::Activity activity)
{
auto result = CFRunLoopActivity{};
if (RunLoopObserver::Activity(activity & RunLoopObserver::Activity::BeforeWaiting) ==
RunLoopObserver::Activity::BeforeWaiting) {
result = result | kCFRunLoopBeforeWaiting;
}
if (RunLoopObserver::Activity(activity & RunLoopObserver::Activity::AfterWaiting) ==
RunLoopObserver::Activity::AfterWaiting) {
result = result | kCFRunLoopAfterWaiting;
}
return result;
}
static RunLoopObserver::Activity toRunLoopActivity(CFRunLoopActivity activity)
{
auto result = RunLoopObserver::Activity{};
if (CFRunLoopActivity(activity & kCFRunLoopBeforeWaiting) == kCFRunLoopBeforeWaiting) {
result = RunLoopObserver::Activity(result | RunLoopObserver::Activity::BeforeWaiting);
}
if (CFRunLoopActivity(activity & kCFRunLoopAfterWaiting) == kCFRunLoopAfterWaiting) {
result = RunLoopObserver::Activity(result | RunLoopObserver::Activity::AfterWaiting);
}
return result;
}
PlatformRunLoopObserver::PlatformRunLoopObserver(
RunLoopObserver::Activity activities,
RunLoopObserver::WeakOwner owner,
CFRunLoopRef runLoop)
: RunLoopObserver(activities, owner), runLoop_(runLoop)
{
// The documentation for `CFRunLoop` family API states that all of the methods are thread-safe.
// See "Thread Safety and Run Loop Objects" section of the "Threading Programming Guide" for more details.
mainRunLoopObserver_ = CFRunLoopObserverCreateWithHandler(
NULL /* allocator */,
toCFRunLoopActivity(activities_) /* activities */,
1u /* repeats */,
0 /* order */,
^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
auto strongOwner = owner.lock();
if (!strongOwner) {
return;
}
this->activityDidChange(toRunLoopActivity(activity));
});
assert(mainRunLoopObserver_);
}
PlatformRunLoopObserver::~PlatformRunLoopObserver()
{
stopObserving();
CFRelease(mainRunLoopObserver_);
}
void PlatformRunLoopObserver::startObserving() const noexcept
{
CFRunLoopAddObserver(runLoop_, mainRunLoopObserver_, kCFRunLoopCommonModes);
}
void PlatformRunLoopObserver::stopObserving() const noexcept
{
CFRunLoopRemoveObserver(runLoop_, mainRunLoopObserver_, kCFRunLoopCommonModes);
}
bool PlatformRunLoopObserver::isOnRunLoopThread() const noexcept
{
return CFRunLoopGetCurrent() == runLoop_;
}
} // namespace facebook::react

View File

@@ -0,0 +1,28 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import <react/renderer/graphics/BackgroundPosition.h>
#import <react/renderer/graphics/BackgroundRepeat.h>
#import <react/renderer/graphics/BackgroundSize.h>
@interface RCTBackgroundImageUtils : NSObject
+ (CGSize)calculateBackgroundImageSize:(const CGRect &)positioningArea
itemIntrinsicSize:(CGSize)itemIntrinsicSize
backgroundSize:(const facebook::react::BackgroundSize &)backgroundSize
backgroundRepeat:(const facebook::react::BackgroundRepeat &)backgroundRepeat;
+ (CALayer *)createBackgroundImageLayerWithSize:(const CGRect &)positioningArea
paintingArea:(const CGRect &)paintingArea
itemSize:(const CGSize &)itemSize
backgroundPosition:(const facebook::react::BackgroundPosition &)backgroundPosition
backgroundRepeat:(const facebook::react::BackgroundRepeat &)backgroundRepeat
itemLayer:(CALayer *)itemLayer;
@end

View File

@@ -0,0 +1,209 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTBackgroundImageUtils.h"
@implementation RCTBackgroundImageUtils
+ (CALayer *)createBackgroundImageLayerWithSize:(const CGRect &)positioningArea
paintingArea:(const CGRect &)paintingArea
itemSize:(const CGSize &)itemSize
backgroundPosition:(const facebook::react::BackgroundPosition &)backgroundPosition
backgroundRepeat:(const facebook::react::BackgroundRepeat &)backgroundRepeat
itemLayer:(CALayer *)itemLayer
{
if ((itemLayer == nullptr) || CGSizeEqualToSize(itemSize, CGSizeZero)) {
return [CALayer layer];
}
if (CGRectIsEmpty(positioningArea) || CGRectIsEmpty(paintingArea)) {
return [CALayer layer];
}
CGFloat gradientFrameX = positioningArea.origin.x;
CGFloat gradientFrameY = positioningArea.origin.y;
CGFloat availableWidth = positioningArea.size.width - itemSize.width;
CGFloat availableHeight = positioningArea.size.height - itemSize.height;
if (backgroundPosition.top.has_value()) {
gradientFrameY = backgroundPosition.top->resolve(static_cast<float>(availableHeight)) + gradientFrameY;
} else if (backgroundPosition.bottom.has_value()) {
gradientFrameY =
(availableHeight - backgroundPosition.bottom->resolve(static_cast<float>(availableHeight))) + gradientFrameY;
}
if (backgroundPosition.left.has_value()) {
gradientFrameX = backgroundPosition.left->resolve(static_cast<float>(availableWidth)) + gradientFrameX;
} else if (backgroundPosition.right.has_value()) {
gradientFrameX =
(availableWidth - backgroundPosition.right->resolve(static_cast<float>(availableWidth))) + gradientFrameX;
}
CALayer *tiledLayer = itemLayer;
CAReplicatorLayer *replicatorX = nil;
CGFloat finalX = gradientFrameX;
CGFloat finalW = itemSize.width;
if (backgroundRepeat.x != facebook::react::BackgroundRepeatStyle::NoRepeat) {
replicatorX = [CAReplicatorLayer layer];
[replicatorX addSublayer:tiledLayer];
tiledLayer = replicatorX;
}
if (backgroundRepeat.x == facebook::react::BackgroundRepeatStyle::Space) {
if (itemSize.width <= 0) {
replicatorX.instanceCount = 1;
} else {
float instanceCount = floor(positioningArea.size.width / itemSize.width);
if (instanceCount > 1) {
gradientFrameX = positioningArea.origin.x;
auto spacing =
static_cast<float>((positioningArea.size.width - instanceCount * itemSize.width) / (instanceCount - 1));
replicatorX.instanceTransform = CATransform3DMakeTranslation(itemSize.width + spacing, 0, 0);
auto tilesBeforeX = static_cast<float>(ceil(static_cast<float>(gradientFrameX) / (itemSize.width + spacing)));
float tilesAfterX = 1 +
static_cast<float>(ceil(
(paintingArea.size.width - (gradientFrameX + itemSize.width)) / (itemSize.width + spacing)));
float totalInstances = tilesBeforeX + tilesAfterX;
replicatorX.instanceCount = static_cast<NSInteger>(totalInstances);
finalX -= (tilesBeforeX * (itemSize.width + spacing));
finalW = totalInstances * (itemSize.width + spacing * (totalInstances - 1));
} else {
replicatorX.instanceCount = 1;
}
}
} else if (
backgroundRepeat.x == facebook::react::BackgroundRepeatStyle::Repeat ||
backgroundRepeat.x == facebook::react::BackgroundRepeatStyle::Round) {
if (itemSize.width <= 0) {
replicatorX.instanceCount = 1;
} else {
replicatorX.instanceTransform = CATransform3DMakeTranslation(itemSize.width, 0, 0);
float tilesBeforeX = ceil(gradientFrameX / itemSize.width);
float tilesAfterX = ceil((paintingArea.size.width - gradientFrameX) / itemSize.width);
float totalInstances = tilesBeforeX + tilesAfterX;
replicatorX.instanceCount = static_cast<NSInteger>(totalInstances);
finalX = gradientFrameX - (tilesBeforeX * itemSize.width);
finalW = totalInstances * itemSize.width;
}
}
CAReplicatorLayer *replicatorY = nil;
CGFloat finalY = gradientFrameY;
CGFloat finalH = itemSize.height;
if (backgroundRepeat.y != facebook::react::BackgroundRepeatStyle::NoRepeat) {
replicatorY = [CAReplicatorLayer layer];
[replicatorY addSublayer:tiledLayer];
tiledLayer = replicatorY;
}
if (backgroundRepeat.y == facebook::react::BackgroundRepeatStyle::Space) {
if (itemSize.height <= 0) {
replicatorY.instanceCount = 1;
} else {
float instanceCount = floor(positioningArea.size.height / itemSize.height);
if (instanceCount > 1) {
gradientFrameY = positioningArea.origin.y;
auto spacing =
static_cast<float>((positioningArea.size.height - instanceCount * itemSize.height) / (instanceCount - 1));
replicatorY.instanceTransform = CATransform3DMakeTranslation(0, itemSize.height + spacing, 0);
auto tilesBeforeY = static_cast<float>(ceil(static_cast<float>(gradientFrameY) / (itemSize.height + spacing)));
float tilesAfterY = 1 +
static_cast<float>(ceil(
(paintingArea.size.height - (gradientFrameY + itemSize.height)) / (itemSize.height + spacing)));
float totalInstances = tilesBeforeY + tilesAfterY;
replicatorY.instanceCount = static_cast<NSInteger>(totalInstances);
finalY -= (tilesBeforeY * (itemSize.height + spacing));
finalH = totalInstances * (itemSize.height + spacing * (totalInstances - 1));
} else {
replicatorY.instanceCount = 1;
}
}
} else if (
backgroundRepeat.y == facebook::react::BackgroundRepeatStyle::Repeat ||
backgroundRepeat.y == facebook::react::BackgroundRepeatStyle::Round) {
if (itemSize.height <= 0) {
replicatorY.instanceCount = 1;
} else {
replicatorY.instanceTransform = CATransform3DMakeTranslation(0, itemSize.height, 0);
float tilesBeforeY = ceil(gradientFrameY / itemSize.height);
float tilesAfterY = ceil((paintingArea.size.height - gradientFrameY) / itemSize.height);
float instanceCount = tilesBeforeY + tilesAfterY;
replicatorY.instanceCount = static_cast<NSInteger>(instanceCount);
finalY = gradientFrameY - (tilesBeforeY * itemSize.height);
finalH = instanceCount * itemSize.height;
}
}
tiledLayer.frame = CGRectMake(finalX, finalY, finalW, finalH);
CALayer *backgroundImageLayer = [CALayer layer];
[backgroundImageLayer addSublayer:tiledLayer];
return backgroundImageLayer;
}
+ (CGSize)calculateBackgroundImageSize:(const CGRect &)positioningArea
itemIntrinsicSize:(CGSize)itemIntrinsicSize
backgroundSize:(const facebook::react::BackgroundSize &)backgroundSize
backgroundRepeat:(const facebook::react::BackgroundRepeat &)backgroundRepeat
{
if (CGSizeEqualToSize(itemIntrinsicSize, CGSizeZero) || CGRectIsEmpty(positioningArea)) {
return CGSizeZero;
}
CGSize itemFinalSize = itemIntrinsicSize;
CGSize positioningAreaSize = positioningArea.size;
if (std::holds_alternative<facebook::react::BackgroundSizeLengthPercentage>(backgroundSize)) {
auto backgroundSizeLengthPercentage = std::get<facebook::react::BackgroundSizeLengthPercentage>(backgroundSize);
if (std::holds_alternative<facebook::react::ValueUnit>(backgroundSizeLengthPercentage.x)) {
auto xValue = std::get<facebook::react::ValueUnit>(backgroundSizeLengthPercentage.x);
if (xValue.unit == facebook::react::UnitType::Percent) {
itemFinalSize.width = xValue.value * positioningAreaSize.width / 100.0;
} else {
itemFinalSize.width = xValue.value;
}
}
if (std::holds_alternative<facebook::react::ValueUnit>(backgroundSizeLengthPercentage.y)) {
auto yValue = std::get<facebook::react::ValueUnit>(backgroundSizeLengthPercentage.y);
if (yValue.unit == facebook::react::UnitType::Percent) {
itemFinalSize.height = yValue.value * positioningAreaSize.height / 100.0;
} else {
itemFinalSize.height = yValue.value;
}
}
}
if (backgroundRepeat.x == facebook::react::BackgroundRepeatStyle::Round) {
if (itemFinalSize.width > 0 && positioningAreaSize.width > 0) {
if (fmod(positioningAreaSize.width, itemFinalSize.width) != 0) {
auto divisor = static_cast<float>(round(positioningAreaSize.width / itemFinalSize.width));
if (divisor > 0) {
itemFinalSize.width = positioningAreaSize.width / divisor;
}
}
}
}
if (backgroundRepeat.y == facebook::react::BackgroundRepeatStyle::Round) {
if (itemFinalSize.height > 0 && positioningAreaSize.height > 0) {
if (fmod(positioningAreaSize.height, itemFinalSize.height) != 0) {
auto divisor = static_cast<float>(round(positioningAreaSize.height / itemFinalSize.height));
if (divisor > 0) {
itemFinalSize.height = positioningAreaSize.height / divisor;
}
}
}
}
return itemFinalSize;
}
@end

View File

@@ -0,0 +1,19 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <vector>
#import <React/RCTBorderDrawing.h>
#import <React/RCTDefines.h>
#import <UIKit/UIKit.h>
#import <react/renderer/graphics/BoxShadow.h>
RCT_EXTERN CALayer *RCTGetBoxShadowLayer(
const facebook::react::BoxShadow &shadow,
RCTCornerRadii cornerRadii,
UIEdgeInsets edgeInsets,
CGSize layerSize);

View File

@@ -0,0 +1,169 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTBoxShadow.h"
#import <React/RCTConversions.h>
#import <react/renderer/graphics/Color.h>
#import <math.h>
using namespace facebook::react;
// See https://drafts.csswg.org/css-backgrounds/#shadow-shape
static CGFloat adjustedCornerRadius(CGFloat cornerRadius, CGFloat spreadDistance)
{
CGFloat adjustment = spreadDistance;
(void)adjustment;
if (cornerRadius < abs(spreadDistance)) {
const CGFloat r = cornerRadius / (CGFloat)abs(spreadDistance);
const CGFloat p = (CGFloat)pow(r - 1.0, 3.0);
adjustment *= 1.0 + p;
}
return fmax(cornerRadius + adjustment, 0);
}
static RCTCornerRadii cornerRadiiForBoxShadow(RCTCornerRadii cornerRadii, CGFloat spreadDistance)
{
return {
adjustedCornerRadius(cornerRadii.topLeftHorizontal, spreadDistance),
adjustedCornerRadius(cornerRadii.topLeftVertical, spreadDistance),
adjustedCornerRadius(cornerRadii.topRightHorizontal, spreadDistance),
adjustedCornerRadius(cornerRadii.topRightVertical, spreadDistance),
adjustedCornerRadius(cornerRadii.bottomLeftHorizontal, spreadDistance),
adjustedCornerRadius(cornerRadii.bottomLeftVertical, spreadDistance),
adjustedCornerRadius(cornerRadii.bottomRightHorizontal, spreadDistance),
adjustedCornerRadius(cornerRadii.bottomRightVertical, spreadDistance)};
}
static CGRect insetRect(CGRect rect, CGFloat left, CGFloat top, CGFloat right, CGFloat bottom)
{
return CGRectMake(
rect.origin.x + left, rect.origin.y + top, rect.size.width - right - left, rect.size.height - bottom - top);
}
static CGColorRef colorRefFromSharedColor(const SharedColor &color)
{
CGColorRef colorRef = RCTUIColorFromSharedColor(color).CGColor;
return (colorRef != nullptr) ? colorRef : [UIColor blackColor].CGColor;
}
static CALayer *initBoxShadowLayer(const BoxShadow &shadow, CGSize layerSize)
{
CALayer *shadowLayer = [CALayer layer];
shadowLayer.frame = CGRectMake(0, 0, layerSize.width, layerSize.height);
shadowLayer.shadowColor = colorRefFromSharedColor(shadow.color);
// Default is (0, -3) believe it or not
shadowLayer.shadowOffset = CGSizeMake(0, 0);
shadowLayer.shadowOpacity = 1;
// Apple's blur is not quite what we want and seems to be a bit overbearing
// with the radius. This is an eyeballed adjustment that has the blur looking
// more like the web.
shadowLayer.shadowRadius = shadow.blurRadius / 2;
shadowLayer.contentsScale = [UIScreen mainScreen].scale;
return shadowLayer;
}
static CALayer *
RCTGetOutsetBoxShadowLayer(const facebook::react::BoxShadow &shadow, RCTCornerRadii cornerRadii, CGSize layerSize)
{
CALayer *shadowLayer = initBoxShadowLayer(shadow, layerSize);
const RCTCornerInsets shadowRectCornerInsets =
RCTGetCornerInsets(cornerRadiiForBoxShadow(cornerRadii, shadow.spreadDistance), UIEdgeInsetsZero);
CGRect shadowRect = CGRectInset(shadowLayer.bounds, -shadow.spreadDistance, -shadow.spreadDistance);
shadowRect = CGRectOffset(shadowRect, shadow.offsetX, shadow.offsetY);
CGPathRef shadowRectPath = RCTPathCreateWithRoundedRect(shadowRect, shadowRectCornerInsets, nil, NO);
shadowLayer.shadowPath = shadowRectPath;
CAShapeLayer *mask = [CAShapeLayer new];
[mask setContentsScale:[UIScreen mainScreen].scale];
CGMutablePathRef path = CGPathCreateMutable();
CGPathRef layerPath =
RCTPathCreateWithRoundedRect(shadowLayer.bounds, RCTGetCornerInsets(cornerRadii, UIEdgeInsetsZero), nil, NO);
CGPathAddPath(path, NULL, layerPath);
CGPathRef paddedShadowRectPath = RCTPathCreateWithRoundedRect(
CGRectInset(shadowRect, -2 * (shadow.blurRadius + 1), -2 * (shadow.blurRadius + 1)),
shadowRectCornerInsets,
nil,
NO);
CGPathAddPath(path, NULL, paddedShadowRectPath);
mask.fillRule = kCAFillRuleEvenOdd;
mask.path = path;
shadowLayer.mask = mask;
CGPathRelease(path);
CGPathRelease(shadowRectPath);
CGPathRelease(layerPath);
CGPathRelease(paddedShadowRectPath);
return shadowLayer;
}
static CALayer *RCTGetInsetBoxShadowLayer(
const facebook::react::BoxShadow &shadow,
RCTCornerRadii cornerRadii,
UIEdgeInsets edgeInsets,
CGSize layerSize)
{
CALayer *shadowLayer = initBoxShadowLayer(shadow, layerSize);
CGMutablePathRef path = CGPathCreateMutable();
// This shadow is padded by the blur to make sure blur artifacts can be cast
// if the clear region is right on the border of the layer
CGRect shadowRect =
insetRect(shadowLayer.bounds, edgeInsets.left, edgeInsets.top, edgeInsets.right, edgeInsets.bottom);
if (CGRectIsNull(shadowRect)) {
shadowRect = CGRectMake(0, 0, 0, 0);
}
CGPathRef shadowPath =
RCTPathCreateWithRoundedRect(CGRectInset(shadowRect, -shadow.blurRadius, -shadow.blurRadius), {}, nil, NO);
CGPathRef layerPath = RCTPathCreateWithRoundedRect(shadowRect, RCTGetCornerInsets(cornerRadii, edgeInsets), nil, NO);
CGPathAddPath(path, NULL, shadowPath);
CGRect clearRegionRect = CGRectOffset(shadowRect, shadow.offsetX, shadow.offsetY);
clearRegionRect = CGRectInset(clearRegionRect, shadow.spreadDistance, shadow.spreadDistance);
if (CGRectIsNull(clearRegionRect)) {
clearRegionRect = CGRectMake(0, 0, 0, 0);
}
CGPathRef clearRegionPath = RCTPathCreateWithRoundedRect(
clearRegionRect,
RCTGetCornerInsets(cornerRadiiForBoxShadow(cornerRadii, -shadow.spreadDistance), edgeInsets),
nil,
YES);
CGPathAddPath(path, NULL, clearRegionPath);
shadowLayer.shadowPath = path;
CAShapeLayer *mask = [CAShapeLayer new];
mask.path = layerPath;
shadowLayer.mask = mask;
CGPathRelease(path);
CGPathRelease(layerPath);
CGPathRelease(shadowPath);
CGPathRelease(clearRegionPath);
return shadowLayer;
}
CALayer *RCTGetBoxShadowLayer(
const facebook::react::BoxShadow &shadow,
RCTCornerRadii cornerRadii,
UIEdgeInsets edgeInsets,
CGSize layerSize)
{
if (shadow.inset) {
return RCTGetInsetBoxShadowLayer(shadow, cornerRadii, edgeInsets, layerSize);
} else {
return RCTGetOutsetBoxShadowLayer(shadow, cornerRadii, layerSize);
}
}

View File

@@ -0,0 +1,16 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#import <React/RCTConvert.h>
@interface RCTColorSpaceUtils : NSObject
+ (void)applyDefaultColorSpace:(RCTColorSpace)colorSpace;
@end

View File

@@ -0,0 +1,23 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTColorSpaceUtils.h"
#import <react/renderer/graphics/ColorComponents.h>
@implementation RCTColorSpaceUtils
+ (void)applyDefaultColorSpace:(RCTColorSpace)colorSpace
{
facebook::react::ColorSpace cxxColorSpace =
colorSpace == RCTColorSpaceSRGB ? facebook::react::ColorSpace::sRGB : facebook::react::ColorSpace::DisplayP3;
RCTSetDefaultColorSpace(colorSpace);
facebook::react::setDefaultColorSpace(cxxColorSpace);
}
@end

View File

@@ -0,0 +1,44 @@
/*
* Copyright (c) Meta Platforms, Inc. and 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>
NS_ASSUME_NONNULL_BEGIN
/**
* General purpose implementation of Delegate Splitter (or Multicast) pattern which allows subscribing multiple
* `receiving` objects to single `sending` object (which normally does not support that feature by itself).
*
* In the case where only one receiving object is registered, using Splitter has zero performance overhead because the
* receiver is being subscribed directly. In the case where more than one receiving objects are registered, using
* Splitter introduces some performance overhead.
*/
@interface RCTGenericDelegateSplitter<DelegateT> : NSObject
@property (nonatomic, copy, nullable) void (^delegateUpdateBlock)(DelegateT _Nullable delegate);
/*
* Creates an object with a given block that will be used to connect a `sending` object with a given `receiving` object.
* The class calls the block every time after each delegate adding or removing procedure, and it calls it twice: the
* first time with `nil` and the second time with actual delegate. This is required to establish a proper connection
* between sending and receiving objects (to reset caches storing information about supported (or not) optional
* methods).
*/
- (instancetype)initWithDelegateUpdateBlock:(void (^)(DelegateT _Nullable delegate))block;
/*
* Adds and removes a delegate.
* The delegates will be called in order of registration.
* If a delegate returns a value, the value from the last call will be passed to the `sending` object.
*/
- (void)addDelegate:(DelegateT)delegate;
- (void)removeDelegate:(DelegateT)delegate;
- (void)removeAllDelegates;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,94 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTGenericDelegateSplitter.h"
@implementation RCTGenericDelegateSplitter {
NSHashTable *_delegates;
}
#pragma mark - Public
- (instancetype)initWithDelegateUpdateBlock:(void (^)(id _Nullable delegate))block
{
if (self = [super init]) {
_delegateUpdateBlock = block;
_delegates = [NSHashTable weakObjectsHashTable];
}
return self;
}
- (void)addDelegate:(id)delegate
{
[_delegates addObject:delegate];
[self _updateDelegate];
}
- (void)removeDelegate:(id)delegate
{
[_delegates removeObject:delegate];
[self _updateDelegate];
}
- (void)removeAllDelegates
{
[_delegates removeAllObjects];
[self _updateDelegate];
}
#pragma mark - Private
- (void)_updateDelegate
{
_delegateUpdateBlock(nil);
if (_delegates.count == 0) {
return;
}
_delegateUpdateBlock(_delegates.count == 1 ? [_delegates allObjects].firstObject : self);
}
#pragma mark - Fast Forwarding
- (BOOL)respondsToSelector:(SEL)selector
{
for (id delegate in _delegates) {
if ([delegate respondsToSelector:selector]) {
return YES;
}
}
return NO;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
{
for (id delegate in _delegates) {
if ([delegate respondsToSelector:selector]) {
return [delegate methodSignatureForSelector:selector];
}
}
return nil;
}
- (void)forwardInvocation:(NSInvocation *)invocation
{
NSMutableArray *targets = [[NSMutableArray alloc] initWithCapacity:_delegates.count];
for (id delegate in _delegates) {
if ([delegate respondsToSelector:[invocation selector]]) {
[targets addObject:delegate];
}
}
for (id target in targets) {
[invocation invokeWithTarget:target];
}
}
@end

View File

@@ -0,0 +1,33 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <Foundation/Foundation.h>
#include <react/renderer/graphics/ColorStop.h>
#import <vector>
NS_ASSUME_NONNULL_BEGIN
@interface RCTGradientUtils : NSObject
+ (std::vector<facebook::react::ProcessedColorStop>)getFixedColorStops:
(const std::vector<facebook::react::ColorStop> &)colorStops
gradientLineLength:(CGFloat)gradientLineLength;
// CAGradientLayer linear gradient squishes the non-square gradient to square gradient.
// This function fixes the "squished" effect.
// See https://stackoverflow.com/a/43176174 for more information.
+ (std::pair<CGPoint, CGPoint>)pointsForCAGradientLayerLinearGradient:(CGPoint)startPoint
endPoint:(CGPoint)endPoint
bounds:(CGSize)bounds;
+ (void)getColors:(NSMutableArray<id> *)colors
andLocations:(NSMutableArray<NSNumber *> *)locations
fromColorStops:(const std::vector<facebook::react::ProcessedColorStop> &)colorStops;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,415 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTGradientUtils.h"
#import <React/RCTAnimationUtils.h>
#import <React/RCTConversions.h>
#import <react/utils/FloatComparison.h>
#include <optional>
#import <vector>
using namespace facebook::react;
namespace {
struct Line;
struct LineSegment {
CGPoint p1;
CGPoint p2;
LineSegment(CGPoint p1, CGPoint p2) : p1(p1), p2(p2) {}
LineSegment(CGPoint p1, CGFloat m, CGFloat distance);
CGFloat getLength() const
{
CGFloat dx = p2.x - p1.x;
CGFloat dy = p2.y - p1.y;
return sqrt(dx * dx + dy * dy);
}
CGFloat getDistance() const
{
return p1.x <= p2.x ? getLength() : -getLength();
}
CGPoint getMidpoint() const
{
return CGPointMake((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
}
CGFloat getSlope() const
{
CGFloat dx = p2.x - p1.x;
if (floatEquality(dx, 0.0)) {
return std::numeric_limits<CGFloat>::infinity();
}
return (p2.y - p1.y) / dx;
}
CGFloat getPerpendicularSlope() const
{
CGFloat slope = getSlope();
if (std::isinf(slope)) {
return 0.0;
}
if (floatEquality(slope, 0.0)) {
return -std::numeric_limits<CGFloat>::infinity();
}
return -1 / slope;
}
Line toLine() const;
LineSegment perpendicularBisector() const
{
CGPoint midpoint = getMidpoint();
CGFloat perpSlope = getPerpendicularSlope();
CGFloat dist = getDistance();
return {LineSegment(midpoint, perpSlope, -dist / 2).p2, LineSegment(midpoint, perpSlope, dist / 2).p2};
}
LineSegment multiplied(CGSize multipliers) const
{
return {
CGPointMake(p1.x * multipliers.width, p1.y * multipliers.height),
CGPointMake(p2.x * multipliers.width, p2.y * multipliers.height)};
}
LineSegment divided(CGSize divisors) const
{
return multiplied(CGSizeMake(1 / divisors.width, 1 / divisors.height));
}
};
struct Line {
CGFloat m;
CGFloat b;
Line(CGFloat m, CGFloat b) : m(m), b(b) {}
Line(CGFloat m, CGPoint p) : m(m), b(p.y - m * p.x) {}
Line(CGPoint p1, CGPoint p2) : m(LineSegment(p1, p2).getSlope()), b(p1.y - m * p1.x) {}
CGFloat y(CGFloat x) const
{
return m * x + b;
}
CGPoint point(CGFloat x) const
{
return CGPointMake(x, y(x));
}
std::optional<CGPoint> intersection(const Line &other) const
{
CGFloat n = other.m;
CGFloat c = other.b;
if (floatEquality(m, n)) {
return std::nullopt;
}
CGFloat x = (c - b) / (m - n);
return point(x);
}
};
LineSegment::LineSegment(CGPoint p1, CGFloat m, CGFloat distance) : p1(p1)
{
Line line(m, p1);
CGPoint measuringPoint = line.point(p1.x + 1);
LineSegment measuringSegment(p1, measuringPoint);
CGFloat measuringDeltaH = measuringSegment.getDistance();
CGFloat deltaX = !floatEquality(measuringDeltaH, 0.0) ? distance / measuringDeltaH : 0.0;
p2 = line.point(p1.x + deltaX);
}
Line LineSegment::toLine() const
{
return {p1, p2};
}
CGSize calculateMultipliers(CGSize bounds)
{
if (bounds.height <= bounds.width) {
return CGSizeMake(1, bounds.width / bounds.height);
} else {
return CGSizeMake(bounds.height / bounds.width, 1);
}
}
} // namespace
static std::optional<Float> resolveColorStopPosition(ValueUnit position, CGFloat gradientLineLength)
{
if (position.unit == UnitType::Point) {
return position.resolve(0.0f) / gradientLineLength;
}
if (position.unit == UnitType::Percent) {
return position.resolve(1.0f);
}
return std::nullopt;
}
// Spec: https://drafts.csswg.org/css-images-4/#coloring-gradient-line (Refer transition hint section)
// Browsers add 9 intermediate color stops when a transition hint is present
// Algorithm is referred from Blink engine
// [source](https://github.com/chromium/chromium/blob/a296b1bad6dc1ed9d751b7528f7ca2134227b828/third_party/blink/renderer/core/css/css_gradient_value.cc#L240).
static std::vector<ProcessedColorStop> processColorTransitionHints(const std::vector<ProcessedColorStop> &originalStops)
{
auto colorStops = std::vector<ProcessedColorStop>(originalStops);
int indexOffset = 0;
for (size_t i = 1; i < originalStops.size() - 1; ++i) {
// Skip if not a color hint
if (originalStops[i].color) {
continue;
}
size_t x = i + indexOffset;
if (x < 1) {
continue;
}
auto offsetLeft = colorStops[x - 1].position.value();
auto offsetRight = colorStops[x + 1].position.value();
auto offset = colorStops[x].position.value();
auto leftDist = offset - offsetLeft;
auto rightDist = offsetRight - offset;
auto totalDist = offsetRight - offsetLeft;
const SharedColor &leftSharedColor = colorStops[x - 1].color;
const SharedColor &rightSharedColor = colorStops[x + 1].color;
if (floatEquality(leftDist, rightDist)) {
colorStops.erase(colorStops.begin() + x);
--indexOffset;
continue;
}
if (floatEquality(leftDist, 0.0)) {
colorStops[x].color = rightSharedColor;
continue;
}
if (floatEquality(rightDist, 0.0)) {
colorStops[x].color = leftSharedColor;
continue;
}
std::vector<ProcessedColorStop> newStops;
newStops.reserve(9);
// Position the new color stops
if (leftDist > rightDist) {
for (int y = 0; y < 7; ++y) {
ProcessedColorStop newStop{.color = SharedColor(), .position = offsetLeft + leftDist * ((7.0f + y) / 13.0f)};
newStops.push_back(newStop);
}
ProcessedColorStop stop1{.color = SharedColor(), .position = offset + rightDist * (1.0f / 3.0f)};
ProcessedColorStop stop2{.color = SharedColor(), .position = offset + rightDist * (2.0f / 3.0f)};
newStops.push_back(stop1);
newStops.push_back(stop2);
} else {
ProcessedColorStop stop1{.color = SharedColor(), .position = offsetLeft + leftDist * (1.0f / 3.0f)};
ProcessedColorStop stop2{.color = SharedColor(), .position = offsetLeft + leftDist * (2.0f / 3.0f)};
newStops.push_back(stop1);
newStops.push_back(stop2);
for (int y = 0; y < 7; ++y) {
ProcessedColorStop newStop{.color = SharedColor(), .position = offset + rightDist * (y / 13.0f)};
newStops.push_back(newStop);
}
}
// calculate colors for the new color hints.
// The color weighting for the new color stops will be
// pointRelativeOffset^(ln(0.5)/ln(hintRelativeOffset)).
auto hintRelativeOffset = leftDist / totalDist;
const auto logRatio = log(0.5) / log(hintRelativeOffset);
auto leftColor = RCTUIColorFromSharedColor(leftSharedColor);
auto rightColor = RCTUIColorFromSharedColor(rightSharedColor);
NSArray<NSNumber *> *inputRange = @[ @0.0, @1.0 ];
NSArray<UIColor *> *outputRange = @[ leftColor, rightColor ];
for (auto &newStop : newStops) {
auto pointRelativeOffset = (newStop.position.value() - offsetLeft) / totalDist;
auto weighting = pow(pointRelativeOffset, logRatio);
if (!std::isfinite(weighting) || std::isnan(weighting)) {
continue;
}
auto interpolatedColor = RCTInterpolateColorInRange(weighting, inputRange, outputRange);
auto alpha = (interpolatedColor >> 24) & 0xFF;
auto red = (interpolatedColor >> 16) & 0xFF;
auto green = (interpolatedColor >> 8) & 0xFF;
auto blue = interpolatedColor & 0xFF;
newStop.color = colorFromRGBA(red, green, blue, alpha);
}
// Replace the color hint with new color stops
colorStops.erase(colorStops.begin() + x);
colorStops.insert(colorStops.begin() + x, newStops.begin(), newStops.end());
indexOffset += 8;
}
return colorStops;
}
@implementation RCTGradientUtils
// https://drafts.csswg.org/css-images-4/#color-stop-fixup
+ (std::vector<ProcessedColorStop>)getFixedColorStops:(const std::vector<ColorStop> &)colorStops
gradientLineLength:(CGFloat)gradientLineLength
{
if (colorStops.empty()) {
return {};
}
std::vector<ProcessedColorStop> fixedColorStops(colorStops.size());
bool hasNullPositions = false;
auto maxPositionSoFar = resolveColorStopPosition(colorStops[0].position, gradientLineLength);
if (!maxPositionSoFar.has_value()) {
maxPositionSoFar = 0.0f;
}
for (size_t i = 0; i < colorStops.size(); i++) {
const auto &colorStop = colorStops[i];
auto newPosition = resolveColorStopPosition(colorStop.position, gradientLineLength);
if (!newPosition.has_value()) {
// Step 1:
// If the first color stop does not have a position,
// set its position to 0%. If the last color stop does not have a position,
// set its position to 100%.
if (i == 0) {
newPosition = 0.0f;
} else if (i == colorStops.size() - 1) {
newPosition = 1.0f;
}
}
// Step 2:
// If a color stop or transition hint has a position
// that is less than the specified position of any color stop or transition hint
// before it in the list, set its position to be equal to the
// largest specified position of any color stop or transition hint before it.
if (newPosition.has_value()) {
newPosition = std::max(newPosition.value(), maxPositionSoFar.value());
fixedColorStops[i] = ProcessedColorStop{.color = colorStop.color, .position = newPosition};
maxPositionSoFar = newPosition;
} else {
hasNullPositions = true;
}
}
// Step 3:
// If any color stop still does not have a position,
// then, for each run of adjacent color stops without positions,
// set their positions so that they are evenly spaced between the preceding and
// following color stops with positions.
if (hasNullPositions) {
size_t lastDefinedIndex = 0;
for (size_t i = 1; i < fixedColorStops.size(); i++) {
auto endPosition = fixedColorStops[i].position;
if (endPosition.has_value()) {
size_t unpositionedStops = i - lastDefinedIndex - 1;
if (unpositionedStops > 0) {
auto startPosition = fixedColorStops[lastDefinedIndex].position;
if (startPosition.has_value()) {
auto increment = (endPosition.value() - startPosition.value()) / (unpositionedStops + 1);
for (size_t j = 1; j <= unpositionedStops; j++) {
fixedColorStops[lastDefinedIndex + j] = ProcessedColorStop{
.color = colorStops[lastDefinedIndex + j].color, .position = startPosition.value() + increment * j};
}
}
}
lastDefinedIndex = i;
}
}
}
return processColorTransitionHints(fixedColorStops);
}
// CAGradientLayer linear gradient squishes the non-square gradient to square gradient.
// This function fixes the "squished" effect.
// See https://stackoverflow.com/a/43176174 for more information.
+ (std::pair<CGPoint, CGPoint>)pointsForCAGradientLayerLinearGradient:(CGPoint)startPoint
endPoint:(CGPoint)endPoint
bounds:(CGSize)bounds
{
if (floatEquality(startPoint.x, endPoint.x) || floatEquality(startPoint.y, endPoint.y)) {
// Apple's implementation of horizontal and vertical gradients works just fine
return {startPoint, endPoint};
}
LineSegment startEnd(startPoint, endPoint);
LineSegment ab = startEnd.multiplied({bounds.width, bounds.height});
const CGPoint a = ab.p1;
const CGPoint b = ab.p2;
LineSegment cd = ab.perpendicularBisector();
CGSize multipliers = calculateMultipliers(bounds);
LineSegment lineSegmentCD = cd.multiplied(multipliers);
LineSegment lineSegmentEF = lineSegmentCD.perpendicularBisector();
LineSegment ef = lineSegmentEF.divided(multipliers);
Line efLine = ef.toLine();
Line aParallelLine(cd.getSlope(), a);
Line bParallelLine(cd.getSlope(), b);
std::optional<CGPoint> g_opt = efLine.intersection(aParallelLine);
std::optional<CGPoint> h_opt = efLine.intersection(bParallelLine);
if (g_opt && h_opt) {
LineSegment gh(*g_opt, *h_opt);
LineSegment result = gh.divided({bounds.width, bounds.height});
return {result.p1, result.p2};
}
return {startPoint, endPoint};
}
+ (void)getColors:(NSMutableArray<id> *)colors
andLocations:(NSMutableArray<NSNumber *> *)locations
fromColorStops:(const std::vector<facebook::react::ProcessedColorStop> &)colorStops
{
// iOS's CAGradientLayer interpolates colors in a way that can cause unexpected results.
// For example, a gradient from a color to `transparent` (which is transparent black) will
// fade the color's RGB components to black, creating a "muddy" or dark appearance.
// To fix this, we detect when a color stop is transparent black and replace it with
// a transparent version of the *previous* color stop. This creates a smooth fade-out effect
// by only interpolating the alpha channel, matching web and Android behavior.
UIColor *lastColor = nil;
for (const auto &colorStop : colorStops) {
UIColor *currentColor = RCTUIColorFromSharedColor(colorStop.color);
CGFloat red = 0.0;
CGFloat green = 0.0;
CGFloat blue = 0.0;
CGFloat alpha = 0.0;
[currentColor getRed:&red green:&green blue:&blue alpha:&alpha];
BOOL isTransparentBlack = alpha == 0.0 && red == 0.0 && green == 0.0 && blue == 0.0;
if (isTransparentBlack && (lastColor != nullptr)) {
[colors addObject:(id)[lastColor colorWithAlphaComponent:0.0].CGColor];
} else {
[colors addObject:(id)currentColor.CGColor];
}
if (!isTransparentBlack) {
lastColor = currentColor;
}
[locations addObject:@(std::max(std::min(colorStop.position.value(), 1.0), 0.0))];
}
}
@end

View File

@@ -0,0 +1,45 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <bitset>
namespace facebook::react {
template <size_t size>
class RCTIdentifierPool {
public:
void enqueue(int index)
{
usage[index] = false;
}
int dequeue()
{
while (true) {
if (!usage[lastIndex]) {
usage[lastIndex] = true;
return lastIndex;
}
lastIndex = (lastIndex + 1) % size;
}
}
void reset()
{
for (int i = 0; i < size; i++) {
usage[i] = false;
}
}
private:
std::bitset<size> usage;
int lastIndex;
};
} // namespace facebook::react

View File

@@ -0,0 +1,19 @@
/*
* Copyright (c) Meta Platforms, Inc. and 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>
#import <react/renderer/components/view/ViewProps.h>
NS_ASSUME_NONNULL_BEGIN
@interface RCTLinearGradient : NSObject
+ (CALayer *)gradientLayerWithSize:(CGSize)size gradient:(const facebook::react::LinearGradient &)gradient;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,138 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTLinearGradient.h"
#import <React/RCTAnimationUtils.h>
#import <React/RCTConversions.h>
#include <react/renderer/graphics/ValueUnit.h>
#import <react/utils/FloatComparison.h>
#import "RCTGradientUtils.h"
using namespace facebook::react;
@implementation RCTLinearGradient
+ (CALayer *)gradientLayerWithSize:(CGSize)size gradient:(const LinearGradient &)gradient
{
const auto &direction = gradient.direction;
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
CGPoint startPoint;
CGPoint endPoint;
if (direction.type == GradientDirectionType::Angle) {
CGFloat angle = std::get<Float>(direction.value);
std::tie(startPoint, endPoint) = getPointsFromAngle(angle, size);
} else if (direction.type == GradientDirectionType::Keyword) {
auto keyword = std::get<GradientKeyword>(direction.value);
CGFloat angle = getAngleForKeyword(keyword, size);
std::tie(startPoint, endPoint) = getPointsFromAngle(angle, size);
} else {
// Default to top-to-bottom gradient
CGFloat centerX = size.width / 2;
startPoint = CGPointMake(centerX, 0.0);
endPoint = CGPointMake(centerX, size.height);
}
CGFloat dx = endPoint.x - startPoint.x;
CGFloat dy = endPoint.y - startPoint.y;
CGFloat gradientLineLength = sqrt(dx * dx + dy * dy);
const auto colorStops = [RCTGradientUtils getFixedColorStops:gradient.colorStops
gradientLineLength:gradientLineLength];
NSMutableArray<id> *colors = [NSMutableArray array];
NSMutableArray<NSNumber *> *locations = [NSMutableArray array];
CGPoint relativeStartPoint = CGPointMake(startPoint.x / size.width, startPoint.y / size.height);
CGPoint relativeEndPoint = CGPointMake(endPoint.x / size.width, endPoint.y / size.height);
CGPoint fixedStartPoint;
CGPoint fixedEndPoint;
std::tie(fixedStartPoint, fixedEndPoint) = [RCTGradientUtils pointsForCAGradientLayerLinearGradient:relativeStartPoint
endPoint:relativeEndPoint
bounds:size];
gradientLayer.startPoint = fixedStartPoint;
gradientLayer.endPoint = fixedEndPoint;
[RCTGradientUtils getColors:colors andLocations:locations fromColorStops:colorStops];
gradientLayer.frame = CGRectMake(0.0f, 0.0f, size.width, size.height);
gradientLayer.colors = colors;
gradientLayer.locations = locations;
return gradientLayer;
}
// Spec: https://www.w3.org/TR/css-images-3/#linear-gradient-syntax
// Reference:
// https://github.com/chromium/chromium/blob/d32abbe13f5d52be7127fe25d5b778498165fab8/third_party/blink/renderer/core/css/css_gradient_value.cc#L1057
static std::pair<CGPoint, CGPoint> getPointsFromAngle(CGFloat angle, CGSize size)
{
angle = fmod(angle, 360.0);
if (angle < 0) {
angle += 360.0;
}
if (angle == 0.0) {
return {CGPointMake(0, size.height), CGPointMake(0, 0)};
}
if (angle == 90.0) {
return {CGPointMake(0, 0), CGPointMake(size.width, 0)};
}
if (angle == 180.0) {
return {CGPointMake(0, 0), CGPointMake(0, size.height)};
}
if (angle == 270.0) {
return {CGPointMake(size.width, 0), CGPointMake(0, 0)};
}
CGFloat radians = (90 - angle) * M_PI / 180.0;
CGFloat slope = tan(radians);
CGFloat perpendicularSlope = -1 / slope;
CGFloat halfHeight = size.height / 2;
CGFloat halfWidth = size.width / 2;
CGPoint endCorner;
if (angle < 90) {
endCorner = CGPointMake(halfWidth, halfHeight);
} else if (angle < 180) {
endCorner = CGPointMake(halfWidth, -halfHeight);
} else if (angle < 270) {
endCorner = CGPointMake(-halfWidth, -halfHeight);
} else {
endCorner = CGPointMake(-halfWidth, halfHeight);
}
CGFloat c = endCorner.y - perpendicularSlope * endCorner.x;
CGFloat endX = c / (slope - perpendicularSlope);
CGFloat endY = perpendicularSlope * endX + c;
return {CGPointMake(halfWidth - endX, halfHeight + endY), CGPointMake(halfWidth + endX, halfHeight - endY)};
}
// Spec: https://www.w3.org/TR/css-images-3/#linear-gradient-syntax
// Refer `using keywords` section
static CGFloat getAngleForKeyword(GradientKeyword keyword, CGSize size)
{
switch (keyword) {
case GradientKeyword::ToTopRight: {
CGFloat angleDeg = atan(size.width / size.height) * 180.0 / M_PI;
return 90.0 - angleDeg;
}
case GradientKeyword::ToBottomRight:
return atan(size.width / size.height) * 180.0 / M_PI + 90.0;
case GradientKeyword::ToTopLeft:
return atan(size.width / size.height) * 180.0 / M_PI + 270.0;
case GradientKeyword::ToBottomLeft:
return atan(size.height / size.width) * 180.0 / M_PI + 180.0;
default:
return 180.0;
}
}
@end

View File

@@ -0,0 +1,19 @@
/*
* Copyright (c) Meta Platforms, Inc. and 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>
#import <react/renderer/components/view/ViewProps.h>
NS_ASSUME_NONNULL_BEGIN
@interface RCTRadialGradient : NSObject
+ (CALayer *)gradientLayerWithSize:(CGSize)size gradient:(const facebook::react::RadialGradient &)gradient;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,197 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTRadialGradient.h"
#import <React/RCTAnimationUtils.h>
#import <React/RCTConversions.h>
#include <react/renderer/graphics/ValueUnit.h>
#import <react/utils/FloatComparison.h>
#include <cmath>
#import "RCTGradientUtils.h"
using namespace facebook::react;
namespace {
using RadiusVector = std::pair<CGFloat, CGFloat>;
RadiusVector RadiusToSide(
CGFloat centerX,
CGFloat centerY,
CGFloat width,
CGFloat height,
bool isCircle,
RadialGradientSize::SizeKeyword size)
{
CGFloat radiusXFromLeftSide = centerX;
CGFloat radiusYFromTopSide = centerY;
CGFloat radiusXFromRightSide = width - centerX;
CGFloat radiusYFromBottomSide = height - centerY;
CGFloat radiusX = NAN;
CGFloat radiusY = NAN;
if (size == RadialGradientSize::SizeKeyword::ClosestSide) {
radiusX = std::min(radiusXFromLeftSide, radiusXFromRightSide);
radiusY = std::min(radiusYFromTopSide, radiusYFromBottomSide);
} else {
radiusX = std::max(radiusXFromLeftSide, radiusXFromRightSide);
radiusY = std::max(radiusYFromTopSide, radiusYFromBottomSide);
}
if (isCircle) {
CGFloat radius = NAN;
if (size == RadialGradientSize::SizeKeyword::ClosestSide) {
radius = std::min(radiusX, radiusY);
} else {
radius = std::max(radiusX, radiusY);
}
return {radius, radius};
}
return {radiusX, radiusY};
}
RadiusVector EllipseRadius(CGFloat offsetX, CGFloat offsetY, CGFloat aspectRatio)
{
if (aspectRatio == 0 || std::isinf(aspectRatio) || std::isnan(aspectRatio)) {
return {0, 0};
}
// Ellipse that passes through a point formula: (x-h)^2/a^2 + (y-k)^2/b^2 = 1
// a = semi major axis length
// b = semi minor axis length = a / aspectRatio
// x - h = offsetX
// y - k = offsetY
CGFloat a = std::sqrt(offsetX * offsetX + offsetY * offsetY * aspectRatio * aspectRatio);
return {a, a / aspectRatio};
}
RadiusVector RadiusToCorner(
CGFloat centerX,
CGFloat centerY,
CGFloat width,
CGFloat height,
bool isCircle,
RadialGradientSize::SizeKeyword keyword)
{
std::array<CGPoint, 4> corners = {
{{.x = 0, .y = 0}, {.x = width, .y = 0}, {.x = width, .y = height}, {.x = 0, .y = height}}};
size_t cornerIndex = 0;
CGFloat distance = hypot(centerX - corners[cornerIndex].x, centerY - corners[cornerIndex].y);
bool isClosestCorner = keyword == RadialGradientSize::SizeKeyword::ClosestCorner;
for (size_t i = 1; i < corners.size(); ++i) {
CGFloat newDistance = hypot(centerX - corners[i].x, centerY - corners[i].y);
if (isClosestCorner) {
if (newDistance < distance) {
distance = newDistance;
cornerIndex = i;
}
} else {
if (newDistance > distance) {
distance = newDistance;
cornerIndex = i;
}
}
}
if (isCircle) {
return {distance, distance};
}
// https://www.w3.org/TR/css-images-3/#typedef-radial-size
// Aspect ratio of corner size ellipse is same as the respective side size ellipse
const RadiusVector sideRadius = RadiusToSide(
centerX,
centerY,
width,
height,
false,
isClosestCorner ? RadialGradientSize::SizeKeyword::ClosestSide : RadialGradientSize::SizeKeyword::FarthestSide);
return EllipseRadius(
corners[cornerIndex].x - centerX, corners[cornerIndex].y - centerY, sideRadius.first / sideRadius.second);
}
RadiusVector GetRadialGradientRadius(
bool isCircle,
const RadialGradientSize &size,
CGFloat centerX,
CGFloat centerY,
CGFloat width,
CGFloat height)
{
if (std::holds_alternative<RadialGradientSize::Dimensions>(size.value)) {
const auto &dimensions = std::get<RadialGradientSize::Dimensions>(size.value);
CGFloat radiusX = dimensions.x.resolve(static_cast<float>(width));
CGFloat radiusY = dimensions.y.resolve(static_cast<float>(height));
if (isCircle) {
CGFloat radius = std::max(radiusX, radiusY);
return {radius, radius};
}
return {radiusX, radiusY};
}
if (std::holds_alternative<RadialGradientSize::SizeKeyword>(size.value)) {
const auto &keyword = std::get<RadialGradientSize::SizeKeyword>(size.value);
if (keyword == RadialGradientSize::SizeKeyword::ClosestSide ||
keyword == RadialGradientSize::SizeKeyword::FarthestSide) {
return RadiusToSide(centerX, centerY, width, height, isCircle, keyword);
}
if (keyword == RadialGradientSize::SizeKeyword::ClosestCorner) {
return RadiusToCorner(centerX, centerY, width, height, isCircle, keyword);
}
}
// defaults to farthest corner
return RadiusToCorner(centerX, centerY, width, height, isCircle, RadialGradientSize::SizeKeyword::FarthestCorner);
}
} // namespace
@implementation RCTRadialGradient
+ (CALayer *)gradientLayerWithSize:(CGSize)size gradient:(const RadialGradient &)gradient
{
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.type = kCAGradientLayerRadial;
CGPoint centerPoint = CGPointMake(size.width / 2.0, size.height / 2.0);
if (gradient.position.top) {
centerPoint.y = gradient.position.top->resolve(static_cast<float>(size.height));
} else if (gradient.position.bottom) {
centerPoint.y = size.height - gradient.position.bottom->resolve(static_cast<float>(size.height));
}
if (gradient.position.left) {
centerPoint.x = gradient.position.left->resolve(static_cast<float>(size.width));
} else if (gradient.position.right) {
centerPoint.x = size.width - gradient.position.right->resolve(static_cast<float>(size.width));
}
bool isCircle = (gradient.shape == RadialGradientShape::Circle);
auto [radiusX, radiusY] =
GetRadialGradientRadius(isCircle, gradient.size, centerPoint.x, centerPoint.y, size.width, size.height);
const auto gradientLineLength = std::max(radiusX, radiusY);
const auto colorStops = [RCTGradientUtils getFixedColorStops:gradient.colorStops
gradientLineLength:gradientLineLength];
gradientLayer.startPoint = CGPointMake(centerPoint.x / size.width, centerPoint.y / size.height);
// endpoint.x is horizontal length and endpoint.y is vertical length
gradientLayer.endPoint = CGPointMake(
gradientLayer.startPoint.x + radiusX / size.width, gradientLayer.startPoint.y + radiusY / size.height);
NSMutableArray<id> *colors = [NSMutableArray array];
NSMutableArray<NSNumber *> *locations = [NSMutableArray array];
[RCTGradientUtils getColors:colors andLocations:locations fromColorStops:colorStops];
gradientLayer.frame = CGRectMake(0.0f, 0.0f, size.width, size.height);
gradientLayer.colors = colors;
gradientLayer.locations = locations;
return gradientLayer;
}
@end

View File

@@ -0,0 +1,34 @@
/*
* Copyright (c) Meta Platforms, Inc. and 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>
NS_ASSUME_NONNULL_BEGIN
/**
* Lightweight wrapper class around a UIView with a react tag which registers a
* constant react tag at initialization time for a stable hash and provides the
* udnerlying view to a caller if that underlying view's react tag has not
* changed from the one provided at initialization time (i.e. recycled).
*/
@interface RCTReactTaggedView : NSObject {
UIView *_view;
NSInteger _tag;
}
+ (RCTReactTaggedView *)wrap:(UIView *)view;
- (instancetype)initWithView:(UIView *)view;
- (nullable UIView *)view;
- (NSInteger)tag;
- (BOOL)isEqual:(id)other;
- (NSUInteger)hash;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,55 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTReactTaggedView.h"
@implementation RCTReactTaggedView
+ (RCTReactTaggedView *)wrap:(UIView *)view
{
return [[RCTReactTaggedView alloc] initWithView:view];
}
- (instancetype)initWithView:(UIView *)view
{
if (self = [super init]) {
_view = view;
_tag = view.tag;
}
return self;
}
- (nullable UIView *)view
{
if (_view.tag == _tag) {
return _view;
}
return nil;
}
- (NSInteger)tag
{
return _tag;
}
- (BOOL)isEqual:(id)other
{
if (other == self) {
return YES;
}
if (!other || ![other isKindOfClass:[self class]]) {
return NO;
}
return _tag == [other tag];
}
- (NSUInteger)hash
{
return _tag;
}
@end