123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- //
- // 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
- */
|