KMPrintPreviewView.swift 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // KMPrintPreviewView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2022/12/8.
  6. //
  7. import Cocoa
  8. class KMPrintPreviewView: BaseXibView {
  9. @IBOutlet weak var previewContentView: NSView!
  10. @IBOutlet weak var previewView: PDFView!
  11. @IBOutlet weak var posterPreView: KMPrintPosterPreviewView!
  12. @IBOutlet weak var titleLabel: NSTextField!
  13. @IBOutlet weak var previousButton: NSButton!
  14. @IBOutlet weak var nextButton: NSButton!
  15. @IBOutlet weak var currentPageTextField: KMBaseTextField!
  16. @IBOutlet weak var pageCountLabel: NSTextField!
  17. @IBOutlet weak var partitionLabel: NSTextField!
  18. var isPrintPreView: Bool = true
  19. var reversePrintOrder: Bool = false
  20. var password: String = ""
  21. var model: KMPrintModel? {
  22. didSet {
  23. self.reloadData()
  24. }
  25. }
  26. private lazy var presenter: KMPrintPreviewPresenter = KMPrintPreviewPresenter()
  27. var pdfDocument: PDFDocument? {
  28. didSet {
  29. self.previewView.document = pdfDocument //PDFDocument(url: pdfDocument!.documentURL)
  30. self.previewView.document?.unlock(withPassword: password)
  31. self.previewView.autoScales = true
  32. self.previewView.displayMode = .singlePage
  33. self.previewView.documentView?.enclosingScrollView?.scrollerStyle = .overlay
  34. if model?.page.operation.type == .poster {
  35. // self.presenter.pdfView = self.previewView
  36. self.presenter.initPresenter(delegate: self, view: self.previewView)
  37. } else {
  38. self.presenter.initPresenter(delegate: self, view: self.previewView)
  39. }
  40. self.reloadData()
  41. }
  42. // didSet {
  43. //
  44. // if self.previewView.document != nil {
  45. // self.previewView.needsDisplay = true
  46. // self.reloadData()
  47. // } else {
  48. //
  49. // var pdfDocument2 = PDFDocument(url: pdfDocument!.documentURL)
  50. // var tempDocument = PDFDocument()
  51. //
  52. // var pages: [PDFPage] = []
  53. // for i in 0..<pdfDocument2!.pageCount {
  54. // let page: PDFPage = (pdfDocument2?.page(at: i))!
  55. // pages.append(page)
  56. // }
  57. //
  58. // for i in 0..<pdfDocument2!.pageCount {
  59. // let tempPage: PDFPage = pages[i]
  60. // let page = KMPrintPage()
  61. // page.pages = pages
  62. // page.page = pages[i]
  63. // page.data = model
  64. // page.drawBlock = { box, context, pages, page, data in
  65. // KMPrintPresenter.drawPage(context: context, page: page, model: data)
  66. // }
  67. // page.setBounds(tempPage.bounds(for: .cropBox), for: .cropBox)
  68. // tempDocument.insert(page, at: Int(i))
  69. // }
  70. //
  71. //
  72. // self.previewView.document = tempDocument
  73. // self.previewView.autoScales = true
  74. // self.previewView.displayMode = .singlePage
  75. // self.previewView.documentView?.enclosingScrollView?.scrollerStyle = .overlay
  76. // if model?.page.operation.type == .poster {
  77. //
  78. // } else {
  79. // self.presenter.initPresenter(delegate: self, view: self.previewView)
  80. // }
  81. // self.previewView.needsDisplay = true
  82. // self.reloadData()
  83. // }
  84. // }
  85. }
  86. deinit {
  87. // self.delegate = nil
  88. }
  89. override func draw(_ dirtyRect: NSRect) {
  90. super.draw(dirtyRect)
  91. // Drawing code here.
  92. }
  93. func setup() {
  94. // self.previewContentView.wantsLayer = true
  95. // self.previewContentView.layer?.backgroundColor = NSColor.km_init(hex: "#F7F8FA").cgColor
  96. self.currentPageTextField.textField.font = NSFont.SFProTextRegularFont(12.0)
  97. // self.currentPageTextField.textField.textColor = NSColor.km_init(hex: "#252629")
  98. self.currentPageTextField.textDidEndEditing = {[weak self] (string) in
  99. self?.presenter.toPage(index: max((Int(string) ?? 0) - 1, 0))
  100. }
  101. // self.partitionLabel.textColor = NSColor.km_init(hex: "#252629")
  102. self.titleLabel.font = NSFont.SFProTextRegularFont(12.0)
  103. // self.titleLabel.textColor = NSColor.km_init(hex: "#616469")
  104. self.pageCountLabel.font = NSFont.SFProTextRegularFont(12.0)
  105. // self.pageCountLabel.textColor = NSColor.km_init(hex: "#252629")
  106. self.previousButton.wantsLayer = true
  107. self.previousButton.layer?.borderWidth = 1
  108. // self.previousButton.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
  109. self.previousButton.layer?.cornerRadius = 4
  110. // self.previousButton.image = NSImage(named: "icon_btn_pagination_back_norm")
  111. self.nextButton.wantsLayer = true
  112. self.nextButton.layer?.borderWidth = 1
  113. // self.nextButton.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
  114. self.nextButton.layer?.cornerRadius = 4
  115. // self.nextButton.image = NSImage(named: "icon_btn_pagination_lastpage_dis")
  116. }
  117. func notification() {
  118. NotificationCenter.default.addObserver(self, selector: #selector(PDFViewDocumentChangedNotification), name: NSNotification.Name.CPDFViewPageChanged, object: nil)
  119. }
  120. //刷新PDFPreview
  121. func reloadData() {
  122. //UI 显示
  123. if self.pdfDocument != nil {
  124. guard let model = model else { return }
  125. let page = self.previewView.currentPage
  126. let size = model.paper.info.size
  127. if page != nil {
  128. let pageIndex = self.previewView.document?.index(for: page ?? PDFPage())
  129. self.currentPageTextField.stringValue = (pageIndex! + 1).description
  130. self.pageCountLabel.stringValue = (self.previewView.document?.pageCount.description)!
  131. self.titleLabel.stringValue = NSLocalizedString("Size", comment: "") + ":" + size.width.description + NSLocalizedString("mm", comment: "") + "*" + String(size.height.description) + NSLocalizedString("mm", comment: "")
  132. }
  133. }
  134. }
  135. //MARK: 通知
  136. @objc func PDFViewDocumentChangedNotification(_ notification: Notification) -> Void {
  137. // self.reloadData()
  138. }
  139. }
  140. protocol KMPrintPreviewViewAction { }
  141. extension KMPrintPreviewView: KMPrintPreviewViewAction {
  142. @IBAction func nextButton(_ sender: NSButton) {
  143. self.presenter.nextPage()
  144. }
  145. @IBAction func previousAction(_ sender: NSButton) {
  146. self.presenter.previousPage()
  147. }
  148. func toPageIndex(_ index: UInt = 0) {
  149. self.presenter.toPage(index: Int(index))
  150. }
  151. }
  152. extension KMPrintPreviewView: KMPrintPreviewPresenterDelegate {
  153. func dataDidChange(presenter: KMPrintPreviewPresenter, view: PDFView) {
  154. self.reloadData()
  155. }
  156. func documentDidChange(presenter: KMPrintPreviewPresenter, document: PDFDocument) {
  157. self.previewView.document = document
  158. self.reloadData()
  159. }
  160. }
  161. extension KMPrintPreviewView {
  162. func updatePosterPreView() {
  163. guard let model = model else { return }
  164. if model.page.operation.type == .poster {
  165. var size = self.previewView.frame.size
  166. let rect = self.previewView.currentPage?.bounds(for: .cropBox)
  167. let scale = self.previewView.scaleFactor
  168. let offest = NSMakePoint((rect?.origin.x)! * scale, (rect?.origin.y)! * scale)
  169. size = NSMakeSize(rect!.size.width * scale, rect!.size.height * scale)
  170. var point = CGPoint(x: 1, y: 1)
  171. if model.page.operation.poster.type == .tile {
  172. point = model.page.operation.poster.tilePoint
  173. } else {
  174. point = model.page.operation.pageOfPaper.point
  175. }
  176. var horizontals: [Int] = []
  177. var verticals: [Int] = []
  178. let pointX: Int = Int(point.x)
  179. let pointY: Int = Int(point.y)
  180. for i in 0..<pointX {
  181. if i != 0 {
  182. horizontals.append(Int(size.width) * i / (pointX) + Int(offest.x))
  183. }
  184. }
  185. for i in 0..<pointY {
  186. if i != 0 {
  187. verticals.append(Int(size.height) * i / (pointY) + Int(offest.y))
  188. }
  189. }
  190. self.posterPreView.horizontals = horizontals
  191. self.posterPreView.verticals = verticals
  192. // self.posterPreView.spaceWidth = spaceWidth
  193. // self.posterPreView.spaceHeight = spaceHeight
  194. self.posterPreView.scale = 1
  195. self.posterPreView.needsDisplay = true
  196. if model.page.range.reversePrintOrder != reversePrintOrder {
  197. reversePrintOrder = model.page.range.reversePrintOrder
  198. // guard let url = self.previewView.document?.documentURL else { return }
  199. guard let pdfDocument = self.previewView.document else { return }
  200. // if model.page.range.reversePrintOrder {
  201. // 逆序排列页面
  202. let pageCount = pdfDocument.pageCount
  203. let reversedPDFDocument = PDFDocument()
  204. for pageIndex in (0..<pageCount).reversed() {
  205. if let page = pdfDocument.page(at: pageIndex) {
  206. reversedPDFDocument.insert(page, at: reversedPDFDocument.pageCount)
  207. }
  208. }
  209. self.previewView.document = reversedPDFDocument
  210. // } else {
  211. // self.previewView.document = pdfDocument
  212. // }
  213. }
  214. }
  215. }
  216. }