// // KMPageEditSplitSettingModel.swift // PDF Master // // Created by tangchao on 2023/1/12. // import Cocoa class KMPageEditSplitSettingModel: KMPageEditSettingBaseModel { var type: Int = 0 var avgNumberForOneFile: Int = 1 var avgSplitNumberFile: Int = 1 var pageRangeType: Int = 1 var pageRangeString: String = "" var origFileNameIsPrefix: Bool = true var hasTag: Bool = true var tagString: String = "part" var hasSep: Bool = true var sepString: String = "-" var getSplitIndexSets: Array? { get { if (type < 0 || type > 2) { return nil } if (self.pageCount == NSNotFound) { return nil } let pageCount = self.pageCount if (self.type == 0) { /// 平均每几页拆分 if (self.avgNumberForOneFile <= 0) { return nil } let avgNumberPage = self.avgNumberForOneFile var fileCount: Int = (pageCount / avgNumberPage) if (pageCount % avgNumberPage != 0) { fileCount += 1 } var files: Array = [] for i in 0 ..< fileCount { var indexs: IndexSet = [] for j in 0 ..< avgNumberPage { if (i * avgNumberPage + j >= pageCount) { break } indexs.insert(i * avgNumberPage + j) } files.append(indexs) } return files } if (self.type == 1) { /// 平均拆分几个文档 if (self.avgSplitNumberFile <= 0) { return nil } let fileCount: Int = self.avgSplitNumberFile var avgNumberPage = (pageCount / fileCount) let pageOffset = pageCount % fileCount // if (pageCount % fileCount != 0) { // avgNumberPage += 1 // } var files: Array = [] for i in 0 ..< fileCount { var indexs: IndexSet = [] let offset: Int = i < pageOffset ? 1 : 0 for j in 0 ..< (avgNumberPage+offset) { if (i * avgNumberPage + j >= pageCount) { break } indexs.insert(i * avgNumberPage + j) } files.append(indexs) } return files } if (self.type == 2) { /// 按页面范围 var indexSet1: IndexSet = [] var indexSet2: IndexSet = [] if (self.pageRangeType == 0) { /// 奇数页 for i in 0 ..< pageCount { if (i % 2 == 0) { indexSet1.insert(i) } else { indexSet2.insert(i) } } } else if (self.pageRangeType == 1) { /// 偶数页 for i in 0 ..< pageCount { if (i % 2 == 1) { indexSet1.insert(i) } else { indexSet2.insert(i) } } } else { /// 自定义 let selectedPages = KMPageRangeTools.findSelectPage(pageRangeString: self.pageRangeString, pageCount: pageCount) if (selectedPages.count <= 0) { return nil } var tmpPages: [Int] = [] for page in selectedPages { if (page > 0) { tmpPages.append(page-1) } } for i in 0 ..< pageCount { if (tmpPages.contains(i)) { indexSet1.insert(i) } else { indexSet2.insert(i) } } } var files: Array = [] if (indexSet1.count > 0) { files.append(indexSet1) } if (indexSet2.count > 0) { files.append(indexSet2) } return files } return nil } } override var outputFileName: String { get { guard let _fileName = self.fileName, !_fileName.isEmpty else { return "" } var result: String = "" var newFileName: String = "" // let pathExtension = _fileName.components(separatedBy: ".").last let files = _fileName.split(separator: ".") for i in 0 ..< files.count-1 { let file: String = String(files[i]) newFileName.append(file) if (i < files.count-2) { newFileName.append(".") } } if (self.origFileNameIsPrefix) { result.append(newFileName) if (self.hasSep) { result.append(self.sepString) } if (self.hasTag) { result.append("\(self.tagString)") } } else { if (self.hasTag) { result.append("\(self.tagString)") } if (self.hasSep) { result.append(self.sepString) } result.append(newFileName) } self.outputFileNameDeletePathExtension = result result.append(".") result.append(self.pathExtension) return result } } func getNewOutputFileName(index: Int) -> String { guard let _fileName = self.fileName, !_fileName.isEmpty else { return "" } var result: String = "" var newFileName: String = "" let files = _fileName.split(separator: ".") for i in 0 ..< files.count-1 { let file: String = String(files[i]) newFileName.append(file) if (i < files.count-2) { newFileName.append(".") } } if (self.origFileNameIsPrefix) { result.append(newFileName) if (self.hasSep) { result.append(self.sepString) } if (self.hasTag) { result.append("\(self.tagString) \(index)") } else { result.append("\(index)") } } else { if (self.hasTag) { result.append("\(self.tagString) \(index)") } else { result.append("\(index)") } if (self.hasSep) { result.append(self.sepString) } result.append(newFileName) } // 文件名兼容 / result = result.replacingOccurrences(of: "/", with: ":") return result } }