-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSVGRectElement.m
105 lines (77 loc) · 2.61 KB
/
SVGRectElement.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// SVGRectElement.m
// SVGKit
//
// Copyright Matt Rajca 2010-2011. All rights reserved.
//
#import "SVGRectElement.h"
#import "SVGElement+Private.h"
#import "SVGShapeElement+Private.h"
@interface SVGRectElement ()
void CGPathAddRoundedRect (CGMutablePathRef path, CGRect rect, CGFloat radius);
@end
@implementation SVGRectElement
@synthesize x = _x;
@synthesize y = _y;
@synthesize width = _width;
@synthesize height = _height;
@synthesize rx = _rx;
@synthesize ry = _ry;
// adapted from http://www.cocoanetics.com/2010/02/drawing-rounded-rectangles/
void CGPathAddRoundedRect (CGMutablePathRef path, CGRect rect, CGFloat radius) {
CGRect innerRect = CGRectInset(rect, radius, radius);
CGFloat innerRight = innerRect.origin.x + innerRect.size.width;
CGFloat right = rect.origin.x + rect.size.width;
CGFloat innerBottom = innerRect.origin.y + innerRect.size.height;
CGFloat bottom = rect.origin.y + rect.size.height;
CGFloat innerTop = innerRect.origin.y;
CGFloat top = rect.origin.y;
CGFloat innerLeft = innerRect.origin.x;
CGFloat left = rect.origin.x;
CGPathMoveToPoint(path, NULL, innerLeft, top);
CGPathAddLineToPoint(path, NULL, innerRight, top);
CGPathAddArcToPoint(path, NULL, right, top, right, innerTop, radius);
CGPathAddLineToPoint(path, NULL, right, innerBottom);
CGPathAddArcToPoint(path, NULL, right, bottom, innerRight, bottom, radius);
CGPathAddLineToPoint(path, NULL, innerLeft, bottom);
CGPathAddArcToPoint(path, NULL, left, bottom, left, innerBottom, radius);
CGPathAddLineToPoint(path, NULL, left, innerTop);
CGPathAddArcToPoint(path, NULL, left, top, innerLeft, top, radius);
CGPathCloseSubpath(path);
}
- (void)parseAttributes:(NSDictionary *)attributes {
[super parseAttributes:attributes];
id value = nil;
if ((value = [attributes objectForKey:@"x"])) {
_x = [value floatValue];
}
if ((value = [attributes objectForKey:@"y"])) {
_y = [value floatValue];
}
if ((value = [attributes objectForKey:@"width"])) {
_width = [value floatValue];
}
if ((value = [attributes objectForKey:@"height"])) {
_height = [value floatValue];
}
if ((value = [attributes objectForKey:@"rx"])) {
_rx = [value floatValue];
}
if ((value = [attributes objectForKey:@"ry"])) {
_ry = [value floatValue];
}
CGMutablePathRef path = CGPathCreateMutable();
CGRect rect = CGRectMake(_x, _y, _width, _height);
if (_rx == 0 && _ry == 0) {
CGPathAddRect(path, NULL, rect);
}
else if (_rx == _ry) {
CGPathAddRoundedRect(path, rect, _rx);
}
else {
NSLog(@"Unsupported corner-radius configuration: rx differs from ry");
}
[self loadPath:path];
CGPathRelease(path);
}
@end