Revision: 4915
          http://skim-app.svn.sourceforge.net/skim-app/?rev=4915&view=rev
Author:   hofman
Date:     2008-11-04 12:21:00 +0000 (Tue, 04 Nov 2008)

Log Message:
-----------
add convenience methods to create pdf or tiff data for a region on a page to 
avoid code duplication

Modified Paths:
--------------
    trunk/PDFPage_SKExtensions.h
    trunk/PDFPage_SKExtensions.m
    trunk/SKPDFView.m

Modified: trunk/PDFPage_SKExtensions.h
===================================================================
--- trunk/PDFPage_SKExtensions.h        2008-11-04 11:20:56 UTC (rev 4914)
+++ trunk/PDFPage_SKExtensions.h        2008-11-04 12:21:00 UTC (rev 4915)
@@ -69,6 +69,9 @@
 - (NSAttributedString *)thumbnail64Attachment;
 - (NSAttributedString *)thumbnail32Attachment;
 
+- (NSData *)PDFDataForRect:(NSRect)rect;
+- (NSData *)TIFFDataForRect:(NSRect)rect;
+
 - (NSArray *)lineRects;
 
 - (unsigned int)pageIndex;

Modified: trunk/PDFPage_SKExtensions.m
===================================================================
--- trunk/PDFPage_SKExtensions.m        2008-11-04 11:20:56 UTC (rev 4914)
+++ trunk/PDFPage_SKExtensions.m        2008-11-04 12:21:00 UTC (rev 4915)
@@ -51,6 +51,7 @@
 #import "SKCFCallBacks.h"
 #import "NSUserDefaults_SKExtensions.h"
 #import "SKMainWindowController.h"
+#import "NSAffineTransform_SKExtensions.h"
 
 NSString *SKPDFPageBoundsDidChangeNotification = 
@"SKPDFPageBoundsDidChangeNotification";
 
@@ -274,6 +275,73 @@
     return [self thumbnailAttachmentWithSize:32.0];
 }
 
+- (NSData *)PDFDataForRect:(NSRect)rect {
+    NSData *data = [self dataRepresentation];
+    
+    if (NSEqualRects(rect, NSZeroRect))
+        return data;
+    if (NSIsEmptyRect(rect))
+        return nil;
+    
+    if ([self rotation]) {
+        NSAffineTransform *transform = [NSAffineTransform transform];
+        NSRect bounds = [self boundsForBox:kPDFDisplayBoxMediaBox];
+        switch ([self rotation]) {
+            case 90:
+                [transform translateXBy:0.0 yBy:NSWidth(bounds)];
+                break;
+            case 180:
+                [transform translateXBy:NSWidth(bounds) yBy:NSHeight(bounds)];
+                break;
+            case 270:
+                [transform translateXBy:NSHeight(bounds) yBy:0.0];
+                break;
+        }
+        [transform rotateByDegrees:-[self rotation]];
+        rect = [transform transformRect:rect];
+    }
+    
+    PDFDocument *pdfDoc = [[PDFDocument alloc] initWithData:data];
+    PDFPage *page = [pdfDoc pageAtIndex:0];
+    
+    if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_4) {
+        [page setBounds:rect forBox:kPDFDisplayBoxMediaBox];
+        [page setBounds:NSZeroRect forBox:kPDFDisplayBoxCropBox];
+    } else {
+        // setting the media box is buggy on Tiger, see bug # 1928384
+        [page setBounds:rect forBox:kPDFDisplayBoxCropBox];
+    }
+    [page setBounds:NSZeroRect forBox:kPDFDisplayBoxBleedBox];
+    [page setBounds:NSZeroRect forBox:kPDFDisplayBoxTrimBox];
+    [page setBounds:NSZeroRect forBox:kPDFDisplayBoxArtBox];
+    data = [page dataRepresentation];
+    [pdfDoc release];
+    
+    return data;
+}
+
+- (NSData *)TIFFDataForRect:(NSRect)rect {
+    NSImage *pageImage = [self imageForBox:kPDFDisplayBoxMediaBox];
+    
+    if (NSEqualRects(rect, NSZeroRect))
+        return [pageImage TIFFRepresentation];
+    if (NSIsEmptyRect(rect))
+        return nil;
+    
+    NSRect pageBounds = [self boundsForBox:kPDFDisplayBoxMediaBox];
+    NSRect sourceRect = rect;
+    NSRect targetRect = {NSZeroPoint, rect.size};
+    sourceRect.origin.x -= NSMinX(pageBounds);
+    sourceRect.origin.y -= NSMinY(pageBounds);
+    
+    NSImage *image = [[NSImage alloc] initWithSize:targetRect.size];
+    [image lockFocus];
+    [pageImage drawInRect:targetRect fromRect:sourceRect 
operation:NSCompositeCopy fraction:1.0];
+    [image unlockFocus];
+    
+    return [image TIFFRepresentation];
+}
+
 - (NSArray *)lineRects {
     NSMutableArray *lines = [NSMutableArray array];
     PDFSelection *sel = [self selectionForRect:[self 
boundsForBox:kPDFDisplayBoxCropBox]];
@@ -489,52 +557,17 @@
     NSData *boundsData = [args objectForKey:@"Bounds"];
     id asTIFF = [args objectForKey:@"AsTIFF"];
     NSData *data = nil;
-    DescType type = 'PDF ';
+    DescType type = 0;
     
-    if ([asTIFF respondsToSelector:@selector(boolValue)] && [asTIFF 
boolValue]) {
-        NSImage *pageImage = [self imageForBox:kPDFDisplayBoxMediaBox];
-        if (boundsData) {
-            NSRect pageBounds = [self boundsForBox:kPDFDisplayBoxMediaBox];
-            NSRect bounds = [boundsData rectValueAsQDRect];
-            NSRect sourceRect = bounds;
-            NSRect targetRect = {NSZeroPoint, sourceRect.size};
-            NSImage *image = nil;
-            
-            sourceRect.origin.x -= NSMinX(pageBounds);
-            sourceRect.origin.y -= NSMinY(pageBounds);
-            image = [[NSImage alloc] initWithSize:targetRect.size];
-            [image lockFocus];
-            [pageImage drawInRect:targetRect fromRect:sourceRect 
operation:NSCompositeCopy fraction:1.0];
-            [image unlockFocus];
-            data = [image TIFFRepresentation];
-        } else {
-            data = [pageImage TIFFRepresentation];
-        }
-        type = typeTIFF;
+    if ([boundsData isKindOfClass:[NSData class]] == NO || [asTIFF 
respondsToSelector:@selector(boolValue)] == NO)
+        return nil;
+    
+    if ([asTIFF boolValue]) {
+        data = [self TIFFDataForRect:[boundsData rectValueAsQDRect]];
+        type = 'TIFF';
     } else {
-        data = [self dataRepresentation];
-        if (boundsData) {
-            if ([boundsData isKindOfClass:[NSData class]] == NO)
-                return nil;
-            PDFDocument *pdfDoc = [[PDFDocument alloc] initWithData:data];
-            PDFPage *page = [pdfDoc pageAtIndex:0];
-            NSRect bounds = [boundsData rectValueAsQDRect];
-            
-            if (NSIsEmptyRect(bounds))
-                return nil;
-            if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_4) {
-                [page setBounds:bounds forBox:kPDFDisplayBoxMediaBox];
-                [page setBounds:NSZeroRect forBox:kPDFDisplayBoxCropBox];
-            } else {
-                // setting the media box is buggy on Tiger, see bug # 1928384
-                [page setBounds:bounds forBox:kPDFDisplayBoxCropBox];
-            }
-            [page setBounds:NSZeroRect forBox:kPDFDisplayBoxBleedBox];
-            [page setBounds:NSZeroRect forBox:kPDFDisplayBoxTrimBox];
-            [page setBounds:NSZeroRect forBox:kPDFDisplayBoxArtBox];
-            data = [page dataRepresentation];
-            [page release];
-        }
+        data = [self PDFDataForRect:[boundsData rectValueAsQDRect]];
+        type = 'PDF ';
     }
     
     return data ? [NSAppleEventDescriptor descriptorWithDescriptorType:type 
data:data] : nil;

Modified: trunk/SKPDFView.m
===================================================================
--- trunk/SKPDFView.m   2008-11-04 11:20:56 UTC (rev 4914)
+++ trunk/SKPDFView.m   2008-11-04 12:21:00 UTC (rev 4915)
@@ -777,60 +777,12 @@
     
     if (toolMode == SKSelectToolMode && NSIsEmptyRect(selectionRect) == NO && 
selectionPageIndex != NSNotFound) {
         NSRect selRect = NSIntegralRect(selectionRect);
-        NSRect targetRect = selRect;
         PDFPage *page = [self currentSelectionPage];
         
-        if ([page rotation]) {
-            NSAffineTransform *transform = [NSAffineTransform transform];
-            NSRect bounds = [page boundsForBox:kPDFDisplayBoxMediaBox];
-            switch ([page rotation]) {
-                case 90:
-                    [transform translateXBy:0.0 yBy:NSWidth(bounds)];
-                    break;
-                case 180:
-                    [transform translateXBy:NSWidth(bounds) 
yBy:NSHeight(bounds)];
-                    break;
-                case 270:
-                    [transform translateXBy:NSHeight(bounds) yBy:0.0];
-                    break;
-            }
-            [transform rotateByDegrees:-[page rotation]];
-            targetRect = [transform transformRect:targetRect];
-        }
-        
-        PDFDocument *pdfDoc = [[PDFDocument alloc] initWithData:[page 
dataRepresentation]];
-        page = [pdfDoc pageAtIndex:0];
-        if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_4) {
-            [page setBounds:targetRect forBox:kPDFDisplayBoxMediaBox];
-            [page setBounds:NSZeroRect forBox:kPDFDisplayBoxCropBox];
-        } else {
-            // setting the media box is buggy on Tiger, see bug # 1928384
-            [page setBounds:targetRect forBox:kPDFDisplayBoxCropBox];
-        }
-        [page setBounds:NSZeroRect forBox:kPDFDisplayBoxBleedBox];
-        [page setBounds:NSZeroRect forBox:kPDFDisplayBoxTrimBox];
-        [page setBounds:NSZeroRect forBox:kPDFDisplayBoxArtBox];
-        
-        if (pdfData = [page dataRepresentation])
+        if (pdfData = [page PDFDataForRect:selRect])
             [types addObject:NSPDFPboardType];
-        [pdfDoc release];
-        
-        NSRect bounds = [[self currentPage] boundsForBox:[self displayBox]];
-        NSRect sourceRect = selRect;
-        NSImage *pageImage = [[self currentPage] imageForBox:[self 
displayBox]];
-        NSImage *image = nil;
-        
-        sourceRect.origin.x -= NSMinX(bounds);
-        sourceRect.origin.y -= NSMinY(bounds);
-        targetRect.origin = NSZeroPoint;
-        targetRect.size = sourceRect.size;
-        image = [[NSImage alloc] initWithSize:targetRect.size];
-        [image lockFocus];
-        [pageImage drawInRect:targetRect fromRect:sourceRect 
operation:NSCompositeCopy fraction:1.0];
-        [image unlockFocus];
-        if (tiffData = [image TIFFRepresentation])
+        if (tiffData = [page TIFFDataForRect:selRect])
             [types addObject:NSTIFFPboardType];
-        [image release];
         
         /*
          Possible hidden default?  Alternate way of getting a bitmap rep; this 
varies resolution with zoom level, which is very useful if you want to copy a 
single figure or equation for a non-PDF-capable program.  The first copy: 
action has some odd behavior, though (view moves).  Preview produces a fixed 
resolution bitmap for a given selection area regardless of zoom.


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to