KMOCRPDFWindowController.swift 38 KB

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