KMPrintPreviewView.swift 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //
  2. // KMPrintPreviewView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2022/12/8.
  6. //
  7. import Cocoa
  8. class KMPrintPreviewView: KMBaseXibView {
  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. override func setup() {
  94. super.setup()
  95. // self.previewContentView.wantsLayer = true
  96. // self.previewContentView.layer?.backgroundColor = NSColor.km_init(hex: "#F7F8FA").cgColor
  97. self.currentPageTextField.textField.font = NSFont.SFProTextRegularFont(12.0)
  98. // self.currentPageTextField.textField.textColor = NSColor.km_init(hex: "#252629")
  99. self.currentPageTextField.textDidEndEditing = {[weak self] (string) in
  100. self?.presenter.toPage(index: max((Int(string) ?? 0) - 1, 0))
  101. }
  102. // self.partitionLabel.textColor = NSColor.km_init(hex: "#252629")
  103. self.titleLabel.font = NSFont.SFProTextRegularFont(12.0)
  104. // self.titleLabel.textColor = NSColor.km_init(hex: "#616469")
  105. self.pageCountLabel.font = NSFont.SFProTextRegularFont(12.0)
  106. // self.pageCountLabel.textColor = NSColor.km_init(hex: "#252629")
  107. self.previousButton.wantsLayer = true
  108. self.previousButton.layer?.borderWidth = 1
  109. // self.previousButton.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
  110. self.previousButton.layer?.cornerRadius = 4
  111. // self.previousButton.image = NSImage(named: "icon_btn_pagination_back_norm")
  112. self.nextButton.wantsLayer = true
  113. self.nextButton.layer?.borderWidth = 1
  114. // self.nextButton.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
  115. self.nextButton.layer?.cornerRadius = 4
  116. // self.nextButton.image = NSImage(named: "icon_btn_pagination_lastpage_dis")
  117. }
  118. func notification() {
  119. NotificationCenter.default.addObserver(self, selector: #selector(PDFViewDocumentChangedNotification), name: NSNotification.Name.CPDFViewPageChanged, object: nil)
  120. }
  121. //刷新PDFPreview
  122. override func reloadData() {
  123. super.reloadData()
  124. //UI 显示
  125. if self.pdfDocument != nil {
  126. guard let model = model else { return }
  127. let page = self.previewView.currentPage
  128. let size = model.paper.info.size
  129. if page != nil {
  130. let pageIndex = self.previewView.document?.index(for: page ?? PDFPage())
  131. self.currentPageTextField.stringValue = (pageIndex! + 1).description
  132. self.pageCountLabel.stringValue = (self.previewView.document?.pageCount.description)!
  133. self.titleLabel.stringValue = NSLocalizedString("Size", comment: "") + ":" + size.width.description + NSLocalizedString("mm", comment: "") + "*" + String(size.height.description) + NSLocalizedString("mm", comment: "")
  134. }
  135. }
  136. }
  137. //MARK: 通知
  138. @objc func PDFViewDocumentChangedNotification(_ notification: Notification) -> Void {
  139. // self.reloadData()
  140. }
  141. }
  142. protocol KMPrintPreviewViewAction { }
  143. extension KMPrintPreviewView: KMPrintPreviewViewAction {
  144. @IBAction func nextButton(_ sender: NSButton) {
  145. self.presenter.nextPage()
  146. }
  147. @IBAction func previousAction(_ sender: NSButton) {
  148. self.presenter.previousPage()
  149. }
  150. func toPageIndex(_ index: UInt = 0) {
  151. self.presenter.toPage(index: Int(index))
  152. }
  153. }
  154. extension KMPrintPreviewView: KMPrintPreviewPresenterDelegate {
  155. func dataDidChange(presenter: KMPrintPreviewPresenter, view: PDFView) {
  156. self.reloadData()
  157. }
  158. func documentDidChange(presenter: KMPrintPreviewPresenter, document: PDFDocument) {
  159. self.previewView.document = document
  160. self.reloadData()
  161. }
  162. }
  163. extension KMPrintPreviewView {
  164. func updatePosterPreView() {
  165. guard let model = model else { return }
  166. if model.page.operation.type == .poster {
  167. var size = self.previewView.frame.size
  168. let rect = self.previewView.currentPage?.bounds(for: .cropBox)
  169. let scale = self.previewView.scaleFactor
  170. let offest = NSMakePoint((rect?.origin.x)! * scale, (rect?.origin.y)! * scale)
  171. size = NSMakeSize(rect!.size.width * scale, rect!.size.height * scale)
  172. var point = CGPoint(x: 1, y: 1)
  173. if model.page.operation.poster.type == .tile {
  174. point = model.page.operation.poster.tilePoint
  175. } else {
  176. point = model.page.operation.pageOfPaper.point
  177. }
  178. var horizontals: [Int] = []
  179. var verticals: [Int] = []
  180. let pointX: Int = Int(point.x)
  181. let pointY: Int = Int(point.y)
  182. for i in 0..<pointX {
  183. if i != 0 {
  184. horizontals.append(Int(size.width) * i / (pointX) + Int(offest.x))
  185. }
  186. }
  187. for i in 0..<pointY {
  188. if i != 0 {
  189. verticals.append(Int(size.height) * i / (pointY) + Int(offest.y))
  190. }
  191. }
  192. self.posterPreView.horizontals = horizontals
  193. self.posterPreView.verticals = verticals
  194. // self.posterPreView.spaceWidth = spaceWidth
  195. // self.posterPreView.spaceHeight = spaceHeight
  196. self.posterPreView.scale = 1
  197. self.posterPreView.needsDisplay = true
  198. if model.page.range.reversePrintOrder != reversePrintOrder {
  199. reversePrintOrder = model.page.range.reversePrintOrder
  200. // guard let url = self.previewView.document?.documentURL else { return }
  201. guard let pdfDocument = self.previewView.document else { return }
  202. // if model.page.range.reversePrintOrder {
  203. // 逆序排列页面
  204. let pageCount = pdfDocument.pageCount
  205. let reversedPDFDocument = PDFDocument()
  206. for pageIndex in (0..<pageCount).reversed() {
  207. if let page = pdfDocument.page(at: pageIndex) {
  208. reversedPDFDocument.insert(page, at: reversedPDFDocument.pageCount)
  209. }
  210. }
  211. self.previewView.document = reversedPDFDocument
  212. // } else {
  213. // self.previewView.document = pdfDocument
  214. // }
  215. }
  216. }
  217. }
  218. }