diff --git a/Source/CoreGraphicsPolyfill.swift b/Source/CoreGraphicsPolyfill.swift index aa7de7e..36ec038 100644 --- a/Source/CoreGraphicsPolyfill.swift +++ b/Source/CoreGraphicsPolyfill.swift @@ -116,6 +116,48 @@ import Foundation return CGRect(x: minX, y: minY, width: maxX - minX, height: maxY - minY) } + public func applyWithBlock(_ block: (UnsafePointer) -> Void) { + for element in elements { + let cgPathElement: CGPathElement + + switch element { + case .moveToPoint(let point): + cgPathElement = CGPathElement( + type: .moveToPoint, + points: (point, CGPoint.zero, CGPoint.zero) + ) + + case .addLineToPoint(let point): + cgPathElement = CGPathElement( + type: .addLineToPoint, + points: (point, CGPoint.zero, CGPoint.zero) + ) + + case .addQuadCurveToPoint(let control, let point): + cgPathElement = CGPathElement( + type: .addQuadCurveToPoint, + points: (control, point, CGPoint.zero) + ) + + case .addCurveToPoint(let control1, let control2, let point): + cgPathElement = CGPathElement( + type: .addCurveToPoint, + points: (control1, control2, point) + ) + + case .closeSubpath: + cgPathElement = CGPathElement( + type: .closeSubpath, + points: (CGPoint.zero, CGPoint.zero, CGPoint.zero) + ) + } + + withUnsafePointer(to: cgPathElement) { pointer in + block(pointer) + } + } + } + public func addRect(_ rect: CGRect) { let newElements: [Element] = [ @@ -192,18 +234,16 @@ import Foundation } public struct CGPathElement { - public var type: CGPathElementType - public var points: (CGPoint, CGPoint, CGPoint) + public var points: [CGPoint] public init(type: CGPathElementType, points: (CGPoint, CGPoint, CGPoint)) { - self.type = type - self.points = points + self.points = [points.0, points.1, points.2] } } - + /// Rules for determining which regions are interior to a path. /// /// When filling a path, regions that a fill rule defines as interior to the path are painted. diff --git a/Tests/CoreGraphicsPolyfillTests/PolyfillTests.swift b/Tests/CoreGraphicsPolyfillTests/PolyfillTests.swift index 01671ba..3bab052 100644 --- a/Tests/CoreGraphicsPolyfillTests/PolyfillTests.swift +++ b/Tests/CoreGraphicsPolyfillTests/PolyfillTests.swift @@ -421,7 +421,7 @@ final class PolyfillTests: XCTestCase { ) XCTAssertEqual(element.type, .moveToPoint) - XCTAssertEqual(element.points.0, CGPoint(x: 1, y: 2)) + XCTAssertEqual(element.points[0], CGPoint(x: 1, y: 2)) } // MARK: - CGPathElementType Tests