Repository Public
1// ORIX Docs2// File: Packages/ORIXDocsKit/Sources/DocReview/ComparisonContext.swift3// Description: Mutable state of one comparison: interning, fingerprints, tokens, change list, statistics and the parts of the4// original document that the merged document has to adopt (notes, assets, styles, lists, references).5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import DocModel1011enum Side { case original, revised }1213/// Where a list of blocks lives and how its changes are labelled.14struct Scope {15 var story: StoryID = .body16 /// Prefix of `DocumentChange.detail` ("Table cell R2C3", "Footnote", "Header, section 1").17 var label: String = ""18 /// Body block indexes reported for every change of the scope (table cells report the table); nil = the list indexes themselves.19 var fixedOriginal: Int?20 var fixedRevised: Int?21 var isBody: Bool = false22 /// A gap of exactly one paragraph on each side is always shown as an edited paragraph (table cells).23 var forcePairSingles: Bool = false24}2526final class ComparisonContext {27 let options: ComparisonOptions28 let original: DocumentModel?29 let revised: DocumentModel?30 let interner = Interner()3132 var changes: [DocumentChange] = []33 var wordsEqual = 0, wordsInserted = 0, wordsDeleted = 0, wordsMoved = 03435 /// Notes of the merged document (revised notes, with tracked changes where they differ, plus adopted original notes).36 var mergedNotes: [String: Note] = [:]37 var mergedAssets: [String: Asset] = [:]38 var extraStyles: [Style] = []39 var extraLists: [ListDefinition] = []40 var extraBib: [BibEntry] = []41 /// Revised note ids already compared with an original note.42 var comparedNotes: Set<String> = []4344 private var revisionCounter = 045 private var changeCounter = 046 private var usedParagraphIDs: Set<String> = []47 private var assetIdentity: [Data: Int] = [:]48 private var noteKeyCache: [Side: [String: String]] = [.original: [:], .revised: [:]]49 private var resolvedCache: [Side: [ResolveKey: ResolvedChar]] = [.original: [:], .revised: [:]]50 private struct ResolveKey: Hashable { var para: String; var charStyle: String?; var props: CharProps }5152 init(options: ComparisonOptions, original: DocumentModel?, revised: DocumentModel?) {53 self.options = options; self.original = original; self.revised = revised54 if let r = revised {55 mergedNotes = r.notes56 mergedAssets = r.assets57 }58 }5960 func doc(_ side: Side) -> DocumentModel? { side == .original ? original : revised }6162 // MARK: Identifiers6364 func nextRevision(_ kind: Revision.Kind) -> Revision {65 revisionCounter += 166 return Revision(id: "cmp-\(revisionCounter)", kind: kind, author: options.author, date: options.date)67 }6869 func nextFormatRevision(previous: CharProps, charStyle: String?) -> FormatRevision {70 revisionCounter += 171 return FormatRevision(id: "cmp-\(revisionCounter)", author: options.author, date: options.date, previous: previous, previousCharStyle: charStyle)72 }7374 func registerParagraphIDs(of blocks: [Block]) {75 DocumentModel.forEachParagraph(in: blocks) { p, _ in self.usedParagraphIDs.insert(p.id) }76 for b in blocks { usedParagraphIDs.insert(b.id) }77 }7879 /// An id for a block adopted from the original document that does not collide with an id of the revised document.80 func uniqueID(_ id: String) -> String {81 var candidate = id82 var n = 083 while usedParagraphIDs.contains(candidate) {84 n += 185 candidate = n == 1 ? "\(id)~o" : "\(id)~o\(n)"86 }87 usedParagraphIDs.insert(candidate)88 return candidate89 }9091 // MARK: Changes9293 func add(_ kind: DocumentChange.Kind, scope: Scope, original: Int?, revised: Int?, before: String = "", after: String = "", detail: String) {94 changeCounter += 195 let d = scope.label.isEmpty ? detail : (detail.isEmpty ? scope.label : "\(scope.label): \(detail)")96 let o = scope.isBody ? original : scope.fixedOriginal97 let r = scope.isBody ? revised : scope.fixedRevised98 changes.append(DocumentChange(id: "change-\(changeCounter)", kind: kind, originalBlock: o, revisedBlock: r, before: before, after: after, detail: d, story: scope.story))99 }100101 // MARK: Fingerprints102103 func normalized(_ s: String) -> String {104 var t = s105 if options.ignoreWhitespace {106 t = t.unicodeScalars.split(whereSeparator: { Tokenizer.whitespace.contains($0) }).map { String(String.UnicodeScalarView($0)) }.joined(separator: " ")107 }108 if options.ignoreCase { t = t.lowercased() }109 return t110 }111112 private func num(_ v: Double) -> String { String(format: "%.2f", v) }113114 /// Identity of an inline object for comparison purposes ("payload"): image asset and size, equation source, citation keys,115 /// note kind (and, when `deep`, the note content), cross-reference target, field, bookmark name.116 func inlineKey(_ inline: Inline, side: Side, deep: Bool) -> String {117 switch inline {118 case .image(let i):119 var identity = "id:\(i.asset)"120 if let data = doc(side)?.assets[i.asset]?.data {121 if let n = assetIdentity[data] { identity = "data:\(n)" } else {122 let n = assetIdentity.count123 assetIdentity[data] = n124 identity = "data:\(n)"125 }126 }127 return "img:\(identity):\(num(i.width))x\(num(i.height))"128 case .shape(let s): return "shape:\(s.kind.rawValue):\(num(s.width))x\(num(s.height)):\(normalized(s.text))"129 case .equation(let e): return "eq:\(e.display ? "D" : "I"):\(e.latex)"130 case .note(let ref): return deep ? "note:\(ref.kind.rawValue):\(noteKey(ref.id, side: side))" : "note:\(ref.kind.rawValue)"131 case .citation(let c):132 return "cite:" + c.items.map { "\($0.key)|\($0.locator ?? "")|\($0.prefix ?? "")|\($0.suffix ?? "")|\($0.suppressAuthor ? 1 : 0)" }.joined(separator: ";")133 case .crossRef(let r): return "xref:\(r.target):\(r.display.rawValue)"134 case .field(let f):135 switch f {136 case .pageNumber: return "field:page"137 case .pageCount: return "field:pages"138 case .date(let format): return "field:date:\(format)"139 case .title: return "field:title"140 case .author: return "field:author"141 case .sequence(let n): return "field:seq:\(n)"142 case .headingText(let l): return "field:heading:\(l)"143 }144 case .bookmark(let n): return "bm:\(n)"145 }146 }147148 func noteKey(_ id: String, side: Side) -> String {149 if let k = noteKeyCache[side]![id] { return k }150 noteKeyCache[side]![id] = "" // guards against a note that references itself151 let k = (doc(side)?.notes[id]?.paragraphs ?? []).map { paragraphKey($0, side: side) }.joined(separator: "\u{2029}")152 noteKeyCache[side]![id] = k153 return k154 }155156 /// Content fingerprint of a paragraph: its text (normalised per the options) with every inline object replaced by its payload key.157 /// Formatting is not part of it, so a paragraph that only changed formatting aligns as equal.158 func paragraphKey(_ p: Paragraph, side: Side) -> String {159 var hasInline = false160 for r in p.runs where r.inline != nil { hasInline = true; break }161 if !hasInline { return normalized(p.plainText) }162 var out = ""163 var text = ""164 for r in p.runs {165 if let i = r.inline {166 out += normalized(text) + "\u{1}" + inlineKey(i, side: side, deep: true) + "\u{2}"167 text = ""168 } else { text += r.text }169 }170 return out + normalized(text)171 }172173 func rowKey(_ row: TableRow, side: Side) -> String {174 row.cells.map { c in c.paragraphs.map { paragraphKey($0, side: side) }.joined(separator: "\u{2029}") }.joined(separator: "\u{3}")175 }176177 func blockKey(_ b: Block, side: Side) -> Int {178 switch b {179 case .paragraph(let p): return interner.id("P" + paragraphKey(p, side: side))180 case .table(let t): return interner.id("T" + t.rows.map { rowKey($0, side: side) }.joined(separator: "\u{4}"))181 case .pageBreak(let br): return interner.id("\u{5}break:\(br.kind.rawValue)")182 case .sectionBreak: return interner.id("\u{5}section")183 case .toc(let t): return interner.id("\u{5}toc:\(t.kind.rawValue)")184 case .bibliography: return interner.id("\u{5}bibliography")185 }186 }187188 // MARK: Tokens189190 func tokens(_ p: Paragraph, side: Side) -> [Token] {191 let text = p.plainText as NSString192 var out: [Token] = []193 var segmentStart = 0194 var pos = 0195 func flush(_ upTo: Int) {196 guard upTo > segmentStart else { return }197 Tokenizer.pieces(of: text, in: NSRange(location: segmentStart, length: upTo - segmentStart)) { loc, len, kind in198 let key: Int199 if kind == .space && self.options.ignoreWhitespace { key = self.interner.id(" ") } else {200 let s = text.substring(with: NSRange(location: loc, length: len))201 key = self.interner.id(self.options.ignoreCase ? s.lowercased() : s)202 }203 out.append(Token(loc: loc, len: len, key: key, kind: kind))204 }205 }206 for r in p.runs {207 let len = r.length208 if let i = r.inline {209 flush(pos)210 out.append(Token(loc: pos, len: len, key: interner.id("\u{1}" + inlineKey(i, side: side, deep: false)), kind: .object))211 segmentStart = pos + len212 }213 pos += len214 }215 flush(pos)216 return out217 }218219 /// Sorted content-token keys of a token list (the bag used for similarity).220 func bag(_ tokens: [Token]) -> [Int] { tokens.filter(\.isContent).map(\.key).sorted() }221222 func tableBag(_ t: Table, side: Side) -> [Int] {223 var keys: [Int] = []224 for row in t.rows { for cell in row.cells { for p in cell.paragraphs { for tok in tokens(p, side: side) where tok.isContent { keys.append(tok.key) } } } }225 return keys.sorted()226 }227228 // MARK: Formatting resolution229230 /// Fully resolved character formatting of a run in its own document.231 func resolved(_ run: Run, paraStyle: String, side: Side) -> ResolvedChar {232 let key = ResolveKey(para: paraStyle, charStyle: run.charStyle, props: run.props)233 if let c = resolvedCache[side]![key] { return c }234 let c: ResolvedChar235 if let d = doc(side) { c = d.styles.charProps(paraStyle: paraStyle, charStyle: run.charStyle, direct: run.props) } else { c = ResolvedChar(run.props) }236 resolvedCache[side]![key] = c237 return c238 }239240 /// What the run adds on top of its paragraph style: character style chain, then direct formatting.241 func override(_ run: Run, side: Side) -> CharProps {242 var c = CharProps.none243 if let cs = run.charStyle, let d = doc(side) { for s in d.styles.chain(cs) { c.apply(s.char) } }244 c.apply(run.props)245 return c246 }247248 // MARK: Adoption of original content249250 /// Makes a paragraph taken from the original document valid inside the merged document: unique id, and the notes, images,251 /// styles, lists and references it points to are carried over.252 func adopt(_ p: inout Paragraph) {253 p.id = uniqueID(p.id)254 for i in p.runs.indices { adopt(&p.runs[i]) }255 guard let o = original, let r = revised else { return }256 if r.styles.style(p.style) == nil, !extraStyles.contains(where: { $0.id == p.style }) {257 for s in o.styles.chain(p.style) where r.styles.style(s.id) == nil && !extraStyles.contains(where: { $0.id == s.id }) { extraStyles.append(s) }258 }259 if let l = p.list, r.list(l.listID) == nil, !extraLists.contains(where: { $0.id == l.listID }), let def = o.list(l.listID) { extraLists.append(def) }260 }261262 func adopt(_ run: inout Run) {263 guard let inline = run.inline, let o = original, let r = revised else { return }264 switch inline {265 case .note(var ref):266 guard let note = o.notes[ref.id] else { return }267 if let existing = r.notes[ref.id] {268 if noteKey(ref.id, side: .original) == noteKey(ref.id, side: .revised) && existing.kind == note.kind { return }269 var copy = note270 var id = ref.id + "~o"271 while mergedNotes[id] != nil && mergedNotes[id] != copy { id += "o" }272 copy.id = id273 mergedNotes[id] = copy274 ref.id = id275 run.inline = .note(ref)276 } else {277 mergedNotes[ref.id] = note278 }279 case .image(var img):280 guard let asset = o.assets[img.asset] else { return }281 if let existing = mergedAssets[img.asset] {282 if existing.data == asset.data { return }283 var copy = asset284 copy.id = img.asset + "~o"285 mergedAssets[copy.id] = copy286 img.asset = copy.id287 run.inline = .image(img)288 } else {289 mergedAssets[img.asset] = asset290 }291 case .citation(let c):292 for item in c.items where r.bibEntry(item.key) == nil && !extraBib.contains(where: { $0.key == item.key }) {293 if let e = o.bibEntry(item.key) { extraBib.append(e) }294 }295 default: break296 }297 }298299 /// A copy of `p` in which every run and the paragraph mark carry a tracked change of `kind`.300 func marked(_ p: Paragraph, _ kind: Revision.Kind, fromOriginal: Bool) -> Paragraph {301 var q = p302 let rev = nextRevision(kind)303 for i in q.runs.indices { q.runs[i].revision = rev; q.runs[i].formatRevision = nil }304 q.markRevision = rev305 if fromOriginal { adopt(&q) }306 return q307 }308309 func marked(_ t: Table, _ kind: Revision.Kind, fromOriginal: Bool) -> Table {310 var out = t311 if fromOriginal { out.id = uniqueID(t.id) }312 for ri in out.rows.indices { out.rows[ri] = marked(out.rows[ri], kind, fromOriginal: fromOriginal) }313 return out314 }315316 func marked(_ row: TableRow, _ kind: Revision.Kind, fromOriginal: Bool) -> TableRow {317 var out = row318 for ci in out.cells.indices { out.cells[ci] = marked(out.cells[ci], kind, fromOriginal: fromOriginal) }319 return out320 }321322 func marked(_ cell: TableCell, _ kind: Revision.Kind, fromOriginal: Bool) -> TableCell {323 var out = cell324 for pi in out.paragraphs.indices { out.paragraphs[pi] = marked(out.paragraphs[pi], kind, fromOriginal: fromOriginal) }325 return out326 }327}328329// MARK: - Shared helpers330331extension Paragraph {332 /// The paragraph with its pending tracked changes accepted: deleted runs removed, revision marks cleared.333 var flattenedForComparison: Paragraph {334 guard hasTrackedChanges else { return self }335 var q = self336 q.runs = runs.compactMap { r in337 if r.isDeleted { return nil }338 var c = r339 c.revision = nil; c.formatRevision = nil340 return c341 }342 q.markRevision = nil343 q.normalize()344 return q345 }346}347348extension DocumentModel {349 /// The document with every pending tracked change accepted at run level. Block indexes are unchanged.350 var flattenedForComparison: DocumentModel {351 var any = false352 forEachParagraphEverywhere { p, _, _ in if p.hasTrackedChanges { any = true } }353 guard any else { return self }354 var d = self355 d.mutateEveryParagraph { p in if p.hasTrackedChanges { p = p.flattenedForComparison } }356 return d357 }358}359360enum Excerpt {361 static let limit = 160362363 /// Readable text of a paragraph: inline objects become bracketed labels.364 static func display(_ p: Paragraph) -> String {365 var out = ""366 for r in p.runs {367 guard let i = r.inline else { out += r.text; continue }368 switch i {369 case .image: out += "[image]"370 case .shape: out += "[shape]"371 case .equation(let e): out += "[equation: \(e.latex)]"372 case .note(let n): out += n.kind == .footnote ? "[footnote]" : "[endnote]"373 case .citation(let c): out += "[citation: \(c.items.map(\.key).joined(separator: ", "))]"374 case .crossRef: out += "[cross-reference]"375 case .field(let f):376 if case .sequence(let name) = f { out += "[\(name) number]" } else { out += "[field]" }377 case .bookmark: break378 }379 }380 return out.replacingOccurrences(of: lineSeparatorCharacter, with: " ").replacingOccurrences(of: "\t", with: " ")381 }382383 static func display(_ b: Block) -> String {384 switch b {385 case .paragraph(let p): return display(p)386 case .table(let t): return t.rows.map { display($0) }.joined(separator: " / ")387 case .pageBreak(let br): return br.kind == .page ? "[page break]" : "[column break]"388 case .sectionBreak: return "[section break]"389 case .toc(let t): return t.kind == .contents ? "[table of contents]" : (t.kind == .figures ? "[list of figures]" : "[list of tables]")390 case .bibliography: return "[bibliography]"391 }392 }393394 static func display(_ row: TableRow) -> String {395 row.cells.map { c in c.paragraphs.map { display($0) }.joined(separator: " ") }.joined(separator: " | ")396 }397398 static func short(_ s: String, limit: Int = Excerpt.limit) -> String {399 let chars = Array(s)400 guard chars.count > limit else { return s }401 return String(chars[..<limit]) + "…"402 }403404 /// Excerpts of two texts centred on the region where they differ, with a little context on each side.405 static func pair(_ a: String, _ b: String, context: Int = 40) -> (String, String) {406 let ca = Array(a), cb = Array(b)407 var prefix = 0408 while prefix < ca.count && prefix < cb.count && ca[prefix] == cb[prefix] { prefix += 1 }409 var suffix = 0410 while suffix < ca.count - prefix && suffix < cb.count - prefix && ca[ca.count - 1 - suffix] == cb[cb.count - 1 - suffix] { suffix += 1 }411 func cut(_ c: [Character]) -> String {412 let lo = max(0, prefix - context)413 let hi = min(c.count, max(lo, c.count - suffix + context))414 var mid = Array(c[lo..<hi])415 var tail = hi < c.count ? "…" : ""416 if mid.count > limit + 2 * context { mid = Array(mid[..<(limit + 2 * context)]); tail = "…" }417 return (lo > 0 ? "…" : "") + String(mid) + tail418 }419 return (cut(ca), cut(cb))420 }421}422