Skip to content

Commit

Permalink
fix software-mansion#1592 : add marker support on Rect, Line, Ellipse…
Browse files Browse the repository at this point in the history
…, Circle and Group
  • Loading branch information
mlecoq committed Nov 9, 2022
1 parent 9c0fa78 commit 85a0f36
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
10 changes: 10 additions & 0 deletions android/src/main/java/com/horcrux/svg/CircleView.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import com.facebook.react.bridge.Dynamic;
import com.facebook.react.bridge.ReactContext;

import java.util.ArrayList;

@SuppressLint("ViewConstructor")
class CircleView extends RenderableView {
private SVGLength mCx;
Expand Down Expand Up @@ -79,6 +81,14 @@ Path getPath(Canvas canvas, Paint paint) {
double r = relativeOnOther(mR);

path.addCircle((float) cx, (float) cy, (float) r, Path.Direction.CW);

elements = new ArrayList<>();
elements.add(new PathElement(ElementType.kCGPathElementMoveToPoint, new Point[]{new Point(mCx.value,mCy.value - mR.value )}));
elements.add(new PathElement(ElementType.kCGPathElementAddLineToPoint, new Point[]{new Point(mCx.value,mCy.value - mR.value ), new Point(mCx.value + mR.value,mCy.value )}));
elements.add(new PathElement(ElementType.kCGPathElementAddLineToPoint, new Point[]{ new Point(mCx.value + mR.value,mCy.value ), new Point(mCx.value,mCy.value + mR.value ) }));
elements.add(new PathElement(ElementType.kCGPathElementAddLineToPoint, new Point[]{new Point(mCx.value,mCy.value + mR.value ),new Point(mCx.value - mR.value,mCy.value )}));
elements.add(new PathElement(ElementType.kCGPathElementAddLineToPoint, new Point[]{new Point(mCx.value - mR.value,mCy.value ), new Point(mCx.value,mCy.value - mR.value ) }));

return path;
}
}
9 changes: 9 additions & 0 deletions android/src/main/java/com/horcrux/svg/EllipseView.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import com.facebook.react.bridge.Dynamic;
import com.facebook.react.bridge.ReactContext;

import java.util.ArrayList;

@SuppressLint("ViewConstructor")
class EllipseView extends RenderableView {
private SVGLength mCx;
Expand Down Expand Up @@ -98,6 +100,13 @@ Path getPath(Canvas canvas, Paint paint) {
new RectF((float) (cx - rx), (float) (cy - ry), (float) (cx + rx), (float) (cy + ry));
path.addOval(oval, Path.Direction.CW);

elements = new ArrayList<>();
elements.add(new PathElement(ElementType.kCGPathElementMoveToPoint, new Point[]{new Point(mCx.value,mCy.value - mRy.value )}));
elements.add(new PathElement(ElementType.kCGPathElementAddLineToPoint, new Point[]{new Point(mCx.value,mCy.value - mRy.value ), new Point(mCx.value + mRx.value,mCy.value )}));
elements.add(new PathElement(ElementType.kCGPathElementAddLineToPoint, new Point[]{ new Point(mCx.value + mRx.value,mCy.value ), new Point(mCx.value,mCy.value + mRy.value ) }));
elements.add(new PathElement(ElementType.kCGPathElementAddLineToPoint, new Point[]{new Point(mCx.value,mCy.value + mRy.value ),new Point(mCx.value - mRx.value,mCy.value )}));
elements.add(new PathElement(ElementType.kCGPathElementAddLineToPoint, new Point[]{new Point(mCx.value - mRx.value,mCy.value ), new Point(mCx.value,mCy.value - mRy.value ) }));

return path;
}
}
8 changes: 8 additions & 0 deletions android/src/main/java/com/horcrux/svg/GroupView.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableMap;
import javax.annotation.Nullable;
import java.util.ArrayList;

@SuppressLint("ViewConstructor")
class GroupView extends RenderableView {
Expand Down Expand Up @@ -79,6 +80,7 @@ void drawGroup(final Canvas canvas, final Paint paint, final float opacity) {
final SvgView svg = getSvgView();
final GroupView self = this;
final RectF groupRect = new RectF();
elements = new ArrayList<>();
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (child instanceof MaskView) {
Expand All @@ -96,6 +98,7 @@ void drawGroup(final Canvas canvas, final Paint paint, final float opacity) {
int count = node.saveAndSetupCanvas(canvas, mCTM);
node.render(canvas, paint, opacity * mOpacity);
RectF r = node.getClientRect();

if (r != null) {
groupRect.union(r);
}
Expand All @@ -109,6 +112,11 @@ void drawGroup(final Canvas canvas, final Paint paint, final float opacity) {
if (node.isResponsible()) {
svg.enableTouchEvents();
}

if(node.elements != null){
elements.addAll(node.elements);
}

} else if (child instanceof SvgView) {
SvgView svgView = (SvgView) child;
svgView.drawChildren(canvas);
Expand Down
7 changes: 7 additions & 0 deletions android/src/main/java/com/horcrux/svg/LineView.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import com.facebook.react.bridge.Dynamic;
import com.facebook.react.bridge.ReactContext;

import java.util.ArrayList;

@SuppressLint("ViewConstructor")
class LineView extends RenderableView {
private SVGLength mX1;
Expand Down Expand Up @@ -96,6 +98,11 @@ Path getPath(Canvas canvas, Paint paint) {

path.moveTo((float) x1, (float) y1);
path.lineTo((float) x2, (float) y2);

elements = new ArrayList<>();
elements.add(new PathElement(ElementType.kCGPathElementMoveToPoint, new Point[]{new Point(mX1.value,mY1.value)}));
elements.add(new PathElement(ElementType.kCGPathElementAddLineToPoint, new Point[]{new Point(mX2.value,mY2.value)}));

return path;
}
}
10 changes: 10 additions & 0 deletions android/src/main/java/com/horcrux/svg/RectView.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import com.facebook.react.bridge.Dynamic;
import com.facebook.react.bridge.ReactContext;

import java.util.ArrayList;

@SuppressLint("ViewConstructor")
class RectView extends RenderableView {
private SVGLength mX;
Expand Down Expand Up @@ -170,6 +172,14 @@ Path getPath(Canvas canvas, Paint paint) {
path.close(); // Ensure isSimplePath = false such that rect doesn't become represented using
// integers
}

elements = new ArrayList<>();
elements.add(new PathElement(ElementType.kCGPathElementMoveToPoint, new Point[]{new Point(mX.value,mY.value)}));
elements.add(new PathElement(ElementType.kCGPathElementAddLineToPoint, new Point[]{new Point(mX.value + mW.value,mY.value)}));
elements.add(new PathElement(ElementType.kCGPathElementAddLineToPoint, new Point[]{new Point(mX.value + mW.value,mY.value + mH.value)}));
elements.add(new PathElement(ElementType.kCGPathElementAddLineToPoint, new Point[]{new Point(mX.value,mY.value + mH.value)}));
elements.add(new PathElement(ElementType.kCGPathElementAddLineToPoint, new Point[]{new Point(mX.value,mY.value)}));

return path;
}
}

0 comments on commit 85a0f36

Please sign in to comment.