Cotton code
176 lines · 8.3 KB · SwiftRaw
// The typed name card — a person with no photograph, and the thread with no// picture.//// It is the same object as an instant print without the print: a square of cream// paper with the name TYPED on it. The reference grid (`07-people-grid`) is what// settles the face — the names on those squares are struck by a typewriter, not// set in a serif — and it is also what makes the card work: a grid of prints with// gaps in it reads as missing data, and a grid of prints and typed cards reads as// a person who has not sent a photograph.//// `detail` is the second line, in pencil: a phone's last four, a thread's count,// whatever the screen has. Nothing when the screen has none.//// A MONOGRAM — a chat's initial, a group's two letters — is struck large, at// the theme's `monogram_size` of the side, and only when the caller says the// card carries one. A short name is still a name: "Jo" is typed at a name's// size, never guessed into a monogram by its letter count.import SwiftUIpublic struct TypedCard: View {    @Environment(\.theme) private var theme    private let name: String    private let detail: String?    /// Red for a birthday's words; nil keeps the meta line's own ink.    private let detailInk: PaperInk?    private let seed: String    private let dye: PaperDye    /// `false` for a card laid square among square prints, where nothing    /// tilts.    private let tilts: Bool    /// Whether `name` is a monogram (a chat's initial, a group's letters)    /// rather than a name.    private let monogram: Bool    public init(        name: String, detail: String? = nil, detailInk: PaperInk? = nil, seed: String, dye: PaperDye = .cream,        tilts: Bool = true, monogram: Bool = false    ) {        self.name = name        self.monogram = monogram        self.detail = detail        self.detailInk = detailInk        self.seed = seed        self.dye = dye        self.tilts = tilts    }    /// Every number a fraction of the card's own side, read from a theme.    struct Print {        let scale: ThemeScale        var nameFraction: CGFloat { TypedCardKnobs.typedCardNameSize }        var nameMin: CGFloat { TypedCardKnobs.typedCardNameMin }        var nameMax: CGFloat { TypedCardKnobs.typedCardNameMax }        var detailSize: CGFloat { TypedCardKnobs.typedCardDetailSize }        var detailMin: CGFloat { TypedCardKnobs.typedCardDetailMin }        var detailMax: CGFloat { TypedCardKnobs.typedCardDetailMax }        var gap: CGFloat { TypedCardKnobs.typedCardGap }        /// A MONOGRAM is struck at the theme's share of the side: at a name's        /// size it would be a speck on a 44pt card.        var monogramSize: CGFloat { scale[.monogramSize] }        var monogramMin: CGFloat { TypedCardKnobs.typedCardMonogramMin }        /// How far a name may shrink to fit — the card's floor on every screen.        var smallestScale: CGFloat { TypedCardKnobs.typedCardSmallestScale }        /// The step the name's tried sizes fall by — a tenth.        var scaleStep: CGFloat { TypedCardKnobs.typedCardNameScaleStep }        /// A birthday's handwritten words against the typed detail size.        var handDetailScale: CGFloat { TypedCardKnobs.typedCardHandDetailScale }        var nameLines: Int { TypedCardKnobs.typedCardNameLines }        /// The size the name — or the monogram — is struck at on a card        /// `side` points wide.        func nameSize(side: CGFloat, monogram: Bool) -> CGFloat {            if monogram { return max(monogramMin, side * monogramSize) }            return min(nameMax, max(nameMin, side * nameFraction))        }        /// The sizes a name is tried at, largest first: the card's own size        /// down to its floor, in steps of a tenth, and never under the        /// theme's caption size — a name two lines do not hold there is cut        /// by its lines, not shrunk further (architect, 16 Oct 2026).        func nameSizes(side: CGFloat, monogram: Bool) -> [CGFloat] {            let caption = scale[.typeCaptionSize]            let full = max(caption, nameSize(side: side, monogram: monogram))            let steps = stride(from: CGFloat(1), to: smallestScale, by: -scaleStep).map { full * $0 }            var sizes: [CGFloat] = []            for size in (steps + [full * smallestScale]).map({ max(caption, $0) }) where sizes.last != size {                sizes.append(size)            }            return sizes        }    }    /// The size the name is struck at on a card `side` points wide, in the    /// caller's worn theme — for the prints outside the kit that strike a name    /// the card's way.    static func nameSize(side: CGFloat, in scale: ThemeScale, monogram: Bool) -> CGFloat {        Print(scale: scale).nameSize(side: side, monogram: monogram)    }    public var body: some View {        let typed = Print(scale: theme.scale)        GeometryReader { geo in            let side = geo.size.width            Paper(dye: dye, edge: .torn, seed: seed, tilt: tilts, fills: true) {                VStack(spacing: side * typed.gap) {                    typedName(side: side, print: typed)                    if let detail {                        let size = min(typed.detailMax, max(typed.detailMin, side * typed.detailSize))                        // A birthday's words are written by hand in red                        // pencil; the time stays typed.                        Group {                            if let detailInk {                                Text(detail).paperHand(size * typed.handDetailScale, ink: detailInk)                            } else {                                Text(detail).paperMeta(size)                            }                        }                        .lineLimit(1)                    }                }                .frame(maxWidth: .infinity, maxHeight: .infinity)            }        }        .aspectRatio(1, contentMode: .fit)        .accessibilityElement(children: .combine)    }    /// The name, struck as a tile's words are (`InkFit.wholeWords`): at the    /// largest of its sizes where it stands whole in its two lines, never    /// broken inside a word — a word wider than the card's line even at the    /// floor is set smaller still, on a line of its own. A Text left to itself    /// keeps the size and splits the word ("Download / s" on the fan's    /// Downloads tile, 362 walk, 6 Nov 2026), so the size is measured here and    /// the break given where a Text would cut (`InkFit.broken`).    private func typedName(side: CGFloat, print typed: Print) -> some View {        let set = typed.name(name, side: side, face: theme.type.body, monogram: monogram)        return Text(set.words)            .paperBody(set.size)            .multilineTextAlignment(.center)            .lineLimit(typed.nameLines)            .frame(maxWidth: .infinity)    }}extension TypedCard.Print {    /// The width a name is typed in on a card `side` points wide: the    /// paper's own margins off it (`Paper`'s inset).    func line(side: CGFloat) -> CGFloat { max(0, side - 2 * scale.inset(forWidth: side)) }    /// The size a name is struck at on a card `side` points wide, and the    /// words as the Text is given them: whole words, at most two lines.    func name(_ name: String, side: CGFloat, face: ThemeFace, monogram: Bool) -> (size: CGFloat, words: String) {        let width = line(side: side)        let size = InkFit.wholeWords(name, face: face, sizes: nameSizes(side: side, monogram: monogram), width: width, lines: nameLines)        return (size, InkFit.broken(name, face: face, size: size, width: width))    }}/// The sizes, counts and timings TypedCard draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum TypedCardKnobs {    static let typedCardDetailMax: CGFloat = 13    static let typedCardDetailMin: CGFloat = 9    static let typedCardDetailSize: CGFloat = 0.085    static let typedCardGap: CGFloat = 0.035    static let typedCardHandDetailScale: CGFloat = 1.15    static let typedCardMonogramMin: CGFloat = 13    static let typedCardNameLines = 2    static let typedCardNameMax: CGFloat = 17    static let typedCardNameMin: CGFloat = 11    static let typedCardNameScaleStep: CGFloat = 0.1    static let typedCardNameSize: CGFloat = 0.115    static let typedCardSmallestScale: CGFloat = 0.55}