123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- //
- // KMPrintPreviewView.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2022/12/8.
- //
- import Cocoa
- class KMPrintPreviewView: BaseXibView {
- @IBOutlet weak var previewContentView: NSView!
- @IBOutlet weak var previewView: PDFView!
- @IBOutlet weak var posterPreView: KMPrintPosterPreviewView!
-
- @IBOutlet weak var titleLabel: NSTextField!
- @IBOutlet weak var previousButton: NSButton!
-
- @IBOutlet weak var nextButton: NSButton!
-
- @IBOutlet weak var currentPageTextField: KMBaseTextField!
- @IBOutlet weak var pageCountLabel: NSTextField!
- @IBOutlet weak var partitionLabel: NSTextField!
-
- var isPrintPreView: Bool = true
- var reversePrintOrder: Bool = false
- var password: String = ""
- var model: KMPrintModel? {
- didSet {
- self.reloadData()
- }
- }
-
- private lazy var presenter: KMPrintPreviewPresenter = KMPrintPreviewPresenter()
- var pdfDocument: PDFDocument? {
- didSet {
- self.previewView.document = pdfDocument //PDFDocument(url: pdfDocument!.documentURL)
- self.previewView.document?.unlock(withPassword: password)
- self.previewView.autoScales = true
- self.previewView.displayMode = .singlePage
- self.previewView.documentView?.enclosingScrollView?.scrollerStyle = .overlay
- if model?.page.operation.type == .poster {
- // self.presenter.pdfView = self.previewView
- self.presenter.initPresenter(delegate: self, view: self.previewView)
- } else {
- self.presenter.initPresenter(delegate: self, view: self.previewView)
- }
- self.reloadData()
- }
- // didSet {
- //
- // if self.previewView.document != nil {
- // self.previewView.needsDisplay = true
- // self.reloadData()
- // } else {
- //
- // var pdfDocument2 = PDFDocument(url: pdfDocument!.documentURL)
- // var tempDocument = PDFDocument()
- //
- // var pages: [PDFPage] = []
- // for i in 0..<pdfDocument2!.pageCount {
- // let page: PDFPage = (pdfDocument2?.page(at: i))!
- // pages.append(page)
- // }
- //
- // for i in 0..<pdfDocument2!.pageCount {
- // let tempPage: PDFPage = pages[i]
- // let page = KMPrintPage()
- // page.pages = pages
- // page.page = pages[i]
- // page.data = model
- // page.drawBlock = { box, context, pages, page, data in
- // KMPrintPresenter.drawPage(context: context, page: page, model: data)
- // }
- // page.setBounds(tempPage.bounds(for: .cropBox), for: .cropBox)
- // tempDocument.insert(page, at: Int(i))
- // }
- //
- //
- // self.previewView.document = tempDocument
- // self.previewView.autoScales = true
- // self.previewView.displayMode = .singlePage
- // self.previewView.documentView?.enclosingScrollView?.scrollerStyle = .overlay
- // if model?.page.operation.type == .poster {
- //
- // } else {
- // self.presenter.initPresenter(delegate: self, view: self.previewView)
- // }
- // self.previewView.needsDisplay = true
- // self.reloadData()
- // }
- // }
- }
-
- deinit {
- // self.delegate = nil
- }
- override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
- // Drawing code here.
-
- }
-
- func setup() {
-
- // self.previewContentView.wantsLayer = true
- // self.previewContentView.layer?.backgroundColor = NSColor.km_init(hex: "#F7F8FA").cgColor
-
- self.currentPageTextField.textField.font = NSFont.SFProTextRegularFont(12.0)
- // self.currentPageTextField.textField.textColor = NSColor.km_init(hex: "#252629")
- self.currentPageTextField.textDidEndEditing = {[weak self] (string) in
- self?.presenter.toPage(index: max((Int(string) ?? 0) - 1, 0))
- }
-
- // self.partitionLabel.textColor = NSColor.km_init(hex: "#252629")
-
- self.titleLabel.font = NSFont.SFProTextRegularFont(12.0)
- // self.titleLabel.textColor = NSColor.km_init(hex: "#616469")
-
- self.pageCountLabel.font = NSFont.SFProTextRegularFont(12.0)
- // self.pageCountLabel.textColor = NSColor.km_init(hex: "#252629")
-
- self.previousButton.wantsLayer = true
- self.previousButton.layer?.borderWidth = 1
- // self.previousButton.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
- self.previousButton.layer?.cornerRadius = 4
- // self.previousButton.image = NSImage(named: "icon_btn_pagination_back_norm")
-
- self.nextButton.wantsLayer = true
- self.nextButton.layer?.borderWidth = 1
- // self.nextButton.layer?.borderColor = NSColor.km_init(hex: "#DFE1E5").cgColor
- self.nextButton.layer?.cornerRadius = 4
- // self.nextButton.image = NSImage(named: "icon_btn_pagination_lastpage_dis")
- }
-
- func notification() {
- NotificationCenter.default.addObserver(self, selector: #selector(PDFViewDocumentChangedNotification), name: NSNotification.Name.CPDFViewPageChanged, object: nil)
- }
-
- //刷新PDFPreview
- func reloadData() {
- //UI 显示
- if self.pdfDocument != nil {
- guard let model = model else { return }
-
- let page = self.previewView.currentPage
- let size = model.paper.info.size
-
- if page != nil {
- let pageIndex = self.previewView.document?.index(for: page ?? PDFPage())
- self.currentPageTextField.stringValue = (pageIndex! + 1).description
- self.pageCountLabel.stringValue = (self.previewView.document?.pageCount.description)!
-
- self.titleLabel.stringValue = NSLocalizedString("Size", comment: "") + ":" + size.width.description + NSLocalizedString("mm", comment: "") + "*" + String(size.height.description) + NSLocalizedString("mm", comment: "")
- }
- }
- }
-
- //MARK: 通知
- @objc func PDFViewDocumentChangedNotification(_ notification: Notification) -> Void {
- // self.reloadData()
- }
- }
- protocol KMPrintPreviewViewAction { }
- extension KMPrintPreviewView: KMPrintPreviewViewAction {
- @IBAction func nextButton(_ sender: NSButton) {
- self.presenter.nextPage()
- }
-
- @IBAction func previousAction(_ sender: NSButton) {
- self.presenter.previousPage()
- }
-
- func toPageIndex(_ index: UInt = 0) {
- self.presenter.toPage(index: Int(index))
- }
- }
- extension KMPrintPreviewView: KMPrintPreviewPresenterDelegate {
- func dataDidChange(presenter: KMPrintPreviewPresenter, view: PDFView) {
- self.reloadData()
- }
-
- func documentDidChange(presenter: KMPrintPreviewPresenter, document: PDFDocument) {
- self.previewView.document = document
- self.reloadData()
- }
- }
- extension KMPrintPreviewView {
- func updatePosterPreView() {
- guard let model = model else { return }
- if model.page.operation.type == .poster {
- var size = self.previewView.frame.size
- let rect = self.previewView.currentPage?.bounds(for: .cropBox)
- let scale = self.previewView.scaleFactor
- let offest = NSMakePoint((rect?.origin.x)! * scale, (rect?.origin.y)! * scale)
- size = NSMakeSize(rect!.size.width * scale, rect!.size.height * scale)
-
- var point = CGPoint(x: 1, y: 1)
- if model.page.operation.poster.type == .tile {
- point = model.page.operation.poster.tilePoint
- } else {
- point = model.page.operation.pageOfPaper.point
- }
- var horizontals: [Int] = []
- var verticals: [Int] = []
-
- let pointX: Int = Int(point.x)
- let pointY: Int = Int(point.y)
- for i in 0..<pointX {
- if i != 0 {
- horizontals.append(Int(size.width) * i / (pointX) + Int(offest.x))
- }
- }
-
- for i in 0..<pointY {
- if i != 0 {
- verticals.append(Int(size.height) * i / (pointY) + Int(offest.y))
- }
- }
-
- self.posterPreView.horizontals = horizontals
- self.posterPreView.verticals = verticals
- // self.posterPreView.spaceWidth = spaceWidth
- // self.posterPreView.spaceHeight = spaceHeight
- self.posterPreView.scale = 1
- self.posterPreView.needsDisplay = true
-
- if model.page.range.reversePrintOrder != reversePrintOrder {
- reversePrintOrder = model.page.range.reversePrintOrder
- // guard let url = self.previewView.document?.documentURL else { return }
- guard let pdfDocument = self.previewView.document else { return }
-
- // if model.page.range.reversePrintOrder {
- // 逆序排列页面
- let pageCount = pdfDocument.pageCount
- let reversedPDFDocument = PDFDocument()
- for pageIndex in (0..<pageCount).reversed() {
- if let page = pdfDocument.page(at: pageIndex) {
- reversedPDFDocument.insert(page, at: reversedPDFDocument.pageCount)
- }
- }
- self.previewView.document = reversedPDFDocument
- // } else {
- // self.previewView.document = pdfDocument
- // }
- }
- }
- }
- }
|