| 1 | #if os(macOS) |
| 2 | import AppKit |
| 3 | import XCTest |
| 4 | @testable import DocsKit |
| 5 | |
| 6 | final class DocumentTypographyTests: XCTestCase { |
| 7 | static let heading = "A shared document with consistent typography on every screen" |
| 8 | static 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." |
| 9 | static 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." |
| 10 | // Synthetic fixture, in Carlito, the default. BrowserTypographyTests' |
| 11 | // testTypographyFixtureIsWhatTheBrowserDraws measures the same blocks in |
| 12 | // WebKit with the bundled fonts and the production stylesheet, and holds |
| 13 | // browserLines to it. |
| 14 | @MainActor func testBrowserFixtureLineBreaks() throws { |
| 15 | let heading = Self.heading, bold = Self.bold, body = Self.body |
| 16 | func block(_ id: String, _ text: String, heading: Bool = false, strong: Bool = false) -> CRDTNode { |
| 17 | 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)])]) |
| 18 | } |
| 19 | let projection = CRDTProjection(children: [block("h", heading, heading: true), block("b", bold, strong: true), block("gap", ""), block("p", body)], canUndo: false, canRedo: false) |
| 20 | let storage = NSTextStorage(attributedString: NativeTextProjection.render(projection)) |
| 21 | let manager = NSLayoutManager() |
| 22 | manager.usesFontLeading = false |
| 23 | let container = DocsPageTextContainer(size: CGSize(width: 636, height: 100000)) |
| 24 | container.geometry = DocsPageGeometry(viewportWidth: 636, paged: false) |
| 25 | container.lineFragmentPadding = 0 |
| 26 | storage.addLayoutManager(manager); manager.addTextContainer(container) |
| 27 | manager.ensureLayout(for: container) |
| 28 | var lines: [String] = [] |
| 29 | var heights: [CGFloat] = [] |
| 30 | manager.enumerateLineFragments(forGlyphRange: NSRange(location: 0, length: manager.numberOfGlyphs)) { rect, _, _, range, _ in |
| 31 | let chars = manager.characterRange(forGlyphRange: range, actualGlyphRange: nil) |
| 32 | let line = (storage.string as NSString).substring(with: chars).trimmingCharacters(in: .whitespacesAndNewlines) |
| 33 | if !line.isEmpty { lines.append(line); heights.append(rect.height) } |
| 34 | } |
| 35 | XCTAssertEqual(heights[2], 16.8, accuracy: 0.01) |
| 36 | XCTAssertEqual(heights[3], 16.8, accuracy: 0.01) |
| 37 | XCTAssertEqual(lines, Self.browserLines) |
| 38 | } |
| 39 | static let browserLines: [String] = [ |
| 40 | "A shared document with consistent typography on every", |
| 41 | "screen", |
| 42 | "This is a synthetic layout fixture for the native and browser editors. The same bundled font,", |
| 43 | "page width and paragraph styles must produce the same line breaks, including a long", |
| 44 | "sentence that wraps across several lines.", |
| 45 | "The quick brown fox jumps over the lazy dog. A small change in font metrics can move the final", |
| 46 | "word onto another line, so this paragraph deliberately includes enough words to exercise", |
| 47 | "wrapping at a fixed document width.", |
| 48 | ]} |
| 49 | #endif |
| 50 | |