123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- //
- // KMBackgroundModel.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2022/12/23.
- //
- import Cocoa
- @objcMembers class KMBackgroundModel: NSObject {
-
- var type: CPDFBackgroundType = .color
- var color: NSColor?
- var imagePath: String?
- var rotation: CGFloat = 0.0
- var opacity: CGFloat = 1.0
- var isScale: Bool = false
- var scale: CGFloat = 1.0
- var verticalMode: Int = 1
- var verticalSpace: CGFloat = 0.0
- var horizontalMode: Int = 1
- var horizontalSpace: CGFloat = 0.0
-
- var backgroundID: String = ""
- var name: String = ""
-
- override init() {
- super.init()
-
- self.type = .color
- if let color = KMBackgroundModel.defaultColors().first {
- self.color = color
- }
- self.imagePath = nil
- self.rotation = 0
- self.opacity = 1
- self.isScale = false
- self.scale = 1
- self.verticalMode = 1
- self.horizontalMode = 1
- self.verticalSpace = 0
- self.horizontalSpace = 0
- self.backgroundID = NSString.tagString()
- self.name = "Background0"
-
- }
-
- func modelPreviewImage() -> NSImage? {
- if let resultPath = KMBackgroundManager.defaultManager.kBackgroundImageFolder?.stringByAppendingPathComponent((self.backgroundID + ".png")) {
- if let image = NSImage(contentsOfFile: resultPath) {
- return image
- }
- }
- return nil
- }
-
- func image() -> NSImage? {
- if let imagePath = self.imagePath {
- if let image = NSImage(contentsOfFile: imagePath) {
- return image
- }
- }
- return nil
- }
-
- //更新预览图
- func updatePreviewImage() {
- if let resultPath = KMBackgroundManager.defaultManager.kBackgroundImageFolder?.stringByAppendingPathComponent((self.backgroundID + ".png")) {
- let image = self.drawImage(rect: CGRectMake(0, 0, 136, 181))
-
- try?image.pngData()?.write(to: URL(fileURLWithPath: resultPath))
- }
- }
-
- func drawImage(rect: CGRect) -> NSImage {
- let 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 _: 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
-
- let oldStrokeColor = self.color?.usingColorSpace(NSColorSpace.deviceRGB)
- oldStrokeColor?.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 == .image) {
- var size: NSSize = CGSizeZero
- if let image = self.image() {
- size = 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
- }
-
- }
- extension KMBackgroundModel {
- class func defaultColors() -> [NSColor] {
-
- let colorA = NSColor(red: 247/255, green: 247/255, blue: 247/255, alpha: 1)
- let colorB = NSColor(red: 255/255, green: 222/255, blue: 222/255, alpha: 1)
- let colorC = NSColor(red: 211/255, green: 242/255, blue: 255/255, alpha: 1)
- let colorD = NSColor(red: 221/255, green: 252/255, blue: 241/255, alpha: 1)
- let colorE = NSColor(red: 255/255, green: 244/255, blue: 204/255, alpha: 1)
-
- return [colorA, colorB, colorC, colorD, colorE]
- }
- }
|