| 1 | #if os(macOS) |
| 2 | import AppKit |
| 3 | @testable import DocsKit |
| 4 | |
| 5 | /// The tests' reference for the Mac's page breaks (spec 2026-11-22 §7, |
| 6 | /// "Incremental pagination" 9): the measurement production ran on every |
| 7 | /// edit before S1b, moved here verbatim. Production's `DocsPagination` |
| 8 | /// must put breaks, line origins and the frame height where a fresh run of |
| 9 | /// this puts them. It differs from production's full path by construction: |
| 10 | /// a copy of the storage and a separate layout stack, laid out whole. |
| 11 | /// |
| 12 | /// Mirrors pages.mjs: keep top-level blocks intact and put a visual separator |
| 13 | /// before the next block exceeding the 648pt text budget. All offsets are layout |
| 14 | /// only; an oversized paragraph/list is never split or rewritten to make a page. |
| 15 | @MainActor enum DocumentPageLayoutOracle { |
| 16 | struct Result { |
| 17 | var starts: [Int: CGFloat] = [:] |
| 18 | var separators: [CGFloat] = [] |
| 19 | } |
| 20 | /// Mapping IDs, formatting marks and colors do not affect glyph positions. |
| 21 | /// Retain only layout inputs so metadata acknowledgements reuse pagination. |
| 22 | static func measurementContent(_ storage: NSAttributedString) -> NSAttributedString { |
| 23 | let keys: Set<NSAttributedString.Key> = [.font, .paragraphStyle, .kern, .ligature, .baselineOffset, |
| 24 | .attachment, .expansion, .obliqueness, .writingDirection, .verticalGlyphForm, |
| 25 | .superscript, .docsBaseFont, .docsLeading, .docsCodeStart, .docsFirstMargin, |
| 26 | .docsTopBlock, .docsMarginBefore, .docsMarginAfter] |
| 27 | let result = NSMutableAttributedString(string: storage.string) |
| 28 | storage.enumerateAttributes(in: NSRange(location: 0, length: storage.length)) { attributes, range, _ in |
| 29 | result.setAttributes(attributes.filter { keys.contains($0.key) }, range: range) |
| 30 | } |
| 31 | return result |
| 32 | } |
| 33 | static func measure(storage: NSTextStorage, manager: NSLayoutManager, container: NSTextContainer) -> Result { |
| 34 | var result = Result() |
| 35 | var pageTop: CGFloat? |
| 36 | var added: CGFloat = 0 |
| 37 | var previousMargin: CGFloat = 0 |
| 38 | storage.enumerateAttribute(.docsTopBlock, in: NSRange(location: 0, length: storage.length)) { value, range, _ in |
| 39 | guard value != nil else { return } |
| 40 | let glyphs = manager.glyphRange(forCharacterRange: range, actualCharacterRange: nil) |
| 41 | var top = CGFloat.greatestFiniteMagnitude |
| 42 | var bottom: CGFloat = 0 |
| 43 | manager.enumerateLineFragments(forGlyphRange: glyphs) { rect, used, _, _, _ in |
| 44 | top = min(top, rect.minY); bottom = max(bottom, used.maxY) |
| 45 | } |
| 46 | guard bottom > top else { return } |
| 47 | let before = storage.attribute(.docsMarginBefore, at: range.location, effectiveRange: nil) as? CGFloat ?? 0 |
| 48 | let after = storage.attribute(.docsMarginAfter, at: range.location, effectiveRange: nil) as? CGFloat ?? 0 |
| 49 | defer { previousMargin = after } |
| 50 | if let currentTop = pageTop { |
| 51 | if bottom - currentTop > 648 { |
| 52 | // Collapse margins independently on either side of the gap. |
| 53 | let collapsed = max(previousMargin, before) |
| 54 | let separator = top + added - collapsed + max(previousMargin, 12) |
| 55 | let extra = max(previousMargin, 12) + 18 + max(before, 12) - collapsed |
| 56 | added += extra |
| 57 | result.starts[range.location] = top + added |
| 58 | result.separators.append(separator) |
| 59 | pageTop = top |
| 60 | } |
| 61 | } else { pageTop = top } |
| 62 | } |
| 63 | return result |
| 64 | } |
| 65 | } |
| 66 | #endif |
| 67 | |
| 68 | #if os(macOS) |
| 69 | /// The oracle's breaks as a container's lookup, as production's |
| 70 | /// `blockStarts` table read them before S1b: a top for the character a |
| 71 | /// break's block starts at. |
| 72 | final class OracleTops: DocsPageTops, Sendable { |
| 73 | let starts: [Int: CGFloat] |
| 74 | init(_ starts: [Int: CGFloat]) { self.starts = starts } |
| 75 | nonisolated func top(at characterIndex: Int) -> CGFloat? { starts[characterIndex] } |
| 76 | } |
| 77 | #endif |
| 78 | |