123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- //
- // KMFileAttribute.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2023/10/12.
- //
- import Cocoa
- @objc enum KMPDFSelectPageStringType: Int {
- case AllPages = 0
- case OnlyOdd
- case OnlyEven
- case PagesString
- }
- @objcMembers class KMFileAttribute: NSObject {
- var filePath: String = "" {
- didSet {
- self.reloadData()
- }
- }
- var oriFilePath: String?
- var myPDFDocument: PDFDocument?
- var pdfDocument: CPDFDocument?
- var bAllPage = false
- private var selectPages: [Int] = []
- var pagesString: String = ""
- var isLocked = false
- var password: String = "" {
- didSet {
- self.reloadData()
- }
- }
- var pagesType: KMPageRange = .all
-
- //新增参数
- var fileSize: CGFloat = 0
- var fileImage: NSImage = NSImage()
- var pageCnt: Int?
-
- /*
- @property (nonatomic, assign) BOOL pageRangeError;
- */
-
- func reloadData() {
- if filePath.count != 0 {
- let attribe = try?FileManager.default.attributesOfItem(atPath: filePath)
- fileSize = attribe?[FileAttributeKey.size] as? CGFloat ?? 0
-
- let document = CPDFDocument.init(url: URL(fileURLWithPath: filePath))
- document?.unlock(withPassword: self.password)
- isLocked = document?.isLocked ?? false
- self.pdfDocument = document
-
- let page = document?.page(at: 0) ?? CPDFPage()
- let image = page.thumbnail(of: page.size)
- fileImage = image ?? NSImage()
- // fileImage = NSImage.previewForFile(path: URL(fileURLWithPath: filePath), ofSize: CGSizeMake(136, 214), asIcon: true) ?? NSImage()
- }
- }
-
- func fetchSelectPages() -> [Int] {
- if let doc = self.pdfDocument, doc.isLocked {
- doc.unlock(withPassword: self.password)
- }
- guard let pdfDocument = pdfDocument else { return [] }
- selectPages.removeAll()
- if pagesType == .all {
- for i in 0..<pdfDocument.pageCount {
- selectPages.append(Int(i) + 1)
- }
- pagesString = "1-\(pdfDocument.pageCount)"
- } else if pagesType == .odd {
- pagesString = ""
- for i in 0..<pdfDocument.pageCount where i % 2 == 0 {
- selectPages.append(Int(i) + 1)
- if pagesString == "" {
- pagesString = "\(i + 1)"
- } else {
- pagesString = "\(pagesString),\(i + 1)"
- }
- }
- } else if pagesType == .even {
- pagesString = ""
- for i in 0..<pdfDocument.pageCount where i % 2 != 0 {
- selectPages.append(Int(i) + 1)
- if pagesString == "" {
- pagesString = "\(i + 1)"
- } else {
- pagesString = "\(pagesString),\(i + 1)"
- }
- }
- } else {
- isInvalidString(pagesString)
- // let pages = KMPageRangeTools.findSelectPage(pageRangeString: pagesString, pageCount: Int(pdfDocument.pageCount ))
- // selectPages = pages
- if !bAllPage {
- self.QuickSort(&selectPages, startIndex: 0, endIndex: selectPages.count-1)
- }
- }
-
- return selectPages
- }
-
- func isInvalidString(_ text: String) -> Bool {
- var document: PDFDocument?
- if (self.myPDFDocument != nil) {
- document = self.myPDFDocument
- } else {
- document = PDFDocument(url: URL(fileURLWithPath: self.filePath ))
- }
-
- if let data = document?.isLocked, data {
- document?.unlock(withPassword: self.password )
- }
- var pageNumber = document?.pageCount ?? 1
- if let cnt = self.pageCnt {
- pageNumber = cnt
- }
- if (self.bAllPage) {
- self.selectPages = []
- for i in 1 ... pageNumber {
- self.selectPages.append(i)
- }
- return false
- }
-
- var pageNumbers: [Int] = []
- var isInvalid = false
- var c: unichar = 0
- for c in text {
- if (c != "0" && c != "1" && c != "2" && c != "3" && c != "4" && c != "5" && c != "6" && c != "7" && c != "8" && c != "9" && c != "," && c != "-") {
- isInvalid = true
- break
- }else{
- isInvalid = false
- }
- }
- if (!isInvalid) {
- let array = text.components(separatedBy: ",")
- for s in array {
- if s.isEmpty {
- isInvalid = true
- break
- }else{
- let pages = s.components(separatedBy: "-")
- if (pages.count>2) {
- isInvalid = true
- break
- }else if(pages.count==1){
- let p = pages.first!
- if p.isEmpty || Int(p)! > pageNumber || Int(p) == 0 {
- isInvalid = true
- break
- }else{
- var isEqual = false
- for pageNumber in pageNumbers {
- if pageNumber == Int(p) {
- isEqual = true
- isInvalid = true
- break
- }
- }
- if (!isEqual) {
- pageNumbers.append(Int(p)!)
- }
- }
- }else if(pages.count==2){
- let p1 = pages[0]
- let p2 = pages[1]
- if p1.isEmpty || p2.isEmpty || Int(p1)! >= Int(p2)! || Int(p2)! > pageNumber || Int(p1) == 0 {
- isInvalid = true
- break
- }else{
- var isEqual = false
- for i in Int(p1)! ... Int(p2)! {
- for pageNumber in pageNumbers {
- if pageNumber == i {
- isEqual = true
- isInvalid = true
- break
- }
- }
- }
- if (!isEqual) {
- for i in Int(p1)! ... Int(p2)! {
- pageNumbers.append(i)
- }
- }
- }
- }
- }
- }
- }
- if (text.isEmpty) {
- isInvalid = true
- }
- if (isInvalid) {
- self.selectPages = []
- }else{
- self.selectPages = pageNumbers
- }
- return isInvalid
- }
-
- func QuickSort(_ list: inout [Int],startIndex: Int, endIndex: Int) {
- if(startIndex >= endIndex) {
- return
- }
-
- let temp = list[startIndex]
- var tempIndex = startIndex
-
- for i in startIndex+1 ... endIndex {
- let t = list[i]
- if (temp > t) {
- tempIndex = tempIndex + 1
-
- let tmp = list[tempIndex]
- list[tempIndex] = list[i]
- list[i] = tmp
- }
- }
-
- let tmp = list[tempIndex]
- list[tempIndex] = list[startIndex]
- list[startIndex] = tmp
-
- self.QuickSort(&list, startIndex: startIndex, endIndex: tempIndex-1)
- self.QuickSort(&list, startIndex: tempIndex+1, endIndex: endIndex)
- }
-
- /*
- /* give our representation to the image browser */
- - (id)imageRepresentation
- {
- return self.filePath;
- }
- /* use the absolute filepath as identifier */
- - (NSString *)imageUID
- {
- return self.filePath;
- }
- - (NSString*)imageTitle
- {
- return [[self.filePath lastPathComponent] stringByDeletingPathExtension];
- }
- */
- }
|