KMWatermarkController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // KMWatermarkController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/1.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMWatermarkControllerDelegate: AnyObject {
  10. @objc optional func kmWatermarkControllerDidUpdateMode(_ view: KMWatermarkController)
  11. //水印数据发生变化时处理
  12. @objc optional func kmWatermarkControllerDidWatermarkUpdated(_ view: KMWatermarkController)
  13. }
  14. class KMWatermarkController: NSViewController {
  15. @IBOutlet var contendView: NSView!
  16. @IBOutlet var preInfoView: NSView!
  17. @IBOutlet var propertyInfoView: NSView!
  18. var documentPreview: KMEditDocumentController = KMEditDocumentController.init()
  19. var propertyController: KMNWatermarkPropertyController = KMNWatermarkPropertyController.init()
  20. var templatesController: KMNWatermarkTemplateController = KMNWatermarkTemplateController()
  21. var saveTemplateMessage: ComponentMessage = ComponentMessage()
  22. var pdfDocument: CPDFDocument?
  23. var currentWatermarkData: KMPDFWatermarkData?
  24. weak open var delegate: KMWatermarkControllerDelegate?
  25. var editSubType: KMPDFEditSubModeType = .none
  26. //MARK: - func
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. // Do view setup here.
  30. documentPreview.view.frame = preInfoView.bounds
  31. documentPreview.view.autoresizingMask = [.width, .height]
  32. preInfoView.addSubview(documentPreview.view)
  33. propertyController.view.frame = propertyInfoView.bounds
  34. propertyController.view.autoresizingMask = [.width, .height]
  35. propertyController.delegate = self
  36. propertyController.view.isHidden = true
  37. propertyInfoView.addSubview(propertyController.view)
  38. templatesController.view.frame = propertyInfoView.bounds
  39. templatesController.view.autoresizingMask = [.width, .height]
  40. templatesController.delegate = self
  41. templatesController.view.isHidden = true
  42. propertyInfoView.addSubview(templatesController.view)
  43. setupProperty()
  44. }
  45. func setupProperty() {
  46. saveTemplateMessage.properties = ComponentMessageProperty(messageType: .success, title: KMLocalizedString("Saved Template"))
  47. }
  48. func resetUI() {
  49. propertyController.view.isHidden = true
  50. templatesController.view.isHidden = true
  51. if editSubType == .template {
  52. templatesController.view.isHidden = false
  53. } else if editSubType == .add {
  54. propertyController.view.isHidden = false
  55. propertyController.editSubType = editSubType
  56. } else if editSubType == .edit {
  57. propertyController.view.isHidden = false
  58. propertyController.editSubType = editSubType
  59. }
  60. }
  61. func reloadData() {
  62. documentPreview.pdfDocument = self.pdfDocument
  63. documentPreview.reloadData()
  64. if editSubType == .template {
  65. templatesController.reloadData()
  66. } else if editSubType == .add {
  67. propertyController.editSubType = editSubType
  68. propertyController.watermarkData = KMWatermarkManager.defaultManager.defaultWatermarkData
  69. propertyController.watermarkData.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .text)
  70. propertyController.reloadData()
  71. currentWatermarkData = propertyController.watermarkData
  72. } else if editSubType == .edit {
  73. propertyController.editSubType = editSubType
  74. propertyController.reloadData()
  75. }
  76. refreshPDFDocumentWaterMark()
  77. }
  78. func clearData() {
  79. removeWatermark(currentWatermarkData?.cPDFWatermark)
  80. currentWatermarkData = nil
  81. refreshPDFDocumentWaterMark()
  82. }
  83. func removeWatermark(_ watermark: CPDFWatermark?) {
  84. if let tempData = watermark {
  85. let document = documentPreview.pdfDocument
  86. document?.removeWatermark(tempData)
  87. }
  88. }
  89. func refreshPDFDocumentWaterMark() {
  90. guard let model = currentWatermarkData else {
  91. documentPreview.refreshUI()
  92. return
  93. }
  94. guard let pdfWatermark = model.cPDFWatermark else {
  95. return
  96. }
  97. let document = documentPreview.pdfDocument
  98. if (model.watermarkType == .text) {
  99. pdfWatermark.text = model.text ?? ""
  100. pdfWatermark.textColor = model.textColor
  101. pdfWatermark.cFont = CPDFFont(familyName: model.fontName, fontStyle: model.fontStyle ?? "Regular")
  102. if model.isScale {
  103. pdfWatermark.setAutoSizeWithPercentage(model.scale)
  104. } else {
  105. pdfWatermark.fontSize = model.fontSize
  106. }
  107. } else if model.watermarkType == .image, let image = model.image() {
  108. pdfWatermark.image = image
  109. if model.isScale {
  110. pdfWatermark.scale = model.scale
  111. } else {
  112. pdfWatermark.scale = 1
  113. }
  114. }
  115. pdfWatermark.opacity = model.opacity
  116. pdfWatermark.rotation = model.rotation
  117. pdfWatermark.tx = model.tx
  118. pdfWatermark.ty = model.ty
  119. pdfWatermark.isFront = model.isFront
  120. pdfWatermark.horizontalPosition = model.horizontalPosition
  121. pdfWatermark.verticalPosition = model.verticalPosition
  122. pdfWatermark.isTilePage = model.isTilePage
  123. pdfWatermark.horizontalSpacing = model.horizontalSpacing
  124. pdfWatermark.verticalSpacing = model.verticalSpacing
  125. if let watermarks = document?.watermarks(){
  126. document?.updateWatermark(pdfWatermark)
  127. } else {
  128. document?.addWatermark(pdfWatermark)
  129. }
  130. documentPreview.refreshUI()
  131. }
  132. }
  133. //MARK: - KMNWatermarkPropertyControllerDelegate
  134. extension KMWatermarkController: KMNWatermarkPropertyControllerDelegate {
  135. func watermarkPropertyControllerDidRemoveWatermark(_ controller: KMNWatermarkPropertyController, _ type: CPDFWatermarkType) {
  136. removeWatermark(controller.watermarkData.cPDFWatermark)
  137. if type == .text {
  138. controller.watermarkData.cPDFWatermark = nil
  139. controller.watermarkData.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .text)
  140. } else if type == .image {
  141. controller.watermarkData.cPDFWatermark = nil
  142. controller.watermarkData.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .image)
  143. }
  144. }
  145. func watermarkPropertyControllerDidUpdate(_ controller: KMNWatermarkPropertyController) {
  146. removeWatermark(currentWatermarkData?.cPDFWatermark)
  147. currentWatermarkData = controller.watermarkData
  148. refreshPDFDocumentWaterMark()
  149. delegate?.kmWatermarkControllerDidWatermarkUpdated?(self)
  150. }
  151. func watermarkPropertyControllerDidChangetoTemplate(_ controller: KMNWatermarkPropertyController) {
  152. editSubType = .template
  153. delegate?.kmWatermarkControllerDidUpdateMode?(self)
  154. resetUI()
  155. removeWatermark(currentWatermarkData?.cPDFWatermark)
  156. currentWatermarkData = nil
  157. delegate?.kmWatermarkControllerDidWatermarkUpdated?(self)
  158. documentPreview.layoutDocumentView()
  159. }
  160. func watermarkPropertyControllerSaveTemplateSuccess(_ controller: KMNWatermarkPropertyController, _ data: KMPDFWatermarkData) {
  161. saveTemplateMessage.frame = CGRectMake((CGRectGetWidth(self.view.frame) - saveTemplateMessage.properties.propertyInfo.viewWidth)/2,
  162. CGRectGetHeight(self.view.frame) - saveTemplateMessage.properties.propertyInfo.viewHeight - 8,
  163. saveTemplateMessage.properties.propertyInfo.viewWidth,
  164. saveTemplateMessage.properties.propertyInfo.viewHeight)
  165. saveTemplateMessage.reloadData()
  166. saveTemplateMessage.show(inView: self.view, autoHideSeconde: 2)
  167. editSubType = .template
  168. delegate?.kmWatermarkControllerDidUpdateMode?(self)
  169. resetUI()
  170. templatesController.selectedWatermark = data
  171. templatesController.reloadData()
  172. removeWatermark(currentWatermarkData?.cPDFWatermark)
  173. currentWatermarkData = templatesController.selectedWatermark
  174. refreshPDFDocumentWaterMark()
  175. }
  176. func watermarkPropertyControllerCancelTemplateEdit(_ controller: KMNWatermarkPropertyController) {
  177. removeWatermark(currentWatermarkData?.cPDFWatermark)
  178. currentWatermarkData = nil
  179. refreshPDFDocumentWaterMark()
  180. editSubType = .template
  181. delegate?.kmWatermarkControllerDidUpdateMode?(self)
  182. resetUI()
  183. templatesController.reloadData()
  184. }
  185. }
  186. //MARK: - KMNWatermarkTemplateControllerDelegate
  187. extension KMWatermarkController: KMNWatermarkTemplateControllerDelegate {
  188. func templateControllerDidAddWatermarkData(_ controller: KMNWatermarkTemplateController) {
  189. editSubType = .add
  190. resetUI()
  191. delegate?.kmWatermarkControllerDidUpdateMode?(self)
  192. propertyController.watermarkData = KMWatermarkManager.defaultManager.defaultWatermarkData
  193. if let value = propertyController.watermarkData.imagePath {
  194. propertyController.watermarkData.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .image)
  195. } else {
  196. propertyController.watermarkData.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .text)
  197. }
  198. propertyController.reloadData()
  199. currentWatermarkData = propertyController.watermarkData
  200. refreshPDFDocumentWaterMark()
  201. delegate?.kmWatermarkControllerDidWatermarkUpdated?(self)
  202. }
  203. func templateControllerDidEditWatermarkData(_ controller: KMNWatermarkTemplateController, _ watermark: KMPDFWatermarkData) {
  204. removeWatermark(currentWatermarkData?.cPDFWatermark)
  205. editSubType = .edit
  206. delegate?.kmWatermarkControllerDidUpdateMode?(self)
  207. resetUI()
  208. propertyController.watermarkData = watermark
  209. propertyController.originalDataDict = KMWatermarkManager.defaultManager.parseWaterMark(model: watermark)
  210. propertyController.reloadData()
  211. currentWatermarkData = propertyController.watermarkData
  212. refreshPDFDocumentWaterMark()
  213. templatesController.selectedWatermark = nil
  214. delegate?.kmWatermarkControllerDidWatermarkUpdated?(self)
  215. }
  216. func templateControllerDidSelectedChanged(_ controller: KMNWatermarkTemplateController) {
  217. removeWatermark(currentWatermarkData?.cPDFWatermark)
  218. if let selectedWatermark = controller.selectedWatermark {
  219. if selectedWatermark.cPDFWatermark == nil {
  220. if selectedWatermark.watermarkType == .text {
  221. selectedWatermark.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .text)
  222. } else if controller.selectedWatermark?.watermarkType == .image {
  223. selectedWatermark.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .image)
  224. }
  225. }
  226. }
  227. currentWatermarkData = controller.selectedWatermark
  228. refreshPDFDocumentWaterMark()
  229. delegate?.kmWatermarkControllerDidWatermarkUpdated?(self)
  230. }
  231. }