KMPrintPresenter.swift 52 KB

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