Skip to content

Commit

Permalink
feat: improve marker rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
msand committed Sep 29, 2019
1 parent 2e3069d commit 9628830
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
7 changes: 4 additions & 3 deletions android/src/main/java/com/horcrux/svg/MarkerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,15 @@ void renderMarker(Canvas canvas, Paint paint, float opacity, RNSVGMarkerPosition

Point origin = position.origin;
Matrix transform = new Matrix();
transform.setTranslate((float)origin.x, (float)origin.y);
transform.setTranslate((float)origin.x * mScale, (float)origin.y * mScale);

double markerAngle = "auto".equals(mOrient) ? -1 : Double.parseDouble(mOrient);
transform.postRotate((float)(markerAngle == -1 ? position.angle : markerAngle));
float degrees = 180 + (float) (markerAngle == -1 ? position.angle : markerAngle);
transform.preRotate(degrees);

boolean useStrokeWidth = "strokeWidth".equals(mMarkerUnits);
if (useStrokeWidth) {
transform.postScale(strokeWidth, strokeWidth);
transform.preScale(strokeWidth, strokeWidth);
}

canvas.concat(transform);
Expand Down
10 changes: 9 additions & 1 deletion ios/Elements/RNSVGMarker.m
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ - (void)setMeetOrSlice:(RNSVGVBMOS)meetOrSlice
_meetOrSlice = meetOrSlice;
}

static CGFloat RNSVG_degToRad = (CGFloat)M_PI / 180;

double deg2rad(CGFloat deg) {
return deg * RNSVG_degToRad;
}

- (void)renderMarker:(CGContextRef)context rect:(CGRect)rect position:(RNSVGMarkerPosition*)position strokeWidth:(CGFloat)strokeWidth
{
CGContextSaveGState(context);
Expand All @@ -158,7 +164,9 @@ - (void)renderMarker:(CGContextRef)context rect:(CGRect)rect position:(RNSVGMark
CGAffineTransform transform = CGAffineTransformMakeTranslation(origin.x, origin.y);

float markerAngle = [@"auto" isEqualToString:_orient] ? -1 : [_orient doubleValue];
transform = CGAffineTransformRotate(transform, markerAngle == -1 ? [position angle] : markerAngle);
float angle = 180 + (markerAngle == -1 ? [position angle] : markerAngle);
float rad = deg2rad(angle);
transform = CGAffineTransformRotate(transform, rad);

bool useStrokeWidth = [@"strokeWidth" isEqualToString:_markerUnits];
if (useStrokeWidth) {
Expand Down
2 changes: 1 addition & 1 deletion ios/Utils/RNSVGMarkerPosition.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ typedef enum RNSVGMarkerType {
kEndMarker
} RNSVGMarkerType;

#define RNSVGNULLPOINT CGRectNull.origin
#define RNSVGZEROPOINT CGRectZero.origin

@interface RNSVGMarkerPosition : NSObject

Expand Down
6 changes: 3 additions & 3 deletions ios/Utils/RNSVGMarkerPosition.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ - (instancetype) init
if (self)
{
_type = kStartMarker;
_origin = RNSVGNULLPOINT;
_origin = RNSVGZEROPOINT;
_angle = 0;
}
return self;
Expand All @@ -25,8 +25,8 @@ + (instancetype) markerPosition:(RNSVGMarkerType)type origin:(CGPoint)origin ang
+ (NSArray<RNSVGMarkerPosition*>*) fromCGPath:(CGPathRef)path {
positions_ = [[NSMutableArray alloc] init];
element_index_ = 0;
origin_ = RNSVGNULLPOINT;
subpath_start_ = RNSVGNULLPOINT;
origin_ = RNSVGZEROPOINT;
subpath_start_ = RNSVGZEROPOINT;
CGPathApply(path, (__bridge void *)positions_, UpdateFromPathElement);
PathIsDone();
return positions_;
Expand Down

0 comments on commit 9628830

Please sign in to comment.