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.Region;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.bridge.WritableMap;
import javax.annotation.Nonnull;
@@ -115,7 +117,12 @@ class RNSVGRenderableManager extends ReactContextBaseJavaModule {
float[] pos = new float[2];
float[] tan = new float[2];
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")
@@ -139,9 +146,17 @@ class RNSVGRenderableManager extends ReactContextBaseJavaModule {
bounds.union(svg.mMarkerBounds);
}
if (clipped) {
bounds.intersect(svg.mClipBounds);
RectF clipBounds = svg.mClipBounds;
if (clipBounds != null) {
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")
@@ -155,10 +170,14 @@ class RNSVGRenderableManager extends ReactContextBaseJavaModule {
float[] values = new float[9];
ctm.getValues(values);
successCallback.invoke(
values[Matrix.MSCALE_X], values[Matrix.MSKEW_X], values[Matrix.MTRANS_X],
values[Matrix.MSKEW_Y], values[Matrix.MSCALE_Y], values[Matrix.MTRANS_Y]
);
WritableMap result = Arguments.createMap();
result.putDouble("a", values[Matrix.MSCALE_X]);
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")
@@ -168,9 +187,13 @@ class RNSVGRenderableManager extends ReactContextBaseJavaModule {
Matrix screenCTM = svg.mCTM;
float[] values = new float[9];
screenCTM.getValues(values);
successCallback.invoke(
values[Matrix.MSCALE_X], values[Matrix.MSKEW_X], values[Matrix.MTRANS_X],
values[Matrix.MSKEW_Y], values[Matrix.MSCALE_Y], values[Matrix.MTRANS_Y]
);
WritableMap result = Arguments.createMap();
result.putDouble("a", values[Matrix.MSCALE_X]);
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 px;
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(CGAffineTransformMakeRotation(angle + r), transform);
@@ -1032,7 +1032,7 @@ TopAlignedLabel *label;
{
textPath = nil;
RNSVGText *parent = (RNSVGText*)[self superview];
CGPathRef path;
CGPathRef path = nil;
while (parent) {
if ([parent class] == [RNSVGTextPath class]) {
textPath = (RNSVGTextPath*) parent;
+1 -1
View File
@@ -19,6 +19,6 @@
- (void)reset;
- (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
+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
// https://gist.github.com/msand/4c7993319425f9d7933be58ad9ada1a4
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 ldy = p2.y - p1.y;
*angle = atan2(ldy, ldx);
*px = p1.x + ldx * percent;
*py = p1.y + ldy * percent;
*x = p1.x + ldx * percent;
*y = p1.y + ldy * percent;
}
@end
+52 -22
View File
@@ -132,7 +132,6 @@ RCT_EXPORT_METHOD(getTotalLength:(nonnull NSNumber *)reactTag callback:(RCTRespo
CGPathRef target = [svg getPath:nil];
RNSVGPathMeasure *measure = [[RNSVGPathMeasure alloc]init];
[measure extractPathData:target];
CGFloat pathLegth = measure.pathLength;
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)
{
id length = [options objectForKey:@"length"];
CGFloat x = (CGFloat)[length floatValue];
CGFloat position = (CGFloat)[length floatValue];
[self
withTag:reactTag
success:^(RNSVGRenderable *svg){
@@ -153,10 +152,18 @@ RCT_EXPORT_METHOD(getPointAtLength:(nonnull NSNumber *)reactTag options:(NSDicti
RNSVGPathMeasure *measure = [[RNSVGPathMeasure alloc]init];
[measure extractPathData:target];
CGFloat angle;
CGFloat px;
CGFloat py;
[measure getPosAndTan:&angle midPoint:fmax(0, fmin(measure.pathLength, x)) px:&px py:&py];
callback(@[[NSNumber numberWithDouble:px], [NSNumber numberWithDouble:py]]);
CGFloat x;
CGFloat y;
[measure getPosAndTan:&angle midPoint:fmax(0, fmin(position, measure.pathLength)) x:&x y:&y];
callback(
@[
@{
@"x":@(x),
@"y":@(y),
@"angle":@(angle)
}
]
);
}
fail:^{
callback(@[[NSNumber numberWithBool:false]]);
@@ -187,16 +194,22 @@ RCT_EXPORT_METHOD(getBBox:(nonnull NSNumber *)reactTag options:(NSDictionary *)o
if (clipped) {
CGPathRef clipPath = [svg getClipPath];
CGRect clipBounds = CGPathGetBoundingBox(clipPath);
bounds = CGRectIntersection(bounds, clipBounds);
if (clipPath && !CGRectIsEmpty(clipBounds)) {
bounds = CGRectIntersection(bounds, clipBounds);
}
}
CGPoint origin = bounds.origin;
CGSize size = bounds.size;
callback(@[
[NSNumber numberWithDouble:origin.x],
[NSNumber numberWithDouble:origin.y],
[NSNumber numberWithDouble:size.width],
[NSNumber numberWithDouble:size.height]
]);
callback(
@[
@{
@"x":@(origin.x),
@"y":@(origin.y),
@"width":@(size.width),
@"height":@(size.height)
}
]
);
}
fail:^{
callback(@[[NSNumber numberWithBool:false]]);
@@ -210,10 +223,18 @@ RCT_EXPORT_METHOD(getCTM:(nonnull NSNumber *)reactTag callback:(RCTResponseSende
withTag:reactTag
success:^(RNSVGRenderable *svg){
CGAffineTransform ctm = svg.ctm;
callback(@[
n(ctm.a), n(ctm.c), n(ctm.tx),
n(ctm.b), n(ctm.d), n(ctm.ty)
]);
callback(
@[
@{
@"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:^{
callback(@[[NSNumber numberWithBool:false]]);
@@ -230,11 +251,19 @@ RCT_EXPORT_METHOD(getScreenCTM:(nonnull NSNumber *)reactTag callback:(RCTRespons
[self
withTag:reactTag
success:^(RNSVGRenderable *svg){
CGAffineTransform screenCTM = svg.screenCTM;
callback(@[
n(screenCTM.a), n(screenCTM.c), n(screenCTM.tx),
n(screenCTM.b), n(screenCTM.d), n(screenCTM.ty)
]);
CGAffineTransform ctm = svg.screenCTM;
callback(
@[
@{
@"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:^{
callback(@[[NSNumber numberWithBool:false]]);
@@ -243,3 +272,4 @@ RCT_EXPORT_METHOD(getScreenCTM:(nonnull NSNumber *)reactTag callback:(RCTRespons
}
@end