// 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.importSwiftUI#if canImport(UIKit)import UIKit#endif/// The ten roles. Adding an eleventh is a design decision, not a call site's.publicenumTextRole: String, CaseIterable, Sendable {case pageTitlecase editorTitlecase screenTitlecase rowTitlecase rowTitleCompactcase bodycase previewcase captioncase sectionLabelcase code/// The theme's key for the authored size, at the default content size.publicvar sizeKey: ThemeScale.Key {switchself {case .pageTitle: return .typePageTitleSizecase .editorTitle: return .typeEditorTitleSizecase .screenTitle: return .typeScreenTitleSizecase .rowTitle: return .typeRowTitleSizecase .rowTitleCompact: return .typeRowTitleCompactSizecase .body: return .typeBodySizecase .preview: return .typePreviewSizecase .caption: return .typeCaptionSizecase .sectionLabel: return .typeSectionLabelSizecase .code: return .typeCodeSize } }/// The theme's key for the authored line BOX height.publicvar lineHeightKey: ThemeScale.Key {switchself {case .pageTitle: return .typePageTitleLineHeightcase .editorTitle: return .typeEditorTitleLineHeightcase .screenTitle: return .typeScreenTitleLineHeightcase .rowTitle: return .typeRowTitleLineHeightcase .rowTitleCompact: return .typeRowTitleCompactLineHeightcase .body: return .typeBodyLineHeightcase .preview: return .typePreviewLineHeightcase .caption: return .typeCaptionLineHeightcase .sectionLabel: return .typeSectionLabelLineHeightcase .code: return .typeCodeLineHeight } }/// The authored size in `theme`, at the default content size.publicfunc size(in theme: Theme) -> CGFloat { theme.scale[sizeKey] }/// The authored line BOX height in `theme`.publicfunc 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.publicvar weight: TextWeight {switchself {case .pageTitle: return .boldcase .editorTitle: return .semiboldcase .screenTitle: return .semiboldcase .rowTitle: return .mediumcase .rowTitleCompact: return .mediumcase .body: return .regularcase .preview: return .regularcase .caption: return .regularcase .sectionLabel: return .semiboldcase .code: return .regular } }/// The role's own ink, unless a call site names another BY NAME.publicvar ink: TextInk {switchself {case .pageTitle, .editorTitle, .screenTitle, .rowTitle, .rowTitleCompact:return .inkcase .body: return .bodycase .preview: return .secondarycase .caption: return .tertiarycase .sectionLabel: return .tertiarycase .code: return .body } }/// `sectionLabel` is the one role that shouts and tracks.publicvar isUppercase: Bool { self == .sectionLabel }publicfunc 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.publicvar textStyle: Font.TextStyle {switchself {case .pageTitle: return .largeTitle // 34case .editorTitle: return .title // 28case .screenTitle: return .title3 // 20case .rowTitle: return .title3 // 20case .rowTitleCompact: return .body // 17case .body: return .callout // 16case .preview: return .subheadline // 15case .caption: return .footnote // 13case .sectionLabel: return .footnote // 13case .code: return .caption // 12 } }/// The only role that names a face.publicvar isMonospaced: Bool { self == .code }}/// Five weights, by name.publicenumTextWeight: String, CaseIterable, Sendable {case regular, medium, semibold, bold, heavy/// The theme's key for the weight's numeric value (400 regular … 800 heavy).publicvar valueKey: ThemeScale.Key {switchself {case .regular: return .typeWeightRegularcase .medium: return .typeWeightMediumcase .semibold: return .typeWeightSemiboldcase .bold: return .typeWeightBoldcase .heavy: return .typeWeightHeavy } }publicvar font: Font.Weight {switchself {case .regular: return .regularcase .medium: return .mediumcase .semibold: return .semiboldcase .bold: return .boldcase .heavy: return .heavy } }var uiFont: UIFont.Weight {switchself {case .regular: return .regularcase .medium: return .mediumcase .semibold: return .semiboldcase .bold: return .boldcase .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.publicenumTextInk: 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.publicvar color: Color {switchself {case .ground: returnCottonColors.groundcase .ink: returnCottonColors.inkcase .body: returnCottonColors.inkBodycase .secondary: returnCottonColors.inkSecondarycase .tertiary: returnCottonColors.inkTertiarycase .accent: returnCottonColors.accentcase .inverse: returnCottonColors.accentContrastcase .error: returnCottonColors.statusError } }}/// The modifier every piece of text in the app goes through.structCottonTypeModifier: ViewModifier {privatelet role: TextRoleprivatelet ink: TextInkprivatelet weight: TextWeightprivatelet 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 privatevar growth: CGFloat @Environment(\.theme) privatevar themeinit(role: TextRole, ink: TextInk? = nil, weight: TextWeight? = nil, tabular: Bool = false) {self.role = roleself.ink = ink ?? role.inkself.weight = weight ?? role.weightself.tabular = tabular _growth = ScaledMetric(wrappedValue: 1, relativeTo: role.textStyle) }publicfunc body(content: Content) -> someView { 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) }privatevar size: CGFloat { role.size(in: theme) * growth }privatevar lineHeight: CGFloat { role.lineHeight(in: theme) * growth }privatevar 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.privatevar extraLineSpacing: CGFloat {let natural = UIFont.systemFont(ofSize: size, weight: weight.uiFont).lineHeightreturn max(0, lineHeight - natural) }}extensionView {/// Wear a role. `ink` and `weight` override the role's own, by name.publicfunc cottonType( _ role: TextRole, ink: TextInk? = nil, weight: TextWeight? = nil, tabular: Bool = false ) -> someView { 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.structScaledFaceModifier: ViewModifier { @ScaledMetric privatevar growth: CGFloatprivatelet font: (CGFloat) -> Fontinit(relativeTo style: Font.TextStyle, font: @escaping (CGFloat) -> Font) { _growth = ScaledMetric(wrappedValue: 1, relativeTo: style)self.font = font }func body(content: Content) -> someView { content.font(font(growth)) }}extensionView {/// Set `font` at the growth Dynamic Type gives `style` (`ScaledFaceModifier`).publicfunc scaledFace(relativeTo style: Font.TextStyle = .body, _ font: @escaping (CGFloat) -> Font) -> someView { 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).@propertyWrapperpublicstructTextGrowth: DynamicProperty { @ScaledMetric privatevar growth: CGFloatpublicinit(relativeTo style: Font.TextStyle) { _growth = ScaledMetric(wrappedValue: 1, relativeTo: style) }publicvar wrappedValue: CGFloat { growth }}