Cotton code
162 lines · 6.6 KB · SwiftRaw
// What the money's objects share: how a transaction's card is read for the// figures they type, and how a painted object is laid on the money's pink// terrazzo (`FaceGround.terrazzo`).//// A transaction's card (`DeckCard`, the money tool's Past and Future views)// carries its amount as the core said it ("₹2,340"), the account by its name,// and, among its facts, the account's balance before and after it ("Balance// before", "Balance after", 27 Oct 2026). It carries no direction: whether// money went out or came in is the kit's to choose by the face it names (a// receipt for a spend, a cheque for money in), and the faces draw what they// are handed. An object types the figures as a period object does, "Rs" and// two places ("Rs 1,24,500.00"), grouped as the core grouped them.import CottonPaperimport CottonValuesimport SwiftUIpublic enum MoneyReading {    /// The table's columns the balances are read from, as the transaction    /// table names them; their labels are what a fact stands under.    static let beforeColumn = "balance_before_minor"    static let afterColumn = "balance_after_minor"    /// The account's balance before and after the transaction, off the    /// card's facts: each by its column's label where the deck read the    /// labels, else the two facts in the order the view names them (before,    /// then after). Nil where the card does not say it.    public static func balances(_ card: DeckCard) -> (before: SaidAmount?, after: SaidAmount?) {        let facts = StudioReading.facts(card)        let labelled = { (column: String) -> SaidAmount? in            guard let label = card.labels[column: column] else { return nil }            return facts.first { $0.label == label }.flatMap { SaidAmount($0.said) }        }        if card.labels[column: beforeColumn] != nil || card.labels[column: afterColumn] != nil {            return (labelled(beforeColumn), labelled(afterColumn))        }        guard facts.count == 2 else { return (nil, nil) }        return (SaidAmount(facts[0].said), SaidAmount(facts[1].said))    }    /// The print's mark for an amount: "Rs" for rupees, as a period object    /// prints it, else the core's own mark ("$", "SGD").    public static func mark(_ amount: SaidAmount) -> String {        amount.isRupees || amount.mark.isEmpty ? "Rs" : amount.mark    }    /// An amount typed with its mark: "Rs 2,340.00".    public static func typed(_ amount: SaidAmount) -> String {        "\(mark(amount)) \(amount.typed)"    }    /// The amount in words, for rupees; nil for another currency, whose    /// words this writer does not know.    public static func words(_ amount: SaidAmount) -> String? {        guard amount.isRupees || amount.mark.isEmpty else { return nil }        return RupeeWords.written(rupees: amount.units, paise: amount.hundredths)    }    /// The record's short id, as a serial is printed: its last six letters    /// and figures, in capitals; nil for a card read off no record.    public static func serial(_ card: DeckCard) -> String? {        guard let id = card.record else { return nil }        let kept = id.filter { $0.isLetter || $0.isNumber }        guard !kept.isEmpty else { return nil }        return String(kept.suffix(6)).uppercased()    }    /// What a transaction is for: its description (`subtitle`), else its    /// category (`body`), and the part it is tagged as.    public static func purpose(_ card: DeckCard) -> (words: String, part: FacePart)? {        if let subtitle = card.text("subtitle") { return (subtitle, .subtitle) }        if let body = card.text("body") { return (body, .body) }        return nil    }}/// A painted layer of a money object, by its catalogue name, at the scale/// it was painted for; nil where this build carries no pictures.func moneyPainted(_ name: String) -> Image? {    CottonMedia.image(name, scale: 3)}/// A painted object lying on the felt: its shadow (painted larger than it/// by `reach` of its width on every side), then the object, at `size`; a/// flat stand-in of `standIn` where the build carries no pictures.struct MoneyPaper: View {    let name: String    let size: CGSize    let reach: CGFloat    let standIn: Color    var corner: CGFloat = 0.02    var body: some View {        let pad = reach * size.width        ZStack(alignment: .topLeading) {            if let shadow = moneyPainted("\(name)-shadow") {                shadow.resizable().interpolation(.high)                    .frame(width: size.width + 2 * pad, height: size.height + 2 * pad)                    .offset(x: -pad, y: -pad)            }            if let paper = moneyPainted(name) {                paper.resizable().interpolation(.high)                    .frame(width: size.width, height: size.height)                    .cardPaperLaid()            } else {                RoundedRectangle(cornerRadius: corner * size.width).fill(standIn)                    .frame(width: size.width, height: size.height)            }        }        .frame(width: size.width, height: size.height, alignment: .topLeading)        .accessibilityHidden(true)    }}/// A dashed rule a till prints between its parts.struct PrintedDashes: View {    let ink: Color    var weight: CGFloat = 1    var body: some View {        GeometryReader { box in            Path { path in                path.move(to: CGPoint(x: 0, y: box.size.height / 2))                path.addLine(to: CGPoint(x: box.size.width, y: box.size.height / 2))            }            .stroke(ink, style: StrokeStyle(lineWidth: weight, dash: [weight * 3.5, weight * 2.2]))        }        .frame(height: max(weight, 1))        .accessibilityHidden(true)    }}/// One line set in a face, made to fit its room: never wrapped, squeezed/// to the room rather than cut.struct FittedLine: View {    let words: String    let font: Font    let ink: Color    var alignment: Alignment = .leading    var tracking: CGFloat = 0    var shrink: CGFloat = 0.5    var body: some View {        Text(words)            .font(font)            .tracking(tracking)            .foregroundStyle(ink)            .lineLimit(1)            .minimumScaleFactor(shrink)            .frame(maxWidth: .infinity, alignment: alignment)    }}extension View {    /// Set `x` in and `y` down from its container's top leading corner, as    /// laid out — not an offset, which moves the drawing and leaves the    /// part's tagged frame (`facePart`) and its accessibility frame behind.    /// A part is tagged before it is placed, so its tag is its own frame.    func placed(x: CGFloat = 0, y: CGFloat = 0) -> some View {        padding(.leading, x).padding(.top, y)    }}