Skip to content

Commit

Permalink
[Not for merge?] Cache path parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon authored Nov 21, 2024
1 parent d1d936a commit 7724bcf
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion android/src/main/java/com/horcrux/svg/PathView.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,33 @@
import android.graphics.Path;
import com.facebook.react.bridge.ReactContext;

import java.util.ArrayList;
import java.util.HashMap;

class ParsedPath {
final Path path;
final ArrayList<PathElement> elements;

ParsedPath(Path path, ArrayList<PathElement> elements) {
this.path = path;
this.elements = elements;
}
}

@SuppressLint("ViewConstructor")
class PathView extends RenderableView {
private Path mPath;

// This grows forever but for our use case (static icons) it's ok.
private static final HashMap<String, ParsedPath> sPathCache = new HashMap<>();

public PathView(ReactContext reactContext) {
super(reactContext);
PathParser.mScale = mScale;
mPath = new Path();
}

public void setD(String d) {
void setDByParsing(String d) {
mPath = PathParser.parse(d);
elements = PathParser.elements;
for (PathElement elem : elements) {
Expand All @@ -33,6 +49,17 @@ public void setD(String d) {
point.y *= mScale;
}
}
}

public void setD(String d) {
ParsedPath cached = sPathCache.get(d);
if (cached != null) {
mPath = cached.path;
elements = cached.elements;
} else {
setDByParsing(d);
sPathCache.put(d, new ParsedPath(mPath, elements));
}
invalidate();
}

Expand Down

0 comments on commit 7724bcf

Please sign in to comment.