oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1#if os(macOS)
2import AppKit
3
4extension NSAttributedString.Key {
5 static let docsBaseFont = NSAttributedString.Key("OficinaDocsBaseFont")
6 static let docsLeading = NSAttributedString.Key("OficinaDocsLeading")
7}
8
9/// CSS line boxes include the paragraph's font strut and each inline run's
10/// half-leading. TextKit's single paragraph multiplier cannot express that when
11/// a line mixes font families or sizes.
12@MainActor final class DocsLayoutManager: NSLayoutManager, @preconcurrency NSLayoutManagerDelegate {
13 override func processEditing(for textStorage: NSTextStorage, edited editMask: NSTextStorageEditActions,
14 range newCharRange: NSRange, changeInLength delta: Int, invalidatedRange invalidatedCharRange: NSRange) {
15 super.processEditing(for: textStorage, edited: editMask, range: newCharRange,
16 changeInLength: delta, invalidatedRange: invalidatedCharRange)
17 for container in textContainers {
18 (container.textView as? DocsPagedTextView)?.storageEdited(edited: newCharRange, delta: delta)
19 }
20 }
21 override init() { super.init(); usesFontLeading = false; delegate = self }
22 required init?(coder: NSCoder) { fatalError("init(coder:) is unavailable") }
23 override func drawBackground(forGlyphRange glyphsToShow: NSRange, at origin: NSPoint) {
24 super.drawBackground(forGlyphRange: glyphsToShow, at: origin)
25 guard let storage = textStorage, let container = textContainers.first as? DocsPageTextContainer else { return }
26 let characters = characterRange(forGlyphRange: glyphsToShow, actualGlyphRange: nil)
27 storage.enumerateAttribute(.docsQuoteBorders, in: characters) { value, range, _ in
28 guard let borders = value as? [CGFloat] else { return }
29 let glyphs = self.glyphRange(forCharacterRange: range, actualCharacterRange: nil)
30 var bounds = self.boundingRect(forGlyphRange: glyphs, in: container)
31 bounds.origin.y += origin.y
32 NSColor(white: 200.0 / 255, alpha: 1).setFill()
33 for inset in borders {
34 CGRect(x: origin.x + container.geometry.textX + inset, y: bounds.minY, width: 2.25, height: bounds.height).fill()
35 }
36 }
37 }
38 func layoutManager(_ layoutManager: NSLayoutManager, shouldSetLineFragmentRect lineFragmentRect: UnsafeMutablePointer<NSRect>,
39 lineFragmentUsedRect: UnsafeMutablePointer<NSRect>, baselineOffset: UnsafeMutablePointer<CGFloat>,
40 in textContainer: NSTextContainer, forGlyphRange glyphRange: NSRange) -> Bool {
41 guard let storage = textStorage, glyphRange.length > 0 else { return false }
42 let range = characterRange(forGlyphRange: glyphRange, actualGlyphRange: nil)
43 guard range.location < storage.length,
44 let base = storage.attribute(.docsBaseFont, at: range.location, effectiveRange: nil) as? NSFont,
45 let leading = storage.attribute(.docsLeading, at: range.location, effectiveRange: nil) as? CGFloat else { return false }
46 var above: CGFloat = 0, below: CGFloat = 0
47 func include(_ font: NSFont) {
48 let halfLeading = (font.pointSize * leading - font.ascender + font.descender) / 2
49 above = max(above, font.ascender + halfLeading)
50 below = max(below, -font.descender + halfLeading)
51 }
52 include(base)
53 var attachment = false
54 storage.enumerateAttributes(in: range) { attributes, _, _ in
55 if let font = attributes[.font] as? NSFont { include(font) }
56 if attributes[.attachment] != nil { attachment = true }
57 }
58 guard !attachment else { return false }
59 let end = NSMaxRange(range)
60 let endsParagraph = end > 0 && (storage.string as NSString).character(at: end - 1) == 10
61 let style = storage.attribute(.paragraphStyle, at: range.location, effectiveRange: nil) as? NSParagraphStyle
62 lineFragmentRect.pointee.size.height = above + below + (endsParagraph ? style?.paragraphSpacing ?? 0 : 0)
63 lineFragmentUsedRect.pointee.size.height = above + below
64 baselineOffset.pointee = above
65 return true
66 }
67}
68#endif
69