KMPrintPresenter_C.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. //
  2. // KMPrintPresenter_C.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2022/12/21.
  6. //
  7. //
  8. import Cocoa
  9. class KMPrintPresenter_C: NSObject {
  10. lazy var printData: KMPrintModel! = KMPrintModel()
  11. var document: CPDFDocument?
  12. fileprivate weak var delegate: KMPrintPresenter_CDeleage?
  13. /**
  14. 初始化presenter
  15. */
  16. func initPresenter(delegate: KMPrintPresenter_CDeleage, data: KMPrintModel, document: CPDFDocument) {
  17. self.delegate = delegate
  18. self.printData = data
  19. self.updatePrintDocument(documentURL: document.documentURL, data: self.printData)
  20. }
  21. func reloadData() {
  22. self.updatePrintDocument(documentURL: self.document!.documentURL, data: self.printData)
  23. }
  24. /**
  25. @abstract 解除绑定
  26. */
  27. func free() {
  28. delegate = nil
  29. }
  30. }
  31. protocol KMPrintPresenter_CDeleage: NSObject {
  32. func showData(presenter: KMPrintPresenter_C, document: CPDFDocument)
  33. }
  34. protocol KMPrintPresenter_CDocument: NSObject {}
  35. extension KMPrintPresenter_C: KMPrintPresenter_CDocument {
  36. /**
  37. @abstract 获取打印document
  38. @param url 源文件url
  39. @param data 数据
  40. @retrun document
  41. */
  42. func updatePrintDocument(documentURL: URL, data: KMPrintModel) -> CPDFDocument {
  43. /**
  44. 获取基本参数
  45. */
  46. let printModel: KMPrintModel = data
  47. /**
  48. 绘制document
  49. */
  50. //创建预览document
  51. let previewDocument = self.creatDocument(url: documentURL)
  52. //获取总page
  53. let pages = self.fetchPages(documentURL: documentURL, range: printModel.page.range)
  54. //插入page
  55. let insertSuccess = self.insertPages(document: previewDocument, printModel: printModel, pages: pages)
  56. if insertSuccess {
  57. KMPrint("插入成功")
  58. } else {
  59. KMPrint("插入失败")
  60. }
  61. //重置document
  62. let filePath = self.saveDocument(document: previewDocument)
  63. if self.delegate != nil {
  64. self.delegate?.showData(presenter: self, document: previewDocument)
  65. }
  66. KMPrint("保存地址" + filePath)
  67. return previewDocument
  68. }
  69. /**
  70. @abstract 插入page
  71. @param paperSet 纸张设置
  72. @param pageSet page设置
  73. @param pages page数组
  74. */
  75. func insertPages(document: CPDFDocument,
  76. printModel: KMPrintModel,
  77. pages: [CPDFPage]) -> Bool {
  78. //每张纸包含的apge数量
  79. var pageOfPaperCount: Int = 1
  80. switch printModel.page.operation.type {
  81. case .multipage:
  82. pageOfPaperCount = Int(printModel.page.operation.pageOfPaper.point.x * printModel.page.operation.pageOfPaper.point.y)
  83. case .poster:
  84. pageOfPaperCount = Int(printModel.page.operation.pageOfPaper.point.x * printModel.page.operation.pageOfPaper.point.y)
  85. default:
  86. pageOfPaperCount = 1
  87. }
  88. //总页数
  89. let count: Int = Int(ceil(Double(pages.count / pageOfPaperCount)))
  90. let size = printModel.page.operation.size
  91. var insertPages: [CPDFPage] = []
  92. for i in 0...count {
  93. //获取多页page
  94. var drawPages: [CPDFPage] = []
  95. for j in 0...pageOfPaperCount {
  96. if i * pageOfPaperCount + j < pages.count {
  97. let drawPage = pages[i * pageOfPaperCount + j]
  98. drawPages.append(drawPage)
  99. }
  100. }
  101. //TODO: 暂时屏蔽
  102. let insertPage = KMPrintPage_C.init()
  103. insertPage.pages = drawPages
  104. insertPage.data = printModel
  105. insertPage.setBounds(CGRect(x: 0, y: 0, width: printModel.paper.info.size.width, height: printModel.paper.info.size.height), for: CPDFDisplayBox.mediaBox)
  106. insertPage.drawBlock = { [weak self] box, context, pages, data in
  107. //绘制page
  108. self?.drawPageToContext(context: context, pages: pages, data: data)
  109. }
  110. insertPages.append(insertPage)
  111. }
  112. var result = true
  113. if insertPages.count != 0 {
  114. for index in 0...insertPages.count - 1 {
  115. let page = insertPages[index]
  116. var success = false
  117. if (printModel.page.range.reversePrintOrder) {
  118. success = document.insertPageObject(page, at: 0)
  119. } else {
  120. success = document.insertPageObject(page, at: UInt(index))
  121. }
  122. if success {
  123. KMPrint("插入成功")
  124. } else {
  125. KMPrint("插入失败")
  126. result = false
  127. }
  128. }
  129. }
  130. // var result = true
  131. // if pages.count != 0 {
  132. // for index in 0...pages.count - 1 {
  133. // let page = pages[index]
  134. // let success = document.insertPageObject(page, at: UInt(index))
  135. // if success {
  136. // print("插入成功")
  137. // } else {
  138. // print("插入失败")
  139. // result = false
  140. // }
  141. // }
  142. // }
  143. return result
  144. }
  145. func creatDocument(url: URL) -> CPDFDocument {
  146. if FileManager.default.fileExists(atPath: NSTemporaryDirectory() + "/PDFReaderProTest") {
  147. try?FileManager.default.createDirectory(atPath: NSTemporaryDirectory() + "/PDFReaderProTest", withIntermediateDirectories: true)
  148. }
  149. let document = CPDFDocument(url: url)!
  150. // document.importPages(IndexSet(integer: 0), from: document, at: 0)
  151. let count = document.pageCount
  152. for _ in 0...count {
  153. document.removePage(at: 0)
  154. }
  155. return document
  156. }
  157. func saveDocument(document: CPDFDocument) -> String {
  158. let filePath = NSTemporaryDirectory() + "/PDFReaderProTest/test2.pdf"
  159. if FileManager.default.fileExists(atPath: filePath) {
  160. try?FileManager.default.removeItem(atPath: filePath)
  161. }
  162. let url: URL = NSURL(string: filePath)! as URL
  163. var success = document.write(to: url)
  164. if !success {
  165. success = NSData(data: document.dataRepresentation()).write(to: url, atomically: true)
  166. }
  167. if success {
  168. KMPrint("保存成功" + filePath)
  169. } else {
  170. KMPrint("保存失败")
  171. }
  172. return filePath
  173. }
  174. /**
  175. 获取pages
  176. @param type 页面类型
  177. @param selectPages 当type 为custom时 传入选中page的下标
  178. */
  179. func fetchPages(documentURL: URL, range: KMPrintPageRange) -> [CPDFPage] {
  180. let document = CPDFDocument.init(url: documentURL)!
  181. if document.pageCount < 1 {
  182. guard let page: CPDFPage = document.page(at: 0) else {
  183. return []
  184. }
  185. return [page]
  186. } else {
  187. var pagesArray: [CPDFPage] = []
  188. switch range.type {
  189. case .allPage:
  190. for index in 0...document.pageCount - 1 {
  191. let page: CPDFPage = document.page(at: index)!
  192. pagesArray.append(page)
  193. }
  194. case .evenPage:
  195. for index in 0...document.pageCount - 1 {
  196. if index % 2 == 0 {
  197. let page: CPDFPage = document.page(at: index)!
  198. pagesArray.append(page)
  199. }
  200. }
  201. case .oddPage:
  202. for index in 0...document.pageCount - 1 {
  203. if index % 2 != 0 {
  204. let page: CPDFPage = document.page(at: index)!
  205. pagesArray.append(page)
  206. }
  207. }
  208. // case .currentPage:
  209. // let page = self.pdfView.currentPage()
  210. // if page != nil {
  211. // pagesArray.append(page!)
  212. // } else {
  213. // print("page不存在")
  214. // }
  215. case .custom:
  216. for index in range.selectPages {
  217. guard let page: CPDFPage = document.page(at: UInt(index - 1)) else {
  218. continue
  219. }
  220. pagesArray.append(page)
  221. }
  222. default:
  223. KMPrint("无匹配选项")
  224. }
  225. return pagesArray
  226. }
  227. }
  228. /**
  229. @abstract 获取context
  230. @param size纸张大小
  231. */
  232. func createContext(size: CGSize) -> CGContext {
  233. let s = CGSize(width: nearbyint(size.width), height: nearbyint(size.height))
  234. let rep = NSBitmapImageRep.init(bitmapDataPlanes: nil,
  235. pixelsWide: Int(s.width),
  236. pixelsHigh: Int(s.height),
  237. bitsPerSample: 8,
  238. samplesPerPixel: 4,
  239. hasAlpha: true,
  240. isPlanar: false,
  241. colorSpaceName: NSColorSpaceName.calibratedRGB,
  242. bytesPerRow: 0,
  243. bitsPerPixel: 0)!
  244. let context: CGContext = NSGraphicsContext.init(bitmapImageRep: rep)!.cgContext
  245. return context
  246. }
  247. /**
  248. @abstract 绘制page
  249. @param context
  250. @pages page数组 [CPDFPage]
  251. */
  252. func drawPageToContext(context: CGContext, pages: [CPDFPage], data: KMPrintModel) {
  253. /**
  254. paper
  255. */
  256. let paperDirection: KMPrintPaperDirectionType = .vertical //打印方向
  257. let paperSize: CGSize = CGSize(width: 595, height: 842) //页面paper大小
  258. var dealPaperSize: CGSize = paperSize //页面paper大小
  259. let paperInset: NSEdgeInsets = NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) //绘制paper大小
  260. var columnCount: Int = 2 //行
  261. var rowCount: Int = 2 //列
  262. let columnSpace: CGFloat = 2.0 //行间距
  263. let rowSpace: CGFloat = 2.0 //列间距
  264. let border: Bool = true
  265. /**
  266. page
  267. */
  268. let pageOrder: KMPrintPageOperation.Multipage.Order = .horizontal //页面顺序
  269. var pageSize: CGSize = CGSize(width: 100, height: 100) //page大小
  270. let contentType: KMPrintContentType = .document
  271. var scale: Float = 1.0 //缩放尺寸
  272. let autoRotate: Bool = false
  273. /**
  274. 计算
  275. */
  276. if paperDirection == .horizontal {
  277. dealPaperSize = CGSize(width: dealPaperSize.height, height: dealPaperSize.width)
  278. }
  279. //减去inset
  280. dealPaperSize = CGSize(width: dealPaperSize.width - paperInset.left - paperInset.right,
  281. height: dealPaperSize.height - paperInset.bottom - paperInset.top)
  282. //计算page尺寸(减去page之间的间隙)
  283. pageSize = CGSize(width: (dealPaperSize.width - CGFloat((columnCount - 1)) * CGFloat(columnSpace)) / CGFloat(columnCount),
  284. height: (dealPaperSize.height - CGFloat((rowCount - 1)) * CGFloat(rowSpace)) / CGFloat(rowCount))
  285. //如果是横向参数需切换
  286. if pageOrder == .horizontal ||
  287. pageOrder == .horizontalReverseSequence {
  288. let temp = columnCount
  289. columnCount = rowCount
  290. rowCount = temp
  291. }
  292. for i in 0...columnCount {
  293. for j in 0...rowCount {
  294. let index = j + i * columnCount
  295. if index < pages.count {
  296. //参数
  297. let page = pages[index]
  298. let rect = page.bounds(for: CPDFDisplayBox.cropBox)
  299. let originSize = rect.size
  300. var dealSize = originSize
  301. var rotate = page.rotation
  302. var annoations: [CPDFAnnotation] = page.annotations
  303. //处理
  304. switch contentType {
  305. case .document:
  306. annoations.removeAll()
  307. case .documentAndStamp:
  308. for annoation in annoations {
  309. if !(annoation.isKind(of: CPDFStampAnnotation.self)) {
  310. annoation.page.removeAnnotation(annoation)
  311. }
  312. }
  313. case .documentAndMarkup:
  314. for annoation in annoations {
  315. if !(annoation.isKind(of: CPDFMarkupAnnotation.self)) {
  316. annoation.page.removeAnnotation(annoation)
  317. }
  318. }
  319. default: break
  320. }
  321. if rotate == 90 ||
  322. rotate == 270 {
  323. dealSize = CGSize(width: originSize.height, height: originSize.width)
  324. }
  325. //取出page横竖时能显示的最大Rect
  326. if autoRotate {
  327. //page旋转度数为0度或者180度时,在指定的大小内,能显示的比例
  328. let scale1 = Float(min(pageSize.width / originSize.width, pageSize.height / originSize.height))
  329. //page旋转度数为90度或者270度时,在指定的大小内,能显示的比例
  330. let scale2 = Float(min(pageSize.width / originSize.height, pageSize.height / originSize.width))
  331. scale = max(scale1, scale2)
  332. if scale1 > scale2 {
  333. //高为竖直排列时,显示最大,则需将page时90度或者270度需要进行旋转
  334. if rotate == 90 ||
  335. rotate == 270 {
  336. rotate = rotate - 90
  337. }
  338. } else {
  339. //宽为竖直排列时,显示最大,则需将page时0度或者180度需要进行旋转
  340. if rotate == 0 ||
  341. rotate == 180 {
  342. rotate = rotate - 90
  343. }
  344. }
  345. if rotate == 90 ||
  346. rotate == 270 {
  347. dealSize = CGSize(width: originSize.height, height: originSize.width)
  348. } else {
  349. dealSize = CGSize(width: originSize.width, height: originSize.height)
  350. }
  351. } else {
  352. scale = Float(min(pageSize.width / dealSize.width, pageSize.height / dealSize.height))
  353. }
  354. scale = min(scale, 1)
  355. //居中
  356. let spaceX: CGFloat = (pageSize.width - dealSize.width * CGFloat(scale)) / 2
  357. let spaceY: CGFloat = (pageSize.height - dealSize.height * CGFloat(scale))/2
  358. var translateX: CGFloat = 0.0
  359. var translateY: CGFloat = 0.0
  360. switch pageOrder {
  361. case .horizontal:
  362. translateX = (pageSize.width + columnSpace) + CGFloat(j) + paperInset.left + spaceX
  363. //页面内容高度 + 下边的行间距 - 第几个cell的高度 +居中
  364. translateY = dealPaperSize.height + paperInset.bottom - (pageSize.height + rowSpace) * (CGFloat(i) + 1) + spaceY + rowSpace
  365. case .horizontalReverseSequence:
  366. translateX = dealPaperSize.width + paperInset.left - (pageSize.width + columnSpace) * (CGFloat(j) + 1) + spaceX + columnSpace
  367. translateY = dealPaperSize.height - (pageSize.height + rowSpace) * (CGFloat(i) + 1) + spaceY + paperInset.bottom + rowSpace
  368. case .vertical:
  369. translateX = (pageSize.width + columnSpace) * CGFloat(i) + paperInset.left + spaceX
  370. translateY = dealPaperSize.height - (pageSize.height + rowSpace) * (CGFloat(j) + 1) + spaceY + paperInset.bottom + rowSpace
  371. case .verticalReverseSequence:
  372. translateX = dealPaperSize.width + paperInset.left - (pageSize.width + columnSpace) * (CGFloat(i) + 1) + spaceX + columnSpace
  373. translateY = dealPaperSize.height - (pageSize.height + rowSpace) * (CGFloat(j) + 1) + spaceY + paperInset.bottom + rowSpace
  374. }
  375. context.saveGState()
  376. //平移
  377. context.translateBy(x: translateX, y: translateY)
  378. //缩放
  379. context.scaleBy(x: CGFloat(scale), y: CGFloat(scale))
  380. page.draw(with: CPDFDisplayBox.cropBox, to: context)
  381. page.transform(context, for: CPDFDisplayBox.cropBox)
  382. if border {
  383. var dirtyRect = rect
  384. if rotate == 90 ||
  385. rotate == 270 {
  386. dirtyRect = NSMakeRect(dirtyRect.origin.x, dirtyRect.origin.y, dirtyRect.size.height, dirtyRect.size.width)
  387. }
  388. context.addRect(dirtyRect)
  389. context.setStrokeColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
  390. context.strokePath()
  391. }
  392. context.restoreGState()
  393. }
  394. }
  395. }
  396. }
  397. }
  398. protocol KMPrintPresenter_CProtocol: NSObject {
  399. }