KMPrintPresenter.swift 44 KB

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