Sources/OrgstarMobile/MobileEditor+Latex.swift

design-themes-widgets
orgstar/Sources/OrgstarMobile/MobileEditor+Latex.swift history · blame · raw

61 lines · 3856 bytes

 1#if os(iOS)
 2import OrgCore
 3import OrgPresentation
 4import UIKit
 5
 6extension MobileEditor {
 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        restyleAll()
14        if showsLatex {
15            let count = LatexPreview.fragments(document.tree, in: 0..<textView.textStorage.length).count
16            onMessage?("\(count) LaTeX \(count == 1 ? "fragment" : "fragments") previewed")
17        } else {
18            onMessage?("LaTeX previews removed")
19        }
20    }
21
22    /// As the Mac editor: each fragment hidden and as wide as its image, which
23    /// `OrgLineFragment` draws; an environment's image covers its lines.
24    func applyLatex(_ range: Range<Int>, in storage: NSTextStorage, revealed: NSRange) {
25        guard showsLatex else { return }
26        let text = storage.string as NSString
27        let scale = textView.traitCollection.displayScale
28        let color = theme.foreground.resolvedColor(with: textView.traitCollection)
29        let lineHeight = theme.font.lineHeight
30        for fragment in LatexPreview.fragments(document.tree, in: range) {
31            let r = NSRange(fragment.range)
32            guard r.length > 0, NSMaxRange(r) <= text.length, shaded.containing(r.location) == nil else { continue }
33            let lines = text.lineRange(for: r)
34            if NSLocationInRange(revealed.location, lines) || NSIntersectionRange(lines, revealed).length > 0 { continue }
35            guard let rendering = LatexPreview.render(fragment.latex, display: fragment.display, fontSize: theme.font.pointSize,
36                                                      color: color, scale: scale) else { continue }
37            // Pretty entities in the source would draw over the image.
38            storage.removeAttribute(.orgEntityDisplay, range: r)
39            storage.removeAttribute(.orgEntityColor, range: r)
40            storage.addAttribute(.orgLatex, value: LatexBox(rendering, environment: fragment.environment), range: NSRange(location: r.location, length: 1))
41            if fragment.environment {
42                storage.addAttributes([.foregroundColor: UIColor.clear, .underlineStyle: 0], range: r)
43                let last = text.lineRange(for: NSRange(location: NSMaxRange(r) - 1, length: 0))
44                let body = text.substring(with: lines)
45                let count = body.components(separatedBy: "\n").count - (body.hasSuffix("\n") ? 1 : 0)
46                let style = (storage.attribute(.paragraphStyle, at: last.location, effectiveRange: nil) as? NSParagraphStyle)?.mutableCopy() as? NSMutableParagraphStyle ?? NSMutableParagraphStyle()
47                style.paragraphSpacing = max(style.paragraphSpacing, rendering.size.height + 4 - CGFloat(count) * (lineHeight + style.lineSpacing))
48                storage.addAttribute(.paragraphStyle, value: style, range: last)
49            } else {
50                storage.addAttributes(theme.hidden, range: r)
51                let hiddenWidth = text.substring(with: r).size(withAttributes: [.font: theme.hidden[.font] as! UIFont]).width
52                storage.addAttribute(.kern, value: max(0, rendering.size.width - hiddenWidth), range: NSRange(location: NSMaxRange(r) - 1, length: 1))
53                let line = text.lineRange(for: NSRange(location: r.location, length: 0))
54                let style = (storage.attribute(.paragraphStyle, at: line.location, effectiveRange: nil) as? NSParagraphStyle)?.mutableCopy() as? NSMutableParagraphStyle ?? NSMutableParagraphStyle()
55                style.minimumLineHeight = max(style.minimumLineHeight, lineHeight, rendering.ascent + rendering.descent + 2)
56                storage.addAttribute(.paragraphStyle, value: style, range: line)
57            }
58        }
59    }
60}
61#endif