Revision: 14353
          http://sourceforge.net/p/skim-app/code/14353
Author:   hofman
Date:     2024-06-30 16:22:26 +0000 (Sun, 30 Jun 2024)
Log Message:
-----------
Scale the path and layer bounds instead of using a scaling affine transform for 
drawing freehand, CAShapeLayer does not scale the path properly and draws the 
path at the wrong resolution

Modified Paths:
--------------
    trunk/NSBezierPath_SKExtensions.h
    trunk/NSBezierPath_SKExtensions.m
    trunk/SKPDFView.m

Modified: trunk/NSBezierPath_SKExtensions.h
===================================================================
--- trunk/NSBezierPath_SKExtensions.h   2024-06-30 14:38:08 UTC (rev 14352)
+++ trunk/NSBezierPath_SKExtensions.h   2024-06-30 16:22:26 UTC (rev 14353)
@@ -49,6 +49,8 @@
 
 @property (nonatomic, readonly) CGPathRef copyCGPath;
 
+- (CGPathRef)copyCGPathWithScaleFactor:(CGFloat)scale;
+
 - (void)halfEllipseFromPoint:(NSPoint)halfwayPoint toPoint:(NSPoint)endPoint;
 
 @end

Modified: trunk/NSBezierPath_SKExtensions.m
===================================================================
--- trunk/NSBezierPath_SKExtensions.m   2024-06-30 14:38:08 UTC (rev 14352)
+++ trunk/NSBezierPath_SKExtensions.m   2024-06-30 16:22:26 UTC (rev 14353)
@@ -93,6 +93,10 @@
 }
 
 - (CGPathRef)copyCGPath {
+    return [self copyCGPathWithScaleFactor:1.0];
+}
+
+- (CGPathRef)copyCGPathWithScaleFactor:(CGFloat)scale {
     CGMutablePathRef mutablePath = CGPathCreateMutable();
     NSInteger numElements = [self elementCount];
     NSPoint points[3];
@@ -101,13 +105,13 @@
     for (i = 0; i < numElements; i++) {
         switch ([self elementAtIndex:i associatedPoints:points]) {
             case NSMoveToBezierPathElement:
-                CGPathMoveToPoint(mutablePath, NULL, points[0].x, points[0].y);
+                CGPathMoveToPoint(mutablePath, NULL, scale * points[0].x, 
scale * points[0].y);
                 break;
             case NSLineToBezierPathElement:
-                CGPathAddLineToPoint(mutablePath, NULL, points[0].x, 
points[0].y);
+                CGPathAddLineToPoint(mutablePath, NULL, scale * points[0].x, 
scale * points[0].y);
                 break;
             case NSCurveToBezierPathElement:
-                CGPathAddCurveToPoint(mutablePath, NULL, points[0].x, 
points[0].y,points[1].x, points[1].y, points[2].x, points[2].y);
+                CGPathAddCurveToPoint(mutablePath, NULL, scale * points[0].x, 
scale * points[0].y, scale * points[1].x, scale * points[1].y, scale * 
points[2].x, scale * points[2].y);
                 break;
             case NSClosePathBezierPathElement:
                 CGPathCloseSubpath(mutablePath);

Modified: trunk/SKPDFView.m
===================================================================
--- trunk/SKPDFView.m   2024-06-30 14:38:08 UTC (rev 14352)
+++ trunk/SKPDFView.m   2024-06-30 16:22:26 UTC (rev 14353)
@@ -4403,6 +4403,20 @@
     return newCurrentAnnotation != nil;
 }
 
+
+static NSRect scaledRect(NSRect rect, CGFloat scale) {
+    return NSMakeRect(scale * NSMinX(rect), scale * NSMinY(rect), scale * 
NSWidth(rect), scale * NSHeight(rect));
+}
+
+static NSArray *scaledDashPattern(NSArray *dashPattern, CGFloat scale) {
+    if ([dashPattern count] == 0)
+        return nil;
+    NSMutableArray *array = [NSMutableArray array];
+    for (NSNumber *dash in dashPattern)
+        [array addObject:[NSNumber numberWithDouble:scale * [dash 
doubleValue]]];
+    return array;
+}
+
 - (void)doDrawFreehandNoteWithEvent:(NSEvent *)theEvent {
     NSPoint point = NSZeroPoint;
     PDFPage *page = [self pageAndPoint:&point forEvent:theEvent nearest:YES];
@@ -4413,14 +4427,14 @@
     BOOL wantsBreak = isOption;
     NSBezierPath *bezierPath = nil;
     CGPathRef cgPath = NULL;
+    CGFloat scale = [self scaleFactor];
     CAShapeLayer *layer = nil;
     NSRect boxBounds = NSIntersectionRect([page boundsForBox:[self 
displayBox]], [self convertRect:[self visibleContentRect] toPage:page]);
-    CGAffineTransform t = 
CGAffineTransformRotate(CGAffineTransformMakeScale([self scaleFactor], [self 
scaleFactor]), -M_PI_2 * [page rotation] / 90.0);
-    CGFloat r = fmin(2.0, 2.0 * [self scaleFactor]);
+    CGAffineTransform t = CGAffineTransformMakeRotation(-M_PI_2 * [page 
rotation] / 90.0);
     NSColor *tmpColor = pdfvFlags.presentationMode ? [[NSUserDefaults 
standardUserDefaults] colorForKey:SKPresentationInkNoteColorKey] : nil;
     layer = [CAShapeLayer layer];
-    // transform and place so that the path is in page coordinates
-    [layer setBounds:NSRectToCGRect(boxBounds)];
+    // transform and place so that the path is in scaled page coordinates
+    [layer setBounds:CGRectMake(scale * NSMinX(boxBounds), scale * 
NSMinY(boxBounds), scale * NSWidth(boxBounds), scale * NSHeight(boxBounds))];
     [layer setAnchorPoint:CGPointZero];
     [layer setPosition:NSPointToCGPoint([self convertPoint:boxBounds.origin 
fromPage:page])];
     [layer setAffineTransform:t];
@@ -4431,12 +4445,13 @@
     [layer setLineCap:kCALineCapRound];
     if (([theEvent modifierFlags] & (NSEventModifierFlagShift | 
NSEventModifierFlagCapsLock)) && [currentAnnotation isInk] && 
[[currentAnnotation page] isEqual:page]) {
         [layer setStrokeColor:[[currentAnnotation color] CGColor]];
-        [layer setLineWidth:[currentAnnotation lineWidth]];
+        [layer setLineWidth:[currentAnnotation lineWidth] * scale];
         if ([currentAnnotation borderStyle] == kPDFBorderStyleDashed) {
-            [layer setLineDashPattern:[currentAnnotation dashPattern]];
+            [layer setLineDashPattern:scaledDashPattern([currentAnnotation 
dashPattern], scale)];
             [layer setLineCap:kCALineCapButt];
         }
-        [layer setShadowRadius:r / [self scaleFactor]];
+        CGFloat r = fmin(2.0, 2.0 / scale);
+        [layer setShadowRadius:r];
         [layer setShadowOffset:CGSizeApplyAffineTransform(CGSizeMake(0.0, -r), 
CGAffineTransformInvert(t))];
         [layer setShadowOpacity:0.33333];
     } else {
@@ -4443,9 +4458,9 @@
         [self setCurrentAnnotation:nil];
         NSUserDefaults *sud = [NSUserDefaults standardUserDefaults];
         [layer setStrokeColor:[tmpColor ?: [sud colorForKey:SKInkNoteColorKey] 
CGColor]];
-        [layer setLineWidth:[sud floatForKey:SKInkNoteLineWidthKey]];
+        [layer setLineWidth:[sud floatForKey:SKInkNoteLineWidthKey] * scale];
         if ((PDFBorderStyle)[sud integerForKey:SKInkNoteLineStyleKey] == 
kPDFBorderStyleDashed) {
-            [layer setLineDashPattern:[sud 
arrayForKey:SKInkNoteDashPatternKey]];
+            [layer setLineDashPattern:scaledDashPattern([sud 
arrayForKey:SKInkNoteDashPatternKey], scale)];
             [layer setLineCap:kCALineCapButt];
         }
     }
@@ -4493,7 +4508,7 @@
             wasOption = isOption;
             wantsBreak = NO;
             
-            cgPath = [bezierPath copyCGPath];
+            cgPath = [bezierPath copyCGPathWithScaleFactor:scale];
             [layer setPath:cgPath];
             CGPathRelease(cgPath);
             

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