oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1import Foundation
2#if os(macOS)
3import AppKit
4#else
5import UIKit
6#endif
7
8/// Personal layout only: never adds characters or attributes to CRDT content.
9/// Letter paper uses the exporter's 612 × 792 point geometry and 72 point margins.
10/// Narrow screens reflow text with smaller side margins, like the web editor.
11struct DocsPageGeometry: Equatable, Sendable {
12 var viewportWidth: CGFloat
13 var paged: Bool
14 static let paperHeight: CGFloat = 792
15 static let verticalMargin: CGFloat = 72
16 static let gap: CGFloat = 24
17 var paperWidth: CGFloat {
18 #if os(macOS)
19 612
20 #else
21 min(612, max(160, viewportWidth - 24))
22 #endif
23 }
24 var paperX: CGFloat { max(0, (viewportWidth - paperWidth) / 2) }
25 var sideMargin: CGFloat { paperWidth < 480 ? 20 : 72 }
26 var textX: CGFloat { paperX + sideMargin }
27 var textWidth: CGFloat { paperWidth - sideMargin * 2 }
28 var stride: CGFloat { Self.paperHeight + Self.gap }
29 var bodyHeight: CGFloat { Self.paperHeight - Self.verticalMargin * 2 }
30 func page(at y: CGFloat) -> Int { paged ? max(0, Int(floor(max(0, y) / stride))) : 0 }
31 func pageRect(_ index: Int) -> CGRect {
32 CGRect(x: paperX, y: CGFloat(index) * stride, width: paperWidth, height: Self.paperHeight)
33 }
34 func lineRect(_ proposal: CGRect) -> CGRect {
35 var rect = proposal
36 rect.origin.x = textX + proposal.minX
37 rect.size.width = max(0, min(proposal.width, textWidth - proposal.minX))
38 if !paged {
39 rect.origin.y = max(Self.verticalMargin, proposal.minY)
40 return rect
41 }
42 let index = page(at: proposal.minY)
43 let top = CGFloat(index) * stride + Self.verticalMargin
44 let bottom = top + bodyHeight
45 rect.origin.y = max(top, proposal.minY)
46 // Oversized attachment lines stay on one page instead of causing a loop.
47 if rect.height <= bodyHeight && rect.maxY > bottom {
48 rect.origin.y = CGFloat(index + 1) * stride + Self.verticalMargin
49 }
50 return rect
51 }
52 func documentHeight(usedHeight: CGFloat) -> CGFloat {
53 if !paged { return max(Self.paperHeight, usedHeight + Self.verticalMargin) }
54 return CGFloat(page(at: max(Self.verticalMargin, usedHeight - 1)) + 1) * stride - Self.gap
55 }
56}
57
58final class DocsPageTextContainer: NSTextContainer {
59 #if os(macOS)
60 /// The Mac's page breaks (spec §7, "Incremental pagination" 5): the
61 /// absolute top of a block that starts a page, looked up by the
62 /// character its first line starts at, and taken as `max(proposed,
63 /// top)`: TextKit proposing one line twice gets the same answer. Changes
64 /// never call `textContainerChangedGeometry`: `DocsPagination`
65 /// invalidates the live layout from the first block that moved.
66 weak var tops: (any DocsPageTops)?
67 #endif
68 var geometry = DocsPageGeometry(viewportWidth: 612, paged: false) {
69 didSet {
70 if oldValue != geometry { layoutManager?.textContainerChangedGeometry(self) }
71 }
72 }
73 /// A layout stack that measures and never draws (probe counts only).
74 var isMeasurement = false
75 override var isSimpleRectangularTextContainer: Bool { false }
76 override func lineFragmentRect(
77 forProposedRect proposedRect: CGRect, at characterIndex: Int,
78 writingDirection baseWritingDirection: NSWritingDirection,
79 remaining remainingRect: UnsafeMutablePointer<CGRect>?
80 ) -> CGRect {
81 #if DOCS_PROBES
82 if isMeasurement { DocsProbes.measureLineRequests += 1 } else { DocsProbes.lineRequests += 1 }
83 #endif
84 remainingRect?.pointee = .zero
85 var rect = geometry.lineRect(proposedRect)
86 #if os(macOS)
87 if let top = tops?.top(at: characterIndex) { rect.origin.y = max(rect.minY, top) }
88 #endif
89 if let storage = layoutManager?.textStorage, characterIndex < storage.length {
90 if characterIndex == 0, let margin = storage.attribute(.docsFirstMargin, at: 0, effectiveRange: nil) as? CGFloat {
91 rect.origin.y = max(rect.minY, DocsPageGeometry.verticalMargin + margin)
92 }
93 var range = NSRange()
94 if let padding = storage.attribute(.docsCodeStart, at: characterIndex, effectiveRange: &range) as? CGFloat,
95 range.location == characterIndex {
96 rect.origin.x += padding
97 rect.size.width = max(0, rect.width - padding)
98 }
99 }
100 return rect
101 }
102}
103