KMPageEditSplitSettingModel.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // KMPageEditSplitSettingModel.swift
  3. // PDF Reader Pro
  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. var files: Array<IndexSet> = []
  58. for i in 0 ..< fileCount {
  59. var indexs: IndexSet = []
  60. let offset: Int = i < pageOffset ? 1 : 0
  61. for j in 0 ..< (avgNumberPage+offset) {
  62. if (i * avgNumberPage + j >= pageCount) {
  63. break
  64. }
  65. indexs.insert(i * avgNumberPage + j)
  66. }
  67. files.append(indexs)
  68. }
  69. return files
  70. }
  71. if (self.type == 2) { /// 按页面范围
  72. var indexSet1: IndexSet = []
  73. var indexSet2: IndexSet = []
  74. if (self.pageRangeType == 0) { /// 奇数页
  75. for i in 0 ..< pageCount {
  76. if (i % 2 == 0) {
  77. indexSet1.insert(i)
  78. } else {
  79. indexSet2.insert(i)
  80. }
  81. }
  82. } else if (self.pageRangeType == 1) { /// 偶数页
  83. for i in 0 ..< pageCount {
  84. if (i % 2 == 1) {
  85. indexSet1.insert(i)
  86. } else {
  87. indexSet2.insert(i)
  88. }
  89. }
  90. } else { /// 自定义
  91. let selectedPages = KMPageRangeTools.findSelectPage(pageRangeString: self.pageRangeString, pageCount: pageCount)
  92. if (selectedPages.count <= 0) {
  93. return nil
  94. }
  95. var tmpPages: [Int] = []
  96. for page in selectedPages {
  97. if (page > 0) {
  98. tmpPages.append(page-1)
  99. }
  100. }
  101. for i in 0 ..< pageCount {
  102. if (tmpPages.contains(i)) {
  103. indexSet1.insert(i)
  104. } else {
  105. indexSet2.insert(i)
  106. }
  107. }
  108. }
  109. var files: Array<IndexSet> = []
  110. if (indexSet1.count > 0) {
  111. files.append(indexSet1)
  112. }
  113. if (indexSet2.count > 0) {
  114. files.append(indexSet2)
  115. }
  116. return files
  117. }
  118. return nil
  119. }
  120. }
  121. override var outputFileName: String {
  122. get {
  123. guard let _fileName = self.fileName, !_fileName.isEmpty else {
  124. return ""
  125. }
  126. var result: String = ""
  127. var newFileName: String = ""
  128. // let pathExtension = _fileName.components(separatedBy: ".").last
  129. let files = _fileName.split(separator: ".")
  130. for i in 0 ..< files.count-1 {
  131. let file: String = String(files[i])
  132. newFileName.append(file)
  133. if (i < files.count-2) {
  134. newFileName.append(".")
  135. }
  136. }
  137. if (self.origFileNameIsPrefix) {
  138. result.append(newFileName)
  139. if (self.hasSep) {
  140. result.append(self.sepString)
  141. }
  142. if (self.hasTag) {
  143. result.append("\(self.tagString)")
  144. }
  145. } else {
  146. if (self.hasTag) {
  147. result.append("\(self.tagString)")
  148. }
  149. if (self.hasSep) {
  150. result.append(self.sepString)
  151. }
  152. result.append(newFileName)
  153. }
  154. self.outputFileNameDeletePathExtension = result
  155. result.append(".")
  156. result.append(self.pathExtension ?? "pdf")
  157. return result
  158. }
  159. }
  160. func getNewOutputFileName(index: Int) -> String {
  161. guard let _fileName = self.fileName, !_fileName.isEmpty else {
  162. return ""
  163. }
  164. var result: String = ""
  165. var newFileName: String = ""
  166. let files = _fileName.split(separator: ".")
  167. for i in 0 ..< files.count-1 {
  168. let file: String = String(files[i])
  169. newFileName.append(file)
  170. if (i < files.count-2) {
  171. newFileName.append(".")
  172. }
  173. }
  174. if (self.origFileNameIsPrefix) {
  175. result.append(newFileName)
  176. if (self.hasSep) {
  177. result.append(self.sepString)
  178. }
  179. if (self.hasTag) {
  180. result.append("\(self.tagString) \(index)")
  181. } else {
  182. result.append("\(index)")
  183. }
  184. } else {
  185. if (self.hasTag) {
  186. result.append("\(self.tagString) \(index)")
  187. } else {
  188. result.append("\(index)")
  189. }
  190. if (self.hasSep) {
  191. result.append(self.sepString)
  192. }
  193. result.append(newFileName)
  194. }
  195. // 文件名兼容 /
  196. result = result.replacingOccurrences(of: "/", with: ":")
  197. return result
  198. }
  199. }