KMPDFConvertManager.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. //
  2. // KMPDFConvertManager.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/12/7.
  6. //
  7. import Cocoa
  8. class KMPDFConvertManager: NSObject {
  9. static let defaultManager = KMPDFConvertManager()
  10. var operationQueue: OperationQueue = OperationQueue()
  11. lazy var ocrLanguages: [COCRLanguage] = {
  12. return [.chinese, .chineseTraditional, .english, .japanese, .korean,
  13. // // Latin
  14. .serbian, .occitan, .danish, .german, .french, .italian, .spanish, .portuguese, .maori, .malay,
  15. .maltese, .dutch, .norwegian, .polish, .romanian, .slovak, .slovenian, .albanian, .swedish, .swahili,
  16. .tagalog, .turish, .uzbek, .vietnamese, .afrikaans, .azerbaijani, .bosnian, .czech, .welsh, .estonian,
  17. .irish, .croatian, .hungarian, .indonesian, .icelandic, .kurdish, .lithuanian, .latvian,
  18. // Devanagari
  19. // .marathi, .nepali, .hindi, .bihari, .maithili, .angika, .bhojpuri, .magahi, .nagpur, .newari,
  20. // .goanKonkani, .saudiArabia
  21. ]
  22. }()
  23. lazy var ocrLanguageStrings: [String] = {
  24. var array: [String] = []
  25. for ocrLanguage in self.ocrLanguages {
  26. array.append(self.ocrLanguageString(ocrLanguage: ocrLanguage))
  27. }
  28. return array
  29. }()
  30. private var _languages_asc: [COCRLanguage] = []
  31. var languages_asc: [COCRLanguage] {
  32. get {
  33. if (!self._languages_asc.isEmpty) {
  34. return self._languages_asc
  35. }
  36. self._languages_asc = self.getOCRLanguages(sortMode: .ascending)
  37. return self._languages_asc
  38. }
  39. }
  40. private var _languages_desc: [COCRLanguage] = []
  41. var languages_desc: [COCRLanguage] {
  42. get {
  43. if (!self._languages_desc.isEmpty) {
  44. return self._languages_desc
  45. }
  46. self._languages_desc = self.getOCRLanguages(sortMode: .descending)
  47. return self._languages_desc
  48. }
  49. }
  50. private var _languages_asc_strings: [String] = []
  51. var languages_asc_strings: [String] {
  52. get {
  53. if (!self._languages_asc_strings.isEmpty) {
  54. return self._languages_asc_strings
  55. }
  56. self._languages_asc_strings = self.getOCRLanguageStrings(sortMode: .ascending)
  57. return self._languages_asc_strings
  58. }
  59. }
  60. private var _languages_desc_strings: [String] = []
  61. var languages_desc_strings: [String] {
  62. get {
  63. if (!self._languages_desc_strings.isEmpty) {
  64. return self._languages_desc_strings
  65. }
  66. self._languages_desc_strings = self.getOCRLanguageStrings(sortMode: .descending)
  67. return self._languages_desc_strings
  68. }
  69. }
  70. override init() {
  71. super.init()
  72. operationQueue.maxConcurrentOperationCount = 1
  73. }
  74. func convert(convert: KMPDFConvert, completion: @escaping (_ finished: Bool, _ error: Error?) -> ()) {
  75. convert.callback = completion
  76. operationQueue.addOperation(convert)
  77. }
  78. func convert(convert: KMPDFConvert, progress:@escaping (Int)->Void, completion: @escaping (_ finished: Bool, _ error: Error?) -> ()) {
  79. convert.callback = completion
  80. convert.progress = progress
  81. operationQueue.addOperation(convert)
  82. }
  83. func cancel(convert: KMPDFConvert) {
  84. convert.cancel()
  85. self.operationQueue.cancelAllOperations()
  86. }
  87. public func ocrLanguageString(ocrLanguage: COCRLanguage) -> String {
  88. switch ocrLanguage {
  89. case .chinese:
  90. return NSLocalizedString("Chinese", comment: "")
  91. case .chineseTraditional:
  92. return NSLocalizedString("Chinese Traditional", comment: "")
  93. case .english:
  94. return NSLocalizedString("English", comment: "")
  95. case .japanese:
  96. return NSLocalizedString("Japanese", comment: "")
  97. case .korean:
  98. return NSLocalizedString("Kroean", comment: "")
  99. /// Latin
  100. case .serbian:
  101. return NSLocalizedString("Serbian", comment: "")
  102. case .occitan:
  103. return NSLocalizedString("Occitan", comment: "")
  104. case .danish:
  105. return NSLocalizedString("Danish", comment: "")
  106. case .german:
  107. return NSLocalizedString("German", comment: "")
  108. case .french:
  109. return NSLocalizedString("French", comment: "")
  110. case .italian:
  111. return NSLocalizedString("Italian", comment: "")
  112. case .spanish:
  113. return NSLocalizedString("Spanish", comment: "")
  114. case .portuguese:
  115. return NSLocalizedString("Portuguese", comment: "")
  116. case .maori:
  117. return NSLocalizedString("Maori", comment: "")
  118. case .malay:
  119. return NSLocalizedString("Malay", comment: "")
  120. case .maltese:
  121. return NSLocalizedString("Maltese", comment: "")
  122. case .dutch:
  123. return NSLocalizedString("Dutch", comment: "")
  124. case .norwegian:
  125. return NSLocalizedString("Norwegian", comment: "")
  126. case .polish:
  127. return NSLocalizedString("Polish", comment: "")
  128. case .romanian:
  129. return NSLocalizedString("Romanian", comment: "")
  130. case .slovak:
  131. return NSLocalizedString("Slovak", comment: "")
  132. case .slovenian:
  133. return NSLocalizedString("Slovenian", comment: "")
  134. case .albanian:
  135. return NSLocalizedString("Albanian", comment: "")
  136. case .swedish:
  137. return NSLocalizedString("Swedish", comment: "")
  138. case .swahili:
  139. return NSLocalizedString("Swahili", comment: "")
  140. case .tagalog:
  141. return NSLocalizedString("Tagalog", comment: "")
  142. case .turish:
  143. return NSLocalizedString("Turish", comment: "")
  144. case .uzbek:
  145. return NSLocalizedString("Uzbek", comment: "")
  146. case .vietnamese:
  147. return NSLocalizedString("Vietnamese", comment: "")
  148. case .afrikaans:
  149. return NSLocalizedString("Afrikaans", comment: "")
  150. case .azerbaijani:
  151. return NSLocalizedString("Azerbaijani", comment: "")
  152. case .bosnian:
  153. return NSLocalizedString("Bosnian", comment: "")
  154. case .czech:
  155. return NSLocalizedString("Czech", comment: "")
  156. case .welsh:
  157. return NSLocalizedString("Welsh", comment: "")
  158. case .estonian:
  159. return NSLocalizedString("Estonian", comment: "")
  160. case .irish:
  161. return NSLocalizedString("Irish", comment: "")
  162. case .croatian:
  163. return NSLocalizedString("Croatian", comment: "")
  164. case .hungarian:
  165. return NSLocalizedString("Hungarian", comment: "")
  166. case .indonesian:
  167. return NSLocalizedString("Indonesian", comment: "")
  168. case .icelandic:
  169. return NSLocalizedString("Icelandic", comment: "")
  170. case .kurdish:
  171. return NSLocalizedString("Kurdish", comment: "")
  172. case .lithuanian:
  173. return NSLocalizedString("Lithuanian", comment: "")
  174. case .latvian:
  175. return NSLocalizedString("Latvian", comment: "")
  176. /// Devanagari
  177. case .marathi:
  178. return NSLocalizedString("Marathi", comment: "")
  179. case .nepali:
  180. return NSLocalizedString("Nepali", comment: "")
  181. case .hindi:
  182. return NSLocalizedString("Hindi", comment: "")
  183. case .bihari:
  184. return NSLocalizedString("Bihari", comment: "")
  185. case .maithili:
  186. return NSLocalizedString("Maithili", comment: "")
  187. case .angika:
  188. return NSLocalizedString("Angika", comment: "")
  189. case .bhojpuri:
  190. return NSLocalizedString("Bhojpuri", comment: "")
  191. case .magahi:
  192. return NSLocalizedString("Magahi", comment: "")
  193. case .nagpur:
  194. return NSLocalizedString("Nagpur", comment: "")
  195. case .newari:
  196. return NSLocalizedString("Newari", comment: "")
  197. case .goanKonkani:
  198. return NSLocalizedString("GoanKonkani", comment: "")
  199. case .saudiArabia:
  200. return NSLocalizedString("SaudiArabia", comment: "")
  201. }
  202. }
  203. //
  204. func ocrLanguageIsSupport(appleLanguage: String?) -> Bool {
  205. if let _ = KMPDFConvertManager.defaultManager._ocrLanguage(appleLanguage: appleLanguage) {
  206. return true
  207. }
  208. return false
  209. }
  210. func ocrLanguage(appleLanguage: String?) -> COCRLanguage? {
  211. return KMPDFConvertManager.defaultManager._ocrLanguage(appleLanguage: appleLanguage)
  212. }
  213. // MARK: - Private Methods
  214. fileprivate func _ocrLanguage(appleLanguage: String?) -> COCRLanguage? {
  215. guard let _appleLanguage = appleLanguage else {
  216. return nil
  217. }
  218. if (_appleLanguage.hasPrefix("zh-Hans")) {
  219. return .chinese
  220. }
  221. if (_appleLanguage.hasPrefix("zh-Hant")) {
  222. return .chineseTraditional
  223. }
  224. if (_appleLanguage.hasPrefix("en")) {
  225. return .english
  226. }
  227. if (_appleLanguage.hasPrefix("ja")) {
  228. return .japanese
  229. }
  230. if (_appleLanguage.hasPrefix("ko")) {
  231. return .korean
  232. }
  233. if (_appleLanguage.hasPrefix("sr")) {
  234. return .serbian
  235. }
  236. if (_appleLanguage.hasPrefix("oc")) {
  237. return .occitan
  238. }
  239. if (_appleLanguage.hasPrefix("da")) {
  240. return .danish
  241. }
  242. if (_appleLanguage.hasPrefix("de")) {
  243. return .german
  244. }
  245. if (_appleLanguage.hasPrefix("fr")) {
  246. return .french
  247. }
  248. if (_appleLanguage.hasPrefix("it")) {
  249. return .italian
  250. }
  251. if (_appleLanguage.hasPrefix("es")) {
  252. return .spanish
  253. }
  254. if (_appleLanguage.hasPrefix("pt")) {
  255. return .portuguese
  256. }
  257. if (_appleLanguage.hasPrefix("mi")) {
  258. return .maori
  259. }
  260. if (_appleLanguage.hasPrefix("ms")) {
  261. return .malay
  262. }
  263. if (_appleLanguage.hasPrefix("mt")) {
  264. return .maltese
  265. }
  266. if (_appleLanguage.hasPrefix("nl")) {
  267. return .dutch
  268. }
  269. if (_appleLanguage.hasPrefix("nb")) {
  270. return .norwegian
  271. }
  272. if (_appleLanguage.hasPrefix("pl")) {
  273. return .polish
  274. }
  275. if (_appleLanguage.hasPrefix("ro")) {
  276. return .romanian
  277. }
  278. if (_appleLanguage.hasPrefix("sk")) {
  279. return .slovak
  280. }
  281. if (_appleLanguage.hasPrefix("sl")) {
  282. return .slovenian
  283. }
  284. if (_appleLanguage.hasPrefix("sq")) {
  285. return .albanian
  286. }
  287. if (_appleLanguage.hasPrefix("sv")) {
  288. return .welsh
  289. }
  290. if (_appleLanguage.hasPrefix("sw")) {
  291. return .swahili
  292. }
  293. if (_appleLanguage.hasPrefix("fil")) { // tagalog 塔加洛语 Filipino 菲律宾语
  294. return .tagalog
  295. }
  296. if (_appleLanguage.hasPrefix("tr")) { // Turkish
  297. return .turish
  298. }
  299. if (_appleLanguage.hasPrefix("uz")) {
  300. return .uzbek
  301. }
  302. if (_appleLanguage.hasPrefix("vi")) {
  303. return .vietnamese
  304. }
  305. if (_appleLanguage.hasPrefix("af")) {
  306. return .afrikaans
  307. }
  308. if (_appleLanguage.hasPrefix("az")) {
  309. return .azerbaijani
  310. }
  311. if (_appleLanguage.hasPrefix("bs")) {
  312. return .bosnian
  313. }
  314. if (_appleLanguage.hasPrefix("cs")) {
  315. return .czech
  316. }
  317. if (_appleLanguage.hasPrefix("cy")) {
  318. return .welsh
  319. }
  320. if (_appleLanguage.hasPrefix("et")) { // eston
  321. return .estonian
  322. }
  323. if (_appleLanguage.hasPrefix("ga")) {
  324. return .irish
  325. }
  326. if (_appleLanguage.hasPrefix("hr")) {
  327. return .croatian
  328. }
  329. if (_appleLanguage.hasPrefix("hu")) {
  330. return .hungarian
  331. }
  332. if (_appleLanguage.hasPrefix("id")) {
  333. return .indonesian
  334. }
  335. if (_appleLanguage.hasPrefix("is")) {
  336. return .icelandic
  337. }
  338. if (_appleLanguage.hasPrefix("ckb")) {
  339. return .kurdish
  340. }
  341. if (_appleLanguage.hasPrefix("lt")) {
  342. return .lithuanian
  343. }
  344. if (_appleLanguage.hasPrefix("lv")) {
  345. return .latvian
  346. }
  347. // if (_appleLanguage.hasPrefix("")) {
  348. //
  349. // }
  350. return nil
  351. }
  352. // public func appleLanguagePrefix(appleLanguage: String?) -> String? {
  353. // guard let _appleLanguage = appleLanguage else {
  354. // return nil
  355. // }
  356. //
  357. // if _appleLanguage.hasPrefix("zh-Hans") {
  358. // return "zh-Hans"
  359. // } else if _appleLanguage.hasPrefix("en") {
  360. // return "en"
  361. // } else if let result = newValue?.hasPrefix("zh-Hant"), result {
  362. // ocrLanuguageIndex = 1
  363. // } else if let result = newValue?.hasPrefix("fr"), result {
  364. // ocrLanuguageIndex = 3
  365. // } else if let result = newValue?.hasPrefix("de"), result {
  366. // ocrLanuguageIndex = 4
  367. // } else if let result = newValue?.hasPrefix("ht"), result { /// 海地克里奥尔语 Haitian
  368. // ocrLanuguageIndex = 5
  369. // } else if let result = newValue?.hasPrefix("ja"), result {
  370. // ocrLanuguageIndex = 6
  371. // } else if let result = newValue?.hasPrefix("ko"), result {
  372. // ocrLanuguageIndex = 7
  373. // } else if let result = newValue?.hasPrefix("la"), result { /// 拉丁语 Latin
  374. // ocrLanuguageIndex = 8
  375. // } else {
  376. // ocrLanuguageIndex = 2
  377. // }
  378. // }
  379. }
  380. extension KMPDFConvertManager {
  381. func getOCRLanguages(sortMode: KMSortMode = .none) -> [COCRLanguage] {
  382. if (sortMode == .none) {
  383. return self.ocrLanguages
  384. }
  385. let result = self.ocrLanguages.sorted {[unowned self] lan1, lan2 in
  386. if self.ocrLanguageString(ocrLanguage: lan1) > self.ocrLanguageString(ocrLanguage: lan2) {
  387. return sortMode != .ascending
  388. }
  389. return sortMode == .ascending
  390. }
  391. return result
  392. }
  393. func getOCRLanguageStrings(sortMode: KMSortMode = .none) -> [String] {
  394. let ocrLanguages = self.getOCRLanguages(sortMode: sortMode)
  395. var array: [String] = []
  396. for ocrLanguage in ocrLanguages {
  397. array.append(self.ocrLanguageString(ocrLanguage: ocrLanguage))
  398. }
  399. return array
  400. }
  401. }
  402. /*
  403. return [.chinese, .chineseTraditional, .english, .japanese, .korean,
  404. // // Latin
  405. .serbian, .occitan, .danish, .german, .french, .italian, .spanish, .portuguese, .maori, .malay,
  406. .maltese, .dutch, .norwegian, .polish, .romanian, .slovak, .slovenian, .albanian, .swedish, .swahili,
  407. .tagalog, .turish, .uzbek, .vietnamese, .afrikaans, .azerbaijani, .bosnian, .czech, .welsh, .estonian,
  408. .irish, .croatian, .hungarian, .indonesian, .icelandic, .kurdish, .lithuanian, .latvian,
  409. // Devanagari
  410. // .marathi, .nepali, .hindi, .bihari, .maithili, .angika, .bhojpuri, .magahi, .nagpur, .newari,
  411. // .goanKonkani, .saudiArabia
  412. ]
  413. chinese 简体中文 zh-Hans-CN
  414. chineseTraditional 繁体中文 zh-Hant-CN
  415. english 英语 en-CN
  416. japanese 日语 ja-CN
  417. korean 韩语 ko
  418. serbian 塞尔维亚语 sr-CN sr-Latn-CN
  419. occitan 奥克西坦语 oc-CN
  420. danish 丹麦语 da-CN
  421. german 德语 de-AT
  422. french 法语 fr
  423. italian 意大利 it-CH
  424. spanish 西班牙 es-419
  425. portuguese 葡萄牙语 pt-BR
  426. maori 毛利人 mi-CN
  427. malay 马来语 ms-CN
  428. maltese 马耳他语 mt-CN
  429. dutch 荷兰 nl-CN
  430. norwegian 挪威语 nb-CN
  431. polish 波兰语 pl-CN
  432. romanian 罗马尼亚语 ro-CN
  433. slovak 斯洛伐克 sk-CN
  434. slovenian 斯洛文尼亚人 sl-CN
  435. albanian 阿尔巴尼亚语 sq-CN
  436. wedish 瑞典 sv-CN s
  437. swahili 斯瓦希里语 sw-CN
  438. tagalog 塔加洛语 Filipino 菲律宾语 fil-CN
  439. turish(Turkish) 土耳其语 tr-CN
  440. uzbek 乌兹别克 uz-Cyrl-CN
  441. vietnamese 越南 vi-CN
  442. afrikaans 南非荷兰语 af-CN
  443. azerbaijani 阿塞拜疆 az-CN
  444. bosnian 波斯尼亚 bs-CN
  445. czech 捷克 cs-CN
  446. welsh 威尔士 cy-CN
  447. estonian(eston) 爱沙尼亚 et-CN
  448. irish 爱尔兰 ga-CN
  449. croatian 克罗地亚 hr-CN
  450. hungarian 匈牙利 hu-CN
  451. indonesian 印度尼西亚 id-CN
  452. icelandic 冰岛 is-CN
  453. kurdish 库尔迪什语 ckb-CN
  454. lithuanian 立陶宛语 lt-CN
  455. latvian 拉脱维亚 lv-CN
  456. */