Revision: 9422
          http://sourceforge.net/p/skim-app/code/9422
Author:   hofman
Date:     2017-06-01 21:25:06 +0000 (Thu, 01 Jun 2017)
Log Message:
-----------
Use lockfocus/unlockfocus to draw thumbnails and snapshots instead of double 
bitmaps, twice as fast

Modified Paths:
--------------
    trunk/PDFPage_SKExtensions.m
    trunk/SKMainWindowController.m
    trunk/SKSnapshotWindowController.m

Modified: trunk/PDFPage_SKExtensions.m
===================================================================
--- trunk/PDFPage_SKExtensions.m        2017-05-16 21:02:53 UTC (rev 9421)
+++ trunk/PDFPage_SKExtensions.m        2017-06-01 21:25:06 UTC (rev 9422)
@@ -196,36 +196,38 @@
         pageRect.origin.y -= shadowOffset;
     }
     
-    image = [NSImage bitmapImageWithSize:thumbnailSize drawingHandler:^(NSRect 
rect){
-        
-        [[NSGraphicsContext currentContext] 
setImageInterpolation:NSImageInterpolationHigh];
-        
-        [NSGraphicsContext saveGraphicsState];
-        [[PDFView defaultPageBackgroundColor] setFill];
+    image = [[[NSImage alloc] initWithSize:thumbnailSize] autorelease];
+    
+    [image lockFocus];
+    
+    [[NSGraphicsContext currentContext] 
setImageInterpolation:NSImageInterpolationHigh];
+    
+    [NSGraphicsContext saveGraphicsState];
+    [[PDFView defaultPageBackgroundColor] setFill];
+    if (shadowBlurRadius > 0.0)
+        [NSShadow setShadowWithColor:[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)
-            [NSShadow setShadowWithColor:[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]; 
-        
-        if (readingBar) {
-            [self transformContextForBox:box];
-            [readingBar drawForPage:self withBox:box 
inContext:[[NSGraphicsContext currentContext] graphicsPort]];
-        }
-        
-        [[NSGraphicsContext currentContext] 
setImageInterpolation:NSImageInterpolationDefault];
-        
-    }];
+            [transform translateXBy:NSMinX(pageRect) yBy:NSMinY(pageRect)];
+        [transform scaleBy:scale];
+        [transform concat];
+    }
     
+    [self drawWithBox:box]; 
+    
+    if (readingBar) {
+        [self transformContextForBox:box];
+        [readingBar drawForPage:self withBox:box inContext:[[NSGraphicsContext 
currentContext] graphicsPort]];
+    }
+    
+    [[NSGraphicsContext currentContext] 
setImageInterpolation:NSImageInterpolationDefault];
+    
+    [image unlockFocus];
+    
     return image;
 }
 
@@ -294,10 +296,13 @@
     
     NSAffineTransform *transform = [self affineTransformForBox:box];
     NSRect sourceRect = SKRectFromPoints([transform 
transformPoint:SKBottomLeftPoint(rect)], [transform 
transformPoint:SKTopRightPoint(rect)]);
+    NSRect destRect = sourceRect;
+    destRect.origin = NSZeroPoint;
     
-    NSImage *image = [NSImage bitmapImageWithSize:sourceRect.size 
drawingHandler:^(NSRect destRect){
-        [pageImage drawInRect:destRect fromRect:sourceRect 
operation:NSCompositeCopy fraction:1.0];
-    }];
+    NSImage *image = [[[NSImage alloc] initWithSize:destRect.size] 
autorelease];
+    [image lockFocus];
+    [pageImage drawInRect:destRect fromRect:sourceRect 
operation:NSCompositeCopy fraction:1.0];
+    [image unlockFocus];
     
     return [image TIFFRepresentation];
 }

Modified: trunk/SKMainWindowController.m
===================================================================
--- trunk/SKMainWindowController.m      2017-05-16 21:02:53 UTC (rev 9421)
+++ trunk/SKMainWindowController.m      2017-06-01 21:25:06 UTC (rev 9422)
@@ -2571,18 +2571,19 @@
         [emptyPage setBounds:[firstPage boundsForBox:kPDFDisplayBoxMediaBox] 
forBox:kPDFDisplayBoxMediaBox];
         [emptyPage setRotation:[firstPage rotation]];
         NSImage *pageImage = [emptyPage thumbnailWithSize:thumbnailCacheSize 
forBox:[pdfView displayBox]];
-        NSImage *image = [NSImage bitmapImageWithSize:[pageImage size] 
drawingHandler:^(NSRect rect){
-            NSRect imgRect = rect;
-            CGFloat width = 0.8 * fmin(NSWidth(imgRect), NSHeight(imgRect));
-            imgRect = NSInsetRect(imgRect, 0.5 * (NSWidth(imgRect) - width), 
0.5 * (NSHeight(imgRect) - width));
-            [pageImage drawInRect:rect fromRect:NSZeroRect 
operation:NSCompositeCopy fraction:1.0];
-            [[NSImage imageNamed:@"NSApplicationIcon"] drawInRect:imgRect 
fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:0.5];
-            if (isLocked)
-                [[[NSWorkspace sharedWorkspace] 
iconForFileType:NSFileTypeForHFSTypeCode(kLockedBadgeIcon)] drawInRect:imgRect 
fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:0.5];
-        }];
+        NSRect rect = NSZeroRect;
+        rect.size = [pageImage size];
+        CGFloat width = 0.8 * fmin(NSWidth(rect), NSHeight(rect));
+        rect = NSInsetRect(rect, 0.5 * (NSWidth(rect) - width), 0.5 * 
(NSHeight(rect) - width));
         
+        [pageImage lockFocus];
+        [[NSImage imageNamed:@"NSApplicationIcon"] drawInRect:rect 
fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:0.5];
+        if (isLocked)
+            [[[NSWorkspace sharedWorkspace] 
iconForFileType:NSFileTypeForHFSTypeCode(kLockedBadgeIcon)] drawInRect:rect 
fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:0.5];
+        [pageImage unlockFocus];
+        
         [pageLabels enumerateObjectsUsingBlock:^(id label, NSUInteger i, BOOL 
*stop) {
-            SKThumbnail *thumbnail = [[SKThumbnail alloc] initWithImage:image 
label:label pageIndex:i];
+            SKThumbnail *thumbnail = [[SKThumbnail alloc] 
initWithImage:pageImage label:label pageIndex:i];
             [thumbnail setDelegate:self];
             [thumbnail setDirty:YES];
             [thumbnails addObject:thumbnail];

Modified: trunk/SKSnapshotWindowController.m
===================================================================
--- trunk/SKSnapshotWindowController.m  2017-05-16 21:02:53 UTC (rev 9421)
+++ trunk/SKSnapshotWindowController.m  2017-06-01 21:25:06 UTC (rev 9422)
@@ -481,19 +481,21 @@
         [transform scaleXBy:(thumbnailSize.width - 2.0 * shadowBlurRadius) / 
NSWidth(bounds) yBy:(thumbnailSize.height - 2.0 * shadowBlurRadius) / 
NSHeight(bounds)];
     }
     
-    image = [NSImage bitmapImageWithSize:thumbnailSize drawingHandler:^(NSRect 
rect){
-        [[NSGraphicsContext currentContext] 
setImageInterpolation:NSImageInterpolationHigh];
-        [transform concat];
-        [NSGraphicsContext saveGraphicsState];
-        [[PDFView defaultPageBackgroundColor] set];
-        if (shadowBlurRadius > 0.0)
-            [NSShadow setShadowWithColor:[NSColor colorWithCalibratedWhite:0.0 
alpha:0.5] blurRadius:shadowBlurRadius yOffset:shadowOffset];
-        NSRectFill(bounds);
-        [[NSGraphicsContext currentContext] 
setImageInterpolation:NSImageInterpolationDefault];
-        [NSGraphicsContext restoreGraphicsState];
-        [imageRep drawInRect:bounds];
-    }];
+    image = [[[NSImage alloc] initWithSize:thumbnailSize] autorelease];
     
+    [image lockFocus];
+    [[NSGraphicsContext currentContext] 
setImageInterpolation:NSImageInterpolationHigh];
+    [transform concat];
+    [NSGraphicsContext saveGraphicsState];
+    [[PDFView defaultPageBackgroundColor] set];
+    if (shadowBlurRadius > 0.0)
+        [NSShadow setShadowWithColor:[NSColor colorWithCalibratedWhite:0.0 
alpha:0.5] blurRadius:shadowBlurRadius yOffset:shadowOffset];
+    NSRectFill(bounds);
+    [[NSGraphicsContext currentContext] 
setImageInterpolation:NSImageInterpolationDefault];
+    [NSGraphicsContext restoreGraphicsState];
+    [imageRep drawInRect:bounds];
+    [image unlockFocus];
+    
     return image;
 }
 

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


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to