KMWatermarkPDFView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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.watermarkModel.pagesString.count > 0) {
  76. // let array = self.watermarkModel.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.textFont
  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.watermarkModel.textColor
  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 = [NSAttributedString.Key.paragraphStyle : style,
  101. NSAttributedString.Key.foregroundColor : color,
  102. NSAttributedString.Key.font : font] as [NSAttributedString.Key : Any]
  103. size = self.watermark.text.boundingRect(with: NSSize(width: 1000, height: 1000), options: NSString.DrawingOptions(rawValue: 3), attributes: dict).size
  104. let radian: CGFloat = self.watermark.rotation*(Double.pi/180.0)
  105. let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
  106. var rect:CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
  107. if (self.watermark.isTilePage) {
  108. context.translateBy(x: w * 0.5, y: h * 0.5)
  109. context.concatenate(t)
  110. context.translateBy(x: -(w/2), y: -(h/2))
  111. let verticalWidth: CGFloat = size.width + self.watermark.tileHorizontalSpace
  112. let horizontalHeight: CGFloat = size.height + self.watermark.tileVerticalSpace
  113. let line: Int = Int(((new_h-self.watermark.tileVerticalSpace)/horizontalHeight)+1)
  114. let row: Int = Int(((new_w-self.watermark.tileHorizontalSpace)/verticalWidth)+1)
  115. 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)
  116. for i in 0 ..< line {
  117. for j in 0 ..< row {
  118. 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)
  119. }
  120. }
  121. for i in 1 ..< line {
  122. for j in 0 ..< row {
  123. 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)
  124. }
  125. }
  126. for i in 0 ..< line {
  127. for j in 1 ..< row {
  128. 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)
  129. }
  130. }
  131. for i in 1 ..< line {
  132. for j in 1 ..< row {
  133. 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)
  134. }
  135. }
  136. } else {
  137. if (self.watermark.verticalMode == 0) {
  138. rect.origin.y = pageBounds.size.height-rect.size.height
  139. } else if (self.watermark.verticalMode == 1) {
  140. rect.origin.y = (pageBounds.size.height-rect.size.height) * 0.5
  141. } else {
  142. rect.origin.y = 0
  143. }
  144. if (self.watermark.horizontalMode == 0) {
  145. rect.origin.x = 0
  146. } else if (self.watermark.horizontalMode == 1) {
  147. rect.origin.x = (pageBounds.size.width-rect.size.width) * 0.5
  148. } else {
  149. rect.origin.x = pageBounds.size.width-rect.size.width
  150. }
  151. rect.origin.x += self.watermark.horizontalSpace
  152. rect.origin.y += self.watermark.verticalSpace
  153. let contextCenter = CGPoint(x: rect.midX, y: rect.midY)
  154. context.translateBy(x: contextCenter.x, y: contextCenter.y)
  155. context.rotate(by: radian)
  156. context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
  157. self.watermark.text.draw(in: rect, withAttributes: dict)
  158. }
  159. } else if (self.watermark.image != nil) {
  160. let tiffData = self.watermark.image.tiffRepresentation
  161. let bitmap: NSBitmapImageRep!
  162. bitmap = NSBitmapImageRep(data: tiffData!)
  163. let ciImage = CIImage(bitmapImageRep: bitmap)
  164. var size: NSSize = (ciImage?.extent.size)!
  165. size.width *= self.watermark.scale
  166. size.height *= self.watermark.scale
  167. let radian = self.watermark.rotation * (Double.pi / 180.0)
  168. let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
  169. var rect = NSMakeRect(0, 0, size.width, size.height)
  170. if (self.watermark.isTilePage) {
  171. context.translateBy(x: w/2,y: h/2)
  172. context.concatenate(t)
  173. context.translateBy(x: -(w/2), y: -(h/2))
  174. let verticalWidth: CGFloat = size.width + self.watermark.tileHorizontalSpace
  175. let horizontalHeight: CGFloat = size.height + self.watermark.tileVerticalSpace
  176. let line: Int = Int((new_h - self.watermark.tileVerticalSpace)/horizontalHeight + 1)
  177. let row: Int = Int((new_w - self.watermark.tileHorizontalSpace) / verticalWidth + 1)
  178. let point = NSPoint(x: w/2 - size.width/2, y: h/2 - size.height/2)
  179. for i in 0 ..< (line/2+1) {
  180. for j in 0 ..< row {
  181. let area = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y + CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
  182. self.watermark.image.draw(in: area, from: NSZeroRect, operation: .overlay, fraction: self.watermark.opacity)
  183. }
  184. }
  185. for i in 1 ..< (line/2+1) {
  186. for j in 0 ..< row {
  187. let area = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y - CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
  188. self.watermark.image.draw(in: area, from: NSZeroRect, operation: .overlay, fraction: self.watermark.opacity)
  189. }
  190. }
  191. for i in 0 ..< (line/2+1) {
  192. for j in 1 ..< row {
  193. let area = CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y + CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
  194. self.watermark.image.draw(in: area, from: NSZeroRect, operation: .overlay, fraction: self.watermark.opacity)
  195. }
  196. }
  197. for i in 1 ..< (line/2+1) {
  198. for j in 1 ..< row {
  199. let area = CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y - CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
  200. self.watermark.image.draw(in: area, from: NSZeroRect, operation: .overlay, fraction: self.watermark.opacity)
  201. }
  202. }
  203. } else {
  204. if (self.watermark.verticalMode == 0) {
  205. rect.origin.y = pageBounds.size.height-rect.size.height
  206. } else if (self.watermark.verticalMode == 1) {
  207. rect.origin.y = (pageBounds.size.height-rect.size.height) * 0.5
  208. } else {
  209. rect.origin.y = 0
  210. }
  211. if (self.watermark.horizontalMode == 0) {
  212. rect.origin.x = 0
  213. } else if (self.watermark.horizontalMode == 1) {
  214. rect.origin.x = (pageBounds.size.width-rect.size.width) * 0.5
  215. } else {
  216. rect.origin.x = pageBounds.size.width-rect.size.width
  217. }
  218. rect.origin.x += self.watermark.horizontalSpace
  219. rect.origin.y += self.watermark.verticalSpace
  220. let contextCenter = CGPoint(x: rect.midX, y: rect.midY)
  221. context.translateBy(x: contextCenter.x, y: contextCenter.y)
  222. context.rotate(by: radian)
  223. context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
  224. self.watermark.image.draw(in: rect, from: NSZeroRect, operation: .overlay, fraction: self.watermark.opacity)
  225. }
  226. }
  227. NSGraphicsContext.restoreGraphicsState()
  228. }
  229. }