KMOCRPDFWindowController.swift 39 KB

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