Browse Source

remove PDF library

marinofaggiana 6 years ago
parent
commit
c53b75cee9

+ 0 - 1
Cartfile

@@ -1,7 +1,6 @@
 github "tilltue/TLPhotoPicker" == 1.8.5
 github "tilltue/TLPhotoPicker" == 1.8.5
 github "kishikawakatsumi/UICKeyChainStore" == 2.1.2
 github "kishikawakatsumi/UICKeyChainStore" == 2.1.2
 github "danielsaidi/Sheeeeeeeeet" == 1.2.2
 github "danielsaidi/Sheeeeeeeeet" == 1.2.2
-github "sgr-ksmt/PDFGenerator" == 2.1
 github "MortimerGoro/MGSwipeTableCell" == 1.6.8
 github "MortimerGoro/MGSwipeTableCell" == 1.6.8
 github "dzenbot/DZNEmptyDataSet" == 1.8.1
 github "dzenbot/DZNEmptyDataSet" == 1.8.1
 github "ealeksandrov/EAIntroView" == 2.12.0
 github "ealeksandrov/EAIntroView" == 2.12.0

+ 0 - 1
Nextcloud.xcodeproj/project.pbxproj

@@ -3405,7 +3405,6 @@
 				"$(SRCROOT)/Carthage/Build/iOS/Sheeeeeeeeet.framework",
 				"$(SRCROOT)/Carthage/Build/iOS/Sheeeeeeeeet.framework",
 				"$(SRCROOT)/Carthage/Build/iOS/TLPhotoPicker.framework",
 				"$(SRCROOT)/Carthage/Build/iOS/TLPhotoPicker.framework",
 				"$(SRCROOT)/Carthage/Build/iOS/UICKeyChainStore.framework",
 				"$(SRCROOT)/Carthage/Build/iOS/UICKeyChainStore.framework",
-				"$(SRCROOT)/Carthage/Build/iOS/PDFGenerator.framework",
 				"$(SRCROOT)/Carthage/Build/iOS/MGSwipeTableCell.framework",
 				"$(SRCROOT)/Carthage/Build/iOS/MGSwipeTableCell.framework",
 				"$(SRCROOT)/Carthage/Build/iOS/DZNEmptyDataSet.framework",
 				"$(SRCROOT)/Carthage/Build/iOS/DZNEmptyDataSet.framework",
 				"$(SRCROOT)/Carthage/Build/iOS/EAIntroView.framework",
 				"$(SRCROOT)/Carthage/Build/iOS/EAIntroView.framework",

+ 64 - 64
iOSClient/Main/Create cloud/NCCreateFormUploadScanDocument.swift

@@ -23,7 +23,6 @@
 
 
 
 
 import Foundation
 import Foundation
-import PDFGenerator
 import WeScan
 import WeScan
 import GoogleMobileVision
 import GoogleMobileVision
 
 
@@ -40,7 +39,7 @@ class NCCreateFormUploadScanDocument: XLFormViewController, NCSelectDelegate {
     var titleServerUrl = ""
     var titleServerUrl = ""
     var arrayImages = [UIImage]()
     var arrayImages = [UIImage]()
     var fileName = CCUtility.createFileNameDate("scan", extension: "pdf")
     var fileName = CCUtility.createFileNameDate("scan", extension: "pdf")
-    var password: PDFPassword = ""
+    var password: String = ""
     var fileType = "PDF"
     var fileType = "PDF"
     var ocr = true
     var ocr = true
     
     
@@ -186,9 +185,9 @@ class NCCreateFormUploadScanDocument: XLFormViewController, NCSelectDelegate {
             self.form.delegate = nil
             self.form.delegate = nil
             
             
             if newValue as! Int == 1 {
             if newValue as! Int == 1 {
-                rowFileTape.selectorOptions = ["TXT"] //["PDF","TXT"]
-                rowFileTape.value = "TXT"
-                fileType = "TXT"
+                rowFileTape.selectorOptions = ["PDF","TXT"]
+                rowFileTape.value = "PDF"
+                fileType = "PDF"
                 rowPassword.disabled = true
                 rowPassword.disabled = true
                 rowCompressionQuality.disabled = true
                 rowCompressionQuality.disabled = true
             } else {
             } else {
@@ -256,9 +255,9 @@ class NCCreateFormUploadScanDocument: XLFormViewController, NCSelectDelegate {
         if formRow.tag == "password" {
         if formRow.tag == "password" {
             let stringPassword = newValue as? String
             let stringPassword = newValue as? String
             if stringPassword != nil {
             if stringPassword != nil {
-                password = PDFPassword(stringPassword!)
+                password = stringPassword!
             } else {
             } else {
-                password = PDFPassword("")
+                password = ""
             }
             }
         }
         }
         
         
@@ -275,7 +274,7 @@ class NCCreateFormUploadScanDocument: XLFormViewController, NCSelectDelegate {
             // rowPassword
             // rowPassword
             if fileType == "JPG" {
             if fileType == "JPG" {
                 rowPassword.value = ""
                 rowPassword.value = ""
-                password = PDFPassword("")
+                password = ""
                 rowPassword.disabled = true
                 rowPassword.disabled = true
             } else {
             } else {
                 rowPassword.disabled = false
                 rowPassword.disabled = false
@@ -449,84 +448,54 @@ class NCCreateFormUploadScanDocument: XLFormViewController, NCSelectDelegate {
         
         
         if fileType == "PDF" {
         if fileType == "PDF" {
             
             
-            var pdfPages = [PDFPage]()
+            let pdfData = NSMutableData()
+            UIGraphicsBeginPDFContextToData(pdfData, CGRect.zero, nil)
+            let context = UIGraphicsGetCurrentContext()
             
             
-            //Generate PDF
-            for var image in self.arrayImages {
+            for image in self.arrayImages {
                 
                 
-                image = changeImageFromQuality(image, dpiQuality: dpiQuality)
-                
-                guard let data = image.jpegData(compressionQuality: 0.5) else {
+                let imageCompress = changeImageFromQuality(image, dpiQuality: dpiQuality)
+                guard let data = imageCompress.jpegData(compressionQuality: 0.5) else {
                     self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
                     self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
                     return
                     return
                 }
                 }
                 
                 
+                UIGraphicsBeginPDFPageWithInfo(CGRect(x: 0, y: 0, width: imageCompress.size.width, height: imageCompress.size.height), nil)
+                UIImageView.init(image: UIImage(data: data)!).layer.render(in: context!)
+                
                 // Text Recognition
                 // Text Recognition
                 if NCBrandOptions.sharedInstance.useMLVision {
                 if NCBrandOptions.sharedInstance.useMLVision {
                     let rowTextRecognition: XLFormRowDescriptor = self.form.formRow(withTag: "textRecognition")!
                     let rowTextRecognition: XLFormRowDescriptor = self.form.formRow(withTag: "textRecognition")!
                     
                     
                     if rowTextRecognition.value as! Int == 1 {
                     if rowTextRecognition.value as! Int == 1 {
-                        
-                        let page = PDFPage.image(UIImage(data: data)!)
-                        
-                        guard let features = self.textDetector?.features(in: image, options: nil) as? [GMVTextBlockFeature] else {
-                            continue
-                        }
-                        
-                        do {
-                            let data = try PDFGenerator.generated(by: [page])
-                            let nddata = NSMutableData(data: data)
-                            
-                            /*
-                            UIGraphicsBeginPDFContextToData(nddata, CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height), nil)
+                        if let features = self.textDetector?.features(in: image, options: nil) as? [GMVTextBlockFeature] {
                             
                             
                             for textBlock in features {
                             for textBlock in features {
                                 for textLine in textBlock.lines {
                                 for textLine in textBlock.lines {
-                                    for textElement in textLine.elements {
-                             
-                                        let font = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.regular)
-                             
-                                        let paragraphStyle:NSMutableParagraphStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
-                                        paragraphStyle.alignment = NSTextAlignment.left
-                                        paragraphStyle.lineBreakMode = NSLineBreakMode.byWordWrapping
-                             
-                                        let textFontAttributes = [
-                                            NSAttributedString.Key.font: font,
-                                            NSAttributedString.Key.foregroundColor: UIColor.red,
-                                            NSAttributedString.Key.paragraphStyle: paragraphStyle
-                                        ]
-                             
-                                        let text:NSString = textElement.value! as NSString
-                             
-                                        text.draw(in: textElement.bounds, withAttributes: textFontAttributes)
-                                    }
+                                    
+                                    let bounds = textLine.bounds
+                                    let text = textLine.value!
+                                    
+                                    let font = UIFont.systemFont(ofSize: bounds.size.height, weight: .regular)
+                                    let bestFittingFont = UIFont.bestFittingFont(for: text, in: bounds, fontDescriptor: font.fontDescriptor, additionalAttributes: nil)
+                                    let paragraphStyle:NSMutableParagraphStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
+                                    paragraphStyle.alignment = NSTextAlignment.natural
+                                    
+                                    let textFontAttributes = [NSAttributedString.Key.font: bestFittingFont, NSAttributedString.Key.foregroundColor: UIColor.red]
+                                    text.draw(in: bounds, withAttributes: textFontAttributes)
                                 }
                                 }
                             }
                             }
-                            
-                            UIGraphicsEndPDFContext()
-                            */
-
-                            let page = PDFPage.binary(nddata as Data)
-                            pdfPages.append(page)
-                            
-                        } catch (let error) {
-                            print(error)
-                            continue
                         }
                         }
                     }
                     }
-                    
-                } else {
-                
-                    let page = PDFPage.image(UIImage(data: data)!)
-                    pdfPages.append(page) 
                 }
                 }
             }
             }
             
             
+            UIGraphicsEndPDFContext();
+            
             do {
             do {
-                try PDFGenerator.generate(pdfPages, to: fileNameGenerateExport, password: password)
+                try pdfData.write(toFile: fileNameGenerateExport, options: .atomic)
             } catch {
             } catch {
-                self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
-                return
+                print("error catched")
             }
             }
         }
         }
         
         
@@ -707,4 +676,35 @@ class NCCreateScanDocument : NSObject, ImageScannerControllerDelegate {
     }
     }
 }
 }
 
 
-
+extension UIFont {
+    
+    /**
+     Will return the best font conforming to the descriptor which will fit in the provided bounds.
+     */
+    static func bestFittingFontSize(for text: String, in bounds: CGRect, fontDescriptor: UIFontDescriptor, additionalAttributes: [NSAttributedString.Key: Any]? = nil) -> CGFloat {
+        let constrainingDimension = min(bounds.width, bounds.height)
+        let properBounds = CGRect(origin: .zero, size: bounds.size)
+        var attributes = additionalAttributes ?? [:]
+        
+        let infiniteBounds = CGSize(width: CGFloat.infinity, height: CGFloat.infinity)
+        var bestFontSize: CGFloat = constrainingDimension
+        
+        for fontSize in stride(from: bestFontSize, through: 0, by: -1) {
+            let newFont = UIFont(descriptor: fontDescriptor, size: fontSize)
+            attributes[.font] = newFont
+            
+            let currentFrame = text.boundingRect(with: infiniteBounds, options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: attributes, context: nil)
+            
+            if properBounds.contains(currentFrame) {
+                bestFontSize = fontSize
+                break
+            }
+        }
+        return bestFontSize
+    }
+    
+    static func bestFittingFont(for text: String, in bounds: CGRect, fontDescriptor: UIFontDescriptor, additionalAttributes: [NSAttributedString.Key: Any]? = nil) -> UIFont {
+        let bestSize = bestFittingFontSize(for: text, in: bounds, fontDescriptor: fontDescriptor, additionalAttributes: additionalAttributes)
+        return UIFont(descriptor: fontDescriptor, size: bestSize)
+    }
+}

+ 2 - 3
iOSClient/Scan/ScanCollectionView.swift

@@ -22,7 +22,6 @@
 //
 //
 
 
 import UIKit
 import UIKit
-import PDFGenerator
 
 
 @available(iOS 11, *)
 @available(iOS 11, *)
 
 
@@ -254,7 +253,7 @@ class DragDropViewController: UIViewController {
                     let fileName = item.dragItem.localObject as! NSString
                     let fileName = item.dragItem.localObject as! NSString
                     let fileNamePathAt = CCUtility.getDirectoryScan() + "/" + (fileName as String)
                     let fileNamePathAt = CCUtility.getDirectoryScan() + "/" + (fileName as String)
                     
                     
-                    guard let data = try? Data(contentsOf: fileNamePathAt.url) else {
+                    guard let data = try? Data(contentsOf: URL(fileURLWithPath: fileNamePathAt)) else {
                         return
                         return
                     }
                     }
                     guard let image =  UIImage(data: data) else {
                     guard let image =  UIImage(data: data) else {
@@ -345,7 +344,7 @@ extension DragDropViewController : UICollectionViewDataSource {
             
             
             let fileNamePath = CCUtility.getDirectoryScan() + "/" + self.itemsSource[indexPath.row]
             let fileNamePath = CCUtility.getDirectoryScan() + "/" + self.itemsSource[indexPath.row]
             
             
-            guard let data = try? Data(contentsOf: fileNamePath.url) else {
+            guard let data = try? Data(contentsOf: URL(fileURLWithPath: fileNamePath)) else {
                 return cell
                 return cell
             }
             }
             
             

+ 1 - 10
iOSClient/Settings/Acknowledgements.rtf

@@ -1,4 +1,4 @@
-{\rtf1\ansi\ansicpg1252\cocoartf1671\cocoasubrtf200
+{\rtf1\ansi\ansicpg1252\cocoartf1671\cocoasubrtf500
 {\fonttbl\f0\fswiss\fcharset0 Helvetica;\f1\fswiss\fcharset0 Helvetica-Bold;}
 {\fonttbl\f0\fswiss\fcharset0 Helvetica;\f1\fswiss\fcharset0 Helvetica-Bold;}
 {\colortbl;\red255\green255\blue255;}
 {\colortbl;\red255\green255\blue255;}
 {\*\expandedcolortbl;;}
 {\*\expandedcolortbl;;}
@@ -247,15 +247,6 @@ The MIT License (MIT)\
 Copyright (c) libobjc\
 Copyright (c) libobjc\
 ____________________________________________\
 ____________________________________________\
 \
 \
-
-\f1\b PDFGenerator
-\f0\b0 \
-\
-The MIT License (MIT)\
-\
-Copyright (c) Suguru Kishimoto\
-____________________________________________\
-\
 Includes a modified version of 
 Includes a modified version of 
 \f1\b WeScan 
 \f1\b WeScan 
 \f0\b0 \
 \f0\b0 \