KMBackgroundModel.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // KMBackgroundModel.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/23.
  6. //
  7. import Cocoa
  8. @objcMembers class KMBackgroundModel: NSObject {
  9. var type: CPDFBackgroundType = .color
  10. var color: NSColor?
  11. var imagePath: String?
  12. var rotation: CGFloat = 0.0
  13. var opacity: CGFloat = 1.0
  14. var isScale: Bool = false
  15. var scale: CGFloat = 1.0
  16. var verticalMode: Int = 1
  17. var verticalSpace: CGFloat = 0.0
  18. var horizontalMode: Int = 1
  19. var horizontalSpace: CGFloat = 0.0
  20. var backgroundID: String = ""
  21. var name: String = ""
  22. override init() {
  23. super.init()
  24. self.type = .color
  25. if let color = KMBackgroundModel.defaultColors().first {
  26. self.color = color
  27. }
  28. self.imagePath = nil
  29. self.rotation = 0
  30. self.opacity = 1
  31. self.isScale = false
  32. self.scale = 1
  33. self.verticalMode = 1
  34. self.horizontalMode = 1
  35. self.verticalSpace = 0
  36. self.horizontalSpace = 0
  37. self.backgroundID = NSString.tagString()
  38. self.name = "Background0"
  39. }
  40. func modelPreviewImage() -> NSImage? {
  41. if let resultPath = KMBackgroundManager.defaultManager.kBackgroundImageFolder?.stringByAppendingPathComponent((self.backgroundID + ".png")) {
  42. if let image = NSImage(contentsOfFile: resultPath) {
  43. return image
  44. }
  45. }
  46. return nil
  47. }
  48. func image() -> NSImage? {
  49. if let imagePath = self.imagePath {
  50. if let image = NSImage(contentsOfFile: imagePath) {
  51. return image
  52. }
  53. }
  54. return nil
  55. }
  56. //更新预览图
  57. func updatePreviewImage() {
  58. if let resultPath = KMBackgroundManager.defaultManager.kBackgroundImageFolder?.stringByAppendingPathComponent((self.backgroundID + ".png")) {
  59. let image = self.drawImage(rect: CGRectMake(0, 0, 136, 181))
  60. try?image.pngData()?.write(to: URL(fileURLWithPath: resultPath))
  61. }
  62. }
  63. func drawImage(rect: CGRect) -> NSImage {
  64. let drawRect: CGRect = NSMakeRect(0, 0, 128, 160)
  65. let image = NSImage(size: drawRect.size)
  66. image.lockFocus()
  67. NSGraphicsContext.current?.imageInterpolation = .high
  68. NSGraphicsContext.saveGraphicsState()
  69. NSColor.clear.set()
  70. drawRect.fill()
  71. NSGraphicsContext.restoreGraphicsState()
  72. let context: CGContext = NSGraphicsContext.current!.cgContext
  73. let _: NSSize = NSMakeSize(drawRect.size.width, drawRect.size.height)
  74. if (context != nil) {
  75. NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
  76. }
  77. NSGraphicsContext.saveGraphicsState()
  78. if (self.type == .color) {
  79. var size = drawRect.size
  80. size.width *= self.scale
  81. size.height *= self.scale
  82. let radian = CGFloat(self.rotation) * (Double.pi/180.0)
  83. let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
  84. var rect = NSMakeRect(0, 0, size.width, size.height).applying(t)
  85. if (self.verticalMode == 0) {
  86. rect.origin.y = drawRect.size.height-rect.size.height
  87. } else if (self.verticalMode == 1) {
  88. rect.origin.y = (drawRect.size.height-rect.size.height) * 0.5
  89. } else if (self.verticalMode == 2) {
  90. rect.origin.y = 0
  91. }
  92. if (self.horizontalMode == 0) {
  93. rect.origin.x = 0
  94. } else if (self.horizontalMode == 1) {
  95. rect.origin.x = (drawRect.size.width-rect.size.width) * 0.5
  96. } else if (self.horizontalMode == 2) {
  97. rect.origin.x = drawRect.size.width-rect.size.width
  98. }
  99. rect.origin.x += self.horizontalSpace
  100. rect.origin.y += self.verticalSpace
  101. let contextCenter = NSPoint(x: rect.midX, y: rect.midY)
  102. context.translateBy(x: contextCenter.x, y: contextCenter.y)
  103. context.rotate(by: radian)
  104. context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
  105. var red: CGFloat = 0
  106. var green: CGFloat = 0
  107. var blue: CGFloat = 0
  108. var alpha: CGFloat = 0
  109. let oldStrokeColor = self.color?.usingColorSpace(NSColorSpace.deviceRGB)
  110. oldStrokeColor?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  111. context.setFillColor(red: red, green: green, blue: blue, alpha: self.opacity);
  112. context.fill(NSMakeRect(rect.origin.x+(rect.size.width-size.width)/2.0,
  113. rect.origin.y+(rect.size.height-size.height)/2.0,
  114. size.width, size.height));
  115. } else if (self.type == .image) {
  116. var size: NSSize = CGSizeZero
  117. if let image = self.image() {
  118. size = image.size
  119. }
  120. size.width *= self.scale
  121. size.height *= self.scale
  122. var wScale = size.width / drawRect.size.width
  123. var hScale = size.height / drawRect.size.height
  124. var scale = max(wScale, hScale)
  125. if (scale > 1) {
  126. size.width /= scale
  127. size.height /= scale
  128. }
  129. let radian = CGFloat(self.rotation) * (Double.pi/180.0)
  130. let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
  131. var rect = NSMakeRect(0, 0, size.width, size.height).applying(t)
  132. if (self.verticalMode == 0) {
  133. rect.origin.y = drawRect.size.height-rect.size.height
  134. } else if (self.verticalMode == 1) {
  135. rect.origin.y = (drawRect.size.height-rect.size.height) * 0.5
  136. } else if (self.verticalMode == 2) {
  137. rect.origin.y = 0
  138. }
  139. if (self.horizontalMode == 0) {
  140. rect.origin.x = 0
  141. } else if (self.horizontalMode == 1) {
  142. rect.origin.x = (drawRect.size.width-rect.size.width) * 0.5
  143. } else if (self.horizontalMode == 2) {
  144. rect.origin.x = drawRect.size.width-rect.size.width
  145. }
  146. rect.origin.x += self.horizontalSpace
  147. rect.origin.y += self.verticalSpace
  148. let contextCenter = NSPoint(x: rect.midX, y: rect.midY)
  149. context.translateBy(x: contextCenter.x, y: contextCenter.y)
  150. context.rotate(by: radian)
  151. context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
  152. let finalRect = NSMakeRect(rect.origin.x+(rect.size.width-size.width)/2.0,
  153. rect.origin.y+(rect.size.height-size.height)/2.0,
  154. size.width, size.height)
  155. image.draw(in: NSMakeRect(rect.origin.x+(rect.size.width-size.width)/2.0,
  156. rect.origin.y+(rect.size.height-size.height)/2.0,
  157. size.width, size.height), from: NSZeroRect, operation: .sourceOver, fraction: self.opacity)
  158. }
  159. NSGraphicsContext.restoreGraphicsState()
  160. NSGraphicsContext.current?.imageInterpolation = .default
  161. image.unlockFocus()
  162. return image
  163. }
  164. }
  165. extension KMBackgroundModel {
  166. class func defaultColors() -> [NSColor] {
  167. let colorA = NSColor(red: 247/255, green: 247/255, blue: 247/255, alpha: 1)
  168. let colorB = NSColor(red: 255/255, green: 222/255, blue: 222/255, alpha: 1)
  169. let colorC = NSColor(red: 211/255, green: 242/255, blue: 255/255, alpha: 1)
  170. let colorD = NSColor(red: 221/255, green: 252/255, blue: 241/255, alpha: 1)
  171. let colorE = NSColor(red: 255/255, green: 244/255, blue: 204/255, alpha: 1)
  172. return [colorA, colorB, colorC, colorD, colorE]
  173. }
  174. }