Repository Public License
1// ORIX Convert — Packages/EnginePDF/Tests/EnginePDFTests/OCRAndDocxTests.swift2// Description: Searchable PDF (invisible text layer positions, structure preservation) and PDF → DOCX / RTF.3// Author: Simon-Pierre Boucher — contact@spboucher.ai4import Foundation5import AppKit6import CoreGraphics7import PDFKit8import Testing9import ConvertCore10@testable import EnginePDF1112/// Runs a body that needs Vision; an unavailable recogniser is reported as a known issue instead of a failure.13func withVision(_ body: () async throws -> Void) async throws {14 do { try await body() } catch ConversionError.engineUnavailable(let reason) {15 withKnownIssue("Vision text recognition unavailable: \(reason)") { Issue.record("skipped") }16 }17}1819extension PDFEngineTests {20 @Suite struct SearchablePDF {21 /// Word boxes of the invisible layer against the boxes the fixture drew, on an upright page, a /Rotate 9022 /// page and a page whose media box is offset with a smaller crop box.23 @Test(arguments: [150.0, 300.0])24 func invisibleLayerMatchesTheScan(dpi: Double) async throws {25 try await withVision {26 let pdf = Fx.scannedPDF("scan-\(Int(dpi)).pdf", dpi: dpi, variants: true)27 let result = try await Fx.run(Fx.request(pdf, to: .pdf, op: PDFEngine.Operation.ocr))28 #expect(result.output.facts["ocr.pages"] == "3")29 let source = try #require(PDFDocument(url: pdf)), output = try #require(PDFDocument(url: result.file))30 #expect(output.pageCount == 3)31 let expected = Fx.scanProbeRects()32 var worst: CGFloat = 0, worstX: CGFloat = 0, worstY: CGFloat = 033 for index in 0..<3 {34 let page = try #require(output.page(at: index)), original = try #require(source.page(at: index))35 #expect(page.rotation == original.rotation)36 #expect(page.bounds(for: .mediaBox) == original.bounds(for: .mediaBox))37 #expect(page.bounds(for: .cropBox) == original.bounds(for: .cropBox))38 let text = page.string ?? ""39 for probe in Fx.scanProbes {40 #expect(text.contains(probe), "page \(index + 1) lacks \(probe) at \(Int(dpi)) dpi: \(text)")41 let selection = try #require(output.findString(probe, withOptions: []).first { $0.pages.first == page }, "no selection for \(probe) on page \(index + 1)")42 let found = selection.bounds(for: page), want = Fx.expectedRect(expected[probe]!, page: index)43 let overlap = found.intersection(want)44 let covered = overlap.isNull ? 0 : (overlap.width * overlap.height) / (want.width * want.height)45 #expect(covered > 0.6, "\(probe) page \(index + 1): found \(found), expected \(want)")46 let drift = max(abs(found.midX - want.midX), abs(found.midY - want.midY))47 worst = max(worst, drift); worstX = max(worstX, abs(found.midX - want.midX)); worstY = max(worstY, abs(found.midY - want.midY))48 #expect(drift < 7, "\(probe) page \(index + 1): centre off by \(drift) pt")49 }50 // The scan itself is carried over, not re-encoded.51 let before = ImageCollector(), after = ImageCollector()52 before.collect(page: original.pageRef!, number: 1); after.collect(page: page.pageRef!, number: 1)53 #expect(after.images.count == 1)54 #expect(CGPDFRead.int(after.images[0].dictionary, "Width") == CGPDFRead.int(before.images[0].dictionary, "Width"))55 #expect(CGPDFRead.filters(after.images[0].dictionary) == CGPDFRead.filters(before.images[0].dictionary))56 }57 // A media box that does not start at the origin, with a smaller crop box.58 let offset = Fx.offsetScanPDF("offset-\(Int(dpi)).pdf", dpi: dpi)59 let shifted = try await Fx.run(Fx.request(offset, to: .pdf, op: PDFEngine.Operation.ocr))60 let shiftedDocument = try #require(PDFDocument(url: shifted.file)), shiftedPage = try #require(shiftedDocument.page(at: 0))61 // PDFKit rewrites the boxes relative to the media-box corner: same sizes, origin moved to (0, 0).62 #expect(shiftedPage.bounds(for: .mediaBox) == CGRect(x: 0, y: 0, width: 612, height: 792))63 #expect(shiftedPage.bounds(for: .cropBox) == CGRect(x: 10, y: 10, width: 592, height: 772))64 for probe in Fx.scanProbes {65 let selection = try #require(shiftedDocument.findString(probe, withOptions: []).first, "no selection for \(probe) on the offset page")66 let found = selection.bounds(for: shiftedPage), want = expected[probe]!67 let drift = max(abs(found.midX - want.midX), abs(found.midY - want.midY))68 worst = max(worst, drift)69 #expect(drift < 7, "\(probe) offset page: found \(found), expected \(want)")70 }71 print("MEASURE ocr-layer dpi=\(Int(dpi)) worst-centre-drift=\(String(format: "%.2f", worst))pt along-page-x=\(String(format: "%.2f", worstX))pt along-page-y=\(String(format: "%.2f", worstY))pt")72 // Words are separate in the extracted text.73 #expect((output.page(at: 0)?.string ?? "").contains("Quantum physics describes nature"))74 }75 }7677 @Test func outlineLinksAnnotationsAndFormsSurvive() async throws {78 try await withVision {79 let merged = try await Fx.run(Fx.request(Fx.richPDF("ocr-rich.pdf"), to: .pdf, op: PDFEngine.Operation.merge, more: [Fx.scannedPDF("ocr-scan.pdf", dpi: 200)]))80 let before = Fx.structure(merged.file)81 #expect(before.pages == 5 && before.outline == 6 && before.links == 2 && before.widgets == 1 && before.squares == 1)82 let result = try await Fx.run(Fx.request(merged.file, to: .pdf, op: PDFEngine.Operation.ocr))83 let after = Fx.structure(result.file)84 #expect(after == before)85 #expect(result.output.facts["ocr.pages"] == "1")86 #expect(Fx.pdfText(result.file, page: 4).contains("Quantum"))87 #expect(Fx.pdfText(result.file, page: 0) == Fx.pdfText(merged.file, page: 0))88 let info = PDFInspector.infoDictionary(result.file, password: nil)89 #expect(info["Title"] == "Rich Fixture" && info["Author"] == "ORIX Tests")90 // Annotations stay annotations: they are not painted into the page content.91 let flat = try await Fx.run(Fx.request(result.file, to: .png, ["pages": .string("3"), "resolution.dpi": .int(72), "annotations": .bool(false)]))92 let pixel = Fx.pixel(try #require(Fx.load(flat.file)), 400, 442)93 #expect(pixel.g > 240)94 let reopened = try #require(PDFDocument(url: result.file)), outline = try #require(reopened.outlineRoot)95 #expect(outline.child(at: 0)?.label == "ocr-rich" && outline.child(at: 0)?.child(at: 1)?.child(at: 0)?.label == "Section 2.1")96 }97 }9899 @Test func documentsWithTextAreReturnedUnchanged() async throws {100 let pdf = Fx.simplePDF("has-text.pdf", pages: 2)101 let result = try await Fx.run(Fx.request(pdf, to: .pdf, op: PDFEngine.Operation.ocr))102 #expect(result.output.facts["result"] == "unchanged" && result.hasWarning("ocr-nothing-added"))103 #expect(try Data(contentsOf: result.file) == Data(contentsOf: pdf))104 #expect(Fx.engine.preflight(Fx.request(pdf, to: .pdf, op: PDFEngine.Operation.ocr), inspection: nil).contains { $0.code == "ocr-not-needed" })105 }106107 @Test func everyPageOnRequest() async throws {108 try await withVision {109 let pdf = Fx.simplePDF("ocr-all.pdf", pages: 1, word: "Papaya")110 let result = try await Fx.run(Fx.request(pdf, to: .pdf, op: PDFEngine.Operation.ocr, ["ocr.pages": .string("all"), "ocr.level": .string("fast")]))111 #expect(result.hasWarning("ocr-duplicate-text") && result.output.facts["ocr.pages"] == "1")112 #expect(Fx.pdfText(result.file, page: 0).components(separatedBy: "Papaya").count - 1 >= 3)113 }114 }115 }116117 @Suite struct RichText {118 static func layoutPDF() -> URL {119 let url = Fx.dir("fixture").appendingPathComponent("layout.pdf")120 let ctx = CGContext(url as CFURL, mediaBox: nil, [kCGPDFContextTitle: "Layout Title"] as CFDictionary)!121 var box = CGRect(x: 0, y: 0, width: 612, height: 792)122 ctx.beginPage(mediaBox: &box)123 Fx.draw("Reconstruction Heading", at: CGPoint(x: 72, y: 700), size: 24, in: ctx)124 Fx.draw("This first paragraph has several lines that wrap across the", at: CGPoint(x: 72, y: 660), size: 12, in: ctx)125 Fx.draw("full width of the column and should be merged into a single", at: CGPoint(x: 72, y: 645), size: 12, in: ctx)126 Fx.draw("paragraph ending here.", at: CGPoint(x: 72, y: 630), size: 12, in: ctx)127 Fx.draw("A second paragraph follows a vertical gap and has a hyphen-", at: CGPoint(x: 72, y: 595), size: 12, in: ctx)128 Fx.draw("ated word plus strong text:", at: CGPoint(x: 72, y: 580), size: 12, in: ctx)129 Fx.draw("Boldword", at: CGPoint(x: 230, y: 580), size: 12, font: "Helvetica-Bold", in: ctx)130 Fx.draw("Slantword", at: CGPoint(x: 300, y: 580), size: 12, font: "Helvetica-Oblique", in: ctx)131 ctx.endPage()132 ctx.beginPage(mediaBox: &box)133 Fx.draw("Second page of the layout fixture.", at: CGPoint(x: 72, y: 700), size: 12, in: ctx)134 ctx.endPage()135 ctx.closePDF()136 return url137 }138139 @Test(arguments: [FormatID.docx, .rtf])140 func rebuildsParagraphsAndStyles(format: FormatID) async throws {141 let result = try await Fx.run(Fx.request(RichText.layoutPDF(), to: format))142 let type: NSAttributedString.DocumentType = format == .rtf ? .rtf : .officeOpenXML143 var attributes: NSDictionary?144 let document = try NSAttributedString(url: result.file, options: [.documentType: type], documentAttributes: &attributes)145 let text = document.string146 let paragraphs = text.components(separatedBy: CharacterSet(charactersIn: "\n\u{0C}\u{2029}")).filter { !$0.isEmpty }147 #expect(paragraphs.contains("Reconstruction Heading"))148 #expect(paragraphs.contains("This first paragraph has several lines that wrap across the full width of the column and should be merged into a single paragraph ending here."))149 #expect(paragraphs.contains { $0.hasPrefix("A second paragraph follows a vertical gap and has a hyphenated word plus strong text:") && $0.contains("Boldword") })150 #expect(text.contains("\u{0C}") && paragraphs.last == "Second page of the layout fixture.")151 func font(of word: String) -> NSFont? {152 let range = (text as NSString).range(of: word)153 return range.location == NSNotFound ? nil : document.attribute(.font, at: range.location, effectiveRange: nil) as? NSFont154 }155 #expect(font(of: "Reconstruction")?.pointSize == 24)156 #expect(font(of: "several")?.pointSize == 12)157 #expect(font(of: "Boldword")?.fontDescriptor.symbolicTraits.contains(.bold) == true)158 #expect(font(of: "Slantword")?.fontDescriptor.symbolicTraits.contains(.italic) == true)159 #expect(font(of: "several")?.fontDescriptor.symbolicTraits.contains(.bold) == false)160 #expect(result.output.facts["headings.detected"] == "1")161 #expect((attributes?[NSAttributedString.DocumentAttributeKey.title] as? String) == "Layout Title")162 if format == .docx { #expect(try Data(contentsOf: result.file).prefix(2) == Data("PK".utf8)) }163 let noBreaks = try await Fx.run(Fx.request(RichText.layoutPDF(), to: format, ["docx.page-breaks": .bool(false), "pages": .string("1")]))164 let single = try NSAttributedString(url: noBreaks.file, options: [.documentType: type], documentAttributes: nil).string165 #expect(!single.contains("\u{0C}") && !single.contains("Second page"))166 }167168 @Test func scannedDocumentsAreRefusedWithAdvice() async throws {169 let pdf = Fx.scannedPDF("scan-docx.pdf", dpi: 72)170 #expect(Fx.engine.preflight(Fx.request(pdf, to: .docx), inspection: nil).contains { $0.code == "scanned-no-text" && $0.severity == .dataLoss })171 do {172 _ = try await Fx.run(Fx.request(pdf, to: .docx))173 Issue.record("a scanned PDF must not convert to DOCX silently")174 } catch let error as ConversionError {175 #expect(error.localizedDescription.contains("OCR"))176 }177 }178 }179}180