RCTConvert+ART.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import "RCTConvert+ART.h"
  8. #import <React/RCTFont.h>
  9. #import <React/RCTLog.h>
  10. #import <React/ARTLinearGradient.h>
  11. #import <React/ARTPattern.h>
  12. #import <React/ARTRadialGradient.h>
  13. #import <React/ARTSolidColor.h>
  14. @implementation RCTConvert (ART)
  15. + (CGPathRef)CGPath:(id)json
  16. {
  17. NSArray *arr = [self NSNumberArray:json];
  18. NSUInteger count = [arr count];
  19. #define NEXT_VALUE [self double:arr[i++]]
  20. CGMutablePathRef path = CGPathCreateMutable();
  21. CGPathMoveToPoint(path, NULL, 0, 0);
  22. @try {
  23. NSUInteger i = 0;
  24. while (i < count) {
  25. NSUInteger type = [arr[i++] unsignedIntegerValue];
  26. switch (type) {
  27. case 0:
  28. CGPathMoveToPoint(path, NULL, NEXT_VALUE, NEXT_VALUE);
  29. break;
  30. case 1:
  31. CGPathCloseSubpath(path);
  32. break;
  33. case 2:
  34. CGPathAddLineToPoint(path, NULL, NEXT_VALUE, NEXT_VALUE);
  35. break;
  36. case 3:
  37. CGPathAddCurveToPoint(path, NULL, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE);
  38. break;
  39. case 4:
  40. CGPathAddArc(path, NULL, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE == 0);
  41. break;
  42. default:
  43. RCTLogError(@"Invalid CGPath type %llu at element %llu of %@", (unsigned long long)type, (unsigned long long)i, arr);
  44. CGPathRelease(path);
  45. return NULL;
  46. }
  47. }
  48. }
  49. @catch (NSException *exception) {
  50. RCTLogError(@"Invalid CGPath format: %@", arr);
  51. CGPathRelease(path);
  52. return NULL;
  53. }
  54. return (CGPathRef)CFAutorelease(path);
  55. }
  56. RCT_ENUM_CONVERTER(CTTextAlignment, (@{
  57. @"auto": @(kCTTextAlignmentNatural),
  58. @"left": @(kCTTextAlignmentLeft),
  59. @"center": @(kCTTextAlignmentCenter),
  60. @"right": @(kCTTextAlignmentRight),
  61. @"justify": @(kCTTextAlignmentJustified),
  62. }), kCTTextAlignmentNatural, integerValue)
  63. // This takes a tuple of text lines and a font to generate a CTLine for each text line.
  64. // This prepares everything for rendering a frame of text in ARTText.
  65. + (ARTTextFrame)ARTTextFrame:(id)json
  66. {
  67. NSDictionary *dict = [self NSDictionary:json];
  68. ARTTextFrame frame;
  69. frame.count = 0;
  70. NSArray *lines = [self NSArray:dict[@"lines"]];
  71. NSUInteger lineCount = [lines count];
  72. if (lineCount == 0) {
  73. return frame;
  74. }
  75. CTFontRef font = (__bridge CTFontRef)[self UIFont:dict[@"font"]];
  76. if (!font) {
  77. return frame;
  78. }
  79. // Create a dictionary for this font
  80. CFDictionaryRef attributes = (__bridge CFDictionaryRef)@{
  81. (NSString *)kCTFontAttributeName:(__bridge id)font,
  82. (NSString *)kCTForegroundColorFromContextAttributeName: @YES
  83. };
  84. // Set up text frame with font metrics
  85. CGFloat size = CTFontGetSize(font);
  86. frame.count = lineCount;
  87. frame.baseLine = size; // estimate base line
  88. frame.lineHeight = size * 1.1; // Base on ART canvas line height estimate
  89. frame.lines = malloc(sizeof(CTLineRef) * lineCount);
  90. frame.widths = malloc(sizeof(CGFloat) * lineCount);
  91. [lines enumerateObjectsUsingBlock:^(NSString *text, NSUInteger i, BOOL *stop) {
  92. CFStringRef string = (__bridge CFStringRef)text;
  93. CFAttributedStringRef attrString = CFAttributedStringCreate(kCFAllocatorDefault, string, attributes);
  94. CTLineRef line = CTLineCreateWithAttributedString(attrString);
  95. CFRelease(attrString);
  96. frame.lines[i] = line;
  97. frame.widths[i] = CTLineGetTypographicBounds(line, NULL, NULL, NULL);
  98. }];
  99. return frame;
  100. }
  101. + (ARTCGFloatArray)ARTCGFloatArray:(id)json
  102. {
  103. NSArray *arr = [self NSNumberArray:json];
  104. NSUInteger count = arr.count;
  105. ARTCGFloatArray array;
  106. array.count = count;
  107. array.array = NULL;
  108. if (count) {
  109. // Ideally, these arrays should already use the same memory layout.
  110. // In that case we shouldn't need this new malloc.
  111. array.array = malloc(sizeof(CGFloat) * count);
  112. for (NSUInteger i = 0; i < count; i++) {
  113. array.array[i] = [arr[i] doubleValue];
  114. }
  115. }
  116. return array;
  117. }
  118. + (ARTBrush *)ARTBrush:(id)json
  119. {
  120. NSArray *arr = [self NSArray:json];
  121. NSUInteger type = [self NSUInteger:arr.firstObject];
  122. switch (type) {
  123. case 0: // solid color
  124. // These are probably expensive allocations since it's often the same value.
  125. // We should memoize colors but look ups may be just as expensive.
  126. return [[ARTSolidColor alloc] initWithArray:arr];
  127. case 1: // linear gradient
  128. return [[ARTLinearGradient alloc] initWithArray:arr];
  129. case 2: // radial gradient
  130. return [[ARTRadialGradient alloc] initWithArray:arr];
  131. case 3: // pattern
  132. return [[ARTPattern alloc] initWithArray:arr];
  133. default:
  134. RCTLogError(@"Unknown brush type: %llu", (unsigned long long)type);
  135. return nil;
  136. }
  137. }
  138. + (CGPoint)CGPoint:(id)json offset:(NSUInteger)offset
  139. {
  140. NSArray *arr = [self NSArray:json];
  141. if (arr.count < offset + 2) {
  142. RCTLogError(@"Too few elements in array (expected at least %llu): %@", (unsigned long long)(2 + offset), arr);
  143. return CGPointZero;
  144. }
  145. return (CGPoint){
  146. [self CGFloat:arr[offset]],
  147. [self CGFloat:arr[offset + 1]],
  148. };
  149. }
  150. + (CGRect)CGRect:(id)json offset:(NSUInteger)offset
  151. {
  152. NSArray *arr = [self NSArray:json];
  153. if (arr.count < offset + 4) {
  154. RCTLogError(@"Too few elements in array (expected at least %llu): %@", (unsigned long long)(4 + offset), arr);
  155. return CGRectZero;
  156. }
  157. return (CGRect){
  158. {[self CGFloat:arr[offset]], [self CGFloat:arr[offset + 1]]},
  159. {[self CGFloat:arr[offset + 2]], [self CGFloat:arr[offset + 3]]},
  160. };
  161. }
  162. + (CGColorRef)CGColor:(id)json offset:(NSUInteger)offset
  163. {
  164. NSArray *arr = [self NSArray:json];
  165. if (arr.count < offset + 4) {
  166. RCTLogError(@"Too few elements in array (expected at least %llu): %@", (unsigned long long)(4 + offset), arr);
  167. return NULL;
  168. }
  169. return [self CGColor:[arr subarrayWithRange:(NSRange){offset, 4}]];
  170. }
  171. + (CGGradientRef)CGGradient:(id)json offset:(NSUInteger)offset
  172. {
  173. NSArray *arr = [self NSArray:json];
  174. if (arr.count < offset) {
  175. RCTLogError(@"Too few elements in array (expected at least %llu): %@", (unsigned long long)offset, arr);
  176. return NULL;
  177. }
  178. arr = [arr subarrayWithRange:(NSRange){offset, arr.count - offset}];
  179. ARTCGFloatArray colorsAndOffsets = [self ARTCGFloatArray:arr];
  180. size_t stops = colorsAndOffsets.count / 5;
  181. CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
  182. CGGradientRef gradient = CGGradientCreateWithColorComponents(
  183. rgb,
  184. colorsAndOffsets.array,
  185. colorsAndOffsets.array + stops * 4,
  186. stops
  187. );
  188. CGColorSpaceRelease(rgb);
  189. free(colorsAndOffsets.array);
  190. return (CGGradientRef)CFAutorelease(gradient);
  191. }
  192. @end