Cotton code
171 lines · 8.8 KB · SwiftRaw
// POST'S STAMP LOOK (assets/design/mail/DECISIONS.md "The look", mail.html; the owner chose the Stamp, 25 Nov 2026):// sage paper, a green-black ink, and one franking-green accent, only where there is something to do (the main button,// the pen, an unfinished record, a failing status line). Unread is bold with an ink dot, never the accent. Instrument// Sans for everything he taps, Newsreader for titles and the words of a mail (a thread reads like a letter), JetBrains// Mono for app passwords and DNS records whatever face Look chooses. Thin rules and space; no cards, shadows or// gradients. Every Post screen draws from this file, through the core's one `look {app: mail}` (papers Sage, Paper,// Night, Coal; inks Franking green, Teal, Oxblood, Ink blue, Graphite), as Android's.#if os(iOS)import CoreTextimport CottonWritingimport SwiftUIimport UIKit/// mail.html's `:root`, as the person's Look stands: the chosen paper's half for the phone's appearance, in the chosen/// ink, from the core's `look`; before the core has ever answered on this phone, the Stamp default for the phone's/// appearance. Read inside a body, a change in Look draws the screen again.@MainActorenum PostInk {    static var look: LookModel { LookModel.standing("mail") }    private static func tone(_ token: String, _ light: UInt32, _ dark: UInt32) -> Color {        if let hex = look.tokens?[token] { return Color(uiColor: LookAnswer.ui(hex)) }        let value = LookAppearance.shared.systemDark ? dark : light        return Color(uiColor: UIColor(red: CGFloat((value >> 16) & 0xFF) / 255, green: CGFloat((value >> 8) & 0xFF) / 255,                                      blue: CGFloat(value & 0xFF) / 255, alpha: 1))    }    /// `--ground`: the paper every Post screen stands on.    static var ground: Color { tone("ground", 0xF1F3EE, 0x131915) }    static var ink: Color { tone("ink", 0x18211C, 0xE3EBE5) }    static var quiet: Color { tone("quiet", 0x56625A, 0x9EABA2) }    static var faint: Color { tone("faint", 0x89958C, 0x68746B) }    static var rule: Color { tone("rule", 0xD2D9D0, 0x27312B) }    /// `--acc`: franking green. "Act here", never a state.    static var accent: Color { tone("accent", 0x1E6B4A, 0x7CC9A0) }    static var onAccent: Color { tone("on_accent", 0xFFFFFF, 0x0B1F15) }    /// Whether the standing paper is light (bars, keyboards and the status bar follow it).    static var isLight: Bool { look.answer == nil ? !LookAppearance.shared.systemDark : look.groundIsLight }    /// The screen's side margin (`.ph` padding 22).    static let side: CGFloat = 22    /// A 1 px rule.    static let hair: CGFloat = 1    /// A button's and a chosen chip's outline (`1.5px`).    static let stroke: CGFloat = 1.5}/// Instrument Sans (words), Newsreader (titles and a mail's words) and JetBrains Mono (passwords, records), each/// variable on its 'wght' axis, SIL OFL, in the bundle's `look` folder (`assets/fonts/look`, the same files Android/// ships: `kits/look/look.json`'s `instrument`, `newsreader` and `jetbrains_mono`).enum PostFonts {    enum Kind { case sans, serif, mono }    private static let weightAxis = 0x7767_6874    private static let registered: [String: String] = {        var names: [String: String] = [:]        for file in ["InstrumentSans", "InstrumentSans-Italic", "Newsreader", "Newsreader-Italic", "JetBrainsMono"] {            guard let url = Bundle.main.url(forResource: file, withExtension: "ttf", subdirectory: "look") else { continue }            CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil)            let descriptors = CTFontManagerCreateFontDescriptorsFromURL(url as CFURL) as? [CTFontDescriptor] ?? []            if let name = descriptors.first.flatMap({ CTFontDescriptorCopyAttribute($0, kCTFontNameAttribute) as? String }) { names[file] = name }        }        return names    }()    static func register() { _ = registered }    /// The face at `size` and 'wght' `weight`, or the system's when the file is not in this build.    static func ui(_ kind: Kind, _ size: CGFloat, weight: Double, italic: Bool = false) -> UIFont {        let file: String        switch kind {        case .sans: file = italic ? "InstrumentSans-Italic" : "InstrumentSans"        case .serif: file = italic ? "Newsreader-Italic" : "Newsreader"        case .mono: file = "JetBrainsMono"        }        guard let name = registered[file] ?? (italic ? registered[file.replacingOccurrences(of: "-Italic", with: "")] : nil) else {            let w: UIFont.Weight = weight >= 650 ? .bold : weight >= 550 ? .semibold : weight >= 450 ? .medium : .regular            switch kind {            case .mono: return .monospacedSystemFont(ofSize: size, weight: w)            case .serif:                let base = UIFont.systemFont(ofSize: size, weight: w)                return base.fontDescriptor.withDesign(.serif).map { UIFont(descriptor: $0, size: size) } ?? base            case .sans: return .systemFont(ofSize: size, weight: w)            }        }        let variation = UIFontDescriptor.AttributeName(rawValue: kCTFontVariationAttribute as String)        return UIFont(descriptor: UIFontDescriptor(name: name, size: size).addingAttributes([variation: [weightAxis: weight]]), size: size)    }}/// mail.html's type. Every size follows Look's size step and the phone's own text size (`relativeTo`), so larger text/// grows the screen rather than clipping it.@MainActorenum PostType {    enum Weight: Double { case regular = 400, medium = 500, semibold = 600, bold = 700 }    /// What he taps: Instrument Sans, or the face Look chose.    static func sans(_ size: CGFloat, _ weight: Weight = .regular, relativeTo style: Font.TextStyle = .body, italic: Bool = false) -> Font {        Font(ui(.sans, size, weight, relativeTo: style, italic: italic))    }    /// Titles and a mail's words: Newsreader, or the face Look chose.    static func serif(_ size: CGFloat, _ weight: Weight = .regular, relativeTo style: Font.TextStyle = .body) -> Font {        Font(ui(.serif, size, weight, relativeTo: style))    }    /// App passwords and DNS records: always JetBrains Mono.    static func mono(_ size: CGFloat, _ weight: Weight = .regular, relativeTo style: Font.TextStyle = .body) -> Font {        Font(UIFontMetrics(forTextStyle: style.postUI).scaledFont(for: PostFonts.ui(.mono, size * PostInk.look.scale, weight: weight.rawValue)))    }    static func ui(_ kind: PostFonts.Kind, _ size: CGFloat, _ weight: Weight = .regular, relativeTo style: Font.TextStyle = .body, italic: Bool = false) -> UIFont {        let look = PostInk.look        let size = size * look.scale        let base: UIFont        if look.face != LookFaces.own, kind != .mono {            let w: UIFont.Weight = weight == .bold ? .bold : weight == .semibold ? .semibold : weight == .medium ? .medium : .regular            base = WritingFonts.font(LookFaces.drawn(look.face, own: "figtree"), size: size, weight: w, italic: italic, scaled: false)        } else {            base = PostFonts.ui(kind, size, weight: weight.rawValue, italic: italic)        }        return UIFontMetrics(forTextStyle: style.postUI).scaledFont(for: base)    }    /// The face as Look's screen draws a choice of it, at `size`.    static func face(_ id: String, size: CGFloat) -> Font {        id == LookFaces.own ? Font(PostFonts.ui(.sans, size, weight: 400))            : Font(WritingFonts.font(LookFaces.drawn(id, own: "figtree"), size: size))    }    // mail.html's sizes, by role.    /// `.ph h3`: a screen's title, Newsreader 32.    static var title: Font { serif(32, .medium, relativeTo: .largeTitle) }    /// `.r .t`: a row's name, 16.5 semibold.    static var rowName: Font { sans(16.5, .semibold) }    /// A read row's name, 16.5 medium.    static var rowRead: Font { sans(16.5, .medium) }    /// Body: 16.    static var body: Font { sans(16) }    /// `.r .s`, `.note`: second lines and notes, 14.    static var small: Font { sans(14, relativeTo: .footnote) }    /// `.ph h4`: a band's heading, 13 semibold quiet.    static var section: Font { sans(13, .semibold, relativeTo: .footnote) }    /// `.r .e`: a row's end (its time, its count), 13.5.    static var end: Font { sans(13.5, relativeTo: .footnote) }    /// `.btn`: a button's words, 16.5 bold.    static var button: Font { sans(16.5, .bold) }    /// The words of a mail: Newsreader 18, so a thread reads like a letter.    static var letter: Font { serif(18, relativeTo: .body) }}extension Font.TextStyle {    fileprivate var postUI: UIFont.TextStyle {        switch self {        case .largeTitle: .largeTitle        case .title: .title1        case .title2: .title2        case .title3: .title3        case .headline: .headline        case .subheadline: .subheadline        case .callout: .callout        case .footnote: .footnote        case .caption: .caption1        case .caption2: .caption2        default: .body        }    }}#endif