123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- //
- // 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<IndexSet>? {
- 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<IndexSet> = []
- 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<IndexSet> = []
- 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<IndexSet> = []
- 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
- }
- }
|