Cotton code
90 lines · 3.5 KB · SwiftRaw
// The five voices. Text on paper uses these and nothing else.//// `Typography.swift`'s ten roles still govern every sentence the app SETS in// chrome — a settings row, a dialog, the keyboard. These five govern what is// PRINTED ON AN OBJECT, and the difference is material rather than taste:////   title   serif        Fraunces Text     the object's own name//   body    typewriter   Courier Prime     the record's content//   meta    typewriter   Courier Prime     a detail line, in pencil//   hand    handwriting  Caveat            annotation — dates, counts, states//   red     typewriter   Courier Prime     an exception, in the palette's red//// Each is a `ViewModifier` that sets a face, an ink and the one blend, so a// screen cannot set half of a voice. They are FIXED-SIZE for the reason the// faces are (`Fonts.swift`): print on an object scales with the object, never// with Dynamic Type, or a long name walks off the paper.import SwiftUI/// One voice. The face, the ink and the blend come off the theme: on paper/// the serif, the typewriter and the hand multiplied into the fibre; in a/// flat family the system face printed plainly.struct PaperVoice: ViewModifier {    public enum Face: String, CaseIterable, Sendable {        case serif, typewriter, typewriterItalic, handwriting    }    /// The size a caller left out — the theme's own for the voice.    enum Size {        case title, body, meta, hand    }    let face: Face    let size: CGFloat?    let fallback: Size    let ink: PaperInk    @Environment(\.theme) private var theme    public func body(content: Content) -> some View {        content            .font(PaperVoice.font(face, size ?? points(fallback), theme: theme))            .foregroundStyle(theme.print(ink))            .blendMode(theme.printBlend)    }    private func points(_ size: Size) -> CGFloat {        switch size {        case .title: return theme.scale.titleSize        case .body: return theme.scale.bodySize        case .meta: return theme.scale.metaSize        case .hand: return theme.scale.handSize        }    }    static func font(_ face: Face, _ size: CGFloat, theme: Theme) -> Font {        switch face {        case .serif: return theme.type.title(size)        case .typewriter: return theme.type.body(size)        case .typewriterItalic: return theme.type.meta(size)        case .handwriting: return theme.type.hand(size)        }    }}extension View {    /// The object's own name, in the serif.    public func paperTitle(_ size: CGFloat? = nil, ink: PaperInk = .ink) -> some View {        modifier(PaperVoice(face: .serif, size: size, fallback: .title, ink: ink))    }    /// The record's content, typed.    public func paperBody(_ size: CGFloat? = nil, ink: PaperInk = .ink) -> some View {        modifier(PaperVoice(face: .typewriter, size: size, fallback: .body, ink: ink))    }    /// A detail line — typed, in pencil.    public func paperMeta(_ size: CGFloat? = nil, ink: PaperInk = .pencil) -> some View {        modifier(PaperVoice(face: .typewriterItalic, size: size, fallback: .meta, ink: ink))    }    /// An annotation, handwritten. The date in a corner, "6 days late", a count.    public func paperHand(_ size: CGFloat? = nil, ink: PaperInk = .pencil) -> some View {        modifier(PaperVoice(face: .handwriting, size: size, fallback: .hand, ink: ink))    }    /// An exception, typed in red ink.    public func paperRed(_ size: CGFloat? = nil) -> some View {        modifier(PaperVoice(face: .typewriter, size: size, fallback: .body, ink: .red))    }}