// // 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.. 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]; } */ }