mirror of
https://github.com/zoriya/react-native-svg.git
synced 2026-06-03 15:14:18 +00:00
refactor text render
Support G inherit props. Refactor text render. Text glyphs will perfectly draw along the path
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
+ (RNSVGCGFloatArray)RNSVGCGFloatArray:(id)json;
|
||||
+ (RNSVGBrush *)RNSVGBrush:(id)json;
|
||||
|
||||
+ (NSArray *)RNSVGBezier:(id)json;
|
||||
+ (CGPoint)CGPoint:(id)json offset:(NSUInteger)offset;
|
||||
+ (CGRect)CGRect:(id)json offset:(NSUInteger)offset;
|
||||
+ (CGColorRef)CGColor:(id)json offset:(NSUInteger)offset;
|
||||
|
||||
@@ -19,14 +19,14 @@
|
||||
+ (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) {
|
||||
@@ -44,9 +44,6 @@
|
||||
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:
|
||||
RCTLogError(@"Invalid CGPath type %zd at element %zd of %@", type, i, arr);
|
||||
CGPathRelease(path);
|
||||
@@ -59,7 +56,7 @@
|
||||
CGPathRelease(path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
return (CGPathRef)CFAutorelease(path);
|
||||
}
|
||||
|
||||
@@ -83,25 +80,25 @@ RCT_ENUM_CONVERTER(RNSVGCGFCRule, (@{
|
||||
NSDictionary *dict = [self NSDictionary:json];
|
||||
RNSVGTextFrame frame;
|
||||
frame.count = 0;
|
||||
|
||||
|
||||
NSArray *lines = [self NSArray:dict[@"lines"]];
|
||||
NSUInteger lineCount = [lines count];
|
||||
if (lineCount == 0) {
|
||||
return frame;
|
||||
}
|
||||
|
||||
|
||||
NSDictionary *fontDict = dict[@"font"];
|
||||
CTFontRef font = (__bridge CTFontRef)[self UIFont:nil withFamily:fontDict[@"fontFamily"] size:fontDict[@"fontSize"] weight:fontDict[@"fontWeight"] style:fontDict[@"fontStyle"] scaleMultiplier:1.0];
|
||||
if (!font) {
|
||||
return frame;
|
||||
}
|
||||
|
||||
|
||||
// Create a dictionary for this font
|
||||
CFDictionaryRef attributes = (__bridge CFDictionaryRef)@{
|
||||
(NSString *)kCTFontAttributeName: (__bridge id)font,
|
||||
(NSString *)kCTForegroundColorFromContextAttributeName: @YES
|
||||
};
|
||||
|
||||
|
||||
// Set up text frame with font metrics
|
||||
CGFloat size = CTFontGetSize(font);
|
||||
frame.count = lineCount;
|
||||
@@ -109,18 +106,18 @@ RCT_ENUM_CONVERTER(RNSVGCGFCRule, (@{
|
||||
frame.lineHeight = size * 1.1; // Base on RNSVG 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;
|
||||
}
|
||||
|
||||
@@ -128,11 +125,11 @@ RCT_ENUM_CONVERTER(RNSVGCGFCRule, (@{
|
||||
{
|
||||
NSArray *arr = [self NSNumberArray:json];
|
||||
NSUInteger count = arr.count;
|
||||
|
||||
|
||||
RNSVGCGFloatArray 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.
|
||||
@@ -141,7 +138,7 @@ RCT_ENUM_CONVERTER(RNSVGCGFCRule, (@{
|
||||
array.array[i] = [arr[i] doubleValue];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
@@ -149,7 +146,7 @@ RCT_ENUM_CONVERTER(RNSVGCGFCRule, (@{
|
||||
{
|
||||
NSArray *arr = [self NSArray:json];
|
||||
NSUInteger type = [self NSUInteger:arr.firstObject];
|
||||
|
||||
|
||||
switch (type) {
|
||||
case 0: // solid color
|
||||
// These are probably expensive allocations since it's often the same value.
|
||||
@@ -163,6 +160,63 @@ RCT_ENUM_CONVERTER(RNSVGCGFCRule, (@{
|
||||
}
|
||||
}
|
||||
|
||||
+ (NSArray *)RNSVGBezier:(id)json
|
||||
{
|
||||
NSArray *arr = [self NSNumberArray:json];
|
||||
|
||||
NSMutableArray<NSArray *> *beziers = [[NSMutableArray alloc] init];
|
||||
|
||||
NSUInteger count = [arr count];
|
||||
|
||||
#define NEXT_VALUE [self double:arr[i++]]
|
||||
@try {
|
||||
NSValue *startPoint = [NSValue valueWithCGPoint: CGPointMake(0, 0)];
|
||||
NSUInteger i = 0;
|
||||
while (i < count) {
|
||||
NSUInteger type = [arr[i++] unsignedIntegerValue];
|
||||
switch (type) {
|
||||
case 0:
|
||||
{
|
||||
startPoint = [NSValue valueWithCGPoint: CGPointMake(NEXT_VALUE, NEXT_VALUE)];
|
||||
[beziers addObject: @[startPoint]];
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
[beziers addObject: @[]];
|
||||
break;
|
||||
case 2:
|
||||
{
|
||||
double x = NEXT_VALUE;
|
||||
double y = NEXT_VALUE;
|
||||
NSValue * destination = [NSValue valueWithCGPoint:CGPointMake(x, y)];
|
||||
[beziers addObject: @[
|
||||
destination,
|
||||
startPoint,
|
||||
destination
|
||||
]];
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
[beziers addObject: @[
|
||||
[NSValue valueWithCGPoint:CGPointMake(NEXT_VALUE, NEXT_VALUE)],
|
||||
[NSValue valueWithCGPoint:CGPointMake(NEXT_VALUE, NEXT_VALUE)],
|
||||
[NSValue valueWithCGPoint:CGPointMake(NEXT_VALUE, NEXT_VALUE)],
|
||||
]];
|
||||
break;
|
||||
default:
|
||||
RCTLogError(@"Invalid RNSVGBezier type %zd at element %zd of %@", type, i, arr);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
@catch (NSException *exception) {
|
||||
RCTLogError(@"Invalid RNSVGBezier format: %@", arr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return beziers;
|
||||
}
|
||||
|
||||
+ (CGPoint)CGPoint:(id)json offset:(NSUInteger)offset
|
||||
{
|
||||
NSArray *arr = [self NSArray:json];
|
||||
|
||||
Reference in New Issue
Block a user