From dfb2933f93fc362c6d4f443d3b42d044a85a5a33 Mon Sep 17 00:00:00 2001 From: khoi Date: Mon, 26 May 2025 08:51:34 +0700 Subject: [PATCH 1/4] Implement CGPath applyWithBlock method for path element iteration - Add public applyWithBlock method to CGPath extension for WASI/Linux - Convert internal PathElement types to CGPathElement for API compatibility - Enable block-based iteration over path elements using unsafe pointers - Support all path element types including curves and subpath operations --- Source/CoreGraphicsPolyfill.swift | 44 ++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/Source/CoreGraphicsPolyfill.swift b/Source/CoreGraphicsPolyfill.swift index aa7de7e..7600188 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] = [ @@ -203,7 +245,7 @@ import Foundation self.points = points } } - + /// 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. From a320f1ff465a65255f3f7015817788502d3b4d2c Mon Sep 17 00:00:00 2001 From: khoi Date: Mon, 26 May 2025 09:09:48 +0700 Subject: [PATCH 2/4] Implement subscript for CGPathElement point access --- Source/CoreGraphicsPolyfill.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Source/CoreGraphicsPolyfill.swift b/Source/CoreGraphicsPolyfill.swift index 7600188..70b7d44 100644 --- a/Source/CoreGraphicsPolyfill.swift +++ b/Source/CoreGraphicsPolyfill.swift @@ -244,6 +244,15 @@ import Foundation self.type = type self.points = points } + + public subscript(index: Int) -> CGPoint { + switch index { + case 0: return points.0 + case 1: return points.1 + case 2: return points.2 + default: fatalError("Index out of bounds") + } + } } /// Rules for determining which regions are interior to a path. From d1f11e6b1bbae4eeb2c4bb958778cd9ee4233bcc Mon Sep 17 00:00:00 2001 From: khoi Date: Mon, 26 May 2025 09:31:16 +0700 Subject: [PATCH 3/4] Refactor CGPathElement to use array instead of tuple for points --- Source/CoreGraphicsPolyfill.swift | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/Source/CoreGraphicsPolyfill.swift b/Source/CoreGraphicsPolyfill.swift index 70b7d44..36ec038 100644 --- a/Source/CoreGraphicsPolyfill.swift +++ b/Source/CoreGraphicsPolyfill.swift @@ -234,24 +234,13 @@ 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 - } - - public subscript(index: Int) -> CGPoint { - switch index { - case 0: return points.0 - case 1: return points.1 - case 2: return points.2 - default: fatalError("Index out of bounds") - } + self.points = [points.0, points.1, points.2] } } From 605604b14bf66bfac6e18fd343e8650a08d95b2b Mon Sep 17 00:00:00 2001 From: khoi Date: Mon, 26 May 2025 09:34:05 +0700 Subject: [PATCH 4/4] Update CGPathElement test for array-based points access --- Tests/CoreGraphicsPolyfillTests/PolyfillTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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