KMPDFThumbnialPageView.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // KMPDFThumbnialPageView.swift
  3. // PDF Master
  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. override init() {
  22. super.init()
  23. self.initDefaultValue()
  24. }
  25. required init?(coder: NSCoder) {
  26. super.init(coder: coder)
  27. self.initDefaultValue()
  28. }
  29. public func initDefaultValue() {
  30. self.levelsOfDetail = 2
  31. self.levelsOfDetailBias = 3
  32. self.tileSize = CGSize(width: 1024, height: 1024)
  33. }
  34. override func draw(in ctx: CGContext) {
  35. super.draw(in: ctx)
  36. if (self.page != nil) {
  37. // if let data = self.page?.pageEditImage {
  38. // ctx.saveGState()
  39. // var bounds = self.page!.bounds
  40. // let transform = self.page!.transform()
  41. // bounds = bounds.applying(transform);
  42. //
  43. // let viewWidth: CGFloat = NSWidth(self.bounds)
  44. // let viewHeight: CGFloat = NSHeight(self.bounds)
  45. //
  46. // if (self.page!.rotation == -90 || self.page!.rotation == -270) {
  47. // let height = bounds.size.height
  48. // bounds.size.height = bounds.size.width
  49. // bounds.size.width = height
  50. // }
  51. //
  52. // ctx.scaleBy(x: viewWidth / bounds.size.width,
  53. // y: viewHeight / bounds.size.height);
  54. // let bitmap = NSBitmapImageRep(data: data.tiffRepresentation!)
  55. // ctx.draw((bitmap?.cgImage)!, in: NSMakeRect(0, 0, viewWidth, viewHeight))
  56. // ctx.restoreGState()
  57. // self.contents = bitmap?.cgImage
  58. // } else {
  59. ctx.saveGState();
  60. var bounds = self.page!.bounds
  61. let transform = self.page!.transform()
  62. bounds = bounds.applying(transform);
  63. let viewWidth: CGFloat = NSWidth(self.bounds)
  64. let viewHeight: CGFloat = NSHeight(self.bounds)
  65. if (self.page!.rotation == -90 || self.page!.rotation == -270) {
  66. let height = bounds.size.height
  67. bounds.size.height = bounds.size.width
  68. bounds.size.width = height
  69. }
  70. ctx.scaleBy(x: viewWidth / bounds.size.width,
  71. y: viewHeight / bounds.size.height);
  72. self.page!.drawEdit(with: .cropBox, to: ctx)
  73. ctx.restoreGState();
  74. if let _image = ctx.makeImage() {
  75. let image = NSImage(cgImage: _image, size: NSSize(width: viewWidth, height: viewHeight))
  76. self.page?.pageEditImage = image
  77. }
  78. // }
  79. }
  80. }
  81. }
  82. class KMPDFThumbnialPageView: NSView {
  83. internal var _page: CPDFPage?
  84. var page: CPDFPage? {
  85. get {
  86. self._page
  87. }
  88. set {
  89. self._page = newValue
  90. if pageLayer != nil {
  91. pageLayer.contents = nil
  92. pageLayer.delegate = nil
  93. pageLayer.removeFromSuperlayer()
  94. }
  95. pageLayer = KMPDFThumbnialPageLayer()
  96. pageLayer.frame = self.bounds
  97. pageLayer.page = newValue
  98. self.layer?.addSublayer(pageLayer)
  99. // if let image = self._page?.pageEditImage {
  100. // if (self.pageImageIv.superview == nil) {
  101. // self.addSubview(self.pageImageIv)
  102. // }
  103. // self.pageImageIv.image = image
  104. // self.pageImageIv.imageScaling = .scaleAxesIndependently
  105. // self.pageImageIv.translatesAutoresizingMaskIntoConstraints = false
  106. // self.pageImageIv.frame = self.bounds
  107. // self.pageImageIv.isHidden = false
  108. //// self.pageImageIv.image = image
  109. // } else {
  110. // self.pageImageIv.isHidden = true
  111. // }
  112. }
  113. }
  114. var pageLayer: KMPDFThumbnialPageLayer!
  115. var pageImageIv = NSImageView()
  116. override init(frame frameRect: NSRect) {
  117. super.init(frame: frameRect)
  118. self.wantsLayer = true
  119. self.layer?.backgroundColor = NSColor.white.cgColor
  120. self.shadow = NSShadow()
  121. self.layer?.shadowColor = NSColor.black.cgColor
  122. self.layer?.shadowOpacity = 0.2
  123. self.layer?.shadowRadius = 3
  124. self.layer?.shadowOffset = CGSize(width: 0, height: -3)
  125. if (pageLayer != nil) {
  126. pageLayer.frame = self.bounds
  127. }
  128. self.pageImageIv.frame = self.bounds
  129. }
  130. required init?(coder: NSCoder) {
  131. super.init(coder: coder)
  132. }
  133. override func layout() {
  134. super.layout()
  135. if (pageLayer != nil) {
  136. pageLayer.frame = self.bounds
  137. pageLayer.setNeedsDisplay()
  138. }
  139. self.pageImageIv.frame = self.bounds
  140. }
  141. }