KMBackgroundModel.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. self.color = NSColor(red: 247/255, green: 247/255, blue: 247/255, alpha: 1)
  26. self.imagePath = nil
  27. self.rotation = 0
  28. self.opacity = 1
  29. self.isScale = false
  30. self.scale = 1
  31. self.verticalMode = 1
  32. self.horizontalMode = 1
  33. self.verticalSpace = 0
  34. self.horizontalSpace = 0
  35. self.backgroundID = NSString.tagString()
  36. self.name = "Background0"
  37. }
  38. func modelPreviewImage() -> NSImage? {
  39. if let resultPath = KMBackgroundManager.defaultManager.kBackgroundImageFolder?.stringByAppendingPathComponent((self.backgroundID + ".png")) {
  40. if let image = NSImage(contentsOfFile: resultPath) {
  41. return image
  42. }
  43. }
  44. return nil
  45. }
  46. func image() -> NSImage? {
  47. if let imagePath = self.imagePath {
  48. if let image = NSImage(contentsOfFile: imagePath) {
  49. return image
  50. }
  51. }
  52. return nil
  53. }
  54. //更新预览图
  55. func updatePreviewImage() {
  56. if let resultPath = KMBackgroundManager.defaultManager.kBackgroundImageFolder?.stringByAppendingPathComponent((self.backgroundID + ".png")) {
  57. let image = self.drawImage(rect: CGRectMake(0, 0, 136, 181))
  58. try?image.pngData()?.write(to: URL(fileURLWithPath: resultPath))
  59. }
  60. }
  61. func removeModelCacheInfo() {
  62. if let imagePath = self.imagePath {
  63. try?FileManager.default.removeItem(atPath: imagePath)
  64. }
  65. if let imagePath = KMBackgroundManager.defaultManager.kBackgroundImageFolder?.stringByAppendingPathComponent((self.backgroundID + ".png")) {
  66. try?FileManager.default.removeItem(atPath: imagePath)
  67. }
  68. }
  69. func drawImage(rect: CGRect) -> NSImage {
  70. let drawRect: CGRect = NSMakeRect(0, 0, 128, 160)
  71. var image = NSImage(size: drawRect.size)
  72. image.lockFocus()
  73. NSGraphicsContext.current?.imageInterpolation = .high
  74. NSGraphicsContext.saveGraphicsState()
  75. NSColor.clear.set()
  76. drawRect.fill()
  77. NSGraphicsContext.restoreGraphicsState()
  78. let context: CGContext = NSGraphicsContext.current!.cgContext
  79. let _: NSSize = NSMakeSize(drawRect.size.width, drawRect.size.height)
  80. if (context != nil) {
  81. NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
  82. }
  83. NSGraphicsContext.saveGraphicsState()
  84. if (self.type == .color) {
  85. var size = drawRect.size
  86. size.width *= self.scale
  87. size.height *= self.scale
  88. let radian = CGFloat(self.rotation) * (Double.pi/180.0)
  89. let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
  90. var rect = NSMakeRect(0, 0, size.width, size.height).applying(t)
  91. if (self.verticalMode == 0) {
  92. rect.origin.y = drawRect.size.height-rect.size.height
  93. } else if (self.verticalMode == 1) {
  94. rect.origin.y = (drawRect.size.height-rect.size.height) * 0.5
  95. } else if (self.verticalMode == 2) {
  96. rect.origin.y = 0
  97. }
  98. if (self.horizontalMode == 0) {
  99. rect.origin.x = 0
  100. } else if (self.horizontalMode == 1) {
  101. rect.origin.x = (drawRect.size.width-rect.size.width) * 0.5
  102. } else if (self.horizontalMode == 2) {
  103. rect.origin.x = drawRect.size.width-rect.size.width
  104. }
  105. rect.origin.x += self.horizontalSpace
  106. rect.origin.y += self.verticalSpace
  107. let contextCenter = NSPoint(x: rect.midX, y: rect.midY)
  108. context.translateBy(x: contextCenter.x, y: contextCenter.y)
  109. context.rotate(by: radian)
  110. context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
  111. var red: CGFloat = 0
  112. var green: CGFloat = 0
  113. var blue: CGFloat = 0
  114. var alpha: CGFloat = 0
  115. let oldStrokeColor = self.color?.usingColorSpace(NSColorSpace.deviceRGB)
  116. oldStrokeColor?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  117. context.setFillColor(red: red, green: green, blue: blue, alpha: self.opacity);
  118. context.fill(NSMakeRect(rect.origin.x+(rect.size.width-size.width)/2.0,
  119. rect.origin.y+(rect.size.height-size.height)/2.0,
  120. size.width, size.height));
  121. } else if (self.type == .image) {
  122. var size: NSSize = CGSizeZero
  123. if let img = self.image() {
  124. image = img
  125. size = image.size
  126. }
  127. size.width *= self.scale
  128. size.height *= self.scale
  129. var wScale = size.width / drawRect.size.width
  130. var hScale = size.height / drawRect.size.height
  131. var scale = max(wScale, hScale)
  132. if (scale > 1) {
  133. size.width /= scale
  134. size.height /= scale
  135. }
  136. let radian = CGFloat(self.rotation) * (Double.pi/180.0)
  137. let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
  138. var rect = NSMakeRect(0, 0, size.width, size.height).applying(t)
  139. if (self.verticalMode == 0) {
  140. rect.origin.y = drawRect.size.height-rect.size.height
  141. } else if (self.verticalMode == 1) {
  142. rect.origin.y = (drawRect.size.height-rect.size.height) * 0.5
  143. } else if (self.verticalMode == 2) {
  144. rect.origin.y = 0
  145. }
  146. if (self.horizontalMode == 0) {
  147. rect.origin.x = 0
  148. } else if (self.horizontalMode == 1) {
  149. rect.origin.x = (drawRect.size.width-rect.size.width) * 0.5
  150. } else if (self.horizontalMode == 2) {
  151. rect.origin.x = drawRect.size.width-rect.size.width
  152. }
  153. rect.origin.x += self.horizontalSpace
  154. rect.origin.y += self.verticalSpace
  155. let contextCenter = NSPoint(x: rect.midX, y: rect.midY)
  156. context.translateBy(x: contextCenter.x, y: contextCenter.y)
  157. context.rotate(by: radian)
  158. context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
  159. let finalRect = NSMakeRect(rect.origin.x+(rect.size.width-size.width)/2.0,
  160. rect.origin.y+(rect.size.height-size.height)/2.0,
  161. size.width, size.height)
  162. image.draw(in: NSMakeRect(rect.origin.x+(rect.size.width-size.width)/2.0,
  163. rect.origin.y+(rect.size.height-size.height)/2.0,
  164. size.width, size.height), from: NSZeroRect, operation: .sourceOver, fraction: self.opacity)
  165. }
  166. NSGraphicsContext.restoreGraphicsState()
  167. NSGraphicsContext.current?.imageInterpolation = .default
  168. image.unlockFocus()
  169. return image
  170. }
  171. }