Revision: 13692
          http://sourceforge.net/p/skim-app/code/13692
Author:   hofman
Date:     2023-10-24 14:38:55 +0000 (Tue, 24 Oct 2023)
Log Message:
-----------
separate method toi draw the visible pages in another context

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

Modified: trunk/SKBasePDFView.h
===================================================================
--- trunk/SKBasePDFView.h       2023-10-24 09:26:31 UTC (rev 13691)
+++ trunk/SKBasePDFView.h       2023-10-24 14:38:55 UTC (rev 13692)
@@ -44,4 +44,6 @@
 
 - (void)colorFiltersDidChange;
 
+- (void)drawPagesInRect:(NSRect)rect toContext:(CGContextRef)context;
+
 @end

Modified: trunk/SKBasePDFView.m
===================================================================
--- trunk/SKBasePDFView.m       2023-10-24 09:26:31 UTC (rev 13691)
+++ trunk/SKBasePDFView.m       2023-10-24 14:38:55 UTC (rev 13692)
@@ -412,18 +412,12 @@
     return CGRectGetWidth(r) > 0.0 && CGRectGetHeight(r) > 0.0 ? 
CGContextConvertRectToUserSpace(context, r) : NSZeroRect;
 }
 
-- (NSBitmapImageRep *)bitmapImageRepCachingDisplayInRect:(NSRect)rect {
-    // draw our own bitmap, because macOS 12 does it wrong
-    // ignore background and page shadows because
-    NSBitmapImageRep *imageRep = [self 
bitmapImageRepForCachingDisplayInRect:rect];
+- (void)drawPagesInRect:(NSRect)rect toContext:(CGContextRef)context {
     PDFDisplayBox *box = [self displayBox];
     CGFloat scale = [self scaleFactor];
-    CGContextRef context = [[NSGraphicsContext 
graphicsContextWithBitmapImageRep:imageRep] CGContext];
     for (PDFPage *page in [self visiblePages]) {
         NSRect pageRect = [self 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);
@@ -432,6 +426,16 @@
         [page drawWithBox:box toContext:context];
         CGContextRestoreGState(context);
     }
+}
+
+- (NSBitmapImageRep *)bitmapImageRepCachingDisplayInRect:(NSRect)rect {
+    // draw our own bitmap, because macOS 12 does it wrong
+    // ignore background and page shadows because
+    NSBitmapImageRep *imageRep = [self 
bitmapImageRepForCachingDisplayInRect:rect];
+    CGContextRef context = [[NSGraphicsContext 
graphicsContextWithBitmapImageRep:imageRep] CGContext];
+    if (NSEqualPoints(rect.origin, NSZeroPoint) == NO)
+        CGContextTranslateCTM(context, -NSMinX(rect), -NSMinY(rect));
+    [self drawPagesInRect:rect toContext:context];
     return imageRep;
 }
 

Modified: trunk/SKSnapshotWindowController.m
===================================================================
--- trunk/SKSnapshotWindowController.m  2023-10-24 09:26:31 UTC (rev 13691)
+++ trunk/SKSnapshotWindowController.m  2023-10-24 14:38:55 UTC (rev 13692)
@@ -489,20 +489,9 @@
 
 #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 rect = [pdfView visibleContentRect];
-    NSRect bounds = rect;
-    NSAffineTransform *transform = nil;
+    NSRect bounds = [pdfView visibleContentRect];
+    NSAffineTransform *transform = [NSAffineTransform transform];
     NSSize thumbnailSize = bounds.size;
     CGFloat shadowBlurRadius = 0.0;
     CGFloat shadowOffset = 0.0;
@@ -517,11 +506,14 @@
             thumbnailSize = NSMakeSize(round((size - 2.0 * shadowBlurRadius) * 
NSWidth(bounds) / NSHeight(bounds) + 2.0 * shadowBlurRadius), size);
         else
             thumbnailSize = NSMakeSize(size, round((size - 2.0 * 
shadowBlurRadius) * NSHeight(bounds) / NSWidth(bounds) + 2.0 * 
shadowBlurRadius));
-        transform = [NSAffineTransform transform];
         [transform translateXBy:shadowBlurRadius yBy:shadowBlurRadius - 
shadowOffset];
         [transform scaleXBy:(thumbnailSize.width - 2.0 * shadowBlurRadius) / 
NSWidth(bounds) yBy:(thumbnailSize.height - 2.0 * shadowBlurRadius) / 
NSHeight(bounds)];
     }
     
+    
+    if (NSEqualPoints(bounds.origin, NSZeroPoint) == NO)
+        [transform translateXBy:-NSMinX(bounds) yBy:-NSMinY(bounds)];
+    
     image = [[[NSImage alloc] initWithSize:thumbnailSize] autorelease];
     
     [image lockFocus];
@@ -529,35 +521,20 @@
     [[NSGraphicsContext currentContext] 
setImageInterpolation:NSImageInterpolationHigh];
     [transform concat];
     
-    if (shadowBlurRadius > 0.0) {
-        [NSGraphicsContext saveGraphicsState];
-        [[NSColor whiteColor] set];
+    [NSGraphicsContext saveGraphicsState];
+    [[NSColor whiteColor] set];
+    if (shadowBlurRadius > 0.0)
         [NSShadow setShadowWithWhite:0.0 alpha:0.5 blurRadius:shadowBlurRadius 
yOffset:shadowOffset];
-        NSRectFill(bounds);
-        [[NSGraphicsContext currentContext] 
setImageInterpolation:NSImageInterpolationDefault];
-        [NSGraphicsContext restoreGraphicsState];
-        [[NSBezierPath bezierPathWithRect:bounds] addClip];
-    }
+    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);
-    }
+    [pdfView drawPagesInRect:bounds toContext:context];
     
     [image unlockFocus];
     

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