257 lines · 11.4 KB · SwiftRaw
// Secrets' drawn marks (secrets.html's `<symbol>`s, the same paths Android draws): one per kind (26-unit box, 1.5// stroke), the few controls (22-unit box, 1.6 stroke), and the app's mark, the six-armed asterisk that stands in for a// hidden character (the owner's choice, 25 Nov 2026). Each is the drawing's own path data, read here, so the two phones// and the page never drift apart.#if os(iOS)import SwiftUI/// One drawn mark, by its secrets.html id.enum SecretsMark: String, CaseIterable { // kinds case pass = "k-pass", passkey = "k-passkey", card = "k-card", note = "k-note", token = "k-token", env = "k-env", keyfile = "k-keyfile" // controls case search = "u-search", plus = "u-plus", back = "u-back", next = "u-next", copy = "u-copy", eye = "u-eye", share = "u-share" case link = "u-link", lock = "u-lock", face = "u-face", open = "u-open", check = "u-check", file = "u-file" // the app's mark case aster = "m-aster" /// The box the path data is drawn in. var box: CGFloat { switch self { case .pass, .passkey, .card, .note, .token, .env, .keyfile: 26 case .aster: 64 default: 22 } } /// The stroke in box units. var stroke: CGFloat { switch self { case .pass, .passkey, .card, .note, .token, .env, .keyfile: 1.5 case .aster: 7 default: 1.6 } } /// secrets.html's paths, as written there (circles and rects in their own words). var shapes: [SecretsShape] { switch self { case .pass: [.circle(8.5, 13, 4.5), .path("M13 13h10M19.5 13v3.5M22.5 13v2.5")] case .passkey: [.path("M7.5 12.5a5.5 5.5 0 0 1 11 0v1.5"), .path("M13 12.5v7.5"), .path("M10 15v3.5M16 16.5v3"), .path("M4.5 13a8.5 8.5 0 0 1 17-1")] case .card: [.rect(3.5, 6.5, 19, 13, 2.5), .path("M3.5 11h19M7 15.5h4")] case .note: [.path("M6.5 4h13v18h-13z"), .path("M9.5 9h7M9.5 12.5h7M9.5 16h4")] case .token: [.path("M9 7.5 3.5 13 9 18.5M17 7.5l5.5 5.5L17 18.5M14.5 6l-3 14")] case .env: [.path("M6 3.5h9l5 5v14H6z"), .path("M15 3.5v5h5"), .path("M9.5 13.5h7M9.5 17h7")] case .keyfile: [.path("M6 3.5h9l5 5v14H6z"), .path("M15 3.5v5h5"), .circle(11, 15, 2.4), .path("M13.4 15H17M15.8 15v2")] case .search: [.circle(9.5, 9.5, 6), .path("m14 14 5 5")] case .plus: [.path("M11 4v14M4 11h14")] case .back: [.path("M13.5 4.5 7 11l6.5 6.5")] case .next: [.path("M8.5 4.5 15 11l-6.5 6.5")] case .copy: [.rect(7.5, 7.5, 10, 11, 2), .path("M14.5 7.5V5.5a2 2 0 0 0-2-2h-6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h1")] case .eye: [.path("M2.5 11S6 5 11 5s8.5 6 8.5 6-3.5 6-8.5 6-8.5-6-8.5-6z"), .circle(11, 11, 2.6)] case .share: [.path("M11 3v11M7 7l4-4 4 4"), .path("M5 11v7h12v-7")] case .link: [.path("M9.5 12.5a3.5 3.5 0 0 0 5 0l3-3a3.5 3.5 0 0 0-5-5l-1 1"), .path("M12.5 9.5a3.5 3.5 0 0 0-5 0l-3 3a3.5 3.5 0 0 0 5 5l1-1")] case .lock: [.rect(4.5, 9.5, 13, 9, 2), .path("M7.5 9.5V7a3.5 3.5 0 0 1 7 0v2.5")] case .face: [.path("M3 7V5a2 2 0 0 1 2-2h2M15 3h2a2 2 0 0 1 2 2v2M19 15v2a2 2 0 0 1-2 2h-2M7 19H5a2 2 0 0 1-2-2v-2"), .path("M8 8v1.5M14 8v1.5M11 8v4h-1M8.5 14.5s1 1.2 2.5 1.2 2.5-1.2 2.5-1.2")] case .open: [.path("M12 4h6v6M18 4l-8 8"), .path("M15 13v5H4V7h5")] case .check: [.path("m4.5 11.5 4 4 9-9")] case .file: [.path("M5 3h8l4 4v12H5z"), .path("M13 3v4h4")] case .aster: [.path("M32 13v38M15.5 22.5l33 19M15.5 41.5l33-19")] } } /// The mark for a kind the core names (`login`, `api_key`, `note`, `card`, `document`, …; the design's five kinds /// with a file in two shapes). static func kind(_ kind: String) -> SecretsMark { switch kind { case "login", "password", "wifi": .pass case "passkey": .passkey case "card": .card case "api_key", "token", "code": .token case "env", "file": .env case "keyfile", "keystore", "document": .keyfile default: .note } } /// The whole mark as one path in a `side` square. func path(side: CGFloat) -> Path { let scale = side / box var path = Path() for shape in shapes { path.addPath(shape.path, transform: CGAffineTransform(scaleX: scale, y: scale)) } return path }}/// One shape of a mark, in its box's units.enum SecretsShape { case circle(CGFloat, CGFloat, CGFloat) case rect(CGFloat, CGFloat, CGFloat, CGFloat, CGFloat) case path(String) var path: Path { switch self { case let .circle(x, y, r): Path(ellipseIn: CGRect(x: x - r, y: y - r, width: 2 * r, height: 2 * r)) case let .rect(x, y, w, h, r): Path(roundedRect: CGRect(x: x, y: y, width: w, height: h), cornerRadius: r) case let .path(data): SecretsPathData.path(data) } }}/// The drawn mark at `size` in `ink`, stroked as the drawing strokes it.struct SecretsMarkView: View { let mark: SecretsMark var size: CGFloat = 22 var ink: Color var body: some View { Canvas { context, canvas in let side = min(canvas.width, canvas.height) context.stroke(mark.path(side: side), with: .color(ink), style: StrokeStyle(lineWidth: mark.stroke * side / mark.box, lineCap: .round, lineJoin: .round)) } .frame(width: size, height: size) .accessibilityHidden(true) }}/// SVG path data (the commands secrets.html uses: M L H V S A Z, each in both cases) to a `Path`.enum SecretsPathData { static func path(_ data: String) -> Path { var path = Path() var numbers: [CGFloat] = [] var command: Character = "M" var current = CGPoint.zero var start = CGPoint.zero var lastControl: CGPoint? var tokens = tokenize(data)[...] func take(_ n: Int) -> [CGFloat]? { guard numbers.count >= n else { return nil } let taken = Array(numbers.prefix(n)) numbers.removeFirst(n) return taken } while !tokens.isEmpty || !numbers.isEmpty { if let token = tokens.first, case let .command(c) = token { command = c tokens.removeFirst() if c == "Z" || c == "z" { path.closeSubpath() current = start lastControl = nil continue } } numbers.removeAll() while let token = tokens.first, case let .number(n) = token { numbers.append(n) tokens.removeFirst() } let relative = command.isLowercase func point(_ x: CGFloat, _ y: CGFloat) -> CGPoint { relative ? CGPoint(x: current.x + x, y: current.y + y) : CGPoint(x: x, y: y) } var first = true repeat { switch command.uppercased() { case "M": guard let v = take(2) else { break } current = point(v[0], v[1]) if first { path.move(to: current); start = current } else { path.addLine(to: current) } lastControl = nil case "L": guard let v = take(2) else { break } current = point(v[0], v[1]); path.addLine(to: current); lastControl = nil case "H": guard let v = take(1) else { break } current = CGPoint(x: relative ? current.x + v[0] : v[0], y: current.y); path.addLine(to: current); lastControl = nil case "V": guard let v = take(1) else { break } current = CGPoint(x: current.x, y: relative ? current.y + v[0] : v[0]); path.addLine(to: current); lastControl = nil case "S": guard let v = take(4) else { break } let c1 = lastControl.map { CGPoint(x: 2 * current.x - $0.x, y: 2 * current.y - $0.y) } ?? current let c2 = point(v[0], v[1]), end = point(v[2], v[3]) path.addCurve(to: end, control1: c1, control2: c2) lastControl = c2; current = end case "A": guard let v = take(7) else { break } let end = point(v[5], v[6]) arc(&path, from: current, to: end, rx: v[0], ry: v[1], large: v[3] != 0, sweep: v[4] != 0) current = end; lastControl = nil default: numbers.removeAll() } first = false } while !numbers.isEmpty } return path } private enum Token { case command(Character), number(CGFloat) } private static func tokenize(_ data: String) -> [Token] { var tokens: [Token] = [] var number = "" func flush() { if let value = Double(number) { tokens.append(.number(CGFloat(value))) } number = "" } for ch in data { if ch.isLetter && ch != "e" { flush() tokens.append(.command(ch)) } else if ch == "-" && !number.isEmpty && number.last != "e" { flush(); number = "-" } else if ch == "." && number.contains(".") { flush(); number = "." } else if ch == " " || ch == "," { flush() } else { number.append(ch) } } flush() return tokens } /// SVG's endpoint arc to cubic curves (the SVG 1.1 implementation notes, F.6.5 and F.6.6; rotation is 0 in every /// mark here). private static func arc(_ path: inout Path, from p0: CGPoint, to p1: CGPoint, rx: CGFloat, ry: CGFloat, large: Bool, sweep: Bool) { var rx = abs(rx), ry = abs(ry) guard rx > 0, ry > 0, p0 != p1 else { path.addLine(to: p1); return } let dx = (p0.x - p1.x) / 2, dy = (p0.y - p1.y) / 2 let lambda = (dx * dx) / (rx * rx) + (dy * dy) / (ry * ry) if lambda > 1 { rx *= sqrt(lambda); ry *= sqrt(lambda) } let num = rx * rx * ry * ry - rx * rx * dy * dy - ry * ry * dx * dx let den = rx * rx * dy * dy + ry * ry * dx * dx var coef = sqrt(max(0, num / den)) if large == sweep { coef = -coef } let cxp = coef * rx * dy / ry, cyp = -coef * ry * dx / rx let cx = cxp + (p0.x + p1.x) / 2, cy = cyp + (p0.y + p1.y) / 2 func angle(_ ux: CGFloat, _ uy: CGFloat, _ vx: CGFloat, _ vy: CGFloat) -> CGFloat { let a = atan2(ux * vy - uy * vx, ux * vx + uy * vy) return a } let theta1 = angle(1, 0, (dx - cxp) / rx, (dy - cyp) / ry) var delta = angle((dx - cxp) / rx, (dy - cyp) / ry, (-dx - cxp) / rx, (-dy - cyp) / ry) if !sweep && delta > 0 { delta -= 2 * .pi } if sweep && delta < 0 { delta += 2 * .pi } let segments = max(1, Int(ceil(abs(delta) / (.pi / 2)))) let step = delta / CGFloat(segments) let k = 4 / 3 * tan(step / 4) var t = theta1 for _ in 0..<segments { let cos1 = cos(t), sin1 = sin(t), cos2 = cos(t + step), sin2 = sin(t + step) let e1 = CGPoint(x: cx + rx * cos1, y: cy + ry * sin1) let e2 = CGPoint(x: cx + rx * cos2, y: cy + ry * sin2) let c1 = CGPoint(x: e1.x - k * rx * sin1, y: e1.y + k * ry * cos1) let c2 = CGPoint(x: e2.x + k * rx * sin2, y: e2.y - k * ry * cos2) path.addCurve(to: e2, control1: c1, control2: c2) t += step } }}#endif