Skip to content

feat: implement cache precalculated value of "d" on "path" element (android) #2589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions android/src/main/java/com/horcrux/svg/PathView.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,25 @@
import android.graphics.Path;
import com.facebook.react.bridge.ReactContext;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

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

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

@SuppressLint("ViewConstructor")
class PathView extends RenderableView {
private Path mPath;
private static final Map<String, ParsedPath> sPathCache = new HashMap<>();

public PathView(ReactContext reactContext) {
super(reactContext);
Expand All @@ -36,6 +52,27 @@ public void setD(String d) {
invalidate();
}

void setDByParsing(String d) {
mPath = PathParser.parse(d);
elements = PathParser.elements;
for (PathElement elem : elements) {
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();
}

@Override
Path getPath(Canvas canvas, Paint paint) {
return mPath;
Expand Down