Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions Sources/OpenCoreGraphics/CGPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<CGAffineTransform>?
) {
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
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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(
Expand Down
78 changes: 78 additions & 0 deletions Tests/OpenCoreGraphicsShimsTests/CGPathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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..<count).map { e.points[$0] }))
}
return list
}

let roundedElems = elements(of: rounded)
let negElems = elements(of: negativeRounded)
let mutElems = elements(of: mutableRounded)
#expect(roundedElems.count == 10)
#expect(negElems.count == 10)
#expect(mutElems.count == 10)
for i in 0..<10 {
#expect(roundedElems[i].0 == negElems[i].0)
#expect(roundedElems[i].0 == mutElems[i].0)
for j in roundedElems[i].1.indices {
#expect(roundedElems[i].1[j].x.isApproximatelyEqual(to: negElems[i].1[j].x, absoluteTolerance: 1e-9))
#expect(roundedElems[i].1[j].y.isApproximatelyEqual(to: negElems[i].1[j].y, absoluteTolerance: 1e-9))
#expect(roundedElems[i].1[j].x.isApproximatelyEqual(to: mutElems[i].1[j].x, absoluteTolerance: 1e-9))
#expect(roundedElems[i].1[j].y.isApproximatelyEqual(to: mutElems[i].1[j].y, absoluteTolerance: 1e-9))
}
}

let halfRadii = CGPath(roundedRect: CGRect(x: 0, y: 0, width: 100, height: 50), cornerWidth: 50, cornerHeight: 25, transform: nil)
let halfBounds = halfRadii.boundingBoxOfPath
#expect(halfBounds.origin.x.isApproximatelyEqual(to: 0, absoluteTolerance: 1e-6))
#expect(halfBounds.origin.y.isApproximatelyEqual(to: 0, absoluteTolerance: 1e-6))
#expect(halfBounds.size.width.isApproximatelyEqual(to: 100, absoluteTolerance: 1e-6))
#expect(halfBounds.size.height.isApproximatelyEqual(to: 50, absoluteTolerance: 1e-6))

var transform = CGAffineTransform(translationX: 100, y: 200)
let transformed = CGPath(roundedRect: CGRect(x: 0, y: 0, width: 40, height: 20), cornerWidth: 5, cornerHeight: 5, transform: &transform)
let mutableTransformed = CGMutablePath()
mutableTransformed.addRoundedRect(in: CGRect(x: 0, y: 0, width: 40, height: 20), cornerWidth: 5, cornerHeight: 5, transform: transform)
let txBounds = transformed.boundingBoxOfPath
let mutTxBounds = mutableTransformed.boundingBoxOfPath
#expect(txBounds.origin.x.isApproximatelyEqual(to: 100, absoluteTolerance: 1e-6))
#expect(txBounds.origin.y.isApproximatelyEqual(to: 200, absoluteTolerance: 1e-6))
#expect(txBounds.size.width.isApproximatelyEqual(to: 40, absoluteTolerance: 1e-6))
#expect(txBounds.size.height.isApproximatelyEqual(to: 20, absoluteTolerance: 1e-6))
#expect(mutTxBounds.origin.x.isApproximatelyEqual(to: 100, absoluteTolerance: 1e-6))
#expect(mutTxBounds.origin.y.isApproximatelyEqual(to: 200, absoluteTolerance: 1e-6))
}
}
Loading