// // ProseTypography.swift // Museion // // #71 (S1.2, map #20 / #70): the three role slots — Body / Headings / Code — // each {face, size}, persisted in UserDefaults (six values), read by BOTH // prose surfaces (styled-source editing + reading). Machine-local faces, // nothing bundled; a missing face falls back to the system font silently. // Chrome stays on system faces. // // Resolution note (#70 deviation, probed in-session): family+weight lookup // misresolves Concourse OT — Thin and Book both advertise weight 4, and // NSFontManager picks Thin. Faces therefore resolve through the family's // member list: prefer the Book/Regular member, else the middle weight. // import AppKit /// Prose typography slots (#70). Static computed accessors — surfaces read /// on render; the Settings pane writes UserDefaults and the surfaces rebuild /// (pane re-entry rebuilds the editor; reading rebuilds per toggle). nonisolated enum ProseTypography { /// v1 defaults (#70): Body = Concourse OT Book 17, Headings = Iowan Old /// Style bold 26, Code = UbuntuMono Nerd Font Mono 13. static let defaultBodyFace = "Concourse OT" static let defaultHeadingsFace = "Iowan Old Style" static let defaultCodeFace = "UbuntuMono Nerd Font Mono" static let defaultBodySize: Double = 17 static let defaultHeadingsSize: Double = 26 static let defaultCodeSize: Double = 13 /// Text flow (#71 polish): line-height multiple + paragraph/heading /// spacing measured in body lines — different faces need different flow. static let defaultLineHeight: Double = 1.25 static let defaultParagraphSpacingLines: Double = 1 static let defaultHeadingSpacingLines: Double = 2 /// Register the six defaults once at app start. static func registerDefaults() { UserDefaults.standard.register(defaults: [ "proseBodyFace": defaultBodyFace, "proseBodySize": defaultBodySize, "proseHeadingsFace": defaultHeadingsFace, "proseHeadingsSize": defaultHeadingsSize, "proseCodeFace": defaultCodeFace, "proseCodeSize": defaultCodeSize, "proseLineHeight": defaultLineHeight, "proseParagraphSpacingLines": defaultParagraphSpacingLines, "proseHeadingSpacingLines": defaultHeadingSpacingLines, ]) } private static func face(_ key: String, defaultFace: String) -> String { UserDefaults.standard.string(forKey: key) ?? defaultFace } private static func size(_ key: String, defaultSize: Double) -> CGFloat { let stored = UserDefaults.standard.double(forKey: key) return stored == 0 ? defaultSize : stored } // MARK: Slot fonts /// Body slot font — the family's Book/Regular member at the slot size. static var bodyFont: NSFont { font(family: face("proseBodyFace", defaultFace: defaultBodyFace), size: size("proseBodySize", defaultSize: defaultBodySize)) } /// Headings slot font — the family's Book/Regular member, bold trait, at /// the slot size (h1). Scaled levels call `headingFont(size:)`. static var headingFont: NSFont { bolded(headingFont(size: size("proseHeadingsSize", defaultSize: defaultHeadingsSize))) } static func headingFont(size: CGFloat) -> NSFont { bolded(font(family: face("proseHeadingsFace", defaultFace: defaultHeadingsFace), size: size)) } /// Code slot font (inline code, fenced blocks, and the editing surface's /// dimmed syntax). static var codeFont: NSFont { font(family: face("proseCodeFace", defaultFace: defaultCodeFace), size: size("proseCodeSize", defaultSize: defaultCodeSize)) } static var italicBodyFont: NSFont { NSFontManager.shared.convert(bodyFont, toHaveTrait: .italicFontMask) } static var boldBodyFont: NSFont { bolded(bodyFont) } // MARK: Text flow (#71 polish) /// Line-height multiple (clamped 1…2; 0/absent → default). static var lineHeightMultiple: CGFloat { clamped("proseLineHeight", default: defaultLineHeight, 1 ... 2) } /// Spacing after body paragraphs, in body lines. static var paragraphSpacingLines: CGFloat { clamped("proseParagraphSpacingLines", default: defaultParagraphSpacingLines, 0 ... 4) } /// Spacing after headings, in body lines. static var headingSpacingLines: CGFloat { clamped("proseHeadingSpacingLines", default: defaultHeadingSpacingLines, 0 ... 6) } /// Every value the prose surfaces render from, summed — a change key /// for live re-attribution when settings move. static var flowKey: String { "\(bodyFont.fontName)@\(bodyFont.pointSize)|\(codeFont.fontName)@\(codeFont.pointSize)|\(headingFont.fontName)@\(headingFont.pointSize)|lh=\(lineHeightMultiple)|p=\(paragraphSpacingLines)|h=\(headingSpacingLines)" } /// One body line at the configured flow, in points. static var linePoints: CGFloat { let f = bodyFont return (f.ascender - f.descender + f.leading) * lineHeightMultiple } private static func clamped(_ key: String, default def: Double, _ range: ClosedRange) -> CGFloat { let stored = UserDefaults.standard.double(forKey: key) let value = stored == 0 ? def : stored return CGFloat(min(max(value, range.lowerBound), range.upperBound)) } // MARK: Attribute stacks (reading + editing share them) static var foregroundColor: NSColor { NSColor(red: 0.10, green: 0.10, blue: 0.09, alpha: 1) } /// Dimmed syntax color (the editing surface's 0.55 dim, #19). static var syntaxColor: NSColor { NSColor(white: 0.55, alpha: 1) } static var quoteRuleColor: NSColor { NSColor(white: 0.78, alpha: 1) } /// Table borders + reading HR hairline (≈ DesignTokens.border). static var tableBorderColor: NSColor { NSColor(red: 0xE8 / 255, green: 0xE5 / 255, blue: 0xDF / 255, alpha: 1) } static var bodyAttributes: [NSAttributedString.Key: Any] { [ .font: bodyFont, .paragraphStyle: ProsePresenter.paragraph(.body), .foregroundColor: foregroundColor, ] } static var codeAttributes: [NSAttributedString.Key: Any] { [ .font: codeFont, .foregroundColor: syntaxColor, ] } // MARK: Resolution /// Family → member PS name → NSFont. Prefer Book/Regular styles, else the /// member closest to weight 5; unknown family → system font (silent, #70). private static func font(family: String, size: CGFloat) -> NSFont { let members = NSFontManager.shared.availableMembers(ofFontFamily: family) ?? [] let regular = members.first { entry in guard let style = entry[safe: 1] as? String else { return false } return style.range(of: "book|regular", options: [.caseInsensitive, .regularExpression]) != nil } ?? members.min { lhs, rhs in abs((lhs[safe: 2] as? Int ?? 5) - 5) < abs((rhs[safe: 2] as? Int ?? 5) - 5) } guard let name = regular?[safe: 0] as? String else { return NSFont.systemFont(ofSize: size) } return NSFont(name: name, size: size) ?? NSFont.systemFont(ofSize: size) } private static func bolded(_ base: NSFont) -> NSFont { NSFontManager.shared.convert(base, toHaveTrait: .boldFontMask) } } private nonisolated extension Array { subscript(safe index: Int) -> Element? { indices.contains(index) ? self[index] : nil } }