SPB Git

Repository Public

ScanPipeline.swift 9.4 KB 159 lines swift

1// SP Drive iOS2// File: Core/Capture/ScanPipeline.swift3// Description: From scanned pages to a PDF: pages are stored as JPEG at a sensible resolution and, when asked, on-device text4//              recognition (Vision) adds an invisible text layer under each line so the PDF can be searched, selected and indexed.5//              No camera and no interface here: the pipeline takes images, so it is tested with generated pages.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import CoreGraphics10import CoreText11import Foundation12import ImageIO13import UniformTypeIdentifiers14import Vision1516public enum ScanPipeline {17    public struct Options: Sendable, Equatable {18        /// Recognise the text on the device and add it as an invisible layer.19        public var ocr: Bool20        /// Recognition languages, most expected first.21        public var languages: [String]22        public var jpegQuality: Double23        /// Longest side of a stored page, in pixels (a scanned A4 page at about 200 dpi).24        public var maxPixel: Int25        /// Resolution assumed to turn pixels into the page's size in points.26        public var dpi: Double27        public init(ocr: Bool = true, languages: [String] = ["fr-FR", "en-US"], jpegQuality: Double = 0.62, maxPixel: Int = 2400, dpi: Double = 200) {28            self.ocr = ocr; self.languages = languages; self.jpegQuality = jpegQuality; self.maxPixel = maxPixel; self.dpi = dpi29        }30    }3132    public struct Line: Sendable, Equatable {33        public var text: String34        /// Normalised to the page, origin at the bottom left (Vision's convention).35        public var box: CGRect36        public var confidence: Float37    }3839    public struct Result: Sendable, Equatable {40        public var url: URL41        public var pageCount: Int42        public var pagesWithText: Int43        public var recognizedCharacters: Int44        public var bytes: Int6445    }4647    public enum Failure: Error, Sendable { case noPages, cannotWrite, cannotEncode }4849    /// Text lines of a page, top to bottom. Runs entirely on the device.50    public static func recognize(_ image: CGImage, languages: [String] = ["fr-FR", "en-US"]) throws -> [Line] {51        let request = VNRecognizeTextRequest()52        request.recognitionLevel = .accurate53        request.usesLanguageCorrection = true54        let supported = (try? request.supportedRecognitionLanguages()) ?? []55        let wanted = languages.filter { supported.contains($0) }56        if !wanted.isEmpty { request.recognitionLanguages = wanted }57        try VNImageRequestHandler(cgImage: image, options: [:]).perform([request])58        return (request.results ?? []).compactMap { observation in59            guard let best = observation.topCandidates(1).first, !best.string.trimmingCharacters(in: .whitespaces).isEmpty else { return nil }60            return Line(text: best.string, box: observation.boundingBox, confidence: best.confidence)61        }.sorted { $0.box.midY > $1.box.midY }62    }6364    /// Writes the pages as one PDF. `progress(page, of)` is called before each page is processed.65    @discardableResult66    public static func makePDF(pages: [CGImage], to url: URL, options: Options = Options(), progress: (@Sendable (Int, Int) -> Void)? = nil) throws -> Result {67        guard !pages.isEmpty else { throw Failure.noPages }68        try? FileManager.default.removeItem(at: url)69        guard let consumer = CGDataConsumer(url: url as CFURL), let context = CGContext(consumer: consumer, mediaBox: nil, [kCGPDFContextCreator: "SP Drive"] as CFDictionary) else { throw Failure.cannotWrite }70        var pagesWithText = 0, characters = 071        for (index, original) in pages.enumerated() {72            progress?(index + 1, pages.count)73            let page = try compressed(original, maxPixel: options.maxPixel, quality: options.jpegQuality)74            let size = CGSize(width: Double(page.width) * 72 / options.dpi, height: Double(page.height) * 72 / options.dpi)75            var box = CGRect(origin: .zero, size: size)76            context.beginPage(mediaBox: &box)77            context.draw(page, in: box)78            if options.ocr {79                let lines = (try? recognize(original, languages: options.languages)) ?? []80                if !lines.isEmpty { pagesWithText += 1 }81                for line in lines { characters += line.text.count; drawInvisible(line, in: context, pageSize: size) }82            }83            context.endPage()84        }85        context.closePDF()86        let bytes = ((try? FileManager.default.attributesOfItem(atPath: url.path)[.size]) as? NSNumber)?.int64Value ?? 087        guard bytes > 0 else { throw Failure.cannotWrite }88        return Result(url: url, pageCount: pages.count, pagesWithText: pagesWithText, recognizedCharacters: characters, bytes: bytes)89    }9091    /// The text of one line, invisible, stretched over the place where it is printed: selection and search then land on the right spot.92    private static func drawInvisible(_ line: Line, in context: CGContext, pageSize: CGSize) {93        let rect = CGRect(x: line.box.minX * pageSize.width, y: line.box.minY * pageSize.height, width: line.box.width * pageSize.width, height: line.box.height * pageSize.height)94        guard rect.width > 1, rect.height > 1 else { return }95        let font = CTFontCreateWithName("Helvetica" as CFString, max(4, rect.height * 0.78), nil)96        let attributed = NSAttributedString(string: line.text, attributes: [NSAttributedString.Key(kCTFontAttributeName as String): font])97        let ctLine = CTLineCreateWithAttributedString(attributed)98        let natural = CTLineGetTypographicBounds(ctLine, nil, nil, nil)99        guard natural > 0 else { return }100        context.saveGState()101        context.setTextDrawingMode(.invisible)102        context.translateBy(x: rect.minX, y: rect.minY + rect.height * 0.22)103        context.scaleBy(x: rect.width / CGFloat(natural), y: 1)104        context.textPosition = .zero105        CTLineDraw(ctLine, context)106        context.restoreGState()107    }108109    /// Downsamples to `maxPixel` and re-encodes as JPEG; Quartz then embeds the JPEG stream as is in the PDF.110    static func compressed(_ image: CGImage, maxPixel: Int, quality: Double) throws -> CGImage {111        let longest = max(image.width, image.height)112        let scale = longest > maxPixel ? Double(maxPixel) / Double(longest) : 1113        let width = max(1, Int(Double(image.width) * scale)), height = max(1, Int(Double(image.height) * scale))114        guard let ctx = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue) else { throw Failure.cannotEncode }115        ctx.interpolationQuality = .high116        ctx.setFillColor(CGColor(red: 1, green: 1, blue: 1, alpha: 1))117        ctx.fill(CGRect(x: 0, y: 0, width: width, height: height))118        ctx.draw(image, in: CGRect(x: 0, y: 0, width: width, height: height))119        guard let flat = ctx.makeImage() else { throw Failure.cannotEncode }120        let data = NSMutableData()121        guard let dest = CGImageDestinationCreateWithData(data, UTType.jpeg.identifier as CFString, 1, nil) else { throw Failure.cannotEncode }122        CGImageDestinationAddImage(dest, flat, [kCGImageDestinationLossyCompressionQuality: quality] as CFDictionary)123        guard CGImageDestinationFinalize(dest), let provider = CGDataProvider(data: data),124              let jpeg = CGImage(jpegDataProviderSource: provider, decode: nil, shouldInterpolate: true, intent: .defaultIntent) else { throw Failure.cannotEncode }125        return jpeg126    }127128    /// A default name for a scan: "Numérisation 2026-11-19 14.32.pdf".129    public static func defaultName(date: Date = .now) -> String {130        let stamp = date.formatted(.iso8601.year().month().day().dateSeparator(.dash)) + " " + date.formatted(.dateTime.hour(.twoDigits(amPM: .omitted)).minute(.twoDigits)).replacingOccurrences(of: ":", with: ".")131        return "Numérisation \(stamp).pdf"132    }133}134135/// Generated pages (black text on white, like a clean scan): fixtures for the tests of the pipeline, and stand-ins for the camera in136/// the simulator, which has none.137public enum ScanFixtures {138    public static func page(lines: [String], size: CGSize = CGSize(width: 1240, height: 1754), fontSize: CGFloat = 44) -> CGImage {139        let ctx = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)!140        ctx.setFillColor(CGColor(red: 1, green: 1, blue: 1, alpha: 1))141        ctx.fill(CGRect(origin: .zero, size: size))142        let font = CTFontCreateWithName("Helvetica" as CFString, fontSize, nil)143        var y = size.height - 160144        for text in lines {145            let attributed = NSAttributedString(string: text, attributes: [NSAttributedString.Key(kCTFontAttributeName as String): font, NSAttributedString.Key(kCTForegroundColorAttributeName as String): CGColor(red: 0, green: 0, blue: 0, alpha: 1)])146            ctx.textPosition = CGPoint(x: 110, y: y)147            CTLineDraw(CTLineCreateWithAttributedString(attributed), ctx)148            y -= fontSize * 2.1149        }150        return ctx.makeImage()!151    }152153    /// Two pages of a plausible invoice.154    public static func invoice() -> [CGImage] {155        [page(lines: ["Facture 2026-1119", "Client : Atelier du Nord", "Montant total : 1 234,56 $", "Merci de votre confiance."]),156         page(lines: ["Conditions de paiement", "Payable dans les trente jours.", "Page 2 de 2"])]157    }158}159
Files main
Menu

Theme