Skip to content

Commit ea168f4

Browse files
committed
Add cgPathFallback property to NSBezierPath for macOS versions < 14 to support conversion to CGPath
1 parent 509d7b2 commit ea168f4

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// NSBezierPath+CGPathFallback.swift
3+
// CodeEditTextView
4+
//
5+
// Created by Tom Ludwig on 27.11.24.
6+
//
7+
8+
import AppKit
9+
10+
extension NSBezierPath {
11+
/// Converts the `NSBezierPath` instance into a `CGPath`, providing a fallback method for compatibility(macOS < 14).
12+
public var cgPathFallback: CGPath {
13+
let path = CGMutablePath()
14+
var points = [CGPoint](repeating: .zero, count: 3)
15+
16+
for index in 0 ..< elementCount {
17+
let type = element(at: index, associatedPoints: &points)
18+
switch type {
19+
case .moveTo:
20+
path.move(to: points[0])
21+
case .lineTo:
22+
path.addLine(to: points[0])
23+
case .curveTo:
24+
path.addCurve(to: points[2], control1: points[0], control2: points[1])
25+
case .closePath:
26+
path.closeSubpath()
27+
@unknown default:
28+
continue
29+
}
30+
}
31+
32+
return path
33+
}
34+
}

0 commit comments

Comments
 (0)