Cotton code
363 lines · 17.7 KB · SwiftRaw
// THE PAINTED TILE: a fan tile drawn on its tool's painted stock, whatever// its type — Cinemas' black felt letterboard in a thin brass frame (Round 19,// the owner's pick, 6 Nov 2026), Downloads' kraft parcel, Money's ruled index// card, Podcasts' bakelite preset (Round 25, 8 Nov 2026) — a pictogram at// its head, its name and count set beneath in the type the tile names.//// Every picture is DATA: the stock (`tile-blank`, painted wordless) and the// pictogram are the tool's kit pictures (`fan_tile`), handed in decoded;// nothing is painted here and no highlight is added over the stock. What the// tile draws itself is only what the painter left to the app (`notes.md`):// the live words, the cast shadow, and the centred tile's lift — about 12%// brighter than its neighbours, laid as the tile again over itself, added// (`plusLighter`), so a dark stays dark and the lights rise.//// THE TYPE (`PaintedTileType`) decides only the words: the letterboard's// cream upright condensed capitals (Barlow Condensed), or courier — the// shell's typed face (Courier Prime, the plain tile's) in the theme's ink, in// the words' own case, low on the stock where every courier blank leaves its// label (Downloads' address field, Podcasts' legend badge).//// THE LAYOUT IS THE PAINTER'S (`layout.json`), in fractions of the 474 x 696// stock: the pictogram's box (219 square, its centre 190.5 down — the same// on every Round 19 and Round 25 blank), and per type the name's baseline// and cap height, the line step and the count's place. Where a tile names// its words (`fan_tile.words`: Music's pale legend field, Keys' brass tag),// their ink and baselines are its own (`PaintedTileType.worded`), the name// and the count in one ink; a key it leaves out is the type's. The pictogram, the// name and the count stand CENTRED across the tile, as the composites// (`fan.png`) have them — the proof sheet's off-centre box is the sheet's,// not the tile's.//// THE WORDS ARE NEVER CUT (`FanTierWords`' rule): they wrap to two lines at// a word, then set smaller; a word longer than the tile sets smaller on its// line, never broken inside. They grow with the phone's text size as far as// the stock holds them, the count beneath never under two thirds of the// name's size. A red act keeps its red as its ink: the words and the// pictogram turn to the kit's red, the stock stays as painted.import CoreTextimport SwiftUI/// How a painted tile's words are set: the tile's `type` (`fan_tile`).public struct PaintedTileType: Equatable, Sendable {    /// The face the words are set in.    public let face: ThemeFace    /// The words are set in capitals.    public let capitals: Bool    /// The name's first baseline and its cap height against the stock's    /// height; the face's cap height against its size.    public let nameBaseline: CGFloat    public let nameCap: CGFloat    public let capRatio: CGFloat    /// A second line's baseline below the first, and the count's baseline    /// below the name's last, against the name's size.    public let lineStep: CGFloat    public let countGap: CGFloat    /// The count's size against the name's.    public let countRatio: CGFloat    /// The words' width against the stock's: clear of its frame or inside    /// its label.    public let wordsWidth: Double    /// The foot no count's baseline passes, against the stock's height.    public let wordsFoot: CGFloat    /// The words' ink, name and count alike; nil, the type's own — the    /// letterboard's cream, courier's the theme's ink.    public var ink: ThemeColor? = nil    /// Round 19's `layout.json` (the 474 x 696 board and the fan composite's    /// type) and `fan.png`: cream capitals pressed beneath the box.    public static let letterboard = PaintedTileType(        face: .letterboard, capitals: true,        // Baseline 403, cap 37 of 696; Barlow Condensed's cap: 700 of 1000.        nameBaseline: 403.0 / 696.0, nameCap: 37.0 / 696.0, capRatio: 0.7,        // A second line 62, the count 99 below the last, at 52 px type.        lineStep: 62.0 / 52.0, countGap: 99.0 / 52.0,        // The count's cap 32 of the name's 37.        countRatio: 32.0 / 37.0,        // Inside the felt (the safe rect is 418 of 474), clear of the frame.        wordsWidth: 0.8,        // The felt's foot (the safe rect's, 640 of 696, less a little).        wordsFoot: 0.9)    /// Round 25's `layout.json` for the courier blanks (Downloads, Podcasts:    /// baseline 500; Money's ruled card 430, set here at 500 with the rest,    /// still on its rules) and `fan.png`: typed words in the label.    public static let courier = PaintedTileType(        face: .courier, capitals: false,        // Baseline 500, cap 30 of 696; Courier Prime's cap: 580 of 1000.        nameBaseline: 500.0 / 696.0, nameCap: 30.0 / 696.0, capRatio: 0.58,        // A second line 58, the count 86 below the last, at 52 px type.        lineStep: 58.0 / 52.0, countGap: 86.0 / 52.0,        // The count at 44 px against the name's 52.        countRatio: 44.0 / 52.0,        // Inside the narrowest label, Podcasts' badge (395 of 474).        wordsWidth: 0.76,        // The badge's foot (615 of 696), less a little.        wordsFoot: 0.875)    /// The type with its words where the tile's painter set them    /// (`fan_tile.words`): `ink`, and the name's first baseline and the    /// count's under a name of one line and of two, in pixels on the    /// 696-high stock. At the painted size each stands where it is given;    /// grown or set smaller, the gaps scale with the name as the type's own    /// do. A value not given is the type's.    public func worded(ink: ThemeColor?, nameBaseline: Double?, countOne: Double?, countTwo: Double?) -> PaintedTileType {        let stock = Double(PaintedTileKnobs.stockHeight)        let painted = stock * Double(nameCap / capRatio)        let name = nameBaseline ?? stock * Double(self.nameBaseline)        let one = countOne ?? name + painted * Double(countGap)        let two = countTwo ?? one + painted * Double(lineStep)        return PaintedTileType(            face: face, capitals: capitals,            nameBaseline: CGFloat(name / stock), nameCap: nameCap, capRatio: capRatio,            lineStep: CGFloat((two - one) / painted), countGap: CGFloat((one - name) / painted),            countRatio: countRatio, wordsWidth: wordsWidth,            // A two-line name's count stands where the painter set it.            wordsFoot: max(wordsFoot, CGFloat(two / stock)),            ink: ink ?? self.ink)    }    /// The type's words are the letterboard's (its face), however inked or placed.    public var letterboard: Bool { face == .letterboard }    /// `words` as the type sets them.    public func cased(_ words: String) -> String { capitals ? words.uppercased() : words }    /// The type at `size`.    public func font(_ size: CGFloat) -> Font { face.font(size) }}/// A painted fan tile (see the file's header). `mark` is what stands in the/// pictogram's box: a pictogram (`PaintedPictogram`), or a list's own/// picture or its members' covers.public struct PaintedTile<Mark: View>: View {    let stock: Image?    let type: PaintedTileType    let label: String    /// What stands beneath the name: its count, or a day's detail.    let figure: String?    let ink: Color?    let centred: Bool    let width: Double    let height: Double    let growth: CGFloat    let mark: Mark    @Environment(\.theme) private var theme    /// `ink`: a red act's red; nil, the type's own — the tile's words' ink    /// where it names one, else the letterboard's cream, courier's the    /// theme's ink.    public init(stock: Image?, type: PaintedTileType = .letterboard, label: String, figure: String?, ink: Color? = nil,                centred: Bool = false, width: Double, height: Double, growth: CGFloat = 1, @ViewBuilder mark: () -> Mark) {        self.stock = stock        self.type = type        self.label = label        self.figure = figure        self.ink = ink        self.centred = centred        self.width = width        self.height = height        self.growth = growth        self.mark = mark()    }    private var drawnInk: Color { ink ?? type.ink?.color ?? (type.letterboard ? PaintedTileKnobs.cream : theme.print(.ink)) }    /// A stock not in hand yet: the letterboard's felt, courier's cream.    private var blank: Color { type.letterboard ? PaintedTileKnobs.felt : theme.sheet(.cream) }    public var body: some View {        let words = PaintedTileWords.set(label, figure: figure, type: type, width: width, height: height, growth: growth)        board(words)            .overlay {                if centred {                    board(words).blendMode(.plusLighter).opacity(PaintedTileKnobs.lift)                }            }            .compositingGroup()            .shadow(color: .black.opacity(PaintedTileKnobs.shadowOpacity), radius: PaintedTileKnobs.shadowRadius,                    x: PaintedTileKnobs.shadowX, y: PaintedTileKnobs.shadowY)            .frame(width: width, height: height)    }    private func board(_ words: PaintedTileWords.Set) -> some View {        ZStack(alignment: .top) {            Group {                if let stock {                    stock.resizable().interpolation(.high)                } else {                    RoundedRectangle(cornerRadius: width * PaintedTileKnobs.blankCorner).fill(blank)                }            }            .frame(width: width, height: height)            mark                .frame(width: width * PaintedTileKnobs.markSide, height: width * PaintedTileKnobs.markSide)                .position(x: width / 2, y: height * PaintedTileKnobs.markCentre)                .frame(width: width, height: height)        }        .frame(width: width, height: height)        // The words lie over the stock, each line's baseline where the        // painter set it: an overlay is laid inside the tile and never        // moves it, however tall the line's type.        .overlay(alignment: .top) {            ZStack(alignment: .top) {                ForEach(Array(words.lines.enumerated()), id: \.offset) { at, line in                    pressed(line, size: words.name, baseline: words.baselines[at])                }                if let figure {                    pressed(type.cased(figure), size: words.count, baseline: words.countBaseline)                }            }            .frame(width: width, height: height, alignment: .top)        }    }    /// One line of the words, centred, its baseline at `baseline` points from the top.    private func pressed(_ line: String, size: CGFloat, baseline: CGFloat) -> some View {        Text(line)            .font(type.font(size))            .foregroundStyle(drawnInk)            .lineLimit(1)            .fixedSize()            .alignmentGuide(.top) { $0[.firstTextBaseline] }            .offset(y: baseline)            // A fixed frame: its top is the baseline's, so however tall the            // type, nothing round it moves.            .frame(width: width, height: height, alignment: .top)    }}/// A kit pictogram in a painted tile's box: as painted, or turned to the/// act's red (`red`) where the act is red — the letterboard's cream plastic/// multiplied by it, a courier stock's dark print filled with it.public struct PaintedPictogram: View {    let image: Image    let red: Color?    let type: PaintedTileType    public init(_ image: Image, red: Color? = nil, type: PaintedTileType = .letterboard) {        self.image = image        self.red = red        self.type = type    }    public var body: some View {        let drawn = image.resizable().interpolation(.high).scaledToFit()        if let red, type.letterboard {            drawn.colorMultiply(red)        } else if let red {            Rectangle().fill(red).mask(drawn)        } else {            drawn        }    }}/// How a painted tile's words are set: the name's lines and size, each/// line's baseline, and the count's size and baseline, in points.public enum PaintedTileWords {    public struct Set: Equatable {        public var lines: [String]        public var name: CGFloat        public var baselines: [CGFloat]        public var count: CGFloat        public var countBaseline: CGFloat    }    public static let lines = 2    /// `label`, as `type` cases it, at the largest size from the painter's,    /// grown by `growth`, at which it stands in two lines inside the tile's    /// words width, broken only between words, and its count beneath stands    /// above the type's foot.    public static func set(_ label: String, figure: String?, type: PaintedTileType = .letterboard,                           width: Double, height: Double, growth: CGFloat = 1) -> Set {        let words = type.cased(label)        let room = CGFloat(width * type.wordsWidth)        let painted = CGFloat(height) * type.nameCap / type.capRatio        // The name's capitals never rise into the pictogram's box: grown,        // their top stands a little under the box's foot.        let boxFoot = CGFloat(height) * PaintedTileKnobs.markCentre + CGFloat(width * PaintedTileKnobs.markSide) / 2        let ceiling = (CGFloat(height) * (type.nameBaseline - PaintedTileKnobs.markGap) - boxFoot) / type.capRatio        let preferred = max(painted, min(painted * max(growth, 1), ceiling))        let floor = painted * PaintedTileKnobs.leastShare        let step = max(0.25, preferred * 0.02)        var size = preferred        var broken = laid(words, face: type.face, size: size, room: room)        while size > floor {            if let lines = broken, lines.count <= Self.lines, stands(lines.count, size: size, type: type, height: height, figure: figure) { break }            size -= step            broken = laid(words, face: type.face, size: size, room: room)        }        size = max(size, floor)        var lines = laid(words, face: type.face, size: size, room: room) ?? []        // Not even the floor holds the words whole in two lines: they go in        // two lines at their spaces, each set smaller until it stands.        if lines.isEmpty || lines.count > Self.lines {            let parts = words.split(separator: " ").map(String.init)            let half = max(1, (parts.count + 1) / 2)            lines = parts.count < 2 ? [words] : [parts.prefix(half).joined(separator: " "), parts.dropFirst(half).joined(separator: " ")]            size = lines.map { InkFit.oneLine($0, face: type.face, size: size, width: room) }.min() ?? size        }        let first = CGFloat(height) * type.nameBaseline        let lineStep = size * type.lineStep        let baselines = lines.indices.map { first + CGFloat($0) * lineStep }        let countSize = max(size * type.countRatio, size * PaintedTileKnobs.countLeast)        return Set(lines: lines, name: size, baselines: baselines, count: countSize,                   countBaseline: (baselines.last ?? first) + size * type.countGap)    }    /// The lines `words` wrap to at `size` inside `room`, broken only between    /// words, as CoreText lays them; nil when a word alone is wider.    private static func laid(_ words: String, face: ThemeFace, size: CGFloat, room: CGFloat) -> [String]? {        guard let laid = InkFit.laid(words, face: face, size: size, width: room) else { return nil }        let text = words as NSString        let lines = laid.map { line -> String in            let range = CTLineGetStringRange(line)            return text.substring(with: NSRange(location: range.location, length: range.length)).trimmingCharacters(in: .whitespaces)        }        // Every line's ink inside the room once centred.        return InkFit.fits(words, face: face, size: size, width: room, lines: lines.count) ? lines : nil    }    /// `lines` of the name at `size` and the count beneath stand above the type's foot.    private static func stands(_ lines: Int, size: CGFloat, type: PaintedTileType, height: Double, figure: String?) -> Bool {        let last = CGFloat(height) * type.nameBaseline + CGFloat(lines - 1) * size * type.lineStep        let foot = figure == nil ? last : last + size * type.countGap        return foot <= CGFloat(height) * type.wordsFoot    }}/// What every painted tile shares, measured off Round 19's and Round 25's/// `layout.json` (the 474 x 696 stock) and `fan.png`.public enum PaintedTileKnobs {    /// The stock's height in its painter's pixels, which a tile's words'    /// baselines are given on (`fan_tile.words`).    public static let stockHeight: CGFloat = 696    /// The pictogram's box: its side against the stock's width (219 of 474)    /// and its centre against the stock's height (190.5 of 696).    public static let markSide = 219.0 / 474.0    public static let markCentre = 190.5 / 696.0    /// The count's size never under two thirds of the name's.    public static let countLeast: CGFloat = 2.0 / 3.0    /// The least clear stock between the pictogram's box and the name's    /// capitals, against the stock's height.    public static let markGap: CGFloat = 0.02    /// The least the name sets at, against the painter's size.    public static let leastShare: CGFloat = 0.4    /// The cream of the letterboard's plastic letters, matched to the    /// pictograms' faces.    public static let cream = CinemasMaterial.letters.colour    /// A letterboard with no stock in hand yet: black felt, rounded as the frame is.    public static let felt = CinemasMaterial.letterFelt.colour    public static let blankCorner = 0.06    /// The centred tile's lift: 12% brighter (`fan.png`, centreBrightness 1.12).    public static let lift = 0.12    /// The cast shadow the app draws (`fan.png`: 65% black, 4 px blur, 1 x 5    /// px), held inside the fan's print padding.    public static let shadowOpacity = 0.65    public static let shadowRadius: CGFloat = 1.6    public static let shadowX: CGFloat = 0.6    public static let shadowY: CGFloat = 2.4}