KMFileAttribute.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. /*
  38. @property (nonatomic, assign) BOOL pageRangeError;
  39. */
  40. func reloadData() {
  41. if filePath.count != 0 {
  42. let attribe = try?FileManager.default.attributesOfItem(atPath: filePath)
  43. fileSize = attribe?[FileAttributeKey.size] as? CGFloat ?? 0
  44. let document = CPDFDocument.init(url: URL(fileURLWithPath: filePath))
  45. document?.unlock(withPassword: self.password)
  46. isLocked = document?.isLocked ?? false
  47. self.pdfDocument = document
  48. let page = document?.page(at: 0) ?? CPDFPage()
  49. let image = page.thumbnail(of: page.size)
  50. fileImage = image ?? NSImage()
  51. // fileImage = NSImage.previewForFile(path: URL(fileURLWithPath: filePath), ofSize: CGSizeMake(136, 214), asIcon: true) ?? NSImage()
  52. }
  53. }
  54. func fetchSelectPages() -> [Int] {
  55. if let doc = self.pdfDocument, doc.isLocked {
  56. doc.unlock(withPassword: self.password)
  57. }
  58. guard let pdfDocument = pdfDocument else { return [] }
  59. selectPages.removeAll()
  60. if pagesType == .all {
  61. for i in 0..<pdfDocument.pageCount {
  62. selectPages.append(Int(i) + 1)
  63. }
  64. pagesString = "1-\(pdfDocument.pageCount)"
  65. } else if pagesType == .odd {
  66. pagesString = ""
  67. for i in 0..<pdfDocument.pageCount where i % 2 == 0 {
  68. selectPages.append(Int(i) + 1)
  69. if pagesString == "" {
  70. pagesString = "\(i + 1)"
  71. } else {
  72. pagesString = "\(pagesString),\(i + 1)"
  73. }
  74. }
  75. } else if pagesType == .even {
  76. pagesString = ""
  77. for i in 0..<pdfDocument.pageCount where i % 2 != 0 {
  78. selectPages.append(Int(i) + 1)
  79. if pagesString == "" {
  80. pagesString = "\(i + 1)"
  81. } else {
  82. pagesString = "\(pagesString),\(i + 1)"
  83. }
  84. }
  85. } else {
  86. isInvalidString(pagesString)
  87. // let pages = KMPageRangeTools.findSelectPage(pageRangeString: pagesString, pageCount: Int(pdfDocument.pageCount ))
  88. // selectPages = pages
  89. if !bAllPage {
  90. self.QuickSort(&selectPages, startIndex: 0, endIndex: selectPages.count-1)
  91. }
  92. }
  93. return selectPages
  94. }
  95. func isInvalidString(_ text: String) -> Bool {
  96. var document: PDFDocument?
  97. if (self.myPDFDocument != nil) {
  98. document = self.myPDFDocument
  99. } else {
  100. document = PDFDocument(url: URL(fileURLWithPath: self.filePath ))
  101. }
  102. if let data = document?.isLocked, data {
  103. document?.unlock(withPassword: self.password )
  104. }
  105. var pageNumber = document?.pageCount ?? 1
  106. if let cnt = self.pageCnt {
  107. pageNumber = cnt
  108. }
  109. if (self.bAllPage) {
  110. self.selectPages = []
  111. for i in 1 ... pageNumber {
  112. self.selectPages.append(i)
  113. }
  114. return false
  115. }
  116. var pageNumbers: [Int] = []
  117. var isInvalid = false
  118. var c: unichar = 0
  119. for c in text {
  120. if (c != "0" && c != "1" && c != "2" && c != "3" && c != "4" && c != "5" && c != "6" && c != "7" && c != "8" && c != "9" && c != "," && c != "-") {
  121. isInvalid = true
  122. break
  123. }else{
  124. isInvalid = false
  125. }
  126. }
  127. if (!isInvalid) {
  128. let array = text.components(separatedBy: ",")
  129. for s in array {
  130. if s.isEmpty {
  131. isInvalid = true
  132. break
  133. }else{
  134. let pages = s.components(separatedBy: "-")
  135. if (pages.count>2) {
  136. isInvalid = true
  137. break
  138. }else if(pages.count==1){
  139. let p = pages.first!
  140. if p.isEmpty || Int(p)! > pageNumber || Int(p) == 0 {
  141. isInvalid = true
  142. break
  143. }else{
  144. var isEqual = false
  145. for pageNumber in pageNumbers {
  146. if pageNumber == Int(p) {
  147. isEqual = true
  148. isInvalid = true
  149. break
  150. }
  151. }
  152. if (!isEqual) {
  153. pageNumbers.append(Int(p)!)
  154. }
  155. }
  156. }else if(pages.count==2){
  157. let p1 = pages[0]
  158. let p2 = pages[1]
  159. if p1.isEmpty || p2.isEmpty || Int(p1)! >= Int(p2)! || Int(p2)! > pageNumber || Int(p1) == 0 {
  160. isInvalid = true
  161. break
  162. }else{
  163. var isEqual = false
  164. for i in Int(p1)! ... Int(p2)! {
  165. for pageNumber in pageNumbers {
  166. if pageNumber == i {
  167. isEqual = true
  168. isInvalid = true
  169. break
  170. }
  171. }
  172. }
  173. if (!isEqual) {
  174. for i in Int(p1)! ... Int(p2)! {
  175. pageNumbers.append(i)
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }
  183. if (text.isEmpty) {
  184. isInvalid = true
  185. }
  186. if (isInvalid) {
  187. self.selectPages = []
  188. }else{
  189. self.selectPages = pageNumbers
  190. }
  191. return isInvalid
  192. }
  193. func QuickSort(_ list: inout [Int],startIndex: Int, endIndex: Int) {
  194. if(startIndex >= endIndex) {
  195. return
  196. }
  197. let temp = list[startIndex]
  198. var tempIndex = startIndex
  199. for i in startIndex+1 ... endIndex {
  200. let t = list[i]
  201. if (temp > t) {
  202. tempIndex = tempIndex + 1
  203. let tmp = list[tempIndex]
  204. list[tempIndex] = list[i]
  205. list[i] = tmp
  206. }
  207. }
  208. let tmp = list[tempIndex]
  209. list[tempIndex] = list[startIndex]
  210. list[startIndex] = tmp
  211. self.QuickSort(&list, startIndex: startIndex, endIndex: tempIndex-1)
  212. self.QuickSort(&list, startIndex: tempIndex+1, endIndex: endIndex)
  213. }
  214. /*
  215. /* give our representation to the image browser */
  216. - (id)imageRepresentation
  217. {
  218. return self.filePath;
  219. }
  220. /* use the absolute filepath as identifier */
  221. - (NSString *)imageUID
  222. {
  223. return self.filePath;
  224. }
  225. - (NSString*)imageTitle
  226. {
  227. return [[self.filePath lastPathComponent] stringByDeletingPathExtension];
  228. }
  229. */
  230. }