forked from nicklockwood/ShapeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuclid+SVGPath.swift
More file actions
208 lines (200 loc) · 7.25 KB
/
Euclid+SVGPath.swift
File metadata and controls
208 lines (200 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//
// Euclid+SVGPath.swift
// ShapeScript Lib
//
// Created by Nick Lockwood on 11/04/2022.
// Copyright © 2022 Nick Lockwood. All rights reserved.
//
import Euclid
#if canImport(SVGPath)
import SVGPath
#endif
public extension SVGPoint {
/// Creates an `SVGPoint` from a Euclid `Vector`.
/// - Parameter point: A Euclid vector.
init(_ point: Vector) {
self.init(x: point.x, y: point.y)
}
}
public extension SVGPath {
/// Creates an`SVGPath` from a Euclid `Path`.
/// - Parameter path: The `Path` to convert.
init(_ path: Path) {
self.init(commands: path.subpaths.flatMap { path -> [SVGCommand] in
guard let start = path.points.first?.position else { return [] }
var commands: [SVGCommand] = [.moveTo(SVGPoint(start))]
for point in path.points.dropFirst() {
commands.append(.lineTo(.init(point.position)))
}
commands.append(.end)
return commands
})
}
}
public extension Vector {
/// Creates a `Vector` from an `SVGPoint`.
/// - Parameter svgPoint: An SVG point.
init(_ svgPoint: SVGPoint) {
self.init(svgPoint.x, svgPoint.y)
}
}
public extension Path {
/// Creates a `Path` from an `SVGPath`.
/// - Parameters:
/// - svgPath: The `SVGPath` to convert.
/// - detail: The detail level to apply when converting curves to line segments.
/// - color: An optional `Color` to apply to the path.
init(_ svgPath: SVGPath, detail: Int = 4, color: Color? = nil) {
var paths = [Path]()
var points = [PathPoint]()
var startingPoint = Vector.zero
var firstCommand: SVGCommand?
var lastCommand: SVGCommand?
func endPath() {
if points.count > 1 {
if points.count > 2, points.first == points.last,
let firstCommand = firstCommand
{
updateLastPoint(nextCommand: firstCommand)
}
paths.append(Path(points))
}
points.removeAll()
firstCommand = nil
}
func updateLastPoint(nextCommand: SVGCommand) {
if points.isEmpty {
points.append(.point(startingPoint, color: color))
return
}
guard let lastElement = lastCommand else {
return
}
let p0, p1, p2: Vector, isCurved: Bool
switch nextCommand {
case .moveTo:
points[points.count - 1].isCurved = false
return
case .end:
if let firstElement = firstCommand {
updateLastPoint(nextCommand: firstElement)
}
return
case let .lineTo(point):
p2 = Vector(point)
isCurved = false
case let .quadratic(control, _), let .cubic(control, _, _):
p2 = Vector(control)
isCurved = true
case .arc:
preconditionFailure()
}
switch lastElement {
case .moveTo, .end:
return
case let .lineTo(point):
guard points.count > 1, isCurved else {
return
}
p0 = points[points.count - 2].position
p1 = Vector(point)
case let .quadratic(control, point), let .cubic(_, control, point):
p0 = Vector(control)
p1 = Vector(point)
case .arc:
preconditionFailure()
}
let d0 = (p1 - p0).normalized()
let d1 = (p2 - p1).normalized()
let isTangent = abs(d0.dot(d1)) > 0.99
points[points.count - 1].isCurved = isTangent
}
func addCommand(_ command: SVGCommand) {
switch command {
case let .moveTo(point):
endPath()
startingPoint = Vector(point)
case let .lineTo(point):
updateLastPoint(nextCommand: command)
points.append(.point(Vector(point), color: color))
case let .quadratic(p1, p2):
updateLastPoint(nextCommand: command)
guard detail > 0 else {
points.append(.curve(Vector(p1), color: color))
points.append(.point(Vector(p2), color: color))
break
}
let detail = max(detail, 2)
var t = 0.0
let step = 1 / Double(detail)
let p0 = points.last?.position ?? startingPoint
for _ in 1 ..< detail {
t += step
points.append(.curve(
quadraticBezier(p0.x, p1.x, p2.x, t),
quadraticBezier(p0.y, p1.y, p2.y, t),
color: color
))
}
points.append(.point(Vector(p2), color: color))
case let .cubic(p1, p2, p3):
updateLastPoint(nextCommand: command)
guard detail > 0 else {
points.append(.curve(Vector(p1), color: color))
points.append(.curve(Vector(p2), color: color))
points.append(.point(Vector(p3), color: color))
break
}
let detail = max(detail * 2, 3)
var t = 0.0
let step = 1 / Double(detail)
let p0 = points.last?.position ?? startingPoint
for _ in 1 ..< detail {
t += step
points.append(.curve(
cubicBezier(p0.x, p1.x, p2.x, p3.x, t),
cubicBezier(p0.y, p1.y, p2.y, p3.y, t),
color: color
))
}
points.append(.point(Vector(p3), color: color))
case let .arc(arc):
let p0 = points.last?.position ?? startingPoint
arc.asBezierPath(from: SVGPoint(p0)).forEach(addCommand)
return
case .end:
if points.last?.position != points.first?.position {
points.append(points[0])
}
startingPoint = points.first?.position ?? .zero
endPath()
}
switch command {
case .moveTo, .end:
break
default:
firstCommand = command
}
lastCommand = command
}
svgPath.commands.forEach(addCommand)
endPath()
self.init(subpaths: paths)
}
}
private func quadraticBezier(_ p0: Double, _ p1: Double, _ p2: Double, _ t: Double) -> Double {
let oneMinusT = 1 - t
let c0 = oneMinusT * oneMinusT * p0
let c1 = 2 * oneMinusT * t * p1
let c2 = t * t * p2
return c0 + c1 + c2
}
private func cubicBezier(_ p0: Double, _ p1: Double, _ p2: Double, _ p3: Double, _ t: Double) -> Double {
let oneMinusT = 1 - t
let oneMinusTSquared = oneMinusT * oneMinusT
let c0 = oneMinusTSquared * oneMinusT * p0
let c1 = 3 * oneMinusTSquared * t * p1
let c2 = 3 * oneMinusT * t * t * p2
let c3 = t * t * t * p3
return c0 + c1 + c2 + c3
}