fix: native method signatures web compatibility / spec conformance

This commit is contained in:
Mikael Sand
2019-10-04 19:55:24 +03:00
parent c28499bcc6
commit 8687a3d1e9
5 changed files with 92 additions and 39 deletions
@@ -15,12 +15,14 @@ import android.graphics.PathMeasure;
import android.graphics.RectF; import android.graphics.RectF;
import android.graphics.Region; import android.graphics.Region;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.UiThreadUtil; import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.bridge.WritableMap;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@@ -115,7 +117,12 @@ class RNSVGRenderableManager extends ReactContextBaseJavaModule {
float[] pos = new float[2]; float[] pos = new float[2];
float[] tan = new float[2]; float[] tan = new float[2];
pm.getPosTan(Math.max(0, Math.min(length, pathLength)), pos, tan); pm.getPosTan(Math.max(0, Math.min(length, pathLength)), pos, tan);
successCallback.invoke(pos[0], pos[1]); double angle = Math.atan2(tan[1], tan[0]);
WritableMap result = Arguments.createMap();
result.putDouble("x", pos[0]);
result.putDouble("y", pos[1]);
result.putDouble("angle", angle);
successCallback.invoke(result);
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")
@@ -139,9 +146,17 @@ class RNSVGRenderableManager extends ReactContextBaseJavaModule {
bounds.union(svg.mMarkerBounds); bounds.union(svg.mMarkerBounds);
} }
if (clipped) { if (clipped) {
RectF clipBounds = svg.mClipBounds;
if (clipBounds != null) {
bounds.intersect(svg.mClipBounds); bounds.intersect(svg.mClipBounds);
} }
successCallback.invoke(bounds.left, bounds.top, bounds.width(), bounds.height()); }
WritableMap result = Arguments.createMap();
result.putDouble("x", bounds.left);
result.putDouble("y", bounds.top);
result.putDouble("width", bounds.width());
result.putDouble("height", bounds.height());
successCallback.invoke(result);
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")
@@ -155,10 +170,14 @@ class RNSVGRenderableManager extends ReactContextBaseJavaModule {
float[] values = new float[9]; float[] values = new float[9];
ctm.getValues(values); ctm.getValues(values);
successCallback.invoke( WritableMap result = Arguments.createMap();
values[Matrix.MSCALE_X], values[Matrix.MSKEW_X], values[Matrix.MTRANS_X], result.putDouble("a", values[Matrix.MSCALE_X]);
values[Matrix.MSKEW_Y], values[Matrix.MSCALE_Y], values[Matrix.MTRANS_Y] result.putDouble("b", values[Matrix.MSKEW_Y]);
); result.putDouble("c", values[Matrix.MSKEW_X]);
result.putDouble("d", values[Matrix.MSCALE_Y]);
result.putDouble("e", values[Matrix.MTRANS_X]);
result.putDouble("f", values[Matrix.MTRANS_Y]);
successCallback.invoke(result);
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")
@@ -168,9 +187,13 @@ class RNSVGRenderableManager extends ReactContextBaseJavaModule {
Matrix screenCTM = svg.mCTM; Matrix screenCTM = svg.mCTM;
float[] values = new float[9]; float[] values = new float[9];
screenCTM.getValues(values); screenCTM.getValues(values);
successCallback.invoke( WritableMap result = Arguments.createMap();
values[Matrix.MSCALE_X], values[Matrix.MSKEW_X], values[Matrix.MTRANS_X], result.putDouble("a", values[Matrix.MSCALE_X]);
values[Matrix.MSKEW_Y], values[Matrix.MSCALE_Y], values[Matrix.MTRANS_Y] result.putDouble("b", values[Matrix.MSKEW_Y]);
); result.putDouble("c", values[Matrix.MSKEW_X]);
result.putDouble("d", values[Matrix.MSCALE_Y]);
result.putDouble("e", values[Matrix.MTRANS_X]);
result.putDouble("f", values[Matrix.MTRANS_Y]);
successCallback.invoke(result);
} }
} }
+2 -2
View File
@@ -959,7 +959,7 @@ TopAlignedLabel *label;
CGFloat angle; CGFloat angle;
CGFloat px; CGFloat px;
CGFloat py; CGFloat py;
[measure getPosAndTan:&angle midPoint:midPoint px:&px py:&py]; [measure getPosAndTan:&angle midPoint:midPoint x:&px y:&py];
transform = CGAffineTransformConcat(CGAffineTransformMakeTranslation(px, py), transform); transform = CGAffineTransformConcat(CGAffineTransformMakeTranslation(px, py), transform);
transform = CGAffineTransformConcat(CGAffineTransformMakeRotation(angle + r), transform); transform = CGAffineTransformConcat(CGAffineTransformMakeRotation(angle + r), transform);
@@ -1032,7 +1032,7 @@ TopAlignedLabel *label;
{ {
textPath = nil; textPath = nil;
RNSVGText *parent = (RNSVGText*)[self superview]; RNSVGText *parent = (RNSVGText*)[self superview];
CGPathRef path; CGPathRef path = nil;
while (parent) { while (parent) {
if ([parent class] == [RNSVGTextPath class]) { if ([parent class] == [RNSVGTextPath class]) {
textPath = (RNSVGTextPath*) parent; textPath = (RNSVGTextPath*) parent;
+1 -1
View File
@@ -19,6 +19,6 @@
- (void)reset; - (void)reset;
- (void)extractPathData:(CGPathRef)path; - (void)extractPathData:(CGPathRef)path;
- (void)getPosAndTan:(CGFloat *)angle midPoint:(CGFloat)midPoint px:(CGFloat *)px py:(CGFloat *)py; - (void)getPosAndTan:(CGFloat *)angle midPoint:(CGFloat)midPoint x:(CGFloat *)x y:(CGFloat *)y;
@end @end
+3 -3
View File
@@ -189,7 +189,7 @@ void subdivideBezierAtT(const CGPoint bez[4], CGPoint bez1[4], CGPoint bez2[4],
} }
} }
- (void)getPosAndTan:(CGFloat *)angle midPoint:(CGFloat)midPoint px:(CGFloat *)px py:(CGFloat *)py { - (void)getPosAndTan:(CGFloat *)angle midPoint:(CGFloat)midPoint x:(CGFloat *)x y:(CGFloat *)y {
// Investigation suggests binary search is faster at lineCount >= 16 // Investigation suggests binary search is faster at lineCount >= 16
// https://gist.github.com/msand/4c7993319425f9d7933be58ad9ada1a4 // https://gist.github.com/msand/4c7993319425f9d7933be58ad9ada1a4
NSUInteger i = _lineCount < 16 ? NSUInteger i = _lineCount < 16 ?
@@ -220,8 +220,8 @@ void subdivideBezierAtT(const CGPoint bez[4], CGPoint bez1[4], CGPoint bez2[4],
CGFloat ldx = p2.x - p1.x; CGFloat ldx = p2.x - p1.x;
CGFloat ldy = p2.y - p1.y; CGFloat ldy = p2.y - p1.y;
*angle = atan2(ldy, ldx); *angle = atan2(ldy, ldx);
*px = p1.x + ldx * percent; *x = p1.x + ldx * percent;
*py = p1.y + ldy * percent; *y = p1.y + ldy * percent;
} }
@end @end
+51 -21
View File
@@ -132,7 +132,6 @@ RCT_EXPORT_METHOD(getTotalLength:(nonnull NSNumber *)reactTag callback:(RCTRespo
CGPathRef target = [svg getPath:nil]; CGPathRef target = [svg getPath:nil];
RNSVGPathMeasure *measure = [[RNSVGPathMeasure alloc]init]; RNSVGPathMeasure *measure = [[RNSVGPathMeasure alloc]init];
[measure extractPathData:target]; [measure extractPathData:target];
CGFloat pathLegth = measure.pathLength; CGFloat pathLegth = measure.pathLength;
callback(@[[NSNumber numberWithDouble:pathLegth]]); callback(@[[NSNumber numberWithDouble:pathLegth]]);
} }
@@ -145,7 +144,7 @@ RCT_EXPORT_METHOD(getTotalLength:(nonnull NSNumber *)reactTag callback:(RCTRespo
RCT_EXPORT_METHOD(getPointAtLength:(nonnull NSNumber *)reactTag options:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback) RCT_EXPORT_METHOD(getPointAtLength:(nonnull NSNumber *)reactTag options:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback)
{ {
id length = [options objectForKey:@"length"]; id length = [options objectForKey:@"length"];
CGFloat x = (CGFloat)[length floatValue]; CGFloat position = (CGFloat)[length floatValue];
[self [self
withTag:reactTag withTag:reactTag
success:^(RNSVGRenderable *svg){ success:^(RNSVGRenderable *svg){
@@ -153,10 +152,18 @@ RCT_EXPORT_METHOD(getPointAtLength:(nonnull NSNumber *)reactTag options:(NSDicti
RNSVGPathMeasure *measure = [[RNSVGPathMeasure alloc]init]; RNSVGPathMeasure *measure = [[RNSVGPathMeasure alloc]init];
[measure extractPathData:target]; [measure extractPathData:target];
CGFloat angle; CGFloat angle;
CGFloat px; CGFloat x;
CGFloat py; CGFloat y;
[measure getPosAndTan:&angle midPoint:fmax(0, fmin(measure.pathLength, x)) px:&px py:&py]; [measure getPosAndTan:&angle midPoint:fmax(0, fmin(position, measure.pathLength)) x:&x y:&y];
callback(@[[NSNumber numberWithDouble:px], [NSNumber numberWithDouble:py]]); callback(
@[
@{
@"x":@(x),
@"y":@(y),
@"angle":@(angle)
}
]
);
} }
fail:^{ fail:^{
callback(@[[NSNumber numberWithBool:false]]); callback(@[[NSNumber numberWithBool:false]]);
@@ -187,16 +194,22 @@ RCT_EXPORT_METHOD(getBBox:(nonnull NSNumber *)reactTag options:(NSDictionary *)o
if (clipped) { if (clipped) {
CGPathRef clipPath = [svg getClipPath]; CGPathRef clipPath = [svg getClipPath];
CGRect clipBounds = CGPathGetBoundingBox(clipPath); CGRect clipBounds = CGPathGetBoundingBox(clipPath);
if (clipPath && !CGRectIsEmpty(clipBounds)) {
bounds = CGRectIntersection(bounds, clipBounds); bounds = CGRectIntersection(bounds, clipBounds);
} }
}
CGPoint origin = bounds.origin; CGPoint origin = bounds.origin;
CGSize size = bounds.size; CGSize size = bounds.size;
callback(@[ callback(
[NSNumber numberWithDouble:origin.x], @[
[NSNumber numberWithDouble:origin.y], @{
[NSNumber numberWithDouble:size.width], @"x":@(origin.x),
[NSNumber numberWithDouble:size.height] @"y":@(origin.y),
]); @"width":@(size.width),
@"height":@(size.height)
}
]
);
} }
fail:^{ fail:^{
callback(@[[NSNumber numberWithBool:false]]); callback(@[[NSNumber numberWithBool:false]]);
@@ -210,10 +223,18 @@ RCT_EXPORT_METHOD(getCTM:(nonnull NSNumber *)reactTag callback:(RCTResponseSende
withTag:reactTag withTag:reactTag
success:^(RNSVGRenderable *svg){ success:^(RNSVGRenderable *svg){
CGAffineTransform ctm = svg.ctm; CGAffineTransform ctm = svg.ctm;
callback(@[ callback(
n(ctm.a), n(ctm.c), n(ctm.tx), @[
n(ctm.b), n(ctm.d), n(ctm.ty) @{
]); @"a":n(ctm.a),
@"b":n(ctm.b),
@"c":n(ctm.c),
@"d":n(ctm.d),
@"e":n(ctm.tx),
@"f":n(ctm.ty)
}
]
);
} }
fail:^{ fail:^{
callback(@[[NSNumber numberWithBool:false]]); callback(@[[NSNumber numberWithBool:false]]);
@@ -230,11 +251,19 @@ RCT_EXPORT_METHOD(getScreenCTM:(nonnull NSNumber *)reactTag callback:(RCTRespons
[self [self
withTag:reactTag withTag:reactTag
success:^(RNSVGRenderable *svg){ success:^(RNSVGRenderable *svg){
CGAffineTransform screenCTM = svg.screenCTM; CGAffineTransform ctm = svg.screenCTM;
callback(@[ callback(
n(screenCTM.a), n(screenCTM.c), n(screenCTM.tx), @[
n(screenCTM.b), n(screenCTM.d), n(screenCTM.ty) @{
]); @"a":n(ctm.a),
@"b":n(ctm.b),
@"c":n(ctm.c),
@"d":n(ctm.d),
@"e":n(ctm.tx),
@"f":n(ctm.ty)
}
]
);
} }
fail:^{ fail:^{
callback(@[[NSNumber numberWithBool:false]]); callback(@[[NSNumber numberWithBool:false]]);
@@ -243,3 +272,4 @@ RCT_EXPORT_METHOD(getScreenCTM:(nonnull NSNumber *)reactTag callback:(RCTRespons
} }
@end @end