Created
May 13, 2016 05:23
-
-
Save zwaldowski/7e6eacc9dea0682690a820afa62e54ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import QuartzCore | |
private final class PathContext { | |
let body: CGPath.Element -> Void | |
init(_ body: CGPath.Element -> Void) { | |
self.body = body | |
} | |
} | |
extension CGPath { | |
enum Element { | |
case Move(to: CGPoint) | |
case Line(to: CGPoint) | |
case QuadCurve(to: CGPoint, via: CGPoint) | |
case CubicCurve(to: CGPoint, via: CGPoint, via: CGPoint) | |
case Close | |
} | |
func forEach(body: Element -> Void) { | |
let boxed = Unmanaged.passRetained(PathContext(body)) | |
defer { boxed.release() } | |
CGPathApply(self, .init(boxed.toOpaque())) { (context, ptr) in | |
let boxed = Unmanaged<PathContext>.fromOpaque(.init(context)).takeUnretainedValue() | |
let points = ptr.memory.points | |
switch ptr.memory.type { | |
case .MoveToPoint: | |
boxed.body(.Move(to: points[0])) | |
case .AddLineToPoint: | |
boxed.body(.Line(to: points[0])) | |
case .AddQuadCurveToPoint: | |
boxed.body(.QuadCurve(to: points[1], via: points[0])) | |
case .AddCurveToPoint: | |
boxed.body(.CubicCurve(to: points[2], via: points[0], via: points[1])) | |
case .CloseSubpath: | |
boxed.body(.Close) | |
} | |
} | |
} | |
} | |
let path = CGPathCreateWithRect(.init(x: 0, y: 0, width: 120, height: 240), nil) | |
path.forEach { el in | |
print(el) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment