@@ -17,6 +17,16 @@ public class CGPath: Hashable {
1717 self . elements = elements
1818 }
1919
20+ /// Creates an immutable path of a rectangle.
21+ public convenience init ( rect : CGRect , transform : UnsafePointer < CGAffineTransform > ? ) {
22+ self . init ( elements : Self . rectElements ( rect , transform : transform ? . pointee ) )
23+ }
24+
25+ /// Creates an immutable path of an ellipse.
26+ public convenience init ( ellipseIn rect : CGRect , transform : UnsafePointer < CGAffineTransform > ? ) {
27+ self . init ( elements : Self . ellipseElements ( in : rect , transform : transform ? . pointee ) )
28+ }
29+
2030 public static func == ( lhs : CGPath , rhs : CGPath ) -> Bool {
2131 lhs . elements == rhs . elements
2232 }
@@ -29,6 +39,33 @@ public class CGPath: Hashable {
2939 elements . isEmpty
3040 }
3141
42+ /// The current point in a graphics path.
43+ ///
44+ /// If the path is empty, this value is `CGPoint.zero`.
45+ public var currentPoint : CGPoint {
46+ var currentPoint : CGPoint ?
47+ var subpathStart : CGPoint ?
48+
49+ for element in elements {
50+ switch element {
51+ case let . moveToPoint ( point ) :
52+ currentPoint = point
53+ subpathStart = point
54+ case let . addLineToPoint ( end ) ,
55+ let . addQuadCurveToPoint ( _, end ) ,
56+ let . addCurveToPoint ( _, _, end ) :
57+ guard currentPoint != nil else { continue }
58+ currentPoint = end
59+ case . closeSubpath :
60+ if let start = subpathStart {
61+ currentPoint = start
62+ }
63+ }
64+ }
65+
66+ return currentPoint ?? . zero
67+ }
68+
3269 /// The smallest rectangle completely enclosing all points in the path,
3370 /// not including control points for Bézier cubic and quadratic curves.
3471 ///
@@ -108,6 +145,38 @@ public class CGPath: Hashable {
108145 public func mutableCopy ( ) -> CGMutablePath ? {
109146 self as? CGMutablePath
110147 }
148+
149+ public func applyWithBlock ( _ block : ( UnsafePointer < CGPathElement > ) -> Void ) {
150+ var pointStorage = ( CGPoint . zero , CGPoint . zero , CGPoint . zero )
151+ withUnsafeMutablePointer ( to : & pointStorage ) { tuplePtr in
152+ tuplePtr . withMemoryRebound ( to : CGPoint . self , capacity : 3 ) { pointsPtr in
153+ for item in elements {
154+ let type : CGPathElementType
155+ switch item {
156+ case let . moveToPoint ( point ) :
157+ type = . moveToPoint
158+ pointsPtr [ 0 ] = point
159+ case let . addLineToPoint ( point ) :
160+ type = . addLineToPoint
161+ pointsPtr [ 0 ] = point
162+ case let . addQuadCurveToPoint ( control , destination ) :
163+ type = . addQuadCurveToPoint
164+ pointsPtr [ 0 ] = control
165+ pointsPtr [ 1 ] = destination
166+ case let . addCurveToPoint ( control1 , control2 , destination ) :
167+ type = . addCurveToPoint
168+ pointsPtr [ 0 ] = control1
169+ pointsPtr [ 1 ] = control2
170+ pointsPtr [ 2 ] = destination
171+ case . closeSubpath :
172+ type = . closeSubpath
173+ }
174+ var element = CGPathElement ( type : type , points : pointsPtr )
175+ withUnsafePointer ( to : & element ) { block ( $0 ) }
176+ }
177+ }
178+ }
179+ }
111180}
112181
113182/// For compatibility with CoreGraphics API.
@@ -149,48 +218,12 @@ public enum PathElement: Hashable {
149218
150219public extension CGPath {
151220
152- func addRect ( _ rect : CGRect ) {
153-
154- let newElements : [ Element ] = [ . moveToPoint ( CGPoint ( x : rect . minX , y : rect . minY ) ) ,
155- . addLineToPoint ( CGPoint ( x : rect . maxX , y : rect . minY ) ) ,
156- . addLineToPoint ( CGPoint ( x : rect . maxX , y : rect . maxY ) ) ,
157- . addLineToPoint ( CGPoint ( x : rect . minX , y : rect . maxY ) ) ,
158- . closeSubpath ]
159-
160- elements . append ( contentsOf : newElements )
221+ func addRect ( _ rect : CGRect , transform : CGAffineTransform = . identity ) {
222+ elements . append ( contentsOf : Self . rectElements ( rect , transform : transform == . identity ? nil : transform ) )
161223 }
162224
163- func addEllipse ( in rect : CGRect ) {
164-
165- var p = CGPoint ( )
166- var p1 = CGPoint ( )
167- var p2 = CGPoint ( )
168-
169- let hdiff = rect . width / 2 * KAPPA
170- let vdiff = rect . height / 2 * KAPPA
171-
172- p = CGPoint ( x : rect . origin . x + rect . width / 2 , y : rect . origin . y + rect . height )
173- elements . append ( . moveToPoint ( p ) )
174-
175- p = CGPoint ( x : rect . origin . x , y : rect . origin . y + rect . height / 2 )
176- p1 = CGPoint ( x : rect . origin . x + rect . width / 2 - hdiff , y : rect . origin . y + rect . height )
177- p2 = CGPoint ( x : rect . origin . x , y : rect . origin . y + rect . height / 2 + vdiff )
178- elements . append ( . addCurveToPoint ( p1 , p2 , p ) )
179-
180- p = CGPoint ( x : rect . origin . x + rect . size . width / 2 , y : rect . origin . y )
181- p1 = CGPoint ( x : rect . origin . x , y : rect . origin . y + rect . size . height / 2 - vdiff )
182- p2 = CGPoint ( x : rect . origin . x + rect . size . width / 2 - hdiff , y : rect . origin . y )
183- elements . append ( . addCurveToPoint ( p1 , p2 , p ) )
184-
185- p = CGPoint ( x : rect . origin . x + rect . size . width , y : rect . origin . y + rect . size . height / 2 )
186- p1 = CGPoint ( x : rect . origin . x + rect . size . width / 2 + hdiff , y : rect . origin . y )
187- p2 = CGPoint ( x : rect . origin . x + rect . size . width , y : rect . origin . y + rect . size . height / 2 - vdiff )
188- elements . append ( . addCurveToPoint ( p1 , p2 , p ) )
189-
190- p = CGPoint ( x : rect . origin . x + rect . size . width / 2 , y : rect . origin . y + rect . size . height )
191- p1 = CGPoint ( x : rect . origin . x + rect . size . width , y : rect . origin . y + rect . size . height / 2 + vdiff )
192- p2 = CGPoint ( x : rect . origin . x + rect . size . width / 2 + hdiff , y : rect . origin . y + rect . size . height )
193- elements . append ( . addCurveToPoint ( p1 , p2 , p ) )
225+ func addEllipse ( in rect : CGRect , transform : CGAffineTransform = . identity ) {
226+ elements . append ( contentsOf : Self . ellipseElements ( in : rect , transform : transform == . identity ? nil : transform ) )
194227 }
195228
196229 func move ( to point : CGPoint ) {
@@ -222,6 +255,62 @@ public extension CGPath {
222255// This magic number is 4 *(sqrt(2) -1)/3
223256private let KAPPA : CGFloat = 0.5522847498
224257
258+ private extension CGPath {
259+ static func rectElements ( _ rect : CGRect , transform : CGAffineTransform ? ) -> [ Element ] {
260+ let rect = rect . standardized
261+ let minX = rect . origin . x
262+ let minY = rect . origin . y
263+ let maxX = minX + rect . size . width
264+ let maxY = minY + rect . size . height
265+ var p0 = CGPoint ( x : minX , y : minY )
266+ var p1 = CGPoint ( x : maxX , y : minY )
267+ var p2 = CGPoint ( x : maxX , y : maxY )
268+ var p3 = CGPoint ( x : minX , y : maxY )
269+ if let transform {
270+ p0 = p0 . applying ( transform )
271+ p1 = p1 . applying ( transform )
272+ p2 = p2 . applying ( transform )
273+ p3 = p3 . applying ( transform )
274+ }
275+ return [
276+ . moveToPoint ( p0 ) ,
277+ . addLineToPoint ( p1 ) ,
278+ . addLineToPoint ( p2 ) ,
279+ . addLineToPoint ( p3 ) ,
280+ . closeSubpath ,
281+ ]
282+ }
283+
284+ static func ellipseElements ( in rect : CGRect , transform : CGAffineTransform ? ) -> [ Element ] {
285+ let rect = rect . standardized
286+ let rx = Double ( rect . size . width ) * 0.5
287+ let ry = Double ( rect . size . height ) * 0.5
288+ var matrix = CGAffineTransform (
289+ a : rx ,
290+ b : 0 ,
291+ c : 0 ,
292+ d : ry ,
293+ tx : Double ( rect . origin . x ) + rx ,
294+ ty : Double ( rect . origin . y ) + ry
295+ )
296+ if let transform {
297+ matrix = matrix . concatenating ( transform )
298+ }
299+ func point ( _ x : CGFloat , _ y : CGFloat ) -> CGPoint {
300+ CGPoint ( x : x , y : y ) . applying ( matrix )
301+ }
302+ let p0 = point ( 1 , 0 )
303+ return [
304+ . moveToPoint ( p0 ) ,
305+ . addCurveToPoint ( point ( 1 , KAPPA ) , point ( KAPPA , 1 ) , point ( 0 , 1 ) ) ,
306+ . addCurveToPoint ( point ( - KAPPA , 1 ) , point ( - 1 , KAPPA ) , point ( - 1 , 0 ) ) ,
307+ . addCurveToPoint ( point ( - 1 , - KAPPA ) , point ( - KAPPA , - 1 ) , point ( 0 , - 1 ) ) ,
308+ . addCurveToPoint ( point ( KAPPA , - 1 ) , point ( 1 , - KAPPA ) , p0 ) ,
309+ . closeSubpath ,
310+ ]
311+ }
312+ }
313+
225314private func expandQuadCurveBounds (
226315 _ p0 : CGFloat ,
227316 _ p1 : CGFloat ,
@@ -291,9 +380,9 @@ public struct CGPathElement {
291380
292381 public var type : CGPathElementType
293382
294- public var points : ( CGPoint , CGPoint , CGPoint )
383+ public var points : UnsafeMutablePointer < CGPoint >
295384
296- public init ( type : CGPathElementType , points : ( CGPoint , CGPoint , CGPoint ) ) {
385+ public init ( type : CGPathElementType , points : UnsafeMutablePointer < CGPoint > ) {
297386
298387 self . type = type
299388 self . points = points
@@ -314,76 +403,42 @@ public enum CGPathFillRule: Int {
314403}
315404
316405/// The type of element found in a path.
317- public enum CGPathElementType {
406+ public enum CGPathElementType : Int32 , Sendable {
318407
319408 /// The path element that starts a new subpath. The element holds a single point for the destination.
320- case moveToPoint
409+ case moveToPoint = 0
321410
322411 /// The path element that adds a line from the current point to a new point.
323412 /// The element holds a single point for the destination.
324- case addLineToPoint
413+ case addLineToPoint = 1
325414
326415 /// The path element that adds a quadratic curve from the current point to the specified point.
327416 /// The element holds a control point and a destination point.
328- case addQuadCurveToPoint
417+ case addQuadCurveToPoint = 2
329418
330419 /// The path element that adds a cubic curve from the current point to the specified point.
331420 /// The element holds two control points and a destination point.
332- case addCurveToPoint
421+ case addCurveToPoint = 3
333422
334423 /// The path element that closes and completes a subpath. The element does not contain any points.
335- case closeSubpath
424+ case closeSubpath = 4
336425}
337426
338427// MARK: - Silica Conversion
339428
340- public extension CGPathElement {
341-
342- init ( _ element : PathElement ) {
343-
344- switch element {
345-
346- case let . moveToPoint ( point ) :
347-
348- self . type = . moveToPoint
349- self . points = ( point , CGPoint ( ) , CGPoint ( ) )
350-
351- case let . addLineToPoint ( point ) :
352-
353- self . type = . addLineToPoint
354- self . points = ( point , CGPoint ( ) , CGPoint ( ) )
355-
356- case let . addQuadCurveToPoint ( control , destination ) :
357-
358- self . type = . addQuadCurveToPoint
359- self . points = ( control , destination , CGPoint ( ) )
360-
361- case let . addCurveToPoint ( control1 , control2 , destination ) :
362-
363- self . type = . addCurveToPoint
364- self . points = ( control1 , control2 , destination )
365-
366- case . closeSubpath :
367-
368- self . type = . closeSubpath
369- self . points = ( CGPoint ( ) , CGPoint ( ) , CGPoint ( ) )
370- }
371- }
372- }
373-
374429public extension PathElement {
375430
376431 init ( _ element : CGPathElement ) {
377432
378433 switch element . type {
379434
380- case . moveToPoint : self = . moveToPoint ( element . points . 0 )
435+ case . moveToPoint : self = . moveToPoint ( element . points [ 0 ] )
381436
382- case . addLineToPoint : self = . addLineToPoint ( element . points . 0 )
437+ case . addLineToPoint : self = . addLineToPoint ( element . points [ 0 ] )
383438
384- case . addQuadCurveToPoint : self = . addQuadCurveToPoint ( element . points . 0 , element . points . 1 )
439+ case . addQuadCurveToPoint : self = . addQuadCurveToPoint ( element . points [ 0 ] , element . points [ 1 ] )
385440
386- case . addCurveToPoint : self = . addCurveToPoint ( element . points . 0 , element . points . 1 , element . points . 2 )
441+ case . addCurveToPoint : self = . addCurveToPoint ( element . points [ 0 ] , element . points [ 1 ] , element . points [ 2 ] )
387442
388443 case . closeSubpath : self = . closeSubpath
389444 }
0 commit comments