KMBackgroundModel.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // KMBackgroundModel.swift
  3. // PDF Master
  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: Int = 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. override func copy() -> Any {
  27. let model: KMBackgroundModel = KMBackgroundModel()
  28. model.type = self.type
  29. model.color = self.color
  30. model.imagePath = self.imagePath
  31. model.rotation = self.rotation
  32. model.opacity = self.opacity
  33. model.scale = self.scale
  34. model.verticalMode = self.verticalMode
  35. model.verticalSpace = self.verticalSpace
  36. model.horizontalMode = self.horizontalMode
  37. model.horizontalSpace = self.horizontalSpace
  38. model.pageRangeType = self.pageRangeType
  39. model.pagesString = self.pagesString
  40. model.backgroundID = self.backgroundID
  41. model.image = self.image
  42. return model
  43. }
  44. func drawImage(rect: CGRect) -> NSImage {
  45. var drawRect: CGRect = NSMakeRect(0, 0, 128, 160)
  46. let image = NSImage(size: drawRect.size)
  47. image.lockFocus()
  48. NSGraphicsContext.current?.imageInterpolation = .high
  49. NSGraphicsContext.saveGraphicsState()
  50. NSColor.clear.set()
  51. drawRect.fill()
  52. NSGraphicsContext.restoreGraphicsState()
  53. let context: CGContext = NSGraphicsContext.current!.cgContext
  54. let imageSize: NSSize = NSMakeSize(drawRect.size.width, drawRect.size.height)
  55. if (context != nil) {
  56. NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
  57. }
  58. NSGraphicsContext.saveGraphicsState()
  59. if (self.type == .color) {
  60. var size = drawRect.size
  61. size.width *= self.scale
  62. size.height *= self.scale
  63. let radian = CGFloat(self.rotation) * (Double.pi/180.0)
  64. let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
  65. var rect = NSMakeRect(0, 0, size.width, size.height).applying(t)
  66. if (self.verticalMode == 0) {
  67. rect.origin.y = drawRect.size.height-rect.size.height
  68. } else if (self.verticalMode == 1) {
  69. rect.origin.y = (drawRect.size.height-rect.size.height) * 0.5
  70. } else if (self.verticalMode == 2) {
  71. rect.origin.y = 0
  72. }
  73. if (self.horizontalMode == 0) {
  74. rect.origin.x = 0
  75. } else if (self.horizontalMode == 1) {
  76. rect.origin.x = (drawRect.size.width-rect.size.width) * 0.5
  77. } else if (self.horizontalMode == 2) {
  78. rect.origin.x = drawRect.size.width-rect.size.width
  79. }
  80. rect.origin.x += self.horizontalSpace
  81. rect.origin.y += self.verticalSpace
  82. let contextCenter = NSPoint(x: rect.midX, y: rect.midY)
  83. context.translateBy(x: contextCenter.x, y: contextCenter.y)
  84. context.rotate(by: radian)
  85. context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
  86. var red: CGFloat = 0
  87. var green: CGFloat = 0
  88. var blue: CGFloat = 0
  89. var alpha: CGFloat = 0
  90. self.color!.usingColorSpaceName(NSColorSpaceName.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  91. context.setFillColor(red: red, green: green, blue: blue, alpha: self.opacity);
  92. context.fill(NSMakeRect(rect.origin.x+(rect.size.width-size.width)/2.0,
  93. rect.origin.y+(rect.size.height-size.height)/2.0,
  94. size.width, size.height));
  95. } else if (self.type == .file) {
  96. let image = self.image
  97. var size: NSSize = image!.size
  98. size.width *= self.scale
  99. size.height *= self.scale
  100. var wScale = size.width / drawRect.size.width
  101. var hScale = size.height / drawRect.size.height
  102. var scale = max(wScale, hScale)
  103. if (scale > 1) {
  104. size.width /= scale
  105. size.height /= scale
  106. }
  107. let radian = CGFloat(self.rotation) * (Double.pi/180.0)
  108. let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
  109. var rect = NSMakeRect(0, 0, size.width, size.height).applying(t)
  110. if (self.verticalMode == 0) {
  111. rect.origin.y = drawRect.size.height-rect.size.height
  112. } else if (self.verticalMode == 1) {
  113. rect.origin.y = (drawRect.size.height-rect.size.height) * 0.5
  114. } else if (self.verticalMode == 2) {
  115. rect.origin.y = 0
  116. }
  117. if (self.horizontalMode == 0) {
  118. rect.origin.x = 0
  119. } else if (self.horizontalMode == 1) {
  120. rect.origin.x = (drawRect.size.width-rect.size.width) * 0.5
  121. } else if (self.horizontalMode == 2) {
  122. rect.origin.x = drawRect.size.width-rect.size.width
  123. }
  124. rect.origin.x += self.horizontalSpace
  125. rect.origin.y += self.verticalSpace
  126. let contextCenter = NSPoint(x: rect.midX, y: rect.midY)
  127. context.translateBy(x: contextCenter.x, y: contextCenter.y)
  128. context.rotate(by: radian)
  129. context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
  130. let finalRect = NSMakeRect(rect.origin.x+(rect.size.width-size.width)/2.0,
  131. rect.origin.y+(rect.size.height-size.height)/2.0,
  132. size.width, size.height)
  133. image?.draw(in: NSMakeRect(rect.origin.x+(rect.size.width-size.width)/2.0,
  134. rect.origin.y+(rect.size.height-size.height)/2.0,
  135. size.width, size.height), from: NSZeroRect, operation: .sourceOver, fraction: self.opacity)
  136. }
  137. NSGraphicsContext.restoreGraphicsState()
  138. NSGraphicsContext.current?.imageInterpolation = .default
  139. image.unlockFocus()
  140. return image
  141. }
  142. }
  143. class KMBackgroundTemplateModel: KMBackgroundModel {
  144. var isSelected: Bool = false
  145. }