123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- //
- // KMPDFThumbnialPageView.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2022/11/24.
- //
- import Cocoa
- extension CPDFPage {
- private static var _pageEditImageKey = "KMPageEditImageKey"
- var pageEditImage: NSImage? {
- get {
- return objc_getAssociatedObject(self, &CPDFPage._pageEditImageKey) as? NSImage
- }
- set {
- objc_setAssociatedObject(self, &CPDFPage._pageEditImageKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
- }
- }
- }
- class KMPDFThumbnialPageLayer: CATiledLayer {
- var page: CPDFPage?
-
- var isThumb = false
-
- override init() {
- super.init()
-
- self.initDefaultValue()
- }
-
- override init(layer: Any) {
- super.init(layer: layer)
-
- self.initDefaultValue()
- }
-
- required init?(coder: NSCoder) {
- super.init(coder: coder)
-
- self.initDefaultValue()
- }
-
- public func initDefaultValue() {
- self.levelsOfDetail = 2
- self.levelsOfDetailBias = 3
- self.tileSize = CGSize(width: 1024, height: 1024)
- }
-
- override func draw(in ctx: CGContext) {
- super.draw(in: ctx)
-
- if (self.page != nil) {
- // if let data = self.page?.pageEditImage {
- // ctx.saveGState()
- // var bounds = self.page!.bounds
- // let transform = self.page!.transform()
- // bounds = bounds.applying(transform);
- //
- // let viewWidth: CGFloat = NSWidth(self.bounds)
- // let viewHeight: CGFloat = NSHeight(self.bounds)
- //
- // if (self.page!.rotation == -90 || self.page!.rotation == -270) {
- // let height = bounds.size.height
- // bounds.size.height = bounds.size.width
- // bounds.size.width = height
- // }
- //
- // ctx.scaleBy(x: viewWidth / bounds.size.width,
- // y: viewHeight / bounds.size.height);
- // let bitmap = NSBitmapImageRep(data: data.tiffRepresentation!)
- // ctx.draw((bitmap?.cgImage)!, in: NSMakeRect(0, 0, viewWidth, viewHeight))
- // ctx.restoreGState()
- // self.contents = bitmap?.cgImage
- // } else {
- ctx.saveGState();
-
- var bounds = self.page!.bounds
- let transform = self.page!.transform()
- bounds = bounds.applying(transform);
-
- let viewWidth: CGFloat = NSWidth(self.bounds)
- let viewHeight: CGFloat = NSHeight(self.bounds)
-
- if (self.page!.rotation == -90 || self.page!.rotation == -270) {
- let height = bounds.size.height
- bounds.size.height = bounds.size.width
- bounds.size.width = height
- }
-
- ctx.scaleBy(x: viewWidth / bounds.size.width,
- y: viewHeight / bounds.size.height);
- // self.page!.drawEdit(with: .cropBox, to: ctx)
- // self.page?.draw(with: .cropBox, to: ctx)
- // if let tiffData = (self.page?.thumbnail(of: self.page?.size ?? .zero))?.tiffRepresentation {
- // if let tiffData = self.page?.PDFListViewTIFFData(for: self.page?.bounds ?? .zero) {
- // let image = NSImage(data: tiffData)
- // image?.draw(in: ctx as! NSRect)
- // image?.draw(in: bounds)
- // let bitmap = NSBitmapImageRep(data: tiffData)
- // ctx.draw((bitmap?.cgImage)!, in: NSMakeRect(0, 0, bounds.size.width, bounds.size.height))
- // ctx.restoreGState();
- // self.contents = bitmap?.cgImage
- // } else {
- // KMPrint("km_log_drawEdit \(self.isThumb) \(self.page)")
- // km_synchronized(self.page!.document) {
- self.page!.drawEdit(with: .cropBox, to: ctx)
- // }
- // self.page!.drawEdit(with: .cropBox, to: ctx)
- ctx.restoreGState();
- // }
- // ctx.restoreGState();
-
- if let _image = ctx.makeImage() {
- let image = NSImage(cgImage: _image, size: NSSize(width: viewWidth, height: viewHeight))
- self.page?.pageEditImage = image
- }
- // }
- }
- }
- }
- class KMPDFThumbnialPageView: NSView {
- internal var _page: CPDFPage?
- var page: CPDFPage? {
- get {
- self._page
- }
- set {
- self._page = newValue
-
- if pageLayer != nil {
- pageLayer.contents = nil
- pageLayer.delegate = nil
- pageLayer.removeFromSuperlayer()
- }
-
- pageLayer = KMPDFThumbnialPageLayer()
- pageLayer.frame = self.bounds
- pageLayer.page = newValue
- self.layer?.addSublayer(pageLayer)
- self.pageLayer.isThumb = self.isThumb
-
- // if let image = self._page?.pageEditImage {
- // if (self.pageImageIv.superview == nil) {
- // self.addSubview(self.pageImageIv)
- // }
- // self.pageImageIv.image = image
- // self.pageImageIv.imageScaling = .scaleAxesIndependently
- // self.pageImageIv.translatesAutoresizingMaskIntoConstraints = false
- // self.pageImageIv.frame = self.bounds
- // self.pageImageIv.isHidden = false
- //// self.pageImageIv.image = image
- // } else {
- // self.pageImageIv.isHidden = true
- // }
- }
- }
-
- var pageLayer: KMPDFThumbnialPageLayer!
- var pageImageIv = NSImageView()
-
- var isThumb = false
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
-
- self.wantsLayer = true
- self.layer?.backgroundColor = NSColor.white.cgColor
-
- self.shadow = NSShadow()
- self.layer?.shadowColor = NSColor.black.cgColor
- self.layer?.shadowOpacity = 0.2
- self.layer?.shadowRadius = 3
- self.layer?.shadowOffset = CGSize(width: 0, height: -3)
-
- if (pageLayer != nil) {
- pageLayer.frame = self.bounds
- }
- self.pageImageIv.frame = self.bounds
- }
-
- required init?(coder: NSCoder) {
- super.init(coder: coder)
- }
-
- override func layout() {
- super.layout()
-
- if (pageLayer != nil) {
- pageLayer.frame = self.bounds
- pageLayer.setNeedsDisplay()
- }
- self.pageImageIv.frame = self.bounds
- }
-
- }
|