UIBezierPath.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Kindly stolen from https://github.com/BigZaphod/Chameleon
  2. /*
  3. * Copyright (c) 2011, The Iconfactory. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * 3. Neither the name of The Iconfactory nor the names of its contributors may
  16. * be used to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE ICONFACTORY BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  27. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <TargetConditionals.h>
  31. #if !TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
  32. #import "UIBezierPath.h"
  33. @implementation UIBezierPath {
  34. CGFloat *_lineDashPattern;
  35. NSInteger _lineDashCount;
  36. CGFloat _lineDashPhase;
  37. }
  38. @synthesize CGPath = _path;
  39. - (id)init {
  40. self = [super init];
  41. if (self) {
  42. _path = CGPathCreateMutable();
  43. _lineWidth = 1;
  44. _lineCapStyle = kCGLineCapButt;
  45. _lineJoinStyle = kCGLineJoinMiter;
  46. _miterLimit = 10;
  47. _flatness = 0.6;
  48. _usesEvenOddFillRule = NO;
  49. _lineDashPattern = NULL;
  50. _lineDashCount = 0;
  51. _lineDashPhase = 0;
  52. }
  53. return self;
  54. }
  55. - (void)dealloc {
  56. if (_path) CGPathRelease(_path);
  57. }
  58. - (id)copyWithZone:(NSZone *)zone {
  59. UIBezierPath *copy = [[self class] new];
  60. copy.CGPath = self.CGPath;
  61. copy.lineWidth = self.lineWidth;
  62. copy.lineCapStyle = self.lineCapStyle;
  63. copy.lineJoinStyle = self.lineJoinStyle;
  64. copy.miterLimit = self.miterLimit;
  65. copy.flatness = self.flatness;
  66. copy.usesEvenOddFillRule = self.usesEvenOddFillRule;
  67. NSInteger lineDashCount = 0;
  68. [self getLineDash:NULL count:&lineDashCount phase:NULL];
  69. if (lineDashCount > 0) {
  70. CGFloat *lineDashPattern = malloc(sizeof(CGFloat) * lineDashCount);
  71. CGFloat lineDashPhase = 0;
  72. [self getLineDash:lineDashPattern count:NULL phase:&lineDashPhase];
  73. [copy setLineDash:lineDashPattern count:lineDashCount phase:lineDashPhase];
  74. free(lineDashPattern);
  75. }
  76. return copy;
  77. }
  78. + (UIBezierPath *)bezierPathWithCGPath:(CGPathRef)CGPath {
  79. NSAssert(CGPath != NULL, @"CGPath must not be NULL");
  80. UIBezierPath *bezierPath = [[self alloc] init];
  81. bezierPath.CGPath = CGPath;
  82. return bezierPath;
  83. }
  84. + (UIBezierPath *)bezierPath {
  85. UIBezierPath *bezierPath = [[self alloc] init];
  86. return bezierPath;
  87. }
  88. + (UIBezierPath *)bezierPathWithRect:(CGRect)rect {
  89. CGMutablePathRef path = CGPathCreateMutable();
  90. CGPathAddRect(path, NULL, rect);
  91. UIBezierPath *bezierPath = [[self alloc] init];
  92. bezierPath->_path = path;
  93. return bezierPath;
  94. }
  95. + (UIBezierPath *)bezierPathWithOvalInRect:(CGRect)rect {
  96. CGMutablePathRef path = CGPathCreateMutable();
  97. CGPathAddEllipseInRect(path, NULL, rect);
  98. UIBezierPath *bezierPath = [[self alloc] init];
  99. bezierPath->_path = path;
  100. return bezierPath;
  101. }
  102. + (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect
  103. cornerRadius:(CGFloat)cornerRadius {
  104. return [self bezierPathWithRoundedRect:rect
  105. byRoundingCorners:UIRectCornerAllCorners
  106. cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
  107. }
  108. + (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect
  109. byRoundingCorners:(UIRectCorner)corners
  110. cornerRadii:(CGSize)cornerRadii {
  111. CGMutablePathRef path = CGPathCreateMutable();
  112. const CGPoint topLeft = rect.origin;
  113. const CGPoint topRight = CGPointMake(CGRectGetMaxX(rect), CGRectGetMinY(rect));
  114. const CGPoint bottomRight = CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect));
  115. const CGPoint bottomLeft = CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect));
  116. if (corners & UIRectCornerTopLeft) {
  117. CGPathMoveToPoint(path, NULL, topLeft.x + cornerRadii.width, topLeft.y);
  118. } else {
  119. CGPathMoveToPoint(path, NULL, topLeft.x, topLeft.y);
  120. }
  121. if (corners & UIRectCornerTopRight) {
  122. CGPathAddLineToPoint(path, NULL, topRight.x - cornerRadii.width, topRight.y);
  123. CGPathAddCurveToPoint(path, NULL, topRight.x, topRight.y, topRight.x, topRight.y + cornerRadii.height, topRight.x, topRight.y + cornerRadii.height);
  124. } else {
  125. CGPathAddLineToPoint(path, NULL, topRight.x, topRight.y);
  126. }
  127. if (corners & UIRectCornerBottomRight) {
  128. CGPathAddLineToPoint(path, NULL, bottomRight.x, bottomRight.y - cornerRadii.height);
  129. CGPathAddCurveToPoint(path, NULL, bottomRight.x, bottomRight.y, bottomRight.x - cornerRadii.width, bottomRight.y, bottomRight.x - cornerRadii.width, bottomRight.y);
  130. } else {
  131. CGPathAddLineToPoint(path, NULL, bottomRight.x, bottomRight.y);
  132. }
  133. if (corners & UIRectCornerBottomLeft) {
  134. CGPathAddLineToPoint(path, NULL, bottomLeft.x + cornerRadii.width, bottomLeft.y);
  135. CGPathAddCurveToPoint(path, NULL, bottomLeft.x, bottomLeft.y, bottomLeft.x, bottomLeft.y - cornerRadii.height, bottomLeft.x, bottomLeft.y - cornerRadii.height);
  136. } else {
  137. CGPathAddLineToPoint(path, NULL, bottomLeft.x, bottomLeft.y);
  138. }
  139. if (corners & UIRectCornerTopLeft) {
  140. CGPathAddLineToPoint(path, NULL, topLeft.x, topLeft.y + cornerRadii.height);
  141. CGPathAddCurveToPoint(path, NULL, topLeft.x, topLeft.y, topLeft.x + cornerRadii.width, topLeft.y, topLeft.x + cornerRadii.width, topLeft.y);
  142. } else {
  143. CGPathAddLineToPoint(path, NULL, topLeft.x, topLeft.y);
  144. }
  145. CGPathCloseSubpath(path);
  146. UIBezierPath *bezierPath = [[self alloc] init];
  147. bezierPath->_path = path;
  148. return bezierPath;
  149. }
  150. + (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center
  151. radius:(CGFloat)radius
  152. startAngle:(CGFloat)startAngle
  153. endAngle:(CGFloat)endAngle
  154. clockwise:(BOOL)clockwise {
  155. CGMutablePathRef path = CGPathCreateMutable();
  156. CGPathAddArc(path, NULL, center.x, center.y, radius, startAngle, endAngle, clockwise);
  157. UIBezierPath *bezierPath = [[self alloc] init];
  158. bezierPath->_path = path;
  159. return bezierPath;
  160. }
  161. - (void)moveToPoint:(CGPoint)point {
  162. CGMutablePathRef mutablePath = CGPathCreateMutableCopy(_path);
  163. CGPathMoveToPoint(mutablePath, NULL, point.x, point.y);
  164. self.CGPath = mutablePath;
  165. CGPathRelease(mutablePath);
  166. }
  167. - (void)addLineToPoint:(CGPoint)point {
  168. CGMutablePathRef mutablePath = CGPathCreateMutableCopy(_path);
  169. CGPathAddLineToPoint(mutablePath, NULL, point.x, point.y);
  170. self.CGPath = mutablePath;
  171. CGPathRelease(mutablePath);
  172. }
  173. - (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise {
  174. CGMutablePathRef mutablePath = CGPathCreateMutableCopy(_path);
  175. CGPathAddArc(mutablePath, NULL, center.x, center.y, radius, startAngle, endAngle, clockwise);
  176. self.CGPath = mutablePath;
  177. CGPathRelease(mutablePath);
  178. }
  179. - (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2 {
  180. CGMutablePathRef mutablePath = CGPathCreateMutableCopy(_path);
  181. CGPathAddCurveToPoint(mutablePath, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPoint.x, endPoint.y);
  182. self.CGPath = mutablePath;
  183. CGPathRelease(mutablePath);
  184. }
  185. - (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint {
  186. CGMutablePathRef mutablePath = CGPathCreateMutableCopy(_path);
  187. CGPathAddQuadCurveToPoint(mutablePath, NULL, controlPoint.x, controlPoint.y, endPoint.x, endPoint.y);
  188. self.CGPath = mutablePath;
  189. CGPathRelease(mutablePath);
  190. }
  191. - (void)closePath {
  192. CGMutablePathRef mutablePath = CGPathCreateMutableCopy(_path);
  193. CGPathCloseSubpath(mutablePath);
  194. self.CGPath = mutablePath;
  195. CGPathRelease(mutablePath);
  196. }
  197. - (void)removeAllPoints {
  198. CGMutablePathRef mutablePath = CGPathCreateMutable();
  199. self.CGPath = mutablePath;
  200. CGPathRelease(mutablePath);
  201. }
  202. - (void)appendPath:(UIBezierPath *)bezierPath {
  203. if (bezierPath) {
  204. CGMutablePathRef mutablePath = CGPathCreateMutableCopy(_path);
  205. CGPathAddPath(mutablePath, NULL, bezierPath.CGPath);
  206. self.CGPath = mutablePath;
  207. CGPathRelease(mutablePath);
  208. }
  209. }
  210. - (void)setCGPath:(CGPathRef)path {
  211. NSAssert(path != NULL, @"path must not be NULL");
  212. if (path != _path) {
  213. if (_path) CGPathRelease(_path);
  214. _path = CGPathCreateCopy(path);
  215. }
  216. }
  217. - (CGPoint)currentPoint {
  218. return CGPathGetCurrentPoint(_path);
  219. }
  220. - (void)setLineDash:(const CGFloat *)pattern count:(NSInteger)count phase:(CGFloat)phase {
  221. free(_lineDashPattern);
  222. if (pattern && count > 0) {
  223. const size_t size = sizeof(CGFloat) * count;
  224. _lineDashPattern = malloc(size);
  225. bcopy(pattern, _lineDashPattern, size);
  226. } else {
  227. _lineDashPattern = NULL;
  228. }
  229. _lineDashCount = count;
  230. _lineDashPhase = phase;
  231. }
  232. - (void)getLineDash:(CGFloat *)pattern count:(NSInteger *)count phase:(CGFloat *)phase {
  233. if (pattern && _lineDashPattern && _lineDashCount > 0) {
  234. const size_t size = sizeof(CGFloat) * _lineDashCount;
  235. bcopy(_lineDashPattern, pattern, size);
  236. }
  237. if (count) {
  238. *count = _lineDashCount;
  239. }
  240. if (phase) {
  241. *phase = _lineDashPhase;
  242. }
  243. }
  244. - (BOOL)containsPoint:(CGPoint)point {
  245. return CGPathContainsPoint(_path, NULL, point, _usesEvenOddFillRule);
  246. }
  247. - (BOOL)isEmpty {
  248. return CGPathIsEmpty(_path);
  249. }
  250. - (CGRect)bounds {
  251. return CGPathGetBoundingBox(_path);
  252. }
  253. - (void)applyTransform:(CGAffineTransform)transform {
  254. CGMutablePathRef mutablePath = CGPathCreateMutable();
  255. CGPathAddPath(mutablePath, &transform, _path);
  256. self.CGPath = mutablePath;
  257. CGPathRelease(mutablePath);
  258. }
  259. @end
  260. #endif