Revision: 13691
          http://sourceforge.net/p/skim-app/code/13691
Author:   hofman
Date:     2023-10-24 09:26:31 +0000 (Tue, 24 Oct 2023)
Log Message:
-----------
draw pages in thumbnail for snapshot directly in images rather tham first in a 
bitmap

Modified Paths:
--------------
    trunk/SKBasePDFView.m
    trunk/SKSnapshotWindowController.m

Modified: trunk/SKBasePDFView.m
===================================================================
--- trunk/SKBasePDFView.m       2023-10-23 22:29:38 UTC (rev 13690)
+++ trunk/SKBasePDFView.m       2023-10-24 09:26:31 UTC (rev 13691)
@@ -414,13 +414,11 @@
 
 - (NSBitmapImageRep *)bitmapImageRepCachingDisplayInRect:(NSRect)rect {
     // draw our own bitmap, because macOS 12 does it wrong
-    // ignore page shadows because
+    // ignore background and page shadows because
     NSBitmapImageRep *imageRep = [self 
bitmapImageRepForCachingDisplayInRect:rect];
     PDFDisplayBox *box = [self displayBox];
     CGFloat scale = [self scaleFactor];
     CGContextRef context = [[NSGraphicsContext 
graphicsContextWithBitmapImageRep:imageRep] CGContext];
-    CGContextSetFillColorWithColor(context, [[self backgroundColor] CGColor]);
-    CGContextFillRect(context, CGRectMake(0.0, 0.0, NSWidth(rect), 
NSHeight(rect)));
     for (PDFPage *page in [self visiblePages]) {
         NSRect pageRect = [self convertRect:[page boundsForBox:box] 
fromPage:page];
         if (NSIntersectsRect(pageRect, rect) == NO) continue;

Modified: trunk/SKSnapshotWindowController.m
===================================================================
--- trunk/SKSnapshotWindowController.m  2023-10-23 22:29:38 UTC (rev 13690)
+++ trunk/SKSnapshotWindowController.m  2023-10-24 09:26:31 UTC (rev 13691)
@@ -59,7 +59,6 @@
 #import "NSPasteboard_SKExtensions.h"
 #import "NSURL_SKExtensions.h"
 #import "NSWindow_SKExtensions.h"
-#import "NSView_SKExtensions.h"
 #import "NSScreen_SKExtensions.h"
 #import "SKApplication.h"
 #import "PDFDocument_SKExtensions.h"
@@ -490,9 +489,19 @@
 
 #pragma mark Thumbnails
 
+static inline CGRect SKPixelAlignedRect(CGRect rect, CGContextRef context) {
+    CGRect r;
+    rect = CGContextConvertRectToDeviceSpace(context, rect);
+    r.origin.x = round(CGRectGetMinX(rect));
+    r.origin.y = round(CGRectGetMinY(rect));
+    r.size.width = round(CGRectGetMaxX(rect)) - CGRectGetMinX(r);
+    r.size.height = round(CGRectGetMaxY(rect)) - CGRectGetMinY(r);
+    return CGRectGetWidth(r) > 0.0 && CGRectGetHeight(r) > 0.0 ? 
CGContextConvertRectToUserSpace(context, r) : NSZeroRect;
+}
+
 - (NSImage *)thumbnailWithSize:(CGFloat)size {
-    NSRect bounds = [pdfView visibleContentRect];
-    NSBitmapImageRep *imageRep = [pdfView 
bitmapImageRepCachingDisplayInRect:bounds];
+    NSRect rect = [pdfView visibleContentRect];
+    NSRect bounds = rect;
     NSAffineTransform *transform = nil;
     NSSize thumbnailSize = bounds.size;
     CGFloat shadowBlurRadius = 0.0;
@@ -516,16 +525,40 @@
     image = [[[NSImage alloc] initWithSize:thumbnailSize] autorelease];
     
     [image lockFocus];
+    
     [[NSGraphicsContext currentContext] 
setImageInterpolation:NSImageInterpolationHigh];
     [transform concat];
-    [NSGraphicsContext saveGraphicsState];
-    [[NSColor whiteColor] set];
-    if (shadowBlurRadius > 0.0)
+    
+    if (shadowBlurRadius > 0.0) {
+        [NSGraphicsContext saveGraphicsState];
+        [[NSColor whiteColor] set];
         [NSShadow setShadowWithWhite:0.0 alpha:0.5 blurRadius:shadowBlurRadius 
yOffset:shadowOffset];
-    NSRectFill(bounds);
-    [[NSGraphicsContext currentContext] 
setImageInterpolation:NSImageInterpolationDefault];
-    [NSGraphicsContext restoreGraphicsState];
-    [imageRep drawInRect:bounds];
+        NSRectFill(bounds);
+        [[NSGraphicsContext currentContext] 
setImageInterpolation:NSImageInterpolationDefault];
+        [NSGraphicsContext restoreGraphicsState];
+        [[NSBezierPath bezierPathWithRect:bounds] addClip];
+    }
+    
+    [[pdfView backgroundColor] setFill];
+    [NSBezierPath fillRect:bounds];
+    
+    PDFDisplayBox *box = [pdfView displayBox];
+    CGFloat scale = [pdfView scaleFactor];
+    CGContextRef context = [[NSGraphicsContext currentContext] CGContext];
+    for (PDFPage *page in [pdfView visiblePages]) {
+        NSRect pageRect = [pdfView convertRect:[page boundsForBox:box] 
fromPage:page];
+        if (NSIntersectsRect(pageRect, rect) == NO) continue;
+        pageRect.origin.x -= NSMinX(rect);
+        pageRect.origin.y -= NSMinY(rect);
+        CGContextSetFillColorWithColor(context, 
CGColorGetConstantColor(kCGColorWhite));
+        CGContextFillRect(context, 
SKPixelAlignedRect(NSRectToCGRect(pageRect), context));
+        CGContextSaveGState(context);
+        CGContextTranslateCTM(context, NSMinX(pageRect), NSMinY(pageRect));
+        CGContextScaleCTM(context, scale, scale);
+        [page drawWithBox:box toContext:context];
+        CGContextRestoreGState(context);
+    }
+    
     [image unlockFocus];
     
     return image;

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



_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to