2019-10-16 20:03:47 +03:00
|
|
|
/*
|
2018-09-12 01:27:47 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2015-04-30 01:28:18 +03:00
|
|
|
*
|
2018-02-17 05:24:55 +03:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-04-30 01:28:18 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTConvert+ART.h"
|
|
|
|
|
2016-12-05 21:28:37 +03:00
|
|
|
#import <React/RCTFont.h>
|
|
|
|
#import <React/RCTLog.h>
|
|
|
|
|
CocoaPods frameworks compatibility: Step 2 (#25619)
Summary:
This is my proposal for fixing `use_frameworks!` compatibility without breaking all `<React/*>` imports I outlined in https://github.com/facebook/react-native/pull/25393#issuecomment-508457700. If accepted, it will fix https://github.com/facebook/react-native/issues/25349.
It builds on the changes I made in https://github.com/facebook/react-native/pull/25496 by ensuring each podspec has a unique value for `header_dir` so that framework imports do not conflict. Every podspec which should be included in the `<React/*>` namespace now includes it's headers from `React-Core.podspec`.
The following pods can still be imported with `<React/*>` and so should not have breaking changes: `React-ART`,`React-DevSupport`, `React-CoreModules`, `React-RCTActionSheet`, `React-RCTAnimation`, `React-RCTBlob`, `React-RCTImage`, `React-RCTLinking`, `React-RCTNetwork`, `React-RCTPushNotification`, `React-RCTSettings`, `React-RCTText`, `React-RCTSettings`, `React-RCTVibration`, `React-RCTWebSocket` .
There are still a few breaking changes which I hope will be acceptable:
- `React-Core.podspec` has been moved to the root of the project. Any `Podfile` that references it will need to update the path.
- ~~`React-turbomodule-core`'s headers now live under `<turbomodule/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823.
- ~~`React-turbomodulesamples`'s headers now live under `<turbomodulesamples/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823.
- ~~`React-TypeSaferty`'s headers now live under `<TypeSafety/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511040967.
- ~~`React-jscallinvoker`'s headers now live under `<jscallinvoker/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823.
- Each podspec now uses `s.static_framework = true`. This means that a minimum of CocoaPods 1.5 ([released in April 2018](http://blog.cocoapods.org/CocoaPods-1.5.0/)) is now required. This is needed so that the ` __has_include` conditions can still work when frameworks are enabled.
Still to do:
- ~~Including `React-turbomodule-core` with `use_frameworks!` enabled causes the C++ import failures we saw in https://github.com/facebook/react-native/issues/25349. I'm sure it will be possible to fix this but I need to dig deeper (perhaps a custom modulemap would be needed).~~ Addressed by https://github.com/facebook/react-native/pull/25619/commits/33573511f02f3502a28bad48e085e9a4b8608302.
- I haven't got Fabric working yet. I wonder if it would be acceptable to move Fabric out of the `<React/*>` namespace since it is new? �
## Changelog
[iOS] [Fixed] - Fixed compatibility with CocoaPods frameworks.
Pull Request resolved: https://github.com/facebook/react-native/pull/25619
Test Plan:
### FB
```
buck build catalyst
```
### Sample Project
Everything should work exactly as before, where `use_frameworks!` is not in `Podfile`s. I have a branch on my [sample project](https://github.com/jtreanor/react-native-cocoapods-frameworks) here which has `use_frameworks!` in its `Podfile` to demonstrate this is fixed.
You can see that it works with these steps:
1. `git clone git@github.com:jtreanor/react-native-cocoapods-frameworks.git`
2. `git checkout fix-frameworks-subspecs`
3. `cd ios && pod install`
4. `cd .. && react-native run-ios`
The sample app will build and run successfully. To see that it still works without frameworks, remove `use_frameworks!` from the `Podfile` and do steps 3 and 4 again.
### RNTesterPods
`RNTesterPodsPods` can now work with or without `use_frameworks!`.
1. Go to the `RNTester` directory and run `pod install`.
2. Run the tests in `RNTesterPods.xcworkspace` to see that everything still works fine.
3. Uncomment the `use_frameworks!` line at the top of `RNTester/Podfile` and run `pod install` again.
4. Run the tests again and see that it still works with frameworks enabled.
Reviewed By: PeteTheHeat
Differential Revision: D16465247
Pulled By: PeteTheHeat
fbshipit-source-id: cad837e9cced06d30cc5b372af1c65c7780b9e7a
2019-07-25 08:26:42 +03:00
|
|
|
#import <React/ARTLinearGradient.h>
|
|
|
|
#import <React/ARTPattern.h>
|
|
|
|
#import <React/ARTRadialGradient.h>
|
|
|
|
#import <React/ARTSolidColor.h>
|
2015-04-30 01:28:18 +03:00
|
|
|
|
|
|
|
@implementation RCTConvert (ART)
|
|
|
|
|
|
|
|
+ (CGPathRef)CGPath:(id)json
|
|
|
|
{
|
|
|
|
NSArray *arr = [self NSNumberArray:json];
|
|
|
|
|
|
|
|
NSUInteger count = [arr count];
|
|
|
|
|
|
|
|
#define NEXT_VALUE [self double:arr[i++]]
|
|
|
|
|
|
|
|
CGMutablePathRef path = CGPathCreateMutable();
|
|
|
|
CGPathMoveToPoint(path, NULL, 0, 0);
|
|
|
|
|
|
|
|
@try {
|
|
|
|
NSUInteger i = 0;
|
|
|
|
while (i < count) {
|
|
|
|
NSUInteger type = [arr[i++] unsignedIntegerValue];
|
|
|
|
switch (type) {
|
|
|
|
case 0:
|
|
|
|
CGPathMoveToPoint(path, NULL, NEXT_VALUE, NEXT_VALUE);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
CGPathCloseSubpath(path);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
CGPathAddLineToPoint(path, NULL, NEXT_VALUE, NEXT_VALUE);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
CGPathAddCurveToPoint(path, NULL, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
CGPathAddArc(path, NULL, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE == 0);
|
|
|
|
break;
|
|
|
|
default:
|
2017-09-25 20:23:02 +03:00
|
|
|
RCTLogError(@"Invalid CGPath type %llu at element %llu of %@", (unsigned long long)type, (unsigned long long)i, arr);
|
2015-04-30 01:28:18 +03:00
|
|
|
CGPathRelease(path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@catch (NSException *exception) {
|
|
|
|
RCTLogError(@"Invalid CGPath format: %@", arr);
|
|
|
|
CGPathRelease(path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (CGPathRef)CFAutorelease(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_ENUM_CONVERTER(CTTextAlignment, (@{
|
|
|
|
@"auto": @(kCTTextAlignmentNatural),
|
|
|
|
@"left": @(kCTTextAlignmentLeft),
|
|
|
|
@"center": @(kCTTextAlignmentCenter),
|
|
|
|
@"right": @(kCTTextAlignmentRight),
|
|
|
|
@"justify": @(kCTTextAlignmentJustified),
|
|
|
|
}), kCTTextAlignmentNatural, integerValue)
|
|
|
|
|
|
|
|
// This takes a tuple of text lines and a font to generate a CTLine for each text line.
|
|
|
|
// This prepares everything for rendering a frame of text in ARTText.
|
|
|
|
+ (ARTTextFrame)ARTTextFrame:(id)json
|
|
|
|
{
|
|
|
|
NSDictionary *dict = [self NSDictionary:json];
|
|
|
|
ARTTextFrame frame;
|
|
|
|
frame.count = 0;
|
|
|
|
|
|
|
|
NSArray *lines = [self NSArray:dict[@"lines"]];
|
|
|
|
NSUInteger lineCount = [lines count];
|
|
|
|
if (lineCount == 0) {
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2016-08-05 22:29:19 +03:00
|
|
|
CTFontRef font = (__bridge CTFontRef)[self UIFont:dict[@"font"]];
|
2015-04-30 01:28:18 +03:00
|
|
|
if (!font) {
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a dictionary for this font
|
|
|
|
CFDictionaryRef attributes = (__bridge CFDictionaryRef)@{
|
2016-08-05 22:29:19 +03:00
|
|
|
(NSString *)kCTFontAttributeName:(__bridge id)font,
|
2015-04-30 01:28:18 +03:00
|
|
|
(NSString *)kCTForegroundColorFromContextAttributeName: @YES
|
|
|
|
};
|
|
|
|
|
|
|
|
// Set up text frame with font metrics
|
|
|
|
CGFloat size = CTFontGetSize(font);
|
|
|
|
frame.count = lineCount;
|
|
|
|
frame.baseLine = size; // estimate base line
|
|
|
|
frame.lineHeight = size * 1.1; // Base on ART canvas line height estimate
|
|
|
|
frame.lines = malloc(sizeof(CTLineRef) * lineCount);
|
|
|
|
frame.widths = malloc(sizeof(CGFloat) * lineCount);
|
|
|
|
|
|
|
|
[lines enumerateObjectsUsingBlock:^(NSString *text, NSUInteger i, BOOL *stop) {
|
|
|
|
|
|
|
|
CFStringRef string = (__bridge CFStringRef)text;
|
|
|
|
CFAttributedStringRef attrString = CFAttributedStringCreate(kCFAllocatorDefault, string, attributes);
|
|
|
|
CTLineRef line = CTLineCreateWithAttributedString(attrString);
|
|
|
|
CFRelease(attrString);
|
|
|
|
|
|
|
|
frame.lines[i] = line;
|
|
|
|
frame.widths[i] = CTLineGetTypographicBounds(line, NULL, NULL, NULL);
|
|
|
|
}];
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (ARTCGFloatArray)ARTCGFloatArray:(id)json
|
|
|
|
{
|
|
|
|
NSArray *arr = [self NSNumberArray:json];
|
|
|
|
NSUInteger count = arr.count;
|
|
|
|
|
|
|
|
ARTCGFloatArray array;
|
|
|
|
array.count = count;
|
|
|
|
array.array = NULL;
|
|
|
|
|
|
|
|
if (count) {
|
|
|
|
// Ideally, these arrays should already use the same memory layout.
|
|
|
|
// In that case we shouldn't need this new malloc.
|
|
|
|
array.array = malloc(sizeof(CGFloat) * count);
|
|
|
|
for (NSUInteger i = 0; i < count; i++) {
|
|
|
|
array.array[i] = [arr[i] doubleValue];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (ARTBrush *)ARTBrush:(id)json
|
|
|
|
{
|
|
|
|
NSArray *arr = [self NSArray:json];
|
2015-08-07 01:44:15 +03:00
|
|
|
NSUInteger type = [self NSUInteger:arr.firstObject];
|
2015-04-30 01:28:18 +03:00
|
|
|
switch (type) {
|
|
|
|
case 0: // solid color
|
|
|
|
// These are probably expensive allocations since it's often the same value.
|
|
|
|
// We should memoize colors but look ups may be just as expensive.
|
|
|
|
return [[ARTSolidColor alloc] initWithArray:arr];
|
|
|
|
case 1: // linear gradient
|
|
|
|
return [[ARTLinearGradient alloc] initWithArray:arr];
|
|
|
|
case 2: // radial gradient
|
|
|
|
return [[ARTRadialGradient alloc] initWithArray:arr];
|
|
|
|
case 3: // pattern
|
|
|
|
return [[ARTPattern alloc] initWithArray:arr];
|
|
|
|
default:
|
2017-09-25 20:23:02 +03:00
|
|
|
RCTLogError(@"Unknown brush type: %llu", (unsigned long long)type);
|
2015-04-30 01:28:18 +03:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (CGPoint)CGPoint:(id)json offset:(NSUInteger)offset
|
|
|
|
{
|
|
|
|
NSArray *arr = [self NSArray:json];
|
|
|
|
if (arr.count < offset + 2) {
|
2017-09-25 20:23:02 +03:00
|
|
|
RCTLogError(@"Too few elements in array (expected at least %llu): %@", (unsigned long long)(2 + offset), arr);
|
2015-04-30 01:28:18 +03:00
|
|
|
return CGPointZero;
|
|
|
|
}
|
|
|
|
return (CGPoint){
|
|
|
|
[self CGFloat:arr[offset]],
|
|
|
|
[self CGFloat:arr[offset + 1]],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (CGRect)CGRect:(id)json offset:(NSUInteger)offset
|
|
|
|
{
|
|
|
|
NSArray *arr = [self NSArray:json];
|
|
|
|
if (arr.count < offset + 4) {
|
2017-09-25 20:23:02 +03:00
|
|
|
RCTLogError(@"Too few elements in array (expected at least %llu): %@", (unsigned long long)(4 + offset), arr);
|
2015-04-30 01:28:18 +03:00
|
|
|
return CGRectZero;
|
|
|
|
}
|
|
|
|
return (CGRect){
|
|
|
|
{[self CGFloat:arr[offset]], [self CGFloat:arr[offset + 1]]},
|
|
|
|
{[self CGFloat:arr[offset + 2]], [self CGFloat:arr[offset + 3]]},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (CGColorRef)CGColor:(id)json offset:(NSUInteger)offset
|
|
|
|
{
|
|
|
|
NSArray *arr = [self NSArray:json];
|
|
|
|
if (arr.count < offset + 4) {
|
2017-09-25 20:23:02 +03:00
|
|
|
RCTLogError(@"Too few elements in array (expected at least %llu): %@", (unsigned long long)(4 + offset), arr);
|
2015-04-30 01:28:18 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return [self CGColor:[arr subarrayWithRange:(NSRange){offset, 4}]];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (CGGradientRef)CGGradient:(id)json offset:(NSUInteger)offset
|
|
|
|
{
|
|
|
|
NSArray *arr = [self NSArray:json];
|
|
|
|
if (arr.count < offset) {
|
2017-09-25 20:23:02 +03:00
|
|
|
RCTLogError(@"Too few elements in array (expected at least %llu): %@", (unsigned long long)offset, arr);
|
2015-04-30 01:28:18 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
arr = [arr subarrayWithRange:(NSRange){offset, arr.count - offset}];
|
|
|
|
ARTCGFloatArray colorsAndOffsets = [self ARTCGFloatArray:arr];
|
|
|
|
size_t stops = colorsAndOffsets.count / 5;
|
|
|
|
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
|
|
|
|
CGGradientRef gradient = CGGradientCreateWithColorComponents(
|
|
|
|
rgb,
|
|
|
|
colorsAndOffsets.array,
|
|
|
|
colorsAndOffsets.array + stops * 4,
|
|
|
|
stops
|
|
|
|
);
|
|
|
|
CGColorSpaceRelease(rgb);
|
|
|
|
free(colorsAndOffsets.array);
|
|
|
|
return (CGGradientRef)CFAutorelease(gradient);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|