123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- //
- // 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)
- if (pageCount % fileCount != 0) {
- avgNumberPage += 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 == 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
- }
-
- for i in 0 ..< pageCount {
- if (selectedPages.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 {
- if (fileName == nil || fileName.isEmpty) {
- 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 (origFileNameIsPrefix) {
- result.append(newFileName)
- if (hasSep) {
- result.append(sepString)
- }
- if (hasTag) {
- result.append(tagString)
- }
- } else {
- if (hasTag) {
- result.append(tagString)
- }
- if (hasSep) {
- result.append(sepString)
- }
- result.append(newFileName)
- }
-
- self.outputFileNameDeletePathExtension = result
-
- result.append(".")
- result.append(self.pathExtension)
-
- return result
- }
- }
- }
|