KMPDFThumbnialPageView.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // KMPDFThumbnialPageView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/11/24.
  6. //
  7. import Cocoa
  8. extension CPDFPage {
  9. private static var _pageEditImageKey = "KMPageEditImageKey"
  10. var pageEditImage: NSImage? {
  11. get {
  12. return objc_getAssociatedObject(self, &CPDFPage._pageEditImageKey) as? NSImage
  13. }
  14. set {
  15. objc_setAssociatedObject(self, &CPDFPage._pageEditImageKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  16. }
  17. }
  18. }
  19. class KMPDFThumbnialPageLayer: CATiledLayer {
  20. var page: CPDFPage?
  21. var isThumb = false
  22. override init() {
  23. super.init()
  24. self.initDefaultValue()
  25. }
  26. override init(layer: Any) {
  27. super.init(layer: layer)
  28. self.initDefaultValue()
  29. }
  30. required init?(coder: NSCoder) {
  31. super.init(coder: coder)
  32. self.initDefaultValue()
  33. }
  34. public func initDefaultValue() {
  35. self.levelsOfDetail = 2
  36. self.levelsOfDetailBias = 3
  37. self.tileSize = CGSize(width: 1024, height: 1024)
  38. }
  39. override func draw(in ctx: CGContext) {
  40. super.draw(in: ctx)
  41. if (self.page != nil) {
  42. // if let data = self.page?.pageEditImage {
  43. // ctx.saveGState()
  44. // var bounds = self.page!.bounds
  45. // let transform = self.page!.transform()
  46. // bounds = bounds.applying(transform);
  47. //
  48. // let viewWidth: CGFloat = NSWidth(self.bounds)
  49. // let viewHeight: CGFloat = NSHeight(self.bounds)
  50. //
  51. // if (self.page!.rotation == -90 || self.page!.rotation == -270) {
  52. // let height = bounds.size.height
  53. // bounds.size.height = bounds.size.width
  54. // bounds.size.width = height
  55. // }
  56. //
  57. // ctx.scaleBy(x: viewWidth / bounds.size.width,
  58. // y: viewHeight / bounds.size.height);
  59. // let bitmap = NSBitmapImageRep(data: data.tiffRepresentation!)
  60. // ctx.draw((bitmap?.cgImage)!, in: NSMakeRect(0, 0, viewWidth, viewHeight))
  61. // ctx.restoreGState()
  62. // self.contents = bitmap?.cgImage
  63. // } else {
  64. ctx.saveGState();
  65. var bounds = self.page!.bounds
  66. let transform = self.page!.transform()
  67. bounds = bounds.applying(transform);
  68. let viewWidth: CGFloat = NSWidth(self.bounds)
  69. let viewHeight: CGFloat = NSHeight(self.bounds)
  70. if (self.page!.rotation == -90 || self.page!.rotation == -270) {
  71. let height = bounds.size.height
  72. bounds.size.height = bounds.size.width
  73. bounds.size.width = height
  74. }
  75. ctx.scaleBy(x: viewWidth / bounds.size.width,
  76. y: viewHeight / bounds.size.height);
  77. // self.page!.drawEdit(with: .cropBox, to: ctx)
  78. // self.page?.draw(with: .cropBox, to: ctx)
  79. // if let tiffData = (self.page?.thumbnail(of: self.page?.size ?? .zero))?.tiffRepresentation {
  80. // if let tiffData = self.page?.PDFListViewTIFFData(for: self.page?.bounds ?? .zero) {
  81. // let image = NSImage(data: tiffData)
  82. // image?.draw(in: ctx as! NSRect)
  83. // image?.draw(in: bounds)
  84. // let bitmap = NSBitmapImageRep(data: tiffData)
  85. // ctx.draw((bitmap?.cgImage)!, in: NSMakeRect(0, 0, bounds.size.width, bounds.size.height))
  86. // ctx.restoreGState();
  87. // self.contents = bitmap?.cgImage
  88. // } else {
  89. // KMPrint("km_log_drawEdit \(self.isThumb) \(self.page)")
  90. // km_synchronized(self.page!.document) {
  91. self.page!.drawEdit(with: .cropBox, to: ctx)
  92. // }
  93. // self.page!.drawEdit(with: .cropBox, to: ctx)
  94. ctx.restoreGState();
  95. // }
  96. // ctx.restoreGState();
  97. if let _image = ctx.makeImage() {
  98. let image = NSImage(cgImage: _image, size: NSSize(width: viewWidth, height: viewHeight))
  99. self.page?.pageEditImage = image
  100. }
  101. // }
  102. }
  103. }
  104. }
  105. class KMPDFThumbnialPageView: NSView {
  106. internal var _page: CPDFPage?
  107. var page: CPDFPage? {
  108. get {
  109. self._page
  110. }
  111. set {
  112. self._page = newValue
  113. if pageLayer != nil {
  114. pageLayer.contents = nil
  115. pageLayer.delegate = nil
  116. pageLayer.removeFromSuperlayer()
  117. }
  118. pageLayer = KMPDFThumbnialPageLayer()
  119. pageLayer.frame = self.bounds
  120. pageLayer.page = newValue
  121. self.layer?.addSublayer(pageLayer)
  122. self.pageLayer.isThumb = self.isThumb
  123. // if let image = self._page?.pageEditImage {
  124. // if (self.pageImageIv.superview == nil) {
  125. // self.addSubview(self.pageImageIv)
  126. // }
  127. // self.pageImageIv.image = image
  128. // self.pageImageIv.imageScaling = .scaleAxesIndependently
  129. // self.pageImageIv.translatesAutoresizingMaskIntoConstraints = false
  130. // self.pageImageIv.frame = self.bounds
  131. // self.pageImageIv.isHidden = false
  132. //// self.pageImageIv.image = image
  133. // } else {
  134. // self.pageImageIv.isHidden = true
  135. // }
  136. }
  137. }
  138. var pageLayer: KMPDFThumbnialPageLayer!
  139. var pageImageIv = NSImageView()
  140. var isThumb = false
  141. override init(frame frameRect: NSRect) {
  142. super.init(frame: frameRect)
  143. self.wantsLayer = true
  144. self.layer?.backgroundColor = NSColor.white.cgColor
  145. self.shadow = NSShadow()
  146. self.layer?.shadowColor = NSColor.black.cgColor
  147. self.layer?.shadowOpacity = 0.2
  148. self.layer?.shadowRadius = 3
  149. self.layer?.shadowOffset = CGSize(width: 0, height: -3)
  150. if (pageLayer != nil) {
  151. pageLayer.frame = self.bounds
  152. }
  153. self.pageImageIv.frame = self.bounds
  154. }
  155. required init?(coder: NSCoder) {
  156. super.init(coder: coder)
  157. }
  158. override func layout() {
  159. super.layout()
  160. if (pageLayer != nil) {
  161. pageLayer.frame = self.bounds
  162. pageLayer.setNeedsDisplay()
  163. }
  164. self.pageImageIv.frame = self.bounds
  165. }
  166. }