Cotton code
272 lines · 11.5 KB · SwiftRaw
// People as the open passport booklet chosen on 30 October 2026.// Codex-painted cloth, paper and stamp ink; every word and the photograph// are live. Only this person's DeckCard is read. No related rows or slips.// Geometry is measured against import-passport-materials.py's cropped book.import CottonPaperimport CottonValuesimport SwiftUI/// The facts this card can ink. Missing facts leave the stamps page bare.public struct PassportReading: Equatable, Sendable {    public struct Stamp: Equatable, Sendable {        public let heading: String        public let words: String    }    public let name: String?    public let role: String?    public let workplace: String?    public let phone: String?    public let email: String?    public let city: String?    public let birthday: String?    public let summary: String?    public let stamps: [Stamp]    public init(_ card: DeckCard) {        name = card.text("title")        // The core joins role/org/city in today's subtitle. A separately        // mapped place/org is needed to identify the workplace reliably.        role = card.text("subtitle")?.components(separatedBy: ShellLines.line("cards", "join")).first        workplace = card.text("place") ?? card.text("org")        phone = Self.first(card.reach.phones) ?? Self.first(card.fields["phones"]) ?? Self.first(card.fields["phone"])        email = Self.first(card.reach.emails) ?? Self.first(card.fields["emails"])        city = card.text("city")        birthday = card.text("birthday") ?? card.corner.flatMap { SaidRun.cut($0, by: "corner.birthday")?.said }        summary = card.text("body")        var marks: [Stamp] = []        if let reached = card.text("reached") {            // Strip only a recognised template's heading. Keep the core's            // relative words and channel; no local clock or inferred contact.            let run = SaidRun.cut(reached, by: "field.reached")            marks.append(Stamp(heading: ShellLines.line("cards", "faces.last_spoke"), words: run.map { $0.said + $0.tail } ?? reached))        }        if let birthday { marks.append(Stamp(heading: "BIRTHDAY", words: birthday)) }        if let since = card.text("known_since") { marks.append(Stamp(heading: ShellLines.line("cards", "faces.known_since"), words: since)) }        if let reach = card.text("reach_90d") { marks.append(Stamp(heading: "90 DAYS", words: reach)) }        stamps = marks    }    private static func first(_ words: [String]) -> String? {        words.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }.first { !$0.isEmpty }    }    private static func first(_ value: RecordValue?) -> String? {        if case .list(let values)? = value { return first(values.compactMap(\.text)) }        return value?.text.flatMap { first([$0]) }    }}struct PassportFace: View {    let card: DeckCard    let photo: FacePicture?    public init(card: DeckCard, photo: FacePicture?) {        self.card = card        self.photo = photo    }    public var body: some View {        GeometryReader { box in            let width = min(box.size.width * 0.98, box.size.height * 0.96 * 1.12)            PassportBook(card: card, photo: photo, width: width)                .frame(width: width, height: width / 1.12)                .frame(width: box.size.width, height: box.size.height)        }    }}private struct PassportBook: View {    let card: DeckCard    let photo: FacePicture?    let width: CGFloat    private var height: CGFloat { width / 1.12 }    private var reading: PassportReading { PassportReading(card) }    @Environment(\.theme) private var theme    private var ink: Color { theme.faceStocks.passportInk }    var body: some View {        ZStack(alignment: .topLeading) {            material("passport-shadow")                .frame(width: width * 1.12, height: height + width * 0.12)                .offset(x: -width * 0.06, y: -width * 0.06)            material("passport-booklet")                .frame(width: width, height: height)            photoPage                .offset(x: width * 0.055, y: height * 0.045)            stampsPage                .offset(x: width * 0.55, y: height * 0.09)        }        .frame(width: width, height: height, alignment: .topLeading)        .accessibilityElement(children: .contain)        .accessibilityIdentifier("passport")    }    private var photoPage: some View {        let page = width * 0.39        return VStack(alignment: .leading, spacing: height * 0.012) {            printPage(side: width * 0.225)                .rotationEffect(.degrees(-1.2))                .facePart(.picture)                .padding(.bottom, height * 0.015)            if let name = reading.name {                Text(name)                    .font(CottonFonts.caslon(width * 0.057))                    .lineLimit(2)                    .minimumScaleFactor(0.65)                    .fixedSize(horizontal: false, vertical: true)                    .facePart(.title)                    .accessibilityIdentifier("face-name")            }            if let role = reading.role {                words(role, size: width * 0.029, lines: 1)                    .facePart(.subtitle)            }            if let workplace = reading.workplace {                words(workplace, size: width * 0.028, lines: 1)                    .facePart(.subtitle)            }            VStack(alignment: .leading, spacing: height * 0.01) {                if let phone = reading.phone { fact("PHONE", phone, part: .call) }                if let email = reading.email { fact("EMAIL", email, part: .email) }                if let city = reading.city { fact("CITY", city, part: .place) }                if let birthday = reading.birthday { fact("BIRTHDAY", birthday, part: .when) }            }            .padding(.top, height * 0.017)            if let summary = reading.summary {                words(summary, size: width * 0.029, lines: 3)                    .facePart(.body)                    .padding(.top, height * 0.016)            }        }        .foregroundStyle(ink)        .frame(width: page, alignment: .leading)    }    private func printPage(side: CGFloat) -> some View {        ZStack {            // The rolodex's painted photographic print, on this page too.            material("rolodex-print").frame(width: side, height: side)            Group {                if let photo {                    photo.frame(width: side * 0.88, height: side * 0.87).clipped()                } else if let initials = reading.name.flatMap(CardPerson.initials) {                    Text(initials).font(CottonFonts.hand(side * 0.32)).foregroundStyle(ink.opacity(0.55))                }            }            .frame(width: side * 0.88, height: side * 0.87)        }        .frame(width: side, height: side)    }    private func fact(_ label: String, _ value: String, part: FacePart) -> some View {        VStack(alignment: .leading, spacing: 3) {            HStack(alignment: .firstTextBaseline, spacing: 3) {                Text(label).font(CottonFonts.type(width * 0.022)).foregroundStyle(ink.opacity(0.7))                    .frame(width: width * 0.115, alignment: .leading)                words(value, size: width * 0.031, lines: 1)            }            Rectangle().fill(ink.opacity(0.17)).frame(height: 0.45)        }        .facePart(part)    }    private var stampsPage: some View {        let colours = theme.faceStocks.passportStamps        let turns = [-8.0, 6.0, -4.0, 8.0]        return VStack(alignment: .leading, spacing: height * 0.04) {            ForEach(Array(reading.stamps.enumerated()), id: \.offset) { index, stamp in                stampView(stamp, ink: colours[index], index: index)                    .rotationEffect(.degrees(turns[index]))                    .offset(x: index.isMultiple(of: 2) ? 0 : width * 0.018)            }        }        .frame(width: width * 0.385, alignment: .leading)    }    private func stampView(_ stamp: PassportReading.Stamp, ink: Color, index: Int) -> some View {        let w = width * 0.365        let h = height * 0.18        return ZStack {            ink.mask(material("passport-stamp-border"))            VStack(alignment: .leading, spacing: 2) {                Text(stamp.heading).font(CottonFonts.type(width * 0.037))                    .lineLimit(1).minimumScaleFactor(0.7)                words(stamp.words, size: width * 0.035, lines: 2)            }            .foregroundStyle(ink)            .frame(width: w * 0.85, height: h * 0.75, alignment: .leading)            .mask(material("passport-stamp-grain"))        }        .frame(width: w, height: h)        .blendMode(.multiply)        .accessibilityElement(children: .ignore)        .accessibilityLabel("\(stamp.heading), \(stamp.words)")        .accessibilityIdentifier("passport-stamp-\(index)")        .facePart(.when)        .modifier(PassportMessagePart(on: stamp.heading == ShellLines.line("cards", "faces.last_spoke") && card.reach.message))    }    private func words(_ text: String, size: CGFloat, lines: Int) -> some View {        Text(text).font(CottonFonts.type(size)).lineLimit(lines).minimumScaleFactor(0.65)            .frame(maxWidth: .infinity, alignment: .leading)    }    private func material(_ name: String) -> some View {        CottonMedia.image(name, scale: 3)?.resizable().interpolation(.high).accessibilityHidden(true)    }}/// The reached ink is the date and, when offered, the person's message way.private struct PassportMessagePart: ViewModifier {    let on: Bool    func body(content: Content) -> some View {        if on { content.facePart(.message) } else { content }    }}public enum PassportFaceCatalogue {    public static let stock: FaceStock = .own    public static let ground: FaceGround = .amethystDesk    public static let face = Face(id: "person.passport", specimens: [        Specimen("friend", friend, picture: "rolodex-ananya"),        Specimen("acquaintance", acquaintance, picture: "rolodex-friend"),        Specimen("long-name", longName, picture: "portrait"),        Specimen("no-picture", friend),        Specimen("unreached", unreached, picture: "rolodex-ananya"),    ], steps: .cut) { card, photo, _ in        PassportFace(card: card, photo: photo)    }    private static func person(_ fields: [String: RecordValue], phones: [String] = [], emails: [String] = []) -> DeckCard {        var card = DeckCard(face: "person.passport", corner: nil, pressing: false, pace: nil, plate: nil, fields: fields)        card.reach = CardReach(phones: phones, emails: emails, call: !phones.isEmpty, message: !phones.isEmpty, email: !emails.isEmpty)        return card    }    public static let friend = person([        "title": .text("Kavya Menon"), "subtitle": .text("Architect"),        "place": .text("Estuary Studio"), "city": .text("Kochi"),        "body": .text("College friend. Collects old maps."),        "birthday": .text("14 March"), "known_since": .text("2019"),        "reached": .text("9 days ago · WhatsApp"), "reach_90d": .text("14 calls"),    ], phones: ["+91 90000 00001"], emails: ["kavya@example.org"])    public static let acquaintance = person([        "title": .text("Nikhil Bhat"), "reached": .text("3 weeks ago · Phone"),    ], phones: ["+91 90000 00002"])    public static let longName: DeckCard = {        var card = friend        card.fields["title"] = .text("Venkata Padmavathi Krishnamurthy")        return card    }()    public static let unreached: DeckCard = {        var card = friend        for slot in ["reached", "known_since", "reach_90d"] { card.fields.removeValue(forKey: slot) }        return card    }()}