SPB Git

Repository Public License

DocumentTextView.swift 20.2 KB 392 lines swift

1// ORIX Scan2// File: App/Sources/Document/DocumentTextView.swift3// Description: Recognized text: full text with search and copy, per-line review and correction over the page image, extracted tables.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import ORIXDesign8import ORIXKit9import SwiftUI1011struct DocumentTextView: View {12    enum Mode: Hashable { case text, review, tables }1314    @Environment(AppModel.self) private var model15    @Environment(\.orixAccent) private var accent16    @Environment(\.dismiss) private var dismiss17    let documentID: UUID1819    @State private var mode: Mode = .text20    @State private var pageTexts: [(page: PageRecord, text: String)] = []21    @State private var query = ""22    @State private var share: SharePayload?23    @State private var loaded = false2425    private var manifest: DocumentManifest? { model.manifest(documentID) }26    private var fullText: String { pageTexts.map(\.text).filter { !$0.isEmpty }.joined(separator: "\n\n") }27    private var matchCount: Int {28        guard !query.isEmpty else { return 0 }29        return fullText.ranges(of: query, options: [.caseInsensitive, .diacriticInsensitive]).count30    }3132    var body: some View {33        NavigationStack {34            VStack(spacing: 0) {35                Picker("View", selection: $mode) {36                    Text("Text").tag(Mode.text)37                    Text("Review").tag(Mode.review)38                    Text("Tables").tag(Mode.tables)39                }40                .pickerStyle(.segmented)41                .padding(ORIX.Space.md)42                .accessibilityIdentifier("text.mode")43                if let progress = model.recognitionProgress[documentID] {44                    VStack(alignment: .leading, spacing: ORIX.Space.xs) {45                        HStack {46                            Label("Recognizing on this device…", systemImage: "text.viewfinder").font(.orixCallout.weight(.medium))47                            Spacer()48                            Button("Cancel") { model.cancelRecognition(documentID) }49                        }50                        ORIXProgressBar(value: progress)51                    }52                    .padding(.horizontal, ORIX.Space.md)53                    .padding(.bottom, ORIX.Space.sm)54                }55                switch mode {56                case .text: textMode57                case .review: ReviewMode(documentID: documentID) { await reload() }58                case .tables: TablesMode(documentID: documentID)59                }60            }61            .background(ORIX.Palette.canvas)62            .navigationTitle("Text")63            .navigationBarTitleDisplayMode(.inline)64            .toolbar {65                ToolbarItem(placement: .confirmationAction) { Button("Done") { dismiss() }.fontWeight(.semibold) }66                ToolbarItem(placement: .topBarLeading) {67                    Menu {68                        Button { model.startRecognition(documentID, force: true) } label: { Label("Recognize Again", systemImage: "arrow.clockwise") }69                        Picker("Mode", selection: Binding(get: { model.settings.recognitionMode }, set: { model.settings.recognitionMode = $0 })) {70                            ForEach(RecognitionMode.allCases) { Text($0.title).tag($0) }71                        }72                    } label: { Label("Recognition", systemImage: "text.viewfinder") }73                    .disabled(model.isRecognizing(documentID))74                }75            }76            .task { await reload() }77            .onChange(of: model.isRecognizing(documentID)) { _, running in if !running { Task { await reload() } } }78            .sheet(item: $share) { ShareSheet(items: $0.urls) }79        }80    }8182    private func reload() async {83        guard let manifest = await model.load(documentID) else { return }84        var texts: [(PageRecord, String)] = []85        for page in manifest.pages { texts.append((page, await model.processor.text(of: page, in: documentID))) }86        pageTexts = texts.map { (page: $0.0, text: $0.1) }87        loaded = true88    }8990    // MARK: Text9192    @ViewBuilder private var textMode: some View {93        if !loaded {94            ProgressView().frame(maxHeight: .infinity)95        } else if fullText.isEmpty {96            ORIXEmptyState(systemName: "text.viewfinder", title: "No Text Yet",97                           message: "Recognize the text of this document. It is read on this device and never uploaded.") {98                Button { model.startRecognition(documentID) } label: { Label("Recognize Text", systemImage: "text.viewfinder") }99                    .buttonStyle(.orixPrimary)100                    .disabled(model.isRecognizing(documentID))101                    .accessibilityIdentifier("text.recognize")102            }103        } else {104            VStack(spacing: 0) {105                HStack(spacing: ORIX.Space.xs) {106                    Image(systemName: "magnifyingglass").foregroundStyle(ORIX.Palette.textSecondary)107                    TextField("Search in text", text: $query).textInputAutocapitalization(.never).autocorrectionDisabled()108                        .accessibilityIdentifier("text.search")109                    if !query.isEmpty {110                        Text("\(matchCount) matches").font(.orixCaption).foregroundStyle(matchCount == 0 ? ORIX.Palette.danger : ORIX.Palette.textSecondary)111                            .accessibilityIdentifier("text.matches")112                        Button { query = "" } label: { Image(systemName: "xmark.circle.fill").foregroundStyle(ORIX.Palette.textTertiary) }113                            .accessibilityLabel("Clear search")114                    }115                }116                .padding(.horizontal, ORIX.Space.sm)117                .frame(minHeight: ORIX.minimumTarget)118                .background(ORIX.Palette.surface, in: RoundedRectangle(cornerRadius: ORIX.Radius.control, style: .continuous))119                .padding(.horizontal, ORIX.Space.md)120                ScrollView {121                    LazyVStack(alignment: .leading, spacing: ORIX.Space.md) {122                        ForEach(Array(pageTexts.enumerated()), id: \.element.page.id) { index, entry in123                            VStack(alignment: .leading, spacing: ORIX.Space.xs) {124                                Text("Page \(index + 1)").font(.orixOverline).foregroundStyle(ORIX.Palette.textSecondary).textCase(.uppercase)125                                if entry.text.isEmpty {126                                    Text("No text on this page").font(.orixCallout).foregroundStyle(ORIX.Palette.textTertiary)127                                } else {128                                    Text(highlighted(entry.text)).font(.orixBody).textSelection(.enabled).frame(maxWidth: .infinity, alignment: .leading)129                                }130                            }131                            .orixCard()132                        }133                    }134                    .padding(ORIX.Space.md)135                }136                HStack(spacing: ORIX.Space.sm) {137                    Button {138                        UIPasteboard.general.string = fullText139                        ORIXHaptics.success()140                        model.toast = String(localized: "Text copied")141                    } label: { Label("Copy All", systemImage: "doc.on.doc") }142                        .buttonStyle(.orixSecondary)143                        .accessibilityIdentifier("text.copy")144                    Button {145                        Task { if let url = await model.attempt({ try await model.processor.exportText(documentID) }) { share = SharePayload(urls: [url]) } }146                    } label: { Label("Export .txt", systemImage: "square.and.arrow.up") }147                        .buttonStyle(.orixSecondary)148                }149                .padding(ORIX.Space.md)150                .background(.bar)151            }152        }153    }154155    private func highlighted(_ text: String) -> AttributedString {156        var attributed = AttributedString(text)157        guard !query.isEmpty else { return attributed }158        for range in text.ranges(of: query, options: [.caseInsensitive, .diacriticInsensitive]) {159            if let lower = AttributedString.Index(range.lowerBound, within: attributed), let upper = AttributedString.Index(range.upperBound, within: attributed) {160                attributed[lower..<upper].backgroundColor = accent.secondary.opacity(0.35)161                attributed[lower..<upper].font = .body.weight(.semibold)162            }163        }164        return attributed165    }166}167168extension String {169    func ranges(of needle: String, options: String.CompareOptions) -> [Range<String.Index>] {170        var found: [Range<String.Index>] = []171        var start = startIndex172        while start < endIndex, let range = range(of: needle, options: options, range: start..<endIndex), !range.isEmpty {173            found.append(range)174            start = range.upperBound175        }176        return found177    }178}179180// MARK: Review181182/// The page image with the recognized line boxes, and the lines as editable fields. Unsure lines are flagged.183private struct ReviewMode: View {184    @Environment(AppModel.self) private var model185    @Environment(\.orixAccent) private var accent186    let documentID: UUID187    var onChange: () async -> Void188    @State private var pageIndex = 0189    @State private var result: OCRPage?190    @State private var image: UIImage?191    @State private var focused: UUID?192    @FocusState private var field: UUID?193194    private var pages: [PageRecord] { model.manifest(documentID)?.pages ?? [] }195    private var page: PageRecord? { pages.indices.contains(pageIndex) ? pages[pageIndex] : nil }196197    var body: some View {198        VStack(spacing: 0) {199            if pages.count > 1 {200                HStack {201                    Button { pageIndex -= 1 } label: { Image(systemName: "chevron.left").frame(width: ORIX.minimumTarget, height: ORIX.minimumTarget) }202                        .disabled(pageIndex == 0).accessibilityLabel("Previous page")203                    Text("Page \(pageIndex + 1) of \(pages.count)").font(.orixCallout.weight(.medium)).monospacedDigit()204                    Button { pageIndex += 1 } label: { Image(systemName: "chevron.right").frame(width: ORIX.minimumTarget, height: ORIX.minimumTarget) }205                        .disabled(pageIndex >= pages.count - 1).accessibilityLabel("Next page")206                }207            }208            if let result, !result.lines.isEmpty {209                ScrollViewReader { proxy in210                    ScrollView {211                        VStack(spacing: ORIX.Space.md) {212                            if let image {213                                Image(uiImage: image).resizable().aspectRatio(contentMode: .fit)214                                    .overlay { GeometryReader { geo in boxes(result, in: geo.size, proxy: proxy) } }215                                    .orixPage()216                                    .frame(maxHeight: 420)217                                    .accessibilityLabel("Page image with recognized text regions")218                            }219                            HStack(spacing: ORIX.Space.xs) {220                                ORIXChip(Text("\(result.lines.count) lines"), systemName: "text.alignleft")221                                ORIXChip(Text("Confidence \(Double(result.meanConfidence), format: .percent.precision(.fractionLength(0)))"), systemName: "gauge.medium",222                                         tint: result.meanConfidence < 0.6 ? ORIX.Palette.warning : ORIX.Palette.success)223                                ORIXChip(Text(result.mode.title), systemName: "cpu")224                                Spacer()225                            }226                            VStack(spacing: 0) {227                                ForEach(result.lines) { line in228                                    lineRow(line).id(line.id)229                                    Divider()230                                }231                            }232                            .orixCard(padding: ORIX.Space.xs)233                            Text("Corrections change the copied text, the text export and the text layer of exported PDFs.")234                                .font(.orixCaption).foregroundStyle(ORIX.Palette.textSecondary)235                        }236                        .padding(ORIX.Space.md)237                    }238                    .scrollDismissesKeyboard(.interactively)239                }240            } else if page != nil {241                ORIXEmptyState(systemName: "text.badge.xmark", title: "Nothing to Review",242                               message: "This page has no recognized text. Imported PDF pages that already contain text are not re-recognized.")243            }244        }245        .task(id: "\(pageIndex)-\(model.isRecognizing(documentID))") { await load() }246    }247248    private func load() async {249        guard let page else { result = nil; image = nil; return }250        let stored = await model.processor.store.ocr(page: page.id, in: documentID)251        result = stored?.geometryKey == page.edit.geometryKey ? stored : nil252        image = await model.thumbnails.image(for: page, in: documentID, maxDimension: 1200)253    }254255    private func boxes(_ result: OCRPage, in size: CGSize, proxy: ScrollViewProxy) -> some View {256        ZStack(alignment: .topLeading) {257            ForEach(result.lines) { line in258                let b = line.quad.bounds259                let unsure = line.confidence < 0.5260                RoundedRectangle(cornerRadius: 2)261                    .stroke(focused == line.id ? accent.secondary : (unsure ? ORIX.Palette.warning : accent.primary), lineWidth: focused == line.id ? 2.5 : 1)262                    .background((focused == line.id ? accent.secondary : accent.primary).opacity(focused == line.id ? 0.25 : 0.08))263                    .frame(width: b.width * size.width, height: b.height * size.height)264                    .offset(x: b.minX * size.width, y: b.minY * size.height)265                    .onTapGesture {266                        focused = line.id267                        withAnimation(ORIX.Motion.standard) { proxy.scrollTo(line.id, anchor: .center) }268                    }269                    .accessibilityHidden(true)270            }271        }272    }273274    private func lineRow(_ line: OCRLine) -> some View {275        HStack(alignment: .firstTextBaseline, spacing: ORIX.Space.xs) {276            if line.confidence < 0.5 {277                Image(systemName: "exclamationmark.triangle.fill").foregroundStyle(ORIX.Palette.warning).font(.caption)278                    .accessibilityLabel("Low confidence")279            }280            TextField("Line text", text: binding(for: line), axis: .vertical)281                .font(.orixBody)282                .focused($field, equals: line.id)283                .onChange(of: field) { _, new in if new == line.id { focused = line.id } }284            if line.isEdited {285                Button { update(line.id) { $0.text = $0.recognizedText } } label: { Image(systemName: "arrow.uturn.backward.circle") }286                    .accessibilityLabel("Revert to recognized text")287            }288        }289        .padding(.horizontal, ORIX.Space.xs)290        .frame(minHeight: ORIX.minimumTarget)291        .background(focused == line.id ? accent.secondary.opacity(0.1) : .clear)292    }293294    private func binding(for line: OCRLine) -> Binding<String> {295        Binding(get: { result?.lines.first { $0.id == line.id }?.text ?? line.text }, set: { new in update(line.id) { $0.text = new } })296    }297298    private func update(_ lineID: UUID, _ change: (inout OCRLine) -> Void) {299        guard var current = result, let index = current.lines.firstIndex(where: { $0.id == lineID }), let page else { return }300        change(&current.lines[index])301        result = current302        let snapshot = current303        Task {304            try? await model.processor.store.saveOCR(snapshot, page: page.id, in: documentID)305            await model.didChange(documentID)306            await onChange()307        }308    }309}310311// MARK: Tables312313private struct TablesMode: View {314    @Environment(AppModel.self) private var model315    @Environment(\.orixAccent) private var accent316    let documentID: UUID317    @State private var tables: [ExtractedTable] = []318    @State private var loaded = false319    @State private var share: SharePayload?320321    var body: some View {322        Group {323            if !loaded {324                ProgressView().frame(maxHeight: .infinity)325            } else if tables.isEmpty {326                ORIXEmptyState(systemName: "tablecells", title: "No Tables Found",327                               message: "Tables are rebuilt from the position of recognized text. Recognize the document first; simple tables with clear columns work best.")328            } else {329                ScrollView {330                    VStack(alignment: .leading, spacing: ORIX.Space.lg) {331                        ORIXBanner(.info, title: Text("Check before you rely on it"),332                                   message: Text("Tables are reconstructed from text positions. Highlighted cells are uncertain; you can correct any cell before exporting."))333                        ForEach($tables) { $table in334                            VStack(alignment: .leading, spacing: ORIX.Space.sm) {335                                HStack {336                                    ORIXChip(Text("\(table.rows.count) × \(table.columnCount)"), systemName: "tablecells")337                                    ORIXChip(Text("Confidence \(table.confidence, format: .percent.precision(.fractionLength(0)))"), systemName: "gauge.medium",338                                             tint: table.confidence < 0.7 ? ORIX.Palette.warning : ORIX.Palette.success)339                                    Spacer()340                                    Button { export(table) } label: { Label("CSV", systemImage: "square.and.arrow.up") }341                                        .buttonStyle(.bordered)342                                        .accessibilityIdentifier("tables.export")343                                }344                                ScrollView(.horizontal) {345                                    Grid(horizontalSpacing: 1, verticalSpacing: 1) {346                                        ForEach(table.rows.indices, id: \.self) { r in347                                            GridRow {348                                                ForEach(table.rows[r].indices, id: \.self) { c in349                                                    TextField("", text: $table.rows[r][c])350                                                        .font(r == 0 ? .orixCallout.weight(.semibold) : .orixCallout)351                                                        .padding(.horizontal, 8)352                                                        .frame(minWidth: 96, minHeight: 40, alignment: .leading)353                                                        .background(table.uncertainCells.contains(.init(row: r, column: c)) ? ORIX.Palette.warning.opacity(0.2) : ORIX.Palette.surface)354                                                        .accessibilityLabel("Row \(r + 1), column \(c + 1)")355                                                }356                                            }357                                        }358                                    }359                                    .background(ORIX.Palette.hairline)360                                    .clipShape(RoundedRectangle(cornerRadius: ORIX.Radius.chip, style: .continuous))361                                }362                            }363                            .orixCard()364                        }365                    }366                    .padding(ORIX.Space.md)367                }368            }369        }370        .task(id: model.isRecognizing(documentID)) {371            tables = (try? await model.processor.tables(of: documentID)) ?? []372            loaded = true373        }374        .sheet(item: $share) { ShareSheet(items: $0.urls) }375    }376377    /// Exports the table as shown, including the user's corrections.378    private func export(_ table: ExtractedTable) {379        Task {380            guard let directory = await model.attempt({ try await model.processor.store.makeExportDirectory() }) else { return }381            let title = DocumentProcessor.safeFileName(model.manifest(documentID)?.title ?? "")382            let url = directory.appendingPathComponent("\(title).csv")383            do {384                try (Data([0xEF, 0xBB, 0xBF]) + Data(table.csv.utf8)).write(to: url, options: [.atomic])385                share = SharePayload(urls: [url])386            } catch {387                model.alert = AlertInfo(error)388            }389        }390    }391}392
Files main
Menu

Theme