|
@@ -1,164 +0,0 @@
|
|
|
-//
|
|
|
-// CPDFPage+PDFListView.m
|
|
|
-// ComPDFKit
|
|
|
-//
|
|
|
-// Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
|
|
|
-//
|
|
|
-// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
|
|
|
-// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
|
|
|
-// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
|
|
|
-// This notice may not be removed from this file.
|
|
|
-//
|
|
|
-
|
|
|
-#import "CPDFPage+PDFListView.h"
|
|
|
-#import "NSShadow+PDFListView.h"
|
|
|
-#import "NSGeometry+PDFListView.h"
|
|
|
-
|
|
|
-@implementation CPDFPage (PDFListView)
|
|
|
-
|
|
|
-- (NSData *)PDFListViewTIFFDataForRect:(NSRect)rect {
|
|
|
- CPDFDisplayBox box = NSEqualRects(rect, [self boundsForBox:CPDFDisplayCropBox]) ? CPDFDisplayCropBox : CPDFDisplayMediaBox;
|
|
|
- NSImage *pageImage = [self PDFListViewThumbnailWithSize:0.0 forBox:box shadowBlurRadius:0.0];
|
|
|
- NSRect bounds = self.bounds;
|
|
|
-
|
|
|
- if (NSEqualRects(rect, NSZeroRect) || NSEqualRects(rect, bounds))
|
|
|
- return [pageImage TIFFRepresentation];
|
|
|
- if (NSIsEmptyRect(rect))
|
|
|
- return nil;
|
|
|
-
|
|
|
- NSAffineTransform *transform = [self PDFListViewAffineTransformForBox:box];
|
|
|
- NSRect sourceRect = CPDFListViewRectFromPoints([transform transformPoint:CPDFListViewBottomLeftPoint(rect)], [transform transformPoint:CPDFListViewTopRightPoint(rect)]);
|
|
|
- NSRect destRect = sourceRect;
|
|
|
- destRect.origin = NSZeroPoint;
|
|
|
-
|
|
|
- NSImage *image = [[NSImage alloc] initWithSize:destRect.size];
|
|
|
- [image lockFocus];
|
|
|
- [pageImage drawInRect:destRect fromRect:sourceRect operation:NSCompositingOperationCopy fraction:1.0];
|
|
|
- [image unlockFocus];
|
|
|
-
|
|
|
- return [image TIFFRepresentation];
|
|
|
-}
|
|
|
-
|
|
|
-- (NSAffineTransform *)PDFListViewAffineTransformForBox:(CPDFDisplayBox)box {
|
|
|
- NSRect bounds = self.bounds;
|
|
|
- NSAffineTransform *transform = [NSAffineTransform transform];
|
|
|
- [transform rotateByDegrees:-[self rotation]];
|
|
|
- switch ([self rotation]) {
|
|
|
- case 0: [transform translateXBy:-NSMinX(bounds) yBy:-NSMinY(bounds)]; break;
|
|
|
- case 90: [transform translateXBy:-NSMaxX(bounds) yBy:-NSMinY(bounds)]; break;
|
|
|
- case 180: [transform translateXBy:-NSMaxX(bounds) yBy:-NSMaxY(bounds)]; break;
|
|
|
- case 270: [transform translateXBy:-NSMinX(bounds) yBy:-NSMaxY(bounds)]; break;
|
|
|
- }
|
|
|
- return transform;
|
|
|
-}
|
|
|
-
|
|
|
-- (NSImage *)PDFListViewThumbnailWithSize:(CGFloat)aSize forBox:(CPDFDisplayBox)box shadowBlurRadius:(CGFloat)shadowBlurRadius {
|
|
|
- NSRect bounds = [self boundsForBox:box];
|
|
|
- NSSize pageSize = bounds.size;
|
|
|
- CGFloat scale = 1.0;
|
|
|
- NSSize thumbnailSize;
|
|
|
- CGFloat shadowOffset = shadowBlurRadius > 0.0 ? - ceil(shadowBlurRadius * 0.75) : 0.0;
|
|
|
- NSRect pageRect = NSZeroRect;
|
|
|
- NSImage *image;
|
|
|
-
|
|
|
- if ([self rotation] % 180 == 90)
|
|
|
- pageSize = NSMakeSize(pageSize.height, pageSize.width);
|
|
|
-
|
|
|
- if (aSize > 0.0) {
|
|
|
- if (pageSize.height > pageSize.width)
|
|
|
- thumbnailSize = NSMakeSize(round((aSize - 2.0 * shadowBlurRadius) * pageSize.width / pageSize.height + 2.0 * shadowBlurRadius), aSize);
|
|
|
- else
|
|
|
- thumbnailSize = NSMakeSize(aSize, round((aSize - 2.0 * shadowBlurRadius) * pageSize.height / pageSize.width + 2.0 * shadowBlurRadius));
|
|
|
- scale = fmax((thumbnailSize.width - 2.0 * shadowBlurRadius) / pageSize.width, (thumbnailSize.height - 2.0 * shadowBlurRadius) / pageSize.height);
|
|
|
- } else {
|
|
|
- thumbnailSize = NSMakeSize(pageSize.width + 2.0 * shadowBlurRadius, pageSize.height + 2.0 * shadowBlurRadius);
|
|
|
- }
|
|
|
-
|
|
|
- pageRect.size = thumbnailSize;
|
|
|
-
|
|
|
- if (shadowBlurRadius > 0.0) {
|
|
|
- pageRect = NSInsetRect(pageRect, shadowBlurRadius, shadowBlurRadius);
|
|
|
- pageRect.origin.y -= shadowOffset;
|
|
|
- }
|
|
|
- if (isnan(thumbnailSize.width) || isnan(thumbnailSize.height)) {
|
|
|
- image = [[NSImage alloc] initWithSize:CGSizeMake(186.0, 256.0)];
|
|
|
- } else {
|
|
|
- image = [[NSImage alloc] initWithSize:thumbnailSize];
|
|
|
- }
|
|
|
-
|
|
|
- [image lockFocus];
|
|
|
-
|
|
|
- CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
|
|
|
-
|
|
|
- [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
|
|
|
-
|
|
|
- [NSGraphicsContext saveGraphicsState];
|
|
|
- [[NSColor whiteColor] setFill];
|
|
|
- if (shadowBlurRadius > 0.0)
|
|
|
- [NSShadow setPDFListViewShadowWithColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.5] blurRadius:shadowBlurRadius yOffset:shadowOffset];
|
|
|
- NSRectFill(pageRect);
|
|
|
- [NSGraphicsContext restoreGraphicsState];
|
|
|
-
|
|
|
- if (fabs(scale - 1.0) > 0.0 || shadowBlurRadius > 0.0) {
|
|
|
- NSAffineTransform *transform = [NSAffineTransform transform];
|
|
|
- if (shadowBlurRadius > 0.0)
|
|
|
- [transform translateXBy:NSMinX(pageRect) yBy:NSMinY(pageRect)];
|
|
|
- [transform scaleBy:scale];
|
|
|
- [transform concat];
|
|
|
- }
|
|
|
-
|
|
|
-// [self drawWithBox:box toContext:context];
|
|
|
- [self drawEditWithBox:box toContext:context];
|
|
|
-
|
|
|
- [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationDefault];
|
|
|
-
|
|
|
- [image unlockFocus];
|
|
|
-
|
|
|
- return image;
|
|
|
-}
|
|
|
-
|
|
|
-@end
|
|
|
-
|
|
|
-@implementation CPDFPage (KMExtension)
|
|
|
-
|
|
|
-- (void)leftRotate {
|
|
|
- NSInteger rotation = self.rotation % 360;
|
|
|
-
|
|
|
- if (rotation == 0) {
|
|
|
- self.rotation = 270;
|
|
|
- } else if (rotation == 90) {
|
|
|
- self.rotation = 0;
|
|
|
- } else if (rotation == 180) {
|
|
|
- self.rotation = 90;
|
|
|
- } else if (rotation == 270) {
|
|
|
- self.rotation = 180;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-- (void)rightRotate {
|
|
|
- NSInteger rotation = self.rotation % 360;
|
|
|
-
|
|
|
- if (rotation == 0) {
|
|
|
- self.rotation = 90;
|
|
|
- } else if (rotation == 90) {
|
|
|
- self.rotation = 180;
|
|
|
- } else if (rotation == 180) {
|
|
|
- self.rotation = 270;
|
|
|
- } else if (rotation == 270) {
|
|
|
- self.rotation = 0;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-- (BOOL)isHorizontal {
|
|
|
- NSInteger rotation = self.rotation % 360;
|
|
|
-
|
|
|
- return labs(rotation) == 90 || labs(self.rotation) == 270;
|
|
|
-}
|
|
|
-
|
|
|
-- (BOOL)isVertical {
|
|
|
- NSInteger rotation = self.rotation % 360;
|
|
|
-
|
|
|
- return rotation == 0 || labs(rotation) == 180;
|
|
|
-}
|
|
|
-
|
|
|
-@end
|