Browse Source

【注释】文本注释支持拖拽添加+移动高相关逻辑

dinglingui 4 months ago
parent
commit
c847b8cb3b

+ 1 - 18
PDF Office/PDF Master/Class/PDFWindowController/PDFListView/CPDFKitExtensions/CPDFAnnotationExtensions/CPDFFreeTextAnnotation+PDFListView.swift

@@ -61,28 +61,11 @@ import Foundation
         context.restoreGState()
         context.saveGState()
         if (self.isResizable() && (pdfView.activeAnnotations.count == 1 || pdfView.selectAnnotations.count > 0) && !isHover) {
-            CPDFListViewDrawFreeTextResizeHandle(context, rect, 4.0 * lineWidth, true)
+            CPDFListViewDrawResizeHandles(context, rect, 4.0 * lineWidth, true)
         }
         context.restoreGState()
     }
     
-    override func resizeHandleForPoint(_ point: NSPoint, scaleFactor: CGFloat) -> CRectEdges {
-        let offsetNum = CPDFListViewConfig.defaultManager.annotationBorderOffset
-        let rect = NSRectFromCGRect(KMRectInset(NSRectToCGRect(self.bounds), CGFloat(-(offsetNum.floatValue)), CGFloat(-(offsetNum.floatValue))))
-        var rectEdges = CPDFListViewResizeHandleForPointFromRect(point, rect, 8.0 / scaleFactor)
-
-        if ((rectEdges.contains(.maxXEdgeMask) && rectEdges.contains(.minYEdgeMask)) ||
-            (rectEdges.contains(.minXEdgeMask) && rectEdges.contains(.maxYEdgeMask)) ||
-            (rectEdges.contains(.maxXEdgeMask) && rectEdges.contains(.maxYEdgeMask)) ||
-            (rectEdges.contains(.minXEdgeMask) && rectEdges.contains(.minYEdgeMask)) ||
-            rectEdges.contains(.maxYEdgeMask) ||
-            rectEdges.contains(.minYEdgeMask)) {
-            rectEdges = .editInEdgeMask
-        }
-        
-        return self.isResizable() ? rectEdges: CRectEdges(rawValue: 0)
-    }
-    
     override func isResizable() -> Bool {
         return true
     }

+ 88 - 22
PDF Office/PDF Master/Class/PDFWindowController/PDFListView/CPDFListViewExtension/CPDFListView+Event.m

@@ -2747,27 +2747,70 @@ static inline CPDFAreaOfInterest CAreaOfInterestForResizeHandle(CRectEdges mask,
         }
     } else if (CAnnotationTypeFreeText == self.annotationType ||
                CAnnotationTypeSignText == self.annotationType) {
-        CPDFAnnotationModel *annotationModel = [[CPDFAnnotationModel alloc] initWithAnnotationType:self.annotationType];
-        
-        CGFloat defaultWidth = annotationModel.fontSize;
-        CGFloat defaultHeight = annotationModel.fontSize;
-        NSSize defaultSize = ([page rotation] % 180 == 0) ? NSMakeSize(defaultWidth, defaultHeight) : NSMakeSize(defaultHeight, defaultWidth);
-        CGRect bounds =  CPDFListViewRectFromRightAndSize(pagePoint, defaultSize);
-        CGFloat fontSize = annotationModel.fontSize;
-        NSString *fontName = annotationModel.fontName;
-        CGFloat opacity = annotationModel.opacity;
-        
-        bounds = CGRectMake(pagePoint.x, pagePoint.y - (fontSize+2), defaultSize.width, (fontSize+2));
-        
-        bounds = CPDFListViewConstrainRect(bounds, page.bounds,[CPDFListViewConfig defaultManager].annotationBorderOffset.floatValue);
-        annotation = [self addAnnotationWithType:self.annotationType selection:nil page:page bounds:bounds];
-        if (self.annotationType == CAnnotationTypeFreeText) {
-            ((CPDFFreeTextAnnotation *)annotation).font = [NSFont fontWithName:fontName size:fontSize];
-            annotation.opacity = opacity;
-        }
-        originalBounds = [annotation bounds];
-        if (annotation) {
-            [self updateActiveAnnotations:@[annotation]];
+        BOOL draggedAnnotation = NO;
+        originalBounds = CPDFListViewRectFromCenterAndSquareSize(CPDFListViewIntegralPoint(pagePoint), 0.0);
+        NSEvent *lastMouseEvent = theEvent;
+        CRectEdges resizeHandle = CMaxXEdgeMask | CMinYEdgeMask;
+        NSUInteger eventMask = NSEventMaskLeftMouseUp | NSEventMaskLeftMouseDragged;
+        while (YES) {
+            theEvent = [[self window] nextEventMatchingMask:eventMask];
+            if ([theEvent type] == NSEventTypeLeftMouseUp) {
+                if (draggedAnnotation) {
+                    if (annotation) {
+                        CPDFAnnotationModel *annotationModel = [[CPDFAnnotationModel alloc] initWithAnnotationType:self.annotationType];
+                        
+                        CGFloat fontSize = annotationModel.fontSize;
+                        NSString *fontName = annotationModel.fontName;
+                        CGFloat opacity = annotationModel.opacity;
+                        ((CPDFFreeTextAnnotation *)annotation).font = [NSFont fontWithName:fontName size:fontSize];
+                        annotation.opacity = opacity;
+                    }
+                } else {
+                    CPDFAnnotationModel *annotationModel = [[CPDFAnnotationModel alloc] initWithAnnotationType:self.annotationType];
+                    
+                    CGFloat defaultWidth = 200;
+                    CGFloat defaultHeight = 64.0;
+                    NSSize defaultSize = ([page rotation] % 180 == 0) ? NSMakeSize(defaultWidth, defaultHeight) : NSMakeSize(defaultHeight, defaultWidth);
+                    CGRect bounds =  CPDFListViewRectFromRightAndSize(pagePoint, defaultSize);
+                    CGFloat fontSize = annotationModel.fontSize;
+                    NSString *fontName = annotationModel.fontName;
+                    CGFloat opacity = annotationModel.opacity;
+                    
+                    bounds = CGRectMake(pagePoint.x, pagePoint.y - (fontSize+2), defaultSize.width, (fontSize+2));
+                    
+                    bounds = CPDFListViewConstrainRect(bounds, page.bounds,[CPDFListViewConfig defaultManager].annotationBorderOffset.floatValue);
+                    annotation = [self addAnnotationWithType:self.annotationType selection:nil page:page bounds:bounds];
+                    if (self.annotationType == CAnnotationTypeFreeText) {
+                        ((CPDFFreeTextAnnotation *)annotation).font = [NSFont fontWithName:fontName size:fontSize];
+                        annotation.opacity = opacity;
+                    }
+                    originalBounds = [annotation bounds];
+                    if (annotation) {
+                        [self updateActiveAnnotations:@[annotation]];
+                    }
+                }
+                break;
+            } else if ([theEvent type] == NSEventTypeLeftMouseDragged) {
+                CGPoint zPoint = pagePoint;
+                [self pageAndPoint:&zPoint forEvent:theEvent nearest:YES];
+                CGRect zRect = CGRectMake(pagePoint.x-2*mouseOffset, pagePoint.y-2*mouseOffset, 4*mouseOffset, 4*mouseOffset);
+                if (CGRectContainsPoint(zRect, zPoint)) {continue;}
+                
+                if (annotation == nil)
+                    annotation = [self addAnnotationWithType:self.annotationType selection:nil page:page bounds:CPDFListViewRectFromCenterAndSquareSize(originalBounds.origin, 0.0)];
+                
+                lastMouseEvent = theEvent;
+                draggedAnnotation = YES;
+                
+                if(annotation) {
+                    [self updateActiveAnnotations:@[annotation]];
+                }
+                
+                [annotation updateAppearanceStream];
+                [self doResizeAnnotationWithEvent:lastMouseEvent fromPoint:pagePoint originalBounds:originalBounds resizeHandle:&resizeHandle];
+                
+                [self setNeedsDisplayAnnotationViewForPage:page];
+            }
         }
     } else if((CAnnotationTypeStamp == self.annotationType ||
                CAnnotationTypeSignSignature == self.annotationType) && self.stampObject) {
@@ -3681,7 +3724,30 @@ static inline CPDFAreaOfInterest CAreaOfInterestForResizeHandle(CRectEdges mask,
                 newBounds.size.width = minWidth;
             }
         }
-        newBounds = [self adjustFreetText:activeAnnotation rect:newBounds];
+        
+        if ((resizeHandle & CMaxYEdgeMask)) {
+            newBounds.size.height += relPoint.y;
+            if (NSMaxY(newBounds)+ offsetPageSet > NSMaxY(pageBounds)) {
+                newBounds.size.height = NSMaxY(pageBounds) - NSMinY(newBounds)- offsetPageSet;
+            }
+            if (NSHeight(newBounds) < minHeight) {
+                newBounds.size.height = minHeight;
+            }
+            
+        } else if ((resizeHandle & CMinYEdgeMask)) {
+            newBounds.origin.y += relPoint.y;
+            newBounds.size.height -= relPoint.y;
+            if (NSMinY(newBounds)-offsetPageSet < NSMinY(pageBounds)) {
+                newBounds.size.height = NSMaxY(newBounds) - NSMinY(pageBounds)-offsetPageSet;
+                newBounds.origin.y = NSMinY(pageBounds)+offsetPageSet;
+            }
+            if (NSHeight(newBounds) < minHeight) {
+                newBounds.origin.y = NSMaxY(newBounds) - minHeight;
+                newBounds.size.height = minHeight;
+            }
+        }
+        
+//        newBounds = [self adjustFreetText:activeAnnotation rect:newBounds];
     } else {
         CGFloat minWidth = MIN_NOTE_SIZE;
         CGFloat minHeight = MIN_NOTE_SIZE;