fix(android): NullPointerException when calling getBBox #1215

Also fixes for other native methods
This commit is contained in:
Mikael Sand
2020-01-15 03:11:15 +02:00
parent 7a23968f3d
commit 3eb82a91b4
@@ -59,7 +59,13 @@ class RNSVGRenderableManager extends ReactContextBaseJavaModule {
return false; return false;
} }
svg.getPath(null, null); try {
svg.getPath(null, null);
} catch (NullPointerException e) {
svg.invalidate();
return false;
}
svg.initBounds(); svg.initBounds();
float scale = svg.mScale; float scale = svg.mScale;
@@ -78,7 +84,15 @@ class RNSVGRenderableManager extends ReactContextBaseJavaModule {
return 0; return 0;
} }
Path path = svg.getPath(null, null); Path path;
try {
path = svg.getPath(null, null);
} catch (NullPointerException e) {
svg.invalidate();
return -1;
}
PathMeasure pm = new PathMeasure(path, false); PathMeasure pm = new PathMeasure(path, false);
return pm.getLength() / svg.mScale; return pm.getLength() / svg.mScale;
} }
@@ -88,10 +102,18 @@ class RNSVGRenderableManager extends ReactContextBaseJavaModule {
public WritableMap getPointAtLength(int tag, ReadableMap options) { public WritableMap getPointAtLength(int tag, ReadableMap options) {
RenderableView svg = RenderableViewManager.getRenderableViewByTag(tag); RenderableView svg = RenderableViewManager.getRenderableViewByTag(tag);
if (svg == null) { if (svg == null) {
return null; return Arguments.createMap();
}
Path path;
try {
path = svg.getPath(null, null);
} catch (NullPointerException e) {
svg.invalidate();
return Arguments.createMap();
} }
Path path = svg.getPath(null, null);
PathMeasure pm = new PathMeasure(path, false); PathMeasure pm = new PathMeasure(path, false);
float length = (float) options.getDouble("length"); float length = (float) options.getDouble("length");
float scale = svg.mScale; float scale = svg.mScale;
@@ -114,7 +136,7 @@ class RNSVGRenderableManager extends ReactContextBaseJavaModule {
public WritableMap getBBox(int tag, ReadableMap options) { public WritableMap getBBox(int tag, ReadableMap options) {
RenderableView svg = RenderableViewManager.getRenderableViewByTag(tag); RenderableView svg = RenderableViewManager.getRenderableViewByTag(tag);
if (svg == null) { if (svg == null) {
return null; return Arguments.createMap();
} }
boolean fill = options.getBoolean("fill"); boolean fill = options.getBoolean("fill");
@@ -122,25 +144,33 @@ class RNSVGRenderableManager extends ReactContextBaseJavaModule {
boolean markers = options.getBoolean("markers"); boolean markers = options.getBoolean("markers");
boolean clipped = options.getBoolean("clipped"); boolean clipped = options.getBoolean("clipped");
Path path = svg.getPath(null, null); try {
svg.getPath(null, null);
} catch (NullPointerException e) {
svg.invalidate();
return Arguments.createMap();
}
float scale = svg.mScale; float scale = svg.mScale;
svg.initBounds(); svg.initBounds();
RectF bounds = new RectF(); RectF bounds = new RectF();
if (fill) { RectF fillBounds = svg.mFillBounds;
bounds.union(svg.mFillBounds); RectF strokeBounds = svg.mStrokeBounds;
RectF markerBounds = svg.mMarkerBounds;
RectF clipBounds = svg.mClipBounds;
if (fill && fillBounds != null) {
bounds.union(fillBounds);
} }
if (stroke) { if (stroke && strokeBounds != null) {
bounds.union(svg.mStrokeBounds); bounds.union(strokeBounds);
} }
if (markers) { if (markers && markerBounds != null) {
bounds.union(svg.mMarkerBounds); bounds.union(markerBounds);
} }
if (clipped) { if (clipped && clipBounds != null) {
RectF clipBounds = svg.mClipBounds; bounds.intersect(clipBounds);
if (clipBounds != null) {
bounds.intersect(svg.mClipBounds);
}
} }
WritableMap result = Arguments.createMap(); WritableMap result = Arguments.createMap();
@@ -156,7 +186,7 @@ class RNSVGRenderableManager extends ReactContextBaseJavaModule {
public WritableMap getCTM(int tag) { public WritableMap getCTM(int tag) {
RenderableView svg = RenderableViewManager.getRenderableViewByTag(tag); RenderableView svg = RenderableViewManager.getRenderableViewByTag(tag);
if (svg == null) { if (svg == null) {
return null; return Arguments.createMap();
} }
float scale = svg.mScale; float scale = svg.mScale;
@@ -182,7 +212,7 @@ class RNSVGRenderableManager extends ReactContextBaseJavaModule {
public WritableMap getScreenCTM(int tag) { public WritableMap getScreenCTM(int tag) {
RenderableView svg = RenderableViewManager.getRenderableViewByTag(tag); RenderableView svg = RenderableViewManager.getRenderableViewByTag(tag);
if (svg == null) { if (svg == null) {
return null; return Arguments.createMap();
} }
float[] values = new float[9]; float[] values = new float[9];