KMPageEditSplitSettingModel.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. if (pageCount % fileCount != 0) {
  57. avgNumberPage += 1
  58. }
  59. var files: Array<IndexSet> = []
  60. for i in 0 ..< fileCount {
  61. var indexs: IndexSet = []
  62. for j in 0 ..< avgNumberPage {
  63. if (i * avgNumberPage + j >= pageCount) {
  64. break
  65. }
  66. indexs.insert(i * avgNumberPage + j)
  67. }
  68. files.append(indexs)
  69. }
  70. return files
  71. }
  72. if (self.type == 2) { /// 按页面范围
  73. var indexSet1: IndexSet = []
  74. var indexSet2: IndexSet = []
  75. if (self.pageRangeType == 0) { /// 奇数页
  76. for i in 0 ..< pageCount {
  77. if (i % 2 == 0) {
  78. indexSet1.insert(i)
  79. } else {
  80. indexSet2.insert(i)
  81. }
  82. }
  83. } else if (self.pageRangeType == 1) { /// 偶数页
  84. for i in 0 ..< pageCount {
  85. if (i % 2 == 1) {
  86. indexSet1.insert(i)
  87. } else {
  88. indexSet2.insert(i)
  89. }
  90. }
  91. } else { /// 自定义
  92. let selectedPages = KMPageRangeTools.findSelectPage(pageRangeString: self.pageRangeString, pageCount: pageCount)
  93. if (selectedPages.count <= 0) {
  94. return nil
  95. }
  96. for i in 0 ..< pageCount {
  97. if (selectedPages.contains(i)) {
  98. indexSet1.insert(i)
  99. } else {
  100. indexSet2.insert(i)
  101. }
  102. }
  103. }
  104. var files: Array<IndexSet> = []
  105. if (indexSet1.count > 0) {
  106. files.append(indexSet1)
  107. }
  108. if (indexSet2.count > 0) {
  109. files.append(indexSet2)
  110. }
  111. return files
  112. }
  113. return nil
  114. }
  115. }
  116. override var outputFileName: String {
  117. get {
  118. if (fileName == nil || fileName.isEmpty) {
  119. return ""
  120. }
  121. var result: String = ""
  122. var newFileName: String = ""
  123. let pathExtension = fileName.components(separatedBy: ".").last
  124. let files = fileName.split(separator: ".")
  125. for i in 0 ..< files.count-1 {
  126. let file: String = String(files[i])
  127. newFileName.append(file)
  128. if (i < files.count-2) {
  129. newFileName.append(".")
  130. }
  131. }
  132. if (origFileNameIsPrefix) {
  133. result.append(newFileName)
  134. if (hasSep) {
  135. result.append(sepString)
  136. }
  137. if (hasTag) {
  138. result.append(tagString)
  139. }
  140. } else {
  141. if (hasTag) {
  142. result.append(tagString)
  143. }
  144. if (hasSep) {
  145. result.append(sepString)
  146. }
  147. result.append(newFileName)
  148. }
  149. self.outputFileNameDeletePathExtension = result
  150. result.append(".")
  151. result.append(self.pathExtension)
  152. return result
  153. }
  154. }
  155. }