Revision: 15233
          http://sourceforge.net/p/skim-app/code/15233
Author:   hofman
Date:     2025-05-19 09:35:00 +0000 (Mon, 19 May 2025)
Log Message:
-----------
convenience method to create block drawing the image and scheduling main queue 
handler

Modified Paths:
--------------
    trunk/SKPresentationView.m

Modified: trunk/SKPresentationView.m
===================================================================
--- trunk/SKPresentationView.m  2025-05-19 08:48:22 UTC (rev 15232)
+++ trunk/SKPresentationView.m  2025-05-19 09:35:00 UTC (rev 15233)
@@ -297,37 +297,47 @@
 
 - (void)removePredrawnImageAtIndex:(NSUInteger)pageIndex {}
 
-static NSImage *imageByDrawingPageToBitmapImageRep(NSBitmapImageRep *imageRep, 
PDFPage *aPage, BOOL autoScales) {
-    NSSize size = [imageRep size];
-    NSRect pageRect = [aPage boundsForBox:kPDFDisplayBoxCropBox];
-    if (([aPage rotation] % 180) != 0)
-        pageRect = NSMakeRect(0.0, 0.0, NSHeight(pageRect), NSWidth(pageRect));
-    CGFloat scale = 1.0;
-    if (autoScales) {
-        scale = fmin(size.height / NSHeight(pageRect), size.width / 
NSWidth(pageRect));
-        pageRect.size.width *= scale;
-        pageRect.size.height *= scale;
-    }
-    pageRect.origin.x = 0.5 * (size.width - NSWidth(pageRect));
-    pageRect.origin.y = 0.5 * (size.height - NSHeight(pageRect));
+- (dispatch_block_t)imageGeneratorForPage:(PDFPage *)aPage handler:(void 
(^)(NSImage *))handler {
+    NSBitmapImageRep *imageRep = [self 
bitmapImageRepForCachingDisplayInRect:[self bounds]];
     
-    CGContextRef context = [[NSGraphicsContext 
graphicsContextWithBitmapImageRep:imageRep] CGContext];
+    if (imageRep == nil)
+        return nil;
     
-    CGContextSaveGState(context);
-    CGContextSetFillColorWithColor(context, 
CGColorGetConstantColor(kCGColorWhite));
-    CGContextFillRect(context, SKPixelAlignedRect(NSRectToCGRect(pageRect), 
context));
-    CGContextRestoreGState(context);
-    CGContextSaveGState(context);
-    CGContextSetInterpolationQuality(context, [[NSUserDefaults 
standardUserDefaults] integerForKey:SKInterpolationQualityKey] + 1);
-    CGContextTranslateCTM(context, NSMinX(pageRect), NSMinY(pageRect));
-    CGContextScaleCTM(context, scale, scale);
-    [aPage drawWithBox:kPDFDisplayBoxCropBox toContext:context];
-    CGContextRestoreGState(context);
+    BOOL autoScales = [self autoScales];
     
-    NSImage *image = [[NSImage alloc] initWithSize:size];
-    [image addRepresentation:imageRep];
-    
-    return image;
+    return dispatch_block_create(0, ^{
+        
+        NSSize size = [imageRep size];
+        NSRect pageRect = [aPage boundsForBox:kPDFDisplayBoxCropBox];
+        if (([aPage rotation] % 180) != 0)
+            pageRect = NSMakeRect(0.0, 0.0, NSHeight(pageRect), 
NSWidth(pageRect));
+        CGFloat scale = 1.0;
+        if (autoScales) {
+            scale = fmin(size.height / NSHeight(pageRect), size.width / 
NSWidth(pageRect));
+            pageRect.size.width *= scale;
+            pageRect.size.height *= scale;
+        }
+        pageRect.origin.x = 0.5 * (size.width - NSWidth(pageRect));
+        pageRect.origin.y = 0.5 * (size.height - NSHeight(pageRect));
+        
+        CGContextRef context = [[NSGraphicsContext 
graphicsContextWithBitmapImageRep:imageRep] CGContext];
+        
+        CGContextSaveGState(context);
+        CGContextSetFillColorWithColor(context, 
CGColorGetConstantColor(kCGColorWhite));
+        CGContextFillRect(context, 
SKPixelAlignedRect(NSRectToCGRect(pageRect), context));
+        CGContextRestoreGState(context);
+        CGContextSaveGState(context);
+        CGContextSetInterpolationQuality(context, [[NSUserDefaults 
standardUserDefaults] integerForKey:SKInterpolationQualityKey] + 1);
+        CGContextTranslateCTM(context, NSMinX(pageRect), NSMinY(pageRect));
+        CGContextScaleCTM(context, scale, scale);
+        [aPage drawWithBox:kPDFDisplayBoxCropBox toContext:context];
+        CGContextRestoreGState(context);
+        
+        NSImage *image = [[NSImage alloc] initWithSize:size];
+        [image addRepresentation:imageRep];
+        
+        dispatch_async(dispatch_get_main_queue(), ^{ handler(image); });
+    });
 }
 
 - (void)displayCurrentPage:(void (^)(void))completionHandler {
@@ -348,9 +358,15 @@
         return;
     }
     
-    NSBitmapImageRep *imageRep = [self 
bitmapImageRepForCachingDisplayInRect:[self bounds]];
+    PDFPage *thePage = page;
+    dispatch_block_t imageGenerator = [self imageGeneratorForPage:page 
handler:^(NSImage *image){
+        if (thePage == page)
+            [pageLayer setContents:image];
+        if (completionHandler)
+            completionHandler();
+    }];
     
-    if (imageRep == nil) {
+    if (imageGenerator == nil) {
         if (completionHandler)
             completionHandler();
         return;
@@ -362,24 +378,8 @@
         drawingQueue = 
dispatch_queue_create("net.sourceforge.skim-app.skim.pageview.drawing", 
queuePriority);
     }
     
-    PDFPage *thePage = page;
-    BOOL autoScales = [self autoScales];
+    dispatch_async(drawingQueue, imageGenerator);
     
-    dispatch_async(drawingQueue, ^{
-        
-        NSImage *image = imageByDrawingPageToBitmapImageRep(imageRep, thePage, 
autoScales);
-        
-        dispatch_async(dispatch_get_main_queue(), ^{
-            
-            if (thePage == page)
-                [pageLayer setContents:image];
-            if (completionHandler)
-                completionHandler();
-            
-        });
-        
-    });
-    
 }
 
 - (void)redisplayAtCurrentScaleFactorIfNeeded {
@@ -522,34 +522,24 @@
     if (imageRep == nil)
         return;
     
-    PDFPage *thePage = [[page document] pageAtIndex:pageIndex];
-    BOOL autoScales = [self autoScales];
+    __block void *block = NULL;
+    dispatch_block_t imageGenerator = [self imageGeneratorForPage:[[page 
document] pageAtIndex:pageIndex] handler:^(NSImage *image){
+        if (predrawnImages && block == NSMapGet(predrawnImages, (void 
*)pageIndex)) {
+            NSMapRemove(predrawnImages, (void *)pageIndex);
+            NSUInteger currentIndex = [page pageIndex];
+            if (pageIndex > currentIndex)
+                NSMapInsert(predrawnImages, (void *)pageIndex, (__bridge void 
*)image);
+            else if (pageIndex == currentIndex)
+                [pageLayer setContents:image];
+        }
+    }];
     
-    __block void *myBlock = NULL;
-    dispatch_block_t block = dispatch_block_create(0, ^{
-
-        NSImage *image = imageByDrawingPageToBitmapImageRep(imageRep, thePage, 
autoScales);
-        
-        dispatch_async(dispatch_get_main_queue(), ^{
-            
-            if (predrawnImages && myBlock == NSMapGet(predrawnImages, (void 
*)pageIndex)) {
-                NSMapRemove(predrawnImages, (void *)pageIndex);
-                if (image) {
-                    NSUInteger currentIndex = [page pageIndex];
-                    if (pageIndex > currentIndex)
-                        NSMapInsert(predrawnImages, (void *)pageIndex, 
(__bridge void *)image);
-                    else if (pageIndex == currentIndex)
-                        [pageLayer setContents:image];
-                }
-            }
-            
-        });
-        
-    });
+    if (imageGenerator == nil)
+        return;
     
     // set this block so we can cancel it
-    myBlock = (__bridge void *)block;
-    NSMapInsert(predrawnImages, (void *)pageIndex, myBlock);
+    block = (__bridge void *)imageGenerator;
+    NSMapInsert(predrawnImages, (void *)pageIndex, block);
     
     static dispatch_queue_t predrawingQueue = nil;
     if (predrawingQueue == nil) {
@@ -557,7 +547,7 @@
         predrawingQueue = 
dispatch_queue_create("net.sourceforge.skim-app.skim.pageview.predrawing", 
queuePriority);
     }
     
-    dispatch_async(predrawingQueue, block);
+    dispatch_async(predrawingQueue, imageGenerator);
     
 }
 

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



_______________________________________________
Skim-app-commit mailing list
Skim-app-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to