SPB Git

Repository Public

RTFImporter.swift 21.7 KB 376 lines swift

1// ORIX Docs2// File: Packages/ORIXDocsKit/Sources/DocFormats/RTFImporter.swift3// Description: RTF import through AppKit's local RTF reader: fonts, sizes, emphasis, colours, super/subscript, paragraph4//              formatting, tabs, links, lists, regular tables and PNG/JPEG pictures. RTFD packages are out of scope.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import AppKit10import DocModel11import DocContainer1213public enum RTFImporter {14    /// Never throws: unreadable input yields an empty document and a `.dropped` warning. Use `read` to get the error instead.15    public static func importDocument(_ data: Data) -> ImportResult {16        do { return try read(data) } catch { return ImportFailure.result(error, format: .rtf) }17    }1819    public static func read(_ data: Data) throws -> ImportResult {20        guard data.count <= FormatLimits.maxRTFBytes else { throw FormatError.tooLarge("the RTF file is larger than \(FormatLimits.maxRTFBytes) bytes") }21        let head = String(decoding: data.prefix(64), as: UTF8.self).trimmingCharacters(in: CharacterSet(charactersIn: "\u{FEFF} \n\r\t"))22        if head.hasPrefix("rtfd") { throw FormatError.unreadable("RTFD packages (RTF with attachments) are not supported; save the document as plain RTF or DOCX") }23        guard head.hasPrefix("{\\rtf") else { throw FormatError.unreadable("the file is not an RTF document") }24        let extracted = RTFPictures.extract(data)25        var attributes: NSDictionary?26        let attributed: NSAttributedString27        do {28            attributed = try NSAttributedString(data: extracted.rtf, options: [.documentType: NSAttributedString.DocumentType.rtf], documentAttributes: &attributes)29        } catch {30            throw FormatError.unreadable("the RTF data cannot be parsed")31        }32        var reader = RTFReader(attributed, pictures: extracted.pictures, attributes: attributes as? [NSAttributedString.DocumentAttributeKey: Any] ?? [:])33        return reader.run()34    }35}3637private struct RTFReader {38    let source: NSAttributedString39    let ns: NSString40    let pictures: [RTFPictures.Picture?]41    let attributes: [NSAttributedString.DocumentAttributeKey: Any]42    var warnings = WarningCollector()43    var images = ImageResolver(loader: nil)44    var lists = ListAllocator()45    var listIDs: [ObjectIdentifier: String] = [:]46    var baseFamily: String?47    var baseSize: Double?48    var baseColour: RGBA?49    var contentWidth = 468.05051    init(_ source: NSAttributedString, pictures: [RTFPictures.Picture?], attributes: [NSAttributedString.DocumentAttributeKey: Any]) {52        self.source = source53        ns = source.string as NSString54        self.pictures = pictures55        self.attributes = attributes56    }5758    private static func rgba(_ colour: NSColor?) -> RGBA? {59        guard let colour else { return nil }60        // A plain \red\green\blue colour table entry arrives as "generic RGB". Every other RTF producer means sRGB by it,61        // so those components are taken as they are instead of being colour-matched (which would turn pure red into 255/38/0).62        let space = colour.colorSpace63        let direct = colour.type == .componentBased && (space == .genericRGB || space == .deviceRGB || space == .sRGB)64        guard let c = direct ? colour : colour.usingColorSpace(.sRGB) else { return nil }65        func q(_ v: CGFloat) -> Double { (Double(v) * 255).rounded() / 255 }66        return RGBA(r: q(c.redComponent), g: q(c.greenComponent), b: q(c.blueComponent), a: q(c.alphaComponent))67    }6869    mutating func run() -> ImportResult {70        var meta = DocumentMetadata()71        if let t = attributes[.title] as? String { meta.title = TextSanitizer.clean(t) }72        if let a = attributes[.author] as? String { meta.author = TextSanitizer.clean(a) }73        if let s = attributes[.subject] as? String { meta.subject = TextSanitizer.clean(s) }74        if let k = attributes[.keywords] as? [String] { meta.keywords = k.map(TextSanitizer.clean) }75        var setup = PageSetup()76        if let size = attributes[.paperSize] as? NSSize, size.width >= 72, size.height >= 72, size.width <= 14_400, size.height <= 14_400 {77            let w = Double(size.width), h = Double(size.height)78            let portrait = (min(w, h), max(w, h))79            setup.paper = PaperSize.all.first { abs($0.width - portrait.0) < 1.5 && abs($0.height - portrait.1) < 1.5 } ?? PaperSize(name: "Custom", width: portrait.0, height: portrait.1)80            setup.landscape = w > h81        }82        func margin(_ key: NSAttributedString.DocumentAttributeKey) -> Double? {83            (attributes[key] as? NSNumber).map(\.doubleValue).flatMap { $0 >= 0 && $0 <= 1440 ? $0 : nil }84        }85        if let l = margin(.leftMargin) { setup.margins.left = l }86        if let r = margin(.rightMargin) { setup.margins.right = r }87        if let t = margin(.topMargin) { setup.margins.top = t }88        if let b = margin(.bottomMargin) { setup.margins.bottom = b }89        contentWidth = setup.contentWidth9091        dominantFormatting()92        var styles = StyleSheet.standard()93        // RTF has no style hierarchy here: paragraphs carry direct formatting on top of a neutral body style.94        if let f = baseFamily { styles.defaultChar.fontFamily = f }95        if let s = baseSize { styles.defaultChar.fontSize = s }96        if let c = baseColour { styles.defaultChar.color = c }97        if var normal = styles.style(StyleID.normal) {98            normal.para.spaceAfter = 099            normal.para.lineSpacing = .multiple(1.0)100            styles.upsert(normal)101        }102103        var body: [Block] = []104        var location = 0105        // Paragraphs grouped by table: consecutive paragraphs whose outermost text block belongs to the same NSTextTable.106        var tableGroup: (table: NSTextTable, items: [(block: NSTextTableBlock, paragraphs: [Paragraph], nested: Bool)])?107        func flushTable(_ body: inout [Block], _ reader: inout RTFReader) {108            guard let group = tableGroup else { return }109            tableGroup = nil110            body.append(contentsOf: reader.table(group.table, group.items))111        }112        while location < ns.length {113            let range = ns.paragraphRange(for: NSRange(location: location, length: 0))114            guard range.length > 0 else { break }115            location = range.location + range.length116            var content = range117            while content.length > 0, let last = Optional(ns.character(at: content.location + content.length - 1)), last == 0x0A || last == 0x0D || last == 0x2029 { content.length -= 1 }118            let style = (source.attribute(.paragraphStyle, at: range.location, effectiveRange: nil) as? NSParagraphStyle) ?? NSParagraphStyle.default119            let pieces = paragraphs(in: content, style: style)120            if let block = style.textBlocks.first as? NSTextTableBlock {121                if let group = tableGroup, group.table !== block.table { flushTable(&body, &self) }122                if tableGroup == nil { tableGroup = (block.table, []) }123                let paragraphsOnly = pieces.compactMap(\.paragraph)124                if let k = tableGroup!.items.lastIndex(where: { $0.block.startingRow == block.startingRow && $0.block.startingColumn == block.startingColumn }) {125                    tableGroup!.items[k].paragraphs += paragraphsOnly126                } else {127                    tableGroup!.items.append((block, paragraphsOnly, style.textBlocks.count > 1))128                }129                continue130            }131            flushTable(&body, &self)132            if !style.textBlocks.isEmpty { warnings.add(.approximated, "Text blocks", "Text blocks other than table cells are imported as ordinary paragraphs.") }133            body.append(contentsOf: pieces)134        }135        flushTable(&body, &self)136        if pictures.contains(where: { $0 == nil }) {137            warnings.add(.dropped, "Images", "Pictures in formats other than PNG and JPEG (WMF, EMF, PICT) were dropped.")138        }139        warnings.add(.info, "Styles", "RTF read through the system reader carries no named styles: every paragraph uses the body style with direct formatting.")140        var doc = DocumentModel(meta: meta, styles: styles, pageSetup: setup, body: body)141        doc.lists = lists.definitions142        doc.assets = images.assets143        return ImportResult(document: doc, warnings: warnings.warnings)144    }145146    /// The most used font family, size and colour become the document defaults, so runs only record what differs.147    private mutating func dominantFormatting() {148        var families: [String: Int] = [:], sizes: [Double: Int] = [:], colours: [RGBA: Int] = [:]149        source.enumerateAttributes(in: NSRange(location: 0, length: ns.length)) { attrs, range, _ in150            if let font = attrs[.font] as? NSFont {151                families[font.familyName ?? font.fontName, default: 0] += range.length152                sizes[Double(font.pointSize), default: 0] += range.length153            }154            colours[RTFReader.rgba(attrs[.foregroundColor] as? NSColor) ?? .black, default: 0] += range.length155        }156        baseFamily = families.max { ($0.value, $1.key) < ($1.value, $0.key) }?.key157        baseSize = sizes.max { ($0.value, $1.key) < ($1.value, $0.key) }?.key158        baseColour = colours.max { $0.value < $1.value }?.key ?? .black159    }160161    private func paraProps(_ style: NSParagraphStyle, isListItem: Bool) -> ParaProps {162        var p = ParaProps.none163        switch style.alignment {164        case .center: p.alignment = .center165        case .right: p.alignment = .right166        case .justified: p.alignment = .justified167        default: break168        }169        if !isListItem {170            let head = Double(style.headIndent), first = Double(style.firstLineHeadIndent)171            if head > 0 { p.indentLeft = head }172            if abs(first - head) > 0.01 { p.firstLineIndent = first - head }173            let custom = style.tabStops.map { Double($0.location) }174            let standard = NSParagraphStyle.default.tabStops.map { Double($0.location) }175            if !custom.isEmpty, custom != standard {176                p.tabs = style.tabStops.prefix(64).map { TabStop(position: Double($0.location), kind: $0.alignment == .center ? .center : $0.alignment == .right ? .right : .left) }177            }178        }179        let tail = Double(style.tailIndent)180        // A positive tail indent is measured from the leading margin; tenths of a point absorb twip rounding of the page width.181        if tail < 0 { p.indentRight = -tail } else if tail > 0, tail < contentWidth - 0.05 { p.indentRight = ((contentWidth - tail) * 10).rounded() / 10 }182        if style.paragraphSpacing > 0 { p.spaceAfter = Double(style.paragraphSpacing) }183        if style.paragraphSpacingBefore > 0 { p.spaceBefore = Double(style.paragraphSpacingBefore) }184        if style.lineHeightMultiple > 0, abs(style.lineHeightMultiple - 1) > 0.001 { p.lineSpacing = .multiple((Double(style.lineHeightMultiple) * 1000).rounded() / 1000) }185        else if style.minimumLineHeight > 0, style.minimumLineHeight == style.maximumLineHeight { p.lineSpacing = .exact(Double(style.minimumLineHeight)) }186        else if style.minimumLineHeight > 0 { p.lineSpacing = .atLeast(Double(style.minimumLineHeight)) }187        if style.baseWritingDirection == .rightToLeft { p.rightToLeft = true }188        return p189    }190191    private mutating func listItem(_ style: NSParagraphStyle) -> ListItem? {192        let chain = style.textLists193        guard let top = chain.first, let innermost = chain.last else { return nil }194        var level = chain.count - 1195        if level > FormatLimits.maxListLevel {196            level = FormatLimits.maxListLevel197            warnings.add(.approximated, "Deep lists", "Lists nested deeper than \(FormatLimits.maxListLevel + 1) levels are flattened to the deepest level.")198        }199        func flavour(_ list: NSTextList) -> (ordered: Bool, format: NumberFormat?) {200            let marker = list.markerFormat.rawValue.lowercased()201            if marker.contains("decimal") { return (true, .decimal) }202            if marker.contains("lower-alpha") || marker.contains("lower-latin") { return (true, .lowerAlpha) }203            if marker.contains("upper-alpha") || marker.contains("upper-latin") { return (true, .upperAlpha) }204            if marker.contains("lower-roman") { return (true, .lowerRoman) }205            if marker.contains("upper-roman") { return (true, .upperRoman) }206            return (false, nil)207        }208        let key = ObjectIdentifier(top)209        var topID = listIDs[key]210        if topID == nil {211            let f = flavour(top)212            topID = lists.newList(ordered: f.ordered, start: max(0, top.startingItemNumber), format: f.format)213            listIDs[key] = topID214        }215        guard level > 0, let parent = topID else { return topID.map { ListItem(listID: $0, level: 0) } }216        let innerKey = ObjectIdentifier(innermost)217        if let known = listIDs[innerKey] { return ListItem(listID: known, level: level) }218        let f = flavour(innermost)219        let id = lists.sublist(parent: parent, level: level, ordered: f.ordered, start: max(0, innermost.startingItemNumber), format: f.format)220        listIDs[innerKey] = id221        return ListItem(listID: id, level: level)222    }223224    private func charProps(_ attrs: [NSAttributedString.Key: Any]) -> CharProps {225        var c = CharProps.none226        if let font = attrs[.font] as? NSFont {227            let family = font.familyName ?? font.fontName228            if family != baseFamily { c.fontFamily = family }229            if Double(font.pointSize) != baseSize { c.fontSize = Double(font.pointSize) }230            let traits = font.fontDescriptor.symbolicTraits231            if traits.contains(.bold) { c.bold = true }232            if traits.contains(.italic) { c.italic = true }233        }234        if let u = attrs[.underlineStyle] as? Int, u != 0 { c.underline = true }235        if let s = attrs[.strikethroughStyle] as? Int, s != 0 { c.strikethrough = true }236        let colour = RTFReader.rgba(attrs[.foregroundColor] as? NSColor) ?? .black237        if colour != baseColour { c.color = colour }238        if let h = RTFReader.rgba(attrs[.backgroundColor] as? NSColor), h.a > 0 { c.highlight = h }239        if let s = attrs[.superscript] as? Int, s != 0 { c.vertical = s > 0 ? .superscript : .subscript }240        else if let offset = attrs[.baselineOffset] as? NSNumber, offset.doubleValue != 0 { c.baselineShift = offset.doubleValue }241        if let kern = attrs[.kern] as? NSNumber, kern.doubleValue != 0 { c.tracking = kern.doubleValue }242        var link: String?243        if let url = attrs[.link] as? URL { link = url.absoluteString } else if let s = attrs[.link] as? String { link = s }244        if let l = link.flatMap(LinkPolicy.sanitize) { c.link = l }245        return c246    }247248    /// Converts one NSString paragraph. Form feeds inside it split it and produce page breaks.249    private mutating func paragraphs(in content: NSRange, style: NSParagraphStyle) -> [Block] {250        let item = listItem(style)251        let props = paraProps(style, isListItem: item != nil)252        var out: [Block] = []253        var runs: [Run] = []254        var first = true255        func flush(_ out: inout [Block], _ runs: inout [Run], force: Bool) {256            guard force || !runs.isEmpty else { return }257            var p = Paragraph(style: item != nil ? StyleID.listParagraph : StyleID.normal, props: props, runs: runs, list: first ? item : nil)258            p.healRunBoundaries()259            out.append(.paragraph(p))260            runs = []261            first = false262        }263        var segments: [(NSRange, [NSAttributedString.Key: Any])] = []264        source.enumerateAttributes(in: content) { attrs, range, _ in segments.append((range, attrs)) }265        var stripMarker = item != nil266        for (range, attrs) in segments {267            let c = charProps(attrs)268            let style: String? = c.link != nil ? StyleID.hyperlink : nil269            if let attachment = attrs[.attachment] as? NSTextAttachment {270                if let data = attachment.fileWrapper?.regularFileContents, let image = images.add(data, alt: attachment.fileWrapper?.preferredFilename ?? "", warnings: &warnings) {271                    runs.append(Run(inline: .image(image)))272                } else {273                    warnings.add(.dropped, "Attachments", "File attachments that are not images were dropped.")274                }275                continue276            }277            var text = ns.substring(with: range)278            if stripMarker {279                // Older systems keep the list marker ("\t1.\t") in the text; labels are computed in the model, never stored.280                stripMarker = false281                if text.hasPrefix("\t"), let second = text.dropFirst().firstIndex(of: "\t"), text.distance(from: text.startIndex, to: second) <= 14 {282                    text = String(text[text.index(after: second)...])283                }284            }285            var buffer = ""286            func emit(_ runs: inout [Run]) {287                if !buffer.isEmpty { runs.append(Run(TextSanitizer.clean(buffer), props: c, charStyle: style)); buffer = "" }288            }289            var scalars = text.unicodeScalars.makeIterator()290            var pendingSentinel: String?291            while let u = scalars.next() {292                if u == RTFPictures.sentinel.unicodeScalars.first! {293                    if let digits = pendingSentinel {294                        pendingSentinel = nil295                        if let index = Int(digits), index < pictures.count {296                            emit(&runs)297                            if let picture = pictures[index], var image = images.add(picture.data, alt: "", warnings: &warnings) {298                                if let w = picture.width, let h = picture.height, w >= 1, h >= 1, w <= 5000, h <= 5000 { image.width = w; image.height = h }299                                runs.append(Run(inline: .image(image)))300                            }301                        }302                    } else { pendingSentinel = "" }303                    continue304                }305                if pendingSentinel != nil {306                    if u.value >= 48, u.value <= 57, pendingSentinel!.count < 9 { pendingSentinel!.unicodeScalars.append(u); continue }307                    buffer += pendingSentinel!; pendingSentinel = nil308                }309                if u.value == 0x0C {310                    emit(&runs)311                    flush(&out, &runs, force: false)312                    out.append(.pageBreak(BreakBlock()))313                    continue314                }315                buffer.unicodeScalars.append(u)316            }317            emit(&runs)318        }319        // An empty paragraph is kept (blank line) unless it only carried a page break.320        let onlyBreaks = !out.isEmpty && runs.isEmpty321        flush(&out, &runs, force: !onlyBreaks)322        return out323    }324325    private mutating func table(_ table: NSTextTable, _ items: [(block: NSTextTableBlock, paragraphs: [Paragraph], nested: Bool)]) -> [Block] {326        func fallback(_ why: String) -> [Block] {327            warnings.add(.approximated, "Tables", why)328            return items.flatMap { $0.paragraphs.map(Block.paragraph) }329        }330        if items.contains(where: \.nested) { warnings.add(.approximated, "Nested tables", "A table inside a table cell is flattened to paragraphs.") }331        let columns = max(table.numberOfColumns, items.map { $0.block.startingColumn + $0.block.columnSpan }.max() ?? 0)332        let rows = items.map { $0.block.startingRow + $0.block.rowSpan }.max() ?? 0333        guard columns > 0, rows > 0, columns <= FormatLimits.maxTableColumns, rows * columns <= FormatLimits.maxTableCells else {334            return fallback("A table too large or without a regular structure is imported as paragraphs.")335        }336        // Occupancy grid: index of the item covering each grid position.337        var grid = Array(repeating: -1, count: rows * columns)338        for (k, item) in items.enumerated() {339            let b = item.block340            guard b.startingRow >= 0, b.startingColumn >= 0, b.rowSpan >= 1, b.columnSpan >= 1 else { return fallback("A table without a regular structure is imported as paragraphs.") }341            for r in b.startingRow..<(b.startingRow + b.rowSpan) {342                for c in b.startingColumn..<(b.startingColumn + b.columnSpan) {343                    guard grid[r * columns + c] == -1 else { return fallback("A table with overlapping cells is imported as paragraphs.") }344                    grid[r * columns + c] = k345                }346            }347        }348        var widths = Array(repeating: 0.0, count: columns)349        var out: [TableRow] = []350        for r in 0..<rows {351            var cells: [TableCell] = []352            var c = 0353            while c < columns {354                let k = grid[r * columns + c]355                guard k >= 0 else { cells.append(TableCell()); c += 1; continue }356                let b = items[k].block357                var cell = TableCell(paragraphs: r == b.startingRow ? items[k].paragraphs.map { p in var q = p; if q.style == StyleID.normal { q.style = StyleID.tableText }; return q } : [],358                                     colSpan: b.columnSpan, vMerge: b.rowSpan > 1 ? (r == b.startingRow ? .restart : .continue) : .none)359                cell.shading = RTFReader.rgba(b.backgroundColor)360                switch b.verticalAlignment {361                case .middleAlignment: cell.vAlign = .center362                case .bottomAlignment: cell.vAlign = .bottom363                default: break364                }365                if b.columnSpan == 1, b.valueType(for: .width) == .absoluteValueType, b.value(for: .width) > 0 { widths[c] = max(widths[c], Double(b.value(for: .width))) }366                cells.append(cell)367                c += b.columnSpan368            }369            out.append(TableRow(cells: cells))370        }371        let known = widths.filter { $0 > 0 }372        let fill = known.isEmpty ? contentWidth / Double(columns) : known.reduce(0, +) / Double(known.count)373        return [.table(Table(columnWidths: widths.map { $0 > 0 ? $0 : fill }, rows: out))]374    }375}376
Files main
Menu

Theme