123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- //
- // KMBackgroundModel.swift
- // PDF Master
- //
- // Created by tangchao on 2022/12/23.
- //
- import Cocoa
- @objc enum KMBackgroundType: Int {
- case color = 0
- case file = 1
- }
- @objcMembers class KMBackgroundModel: KMWatermarkAdjectiveBaseModel {
- var type: KMBackgroundType = .color
- var color: NSColor? = NSColor.red
- var imagePath: String = ""
-
- var rotation: Int = 0
- var opacity: CGFloat = 1.0
-
- var scale: CGFloat = 1.0
-
- var verticalMode: Int = 1
- var verticalSpace: CGFloat = 0.0
- var horizontalMode: Int = 1
- var horizontalSpace: CGFloat = 0.0
-
- var pagesString: String = ""
-
- var backgroundID: String = ""
-
- var image: NSImage!
-
- override func copy() -> Any {
- let model: KMBackgroundModel = KMBackgroundModel()
- model.type = self.type
- model.color = self.color
- model.imagePath = self.imagePath
-
- model.rotation = self.rotation
- model.opacity = self.opacity
-
- model.scale = self.scale
-
- model.verticalMode = self.verticalMode
- model.verticalSpace = self.verticalSpace
- model.horizontalMode = self.horizontalMode
- model.horizontalSpace = self.horizontalSpace
-
- model.pageRangeType = self.pageRangeType
- model.pagesString = self.pagesString
-
- model.backgroundID = self.backgroundID
- model.image = self.image
-
- return model
- }
-
- func drawImage(rect: CGRect) -> NSImage {
- var drawRect: CGRect = NSMakeRect(0, 0, 128, 160)
- let image = NSImage(size: drawRect.size)
- image.lockFocus()
- NSGraphicsContext.current?.imageInterpolation = .high
- NSGraphicsContext.saveGraphicsState()
- NSColor.clear.set()
- drawRect.fill()
- NSGraphicsContext.restoreGraphicsState()
- let context: CGContext = NSGraphicsContext.current!.cgContext
- let imageSize: NSSize = NSMakeSize(drawRect.size.width, drawRect.size.height)
- if (context != nil) {
- NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
- }
- NSGraphicsContext.saveGraphicsState()
- if (self.type == .color) {
- var size = drawRect.size
- size.width *= self.scale
- size.height *= self.scale
-
- let radian = CGFloat(self.rotation) * (Double.pi/180.0)
- let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
- var rect = NSMakeRect(0, 0, size.width, size.height).applying(t)
-
- if (self.verticalMode == 0) {
- rect.origin.y = drawRect.size.height-rect.size.height
- } else if (self.verticalMode == 1) {
- rect.origin.y = (drawRect.size.height-rect.size.height) * 0.5
- } else if (self.verticalMode == 2) {
- rect.origin.y = 0
- }
-
- if (self.horizontalMode == 0) {
- rect.origin.x = 0
- } else if (self.horizontalMode == 1) {
- rect.origin.x = (drawRect.size.width-rect.size.width) * 0.5
- } else if (self.horizontalMode == 2) {
- rect.origin.x = drawRect.size.width-rect.size.width
- }
-
- rect.origin.x += self.horizontalSpace
- rect.origin.y += self.verticalSpace
-
- let contextCenter = NSPoint(x: rect.midX, y: rect.midY)
- context.translateBy(x: contextCenter.x, y: contextCenter.y)
- context.rotate(by: radian)
- context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
-
- var red: CGFloat = 0
- var green: CGFloat = 0
- var blue: CGFloat = 0
- var alpha: CGFloat = 0
- self.color!.usingColorSpaceName(NSColorSpaceName.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
- context.setFillColor(red: red, green: green, blue: blue, alpha: self.opacity);
- context.fill(NSMakeRect(rect.origin.x+(rect.size.width-size.width)/2.0,
- rect.origin.y+(rect.size.height-size.height)/2.0,
- size.width, size.height));
- } else if (self.type == .file) {
- let image = self.image
- var size: NSSize = image!.size
- size.width *= self.scale
- size.height *= self.scale
-
- var wScale = size.width / drawRect.size.width
- var hScale = size.height / drawRect.size.height
-
- var scale = max(wScale, hScale)
- if (scale > 1) {
- size.width /= scale
- size.height /= scale
- }
-
- let radian = CGFloat(self.rotation) * (Double.pi/180.0)
- let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
- var rect = NSMakeRect(0, 0, size.width, size.height).applying(t)
-
- if (self.verticalMode == 0) {
- rect.origin.y = drawRect.size.height-rect.size.height
- } else if (self.verticalMode == 1) {
- rect.origin.y = (drawRect.size.height-rect.size.height) * 0.5
- } else if (self.verticalMode == 2) {
- rect.origin.y = 0
- }
-
- if (self.horizontalMode == 0) {
- rect.origin.x = 0
- } else if (self.horizontalMode == 1) {
- rect.origin.x = (drawRect.size.width-rect.size.width) * 0.5
- } else if (self.horizontalMode == 2) {
- rect.origin.x = drawRect.size.width-rect.size.width
- }
-
- rect.origin.x += self.horizontalSpace
- rect.origin.y += self.verticalSpace
-
- let contextCenter = NSPoint(x: rect.midX, y: rect.midY)
- context.translateBy(x: contextCenter.x, y: contextCenter.y)
- context.rotate(by: radian)
- context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
-
- let finalRect = NSMakeRect(rect.origin.x+(rect.size.width-size.width)/2.0,
- rect.origin.y+(rect.size.height-size.height)/2.0,
- size.width, size.height)
- image?.draw(in: NSMakeRect(rect.origin.x+(rect.size.width-size.width)/2.0,
- rect.origin.y+(rect.size.height-size.height)/2.0,
- size.width, size.height), from: NSZeroRect, operation: .sourceOver, fraction: self.opacity)
- }
- NSGraphicsContext.restoreGraphicsState()
- NSGraphicsContext.current?.imageInterpolation = .default
- image.unlockFocus()
- return image
- }
- }
- class KMBackgroundTemplateModel: KMBackgroundModel {
- var isSelected: Bool = false
-
- }
|