123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521 |
- //
- // KMWatermarkAdjectiveBaseController.swift
- // PDF Master
- //
- // Created by tangchao on 2022/12/15.
- //
- import Cocoa
- class KMWatermarkTestWindow: NSWindow {
-
- var controller: KMWatermarkAdjectivePreViewBaseController!
-
- override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
- super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: flag);
-
- isReleasedWhenClosed = false
-
- let controller_: KMCropPreviewController = KMCropPreviewController()
- contentView?.addSubview(controller_.view)
- controller_.view.frame = CGRect(x: 0, y: 0, width: contentRect.size.width, height: contentRect.size.height)
- controller = controller_
- }
-
- }
- typealias KMWatermarkAdjectivePageNumberViewCallback = (_ value: String) -> ()
- class KMWatermarkAdjectivePageNumberView: NSView , NSTextFieldDelegate{
- var numberTextField = NSTextField()
- var rightLabel = NSTextField(labelWithString: "")
-
- var callback: KMWatermarkAdjectivePageNumberViewCallback?
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
-
- self.initSubViews()
- }
-
- required init?(coder: NSCoder) {
- super.init(coder: coder)
-
- self.initSubViews()
- }
-
- func initSubViews() {
- self.addSubview(self.numberTextField)
- self.addSubview(self.rightLabel)
-
- self.numberTextField.alignment = .center
- self.rightLabel.alignment = .left
- self.numberTextField.textColor = NSColor.white
- self.rightLabel.textColor = NSColor.white
- self.numberTextField.isBordered = false
- self.numberTextField.drawsBackground = false
- self.numberTextField.focusRingType = .none
- self.numberTextField.delegate = self
- self.numberTextField.formatter = TextFieldFormatter()
- self.numberTextField.maximumNumberOfLines = 1
- self.numberTextField.lineBreakMode = .byTruncatingTail
- }
-
- override func layout() {
- super.layout()
-
- let width: CGFloat = NSWidth(self.bounds)
- let height: CGFloat = NSHeight(self.bounds)
-
- let contentH: CGFloat = 20
- let contentY: CGFloat = (height-contentH) * 0.5
- self.numberTextField.frame = NSRect(x: 10, y: contentY, width: width * 0.5-10, height: contentH)
- self.rightLabel.frame = NSRect(x: width * 0.5, y: contentY, width: width * 0.5, height: contentH)
- }
-
- func controlTextDidChange(_ obj: Notification) {
- if (self.numberTextField.isEqual(to: obj.object)) {
- guard let callback = self.callback else {
- return
- }
-
- callback(self.numberTextField.stringValue)
- }
- }
- }
- typealias KMWatermarkAdjectivePreViewItemClick = (_ index: Int, _ param: Any?) -> ()
- /// 水印相关 预览控制器(基类)
- class KMWatermarkAdjectivePreViewBaseController: NSViewController {
- @IBOutlet weak var topBarBox: NSBox!
- lazy var topBarView: KMWatermarkAdjectiveTopBarView = {
- let view = KMWatermarkAdjectiveTopBarView()
- return view
- }()
-
- @IBOutlet weak var splitView: NSSplitView!
-
- @IBOutlet weak var pageInputBox: NSBox!
- @IBOutlet weak var preViewBox: NSBox!
- var preView: CPDFView!
- var documentURL: URL!
- var applyDocument: CPDFDocument?
-
- var pageNumberView = KMWatermarkAdjectivePageNumberView()
-
- var watermarkArray: Array<KMWatermarkModel> = []
-
- @IBOutlet weak var rightBox: NSBox!
- var rightViewController: NSViewController!
-
- @IBOutlet weak var tipBox: NSBox!
-
- private var maskView: NSButton!
- private var indicator: NSProgressIndicator!
-
- var itemClick: KMWatermarkAdjectivePreViewItemClick?
- var model: KMWatermarkAdjectiveBaseModel?
-
- init() {
- super.init(nibName: "KMWatermarkAdjectivePreViewBaseController", bundle: nil)
- }
-
- required init?(coder: NSCoder) {
- super.init(nibName: "KMWatermarkAdjectivePreViewBaseController", bundle: nil)
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
- self.topBarView.frame = self.topBarBox.contentView!.bounds
- self.topBarView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
- self.topBarBox.contentView?.addSubview(self.topBarView)
- self.topBarBox.fillColor = NSColor.white
-
- self.view.wantsLayer = true
- self.view.layer?.backgroundColor = NSColor.white.cgColor
-
- self.topBarView.isCanApply(can: false)
- self.topBarView.cancelClick = {
- [unowned self] in
- self.cancelAction()
- }
-
- self.topBarView.applyClick = {
- [unowned self] in
- self.applyAction()
- }
-
- self.topBarView.itemClick = { [weak self] section, item in
- self!.topItemClick(index: item)
- }
-
- self.rightBox.fillColor = NSColor(white: 243.0/255.0, alpha: 1.0)
- self.splitView.isVertical = true
- self.splitView.dividerStyle = .thin
- self.splitView.delegate = self
- // self.splitView.arrangedSubviews.first?.frame = NSMakeRect(0, 0, self.splitView.frame.size.width-260, self.splitView.frame.size.height)
- // self.splitView.arrangedSubviews.last?.frame = NSMakeRect(self.splitView.frame.size.width-260, 0, 260, self.splitView.frame.size.height)
-
- let pageNumberView = self.pageNumberView
- pageNumberView.frame = self.pageInputBox.contentView!.bounds
- pageNumberView.autoresizingMask = [.width, .height]
- self.pageInputBox.contentView?.addSubview(pageNumberView)
- self.pageInputBox.fillColor = NSColor.black
-
- self.pageNumberView.callback = { [weak self] string in
- var number: Int = 0
- if (string.isEmpty) {
- number = 1
- } else {
- number = Int(string)!
- if (number <= 0) {
- number = 1
- } else if (number > (self?.preView.document.pageCount)!) {
- number = Int((self?.preView.document.pageCount)!)
- }
- }
-
- self?.pageNumberView.numberTextField.stringValue = "\(number)"
- self?.preView.go(toPageIndex: number-1, animated: true)
- }
- }
-
- override func viewWillAppear() {
- super.viewWillAppear()
-
- if (self.documentURL != nil) {
- self.preView.document = CPDFDocument(url: self.documentURL)
-
- self.pageNumberView.numberTextField.stringValue = "1"
- self.pageNumberView.rightLabel.stringValue = "/ \(self.preView.document.pageCount)"
- }
- }
-
- func cancelAction() {
- guard let callback = self.itemClick else {
- return
- }
-
- callback(0, nil)
- }
-
- func applyAction() {
-
- }
-
- func topItemClick(index: Int) {
-
- }
-
- func right_gotoViewController(viewController: NSViewController) {
- for subview in self.rightBox.contentView!.subviews {
- subview.removeFromSuperview()
- }
-
- viewController.view.frame = self.rightBox.contentView!.bounds
- viewController.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
- self.rightBox.contentView?.addSubview(viewController.view)
- self.rightViewController = viewController
- }
- func addWatermark(model: KMWatermarkModel, toPath: String, completion: @escaping (_ result: Bool) -> ()) {
- DispatchQueue.global().async {
- var property: CPDFWatermark!
- var scale: CGFloat = model.scale
- if (!model.text.isEmpty) {
- property = CPDFWatermark(document: self.preView.document, type: .text)
- property.text = model.text
- property.textColor = model.getTextColor()
- scale = model.getTextFontSize() / 24.0
- } else {
- property = CPDFWatermark(document: self.preView.document, type: .image)
- property.image = model.image
- }
-
- property.scale = scale
- property.rotation = -model.rotation
- property.opacity = model.opacity
- property.tx = model.horizontalSpace
- property.ty = model.verticalSpace
- property.isFront = model.isFront
- var pageString: String = ""
- if (model.pageRangeType == 0) {
- for i in 0 ..< self.preView.document.pageCount {
- pageString.append("\(i)")
-
- if (i != self.preView.document.pageCount-1) {
- pageString.append(",")
- }
- }
- } else if (model.pageRangeType == 1) {
- for i in 0 ..< self.preView.document.pageCount {
- if (i % 2 == 0) {
- pageString.append("\(i)")
- } else {
- continue
- }
-
- if (i != self.preView.document.pageCount-1) {
- pageString.append(",")
- }
- }
- } else if (model.pageRangeType == 2) {
- for i in 0 ..< self.preView.document.pageCount {
- if (i % 2 == 1) {
- pageString.append("\(i)")
- } else {
- continue
- }
-
- if (i != self.preView.document.pageCount-1) {
- pageString.append(",")
- }
- }
- } else {
- pageString = model.pagesString
- }
- property.pageString = pageString
- property.isTilePage = model.isTilePage
- property.horizontalSpacing = model.tileHorizontalSpace / scale
- property.verticalSpacing = model.tileVerticalSpace / scale
- if (model.verticalMode == 0) {
- property.verticalPosition = .top
- } else if (model.verticalMode == 1) {
- property.verticalPosition = .center
- } else if (model.verticalMode == 2) {
- property.verticalPosition = .bottom
- }
-
- if (model.horizontalMode == 0) {
- property.horizontalPosition = .left
- } else if (model.horizontalMode == 1) {
- property.horizontalPosition = .center
- } else if (model.horizontalMode == 2) {
- property.horizontalPosition = .right
- }
- model.watermark = property
-
- self.preView.document.addWatermark(property)
-
- /// 保存到临时路径
- let documentPath = NSTemporaryDirectory()
- let tempPath: String = "\(documentPath)/\(toPath.lastPathComponent)"
- if (FileManager.default.fileExists(atPath: tempPath)) {
- try?FileManager.default.removeItem(atPath: tempPath)
- }
-
- let result = self.preView.document.write(to: URL(fileURLWithPath: tempPath))
- if (result) {
- if (FileManager.default.fileExists(atPath: toPath)) {
- try?FileManager.default.removeItem(atPath: toPath)
- }
-
- try?FileManager.default.moveItem(atPath: tempPath, toPath: toPath)
- } else {
- try?FileManager.default.removeItem(atPath: tempPath)
- }
-
- DispatchQueue.main.async {
- completion(result)
- }
- }
-
- func loadAllWatermarks() {
- self.watermarkArray.removeAll()
- let watermarks: Array<CPDFWatermark> = self.preView.document.watermarks()
- for watermark in watermarks {
- let model = KMWatermarkModel()
- model.scale = watermark.scale
- model.rotation = -watermark.rotation
- model.opacity = watermark.opacity
- model.verticalSpace = watermark.tx
- model.horizontalSpace = watermark.ty
- model.pagesString = watermark.pageString
- // model.textColor = watermark.textColor
- model.isFront = watermark.isFront
- model.isTilePage = watermark.isTilePage
- model.tileVerticalSpace = watermark.verticalSpacing
- model.tileHorizontalSpace = watermark.horizontalSpacing
-
- if (watermark.type == .text) {
- model.text = watermark.text
- // model.textFont
- model.scale = watermark.scale * 24
- } else if (watermark.type == .image) {
- model.image = watermark.image
- }
-
- if (watermark.verticalPosition == .top) {
- model.verticalMode = 0
- } else if (watermark.verticalPosition == .center) {
- model.verticalMode = 1
- } else if (watermark.verticalPosition == .bottom) {
- model.verticalMode = 2
- }
-
- if (watermark.horizontalPosition == .left) {
- model.horizontalMode = 0
- } else if (watermark.horizontalPosition == .center) {
- model.horizontalMode = 1
- } else if (watermark.horizontalPosition == .right) {
- model.horizontalMode = 2
- }
-
- model.watermark = watermark
- self.watermarkArray.append(model)
- }
- }
- }
-
- func deleteWatermarks(toPath: String, completion: @escaping (_ result: Bool) -> ()) -> () {
- if (self.preView.document.watermarks().count <= 0) {
- completion(false)
- }
- DispatchQueue.global().async {
- let array: Array<CPDFWatermark> = self.preView.document.watermarks()
- for model in array {
- self.preView.document.removeWatermark(model)
- }
- /// 保存到临时路径
- let documentPath = NSTemporaryDirectory()
- let tempPath: String = "\(documentPath)/\(toPath.lastPathComponent)"
- if (FileManager.default.fileExists(atPath: tempPath)) {
- try?FileManager.default.removeItem(atPath: tempPath)
- }
- let result = self.preView.document.write(to: URL(fileURLWithPath: tempPath))
- if (result) {
- if (FileManager.default.fileExists(atPath: toPath)) {
- try?FileManager.default.removeItem(atPath: toPath)
- }
- try?FileManager.default.moveItem(atPath: tempPath, toPath: toPath)
- } else {
- try?FileManager.default.removeItem(atPath: tempPath)
- }
- DispatchQueue.main.async {
- completion(result)
- }
- }
- }
-
- /// 开始加载loading
- func beginLoading() {
- DispatchQueue.main.async { [self] in
- self.maskView = NSButton()
- self.maskView.isBordered = false
- self.maskView.title = ""
- self.maskView.wantsLayer = true
- self.maskView.layer?.backgroundColor = NSColor(white: 1, alpha: 0.3 ).cgColor
- self.maskView.target = self
- self.maskView.action = #selector(maskViewClick)
- self.maskView.frame = (self.view.bounds)
- self.maskView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
- self.view.addSubview(self.maskView)
-
- self.indicator = NSProgressIndicator()
- self.indicator.style = .spinning
- self.indicator.controlSize = .regular
- self.indicator.isIndeterminate = true
- self.view.addSubview(self.indicator)
- let indicatorSize: CGFloat = 32
- self.indicator.frame = NSMakeRect(0.5*(NSWidth((self.view.frame))-indicatorSize), 0.5*(NSHeight((self.view.frame))-indicatorSize), indicatorSize, indicatorSize)
- self.maskView.autoresizingMask = NSView.AutoresizingMask(rawValue: 45)
-
- self.indicator.startAnimation(nil)
- }
- }
-
- /// 结束加载loading
- func endLoading() {
- DispatchQueue.main.async {
- self.indicator.stopAnimation(nil)
-
- self.indicator.removeFromSuperview()
- self.indicator = nil
-
- self.maskView.removeFromSuperview()
- self.maskView = nil
- }
- }
-
- @objc func maskViewClick() {
-
- }
-
- func findPagesString(_ model: KMWatermarkAdjectiveBaseModel) -> String{
- if (model.pageRangeType == 0) { /// 全部页面
- return "0-\(model.pageCount-1)"
- } else if (model.pageRangeType == 1) { /// 奇数页面
- var string: String = ""
- for i in 0 ..< model.pageCount {
- if (i % 2 == 1) {
- continue
- }
-
- string.append("\(i)")
- if (i != model.pageCount-1) {
- string.append(",")
- }
- }
- return string
- } else if (model.pageRangeType == 2) { /// 偶数页面
- var string: String = ""
- for i in 0 ..< model.pageCount {
- if (i % 2 == 0) {
- continue
- }
-
- string.append("\(i)")
- if (i != model.pageCount-1) {
- string.append(",")
- }
- }
- return string
- } else { /// 自定义
- return model.pageRangeString
- }
- }
- }
- extension KMWatermarkAdjectivePreViewBaseController: NSSplitViewDelegate {
- // func splitView(_ splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
- // if (dividerIndex == 0) {
- // return NSWidth(splitView.frame)-260
- // }
- // return proposedMinimumPosition
- // }
- //
- // func splitView(_ splitView: NSSplitView, constrainMaxCoordinate proposedMaximumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
- // if (dividerIndex == 0) {
- // return NSWidth(splitView.frame)-260
- // }
- // return proposedMaximumPosition
- // }
- func splitView(_ splitView: NSSplitView, shouldAdjustSizeOfSubview view: NSView) -> Bool {
- return true
- }
-
- func splitView(_ splitView: NSSplitView, shouldHideDividerAt dividerIndex: Int) -> Bool {
- return true
- }
-
- func splitView(_ splitView: NSSplitView, resizeSubviewsWithOldSize oldSize: NSSize) {
- let dividerThickness: CGFloat = splitView.dividerThickness
- var leftRect: NSRect = splitView.subviews.first!.frame
- var rightRect: NSRect = splitView.subviews.last!.frame
-
- let newFrame: NSRect = splitView.frame
- rightRect.size.height = newFrame.size.height
- leftRect.origin.x = 0
- leftRect.size.width = newFrame.size.width-rightRect.size.width-dividerThickness
- leftRect.size.height = newFrame.size.height
- rightRect.origin.x = leftRect.size.width
-
- splitView.subviews.first?.frame = leftRect
- splitView.subviews.last?.frame = rightRect
- }
- }
|