KMNFileAttribute.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // KMFileAttribute.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/10/12.
  6. //
  7. import Cocoa
  8. @objc enum KMNFileAttributePageType: Int {
  9. case AllPages = 0
  10. case OnlyOdd
  11. case OnlyEven
  12. case PagesString
  13. }
  14. @objcMembers class KMNFileAttribute: NSObject {
  15. var filePath: String = ""
  16. var pdfDocument: CPDFDocument?
  17. var bAllPage = false
  18. var pagesString: String = ""
  19. var isLocked = false
  20. var password: String?
  21. var pagesType: KMNFileAttributePageType = .AllPages
  22. private var selectPages: [Int] = []
  23. func fetchSelectPages() -> [Int] {
  24. if let doc = self.pdfDocument, doc.isLocked {
  25. doc.unlock(withPassword: self.password)
  26. }
  27. guard let pdfDocument = pdfDocument else { return [] }
  28. selectPages.removeAll()
  29. if pagesType == .AllPages {
  30. for i in 0..<pdfDocument.pageCount {
  31. selectPages.append(Int(i) + 1)
  32. }
  33. pagesString = "1-\(pdfDocument.pageCount)"
  34. } else if pagesType == .OnlyOdd {
  35. pagesString = ""
  36. for i in 0..<pdfDocument.pageCount where i % 2 == 0 {
  37. selectPages.append(Int(i) + 1)
  38. if pagesString == "" {
  39. pagesString = "\(i + 1)"
  40. } else {
  41. pagesString = "\(pagesString),\(i + 1)"
  42. }
  43. }
  44. } else if pagesType == .OnlyEven {
  45. pagesString = ""
  46. for i in 0..<pdfDocument.pageCount where i % 2 != 0 {
  47. selectPages.append(Int(i) + 1)
  48. if pagesString == "" {
  49. pagesString = "\(i + 1)"
  50. } else {
  51. pagesString = "\(pagesString),\(i + 1)"
  52. }
  53. }
  54. } else {
  55. let _ = isInvalidString(pagesString)
  56. if !bAllPage {
  57. self.quickSort(&selectPages, startIndex: 0, endIndex: selectPages.count-1)
  58. }
  59. }
  60. return selectPages
  61. }
  62. func isInvalidString(_ text: String) -> Bool {
  63. var document: CPDFDocument?
  64. if (pdfDocument != nil) {
  65. document = pdfDocument
  66. } else {
  67. document = CPDFDocument(url:URL(fileURLWithPath: self.filePath ))
  68. }
  69. if let data = document?.isLocked, data {
  70. document?.unlock(withPassword: self.password )
  71. }
  72. var pageNumber = document?.pageCount ?? 0
  73. if (self.bAllPage) {
  74. self.selectPages = []
  75. for i in 1 ... pageNumber {
  76. self.selectPages.append(Int(i))
  77. }
  78. return false
  79. }
  80. var pageNumbers: [Int] = []
  81. var isInvalid = false
  82. for c in text {
  83. if (c != "0" && c != "1" && c != "2" && c != "3" && c != "4" && c != "5" && c != "6" && c != "7" && c != "8" && c != "9" && c != "," && c != "-") {
  84. isInvalid = true
  85. break
  86. }else{
  87. isInvalid = false
  88. }
  89. }
  90. if (!isInvalid) {
  91. let array = text.components(separatedBy: ",")
  92. for s in array {
  93. if s.isEmpty {
  94. isInvalid = true
  95. break
  96. }else{
  97. let pages = s.components(separatedBy: "-")
  98. if (pages.count>2) {
  99. isInvalid = true
  100. break
  101. }else if(pages.count==1){
  102. let p = pages.first!
  103. if p.isEmpty || Int(p)! > pageNumber || Int(p) == 0 {
  104. isInvalid = true
  105. break
  106. }else{
  107. var isEqual = false
  108. for pageNumber in pageNumbers {
  109. if pageNumber == Int(p) {
  110. isEqual = true
  111. isInvalid = true
  112. break
  113. }
  114. }
  115. if (!isEqual) {
  116. pageNumbers.append(Int(p)!)
  117. }
  118. }
  119. }else if(pages.count==2){
  120. let p1 = pages[0]
  121. let p2 = pages[1]
  122. if p1.isEmpty || p2.isEmpty || Int(p1)! >= Int(p2)! || Int(p2)! > pageNumber || Int(p1) == 0 {
  123. isInvalid = true
  124. break
  125. }else{
  126. var isEqual = false
  127. for i in Int(p1)! ... Int(p2)! {
  128. for pageNumber in pageNumbers {
  129. if pageNumber == i {
  130. isEqual = true
  131. isInvalid = true
  132. break
  133. }
  134. }
  135. }
  136. if (!isEqual) {
  137. for i in Int(p1)! ... Int(p2)! {
  138. pageNumbers.append(i)
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. if (text.isEmpty) {
  147. isInvalid = true
  148. }
  149. if (isInvalid) {
  150. self.selectPages = []
  151. }else{
  152. self.selectPages = pageNumbers
  153. }
  154. return isInvalid
  155. }
  156. func quickSort(_ list: inout [Int],startIndex: Int, endIndex: Int) {
  157. if(startIndex >= endIndex) {
  158. return
  159. }
  160. let temp = list[startIndex]
  161. var tempIndex = startIndex
  162. for i in startIndex+1 ... endIndex {
  163. let t = list[i]
  164. if (temp > t) {
  165. tempIndex = tempIndex + 1
  166. let tmp = list[tempIndex]
  167. list[tempIndex] = list[i]
  168. list[i] = tmp
  169. }
  170. }
  171. let tmp = list[tempIndex]
  172. list[tempIndex] = list[startIndex]
  173. list[startIndex] = tmp
  174. quickSort(&list, startIndex: startIndex, endIndex: tempIndex-1)
  175. quickSort(&list, startIndex: tempIndex+1, endIndex: endIndex)
  176. }
  177. }