import SwiftUI /// The mock's icons are 24-unit SVG paths, stroked at 1.6. This reads the few /// path commands they use (M L H V C A Z, absolute and relative) into a /// SwiftUI `Path`, so the cards draw the mock's own glyphs rather than /// lookalikes. struct SVGPath: Shape { let commands: [String] init(_ commands: String...) { self.commands = commands } func path(in rect: CGRect) -> Path { let scale = min(rect.width, rect.height) / 24 var path = Path() for command in commands { SVGPathParser.append(command, to: &path) } return path.applying( CGAffineTransform(translationX: rect.minX, y: rect.minY).scaledBy(x: scale, y: scale) ) } } /// A rounded rectangle and a circle in the same 24-unit space. extension SVGPath { static func rect(_ x: Double, _ y: Double, _ w: Double, _ h: Double, rx: Double) -> String { "M\(x + rx) \(y)H\(x + w - rx)A\(rx) \(rx) 0 0 1 \(x + w) \(y + rx)V\(y + h - rx)" + "A\(rx) \(rx) 0 0 1 \(x + w - rx) \(y + h)H\(x + rx)A\(rx) \(rx) 0 0 1 \(x) \(y + h - rx)" + "V\(y + rx)A\(rx) \(rx) 0 0 1 \(x + rx) \(y)Z" } static func circle(_ cx: Double, _ cy: Double, r: Double) -> String { "M\(cx - r) \(cy)A\(r) \(r) 0 1 1 \(cx + r) \(cy)A\(r) \(r) 0 1 1 \(cx - r) \(cy)Z" } } enum SVGPathParser { static func append(_ data: String, to path: inout Path) { var tokens = Tokens(data) var current = CGPoint.zero var start = CGPoint.zero var command: Character = "M" while let next = tokens.nextCommandOrNumber() { if case .command(let c) = next { command = c if c == "Z" || c == "z" { path.closeSubpath() current = start continue } } else { tokens.pushBack() } let relative = command.isLowercase let base = relative ? current : .zero switch command.uppercased().first! { case "M": guard let x = tokens.number(), let y = tokens.number() else { return } current = CGPoint(x: base.x + x, y: base.y + y) start = current path.move(to: current) // Further pairs after a moveto are linetos. command = relative ? "l" : "L" case "L": guard let x = tokens.number(), let y = tokens.number() else { return } current = CGPoint(x: base.x + x, y: base.y + y) path.addLine(to: current) case "H": guard let x = tokens.number() else { return } current = CGPoint(x: (relative ? current.x : 0) + x, y: current.y) path.addLine(to: current) case "V": guard let y = tokens.number() else { return } current = CGPoint(x: current.x, y: (relative ? current.y : 0) + y) path.addLine(to: current) case "C": guard let x1 = tokens.number(), let y1 = tokens.number(), let x2 = tokens.number(), let y2 = tokens.number(), let x = tokens.number(), let y = tokens.number() else { return } let end = CGPoint(x: base.x + x, y: base.y + y) path.addCurve( to: end, control1: CGPoint(x: base.x + x1, y: base.y + y1), control2: CGPoint(x: base.x + x2, y: base.y + y2) ) current = end case "A": guard let rx = tokens.number(), let ry = tokens.number(), let phi = tokens.number(), let large = tokens.number(), let sweep = tokens.number(), let x = tokens.number(), let y = tokens.number() else { return } let end = CGPoint(x: base.x + x, y: base.y + y) for point in arc(from: current, to: end, rx: rx, ry: ry, phi: phi, large: large != 0, sweep: sweep != 0) { path.addLine(to: point) } current = end default: return } } } /// An elliptical arc as a polyline (SVG 1.1, F.6.5: endpoint to centre). static func arc(from p0: CGPoint, to p1: CGPoint, rx: Double, ry: Double, phi: Double, large: Bool, sweep: Bool) -> [CGPoint] { var rx = abs(rx), ry = abs(ry) guard rx > 0, ry > 0, p0 != p1 else { return [p1] } let angle = phi * .pi / 180 let cosA = cos(angle), sinA = sin(angle) let dx = (p0.x - p1.x) / 2, dy = (p0.y - p1.y) / 2 let x1 = cosA * dx + sinA * dy let y1 = -sinA * dx + cosA * dy let lambda = (x1 * x1) / (rx * rx) + (y1 * y1) / (ry * ry) if lambda > 1 { rx *= lambda.squareRoot(); ry *= lambda.squareRoot() } let numerator = rx * rx * ry * ry - rx * rx * y1 * y1 - ry * ry * x1 * x1 let denominator = rx * rx * y1 * y1 + ry * ry * x1 * x1 var factor = (max(0, numerator) / denominator).squareRoot() if large == sweep { factor = -factor } let cx1 = factor * rx * y1 / ry let cy1 = -factor * ry * x1 / rx let cx = cosA * cx1 - sinA * cy1 + (p0.x + p1.x) / 2 let cy = sinA * cx1 + cosA * cy1 + (p0.y + p1.y) / 2 func vectorAngle(_ ux: Double, _ uy: Double, _ vx: Double, _ vy: Double) -> Double { atan2(ux * vy - uy * vx, ux * vx + uy * vy) } let theta1 = vectorAngle(1, 0, (x1 - cx1) / rx, (y1 - cy1) / ry) var delta = vectorAngle((x1 - cx1) / rx, (y1 - cy1) / ry, (-x1 - cx1) / rx, (-y1 - cy1) / ry) if !sweep, delta > 0 { delta -= 2 * .pi } if sweep, delta < 0 { delta += 2 * .pi } let segments = max(4, Int(abs(delta) / (.pi / 32))) return (1...segments).map { step in let t = theta1 + delta * Double(step) / Double(segments) let ex = rx * cos(t), ey = ry * sin(t) return CGPoint(x: cosA * ex - sinA * ey + cx, y: sinA * ex + cosA * ey + cy) } } private struct Tokens { enum Token { case command(Character), number(Double) } private let scalars: [Character] private var index = 0 private var last = 0 init(_ text: String) { scalars = Array(text) } private mutating func skipSeparators() { while index < scalars.count, scalars[index] == " " || scalars[index] == "," || scalars[index] == "\n" { index += 1 } } mutating func nextCommandOrNumber() -> Token? { skipSeparators() last = index guard index < scalars.count else { return nil } let c = scalars[index] if c.isLetter { index += 1 return .command(c) } return number().map(Token.number) } mutating func pushBack() { index = last } mutating func number() -> Double? { skipSeparators() let begin = index if index < scalars.count, scalars[index] == "-" || scalars[index] == "+" { index += 1 } var seenDot = false while index < scalars.count { let c = scalars[index] if c.isNumber { index += 1 } else if c == ".", !seenDot { seenDot = true; index += 1 } else { break } } guard index > begin else { return nil } return Double(String(scalars[begin..