Cotton code
422 lines · 18.2 KB · SwiftRaw
// The ALBUM as INSTANT PRINTS PINNED ON A CORK BOARD (the owner's pick, 30 Oct// 2026, 00:25 IST: "for albums I like the album board"; the album picture// `objects-2/photos-2/5-album-board`): a cork board in its thin pine frame// lying bare on the Photos tool's oxblood bookcloth (`FaceGround.bookcloth`),// with no paper and no rule of the deck's. The board, its frame, its shadow,// the brass pins, the instant prints' card and the label's paper are// PAINTED (`Media.xcassets`, cut and imported by// `scripts/import-photos-materials.py`); the face draws live only what the// album decides:////   up to five of its prints, each a picture drawn inside a painted instant//   print's square window, laid at the board's five places and tilts, the//   cover first, largest and on top, the rest under and round it;//   a brass pin in each place, holding its print — and in a place the//   album has no print for, standing in the bare cork: fewer prints leave//   the cork bare, never a stand-in print;//   the typed paper label pinned at the board's top: the album's name (three//   lines at most, the label growing with it) and under it how many photos it holds and its days.//// Every word is the core's (`DeckCard`): the name (`title`), the count// (`count`, said "23 photos" by the shell's own count line) and the days// (`span`); the prints are the album's own rows the deck reads for its card// (`DeckCard.prints`, the cover named by `photo` first). Stepped by a tap,// it cuts (`FaceStep.cut`).import CottonPaperimport CottonValuesimport SwiftUIenum AlbumBoardFaceKnobs {    // THE BOARD, as painted (`album-board`, 1050 by 1308 pixels): its width    // over its height, and how far its shadow (`album-board-shadow`)    // reaches past it on every side, as a share of its width.    static let aspect: CGFloat = 0.8025    static let shadowReach: CGFloat = 0.0577    /// The board in the card's room: the most of its width and height.    static let fill: CGFloat = 0.97    // AN INSTANT PRINT, as painted (`album-instant`, 680 by 836): the    // layer's width over its height, and the card's and the picture    // window's place in it (left, top, right, bottom, as shares of the    // layer; the rest is its shadow).    static let instantAspect: CGFloat = 680.0 / 836.0    static let instantCard: [CGFloat] = [0.0588, 0.0478, 0.9412, 0.9522]    static let instantWindow: [CGFloat] = [0.1091, 0.0956, 0.8928, 0.7332]    /// An instant print's card, its width over its height, as painted.    static var cardAspect: CGFloat {        (instantCard[2] - instantCard[0]) * instantAspect / (instantCard[3] - instantCard[1])    }    // A PIN (`album-pin`, 132 square): its head's share of the layer.    static let pinHead: CGFloat = 0.7272    /// A pin's head across, as a share of the board's width.    static let pinSize: CGFloat = 0.05    /// How far below a print's top edge its pin stands, as a share of the    /// print's height.    static let pinDrop: CGFloat = 0.055    // THE LABEL (`album-label`, 780 by 279): its paper's place in the layer.    static let labelPaper: [CGFloat] = [0.0385, 0.1075, 0.9615, 0.8925]    /// The label's paper: its width, its top and its middle across, as    /// shares of the board's.    static let labelWidth: CGFloat = 0.72    static let labelTop: CGFloat = 0.035    static let labelTurn: Double = -0.6    /// The type on the label, as shares of the board's width (on a phone's    /// board, about 17 and 12 points): print on an object scales with the    /// object, never with Dynamic Type.    static let nameSize: CGFloat = 0.054    static let factsSize: CGFloat = 0.037    /// The name takes up to three lines, the label growing with it, and a    /// name longer still is set a little smaller, down to `nameLeast` of its    /// size, before its last line is cut.    static let nameLines = 3    static let nameLeast: CGFloat = 0.8    /// The label's paper round its words, as shares of the board's width.    static let labelPadding = EdgeInsets(top: 0.03, leading: 0.04, bottom: 0.028, trailing: 0.04)    static let lineGap: CGFloat = 0.006}/// One of the board's five places: where a print's card stands (its middle,/// as shares of the board's width and height), how wide it is (a share of/// the board's width), how far it is tilted, and where across its top its/// pin goes in (a share of its width from its middle), so every pin stands/// clear of the prints laid over it.public struct BoardPlace: Equatable, Sendable {    public var x: CGFloat    public var y: CGFloat    public var width: CGFloat    public var turn: Double    public var pin: CGFloat    /// The five places, the cover's first, in the order the album's prints    /// fill them.    public static let all = [        BoardPlace(x: 0.42, y: 0.45, width: 0.5, turn: -1.5, pin: 0),        BoardPlace(x: 0.79, y: 0.36, width: 0.32, turn: 3.5, pin: 0),        BoardPlace(x: 0.21, y: 0.77, width: 0.36, turn: -4, pin: -0.22),        BoardPlace(x: 0.6, y: 0.79, width: 0.34, turn: -2.5, pin: 0.32),        BoardPlace(x: 0.8, y: 0.66, width: 0.32, turn: 3, pin: 0),    ]    /// The order the places are laid in, back to front: the cover last, on top.    public static let stacking = [1, 4, 3, 2, 0]    /// The card's height on a board `board` points.    func height(on board: CGSize) -> CGFloat {        width * board.width / AlbumBoardFaceKnobs.cardAspect    }    /// Where its pin stands on a board `board` points: at its print's top,    /// a little down and along it as the place says, turned with it.    public func pin(on board: CGSize) -> CGPoint {        let height = height(on: board)        let up = height / 2 - AlbumBoardFaceKnobs.pinDrop * height        let across = pin * width * board.width        let angle = turn * .pi / 180        return CGPoint(            x: x * board.width + cos(angle) * across + sin(angle) * up,            y: y * board.height + sin(angle) * across - cos(angle) * up)    }    /// Whether its card, on a board `board` points, covers `point`.    public func covers(_ point: CGPoint, on board: CGSize) -> Bool {        let angle = -turn * .pi / 180        let dx = point.x - x * board.width        let dy = point.y - y * board.height        let along = dx * cos(angle) - dy * sin(angle)        let down = dx * sin(angle) + dy * cos(angle)        return abs(along) <= width * board.width / 2 && abs(down) <= height(on: board) / 2    }}/// The prints an album's board shows: the cover first, then the album's/// others as it holds them, at most one for each place.public enum BoardPrints {    public static func shown(_ card: DeckCard) -> [String] {        Array(AlbumPrints.ordered(card.prints ?? [], cover: card.text("photo")).prefix(BoardPlace.all.count))    }    /// The facts typed under the album's name: how many photos it holds and    /// its days, as the core says them; nil when it says neither.    public static func facts(_ card: DeckCard) -> String? {        let said = [count(card), card.text("span")].compactMap { $0 }        return said.isEmpty ? nil : said.joined(separator: ShellLines.line("cards", "join"))    }    /// How many photos the album holds, "23 photos", in the shell's own    /// count line; nil when the card says no count.    public static func count(_ card: DeckCard) -> String? {        card.text("count").map { words in            let number = Int(words.filter(\.isNumber)) ?? 0            return ShellLines.line("projects", number == 1 ? "count.photo.one" : "count.photo.many", ["n": words])        }    }}public struct AlbumBoardFace: View {    let card: DeckCard    public init(card: DeckCard) {        self.card = card    }    public var body: some View {        let knobs = AlbumBoardFaceKnobs.self        GeometryReader { box in            let width = min(box.size.width * knobs.fill, box.size.height * knobs.fill * knobs.aspect)            let size = CGSize(width: width, height: width / knobs.aspect)            CorkBoard(card: card, prints: BoardPrints.shown(card), size: size)                .frame(width: size.width, height: size.height)                .frame(width: box.size.width, height: box.size.height)        }        .accessibilityElement(children: .contain)        .accessibilityIdentifier("album-board")    }}/// A painted layer, by its catalogue name, at the scale it was painted/// for; nil where this build carries no pictures.private func painted(_ name: String) -> Image? {    CottonMedia.image(name, scale: 3)}/// The board lying on the cloth: its shadow, the board, the prints in their/// places with their pins, the empty places' pins, and the label over all.private struct CorkBoard: View {    let card: DeckCard    let prints: [String]    let size: CGSize    var body: some View {        let knobs = AlbumBoardFaceKnobs.self        let w = size.width        let reach = knobs.shadowReach * w        ZStack(alignment: .topLeading) {            if let shadow = painted("album-board-shadow") {                shadow.resizable()                    .frame(width: w + 2 * reach, height: size.height + 2 * reach)                    .offset(x: -reach, y: -reach)                    .accessibilityHidden(true)            }            Group {                if let board = painted("album-board") {                    board.resizable().interpolation(.high)                } else {                    PhotosMaterial.pine.colour                        .overlay(PhotosMaterial.cork.colour.padding(w * 0.045))                }            }            .frame(width: w, height: size.height)            .accessibilityHidden(true)            ForEach(BoardPlace.stacking, id: \.self) { index in                let place = BoardPlace.all[index]                if index < prints.count {                    InstantPrint(key: prints[index], width: place.width * w)                        .rotationEffect(.degrees(place.turn))                        .position(x: place.x * w, y: place.y * size.height)                        .facePart(.picture)                }                Pin(size: knobs.pinSize * w)                    .position(place.pin(on: size))            }            BoardLabel(card: card, width: knobs.labelWidth * w, board: w)                .rotationEffect(.degrees(knobs.labelTurn))                .frame(width: w, alignment: .top)                .padding(.top, knobs.labelTop * size.height)        }        .frame(width: w, height: size.height, alignment: .topLeading)    }}/// One instant print: the picture in the painted print's square window,/// the painted card over its edge, its shadow round it.private struct InstantPrint: View {    let key: String    /// The card's width, in points.    let width: CGFloat    var body: some View {        let knobs = AlbumBoardFaceKnobs.self        let card = knobs.instantCard        let window = knobs.instantWindow        let layer = CGSize(width: width / (card[2] - card[0]), height: width / (card[2] - card[0]) / knobs.instantAspect)        let opening = CGRect(            x: window[0] * layer.width, y: window[1] * layer.height,            width: (window[2] - window[0]) * layer.width, height: (window[3] - window[1]) * layer.height)        ZStack(alignment: .topLeading) {            FacePicture(key: key, edge: max(opening.width, opening.height))                .frame(width: opening.width + 2, height: opening.height + 2)                .clipped()                .offset(x: opening.minX - 1, y: opening.minY - 1)            if let print = painted("album-instant") {                print.resizable().interpolation(.high)                    .frame(width: layer.width, height: layer.height)            } else {                Rectangle()                    .path(in: CGRect(                        x: card[0] * layer.width, y: card[1] * layer.height,                        width: (card[2] - card[0]) * layer.width, height: (card[3] - card[1]) * layer.height))                    .subtracting(Rectangle().path(in: opening))                    .fill(PhotosMaterial.card.colour)            }        }        .frame(width: layer.width, height: layer.height, alignment: .topLeading)        .accessibilityHidden(true)    }}/// A brass drawing pin's head, `size` points across, its shadow round it.private struct Pin: View {    let size: CGFloat    var body: some View {        let layer = size / AlbumBoardFaceKnobs.pinHead        Group {            if let pin = painted("album-pin") {                pin.resizable().interpolation(.high)            } else {                Circle().fill(PhotosMaterial.brass.colour).padding(layer * (1 - AlbumBoardFaceKnobs.pinHead) / 2)            }        }        .frame(width: layer, height: layer)        .accessibilityHidden(true)    }}/// The typed paper label pinned at the board's top: the album's name, and/// under it its count and its days.private struct BoardLabel: View {    let card: DeckCard    /// The paper's width, and the board's, in points.    let width: CGFloat    let board: CGFloat    var body: some View {        let knobs = AlbumBoardFaceKnobs.self        let paper = knobs.labelPaper        let pad = knobs.labelPadding        let facts = BoardPrints.facts(card)        VStack(spacing: knobs.lineGap * board) {            if let title = card.text("title") {                Text(title)                    .font(CottonFonts.type(knobs.nameSize * board))                    .lineLimit(knobs.nameLines)                    .minimumScaleFactor(knobs.nameLeast)                    .multilineTextAlignment(.center)                    .fixedSize(horizontal: false, vertical: true)                    .foregroundStyle(PhotosMaterial.type.colour)                    .accessibilityIdentifier("deck-title")                    .facePart(.title)            }            if let facts {                FactsLine(card: card, size: knobs.factsSize * board)                    .accessibilityRepresentation { Text(facts) }                    .accessibilityIdentifier("album-board-facts")            }        }        .padding(EdgeInsets(top: pad.top * board, leading: pad.leading * board, bottom: pad.bottom * board, trailing: pad.trailing * board))        .frame(width: width)        .background {            GeometryReader { room in                let layer = CGSize(width: room.size.width / (paper[2] - paper[0]), height: room.size.height / (paper[3] - paper[1]))                Group {                    if let label = painted("album-label") {                        label.resizable().interpolation(.high)                    } else {                        PhotosMaterial.label.colour                            .padding(EdgeInsets(                                top: paper[1] * layer.height, leading: paper[0] * layer.width,                                bottom: (1 - paper[3]) * layer.height, trailing: (1 - paper[2]) * layer.width))                    }                }                .frame(width: layer.width, height: layer.height)                .offset(x: -paper[0] * layer.width, y: -paper[1] * layer.height)                .accessibilityHidden(true)            }        }        .overlay(alignment: .top) {            Pin(size: knobs.pinSize * board)                .offset(y: -knobs.pinSize * board * 0.35)        }    }}/// The label's second line: the count (the corner's) and the days (when),/// each its own part, joined as the core joins facts.private struct FactsLine: View {    let card: DeckCard    let size: CGFloat    var body: some View {        let count = BoardPrints.count(card)        let span = card.text("span")        HStack(spacing: 0) {            if let count {                Text(count).facePart(.corner)            }            if count != nil, span != nil {                Text(ShellLines.line("cards", "join"))            }            if let span {                Text(span).facePart(.when)            }        }        .font(CottonFonts.type(size))        .lineLimit(1)        .minimumScaleFactor(0.6)        .foregroundStyle(PhotosMaterial.type.colour)    }}/// The album board, by its id beside the album's own face, and the albums/// it is reviewed in: invented pictures only.public enum AlbumBoardFaceCatalogue {    public static let stock: FaceStock = .own    public static let ground: FaceGround = .bookcloth    public static let face = Face(id: "album.board", specimens: [        Specimen("weekend", weekend, picture: "kelmoor-lake"), Specimen("two", two, picture: "verandah"),        Specimen("long-name", longName, picture: "waterfall"),    ], steps: .cut) { card, _, _ in        AlbumBoardFace(card: card)    }    static func album(_ fields: [String: RecordValue], prints: [String]) -> DeckCard {        var card = DeckCard(face: "album.board", corner: nil, pressing: false, pace: nil, plate: nil, fields: fields)        card.prints = prints        return card    }    /// A weekend away: more prints than the board holds, the lake its cover.    public static let weekend = album([        "title": .text("Weekend in Kelmoor"),        "count": .text("23"),        "span": .text("Sun 26 Oct"),        "subtitle": .text("Kelmoor"),        "photo": .text("kelmoor-lake"),    ], prints: ["kelmoor-jetty", "kelmoor-lake", "portrait", "verandah", "waterfall", "verandah-wide", "invitation-dinner"])    /// An afternoon's two prints: three places left to the bare cork.    public static let two = album([        "title": .text("Tea at the hill house"),        "count": .text("2"),        "span": .text("Sat 17 Oct"),        "photo": .text("verandah"),    ], prints: ["verandah", "verandah-wide"])    /// A name that takes the label's two lines, over ten days.    public static let longName = album([        "title": .text("The long drive down the coast and the night at the lighthouse"),        "count": .text("41"),        "span": .text("Fri 2 Oct \u{2013} Sun 11 Oct"),        "photo": .text("waterfall"),    ], prints: ["waterfall", "kelmoor-jetty", "verandah-wide", "portrait", "kelmoor-lake", "verandah"])}/// The prints an album's card lays out.enum AlbumPrints {    /// The prints in the order the card lays them: the cover first when the    /// album names one, then the rest as the album holds them, each once.    static func ordered(_ keys: [String], cover: String?) -> [String] {        var seen = Set<String>()        return ([cover].compactMap { $0 } + keys).filter { !$0.isEmpty && seen.insert($0).inserted }    }}