// THE type primitive — one scale, ten roles, zero loaded fonts. // // The Bear design constitution's rule is that // Cotton wears the SYSTEM face: no `useFonts`, no bundled family, nothing to // wait for at boot. `code` is the one role that names a face at all (Menlo, // which ships with iOS), and it does so because an indent has to be an indent. // // THE ONE AMENDMENT is `Fonts.swift`: two bundled faces for LETTERING ON AN // OBJECT — the names printed on Home's bottle caps and the word over them // (the owner's design of 2026-09-13). They are fixed-size, they go through // none of the ten roles, and nothing that SETS a sentence may reach them. // // DYNAMIC TYPE. The phone's roles are absolute point sizes, which on iOS would // mean text that never grows. Each role therefore declares the system text // STYLE it is relative to, and the size is scaled through `@ScaledMetric // (relativeTo:)` — UIFontMetrics under the hood, which is the real thing rather // than a multiplier of our own. The authored size is what the number MEANS at // the default content size, which is what the tests assert. // // LINE HEIGHT is the RN line BOX, not SwiftUI's `lineSpacing` (which is the gap // BETWEEN lines). The modifier converts: it asks UIFont for the face's natural // line height at the scaled size and adds the difference, so a role's rendered // pitch matches the phone's rather than approximating it. import SwiftUI #if canImport(UIKit) import UIKit #endif /// The ten roles. Adding an eleventh is a design decision, not a call site's. public enum TextRole: String, CaseIterable, Sendable { case pageTitle case editorTitle case screenTitle case rowTitle case rowTitleCompact case body case preview case caption case sectionLabel case code /// The theme's key for the authored size, at the default content size. public var sizeKey: ThemeScale.Key { switch self { case .pageTitle: return .typePageTitleSize case .editorTitle: return .typeEditorTitleSize case .screenTitle: return .typeScreenTitleSize case .rowTitle: return .typeRowTitleSize case .rowTitleCompact: return .typeRowTitleCompactSize case .body: return .typeBodySize case .preview: return .typePreviewSize case .caption: return .typeCaptionSize case .sectionLabel: return .typeSectionLabelSize case .code: return .typeCodeSize } } /// The theme's key for the authored line BOX height. public var lineHeightKey: ThemeScale.Key { switch self { case .pageTitle: return .typePageTitleLineHeight case .editorTitle: return .typeEditorTitleLineHeight case .screenTitle: return .typeScreenTitleLineHeight case .rowTitle: return .typeRowTitleLineHeight case .rowTitleCompact: return .typeRowTitleCompactLineHeight case .body: return .typeBodyLineHeight case .preview: return .typePreviewLineHeight case .caption: return .typeCaptionLineHeight case .sectionLabel: return .typeSectionLabelLineHeight case .code: return .typeCodeLineHeight } } /// The authored size in `theme`, at the default content size. public func size(in theme: Theme) -> CGFloat { theme.scale[sizeKey] } /// The authored line BOX height in `theme`. public func lineHeight(in theme: Theme) -> CGFloat { theme.scale[lineHeightKey] } /// The role's own weight. `rowTitle` is MEDIUM, not semibold: a list where /// every title is semibold has no quiet register left. public var weight: TextWeight { switch self { case .pageTitle: return .bold case .editorTitle: return .semibold case .screenTitle: return .semibold case .rowTitle: return .medium case .rowTitleCompact: return .medium case .body: return .regular case .preview: return .regular case .caption: return .regular case .sectionLabel: return .semibold case .code: return .regular } } /// The role's own ink, unless a call site names another BY NAME. public var ink: TextInk { switch self { case .pageTitle, .editorTitle, .screenTitle, .rowTitle, .rowTitleCompact: return .ink case .body: return .body case .preview: return .secondary case .caption: return .tertiary case .sectionLabel: return .tertiary case .code: return .body } } /// `sectionLabel` is the one role that shouts and tracks. public var isUppercase: Bool { self == .sectionLabel } public func letterSpacing(in theme: Theme) -> CGFloat { self == .sectionLabel ? theme.scale[.typeSectionLabelTracking] : 0 } /// The system text style this role scales WITH. Chosen by nearest authored /// size so a role's growth curve is the one iOS uses for text of that size. public var textStyle: Font.TextStyle { switch self { case .pageTitle: return .largeTitle // 34 case .editorTitle: return .title // 28 case .screenTitle: return .title3 // 20 case .rowTitle: return .title3 // 20 case .rowTitleCompact: return .body // 17 case .body: return .callout // 16 case .preview: return .subheadline // 15 case .caption: return .footnote // 13 case .sectionLabel: return .footnote // 13 case .code: return .caption // 12 } } /// The only role that names a face. public var isMonospaced: Bool { self == .code } } /// Five weights, by name. public enum TextWeight: String, CaseIterable, Sendable { case regular, medium, semibold, bold, heavy /// The theme's key for the weight's numeric value (400 regular … 800 heavy). public var valueKey: ThemeScale.Key { switch self { case .regular: return .typeWeightRegular case .medium: return .typeWeightMedium case .semibold: return .typeWeightSemibold case .bold: return .typeWeightBold case .heavy: return .typeWeightHeavy } } public var font: Font.Weight { switch self { case .regular: return .regular case .medium: return .medium case .semibold: return .semibold case .bold: return .bold case .heavy: return .heavy } } var uiFont: UIFont.Weight { switch self { case .regular: return .regular case .medium: return .medium case .semibold: return .semibold case .bold: return .bold case .heavy: return .heavy } } } /// The ink levels. A call site overrides ink BY NAME, never by a colour — /// which is the whole reason this enum exists and `PaletteGuardTests` refuses /// a literal. public enum TextInk: String, CaseIterable, Sendable { case ink, body, secondary, tertiary, accent, inverse, error /// The page GROUND as an ink — for a label on a control filled with the /// `ink` role (Home's New thought pill, design 2026-09-13 D5/D9). Paper: /// white on charcoal; Graphite: the palette inverts both and the pill is /// still legible. Not `inverse`, which is the ACCENT's contrast. case ground /// The ink in Cotton's own palette, for a reader with no view around it /// (a UIKit seam, an attributed string). A view reads it off the theme /// through `cottonType`, so a database's theme reaches it. public var color: Color { switch self { case .ground: return CottonColors.ground case .ink: return CottonColors.ink case .body: return CottonColors.inkBody case .secondary: return CottonColors.inkSecondary case .tertiary: return CottonColors.inkTertiary case .accent: return CottonColors.accent case .inverse: return CottonColors.accentContrast case .error: return CottonColors.statusError } } } /// The modifier every piece of text in the app goes through. struct CottonTypeModifier: ViewModifier { private let role: TextRole private let ink: TextInk private let weight: TextWeight private let tabular: Bool /// The Dynamic Type factor for the role's text style; the theme's sizes are /// the authored ones at the default content size, where this is exactly 1. @ScaledMetric private var growth: CGFloat @Environment(\.theme) private var theme init(role: TextRole, ink: TextInk? = nil, weight: TextWeight? = nil, tabular: Bool = false) { self.role = role self.ink = ink ?? role.ink self.weight = weight ?? role.weight self.tabular = tabular _growth = ScaledMetric(wrappedValue: 1, relativeTo: role.textStyle) } public func body(content: Content) -> some View { content .font(font) .foregroundStyle(theme.text(ink)) .tracking(role.letterSpacing(in: theme)) .lineSpacing(extraLineSpacing) // The line box is `lineHeight`; SwiftUI centres a taller box around // the glyphs, so half the extra goes above and half below. .padding(.vertical, extraLineSpacing * 0.5) .textCase(role.isUppercase ? .uppercase : nil) } private var size: CGFloat { role.size(in: theme) * growth } private var lineHeight: CGFloat { role.lineHeight(in: theme) * growth } private var font: Font { let base = Font.system( size: size, weight: weight.font, design: role.isMonospaced ? .monospaced : .default ) // Tabular figures for a column of numbers — a money screen's amounts // must line up on the decimal point. return tabular ? base.monospacedDigit() : base } /// The gap SwiftUI must add between lines so the rendered pitch is the /// authored line box. Never negative: a role whose box is tighter than the /// face's own line height keeps the face's. private var extraLineSpacing: CGFloat { let natural = UIFont.systemFont(ofSize: size, weight: weight.uiFont).lineHeight return max(0, lineHeight - natural) } } extension View { /// Wear a role. `ink` and `weight` override the role's own, by name. public func cottonType( _ role: TextRole, ink: TextInk? = nil, weight: TextWeight? = nil, tabular: Bool = false ) -> some View { modifier(CottonTypeModifier(role: role, ink: ink, weight: weight, tabular: tabular)) } } /// A face set at an authored size that grows with Dynamic Type: the shell's /// own words over a deck (its top line, a view tile's name and count) wear /// the typewriter, not a role, yet scale as the phone's text size says. /// `font` is handed the growth for `style` — exactly 1 at the default size. struct ScaledFaceModifier: ViewModifier { @ScaledMetric private var growth: CGFloat private let font: (CGFloat) -> Font init(relativeTo style: Font.TextStyle, font: @escaping (CGFloat) -> Font) { _growth = ScaledMetric(wrappedValue: 1, relativeTo: style) self.font = font } func body(content: Content) -> some View { content.font(font(growth)) } } extension View { /// Set `font` at the growth Dynamic Type gives `style` (`ScaledFaceModifier`). public func scaledFace(relativeTo style: Font.TextStyle = .body, _ font: @escaping (CGFloat) -> Font) -> some View { modifier(ScaledFaceModifier(relativeTo: style, font: font)) } } /// The growth Dynamic Type gives a text style, for a view that sizes more /// than a font by it (a fan tile fitting its words to its card). @propertyWrapper public struct TextGrowth: DynamicProperty { @ScaledMetric private var growth: CGFloat public init(relativeTo style: Font.TextStyle) { _growth = ScaledMetric(wrappedValue: 1, relativeTo: style) } public var wrappedValue: CGFloat { growth } }