Repository Public
1// ORIX Docs2// File: Packages/ORIXDocsKit/Tests/DocFormatsTests/RTFTests.swift3// Description: RTF import and export through the system codec: character and paragraph formatting, lists, tables, links,4// pictures (our \pict layer), hand-written RTF from other producers, and what the format cannot carry.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Testing9import Foundation10import ImageIO11import DocModel12import DocContainer13@testable import DocFormats1415@Suite struct RTFTests {16 private func resolved(_ p: Paragraph, _ run: Run, _ doc: DocumentModel) -> ResolvedChar { doc.styles.charProps(paraStyle: p.style, charStyle: run.charStyle, direct: run.props) }1718 @Test func handWrittenRTFFromAnotherProducer() throws {19 // Word-style RTF: font table, colour table, \b \i \ul \strike, \super, \highlight via \cb, alignment, indents, spacing, tabs, \line, \page.20 let rtf = #"""21 {\rtf1\ansi\ansicpg1252\deff022 {\fonttbl{\f0\froman Times New Roman;}{\f1\fswiss Helvetica;}{\f2\fmodern Courier New;}}23 {\colortbl;\red255\green0\blue0;\red0\green0\blue255;\red255\green255\blue0;}24 {\info{\title The Title}{\author An Author}{\subject Subj}{\keywords k1, k2}}25 \paperw11906\paperh16838\margl1134\margr1134\margt850\margb85026 \pard\qc\sb120\sa240\f1\fs36\b Centered heading\b0\par27 \pard\qj\li720\ri360\fi-360\sl360\slmult1\f0\fs24 Plain {\b bold} {\i italic} {\ul under} {\strike struck} {\cf1 red} {\cb3 lit} x{\super 2} H{\sub 2}O {\f2\fs20 mono} caf\'e9 <U>8364? \u-10179?\u-8704? end\line second line\par28 \pard\tx1440\tqr\tx7200 a\tab b\tab c\par29 \pard\qr right aligned\par30 \pard \par31 \pard before break\page after break\par32 }33 """#34 // "<U>" stands for the RTF unicode escape introducer, kept out of the source so no tool mistakes it for a string escape.35 let result = try RTFImporter.read(Data(rtf.replacingOccurrences(of: "<U>", with: "\\u").utf8))36 let doc = result.document37 #expect(Describe.blocks(doc) == [38 "Normal: {b:Centered heading}",39 "Normal: Plain {b:bold} {i:italic} {u:under} {s:struck} red {h:lit} x{^:2} H{_:2}O mono café € 😀 end⏎second line",40 "Normal: a\tb\tc",41 "Normal: right aligned",42 "Normal: ",43 "Normal: before break",44 "PAGEBREAK",45 "Normal: after break",46 ])47 #expect(doc.meta.title == "The Title" && doc.meta.author == "An Author" && doc.meta.subject == "Subj" && doc.meta.keywords == ["k1", "k2"])48 #expect(doc.pageSetup.paper == .a4)49 #expect(abs(doc.pageSetup.margins.left - 56.7) < 0.01 && abs(doc.pageSetup.margins.top - 42.5) < 0.01)50 // The dominant font becomes the document default; runs only record differences.51 #expect(doc.styles.defaultChar.fontFamily == "Times New Roman" && doc.styles.defaultChar.fontSize == 12)52 let heading = try #require(doc.body[0].paragraph)53 #expect(heading.props.alignment == .center && heading.props.spaceBefore == 6 && heading.props.spaceAfter == 12)54 #expect(heading.runs[0].props.fontFamily == "Helvetica" && heading.runs[0].props.fontSize == 18 && heading.runs[0].props.bold == true)55 let body = try #require(doc.body[1].paragraph)56 #expect(body.props.alignment == .justified && body.props.indentLeft == 36 && body.props.firstLineIndent == -18)57 #expect(body.props.indentRight == 18, "\(String(describing: body.props.indentRight))")58 #expect(body.props.lineSpacing == .multiple(1.5))59 #expect(body.runs.first { $0.text == "red" }?.props.color == RGBA(hex: 0xFF0000))60 #expect(body.runs.first { $0.text == "lit" }?.props.highlight == RGBA(hex: 0xFFFF00))61 #expect(body.runs.first { $0.text == "mono" }?.props.fontFamily == "Courier New")62 #expect(body.runs.first { $0.text == "mono" }?.props.fontSize == 10)63 #expect(body.runs.first { $0.text == "Plain " }?.props == CharProps.none)64 #expect(doc.body[2].paragraph?.props.tabs == [TabStop(position: 72), TabStop(position: 360, kind: .right)])65 #expect(doc.body[3].paragraph?.props.alignment == .right)66 #expect(result.warnings.map(\.feature) == ["Styles"])67 }6869 @Test func richDocumentRoundTrip() throws {70 let source = Fixture.rich()71 let exported = RTFExporter.export(source)72 let text = String(decoding: exported.data, as: UTF8.self)73 #expect(text.hasPrefix("{\\rtf1\\ansi"))74 #expect(text.contains("{\\pict\\pngblip\\picw40\\pich30\\picwgoal2400\\pichgoal1800 "))75 #expect(text.contains("{\\pict\\jpegblip\\picw10\\pich10\\picwgoal200\\pichgoal200 "))76 #expect(text.contains("HYPERLINK \"https://example.org/a?b=1&c=2\""))77 #expect(!text.contains("\u{E0F0}") && !text.contains("\\u57584") && !text.contains("\\u-7952")) // no picture placeholder is left behind78 for expected in ["Comments", "Tracked changes", "Equations", "Footnotes and endnotes", "Headers and footers", "Sections", "Shapes", "Styles", "Bookmarks", "Fonts", "Table formatting", "Floating objects"] {79 #expect(exported.warnings.contains { $0.feature == expected }, "missing warning: \(expected)")80 }81 #expect(exported.warnings.first { $0.feature == "Equations" }?.detail.contains("LaTeX") == true)82 #expect(exported.warnings.first { $0.feature == "Footnotes and endnotes" }?.detail.contains("appended") == true)8384 let back = try RTFImporter.read(exported.data)85 let doc = back.document86 let lines = Describe.blocks(doc)87 #expect(lines == [88 "Normal: {b:Rich fixture}",89 "Normal: {b:Sommaire}", "Normal: {b:Introduction}", "Normal: Second section",90 "Normal: {b:Introduction}",91 "Normal: Plain {b:bold} {i:italic} [link](https://example.org/a?b=1&c=2) {us^h:styled} inserted<img 120x90 alt= data=\(source.assets["img/1 é"]!.data.count)><img 10x10 alt= data=\(source.assets["photo"]!.data.count)>Notee^{i\\pi}+1=0{^:1i}[see @knuth1984, p. 4; -@doe:2020, passim]Figure 1?2026Rich « fixture »Ada Lovelace end⏎second line\ttabbed",92 "Normal: \\int_0^1 x^2\\,dx = \\frac{1}{3}",93 "Normal: Figure 1: A caption",94 "ListParagraph L0(•): First bullet", "ListParagraph L1(◦): Nested bullet", "ListParagraph L0(1.): One", "ListParagraph L1(1.): One point one",95 "Normal: Custom styled",96 "TABLE 3 cols",97 " R <span 2>TableText: Header A+B | <restart>TableText: Tall",98 " R TableText: a1 | TableText: b1 | <continue>TableText: ",99 " R TableText: a2 | TableText: line one / TableText: line two | TableText: 42",100 "PAGEBREAK", "PAGEBREAK",101 "Normal: {b:Second section}", "Normal: {i:A quotation.}", "Normal: let x = 1⏎print(x)",102 "Normal: {b:Figure 1: A caption}", // the list of figures, as static text in the level 1 contents style103 "Normal: {b:Tables}", "Normal: {b:Références}",104 "Normal: ", // the rule above the appended notes (RTF has no paragraph borders through this codec)105 "Normal: 1. A footnote with {i:emphasis}.", "Normal: i. An endnote.", "Normal: Second paragraph.",106 ])107 // The pictures are the original bytes, not re-encoded copies.108 #expect(Set(doc.assets.values.map(\.data)) == Set(source.assets.values.map(\.data)))109 #expect(doc.meta.title == source.meta.title && doc.meta.author == source.meta.author && doc.meta.keywords == ["alpha", "beta", "gamma"])110 // Resolved formatting of the "styled" run is what the source resolves to (font, size, colour, decorations).111 let sourceParagraph = try #require(source.body[3].paragraph), backParagraph = try #require(doc.body[5].paragraph)112 let want = resolved(sourceParagraph, try #require(sourceParagraph.runs.first { $0.text == "styled" }), source)113 let got = resolved(backParagraph, try #require(backParagraph.runs.first { $0.text == "styled" }), doc)114 #expect(got.fontFamily == "Georgia" && got.fontSize == want.fontSize && got.underline && got.strikethrough && got.vertical == .superscript)115 #expect(got.highlight == RGBA(hex: 0xFFFF00))116 #expect(abs(got.color.r - want.color.r) < 0.01 && abs(got.color.a - 0.5) < 0.01)117 #expect(abs(got.tracking - 0.4) < 0.001)118 // Paragraph formatting of the same paragraph.119 let wantPara = source.styles.paraProps(style: sourceParagraph.style, direct: sourceParagraph.props)120 let gotPara = doc.styles.paraProps(style: backParagraph.style, direct: backParagraph.props)121 #expect(gotPara.alignment == .justified && gotPara.indentLeft == wantPara.indentLeft && gotPara.firstLineIndent == wantPara.firstLineIndent)122 #expect(gotPara.spaceBefore == wantPara.spaceBefore && gotPara.spaceAfter == wantPara.spaceAfter && gotPara.lineSpacing == .exact(15))123 #expect(gotPara.tabs == [TabStop(position: 72, kind: .center)])124 // Table cell shading and vertical alignment.125 let table = try #require(doc.body.compactMap(\.table).first)126 #expect(table.rows[0].cells[0].shading == RGBA(hexString: "ddeeff"))127 #expect(table.rows[0].cells[0].vAlign == .center && table.rows[2].cells[1].vAlign == .bottom)128 #expect(table.columnWidths.count == 3)129 #expect(back.warnings.map(\.feature) == ["Styles"])130 }131132 @Test func listsNumberingAndNesting() throws {133 var doc = DocumentModel()134 var numbered = ListDefinition.numbered(id: "n")135 numbered.levels[0].start = 3136 numbered.levels[1].format = .upperRoman137 doc.lists = [numbered, .bullets(id: "b")]138 func item(_ text: String, _ list: String, _ level: Int = 0) -> Block { .paragraph(Paragraph(style: StyleID.listParagraph, runs: [Run(text)], list: ListItem(listID: list, level: level))) }139 doc.body = [item("three", "n"), item("sub one", "n", 1), item("sub two", "n", 1), item("four", "n"), .paragraph(Paragraph("between")), item("bullet", "b"), item("deep", "b", 2)]140 let back = try RTFImporter.read(RTFExporter.export(doc).data).document141 #expect(Describe.blocks(back) == [142 "ListParagraph L0(3.): three", "ListParagraph L1(I.): sub one", "ListParagraph L1(II.): sub two", "ListParagraph L0(4.): four",143 "Normal: between", "ListParagraph L0(•): bullet", "ListParagraph L2(▪): deep",144 ])145 #expect(back.body.compactMap { $0.paragraph?.visibleText } == ["three", "sub one", "sub two", "four", "between", "bullet", "deep"]) // no marker text left in the paragraphs146 #expect(Set(back.body.compactMap { $0.paragraph?.list?.listID }).count == 2)147 }148149 @Test func picturesFromOtherProducers() throws {150 let png = Fixture.png(width: 20, height: 10)151 let hex = png.map { String(format: "%02X", $0) }.joined()152 var wrapped = ""153 for (k, ch) in hex.enumerated() { wrapped.append(ch); if k % 64 == 63 { wrapped += "\r\n" } }154 // Word writes a PNG in {\*\shppict} followed by a WMF fallback in {\nonshppict}; a bare WMF picture cannot be decoded.155 let rtf = "{\\rtf1\\ansi{\\fonttbl{\\f0 Arial;}}\\f0 Before {\\*\\shppict{\\pict{\\*\\picprop\\shplid1025{\\sp{\\sn shapeType}{\\sv 75}}}\\picscalex200\\picscaley50\\piccropl0\\picw529\\pich264\\picwgoal300\\pichgoal150\\pngblip\\bliptag1{\\*\\blipuid 0102}\(wrapped)}}"156 + "{\\nonshppict{\\pict\\wmetafile8\\picw529\\pich264 010009000003}} middle {\\pict\\wmetafile8 0100090000} {\\pict\\jpegblip\\bin4 \u{1}\u{2}{}} end\\par}"157 let result = try RTFImporter.read(Data(rtf.utf8))158 let p = try #require(result.document.body[0].paragraph)159 #expect(p.visibleText == "Before middle end")160 let images = p.runs.compactMap { r -> ImageObject? in if case .image(let i)? = r.inline { return i } else { return nil } }161 #expect(images.count == 1) // the PNG once: the WMF copies and the truncated JPEG are not images162 #expect(result.document.assets[images[0].asset]?.data == png)163 #expect(images[0].width == 30 && images[0].height == 3.75) // 300 twips × 200 % and 150 twips × 50 %164 #expect(result.warnings.contains { $0.feature == "Images" && $0.severity == .dropped })165 // The extractor alone: placeholders in, pictures out, everything else untouched.166 let extracted = RTFPictures.extract(Data(rtf.utf8))167 #expect(extracted.pictures.count == 3)168 #expect(extracted.pictures[0]?.data == png && extracted.pictures[1] == nil)169 let rewritten = String(decoding: extracted.rtf, as: UTF8.self)170 #expect(!rewritten.contains("pict") && rewritten.contains("Before ") && rewritten.contains(" end\\par}"))171 // Braces and backslashes that are escaped, and \binN payloads containing braces, do not confuse the scanner.172 let tricky = RTFPictures.extract(Data("{\\rtf1 a\\{b\\}c\\\\ {\\*\\generator x{\\pict\\pngblip 00}} d}".utf8))173 #expect(tricky.pictures.isEmpty) // a picture inside an ignorable destination is left alone174 #expect(String(decoding: tricky.rtf, as: UTF8.self) == "{\\rtf1 a\\{b\\}c\\\\ {\\*\\generator x{\\pict\\pngblip 00}} d}")175 }176177 @Test func nonPNGImagesAreConvertedAndMissingOnesReported() throws {178 // A TIFF asset becomes a PNG picture; an image without data is replaced by its alternative text.179 let tiff: Data = {180 let png = Fixture.png(width: 4, height: 4)181 let source = CGImageSourceCreateWithData(png as CFData, nil)!182 let out = NSMutableData()183 let dest = CGImageDestinationCreateWithData(out, "public.tiff" as CFString, 1, nil)!184 CGImageDestinationAddImage(dest, CGImageSourceCreateImageAtIndex(source, 0, nil)!, nil)185 CGImageDestinationFinalize(dest)186 return out as Data187 }()188 var doc = DocumentModel(body: [.paragraph(Paragraph(runs: [Run("a"), Run(inline: .image(ImageObject(asset: "t", width: 40, height: 40))), Run("b"),189 Run(inline: .image(ImageObject(asset: "gone", width: 5, height: 5, altText: "ALT"))), Run("c")]))])190 doc.assets = ["t": Asset(id: "t", mimeType: "image/tiff", data: tiff)]191 let exported = RTFExporter.export(doc)192 #expect(exported.warnings.contains { $0.feature == "Images" && $0.detail.contains("converted to PNG") })193 #expect(exported.warnings.contains { $0.feature == "Missing images" })194 let back = try RTFImporter.read(exported.data).document195 #expect(Describe.blocks(back).first?.hasPrefix("Normal: a<img 40x40 alt= data=") == true)196 #expect(back.body[0].paragraph?.visibleText == "abALTc")197 #expect(back.assets.values.first?.mimeType == "image/png")198 }199200 @Test func rejectsWhatIsNotRTF() {201 #expect(throws: FormatError.unreadable("the file is not an RTF document")) { _ = try RTFImporter.read(Data("Just text".utf8)) }202 #expect(throws: FormatError.unreadable("the file is not an RTF document")) { _ = try RTFImporter.read(Data()) }203 #expect(throws: FormatError.unreadable("the file is not an RTF document")) { _ = try RTFImporter.read(Data([0x50, 0x4B, 0x03, 0x04, 0, 0, 0, 0])) }204 do { _ = try RTFImporter.read(Data("rtfd\u{0}\u{0}\u{0}\u{0}\u{3}\u{0}\u{0}\u{0}TXT.rtf".utf8)); Issue.record("RTFD should be refused") } catch let e as FormatError {205 guard case .unreadable(let why) = e else { Issue.record("\(e)"); return }206 #expect(why.contains("RTFD"))207 } catch { Issue.record("\(error)") }208 // The non-throwing entry point reports the same thing as a warning on an empty document.209 let result = RTFImporter.importDocument(Data("nope".utf8))210 #expect(result.warnings == [FormatWarning(.dropped, feature: "Unreadable Rich Text (RTF)", detail: "The document cannot be read: the file is not an RTF document")])211 #expect(Describe.blocks(result.document) == ["Normal: "])212 // Truncated and unbalanced RTF still yields its text.213 let truncated = RTFImporter.importDocument(Data("{\\rtf1\\ansi{\\fonttbl{\\f0 Arial;}}\\f0 Hello {\\b wor".utf8))214 let recoveredText = truncated.document.body[0].paragraph?.visibleText ?? ""215 #expect(recoveredText.hasPrefix("Hello") || truncated.warnings.contains { $0.feature == "Unreadable Rich Text (RTF)" })216 }217218 @Test func unicodeSurvivesRTF() throws {219 let samples = ["Ünïcödé « guillemets » — dash", "中文字符 日本語 한국어", "العربية نص", "עברית", "👩👩👧👦 🇨🇦 👍🏽", "e\u{301} a\u{308}\u{304}", "𝒳 𝔘 \u{1F600}", "tab\there", "{braces} \\backslash \\par not a command"]220 var doc = DocumentModel(body: samples.map { .paragraph(Paragraph($0)) })221 doc.body.append(.paragraph(Paragraph(props: ParaProps(rightToLeft: true), runs: [Run("مرحبا "), Run("بالعالم", props: CharProps(bold: true))])))222 let back = try RTFImporter.read(RTFExporter.export(doc).data).document223 #expect(Describe.texts(back) == samples + ["مرحبا بالعالم"])224 #expect(back.body.last?.paragraph?.props.rightToLeft == true)225 #expect(back.body.last?.paragraph?.runs.last?.props.bold == true)226 }227228 @Test func emptyAndWhitespaceDocuments() throws {229 let empty = try RTFImporter.read(RTFExporter.export(DocumentModel()).data).document230 #expect(Describe.blocks(empty) == ["Normal: "])231 let blank = try RTFImporter.read(RTFExporter.export(DocumentModel(body: [.paragraph(Paragraph("a")), .paragraph(Paragraph()), .paragraph(Paragraph(" spaced "))])).data).document232 #expect(Describe.texts(blank) == ["a", "", " spaced "])233 }234}235