Cotton code
299 lines · 14.7 KB · SwiftRaw
// The three bundled faces — and the amendment they are.//// `Typography.swift`'s rule is that Cotton wears the SYSTEM face: ten roles,// zero loaded fonts. That rule still governs every sentence the app SETS —// rows, titles, captions, the editor. What these faces are for is the// thing that rule never covered: LETTERING ON AN OBJECT. Home's project caps// (`Paper/ProjectCaps.swift`) are photographs of enamelled bottle caps// with the project's name printed on them, and print on a cap has a face of// its own the way a label on a jar does. The owner's ruling of 2026-09-13,// with the reference picture: "the text has to look like this" — a condensed// serif italic on the caps, a heavier display serif over the grid. The todo// slips are the second object: photographs of paper// index cards with the todo TYPED on them, and typing has a face of its own.//// All are SIL Open Font License (the licences sit beside the files):////   * `InstrumentSerif-Italic` — the cap lettering. One style, on purpose://     the reference mixed three faces because a picture generator does; a//     collection of caps from one bottler is set in one.//   * `FrauncesTitle-SemiBold` — a PAGE's title: "Cotton", "Todos", "Notes".//     A static instance (opsz 48, wght 580, no wonk) cut from the variable//     font, because `Font.custom` cannot set a variation axis.//   * `FrauncesText-Regular` — the same family at its TEXT optical size//     (opsz 18, wght 450): section headings, an object's name on paper, a//     person's name. The redesign's pictures set everything below the page//     title in a book-weight serif; one high-contrast display cut at every//     size was the largest gap between the built screens and the pictures.//     Both are cut by `scripts/gen-fonts/cut_fraunces.py`.//   * `CourierPrime-Regular` and `CourierPrime-Italic` — the typewriter on//     the cards: the label in the upright, the day and the counts in the//     italic. Two styles because a typed card has both; no bold, because a//     typewriter has none.//   * `Caveat-Regular` — the handwriting the paper kit added (`Paper/`). It is//     the voice of ANNOTATION and nothing else: the date in a sheet's corner,//     "6 days late", "3 of 12", "totals agree", "Saved". One style, because a//     person writing in a margin does not change pens.//   * `LibreCaslonText-Regular` — the book-weight Caslon the invitation's//     pictures set its title in, now a file's, a passport's and a blank//     jacket's title (`PersonFileFace`, `PassportFace`, `BookHardbackFace`). A static instance (wght 400) cut from the variable//     font by `scripts/gen-fonts/cut_libre_caslon.py`.//   * `MsMadi-Regular` — a signature's hand (a rolodex's foot, a slip's time,//     a cassette's label). One style; it is a pen, not a family.//   * `BarlowCondensed-ExtraBoldItalic` — a record sleeve's printed title and//     artist where the album keeps no cover (`AlbumSleeveFace`): the heavy,//     condensed, leaning sans the dark room's covers are set in. One style,//     the static cut Google Fonts ships.//   * `BarlowCondensed-SemiBold` — the letterboard's cream capitals: the//     upright of the same family, set on a tool's fan tiles where the tool's//     tile says `letterboard` (Cotton Cinemas, 6 Nov 2026). Its Latin alone,//     cut by `scripts/gen-fonts/cut_barlow_condensed.py`.//// THEY ARE FIXED-SIZE. A cap is a photograph and its lettering scales with// the cap, not with Dynamic Type — a name that grew with the text size would// walk off the enamel, and a label that grew would walk off the card. So no// face here goes through `cottonType`, and nothing else in the app reaches// them: the caps and the cards are the two callers, and a third is a design// decision, not a call site's.//// REGISTERED FROM THE PACKAGE BUNDLE, not from `UIAppFonts`. The faces are// resources of `CottonUI` (`Package.swift`: `.copy("Resources/Fonts")`), and// `UIAppFonts` reads the APP bundle only — so they are registered once, with// CoreText, the first time a caller asks. The Mac has the same bundle, FOUND// BY PATH (`Resources.swift`), so the faces register there too; the system// faces — serif for the caps and the word, monospaced for the typewriter —// are the fallback for a Mac build that carries no bundle at all, not the// Mac's normal state.import CoreTextimport SwiftUIpublic enum CottonFonts {    /// The cap lettering's PostScript name.    public static let capFace = "InstrumentSerif-Italic"    /// The page title's PostScript name.    public static let displayFace = "FrauncesTitle-SemiBold"    /// The typewriter's PostScript name — the label on a card.    public static let typeFace = "CourierPrime-Regular"    /// The typewriter's italic — the day on a card, the count on a section.    public static let typeItalicFace = "CourierPrime-Italic"    /// The handwriting's PostScript name — annotation on paper.    public static let handFace = "Caveat-Regular"    /// The serif at its text size — headings and names on paper.    public static let textFace = "FrauncesText-Regular"    /// The book-weight Caslon a name is set in (`PersonFileFace`, `BookHardbackFace`).    public static let caslonFace = "LibreCaslonText-Regular"    /// The signature hand (`RolodexFace`, `ThreadSlipsFace`, the cassette's label: `BareCassette`).    public static let signatureFace = "MsMadi-Regular"    /// The record sleeve's condensed heavy sans (`AlbumSleeveFace`).    public static let sleeveFace = "BarlowCondensed-ExtraBoldItalic"    /// The letterboard's upright condensed capitals (`PaintedTile`).    public static let letterboardFace = "BarlowCondensed-SemiBold"    /// Every bundled face, in the order they arrived; what the registration    /// loop walks and what the resource guard checks.    public static let faces = [capFace, displayFace, typeFace, typeItalicFace, handFace, textFace, caslonFace, signatureFace, sleeveFace, letterboardFace]    /// A BUSINESS FLAVOUR'S FACES — worn only where a flavour's look names    /// them (`FlavourLook`), never by Cotton's own theme. Montserrat (OFL,    /// the licence beside the files) is Sharmila's Yoga Zone's: the regular    /// for what is read, the semibold for what heads it. Static cuts, since    /// `Font.custom` cannot set a variation axis. Not in `faces`: Cotton's own    /// theme never names these.    public static let brandRegularFace = "Montserrat-Regular"    public static let brandHeadingFace = "Montserrat-SemiBold"    public static let brandFaces = [brandRegularFace, brandHeadingFace]    /// A bundled font file by name and extension, for a page that loads its    /// own type (the book reader's EPUB sets Libre Caslon's `ttf` itself).    /// Nil in a build with no resources.    public static func file(_ name: String, extension ext: String) -> URL? {        CottonResources.bundle?.url(forResource: name, withExtension: ext, subdirectory: "Fonts")    }    /// A flavour's face by its PostScript name, at a size the caller decides;    /// the system face at `fallback` weight where the file is not registered.    public static func brand(_ face: String, _ size: CGFloat, fallback: Font.Weight) -> Font {        register()        #if canImport(UIKit)        return .custom(face, fixedSize: drawable(size))        #else        return brandFacesLoaded ? .custom(face, fixedSize: drawable(size)) : .system(size: drawable(size), weight: fallback)        #endif    }    /// The lettering on a cap, at a size the CAP decides.    public static func cap(_ size: CGFloat) -> Font {        register()        #if canImport(UIKit)        return .custom(capFace, fixedSize: drawable(size))        #else        return facesLoaded ? .custom(capFace, fixedSize: drawable(size)) : .system(size: size, design: .serif).italic()        #endif    }    /// A page's title, at a size the page decides. Nothing below the title    /// wears it — that is `serif(_:)`.    public static func display(_ size: CGFloat) -> Font {        register()        #if canImport(UIKit)        return .custom(displayFace, fixedSize: drawable(size))        #else        return facesLoaded ? .custom(displayFace, fixedSize: drawable(size)) : .system(size: size, weight: .semibold, design: .serif)        #endif    }    /// The serif at its text optical size — a section heading, an object's    /// name, at a size the object decides.    public static func serif(_ size: CGFloat) -> Font {        register()        #if canImport(UIKit)        return .custom(textFace, fixedSize: drawable(size))        #else        return facesLoaded ? .custom(textFace, fixedSize: drawable(size)) : .system(size: size, design: .serif)        #endif    }    /// An invitation's title, at a size the card decides.    public static func caslon(_ size: CGFloat) -> Font {        register()        #if canImport(UIKit)        return .custom(caslonFace, fixedSize: drawable(size))        #else        return facesLoaded ? .custom(caslonFace, fixedSize: drawable(size)) : .system(size: size, design: .serif)        #endif    }    /// A place written as a signature, at a size the card decides.    public static func signature(_ size: CGFloat) -> Font {        register()        #if canImport(UIKit)        return .custom(signatureFace, fixedSize: drawable(size))        #else        return facesLoaded ? .custom(signatureFace, fixedSize: drawable(size)) : .system(size: size, design: .serif).italic()        #endif    }    /// A record sleeve's printed title, at a size the SLEEVE decides.    public static func sleeve(_ size: CGFloat) -> Font {        register()        #if canImport(UIKit)        return .custom(sleeveFace, fixedSize: drawable(size))        #else        return facesLoaded ? .custom(sleeveFace, fixedSize: drawable(size)) : .system(size: size, weight: .heavy).width(.condensed).italic()        #endif    }    /// A letterboard tile's capitals, at a size the TILE decides.    public static func letterboard(_ size: CGFloat) -> Font {        register()        #if canImport(UIKit)        return .custom(letterboardFace, fixedSize: drawable(size))        #else        return facesLoaded ? .custom(letterboardFace, fixedSize: drawable(size)) : .system(size: size, weight: .semibold).width(.condensed)        #endif    }    /// The typing on a card, at a size the CARD decides.    public static func type(_ size: CGFloat) -> Font {        register()        #if canImport(UIKit)        return .custom(typeFace, fixedSize: drawable(size))        #else        return facesLoaded ? .custom(typeFace, fixedSize: drawable(size)) : .system(size: size, design: .monospaced)        #endif    }    /// The typing's italic — the day on a card, the count on a section.    public static func typeItalic(_ size: CGFloat) -> Font {        register()        #if canImport(UIKit)        return .custom(typeItalicFace, fixedSize: drawable(size))        #else        return facesLoaded ? .custom(typeItalicFace, fixedSize: drawable(size)) : .system(size: size, design: .monospaced).italic()        #endif    }    /// A departures board's flap lettering, now a plain video sleeve's    /// title (`FilmVHSFace`): the system sans at its compressed width and a    /// semibold weight — tall, narrow capitals. A    /// system face, so nothing is registered for it, and fixed-size as the    /// faces above are: a letter that grew would walk off its flap.    public static func flap(_ size: CGFloat) -> Font {        .system(size: drawable(size), weight: .semibold).width(.compressed)    }    /// The annotation written on a sheet, at a size the SHEET decides. Caveat    /// runs small for its point size — its x-height is well under the    /// typewriter's — so a caller asking for "the same size as the meta line"    /// should ask for one or two points more.    /// An SF Symbol's glyph at `size` — the one door a face sizes a symbol    /// through, so the size is a theme key and never a literal at the call.    public static func symbol(_ size: CGFloat, weight: Font.Weight = .regular) -> Font {        .system(size: size, weight: weight)    }    public static func hand(_ size: CGFloat) -> Font {        register()        #if canImport(UIKit)        return .custom(handFace, fixedSize: drawable(size))        #else        return facesLoaded ? .custom(handFace, fixedSize: drawable(size)) : .system(size: size, design: .serif).italic()        #endif    }    /// Once. CoreText refuses a second registration of the same file with an    /// error this ignores on purpose: "already registered" is the state it    /// wanted.    ///    /// The bundle is `CottonResources.bundle` — `Bundle.module` on the phone,    /// and on the Mac the same bundle FOUND BY PATH (`Resources.swift`), where    /// `.copy("Resources/Fonts")` has put the files as-is. A Mac build with no    /// bundle registers nothing and the accessors keep their system fallbacks.    private static let registration: Bool = {        guard let bundle = CottonResources.bundle else { return false }        var registered = 0        for face in faces {            guard let url = bundle.url(forResource: face, withExtension: "ttf", subdirectory: "Fonts") else {                continue            }            _ = CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil)            registered += 1        }        return registered == faces.count    }()    /// The flavours' faces, registered the same way and counted apart, so a    /// build without them still has every house face.    private static let brandRegistration: Bool = {        guard let bundle = CottonResources.bundle else { return false }        var registered = 0        for face in brandFaces {            guard let url = bundle.url(forResource: face, withExtension: "ttf", subdirectory: "Fonts") else { continue }            _ = CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil)            registered += 1        }        return registered == brandFaces.count    }()    static func register() {        _ = registration        _ = brandRegistration    }    /// A face sized from a layout's geometry is asked for at 0 pt on the    /// first pass, and a 0 pt font cannot be archived: a renderer refused the    /// whole view ("invalidFont") and a gallery preview of the caps, the tools    /// or the chats drew blank (5 Oct 2026). Never    /// smaller than a point.    static func drawable(_ size: CGFloat) -> CGFloat {        size.isFinite ? max(size, 1) : 1    }    /// Every face is registered — on the Mac, the difference between the    /// typewriter and a system monospaced stand-in.    static var facesLoaded: Bool { registration }    /// Every flavour face is registered.    static var brandFacesLoaded: Bool { brandRegistration }}