CPDFListView.m 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. //
  2. // CPDFListView.m
  3. // ComPDFKit
  4. //
  5. // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. #import "CPDFListView.h"
  13. #import "CPDFAnnotationModel.h"
  14. #import "CStampSignatureObject.h"
  15. #import "CSelfSignAnnotation.h"
  16. #import "CSelfSignAnnotationFreeText.h"
  17. #import "CPDFListViewConfig.h"
  18. #import "CPDFListEditAnnotationViewController.h"
  19. #import "CPDFListView+Private.h"
  20. #import "CPDFListView+Event.h"
  21. #import "CPDFListView+Tool.h"
  22. #import "CPDFAnnotation+PDFListView.h"
  23. #import "CPDFSelection+PDFListView.h"
  24. #import "CPDFPage+PDFListView.h"
  25. #import "CPDFListView+Extension.h"
  26. #import "NSString+PDFListView.h"
  27. #import "NSImage+PDFListView.h"
  28. #import "CPDFListStampAnnotation+Private.h"
  29. #import "CPDFListSignatureAnnotation+Private.h"
  30. #import "CPDFMarkupAnnotation+PDFListView.h"
  31. #import "CPDFButtonWidgetAnnotation+PDFListView.h"
  32. #import "CPDFTextWidgetAnnotation+PDFListView.h"
  33. #import "CPDFChoiceWidgetAnnotation+PDFListView.h"
  34. #import "CPDFChoiceWidgetAnnotation+PDFListView.h"
  35. #import "CPDFLineAnnotation+PDFListView.h"
  36. #import "NSPopover+PDFListView.h"
  37. NSNotificationName const CPDFListViewSelectionChangedNotification = @"CPDFListViewSelectionChangedNotification";
  38. NSNotificationName const CPDFListViewToolModeChangeNotification = @"CPDFListViewToolModeChangeNotification";
  39. NSNotificationName const CPDFListViewAnnotationTypeChangeNotification = @"CPDFListViewAnnotationTypeChangeNotification";
  40. NSNotificationName const CPDFListViewMagnificationChangedNotification = @"CPDFListViewMagnificationChangedNotification";
  41. NSNotificationName const CPDFListViewDidAddAnnotationNotification = @"CPDFListViewDidAddAnnotationNotification";
  42. NSNotificationName const CPDFListViewDidRemoveAnnotationNotification = @"CPDFListViewDidRemoveAnnotationNotification";
  43. NSNotificationName const CPDFListViewActiveAnnotationsChangeNotification = @"CPDFListViewActiveAnnotationsChangeNotification";
  44. @implementation CPDFListView
  45. + (void)configDefaultValues {
  46. }
  47. #pragma mark - Init
  48. - (id)init {
  49. self = [super init];
  50. if (self) {
  51. [self commonInitialization];
  52. }
  53. return self;
  54. }
  55. - (id)initWithFrame:(NSRect)frameRect {
  56. self = [super initWithFrame:frameRect];
  57. if (self) {
  58. [self commonInitialization];
  59. }
  60. return self;
  61. }
  62. - (id)initWithCoder:(NSCoder *)decoder {
  63. self = [super initWithCoder:decoder];
  64. if (self) {
  65. [self commonInitialization];
  66. }
  67. return self;
  68. }
  69. - (void)commonInitialization {
  70. self.selectionPageIndex = NSNotFound;
  71. self.clickLineAnnotation = nil;
  72. self.isClickDoubleCreatLine = NO;
  73. self.activeAnnotations = [NSMutableArray array];
  74. self.selectAnnotations = [NSMutableArray array];
  75. self.dragHoverPoints = [NSMutableArray array];
  76. self.aCopyAnnotations = [NSMutableArray array];
  77. }
  78. #pragma mark - Setter & Get
  79. - (void)setDocument:(CPDFDocument *)document {
  80. @synchronized (self) {
  81. self.selectionRect = NSZeroRect;
  82. self.selectionPageIndex = NSNotFound;
  83. }
  84. // self.annotationType = CAnnotationTypeSquare;
  85. // self.toolMode = CSelectToolMode;
  86. //
  87. [super setDocument:document];
  88. NSURL *initialUserDefaultsURL = [[NSBundle mainBundle] URLForResource:@"InitialUserDefaults" withExtension:@"plist"];
  89. NSDictionary *initialUserDefaultsDict = [NSDictionary dictionaryWithContentsOfURL:initialUserDefaultsURL];
  90. NSDictionary *initialValuesDict = [initialUserDefaultsDict objectForKey:@"RegisteredDefaults"];
  91. NSArray *resettableUserDefaultsKeys;
  92. // set them in the standard user defaults
  93. [[NSUserDefaults standardUserDefaults] registerDefaults:initialValuesDict];
  94. [self.window makeFirstResponder:self];
  95. [NSImage makePDFListViewCursorImages];
  96. }
  97. -(void)setToolMode:(CToolMode)newToolMode {
  98. if(newToolMode != _toolMode) {
  99. if ((CTextToolMode == _toolMode || CNoteToolMode == _toolMode) && CTextToolMode != newToolMode) {
  100. if (newToolMode != CNoteToolMode && self.activeAnnotation)
  101. [self updateActiveAnnotations:@[]];
  102. if ([self currentSelection])
  103. [self setCurrentSelection:nil];
  104. } else if (_toolMode == CSelectToolMode && NSEqualRects(self.selectionRect, NSZeroRect) == NO) {
  105. self.selectionRect = NSZeroRect;
  106. dispatch_async(dispatch_get_main_queue(), ^{
  107. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFViewSelectionChangedNotification object:self];
  108. });
  109. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangedSelectionOrMagnification:)])
  110. [self.pdfListViewDelegate PDFListViewChangedSelectionOrMagnification:self];
  111. }
  112. if(CFormToolMode == newToolMode || CFormToolMode == _toolMode)
  113. [self setNeedsDisplayAnnotationViewForVisiblePages];
  114. _toolMode = newToolMode;
  115. self.selectZoomToolModeZoomOut = NO;
  116. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangedToolMode:forToolMode:)])
  117. [self.pdfListViewDelegate PDFListViewChangedToolMode:self forToolMode:newToolMode];
  118. dispatch_async(dispatch_get_main_queue(), ^{
  119. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewToolModeChangeNotification object:self];
  120. });
  121. [self resetPDFViewAnnotation];
  122. }
  123. }
  124. - (void)setAnnotationType:(CAnnotationType)annotationType {
  125. if (_annotationType != annotationType) {
  126. _annotationType = annotationType;
  127. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangedAnnotationType:forAnnotationType:)])
  128. [self.pdfListViewDelegate PDFListViewChangedAnnotationType:self forAnnotationType:annotationType];
  129. dispatch_async(dispatch_get_main_queue(), ^{
  130. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewAnnotationTypeChangeNotification object:self];
  131. });
  132. }
  133. [self resetPDFViewAnnotation];
  134. }
  135. - (NSRect)currentSelectionRect {
  136. if (CSelectToolMode == self.toolMode)
  137. return self.selectionRect;
  138. return NSZeroRect;
  139. }
  140. - (CPDFAnnotation *)activeAnnotation {
  141. return self.activeAnnotations.lastObject;
  142. }
  143. #pragma mark - Public
  144. - (CPDFAnnotation *)addAnnotationWithType:(CAnnotationType)annotationType selection:(CPDFSelection *)selection page:(CPDFPage *)page bounds:(NSRect)bounds {
  145. CPDFAnnotation *annotation = nil;
  146. NSString *text = [selection PDFListViewCleanedString];
  147. if([CPDFListView isMarkupAnnotationType:annotationType]) {
  148. if(selection){
  149. annotation = [self addPDFSelection:selection annotationTyoe:annotationType];
  150. [annotation setContents:text?:@""];
  151. } else NSBeep();
  152. } else {
  153. BOOL isInitial = NO;
  154. if(selection) {
  155. CGRect noteRect = selection.bounds;
  156. isInitial = NSEqualSizes(noteRect.size, NSZeroSize);
  157. } else {
  158. isInitial = NSEqualSizes(bounds.size, NSZeroSize);
  159. }
  160. CPDFAnnotationModel *annotationModel = [[CPDFAnnotationModel alloc]initWithAnnotationType:annotationType];
  161. CGSize size = CGSizeMake(annotationModel.noteWidth, annotationModel.noteHeight);
  162. if (isInitial)
  163. bounds = annotationType == CAnnotationTypeAnchored ? CPDFListViewRectFromCenterAndSize(bounds.origin, size) : CPDFListViewRectFromCenterAndSquareSize(bounds.origin, MIN_NOTE_SIZE);
  164. switch (annotationType) {
  165. case CAnnotationTypeFreeText:
  166. annotation = [[CPDFFreeTextAnnotation alloc]initPDFListViewNoteWithDocument:self.document];
  167. annotation.bounds = bounds;
  168. break;
  169. case CAnnotationTypeAnchored:
  170. annotation = [[CPDFTextAnnotation alloc]initPDFListViewNoteWithDocument:self.document];
  171. annotation.bounds = bounds;
  172. break;
  173. case CAnnotationTypeCircle:
  174. annotation = [[CPDFCircleAnnotation alloc]initPDFListViewNoteWithDocument:self.document];
  175. annotation.bounds = bounds;
  176. break;
  177. case CAnnotationTypeSquare:
  178. annotation = [[CPDFSquareAnnotation alloc]initPDFListViewNoteWithDocument:self.document];
  179. annotation.bounds = bounds;
  180. break;
  181. case CAnnotationTypeLine:
  182. annotation = [[CPDFLineAnnotation alloc]initPDFListViewNoteWithDocument:self.document];
  183. [(CPDFLineAnnotation *)annotation setObservedStartPoint:CGPointMake(bounds.origin.x, bounds.origin.y)];
  184. [(CPDFLineAnnotation *)annotation setObservedEndPoint:CGPointMake(bounds.origin.x + bounds.size.width,bounds.origin.y+ bounds.size.height)];
  185. [(CPDFLineAnnotation *)annotation setStartLineStyle:CPDFLineStyleNone];
  186. [(CPDFLineAnnotation *)annotation setEndLineStyle:CPDFLineStyleNone];
  187. break;
  188. case CAnnotationTypeArrow: {
  189. CPDFAnnotationModel *annotationModel = [[CPDFAnnotationModel alloc] initWithAnnotationType:CAnnotationTypeArrow];
  190. annotation = [[CPDFLineAnnotation alloc]initPDFListViewNoteWithDocument:self.document];
  191. [(CPDFLineAnnotation *)annotation setObservedStartPoint:CGPointMake(bounds.origin.x, bounds.origin.y)];
  192. [(CPDFLineAnnotation *)annotation setObservedEndPoint:CGPointMake(bounds.origin.x + bounds.size.width,bounds.origin.y+ bounds.size.height)];
  193. [(CPDFLineAnnotation *)annotation setStartLineStyle:annotationModel.startLineStyle];
  194. [(CPDFLineAnnotation *)annotation setEndLineStyle:annotationModel.endLineStyle];
  195. }
  196. break;
  197. case CAnnotationTypeLink:
  198. annotation = [[CPDFLinkAnnotation alloc] initPDFListViewNoteWithDocument:self.document];
  199. annotation.bounds = bounds;
  200. break;
  201. case CAnnotationTypeTextField:
  202. annotation = [[CPDFTextWidgetAnnotation alloc] initPDFListViewNoteWithDocument:self.document];
  203. annotation.bounds = bounds;
  204. break;
  205. case CAnnotationTypeCheckBox:
  206. annotation = [[CPDFButtonWidgetAnnotation alloc] initPDFListViewNoteWithDocument:self.document controlType:CPDFWidgetCheckBoxControl];
  207. annotation.bounds = bounds;
  208. break;
  209. case CAnnotationTypeRadioButton:
  210. annotation = [[CPDFButtonWidgetAnnotation alloc] initPDFListViewNoteWithDocument:self.document controlType:CPDFWidgetRadioButtonControl];
  211. annotation.bounds = bounds;
  212. break;
  213. case CAnnotationTypeListMenu:
  214. annotation = [[CPDFChoiceWidgetAnnotation alloc] initPDFListViewNoteWithDocument:self.document listChoice:YES];
  215. annotation.bounds = bounds;
  216. break;
  217. case CAnnotationTypeComboBox:
  218. annotation = [[CPDFChoiceWidgetAnnotation alloc] initPDFListViewNoteWithDocument:self.document listChoice:NO];
  219. annotation.bounds = bounds;
  220. break;
  221. case CAnnotationTypeSignature:
  222. annotation = [[CPDFSignatureWidgetAnnotation alloc] initPDFListViewNoteWithDocument:self.document];
  223. annotation.bounds = bounds;
  224. break;
  225. case CAnnotationTypeActionButton:
  226. annotation = [[CPDFButtonWidgetAnnotation alloc] initPDFListViewNoteWithDocument:self.document controlType:CPDFWidgetPushButtonControl];
  227. annotation.bounds = bounds;
  228. break;
  229. case CAnnotationTypeSignFalse:
  230. case CAnnotationTypeSignTure:
  231. case CAnnotationTypeSignCircle:
  232. case CAnnotationTypeSignLine:
  233. case CAnnotationTypeSignDot:
  234. annotation = [[CSelfSignAnnotation alloc]initPDFListViewNoteWithDocument:self.document type:self.annotationType];
  235. annotation.bounds = bounds;
  236. [(CSelfSignAnnotation *)annotation updateAppearanceStream];
  237. break;
  238. case CAnnotationTypeSignText:
  239. case CAnnotationTypeSignConfig:
  240. case CAnnotationTypeSignDate:
  241. annotation = [[CSelfSignAnnotationFreeText alloc]initPDFListViewNoteWithDocument:self.document subType:self.annotationType string:@"" bounds:bounds];
  242. break;
  243. default:
  244. break;
  245. }
  246. }
  247. if (annotation) {
  248. [self addAnnotation:annotation toPage:page];
  249. }
  250. return annotation;
  251. }
  252. - (void)drawAnnotation:(CPDFAnnotation *)annotation toContext:(CGContextRef)context {
  253. [super drawAnnotation:annotation toContext:context];
  254. if (CFormToolMode == _toolMode &&
  255. [annotation isKindOfClass:[CPDFWidgetAnnotation class]]) {
  256. CPDFWidgetAnnotation *annotationWidget = (CPDFWidgetAnnotation*)annotation;
  257. if (annotationWidget.fieldName.length > 0) {
  258. CGContextSaveGState(context);
  259. NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
  260. paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
  261. paragraphStyle.alignment = NSTextAlignmentLeft;
  262. NSColor *backgroundColor = [NSColor blackColor];
  263. if ([self.activeAnnotations containsObject:annotation]) {
  264. backgroundColor = [NSColor colorWithRed:82.0/255.0 green:102.0/255.0 blue:204.0/255.0 alpha:1.0];
  265. }
  266. NSDictionary *attributes = @{NSFontAttributeName : [NSFont systemFontOfSize:12],
  267. NSForegroundColorAttributeName : [NSColor whiteColor],
  268. NSBackgroundColorAttributeName : backgroundColor,
  269. NSParagraphStyleAttributeName : paragraphStyle};
  270. NSString* drawString = annotationWidget.fieldName;
  271. drawString = [drawString getMaxStringWithBounds:annotationWidget.bounds attributes:attributes];
  272. CGSize size = [drawString sizeWithAttributes:attributes];
  273. CGRect drawRect = annotationWidget.bounds;
  274. drawRect.origin.y += (drawRect.size.height - size.height)/2.0;
  275. drawRect.origin.x += (drawRect.size.width - size.width)/2.0;
  276. drawRect.size.height = size.height;
  277. drawRect.size.width = size.width;
  278. [NSGraphicsContext saveGraphicsState];
  279. [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]];
  280. [drawString drawInRect:drawRect withAttributes:attributes];
  281. [NSGraphicsContext restoreGraphicsState];
  282. CGContextRestoreGState(context);
  283. NSRect rect = [self integralRect:[annotationWidget bounds] onPage:[annotationWidget page]];
  284. CGFloat lineWidth = [self unitWidthOnPage:[annotationWidget page]];
  285. CGContextSaveGState(context);
  286. CGColorRef color = [NSColor blackColor].CGColor;
  287. CGContextSetStrokeColorWithColor(context, color);
  288. CGContextStrokeRectWithWidth(context, CGRectInset(NSRectToCGRect(rect), 0,0), lineWidth);
  289. }
  290. } else if(CNoteToolMode== _toolMode && _annotationType == CAnnotationTypeLink && [annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
  291. CGContextSaveGState(context);
  292. NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
  293. paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
  294. paragraphStyle.alignment = NSTextAlignmentLeft;
  295. NSColor *backgroundColor = [NSColor clearColor];
  296. NSDictionary *attributes = @{NSFontAttributeName : [NSFont systemFontOfSize:12],
  297. NSForegroundColorAttributeName : [NSColor whiteColor],
  298. NSBackgroundColorAttributeName : backgroundColor,
  299. NSParagraphStyleAttributeName : paragraphStyle};
  300. NSString *drawString = NSLocalizedString(@"No Destination", nil);
  301. CPDFDestination *destination = [(CPDFLinkAnnotation *)annotation destination];
  302. if(destination) {
  303. CPDFPage *page = destination.page;
  304. NSString * url = [(CPDFLinkAnnotation *)annotation URL];
  305. if(destination.page) {
  306. NSUInteger index = [self.document indexForPage:page] + 1;
  307. drawString = [NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"to Page",nil),@(index)];
  308. } else if(url.length > 0) {
  309. drawString = url;
  310. }
  311. }
  312. drawString = [drawString getMaxStringWithBounds:annotation.bounds attributes:attributes];
  313. CGSize size = [drawString sizeWithAttributes:attributes];
  314. CGRect drawRect = annotation.bounds;
  315. drawRect.origin.y += (drawRect.size.height - size.height)/2.0;
  316. drawRect.origin.x += (drawRect.size.width - size.width)/2.0;
  317. drawRect.size.height = size.height;
  318. drawRect.size.width = size.width;
  319. CGContextSaveGState(context);
  320. [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]];
  321. CGContextSetFillColorWithColor(context, [NSColor blueColor].CGColor);
  322. CGContextFillRect(context,annotation.bounds);
  323. CGContextRestoreGState(context);
  324. [NSGraphicsContext saveGraphicsState];
  325. [drawString drawInRect:drawRect withAttributes:attributes];
  326. [NSGraphicsContext restoreGraphicsState];
  327. CGContextRestoreGState(context);
  328. }
  329. }
  330. - (void)setAddStampObject:(CStampSignatureObject *)stampObject keepToolModel:(BOOL)isToolModel {
  331. if (CAnnotationTypeStamp == self.annotationType ||
  332. CAnnotationTypeSignSignature == self.annotationType) {
  333. self.stampObject = stampObject;
  334. self.isStampModel = isToolModel;
  335. }
  336. }
  337. - (void)addAnnotationWithImage:(NSImage *)image page:(CPDFPage *)page point:(NSPoint)point {
  338. NSRect bounds = NSZeroRect;
  339. CPDFSelection *selection = [self currentSelection];
  340. CGFloat defaultWidth = 360;
  341. CGFloat defaultHeight = 90;
  342. NSSize defaultSize = ([page rotation] % 180 == 0) ? NSMakeSize(defaultWidth, defaultHeight) : NSMakeSize(defaultHeight, defaultWidth);
  343. bounds = CPDFListViewRectFromCenterAndSize(point, defaultSize);
  344. // Make sure it fits in the page
  345. bounds = CPDFListViewConstrainRect(bounds, [page boundsForBox:[self displayBox]]);
  346. if (page != nil) {
  347. BOOL isInitial = NSEqualSizes(bounds.size, NSZeroSize) && selection == nil;
  348. // new note added by note tool mode, don't add actual zero sized notes
  349. if (isInitial)
  350. bounds = CPDFListViewRectFromCenterAndSquareSize(bounds.origin, 8.0);
  351. CGFloat borderDefaultWidth = image.size.width;
  352. CGFloat borderScale = borderDefaultWidth/image.size.height;
  353. if (bounds.size.width/bounds.size.height > borderScale) {
  354. bounds.size.height = bounds.size.height;
  355. bounds.size.width = bounds.size.height*borderScale;
  356. } else {
  357. bounds.size.width = bounds.size.width;
  358. bounds.size.height = bounds.size.width/borderScale;
  359. }
  360. CPDFListStampAnnotation *newAnnotation = [[CPDFListStampAnnotation alloc] initWithDocument:self.document image:image];
  361. newAnnotation.bounds = bounds;
  362. [newAnnotation setBorderBoundsWithPImage:image];
  363. [self addAnnotation:newAnnotation toPage:page];
  364. [[self undoManager] setActionName:NSLocalizedString(@"Add Note", @"Undo action name")];
  365. [self addActiveAnnotations:@[newAnnotation]];
  366. [self setNeedsDisplayAnnotation:newAnnotation];
  367. }
  368. else NSBeep();
  369. }
  370. - (void)updateActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations {
  371. if(activeAnnotations) {
  372. NSArray *selectTure = [activeAnnotations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF in %@", self.activeAnnotations]];
  373. BOOL isSame = NO;
  374. if(self.activeAnnotations.count == activeAnnotations.count && self.activeAnnotations.count == selectTure.count) isSame = YES;
  375. if(!isSame) {
  376. self.activeAnnotations = [NSMutableArray arrayWithArray:activeAnnotations];
  377. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:)])
  378. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations];
  379. dispatch_async(dispatch_get_main_queue(), ^{
  380. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  381. });
  382. }
  383. } else if (!activeAnnotations && self.activeAnnotations.count > 0) {
  384. [self.activeAnnotations removeAllObjects];
  385. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:)])
  386. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations];
  387. dispatch_async(dispatch_get_main_queue(), ^{
  388. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  389. });
  390. }
  391. }
  392. - (void)addActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations {
  393. BOOL isContains = YES;
  394. for (CPDFAnnotation *annotation in activeAnnotations) {
  395. if(![self.activeAnnotations containsObject:annotation]) {
  396. [self.activeAnnotations addObject:annotation];
  397. isContains = NO;
  398. }
  399. }
  400. if(!isContains) {
  401. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:)])
  402. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations];
  403. dispatch_async(dispatch_get_main_queue(), ^{
  404. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  405. });
  406. }
  407. }
  408. - (void)removeActiveAnnotations:(NSArray<CPDFAnnotation *> *)removeAnnotations {
  409. BOOL isContains = NO;
  410. for (CPDFAnnotation *annotation in removeAnnotations) {
  411. if([self.activeAnnotations containsObject:annotation]) {
  412. [self.activeAnnotations removeObject:annotation];
  413. isContains = YES;
  414. }
  415. }
  416. if(isContains) {
  417. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:)])
  418. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations];
  419. dispatch_async(dispatch_get_main_queue(), ^{
  420. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  421. });
  422. }
  423. }
  424. - (void)deleteAnnotations:(NSArray<CPDFAnnotation *> *)annotations {
  425. }
  426. - (void)editAnnotation:(CPDFAnnotation *)annotation {
  427. if ([annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  428. [self editAnnotationFreeText:(CPDFFreeTextAnnotation *)annotation];
  429. [[self window] makeFirstResponder:self];
  430. [self setNeedsDisplayAnnotation:annotation];
  431. } else {
  432. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewEditAnnotation:forAnnotation:)]) {
  433. [self.pdfListViewDelegate PDFListViewEditAnnotation:self forAnnotation:annotation];
  434. } else {
  435. [CPDFListView cancelPreviousPerformRequestsWithTarget:self selector:@selector(showHUDHint:) object:self.hoverAnnotation];
  436. if (!self.popOver) {
  437. _popOver = [[NSPopover alloc] init];
  438. }
  439. __block typeof(self) blockSelf = self;
  440. _popOver.delegate = self;
  441. CPDFListEditAnnotationViewController *vc = [[CPDFListEditAnnotationViewController alloc] initWithNibName:@"CPDFListEditAnnotationViewController" bundle:[NSBundle mainBundle] annotation:annotation];
  442. _popOver.contentViewController = vc;
  443. vc.closeCallBack = ^{
  444. [blockSelf.popOver close];
  445. };
  446. vc.changeColorCallBack = ^{
  447. blockSelf.popOver.backgroundColor = annotation.color?:[NSColor whiteColor];
  448. };
  449. if([annotation isKindOfClass:[CPDFMarkupAnnotation class]] ||
  450. [annotation isKindOfClass:[CPDFLineAnnotation class]] ||
  451. [annotation isKindOfClass:[CPDFSquareAnnotation class]] ||
  452. [annotation isKindOfClass:[CPDFCircleAnnotation class]] ||
  453. [annotation isKindOfClass:[CPDFTextAnnotation class]]) {
  454. _popOver.backgroundColor = annotation.color?:[NSColor whiteColor];
  455. } else {
  456. _popOver.backgroundColor = [NSColor whiteColor];
  457. }
  458. _popOver.animates = YES;
  459. _popOver.behavior = NSPopoverBehaviorTransient;
  460. [_popOver showRelativeToRect:[self convertRect:annotation.bounds fromPage:annotation.page] ofView:self preferredEdge:NSMaxXEdge];
  461. }
  462. }
  463. }
  464. - (void)rotateStampAnnotation:(CPDFListStampAnnotation *)annotation rotateAngle:(NSInteger)angle {
  465. if([annotation isKindOfClass:[CPDFListStampAnnotation class]]) {
  466. CPDFListStampAnnotation *stampAnnotation = (CPDFListStampAnnotation *)annotation;
  467. NSInteger rotation = stampAnnotation.rotation;
  468. rotation = rotation - angle;
  469. if (rotation < 0) {
  470. rotation += 360;
  471. } else if (rotation >= 360) {
  472. rotation-=360;
  473. }
  474. [annotation setRotation:rotation];
  475. rotation = [annotation appearanceStreamRotation];
  476. [self setNeedsDisplayAnnotationViewForPage:annotation.page];
  477. [[[self undoManager] prepareWithInvocationTarget:self] rotateStampAnnotation:annotation rotateAngle:-angle];
  478. }
  479. }
  480. - (void)rotateSignatureAnnotation:(CPDFListSignatureAnnotation *)annotation rotateAngle:(NSInteger)angle {
  481. if([annotation isKindOfClass:[CPDFListSignatureAnnotation class]]) {
  482. CPDFListSignatureAnnotation *signatureAnnotation = (CPDFListSignatureAnnotation *)annotation;
  483. NSInteger rotation = signatureAnnotation.rotation;
  484. rotation = rotation - angle;
  485. if (rotation < 0) {
  486. rotation += 360;
  487. } else if (rotation >= 360) {
  488. rotation-=360;
  489. }
  490. [signatureAnnotation setRotation:rotation];
  491. rotation = [signatureAnnotation appearanceStreamRotation];
  492. [self setNeedsDisplayAnnotationViewForPage:annotation.page];
  493. [[[self undoManager] prepareWithInvocationTarget:self] rotateSignatureAnnotation:annotation rotateAngle:-angle];
  494. }
  495. }
  496. - (void)addAnnotation:(CPDFAnnotation *)annotation toPage:(CPDFPage *)page {
  497. [[[self undoManager] prepareWithInvocationTarget:self] removeAnnotation:annotation];
  498. [annotation setModificationDate:[NSDate date]];
  499. //widget设置作者与fieldName冲突
  500. if (![annotation isKindOfClass:[CPDFWidgetAnnotation class]]) {
  501. [annotation setUserName:CPDFKitShareConfig.annotationAuthor?:NSFullUserName()];
  502. }
  503. [page addAnnotation:annotation];
  504. [[self undoManager] setActionName:NSLocalizedString(@"Add Note", @"Undo action name")];
  505. dispatch_async(dispatch_get_main_queue(), ^{
  506. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewDidAddAnnotationNotification object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:page, CPDFListViewPageKey, annotation, CPDFListViewAnnotationKey, nil]];
  507. });
  508. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewAddAnnotations:forAddAnnotations:inPage:)])
  509. [self.pdfListViewDelegate PDFListViewAddAnnotations:self forAddAnnotations:@[annotation] inPage:page];
  510. }
  511. - (void)removeAnnotation:(CPDFAnnotation *)annotation {
  512. CPDFAnnotation *wasAnnotation = annotation;
  513. CPDFPage *page = wasAnnotation.page;
  514. [[[self undoManager] prepareWithInvocationTarget:self] addAnnotation:wasAnnotation toPage:page];
  515. if([self.activeAnnotations containsObject:wasAnnotation]) {
  516. [self.activeAnnotations removeObject:wasAnnotation];
  517. }
  518. [self setNeedsDisplayAnnotation:wasAnnotation];
  519. [page removeAnnotation:wasAnnotation];
  520. [self annotationsChangedOnPage:page];
  521. dispatch_async(dispatch_get_main_queue(), ^{
  522. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewDidRemoveAnnotationNotification object:self
  523. userInfo:[NSDictionary dictionaryWithObjectsAndKeys:wasAnnotation, CPDFListViewAnnotationKey, page, CPDFListViewPageKey, nil]];
  524. });
  525. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewRemoveAnnotations:forRemoveAnnotations:inPage:)])
  526. [self.pdfListViewDelegate PDFListViewRemoveAnnotations:self forRemoveAnnotations:@[annotation] inPage:page];
  527. }
  528. - (void)drawPage:(CPDFPage *)pdfPage toContext:(CGContextRef)context {
  529. [super drawPage:pdfPage toContext:context];
  530. [self drawSelectionForPage:pdfPage inContext:context];
  531. if(self.activeAnnotations.count > 1) {
  532. CGRect selectAnnotationBounds = [self selectionMultipleAnnotationBoundsWithPage:pdfPage];
  533. NSRect rect = selectAnnotationBounds;
  534. CGFloat lineWidth = [self unitWidthOnPage:pdfPage];
  535. CGContextSaveGState(context);
  536. CGColorRef color = [CPDFListViewConfig defaultManager].annotationBorderColor.CGColor;
  537. CGContextSetStrokeColorWithColor(context, color);
  538. CGContextStrokeRectWithWidth(context, CGRectInset(NSRectToCGRect(rect), 0,0), lineWidth);
  539. CGContextRestoreGState(context);
  540. CGContextSaveGState(context);
  541. CPDFListViewDrawResizeHandles(context, rect, 4.0 * lineWidth, YES);
  542. CGContextRestoreGState(context);
  543. }
  544. [self.activeAnnotations enumerateObjectsUsingBlock:^(CPDFAnnotation *annotation, NSUInteger idx, BOOL * _Nonnull stop) {
  545. if (annotation.page && [annotation.page isEqual:pdfPage]) {
  546. [annotation drawSelectionHighlightForView:self inContext:context isHover:NO];
  547. }
  548. }];
  549. [self.selectAnnotations enumerateObjectsUsingBlock:^(CPDFAnnotation *annotation, NSUInteger idx, BOOL * _Nonnull stop) {
  550. if (annotation.page && [annotation.page isEqual:pdfPage]) {
  551. [annotation drawSelectionHighlightForView:self inContext:context isHover:NO];
  552. }
  553. }];
  554. if (self.hoverAnnotation.page && [self.hoverAnnotation.page isEqual:pdfPage] && ![self.activeAnnotations containsObject:self.hoverAnnotation]) {
  555. [self.hoverAnnotation drawSelectionHighlightForView:self inContext:context isHover:YES];
  556. }
  557. if(!CGRectEqualToRect(self.multiplSelectBounds, CGRectZero)) {
  558. @synchronized (self) {
  559. CGContextSetLineCap(context, kCGLineCapRound);
  560. CGContextSetLineWidth(context, 1.0);
  561. CGContextSetStrokeColorWithColor(context, [NSColor redColor].CGColor);
  562. CGContextBeginPath(context);
  563. CGContextMoveToPoint(context, self.multiplSelectBounds.origin.x, self.multiplSelectBounds.origin.y);
  564. CGContextAddLineToPoint(context, self.multiplSelectBounds.origin.x+ self.multiplSelectBounds.size.width,self.multiplSelectBounds.origin.y);
  565. CGContextAddLineToPoint(context, self.multiplSelectBounds.origin.x + self.multiplSelectBounds.size.width,self.multiplSelectBounds.origin.y + + self.multiplSelectBounds.size.height);
  566. CGContextAddLineToPoint(context, self.multiplSelectBounds.origin.x,self.multiplSelectBounds.origin.y + + self.multiplSelectBounds.size.height);
  567. CGContextClosePath(context);
  568. CGFloat lengths[] = {5,5};
  569. CGContextSetLineDash(context, 0, lengths,2);
  570. CGContextStrokePath(context);
  571. }
  572. }
  573. if (self.dragHoverPoints.count > 0) {
  574. NSInteger count = 2;
  575. CGContextSaveGState(context);
  576. for (NSUInteger i=0; i+count <= self.dragHoverPoints.count;) {
  577. NSPoint ptlt = [self.dragHoverPoints[i++] pointValue];
  578. NSPoint ptrt = [self.dragHoverPoints[i++] pointValue];
  579. CGContextMoveToPoint(context, ptlt.x, ptlt.y);
  580. CGContextAddLineToPoint(context, ptrt.x,ptrt.y);
  581. }
  582. CGContextSetStrokeColorWithColor(context, [CPDFListViewConfig defaultManager].dragHoverColor.CGColor);
  583. CGContextDrawPath(context, kCGPathStroke);
  584. CGContextRestoreGState(context);
  585. }
  586. }
  587. - (void)drawSelectionForPage:(CPDFPage *)pdfPage inContext:(CGContextRef)context {
  588. NSRect rect;
  589. NSUInteger pageIndex;
  590. @synchronized (self) {
  591. pageIndex = self.selectionPageIndex;
  592. rect = self.selectionRect;
  593. }
  594. if (pageIndex != NSNotFound) {
  595. BOOL isWidget = NO;
  596. for (CPDFAnnotation * annotation in self.activeAnnotations) {
  597. if ([annotation isForm]) {
  598. isWidget = YES;
  599. break;
  600. }
  601. }
  602. if (isWidget &&
  603. _toolMode != CFormToolMode) {
  604. return;
  605. }
  606. NSRect bounds = [pdfPage boundsForBox:[self displayBox]];
  607. CGFloat radius = HANDLE_SIZE * [self unitWidthOnPage:pdfPage];
  608. CGColorRef color = CGColorCreateGenericGray(0.0, 0.6);
  609. CGContextSetFillColorWithColor(context, color);
  610. CGColorRelease(color);
  611. CGContextBeginPath(context);
  612. CGContextAddRect(context, NSRectToCGRect(bounds));
  613. CGContextAddRect(context, NSRectToCGRect(rect));
  614. CGContextEOFillPath(context);
  615. if ([pdfPage pageIndex] != pageIndex) {
  616. color = CGColorCreateGenericGray(0.0, 0.3);
  617. CGContextSetFillColorWithColor(context, color);
  618. CGColorRelease(color);
  619. CGContextFillRect(context, NSRectToCGRect(rect));
  620. }
  621. CPDFListViewDrawResizeHandles(context, rect, radius, YES);
  622. }
  623. }
  624. #pragma mark - NSPopoverDelegate
  625. - (void)popoverWillClose:(NSNotification *)notification {
  626. NSPopover *popover = notification.object;
  627. if([popover.contentViewController isKindOfClass:[CPDFListEditAnnotationViewController class]]) {
  628. CPDFListEditAnnotationViewController *listEditAnnotationViewController = (CPDFListEditAnnotationViewController *)popover.contentViewController;
  629. CPDFAnnotation *annotation = listEditAnnotationViewController.annotation;
  630. annotation.contents = listEditAnnotationViewController.contentString;
  631. [self setNeedsDisplayAnnotation:annotation];
  632. }
  633. self.popOver = nil;
  634. }
  635. @end