From b068e03a6ce8c1dcc93222932446091f7397828b Mon Sep 17 00:00:00 2001 From: Kyle Date: Sun, 22 Nov 2026 14:44:47 +0800 Subject: [PATCH] feat(path): add CGPath roundedRect initializer and addRoundedRect --- Sources/OpenCoreGraphics/CGPath.swift | 72 +++++++++++++++++ .../CGPathTests.swift | 78 +++++++++++++++++++ 2 files changed, 150 insertions(+) diff --git a/Sources/OpenCoreGraphics/CGPath.swift b/Sources/OpenCoreGraphics/CGPath.swift index 1420fd4..267fe13 100644 --- a/Sources/OpenCoreGraphics/CGPath.swift +++ b/Sources/OpenCoreGraphics/CGPath.swift @@ -27,6 +27,23 @@ public class CGPath: Hashable { self.init(elements: Self.ellipseElements(in: rect, transform: transform?.pointee)) } + /// Creates an immutable path of a rounded rectangle. + public convenience init( + roundedRect rect: CGRect, + cornerWidth: CGFloat, + cornerHeight: CGFloat, + transform: UnsafePointer? + ) { + self.init( + elements: Self.roundedRectElements( + in: rect, + cornerWidth: cornerWidth, + cornerHeight: cornerHeight, + transform: transform?.pointee + ) + ) + } + public static func == (lhs: CGPath, rhs: CGPath) -> Bool { lhs.elements == rhs.elements } @@ -226,6 +243,22 @@ public extension CGPath { elements.append(contentsOf: Self.ellipseElements(in: rect, transform: transform == .identity ? nil : transform)) } + func addRoundedRect( + in rect: CGRect, + cornerWidth: CGFloat, + cornerHeight: CGFloat, + transform: CGAffineTransform = .identity + ) { + elements.append( + contentsOf: Self.roundedRectElements( + in: rect, + cornerWidth: cornerWidth, + cornerHeight: cornerHeight, + transform: transform == .identity ? nil : transform + ) + ) + } + func move(to point: CGPoint) { elements.append(.moveToPoint(point)) @@ -309,6 +342,45 @@ private extension CGPath { .closeSubpath, ] } + + static func roundedRectElements( + in rect: CGRect, + cornerWidth: CGFloat, + cornerHeight: CGFloat, + transform: CGAffineTransform? + ) -> [Element] { + let rect = rect.standardized + precondition( + cornerWidth >= 0 && 2 * cornerWidth <= rect.size.width && + cornerHeight >= 0 && 2 * cornerHeight <= rect.size.height + ) + if cornerWidth == 0 || cornerHeight == 0 || rect.isNull { + return rectElements(rect, transform: transform) + } + let minX = rect.origin.x + let minY = rect.origin.y + let maxX = minX + rect.size.width + let maxY = minY + rect.size.height + let midY = minY + rect.size.height * 0.5 + let dx = cornerWidth * (1 - KAPPA) + let dy = cornerHeight * (1 - KAPPA) + func point(_ x: CGFloat, _ y: CGFloat) -> CGPoint { + let p = CGPoint(x: x, y: y) + return transform.map { p.applying($0) } ?? p + } + return [ + .moveToPoint(point(maxX, midY)), + .addLineToPoint(point(maxX, maxY - cornerHeight)), + .addCurveToPoint(point(maxX, maxY - dy), point(maxX - dx, maxY), point(maxX - cornerWidth, maxY)), + .addLineToPoint(point(minX + cornerWidth, maxY)), + .addCurveToPoint(point(minX + dx, maxY), point(minX, maxY - dy), point(minX, maxY - cornerHeight)), + .addLineToPoint(point(minX, minY + cornerHeight)), + .addCurveToPoint(point(minX, minY + dy), point(minX + dx, minY), point(minX + cornerWidth, minY)), + .addLineToPoint(point(maxX - cornerWidth, minY)), + .addCurveToPoint(point(maxX - dx, minY), point(maxX, minY + dy), point(maxX, minY + cornerHeight)), + .closeSubpath, + ] + } } private func expandQuadCurveBounds( diff --git a/Tests/OpenCoreGraphicsShimsTests/CGPathTests.swift b/Tests/OpenCoreGraphicsShimsTests/CGPathTests.swift index 051ab2f..dfb66d3 100644 --- a/Tests/OpenCoreGraphicsShimsTests/CGPathTests.swift +++ b/Tests/OpenCoreGraphicsShimsTests/CGPathTests.swift @@ -233,4 +233,82 @@ struct CGPathTests { let nullEllipse = CGPath(ellipseIn: .null, transform: nil) #expect(nullEllipse.currentPoint == CGPoint(x: CGFloat.infinity, y: CGFloat.infinity)) } + + @Test + func roundedRectInitializerAndTransform() { + let nullRounded = CGPath(roundedRect: .null, cornerWidth: 0, cornerHeight: 0, transform: nil) + #expect(!nullRounded.isEmpty) + #expect(nullRounded.boundingBoxOfPath == .null) + + let rect = CGRect(x: 5, y: 6, width: 100, height: 50) + #expect(CGPath(roundedRect: rect, cornerWidth: 0, cornerHeight: 10, transform: nil) == CGPath(rect: rect, transform: nil)) + #expect(CGPath(roundedRect: rect, cornerWidth: 10, cornerHeight: 0, transform: nil) == CGPath(rect: rect, transform: nil)) + + let rounded = CGPath(roundedRect: rect, cornerWidth: 10, cornerHeight: 20, transform: nil) + let negativeRounded = CGPath(roundedRect: CGRect(x: 105, y: 56, width: -100, height: -50), cornerWidth: 10, cornerHeight: 20, transform: nil) + let mutableRounded = CGMutablePath() + mutableRounded.addRoundedRect(in: rect, cornerWidth: 10, cornerHeight: 20) + + let bounds = rounded.boundingBoxOfPath + #expect(bounds.origin.x.isApproximatelyEqual(to: 5, absoluteTolerance: 1e-6)) + #expect(bounds.origin.y.isApproximatelyEqual(to: 6, absoluteTolerance: 1e-6)) + #expect(bounds.size.width.isApproximatelyEqual(to: 100, absoluteTolerance: 1e-6)) + #expect(bounds.size.height.isApproximatelyEqual(to: 50, absoluteTolerance: 1e-6)) + #expect(rounded.currentPoint.x.isApproximatelyEqual(to: 105, absoluteTolerance: 1e-6)) + #expect(rounded.currentPoint.y.isApproximatelyEqual(to: 31, absoluteTolerance: 1e-6)) + + func elements(of path: CGPath) -> [(Int32, [CGPoint])] { + var list: [(Int32, [CGPoint])] = [] + path.applyWithBlock { ptr in + let e = ptr.pointee + let count: Int + switch e.type { + case .moveToPoint, .addLineToPoint: count = 1 + case .addQuadCurveToPoint: count = 2 + case .addCurveToPoint: count = 3 + case .closeSubpath: count = 0 + @unknown default: count = 0 + } + list.append((e.type.rawValue, (0..