KMFileAttribute.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // KMFileAttribute.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/10/12.
  6. //
  7. import Cocoa
  8. @objc enum KMPDFSelectPageStringType: Int {
  9. case AllPages = 0
  10. case OnlyOdd
  11. case OnlyEven
  12. case PagesString
  13. }
  14. @objcMembers class KMFileAttribute: NSObject {
  15. var filePath: String = "" {
  16. didSet {
  17. self.reloadData()
  18. }
  19. }
  20. var oriFilePath: String?
  21. var myPDFDocument: PDFDocument?
  22. var pdfDocument: CPDFDocument?
  23. var bAllPage = false
  24. private var selectPages: [Int] = []
  25. var pagesString: String = ""
  26. var isLocked = false
  27. var password: String = "" {
  28. didSet {
  29. self.reloadData()
  30. }
  31. }
  32. var pagesType: KMPageRange = .all
  33. //新增参数
  34. var fileSize: CGFloat = 0
  35. var fileImage: NSImage = NSImage()
  36. var pageCnt: Int?
  37. func reloadData() {
  38. if filePath.count != 0 {
  39. let attribe = try?FileManager.default.attributesOfItem(atPath: filePath)
  40. fileSize = attribe?[FileAttributeKey.size] as? CGFloat ?? 0
  41. let document = CPDFDocument.init(url: URL(fileURLWithPath: filePath))
  42. document?.unlock(withPassword: self.password)
  43. isLocked = document?.isLocked ?? false
  44. self.pdfDocument = document
  45. let page = document?.page(at: 0) ?? CPDFPage()
  46. let image = page.thumbnail(of: page.size)
  47. fileImage = image ?? NSImage()
  48. }
  49. }
  50. func fetchSelectPages() -> [Int] {
  51. if let doc = self.pdfDocument, doc.isLocked {
  52. doc.unlock(withPassword: self.password)
  53. }
  54. guard let pdfDocument = pdfDocument else { return [] }
  55. selectPages.removeAll()
  56. if pagesType == .all {
  57. for i in 0..<pdfDocument.pageCount {
  58. selectPages.append(Int(i) + 1)
  59. }
  60. pagesString = "1-\(pdfDocument.pageCount)"
  61. } else if pagesType == .odd {
  62. pagesString = ""
  63. for i in 0..<pdfDocument.pageCount where i % 2 == 0 {
  64. selectPages.append(Int(i) + 1)
  65. if pagesString == "" {
  66. pagesString = "\(i + 1)"
  67. } else {
  68. pagesString = "\(pagesString),\(i + 1)"
  69. }
  70. }
  71. } else if pagesType == .even {
  72. pagesString = ""
  73. for i in 0..<pdfDocument.pageCount where i % 2 != 0 {
  74. selectPages.append(Int(i) + 1)
  75. if pagesString == "" {
  76. pagesString = "\(i + 1)"
  77. } else {
  78. pagesString = "\(pagesString),\(i + 1)"
  79. }
  80. }
  81. } else {
  82. isInvalidString(pagesString)
  83. if !bAllPage {
  84. self.QuickSort(&selectPages, startIndex: 0, endIndex: selectPages.count-1)
  85. }
  86. }
  87. return selectPages
  88. }
  89. func isInvalidString(_ text: String) -> Bool {
  90. var document: PDFDocument?
  91. if (self.myPDFDocument != nil) {
  92. document = self.myPDFDocument
  93. } else {
  94. document = PDFDocument(url: URL(fileURLWithPath: self.filePath ))
  95. }
  96. if let data = document?.isLocked, data {
  97. document?.unlock(withPassword: self.password )
  98. }
  99. var pageNumber = document?.pageCount ?? 1
  100. if let cnt = self.pageCnt {
  101. pageNumber = cnt
  102. }
  103. if (self.bAllPage) {
  104. self.selectPages = []
  105. for i in 1 ... pageNumber {
  106. self.selectPages.append(i)
  107. }
  108. return false
  109. }
  110. var pageNumbers: [Int] = []
  111. var isInvalid = false
  112. var c: unichar = 0
  113. for c in text {
  114. if (c != "0" && c != "1" && c != "2" && c != "3" && c != "4" && c != "5" && c != "6" && c != "7" && c != "8" && c != "9" && c != "," && c != "-") {
  115. isInvalid = true
  116. break
  117. }else{
  118. isInvalid = false
  119. }
  120. }
  121. if (!isInvalid) {
  122. let array = text.components(separatedBy: ",")
  123. for s in array {
  124. if s.isEmpty {
  125. isInvalid = true
  126. break
  127. }else{
  128. let pages = s.components(separatedBy: "-")
  129. if (pages.count>2) {
  130. isInvalid = true
  131. break
  132. }else if(pages.count==1){
  133. let p = pages.first!
  134. if p.isEmpty || Int(p)! > pageNumber || Int(p) == 0 {
  135. isInvalid = true
  136. break
  137. }else{
  138. var isEqual = false
  139. for pageNumber in pageNumbers {
  140. if pageNumber == Int(p) {
  141. isEqual = true
  142. isInvalid = true
  143. break
  144. }
  145. }
  146. if (!isEqual) {
  147. pageNumbers.append(Int(p)!)
  148. }
  149. }
  150. }else if(pages.count==2){
  151. let p1 = pages[0]
  152. let p2 = pages[1]
  153. if p1.isEmpty || p2.isEmpty || Int(p1)! >= Int(p2)! || Int(p2)! > pageNumber || Int(p1) == 0 {
  154. isInvalid = true
  155. break
  156. }else{
  157. var isEqual = false
  158. for i in Int(p1)! ... Int(p2)! {
  159. for pageNumber in pageNumbers {
  160. if pageNumber == i {
  161. isEqual = true
  162. isInvalid = true
  163. break
  164. }
  165. }
  166. }
  167. if (!isEqual) {
  168. for i in Int(p1)! ... Int(p2)! {
  169. pageNumbers.append(i)
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176. }
  177. if (text.isEmpty) {
  178. isInvalid = true
  179. }
  180. if (isInvalid) {
  181. self.selectPages = []
  182. }else{
  183. self.selectPages = pageNumbers
  184. }
  185. return isInvalid
  186. }
  187. func QuickSort(_ list: inout [Int],startIndex: Int, endIndex: Int) {
  188. if(startIndex >= endIndex) {
  189. return
  190. }
  191. let temp = list[startIndex]
  192. var tempIndex = startIndex
  193. for i in startIndex+1 ... endIndex {
  194. let t = list[i]
  195. if (temp > t) {
  196. tempIndex = tempIndex + 1
  197. let tmp = list[tempIndex]
  198. list[tempIndex] = list[i]
  199. list[i] = tmp
  200. }
  201. }
  202. let tmp = list[tempIndex]
  203. list[tempIndex] = list[startIndex]
  204. list[startIndex] = tmp
  205. self.QuickSort(&list, startIndex: startIndex, endIndex: tempIndex-1)
  206. self.QuickSort(&list, startIndex: tempIndex+1, endIndex: endIndex)
  207. }
  208. }