KMBackgroundModel.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // KMBackgroundModel.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/23.
  6. //
  7. import Cocoa
  8. @objc enum KMBackgroundType: Int {
  9. case color = 0
  10. case file = 1
  11. }
  12. @objcMembers class KMBackgroundModel: KMWatermarkAdjectiveBaseModel {
  13. var type: KMBackgroundType = .color
  14. var color: NSColor? = NSColor.red
  15. var imagePath: String = ""
  16. var rotation: CGFloat = 0.0
  17. var opacity: CGFloat = 1.0
  18. var scale: CGFloat = 1.0
  19. var verticalMode: Int = 1
  20. var verticalSpace: CGFloat = 0.0
  21. var horizontalMode: Int = 1
  22. var horizontalSpace: CGFloat = 0.0
  23. var pagesString: String = ""
  24. var backgroundID: String = ""
  25. var image: NSImage!
  26. // 是否数据迁移(原App数据迁移)
  27. var isMigrate = false
  28. // 是否已被更新
  29. var isUpdated = false
  30. var modificationDate: TimeInterval?
  31. var isRemoved = false
  32. override func copy() -> Any {
  33. let model: KMBackgroundModel = KMBackgroundModel()
  34. model.type = self.type
  35. model.color = self.color
  36. model.imagePath = self.imagePath
  37. model.rotation = self.rotation
  38. model.opacity = self.opacity
  39. model.scale = self.scale
  40. model.verticalMode = self.verticalMode
  41. model.verticalSpace = self.verticalSpace
  42. model.horizontalMode = self.horizontalMode
  43. model.horizontalSpace = self.horizontalSpace
  44. model.pageRangeType = self.pageRangeType
  45. model.pagesString = self.pagesString
  46. model.backgroundID = self.backgroundID
  47. model.image = self.image
  48. model.isMigrate = self.isMigrate
  49. model.isUpdated = self.isUpdated
  50. model.modificationDate = self.modificationDate
  51. model.isRemoved = self.isRemoved
  52. return model
  53. }
  54. func drawImage(rect: CGRect) -> NSImage {
  55. var drawRect: CGRect = NSMakeRect(0, 0, 128, 160)
  56. let image = NSImage(size: drawRect.size)
  57. image.lockFocus()
  58. NSGraphicsContext.current?.imageInterpolation = .high
  59. NSGraphicsContext.saveGraphicsState()
  60. NSColor.clear.set()
  61. drawRect.fill()
  62. NSGraphicsContext.restoreGraphicsState()
  63. let context: CGContext = NSGraphicsContext.current!.cgContext
  64. let imageSize: NSSize = NSMakeSize(drawRect.size.width, drawRect.size.height)
  65. if (context != nil) {
  66. NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
  67. }
  68. NSGraphicsContext.saveGraphicsState()
  69. if (self.type == .color) {
  70. var size = drawRect.size
  71. size.width *= self.scale
  72. size.height *= self.scale
  73. let radian = CGFloat(self.rotation) * (Double.pi/180.0)
  74. let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
  75. var rect = NSMakeRect(0, 0, size.width, size.height).applying(t)
  76. if (self.verticalMode == 0) {
  77. rect.origin.y = drawRect.size.height-rect.size.height
  78. } else if (self.verticalMode == 1) {
  79. rect.origin.y = (drawRect.size.height-rect.size.height) * 0.5
  80. } else if (self.verticalMode == 2) {
  81. rect.origin.y = 0
  82. }
  83. if (self.horizontalMode == 0) {
  84. rect.origin.x = 0
  85. } else if (self.horizontalMode == 1) {
  86. rect.origin.x = (drawRect.size.width-rect.size.width) * 0.5
  87. } else if (self.horizontalMode == 2) {
  88. rect.origin.x = drawRect.size.width-rect.size.width
  89. }
  90. rect.origin.x += self.horizontalSpace
  91. rect.origin.y += self.verticalSpace
  92. let contextCenter = NSPoint(x: rect.midX, y: rect.midY)
  93. context.translateBy(x: contextCenter.x, y: contextCenter.y)
  94. context.rotate(by: radian)
  95. context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
  96. var red: CGFloat = 0
  97. var green: CGFloat = 0
  98. var blue: CGFloat = 0
  99. var alpha: CGFloat = 0
  100. self.color!.usingColorSpaceName(NSColorSpaceName.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  101. context.setFillColor(red: red, green: green, blue: blue, alpha: self.opacity);
  102. context.fill(NSMakeRect(rect.origin.x+(rect.size.width-size.width)/2.0,
  103. rect.origin.y+(rect.size.height-size.height)/2.0,
  104. size.width, size.height));
  105. } else if (self.type == .file) {
  106. let image = self.image
  107. var size: NSSize = image!.size
  108. size.width *= self.scale
  109. size.height *= self.scale
  110. var wScale = size.width / drawRect.size.width
  111. var hScale = size.height / drawRect.size.height
  112. var scale = max(wScale, hScale)
  113. if (scale > 1) {
  114. size.width /= scale
  115. size.height /= scale
  116. }
  117. let radian = CGFloat(self.rotation) * (Double.pi/180.0)
  118. let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
  119. var rect = NSMakeRect(0, 0, size.width, size.height).applying(t)
  120. if (self.verticalMode == 0) {
  121. rect.origin.y = drawRect.size.height-rect.size.height
  122. } else if (self.verticalMode == 1) {
  123. rect.origin.y = (drawRect.size.height-rect.size.height) * 0.5
  124. } else if (self.verticalMode == 2) {
  125. rect.origin.y = 0
  126. }
  127. if (self.horizontalMode == 0) {
  128. rect.origin.x = 0
  129. } else if (self.horizontalMode == 1) {
  130. rect.origin.x = (drawRect.size.width-rect.size.width) * 0.5
  131. } else if (self.horizontalMode == 2) {
  132. rect.origin.x = drawRect.size.width-rect.size.width
  133. }
  134. rect.origin.x += self.horizontalSpace
  135. rect.origin.y += self.verticalSpace
  136. let contextCenter = NSPoint(x: rect.midX, y: rect.midY)
  137. context.translateBy(x: contextCenter.x, y: contextCenter.y)
  138. context.rotate(by: radian)
  139. context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
  140. let finalRect = NSMakeRect(rect.origin.x+(rect.size.width-size.width)/2.0,
  141. rect.origin.y+(rect.size.height-size.height)/2.0,
  142. size.width, size.height)
  143. image?.draw(in: NSMakeRect(rect.origin.x+(rect.size.width-size.width)/2.0,
  144. rect.origin.y+(rect.size.height-size.height)/2.0,
  145. size.width, size.height), from: NSZeroRect, operation: .sourceOver, fraction: self.opacity)
  146. }
  147. NSGraphicsContext.restoreGraphicsState()
  148. NSGraphicsContext.current?.imageInterpolation = .default
  149. image.unlockFocus()
  150. return image
  151. }
  152. }
  153. class KMBackgroundTemplateModel: KMBackgroundModel {
  154. var isSelected: Bool = false
  155. }