Map #20 execution child, built per decisions #68/#69/#70: - ReadingPresenter: block-aware renderer (ADR-0010 stance) — blank-line split, one block per chunk; lists (bullets + ordered + nesting + auto-renumber via per-level counters — one rendering path), headings by level (h1 = slot size, h2 = 0.77x, ### = body+1, #### floors at bold body), HR via the editing syntaxOnly predicate, fenced code, blockquotes (QuoteTextBlock hairline), tables (NSTextTable 1px border, delimiter-row alignment), images (hover payload), links styled + retained. Footnotes stay fallback. Source ranges ride every block (click→edit map). - ReadingSurface: read-only selectable NSTextView canvas (#68: a presentation of the same TextKit2 document). Tap with no selection after mouseDown returns = edit intent at the clicked block (the tracking-loop fix); ⌘-click opens links; scroll tracking feeds the flip-back caret; image hover NSPopover loads off-main. - ProseTypography (#70): three role slots — Body / Headings / Code — each {face, size}, UserDefaults six values, member-based font resolution (family+weight lookup misresolves Concourse Book→Thin), silent system fallback; both prose surfaces read them; editing presenter's headings + dimmed syntax rewired through the slots. - PostEditorView: Read⇄Edit toolbar button (far left) + View menu "Toggle Reading Mode" + ⌘R; reading state transient per open; drafts included (draft mirror); flip-back caret hint applied once per token. - SettingsPaneView: Typography section — three slot rows (family picker incl. current face not in the installed list + size field). - kitchen-sink.md (+png): corpus piece exercising constructs absent from the eight posts, per #69. Verified: 94 unit tests (renderer classification, renumbering, source ranges, fallbacks) + UI smoke; PERF-3k renders 128 blocks in ~17 ms; live-app probe — Read renders kitchen-sink markers-hidden, renumbered, byte-stable file after full round-trip; click-to-edit flips at the clicked paragraph; typography ladder pixel-verified (20/15 ink px).
149 lines
5.0 KiB
Swift
149 lines
5.0 KiB
Swift
//
|
|
// 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
|
|
|
|
/// 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,
|
|
])
|
|
}
|
|
|
|
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: 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
|
|
}
|
|
}
|