KMOCRPDFWindowController.swift 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. //
  2. // KMOCRPDFWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2023/11/14.
  6. //
  7. import Cocoa
  8. import PDFKit
  9. class KMOCRPDFWindowController: NSWindowController, NSWindowDelegate, KMGOCRManagerDelegate, NSPopoverDelegate,NSTextFieldDelegate{
  10. var resultString: String = ""
  11. var ocrDictionary: NSMutableDictionary?
  12. var currentIndexPage: Int = 0
  13. // {
  14. // didSet {
  15. // if PDFView.document.pageCount > currentIndexPage {
  16. // currentIndexPage = 0
  17. // self.currentPageLabel.stringValue = "\(currentIndexPage + 1)"
  18. // // let page = self.PDFView.document.page(at: UInt(currentIndexPage))
  19. // self.PDFView.go(toPageIndex: currentIndexPage, animated: true)
  20. // }
  21. // }
  22. // }
  23. var PDFView: CPDFView!
  24. @IBOutlet var prePdfBGView: NSView!
  25. @IBOutlet var currentPageLabel: NSTextField!
  26. @IBOutlet var totalPageLabel: NSTextField!
  27. @IBOutlet var pageLabel: NSTextField!
  28. @IBOutlet var planLabel: NSTextField!
  29. @IBOutlet var ocrCopyButton: NSButton!
  30. @IBOutlet var pageRangeBox: NSComboBox!
  31. @IBOutlet var planComboBox: NSPopUpButton!
  32. @IBOutlet var progressIndicator: NSProgressIndicator!
  33. @IBOutlet var progressControl: NSProgressIndicator!
  34. @IBOutlet var failedBox: NSBox!
  35. @IBOutlet var failedLabel: NSTextField!
  36. @IBOutlet var cancelButton: NSButton!
  37. @IBOutlet var startButton: NSButton!
  38. @IBOutlet var languageLabel: NSTextField!
  39. @IBOutlet var ocrResultLabel: NSTextField!
  40. @IBOutlet var languageButton: NSButton!
  41. @IBOutlet var txtTextView: NSTextView!
  42. @IBOutlet var deleteButton: NSButton!
  43. @IBOutlet var emptyBox: NSBox!
  44. @IBOutlet var emptyLabel: NSTextField!
  45. var errorOCRArrays: Array<Any>?
  46. var pageIndexs: Array<Any>?
  47. var password: String = ""
  48. var pathFile: String = ""
  49. var pdfDocument: CPDFDocument?
  50. @IBOutlet var saveButton: NSButton!
  51. @IBOutlet var box1: NSBox!
  52. @IBOutlet var boxLabel1: NSTextField!
  53. @IBOutlet var previewLabel: NSTextField!
  54. @IBOutlet var savePDFButton: NSButton!
  55. var ocrCurrentIndex: Int = 0
  56. var appleOCRManger: KMGOCRManager?
  57. var googleOCRManger: KMGOCRManager?
  58. var savedFileName: String = ""
  59. private var _fileAttri: KMFileAttribute?
  60. func getOCRResrultsFolderPath() -> String {
  61. var path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last
  62. path?.append("/")
  63. path?.append(Bundle.main.bundleIdentifier!)
  64. if (FileManager.default.fileExists(atPath: path!) == false) {
  65. try?FileManager.default.createDirectory(atPath: path!, withIntermediateDirectories: false)
  66. }
  67. path?.append("/")
  68. path?.append("OCR_Resrults")
  69. return path!
  70. }
  71. convenience init(cpdfDocument: CPDFDocument, pwd: String?) {
  72. self.init(windowNibName: "KMOCRPDFWindowController")
  73. self.pdfDocument = cpdfDocument
  74. self.password = pwd ?? ""
  75. let url: URL? = cpdfDocument.documentURL
  76. let lastPathComponent = url?.deletingPathExtension()
  77. if let str = lastPathComponent?.lastPathComponent {
  78. if str.count > 0 {
  79. self.pathFile = str
  80. }else {
  81. self.pathFile = NSLocalizedString("Untitled", comment: "")
  82. }
  83. } else {
  84. self.pathFile = NSLocalizedString("Untitled", comment: "")
  85. }
  86. self._fileAttri = KMFileAttribute()
  87. self._fileAttri?.password = self.pdfDocument?.password ?? ""
  88. if self.pdfDocument?.documentURL != nil && self.pdfDocument?.documentURL.path != nil{
  89. self._fileAttri?.filePath = self.pdfDocument?.documentURL.path ?? ""
  90. }else {
  91. self._fileAttri?.filePath = ""
  92. }
  93. }
  94. convenience init(filePath: String, pwd: String?) {
  95. self.init(windowNibName: "KMOCRPDFWindowController")
  96. self.password = pwd ?? ""
  97. let pathExtension = filePath.lastPathComponent.customPathExtension
  98. if pathExtension.count > 0{
  99. if pathExtension.lowercased() == "pdf" {
  100. pdfDocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
  101. } else {
  102. if let image = NSImage(contentsOf: URL(fileURLWithPath: filePath)) {
  103. self.pdfDocument = CPDFDocument()
  104. _ = pdfDocument?.km_insertPage(image.size, withImage: filePath, at: pdfDocument?.pageCount ?? 0)
  105. }
  106. }
  107. }
  108. self.pathFile = filePath.lastPathComponent.deletingPathExtension
  109. self._fileAttri = KMFileAttribute()
  110. self._fileAttri?.password = self.pdfDocument?.password ?? ""
  111. self._fileAttri?.filePath = self.pdfDocument?.documentURL.path ?? ""
  112. }
  113. deinit {
  114. NotificationCenter.default.removeObserver(self)
  115. KMGOCRManager.default().delegate = nil
  116. appleOCRManger?.cancelRecognition()
  117. appleOCRManger?.delegate = nil
  118. googleOCRManger?.cancelRecognition()
  119. googleOCRManger?.delegate = nil
  120. }
  121. override func windowDidLoad() {
  122. super.windowDidLoad()
  123. let preView: CPDFView = CPDFView(frame: self.prePdfBGView.bounds)
  124. self.prePdfBGView.addSubview(preView)
  125. self.PDFView = preView
  126. self.PDFView.setDisplay(.singlePage) //self.displayViewMode = CPDFDisplayViewSinglePageContinuous;
  127. self.pageLabel.stringValue = NSLocalizedString("Page Range", comment: "")
  128. self.planLabel.stringValue = NSLocalizedString("OCR Plan", comment: "")
  129. self.planComboBox.removeAllItems()
  130. self.planComboBox.addItems(withTitles: [NSLocalizedString("Plan 1 (Online)", comment: ""), NSLocalizedString("Plan 2 (Offline)", comment: "")])
  131. let plan = UserDefaults.standard.integer(forKey: "KMOCRCurrentPlanKey")
  132. if plan == 0 {
  133. KMGOCRManager.default().ocrType = .google
  134. self.planComboBox.selectItem(at: 0)
  135. } else {
  136. KMGOCRManager.default().ocrType = .apple
  137. self.planComboBox.selectItem(at: 1)
  138. }
  139. self.pageRangeBox.addItems(withObjectValues: [NSLocalizedString("All Pages", comment: ""), NSLocalizedString("Current Page", comment: ""), NSLocalizedString("Odd Pages Only", comment: ""), NSLocalizedString("Even Pages Only", comment: ""), NSLocalizedString("e.g. 1,3-5,10", comment: "")])
  140. self.pageRangeBox.placeholderString = NSLocalizedString("e.g. 1,3-5,10", comment: "")
  141. self.pageRangeBox.selectItem(at: 0)
  142. self.pageRangeBox.isEditable = false
  143. if self.pageRangeBox.indexOfSelectedItem == 4 {
  144. self.window?.makeFirstResponder(self.pageRangeBox)
  145. self.pageRangeBox.stringValue = ""
  146. self.pageRangeBox.isEditable = true
  147. }
  148. self.emptyLabel.stringValue = NSLocalizedString("Recognize text from Image-based or Scanned PDF with OCR", comment: "")
  149. self.failedLabel.stringValue = NSLocalizedString("OCR failed. Please try to change the OCR Plan to \"Plan 2 (Offline)\"", comment: "")
  150. self.txtTextView.textColor = NSColor.textColor
  151. self.txtTextView.enclosingScrollView?.autohidesScrollers = true
  152. self.ocrResultLabel.stringValue = NSLocalizedString("OCR Results", comment: "")
  153. self.ocrCopyButton?.toolTip = KMLocalizedString("Copy and edit text from documents with OCR.", nil)
  154. self.deleteButton?.toolTip = NSLocalizedString("Delete", comment: "")
  155. self.deleteButton?.title = NSLocalizedString("Delete", comment: "")
  156. self.ocrCopyButton?.title = NSLocalizedString("Copy", comment: "")
  157. self.errorOCRArrays = []
  158. self.pageIndexs = []
  159. self.ocrCopyButton.isEnabled = false
  160. self.deleteButton.isEnabled = false
  161. self.saveButton.isEnabled = false
  162. self.savePDFButton.isEnabled = false
  163. self.boxLabel1.stringValue = NSLocalizedString("Settings", comment: "")
  164. self.previewLabel.stringValue = NSLocalizedString("Preview", comment: "")
  165. self.saveButton.title = NSLocalizedString("Save as TXT", comment: "")
  166. self.savePDFButton.title = NSLocalizedString("Save as PDF", comment: "")
  167. self.saveButton.toolTip = NSLocalizedString("Export as a searchable PDF or text file.", comment: "")
  168. self.savePDFButton.toolTip = NSLocalizedString("Export as a searchable PDF or text file.", comment: "")
  169. self.cancelButton.title = NSLocalizedString("Cancel", comment: "")
  170. self.startButton.title = NSLocalizedString("OCR", comment: "")
  171. self.languageLabel.stringValue = NSLocalizedString("Select OCR Language:", comment: "")
  172. self.updateLanguageButton((KMGOCRManager.default().selectedLanguages?.value(forKeyPath: KMGOCRLanguageStringKey) as? [String]))
  173. NotificationCenter.default.addObserver(self, selector: #selector(OCRSelectedLanguagesChangeNotification(notification:)), name: NSNotification.Name("KMOCRSelectedLanguagesChangeNotification"), object: nil)
  174. NotificationCenter.default.addObserver(self, selector: #selector(OCRSelectedPlanChangeNotification(notification:)), name: NSNotification.Name("KMOCRSelectedPlanChangeNotification"), object: nil)
  175. self.progressControl.isHidden = true
  176. emptyBox.isHidden = false
  177. failedBox.isHidden = true
  178. self.PDFView.document = self.pdfDocument
  179. self.PDFView.go(toPageIndex: self.currentIndexPage, animated: false)
  180. self.PDFView.autoScales = true
  181. self.reloadPDFData()
  182. if !(self.pdfDocument?.isLocked ?? false) && ((self.pdfDocument?.unlock(withPassword: self.password)) != nil) {
  183. } else {
  184. if let docUrl = self.pdfDocument?.documentURL {
  185. KMBaseWindowController.checkPassword(url: self.pdfDocument!.documentURL, type: .owner) { success, pwd in
  186. if success {
  187. self.pdfDocument?.unlock(withPassword: pwd)
  188. self.password = pwd
  189. self.reloadPDFData()
  190. } else {
  191. self.close()
  192. }
  193. }
  194. } else {
  195. KMPrint("document url is nil")
  196. }
  197. }
  198. NotificationCenter.default.addObserver(self, selector: #selector(PDFViewDocumentChangedNotification(notification:)), name: NSNotification.Name.PDFViewPageChanged, object: nil)
  199. }
  200. func windowShouldClose(_ sender: NSWindow) -> Bool {
  201. close()
  202. return true
  203. }
  204. override func close() {
  205. if ((self.window?.isSheet) != nil) {
  206. // self.km_endSheet()
  207. self.window?.sheetParent?.endSheet(self.window!)
  208. } else {
  209. super.close()
  210. }
  211. }
  212. func reloadPDFData() { // 隐藏PDFView滑动条
  213. self.PDFView.documentView()?.enclosingScrollView?.hasVerticalScroller = false
  214. self.PDFView.documentView()?.enclosingScrollView?.hasHorizontalScroller = false
  215. let pageCount: Int = Int(self.pdfDocument?.pageCount ?? 0)
  216. let currentPageIndex = self.pdfDocument?.index(for: self.PDFView.currentPage())
  217. self.currentPageLabel.stringValue = "\((currentPageIndex ?? 0) + 1)"
  218. self.totalPageLabel.stringValue = "/ \(pageCount)"
  219. }
  220. //MARK: KMOCRSelectedLanguagesChangeNotification
  221. @objc func OCRSelectedLanguagesChangeNotification(notification: NSNotification) {
  222. if let selectedLanguages = notification.object as? [String] {
  223. updateLanguageButton(selectedLanguages)
  224. }
  225. }
  226. @objc func OCRSelectedPlanChangeNotification(notification: NSNotification) {
  227. let plan = UserDefaults.standard.integer(forKey: "KMOCRCurrentPlanKey")
  228. if plan == 0 {
  229. self.planComboBox.selectItem(at: 0)
  230. } else {
  231. self.planComboBox.selectItem(at: 1)
  232. }
  233. KMGOCRManager.default().selectedLanguages = NSMutableArray()
  234. updateLanguageButton(KMGOCRManager.default().selectedLanguages?.value(forKeyPath: KMGOCRLanguageStringKey) as? [String])
  235. }
  236. @objc func PDFViewDocumentChangedNotification(notification: NSNotification) {
  237. let page: CPDFPage = self.PDFView.currentPage()
  238. let pageIndex = self.PDFView.document.index(for: page)
  239. self.currentPageLabel.stringValue = "\(pageIndex + 1)"
  240. }
  241. func updateLanguageButton(_ languages: [String]?) {
  242. if languages?.count ?? 0 < 1 {
  243. self.languageButton.title = NSLocalizedString("Auto Detection", comment: "")
  244. return
  245. }
  246. var languageName: String? = nil
  247. if languages?.count ?? 0 > 0 {
  248. for i in 0..<(languages?.count ?? 0) {
  249. let language = languages?[i]
  250. if i == 0 {
  251. languageName = language
  252. } else {
  253. languageName = languageName?.appendingFormat(",%@", language ?? "")
  254. }
  255. }
  256. } else {
  257. languageName = ""
  258. }
  259. self.languageButton.title = languageName ?? ""
  260. }
  261. func imageRep(withSize size: NSSize, scale: CGFloat, drawingHandler: (NSRect) -> Void) -> Any? {
  262. let bmpImageRep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(size.width * scale), pixelsHigh: Int(size.height * scale), bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .calibratedRGB, bitmapFormat: .alphaFirst, bytesPerRow: 0, bitsPerPixel: 0)
  263. // bmpImageRep?.bitmapImageRepByRetaggingWithColorSpace = .sRGB
  264. bmpImageRep?.size = size
  265. NSGraphicsContext.saveGraphicsState()
  266. NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: bmpImageRep!)
  267. // drawingHandler(.zero, size)
  268. drawingHandler(NSRect(origin: NSZeroPoint, size: size))
  269. NSGraphicsContext.restoreGraphicsState()
  270. return bmpImageRep
  271. }
  272. func bitmapImage(withSize size: NSSize, drawingHandler: (NSRect) -> Void) -> NSImage? {
  273. let image = NSImage(size: size)
  274. let scale = KMImageScale
  275. image.addRepresentation(imageRep(withSize: size, scale: scale, drawingHandler: drawingHandler) as! NSBitmapImageRep)
  276. return image
  277. }
  278. func thumbnail(with page: CPDFPage) -> NSImage? {
  279. let bounds = page.bounds(for: .cropBox)
  280. var pageSize = bounds.size
  281. var scale: CGFloat = 1.0
  282. var thumbnailSize: NSSize
  283. var pageRect = NSZeroRect
  284. var image: NSImage?
  285. let aSize = pageSize.width
  286. if page.rotation % 180 == 90 {
  287. pageSize = NSSize(width: pageSize.height, height: pageSize.width)
  288. }
  289. if aSize > 0.0 {
  290. if pageSize.height > pageSize.width {
  291. thumbnailSize = NSSize(width: round(aSize * pageSize.width / pageSize.height), height: aSize)
  292. } else {
  293. thumbnailSize = NSSize(width: aSize, height: round(aSize * pageSize.height / pageSize.width))
  294. }
  295. let kKMMaxPixelsLimit = 75000000
  296. let totalPixelNumber = Int((thumbnailSize.width * KMImageScale) * (thumbnailSize.height * KMImageScale))
  297. if totalPixelNumber >= kKMMaxPixelsLimit {
  298. let sizeScale = sqrt(Float(kKMMaxPixelsLimit - 1000) / Float(totalPixelNumber))
  299. thumbnailSize = NSSize(width: floor(thumbnailSize.width * CGFloat(sizeScale)), height: floor(thumbnailSize.height * CGFloat(sizeScale)))
  300. }
  301. scale = max(thumbnailSize.width / pageSize.width, (thumbnailSize.height) / pageSize.height)
  302. } else {
  303. thumbnailSize = NSSize(width: pageSize.width, height: pageSize.height)
  304. }
  305. if thumbnailSize.width.isNaN || thumbnailSize.height.isNaN || thumbnailSize.width == 0.0 || thumbnailSize.height == 0.0 {
  306. thumbnailSize = NSSize(width: 186.0, height: 256.0)
  307. }
  308. pageRect.size = thumbnailSize
  309. image = bitmapImage(withSize: thumbnailSize) { rect in
  310. NSGraphicsContext.current?.imageInterpolation = .high
  311. NSGraphicsContext.saveGraphicsState()
  312. NSColor.white.setFill()
  313. pageRect.fill()
  314. NSGraphicsContext.restoreGraphicsState()
  315. if abs(scale - 1.0) > 0.0 {
  316. let transform = NSAffineTransform()
  317. transform.scale(by: scale)
  318. transform.concat()
  319. }
  320. page.draw(with: .cropBox, to: (NSGraphicsContext.current?.cgContext as! CGContext))
  321. NSGraphicsContext.current?.imageInterpolation = .default
  322. }
  323. return image
  324. }
  325. func savedName() -> String {
  326. var resultArrays: Array<Any> = []
  327. var ocrIndexArrays: Array<Any> = []
  328. let sortedKeys = self.ocrDictionary?.allKeys.sorted(by: {
  329. ($0 as? String)?.compare($1 as? String ?? "") == .orderedAscending
  330. })
  331. for i in 0 ..< (sortedKeys?.count ?? 0) {
  332. let keyS = sortedKeys?[i]
  333. ocrIndexArrays.append(keyS as Any)
  334. resultArrays.append(self.ocrDictionary?[keyS as Any] as Any)
  335. }
  336. var fileName = self.savedFileName
  337. if fileName.count > 50 {
  338. fileName = String(fileName.prefix(50))
  339. }
  340. if sortedKeys?.count ?? 0 > 1 {
  341. fileName = "\(self.pathFile) Pages \(fileName) _OCR"
  342. } else {
  343. fileName = "\(self.pathFile) Page \(fileName) _OCR"
  344. }
  345. var returnName = fileName
  346. switch self.pageRangeBox.indexOfSelectedItem {
  347. case 0:
  348. returnName = "\(self.pathFile)_OCR"
  349. case 2:
  350. returnName = "\(self.pathFile) Odd_OCR"
  351. case 3:
  352. returnName = "\(self.pathFile) Even_OCR"
  353. default:
  354. break
  355. }
  356. return returnName
  357. }
  358. func savePDF() {
  359. var resultArrays: Array<Any> = []
  360. var ocrIndexArrays: Array<Any> = []
  361. let sortedKeys = self.ocrDictionary?.allKeys.sorted(by: { ($0 as? String)?.compare($1 as? String ?? "") == .orderedAscending })
  362. for i in 0 ..< (sortedKeys?.count ?? 0) {
  363. let keyS = sortedKeys?[i]
  364. ocrIndexArrays.append(keyS as Any)
  365. resultArrays.append(self.ocrDictionary?[keyS as Any] as Any)
  366. }
  367. let fileName = self.savedFileName
  368. let saveAccessCtr = KMSavePanelAccessoryController()
  369. let outputSavePanel = NSSavePanel()
  370. outputSavePanel.allowedFileTypes = ["pdf"]
  371. outputSavePanel.nameFieldStringValue = fileName
  372. outputSavePanel.accessoryView = saveAccessCtr.view
  373. outputSavePanel.beginSheetModal(for: self.window!) { [self] (result) in
  374. if result == .OK {
  375. let savePDFPath = outputSavePanel.url?.path ?? ""
  376. if !FileManager.default.fileExists(atPath: getOCRResrultsFolderPath()) {
  377. try? FileManager.default.createDirectory(atPath: getOCRResrultsFolderPath(), withIntermediateDirectories: false, attributes: nil)
  378. }
  379. var imagePath = [Any]()
  380. for i in 0..<ocrIndexArrays.count {
  381. let rPath = (getOCRResrultsFolderPath() as NSString).appendingPathComponent("\(i).png")
  382. if let index = ocrIndexArrays[i] as? NSNumber, let page = self.pdfDocument?.page(at: UInt(index.intValue)), let image = self.thumbnail(with: page) {
  383. try? image.tiffRepresentation?.write(to: URL(fileURLWithPath: rPath), options: .atomic)
  384. }
  385. imagePath.append(rPath)
  386. }
  387. KMGOCRManager.default().createPDFFile(savePDFPath, imagePaths: imagePath, results: resultArrays, scale: KMImageScale)
  388. if saveAccessCtr.openAutomaticButton.state == .on {
  389. self.cancelButtonAction("")
  390. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePDFPath), display: true, completionHandler: {document,documentWasAlreadyOpen,error in
  391. })
  392. } else {
  393. self.viewFileAtFinder(savePDFPath)
  394. }
  395. }
  396. }
  397. }
  398. func viewFileAtFinder(_ fileName: String) {
  399. let workspace = NSWorkspace.shared
  400. let url = URL(fileURLWithPath: fileName)
  401. workspace.activateFileViewerSelecting([url])
  402. }
  403. func saveText() {
  404. let sortedKeys = self.ocrDictionary?.allKeys.sorted(by: { ($0 as? String)?.compare($1 as? String ?? "") == .orderedAscending })
  405. let fileName = self.savedFileName
  406. let saveAccessCtr = KMSavePanelAccessoryController()
  407. let outputSavePanel = NSSavePanel()
  408. outputSavePanel.allowedFileTypes = ["txt"]
  409. outputSavePanel.nameFieldStringValue = fileName
  410. outputSavePanel.accessoryView = saveAccessCtr.view
  411. outputSavePanel.beginSheetModal(for: self.window!) { (result) in
  412. if result == .OK {
  413. let outputURL = outputSavePanel.url
  414. try? self.txtTextView?.string.write(to: outputURL!, atomically: true, encoding: .utf8)
  415. if saveAccessCtr.openAutomaticButton?.state == .on {
  416. NSWorkspace.shared.open(outputURL!)
  417. } else {
  418. self.viewFileAtFinder(outputURL?.path ?? "")
  419. }
  420. }
  421. }
  422. }
  423. func fileNameWithNums(_ nums: [NSNumber]) -> String {
  424. var fileName: String? = nil
  425. if nums.count > 0 {
  426. if nums.count == 1 {
  427. if let num = nums.first {
  428. let idx = num.intValue + 1
  429. return "\(idx)"
  430. }
  431. }
  432. var sortIndex = NSSet()
  433. for num in nums {
  434. let idx = num.intValue + 1
  435. // sortIndex.insert(NSNumber(value: idx))
  436. sortIndex.adding(NSNumber(value: idx))
  437. }
  438. let sort = NSSortDescriptor(key: nil, ascending: true)
  439. let sortDesc = [sort]
  440. let sortArray = sortIndex.sortedArray(using: sortDesc)
  441. var a = 0
  442. var b = 0
  443. if sortArray.count == 1 {
  444. let num: NSNumber = sortArray.last as! NSNumber
  445. fileName = "\(num.intValue)"
  446. return fileName ?? ""
  447. }
  448. for i in 0 ..< sortArray.count {
  449. let num = sortArray[i]
  450. if fileName?.count ?? 0 > 0 {
  451. if (num as AnyObject).intValue == b + 1 {
  452. b = (num as AnyObject).intValue
  453. if (i == sortArray.count - 1) {
  454. fileName = (fileName ?? "") + "\(a)-\(b)"
  455. }
  456. } else {
  457. if a == b {
  458. fileName = (fileName ?? "") + "\(a),"
  459. } else {
  460. fileName = (fileName ?? "") + "\(a)-\(b),"
  461. }
  462. a = (num as AnyObject).intValue
  463. b = (num as AnyObject).intValue
  464. if (i == sortArray.count - 1) {
  465. fileName = (fileName ?? "") + "\(a)"
  466. }
  467. }
  468. } else {
  469. fileName = ""
  470. a = (num as AnyObject).intValue
  471. b = (num as AnyObject).intValue
  472. }
  473. }
  474. return fileName ?? ""
  475. }
  476. return ""
  477. }
  478. @IBAction func cancelButtonAction(_ sender: Any) {
  479. KMGOCRManager.default().cancelRecognition()
  480. self.close()
  481. }
  482. @IBAction func startButtonAction(_ sender: Any) {
  483. self.ocrCurrentIndex = 0
  484. txtTextView.string = ""
  485. ocrDictionary = NSMutableDictionary()
  486. let pdfDocument = self.pdfDocument
  487. errorOCRArrays?.removeAll()
  488. pageIndexs?.removeAll()
  489. if self.pageRangeBox.indexOfSelectedItem == 0 {
  490. for i in 0..<(pdfDocument?.pageCount ?? 0) {
  491. pageIndexs?.append(NSNumber(value: i))
  492. }
  493. } else if self.pageRangeBox.indexOfSelectedItem == 1 {
  494. let page = PDFView.currentPage()
  495. let pageIndex = PDFView.document?.index(for: page)
  496. pageIndexs?.append(NSNumber(value: pageIndex!))
  497. } else if self.pageRangeBox.indexOfSelectedItem == 2 {
  498. for i in 0..<(pdfDocument?.pageCount ?? 0) where i % 2 == 0 {
  499. pageIndexs?.append(NSNumber(value: i))
  500. }
  501. } else if self.pageRangeBox.indexOfSelectedItem == 3 {
  502. for i in 0..<(pdfDocument?.pageCount ?? 0) where i % 2 != 0 {
  503. pageIndexs?.append(NSNumber(value: i))
  504. }
  505. } else {
  506. var attribute = self._fileAttri
  507. if attribute == nil {
  508. attribute = KMFileAttribute()
  509. self._fileAttri = attribute
  510. attribute?.password = self.pdfDocument?.password ?? ""
  511. attribute?.filePath = self.pdfDocument?.documentURL.path ?? ""
  512. }
  513. // attribute.pdfDocument = pdfDocument
  514. attribute?.bAllPage = false
  515. attribute?.pagesType = .custom
  516. attribute?.pagesString = self.pageRangeBox.stringValue
  517. let selectPages = attribute?.fetchSelectPages() ?? []
  518. if selectPages.count > 0 {
  519. for num in selectPages {
  520. pageIndexs?.append(NSNumber(value: num - 1))
  521. }
  522. }
  523. }
  524. batchesOCR()
  525. }
  526. func batchesOCR() {
  527. savedFileName = savedName()
  528. let intervalOCR: UInt = 10
  529. var selctPageImages: NSMutableArray = NSMutableArray()
  530. for i in 0..<intervalOCR {
  531. if ocrCurrentIndex + Int(i) >= self.pageIndexs?.count ?? 0 { continue }
  532. autoreleasepool {
  533. let index = self.pageIndexs?[self.ocrCurrentIndex + Int(i)] as! NSNumber
  534. let page = self.pdfDocument?.page(at: UInt(index.intValue))
  535. let img: NSImage = thumbnail(with: page!)!
  536. let data: NSData = img.tiffRepresentation! as NSData
  537. selctPageImages.add(data)
  538. }
  539. }
  540. if selctPageImages.count < 1 && ocrCurrentIndex == 0 {
  541. let alert = NSAlert()
  542. alert.alertStyle = .critical
  543. alert.messageText = NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  544. if #available(OSX 10.11, *) {
  545. alert.beginSheetModal(for: self.window!, completionHandler: nil)
  546. } else {
  547. alert.runModal()
  548. }
  549. }
  550. if selctPageImages.count < 1 {
  551. return
  552. }
  553. if appleOCRManger != nil {
  554. appleOCRManger?.cancelRecognition()
  555. appleOCRManger?.delegate = nil
  556. appleOCRManger = nil
  557. }
  558. if googleOCRManger != nil {
  559. googleOCRManger?.cancelRecognition()
  560. googleOCRManger?.delegate = nil
  561. googleOCRManger = nil
  562. }
  563. ocrCopyButton.isEnabled = false
  564. saveButton.isEnabled = false
  565. savePDFButton.isEnabled = false
  566. startButton.isEnabled = false
  567. deleteButton.isEnabled = false
  568. progressControl.isHidden = false
  569. progressControl.startAnimation(nil)
  570. emptyBox.isHidden = true
  571. failedBox.isHidden = true
  572. DispatchQueue.main.async {
  573. let languages = KMGOCRManager.default().selectedLanguages?.value(forKeyPath: KMGOCRLanguageCodeKey)
  574. if self.planComboBox.indexOfSelectedItem == 0 {
  575. self.googleOCRManger = KMGOCRManager()
  576. self.googleOCRManger?.ocrType = .google
  577. self.googleOCRManger?.delegate = self
  578. self.googleOCRManger?.recognitionImages((selctPageImages as! [Any]), withLanguages: languages as? [Any])
  579. } else {
  580. self.appleOCRManger = KMGOCRManager()
  581. self.appleOCRManger?.ocrType = .apple
  582. self.appleOCRManger?.delegate = self
  583. self.appleOCRManger?.recognitionImages((selctPageImages as! [Any]), withLanguages: languages as? [Any])
  584. }
  585. }
  586. }
  587. @IBAction func planSelectButtonAction(_ sender: NSPopUpButton) {
  588. let plan = UserDefaults.standard.integer(forKey: "KMOCRCurrentPlanKey")
  589. if plan != sender.indexOfSelectedItem {
  590. KMGOCRManager.default().selectedLanguages = NSMutableArray()
  591. updateLanguageButton(KMGOCRManager.default().selectedLanguages?.value(forKeyPath: KMGOCRLanguageStringKey) as? [String])
  592. }
  593. UserDefaults.standard.set(sender.indexOfSelectedItem, forKey: "KMOCRCurrentPlanKey")
  594. UserDefaults.standard.synchronize()
  595. if self.planComboBox.indexOfSelectedItem == 0 {
  596. KMGOCRManager.default().ocrType = .google
  597. } else {
  598. KMGOCRManager.default().ocrType = .apple
  599. }
  600. NotificationCenter.default.post(name: NSNotification.Name("KMOCRSelectedPlanChangeNotification"), object: nil)
  601. }
  602. @IBAction func languageButtonAction(_ sender: Any) {
  603. let popover = NSPopover()
  604. popover.delegate = self
  605. popover.contentViewController = KMLanguageViewController(nibName: "KMLanguageViewController", bundle: Bundle.main)
  606. popover.animates = true
  607. popover.behavior = .transient
  608. popover.show(relativeTo: (sender as! NSView).bounds, of: sender as! NSView, preferredEdge: .minX)
  609. }
  610. @IBAction func helpButtonAction(_ sender: Any) {
  611. let helpController = NSViewController()
  612. let textView = NSTextView(frame: NSRectToCGRect(NSMakeRect(0, 0, 300.0, 50.0)))
  613. textView.backgroundColor = NSColor.clear
  614. textView.isEditable = false
  615. textView.layer?.cornerRadius = 6
  616. let tStrAuto = NSLocalizedString("Choose automatic language detection for better OCR results.", comment: "")
  617. textView.alignment = .justified
  618. textView.string = "\n\(tStrAuto)"
  619. helpController.view = textView
  620. let popover = NSPopover()
  621. popover.delegate = self
  622. popover.contentViewController = helpController
  623. popover.animates = true
  624. popover.behavior = .transient
  625. popover.show(relativeTo: (sender as! NSView).bounds, of: sender as! NSView, preferredEdge: .minY)
  626. }
  627. @IBAction func buttonItemClick_CopyTxt(_ sender: NSButton) {
  628. let pasteboard = NSPasteboard.general
  629. pasteboard.clearContents()
  630. let str: NSPasteboardWriting = self.txtTextView.string as NSPasteboardWriting
  631. pasteboard.writeObjects([str])
  632. let _ = CustomAlertView.alertView(message: NSLocalizedString("Copy successful!", comment: ""), fromView: sender.superview!, withStyle: .black)
  633. }
  634. @IBAction func buttonItemClick_Delete(_ sender: NSButton) {
  635. ocrCopyButton.isEnabled = false
  636. saveButton.isEnabled = false
  637. savePDFButton.isEnabled = false
  638. deleteButton.isEnabled = false
  639. emptyBox.isHidden = false
  640. failedBox.isHidden = true
  641. resultString = ""
  642. txtTextView.string = ""
  643. ocrDictionary = NSMutableDictionary()
  644. }
  645. @IBAction func boxItemClicked_PageRange(_ sender: NSButton) {
  646. if 4 != pageRangeBox.indexOfSelectedItem {
  647. self.window?.makeFirstResponder(self)
  648. pageRangeBox.isEditable = false
  649. } else {
  650. pageRangeBox.stringValue = ""
  651. pageRangeBox.isEditable = true
  652. self.window?.makeFirstResponder(pageRangeBox)
  653. }
  654. }
  655. @IBAction func nextButtonAction(_ sender: NSButton) {
  656. self.PDFView.goToNextPage(nil)
  657. self.reloadPDFData()
  658. }
  659. @IBAction func previousButtonAction(_ sender: NSButton) {
  660. self.PDFView.goToPreviousPage(nil)
  661. self.reloadPDFData()
  662. }
  663. @IBAction func buttonClicked_SaveButton(_ sender: NSButton) {
  664. self.saveText()
  665. }
  666. @IBAction func savePDFButtonAction(_ sender: NSButton) {
  667. self.savePDF()
  668. }
  669. func controlTextDidEndEditing(_ notification: Notification) {
  670. guard let textField = notification.object as? NSTextField else { return }
  671. if textField == currentPageLabel {
  672. let index = Int(currentPageLabel.stringValue) ?? 0
  673. let pageCount = pdfDocument?.pageCount
  674. let currentPageIndex = pdfDocument?.index(for: PDFView.currentPage())
  675. if index > 0 && index <= pageCount ?? 0 {
  676. PDFView.go(to: pdfDocument?.page(at: UInt(index-1))!)
  677. reloadPDFData()
  678. } else {
  679. currentPageLabel.stringValue = "\((currentPageIndex ?? 0)+1)"
  680. }
  681. }
  682. }
  683. func controlTextDidChange(_ notification: Notification) {
  684. guard let textField = notification.object as? NSTextField else { return }
  685. if textField == currentPageLabel {
  686. let string = textField.formatter?.string(for: NSNumber(value: Int(textField.stringValue) ?? 0))
  687. textField.stringValue = string ?? ""
  688. }
  689. }
  690. func popoverDidClose(_ notification: Notification) {
  691. }
  692. func gocrManagerDidStartOCR(_ manager: KMGOCRManager!) {
  693. }
  694. //MARK: KMGOCRManagerDelegate
  695. func gocrManagerDidFinishOCR(_ manager: KMGOCRManager!) {
  696. self.batchesOCR()
  697. }
  698. func gocrManager(_ manager: KMGOCRManager!, didCancelOCRImageAt index: Int) {
  699. }
  700. func gocrManager(_ manager: KMGOCRManager!, didStartOCRImageAt index: Int) {
  701. DispatchQueue.main.async {
  702. self.pageRangeBox.isEnabled = false
  703. self.languageButton.isEnabled = false
  704. self.planComboBox.isEnabled = false
  705. }
  706. }
  707. func gocrManager(_ manager: KMGOCRManager!, didFinishOCRImageAt index: Int, results: [KMGOCRResult]!) {
  708. self.dealWithResults(results, OCRImageAtIndex: index)
  709. }
  710. func gocrManager(_ manager: KMGOCRManager!, didFailureOCRImageAt index: Int, error: Error!) {
  711. let pagenum = self.pageIndexs?[index] as! NSNumber
  712. if (error != nil) {
  713. self.errorOCRArrays?.append(pagenum)
  714. }
  715. self.dealWithResults([], OCRImageAtIndex: index)
  716. }
  717. func dealWithResults(_ rlts: [KMGOCRResult]?, OCRImageAtIndex index: Int) {
  718. if index >= self.pageIndexs?.count ?? 0 {
  719. return
  720. }
  721. let key = self.pageIndexs?[self.ocrCurrentIndex] as! NSNumber
  722. if ocrDictionary == nil {
  723. ocrDictionary = NSMutableDictionary()
  724. }
  725. ocrDictionary?.setObject(rlts as Any, forKey: key)
  726. let sortedKeys = self.ocrDictionary?.allKeys.sorted(by: { ($0 as! NSNumber).compare($1 as! NSNumber) == .orderedAscending })
  727. var textString = ""
  728. for key in sortedKeys! {
  729. let results: Array<KMGOCRResult> = self.ocrDictionary?.object(forKey: key) as! Array<KMGOCRResult>
  730. var rStr = ""
  731. if results.count > 0 {
  732. rStr = results[0].text
  733. }
  734. if textString.count > 0 {
  735. textString += "\n\n"
  736. }
  737. textString += String(format: NSLocalizedString("Page %ld", comment: ""), (key as! NSNumber).intValue + 1)
  738. textString += "\n"
  739. textString += rStr
  740. }
  741. self.txtTextView.string = textString
  742. self.resultString = textString
  743. self.ocrCurrentIndex += 1
  744. if self.ocrCurrentIndex >= (self.pageIndexs?.count ?? 0) - 1 {
  745. self.progressControl.stopAnimation(nil)
  746. self.progressControl.isHidden = true
  747. self.saveButton.isEnabled = true
  748. self.savePDFButton.isEnabled = true
  749. self.ocrCopyButton.isEnabled = true
  750. self.deleteButton.isEnabled = true
  751. self.startButton.isEnabled = true
  752. self.planComboBox.isEnabled = true
  753. self.pageRangeBox.isEnabled = true
  754. self.languageButton.isEnabled = true
  755. if self.errorOCRArrays?.count ?? 0 < 1 {
  756. } else if self.errorOCRArrays?.count == self.pageIndexs?.count {
  757. if KMGOCRManager.default().ocrType == .google {
  758. self.failedLabel.stringValue = NSLocalizedString("OCR failed.Please try to change the OCR Plan to \"Plan 2 (Offline)\"", comment: "")
  759. self.failedBox.isHidden = false
  760. } else {
  761. let alert = NSAlert()
  762. alert.alertStyle = NSAlert.Style.critical
  763. alert.messageText = ""
  764. if #available(macOS 10.15, *) {
  765. alert.informativeText = NSLocalizedString("Unable to perform OCR on this document. Please try again later.", comment: "")
  766. } else {
  767. alert.informativeText = NSLocalizedString("OCR failed, please try again. Note: OCR Plan 2(Offline) is supported in macOS 10.15+.", comment: "")
  768. self.failedLabel.stringValue = NSLocalizedString("OCR failed, please try again. Note: OCR Plan 2(Offline) is supported in macOS 10.15+.", comment: "")
  769. self.failedBox.isHidden = false
  770. }
  771. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  772. let response = alert.runModal()
  773. if response == NSApplication.ModalResponse.alertFirstButtonReturn {
  774. }
  775. }
  776. } else {
  777. var contextString = String(format: "%@", NSLocalizedString("Some problems occurred during the last operation:", comment: ""))
  778. contextString += "\n"
  779. if self.errorOCRArrays?.count ?? 0 > 1 {
  780. contextString += NSLocalizedString("Pages", comment: "")
  781. } else {
  782. contextString += NSLocalizedString("Page", comment: "")
  783. }
  784. if self.errorOCRArrays?.count ?? 0 > 0 {
  785. contextString += String(format: ": %@", self.fileNameWithNums(self.errorOCRArrays as! Array<NSNumber>))
  786. }
  787. let alert = NSAlert()
  788. alert.alertStyle = NSAlert.Style.critical
  789. alert.messageText = NSLocalizedString("Completed", comment: "")
  790. alert.informativeText = contextString
  791. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  792. let response = alert.runModal()
  793. if response == NSApplication.ModalResponse.alertFirstButtonReturn {
  794. }
  795. }
  796. }
  797. }
  798. }