#if os(macOS) import AppKit @testable import DocsKit /// The tests' reference for the Mac's page breaks (spec 2026-11-22 ยง7, /// "Incremental pagination" 9): the measurement production ran on every /// edit before S1b, moved here verbatim. Production's `DocsPagination` /// must put breaks, line origins and the frame height where a fresh run of /// this puts them. It differs from production's full path by construction: /// a copy of the storage and a separate layout stack, laid out whole. /// /// Mirrors pages.mjs: keep top-level blocks intact and put a visual separator /// before the next block exceeding the 648pt text budget. All offsets are layout /// only; an oversized paragraph/list is never split or rewritten to make a page. @MainActor enum DocumentPageLayoutOracle { struct Result { var starts: [Int: CGFloat] = [:] var separators: [CGFloat] = [] } /// Mapping IDs, formatting marks and colors do not affect glyph positions. /// Retain only layout inputs so metadata acknowledgements reuse pagination. static func measurementContent(_ storage: NSAttributedString) -> NSAttributedString { let keys: Set = [.font, .paragraphStyle, .kern, .ligature, .baselineOffset, .attachment, .expansion, .obliqueness, .writingDirection, .verticalGlyphForm, .superscript, .docsBaseFont, .docsLeading, .docsCodeStart, .docsFirstMargin, .docsTopBlock, .docsMarginBefore, .docsMarginAfter] let result = NSMutableAttributedString(string: storage.string) storage.enumerateAttributes(in: NSRange(location: 0, length: storage.length)) { attributes, range, _ in result.setAttributes(attributes.filter { keys.contains($0.key) }, range: range) } return result } static func measure(storage: NSTextStorage, manager: NSLayoutManager, container: NSTextContainer) -> Result { var result = Result() var pageTop: CGFloat? var added: CGFloat = 0 var previousMargin: CGFloat = 0 storage.enumerateAttribute(.docsTopBlock, in: NSRange(location: 0, length: storage.length)) { value, range, _ in guard value != nil else { return } let glyphs = manager.glyphRange(forCharacterRange: range, actualCharacterRange: nil) var top = CGFloat.greatestFiniteMagnitude var bottom: CGFloat = 0 manager.enumerateLineFragments(forGlyphRange: glyphs) { rect, used, _, _, _ in top = min(top, rect.minY); bottom = max(bottom, used.maxY) } guard bottom > top else { return } let before = storage.attribute(.docsMarginBefore, at: range.location, effectiveRange: nil) as? CGFloat ?? 0 let after = storage.attribute(.docsMarginAfter, at: range.location, effectiveRange: nil) as? CGFloat ?? 0 defer { previousMargin = after } if let currentTop = pageTop { if bottom - currentTop > 648 { // Collapse margins independently on either side of the gap. let collapsed = max(previousMargin, before) let separator = top + added - collapsed + max(previousMargin, 12) let extra = max(previousMargin, 12) + 18 + max(before, 12) - collapsed added += extra result.starts[range.location] = top + added result.separators.append(separator) pageTop = top } } else { pageTop = top } } return result } } #endif #if os(macOS) /// The oracle's breaks as a container's lookup, as production's /// `blockStarts` table read them before S1b: a top for the character a /// break's block starts at. final class OracleTops: DocsPageTops, Sendable { let starts: [Int: CGFloat] init(_ starts: [Int: CGFloat]) { self.starts = starts } nonisolated func top(at characterIndex: Int) -> CGFloat? { starts[characterIndex] } } #endif