Skip to content

Commit

Permalink
fix(android): NullPointerException when calling getBBox #1215
Browse files Browse the repository at this point in the history
Also fixes for other native methods
  • Loading branch information
msand committed Jan 15, 2020
1 parent 7a23968 commit 3eb82a9
Showing 1 changed file with 49 additions and 19 deletions.
68 changes: 49 additions & 19 deletions android/src/main/java/com/horcrux/svg/RNSVGRenderableManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ public boolean isPointInStroke(int tag, ReadableMap options) {
return false;
}

svg.getPath(null, null);
try {
svg.getPath(null, null);
} catch (NullPointerException e) {
svg.invalidate();
return false;
}

svg.initBounds();

float scale = svg.mScale;
Expand All @@ -78,7 +84,15 @@ public float getTotalLength(int tag) {
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);
return pm.getLength() / svg.mScale;
}
Expand All @@ -88,10 +102,18 @@ public float getTotalLength(int tag) {
public WritableMap getPointAtLength(int tag, ReadableMap options) {
RenderableView svg = RenderableViewManager.getRenderableViewByTag(tag);
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);
float length = (float) options.getDouble("length");
float scale = svg.mScale;
Expand All @@ -114,33 +136,41 @@ public WritableMap getPointAtLength(int tag, ReadableMap options) {
public WritableMap getBBox(int tag, ReadableMap options) {
RenderableView svg = RenderableViewManager.getRenderableViewByTag(tag);
if (svg == null) {
return null;
return Arguments.createMap();
}

boolean fill = options.getBoolean("fill");
boolean stroke = options.getBoolean("stroke");
boolean markers = options.getBoolean("markers");
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;
svg.initBounds();

RectF bounds = new RectF();
if (fill) {
bounds.union(svg.mFillBounds);
RectF fillBounds = svg.mFillBounds;
RectF strokeBounds = svg.mStrokeBounds;
RectF markerBounds = svg.mMarkerBounds;
RectF clipBounds = svg.mClipBounds;

if (fill && fillBounds != null) {
bounds.union(fillBounds);
}
if (stroke) {
bounds.union(svg.mStrokeBounds);
if (stroke && strokeBounds != null) {
bounds.union(strokeBounds);
}
if (markers) {
bounds.union(svg.mMarkerBounds);
if (markers && markerBounds != null) {
bounds.union(markerBounds);
}
if (clipped) {
RectF clipBounds = svg.mClipBounds;
if (clipBounds != null) {
bounds.intersect(svg.mClipBounds);
}
if (clipped && clipBounds != null) {
bounds.intersect(clipBounds);
}

WritableMap result = Arguments.createMap();
Expand All @@ -156,7 +186,7 @@ public WritableMap getBBox(int tag, ReadableMap options) {
public WritableMap getCTM(int tag) {
RenderableView svg = RenderableViewManager.getRenderableViewByTag(tag);
if (svg == null) {
return null;
return Arguments.createMap();
}

float scale = svg.mScale;
Expand All @@ -182,7 +212,7 @@ public WritableMap getCTM(int tag) {
public WritableMap getScreenCTM(int tag) {
RenderableView svg = RenderableViewManager.getRenderableViewByTag(tag);
if (svg == null) {
return null;
return Arguments.createMap();
}

float[] values = new float[9];
Expand Down

0 comments on commit 3eb82a9

Please sign in to comment.