diff --git a/Sources/OpenCoreGraphics/CGPath.swift b/Sources/OpenCoreGraphics/CGPath.swift index 0244b8f..1420fd4 100644 --- a/Sources/OpenCoreGraphics/CGPath.swift +++ b/Sources/OpenCoreGraphics/CGPath.swift @@ -17,6 +17,16 @@ public class CGPath: Hashable { self.elements = elements } + /// Creates an immutable path of a rectangle. + public convenience init(rect: CGRect, transform: UnsafePointer?) { + self.init(elements: Self.rectElements(rect, transform: transform?.pointee)) + } + + /// Creates an immutable path of an ellipse. + public convenience init(ellipseIn rect: CGRect, transform: UnsafePointer?) { + self.init(elements: Self.ellipseElements(in: rect, transform: transform?.pointee)) + } + public static func == (lhs: CGPath, rhs: CGPath) -> Bool { lhs.elements == rhs.elements } @@ -29,9 +39,144 @@ public class CGPath: Hashable { elements.isEmpty } + /// The current point in a graphics path. + /// + /// If the path is empty, this value is `CGPoint.zero`. + public var currentPoint: CGPoint { + var currentPoint: CGPoint? + var subpathStart: CGPoint? + + for element in elements { + switch element { + case let .moveToPoint(point): + currentPoint = point + subpathStart = point + case let .addLineToPoint(end), + let .addQuadCurveToPoint(_, end), + let .addCurveToPoint(_, _, end): + guard currentPoint != nil else { continue } + currentPoint = end + case .closeSubpath: + if let start = subpathStart { + currentPoint = start + } + } + } + + return currentPoint ?? .zero + } + + /// The smallest rectangle completely enclosing all points in the path, + /// not including control points for Bézier cubic and quadratic curves. + /// + /// If the path is empty, this value is `CGRect.null`. + public var boundingBoxOfPath: CGRect { + var minX = CGFloat.infinity + var minY = CGFloat.infinity + var maxX = -CGFloat.infinity + var maxY = -CGFloat.infinity + + func include(_ point: CGPoint) { + minX = min(minX, point.x) + minY = min(minY, point.y) + maxX = max(maxX, point.x) + maxY = max(maxY, point.y) + } + + var currentPoint: CGPoint? + var subpathStart: CGPoint? + var pendingMove: CGPoint? + + for element in elements { + switch element { + case let .moveToPoint(point): + guard point.x.isFinite, point.y.isFinite else { continue } + currentPoint = point + subpathStart = point + pendingMove = point + case let .addLineToPoint(end): + guard end.x.isFinite, end.y.isFinite, let start = currentPoint else { continue } + pendingMove = nil + include(start) + include(end) + currentPoint = end + case let .addQuadCurveToPoint(control, end): + guard control.x.isFinite, control.y.isFinite, + end.x.isFinite, end.y.isFinite, + let start = currentPoint else { continue } + pendingMove = nil + include(start) + include(end) + expandQuadCurveBounds(start.x, control.x, end.x, minValue: &minX, maxValue: &maxX) + expandQuadCurveBounds(start.y, control.y, end.y, minValue: &minY, maxValue: &maxY) + currentPoint = end + case let .addCurveToPoint(control1, control2, end): + guard control1.x.isFinite, control1.y.isFinite, + control2.x.isFinite, control2.y.isFinite, + end.x.isFinite, end.y.isFinite, + let start = currentPoint else { continue } + pendingMove = nil + include(start) + include(end) + expandCubicCurveBounds(start.x, control1.x, control2.x, end.x, minValue: &minX, maxValue: &maxX) + expandCubicCurveBounds(start.y, control1.y, control2.y, end.y, minValue: &minY, maxValue: &maxY) + currentPoint = end + case .closeSubpath: + if let pending = pendingMove { + include(pending) + pendingMove = nil + } + if let start = subpathStart { + currentPoint = start + } + } + } + + if let pending = pendingMove { + include(pending) + } + + guard minX <= maxX, minY <= maxY else { + return .null + } + return CGRect(x: minX, y: minY, width: maxX - minX, height: maxY - minY) + } + public func mutableCopy() -> CGMutablePath? { self as? CGMutablePath } + + public func applyWithBlock(_ block: (UnsafePointer) -> Void) { + var pointStorage = (CGPoint.zero, CGPoint.zero, CGPoint.zero) + withUnsafeMutablePointer(to: &pointStorage) { tuplePtr in + tuplePtr.withMemoryRebound(to: CGPoint.self, capacity: 3) { pointsPtr in + for item in elements { + let type: CGPathElementType + switch item { + case let .moveToPoint(point): + type = .moveToPoint + pointsPtr[0] = point + case let .addLineToPoint(point): + type = .addLineToPoint + pointsPtr[0] = point + case let .addQuadCurveToPoint(control, destination): + type = .addQuadCurveToPoint + pointsPtr[0] = control + pointsPtr[1] = destination + case let .addCurveToPoint(control1, control2, destination): + type = .addCurveToPoint + pointsPtr[0] = control1 + pointsPtr[1] = control2 + pointsPtr[2] = destination + case .closeSubpath: + type = .closeSubpath + } + var element = CGPathElement(type: type, points: pointsPtr) + withUnsafePointer(to: &element) { block($0) } + } + } + } + } } /// For compatibility with CoreGraphics API. @@ -73,48 +218,12 @@ public enum PathElement: Hashable { public extension CGPath { - func addRect(_ rect: CGRect) { - - let newElements: [Element] = [.moveToPoint(CGPoint(x: rect.minX, y: rect.minY)), - .addLineToPoint(CGPoint(x: rect.maxX, y: rect.minY)), - .addLineToPoint(CGPoint(x: rect.maxX, y: rect.maxY)), - .addLineToPoint(CGPoint(x: rect.minX, y: rect.maxY)), - .closeSubpath] - - elements.append(contentsOf: newElements) + func addRect(_ rect: CGRect, transform: CGAffineTransform = .identity) { + elements.append(contentsOf: Self.rectElements(rect, transform: transform == .identity ? nil : transform)) } - func addEllipse(in rect: CGRect) { - - var p = CGPoint() - var p1 = CGPoint() - var p2 = CGPoint() - - let hdiff = rect.width / 2 * KAPPA - let vdiff = rect.height / 2 * KAPPA - - p = CGPoint(x: rect.origin.x + rect.width / 2, y: rect.origin.y + rect.height) - elements.append(.moveToPoint(p)) - - p = CGPoint(x: rect.origin.x, y: rect.origin.y + rect.height / 2) - p1 = CGPoint(x: rect.origin.x + rect.width / 2 - hdiff, y: rect.origin.y + rect.height) - p2 = CGPoint(x: rect.origin.x, y: rect.origin.y + rect.height / 2 + vdiff) - elements.append(.addCurveToPoint(p1, p2, p)) - - p = CGPoint(x: rect.origin.x + rect.size.width / 2, y: rect.origin.y) - p1 = CGPoint(x: rect.origin.x, y: rect.origin.y + rect.size.height / 2 - vdiff) - p2 = CGPoint(x: rect.origin.x + rect.size.width / 2 - hdiff, y: rect.origin.y) - elements.append(.addCurveToPoint(p1, p2, p)) - - p = CGPoint(x: rect.origin.x + rect.size.width, y: rect.origin.y + rect.size.height / 2) - p1 = CGPoint(x: rect.origin.x + rect.size.width / 2 + hdiff, y: rect.origin.y) - p2 = CGPoint(x: rect.origin.x + rect.size.width, y: rect.origin.y + rect.size.height / 2 - vdiff) - elements.append(.addCurveToPoint(p1, p2, p)) - - p = CGPoint(x: rect.origin.x + rect.size.width / 2, y: rect.origin.y + rect.size.height) - p1 = CGPoint(x: rect.origin.x + rect.size.width, y: rect.origin.y + rect.size.height / 2 + vdiff) - p2 = CGPoint(x: rect.origin.x + rect.size.width / 2 + hdiff, y: rect.origin.y + rect.size.height) - elements.append(.addCurveToPoint(p1, p2, p)) + func addEllipse(in rect: CGRect, transform: CGAffineTransform = .identity) { + elements.append(contentsOf: Self.ellipseElements(in: rect, transform: transform == .identity ? nil : transform)) } func move(to point: CGPoint) { @@ -146,15 +255,134 @@ public extension CGPath { // This magic number is 4 *(sqrt(2) -1)/3 private let KAPPA: CGFloat = 0.5522847498 +private extension CGPath { + static func rectElements(_ rect: CGRect, transform: CGAffineTransform?) -> [Element] { + let rect = rect.standardized + let minX = rect.origin.x + let minY = rect.origin.y + let maxX = minX + rect.size.width + let maxY = minY + rect.size.height + var p0 = CGPoint(x: minX, y: minY) + var p1 = CGPoint(x: maxX, y: minY) + var p2 = CGPoint(x: maxX, y: maxY) + var p3 = CGPoint(x: minX, y: maxY) + if let transform { + p0 = p0.applying(transform) + p1 = p1.applying(transform) + p2 = p2.applying(transform) + p3 = p3.applying(transform) + } + return [ + .moveToPoint(p0), + .addLineToPoint(p1), + .addLineToPoint(p2), + .addLineToPoint(p3), + .closeSubpath, + ] + } + + static func ellipseElements(in rect: CGRect, transform: CGAffineTransform?) -> [Element] { + let rect = rect.standardized + let rx = Double(rect.size.width) * 0.5 + let ry = Double(rect.size.height) * 0.5 + var matrix = CGAffineTransform( + a: rx, + b: 0, + c: 0, + d: ry, + tx: Double(rect.origin.x) + rx, + ty: Double(rect.origin.y) + ry + ) + if let transform { + matrix = matrix.concatenating(transform) + } + func point(_ x: CGFloat, _ y: CGFloat) -> CGPoint { + CGPoint(x: x, y: y).applying(matrix) + } + let p0 = point(1, 0) + return [ + .moveToPoint(p0), + .addCurveToPoint(point(1, KAPPA), point(KAPPA, 1), point(0, 1)), + .addCurveToPoint(point(-KAPPA, 1), point(-1, KAPPA), point(-1, 0)), + .addCurveToPoint(point(-1, -KAPPA), point(-KAPPA, -1), point(0, -1)), + .addCurveToPoint(point(KAPPA, -1), point(1, -KAPPA), p0), + .closeSubpath, + ] + } +} + +private func expandQuadCurveBounds( + _ p0: CGFloat, + _ p1: CGFloat, + _ p2: CGFloat, + minValue: inout CGFloat, + maxValue: inout CGFloat +) { + let denom = p0 - 2 * p1 + p2 + guard denom != 0 else { return } + let t = (p0 - p1) / denom + guard t > 0, t < 1 else { return } + let u = 1 - t + let value = u * u * p0 + 2 * u * t * p1 + t * t * p2 + minValue = min(minValue, value) + maxValue = max(maxValue, value) +} + +private func expandCubicCurveRoot( + _ p0: CGFloat, + _ p1: CGFloat, + _ p2: CGFloat, + _ p3: CGFloat, + t: CGFloat, + minValue: inout CGFloat, + maxValue: inout CGFloat +) { + guard t > 0, t < 1 else { return } + let u = 1 - t + let value = u * u * u * p0 + 3 * u * u * t * p1 + 3 * u * t * t * p2 + t * t * t * p3 + minValue = min(minValue, value) + maxValue = max(maxValue, value) +} + +private func expandCubicCurveBounds( + _ p0: CGFloat, + _ p1: CGFloat, + _ p2: CGFloat, + _ p3: CGFloat, + minValue: inout CGFloat, + maxValue: inout CGFloat +) { + let a = -p0 + 3 * p1 - 3 * p2 + p3 + let b = 2 * (p0 - 2 * p1 + p2) + let c = p1 - p0 + if a == 0 { + guard b != 0 else { return } + expandCubicCurveRoot(p0, p1, p2, p3, t: -c / b, minValue: &minValue, maxValue: &maxValue) + return + } + let discriminant = b * b - 4 * a * c + guard discriminant >= 0 else { return } + if discriminant == 0 { + expandCubicCurveRoot(p0, p1, p2, p3, t: -b / (2 * a), minValue: &minValue, maxValue: &maxValue) + return + } + let root = discriminant.squareRoot() + let q = -0.5 * (b + (b >= 0 ? root : -root)) + expandCubicCurveRoot(p0, p1, p2, p3, t: q / a, minValue: &minValue, maxValue: &maxValue) + if q != 0 { + expandCubicCurveRoot(p0, p1, p2, p3, t: c / q, minValue: &minValue, maxValue: &maxValue) + } +} + // MARK: - CoreGraphics API public struct CGPathElement { public var type: CGPathElementType - public var points: (CGPoint, CGPoint, CGPoint) + public var points: UnsafeMutablePointer - public init(type: CGPathElementType, points: (CGPoint, CGPoint, CGPoint)) { + public init(type: CGPathElementType, points: UnsafeMutablePointer) { self.type = type self.points = points @@ -175,76 +403,42 @@ public enum CGPathFillRule: Int { } /// The type of element found in a path. -public enum CGPathElementType { +public enum CGPathElementType: Int32, Sendable { /// The path element that starts a new subpath. The element holds a single point for the destination. - case moveToPoint + case moveToPoint = 0 /// The path element that adds a line from the current point to a new point. /// The element holds a single point for the destination. - case addLineToPoint + case addLineToPoint = 1 /// The path element that adds a quadratic curve from the current point to the specified point. /// The element holds a control point and a destination point. - case addQuadCurveToPoint + case addQuadCurveToPoint = 2 /// The path element that adds a cubic curve from the current point to the specified point. /// The element holds two control points and a destination point. - case addCurveToPoint + case addCurveToPoint = 3 /// The path element that closes and completes a subpath. The element does not contain any points. - case closeSubpath + case closeSubpath = 4 } // MARK: - Silica Conversion -public extension CGPathElement { - - init(_ element: PathElement) { - - switch element { - - case let .moveToPoint(point): - - self.type = .moveToPoint - self.points = (point, CGPoint(), CGPoint()) - - case let .addLineToPoint(point): - - self.type = .addLineToPoint - self.points = (point, CGPoint(), CGPoint()) - - case let .addQuadCurveToPoint(control, destination): - - self.type = .addQuadCurveToPoint - self.points = (control, destination, CGPoint()) - - case let .addCurveToPoint(control1, control2, destination): - - self.type = .addCurveToPoint - self.points = (control1, control2, destination) - - case .closeSubpath: - - self.type = .closeSubpath - self.points = (CGPoint(), CGPoint(), CGPoint()) - } - } -} - public extension PathElement { init(_ element: CGPathElement) { switch element.type { - case .moveToPoint: self = .moveToPoint(element.points.0) + case .moveToPoint: self = .moveToPoint(element.points[0]) - case .addLineToPoint: self = .addLineToPoint(element.points.0) + case .addLineToPoint: self = .addLineToPoint(element.points[0]) - case .addQuadCurveToPoint: self = .addQuadCurveToPoint(element.points.0, element.points.1) + case .addQuadCurveToPoint: self = .addQuadCurveToPoint(element.points[0], element.points[1]) - case .addCurveToPoint: self = .addCurveToPoint(element.points.0, element.points.1, element.points.2) + case .addCurveToPoint: self = .addCurveToPoint(element.points[0], element.points[1], element.points[2]) case .closeSubpath: self = .closeSubpath } diff --git a/Tests/OpenCoreGraphicsShimsTests/CGPathTests.swift b/Tests/OpenCoreGraphicsShimsTests/CGPathTests.swift new file mode 100644 index 0000000..051ab2f --- /dev/null +++ b/Tests/OpenCoreGraphicsShimsTests/CGPathTests.swift @@ -0,0 +1,236 @@ +// +// CGPathTests.swift +// OpenCoreGraphicsShimsTests + +import Numerics +import OpenCoreGraphicsShims +import Testing +#if OPENCOREGRAPHICS_COREGRAPHICS +import struct CoreGraphics.CGAffineTransform +#else +import struct OpenCoreGraphics.CGAffineTransform +#endif + +struct CGPathTests { + @Test + func emptyAndInvalidPathsReturnNullBoundingBox() { + let empty = CGMutablePath() + #expect(empty.boundingBoxOfPath.isNull) + + let lineWithoutMove = CGMutablePath() + lineWithoutMove.addLine(to: CGPoint(x: 10, y: 20)) + #expect(lineWithoutMove.boundingBoxOfPath.isNull) + + let nanMove = CGMutablePath() + nanMove.move(to: CGPoint(x: CGFloat.nan, y: 0)) + #expect(nanMove.boundingBoxOfPath.isNull) + } + + @Test + func moveCoalescingAndSubpathClosureBounds() { + let singleMove = CGMutablePath() + singleMove.move(to: CGPoint(x: 10, y: 20)) + #expect(singleMove.boundingBoxOfPath == CGRect(x: 10, y: 20, width: 0, height: 0)) + + let consecutiveMoves = CGMutablePath() + consecutiveMoves.move(to: CGPoint(x: 100, y: 200)) + consecutiveMoves.move(to: CGPoint(x: 10, y: 20)) + consecutiveMoves.addLine(to: CGPoint(x: 30, y: 40)) + #expect(consecutiveMoves.boundingBoxOfPath == CGRect(x: 10, y: 20, width: 20, height: 20)) + + let trailingMove = CGMutablePath() + trailingMove.move(to: CGPoint(x: 10, y: 20)) + trailingMove.addLine(to: CGPoint(x: 30, y: 40)) + trailingMove.move(to: CGPoint(x: 100, y: 200)) + #expect(trailingMove.boundingBoxOfPath == CGRect(x: 10, y: 20, width: 90, height: 180)) + + let closedThenLine = CGMutablePath() + closedThenLine.move(to: CGPoint(x: 10, y: 20)) + closedThenLine.addLine(to: CGPoint(x: 30, y: 40)) + closedThenLine.closeSubpath() + closedThenLine.addLine(to: CGPoint(x: 5, y: 5)) + #expect(closedThenLine.boundingBoxOfPath == CGRect(x: 5, y: 5, width: 25, height: 35)) + } + + @Test + func rectAndEllipseBoundingBoxes() { + let negativeRectPath = CGMutablePath() + negativeRectPath.addRect(CGRect(x: 10, y: 20, width: -6, height: -8)) + #expect(negativeRectPath.boundingBoxOfPath == CGRect(x: 4, y: 12, width: 6, height: 8)) + + let ellipsePath = CGMutablePath() + ellipsePath.addEllipse(in: CGRect(x: 10, y: 20, width: 40, height: 60)) + let bounds = ellipsePath.boundingBoxOfPath + #expect(bounds.origin.x.isApproximatelyEqual(to: 10, absoluteTolerance: 1e-6)) + #expect(bounds.origin.y.isApproximatelyEqual(to: 20, absoluteTolerance: 1e-6)) + #expect(bounds.size.width.isApproximatelyEqual(to: 40, absoluteTolerance: 1e-6)) + #expect(bounds.size.height.isApproximatelyEqual(to: 60, absoluteTolerance: 1e-6)) + } + + @Test + func rectInitializerAndTransform() { + let nullRect = CGPath(rect: .null, transform: nil) + #expect(!nullRect.isEmpty) + #expect(nullRect.boundingBoxOfPath == .null) + #expect(CGPath(rect: CGRect(x: CGFloat.infinity, y: 2, width: 10, height: 20), transform: nil) == nullRect) + + let rect = CGRect(x: 10, y: 20, width: 30, height: 40) + let mutableRect = CGMutablePath() + mutableRect.addRect(rect) + #expect(CGPath(rect: rect, transform: nil) == mutableRect) + + let negativeRect = CGPath(rect: CGRect(x: 10, y: 20, width: -6, height: -8), transform: nil) + #expect(negativeRect == CGPath(rect: CGRect(x: 4, y: 12, width: 6, height: 8), transform: nil)) + #expect(negativeRect.boundingBoxOfPath == CGRect(x: 4, y: 12, width: 6, height: 8)) + + var transform = CGAffineTransform(a: 2, b: 1, c: -1, d: 3, tx: 5, ty: 7) + let transformedRect = CGPath(rect: rect, transform: &transform) + let mutableTransformedRect = CGMutablePath() + mutableTransformedRect.addRect(rect, transform: transform) + #expect(transformedRect == mutableTransformedRect) + #expect(transformedRect.boundingBoxOfPath == CGRect(x: -35, y: 77, width: 100, height: 150)) + } + + @Test + func ellipseInitializerAndTransform() { + let nullEllipse = CGPath(ellipseIn: .null, transform: nil) + #expect(!nullEllipse.isEmpty) + #expect(nullEllipse.boundingBoxOfPath == .null) + + let rect = CGRect(x: 10, y: 20, width: 40, height: 60) + let mutableEllipse = CGMutablePath() + mutableEllipse.addEllipse(in: rect) + #expect(CGPath(ellipseIn: rect, transform: nil) == mutableEllipse) + + let negativeEllipse = CGPath(ellipseIn: CGRect(x: 10, y: 20, width: -40, height: -60), transform: nil) + #expect(negativeEllipse == CGPath(ellipseIn: CGRect(x: -30, y: -40, width: 40, height: 60), transform: nil)) + let negativeBounds = negativeEllipse.boundingBoxOfPath + #expect(negativeBounds.origin.x.isApproximatelyEqual(to: -30, absoluteTolerance: 1e-6)) + #expect(negativeBounds.origin.y.isApproximatelyEqual(to: -40, absoluteTolerance: 1e-6)) + #expect(negativeBounds.size.width.isApproximatelyEqual(to: 40, absoluteTolerance: 1e-6)) + #expect(negativeBounds.size.height.isApproximatelyEqual(to: 60, absoluteTolerance: 1e-6)) + + var transform = CGAffineTransform(a: 2, b: 1, c: -1, d: 3, tx: 5, ty: 7) + let transformedEllipse = CGPath(ellipseIn: rect, transform: &transform) + let mutableTransformedEllipse = CGMutablePath() + mutableTransformedEllipse.addEllipse(in: rect, transform: transform) + #expect(transformedEllipse == mutableTransformedEllipse) + let transformedBounds = transformedEllipse.boundingBoxOfPath + #expect(transformedBounds.origin.x.isApproximatelyEqual(to: -35.0029425866, absoluteTolerance: 1e-6)) + #expect(transformedBounds.origin.y.isApproximatelyEqual(to: 94.7848067788, absoluteTolerance: 1e-6)) + #expect(transformedBounds.size.width.isApproximatelyEqual(to: 100.0058851731, absoluteTolerance: 1e-6)) + #expect(transformedBounds.size.height.isApproximatelyEqual(to: 184.4303864424, absoluteTolerance: 1e-6)) + } + + @Test + func quadAndCubicCurveTightExtremaExcludeControlPoints() { + let quad = CGMutablePath() + quad.move(to: CGPoint(x: 0, y: 0)) + quad.addQuadCurve(to: CGPoint(x: 100, y: 0), control: CGPoint(x: 50, y: 100)) + let quadBounds = quad.boundingBoxOfPath + #expect(quadBounds.origin.x.isApproximatelyEqual(to: 0, absoluteTolerance: 1e-6)) + #expect(quadBounds.origin.y.isApproximatelyEqual(to: 0, absoluteTolerance: 1e-6)) + #expect(quadBounds.size.width.isApproximatelyEqual(to: 100, absoluteTolerance: 1e-6)) + #expect(quadBounds.size.height.isApproximatelyEqual(to: 50, absoluteTolerance: 1e-6)) + + let cubic = CGMutablePath() + cubic.move(to: CGPoint(x: 0, y: 0)) + cubic.addCurve( + to: CGPoint(x: 100, y: 0), + control1: CGPoint(x: 0, y: 100), + control2: CGPoint(x: 100, y: -100) + ) + let cubicBounds = cubic.boundingBoxOfPath + let expectedPeak = 50 / CGFloat(3).squareRoot() + #expect(cubicBounds.origin.x.isApproximatelyEqual(to: 0, absoluteTolerance: 1e-6)) + #expect(cubicBounds.origin.y.isApproximatelyEqual(to: -expectedPeak, absoluteTolerance: 1e-6)) + #expect(cubicBounds.size.width.isApproximatelyEqual(to: 100, absoluteTolerance: 1e-6)) + #expect(cubicBounds.size.height.isApproximatelyEqual(to: 2 * expectedPeak, absoluteTolerance: 1e-6)) + } + + @Test + func applyWithBlockVisitsElementsInOrder() { + let path = CGMutablePath() + path.move(to: CGPoint(x: 1, y: 2)) + path.addLine(to: CGPoint(x: 3, y: 4)) + path.addQuadCurve(to: CGPoint(x: 7, y: 8), control: CGPoint(x: 5, y: 6)) + path.addCurve(to: CGPoint(x: 13, y: 14), control1: CGPoint(x: 9, y: 10), control2: CGPoint(x: 11, y: 12)) + path.closeSubpath() + + var recorded: [(Int32, [CGPoint])] = [] + path.applyWithBlock { elementPtr in + let element = elementPtr.pointee + let count: Int + switch element.type { + case .moveToPoint, .addLineToPoint: count = 1 + case .addQuadCurveToPoint: count = 2 + case .addCurveToPoint: count = 3 + case .closeSubpath: count = 0 + @unknown default: count = 0 + } + let points = (0..