Cotton code
436 lines · 20.0 KB · SwiftRaw
// The SEASON as its CREAM SEASON CARD, laid on the Picture House's// curtain-red velvet under its one warm house light (`FaceGround.velvet`;// Cinemas leftovers, 7 Nov 2026: a season's own page stood on plain cream// paper with a generic face while its series wore the one-sheet and its// episodes the lobby card). The card is the Round 14 season card// (`cinemas14-materials`, `season-card`, the tab a series' frame fans behind// its top edge), PAINTED (`Media.xcassets`, imported by// `scripts/import-cinemas-materials.py`) and laid whole in its 1565 x 663// box, scaled by the drawn width over 1565; the progress strip and the// ticket stub are the lobby card's (`CinemaParts.swift`). The face draws// live only what the card decides:////   the POSTER is the series' own picture, laid in a rounded window at the//   card's left; a series kept without one shows the velvet through a dark//   field there, the season's number typed large on it in the cream.//   the TAB says the season ("SEASON 2", the film's season line from its//   number) in the typewriter face across the card's top.//   the TITLE is the season's own in Libre Caslon, on one line taken down//   until it fits, then on two balanced lines, past the least size cut; a//   title that only repeats the tab's words gives way to the series' title.//   the FACTS under it: the year it aired and its episodes, as the core//   counted and said them ("2019 · 8 episodes").//   the WATCHED line and the PROGRESS STRIP at the card's foot once any//   episode is watched: "3 of 8 watched", the strip filled as far.//   the STUB at the card's upper right corner once every episode is//   watched: stamped WATCHED, no day (a season is watched over many).//// Tagged: the poster (`picture`), the title (`title`), the facts (`body`),// the strip (`state`), the stub (`done`). Stepped by a tap, it cuts. Print// on an object scales with the object, never with Dynamic Type.//// What the card holds (the slots the Cinemas kit fills on `film_season`):// `photo` (the series' poster), `title`, `number` (the season's number, the// figure alone), `year`, `subtitle` (its episodes, said in their count words),// `limit` (its episodes, the number), `used` (its episodes watched, the// number), `source` (the series' title). Every word is the core's or a// line: the face only sets them.import CottonPaperimport CottonValuesimport CoreTextimport SwiftUIenum SeasonCardFaceKnobs {    // THE CARD'S BOX every placement is in, in the painting's pixels    // (`layout.json`'s `season-card`, 1565 x 663): the face draws the card    // at most `widest` points wide, and every place below is scaled by the    // drawn width over the box's.    static let box = CGSize(width: 1565, height: 663)    static let widest: CGFloat = 340    /// The most of the card's room the card and what it carries take.    static let fillWidth: CGFloat = 0.96    static let fillHeight: CGFloat = 0.94    /// The card's shadow on the velvet: its reach and how far it falls, in    /// the box's pixels, and how dark it lies (the painting carries none).    static let shadowRadius: CGFloat = 28    static let shadowDrop: CGFloat = 18    static let shadowStrength: Double = 0.55    /// What the shadow reaches past the card on every side.    static let shadowReach: CGFloat = 64    // THE POSTER: its window on the card's left, the series' one-sheet    // proportion (two across, three down), and its rounding; a poster laid a    // hair past it. Where none is kept: how strongly the dark is laid over    // the velvet and the season's number typed on it, its size.    static let poster = CGRect(x: 78, y: 104, width: 324, height: 486)    static let posterRadius: CGFloat = 24    static let posterBleed: CGFloat = 4    static let screenStrength: Double = 0.5    static let numeralSize: CGFloat = 260    // THE WORDS, right of the poster: the tab across the card's top, the    // title under it, the facts, the watched line and the strip at the foot.    static let tab = CGRect(x: 450, y: 70, width: 1040, height: 80)    static let tabSize: CGFloat = 60    static let title = CGRect(x: 450, y: 170, width: 1040, height: 200)    static let titleSize: CGFloat = 92    static let titleLeast: CGFloat = 60    static let titleShrink: CGFloat = 0.94    static let titleStep: CGFloat = 1.08    static let titleLines = 2    static let facts = CGRect(x: 450, y: 384, width: 1040, height: 60)    static let factsSize: CGFloat = 42    static let watched = CGRect(x: 450, y: 470, width: 1040, height: 56)    static let watchedSize: CGFloat = 38    static let progress = CGRect(x: 450, y: 548, width: 1000, height: 16)    // THE STUB, over the card's upper right corner (the one-sheet's tuck,    // at the season card's scale), in the card's box.    static let stub = CGRect(x: 1180, y: -72, width: 380, height: 241.1)}/// How the season's card is set.public enum SeasonCardSetting {    /// The tab's words, the season said from its number ("SEASON 2"); nil,    /// no number kept.    public static func tab(_ card: DeckCard) -> String? {        guard let number = card.text("number") else { return nil }        return ShellLines.line("cards", "film.season", ["n": number]).uppercased()    }    /// The title set in Caslon: the season's own, unless it only repeats    /// the tab's words, then the series'; nil, neither kept.    public static func title(_ card: DeckCard) -> String? {        let own = card.text("title")?.split(whereSeparator: \.isWhitespace).joined(separator: " ")        if let own, own.uppercased() != tab(card) { return own }        return card.text("source")?.split(whereSeparator: \.isWhitespace).joined(separator: " ") ?? own    }    /// The title set: on one line at its own size, taken down a step at a    /// time to the least; then on two balanced lines from its size down    /// again; past the least size cut with an ellipsis. In the card's pixels.    public static func titleLines(_ card: DeckCard) -> (lines: [String], size: CGFloat) {        let knobs = SeasonCardFaceKnobs.self        guard let title = title(card) else { return ([], knobs.titleSize) }        let room = knobs.title        func fits(_ size: CGFloat, lines count: Int) -> Bool {            let lines = HighlightPassageSetting.lines(title, font: FilmPosterSetting.caslon(size), width: room.width, limit: .max)            return lines.count <= count && CGFloat(lines.count) * size * knobs.titleStep <= room.height        }        for count in 1...knobs.titleLines {            var size = knobs.titleSize            while !fits(size, lines: count), size * knobs.titleShrink >= knobs.titleLeast { size *= knobs.titleShrink }            if fits(size, lines: count) {                return (BookHardbackSetting.balanced(title, font: FilmPosterSetting.caslon(size), width: room.width, limit: count), size)            }        }        var size = knobs.titleSize        while size * knobs.titleShrink >= knobs.titleLeast { size *= knobs.titleShrink }        return (HighlightPassageSetting.lines(title, font: FilmPosterSetting.caslon(size), width: room.width, limit: knobs.titleLines), size)    }    /// The facts under the title: the year it aired and its episodes as the    /// core said them, joined as the cards join words; nil, neither kept.    public static func facts(_ card: DeckCard) -> String? {        let facts = [card.text("year"), card.text("subtitle")].compactMap { $0 }        return facts.isEmpty ? nil : facts.joined(separator: ShellLines.line("cards", "join"))    }    /// Its episodes, and those watched, as the core counted them.    static func episodes(_ card: DeckCard) -> (watched: Int, of: Int)? {        guard let of = card.number("limit"), of > 0 else { return nil }        let watched = min(of, max(0, card.number("used") ?? 0))        return (Int(watched.rounded()), Int(of.rounded()))    }    /// The watched line ("3 of 8 watched"), once any episode is watched.    public static func watched(_ card: DeckCard) -> String? {        guard let counted = episodes(card), counted.watched > 0 else { return nil }        return ShellLines.line("cards", "film.season_watched", ["n": "\(counted.watched)", "of": "\(counted.of)"])    }    /// How far the strip is filled, nought to one: the episodes watched of    /// its episodes; nil, none watched.    public static func share(_ card: DeckCard) -> Double? {        guard let counted = episodes(card), counted.watched > 0 else { return nil }        return Double(counted.watched) / Double(counted.of)    }    /// The stub's stamp once every episode is watched, with no day; nil    /// otherwise.    public static func stub(_ card: DeckCard) -> String? {        guard let counted = episodes(card), counted.watched == counted.of else { return nil }        return ShellLines.line("cards", "film.stub").uppercased()    }    /// The season's number, typed on a posterless window; nil, none kept.    public static func numeral(_ card: DeckCard) -> String? {        card.text("number")    }    /// The typewriter face as CoreText measures it.    static func type(_ size: CGFloat) -> CTFont {        EpisodeLobbySetting.type(size)    }    /// What the face reaches, in the card's box: the card, its shadow, its    /// stub (always kept room for, so a season's card is the same size    /// watched or not).    static var reach: CGRect {        let knobs = SeasonCardFaceKnobs.self        let card = CGRect(origin: .zero, size: knobs.box)        return card.insetBy(dx: -knobs.shadowReach, dy: -knobs.shadowReach).union(knobs.stub)    }}public struct SeasonCardFace<Poster: View>: View {    let card: DeckCard    /// The series' poster laid in the window; nil shows the dark field.    var poster: Poster?    public init(card: DeckCard, poster: Poster? = nil) {        self.card = card        self.poster = poster    }    public var body: some View {        let knobs = SeasonCardFaceKnobs.self        let reach = SeasonCardSetting.reach        GeometryReader { room in            let across = 2 * max(knobs.box.width / 2 - reach.minX, reach.maxX - knobs.box.width / 2)            let s = min(knobs.widest / knobs.box.width, room.size.width * knobs.fillWidth / across,                        room.size.height * knobs.fillHeight / reach.height)            SeasonCard(card: card, poster: poster, s: s)                .frame(width: knobs.box.width * s, height: knobs.box.height * s, alignment: .topLeading)                .offset(x: (room.size.width - knobs.box.width * s) / 2,                        y: (room.size.height - reach.height * s) / 2 - reach.minY * s)                .frame(width: room.size.width, height: room.size.height, alignment: .topLeading)        }        .accessibilityElement(children: .contain)        .accessibilityIdentifier("season-card")    }}/// The card in its box, scaled by `s`: the card on its shadow, the poster/// or the dark field, the tab, the title, the facts, the watched line and/// the strip, the stub.private struct SeasonCard<Poster: View>: View {    let card: DeckCard    let poster: Poster?    let s: CGFloat    var body: some View {        let knobs = SeasonCardFaceKnobs.self        ZStack(alignment: .topLeading) {            CinemaPainted(name: "season-card")                .cinemaLaid(CGRect(origin: .zero, size: knobs.box), s)                .shadow(color: CinemasMaterial.screen.colour.opacity(knobs.shadowStrength),                        radius: knobs.shadowRadius * s, y: knobs.shadowDrop * s)            Window(card: card, poster: poster, s: s)            if let tab = SeasonCardSetting.tab(card) {                typed(tab, size: knobs.tabSize, in: knobs.tab)                    .accessibilityHidden(true)                    .offset(x: knobs.tab.minX * s, y: knobs.tab.minY * s)            }            Title(card: card, s: s)            if let facts = SeasonCardSetting.facts(card) {                typed(facts, size: knobs.factsSize, in: knobs.facts)                    .contentShape(Rectangle())                    .accessibilityElement()                    .accessibilityLabel(facts)                    .accessibilityIdentifier("season-card-facts")                    .facePart(.body)                    .offset(x: knobs.facts.minX * s, y: knobs.facts.minY * s)            }            if let watched = SeasonCardSetting.watched(card) {                typed(watched, size: knobs.watchedSize, in: knobs.watched)                    .accessibilityHidden(true)                    .offset(x: knobs.watched.minX * s, y: knobs.watched.minY * s)            }            if let share = SeasonCardSetting.share(card) {                CinemaStrip(rect: knobs.progress, share: share, said: SeasonCardSetting.watched(card) ?? "",                            identifier: "season-card-progress", s: s)            }            if let stamp = SeasonCardSetting.stub(card) {                CinemaStub(rect: knobs.stub, stamp: stamp, day: nil, identifier: "season-card-stub", s: s)            }        }        .frame(width: knobs.box.width * s, height: knobs.box.height * s, alignment: .topLeading)    }    /// `words` in the typewriter at `size`, set from the leading edge of a    /// room `rect`'s size on one line, taken down to fit it; placed by the    /// caller, after it is tagged.    private func typed(_ words: String, size: CGFloat, in rect: CGRect) -> some View {        Text(words)            .font(CottonFonts.type(size * s))            .foregroundStyle(CinemasMaterial.ink.colour)            .lineLimit(1)            .minimumScaleFactor(0.5)            .frame(width: rect.width * s, height: rect.height * s, alignment: .leading)    }}/// The poster's window on the card: the series' poster, or the velvet/// through a dark field with the season's number on it; tagged as the/// card's picture.private struct Window<Poster: View>: View {    let card: DeckCard    let poster: Poster?    let s: CGFloat    var body: some View {        let knobs = SeasonCardFaceKnobs.self        let shape = RoundedRectangle(cornerRadius: knobs.posterRadius * s, style: .continuous)        let bled = knobs.poster.insetBy(dx: -knobs.posterBleed, dy: -knobs.posterBleed)        ZStack {            if let poster {                poster                    .frame(width: bled.width * s, height: bled.height * s)                    .clipped()                    .clipShape(shape)            } else {                CinemasMaterial.velvet.colour                    .overlay { CottonMedia.image(FaceGround.velvet.tile ?? "", scale: 3)?.resizable(resizingMode: .tile) }                    .clipShape(shape)                shape.fill(CinemasMaterial.screen.colour.opacity(knobs.screenStrength))                if let numeral = SeasonCardSetting.numeral(card) {                    Text(numeral)                        .font(CottonFonts.type(knobs.numeralSize * s))                        .foregroundStyle(CinemasMaterial.cream.colour)                        .lineLimit(1)                        .minimumScaleFactor(0.3)                }            }        }        .frame(width: bled.width * s, height: bled.height * s)        .contentShape(shape)        .accessibilityElement(children: .ignore)        .accessibilityLabel(SeasonCardSetting.tab(card) ?? "")        .accessibilityIdentifier("season-card-poster")        .facePart(.picture)        .offset(x: bled.minX * s, y: bled.minY * s)    }}/// The title in Caslon, set from the words' leading edge.private struct Title: View {    let card: DeckCard    let s: CGFloat    var body: some View {        let knobs = SeasonCardFaceKnobs.self        let (lines, size) = SeasonCardSetting.titleLines(card)        VStack(alignment: .leading, spacing: 0) {            ForEach(Array(lines.enumerated()), id: \.offset) { _, line in                Text(line)                    .font(CottonFonts.caslon(size * s))                    .lineLimit(1)                    .fixedSize()                    .frame(height: size * knobs.titleStep * s)            }        }        .foregroundStyle(CinemasMaterial.ink.colour)        .frame(width: knobs.title.width * s, height: knobs.title.height * s, alignment: .leading)        .contentShape(Rectangle())        .accessibilityElement()        .accessibilityLabel([SeasonCardSetting.tab(card), lines.isEmpty ? nil : lines.joined(separator: " ")]            .compactMap { $0 }.joined(separator: ShellLines.line("cards", "join")))        .accessibilityAddTraits(.isHeader)        .accessibilityIdentifier("deck-title")        .facePart(.title)        .offset(x: knobs.title.minX * s, y: knobs.title.minY * s)    }}/// The season's card, by its id beside the series' one-sheet and the/// episode's lobby card, and the seasons it is reviewed in: invented titles,/// an invented poster. An episode count is said as the core says it (the/// column's count words).public enum SeasonCardFaceCatalogue {    public static let stock: FaceStock = .own    public static let ground: FaceGround = .velvet    public static let face = Face(id: "season.card", specimens: [        Specimen("poster", poster, picture: "poster-ferry"), Specimen("no-poster", noPoster),        Specimen("part-watched", partWatched, picture: "poster-ferry"), Specimen("watched", watched, picture: "poster-ferry"),        Specimen("longest-title", longestTitle), Specimen("largest", largest, picture: "poster-ferry"),    ], steps: .cut) { card, picture, _ in        SeasonCardFace(card: card, poster: picture)    }    static func season(_ fields: [String: RecordValue]) -> DeckCard {        DeckCard(face: "season.card", corner: nil, pressing: false, pace: nil, plate: nil, fields: fields)    }    /// None watched yet, the series' poster kept; its title its own.    public static let poster = season([        "title": .text("The Winter Crossings"),        "number": .text("2"),        "year": .text("2021"),        "subtitle": .text("8 episodes"),        "limit": .number(8),        "used": .number(0),        "photo": .text("poster-ferry"),        "source": .text("Night Ferry"),    ])    /// No poster kept: the dark field and the season's number on it; its    /// title only repeats the tab, so the series' title is set.    public static let noPoster = season([        "title": .text("Season 1"),        "number": .text("1"),        "year": .text("2019"),        "subtitle": .text("6 episodes"),        "limit": .number(6),        "used": .number(0),        "source": .text("The Fog Bell"),    ])    /// Three of eight watched: the watched line and the strip.    public static let partWatched = season([        "title": .text("The Winter Crossings"),        "number": .text("2"),        "year": .text("2021"),        "subtitle": .text("8 episodes"),        "limit": .number(8),        "used": .number(3),        "photo": .text("poster-ferry"),        "source": .text("Night Ferry"),    ])    /// Every episode watched: the strip full and the stub at the corner.    public static let watched = season([        "title": .text("The Winter Crossings"),        "number": .text("2"),        "year": .text("2021"),        "subtitle": .text("8 episodes"),        "limit": .number(8),        "used": .number(8),        "photo": .text("poster-ferry"),        "source": .text("Night Ferry"),    ])    /// The longest title: on two lines, cut; no poster, no year.    public static let longestTitle = season([        "title": .text("The Lamp-Keeper's Daughter Rows Out Past the Breakwater on the Longest Night of the Year"),        "number": .text("12"),        "subtitle": .text("104 episodes"),        "limit": .number(104),        "used": .number(40),        "source": .text("The Lantern Coast"),    ])    /// The fullest card, drawn at every text size Loom has, the largest    /// among them: watched through, a long title, a long count.    public static let largest = season([        "title": .text("The Lantern Coast and the Harbour Towns"),        "number": .text("10"),        "year": .text("2014"),        "subtitle": .text("68 episodes"),        "limit": .number(68),        "used": .number(68),        "photo": .text("poster-ferry"),        "source": .text("The Lantern Coast"),    ])}