KMPrintPresenter.swift 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. //
  2. // KMPrintPresenter.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2022/12/21.
  6. //
  7. import Cocoa
  8. import PDFKit
  9. //MARK: CPDFKit page 方法无法使用 暂时使用系统方法
  10. class KMPrintPresenter: NSObject {
  11. lazy var printData: KMPrintModel = KMPrintModel() {
  12. didSet {
  13. self.reloadData()
  14. }
  15. }
  16. var document: CPDFDocument?
  17. fileprivate weak var delegate: KMPrintPresenterDeleage?
  18. /**
  19. 初始化presenter 绑定数据
  20. */
  21. func initPresenter(delegate: KMPrintPresenterDeleage, data: KMPrintModel, document: CPDFDocument) {
  22. self.delegate = delegate
  23. self.document = document
  24. self.printData = data
  25. let pdfDocument = self.updatePrintDocument(documentURL: document.documentURL, data: self.printData)
  26. self.printData.url = pdfDocument.documentURL
  27. }
  28. /**
  29. 刷新数据
  30. */
  31. func reloadData() {
  32. guard let document = document else { return }
  33. let pdfDocument = self.updatePrintDocument(documentURL: document.documentURL, data: self.printData)
  34. self.printData.url = pdfDocument.documentURL
  35. }
  36. /**
  37. @abstract 解除绑定
  38. */
  39. func free() {
  40. delegate = nil
  41. }
  42. }
  43. protocol KMPrintPresenterDeleage: NSObject {
  44. func showData(presenter: KMPrintPresenter, document: CPDFDocument)
  45. }
  46. protocol KMPrintPresenterDocument: NSObject {}
  47. extension KMPrintPresenter: KMPrintPresenterDocument {
  48. /**
  49. @abstract 获取打印document
  50. @param url 源文件url
  51. @param data 数据
  52. @retrun document
  53. */
  54. func updatePrintDocument(documentURL: URL, data: KMPrintModel) -> CPDFDocument {
  55. // 获取基本参数
  56. let printModel: KMPrintModel = data
  57. //获取总page
  58. let pages = self.fetchPages(documentURL, printModel.page)
  59. //绘制PDF
  60. let filePath = self.drawPages(nil, printModel, pages)
  61. let result = CPDFDocument(url: URL(fileURLWithPath: filePath))!
  62. if self.delegate != nil {
  63. self.delegate?.showData(presenter: self, document: result)
  64. }
  65. KMPrint("保存地址" + filePath)
  66. return result
  67. }
  68. /**
  69. @abstract 插入page
  70. @param paperSet 纸张设置
  71. @param pageSet page设置
  72. @param pages page数组
  73. */
  74. func drawPages(_ toFilePath: String?,
  75. _ printModel: KMPrintModel,
  76. _ pages: [KMPrintDrawPage]) -> String {
  77. /**
  78. 参数
  79. */
  80. //纸张大小
  81. let paperSize: CGSize = self.fetchPaperSize(printModel.paper)
  82. //总页数
  83. let paperCount: Int = self.fetchTotalPaperCount(paperSize, pages, printModel.page)
  84. //每页page数
  85. let pageOfPaperCount: Int = self.fetchPageOfPaper(printModel.page)
  86. //获取每张纸的page
  87. let drawPages: [[KMPrintDrawPage]] = self.fetchDrawPages(paperSize, printModel.page, paperCount, pageOfPaperCount, pages)
  88. //导出地址
  89. let filePath = KMPrintPresenter.fetchSaveFilePath(toFilePath)
  90. /**
  91. 绘制每张纸的内容
  92. */
  93. //创建画布
  94. let context: CGContext = self.createContext(filePath, paperSize)
  95. for drawPage in drawPages {
  96. context.beginPDFPage(nil)
  97. self.drawPageToContext(context, drawPage, printModel)
  98. context.endPDFPage()
  99. }
  100. context.closePDF()
  101. return filePath
  102. }
  103. /**
  104. 获取绘制的pages
  105. @pageModel page参数
  106. @param paperCount 纸张数量
  107. @param pageOfPaperCount 每张纸的page数量
  108. @param pages 所有page数量
  109. */
  110. func fetchDrawPages(_ paperSize: CGSize, _ pageModel: KMPrintPageModel,_ paperCount: Int, _ pageOfPaperCount: Int, _ pages: [KMPrintDrawPage]) -> [[KMPrintDrawPage]] {
  111. guard pages.count != 0 else {
  112. return []
  113. }
  114. //一个page重复获取次数
  115. var pageRepetitionCount = 1
  116. if (pageModel.operation.type == .poster) {
  117. if (pageModel.operation.poster.type == .tile) {
  118. pageRepetitionCount = Int(pageModel.operation.poster.tilePoint.x * pageModel.operation.poster.tilePoint.y)
  119. } else if (pageModel.operation.poster.type == .breakUp) {
  120. pageRepetitionCount = Int(pageModel.operation.poster.pageOfPaper.point.x * pageModel.operation.poster.pageOfPaper.point.y)
  121. }
  122. }
  123. var drawPages:[[KMPrintDrawPage]] = []
  124. for i in 0...(paperCount - 1) {
  125. //获取多页page
  126. var tempPags: [KMPrintDrawPage] = []
  127. for j in 0...(pageOfPaperCount - 1) {
  128. let pageIndex = i / pageRepetitionCount
  129. if (pageIndex * pageOfPaperCount + j < pages.count) {
  130. let originDrawPage = (pages[pageIndex * pageOfPaperCount + j])
  131. let drawPage = KMPrintDrawPage()
  132. drawPage.page = originDrawPage.page
  133. var pageCropRect = self.fetchPageCropRect(paperSize,i % pageRepetitionCount, pageModel, drawPage)
  134. var pageShowRect = pageCropRect
  135. if (pageModel.operation.type == .poster) {
  136. if (pageModel.operation.poster.type == .tile) {
  137. pageShowRect = self.fetchPageShowRect(paperSize, i % pageRepetitionCount, pageModel, drawPage)
  138. } else if (pageModel.operation.poster.type == .breakUp) {
  139. pageShowRect = pageCropRect
  140. }
  141. }
  142. drawPage.cropRect = pageCropRect
  143. drawPage.showRect = pageShowRect
  144. tempPags.append(drawPage)
  145. }
  146. }
  147. drawPages.append(tempPags)
  148. }
  149. return drawPages
  150. }
  151. /**
  152. 获取pages
  153. @param type 页面类型
  154. @param contentType annoation类型
  155. @param selectPages 当type 为custom时 传入选中page的下标
  156. */
  157. static func fetchSaveFilePath(_ filePath: String?) -> String {
  158. var saveFilePath = filePath ?? ""
  159. if saveFilePath.count == 0 {
  160. saveFilePath = NSTemporaryDirectory() + "/PDFMasterTest/test2.pdf"
  161. }
  162. if !FileManager.default.fileExists(atPath: NSTemporaryDirectory() + "/PDFMasterTest") {
  163. try?FileManager.default.createDirectory(atPath: NSTemporaryDirectory() + "/PDFMasterTest", withIntermediateDirectories: true)
  164. }
  165. if FileManager.default.fileExists(atPath: saveFilePath) {
  166. try?FileManager.default.removeItem(atPath: saveFilePath)
  167. }
  168. return saveFilePath
  169. }
  170. /**
  171. 获取pages
  172. @param type 页面类型
  173. @param contentType annoation类型
  174. @param selectPages 当type 为custom时 传入选中page的下标
  175. */
  176. static func creatDocument(_ url: URL) -> CPDFDocument {
  177. if FileManager.default.fileExists(atPath: NSTemporaryDirectory() + "/PDFMasterTest") {
  178. try?FileManager.default.createDirectory(atPath: NSTemporaryDirectory() + "/PDFMasterTest", withIntermediateDirectories: true)
  179. }
  180. let document = CPDFDocument(url: url)!
  181. // document.importPages(IndexSet(integer: 0), from: document, at: 0)
  182. let count = document.pageCount
  183. for _ in 0...(count - 1) {
  184. document.removePage(at: 0)
  185. }
  186. return document
  187. }
  188. /**
  189. 获取pages
  190. @param type 页面类型
  191. @param contentType annoation类型
  192. @param selectPages 当type 为custom时 传入选中page的下标
  193. */
  194. func fetchPages(_ documentURL: URL, _ pageModel: KMPrintPageModel) -> [KMPrintDrawPage] {
  195. let document = PDFDocument.init(url: documentURL)!
  196. var pageIndexs: [Int] = []
  197. let range = pageModel.range
  198. let contentType = pageModel.contentType
  199. let reversePrintOrder = range.reversePrintOrder
  200. switch range.type {
  201. case .allPage:
  202. for index in 0...document.pageCount - 1 {
  203. pageIndexs.append(index)
  204. }
  205. case .evenPage:
  206. for index in 0...document.pageCount - 1 {
  207. if index % 2 == 0 {
  208. pageIndexs.append(index)
  209. }
  210. }
  211. case .oddPage:
  212. for index in 0...document.pageCount - 1 {
  213. if index % 2 != 0 {
  214. pageIndexs.append(index)
  215. }
  216. }
  217. case .currentPage:
  218. pageIndexs.append(0)
  219. case .custom:
  220. pageIndexs.append(0)
  221. default:
  222. pageIndexs.append(0)
  223. }
  224. var pagesArray: [KMPrintDrawPage] = []
  225. for index in pageIndexs {
  226. let page = document.page(at: index)!
  227. let drawPage = KMPrintDrawPage()
  228. drawPage.page = page
  229. self.dealPageContent(contentType, [drawPage])
  230. if reversePrintOrder {
  231. pagesArray.insert(drawPage, at: 0)
  232. } else {
  233. pagesArray.append(drawPage)
  234. }
  235. }
  236. return pagesArray
  237. }
  238. /**
  239. 处理page annoation 内容
  240. @param contentType annoation类型
  241. @param pages page
  242. */
  243. func dealPageContent (_ contentType: KMPrintContentType, _ pages: [KMPrintDrawPage]) -> Void {
  244. for page in pages {
  245. let annoations: [PDFAnnotation] = page.page.annotations
  246. //内容处理
  247. switch contentType {
  248. case .document:
  249. for annoation in annoations {
  250. annoation.page!.removeAnnotation(annoation)
  251. }
  252. case .documentAndStamp:
  253. for annoation in annoations {
  254. if !self.isAnnoationStamp(type: annoation.type!) {
  255. annoation.page!.removeAnnotation(annoation)
  256. }
  257. }
  258. case .documentAndMarkup:
  259. for annoation in annoations {
  260. if !self.isAnnoationMarkup(type: annoation.type!) {
  261. annoation.page!.removeAnnotation(annoation)
  262. }
  263. }
  264. case .documentAndForm:
  265. for annoation in annoations {
  266. if !self.isAnnoationForm(type: annoation.type!) {
  267. annoation.page!.removeAnnotation(annoation)
  268. }
  269. }
  270. default:
  271. KMPrint("未找到")
  272. break
  273. }
  274. }
  275. }
  276. /**
  277. @abstract 获取context
  278. @param size纸张大小
  279. */
  280. func createContext(_ saveFilePath: String, _ size: CGSize) -> CGContext {
  281. var mediaBox: CGRect = NSMakeRect(0, 0, size.width, size.height)
  282. let url = CFURLCreateWithFileSystemPath(nil, saveFilePath as CFString, .cfurlposixPathStyle, false)
  283. let content: CGContext = CGContext.init(url!, mediaBox: &mediaBox, nil)!
  284. return content
  285. }
  286. /**
  287. @abstract 绘制page
  288. @param context
  289. @pages page数组 [CPDFPage]
  290. */
  291. func drawPageToContext(_ context: CGContext, _ pages: [KMPrintDrawPage], _ data: KMPrintModel, _ drawPageRect: CGRect = NSZeroRect) {
  292. //左下角有坐标系原点
  293. /**
  294. paper
  295. */
  296. let paperSize: CGSize = self.fetchPaperSize(data.paper)//纸张大小
  297. let paperItemSize: CGSize = self.fetchPaperItemSize(data.paper) //页面paper大小(去除边框)
  298. let paperInset: NSEdgeInsets = data.paper.info.inset //绘制paper大小
  299. let border: Bool = true //是否存在边框
  300. /**
  301. page
  302. */
  303. let pageOrder: KMPrintPageOperation.Multipage.Order = .horizontal //页面顺序
  304. let pageSize: CGSize = self.fetchPageItemSize(data.page, paperItemSize) //page大小
  305. let showModel: KMPrintPageOperation.Size = self.fetchShowModel(data.page)
  306. let autoRotate = self.fetchAutoRotate(data.page)
  307. let autoSize: Bool = self.fetchAutoSize(data.page)
  308. //行列
  309. let columnAndRow = self.fetchPageColumnAndRow(data.page) //行 列数量
  310. let columnAndRowSpace = CGPoint(x: 2, y: 2) //行 列之间的空间
  311. for i in 0...Int(columnAndRow.x) - 1 {
  312. for j in 0...(Int(columnAndRow.y) - 1) {
  313. let index = j + i * Int(columnAndRow.x)
  314. if index < pages.count {
  315. //参数
  316. let page: KMPrintDrawPage = pages[index]
  317. let rect = page.showRect
  318. //裁剪当前Page
  319. page.page.setBounds(page.cropRect, for: .cropBox)
  320. let pageItemSize = rect.size
  321. let rotate = page.page.rotation
  322. var scale = self.fetchPageScale(page, pageSize, autoRotate, autoSize)
  323. if data.page.operation.type == .size {
  324. if showModel.model == .custom {
  325. scale *= showModel.scale
  326. } else if showModel.model == .full {
  327. scale = 1
  328. }
  329. } else if (data.page.operation.type == .poster) {
  330. if (data.page.operation.poster.type == .tile) {
  331. scale = data.page.operation.poster.scale
  332. }
  333. }
  334. //当前item的自身中心点
  335. let center = CGPoint(x: (pageSize.width - pageItemSize.width * scale) / 2.0 ,
  336. y: (pageSize.height - pageItemSize.height * scale) / 2.0)
  337. var origin = rect.origin
  338. //多页Page自动旋转
  339. if autoSize {
  340. switch pageOrder {
  341. case .horizontal:
  342. origin.x = (pageSize.width + columnAndRowSpace.x) * CGFloat(j) + paperInset.left + center.x
  343. //页面内容高度 + 下边的行间距 - 第几个cell的高度 +居中
  344. origin.y = paperSize.height - (pageSize.height + columnAndRowSpace.y) * (CGFloat(i) + 1) + center.y + columnAndRowSpace.y
  345. case .horizontalReverseSequence:
  346. origin.x = paperSize.width - (pageSize.width + columnAndRowSpace.x) * (CGFloat(j) + 1) + center.x + columnAndRowSpace.x
  347. origin.y = paperSize.height - (pageSize.height + columnAndRowSpace.y) * (CGFloat(i) + 1) + center.y + paperInset.bottom + columnAndRowSpace.y
  348. case .vertical:
  349. origin.x = (pageSize.width + columnAndRowSpace.x) * CGFloat(i) + paperInset.left + center.x
  350. origin.y = paperSize.height - (pageSize.height + columnAndRowSpace.y) * (CGFloat(j) + 1) + center.y + paperInset.bottom + columnAndRowSpace.y
  351. case .verticalReverseSequence:
  352. origin.x = paperSize.width - (pageSize.width + columnAndRowSpace.x) * (CGFloat(i) + 1) + center.x + columnAndRowSpace.x
  353. origin.y = paperSize.height - (pageSize.height + columnAndRowSpace.y) * (CGFloat(j) + 1) + center.y + paperInset.bottom + columnAndRowSpace.y
  354. default:
  355. KMPrint("未找到")
  356. break
  357. }
  358. }
  359. NSGraphicsContext.saveGraphicsState()
  360. //平移
  361. context.translateBy(x: origin.x, y: origin.y)
  362. //缩放
  363. context.scaleBy(x: CGFloat(scale), y: CGFloat(scale))
  364. page.page.draw(with: PDFDisplayBox.cropBox, to: context)
  365. // page.transform(context, for: PDFDisplayBox.cropBox)
  366. if border {
  367. var dirtyRect = rect
  368. if rotate == 90 ||
  369. rotate == 270 {
  370. dirtyRect = NSMakeRect(dirtyRect.origin.x, dirtyRect.origin.y, dirtyRect.size.height, dirtyRect.size.width)
  371. }
  372. context.addRect(dirtyRect)
  373. context.setStrokeColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
  374. context.strokePath()
  375. }
  376. NSGraphicsContext.restoreGraphicsState()
  377. // page.setBounds(NSRect(x: 0, y: 0, width: pageRect.size.width, height: pageRect.size.height), for: .cropBox)
  378. }
  379. }
  380. }
  381. }
  382. }
  383. protocol KMPrintPresenterPage {}
  384. extension KMPrintPresenter: KMPrintPresenterPage {
  385. /**
  386. 获取pages
  387. @param type 页面类型
  388. @param contentType annoation类型
  389. @param selectPages 当type 为custom时 传入选中page的下标
  390. */
  391. func fetchPageCropRect(_ paperSize: CGSize, _ index: Int, _ pageModel: KMPrintPageModel, _ page: KMPrintDrawPage) -> CGRect {
  392. var newRect = page.page.bounds(for: .cropBox)
  393. if (pageModel.operation.type == .poster) {
  394. let originSize = newRect.size
  395. var cropPoint = CGPoint(x: 1, y: 1)
  396. var scale = 1.0
  397. if (pageModel.operation.poster.type == .tile) {
  398. cropPoint = pageModel.operation.poster.tilePoint
  399. // scale = pageModel.operation.poster.scale
  400. } else if (pageModel.operation.poster.type == .breakUp) {
  401. cropPoint = pageModel.operation.poster.pageOfPaper.point
  402. }
  403. let width = originSize.width / cropPoint.x
  404. let height = originSize.height / cropPoint.y
  405. let column: Int = Int(cropPoint.x) //行
  406. let row: Int = Int(cropPoint.y) //列
  407. for i in 0...column - 1 {
  408. for j in 0...row - 1 {
  409. if i + j + i * (row - 1) == index {
  410. newRect.origin.x = CGFloat(i) * width * scale
  411. newRect.origin.y = ((originSize.height - CGFloat(j) * height - height)) * scale
  412. newRect.size.width = width * scale
  413. newRect.size.height = height * scale
  414. return newRect
  415. }
  416. }
  417. }
  418. }
  419. return newRect
  420. }
  421. func fetchPageShowRect(_ paperSize: CGSize, _ index: Int, _ pageModel: KMPrintPageModel, _ page: KMPrintDrawPage) -> CGRect {
  422. var newRect = NSZeroRect
  423. var pageRect = CGRect(x: 0, y: 0, width: paperSize.width, height: paperSize.height)
  424. if (pageModel.operation.type == .poster) {
  425. let pageSize = page.page.bounds(for: .cropBox)
  426. var cropPoint = CGPoint(x: 1, y: 1)
  427. var scale = 1.0
  428. if (pageModel.operation.poster.type == .tile) {
  429. cropPoint = pageModel.operation.poster.tilePoint
  430. scale = pageModel.operation.poster.scale
  431. } else if (pageModel.operation.poster.type == .breakUp) {
  432. cropPoint = pageModel.operation.poster.pageOfPaper.point
  433. }
  434. if (cropPoint.x == 1 && cropPoint.y == 1) {
  435. } else {
  436. let originPaperSize = CGSize(width: paperSize.width * cropPoint.x, height: paperSize.height * cropPoint.y)
  437. let pageWidth = pageSize.width * scale
  438. let pageHeight = pageSize.height * scale
  439. let pageOrigin = CGPoint(x: (originPaperSize.width - pageWidth) / 2, y: (originPaperSize.height - pageHeight) / 2)
  440. let width = originPaperSize.width / cropPoint.x
  441. let height = originPaperSize.height / cropPoint.y
  442. let column: Int = Int(cropPoint.x) //行
  443. let row: Int = Int(cropPoint.y) //列
  444. for i in 0...column - 1 {
  445. for j in 0...row - 1 {
  446. if i + j + i * (row - 1) == index {
  447. newRect.origin.x = CGFloat(i) * width
  448. newRect.origin.y = ((originPaperSize.height - CGFloat(j) * height - height))
  449. newRect.size.width = width
  450. newRect.size.height = height
  451. if (pageOrigin.x > newRect.origin.x) {
  452. pageRect.origin.x = pageOrigin.x
  453. pageRect.size.width = newRect.size.width - pageOrigin.x
  454. } else if (originPaperSize.width - pageOrigin.x > newRect.origin.x) {
  455. pageRect.origin.x = 0
  456. pageRect.size.width = newRect.size.width - pageOrigin.x
  457. } else {
  458. pageRect.origin.x = 0
  459. pageRect.size.width = newRect.size.width
  460. }
  461. if (originPaperSize.height - pageOrigin.y < newRect.origin.y + newRect.size.height) {
  462. pageRect.origin.y = 0
  463. pageRect.size.height = newRect.size.height - pageOrigin.y
  464. } else if (pageOrigin.y > newRect.origin.y) {
  465. pageRect.origin.y = pageOrigin.y
  466. pageRect.size.height = newRect.size.height - pageOrigin.y
  467. } else {
  468. pageRect.origin.y = newRect.origin.y - pageOrigin.y
  469. pageRect.size.height = newRect.size.height
  470. }
  471. return pageRect
  472. }
  473. }
  474. }
  475. }
  476. }
  477. return pageRect
  478. }
  479. func fetchPageColumnAndRow(_ pageModel: KMPrintPageModel) -> CGPoint {
  480. var point = NSZeroPoint
  481. let pageOrder: KMPrintPageOperation.Multipage.Order = pageModel.operation.multipage.orderType //页面顺序
  482. switch pageModel.operation.type {
  483. case .multipage:
  484. point.x = pageModel.operation.multipage.pageOfPaper.point.x
  485. point.y = pageModel.operation.multipage.pageOfPaper.point.y
  486. default:
  487. point.x = 1
  488. point.y = 1
  489. }
  490. //如果是横向参数需切换
  491. if pageOrder == .horizontal ||
  492. pageOrder == .horizontalReverseSequence {
  493. let temp = point.x
  494. point.x = point.y
  495. point.y = temp
  496. }
  497. return point
  498. }
  499. func fetchAutoRotate(_ pageModel: KMPrintPageModel) -> Bool {
  500. var autoRotate = false
  501. switch pageModel.operation.type {
  502. case .multipage:
  503. autoRotate = pageModel.operation.multipage.isAutoRotate
  504. case .pamphlet:
  505. autoRotate = pageModel.operation.pamphlet.isAutoRotate
  506. default:
  507. autoRotate = false
  508. }
  509. return autoRotate
  510. }
  511. func fetchAutoSize(_ pageModel: KMPrintPageModel) -> Bool {
  512. var autoSize = false
  513. switch pageModel.operation.type {
  514. case .size:
  515. autoSize = true
  516. default:
  517. autoSize = false
  518. }
  519. return autoSize
  520. }
  521. func fetchShowModel(_ pageModel: KMPrintPageModel) -> KMPrintPageOperation.Size {
  522. var model = KMPrintPageOperation.Size()
  523. switch pageModel.operation.type {
  524. case .size:
  525. model = pageModel.operation.size
  526. default:
  527. model = KMPrintPageOperation.Size()
  528. }
  529. return model
  530. }
  531. func fetchPageItemSize(_ pageModel: KMPrintPageModel, _ paperSize: CGSize) -> CGSize {
  532. var size = NSZeroSize
  533. let columnAndRow = self.fetchPageColumnAndRow(pageModel) //行 列数量
  534. let columnAndRowSpace = CGPoint(x: 2, y: 2) //行 列之间的空间
  535. //page大小
  536. size = CGSize(width: (paperSize.width - CGFloat((columnAndRow.x - 1)) * CGFloat(columnAndRowSpace.x)) / columnAndRow.x,
  537. height: (paperSize.height - CGFloat((columnAndRow.y - 1)) * CGFloat(columnAndRowSpace.y)) / columnAndRow.y)
  538. return size
  539. }
  540. func fetchPageScale(_ page: KMPrintDrawPage, _ pageItemSize: CGSize, _ autoRotate: Bool, _ autoSize: Bool) -> CGFloat {
  541. var scale = 1.0
  542. let originSize = page.page.bounds(for: .cropBox)
  543. var rotate = page.page.rotation
  544. //取出page横竖时能显示的最大Rect
  545. if autoRotate {
  546. //page旋转度数为0度或者180度时,在指定的大小内,能显示的比例
  547. let scale1 = min(pageItemSize.width / originSize.width, pageItemSize.height / originSize.height)
  548. //page旋转度数为90度或者270度时,在指定的大小内,能显示的比例
  549. let scale2 = min(pageItemSize.width / originSize.height, pageItemSize.height / originSize.width)
  550. scale = max(scale1, scale2)
  551. if scale1 > scale2 {
  552. //高为竖直排列时,显示最大,则需将page时90度或者270度需要进行旋转
  553. if rotate == 90 || rotate == 270 {
  554. rotate = rotate - 90
  555. }
  556. } else {
  557. //宽为竖直排列时,显示最大,则需将page时0度或者180度需要进行旋转
  558. if rotate == 0 || rotate == 180 {
  559. rotate = rotate - 90
  560. }
  561. }
  562. } else {
  563. scale = min(pageItemSize.width / originSize.width, pageItemSize.height / originSize.height)
  564. }
  565. if (autoSize) {
  566. } else {
  567. scale = min(scale, 1)
  568. }
  569. return scale
  570. }
  571. func fetchPageRotate(_ page: KMPrintDrawPage, _ pageItemSize: CGSize, _ autoRotate: Bool) -> CGFloat {
  572. var scale = 1.0
  573. let originSize = page.page.bounds(for: .cropBox)
  574. var rotate = page.page.rotation
  575. //取出page横竖时能显示的最大Rect
  576. if autoRotate {
  577. //page旋转度数为0度或者180度时,在指定的大小内,能显示的比例
  578. let scale1 = min(pageItemSize.width / originSize.width, pageItemSize.height / originSize.height)
  579. //page旋转度数为90度或者270度时,在指定的大小内,能显示的比例
  580. let scale2 = min(pageItemSize.width / originSize.height, pageItemSize.height / originSize.width)
  581. scale = max(scale1, scale2)
  582. if scale1 > scale2 {
  583. //高为竖直排列时,显示最大,则需将page时90度或者270度需要进行旋转
  584. if rotate == 90 || rotate == 270 {
  585. rotate = rotate - 90
  586. }
  587. } else {
  588. //宽为竖直排列时,显示最大,则需将page时0度或者180度需要进行旋转
  589. if rotate == 0 || rotate == 180 {
  590. rotate = rotate - 90
  591. }
  592. }
  593. } else {
  594. scale = min(pageItemSize.width / originSize.width, pageItemSize.height / originSize.height)
  595. }
  596. return scale
  597. }
  598. func fetchPageLite(pageModel: KMPrintPageModel) {
  599. }
  600. }
  601. protocol KMPrintPresenterPaper {}
  602. extension KMPrintPresenter: KMPrintPresenterPaper {
  603. /**
  604. 获取每张纸的page
  605. @param type 页面类型
  606. @param contentType annoation类型
  607. @param selectPages 当type 为custom时 传入选中page的下标
  608. */
  609. func fetchPageOfPaper(_ pageModel: KMPrintPageModel) -> Int {
  610. var count = 1
  611. switch pageModel.operation.type {
  612. case .multipage:
  613. count = Int(pageModel.operation.multipage.pageOfPaper.point.x * pageModel.operation.multipage.pageOfPaper.point.y)
  614. case .poster:
  615. count = Int(pageModel.operation.multipage.pageOfPaper.point.x * pageModel.operation.multipage.pageOfPaper.point.y)
  616. default:
  617. count = 1
  618. }
  619. return count
  620. }
  621. /**
  622. 获取总纸张数
  623. @param pages page总数
  624. @param contentType annoation类型
  625. @param selectPages 当type 为custom时 传入选中page的下标
  626. */
  627. func fetchTotalPaperCount (_ paperSize: CGSize, _ pages: [KMPrintDrawPage], _ pageModel: KMPrintPageModel) -> Int {
  628. var count = 1
  629. let pageOfPaper = self.fetchPageOfPaper(pageModel)
  630. switch pageModel.operation.type {
  631. case .multipage:
  632. count = Int(ceilf(Float(pages.count / pageOfPaper)))
  633. case .poster:
  634. if (pageModel.operation.poster.type == .tile) {
  635. //1 2 4 9 16
  636. let scale = pageModel.operation.poster.scale
  637. let pageSize = pages.first?.page.bounds(for: .cropBox).size ?? paperSize
  638. let point = self.fetchPosterPageCount(paperSize: paperSize, pageSize: pageSize, scale: scale)
  639. pageModel.operation.poster.tilePoint = point
  640. count = Int((point.x * point.y)) * pages.count
  641. } else if (pageModel.operation.poster.type == .breakUp) {
  642. count = Int((pageModel.operation.poster.pageOfPaper.point.x * pageModel.operation.poster.pageOfPaper.point.y)) * pages.count
  643. } else {
  644. count = Int(ceilf(Float(pages.count / pageOfPaper)))
  645. }
  646. default:
  647. count = Int(ceilf(Float(pages.count / pageOfPaper)))
  648. }
  649. return count
  650. }
  651. func fetchPosterPageCount(paperSize: CGSize, pageSize: CGSize, scale: CGFloat) -> CGPoint {
  652. var xCount: Int = 1
  653. var yCount: Int = 1
  654. var contain: Bool = true
  655. while (contain) {
  656. if (pageSize.width * scale < CGFloat(xCount) * paperSize.width &&
  657. pageSize.height * scale < CGFloat(yCount) * paperSize.height) {
  658. contain = false
  659. break
  660. }
  661. //增加行数 和 列数
  662. if xCount == yCount {
  663. xCount += 1
  664. } else {
  665. yCount += 1
  666. }
  667. }
  668. return CGPoint(x: xCount, y: yCount)
  669. }
  670. /**
  671. 获取pages
  672. @param type 页面类型
  673. @param contentType annoation类型
  674. @param selectPages 当type 为custom时 传入选中page的下标
  675. */
  676. func fetchPaperSize(_ paperModel: KMPrintPaperModel) -> CGSize {
  677. var paperSize = paperModel.info.size
  678. let direction = paperModel.direction
  679. if direction == .vertical {
  680. paperSize = CGSize(width: paperModel.info.size.width, height: paperModel.info.size.height)
  681. } else if direction == .horizontal {
  682. paperSize = CGSize(width: paperModel.info.size.height, height: paperModel.info.size.width)
  683. }
  684. return paperSize
  685. }
  686. func fetchPaperItemSize(_ paperModel: KMPrintPaperModel) -> CGSize {
  687. var paperSize = self.fetchPaperSize(paperModel)
  688. let paperInset = paperModel.info.inset
  689. paperSize = CGSize(width: paperSize.width - paperInset.left - paperInset.right,
  690. height: paperSize.height - paperInset.bottom - paperInset.top)
  691. return paperSize
  692. }
  693. }
  694. protocol KMPrintPresenterDraw {}
  695. extension KMPrintPresenter: KMPrintPresenterDraw {
  696. // -(void)drawCutMarkContext:(CGContextRef)context contextSize:(CGSize)size
  697. // {
  698. // CGContextSetStrokeColorWithColor(context, [NSColor blackColor].CGColor);
  699. // CGContextSetLineWidth(context, 1.0);
  700. // CGContextSetLineCap(context, kCGLineCapSquare);
  701. //
  702. // if(self.columnIndex == 1 && self.lineIndex == 1) {
  703. // [self drawLeftBottomCutMarks:context size:size];
  704. // [self drawRightTopCutMarks:context size:size];
  705. // [self drawRightBottomCutMarks:context size:size];
  706. // } else if (self.lineIndex == 1 && self.columnIndex == self.vertArray.count) {
  707. // [self drawLeftTopCutMarks:context size:size];
  708. // [self drawRightTopCutMarks:context size:size];
  709. // [self drawRightBottomCutMarks:context size:size];
  710. // } else if (self.columnIndex == 1 && self.lineIndex == self.hourArray.count) {
  711. // [self drawLeftTopCutMarks:context size:size];
  712. // [self drawLeftBottomCutMarks:context size:size];
  713. // [self drawRightBottomCutMarks:context size:size];
  714. // } else if (self.columnIndex == self.vertArray.count && self.hourArray.count == self.lineIndex) {
  715. // [self drawLeftTopCutMarks:context size:size];
  716. // [self drawLeftBottomCutMarks:context size:size];
  717. // [self drawRightTopCutMarks:context size:size];
  718. // } else {
  719. // [self drawLeftTopCutMarks:context size:size];
  720. // [self drawLeftBottomCutMarks:context size:size];
  721. // [self drawRightTopCutMarks:context size:size];
  722. // [self drawRightBottomCutMarks:context size:size];
  723. // }
  724. // //绘制完成
  725. // CGContextStrokePath(context);
  726. // }
  727. //
  728. // //左上角切割标记
  729. // -(void)drawLeftTopCutMarks:(CGContextRef)context size:(CGSize)size
  730. // {
  731. // CGFloat KBlankA4H =size.height;
  732. // CGPoint point_LeftTop = CGPointZero;
  733. //
  734. // if (self.PDFPrint.splitType == kKMPDFPosterSplitType_PageNumber) {
  735. // point_LeftTop.x = self.PDFPrint.fullPageEdgeInsets.left;
  736. // } else {
  737. // point_LeftTop.x = self.PDFPrint.edgeInsets.left;
  738. // }
  739. //
  740. // point_LeftTop.y = KBlankA4H - self.PDFPrint.edgeInsets.top;
  741. //
  742. // CGContextMoveToPoint(context, 10,point_LeftTop.y);
  743. // CGContextAddLineToPoint(context,point_LeftTop.x, point_LeftTop.y);
  744. //
  745. // CGContextMoveToPoint(context, point_LeftTop.x - 10,KBlankA4H -10);
  746. // CGContextAddLineToPoint(context,point_LeftTop.x - 10,point_LeftTop.y);
  747. // }
  748. //
  749. // //右上角切割标记
  750. // -(void)drawRightTopCutMarks:(CGContextRef)context size:(CGSize)size
  751. // {
  752. // CGFloat KBlankA4W =size.width;
  753. // CGFloat KBlankA4H =size.height;
  754. //
  755. // CGPoint point_RightTop = CGPointZero;//右上角
  756. //
  757. // if (self.PDFPrint.splitType == kKMPDFPosterSplitType_PageNumber) {
  758. // point_RightTop.x = KBlankA4W - self.PDFPrint.fullPageEdgeInsets.right;
  759. // point_RightTop.y = KBlankA4H - self.PDFPrint.fullPageEdgeInsets.top;
  760. // } else {
  761. // point_RightTop.x = KBlankA4W - self.PDFPrint.edgeInsets.right;
  762. // point_RightTop.y = KBlankA4H - self.PDFPrint.edgeInsets.top;
  763. // }
  764. //
  765. // CGContextMoveToPoint(context,point_RightTop.x,point_RightTop.y);
  766. // CGContextAddLineToPoint(context,KBlankA4W - 10,point_RightTop.y);
  767. //
  768. // CGContextMoveToPoint(context,point_RightTop.x + 10,KBlankA4H - 10);
  769. // CGContextAddLineToPoint(context,point_RightTop.x + 10,point_RightTop.y);
  770. // }
  771. //
  772. // //左下角切割标记
  773. // -(void)drawLeftBottomCutMarks:(CGContextRef)context size:(CGSize)size
  774. // {
  775. // CGPoint point_LeftBottom = CGPointZero;//左下角
  776. // if (self.PDFPrint.splitType == kKMPDFPosterSplitType_PageNumber) {
  777. // point_LeftBottom.x = self.PDFPrint.fullPageEdgeInsets.left;
  778. // point_LeftBottom.y = self.PDFPrint.fullPageEdgeInsets.bottom;
  779. // } else {
  780. // point_LeftBottom.x = self.PDFPrint.edgeInsets.left;
  781. // point_LeftBottom.y = self.PDFPrint.edgeInsets.bottom;
  782. // }
  783. //
  784. // //左下角
  785. // CGContextMoveToPoint(context, 10,point_LeftBottom.y);
  786. // CGContextAddLineToPoint(context,point_LeftBottom.x, point_LeftBottom.y);
  787. //
  788. // CGContextMoveToPoint(context, point_LeftBottom.x- 10,10);
  789. // CGContextAddLineToPoint(context,point_LeftBottom.x - 10,point_LeftBottom.y);
  790. // }
  791. //
  792. // //右下角切割标记
  793. // -(void)drawRightBottomCutMarks:(CGContextRef)context size:(CGSize)size
  794. // {
  795. // CGFloat KBlankA4W =size.width;
  796. //
  797. // CGPoint point_RightBottom = CGPointZero;//右下角
  798. // if (self.PDFPrint.splitType == kKMPDFPosterSplitType_PageNumber) {
  799. // point_RightBottom.x = KBlankA4W - self.PDFPrint.fullPageEdgeInsets.right;
  800. // point_RightBottom.y = self.PDFPrint.fullPageEdgeInsets.bottom;
  801. // } else {
  802. // point_RightBottom.x = KBlankA4W - self.PDFPrint.edgeInsets.right;
  803. // point_RightBottom.y = self.PDFPrint.edgeInsets.bottom;
  804. // }
  805. //
  806. // CGContextMoveToPoint(context,KBlankA4W - 10,point_RightBottom.y);
  807. // CGContextAddLineToPoint(context,point_RightBottom.x,point_RightBottom.y);
  808. //
  809. // CGContextMoveToPoint(context,point_RightBottom.x+ 10,10);
  810. // CGContextAddLineToPoint(context,point_RightBottom.x+ 10,point_RightBottom.y);
  811. // }
  812. func drawString(_ pageModel: KMPrintPageModel, _ contextSize: CGSize) {
  813. var string = pageModel.operation.poster.tags.first ?? ""
  814. // if string.isEmpty {
  815. // string =
  816. // }
  817. }
  818. //
  819. // - (void)drawLabelTextContextSize:(CGSize)contextSize
  820. // {
  821. // CGFloat KBlankA4W =contextSize.width;
  822. // CGFloat KBlankA4H =contextSize.height;
  823. //
  824. // NSString *contextString = nil;
  825. // if (self.PDFPrint.labelString && self.PDFPrint.labelString.length > 0) {
  826. // contextString = self.PDFPrint.labelString;
  827. // } else {
  828. // NSDate *date = [NSDate date];
  829. // NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
  830. // [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
  831. // contextString = [NSString stringWithFormat:@"(%ld,%ld) %@ %@",self.columnIndex,self.lineIndex,[self.filePath lastPathComponent],[formatter stringFromDate:date]];
  832. // }
  833. //
  834. // CGFloat fontSize = 12.0 * (MAX(KBlankA4W, KBlankA4H)/842);
  835. // NSFont *font = [NSFont systemFontOfSize:fontSize];
  836. // NSColor *color = [NSColor blackColor];
  837. //
  838. // NSSize size = NSZeroSize;
  839. // NSMutableParagraphStyle *style = [[[NSMutableParagraphStyle alloc] init] autorelease];
  840. // [style setAlignment:NSCenterTextAlignment];
  841. // [style setLineBreakMode:NSLineBreakByCharWrapping];
  842. // NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
  843. // [dictionary setObject:style forKey:NSParagraphStyleAttributeName];
  844. // [dictionary setObject:color forKey:NSForegroundColorAttributeName];
  845. // [dictionary setObject:font forKey:NSFontAttributeName];
  846. // size = [contextString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT)
  847. // options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
  848. // attributes:dictionary].size;
  849. //
  850. // if (self.PDFPrint.splitType == kKMPDFPosterSplitType_PageNumber) {
  851. // [contextString drawInRect:CGRectMake(self.PDFPrint.fullPageEdgeInsets.left +10,
  852. // KBlankA4H - self.PDFPrint.fullPageEdgeInsets.top + size.height,
  853. // size.width, size.height)
  854. // withAttributes:dictionary];
  855. // } else {
  856. // [contextString drawInRect:CGRectMake(self.PDFPrint.edgeInsets.left +10,
  857. // KBlankA4H - self.PDFPrint.edgeInsets.top + size.height,
  858. // size.width, size.height)
  859. // withAttributes:dictionary];
  860. // }
  861. //
  862. // }
  863. }
  864. protocol KMPrintPresenterPrivate {}
  865. extension KMPrintPresenter: KMPrintPresenterPrivate {
  866. func isAnnoationStamp(type: String) -> Bool {
  867. let annotationStamp: [String] = ["Square"]
  868. return annotationStamp.contains(type)
  869. }
  870. func isAnnoationMarkup(type: String) -> Bool {
  871. let annotationStamp: [String] = ["Widget", "Freehand"]
  872. return annotationStamp.contains(type)
  873. }
  874. func isAnnoationForm(type: String) -> Bool {
  875. let annotationStamp: [String] = ["FreeText"]
  876. return annotationStamp.contains(type)
  877. }
  878. }
  879. protocol KMPrintPresenterProtocol: NSObject {
  880. }
  881. ///**
  882. // @abstract 获取context
  883. // @param size纸张大小
  884. // */
  885. //func createContext(_ saveFilePath: String, size: CGSize) -> CGContext {
  886. // let s = CGSize(width: nearbyint(size.width), height: nearbyint(size.height))
  887. // let rep = NSBitmapImageRep.init(bitmapDataPlanes: nil,
  888. // pixelsWide: Int(s.width),
  889. // pixelsHigh: Int(s.height),
  890. // bitsPerSample: 8,
  891. // samplesPerPixel: 4,
  892. // hasAlpha: true,
  893. // isPlanar: false,
  894. // colorSpaceName: NSColorSpaceName.calibratedRGB,
  895. // bytesPerRow: 0,
  896. // bitsPerPixel: 0)!
  897. // let context: CGContext = NSGraphicsContext.init(bitmapImageRep: rep)!.cgContext
  898. // return context
  899. //}