Sources/OrgEditorAppKit/OrgEditor+Latex.swift
62 lines · 4071 bytes
1#if os(macOS)
2import AppKit
3import OrgCore
4import OrgPresentation
5
6extension OrgEditor {
7 // MARK: - LaTeX previews
8
9 /// `org-latex-preview` over the whole buffer: fragments and environments show as typeset
10 /// math, except on the caret's line.
11 func toggleLatex() {
12 showsLatex.toggle()
13 refreshImages()
14 if showsLatex {
15 let count = LatexPreview.fragments(document.tree, in: 0..<utf16Length).count
16 onMessage?("\(count) LaTeX \(count == 1 ? "fragment" : "fragments") previewed")
17 } else {
18 onMessage?("LaTeX previews removed")
19 }
20 }
21
22 /// Each fragment's text hidden, as wide as its image, which `LatexFragment` draws; an
23 /// environment's image covers its lines.
24 func applyLatex(_ range: Range<Int>, in storage: NSTextStorage, revealed: NSRange) {
25 guard showsLatex, isOrg else { return }
26 let text = storage.string as NSString
27 let scale = textView.window?.backingScaleFactor ?? 2
28 let lineHeight = theme.font.ascender - theme.font.descender + theme.font.leading
29 // The image is drawn in the appearance's color of the moment.
30 var color = theme.foreground
31 textView.effectiveAppearance.performAsCurrentDrawingAppearance { color = theme.foreground.usingColorSpace(.sRGB) ?? .textColor }
32 for fragment in LatexPreview.fragments(document.tree, in: range) {
33 let r = NSRange(fragment.range)
34 guard r.length > 0, NSMaxRange(r) <= text.length, shaded.containing(r.location) == nil else { continue }
35 let lines = text.lineRange(for: r)
36 if NSLocationInRange(revealed.location, lines) || NSIntersectionRange(lines, revealed).length > 0 { continue }
37 guard let rendering = LatexPreview.render(fragment.latex, display: fragment.display, fontSize: theme.font.pointSize,
38 color: color, scale: scale) else { continue }
39 // Pretty entities in the source would draw over the image.
40 storage.removeAttribute(.orgEntityDisplay, range: r)
41 storage.addAttribute(.orgLatex, value: LatexBox(rendering, environment: fragment.environment), range: NSRange(location: r.location, length: 1))
42 if fragment.environment {
43 // The lines keep their height, under the image; a taller image makes room after them.
44 storage.addAttributes([.foregroundColor: NSColor.clear, .underlineStyle: 0], range: r)
45 let last = text.lineRange(for: NSRange(location: NSMaxRange(r) - 1, length: 0))
46 let count = text.substring(with: lines).components(separatedBy: "\n").count - (text.substring(with: lines).hasSuffix("\n") ? 1 : 0)
47 let style = (storage.attribute(.paragraphStyle, at: last.location, effectiveRange: nil) as? NSParagraphStyle)?.mutableCopy() as? NSMutableParagraphStyle ?? NSMutableParagraphStyle()
48 style.paragraphSpacing = max(style.paragraphSpacing, rendering.size.height + 4 - CGFloat(count) * (lineHeight + style.lineSpacing))
49 storage.addAttribute(.paragraphStyle, value: style, range: last)
50 } else {
51 storage.addAttributes(theme.hidden, range: r)
52 let hiddenWidth = text.substring(with: r).size(withAttributes: [.font: theme.hidden[.font] as! NSFont]).width
53 storage.addAttribute(.kern, value: max(0, rendering.size.width - hiddenWidth), range: NSRange(location: NSMaxRange(r) - 1, length: 1))
54 let line = text.lineRange(for: NSRange(location: r.location, length: 0))
55 let style = (storage.attribute(.paragraphStyle, at: line.location, effectiveRange: nil) as? NSParagraphStyle)?.mutableCopy() as? NSMutableParagraphStyle ?? NSMutableParagraphStyle()
56 style.minimumLineHeight = max(style.minimumLineHeight, lineHeight, rendering.ascent + rendering.descent + 2)
57 storage.addAttribute(.paragraphStyle, value: style, range: line)
58 }
59 }
60 }
61}
62#endif