SPB Git

Repository Public License

LunarGlyph.swift 12.9 KB 299 lines swift

1import SwiftUI23/// The proprietary glyphs of the signature modules. Every glyph is drawn in code on a 24-unit grid with a single4/// stroke weight and at most a couple of solid accents, so the set stays coherent and readable at 16 pt.5public enum LunarGlyph: String, Sendable, CaseIterable, Identifiable {6    case command, orbit, split, agent, memory, ghost, time, canvas, blink, lens7    case shield, archive, morph, reader, dev, flow, capsule, bridge, media, focus89    public var id: String { rawValue }1011    /// English module name (used as the default accessibility label).12    public var displayName: String { rawValue.prefix(1).uppercased() + rawValue.dropFirst() }1314    /// Side of the design grid.15    public static let gridSize: CGFloat = 2416    /// Stroke weight on the design grid.17    public static let strokeWeight: CGFloat = 1.71819    /// The glyph on the 24-unit design grid: an open-stroke part and a solid part.20    public var geometry: LunarGlyphGeometry { LunarGlyphGeometry.make(self) }2122    /// The glyph's stroke part scaled to `rect` (aspect-fit, centred).23    public func strokePath(in rect: CGRect) -> Path { geometry.stroke.applying(Self.transform(to: rect)) }2425    /// The glyph's solid part scaled to `rect` (aspect-fit, centred).26    public func fillPath(in rect: CGRect) -> Path { geometry.fill.applying(Self.transform(to: rect)) }2728    /// The complete outline of the glyph as one fillable path (strokes expanded to their outline).29    public func outlinePath(in rect: CGRect) -> Path {30        let scale = min(rect.width, rect.height) / Self.gridSize31        var path = strokePath(in: rect).strokedPath(Self.strokeStyle(scale: scale))32        path.addPath(fillPath(in: rect))33        return path34    }3536    static func strokeStyle(scale: CGFloat) -> StrokeStyle {37        StrokeStyle(lineWidth: strokeWeight * scale, lineCap: .round, lineJoin: .round)38    }3940    static func transform(to rect: CGRect) -> CGAffineTransform {41        let scale = min(rect.width, rect.height) / gridSize42        let dx = rect.minX + (rect.width - gridSize * scale) / 243        let dy = rect.minY + (rect.height - gridSize * scale) / 244        return CGAffineTransform(translationX: dx, y: dy).scaledBy(x: scale, y: scale)45    }46}4748/// Stroke and fill parts of a glyph on the design grid.49public struct LunarGlyphGeometry: Sendable {50    public var stroke: Path51    public var fill: Path52}5354/// Draws a `LunarGlyph` in the current foreground style.55public struct LunarGlyphView: View {56    private let glyph: LunarGlyph57    private let size: CGFloat58    private let label: String?5960    /// - Parameters:61    ///   - size: side of the glyph in points.62    ///   - label: accessibility label; pass `nil` when the glyph is decorative (it is then hidden from assistive63    ///     technologies).64    public init(_ glyph: LunarGlyph, size: CGFloat = LunarMetrics.icon, label: String? = nil) {65        self.glyph = glyph66        self.size = size67        self.label = label68    }6970    public var body: some View {71        LunarGlyphShape(glyph: glyph)72            .frame(width: size, height: size)73            .accessibilityElement()74            .accessibilityLabel(label ?? "")75            .accessibilityAddTraits(.isImage)76            .accessibilityHidden(label == nil)77    }78}7980/// The glyph as a `Shape` (complete outline), for use with `fill`, masks and custom layouts.81public struct LunarGlyphShape: Shape {82    public var glyph: LunarGlyph8384    public init(glyph: LunarGlyph) { self.glyph = glyph }8586    public func path(in rect: CGRect) -> Path { glyph.outlinePath(in: rect) }87}8889// MARK: - Geometry9091extension LunarGlyphGeometry {92    private static func circle(_ x: CGFloat, _ y: CGFloat, _ r: CGFloat) -> Path {93        Path(ellipseIn: CGRect(x: x - r, y: y - r, width: r * 2, height: r * 2))94    }9596    private static func rounded(_ x: CGFloat, _ y: CGFloat, _ w: CGFloat, _ h: CGFloat, _ r: CGFloat) -> Path {97        Path(roundedRect: CGRect(x: x, y: y, width: w, height: h), cornerRadius: r, style: .continuous)98    }99100    private static func line(_ path: inout Path, _ points: (CGFloat, CGFloat)...) {101        guard let first = points.first else { return }102        path.move(to: CGPoint(x: first.0, y: first.1))103        for point in points.dropFirst() { path.addLine(to: CGPoint(x: point.0, y: point.1)) }104    }105106    /// A four-point star with concave sides (the "spark").107    private static func spark(_ cx: CGFloat, _ cy: CGFloat, _ r: CGFloat) -> Path {108        var path = Path()109        let k = r * 0.16110        let tips = [CGPoint(x: cx, y: cy - r), CGPoint(x: cx + r, y: cy), CGPoint(x: cx, y: cy + r),111                    CGPoint(x: cx - r, y: cy)]112        let controls = [CGPoint(x: cx + k, y: cy - k), CGPoint(x: cx + k, y: cy + k),113                        CGPoint(x: cx - k, y: cy + k), CGPoint(x: cx - k, y: cy - k)]114        path.move(to: tips[0])115        for index in 0..<4 { path.addQuadCurve(to: tips[(index + 1) % 4], control: controls[index]) }116        path.closeSubpath()117        return path118    }119120    static func make(_ glyph: LunarGlyph) -> LunarGlyphGeometry {121        var stroke = Path()122        var fill = Path()123        switch glyph {124        case .command:125            // A floating command capsule above two result lines.126            stroke.addPath(rounded(3, 4, 18, 7.5, 3.75))127            fill.addPath(circle(7.2, 7.75, 1.35))128            line(&stroke, (5, 15.5), (19, 15.5))129            line(&stroke, (5, 19.5), (13.5, 19.5))130131        case .orbit:132            // A core, a tilted orbital ring and its satellite.133            let tilt = CGAffineTransform(translationX: 12, y: 12).rotated(by: -.pi / 6)134            stroke.addPath(Path(ellipseIn: CGRect(x: -10, y: -4.4, width: 20, height: 8.8)).applying(tilt))135            fill.addPath(circle(12, 12, 2.7))136            let satellite = CGPoint(x: 10 * cos(-0.9), y: 4.4 * sin(-0.9)).applying(tilt)137            fill.addPath(circle(satellite.x, satellite.y, 1.7))138139        case .split:140            // A window with a divider; one pane holds content.141            stroke.addPath(rounded(3, 5, 18, 14, 4))142            line(&stroke, (12, 5), (12, 19))143            fill.addPath(rounded(5.6, 7.6, 3.8, 8.8, 1.4))144145        case .agent:146            // A large spark with a small companion.147            stroke.addPath(spark(10.5, 13, 7.6))148            fill.addPath(spark(18.6, 5.6, 3.4))149150        case .memory:151            // A spiral winding out from a kept point: what was seen stays within reach.152            let turns = 1.75, steps = 64153            for step in 0...steps {154                let t = Double(step) / Double(steps)155                let angle = t * turns * 2 * .pi + .pi * 0.25156                let radius = 3.4 + 6.1 * t157                let point = CGPoint(x: 12 + radius * cos(angle), y: 12 + radius * sin(angle))158                if step == 0 { stroke.move(to: point) } else { stroke.addLine(to: point) }159            }160            fill.addPath(circle(12, 12, 1.9))161162        case .ghost:163            // A hooded silhouette with a scalloped hem.164            stroke.move(to: CGPoint(x: 5, y: 20))165            stroke.addLine(to: CGPoint(x: 5, y: 11))166            stroke.addArc(center: CGPoint(x: 12, y: 11), radius: 7, startAngle: .degrees(180),167                          endAngle: .degrees(360), clockwise: false)168            stroke.addLine(to: CGPoint(x: 19, y: 20))169            for point in [(16.67, 17.6), (14.33, 20), (12, 17.6), (9.67, 20), (7.33, 17.6), (5, 20)] {170                stroke.addLine(to: CGPoint(x: point.0, y: point.1))171            }172            stroke.closeSubpath()173            fill.addPath(circle(9.4, 11.4, 1.35))174            fill.addPath(circle(14.6, 11.4, 1.35))175176        case .time:177            // A dial that winds backwards.178            stroke.addArc(center: CGPoint(x: 12, y: 12), radius: 8.5, startAngle: .degrees(-128),179                          endAngle: .degrees(172), clockwise: false)180            line(&stroke, (3.2, 3.6), (6.6, 5.2), (4.6, 8.4))181            line(&stroke, (12, 7.6), (12, 12), (15.2, 13.9))182183        case .canvas:184            // Two cards laid on a board, one overlapping the other.185            stroke.addPath(rounded(3, 3.5, 11.5, 10.5, 3))186            fill.addPath(rounded(10, 10.5, 11, 10, 3))187188        case .blink:189            // An eye caught mid-glance.190            stroke.move(to: CGPoint(x: 2.5, y: 12))191            stroke.addQuadCurve(to: CGPoint(x: 21.5, y: 12), control: CGPoint(x: 12, y: 0.4))192            stroke.addQuadCurve(to: CGPoint(x: 2.5, y: 12), control: CGPoint(x: 12, y: 23.6))193            stroke.closeSubpath()194            fill.addPath(circle(12, 12, 3))195196        case .lens:197            // A magnifying lens focused on a point.198            stroke.addPath(circle(10.5, 10.5, 6.6))199            line(&stroke, (15.4, 15.4), (20.5, 20.5))200            fill.addPath(circle(10.5, 10.5, 2.1))201202        case .shield:203            // A shield with a protected core.204            stroke.move(to: CGPoint(x: 12, y: 2.8))205            stroke.addLine(to: CGPoint(x: 19.5, y: 5.8))206            stroke.addLine(to: CGPoint(x: 19.5, y: 11.5))207            stroke.addCurve(to: CGPoint(x: 12, y: 21.2), control1: CGPoint(x: 19.5, y: 16.6),208                            control2: CGPoint(x: 15.6, y: 19.8))209            stroke.addCurve(to: CGPoint(x: 4.5, y: 11.5), control1: CGPoint(x: 8.4, y: 19.8),210                            control2: CGPoint(x: 4.5, y: 16.6))211            stroke.addLine(to: CGPoint(x: 4.5, y: 5.8))212            stroke.closeSubpath()213            fill.addPath(circle(12, 11.2, 2.4))214215        case .archive:216            // A lidded box with a pull.217            stroke.addPath(rounded(3, 4.5, 18, 5, 2))218            stroke.move(to: CGPoint(x: 5, y: 9.5))219            stroke.addLine(to: CGPoint(x: 5, y: 17))220            stroke.addQuadCurve(to: CGPoint(x: 7.5, y: 19.5), control: CGPoint(x: 5, y: 19.5))221            stroke.addLine(to: CGPoint(x: 16.5, y: 19.5))222            stroke.addQuadCurve(to: CGPoint(x: 19, y: 17), control: CGPoint(x: 19, y: 19.5))223            stroke.addLine(to: CGPoint(x: 19, y: 9.5))224            line(&stroke, (10, 13.6), (14, 13.6))225226        case .morph:227            // A circle held inside a diamond: one form turning into another.228            line(&stroke, (12, 2.6), (21.4, 12), (12, 21.4), (2.6, 12))229            stroke.closeSubpath()230            fill.addPath(circle(12, 12, 3.3))231232        case .reader:233            // A page of text.234            stroke.addPath(rounded(5, 3, 14, 18, 3.5))235            line(&stroke, (8.6, 8), (15.4, 8))236            line(&stroke, (8.6, 12), (15.4, 12))237            line(&stroke, (8.6, 16), (12.4, 16))238239        case .dev:240            // Angle brackets around a slash.241            line(&stroke, (8.5, 7), (3.5, 12), (8.5, 17))242            line(&stroke, (15.5, 7), (20.5, 12), (15.5, 17))243            line(&stroke, (13.6, 4.8), (10.4, 19.2))244245        case .flow:246            // A step flowing into the next.247            stroke.addPath(circle(5.6, 6.5, 2.6))248            stroke.move(to: CGPoint(x: 8.2, y: 6.5))249            stroke.addCurve(to: CGPoint(x: 15.8, y: 17.5), control1: CGPoint(x: 14.5, y: 6.5),250                            control2: CGPoint(x: 9.5, y: 17.5))251            fill.addPath(circle(18.4, 17.5, 2.9))252253        case .capsule:254            // A capsule, half full.255            let tilt = CGAffineTransform(translationX: 12, y: 12).rotated(by: -.pi / 4)256            stroke.addPath(rounded(-10, -4.6, 20, 9.2, 4.6).applying(tilt))257            var half = Path()258            half.move(to: CGPoint(x: 0, y: -4.6))259            half.addLine(to: CGPoint(x: 5.4, y: -4.6))260            half.addArc(center: CGPoint(x: 5.4, y: 0), radius: 4.6, startAngle: .degrees(-90),261                        endAngle: .degrees(90), clockwise: false)262            half.addLine(to: CGPoint(x: 0, y: 4.6))263            half.closeSubpath()264            fill.addPath(half.applying(tilt))265266        case .bridge:267            // A suspension arch over a deck.268            line(&stroke, (2.5, 16), (21.5, 16))269            stroke.move(to: CGPoint(x: 4, y: 16))270            stroke.addQuadCurve(to: CGPoint(x: 20, y: 16), control: CGPoint(x: 12, y: 1))271            line(&stroke, (8, 10.6), (8, 16))272            line(&stroke, (12, 8.7), (12, 16))273            line(&stroke, (16, 10.6), (16, 16))274            line(&stroke, (5.5, 16), (5.5, 20.5))275            line(&stroke, (18.5, 16), (18.5, 20.5))276277        case .media:278            // A play head in a disc.279            stroke.addPath(circle(12, 12, 9))280            var play = Path()281            play.move(to: CGPoint(x: 9.8, y: 7.9))282            play.addLine(to: CGPoint(x: 16.4, y: 12))283            play.addLine(to: CGPoint(x: 9.8, y: 16.1))284            play.closeSubpath()285            fill.addPath(play)286287        case .focus:288            // A reticle locked on a point.289            stroke.addPath(circle(12, 12, 5.8))290            line(&stroke, (12, 2), (12, 4.2))291            line(&stroke, (12, 19.8), (12, 22))292            line(&stroke, (2, 12), (4.2, 12))293            line(&stroke, (19.8, 12), (22, 12))294            fill.addPath(circle(12, 12, 2.1))295        }296        return LunarGlyphGeometry(stroke: stroke, fill: fill)297    }298}299
Files main
Menu

Theme