Bladeren bron

【标记密文】KMRedactPDFView 新增

tangchao 1 jaar geleden
bovenliggende
commit
74ecebfa61

+ 8 - 0
PDF Office/PDF Master.xcodeproj/project.pbxproj

@@ -2729,6 +2729,9 @@
 		BB3D970E2B2FEAF9007094C8 /* KMPDFRedactViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = BB3D970D2B2FEAF7007094C8 /* KMPDFRedactViewController.xib */; };
 		BB3D970F2B2FEAF9007094C8 /* KMPDFRedactViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = BB3D970D2B2FEAF7007094C8 /* KMPDFRedactViewController.xib */; };
 		BB3D97102B2FEAF9007094C8 /* KMPDFRedactViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = BB3D970D2B2FEAF7007094C8 /* KMPDFRedactViewController.xib */; };
+		BB3D97122B301CF1007094C8 /* KMRedactPDFView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB3D97112B301CF1007094C8 /* KMRedactPDFView.swift */; };
+		BB3D97132B301CF1007094C8 /* KMRedactPDFView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB3D97112B301CF1007094C8 /* KMRedactPDFView.swift */; };
+		BB3D97142B301CF1007094C8 /* KMRedactPDFView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB3D97112B301CF1007094C8 /* KMRedactPDFView.swift */; };
 		BB3EAEAD293E3D6000D92407 /* KMConvertBaseWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB3EAEAB293E3D6000D92407 /* KMConvertBaseWindowController.swift */; };
 		BB3EAEAE293E3D6000D92407 /* KMConvertBaseWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB3EAEAB293E3D6000D92407 /* KMConvertBaseWindowController.swift */; };
 		BB3EAEAF293E3D6000D92407 /* KMConvertBaseWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB3EAEAB293E3D6000D92407 /* KMConvertBaseWindowController.swift */; };
@@ -5307,6 +5310,7 @@
 		BB3AD6F829935483004FC1AE /* Reachability.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Reachability.h; sourceTree = "<group>"; };
 		BB3D97092B2FEAC8007094C8 /* KMPDFRedactViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMPDFRedactViewController.swift; sourceTree = "<group>"; };
 		BB3D970D2B2FEAF7007094C8 /* KMPDFRedactViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = KMPDFRedactViewController.xib; sourceTree = "<group>"; };
+		BB3D97112B301CF1007094C8 /* KMRedactPDFView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMRedactPDFView.swift; sourceTree = "<group>"; };
 		BB3EAEAB293E3D6000D92407 /* KMConvertBaseWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMConvertBaseWindowController.swift; sourceTree = "<group>"; };
 		BB3EAEAC293E3D6000D92407 /* KMConvertBaseWindowController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = KMConvertBaseWindowController.xib; sourceTree = "<group>"; };
 		BB403BA92B15CA6E00B3106D /* KMBatchConvertOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMBatchConvertOperation.swift; sourceTree = "<group>"; };
@@ -9271,6 +9275,7 @@
 				BB4EEF4B2976544F003A3537 /* KMRedactAligementView.swift */,
 				BBB14A5A2978EBBE00936EDB /* KMRedactMutilPageFlagContentView.swift */,
 				BBB14A6229792D6900936EDB /* KMRedactPageRangeContentView.swift */,
+				BB3D97112B301CF1007094C8 /* KMRedactPDFView.swift */,
 			);
 			path = View;
 			sourceTree = "<group>";
@@ -12795,6 +12800,7 @@
 				9FD0FA3129CD947000F2AB0D /* KMOpacityPanel.swift in Sources */,
 				9F0201792A1B5C0300C9B673 /* KMAIServerConfig.swift in Sources */,
 				BB1969E02B2856A900922736 /* KMSnapshotPDFView.swift in Sources */,
+				BB3D97122B301CF1007094C8 /* KMRedactPDFView.swift in Sources */,
 				9F1FE4F329406E4700E952CA /* FastResizeView.m in Sources */,
 				AD1D483A2AFB81ED007AC1F0 /* KMMergeBlankView.swift in Sources */,
 				9F1FE4FC29406E4700E952CA /* CTTabStripController.m in Sources */,
@@ -13492,6 +13498,7 @@
 				BBF729902B1960FF00576AC5 /* KMCompressOperationQueue.swift in Sources */,
 				BBF8A3FE2AE8B04100788BAC /* KMBatchOperateFile.swift in Sources */,
 				ADBC375529CAE94700D93208 /* KMComparativeOutlineSectionCell.swift in Sources */,
+				BB3D97132B301CF1007094C8 /* KMRedactPDFView.swift in Sources */,
 				BB853C7E2AF8B5D6009C20C1 /* KMBatchOperateAddPasswordViewController.swift in Sources */,
 				BBEFD0292AFA285A003FABD8 /* KMHeaderFooterTableCellView.swift in Sources */,
 				8942F7C02923670F00389627 /* KMBOTAManagerClass.swift in Sources */,
@@ -15175,6 +15182,7 @@
 				ADD5AE572A64D31200C14249 /* KMPurchaseAlertView.swift in Sources */,
 				BB4F7E8B2B0C55E70077EC8C /* KMFilterAuthorLabel.swift in Sources */,
 				ADF6B86E2A480C5F0090CB78 /* KMComparativeView.swift in Sources */,
+				BB3D97142B301CF1007094C8 /* KMRedactPDFView.swift in Sources */,
 				ADE86AF92B0AF59A00414DFA /* KMCompareContentSettingView.swift in Sources */,
 				BBF729A92B19627500576AC5 /* KMRemoveBackgroundOperationQueue.swift in Sources */,
 				BB1BFF832AEA139F003EB179 /* KMLongerButton.swift in Sources */,

+ 61 - 62
PDF Office/PDF Master/Class/PDFTools/Redact/Controller/KMPDFRedactViewController.swift

@@ -12,11 +12,11 @@ class KMPDFRedactViewController: NSViewController {
     var scaleFactor: CGFloat = 0
     
     /*
-     @property (nonatomic,retain) IBOutlet KMRedactPDFView *redactPdfView;
      @property (nonatomic,copy) void (^callback)(BOOL result,NSInteger currentPageIndex,BOOL saveResult,NSURL *saveUrl);
      @property (nonatomic,copy) void (^titleBack)(NSString *title);
      */
     
+    @IBOutlet var redactPdfView: KMRedactPDFView!
     
     @IBOutlet weak var redactTipLabel: NSTextField!
     @IBOutlet weak var propertiesLabel: NSTextField!
@@ -46,7 +46,7 @@ class KMPDFRedactViewController: NSViewController {
         KMPrint("KMPDFRedactViewController deinit.")
         
         NotificationCenter.default.removeObserver(self)
-//        _redactPdfView.delegate = nil;
+        self.redactPdfView.delegate = nil
     }
     
     convenience init(url: URL, password: String?) {
@@ -99,22 +99,23 @@ class KMPDFRedactViewController: NSViewController {
         if let pwd = self._password, pwd.isEmpty == false {
             document?.unlock(withPassword: pwd)
         }
-//        self.redactPdfView.document = document;
-//    //    self.redactPdfView.scaleFactor = self.scaleFactor;
-//        self.redactPdfView.autoScales = YES;
-//        self.redactPdfView.delegate = self;
+        self.redactPdfView.document = document
+    //    self.redactPdfView.scaleFactor = self.scaleFactor;
+        self.redactPdfView.autoScales = true
+        self.redactPdfView.delegate = self
 //        self.redactPdfView.operationType = KMPDFRedactViewOperationTypeRedact;
-//    //    [self.redactPdfView layoutDocumentView];
+        
+    //    [self.redactPdfView layoutDocumentView];
 
-//         frame = CGRectMake((self.redactPdfView.frame.size.width-32)/2.0, (self.redactPdfView.frame.size.height-32)/2.0, 32, 32);
-//        self._indicator = [[[NSProgressIndicator alloc] initWithFrame:frame] autorelease];
-//        self._indicator.autoresizingMask = NSViewMinXMargin|NSViewMaxXMargin|NSViewMinYMargin|NSViewMaxYMargin;
-//        self._indicator.style = NSProgressIndicatorStyleSpinning;
-//        self._indicator.controlSize = NSControlSizeRegular;
-//        self._indicator.indeterminate = YES;
-//        [self._indicator startAnimation:nil];
-//        [self.redactPdfView addSubview:self.indicator];
-//        self._indicator.hidden = YES;
+        let frame = CGRectMake((self.redactPdfView.frame.size.width-32)/2.0, (self.redactPdfView.frame.size.height-32)/2.0, 32, 32)
+        self._indicator = NSProgressIndicator(frame: frame)
+        self._indicator?.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
+        self._indicator?.style = .spinning
+        self._indicator?.controlSize = .regular
+        self._indicator?.isIndeterminate = true
+        self._indicator?.startAnimation(nil)
+        self.redactPdfView.addSubview(self._indicator!)
+        self._indicator?.isHidden = true
     }
     
     override func viewDidLoad() {
@@ -126,7 +127,7 @@ class KMPDFRedactViewController: NSViewController {
     }
     
     func setCurrentPageIndex(_ currentPageIndex: Int) {
-        
+        self.redactPdfView.go(toPageIndex: currentPageIndex, animated: false)
     }
     
     
@@ -144,13 +145,6 @@ class KMPDFRedactViewController: NSViewController {
              self.titleBack(title);
          }
      }
-
-     #pragma mark - Setter Methods
-
-     - (void)setCurrentPageIndex:(NSInteger)currentPageIndex {
-         [self.redactPdfView goToPageIndex:currentPageIndex animated:NO];
-     }
-
      */
     
     // MARK: - Button Actions
@@ -163,16 +157,19 @@ class KMPDFRedactViewController: NSViewController {
     }
 
     @IBAction func apply_button(_ sender: AnyObject?) {
-//         BOOL isApply = NO;
-//         for (NSUInteger i = 0; i < self.redactPdfView.document.pageCount; i++) {
-//             CPDFPage *page = [self.redactPdfView.document pageAtIndex:i];
-//             for(CPDFAnnotation *annotation in page.annotations) {
-//                 if([annotation isKindOfClass:[CPDFRedactAnnotation class]]) {
-//                     isApply = YES;
-//                 }
-//             }
-//         }
+        var isApply = false
+        for i in 0 ..< self.redactPdfView.document.pageCount {
+            let page = self.redactPdfView.document.page(at: i)
+            for anno in page?.annotations ?? [] {
+                if anno.isKind(of: CPDFRedactAnnotation.self) {
+                     isApply = true
+                 }
+             }
+         }
 //         if(self.redactPdfView.newAddAnnotation.count == 0 && !isApply) return;
+        if !isApply {
+            return
+        }
 
         let returnCode = KMAlertTool.runModelForMainThread_r(message: "", informative: KMLocalizedString("This will permanently remove the redacted information from this document. Once you save this document, you won’t be able to retrieve the redacted information.", nil), buttons: [KMLocalizedString("Apply", nil), KMLocalizedString("Cancel", nil)])
         if returnCode == .alertFirstButtonReturn {
@@ -188,40 +185,38 @@ class KMPDFRedactViewController: NSViewController {
 
     func saveAsPath() {
         let saveAccessCtr = KMSavePanelAccessoryController()
-//         NSString * fileName = [self.redactPdfView.document.documentURL.path.lastPathComponent stringByDeletingPathExtension];
-//         fileName = [NSString stringWithFormat:@"%@_%@.pdf",fileName,@"Redact"];
-//         NSSavePanel * outputSavePanel = [NSSavePanel savePanel];
-//         [outputSavePanel setAllowedFileTypes:[NSArray arrayWithObject:@"pdf"]];
-//         [outputSavePanel setNameFieldStringValue:fileName];
-//         outputSavePanel.accessoryView = saveAccessCtr.view;
-//         [outputSavePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result){
-//             if (result == NSModalResponseOK) {
-//                 [saveAccessCtr retain];
-//                 [self.redactPdfView.document applyRedactions];
-//                 self.indicator.hidden = NO;
-//                 [self.indicator startAnimation:nil];
-//                 NSString * savePDFPath = [outputSavePanel URL].path;
-//
-//                 BOOL isSuccess = [self.redactPdfView.document writeToURL:[NSURL fileURLWithPath:savePDFPath]];
-//                 if (isSuccess) {
+        var fileName = self.redactPdfView.document.documentURL.deletingLastPathComponent().lastPathComponent
+        fileName = String(format: "%@_%@.pdf", fileName, "Redact")
+        let outputSavePanel = NSSavePanel()
+        outputSavePanel.allowedFileTypes = ["pdf"]
+        outputSavePanel.nameFieldStringValue = fileName
+        outputSavePanel.accessoryView = saveAccessCtr.view
+        outputSavePanel.beginSheetModal(for: NSApp.mainWindow!) { result in
+            if result == .OK {
+                self.redactPdfView.document.applyRedactions()
+                self._indicator?.isHidden = false
+                self._indicator?.startAnimation(nil)
+                let savePDFPath = outputSavePanel.url!.path
+
+                let isSuccess = self.redactPdfView.document.write(toFile: savePDFPath)
+                 if (isSuccess) {
 //                     [self.redactPdfView.newAddAnnotation removeAllObjects];
-//                     if (saveAccessCtr.openAutomaticButton.state == NSControlStateValueOn) {
-//                         [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:outputSavePanel.URL display:YES completionHandler:^(NSDocument * _Nullable document, BOOL documentWasAlreadyOpen, NSError * _Nullable error) {
-//                         }];
-//                     } else {
-//                         [self viewFileAtFinder:outputSavePanel.URL.path];
-//                     }
-//                 }
-//                 [self.indicator stopAnimation:nil];
-//                 self.indicator.hidden = YES;
-//                 [saveAccessCtr release];
+                     if (saveAccessCtr.openAutomaticButton.state == .on) {
+                         NSDocumentController.shared.km_safe_openDocument(withContentsOf: outputSavePanel.url!, display: true) { _, _, _ in
+                             
+                         }
+                     } else {
+                         KMTools.viewFile(at: savePDFPath)
+                     }
+                 }
+                self._indicator?.stopAnimation(nil)
+                self._indicator?.isHidden = true
 //
 //                 if (self.callback) {
 //                     self.callback(YES, self.redactPdfView.currentPageIndex,NO,[NSURL fileURLWithPath:savePDFPath]);
 //                 }
-//             }
-//             [saveAccessCtr release];
-//         }];
+             }
+         }
      }
 
     /*
@@ -262,3 +257,7 @@ class KMPDFRedactViewController: NSViewController {
      }
      */
 }
+
+extension KMPDFRedactViewController: CPDFViewDelegate {
+    
+}

+ 1 - 1
PDF Office/PDF Master/Class/PDFTools/Redact/Controller/KMPDFRedactViewController.xib

@@ -203,7 +203,7 @@
                         <constraint firstItem="iwX-y1-ea2" firstAttribute="centerY" secondItem="2S1-Gb-ePE" secondAttribute="centerY" id="sWN-bH-6QS"/>
                     </constraints>
                 </customView>
-                <customView translatesAutoresizingMaskIntoConstraints="NO" id="Ljd-Vm-9rM" customClass="KMRedactPDFView">
+                <customView translatesAutoresizingMaskIntoConstraints="NO" id="Ljd-Vm-9rM" customClass="KMRedactPDFView" customModule="PDF_Master" customModuleProvider="target">
                     <rect key="frame" x="0.0" y="0.0" width="1000" height="321"/>
                 </customView>
             </subviews>

+ 480 - 0
PDF Office/PDF Master/Class/PDFTools/Redact/View/KMRedactPDFView.swift

@@ -0,0 +1,480 @@
+//
+//  KMRedactPDFView.swift
+//  PDF Master
+//
+//  Created by tangchao on 2023/12/18.
+//
+
+import Cocoa
+
+class KMRedactPDFView: CPDFView {
+    
+    /*
+     typedef enum {
+         KMPDFRedactViewOperationTypeNone,//没有进入任何模式
+         KMPDFRedactViewOperationTypeRedact,//标记密文模式
+         KMPDFRedactViewOperationTypeEidtText,//文本编辑
+
+     }KMPDFRedactViewOperationType;
+
+     @interface KMRedactPDFView : CPDFView
+
+     @property (nonatomic,retain) CPDFAnnotation *mouseMoveAnnotation;
+
+     @property (nonatomic,retain) CPDFRedactAnnotation *currentAnnotation;
+
+     @property (nonatomic,retain) NSMutableArray *newAddAnnotation;
+
+     @property (nonatomic,retain) NSMutableArray *activeAnnotations;
+
+     @property (nonatomic,assign) KMPDFRedactViewOperationType operationType;
+
+     @property (nonatomic,assign) BOOL isEidtImageModel;
+     @property (nonatomic,assign) BOOL isEidtTextModel;
+
+     @property (nonatomic, copy) void(^eventColorChanged)(NSColor *color);
+
+     @property (nonatomic, copy) void(^eventFontChanged)(void);
+
+     @property (nonatomic, copy) void(^exportBtnTaped)(NSInteger type);
+
+     - (void)resignMonitor;
+     */
+    
+    /*
+     @property (nonatomic, retain) id localMonitor;
+     */
+
+    override func draw(_ dirtyRect: NSRect) {
+        super.draw(dirtyRect)
+
+        // Drawing code here.
+    }
+    
+    /*
+     - (void)dealloc {
+         [_currentAnnotation release];
+         [_mouseMoveAnnotation release];
+         [_newAddAnnotation release];
+         [_activeAnnotations release];
+         [_localMonitor release];
+         Block_release(_eventColorChanged);
+         Block_release(_eventFontChanged);
+         
+         [super dealloc];
+     }
+
+     - (id)initWithFrame:(NSRect)frameRect {
+         self = [super initWithFrame:frameRect];
+         if (self) {
+             self.operationType = KMPDFRedactViewOperationTypeNone;
+             [self addTrackingArea];
+             [self initMonitor];
+         }
+         return self;
+     }
+
+     - (id)initWithCoder:(NSCoder *)decoder {
+         self = [super initWithCoder:decoder];
+         if (self) {
+             self.operationType = KMPDFRedactViewOperationTypeNone;
+             [self addTrackingArea];
+             [self initMonitor];
+         }
+         return self;
+     }
+
+     - (void)addTrackingArea
+     {
+         self.newAddAnnotation = [NSMutableArray array];
+         self.activeAnnotations = [NSMutableArray array];
+         NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect | NSTrackingActiveInKeyWindow | NSTrackingMouseMoved owner:self userInfo:nil];
+         [self addTrackingArea:trackingArea];
+         [trackingArea release];
+     }
+
+     - (void)initMonitor {
+         NSEventMask mask = NSEventMaskKeyDown;
+         if (!_localMonitor) {
+             _localMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:mask handler:^NSEvent * _Nullable(NSEvent * _Nonnull event) {
+                 unichar eventChar = [event firstCharacter];
+                 NSUInteger modifiers = [event standardModifierFlags];
+
+                 if ((eventChar == NSDeleteCharacter || eventChar == NSDeleteFunctionKey) &&
+                     (modifiers == 0)) {
+                     [self delete];
+                 }
+                 if ([event keyCode] == 36 && modifiers == 0) {
+                     [self corpImageDoneWithEnter];
+                 }
+                 return event;
+             }];
+         }
+     }
+
+     //注销检测器
+     - (void)resignMonitor
+     {
+         [NSEvent removeMonitor:_localMonitor];
+         _localMonitor = nil;
+     }
+
+     - (CGSize)getWidthFromText:(NSString *)text WithSize:(NSFont *)font AboutWidth:(CGFloat)width AndHeight:(CGFloat)height
+     {
+        if (!text) {
+            return CGSizeMake(0, 0);
+        }
+        CGRect rect = [text boundingRectWithSize:CGSizeMake(width, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
+        return rect.size;
+     }
+
+     - (NSMenu *)menuForEvent:(NSEvent *)event {
+         NSMenu *menu = [super menuForEvent:event];
+         if (!menu) {
+             menu = [[[NSMenu alloc] init] autorelease];
+         }
+         NSPoint pagePoint = NSZeroPoint;
+         CPDFPage *page = [self pageAndPoint:&pagePoint forEvent:event nearest:YES];
+         CPDFAnnotation *annotation = [page annotationAtPoint:pagePoint];
+         if(annotation && [annotation isKindOfClass:[CPDFRedactAnnotation class]] && self.operationType == KMPDFRedactViewOperationTypeRedact) {
+            NSMenuItem *item = [menu insertItemWithTitle:NSLocalizedString(@"Delete", nil) action:@selector(deleteAnnotation:) target:self atIndex:0];
+             [item setRepresentedObject:annotation];
+             [menu insertItem:[NSMenuItem separatorItem] atIndex:1];
+             NSMenuItem *itemDefault = [menu insertItemWithTitle:NSLocalizedString(@"Make Current Properties Default", nil) action:@selector(setPropertiesDefault:) target:self atIndex:2];
+             [itemDefault setRepresentedObject:annotation];
+             [menu insertItemWithTitle:NSLocalizedString(@"Properties...", nil) action:@selector(properties:) target:self atIndex:3];
+             [menu insertItem:[NSMenuItem separatorItem] atIndex:4];
+             [menu insertItemWithTitle:NSLocalizedString(@"Repeat Mark Across Pages", nil) action:@selector(repeatMark:) target:self atIndex:5];
+             [menu insertItemWithTitle:NSLocalizedString(@"Apply Redactions", nil) action:@selector(applyRedact:) target:self atIndex:6];
+             self.currentAnnotation = (CPDFRedactAnnotation *)annotation;
+         }
+         return menu;
+     }
+
+     #pragma mark Menu validation
+
+     - (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
+         if (!self.document || [self.document isLocked]) {
+             return NO;
+         }
+         SEL action = [menuItem action];
+         if (action == @selector(deleteAnnotation:)) {
+             return YES;
+         } else if (action == @selector(setPropertiesDefault:)) {
+            
+             return YES;
+         } else if (action == @selector(properties:)) {
+         
+             return YES;
+         } else if (action == @selector(repeatMark:)) {
+             if(self.document.pageCount == 1)
+                 return NO;
+             return YES;
+         } else if (action == @selector(applyRedact:)) {
+             return YES;
+         } else {
+             return [super validateMenuItem:menuItem];
+         }
+     }
+
+     - (NSArray<NSMenuItem *> *)menuItemsEditingAtPoint:(CGPoint)point forPage:(CPDFPage *)page {
+         NSMutableArray <NSMenuItem *>*menuItems = [super menuItemsEditingAtPoint:point forPage:page].mutableCopy;
+         if (!menuItems) {
+             menuItems  = [NSMutableArray array];
+         }
+         if ([self isSelectEditCharRange] || [self isSelecteditAreaWithPoint:point]) {
+             [menuItems insertObject:[NSMenuItem separatorItem] atIndex:0];
+             [menuItems insertObject:[self fontColorMenuItem] atIndex:0];
+             [menuItems insertObject:[self fontSizeMenuItem] atIndex:0];
+         }
+         
+         if (self.editingArea) {
+             if (self.editingArea.IsImageArea) {
+                 [menuItems insertObject:[NSMenuItem separatorItem] atIndex:0];
+     //            [menuItems insertObject:[self imageCutMenuItem] atIndex:0];
+     //            [menuItems insertObject:[self imagePasteMenuItem] atIndex:0];
+                 [menuItems insertObject:[self imageExportMenuItem] atIndex:0];
+                 [menuItems insertObject:[self imageRotateMenuItem] atIndex:0];
+             }
+         }
+         return menuItems;
+         
+     }
+
+
+     - (void)mouseMoved:(NSEvent *)theEvent {
+         [self.window mouseMoved:theEvent];
+         [super mouseMoved:theEvent];
+         if(self.operationType != KMPDFRedactViewOperationTypeRedact) return;
+         NSPoint pagePoint = NSZeroPoint;
+         CPDFPage *page = [self pageAndPoint:&pagePoint forEvent:theEvent nearest:YES];
+         NSPoint newpoint = [self convertPoint:[theEvent locationInWindow] fromView:nil];
+         CPDFAreaOfInterest area = [self areaOfInterestForPoint:newpoint];
+         if (area & CPDFTextArea) {
+             [[NSCursor IBeamCursor] set];
+         }else{
+             [[NSCursor arrowCursor] set];
+         }
+         CPDFAnnotation *newActiveAnnotation = [page annotationAtPoint:pagePoint];
+         if (newActiveAnnotation && [newActiveAnnotation isKindOfClass:[CPDFRedactAnnotation class]] && (self.mouseMoveAnnotation == newActiveAnnotation)) {
+              [(CPDFRedactAnnotation *)newActiveAnnotation setDrawRedactionsAsRedacted:YES];
+             [self setNeedsDisplayAnnotationViewForPage:page];
+         } else if(self.mouseMoveAnnotation && [self.mouseMoveAnnotation isKindOfClass:[CPDFRedactAnnotation class]]){
+             [(CPDFRedactAnnotation *)self.mouseMoveAnnotation setDrawRedactionsAsRedacted:NO];
+            [self setNeedsDisplayAnnotationViewForPage:page];
+         }
+         self.mouseMoveAnnotation = newActiveAnnotation;
+     }
+
+     - (void)mouseDown:(NSEvent *)theEvent {
+         NSPoint pagePoint = NSZeroPoint;
+         if(self.operationType != KMPDFRedactViewOperationTypeRedact) return;
+         CPDFPage *page = [self pageAndPoint:&pagePoint forEvent:theEvent nearest:YES];
+         CPDFAnnotation *newActiveAnnotation = [page annotationAtPoint:pagePoint];
+         NSPoint newpoint = [self convertPoint:[theEvent locationInWindow] fromView:nil];
+         CPDFAreaOfInterest area = [self areaOfInterestForPoint:newpoint];
+         [self.activeAnnotations removeAllObjects]; //预留
+         if(newActiveAnnotation) {
+             if(![self.activeAnnotations containsObject:newActiveAnnotation]) {
+                 [self.activeAnnotations addObject:newActiveAnnotation];
+             }
+             [self setNeedsDisplayAnnotationViewForPage:page];
+             [self doDragMouseWithEvent:theEvent];
+         } else if(area & CPDFTextArea) {
+             [super mouseDown:theEvent];
+             [self doMarkUpWithEvent:theEvent];
+             self.currentSelection = nil;
+         } else {
+             [self doRedactWithEvent:theEvent];
+         }
+     }
+
+     #pragma mark - keyDown
+
+     - (void)delete {
+         [self.activeAnnotations enumerateObjectsUsingBlock:^(CPDFAnnotation *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
+             if([obj isKindOfClass:[CPDFRedactAnnotation class]]) {
+                 [self removeAnnotation:obj];
+             }
+         }];
+     }
+
+
+     - (void)corpImageDoneWithEnter {
+         if([self.editingArea isKindOfClass:[CPDFEditImageArea class]]) {
+             CPDFEditImageArea *editImageArea = (CPDFEditImageArea *)self.editingArea;
+             if(editImageArea.isCropMode) {
+                 [self cropEditImageArea:editImageArea withBounds:editImageArea.cropRect];
+                 [self exitCropWithEditImageArea:editImageArea];
+             }
+         }
+     }
+
+     #pragma mark - Rendering
+
+     - (void)drawPage:(CPDFPage *)page toContext:(CGContextRef)context
+     {
+         [self.activeAnnotations enumerateObjectsUsingBlock:^(CPDFAnnotation *annotation, NSUInteger idx, BOOL * _Nonnull stop) {
+             if (annotation.page && [annotation.page isEqual:page]) {
+                 [annotation drawSelectionHighlightForView:self inContext:context];
+             }
+         }];
+     }
+
+     - (BOOL)doDragMouseWithEvent:(NSEvent *)theEvent
+     {
+         BOOL didDrag = NO;;
+         while (YES) {
+             if ([[[self window] nextEventMatchingMask: NSEventMaskLeftMouseUp | NSEventMaskLeftMouseDragged] type] == NSEventTypeLeftMouseUp)
+                 break;
+             didDrag = YES;
+         }
+         return didDrag;
+     }
+
+     - (void)doMarkUpWithEvent:(NSEvent *)theEvent
+     {
+         NSUInteger eventMask = NSEventMaskLeftMouseUp | NSEventMaskLeftMouseDragged;
+         
+         while (YES) {
+             theEvent = [[self window] nextEventMatchingMask:eventMask];
+             if ([theEvent type] == NSEventTypeLeftMouseUp) {
+                 if (self.currentSelection) {
+                     CPDFPage *page = self.currentSelection.page;
+                     CPDFAnnotation *annotation = [self addRedactPDFSelection:self.currentSelection];
+                     [annotation setModificationDate:[NSDate date]];
+                     NSString *userName = [[NSUserDefaults standardUserDefaults] stringForKey:@"SKUserName"];
+                     [annotation setUserName:userName ? : NSFullUserName()];
+                     
+                     if ([annotation isKindOfClass:[CPDFRedactAnnotation class]]) {
+                         [annotation setBorderWidth:10];
+                         [(CPDFRedactAnnotation *)annotation setBorderColor:[KMPDFAnnotationRedactConfig sharedInstance].redactOutlineColor];
+                         [(CPDFRedactAnnotation *)annotation setInteriorColor:[KMPDFAnnotationRedactConfig sharedInstance].redactFillColor];
+                         [(CPDFRedactAnnotation *)annotation setFontColor:[KMPDFAnnotationRedactConfig sharedInstance].redactFontColor];
+                         if([KMPDFAnnotationRedactConfig sharedInstance].overlayText) {
+                             [(CPDFRedactAnnotation *)annotation setAlignment:[KMPDFAnnotationRedactConfig sharedInstance].textAlignment];
+                             NSFont* font = [NSFont fontWithName:@"Helvetica" size:[KMPDFAnnotationRedactConfig sharedInstance].fontSize];
+                             [(CPDFRedactAnnotation *)annotation setFont:font];
+                             [(CPDFRedactAnnotation *)annotation setOverlayText:[KMPDFAnnotationRedactConfig sharedInstance].overlayTextString];
+                         }
+                     }
+                     [self addAnnotation:annotation toPage:page];
+                     [self.newAddAnnotation addObject:annotation];
+                     [self setNeedsDisplayForPage:page];
+                 }
+                 break;
+             } else if([theEvent type] == NSEventTypeLeftMouseDragged) {
+                 [super mouseDragged:theEvent];
+             }
+         }
+     }
+
+     - (void)doRedactWithEvent:(NSEvent *)theEvent
+     {
+         NSPoint point = NSZeroPoint;
+         CPDFPage *page = [self pageAndPoint:&point forEvent:theEvent nearest:YES];
+         BOOL wasMouseCoalescingEnabled = [NSEvent isMouseCoalescingEnabled];
+
+         NSWindow *window = [self window];
+         NSBezierPath *bezierPath = nil;
+         CAShapeLayer *layer = nil;
+
+         NSRect boxBounds = page.bounds;
+         CGAffineTransform t = CGAffineTransformRotate(CGAffineTransformMakeScale([self scaleFactor], [self scaleFactor]), -M_PI_2 * [page rotation] / 90.0);
+         layer = [CAShapeLayer layer];
+         [layer setBounds:NSRectToCGRect(boxBounds)];
+         [layer setAnchorPoint:CGPointZero];
+         [layer setPosition:NSPointToCGPoint([self convertPoint:boxBounds.origin fromPage:page])];
+         [layer setAffineTransform:t];
+         [layer setZPosition:1.0];
+         [layer setMasksToBounds:YES];
+         [layer setFillColor:[KMPDFAnnotationRedactConfig sharedInstance].redactFillColor.CGColor];
+         [layer setStrokeColor:CGColorGetConstantColor(kCGColorBlack)];
+         [layer setLineJoin:kCALineJoinRound];
+         [layer setLineCap:kCALineCapRound];
+         
+         NSEvent *lastMouseEvent = theEvent;
+         SKRectEdges  resizeHandle = SKMinYEdgeMask | SKMaxXEdgeMask;
+         CGRect originalBounds = CGRectMake(point.x, point.y, 0, 0);
+         [[self layer] addSublayer:layer];
+         
+         NSUInteger eventMask = NSEventMaskLeftMouseUp | NSEventMaskLeftMouseDragged;
+         CGRect rect = CGRectZero;
+         while (YES) {
+             theEvent = [window nextEventMatchingMask:eventMask];
+             if ([theEvent type] == NSEventTypeLeftMouseUp) {
+                 if (rect.size.width < MIN_NOTE_SIZE || rect.size.height < MIN_NOTE_SIZE) {
+                     break;
+                 }
+                 NSMutableArray *quadrilateralPoints = [NSMutableArray array];
+                 CPDFRedactAnnotation *annotation = [[[CPDFRedactAnnotation alloc] initWithDocument:self.document] autorelease];
+                 CGRect bounds = rect;
+                 [quadrilateralPoints addObject:[NSValue valueWithPoint:CGPointMake(CGRectGetMinX(bounds), CGRectGetMaxY(bounds))]];
+                 [quadrilateralPoints addObject:[NSValue valueWithPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))]];
+                 [quadrilateralPoints addObject:[NSValue valueWithPoint:CGPointMake(CGRectGetMinX(bounds), CGRectGetMinY(bounds))]];
+                 [quadrilateralPoints addObject:[NSValue valueWithPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMinY(bounds))]];
+                 [annotation setQuadrilateralPoints:quadrilateralPoints];
+                 [annotation setModificationDate:[NSDate date]];
+                 NSString *userName = [[NSUserDefaults standardUserDefaults] stringForKey:@"SKUserName"];
+                 [annotation setUserName:userName ? : NSFullUserName()];
+                 
+                 if ([annotation isKindOfClass:[CPDFRedactAnnotation class]]) {
+                     [annotation setBorderWidth:10];
+                     [(CPDFRedactAnnotation *)annotation setBorderColor:[KMPDFAnnotationRedactConfig sharedInstance].redactOutlineColor];
+                     [(CPDFRedactAnnotation *)annotation setInteriorColor:[KMPDFAnnotationRedactConfig sharedInstance].redactFillColor];
+                     [(CPDFRedactAnnotation *)annotation setFontColor:[KMPDFAnnotationRedactConfig sharedInstance].redactFontColor];
+                     if([KMPDFAnnotationRedactConfig sharedInstance].overlayText) {
+                         [(CPDFRedactAnnotation *)annotation setAlignment:[KMPDFAnnotationRedactConfig sharedInstance].textAlignment];
+                         NSFont* font = [NSFont fontWithName:@"Helvetica" size:[KMPDFAnnotationRedactConfig sharedInstance].fontSize];
+                         [(CPDFRedactAnnotation *)annotation setFont:font];
+                         [(CPDFRedactAnnotation *)annotation setOverlayText:[KMPDFAnnotationRedactConfig sharedInstance].overlayTextString];
+                     }
+                 }
+                 [self addAnnotation:annotation toPage:page];
+                 [self.newAddAnnotation addObject:annotation];
+                 break;
+             } else if ([theEvent type] == NSEventTypeLeftMouseDragged) {
+                 rect = [self doResizeLinkWithEvent:lastMouseEvent fromPoint:point originalBounds:originalBounds page:page resizeHandle:&resizeHandle];
+                 bezierPath = [NSBezierPath bezierPathWithRect:rect];
+                 [layer setPath:[bezierPath CGPath]];
+                 lastMouseEvent = theEvent;
+             }
+         }
+         
+         [layer removeFromSuperlayer];
+
+         [NSEvent setMouseCoalescingEnabled:wasMouseCoalescingEnabled];
+     }
+
+
+
+     - (CPDFPage *)pageAndPoint:(NSPoint *)point forEvent:(NSEvent *)event nearest:(BOOL)nearest {
+         NSPoint p = [event locationInView:self];
+         CPDFPage *page = [self pageForPoint:p nearest:nearest];
+         if (page && point)
+             *point = [self convertPoint:p toPage:page];
+         return page;
+     }
+
+     - (CGRect)doResizeLinkWithEvent:(NSEvent *)theEvent fromPoint:(NSPoint)originalPagePoint originalBounds:(NSRect)originalBounds page:(CPDFPage*)page resizeHandle:(SKRectEdges *)resizeHandlePtr
+     {
+         NSPoint currentPagePoint = [self convertPoint:[theEvent locationInView:self] toPage:page];
+         
+         NSRect newBounds = originalBounds;
+         NSRect pageBounds = page.bounds;
+         NSPoint relPoint = SKSubstractPoints(currentPagePoint, originalPagePoint);
+         SKRectEdges resizeHandle = *resizeHandlePtr;
+         
+         if (NSEqualSizes(originalBounds.size, NSZeroSize)) {
+             SKRectEdges currentResizeHandle = (relPoint.x < 0.0 ? SKMinXEdgeMask : SKMaxXEdgeMask) | (relPoint.y <= 0.0 ? SKMinYEdgeMask : SKMaxYEdgeMask);
+             if (currentResizeHandle != resizeHandle) {
+                 *resizeHandlePtr = resizeHandle = currentResizeHandle;
+             }
+         }
+         CGFloat minWidth = MIN_NOTE_SIZE;
+         CGFloat minHeight = MIN_NOTE_SIZE;
+         
+         if ((resizeHandle & SKMaxXEdgeMask)) {
+             newBounds.size.width += relPoint.x;
+             if (NSMaxX(newBounds) > NSMaxX(pageBounds))
+                 newBounds.size.width = NSMaxX(pageBounds) - NSMinX(newBounds);
+             if (NSWidth(newBounds) < minWidth) {
+                 newBounds.size.width = minWidth;
+             }
+         } else if ((resizeHandle & SKMinXEdgeMask)) {
+             newBounds.origin.x += relPoint.x;
+             newBounds.size.width -= relPoint.x;
+             if (NSMinX(newBounds) < NSMinX(pageBounds)) {
+                 newBounds.size.width = NSMaxX(newBounds) - NSMinX(pageBounds);
+                 newBounds.origin.x = NSMinX(pageBounds);
+             }
+             if (NSWidth(newBounds) < minWidth) {
+                 newBounds.origin.x = NSMaxX(newBounds) - minWidth;
+                 newBounds.size.width = minWidth;
+             }
+         }
+         if ((resizeHandle & SKMaxYEdgeMask)) {
+             newBounds.size.height += relPoint.y;
+             if (NSMaxY(newBounds) > NSMaxY(pageBounds)) {
+                 newBounds.size.height = NSMaxY(pageBounds) - NSMinY(newBounds);
+             }
+             if (NSHeight(newBounds) < minHeight) {
+                 newBounds.size.height = minHeight;
+             }
+         } else if ((resizeHandle & SKMinYEdgeMask)) {
+             newBounds.origin.y += relPoint.y;
+             newBounds.size.height -= relPoint.y;
+             if (NSMinY(newBounds) < NSMinY(pageBounds)) {
+                 newBounds.size.height = NSMaxY(newBounds) - NSMinY(pageBounds);
+                 newBounds.origin.y = NSMinY(pageBounds);
+             }
+             if (NSHeight(newBounds) < minHeight) {
+                 newBounds.origin.y = NSMaxY(newBounds) - minHeight;
+                 newBounds.size.height = minHeight;
+             }
+         }
+         return newBounds;
+     }
+     */
+    
+}

+ 1 - 1
PDF Office/PDF Master/Class/PDFWindowController/Toolbar/KMToolbarController.swift

@@ -158,7 +158,7 @@ class KMToolbarController: NSViewController {
                 bottomOffset.constant = 0
                 childToolBarBox.isHidden = true
                 self.delegate?.toolbarController?(self, heightOffsetChange: 40+8)
-            } else if _toolbarType == .Page || _toolbarType == .LeftPanel {
+            } else if _toolbarType == .Page || _toolbarType == .LeftPanel || _toolbarType == .redact {
                 bottomOffset.constant = 0
                 childToolBarBox.isHidden = true
                 self.delegate?.toolbarController?(self, heightOffsetChange: 40+8)