Skip to content

Commit 8687a3d

Browse files
committed
fix: native method signatures web compatibility / spec conformance
1 parent c28499b commit 8687a3d

File tree

5 files changed

+92
-39
lines changed

5 files changed

+92
-39
lines changed

android/src/main/java/com/horcrux/svg/RNSVGRenderableManager.java

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
import android.graphics.RectF;
1616
import android.graphics.Region;
1717

18+
import com.facebook.react.bridge.Arguments;
1819
import com.facebook.react.bridge.Callback;
1920
import com.facebook.react.bridge.ReactApplicationContext;
2021
import com.facebook.react.bridge.ReactContextBaseJavaModule;
2122
import com.facebook.react.bridge.ReactMethod;
2223
import com.facebook.react.bridge.ReadableMap;
2324
import com.facebook.react.bridge.UiThreadUtil;
25+
import com.facebook.react.bridge.WritableMap;
2426

2527
import javax.annotation.Nonnull;
2628

@@ -115,7 +117,12 @@ public void getPointAtLength(int tag, ReadableMap options, Callback successCallb
115117
float[] pos = new float[2];
116118
float[] tan = new float[2];
117119
pm.getPosTan(Math.max(0, Math.min(length, pathLength)), pos, tan);
118-
successCallback.invoke(pos[0], pos[1]);
120+
double angle = Math.atan2(tan[1], tan[0]);
121+
WritableMap result = Arguments.createMap();
122+
result.putDouble("x", pos[0]);
123+
result.putDouble("y", pos[1]);
124+
result.putDouble("angle", angle);
125+
successCallback.invoke(result);
119126
}
120127

121128
@SuppressWarnings("unused")
@@ -139,9 +146,17 @@ public void getBBox(int tag, ReadableMap options, Callback successCallback) {
139146
bounds.union(svg.mMarkerBounds);
140147
}
141148
if (clipped) {
142-
bounds.intersect(svg.mClipBounds);
149+
RectF clipBounds = svg.mClipBounds;
150+
if (clipBounds != null) {
151+
bounds.intersect(svg.mClipBounds);
152+
}
143153
}
144-
successCallback.invoke(bounds.left, bounds.top, bounds.width(), bounds.height());
154+
WritableMap result = Arguments.createMap();
155+
result.putDouble("x", bounds.left);
156+
result.putDouble("y", bounds.top);
157+
result.putDouble("width", bounds.width());
158+
result.putDouble("height", bounds.height());
159+
successCallback.invoke(result);
145160
}
146161

147162
@SuppressWarnings("unused")
@@ -155,10 +170,14 @@ public void getCTM(int tag, Callback successCallback) {
155170

156171
float[] values = new float[9];
157172
ctm.getValues(values);
158-
successCallback.invoke(
159-
values[Matrix.MSCALE_X], values[Matrix.MSKEW_X], values[Matrix.MTRANS_X],
160-
values[Matrix.MSKEW_Y], values[Matrix.MSCALE_Y], values[Matrix.MTRANS_Y]
161-
);
173+
WritableMap result = Arguments.createMap();
174+
result.putDouble("a", values[Matrix.MSCALE_X]);
175+
result.putDouble("b", values[Matrix.MSKEW_Y]);
176+
result.putDouble("c", values[Matrix.MSKEW_X]);
177+
result.putDouble("d", values[Matrix.MSCALE_Y]);
178+
result.putDouble("e", values[Matrix.MTRANS_X]);
179+
result.putDouble("f", values[Matrix.MTRANS_Y]);
180+
successCallback.invoke(result);
162181
}
163182

164183
@SuppressWarnings("unused")
@@ -168,9 +187,13 @@ public void getScreenCTM(int tag, Callback successCallback) {
168187
Matrix screenCTM = svg.mCTM;
169188
float[] values = new float[9];
170189
screenCTM.getValues(values);
171-
successCallback.invoke(
172-
values[Matrix.MSCALE_X], values[Matrix.MSKEW_X], values[Matrix.MTRANS_X],
173-
values[Matrix.MSKEW_Y], values[Matrix.MSCALE_Y], values[Matrix.MTRANS_Y]
174-
);
190+
WritableMap result = Arguments.createMap();
191+
result.putDouble("a", values[Matrix.MSCALE_X]);
192+
result.putDouble("b", values[Matrix.MSKEW_Y]);
193+
result.putDouble("c", values[Matrix.MSKEW_X]);
194+
result.putDouble("d", values[Matrix.MSCALE_Y]);
195+
result.putDouble("e", values[Matrix.MTRANS_X]);
196+
result.putDouble("f", values[Matrix.MTRANS_Y]);
197+
successCallback.invoke(result);
175198
}
176199
}

ios/Text/RNSVGTSpan.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ A negative value is an error (see Error processing).
959959
CGFloat angle;
960960
CGFloat px;
961961
CGFloat py;
962-
[measure getPosAndTan:&angle midPoint:midPoint px:&px py:&py];
962+
[measure getPosAndTan:&angle midPoint:midPoint x:&px y:&py];
963963

964964
transform = CGAffineTransformConcat(CGAffineTransformMakeTranslation(px, py), transform);
965965
transform = CGAffineTransformConcat(CGAffineTransformMakeRotation(angle + r), transform);
@@ -1032,7 +1032,7 @@ - (void)setupTextPath:(CGContextRef)context
10321032
{
10331033
textPath = nil;
10341034
RNSVGText *parent = (RNSVGText*)[self superview];
1035-
CGPathRef path;
1035+
CGPathRef path = nil;
10361036
while (parent) {
10371037
if ([parent class] == [RNSVGTextPath class]) {
10381038
textPath = (RNSVGTextPath*) parent;

ios/Utils/RNSVGPathMeasure.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919

2020
- (void)reset;
2121
- (void)extractPathData:(CGPathRef)path;
22-
- (void)getPosAndTan:(CGFloat *)angle midPoint:(CGFloat)midPoint px:(CGFloat *)px py:(CGFloat *)py;
22+
- (void)getPosAndTan:(CGFloat *)angle midPoint:(CGFloat)midPoint x:(CGFloat *)x y:(CGFloat *)y;
2323

2424
@end

ios/Utils/RNSVGPathMeasure.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ - (void)extractPathData:(CGPathRef)path {
189189
}
190190
}
191191

192-
- (void)getPosAndTan:(CGFloat *)angle midPoint:(CGFloat)midPoint px:(CGFloat *)px py:(CGFloat *)py {
192+
- (void)getPosAndTan:(CGFloat *)angle midPoint:(CGFloat)midPoint x:(CGFloat *)x y:(CGFloat *)y {
193193
// Investigation suggests binary search is faster at lineCount >= 16
194194
// https://gist.github.com/msand/4c7993319425f9d7933be58ad9ada1a4
195195
NSUInteger i = _lineCount < 16 ?
@@ -220,8 +220,8 @@ - (void)getPosAndTan:(CGFloat *)angle midPoint:(CGFloat)midPoint px:(CGFloat *)p
220220
CGFloat ldx = p2.x - p1.x;
221221
CGFloat ldy = p2.y - p1.y;
222222
*angle = atan2(ldy, ldx);
223-
*px = p1.x + ldx * percent;
224-
*py = p1.y + ldy * percent;
223+
*x = p1.x + ldx * percent;
224+
*y = p1.y + ldy * percent;
225225
}
226226

227227
@end

ios/ViewManagers/RNSVGRenderableManager.m

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ - (void)withTag:(nonnull NSNumber *)reactTag success:(RNSVGSuccessBlock)successB
132132
CGPathRef target = [svg getPath:nil];
133133
RNSVGPathMeasure *measure = [[RNSVGPathMeasure alloc]init];
134134
[measure extractPathData:target];
135-
136135
CGFloat pathLegth = measure.pathLength;
137136
callback(@[[NSNumber numberWithDouble:pathLegth]]);
138137
}
@@ -145,18 +144,26 @@ - (void)withTag:(nonnull NSNumber *)reactTag success:(RNSVGSuccessBlock)successB
145144
RCT_EXPORT_METHOD(getPointAtLength:(nonnull NSNumber *)reactTag options:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback)
146145
{
147146
id length = [options objectForKey:@"length"];
148-
CGFloat x = (CGFloat)[length floatValue];
147+
CGFloat position = (CGFloat)[length floatValue];
149148
[self
150149
withTag:reactTag
151150
success:^(RNSVGRenderable *svg){
152151
CGPathRef target = [svg getPath:nil];
153152
RNSVGPathMeasure *measure = [[RNSVGPathMeasure alloc]init];
154153
[measure extractPathData:target];
155154
CGFloat angle;
156-
CGFloat px;
157-
CGFloat py;
158-
[measure getPosAndTan:&angle midPoint:fmax(0, fmin(measure.pathLength, x)) px:&px py:&py];
159-
callback(@[[NSNumber numberWithDouble:px], [NSNumber numberWithDouble:py]]);
155+
CGFloat x;
156+
CGFloat y;
157+
[measure getPosAndTan:&angle midPoint:fmax(0, fmin(position, measure.pathLength)) x:&x y:&y];
158+
callback(
159+
@[
160+
@{
161+
@"x":@(x),
162+
@"y":@(y),
163+
@"angle":@(angle)
164+
}
165+
]
166+
);
160167
}
161168
fail:^{
162169
callback(@[[NSNumber numberWithBool:false]]);
@@ -187,16 +194,22 @@ - (void)withTag:(nonnull NSNumber *)reactTag success:(RNSVGSuccessBlock)successB
187194
if (clipped) {
188195
CGPathRef clipPath = [svg getClipPath];
189196
CGRect clipBounds = CGPathGetBoundingBox(clipPath);
190-
bounds = CGRectIntersection(bounds, clipBounds);
197+
if (clipPath && !CGRectIsEmpty(clipBounds)) {
198+
bounds = CGRectIntersection(bounds, clipBounds);
199+
}
191200
}
192201
CGPoint origin = bounds.origin;
193202
CGSize size = bounds.size;
194-
callback(@[
195-
[NSNumber numberWithDouble:origin.x],
196-
[NSNumber numberWithDouble:origin.y],
197-
[NSNumber numberWithDouble:size.width],
198-
[NSNumber numberWithDouble:size.height]
199-
]);
203+
callback(
204+
@[
205+
@{
206+
@"x":@(origin.x),
207+
@"y":@(origin.y),
208+
@"width":@(size.width),
209+
@"height":@(size.height)
210+
}
211+
]
212+
);
200213
}
201214
fail:^{
202215
callback(@[[NSNumber numberWithBool:false]]);
@@ -210,10 +223,18 @@ - (void)withTag:(nonnull NSNumber *)reactTag success:(RNSVGSuccessBlock)successB
210223
withTag:reactTag
211224
success:^(RNSVGRenderable *svg){
212225
CGAffineTransform ctm = svg.ctm;
213-
callback(@[
214-
n(ctm.a), n(ctm.c), n(ctm.tx),
215-
n(ctm.b), n(ctm.d), n(ctm.ty)
216-
]);
226+
callback(
227+
@[
228+
@{
229+
@"a":n(ctm.a),
230+
@"b":n(ctm.b),
231+
@"c":n(ctm.c),
232+
@"d":n(ctm.d),
233+
@"e":n(ctm.tx),
234+
@"f":n(ctm.ty)
235+
}
236+
]
237+
);
217238
}
218239
fail:^{
219240
callback(@[[NSNumber numberWithBool:false]]);
@@ -230,11 +251,19 @@ - (void)withTag:(nonnull NSNumber *)reactTag success:(RNSVGSuccessBlock)successB
230251
[self
231252
withTag:reactTag
232253
success:^(RNSVGRenderable *svg){
233-
CGAffineTransform screenCTM = svg.screenCTM;
234-
callback(@[
235-
n(screenCTM.a), n(screenCTM.c), n(screenCTM.tx),
236-
n(screenCTM.b), n(screenCTM.d), n(screenCTM.ty)
237-
]);
254+
CGAffineTransform ctm = svg.screenCTM;
255+
callback(
256+
@[
257+
@{
258+
@"a":n(ctm.a),
259+
@"b":n(ctm.b),
260+
@"c":n(ctm.c),
261+
@"d":n(ctm.d),
262+
@"e":n(ctm.tx),
263+
@"f":n(ctm.ty)
264+
}
265+
]
266+
);
238267
}
239268
fail:^{
240269
callback(@[[NSNumber numberWithBool:false]]);
@@ -243,3 +272,4 @@ - (void)withTag:(nonnull NSNumber *)reactTag success:(RNSVGSuccessBlock)successB
243272
}
244273

245274
@end
275+

0 commit comments

Comments
 (0)