Cotton code
207 lines · 10.3 KB · SwiftRaw
// THE SURFACE OF A SHEET IS THE PHOTOGRAPH. The outline is drawn; the material// is not.//// The kit shipped (#620) with the paper ASSEMBLED — a measured dye, a grain tile// multiplied over it, two seeded wash blooms, a pale rim and a thickness stroke.// It passed its own acceptance comparison at swatch size and then failed the// only test that matters: the owner looked at the built screens beside the// screenshots and asked whether the cards were being coded or// photographed. They were being coded. This file is the answer: they are// photographed again, the way Home's bottle caps always were// (`Paper/ProjectCaps.swift`) and the way the todo card was before the// kit (`TodoCard` at e0b47cf5) — a picture, with the lettering set live and// multiplied onto it.//// WHAT HAD TO CHANGE for a photograph to be `Paper`, which is any size and any// aspect:////   1. THE SHADOW CAME OUT OF THE PICTURE. `card-*` is cut at alpha > 24, which//      keeps the soft shadow under the card — and a baked shadow grows with the//      picture, so one sheet drawn at four sizes would wear four shadows. The//      `paper-*` set (`scripts/gen-cards/export_paper.py`) is the same six//      masters cut at alpha > 200: the paper and nothing else. The lift is//      SwiftUI's, one pair of shadows at every size (`PaperLift`).////   2. THE PICTURE IS NINE-SLICED, so its worn rim and its four rounded corners//      keep their own thickness whatever the frame is. A page-tall sheet drawn//      as a plain `.resizable()` wears a rim three times too fat along its top//      and bottom, which is the single thing that gives a stretched photograph//      away.////   3. THE CAP INSETS SCALE WITH THE SHEET, below the master's own size. This is//      what makes one code path cover a 28pt chip and a 700pt sheet, and it is//      worth following: the caps are `naturalCap * min(1, shorter / masterShorter)`,//      so a sheet AT OR ABOVE the master's size gets the rim at its true//      photographic thickness, and a sheet below it gets the rim scaled down by//      exactly the ratio the whole picture is being scaled down by. On a card at//      the master's own 1.59 aspect that makes every one of the nine slices land//      at the SAME scale — the nine-slice degrades to a plain uniform//      `.resizable()`, which is to say the board's card is the photograph whole,//      exactly as it was at e0b47cf5. No size class, no branch; the arithmetic//      is the size class.////   4. THE MIDDLE IS STRETCHED, NOT TILED, and the fibre the stretch loses is//      put back by the seamless `paper-grain` tile. Tiling was the first design//      and it is wrong for this picture: the master's centre carries creases and//      a lighting gradient, so tiling it lays a visible seam every 195 points//      down a long sheet, and no amount of mirroring hides a crease that meets//      its own reflection. Stretching a centre that has had its detail removed//      is the standard way out — stretch the low frequency, tile the high — and//      the kit already owns the high-frequency tile. `PaperGrain.lostToStretch`//      is the whole rule: it is zero while the picture is being drawn at or//      under its own pixel density (a board card, an open card), and it rises as//      the sheet is pulled past it.//// EVERYTHING ELSE THE ASSEMBLED PAPER DREW IS GONE: the two wash blooms, the// radial vignette, the pale pulp rim and the corner-to-corner thickness stroke.// Each was a guess at something the photograph actually has, and a guess laid// over the real thing reads as dirt. `PaperShape` stays, as the CLIP and nothing// else — a cut card, a torn slip and a perforated stub are three outlines of one// material.import CoreGraphicsimport SwiftUI/// The photographed sheet: which picture, how big it really is, and how it is/// sliced into a frame.////// Its own type, with no view in it, so the arithmetic that decides a rim's/// thickness is somewhere a test can call without drawing anything. Built/// from the theme's scale (`paper_photo_*`), which carries the master's/// measured geometry.public struct PaperPhoto {    public let scale: ThemeScale    public init(scale: ThemeScale) {        self.scale = scale    }    /// The master's pixels. Every `paper-*` is cut with cream's box and written    /// at this size, so the seven share one geometry — `export_paper.py`, and    /// `ProjectCapsTests` holds them to it.    public var masterPixels: CGSize {        CGSize(width: PaperPhotoKnobs.paperPhotoMasterWidth, height: PaperPhotoKnobs.paperPhotoMasterHeight)    }    /// The scale the imagesets declare. 720 px at 2x is 360 points: a sheet    /// drawn 360 points wide is the photograph at its own size.    public var masterScale: CGFloat { PaperPhotoKnobs.paperPhotoMasterScale }    /// How deep the worn rim runs into the master, in ITS pixels.    ///    /// Measured, not chosen: the luminance across every one of the four sides    /// falls from the sheet's centre tone over the first dozen pixels and is    /// back to it by about thirty. Thirty-two is that, rounded up so the corner    /// fray is inside the cap rather than straddling it.    public var rimPixels: CGFloat { PaperPhotoKnobs.paperPhotoRimPixels }    /// The master's size in points — 360 x 226.5.    public var masterPoints: CGSize {        CGSize(width: masterPixels.width / masterScale, height: masterPixels.height / masterScale)    }    /// The rim's true thickness in points, on a sheet drawn at the master's own    /// size or bigger.    public var naturalCap: CGFloat { rimPixels / masterScale }    /// The cap inset a sheet of `size` draws its rim at.    ///    /// At or above the master's shorter side the rim is the photograph's own    /// thickness. Below it the rim is scaled by the same ratio the sheet is —    /// which is what keeps a 28pt chip from wearing a 16pt frame, and what makes    /// a card at the master's aspect a uniform scale rather than a nine-slice.    public func cap(for size: CGSize) -> CGFloat {        let shorter = min(size.width, size.height)        guard shorter > 0 else { return naturalCap }        return naturalCap * min(1, shorter / min(masterPoints.width, masterPoints.height))    }    /// The scale the picture must be handed to SwiftUI at for its cap insets to    /// land at `cap(for:)` points. `masterScale` when the sheet is big enough to    /// wear the rim at its true size.    public func drawingScale(for size: CGSize) -> CGFloat {        let wanted = cap(for: size)        guard wanted > PaperPhotoKnobs.paperPhotoMinCap else { return masterScale }        return rimPixels / wanted    }    /// The four ways one sheet of paper can be laid down, as a seeded pair of    /// scale factors.    ///    /// WITHOUT THIS, A GRID IS TWELVE COPIES OF ONE CARD. There is one    /// photograph per dye, and the master has a crease running out of its    /// top-left corner: laid twelve times on a notes grid, that crease is    /// twelve creases in the same place and the eye reads a repeat before it    /// reads paper. Turning the sheet over and around costs nothing, changes no    /// pixel of the material, and gives four arrangements — which with six dyes    /// and a seeded outline is enough that no two sheets on a screen are the    /// same sheet.    ///    /// It is a FLIP and never a rotation: the master is landscape, and a    /// quarter-turn would put its long fibre across the sheet.    public static func flip(seed: String) -> (x: CGFloat, y: CGFloat) {        (            x: PaperNoise.unit(seed: seed, salt: PaperSalt.flip) < 0.5 ? 1 : -1,            y: PaperNoise.unit(seed: seed, salt: PaperSalt.flip ^ PaperSalt.flipVertical) < 0.5 ? 1 : -1        )    }    /// How many of the master's pixels land on a point, in each axis, once the    /// sheet is sliced into `size`. Under 1 means the picture is being    /// magnified and its fibre is going soft.    func density(for size: CGSize) -> CGSize {        let rim = cap(for: size)        let inset = rim + rim        let middle = CGSize(            width: max(1, size.width - inset),            height: max(1, size.height - inset)        )        let rims = rimPixels + rimPixels        return CGSize(            width: (masterPixels.width - rims) / middle.width,            height: (masterPixels.height - rims) / middle.height        )    }}extension PaperDye {    /// The photograph this dye's paper is. Six were photographed; `newsprint`    /// was carried to its measured colour off the cream master by the same    /// exporter, because a dye with no surface would be the one sheet in the app    /// that is still a drawing.    public var photograph: String { "paper-\(rawValue)" }}extension PaperGrain {    /// How much of the master's own fibre a sheet of `size` has lost to being    /// stretched — 0 while the picture is drawn at or under its own pixel    /// density, rising as it is pulled past it.    ///    /// Two pixels to a point (`paper_grain_sharp_density`) is the knee: above    /// it the picture is being downsampled and carries every fibre it ever    /// had, at one pixel to a point it is at true size and beginning to    /// soften, and below that it is being magnified.    ///    /// The result is scaled by the strength the fibre tile is multiplied on    /// at over a photograph (`paper_grain_over_photograph`) — never quite    /// full: the stretched picture still carries some of its own.    static func lostToStretch(at size: CGSize, scale: ThemeScale) -> Double {        let density = PaperPhoto(scale: scale).density(for: size)        let softest = min(density.width, density.height)        let lost = 1 - min(1, softest / PaperPhotoKnobs.paperGrainSharpDensity)        return Double(max(0, min(1, lost))) * Double(PaperPhotoKnobs.paperGrainOverPhotograph)    }}/// The sizes, counts and timings PaperPhoto draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum PaperPhotoKnobs {    static let paperGrainOverPhotograph: CGFloat = 0.85    static let paperGrainSharpDensity: CGFloat = 2    static let paperPhotoMasterHeight: CGFloat = 453    static let paperPhotoMasterScale: CGFloat = 2    static let paperPhotoMasterWidth: CGFloat = 720    static let paperPhotoMinCap: CGFloat = 0.01    static let paperPhotoRimPixels: CGFloat = 32}