KMPrintPresenter.swift 52 KB

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