123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- //
- // KMWatermarkController.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/11/1.
- //
- import Cocoa
- import KMComponentLibrary
- @objc protocol KMWatermarkControllerDelegate: AnyObject {
-
- @objc optional func kmWatermarkControllerDidUpdateMode(_ view: KMWatermarkController)
-
- //水印数据发生变化时处理
- @objc optional func kmWatermarkControllerDidWatermarkUpdated(_ view: KMWatermarkController)
- }
- class KMWatermarkController: NSViewController {
-
- @IBOutlet var contendView: NSView!
-
- @IBOutlet var preInfoView: NSView!
- @IBOutlet var propertyInfoView: NSView!
-
- var documentPreview: KMEditDocumentController = KMEditDocumentController.init()
- var propertyController: KMNWatermarkPropertyController = KMNWatermarkPropertyController.init()
- var templatesController: KMNWatermarkTemplateController = KMNWatermarkTemplateController()
-
- var saveTemplateMessage: ComponentMessage = ComponentMessage()
-
- var pdfDocument: CPDFDocument?
-
- var currentWatermarkData: KMPDFWatermarkData?
-
- weak open var delegate: KMWatermarkControllerDelegate?
-
- var editSubType: KMPDFEditSubModeType = .none
-
- //MARK: - func
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
-
- documentPreview.view.frame = preInfoView.bounds
- documentPreview.view.autoresizingMask = [.width, .height]
- preInfoView.addSubview(documentPreview.view)
-
- propertyController.view.frame = propertyInfoView.bounds
- propertyController.view.autoresizingMask = [.width, .height]
- propertyController.delegate = self
- propertyController.view.isHidden = true
- propertyInfoView.addSubview(propertyController.view)
-
- templatesController.view.frame = propertyInfoView.bounds
- templatesController.view.autoresizingMask = [.width, .height]
- templatesController.delegate = self
- templatesController.view.isHidden = true
- propertyInfoView.addSubview(templatesController.view)
-
- setupProperty()
- }
-
- func setupProperty() {
- saveTemplateMessage.properties = ComponentMessageProperty(messageType: .success, title: KMLocalizedString("Saved Template"))
- }
-
- func resetUI() {
- propertyController.view.isHidden = true
- templatesController.view.isHidden = true
-
- if editSubType == .template {
- templatesController.view.isHidden = false
- } else if editSubType == .add {
- propertyController.view.isHidden = false
- propertyController.editSubType = editSubType
- } else if editSubType == .edit {
- propertyController.view.isHidden = false
- propertyController.editSubType = editSubType
- }
- }
-
- func reloadData() {
- documentPreview.pdfDocument = self.pdfDocument
- documentPreview.reloadData()
-
- if editSubType == .template {
- templatesController.reloadData()
- } else if editSubType == .add {
- propertyController.editSubType = editSubType
- propertyController.watermarkData = KMPDFWatermarkData.defaultData()
- propertyController.watermarkData.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .text)
- propertyController.reloadData()
-
- currentWatermarkData = propertyController.watermarkData
- } else if editSubType == .edit {
- propertyController.editSubType = editSubType
- propertyController.reloadData()
- }
- refreshPDFDocumentWaterMark()
- }
-
- func removeWatermark(_ watermark: CPDFWatermark?) {
- if let tempData = watermark {
- let document = documentPreview.pdfDocument
- document?.removeWatermark(tempData)
- }
- }
-
- func refreshPDFDocumentWaterMark() {
- guard let model = currentWatermarkData else {
- documentPreview.refreshUI()
- return
- }
- guard let pdfWatermark = model.cPDFWatermark else {
- return
- }
-
- let document = documentPreview.pdfDocument
-
- if (model.watermarkType == .text) {
- pdfWatermark.text = model.text
- pdfWatermark.textColor = model.textColor
- pdfWatermark.cFont = CPDFFont(familyName: model.fontName, fontStyle: model.fontStyle ?? "Regular")
- if model.isScale {
- pdfWatermark.setAutoSizeWithPercentage(model.scale)
- } else {
- pdfWatermark.fontSize = model.fontSize
- }
- } else if model.watermarkType == .image {
- pdfWatermark.image = model.image()
-
- if model.isScale {
- pdfWatermark.scale = model.scale
- } else {
- pdfWatermark.scale = 1
- }
- }
-
- pdfWatermark.opacity = model.opacity
- pdfWatermark.rotation = model.rotation
-
- pdfWatermark.tx = model.tx
- pdfWatermark.ty = model.ty
-
- pdfWatermark.isFront = model.isFront
-
- pdfWatermark.horizontalPosition = model.horizontalPosition
- pdfWatermark.verticalPosition = model.verticalPosition
-
- pdfWatermark.isTilePage = model.isTilePage
- pdfWatermark.horizontalSpacing = model.horizontalSpacing
- pdfWatermark.verticalSpacing = model.verticalSpacing
-
- if let watermarks = document?.watermarks(){
- document?.updateWatermark(pdfWatermark)
- } else {
- document?.addWatermark(pdfWatermark)
- }
- documentPreview.refreshUI()
-
-
- }
- }
- //MARK: - KMNWatermarkPropertyControllerDelegate
- extension KMWatermarkController: KMNWatermarkPropertyControllerDelegate {
- func watermarkPropertyControllerDidRemoveWatermark(_ controller: KMNWatermarkPropertyController, _ type: CPDFWatermarkType) {
- removeWatermark(controller.watermarkData.cPDFWatermark)
-
- if type == .text {
- controller.watermarkData.cPDFWatermark = nil
- controller.watermarkData.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .text)
- } else if type == .image {
- controller.watermarkData.cPDFWatermark = nil
- controller.watermarkData.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .image)
- }
- }
-
- func watermarkPropertyControllerDidUpdate(_ controller: KMNWatermarkPropertyController) {
-
- removeWatermark(currentWatermarkData?.cPDFWatermark)
-
- currentWatermarkData = controller.watermarkData
-
- refreshPDFDocumentWaterMark()
-
- delegate?.kmWatermarkControllerDidWatermarkUpdated?(self)
-
- }
-
- func watermarkPropertyControllerDidChangetoTemplate(_ controller: KMNWatermarkPropertyController) {
- editSubType = .template
- delegate?.kmWatermarkControllerDidUpdateMode?(self)
-
- resetUI()
-
- removeWatermark(currentWatermarkData?.cPDFWatermark)
- currentWatermarkData = nil
-
- delegate?.kmWatermarkControllerDidWatermarkUpdated?(self)
-
- documentPreview.layoutDocumentView()
- }
-
- func watermarkPropertyControllerSaveTemplateSuccess(_ controller: KMNWatermarkPropertyController, _ data: KMPDFWatermarkData) {
- saveTemplateMessage.frame = CGRectMake((CGRectGetWidth(self.view.frame) - saveTemplateMessage.properties.propertyInfo.viewWidth)/2,
- CGRectGetHeight(self.view.frame) - saveTemplateMessage.properties.propertyInfo.viewHeight - 8,
- saveTemplateMessage.properties.propertyInfo.viewWidth,
- saveTemplateMessage.properties.propertyInfo.viewHeight)
- saveTemplateMessage.reloadData()
- saveTemplateMessage.show(inView: self.view, autoHideSeconde: 2)
-
- editSubType = .template
- delegate?.kmWatermarkControllerDidUpdateMode?(self)
-
- resetUI()
-
- templatesController.selectedWatermark = data
- templatesController.reloadData()
-
- removeWatermark(currentWatermarkData?.cPDFWatermark)
- currentWatermarkData = templatesController.selectedWatermark
-
- refreshPDFDocumentWaterMark()
- }
-
- func watermarkPropertyControllerCancelTemplateEdit(_ controller: KMNWatermarkPropertyController) {
- removeWatermark(currentWatermarkData?.cPDFWatermark)
-
- currentWatermarkData = nil
- refreshPDFDocumentWaterMark()
-
- editSubType = .template
- delegate?.kmWatermarkControllerDidUpdateMode?(self)
-
- resetUI()
-
- templatesController.reloadData()
-
- }
-
- }
- //MARK: - KMNWatermarkTemplateControllerDelegate
- extension KMWatermarkController: KMNWatermarkTemplateControllerDelegate {
- func templateControllerDidAddWatermarkData(_ controller: KMNWatermarkTemplateController) {
- editSubType = .add
- resetUI()
-
- delegate?.kmWatermarkControllerDidUpdateMode?(self)
-
- propertyController.watermarkData = KMPDFWatermarkData.defaultData()
- propertyController.watermarkData.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .text)
- propertyController.reloadData()
-
- currentWatermarkData = propertyController.watermarkData
- refreshPDFDocumentWaterMark()
-
- delegate?.kmWatermarkControllerDidWatermarkUpdated?(self)
-
- }
-
- func templateControllerDidEditWatermarkData(_ controller: KMNWatermarkTemplateController, _ watermark: KMPDFWatermarkData) {
- removeWatermark(currentWatermarkData?.cPDFWatermark)
-
- editSubType = .edit
- delegate?.kmWatermarkControllerDidUpdateMode?(self)
-
- resetUI()
-
- propertyController.watermarkData = watermark
- propertyController.originalDataDict = KMWatermarkManager.defaultManager.parseWaterMark(model: watermark)
- propertyController.reloadData()
-
- currentWatermarkData = propertyController.watermarkData
- refreshPDFDocumentWaterMark()
-
- templatesController.selectedWatermark = nil
-
- delegate?.kmWatermarkControllerDidWatermarkUpdated?(self)
- }
-
- func templateControllerDidSelectedChanged(_ controller: KMNWatermarkTemplateController) {
- removeWatermark(currentWatermarkData?.cPDFWatermark)
-
- if let selectedWatermark = controller.selectedWatermark {
- if selectedWatermark.cPDFWatermark == nil {
- if selectedWatermark.watermarkType == .text {
- selectedWatermark.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .text)
- } else if controller.selectedWatermark?.watermarkType == .image {
- selectedWatermark.cPDFWatermark = CPDFWatermark(document: pdfDocument, type: .image)
- }
- }
- }
- currentWatermarkData = controller.selectedWatermark
- refreshPDFDocumentWaterMark()
-
- delegate?.kmWatermarkControllerDidWatermarkUpdated?(self)
- }
- }
|