diff --git a/Museion/PostEditorView.swift b/Museion/PostEditorView.swift index b6fcf21..a87cb00 100644 --- a/Museion/PostEditorView.swift +++ b/Museion/PostEditorView.swift @@ -282,7 +282,7 @@ struct ProseTextView: NSViewRepresentable { // width — the fixed 640pt measure clipped in narrower columns. tv.textContainer?.containerSize = NSSize(width: 0, height: CGFloat.greatestFiniteMagnitude) tv.textContainer?.widthTracksTextView = true - tv.textContainer?.lineFragmentPadding = 12 + tv.textContainer?.lineFragmentPadding = 24 tv.textContainerInset = NSSize(width: 0, height: 20) let scroll = NSScrollView() scroll.documentView = tv diff --git a/Museion/ReadingPresenter.swift b/Museion/ReadingPresenter.swift index bee19bb..a1744cf 100644 --- a/Museion/ReadingPresenter.swift +++ b/Museion/ReadingPresenter.swift @@ -19,6 +19,11 @@ import AppKit /// Reading-mode presentation: text → rendered blocks. Pure over its input; /// called on the main actor (NSTextTable construction is MainActor). enum ReadingPresenter { + /// Vertical rhythm (user measurements, #71 polish): line-height ≈1.25, + /// 1 blank line after paragraphs, 2 after headings. + private static let lineHeightMultiple: CGFloat = 1.25 + private static let paragraphSpacingAfter = lineHeightMultiple // 1 line + private static let headingSpacingAfter = lineHeightMultiple * 2 // 2 lines /// One rendered block. `sourceRange` is the block's UTF-16 range in the /// source text (delimiter-exclusive) — the #68 click→edit map. struct Block { @@ -63,20 +68,16 @@ enum ReadingPresenter { static func document(for text: String, resolvingImagesIn postFolder: URL?) -> Document { let out = NSMutableAttributedString() var bounds: [Bound] = [] - for block in blocks(for: text) { + for block in blocks(for: text, resolvingImagesIn: postFolder) { if out.length > 0, !out.string.hasSuffix("\n") { out.append(NSAttributedString(string: "\n", attributes: ProseTypography.bodyAttributes)) } let start = out.length out.append(block.content) - let image = block.imageURL.flatMap { raw -> URL? in - guard raw.scheme == nil, let postFolder else { return raw } - return postFolder.appendingPathComponent(raw.path) - } bounds.append(Bound( docRange: NSRange(location: start, length: out.length - start), sourceRange: block.sourceRange, - imageURL: image, + imageURL: block.imageURL, decoration: block.decoration, )) } @@ -87,7 +88,8 @@ enum ReadingPresenter { /// Render `text` into reading blocks: blank-line split, one block per /// chunk (a run of list items or quote lines renders as one block). - static func blocks(for text: String) -> [Block] { + /// `postFolder` resolves relative image URLs before the attachment loads. + static func blocks(for text: String, resolvingImagesIn postFolder: URL?) -> [Block] { var out: [Block] = [] let ns = text as NSString var loc = 0 @@ -98,7 +100,7 @@ enum ReadingPresenter { if end > loc { let chunkRange = NSRange(location: loc, length: end - loc) let chunk = ns.substring(with: chunkRange) - out.append(block(chunk: chunk, sourceRange: chunkRange)) + out.append(block(chunk: chunk, sourceRange: chunkRange, postFolder: postFolder)) } loc = delim.location == NSNotFound ? ns.length : delim.location + 2 } @@ -106,9 +108,15 @@ enum ReadingPresenter { } /// Classify one chunk and render it. - private static func block(chunk: String, sourceRange: NSRange) -> Block { - if let url = imageURL(in: chunk) { - return Block(content: inlineParse(chunk), sourceRange: sourceRange, imageURL: url) + private static func block(chunk: String, sourceRange: NSRange, postFolder: URL?) -> Block { + if let raw = imageURL(in: chunk) { + let url = (raw.scheme == nil && raw.path.count > 0) + ? postFolder?.appendingPathComponent(raw.path) ?? raw : raw + return Block( + content: imageBlockContent(url: url, alt: altText(in: chunk)), + sourceRange: sourceRange, + imageURL: url, + ) } let lines = chunk.split(separator: "\n", omittingEmptySubsequences: false).map(String.init) guard let first = lines.first else { @@ -133,6 +141,35 @@ enum ReadingPresenter { // MARK: Flow blocks + /// Image block: the picture inline (scaled to the column), alt as the + /// caption below, link preserved for ⌘-click. Fails soft to alt-text + /// paragraph when the file can't load. + private static func imageBlockContent(url: URL, alt: String) -> NSAttributedString { + let ps = NSMutableParagraphStyle() + ps.paragraphSpacing = 10 + ps.paragraphSpacingBefore = 8 + guard let image = NSImage(contentsOf: url), image.size.width > 0 else { + return inlineParse(alt.isEmpty ? "![missing](\(url.lastPathComponent))" : alt) + } + let attachment = NSTextAttachment() + attachment.image = image + let maxW: CGFloat = 480 + if image.size.width > maxW { + attachment.image?.size = NSSize(width: maxW, height: maxW * image.size.height / image.size.width) + } + let cell = NSMutableAttributedString(attachment: attachment, attributes: [ + .paragraphStyle: ps, + ]) + if !alt.isEmpty { + cell.append(NSAttributedString(string: "\n" + alt, attributes: [ + .font: ProseTypography.codeFont, + .foregroundColor: ProseTypography.syntaxColor, + .paragraphStyle: ps, + ])) + } + return cell + } + private static func flowContent(_ lines: [String], first: String) -> NSAttributedString { if let level = headingLevel(first) { let body = first.drop { $0 == "#" }.trimmingCharacters(in: .whitespaces) @@ -178,7 +215,8 @@ enum ReadingPresenter { default: ProseTypography.bodyFont.pointSize } let ps = NSMutableParagraphStyle() - ps.paragraphSpacing = level <= 2 ? 10 : 8 + ps.lineHeightMultiple = lineHeightMultiple + ps.paragraphSpacing = headingSpacingAfter ps.paragraphSpacingBefore = level == 1 ? 26 : (level == 2 ? 20 : 14) return NSAttributedString(string: text, attributes: [ .font: ProseTypography.headingFont(size: size), @@ -189,7 +227,8 @@ enum ReadingPresenter { private static func codeContent(_ code: String) -> NSAttributedString { let ps = NSMutableParagraphStyle() - ps.paragraphSpacing = 10 + ps.lineHeightMultiple = lineHeightMultiple + ps.paragraphSpacing = paragraphSpacingAfter ps.paragraphSpacingBefore = 6 ps.firstLineHeadIndent = 12 ps.headIndent = 12 @@ -203,6 +242,7 @@ enum ReadingPresenter { private static func hrContent() -> NSAttributedString { // Hairline drawn by the canvas (Decoration.rule). let ps = NSMutableParagraphStyle() + ps.lineHeightMultiple = lineHeightMultiple ps.paragraphSpacingBefore = 8 ps.paragraphSpacing = 8 return NSAttributedString(string: " ", attributes: [ @@ -223,6 +263,7 @@ enum ReadingPresenter { guard let marker = listItemMarker(line) else { continue } let indent: CGFloat = 16 + CGFloat(marker.level - 1) * 18 let ps = NSMutableParagraphStyle() + ps.lineHeightMultiple = lineHeightMultiple ps.firstLineHeadIndent = indent ps.headIndent = indent + 18 ps.paragraphSpacing = 5 @@ -258,6 +299,7 @@ enum ReadingPresenter { let depth = blockquoteDepth(line) let indent = CGFloat(6 + (depth - 1) * 18) let ps = NSMutableParagraphStyle() + ps.lineHeightMultiple = lineHeightMultiple ps.firstLineHeadIndent = indent ps.headIndent = indent ps.paragraphSpacing = 4 @@ -300,7 +342,7 @@ enum ReadingPresenter { widths[column] = max(widths[column], cell.count) } } - let pad = 2 + let pad = 3 let codeAttrs: [NSAttributedString.Key: Any] = [ .font: ProseTypography.codeFont, .foregroundColor: ProseTypography.foregroundColor, @@ -360,7 +402,7 @@ enum ReadingPresenter { var running = 0.0 for width in widths { offsets.append(running) - running += CGFloat(width + 4) * advance + running += CGFloat(width + 6) * advance // cell pad 3 + 3, matches gridLine } return .table(columns: offsets + [running]) } @@ -428,6 +470,13 @@ enum ReadingPresenter { return out } + /// Alt text of an `![alt](url)` chunk (empty when absent). + private static func altText(in chunk: String) -> String { + let trimmed = chunk.trimmingCharacters(in: .whitespacesAndNewlines) + guard trimmed.hasPrefix("!["), let closeBracket = trimmed.firstIndex(of: "]") else { return "" } + return String(trimmed[trimmed.index(trimmed.startIndex, offsetBy: 2) ..< closeBracket]) + } + // MARK: Construct matchers private struct ListMarker { diff --git a/Museion/ReadingSurface.swift b/Museion/ReadingSurface.swift index 7148c32..6cc9e47 100644 --- a/Museion/ReadingSurface.swift +++ b/Museion/ReadingSurface.swift @@ -68,8 +68,8 @@ struct ReadingSurface: NSViewRepresentable { // view width — a fixed 640pt measure clipped in narrower columns. tv.textContainer?.containerSize = NSSize(width: 0, height: CGFloat.greatestFiniteMagnitude) tv.textContainer?.widthTracksTextView = true - tv.textContainer?.lineFragmentPadding = 12 - tv.textContainerInset = NSSize(width: 0, height: 20) + tv.textContainer?.lineFragmentPadding = 24 + tv.textContainerInset = NSSize(width: 0, height: 24) let scroll = NSScrollView() scroll.documentView = tv scroll.hasVerticalScroller = true