KMWatermarkPDFView.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // KMWatermarkPDFView.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/12/19.
  6. //
  7. import Cocoa
  8. class KMWatermarkPDFView: CPDFView {
  9. var watermarkModel: KMWatermarkModel!
  10. override func draw(_ page: CPDFPage!, to context: CGContext!) {
  11. if (self.watermarkModel == nil) {
  12. super.draw(page, to: context)
  13. return
  14. }
  15. if (self.watermarkModel.isFront) {
  16. super.draw(page, to: context)
  17. if (self.needDraw(page)) {
  18. drawWatermarkPage(page, to: context)
  19. }
  20. } else {
  21. if (self.needDraw(page)) {
  22. drawWatermarkPage(page, to: context)
  23. }
  24. super.draw(page, to: context)
  25. }
  26. }
  27. func needDraw(_ page: CPDFPage) -> Bool {
  28. if (self.watermarkModel == nil) {
  29. return true
  30. }
  31. if (self.watermarkModel.pageRangeType == 0) {
  32. return true
  33. }
  34. // else if (self.watermarkModel.pageRangeType == 1) {
  35. // let array = self.watermarkModel.pagesString.components(separatedBy: ",")
  36. // let index: Int = Int(self.document.index(for: page))
  37. // if (!array.contains("\(index+1)")) {
  38. // return false
  39. // }
  40. // }
  41. else if (self.watermarkModel.pageRangeType == 1) {
  42. let index: Int = Int(self.document.index(for: page))
  43. return (index % 2 == 0)
  44. } else if (self.watermarkModel.pageRangeType == 2) {
  45. let index: Int = Int(self.document.index(for: page))
  46. return (index % 2 == 1)
  47. } else if (self.watermarkModel.pageRangeType == 3) {
  48. if (self.watermarkModel.pagesString.isEmpty) {
  49. return false
  50. }
  51. let array = self.watermarkModel.pagesString.components(separatedBy: ",")
  52. let index: Int = Int(self.document.index(for: page))
  53. if (!array.contains("\(index+1)")) {
  54. return false
  55. }
  56. }
  57. return true
  58. }
  59. func drawWatermarkPage(_ page: CPDFPage, to context: CGContext) {
  60. let pageBounds = page.bounds(for: .cropBox)
  61. let w: CGFloat = NSWidth(pageBounds)
  62. let h: CGFloat = NSHeight(pageBounds)
  63. let width: CGFloat = sqrt(w*w+h*h)
  64. var newRect: CGRect = CGRect(x: -(width-w)*0.5, y: -(width-h)*0.5, width: width, height: width)
  65. let new_w: CGFloat = NSWidth(newRect)
  66. let new_h: CGFloat = NSHeight(newRect)
  67. if (context != nil) {
  68. NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
  69. }
  70. NSGraphicsContext.saveGraphicsState()
  71. page.transform(context, for: .cropBox)
  72. // if (self.watermarkModel.pagesString.count > 0) {
  73. // let array = self.watermarkModel.pagesString.components(separatedBy: ",")
  74. // let index: Int = Int(self.document.index(for: page))
  75. // if (!array.contains("\(index)")) {
  76. // return
  77. // }
  78. // }
  79. if (!self.watermarkModel.text.isEmpty) {
  80. var font = self.watermarkModel.textFont
  81. if (font == nil) {
  82. // font = NSFont(name: "Helvetica", size: 48)
  83. }
  84. var color = NSColor.black
  85. if (self.watermarkModel.textColor != nil) {
  86. // color = self.watermarkModel.textColor
  87. }
  88. var red: CGFloat = 0
  89. var green: CGFloat = 0
  90. var blue: CGFloat = 0
  91. color.usingColorSpaceName(NSColorSpaceName.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: nil)
  92. color = NSColor(red: red, green: green, blue: blue, alpha: self.watermarkModel.opacity)
  93. var size = NSZeroSize
  94. let style = NSMutableParagraphStyle()
  95. style.alignment = self.watermarkModel.textAligement
  96. style.lineBreakMode = .byCharWrapping
  97. let dict = [NSAttributedString.Key.paragraphStyle : style,
  98. NSAttributedString.Key.foregroundColor : color,
  99. NSAttributedString.Key.font : font] as [NSAttributedString.Key : Any]
  100. size = self.watermarkModel.text.boundingRect(with: NSSize(width: 1000, height: 1000), options: NSString.DrawingOptions(rawValue: 3), attributes: dict).size
  101. let radian: CGFloat = self.watermarkModel.rotation*(Double.pi/180.0)
  102. let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
  103. var rect:CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
  104. if (self.watermarkModel.isTilePage) {
  105. context.translateBy(x: w * 0.5, y: h * 0.5)
  106. context.concatenate(t)
  107. context.translateBy(x: -(w/2), y: -(h/2))
  108. let verticalWidth: CGFloat = size.width + self.watermarkModel.tileHorizontalSpace
  109. let horizontalHeight: CGFloat = size.height + self.watermarkModel.tileVerticalSpace
  110. let line: Int = Int(((new_h-self.watermarkModel.tileVerticalSpace)/horizontalHeight)+1)
  111. let row: Int = Int(((new_w-self.watermarkModel.tileHorizontalSpace)/verticalWidth)+1)
  112. let point: CGPoint = CGPoint(x: w*0.5-size.width*0.5+self.watermarkModel.horizontalSpace, y: h*0.5-size.height*0.5+self.watermarkModel.verticalSpace)
  113. for i in 0 ..< line {
  114. for j in 0 ..< row {
  115. self.watermarkModel.text.draw(in: NSRect(x: point.x+CGFloat(j)*verticalWidth, y: point.y+CGFloat(i)*horizontalHeight, width: size.width, height: size.height), withAttributes: dict)
  116. }
  117. }
  118. for i in 1 ..< line {
  119. for j in 0 ..< row {
  120. self.watermarkModel.text.draw(in: NSRect(x: point.x+CGFloat(j)*verticalWidth, y: point.y-CGFloat(i)*horizontalHeight, width: size.width, height: size.height), withAttributes: dict)
  121. }
  122. }
  123. for i in 0 ..< line {
  124. for j in 1 ..< row {
  125. self.watermarkModel.text.draw(in: NSRect(x: point.x-CGFloat(j)*verticalWidth, y: point.y+CGFloat(i)*horizontalHeight, width: size.width, height: size.height), withAttributes: dict)
  126. }
  127. }
  128. for i in 1 ..< line {
  129. for j in 1 ..< row {
  130. self.watermarkModel.text.draw(in: NSRect(x: point.x-CGFloat(j)*verticalWidth, y: point.y-CGFloat(i)*horizontalHeight, width: size.width, height: size.height), withAttributes: dict)
  131. }
  132. }
  133. } else {
  134. if (self.watermarkModel.verticalMode == 0) {
  135. rect.origin.y = pageBounds.size.height-rect.size.height
  136. } else if (self.watermarkModel.verticalMode == 1) {
  137. rect.origin.y = (pageBounds.size.height-rect.size.height) * 0.5
  138. } else {
  139. rect.origin.y = 0
  140. }
  141. if (self.watermarkModel.horizontalMode == 0) {
  142. rect.origin.x = 0
  143. } else if (self.watermarkModel.horizontalMode == 1) {
  144. rect.origin.x = (pageBounds.size.width-rect.size.width) * 0.5
  145. } else {
  146. rect.origin.x = pageBounds.size.width-rect.size.width
  147. }
  148. rect.origin.x += self.watermarkModel.horizontalSpace
  149. rect.origin.y += self.watermarkModel.verticalSpace
  150. let contextCenter = CGPoint(x: rect.midX, y: rect.midY)
  151. context.translateBy(x: contextCenter.x, y: contextCenter.y)
  152. context.rotate(by: radian)
  153. context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
  154. self.watermarkModel.text.draw(in: rect, withAttributes: dict)
  155. }
  156. } else if (self.watermarkModel.image != nil) {
  157. let tiffData = self.watermarkModel.image.tiffRepresentation
  158. let bitmap: NSBitmapImageRep!
  159. bitmap = NSBitmapImageRep(data: tiffData!)
  160. let ciImage = CIImage(bitmapImageRep: bitmap)
  161. var size: NSSize = (ciImage?.extent.size)!
  162. size.width *= self.watermarkModel.scale
  163. size.height *= self.watermarkModel.scale
  164. let radian = self.watermarkModel.rotation * (Double.pi / 180.0)
  165. let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
  166. var rect = NSMakeRect(0, 0, size.width, size.height)
  167. if (self.watermarkModel.isTilePage) {
  168. context.translateBy(x: w/2,y: h/2)
  169. context.concatenate(t)
  170. context.translateBy(x: -(w/2), y: -(h/2))
  171. let verticalWidth: CGFloat = size.width + self.watermarkModel.tileHorizontalSpace
  172. let horizontalHeight: CGFloat = size.height + self.watermarkModel.tileVerticalSpace
  173. let line: Int = Int((new_h - self.watermarkModel.tileVerticalSpace)/horizontalHeight + 1)
  174. let row: Int = Int((new_w - self.watermarkModel.tileHorizontalSpace) / verticalWidth + 1)
  175. let point = NSPoint(x: w/2 - size.width/2, y: h/2 - size.height/2)
  176. for i in 0 ..< (line/2+1) {
  177. for j in 0 ..< row {
  178. let area = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y + CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
  179. self.watermarkModel.image.draw(in: area, from: NSZeroRect, operation: .overlay, fraction: self.watermarkModel.opacity)
  180. }
  181. }
  182. for i in 1 ..< (line/2+1) {
  183. for j in 0 ..< row {
  184. let area = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y - CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
  185. self.watermarkModel.image.draw(in: area, from: NSZeroRect, operation: .overlay, fraction: self.watermarkModel.opacity)
  186. }
  187. }
  188. for i in 0 ..< (line/2+1) {
  189. for j in 1 ..< row {
  190. let area = CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y + CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
  191. self.watermarkModel.image.draw(in: area, from: NSZeroRect, operation: .overlay, fraction: self.watermarkModel.opacity)
  192. }
  193. }
  194. for i in 1 ..< (line/2+1) {
  195. for j in 1 ..< row {
  196. let area = CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y - CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
  197. self.watermarkModel.image.draw(in: area, from: NSZeroRect, operation: .overlay, fraction: self.watermarkModel.opacity)
  198. }
  199. }
  200. } else {
  201. if (self.watermarkModel.verticalMode == 0) {
  202. rect.origin.y = pageBounds.size.height-rect.size.height
  203. } else if (self.watermarkModel.verticalMode == 1) {
  204. rect.origin.y = (pageBounds.size.height-rect.size.height) * 0.5
  205. } else {
  206. rect.origin.y = 0
  207. }
  208. if (self.watermarkModel.horizontalMode == 0) {
  209. rect.origin.x = 0
  210. } else if (self.watermarkModel.horizontalMode == 1) {
  211. rect.origin.x = (pageBounds.size.width-rect.size.width) * 0.5
  212. } else {
  213. rect.origin.x = pageBounds.size.width-rect.size.width
  214. }
  215. rect.origin.x += self.watermarkModel.horizontalSpace
  216. rect.origin.y += self.watermarkModel.verticalSpace
  217. let contextCenter = CGPoint(x: rect.midX, y: rect.midY)
  218. context.translateBy(x: contextCenter.x, y: contextCenter.y)
  219. context.rotate(by: radian)
  220. context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
  221. self.watermarkModel.image.draw(in: rect, from: NSZeroRect, operation: .overlay, fraction: self.watermarkModel.opacity)
  222. }
  223. }
  224. NSGraphicsContext.restoreGraphicsState()
  225. }
  226. }