|
@@ -1101,12 +1101,157 @@ extension KMBrowserWindowController: KMSystemFileMenuProtocol {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ func isEncapsulatedPostScriptData(_ data: Data) -> Bool {
|
|
|
|
+ let epsHeaderData = Data(bytes: [69, 80, 83, 70, 45], count: 5)
|
|
|
|
+ let rg: Range = (14..<20)
|
|
|
|
+ return (data.count >= 20 && (data.range(of: epsHeaderData, options: .anchored, in: rg) != nil))
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ func convertTIFFDataToPDF(_ tiffData: Data) -> Data? {
|
|
|
|
+ guard let imsrc = CGImageSourceCreateWithData(tiffData as CFData, [kCGImageSourceTypeIdentifierHint: kUTTypeTIFF] as CFDictionary), CGImageSourceGetCount(imsrc) > 0, let cgImage = CGImageSourceCreateImageAtIndex(imsrc, 0, nil) else { return nil }
|
|
|
|
+ let pdfData = NSMutableData(capacity: tiffData.count)
|
|
|
|
+ let consumer = CGDataConsumer(data: pdfData! as CFMutableData)!
|
|
|
|
+
|
|
|
|
+ var rect = CGRect(x: 0, y: 0, width: CGFloat(cgImage.width), height: CGFloat(cgImage.height))
|
|
|
|
+ let ctxt = CGContext(consumer: consumer, mediaBox: &rect, nil)
|
|
|
|
+ ctxt!.beginPDFPage(nil)
|
|
|
|
+ ctxt!.draw(cgImage, in: rect)
|
|
|
|
+ ctxt!.endPDFPage()
|
|
|
|
+ ctxt!.flush()
|
|
|
|
+
|
|
|
|
+ return pdfData as? Data
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ func openDocumentWithImageFromPasteboard(_ pboard: NSPasteboard, error outError: AutoreleasingUnsafeMutablePointer<NSError?>?) -> Any? {
|
|
|
|
+ var document: CPDFDocument? = nil
|
|
|
|
+ var data: Data? = nil
|
|
|
|
+
|
|
|
|
+ if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.pdf.rawValue]) {
|
|
|
|
+// pboard.types
|
|
|
|
+ data = pboard.data(forType: NSPasteboard.PasteboardType.pdf)
|
|
|
|
+ } else if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.postScript.rawValue]) {
|
|
|
|
+// pboard.types
|
|
|
|
+ data = pboard.data(forType: NSPasteboard.PasteboardType.postScript)
|
|
|
|
+ } else if pboard.canReadItem(withDataConformingToTypes: [NSPasteboard.PasteboardType.tiff.rawValue]) {
|
|
|
|
+// pboard.types
|
|
|
|
+ data = convertTIFFDataToPDF(pboard.data(forType: NSPasteboard.PasteboardType.tiff) ?? Data())
|
|
|
|
+ } else {
|
|
|
|
+ let images = pboard.readObjects(forClasses: [NSImage.self], options: [:])
|
|
|
|
+ let strings = pboard.readObjects(forClasses: [NSAttributedString.self], options: [:])
|
|
|
|
+ if images?.count ?? 0 > 0 {
|
|
|
|
+ data = convertTIFFDataToPDF((images![0] as AnyObject).tiffRepresentation!)
|
|
|
|
+ } else if strings?.count ?? 0 > 0 {
|
|
|
|
+ data = KMOCTool.convertStringsToPDF(withString: strings ?? [""]) // convertStringsToPDF(strings!)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if let data = data {
|
|
|
|
+ let documentC = NSDocumentController.shared
|
|
|
|
+
|
|
|
|
+ document = CPDFDocument(data: data)
|
|
|
|
+
|
|
|
|
+ let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
|
|
|
|
+ let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
|
|
|
|
+ let filePath = savePath.deletingLastPathComponent
|
|
|
|
+ if FileManager.default.fileExists(atPath: filePath) == false {
|
|
|
|
+ try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ document?.write(to: URL(fileURLWithPath: savePath))
|
|
|
|
+ NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
|
|
|
|
+ if error != nil {
|
|
|
|
+ NSApp.presentError(error!)
|
|
|
|
+ } else {
|
|
|
|
+ if document is KMMainDocument {
|
|
|
|
+ let newDocument = document
|
|
|
|
+ (newDocument as! KMMainDocument).isNewCreated = true
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ } else if let outError = outError {
|
|
|
|
+ outError.pointee = NSError(domain: "SKDocumentErrorDomain", code: 3, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("Unable to load data from clipboard", comment: "Error description")])
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return document
|
|
|
|
+ }
|
|
|
|
+ func openDocument(withURLFromPasteboard pboard: NSPasteboard, showNotes: Bool, error outError: inout NSError?) -> Any? {
|
|
|
|
+ let theURLs = NSURL.readURLs(from: pboard)
|
|
|
|
+ let url = theURLs?.count ?? 0 > 0 ? theURLs?[0] : nil
|
|
|
|
+ let theURL: NSURL? = url as? NSURL
|
|
|
|
+ var documentC = NSDocumentController.shared
|
|
|
|
+ var document: NSDocument? = nil
|
|
|
|
+ if (theURL as AnyObject).isFileURL == true {
|
|
|
|
+ var _: NSError? = nil
|
|
|
|
+ let type = try? documentC.typeForContents(of: theURL as! URL)//ForContents(ofURL: theURL, error: &error)
|
|
|
|
+
|
|
|
|
+ if showNotes == false || NSDocument.readableTypes.contains(type ?? "") {
|
|
|
|
+ documentC.openDocument(withContentsOf: theURL as! URL, display: true, completionHandler: { resultDocument, success, err in
|
|
|
|
+ document = resultDocument
|
|
|
|
+ })//openDocument(withContentsOfURL: theURL, display: true, error: &outError)
|
|
|
|
+ } else if NSDocument.readableTypes.contains(type ?? "") {
|
|
|
|
+ for doc in documentC.documents {
|
|
|
|
+ let sel = NSSelectorFromString("sourceFileURL")
|
|
|
|
+ if doc.responds(to: sel) && doc.fileURL == theURL as? URL {
|
|
|
|
+ document = doc
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if let document: NSDocument = document {
|
|
|
|
+ document.showWindows()
|
|
|
|
+ } else {
|
|
|
|
+ var data: Data? = nil
|
|
|
|
+
|
|
|
|
+// if NSWorkspace.shared.type(type ?? "", conformsToType: KMPDFBundleDocumentType) {
|
|
|
|
+// let skimFileURL = FileManager.default.exte
|
|
|
|
+// data = skimFileURL != nil ? try? Data(contentsOf: skimFileURL!) : nil
|
|
|
|
+// } else {
|
|
|
|
+// data = SKNExtendedAttributeManager.shared().extendedAttributeNamed(SKIM_NOTES_KEY, atPath: theURL.path, traverseLink: true, error: &error)
|
|
|
|
+// }
|
|
|
|
+// let documentNew: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
|
|
|
|
+
|
|
|
|
+ if let document = try? documentC.makeUntitledDocument(ofType: KMNotesDocumentType) {
|
|
|
|
+ document.fileURL = URL(fileURLWithPath: theURL?.path ?? "")
|
|
|
|
+
|
|
|
|
+// if data == nil || document.read(fromData: data!, ofType: SKNotesDocumentType, error: &error) {
|
|
|
|
+ documentC.addDocument(document)
|
|
|
|
+ document.makeWindowControllers()
|
|
|
|
+ document.showWindows()
|
|
|
|
+// } else {
|
|
|
|
+// document = nil
|
|
|
|
+// if let error = error {
|
|
|
|
+// outError = error
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+// else if showNotes == false && theURL != nil {
|
|
|
|
+// document = SKDownloadController.shared().addDownload(for: theURL!)
|
|
|
|
+// } else if outError != nil {
|
|
|
|
+// outError = NSError.readPasteboardError(withLocalizedDescription: NSLocalizedString("Unable to load data from clipboard", comment: "Error description"))
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+ return document
|
|
|
|
+ }
|
|
|
|
+
|
|
@IBAction func newDocumentFromClipboard(_ sender: Any?) {
|
|
@IBAction func newDocumentFromClipboard(_ sender: Any?) {
|
|
KMPrint("newDocumentFromClipboard")
|
|
KMPrint("newDocumentFromClipboard")
|
|
|
|
+ var error: NSError?
|
|
|
|
+ let pboard = NSPasteboard.general
|
|
|
|
+ var document = openDocumentWithImageFromPasteboard(pboard, error: &error)
|
|
|
|
+
|
|
|
|
+ if document == nil{
|
|
|
|
+ document = openDocument(withURLFromPasteboard: pboard, showNotes: false, error: &error)
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
@IBAction func newDocumentFromImage(_ sender: Any?) {
|
|
@IBAction func newDocumentFromImage(_ sender: Any?) {
|
|
KMPrint("importFromWebPage")
|
|
KMPrint("importFromWebPage")
|
|
|
|
+ createPDFFromImage()
|
|
}
|
|
}
|
|
|
|
|
|
@IBAction func importFromWebPage(_ sender: Any?) {
|
|
@IBAction func importFromWebPage(_ sender: Any?) {
|
|
@@ -1136,6 +1281,118 @@ extension KMBrowserWindowController: KMSystemFileMenuProtocol {
|
|
// MARK: - KMSystemViewMenuProtocol
|
|
// MARK: - KMSystemViewMenuProtocol
|
|
|
|
|
|
extension KMBrowserWindowController: KMSystemViewMenuProtocol {
|
|
extension KMBrowserWindowController: KMSystemViewMenuProtocol {
|
|
|
|
+ func createPDFFromImage() {
|
|
|
|
+ let openPanel = NSOpenPanel()
|
|
|
|
+ openPanel.allowedFileTypes = KMImageAccessoryController.supportedImageTypes()
|
|
|
|
+ openPanel.allowsMultipleSelection = true
|
|
|
|
+ openPanel.message = NSLocalizedString("Select images to create a new document. To select multiple files press cmd ⌘ button on keyboard and click on the target files one by one.", comment: "")
|
|
|
|
+ openPanel.beginSheetModal(for: NSApp.mainWindow!) { response in
|
|
|
|
+ if response == .OK {
|
|
|
|
+ self.creatPdfFromImg(urls: openPanel.urls)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ func creatPdfFromImg(urls: Array<URL>) {
|
|
|
|
+// let savePath = self.newDocumentTempSavePath("convertToPDF.pdf")
|
|
|
|
+ let pdf = CPDFDocument()
|
|
|
|
+ var isSucceed = true
|
|
|
|
+
|
|
|
|
+ let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
|
|
|
|
+ let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
|
|
|
|
+ let filePath = savePath.deletingLastPathComponent
|
|
|
|
+ if FileManager.default.fileExists(atPath: filePath) == false {
|
|
|
|
+ try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for fileURL in urls {
|
|
|
|
+ if let imag = NSImage(contentsOfFile: fileURL.path ) {
|
|
|
|
+ _ = pdf?.km_insertPage(imag.size, withImage: fileURL.path , at: pdf?.pageCount ?? 0)
|
|
|
|
+ pdf?.write(to: URL(fileURLWithPath: savePath))
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
|
|
|
|
+ if error != nil {
|
|
|
|
+ NSApp.presentError(error!)
|
|
|
|
+ } else {
|
|
|
|
+ if document is KMMainDocument {
|
|
|
|
+ let newDocument = document
|
|
|
|
+ (newDocument as! KMMainDocument).isNewCreated = true
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+// self.homeContentView?.reloadData()
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ func createPdfWithParam() {
|
|
|
|
+ let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
|
|
|
|
+ let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
|
|
|
|
+ let filePath = savePath.deletingLastPathComponent
|
|
|
|
+ if FileManager.default.fileExists(atPath: filePath) == false {
|
|
|
|
+ try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ let pdfDocument = CPDFDocument()
|
|
|
|
+ pdfDocument?.insertPage(CGSize(width: 595, height: 842), at: 0)
|
|
|
|
+ pdfDocument?.write(to: URL(fileURLWithPath: savePath))
|
|
|
|
+ NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
|
|
|
|
+ if error != nil {
|
|
|
|
+ NSApp.presentError(error!)
|
|
|
|
+ } else {
|
|
|
|
+ if document is KMMainDocument {
|
|
|
|
+ let newDocument = document
|
|
|
|
+ (newDocument as! KMMainDocument).isNewCreated = true
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+// self.homeContentView?.reloadData()
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ func savePdf(_ filePath: String) -> Void {
|
|
|
|
+ let docVc = KMDocumentController.shared
|
|
|
|
+
|
|
|
|
+ docVc.openDocument(withContentsOf: URL(fileURLWithPath: filePath), display: true) { doc, suc, err in
|
|
|
|
+ try? FileManager.default.removeItem(atPath: filePath)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ func newDocumentTempSavePath(_ fileName: String) -> String {
|
|
|
|
+ let searchPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last
|
|
|
|
+ let append1 = searchPath?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!)
|
|
|
|
+ let append2 = append1!.stringByAppendingPathComponent(String(format: "%@", fileName))
|
|
|
|
+ return append2
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ func fetchUniquePath(_ originalPath: String) -> String {
|
|
|
|
+ var path = originalPath
|
|
|
|
+ let dManager = FileManager.default
|
|
|
|
+ if !dManager.fileExists(atPath: path) {
|
|
|
|
+ if path.extension.count < 1 {
|
|
|
|
+ path = path.stringByAppendingPathExtension("pdf")
|
|
|
|
+ }
|
|
|
|
+ return path
|
|
|
|
+ } else {
|
|
|
|
+ let originalFullFileName = path.lastPathComponent
|
|
|
|
+ let originalFileName = path.lastPathComponent.deletingPathExtension
|
|
|
|
+ let originalExtension = path.extension
|
|
|
|
+
|
|
|
|
+ let startIndex: Int = 0
|
|
|
|
+ let endIndex: Int = startIndex + originalPath.count - originalFullFileName.count - 1
|
|
|
|
+ let fileLocatePath = originalPath.substring(to: endIndex)
|
|
|
|
+ var i = 1
|
|
|
|
+ while (1 != 0) {
|
|
|
|
+ var newName = String(format: "%@%ld", originalFileName, i)
|
|
|
|
+ newName = newName.stringByAppendingPathExtension(originalExtension)
|
|
|
|
+ let newPath = fileLocatePath.stringByAppendingPathComponent(newName)
|
|
|
|
+ if !dManager.fileExists(atPath: newPath) {
|
|
|
|
+ return newPath
|
|
|
|
+ } else {
|
|
|
|
+ i+=1
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
func menuItemAction_adjustWidth(_ sender: Any) {
|
|
func menuItemAction_adjustWidth(_ sender: Any) {
|
|
if (self.canResponseDocumentAction() == false) {
|
|
if (self.canResponseDocumentAction() == false) {
|
|
return
|
|
return
|