@nativue/core 0.15.3 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +13 -2
- package/android/nativue.settings.gradle.kts +29 -9
- package/android/src/main/java/dev/nativue/Bridge.kt +4 -14
- package/android/src/main/java/dev/nativue/Navigation.kt +31 -37
- package/android/src/main/java/dev/nativue/NavigationIcons.kt +24 -0
- package/android/src/main/java/dev/nativue/Plugins.kt +13 -1
- package/android/src/main/java/dev/nativue/Views.kt +53 -31
- package/android/src/main/res/drawable/nativue_arrow_back.xml +14 -0
- package/android/src/main/res/drawable/nativue_close.xml +14 -0
- package/cli/link.mjs +25 -6
- package/cli/nativue.mjs +0 -1
- package/ios/AppLifecycle.swift +15 -0
- package/ios/Bridge.swift +40 -20
- package/ios/ImageView.swift +32 -10
- package/ios/ModalController.swift +17 -6
- package/ios/NavigationIcons.swift +49 -0
- package/ios/Plugins.swift +13 -1
- package/ios/PressableView.swift +3 -1
- package/ios/ScreenController.swift +6 -3
- package/ios/Style.swift +1 -1
- package/package.json +1 -1
- package/src/native-components.ts +0 -14
- package/src/renderer.ts +173 -8
- package/src/router.ts +20 -7
- package/android/src/main/java/dev/nativue/SvgView.kt +0 -443
- package/ios/SvgView.swift +0 -464
package/ios/SvgView.swift
DELETED
|
@@ -1,464 +0,0 @@
|
|
|
1
|
-
import UIKit
|
|
2
|
-
|
|
3
|
-
/// View behind <Svg>: draws SVG elements ([tag, attributes] pairs, the shape of Lucide's icon nodes),
|
|
4
|
-
/// scaled from `viewBox` to its bounds like preserveAspectRatio="xMidYMid meet". Elements are stroked
|
|
5
|
-
/// with `color` (round caps and joins, as icon sets expect); those whose fill isn't "none" are also
|
|
6
|
-
/// filled with it.
|
|
7
|
-
/// Limitation: Only the shapes icon sets use and a single color; add per-element colors, gradients or
|
|
8
|
-
/// transforms when a real-world SVG needs them.
|
|
9
|
-
final class SvgView: UIView {
|
|
10
|
-
var viewBox = CGRect(x: 0, y: 0, width: 24, height: 24) {
|
|
11
|
-
didSet { setNeedsLayout() }
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
var strokeWidth: CGFloat = 2 {
|
|
15
|
-
didSet { setNeedsLayout() }
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
var color: UIColor = .black {
|
|
19
|
-
didSet { updateColors() }
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
var elements: [[Any]] = [] {
|
|
23
|
-
didSet {
|
|
24
|
-
buildPaths()
|
|
25
|
-
setNeedsLayout()
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
private let fillLayer = CAShapeLayer()
|
|
30
|
-
private let strokeLayer = CAShapeLayer()
|
|
31
|
-
private var strokePath = CGMutablePath()
|
|
32
|
-
private var fillPath = CGMutablePath()
|
|
33
|
-
|
|
34
|
-
override init(frame: CGRect) {
|
|
35
|
-
super.init(frame: frame)
|
|
36
|
-
|
|
37
|
-
isUserInteractionEnabled = false // a press on an icon presses what contains it
|
|
38
|
-
strokeLayer.fillColor = nil
|
|
39
|
-
strokeLayer.lineCap = .round
|
|
40
|
-
strokeLayer.lineJoin = .round
|
|
41
|
-
fillLayer.strokeColor = nil
|
|
42
|
-
layer.addSublayer(fillLayer)
|
|
43
|
-
layer.addSublayer(strokeLayer)
|
|
44
|
-
updateColors()
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
required init?(coder: NSCoder) {
|
|
48
|
-
fatalError("init(coder:) is not supported")
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/// [minX, minY, width, height], as sent from JS.
|
|
52
|
-
static func rect(fromViewBox value: Any?) -> CGRect? {
|
|
53
|
-
let numbers = (value as? [NSNumber])?.map { CGFloat($0.doubleValue) } ?? []
|
|
54
|
-
guard numbers.count == 4 else { return nil }
|
|
55
|
-
|
|
56
|
-
return CGRect(x: numbers[0], y: numbers[1], width: numbers[2], height: numbers[3])
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/// A tab bar or header item's icon: `systemImage` is an SF Symbol name, `svg` ({ viewBox, elements }) an
|
|
60
|
-
/// icon from @nativue/icons.
|
|
61
|
-
static func iconImage(_ item: [String: Any], size: CGFloat = 25) -> UIImage? {
|
|
62
|
-
if let name = item["systemImage"] as? String { return UIImage(systemName: name) }
|
|
63
|
-
guard let svg = item["svg"] as? [String: Any] else { return nil }
|
|
64
|
-
|
|
65
|
-
return templateImage(elements: svg["elements"] as? [[Any]] ?? [], viewBox: svg["viewBox"], size: size)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/// The elements drawn into a template image (tinted by its container, like SF Symbols), sized like
|
|
69
|
-
/// a tab bar icon.
|
|
70
|
-
static func templateImage(elements: [[Any]], viewBox: Any?, size: CGFloat = 25) -> UIImage {
|
|
71
|
-
let svgView = SvgView(frame: CGRect(x: 0, y: 0, width: size, height: size))
|
|
72
|
-
if let rect = rect(fromViewBox: viewBox) { svgView.viewBox = rect }
|
|
73
|
-
svgView.elements = elements
|
|
74
|
-
svgView.layoutIfNeeded()
|
|
75
|
-
|
|
76
|
-
let image = UIGraphicsImageRenderer(bounds: svgView.bounds).image { context in
|
|
77
|
-
svgView.layer.render(in: context.cgContext)
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return image.withRenderingMode(.alwaysTemplate)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
override func layoutSubviews() {
|
|
84
|
-
super.layoutSubviews()
|
|
85
|
-
|
|
86
|
-
guard viewBox.width > 0, viewBox.height > 0 else { return }
|
|
87
|
-
|
|
88
|
-
let scale = min(bounds.width / viewBox.width, bounds.height / viewBox.height)
|
|
89
|
-
var transform = CGAffineTransform(
|
|
90
|
-
translationX: (bounds.width - viewBox.width * scale) / 2,
|
|
91
|
-
y: (bounds.height - viewBox.height * scale) / 2
|
|
92
|
-
)
|
|
93
|
-
.scaledBy(x: scale, y: scale)
|
|
94
|
-
.translatedBy(x: -viewBox.minX, y: -viewBox.minY)
|
|
95
|
-
|
|
96
|
-
CATransaction.begin()
|
|
97
|
-
CATransaction.setDisableActions(true) // shape layers would animate every path change
|
|
98
|
-
for shapeLayer in [fillLayer, strokeLayer] { shapeLayer.frame = bounds }
|
|
99
|
-
strokeLayer.path = strokePath.copy(using: &transform)
|
|
100
|
-
fillLayer.path = fillPath.copy(using: &transform)
|
|
101
|
-
strokeLayer.lineWidth = strokeWidth * scale
|
|
102
|
-
CATransaction.commit()
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
private func updateColors() {
|
|
106
|
-
strokeLayer.strokeColor = color.cgColor
|
|
107
|
-
fillLayer.fillColor = color.cgColor
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
private func buildPaths() {
|
|
111
|
-
strokePath = CGMutablePath()
|
|
112
|
-
fillPath = CGMutablePath()
|
|
113
|
-
|
|
114
|
-
for element in elements {
|
|
115
|
-
guard
|
|
116
|
-
let tag = element.first as? String,
|
|
117
|
-
let attributes = element.last as? [String: Any],
|
|
118
|
-
let shape = Self.shape(tag, SvgAttributes(values: attributes))
|
|
119
|
-
else { continue }
|
|
120
|
-
|
|
121
|
-
strokePath.addPath(shape)
|
|
122
|
-
if let fill = attributes["fill"] as? String, fill != "none" { fillPath.addPath(shape) }
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
private static func shape(_ tag: String, _ attributes: SvgAttributes) -> CGPath? {
|
|
127
|
-
switch tag {
|
|
128
|
-
case "path":
|
|
129
|
-
var parser = SvgPathParser(attributes.text("d"))
|
|
130
|
-
return parser.parse()
|
|
131
|
-
|
|
132
|
-
case "circle":
|
|
133
|
-
let radius = attributes.number("r")
|
|
134
|
-
return CGPath(ellipseIn: ellipseBounds(attributes, radiusX: radius, radiusY: radius), transform: nil)
|
|
135
|
-
|
|
136
|
-
case "ellipse":
|
|
137
|
-
let bounds = ellipseBounds(attributes, radiusX: attributes.number("rx"), radiusY: attributes.number("ry"))
|
|
138
|
-
return CGPath(ellipseIn: bounds, transform: nil)
|
|
139
|
-
|
|
140
|
-
case "rect":
|
|
141
|
-
return roundedRect(attributes)
|
|
142
|
-
|
|
143
|
-
case "line":
|
|
144
|
-
let path = CGMutablePath()
|
|
145
|
-
path.move(to: CGPoint(x: attributes.number("x1"), y: attributes.number("y1")))
|
|
146
|
-
path.addLine(to: CGPoint(x: attributes.number("x2"), y: attributes.number("y2")))
|
|
147
|
-
return path
|
|
148
|
-
|
|
149
|
-
case "polyline", "polygon":
|
|
150
|
-
let path = CGMutablePath()
|
|
151
|
-
var parser = SvgPathParser(attributes.text("points"))
|
|
152
|
-
path.addLines(between: parser.points())
|
|
153
|
-
if tag == "polygon" { path.closeSubpath() }
|
|
154
|
-
return path
|
|
155
|
-
|
|
156
|
-
default:
|
|
157
|
-
return nil
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
private static func ellipseBounds(_ attributes: SvgAttributes, radiusX: CGFloat, radiusY: CGFloat) -> CGRect {
|
|
162
|
-
CGRect(
|
|
163
|
-
x: attributes.number("cx") - radiusX,
|
|
164
|
-
y: attributes.number("cy") - radiusY,
|
|
165
|
-
width: radiusX * 2,
|
|
166
|
-
height: radiusY * 2
|
|
167
|
-
)
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/// rx and ry default to each other, and can't exceed half the side (like SVG).
|
|
171
|
-
private static func roundedRect(_ attributes: SvgAttributes) -> CGPath {
|
|
172
|
-
let rect = CGRect(
|
|
173
|
-
x: attributes.number("x"),
|
|
174
|
-
y: attributes.number("y"),
|
|
175
|
-
width: attributes.number("width"),
|
|
176
|
-
height: attributes.number("height")
|
|
177
|
-
)
|
|
178
|
-
let radiusX = attributes.optionalNumber("rx") ?? attributes.optionalNumber("ry") ?? 0
|
|
179
|
-
let radiusY = attributes.optionalNumber("ry") ?? radiusX
|
|
180
|
-
|
|
181
|
-
return CGPath(
|
|
182
|
-
roundedRect: rect,
|
|
183
|
-
cornerWidth: min(radiusX, rect.width / 2),
|
|
184
|
-
cornerHeight: min(radiusY, rect.height / 2),
|
|
185
|
-
transform: nil
|
|
186
|
-
)
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/// Attribute values arrive as strings ("12") or numbers.
|
|
191
|
-
private struct SvgAttributes {
|
|
192
|
-
let values: [String: Any]
|
|
193
|
-
|
|
194
|
-
func text(_ name: String) -> String {
|
|
195
|
-
values[name].map { "\($0)" } ?? ""
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
func optionalNumber(_ name: String) -> CGFloat? {
|
|
199
|
-
if let number = values[name] as? NSNumber { return CGFloat(number.doubleValue) }
|
|
200
|
-
|
|
201
|
-
return (values[name] as? String).flatMap(Double.init).map { CGFloat($0) }
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
func number(_ name: String) -> CGFloat {
|
|
205
|
-
optionalNumber(name) ?? 0
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/// Parses SVG path data ("M12 2a10 10 0 1 0 0 20z"): every command, absolute and relative, including
|
|
210
|
-
/// the compact forms optimizers emit ("l.486.9", arc flags written "01").
|
|
211
|
-
private struct SvgPathParser {
|
|
212
|
-
private let characters: [Character]
|
|
213
|
-
private var index = 0
|
|
214
|
-
|
|
215
|
-
init(_ text: String) {
|
|
216
|
-
characters = Array(text)
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/// "x,y x,y" (polyline / polygon points).
|
|
220
|
-
mutating func points() -> [CGPoint] {
|
|
221
|
-
var points: [CGPoint] = []
|
|
222
|
-
while let x = number(), let y = number() { points.append(CGPoint(x: x, y: y)) }
|
|
223
|
-
|
|
224
|
-
return points
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
mutating func parse() -> CGPath {
|
|
228
|
-
let path = CGMutablePath()
|
|
229
|
-
var command: Character = "M"
|
|
230
|
-
var current = CGPoint.zero
|
|
231
|
-
var subpathStart = CGPoint.zero
|
|
232
|
-
var lastCubicControl: CGPoint?
|
|
233
|
-
var lastQuadraticControl: CGPoint?
|
|
234
|
-
|
|
235
|
-
while true {
|
|
236
|
-
skipSeparators()
|
|
237
|
-
guard index < characters.count else { break }
|
|
238
|
-
|
|
239
|
-
if characters[index].isLetter {
|
|
240
|
-
command = characters[index]
|
|
241
|
-
index += 1
|
|
242
|
-
} else if command == "Z" || command == "z" {
|
|
243
|
-
break // numbers can't follow a close
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
let isRelative = command.isLowercase
|
|
247
|
-
let origin = isRelative ? current : .zero
|
|
248
|
-
var cubicControl: CGPoint?
|
|
249
|
-
var quadraticControl: CGPoint?
|
|
250
|
-
|
|
251
|
-
switch command.uppercased() {
|
|
252
|
-
case "M":
|
|
253
|
-
guard let point = point(from: origin) else { return path }
|
|
254
|
-
path.move(to: point)
|
|
255
|
-
current = point
|
|
256
|
-
subpathStart = point
|
|
257
|
-
command = isRelative ? "l" : "L" // further pairs are lines
|
|
258
|
-
|
|
259
|
-
case "L":
|
|
260
|
-
guard let point = point(from: origin) else { return path }
|
|
261
|
-
path.addLine(to: point)
|
|
262
|
-
current = point
|
|
263
|
-
|
|
264
|
-
case "H":
|
|
265
|
-
guard let x = number() else { return path }
|
|
266
|
-
current = CGPoint(x: x + origin.x, y: current.y)
|
|
267
|
-
path.addLine(to: current)
|
|
268
|
-
|
|
269
|
-
case "V":
|
|
270
|
-
guard let y = number() else { return path }
|
|
271
|
-
current = CGPoint(x: current.x, y: y + origin.y)
|
|
272
|
-
path.addLine(to: current)
|
|
273
|
-
|
|
274
|
-
case "C":
|
|
275
|
-
guard
|
|
276
|
-
let control1 = point(from: origin),
|
|
277
|
-
let control2 = point(from: origin),
|
|
278
|
-
let end = point(from: origin)
|
|
279
|
-
else { return path }
|
|
280
|
-
path.addCurve(to: end, control1: control1, control2: control2)
|
|
281
|
-
cubicControl = control2
|
|
282
|
-
current = end
|
|
283
|
-
|
|
284
|
-
case "S":
|
|
285
|
-
guard let control2 = point(from: origin), let end = point(from: origin) else { return path }
|
|
286
|
-
let control1 = lastCubicControl.map { reflect($0, around: current) } ?? current
|
|
287
|
-
path.addCurve(to: end, control1: control1, control2: control2)
|
|
288
|
-
cubicControl = control2
|
|
289
|
-
current = end
|
|
290
|
-
|
|
291
|
-
case "Q":
|
|
292
|
-
guard let control = point(from: origin), let end = point(from: origin) else { return path }
|
|
293
|
-
path.addQuadCurve(to: end, control: control)
|
|
294
|
-
quadraticControl = control
|
|
295
|
-
current = end
|
|
296
|
-
|
|
297
|
-
case "T":
|
|
298
|
-
guard let end = point(from: origin) else { return path }
|
|
299
|
-
let control = lastQuadraticControl.map { reflect($0, around: current) } ?? current
|
|
300
|
-
path.addQuadCurve(to: end, control: control)
|
|
301
|
-
quadraticControl = control
|
|
302
|
-
current = end
|
|
303
|
-
|
|
304
|
-
case "A":
|
|
305
|
-
guard
|
|
306
|
-
let radiusX = number(),
|
|
307
|
-
let radiusY = number(),
|
|
308
|
-
let rotation = number(),
|
|
309
|
-
let largeArc = flag(),
|
|
310
|
-
let sweep = flag(),
|
|
311
|
-
let end = point(from: origin)
|
|
312
|
-
else { return path }
|
|
313
|
-
SvgArc(
|
|
314
|
-
from: current, to: end, radiusX: radiusX, radiusY: radiusY,
|
|
315
|
-
rotationDegrees: rotation, largeArc: largeArc, sweep: sweep
|
|
316
|
-
).add(to: path)
|
|
317
|
-
current = end
|
|
318
|
-
|
|
319
|
-
case "Z":
|
|
320
|
-
path.closeSubpath()
|
|
321
|
-
current = subpathStart
|
|
322
|
-
|
|
323
|
-
default:
|
|
324
|
-
return path
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
lastCubicControl = cubicControl
|
|
328
|
-
lastQuadraticControl = quadraticControl
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
return path
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
private func reflect(_ point: CGPoint, around center: CGPoint) -> CGPoint {
|
|
335
|
-
CGPoint(x: 2 * center.x - point.x, y: 2 * center.y - point.y)
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
private mutating func point(from origin: CGPoint) -> CGPoint? {
|
|
339
|
-
guard let x = number(), let y = number() else { return nil }
|
|
340
|
-
|
|
341
|
-
return CGPoint(x: x + origin.x, y: y + origin.y)
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
private mutating func skipSeparators() {
|
|
345
|
-
while index < characters.count, characters[index].isWhitespace || characters[index] == "," {
|
|
346
|
-
index += 1
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
private mutating func flag() -> Bool? {
|
|
351
|
-
skipSeparators()
|
|
352
|
-
guard index < characters.count, characters[index] == "0" || characters[index] == "1" else { return nil }
|
|
353
|
-
|
|
354
|
-
index += 1
|
|
355
|
-
|
|
356
|
-
return characters[index - 1] == "1"
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
/// "-1.5e-3", ".5" and "1.5.5" (two numbers) all parse.
|
|
360
|
-
private mutating func number() -> CGFloat? {
|
|
361
|
-
skipSeparators()
|
|
362
|
-
let start = index
|
|
363
|
-
var hasDot = false
|
|
364
|
-
|
|
365
|
-
if index < characters.count, characters[index] == "-" || characters[index] == "+" { index += 1 }
|
|
366
|
-
while index < characters.count {
|
|
367
|
-
let character = characters[index]
|
|
368
|
-
if character.isNumber {
|
|
369
|
-
index += 1
|
|
370
|
-
} else if character == ".", !hasDot {
|
|
371
|
-
hasDot = true
|
|
372
|
-
index += 1
|
|
373
|
-
} else if character == "e" || character == "E" {
|
|
374
|
-
index += 1
|
|
375
|
-
if index < characters.count, characters[index] == "-" || characters[index] == "+" { index += 1 }
|
|
376
|
-
} else {
|
|
377
|
-
break
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
return Double(String(characters[start..<index])).map { CGFloat($0) }
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
/// An elliptical arc as cubic Béziers, one per quarter turn at most (SVG spec, appendix B.2.4).
|
|
386
|
-
private struct SvgArc {
|
|
387
|
-
let from: CGPoint
|
|
388
|
-
let to: CGPoint
|
|
389
|
-
let radiusX: CGFloat
|
|
390
|
-
let radiusY: CGFloat
|
|
391
|
-
let rotationDegrees: CGFloat
|
|
392
|
-
let largeArc: Bool
|
|
393
|
-
let sweep: Bool
|
|
394
|
-
|
|
395
|
-
func add(to path: CGMutablePath) {
|
|
396
|
-
guard from != to else { return }
|
|
397
|
-
guard radiusX != 0, radiusY != 0 else { return path.addLine(to: to) }
|
|
398
|
-
|
|
399
|
-
let angle = rotationDegrees * .pi / 180
|
|
400
|
-
let (cosine, sine) = (cos(angle), sin(angle))
|
|
401
|
-
|
|
402
|
-
// The start point in the ellipse's own axes, relative to the chord's middle.
|
|
403
|
-
let halfDeltaX = (from.x - to.x) / 2
|
|
404
|
-
let halfDeltaY = (from.y - to.y) / 2
|
|
405
|
-
let startX = cosine * halfDeltaX + sine * halfDeltaY
|
|
406
|
-
let startY = -sine * halfDeltaX + cosine * halfDeltaY
|
|
407
|
-
|
|
408
|
-
// Radii too small to reach the end are scaled up just enough.
|
|
409
|
-
var (rx, ry) = (abs(radiusX), abs(radiusY))
|
|
410
|
-
let radiiScale = (startX * startX) / (rx * rx) + (startY * startY) / (ry * ry)
|
|
411
|
-
if radiiScale > 1 {
|
|
412
|
-
rx *= sqrt(radiiScale)
|
|
413
|
-
ry *= sqrt(radiiScale)
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
let numerator = rx * rx * ry * ry - rx * rx * startY * startY - ry * ry * startX * startX
|
|
417
|
-
let denominator = rx * rx * startY * startY + ry * ry * startX * startX
|
|
418
|
-
var coefficient = sqrt(max(0, numerator / denominator))
|
|
419
|
-
if largeArc == sweep { coefficient = -coefficient }
|
|
420
|
-
|
|
421
|
-
let centerX = coefficient * rx * startY / ry
|
|
422
|
-
let centerY = -coefficient * ry * startX / rx
|
|
423
|
-
let center = CGPoint(
|
|
424
|
-
x: cosine * centerX - sine * centerY + (from.x + to.x) / 2,
|
|
425
|
-
y: sine * centerX + cosine * centerY + (from.y + to.y) / 2
|
|
426
|
-
)
|
|
427
|
-
|
|
428
|
-
let startVector = CGPoint(x: (startX - centerX) / rx, y: (startY - centerY) / ry)
|
|
429
|
-
let endVector = CGPoint(x: (-startX - centerX) / rx, y: (-startY - centerY) / ry)
|
|
430
|
-
let startAngle = Self.angle(from: CGPoint(x: 1, y: 0), to: startVector)
|
|
431
|
-
var sweepAngle = Self.angle(from: startVector, to: endVector)
|
|
432
|
-
if !sweep, sweepAngle > 0 { sweepAngle -= 2 * .pi }
|
|
433
|
-
if sweep, sweepAngle < 0 { sweepAngle += 2 * .pi }
|
|
434
|
-
|
|
435
|
-
let segmentCount = max(1, Int(ceil(abs(sweepAngle) / (.pi / 2))))
|
|
436
|
-
let segmentAngle = sweepAngle / CGFloat(segmentCount)
|
|
437
|
-
let handle = 4 / 3 * tan(segmentAngle / 4)
|
|
438
|
-
|
|
439
|
-
// Unit-circle points mapped onto the rotated ellipse.
|
|
440
|
-
func onEllipse(_ x: CGFloat, _ y: CGFloat) -> CGPoint {
|
|
441
|
-
CGPoint(
|
|
442
|
-
x: center.x + rx * cosine * x - ry * sine * y,
|
|
443
|
-
y: center.y + rx * sine * x + ry * cosine * y
|
|
444
|
-
)
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
for segment in 0..<segmentCount {
|
|
448
|
-
let segmentStart = startAngle + CGFloat(segment) * segmentAngle
|
|
449
|
-
let segmentEnd = segmentStart + segmentAngle
|
|
450
|
-
let (startCos, startSin) = (cos(segmentStart), sin(segmentStart))
|
|
451
|
-
let (endCos, endSin) = (cos(segmentEnd), sin(segmentEnd))
|
|
452
|
-
|
|
453
|
-
path.addCurve(
|
|
454
|
-
to: onEllipse(endCos, endSin),
|
|
455
|
-
control1: onEllipse(startCos - handle * startSin, startSin + handle * startCos),
|
|
456
|
-
control2: onEllipse(endCos + handle * endSin, endSin - handle * endCos)
|
|
457
|
-
)
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
private static func angle(from first: CGPoint, to second: CGPoint) -> CGFloat {
|
|
462
|
-
atan2(first.x * second.y - first.y * second.x, first.x * second.x + first.y * second.y)
|
|
463
|
-
}
|
|
464
|
-
}
|