// // KMPDFConvertManager.swift // PDF Master // // Created by tangchao on 2022/12/7. // import Cocoa class KMPDFConvertManager: NSObject { static let defaultManager = KMPDFConvertManager() var operationQueue: OperationQueue = OperationQueue() lazy var ocrLanguages: [COCRLanguage] = { return [.chinese, .chineseTraditional, .english, .japanese, .korean, // // Latin .serbian, .occitan, .danish, .german, .french, .italian, .spanish, .portuguese, .maori, .malay, .maltese, .dutch, .norwegian, .polish, .romanian, .slovak, .slovenian, .albanian, .swedish, .swahili, .tagalog, .turish, .uzbek, .vietnamese, .afrikaans, .azerbaijani, .bosnian, .czech, .welsh, .estonian, .irish, .croatian, .hungarian, .indonesian, .icelandic, .kurdish, .lithuanian, .latvian, // Devanagari // .marathi, .nepali, .hindi, .bihari, .maithili, .angika, .bhojpuri, .magahi, .nagpur, .newari, // .goanKonkani, .saudiArabia ] }() lazy var ocrLanguageStrings: [String] = { var array: [String] = [] for ocrLanguage in self.ocrLanguages { array.append(self.ocrLanguageString(ocrLanguage: ocrLanguage)) } return array }() private var _languages_asc: [COCRLanguage] = [] var languages_asc: [COCRLanguage] { get { if (!self._languages_asc.isEmpty) { return self._languages_asc } self._languages_asc = self.getOCRLanguages(sortMode: .ascending) return self._languages_asc } } private var _languages_desc: [COCRLanguage] = [] var languages_desc: [COCRLanguage] { get { if (!self._languages_desc.isEmpty) { return self._languages_desc } self._languages_desc = self.getOCRLanguages(sortMode: .descending) return self._languages_desc } } private var _languages_asc_strings: [String] = [] var languages_asc_strings: [String] { get { if (!self._languages_asc_strings.isEmpty) { return self._languages_asc_strings } self._languages_asc_strings = self.getOCRLanguageStrings(sortMode: .ascending) return self._languages_asc_strings } } private var _languages_desc_strings: [String] = [] var languages_desc_strings: [String] { get { if (!self._languages_desc_strings.isEmpty) { return self._languages_desc_strings } self._languages_desc_strings = self.getOCRLanguageStrings(sortMode: .descending) return self._languages_desc_strings } } override init() { super.init() operationQueue.maxConcurrentOperationCount = 1 } func convert(convert: KMPDFConvert, completion: @escaping (_ finished: Bool, _ error: Error?) -> ()) { convert.callback = completion operationQueue.addOperation(convert) } func convert(convert: KMPDFConvert, progress:@escaping (Int)->Void, completion: @escaping (_ finished: Bool, _ error: Error?) -> ()) { convert.callback = completion convert.progress = progress operationQueue.addOperation(convert) } func cancel(convert: KMPDFConvert) { convert.cancel() self.operationQueue.cancelAllOperations() } public func ocrLanguageString(ocrLanguage: COCRLanguage) -> String { switch ocrLanguage { case .chinese: return NSLocalizedString("Chinese", comment: "") case .chineseTraditional: return NSLocalizedString("Chinese Traditional", comment: "") case .english: return NSLocalizedString("English", comment: "") case .japanese: return NSLocalizedString("Japanese", comment: "") case .korean: return NSLocalizedString("Kroean", comment: "") /// Latin case .serbian: return NSLocalizedString("Serbian", comment: "") case .occitan: return NSLocalizedString("Occitan", comment: "") case .danish: return NSLocalizedString("Danish", comment: "") case .german: return NSLocalizedString("German", comment: "") case .french: return NSLocalizedString("French", comment: "") case .italian: return NSLocalizedString("Italian", comment: "") case .spanish: return NSLocalizedString("Spanish", comment: "") case .portuguese: return NSLocalizedString("Portuguese", comment: "") case .maori: return NSLocalizedString("Maori", comment: "") case .malay: return NSLocalizedString("Malay", comment: "") case .maltese: return NSLocalizedString("Maltese", comment: "") case .dutch: return NSLocalizedString("Dutch", comment: "") case .norwegian: return NSLocalizedString("Norwegian", comment: "") case .polish: return NSLocalizedString("Polish", comment: "") case .romanian: return NSLocalizedString("Romanian", comment: "") case .slovak: return NSLocalizedString("Slovak", comment: "") case .slovenian: return NSLocalizedString("Slovenian", comment: "") case .albanian: return NSLocalizedString("Albanian", comment: "") case .swedish: return NSLocalizedString("Swedish", comment: "") case .swahili: return NSLocalizedString("Swahili", comment: "") case .tagalog: return NSLocalizedString("Tagalog", comment: "") case .turish: return NSLocalizedString("Turish", comment: "") case .uzbek: return NSLocalizedString("Uzbek", comment: "") case .vietnamese: return NSLocalizedString("Vietnamese", comment: "") case .afrikaans: return NSLocalizedString("Afrikaans", comment: "") case .azerbaijani: return NSLocalizedString("Azerbaijani", comment: "") case .bosnian: return NSLocalizedString("Bosnian", comment: "") case .czech: return NSLocalizedString("Czech", comment: "") case .welsh: return NSLocalizedString("Welsh", comment: "") case .estonian: return NSLocalizedString("Estonian", comment: "") case .irish: return NSLocalizedString("Irish", comment: "") case .croatian: return NSLocalizedString("Croatian", comment: "") case .hungarian: return NSLocalizedString("Hungarian", comment: "") case .indonesian: return NSLocalizedString("Indonesian", comment: "") case .icelandic: return NSLocalizedString("Icelandic", comment: "") case .kurdish: return NSLocalizedString("Kurdish", comment: "") case .lithuanian: return NSLocalizedString("Lithuanian", comment: "") case .latvian: return NSLocalizedString("Latvian", comment: "") /// Devanagari case .marathi: return NSLocalizedString("Marathi", comment: "") case .nepali: return NSLocalizedString("Nepali", comment: "") case .hindi: return NSLocalizedString("Hindi", comment: "") case .bihari: return NSLocalizedString("Bihari", comment: "") case .maithili: return NSLocalizedString("Maithili", comment: "") case .angika: return NSLocalizedString("Angika", comment: "") case .bhojpuri: return NSLocalizedString("Bhojpuri", comment: "") case .magahi: return NSLocalizedString("Magahi", comment: "") case .nagpur: return NSLocalizedString("Nagpur", comment: "") case .newari: return NSLocalizedString("Newari", comment: "") case .goanKonkani: return NSLocalizedString("GoanKonkani", comment: "") case .saudiArabia: return NSLocalizedString("SaudiArabia", comment: "") } } // func ocrLanguageIsSupport(appleLanguage: String?) -> Bool { if let _ = KMPDFConvertManager.defaultManager._ocrLanguage(appleLanguage: appleLanguage) { return true } return false } func ocrLanguage(appleLanguage: String?) -> COCRLanguage? { return KMPDFConvertManager.defaultManager._ocrLanguage(appleLanguage: appleLanguage) } // MARK: - Private Methods fileprivate func _ocrLanguage(appleLanguage: String?) -> COCRLanguage? { guard let _appleLanguage = appleLanguage else { return nil } if (_appleLanguage.hasPrefix("zh-Hans")) { return .chinese } if (_appleLanguage.hasPrefix("zh-Hant")) { return .chineseTraditional } if (_appleLanguage.hasPrefix("en")) { return .english } if (_appleLanguage.hasPrefix("ja")) { return .japanese } if (_appleLanguage.hasPrefix("ko")) { return .korean } if (_appleLanguage.hasPrefix("sr")) { return .serbian } if (_appleLanguage.hasPrefix("oc")) { return .occitan } if (_appleLanguage.hasPrefix("da")) { return .danish } if (_appleLanguage.hasPrefix("de")) { return .german } if (_appleLanguage.hasPrefix("fr")) { return .french } if (_appleLanguage.hasPrefix("it")) { return .italian } if (_appleLanguage.hasPrefix("es")) { return .spanish } if (_appleLanguage.hasPrefix("pt")) { return .portuguese } if (_appleLanguage.hasPrefix("mi")) { return .maori } if (_appleLanguage.hasPrefix("ms")) { return .malay } if (_appleLanguage.hasPrefix("mt")) { return .maltese } if (_appleLanguage.hasPrefix("nl")) { return .dutch } if (_appleLanguage.hasPrefix("nb")) { return .norwegian } if (_appleLanguage.hasPrefix("pl")) { return .polish } if (_appleLanguage.hasPrefix("ro")) { return .romanian } if (_appleLanguage.hasPrefix("sk")) { return .slovak } if (_appleLanguage.hasPrefix("sl")) { return .slovenian } if (_appleLanguage.hasPrefix("sq")) { return .albanian } if (_appleLanguage.hasPrefix("sv")) { return .welsh } if (_appleLanguage.hasPrefix("sw")) { return .swahili } if (_appleLanguage.hasPrefix("fil")) { // tagalog 塔加洛语 Filipino 菲律宾语 return .tagalog } if (_appleLanguage.hasPrefix("tr")) { // Turkish return .turish } if (_appleLanguage.hasPrefix("uz")) { return .uzbek } if (_appleLanguage.hasPrefix("vi")) { return .vietnamese } if (_appleLanguage.hasPrefix("af")) { return .afrikaans } if (_appleLanguage.hasPrefix("az")) { return .azerbaijani } if (_appleLanguage.hasPrefix("bs")) { return .bosnian } if (_appleLanguage.hasPrefix("cs")) { return .czech } if (_appleLanguage.hasPrefix("cy")) { return .welsh } if (_appleLanguage.hasPrefix("et")) { // eston return .estonian } if (_appleLanguage.hasPrefix("ga")) { return .irish } if (_appleLanguage.hasPrefix("hr")) { return .croatian } if (_appleLanguage.hasPrefix("hu")) { return .hungarian } if (_appleLanguage.hasPrefix("id")) { return .indonesian } if (_appleLanguage.hasPrefix("is")) { return .icelandic } if (_appleLanguage.hasPrefix("ckb")) { return .kurdish } if (_appleLanguage.hasPrefix("lt")) { return .lithuanian } if (_appleLanguage.hasPrefix("lv")) { return .latvian } // if (_appleLanguage.hasPrefix("")) { // // } return nil } // public func appleLanguagePrefix(appleLanguage: String?) -> String? { // guard let _appleLanguage = appleLanguage else { // return nil // } // // if _appleLanguage.hasPrefix("zh-Hans") { // return "zh-Hans" // } else if _appleLanguage.hasPrefix("en") { // return "en" // } else if let result = newValue?.hasPrefix("zh-Hant"), result { // ocrLanuguageIndex = 1 // } else if let result = newValue?.hasPrefix("fr"), result { // ocrLanuguageIndex = 3 // } else if let result = newValue?.hasPrefix("de"), result { // ocrLanuguageIndex = 4 // } else if let result = newValue?.hasPrefix("ht"), result { /// 海地克里奥尔语 Haitian // ocrLanuguageIndex = 5 // } else if let result = newValue?.hasPrefix("ja"), result { // ocrLanuguageIndex = 6 // } else if let result = newValue?.hasPrefix("ko"), result { // ocrLanuguageIndex = 7 // } else if let result = newValue?.hasPrefix("la"), result { /// 拉丁语 Latin // ocrLanuguageIndex = 8 // } else { // ocrLanuguageIndex = 2 // } // } } extension KMPDFConvertManager { func getOCRLanguages(sortMode: KMSortMode = .none) -> [COCRLanguage] { if (sortMode == .none) { return self.ocrLanguages } let result = self.ocrLanguages.sorted {[unowned self] lan1, lan2 in if self.ocrLanguageString(ocrLanguage: lan1) > self.ocrLanguageString(ocrLanguage: lan2) { return sortMode != .ascending } return sortMode == .ascending } return result } func getOCRLanguageStrings(sortMode: KMSortMode = .none) -> [String] { let ocrLanguages = self.getOCRLanguages(sortMode: sortMode) var array: [String] = [] for ocrLanguage in ocrLanguages { array.append(self.ocrLanguageString(ocrLanguage: ocrLanguage)) } return array } } /* return [.chinese, .chineseTraditional, .english, .japanese, .korean, // // Latin .serbian, .occitan, .danish, .german, .french, .italian, .spanish, .portuguese, .maori, .malay, .maltese, .dutch, .norwegian, .polish, .romanian, .slovak, .slovenian, .albanian, .swedish, .swahili, .tagalog, .turish, .uzbek, .vietnamese, .afrikaans, .azerbaijani, .bosnian, .czech, .welsh, .estonian, .irish, .croatian, .hungarian, .indonesian, .icelandic, .kurdish, .lithuanian, .latvian, // Devanagari // .marathi, .nepali, .hindi, .bihari, .maithili, .angika, .bhojpuri, .magahi, .nagpur, .newari, // .goanKonkani, .saudiArabia ] chinese 简体中文 zh-Hans-CN chineseTraditional 繁体中文 zh-Hant-CN english 英语 en-CN japanese 日语 ja-CN korean 韩语 ko serbian 塞尔维亚语 sr-CN sr-Latn-CN occitan 奥克西坦语 oc-CN danish 丹麦语 da-CN german 德语 de-AT french 法语 fr italian 意大利 it-CH spanish 西班牙 es-419 portuguese 葡萄牙语 pt-BR maori 毛利人 mi-CN malay 马来语 ms-CN maltese 马耳他语 mt-CN dutch 荷兰 nl-CN norwegian 挪威语 nb-CN polish 波兰语 pl-CN romanian 罗马尼亚语 ro-CN slovak 斯洛伐克 sk-CN slovenian 斯洛文尼亚人 sl-CN albanian 阿尔巴尼亚语 sq-CN wedish 瑞典 sv-CN s swahili 斯瓦希里语 sw-CN tagalog 塔加洛语 Filipino 菲律宾语 fil-CN turish(Turkish) 土耳其语 tr-CN uzbek 乌兹别克 uz-Cyrl-CN vietnamese 越南 vi-CN afrikaans 南非荷兰语 af-CN azerbaijani 阿塞拜疆 az-CN bosnian 波斯尼亚 bs-CN czech 捷克 cs-CN welsh 威尔士 cy-CN estonian(eston) 爱沙尼亚 et-CN irish 爱尔兰 ga-CN croatian 克罗地亚 hr-CN hungarian 匈牙利 hu-CN indonesian 印度尼西亚 id-CN icelandic 冰岛 is-CN kurdish 库尔迪什语 ckb-CN lithuanian 立陶宛语 lt-CN latvian 拉脱维亚 lv-CN */