Cotton code
246 lines · 8.9 KB · SwiftRaw
// The pieces the reference cards share (`~/lanes/faces/cards`): the small// typed word that names what a card is ("Transaction", "Machine"), a fact// typed under its small label, a luggage tag on a string for a category, and// a rubber stamp in red for a state ("Not reporting", "Cancelled", "Not read// yet"). Every word they draw is the core's; they only set it.import CottonPaperimport CottonValuesimport SwiftUIpublic enum FacePartsKnobs {    public static let kindSize: CGFloat = 11.5    public static let labelSize: CGFloat = 11    public static let valueSize: CGFloat = 16    public static let valueHand: CGFloat = 23    public static let labelGap: CGFloat = 3    public static let tagSize: CGFloat = 14    public static let tagHeight: CGFloat = 34    public static let stampSize: CGFloat = 13}/// The small typed word over a card's heading.public struct FaceKind: View {    let words: String    public init(words: String) {        self.words = words    }    public var body: some View {        Text(words)            .font(CottonFonts.type(FacePartsKnobs.kindSize))            .lineLimit(1)            .faceInk(.ink)    }}/// The label a face's fact is drawn under, in the core's words (the cards/// set's `label`, package row 8): the face's own where it says it/// differently ("Last used" on a password's date), else the slot's; nil/// when neither is said.public enum FactLabel {    public static func of(_ face: String, _ slot: String) -> String? {        for path in ["label.face.\(face).\(slot)", "label.slot.\(slot)"] {            let said = ShellLines.line("cards", path)            if said != "cards.\(path)" { return said }        }        return nil    }}extension DeckCard {    /// The label over a place: the card's own words for it when its place    /// says some (`fields.labels`, keyed by slot — Money's Future "Due"),    /// else the face's or the slot's line (`FactLabel`).    public func label(_ slot: String) -> String? {        if case .object(let own)? = fields["labels"], let said = own[slot]?.text, !said.isEmpty { return said }        return FactLabel.of(face, slot)    }}/// A fact's small label, typed in pencil over it.public struct FaceLabel: View {    let words: String    public init(words: String) {        self.words = words    }    public var body: some View {        Text(words)            .font(CottonFonts.type(FacePartsKnobs.labelSize))            .lineLimit(1)            .faceInk(.pencil)    }}/// A fact typed under its small label; a date or a when is written by hand.public struct FaceFact: View {    var label: String?    let value: String    var hand = false    var ink: PaperInk = .ink    var size: CGFloat = FacePartsKnobs.valueSize    /// One line shrinks to fit rather than wrapping (a key, a number).    var lines = 2    public init(        label: String? = nil, value: String, hand: Bool = false, ink: PaperInk = .ink,        size: CGFloat = FacePartsKnobs.valueSize, lines: Int = 2    ) {        self.label = label        self.value = value        self.hand = hand        self.ink = ink        self.size = size        self.lines = lines    }    public var body: some View {        VStack(alignment: .leading, spacing: FacePartsKnobs.labelGap) {            if let label {                Text(label)                    .font(CottonFonts.type(FacePartsKnobs.labelSize))                    .lineLimit(1)                    .faceInk(.pencil)            }            Text(hand ? HandRun.tail(value) : value)                .font(hand ? CottonFonts.hand(FacePartsKnobs.valueHand * size / FacePartsKnobs.valueSize) : CottonFonts.type(size))                .lineLimit(lines)                .minimumScaleFactor(lines == 1 ? 0.6 : 1)                .faceInk(ink)                .fixedSize(horizontal: false, vertical: true)        }        .accessibilityElement(children: .combine)    }}/// A word on a buff luggage tag, its eyelet at the left and a loop of string/// through it (the money cards' category).public struct HangTag: View {    let words: String    @Environment(\.theme) private var theme    public init(words: String) {        self.words = words    }    public var body: some View {        Text(words)            .font(CottonFonts.type(FacePartsKnobs.tagSize))            .lineLimit(1)            .faceInk(.ink)            .padding(.leading, 36)            .padding(.trailing, 18)            .frame(height: FacePartsKnobs.tagHeight)            .background(                PaperSurface(dye: .buff, edge: .cut, seed: "hang-\(words)")                    .saturation(1.3)                    .clipShape(TagShape())                    .shadow(color: theme.colors.shadow.color.opacity(0.22), radius: 1.6, x: 0.5, y: 1.2)            )            .overlay(alignment: .leading) {                ZStack {                    Circle().fill(theme.colors.shadow.color.opacity(0.35)).frame(width: 7, height: 7)                    Circle().stroke(theme.print(.pencil).opacity(0.6), lineWidth: 1.2).frame(width: 10, height: 10)                }                .padding(.leading, 14)            }            .overlay(alignment: .leading) {                TagString()                    .stroke(theme.print(.pencil).opacity(0.75), style: StrokeStyle(lineWidth: 1.1, lineCap: .round))                    .frame(width: 34, height: 22)                    .offset(x: -16, y: 2)                    .blendMode(theme.printBlend)            }            .rotationEffect(.degrees(-3))            .accessibilityIdentifier("face-tag")    }}/// A luggage tag: the left corners cut off.struct TagShape: Shape {    func path(in rect: CGRect) -> Path {        let cut = min(rect.height * 0.32, 12)        var path = Path()        path.move(to: CGPoint(x: rect.minX + cut, y: rect.minY))        path.addLine(to: CGPoint(x: rect.maxX, y: rect.minY))        path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))        path.addLine(to: CGPoint(x: rect.minX + cut, y: rect.maxY))        path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY - cut))        path.addLine(to: CGPoint(x: rect.minX, y: rect.minY + cut))        path.closeSubpath()        return path    }}/// The loop of string through a tag's eyelet, trailing off to the left.private struct TagString: Shape {    func path(in rect: CGRect) -> Path {        var path = Path()        path.move(to: CGPoint(x: rect.maxX, y: rect.midY - 2))        path.addCurve(to: CGPoint(x: rect.minX, y: rect.maxY), control1: CGPoint(x: rect.midX, y: rect.minY - 4), control2: CGPoint(x: rect.minX + 6, y: rect.midY))        path.move(to: CGPoint(x: rect.maxX, y: rect.midY + 2))        path.addCurve(to: CGPoint(x: rect.minX + 4, y: rect.maxY + 2), control1: CGPoint(x: rect.midX, y: rect.maxY + 6), control2: CGPoint(x: rect.minX + 10, y: rect.maxY))        return path    }}/// A rubber stamp in red: the words inside a ruled box, inked unevenly,/// struck at an angle.public struct RubberStamp: View {    let words: String    var size: CGFloat = FacePartsKnobs.stampSize    var turn: Double = -5    /// Red for a state that wants the owner; a face may lay it in another ink.    var ink: PaperInk = .red    @Environment(\.theme) private var theme    public init(words: String, size: CGFloat = FacePartsKnobs.stampSize, turn: Double = -5, ink: PaperInk = .red) {        self.words = words        self.size = size        self.turn = turn        self.ink = ink    }    public var body: some View {        Text(words)            .font(CottonFonts.type(size))            .lineLimit(1)            .padding(.horizontal, size * 0.6)            .padding(.vertical, size * 0.3)            .overlay(RoundedRectangle(cornerRadius: 2).inset(by: 1).stroke(lineWidth: 1.5))            .overlay(RoundedRectangle(cornerRadius: 3).inset(by: -2).stroke(lineWidth: 0.8))            .foregroundStyle(theme.print(ink))            .mask(StampWear(seed: words))            .blendMode(theme.printBlend)            .rotationEffect(.degrees(turn))            .accessibilityIdentifier("face-state")    }}/// Where a stamp's ink did not take: a few faint gaps across it.public struct StampWear: View {    let seed: String    public init(seed: String) {        self.seed = seed    }    public var body: some View {        Canvas { context, size in            context.fill(Path(CGRect(origin: .zero, size: size)), with: .color(.black))            var state = UInt64(truncatingIfNeeded: seed.unicodeScalars.reduce(5381) { ($0 << 5) &+ $0 &+ UInt64($1.value) })            func next() -> CGFloat {                state = state &* 6364136223846793005 &+ 1442695040888963407                return CGFloat(state >> 33) / CGFloat(UInt32.max >> 1)            }            context.blendMode = .destinationOut            for _ in 0..<14 {                let rect = CGRect(x: next() * size.width, y: next() * size.height, width: 2 + next() * 7, height: 1 + next() * 2.5)                context.fill(Path(ellipseIn: rect), with: .color(.black.opacity(0.75)))            }        }    }}