Revision: 14282
          http://sourceforge.net/p/skim-app/code/14282
Author:   hofman
Date:     2024-05-31 23:03:17 +0000 (Fri, 31 May 2024)
Log Message:
-----------
Use single method for page thumbnails. Round off shadow insets and offset by 
pixel rather than point.

Modified Paths:
--------------
    trunk/PDFPage_SKExtensions.h
    trunk/PDFPage_SKExtensions.m
    trunk/SKImageToolTipContext.m
    trunk/SKMainWindowController.m

Modified: trunk/PDFPage_SKExtensions.h
===================================================================
--- trunk/PDFPage_SKExtensions.h        2024-05-31 16:37:56 UTC (rev 14281)
+++ trunk/PDFPage_SKExtensions.h        2024-05-31 23:03:17 UTC (rev 14282)
@@ -60,9 +60,7 @@
 @property (nonatomic, readonly) NSRect autoCropBox;
 @property (nonatomic, readonly) NSRect boundingBox;
 
-- (NSImage *)thumbnailWithSize:(CGFloat)size forBox:(PDFDisplayBox)box;
-- (NSImage *)thumbnailWithSize:(CGFloat)size forBox:(PDFDisplayBox)box 
highlights:(nullable NSArray *)highlights;
-- (NSImage *)thumbnailWithSize:(CGFloat)size forBox:(PDFDisplayBox)box 
shadowBlurRadius:(CGFloat)shadowBlurRadius highlights:(nullable NSArray 
*)highlights;
+- (NSImage *)thumbnailWithSize:(CGFloat)size forBox:(PDFDisplayBox)box 
hasShadow:(BOOL)hasShadow highlights:(nullable NSArray *)highlights;
 
 - (NSAttributedString *)thumbnailAttachmentWithSize:(CGFloat)size;
 @property (nonatomic, readonly) NSAttributedString *thumbnailAttachment;

Modified: trunk/PDFPage_SKExtensions.m
===================================================================
--- trunk/PDFPage_SKExtensions.m        2024-05-31 16:37:56 UTC (rev 14281)
+++ trunk/PDFPage_SKExtensions.m        2024-05-31 23:03:17 UTC (rev 14282)
@@ -148,24 +148,22 @@
     return NSIntersectionRect(NSInsetRect([self foregroundRect], -2.0, -2.0), 
[self boundsForBox:kPDFDisplayBoxCropBox]);
 }
 
-- (NSImage *)thumbnailWithSize:(CGFloat)aSize forBox:(PDFDisplayBox)box {
-    return  [self thumbnailWithSize:aSize forBox:box highlights:nil];
-}
-
-- (NSImage *)thumbnailWithSize:(CGFloat)aSize forBox:(PDFDisplayBox)box 
highlights:(NSArray *)highlights {
-    CGFloat shadowBlurRadius = round(aSize / 32.0);
-    return  [self thumbnailWithSize:aSize forBox:box 
shadowBlurRadius:shadowBlurRadius highlights:highlights];
-}
-
-- (NSImage *)thumbnailWithSize:(CGFloat)aSize forBox:(PDFDisplayBox)box 
shadowBlurRadius:(CGFloat)shadowBlurRadius highlights:(NSArray *)highlights {
+- (NSImage *)thumbnailWithSize:(CGFloat)aSize forBox:(PDFDisplayBox)box 
hasShadow:(BOOL)hasShadow highlights:(NSArray *)highlights {
     NSRect bounds = [self boundsForBox:box];
     NSSize pageSize = bounds.size;
     CGFloat scale = 1.0;
     NSSize thumbnailSize;
-    CGFloat shadowOffset = shadowBlurRadius > 0.0 ? - ceil(shadowBlurRadius * 
0.75) : 0.0;
+    CGFloat backingScale = [[[NSScreen screens] 
valueForKeyPath:@"@max.backingScaleFactor"] doubleValue];
+    CGFloat shadowBlurRadius = 0.0;
+    CGFloat shadowOffset = 0.0;
     NSRect pageRect = NSZeroRect;
     NSImage *image;
     
+    if (hasShadow) {
+        shadowBlurRadius = round(backingScale * aSize / 32.0) / backingScale;
+        shadowOffset = - ceil(backingScale * shadowBlurRadius * 0.75) / 
backingScale;
+    }
+    
     if ([self rotation] % 180 == 90)
         pageSize = NSMakeSize(pageSize.height, pageSize.width);
     
@@ -176,30 +174,30 @@
             thumbnailSize = NSMakeSize(aSize, round((aSize - 2.0 * 
shadowBlurRadius) * pageSize.height / pageSize.width + 2.0 * shadowBlurRadius));
         scale = fmax((thumbnailSize.width - 2.0 * shadowBlurRadius) / 
pageSize.width, (thumbnailSize.height - 2.0 * shadowBlurRadius) / 
pageSize.height);
     } else {
-        thumbnailSize = NSMakeSize(pageSize.width + 2.0 * shadowBlurRadius, 
pageSize.height + 2.0 * shadowBlurRadius);
+        thumbnailSize = hasShadow ? NSMakeSize(pageSize.width + 2.0 * 
shadowBlurRadius, pageSize.height + 2.0 * shadowBlurRadius) : pageSize;
     }
     
     pageRect.size = thumbnailSize;
     
-    if (shadowBlurRadius > 0.0) {
+    if (hasShadow) {
         pageRect = NSInsetRect(pageRect, shadowBlurRadius, shadowBlurRadius);
         pageRect.origin.y -= shadowOffset;
     }
     
-    image = [NSImage bitmapImageWithSize:thumbnailSize scale:0.0 
drawingHandler:^(NSRect dstRect){
+    image = [NSImage bitmapImageWithSize:thumbnailSize scale:backingScale 
drawingHandler:^(NSRect dstRect){
         
         [[NSGraphicsContext currentContext] 
setImageInterpolation:[[NSUserDefaults standardUserDefaults] 
integerForKey:SKInterpolationQualityKey] + 1];
         
         [NSGraphicsContext saveGraphicsState];
         [[NSColor whiteColor] setFill];
-        if (shadowBlurRadius > 0.0)
+        if (hasShadow)
             [NSShadow setShadowWithWhite:0.0 alpha:0.3 
blurRadius:shadowBlurRadius yOffset:shadowOffset];
         NSRectFill(pageRect);
         [NSGraphicsContext restoreGraphicsState];
         
-        if (fabs(scale - 1.0) > 0.0 || shadowBlurRadius > 0.0) {
+        if (fabs(scale - 1.0) > 0.0 || hasShadow) {
             NSAffineTransform *transform = [NSAffineTransform transform];
-            if (shadowBlurRadius > 0.0)
+            if (hasShadow)
                 [transform translateXBy:NSMinX(pageRect) yBy:NSMinY(pageRect)];
             [transform scaleBy:scale];
             [transform concat];
@@ -221,7 +219,7 @@
 }
 
 - (NSAttributedString *)thumbnailAttachmentWithSize:(CGFloat)aSize {
-    NSImage *image = [self thumbnailWithSize:aSize 
forBox:kPDFDisplayBoxCropBox];
+    NSImage *image = [self thumbnailWithSize:aSize 
forBox:kPDFDisplayBoxCropBox hasShadow:YES highlights:nil];
     
     NSFileWrapper *wrapper = [[NSFileWrapper alloc] 
initRegularFileWithContents:[image TIFFRepresentation]];
     NSString *filename = [NSString stringWithFormat:@"page_%lu.tiff", 
(unsigned long)([self pageIndex] + 1)];
@@ -267,7 +265,7 @@
 
 - (NSData *)TIFFDataForRect:(NSRect)rect {
     PDFDisplayBox box = NSEqualRects(rect, [self 
boundsForBox:kPDFDisplayBoxCropBox]) ? kPDFDisplayBoxCropBox : 
kPDFDisplayBoxMediaBox;
-    NSImage *pageImage = [self thumbnailWithSize:0.0 forBox:box 
shadowBlurRadius:0.0 highlights:nil];
+    NSImage *pageImage = [self thumbnailWithSize:0.0 forBox:box hasShadow:NO 
highlights:nil];
     NSRect bounds = [self boundsForBox:box];
     
     if (NSEqualRects(rect, NSZeroRect) || NSEqualRects(rect, bounds))

Modified: trunk/SKImageToolTipContext.m
===================================================================
--- trunk/SKImageToolTipContext.m       2024-05-31 16:37:56 UTC (rev 14281)
+++ trunk/SKImageToolTipContext.m       2024-05-31 23:03:17 UTC (rev 14282)
@@ -110,7 +110,7 @@
     NSPoint point = [[self effectiveDestinationForView:nil] point];
     NSRect bounds = [page boundsForBox:kPDFDisplayBoxCropBox];
     CGFloat size = isScaled ? ceil(scale * fmax(NSWidth(bounds), 
NSHeight(bounds))) : 0.0;
-    NSImage *pageImage = [page thumbnailWithSize:size 
forBox:kPDFDisplayBoxCropBox shadowBlurRadius:0.0 highlights:selections];
+    NSImage *pageImage = [page thumbnailWithSize:size 
forBox:kPDFDisplayBoxCropBox hasShadow:NO highlights:selections];
     NSRect pageImageRect = {NSZeroPoint, [pageImage size]};
     NSRect sourceRect = NSZeroRect;
     PDFSelection *pageSelection = [page selectionForRect:bounds];
@@ -257,7 +257,7 @@
 @implementation PDFPage (SKImageToolTipContext)
 
 - (NSImage *)toolTipImageWithScale:(CGFloat)scale {
-    NSImage *image = [self thumbnailWithSize:256.0 
forBox:kPDFDisplayBoxCropBox shadowBlurRadius:0.0 highlights:nil];
+    NSImage *image = [self thumbnailWithSize:256.0 
forBox:kPDFDisplayBoxCropBox hasShadow:NO highlights:nil];
     [[[image representations] firstObject] setOpaque:YES];
     return image;
 }

Modified: trunk/SKMainWindowController.m
===================================================================
--- trunk/SKMainWindowController.m      2024-05-31 16:37:56 UTC (rev 14281)
+++ trunk/SKMainWindowController.m      2024-05-31 23:03:17 UTC (rev 14282)
@@ -2819,7 +2819,7 @@
     PDFDisplayBox box = [pdfView displayBox];
     
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 
0), ^{
-        NSImage *image = [page thumbnailWithSize:thumbnailCacheSize forBox:box 
highlights:highlights];
+        NSImage *image = [page thumbnailWithSize:thumbnailCacheSize forBox:box 
hasShadow:YES highlights:highlights];
         [image setAccessibilityDescription:[NSString 
stringWithFormat:NSLocalizedString(@"Page %@", @""), [page displayLabel]]];
         
         dispatch_async(dispatch_get_main_queue(), ^{
@@ -2879,7 +2879,7 @@
         if ([[pdfView document] isLocked])
             [stamps addObject:[[SKThumbnailStamp alloc] 
initWithImage:[[NSWorkspace sharedWorkspace] 
iconForFileType:NSFileTypeForHFSTypeCode(kLockedBadgeIcon)] rect:rect 
fraction:0.5]];
         
-        NSImage *pageImage = [emptyPage thumbnailWithSize:thumbnailCacheSize 
forBox:box highlights:stamps];
+        NSImage *pageImage = [emptyPage thumbnailWithSize:thumbnailCacheSize 
forBox:box hasShadow:YES highlights:stamps];
         
         [pageLabels enumerateObjectsUsingBlock:^(NSString *label, NSUInteger 
i, BOOL *stop) {
             SKThumbnail *thumbnail = [[SKThumbnail alloc] 
initWithImage:pageImage label:label pageIndex:i];

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