1
1
mirror of https://github.com/exyte/Macaw.git synced 2024-08-15 08:00:31 +03:00

Add toMacaw for CGPath

This commit is contained in:
Alisa Mylnikova 2020-05-28 15:34:37 +07:00
parent c7c78266fc
commit 195f828f77

View File

@ -153,3 +153,52 @@ public extension Node {
}
}
extension CGPath {
public func toMacaw() -> Path {
func createPathSegment(type: PathSegmentType, points: UnsafeMutablePointer<CGPoint>, count: Int) -> PathSegment {
var data = [Double]()
for index in 0..<count {
let point = points[index]
data.append(contentsOf: [Double(point.x), Double(point.y)])
}
return PathSegment(type: type, data: data)
}
var segments = [PathSegment]()
self.forEach(body: { (element: CGPathElement) in
let segment: PathSegment
switch element.type {
case .moveToPoint:
segment = createPathSegment(type: .M, points: element.points, count: 1)
case .addLineToPoint:
segment = createPathSegment(type: .L, points: element.points, count: 1)
case .addQuadCurveToPoint:
segment = createPathSegment(type: .Q, points: element.points, count: 2)
case .addCurveToPoint:
segment = createPathSegment(type: .C, points: element.points, count: 3)
case .closeSubpath:
segment = PathSegment(type: .z)
@unknown default:
fatalError()
}
segments.append(segment)
})
return Path(segments: segments, fillRule: .nonzero)
}
private func forEach( body: @escaping @convention(block) (CGPathElement) -> Void) {
typealias Body = @convention(block) (CGPathElement) -> Void
func callback(info: UnsafeMutableRawPointer?, element: UnsafePointer<CGPathElement>) {
let body = unsafeBitCast(info, to: Body.self)
body(element.pointee)
}
let unsafeBody = unsafeBitCast(body, to: UnsafeMutableRawPointer.self)
self.apply(info: unsafeBody, function: callback)
}
}