#if os(macOS) import AppKit import XCTest @testable import DocsKit final class DocumentTypographyTests: XCTestCase { // Synthetic fixture also measured in WebKit at 96 CSS px/in with the same // bundled Liberation fonts and the production [docs-text] CSS rules. @MainActor func testBrowserFixtureLineBreaks() throws { let heading = "A shared document with consistent typography on every screen" let bold = "This is a synthetic layout fixture for the native and browser editors. The same bundled font, page width and paragraph styles must produce the same line breaks, including a long sentence that wraps across several lines." let body = "The quick brown fox jumps over the lazy dog. A small change in font metrics can move the final word onto another line, so this paragraph deliberately includes enough words to exercise wrapping at a fixed document width." func block(_ id: String, _ text: String, heading: Bool = false, strong: Bool = false) -> CRDTNode { CRDTNode(id: id, type: heading ? "heading" : "paragraph", attrs: heading ? ["level": "1"] : nil, children: [CRDTNode(id: id + "t", type: "text", runs: [CRDTRun(insert: text, marks: strong ? ["strong": .object([:])] : nil)])]) } let projection = CRDTProjection(children: [block("h", heading, heading: true), block("b", bold, strong: true), block("gap", ""), block("p", body)], canUndo: false, canRedo: false) let storage = NSTextStorage(attributedString: NativeTextProjection.render(projection)) let manager = NSLayoutManager() manager.usesFontLeading = false let container = DocsPageTextContainer(size: CGSize(width: 636, height: 100000)) container.geometry = DocsPageGeometry(viewportWidth: 636, paged: false) container.lineFragmentPadding = 0 storage.addLayoutManager(manager); manager.addTextContainer(container) manager.ensureLayout(for: container) var lines: [String] = [] var heights: [CGFloat] = [] manager.enumerateLineFragments(forGlyphRange: NSRange(location: 0, length: manager.numberOfGlyphs)) { rect, _, _, range, _ in let chars = manager.characterRange(forGlyphRange: range, actualGlyphRange: nil) let line = (storage.string as NSString).substring(with: chars).trimmingCharacters(in: .whitespacesAndNewlines) if !line.isEmpty { lines.append(line); heights.append(rect.height) } } XCTAssertEqual(heights[2], 16.8, accuracy: 0.01) XCTAssertEqual(heights[3], 16.8, accuracy: 0.01) XCTAssertEqual(lines, Self.browserLines) } private static let browserLines: [String] = [ "A shared document with consistent typography on", "every screen", "This is a synthetic layout fixture for the native and browser editors. The same", "bundled font, page width and paragraph styles must produce the same line", "breaks, including a long sentence that wraps across several lines.", "The quick brown fox jumps over the lazy dog. A small change in font metrics can move", "the final word onto another line, so this paragraph deliberately includes enough words to", "exercise wrapping at a fixed document width." ] } #endif