// // KMPDFThumbnialPageView.swift // PDF Master // // 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? override init() { super.init() 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) 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) // 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() 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 } }