// Cotton Notes' own icons (assets/design/notes/DECISIONS.md "Look": one set, // thin rounded line, one weight): the drawings `scripts/draw-notes-icons.py` // writes for both phones (`WritingMarkSet.swift` here, Android's // `mark_.xml`), drawn on a 24-unit grid in one round-ended stroke and // tinted by whoever draws them. Nothing here is a system symbol. import CoreGraphics import Foundation public enum WritingMarks { /// One part of a drawing: a line drawn with the set's stroke, or a filled shape (a dot). public struct Part: Sendable { public var d: String public var filled: Bool init(stroke d: String) { self.d = d; filled = false } init(fill d: String) { self.d = d; filled = true } } /// Older names some screens give a mark, each the set's drawing of that thing (as Android's `Marks.ALIASES`). static let aliases = ["image": "picture"] /// Whether the set draws `icon`. public static func draws(_ icon: String?) -> Bool { guard let icon else { return false } return set[aliases[icon] ?? icon] != nil } /// An icon's parts, each made a path on the 24-unit grid once. public static func paths(_ icon: String) -> [(path: CGPath, filled: Bool)]? { lock.lock() defer { lock.unlock() } let name = aliases[icon] ?? icon if let made = made[name] { return made } guard let parts = set[name] else { return nil } let out = parts.map { (path: WritingMarkPath.make($0.d), filled: $0.filled) } made[name] = out return out } nonisolated(unsafe) private static var made: [String: [(path: CGPath, filled: Bool)]] = [:] private static let lock = NSLock() /// The stroke's width at `side` points. public static func lineWidth(_ side: CGFloat) -> CGFloat { CGFloat(stroke) * side / 24 } } /// SVG path data as the icon script writes it (absolute M, L, H, V, C, A and Z) made into a path. enum WritingMarkPath { static func make(_ d: String) -> CGPath { let path = CGMutablePath() var tokens = tokenise(d)[...] var command: Character = "M" var current = CGPoint.zero var start = CGPoint.zero func number() -> CGFloat { guard case .number(let n)? = tokens.first else { return 0 } tokens = tokens.dropFirst() return n } while let token = tokens.first { if case .command(let c) = token { command = c tokens = tokens.dropFirst() } switch command { case "M": current = CGPoint(x: number(), y: number()) start = current path.move(to: current) command = "L" // further pairs are lines case "L": current = CGPoint(x: number(), y: number()) path.addLine(to: current) case "H": current.x = number() path.addLine(to: current) case "V": current.y = number() path.addLine(to: current) case "C": let c1 = CGPoint(x: number(), y: number()) let c2 = CGPoint(x: number(), y: number()) current = CGPoint(x: number(), y: number()) path.addCurve(to: current, control1: c1, control2: c2) case "A": let rx = number(), ry = number(), rotation = number(), large = number() != 0, sweep = number() != 0 let end = CGPoint(x: number(), y: number()) arc(path, from: current, to: end, rx: rx, ry: ry, rotation: rotation, large: large, sweep: sweep) current = end case "Z": path.closeSubpath() current = start // A Z takes no numbers: anything after it begins a new command. if case .number? = tokens.first { tokens = tokens.dropFirst() } default: tokens = tokens.dropFirst() } } return path } private enum Token { case command(Character), number(CGFloat) } private static func tokenise(_ d: String) -> [Token] { var out: [Token] = [] var digits = "" func flush() { if let n = Double(digits) { out.append(.number(CGFloat(n))) } digits = "" } for ch in d { if ch.isLetter, ch != "e" { flush() out.append(.command(ch)) } else if ch == " " || ch == "," { flush() } else if ch == "-", !digits.isEmpty, digits.last != "e" { flush() digits.append(ch) } else { digits.append(ch) } } flush() return out } /// An SVG elliptical arc (endpoint form) as cubic curves, at most a quarter turn each. private static func arc(_ path: CGMutablePath, from p0: CGPoint, to p1: CGPoint, rx: CGFloat, ry: CGFloat, rotation: 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 phi = rotation * .pi / 180 let cosP = cos(phi), sinP = sin(phi) let dx = (p0.x - p1.x) / 2, dy = (p0.y - p1.y) / 2 let x1 = cosP * dx + sinP * dy let y1 = -sinP * dx + cosP * dy let lambda = (x1 * x1) / (rx * rx) + (y1 * y1) / (ry * ry) if lambda > 1 { rx *= sqrt(lambda) ry *= sqrt(lambda) } let num = rx * rx * ry * ry - rx * rx * y1 * y1 - ry * ry * x1 * x1 let den = rx * rx * y1 * y1 + ry * ry * x1 * x1 var k = den == 0 ? 0 : sqrt(max(0, num / den)) if large == sweep { k = -k } let cx1 = k * rx * y1 / ry let cy1 = -k * ry * x1 / rx let cx = cosP * cx1 - sinP * cy1 + (p0.x + p1.x) / 2 let cy = sinP * cx1 + cosP * cy1 + (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, (x1 - cx1) / rx, (y1 - cy1) / ry) var delta = angle((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(1, Int(ceil(abs(delta) / (.pi / 2)))) let step = delta / CGFloat(segments) let t = 4 / 3 * tan(step / 4) func point(_ a: CGFloat) -> CGPoint { CGPoint(x: cx + rx * cos(a) * cosP - ry * sin(a) * sinP, y: cy + rx * cos(a) * sinP + ry * sin(a) * cosP) } func derivative(_ a: CGFloat) -> CGPoint { CGPoint(x: -rx * sin(a) * cosP - ry * cos(a) * sinP, y: -rx * sin(a) * sinP + ry * cos(a) * cosP) } var a = theta1 for i in 0.. UIImage? { guard let paths = paths(icon) else { return nil } let renderer = UIGraphicsImageRenderer(size: CGSize(width: size, height: size)) let image = renderer.image { ctx in let g = ctx.cgContext g.scaleBy(x: size / 24, y: size / 24) g.setLineCap(.round) g.setLineJoin(.round) g.setLineWidth(CGFloat(stroke)) UIColor.black.setStroke() UIColor.black.setFill() for part in paths { g.addPath(part.path) if part.filled { g.fillPath() } else { g.strokePath() } } } return image.withRenderingMode(.alwaysTemplate) } } #endif