KMWatermarkModel.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. //
  2. // KMWatermarkModel.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/12/19.
  6. //
  7. import Cocoa
  8. @objcMembers class KMWatermarkModel: KMWatermarkAdjectiveBaseModel {
  9. var text: String = ""
  10. var image: NSImage!
  11. var imagePath: String!
  12. var textAligement: NSTextAlignment = .left
  13. var rotation: CGFloat = 0.0
  14. var opacity: CGFloat = 0.0
  15. var scale: CGFloat = 0.0
  16. /**
  17. (Top:0 Middle:1 Bottom:2)
  18. */
  19. var verticalMode: Int = 1
  20. var verticalSpace: CGFloat = 0.0
  21. /**
  22. (Left:0 Middle:1 Right:2)
  23. */
  24. var horizontalMode: Int = 1
  25. var horizontalSpace: CGFloat = 0.0
  26. var tileHorizontalSpace: CGFloat = 60
  27. var tileVerticalSpace: CGFloat = 60
  28. var pagesString: String!
  29. var isFront: Bool = true
  30. var isTilePage: Bool = false
  31. var tag: String!
  32. var creatTemplateDate: String!
  33. var watermark: CPDFWatermark!
  34. var watermarkID: String!
  35. override func copy() -> Any {
  36. let model = KMWatermarkModel()
  37. model.text = self.text
  38. model.image = self.image
  39. model.imagePath = self.imagePath
  40. model.textColor = self.textColor
  41. model.textFont = self.textFont
  42. model.rotation = self.rotation
  43. model.opacity = self.opacity
  44. model.scale = self.scale
  45. model.verticalMode = self.verticalMode
  46. model.verticalSpace = self.verticalSpace
  47. model.horizontalMode = self.horizontalMode
  48. model.horizontalSpace = self.horizontalSpace
  49. model.tileHorizontalSpace = self.tileHorizontalSpace
  50. model.tileVerticalSpace = self.tileVerticalSpace
  51. model.pagesString = self.pagesString
  52. model.isFront = self.isFront
  53. model.isTilePage = self.isTilePage
  54. model.tag = self.tag
  55. model.pageRangeType = self.pageRangeType
  56. model.creatTemplateDate = self.creatTemplateDate
  57. model.watermark = self.watermark
  58. model.watermarkID = self.watermarkID
  59. return model
  60. }
  61. func updateData(document: CPDFDocument) {
  62. if (self.watermark == nil) {
  63. var scale: CGFloat = self.scale
  64. if (!self.text.isEmpty) {
  65. self.watermark = CPDFWatermark(document: document, type: .text)
  66. self.watermark.text = self.text
  67. self.watermark.textColor = self.getTextColor()
  68. scale = self.getTextFont().pointSize / 24.0
  69. } else {
  70. self.watermark = CPDFWatermark(document: document, type: .image)
  71. self.watermark.image = self.image
  72. }
  73. } else {
  74. if (!self.text.isEmpty) {
  75. self.watermark.text = self.text
  76. self.watermark.textColor = self.getTextColor()
  77. scale = self.getTextFont().pointSize / 24.0
  78. } else {
  79. self.watermark.image = self.image
  80. }
  81. }
  82. self.watermark.scale = scale
  83. self.watermark.rotation = -self.rotation
  84. self.watermark.opacity = self.opacity
  85. self.watermark.tx = self.horizontalSpace
  86. self.watermark.ty = self.verticalSpace
  87. self.watermark.isFront = self.isFront
  88. self.watermark.pageString = self.pagesString
  89. self.watermark.isTilePage = self.isTilePage
  90. self.watermark.horizontalSpacing = self.tileHorizontalSpace / scale
  91. self.watermark.verticalSpacing = self.tileVerticalSpace / scale
  92. if (self.verticalMode == 0) {
  93. self.watermark.verticalPosition = .top
  94. } else if (self.verticalMode == 1) {
  95. self.watermark.verticalPosition = .center
  96. } else if (self.verticalMode == 2) {
  97. self.watermark.verticalPosition = .bottom
  98. }
  99. if (self.horizontalMode == 0) {
  100. self.watermark.horizontalPosition = .left
  101. } else if (self.horizontalMode == 1) {
  102. self.watermark.horizontalPosition = .center
  103. } else if (self.horizontalMode == 2) {
  104. self.watermark.horizontalPosition = .right
  105. }
  106. }
  107. func drawImage(rect: CGRect) -> NSImage {
  108. var size: NSSize = NSZeroSize
  109. var drawRect: CGRect = NSMakeRect(0, 0, 128, 160)
  110. if (!self.text.isEmpty) {
  111. var font = NSFont.systemFont(ofSize: 48)
  112. if (self.isTilePage) {
  113. font = self.getTextFont()
  114. }
  115. var style = NSMutableParagraphStyle()
  116. style.alignment = self.textAligement
  117. style.lineBreakMode = .byCharWrapping
  118. let dictionary = [NSAttributedString.Key.paragraphStyle : style, NSAttributedString.Key.font : font]
  119. size = self.text.boundingRect(with: NSSize(width: 1000, height: 1000), options: NSString.DrawingOptions(rawValue: 3), attributes: dictionary).size
  120. } else if (self.image != nil) {
  121. size = self.image.size
  122. size.width = size.width * self.scale
  123. size.height = size.height * self.scale
  124. }
  125. let radian = self.rotation * (Double.pi/180.0)
  126. let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
  127. if (!self.isTilePage) {
  128. drawRect = NSMakeRect(0, 0, size.width+40, size.height+40).applying(t);
  129. }
  130. let image = NSImage(size: drawRect.size)
  131. image.lockFocus()
  132. NSGraphicsContext.current?.imageInterpolation = .high
  133. NSGraphicsContext.saveGraphicsState()
  134. NSColor.clear.set()
  135. drawRect.fill()
  136. NSGraphicsContext.restoreGraphicsState()
  137. let context: CGContext = NSGraphicsContext.current!.cgContext
  138. let imageSize: NSSize = NSMakeSize(drawRect.size.width, drawRect.size.height)
  139. if (context != nil) {
  140. NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
  141. }
  142. NSGraphicsContext.saveGraphicsState()
  143. if (!self.text.isEmpty) {
  144. var font = NSFont.systemFont(ofSize: 48)
  145. if (self.isTilePage) {
  146. font = self.getTextFont()
  147. }
  148. var color = NSColor.black
  149. if (self.textColor != nil) {
  150. color = self.getTextColor()
  151. }
  152. var red: CGFloat = 0
  153. var green: CGFloat = 0
  154. var blue: CGFloat = 0
  155. color.usingColorSpaceName(NSColorSpaceName.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: nil)
  156. color = NSColor(calibratedRed: red, green: green, blue: blue, alpha: 1.0)
  157. var size = NSZeroSize
  158. let style = NSMutableParagraphStyle()
  159. style.alignment = self.textAligement
  160. style.lineBreakMode = .byCharWrapping
  161. let dictionary = [NSAttributedString.Key.paragraphStyle : style, NSAttributedString.Key.foregroundColor : color, NSAttributedString.Key.font : font]
  162. size = self.text.boundingRect(with: NSSize(width: 1000, height: 1000), options: NSString.DrawingOptions(rawValue: 3), attributes: dictionary).size
  163. let radian: CGFloat = self.rotation * (Double.pi / 180.0)
  164. let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
  165. var textRect = NSMakeRect(0, 0, size.width, size.height).applying(t)
  166. if (self.isTilePage) {
  167. let width = sqrt(image.size.height * image.size.height + image.size.width * image.size.width)
  168. let newRect = NSMakeRect(-(width - image.size.width)/2, -(width - image.size.height)/2, width, width)
  169. let new_w = NSWidth(newRect)
  170. let new_h = NSHeight(newRect)
  171. context.translateBy(x: image.size.width/2,y: image.size.height/2);
  172. context.concatenate(t);
  173. context.translateBy(x: -(image.size.width/2), y: -(image.size.height/2));
  174. let verticalWidth: CGFloat = size.width + self.tileHorizontalSpace/3.0
  175. let horizontalHeight: CGFloat = size.height + self.tileVerticalSpace/3.0
  176. let line: Int = Int(((new_h - self.tileHorizontalSpace/3.0)/horizontalHeight) + 1)
  177. let row: Int = Int((new_w - self.tileVerticalSpace/3.0) / verticalWidth + 1)
  178. let point = NSPoint(x: image.size.width/2 - size.width/2+self.horizontalSpace/3.0, y: image.size.height/2 - size.height/2+self.verticalSpace/3.0)
  179. for i in 0 ..< line {
  180. for j in 0 ..< row {
  181. self.text.draw(in: NSRect(x: point.x+CGFloat(j)*verticalWidth, y: point.y+CGFloat(i)*horizontalHeight, width: size.width, height: size.height), withAttributes: dictionary)
  182. }
  183. }
  184. for i in 1 ..< line {
  185. for j in 0 ..< row {
  186. self.text.draw(in: NSRect(x: point.x+CGFloat(j)*verticalWidth, y: point.y-CGFloat(i)*horizontalHeight, width: size.width, height: size.height), withAttributes: dictionary)
  187. }
  188. }
  189. for i in 0 ..< line {
  190. for j in 1 ..< row {
  191. self.text.draw(in: NSRect(x: point.x-CGFloat(j)*verticalWidth, y: point.y+CGFloat(i)*horizontalHeight, width: size.width, height: size.height), withAttributes: dictionary)
  192. }
  193. }
  194. for i in 1 ..< line {
  195. for j in 1 ..< row {
  196. self.text.draw(in: NSRect(x: point.x-CGFloat(j)*verticalWidth, y: point.y-CGFloat(i)*horizontalHeight, width: size.width, height: size.height), withAttributes: dictionary)
  197. }
  198. }
  199. } else {
  200. if (self.verticalMode == 0) {
  201. textRect.origin.y = imageSize.height-textRect.size.height
  202. } else if (self.verticalMode == 1) {
  203. textRect.origin.y = (imageSize.height-textRect.size.height) * 0.5
  204. } else {
  205. textRect.origin.y = 0
  206. }
  207. if (self.horizontalMode == 0) {
  208. textRect.origin.x = 0
  209. } else if (self.horizontalMode == 1) {
  210. textRect.origin.x = (imageSize.width-textRect.size.width) * 0.5
  211. } else {
  212. textRect.origin.x = imageSize.width-textRect.size.width
  213. }
  214. textRect.origin.x += self.horizontalSpace
  215. textRect.origin.y += self.verticalSpace
  216. let contextCenter = CGPoint(x: textRect.midX, y: textRect.midY)
  217. context.translateBy(x: contextCenter.x, y: contextCenter.y)
  218. context.rotate(by: radian)
  219. context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
  220. let testRect = NSMakeRect(textRect.origin.x+(textRect.size.width-size.width)/2.0,
  221. textRect.origin.y+(textRect.size.height-size.height)/2.0,
  222. size.width, size.height)
  223. self.text.draw(in: NSMakeRect(textRect.origin.x+(textRect.size.width-size.width)/2.0,
  224. textRect.origin.y+(textRect.size.height-size.height)/2.0,
  225. size.width, size.height), withAttributes: dictionary)
  226. }
  227. } else if (self.image != nil) {
  228. var size = self.image.size
  229. size.width *= self.scale
  230. size.height *= self.scale
  231. let width: CGFloat = sqrt(image.size.height * image.size.height + image.size.width * image.size.width)
  232. let newRect = NSMakeRect(-(width - image.size.width)/2, -(width - image.size.height)/2, width, width)
  233. let new_w = newRect.size.width
  234. let new_h = newRect.size.height
  235. let radian: CGFloat = self.rotation * (Double.pi / 180.0)
  236. let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
  237. if (self.isTilePage) {
  238. context.translateBy(x: image.size.width/2,y: image.size.height/2)
  239. context.concatenate(t)
  240. context.translateBy(x: -(image.size.width/2), y: -(image.size.height/2))
  241. let verticalWidth: CGFloat = size.width + self.tileHorizontalSpace * self.scale
  242. let horizontalHeight: CGFloat = size.height + self.tileVerticalSpace * self.scale
  243. let line: Int = Int((new_h - self.tileVerticalSpace * self.scale) / horizontalHeight + 1)
  244. let row: Int = Int((new_w - self.tileHorizontalSpace * self.scale) / verticalWidth + 1)
  245. let point = NSPoint(x: image.size.width/2 - size.width/2+self.horizontalSpace*self.scale, y: image.size.height/2 - size.height/2+self.verticalSpace * self.scale)
  246. for i in 0 ..< (line/2+1) {
  247. for j in 0 ..< row {
  248. let area = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y + CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
  249. self.image.draw(in: area, from: NSZeroRect, operation: .overlay, fraction: self.opacity)
  250. }
  251. }
  252. for i in 1 ..< (line/2+1) {
  253. for j in 0 ..< row {
  254. let area = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y - CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
  255. self.image.draw(in: area, from: NSZeroRect, operation: .overlay, fraction: self.opacity)
  256. }
  257. }
  258. for i in 0 ..< (line/2+1) {
  259. for j in 1 ..< row {
  260. let area = CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y + CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
  261. self.image.draw(in: area, from: NSZeroRect, operation: .overlay, fraction: self.opacity)
  262. }
  263. }
  264. for i in 1 ..< (line/2+1) {
  265. for j in 1 ..< row {
  266. let area = CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y - CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
  267. self.image.draw(in: area, from: NSZeroRect, operation: .overlay, fraction: self.opacity)
  268. }
  269. }
  270. } else {
  271. var size = self.image.size
  272. size.width *= self.scale
  273. size.height *= self.scale
  274. let radian: CGFloat = self.rotation * (Double.pi / 180.0)
  275. let t: CGAffineTransform = CGAffineTransform(rotationAngle: radian)
  276. var imageRect = NSMakeRect(0, 0, size.width, size.height).applying(t)
  277. if (self.verticalMode == 0) {
  278. imageRect.origin.y = imageSize.height-imageRect.size.height
  279. } else if (self.verticalMode == 1) {
  280. imageRect.origin.y = (imageSize.height-imageRect.size.height) * 0.5
  281. } else {
  282. imageRect.origin.y = 0
  283. }
  284. if (self.horizontalMode == 0) {
  285. imageRect.origin.x = 0
  286. } else if (self.horizontalMode == 1) {
  287. imageRect.origin.x = (imageSize.width-imageRect.size.width) * 0.5
  288. } else {
  289. imageRect.origin.x = imageSize.width-imageRect.size.width
  290. }
  291. let contextCenter = CGPoint(x: rect.midX, y: rect.midY)
  292. context.translateBy(x: contextCenter.x, y: contextCenter.y)
  293. context.rotate(by: radian)
  294. context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
  295. let finalRect = NSMakeRect(imageRect.origin.x+(imageRect.size.width-size.width)/2.0,
  296. imageRect.origin.y+(imageRect.size.height-size.height)/2.0,
  297. size.width, size.height)
  298. self.image.draw(in: finalRect, from: NSZeroRect, operation: .overlay, fraction: self.opacity)
  299. }
  300. }
  301. NSGraphicsContext.restoreGraphicsState()
  302. NSGraphicsContext.current?.imageInterpolation = .default
  303. image.unlockFocus()
  304. return image
  305. }
  306. }