Quellcode durchsuchen

【水平预览】- 管理模版预览

liujiajie vor 11 Monaten
Ursprung
Commit
70eeb64b99

+ 2 - 2
PDF Office/PDF Master/Class/PDFTools/Watermark/Controller/KMBatchOperateAddWatermarkViewController.swift

@@ -142,7 +142,7 @@ class KMBatchOperateAddWatermarkViewController: KMBatchOperateBaseViewController
         self._loadData()
         self.currentType = watermark.image != nil ? .file : .txt
         let arr = self.currentType == .file ? self._fileTemplateArray : self._textTemplateArray
-        let index = arr?.index(of: watermark)
+        let index = arr?.firstIndex(of: watermark)
         if (index != NSNotFound) {
             let indexpath = IndexPath(item: index!, section: 0)
             var set = Set<IndexPath>()
@@ -161,7 +161,7 @@ class KMBatchOperateAddWatermarkViewController: KMBatchOperateBaseViewController
         self._loadData()
         self.currentBackgroundType = background.type
         let arr = self.currentBackgroundType == .file ? self._fileTemplateArray : self._textTemplateArray
-        let index = arr?.index(of: background)
+        let index = arr?.firstIndex(of: background)
         if (index != NSNotFound) {
             let indexpath = IndexPath(item: index!, section: 0)
             var set = Set<IndexPath>()

+ 78 - 78
PDF Office/PDF Master/Class/PDFTools/Watermark/Model/KMWatermarkModel.swift

@@ -372,7 +372,11 @@ import Cocoa
     class func drawImageAtpageRect(rect: NSRect, data: KMWatermarkModel) -> NSImage? {
         var size = NSZeroSize
         let text: String = data.text
-        if text.count > 0 {
+        if let image = data.image {
+            size = image.size
+            size.width *= data.scale
+            size.height *= data.scale
+        }else if text.count > 0 {
             var font = NSFont.systemFont(ofSize: 52)
             if data.isTilePage {
                 font = NSFont.systemFont(ofSize: data.getTextFontSize())
@@ -382,10 +386,6 @@ import Cocoa
             style.lineBreakMode = .byCharWrapping
             let attributes: [NSAttributedString.Key: Any] = [ .paragraphStyle: style, .font: font ]
             size = text.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude), options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: attributes).size
-        } else if let image = data.image {
-            size = image.size
-            size.width *= data.scale
-            size.height *= data.scale
         }
         let radian = data.rotation * (CGFloat.pi/180)
         let t = CGAffineTransform(rotationAngle: radian)
@@ -404,8 +404,79 @@ import Cocoa
         let imageSize = newRect.size
         NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
         NSGraphicsContext.saveGraphicsState()
-
-        if text.count > 0 {
+        
+        if let image = data.image {
+            var size = image.size
+            size.width *= data.scale
+            size.height *= data.scale
+            let width = sqrt(image.size.height * image.size.height + image.size.width * image.size.width)
+            let newRect = CGRect(x: -(width - image.size.width)/2, y: -(width - image.size.height)/2, width: width, height: width)
+            let new_w = newRect.size.width
+            let new_h = newRect.size.height
+            let radian = data.rotation * (CGFloat.pi / 180)
+            let t = CGAffineTransform(rotationAngle: radian)
+            if data.isTilePage {
+                context.translateBy(x: image.size.width/2, y: image.size.height/2)
+                context.concatenate(t)
+                context.translateBy(x: -(image.size.width/2), y: -(image.size.height/2))
+                
+                let verticalWidth = size.width + data.tileHorizontalSpace * data.scale
+                let horizontalHeight = size.height + data.tileVerticalSpace * data.scale
+                let line: Int = Int((new_h - data.tileVerticalSpace * data.scale)/horizontalHeight + 1)
+                let row: Int = Int((new_w - data.tileHorizontalSpace * data.scale) / verticalWidth + 1)
+                let point = CGPoint(x: image.size.width/2 - size.width/2 + data.horizontalSpace*data.scale, y:image.size.height/2 - size.height/2 + data.verticalSpace * data.scale)
+                for i in 0..<line/2+1 {
+                    for j in 0..<row {
+                        let area = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y + CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
+                        data.image.draw(in: area, from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
+                    }
+                }
+                for i in 1..<line/2+1 {
+                    for j in 0..<row {
+                        let area = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y - CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
+                        data.image.draw(in: area, from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
+                    }
+                }
+                for i in 0..<line/2+1 {
+                    for j in 1..<row {
+                        let area = CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y + CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
+                        data.image.draw(in: area, from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
+                    }
+                }
+                for i in 1..<(line/2 + 1) {
+                    for j in 1..<row {
+                        let area = CGRect(x: point.x - CGFloat(j)*verticalWidth, y: point.y - CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
+                        data.image.draw(in: area, from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
+                    }
+                }
+            }else {
+                var size = data.image.size
+                size.width *= data.scale
+                size.height *= data.scale
+                let radian = data.rotation * (CGFloat.pi / 180)
+                let t = CGAffineTransform(rotationAngle: radian)
+                var imageRect = CGRect(origin: .zero, size: size).applying(t)
+                if data.verticalMode == 0 {
+                    imageRect.origin.y = imageSize.height - imageRect.size.height
+                } else if data.verticalMode == 1 {
+                    imageRect.origin.y = (imageSize.height - imageRect.size.height) / 2.0
+                } else {
+                    imageRect.origin.y = 0
+                }
+                if data.horizontalMode == 0 {
+                    imageRect.origin.x = 0
+                } else if data.horizontalMode == 1 {
+                    imageRect.origin.x = (imageSize.width - imageRect.size.width) / 2.0
+                } else {
+                    imageRect.origin.x = imageSize.width - imageRect.size.width
+                }
+                let contextCenter = CGPoint(x: imageRect.midX, y: imageRect.midY)
+                context.translateBy(x: contextCenter.x, y: contextCenter.y)
+                context.rotate(by: radian)
+                context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
+                data.image.draw(in: CGRect(x: imageRect.origin.x+(imageRect.size.width-size.width)/2.0, y: imageRect.origin.y+(imageRect.size.height-size.height)/2.0, width: size.width, height: size.height), from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
+            }
+        }else if text.count > 0 {
             var font = NSFont.systemFont(ofSize: 52)
             if data.isTilePage {
                 font = NSFont.systemFont(ofSize: data.getTextFontSize())
@@ -488,77 +559,6 @@ import Cocoa
                 context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
                 data.text.draw(in: CGRect(x: textRect.origin.x + (textRect.size.width - size.width) / 2.0, y: textRect.origin.y + (textRect.size.height - size.height) / 2.0, width: size.width, height: size.height), withAttributes: attributes)
             }
-        }else if let image = data.image {
-            var size = image.size
-            size.width *= data.scale
-            size.height *= data.scale
-            let width = sqrt(image.size.height * image.size.height + image.size.width * image.size.width)
-            let newRect = CGRect(x: -(width - image.size.width)/2, y: -(width - image.size.height)/2, width: width, height: width)
-            let new_w = newRect.size.width
-            let new_h = newRect.size.height
-            let radian = data.rotation * (CGFloat.pi / 180)
-            let t = CGAffineTransform(rotationAngle: radian)
-            if data.isTilePage {
-                context.translateBy(x: image.size.width/2, y: image.size.height/2)
-                context.concatenate(t)
-                context.translateBy(x: -(image.size.width/2), y: -(image.size.height/2))
-                
-                let verticalWidth = size.width + data.tileHorizontalSpace * data.scale
-                let horizontalHeight = size.height + data.tileVerticalSpace * data.scale
-                let line: Int = Int((new_h - data.tileVerticalSpace * data.scale)/horizontalHeight + 1)
-                let row: Int = Int((new_w - data.tileHorizontalSpace * data.scale) / verticalWidth + 1)
-                let point = CGPoint(x: image.size.width/2 - size.width/2 + data.horizontalSpace*data.scale, y:image.size.height/2 - size.height/2 + data.verticalSpace * data.scale)
-                for i in 0..<line/2+1 {
-                    for j in 0..<row {
-                        let area = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y + CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
-                        data.image.draw(in: area, from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
-                    }
-                }
-                for i in 1..<line/2+1 {
-                    for j in 0..<row {
-                        let area = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y - CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
-                        data.image.draw(in: area, from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
-                    }
-                }
-                for i in 0..<line/2+1 {
-                    for j in 1..<row {
-                        let area = CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y + CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
-                        data.image.draw(in: area, from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
-                    }
-                }
-                for i in 1..<(line/2 + 1) {
-                    for j in 1..<row {
-                        let area = CGRect(x: point.x - CGFloat(j)*verticalWidth, y: point.y - CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
-                        data.image.draw(in: area, from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
-                    }
-                }
-            }else {
-                var size = data.image.size
-                size.width *= data.scale
-                size.height *= data.scale
-                let radian = data.rotation * (CGFloat.pi / 180)
-                let t = CGAffineTransform(rotationAngle: radian)
-                var imageRect = CGRect(origin: .zero, size: size).applying(t)
-                if data.verticalMode == 0 {
-                    imageRect.origin.y = imageSize.height - imageRect.size.height
-                } else if data.verticalMode == 1 {
-                    imageRect.origin.y = (imageSize.height - imageRect.size.height) / 2.0
-                } else {
-                    imageRect.origin.y = 0
-                }
-                if data.horizontalMode == 0 {
-                    imageRect.origin.x = 0
-                } else if data.horizontalMode == 1 {
-                    imageRect.origin.x = (imageSize.width - imageRect.size.width) / 2.0
-                } else {
-                    imageRect.origin.x = imageSize.width - imageRect.size.width
-                }
-                let contextCenter = CGPoint(x: imageRect.midX, y: imageRect.midY)
-                context.translateBy(x: contextCenter.x, y: contextCenter.y)
-                context.rotate(by: radian)
-                context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
-                data.image.draw(in: CGRect(x: imageRect.origin.x+(imageRect.size.width-size.width)/2.0, y: imageRect.origin.y+(imageRect.size.height-size.height)/2.0, width: size.width, height: size.height), from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
-            }
         }
         NSGraphicsContext.restoreGraphicsState()
         NSGraphicsContext.current?.imageInterpolation = .default