KMWatermarkController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 = KMPDFWatermarkData.defaultData()
  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 removeWatermark(_ watermark: CPDFWatermark?) {
  79. if let tempData = watermark {
  80. let document = documentPreview.pdfDocument
  81. document?.removeWatermark(tempData)
  82. }
  83. }
  84. func refreshPDFDocumentWaterMark() {
  85. guard let model = currentWatermarkData else {
  86. documentPreview.refreshUI()
  87. return
  88. }
  89. guard let pdfWatermark = model.cPDFWatermark else {
  90. return
  91. }
  92. let document = documentPreview.pdfDocument
  93. if (model.watermarkType == .text) {
  94. pdfWatermark.text = model.text
  95. pdfWatermark.textColor = model.textColor
  96. pdfWatermark.cFont = CPDFFont(familyName: model.fontName, fontStyle: model.fontStyle ?? "Regular")
  97. if model.isScale {
  98. pdfWatermark.setAutoSizeWithPercentage(model.scale)
  99. } else {
  100. pdfWatermark.fontSize = model.fontSize
  101. }
  102. } else if model.watermarkType == .image {
  103. pdfWatermark.image = model.image()
  104. if model.isScale {
  105. pdfWatermark.scale = model.scale
  106. } else {
  107. pdfWatermark.scale = 1
  108. }
  109. }
  110. pdfWatermark.opacity = model.opacity
  111. pdfWatermark.rotation = model.rotation
  112. pdfWatermark.tx = model.tx
  113. pdfWatermark.ty = model.ty
  114. pdfWatermark.isFront = model.isFront
  115. pdfWatermark.horizontalPosition = model.horizontalPosition
  116. pdfWatermark.verticalPosition = model.verticalPosition
  117. pdfWatermark.isTilePage = model.isTilePage
  118. pdfWatermark.horizontalSpacing = model.horizontalSpacing
  119. pdfWatermark.verticalSpacing = model.verticalSpacing
  120. if let watermarks = document?.watermarks(){
  121. document?.updateWatermark(pdfWatermark)
  122. } else {
  123. document?.addWatermark(pdfWatermark)
  124. }
  125. documentPreview.refreshUI()
  126. }
  127. }
  128. //MARK: - KMNWatermarkPropertyControllerDelegate
  129. extension KMWatermarkController: KMNWatermarkPropertyControllerDelegate {
  130. func watermarkPropertyControllerDidRemoveWatermark(_ controller: KMNWatermarkPropertyController, _ type: CPDFWatermarkType) {
  131. removeWatermark(controller.watermarkData.cPDFWatermark)
  132. if type == .text {
  133. controller.watermarkData.cPDFWatermark = nil
  134. controller.watermarkData.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .text)
  135. } else if type == .image {
  136. controller.watermarkData.cPDFWatermark = nil
  137. controller.watermarkData.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .image)
  138. }
  139. }
  140. func watermarkPropertyControllerDidUpdate(_ controller: KMNWatermarkPropertyController) {
  141. removeWatermark(currentWatermarkData?.cPDFWatermark)
  142. currentWatermarkData = controller.watermarkData
  143. refreshPDFDocumentWaterMark()
  144. delegate?.kmWatermarkControllerDidWatermarkUpdated?(self)
  145. }
  146. func watermarkPropertyControllerDidChangetoTemplate(_ controller: KMNWatermarkPropertyController) {
  147. editSubType = .template
  148. delegate?.kmWatermarkControllerDidUpdateMode?(self)
  149. resetUI()
  150. removeWatermark(currentWatermarkData?.cPDFWatermark)
  151. currentWatermarkData = nil
  152. delegate?.kmWatermarkControllerDidWatermarkUpdated?(self)
  153. documentPreview.layoutDocumentView()
  154. }
  155. func watermarkPropertyControllerSaveTemplateSuccess(_ controller: KMNWatermarkPropertyController, _ data: KMPDFWatermarkData) {
  156. saveTemplateMessage.frame = CGRectMake((CGRectGetWidth(self.view.frame) - saveTemplateMessage.properties.propertyInfo.viewWidth)/2,
  157. CGRectGetHeight(self.view.frame) - saveTemplateMessage.properties.propertyInfo.viewHeight - 8,
  158. saveTemplateMessage.properties.propertyInfo.viewWidth,
  159. saveTemplateMessage.properties.propertyInfo.viewHeight)
  160. saveTemplateMessage.reloadData()
  161. saveTemplateMessage.show(inView: self.view, autoHideSeconde: 2)
  162. editSubType = .template
  163. delegate?.kmWatermarkControllerDidUpdateMode?(self)
  164. resetUI()
  165. templatesController.selectedWatermark = data
  166. templatesController.reloadData()
  167. removeWatermark(currentWatermarkData?.cPDFWatermark)
  168. currentWatermarkData = templatesController.selectedWatermark
  169. refreshPDFDocumentWaterMark()
  170. }
  171. func watermarkPropertyControllerCancelTemplateEdit(_ controller: KMNWatermarkPropertyController) {
  172. removeWatermark(currentWatermarkData?.cPDFWatermark)
  173. currentWatermarkData = nil
  174. refreshPDFDocumentWaterMark()
  175. editSubType = .template
  176. delegate?.kmWatermarkControllerDidUpdateMode?(self)
  177. resetUI()
  178. templatesController.reloadData()
  179. }
  180. }
  181. //MARK: - KMNWatermarkTemplateControllerDelegate
  182. extension KMWatermarkController: KMNWatermarkTemplateControllerDelegate {
  183. func templateControllerDidAddWatermarkData(_ controller: KMNWatermarkTemplateController) {
  184. editSubType = .add
  185. resetUI()
  186. delegate?.kmWatermarkControllerDidUpdateMode?(self)
  187. propertyController.watermarkData = KMPDFWatermarkData.defaultData()
  188. propertyController.watermarkData.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .text)
  189. propertyController.reloadData()
  190. currentWatermarkData = propertyController.watermarkData
  191. refreshPDFDocumentWaterMark()
  192. delegate?.kmWatermarkControllerDidWatermarkUpdated?(self)
  193. }
  194. func templateControllerDidEditWatermarkData(_ controller: KMNWatermarkTemplateController, _ watermark: KMPDFWatermarkData) {
  195. removeWatermark(currentWatermarkData?.cPDFWatermark)
  196. editSubType = .edit
  197. delegate?.kmWatermarkControllerDidUpdateMode?(self)
  198. resetUI()
  199. propertyController.watermarkData = watermark
  200. propertyController.originalDataDict = KMWatermarkManager.defaultManager.parseWaterMark(model: watermark)
  201. propertyController.reloadData()
  202. currentWatermarkData = propertyController.watermarkData
  203. refreshPDFDocumentWaterMark()
  204. templatesController.selectedWatermark = nil
  205. delegate?.kmWatermarkControllerDidWatermarkUpdated?(self)
  206. }
  207. func templateControllerDidSelectedChanged(_ controller: KMNWatermarkTemplateController) {
  208. removeWatermark(currentWatermarkData?.cPDFWatermark)
  209. if let selectedWatermark = controller.selectedWatermark {
  210. if selectedWatermark.cPDFWatermark == nil {
  211. if selectedWatermark.watermarkType == .text {
  212. selectedWatermark.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .text)
  213. } else if controller.selectedWatermark?.watermarkType == .image {
  214. selectedWatermark.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .image)
  215. }
  216. }
  217. }
  218. currentWatermarkData = controller.selectedWatermark
  219. refreshPDFDocumentWaterMark()
  220. delegate?.kmWatermarkControllerDidWatermarkUpdated?(self)
  221. }
  222. }