KMPageEditSplitSettingModel.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // KMPageEditSplitSettingModel.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/1/12.
  6. //
  7. import Cocoa
  8. class KMPageEditSplitSettingModel: KMPageEditSettingBaseModel {
  9. var type: Int = 0
  10. var avgNumberForOneFile: Int = 1
  11. var avgSplitNumberFile: Int = 1
  12. var pageRangeType: Int = 1
  13. var pageRangeString: String = ""
  14. var origFileNameIsPrefix: Bool = true
  15. var hasTag: Bool = true
  16. var tagString: String = "part"
  17. var hasSep: Bool = true
  18. var sepString: String = "-"
  19. var getSplitIndexSets: Array<IndexSet>? {
  20. get {
  21. if (type < 0 || type > 2) {
  22. return nil
  23. }
  24. if (self.pageCount == NSNotFound) {
  25. return nil
  26. }
  27. let pageCount = self.pageCount
  28. if (self.type == 0) { /// 平均每几页拆分
  29. if (self.avgNumberForOneFile <= 0) {
  30. return nil
  31. }
  32. let avgNumberPage = self.avgNumberForOneFile
  33. var fileCount: Int = (pageCount / avgNumberPage)
  34. if (pageCount % avgNumberPage != 0) {
  35. fileCount += 1
  36. }
  37. var files: Array<IndexSet> = []
  38. for i in 0 ..< fileCount {
  39. var indexs: IndexSet = []
  40. for j in 0 ..< avgNumberPage {
  41. if (i * avgNumberPage + j >= pageCount) {
  42. break
  43. }
  44. indexs.insert(i * avgNumberPage + j)
  45. }
  46. files.append(indexs)
  47. }
  48. return files
  49. }
  50. if (self.type == 1) { /// 平均拆分几个文档
  51. if (self.avgSplitNumberFile <= 0) {
  52. return nil
  53. }
  54. let fileCount: Int = self.avgSplitNumberFile
  55. var avgNumberPage = (pageCount / fileCount)
  56. let pageOffset = pageCount % fileCount
  57. // if (pageCount % fileCount != 0) {
  58. // avgNumberPage += 1
  59. // }
  60. var files: Array<IndexSet> = []
  61. for i in 0 ..< fileCount {
  62. var indexs: IndexSet = []
  63. let offset: Int = i < pageOffset ? 1 : 0
  64. for j in 0 ..< (avgNumberPage+offset) {
  65. if (i * avgNumberPage + j >= pageCount) {
  66. break
  67. }
  68. indexs.insert(i * avgNumberPage + j)
  69. }
  70. files.append(indexs)
  71. }
  72. return files
  73. }
  74. if (self.type == 2) { /// 按页面范围
  75. var indexSet1: IndexSet = []
  76. var indexSet2: IndexSet = []
  77. if (self.pageRangeType == 0) { /// 奇数页
  78. for i in 0 ..< pageCount {
  79. if (i % 2 == 0) {
  80. indexSet1.insert(i)
  81. } else {
  82. indexSet2.insert(i)
  83. }
  84. }
  85. } else if (self.pageRangeType == 1) { /// 偶数页
  86. for i in 0 ..< pageCount {
  87. if (i % 2 == 1) {
  88. indexSet1.insert(i)
  89. } else {
  90. indexSet2.insert(i)
  91. }
  92. }
  93. } else { /// 自定义
  94. let selectedPages = KMPageRangeTools.findSelectPage(pageRangeString: self.pageRangeString, pageCount: pageCount)
  95. if (selectedPages.count <= 0) {
  96. return nil
  97. }
  98. var tmpPages: [Int] = []
  99. for page in selectedPages {
  100. if (page > 0) {
  101. tmpPages.append(page-1)
  102. }
  103. }
  104. for i in 0 ..< pageCount {
  105. if (tmpPages.contains(i)) {
  106. indexSet1.insert(i)
  107. } else {
  108. indexSet2.insert(i)
  109. }
  110. }
  111. }
  112. var files: Array<IndexSet> = []
  113. if (indexSet1.count > 0) {
  114. files.append(indexSet1)
  115. }
  116. if (indexSet2.count > 0) {
  117. files.append(indexSet2)
  118. }
  119. return files
  120. }
  121. return nil
  122. }
  123. }
  124. override var outputFileName: String {
  125. get {
  126. guard let _fileName = self.fileName, !_fileName.isEmpty else {
  127. return ""
  128. }
  129. var result: String = ""
  130. var newFileName: String = ""
  131. // let pathExtension = _fileName.components(separatedBy: ".").last
  132. let files = _fileName.split(separator: ".")
  133. for i in 0 ..< files.count-1 {
  134. let file: String = String(files[i])
  135. newFileName.append(file)
  136. if (i < files.count-2) {
  137. newFileName.append(".")
  138. }
  139. }
  140. if (self.origFileNameIsPrefix) {
  141. result.append(newFileName)
  142. if (self.hasSep) {
  143. result.append(self.sepString)
  144. }
  145. if (self.hasTag) {
  146. result.append("\(self.tagString)")
  147. }
  148. } else {
  149. if (self.hasTag) {
  150. result.append("\(self.tagString)")
  151. }
  152. if (self.hasSep) {
  153. result.append(self.sepString)
  154. }
  155. result.append(newFileName)
  156. }
  157. self.outputFileNameDeletePathExtension = result
  158. result.append(".")
  159. result.append(self.pathExtension)
  160. return result
  161. }
  162. }
  163. func getNewOutputFileName(index: Int) -> String {
  164. guard let _fileName = self.fileName, !_fileName.isEmpty else {
  165. return ""
  166. }
  167. var result: String = ""
  168. var newFileName: String = ""
  169. let files = _fileName.split(separator: ".")
  170. for i in 0 ..< files.count-1 {
  171. let file: String = String(files[i])
  172. newFileName.append(file)
  173. if (i < files.count-2) {
  174. newFileName.append(".")
  175. }
  176. }
  177. if (self.origFileNameIsPrefix) {
  178. result.append(newFileName)
  179. if (self.hasSep) {
  180. result.append(self.sepString)
  181. }
  182. if (self.hasTag) {
  183. result.append("\(self.tagString) \(index)")
  184. } else {
  185. result.append("\(index)")
  186. }
  187. } else {
  188. if (self.hasTag) {
  189. result.append("\(self.tagString) \(index)")
  190. } else {
  191. result.append("\(index)")
  192. }
  193. if (self.hasSep) {
  194. result.append(self.sepString)
  195. }
  196. result.append(newFileName)
  197. }
  198. // 文件名兼容 /
  199. result = result.replacingOccurrences(of: "/", with: ":")
  200. return result
  201. }
  202. }