1
1
mirror of https://github.com/exyte/Macaw.git synced 2024-09-11 05:05:23 +03:00

Move toPath() out of Locus extension to make it overridable.

Change access modifiers for helper functions.
This commit is contained in:
Eduard Sergeev 2019-03-14 11:29:55 +07:00
parent ec167bf9f7
commit fd64147a3a
2 changed files with 26 additions and 26 deletions

View File

@ -2,15 +2,15 @@ import Foundation
extension Locus {
fileprivate func rectToPath(_ rect: Rect) -> Path {
internal func rectToPath(_ rect: Rect) -> Path {
return MoveTo(x: rect.x, y: rect.y).lineTo(x: rect.x, y: rect.y + rect.h).lineTo(x: rect.x + rect.w, y: rect.y + rect.h).lineTo(x: rect.x + rect.w, y: rect.y).close().build()
}
fileprivate func circleToPath(_ circle: Circle) -> Path {
internal func circleToPath(_ circle: Circle) -> Path {
return MoveTo(x: circle.cx, y: circle.cy).m(-circle.r, 0).a(circle.r, circle.r, 0.0, true, false, circle.r * 2.0, 0.0).a(circle.r, circle.r, 0.0, true, false, -(circle.r * 2.0), 0.0).build()
}
fileprivate func arcToPath(_ arc: Arc) -> Path {
internal func arcToPath(_ arc: Arc) -> Path {
let rx = arc.ellipse.rx
let ry = arc.ellipse.ry
let cx = arc.ellipse.cx
@ -36,11 +36,11 @@ extension Locus {
return PathBuilder(segment: PathSegment(type: .M, data: [x1, y1])).A(rx, ry, 0.0, largeArcFlag, sweepFlag, x2, y2).build()
}
fileprivate func pointToPath(_ point: Point) -> Path {
internal func pointToPath(_ point: Point) -> Path {
return MoveTo(x: point.x, y: point.y).lineTo(x: point.x, y: point.y).build()
}
fileprivate func pointsToPath(_ points: [Double], close: Bool = false) -> Path {
internal func pointsToPath(_ points: [Double], close: Bool = false) -> Path {
var pb = PathBuilder(segment: PathSegment(type: .M, data: [points[0], points[1]]))
if points.count > 2 {
let parts = stride(from: 2, to: points.count, by: 2).map { Array(points[$0 ..< $0 + 2]) }
@ -53,25 +53,4 @@ extension Locus {
}
return pb.build()
}
public func toPath() -> Path {
if let rect = self as? Rect {
return rectToPath(rect)
} else if let circle = self as? Circle {
return circleToPath(circle)
} else if let arc = self as? Arc {
return arcToPath(arc)
} else if let point = self as? Point {
return MoveTo(x: point.x, y: point.y).lineTo(x: point.x, y: point.y).build()
} else if let line = self as? Line {
return MoveTo(x: line.x1, y: line.y1).lineTo(x: line.x2, y: line.y2).build()
} else if let polygon = self as? Polygon {
return pointsToPath(polygon.points, close: true)
} else if let polyline = self as? Polyline {
return pointsToPath(polyline.points)
} else if let path = self as? Path {
return path
}
fatalError("Unsupported locus: \(self)")
}
}

View File

@ -18,4 +18,25 @@ open class Locus {
open func stroke(fill: Fill = Color.black, width: Double = 1, cap: LineCap = .butt, join: LineJoin = .miter, dashes: [Double] = []) -> Shape {
return Shape(form: self, stroke: Stroke(fill: fill, width: width, cap: cap, join: join, dashes: dashes))
}
open func toPath() -> Path {
if let rect = self as? Rect {
return rectToPath(rect)
} else if let circle = self as? Circle {
return circleToPath(circle)
} else if let arc = self as? Arc {
return arcToPath(arc)
} else if let point = self as? Point {
return MoveTo(x: point.x, y: point.y).lineTo(x: point.x, y: point.y).build()
} else if let line = self as? Line {
return MoveTo(x: line.x1, y: line.y1).lineTo(x: line.x2, y: line.y2).build()
} else if let polygon = self as? Polygon {
return pointsToPath(polygon.points, close: true)
} else if let polyline = self as? Polyline {
return pointsToPath(polyline.points)
} else if let path = self as? Path {
return path
}
fatalError("Unsupported locus: \(self)")
}
}