KMWatermarkPDFView.swift 12 KB

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