CPDFListView.m 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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 "CSelfSignAnnotation.h"
  15. #import "CSelfSignAnnotationFreeText.h"
  16. #import "CPDFListViewConfig.h"
  17. #import "CPDFListEditAnnotationViewController.h"
  18. #import "CPDFListAnnotationNoteWindowController.h"
  19. #import "CPDFListView+Private.h"
  20. #import "CPDFListView+Event.h"
  21. #import "CPDFListView+Tool.h"
  22. #import "CPDFListView+Extension.h"
  23. #import "CPDFListView+UndoManager.h"
  24. #import "NSString+PDFListView.h"
  25. #import "NSImage+PDFListView.h"
  26. #import "NSCursor+PDFListView.h"
  27. #import "CPDFMarkupAnnotation+PDFListView.h"
  28. #import "CPDFTextWidgetAnnotation+PDFListView.h"
  29. #import "CPDFLineAnnotation+PDFListView.h"
  30. #import "NSPopover+PDFListView.h"
  31. #import "CPDFListView+Extension.h"
  32. #import <PDF_Master-Swift.h>
  33. NSNotificationName const CPDFListViewSelectionChangedNotification = @"CPDFListViewSelectionChangedNotification";
  34. NSNotificationName const CPDFListViewToolModeChangeNotification = @"CPDFListViewToolModeChangeNotification";
  35. NSNotificationName const CPDFListViewAnnotationTypeChangeNotification = @"CPDFListViewAnnotationTypeChangeNotification";
  36. NSNotificationName const CPDFListViewMagnificationChangedNotification = @"CPDFListViewMagnificationChangedNotification";
  37. NSNotificationName const CPDFListViewDidAddAnnotationNotification = @"CPDFListViewDidAddAnnotationNotification";
  38. NSNotificationName const CPDFListViewDidRemoveAnnotationNotification = @"CPDFListViewDidRemoveAnnotationNotification";
  39. NSNotificationName const CPDFListViewActiveAnnotationsChangeNotification = @"CPDFListViewActiveAnnotationsChangeNotification";
  40. NSNotificationName const CPDFListViewAnnotationsAttributeHasChangeNotification = @"CPDFListViewAnnotationsAttributeHasChangeNotification";
  41. @implementation CPDFListView
  42. #pragma mark - Init
  43. - (id)init {
  44. self = [super init];
  45. if (self) {
  46. [self commonInitialization];
  47. }
  48. return self;
  49. }
  50. - (id)initWithFrame:(NSRect)frameRect {
  51. self = [super initWithFrame:frameRect];
  52. if (self) {
  53. [self commonInitialization];
  54. }
  55. return self;
  56. }
  57. - (id)initWithCoder:(NSCoder *)decoder {
  58. self = [super initWithCoder:decoder];
  59. if (self) {
  60. [self commonInitialization];
  61. }
  62. return self;
  63. }
  64. - (void)updateLayer {
  65. [super updateLayer];
  66. if (@available(macOS 10.14, *)) {
  67. if ([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewBackgroundColor)]) {
  68. NSColor *color = [self.pdfListViewDelegate PDFListViewBackgroundColor];
  69. self.backgroundColor = color;
  70. }
  71. }
  72. }
  73. - (void)commonInitialization {
  74. self.selectionPageIndex = NSNotFound;
  75. self.clickLineAnnotation = nil;
  76. self.isClickDoubleCreatLine = NO;
  77. self.activeAnnotations = [NSMutableArray array];
  78. self.selectAnnotations = [NSMutableArray array];
  79. self.dragHoverPoints = [NSMutableArray array];
  80. self.aCopyAnnotations = [NSMutableArray array];
  81. [self registerAsObserver];
  82. [self addTrackingArea];
  83. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlePageChangedNotification:)
  84. name:CPDFViewPageChangedNotification object:self];
  85. }
  86. - (void)addTrackingArea {
  87. NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect | NSTrackingActiveInKeyWindow | NSTrackingMouseMoved owner:self userInfo:nil];
  88. [self addTrackingArea:trackingArea];
  89. }
  90. - (void)goToPageIndex:(NSInteger)pageIndex animated:(BOOL)animated {
  91. if (self.currentPageIndex != pageIndex) {
  92. [self recordForwardBackWithPageIndex:self.currentPageIndex];
  93. }
  94. [super goToPageIndex:pageIndex animated:animated];
  95. }
  96. - (void)goToSelection:(CPDFSelection *)selection animated:(BOOL)animated {
  97. if (self.currentPageIndex != selection.page.pageIndex) {
  98. [self recordForwardBackWithPageIndex:self.currentPageIndex];
  99. }
  100. [super goToSelection:selection animated:animated];
  101. }
  102. - (void)goToDestination:(CPDFDestination *)destination {
  103. [super goToDestination:destination];
  104. }
  105. - (void)goToRect:(CGRect)rect onPage:(CPDFPage *)page animated:(BOOL)animated {
  106. if (self.currentPageIndex != page.pageIndex) {
  107. [self recordForwardBackWithPageIndex:self.currentPageIndex];
  108. }
  109. [super goToRect:rect onPage:page animated:animated];
  110. }
  111. - (void)goToTargetPoint:(NSPoint)point onPage:(CPDFPage *)page atScrollPosition:(CScrollPosition)scrollPosition {
  112. if (self.currentPageIndex != page.pageIndex) {
  113. [self recordForwardBackWithPageIndex:self.currentPageIndex];
  114. }
  115. [super goToTargetPoint:point onPage:page atScrollPosition:scrollPosition];
  116. }
  117. #pragma mark - Setter & Get
  118. - (void)setDocument:(CPDFDocument *)document {
  119. @synchronized (self) {
  120. self.selectionRect = NSZeroRect;
  121. self.selectionPageIndex = NSNotFound;
  122. }
  123. // self.toolMode = CFormToolMode;
  124. // self.annotationType = CAnnotationTypeRadioButton;
  125. // CStampSignatureObject *objc = [[CStampSignatureObject alloc] initImageStampWithFilePath:[[NSBundle mainBundle] pathForResource:@"Quick Start Guide" ofType:@"pdf"]];
  126. // [self setAddStampObject:objc keepToolModel:YES];
  127. //
  128. [super setDocument:document];
  129. [self.window makeFirstResponder:self];
  130. }
  131. -(void)setToolMode:(CToolMode)newToolMode {
  132. if(newToolMode != _toolMode) {
  133. BOOL isDisplayAnnotationView = NO;
  134. if(self.isSetLinkDestinationArea)
  135. self.isSetLinkDestinationArea = NO;
  136. if ((CTextToolMode == _toolMode || CNoteToolMode == _toolMode) && CTextToolMode != newToolMode) {
  137. if (newToolMode != CNoteToolMode && self.activeAnnotation) {
  138. if([self.activeAnnotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  139. CPDFFreeTextAnnotation *freeTextAn = (CPDFFreeTextAnnotation *)self.activeAnnotation;
  140. if ([self isEditWithCurrentFreeText:freeTextAn]) {
  141. [self commitEditAnnotationFreeText:freeTextAn];
  142. }
  143. }
  144. [self updateActiveAnnotations:@[]];
  145. }
  146. if ([self currentSelection])
  147. [self setCurrentSelection:nil];
  148. } else if (_toolMode == CSelectToolMode && NSEqualRects(self.selectionRect, NSZeroRect) == NO) {
  149. self.selectionRect = NSZeroRect;
  150. self.selectionPageIndex = NSNotFound;
  151. dispatch_async(dispatch_get_main_queue(), ^{
  152. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFViewSelectionChangedNotification object:self];
  153. });
  154. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangedSelectionOrMagnification:)])
  155. [self.pdfListViewDelegate PDFListViewChangedSelectionOrMagnification:self];
  156. isDisplayAnnotationView = YES;
  157. }
  158. BOOL highlightFormFiled = [[NSUserDefaults standardUserDefaults] boolForKey:@"kPDFViewHighlightFormFiledKey"];
  159. if (CFormToolMode == _toolMode && !highlightFormFiled) {
  160. [[CPDFKitConfig sharedInstance] setEnableFormFieldHighlight:highlightFormFiled];
  161. isDisplayAnnotationView = YES;
  162. } else if (CFormToolMode == newToolMode && !highlightFormFiled) {
  163. [[CPDFKitConfig sharedInstance] setEnableFormFieldHighlight:YES];
  164. isDisplayAnnotationView = YES;
  165. }
  166. if(CEditPDFToolMode == newToolMode)
  167. [self beginEditingLoadType:CEditingLoadTypeText|CEditingLoadTypeImage];
  168. if(_toolMode == CEditPDFToolMode) {
  169. if(self.isEdited) {
  170. [self commitEditing];
  171. }
  172. [self endOfEditing];
  173. }
  174. _toolMode = newToolMode;
  175. self.selectZoomToolModeZoomOut = NO;
  176. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangedToolMode:forToolMode:)])
  177. [self.pdfListViewDelegate PDFListViewChangedToolMode:self forToolMode:newToolMode];
  178. dispatch_async(dispatch_get_main_queue(), ^{
  179. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewToolModeChangeNotification object:self];
  180. });
  181. [self resetPDFViewAnnotation];
  182. if(isDisplayAnnotationView)
  183. [self setNeedsDisplayAnnotationViewForVisiblePages];
  184. }
  185. }
  186. -(void)setShowHopnumber:(BOOL)showHopnumber {
  187. _showHopnumber = showHopnumber;
  188. [self setNeedsDisplayAnnotationViewForVisiblePages];
  189. }
  190. - (void)setAnnotationType:(CAnnotationType)annotationType {
  191. if (_annotationType != annotationType) {
  192. if(CAnnotationTypeLink == annotationType || CAnnotationTypeLink == _annotationType)
  193. [self setNeedsDisplayAnnotationViewForVisiblePages];
  194. _annotationType = annotationType;
  195. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangedAnnotationType:forAnnotationType:)])
  196. [self.pdfListViewDelegate PDFListViewChangedAnnotationType:self forAnnotationType:annotationType];
  197. dispatch_async(dispatch_get_main_queue(), ^{
  198. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewAnnotationTypeChangeNotification object:self];
  199. });
  200. }
  201. if(self.activeAnnotations.count > 0) {
  202. if([self.activeAnnotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  203. CPDFFreeTextAnnotation *freeTextAn = (CPDFFreeTextAnnotation *)self.activeAnnotation;
  204. if ([self isEditWithCurrentFreeText:freeTextAn]) {
  205. [self commitEditAnnotationFreeText:freeTextAn];
  206. }
  207. }
  208. [self updateActiveAnnotations:@[]];
  209. [self setNeedsDisplayAnnotationViewForVisiblePages];
  210. }
  211. if(self.isSetLinkDestinationArea)
  212. self.isSetLinkDestinationArea = NO;
  213. [self resetPDFViewAnnotation];
  214. }
  215. - (NSRect)currentSelectionRect {
  216. if (CSelectToolMode == self.toolMode)
  217. return self.selectionRect;
  218. return NSZeroRect;
  219. }
  220. - (CPDFAnnotation *)activeAnnotation {
  221. return self.activeAnnotations.lastObject;
  222. }
  223. - (void)setHideNotes:(BOOL)hideNotes {
  224. _hideNotes = hideNotes;
  225. for(NSInteger i = 0;i<self.document.pageCount;i++) {
  226. CPDFPage *page = [self.document pageAtIndex:i];
  227. for (CPDFAnnotation *annotation in page.annotations) {
  228. [annotation setAnnotationShouldDisplay:!self.hideNotes];
  229. [annotation setHidden: self.hideNotes];
  230. }
  231. }
  232. [self setNeedsDisplayAnnotationViewForVisiblePages];
  233. }
  234. -(void)setIsSetLinkDestinationArea:(BOOL)isSetLinkDestinationArea {
  235. _isSetLinkDestinationArea = isSetLinkDestinationArea;
  236. if (_isSetLinkDestinationArea) {
  237. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewLinkDestinationStart:withActiveAnnotation:)]) {
  238. [self.pdfListViewDelegate PDFListViewLinkDestinationStart:self withActiveAnnotation:self.activeAnnotation];
  239. }
  240. } else {
  241. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewLinkDestinationEnd:withActiveAnnotation:)]) {
  242. [self.pdfListViewDelegate PDFListViewLinkDestinationEnd:self withActiveAnnotation:self.activeAnnotation];
  243. }
  244. }
  245. }
  246. #pragma mark - Public
  247. - (CPDFAnnotation *)addAnnotationWithType:(CAnnotationType)annotationType selection:(CPDFSelection *)selection page:(CPDFPage *)page bounds:(NSRect)bounds {
  248. CPDFAnnotation *annotation = nil;
  249. NSString *text = [selection PDFListViewCleanedString];
  250. if([CPDFListView isMarkupAnnotationType:annotationType]) {
  251. if(selection){
  252. annotation = [self addPDFSelection:selection annotationTyoe:annotationType];
  253. [annotation setContents:text?:@""];
  254. } else NSBeep();
  255. } else {
  256. BOOL isInitial = NO;
  257. if(selection) {
  258. CGRect noteRect = selection.bounds;
  259. isInitial = NSEqualSizes(noteRect.size, NSZeroSize);
  260. } else {
  261. isInitial = NSEqualSizes(bounds.size, NSZeroSize);
  262. }
  263. CPDFAnnotationModel *annotationModel = [[CPDFAnnotationModel alloc]initWithAnnotationType:annotationType];
  264. CGSize size = CGSizeMake(annotationModel.noteWidth, annotationModel.noteHeight);
  265. if(annotationType == CAnnotationTypeAnchored)
  266. bounds = CGRectMake(bounds.origin.x, bounds.origin.y, size.width, size.height);
  267. if (annotationType == CAnnotationTypeCircle || annotationType == CAnnotationTypeSquare || annotationType == CAnnotationTypeLine || annotationType == CAnnotationTypeArrow || annotationType == CAnnotationTypeAnchored) {
  268. bounds = KMConstrainRect(bounds, [page boundsForBox:[self displayBox]]);
  269. }
  270. if (isInitial)
  271. bounds = annotationType == CAnnotationTypeAnchored ? CPDFListViewRectFromCenterAndSize(bounds.origin, size) : CPDFListViewRectFromCenterAndSquareSize(bounds.origin, MIN_NOTE_SIZE);
  272. switch (annotationType) {
  273. case CAnnotationTypeFreeText:
  274. annotation = [[CPDFFreeTextAnnotation alloc]initWithPDFListViewNoteWith:self.document];
  275. annotation.bounds = bounds;
  276. break;
  277. case CAnnotationTypeAnchored:
  278. annotation = [[CPDFTextAnnotation alloc]initWithPDFListViewNoteWith:self.document];
  279. annotation.bounds = bounds;
  280. break;
  281. case CAnnotationTypeCircle:
  282. annotation = [[CPDFCircleAnnotation alloc]initWithPDFListViewNoteWith:self.document];
  283. annotation.bounds = bounds;
  284. break;
  285. case CAnnotationTypeSquare:
  286. annotation = [[CPDFSquareAnnotation alloc]initWithPDFListViewNoteWith:self.document];
  287. annotation.bounds = bounds;
  288. break;
  289. case CAnnotationTypeLine:
  290. annotation = [[CPDFLineAnnotation alloc]initPDFListViewNoteWithDocument:self.document annotationType:CAnnotationTypeLine];
  291. [(CPDFLineAnnotation *)annotation setObservedStartPoint:CGPointMake(bounds.origin.x, bounds.origin.y)];
  292. [(CPDFLineAnnotation *)annotation setObservedEndPoint:CGPointMake(bounds.origin.x + bounds.size.width,bounds.origin.y+ bounds.size.height)];
  293. [(CPDFLineAnnotation *)annotation setStartLineStyle:CPDFLineStyleNone];
  294. [(CPDFLineAnnotation *)annotation setEndLineStyle:CPDFLineStyleNone];
  295. break;
  296. case CAnnotationTypeArrow: {
  297. annotation = [[CPDFLineAnnotation alloc]initPDFListViewNoteWithDocument:self.document annotationType:CAnnotationTypeArrow];
  298. [(CPDFLineAnnotation *)annotation setObservedStartPoint:CGPointMake(bounds.origin.x, bounds.origin.y)];
  299. [(CPDFLineAnnotation *)annotation setObservedEndPoint:CGPointMake(bounds.origin.x + bounds.size.width,bounds.origin.y+ bounds.size.height)];
  300. }
  301. break;
  302. case CAnnotationTypeLink:
  303. annotation = [[CPDFLinkAnnotation alloc] initWithPDFListViewNoteWith:self.document];
  304. annotation.bounds = bounds;
  305. break;
  306. case CAnnotationTypeTextField:
  307. annotation = [[CPDFTextWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document];
  308. annotation.bounds = bounds;
  309. break;
  310. case CAnnotationTypeCheckBox:
  311. annotation = [[CPDFButtonWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document controlType:CPDFWidgetCheckBoxControl];
  312. annotation.bounds = bounds;
  313. break;
  314. case CAnnotationTypeRadioButton:
  315. annotation = [[CPDFButtonWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document controlType:CPDFWidgetRadioButtonControl];
  316. annotation.bounds = bounds;
  317. break;
  318. case CAnnotationTypeListMenu:
  319. annotation = [[CPDFChoiceWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document listChoice:YES];
  320. annotation.bounds = bounds;
  321. break;
  322. case CAnnotationTypeComboBox:
  323. annotation = [[CPDFChoiceWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document listChoice:NO];
  324. annotation.bounds = bounds;
  325. break;
  326. case CAnnotationTypeSignature:
  327. annotation = [[CPDFSignatureWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document];
  328. annotation.bounds = bounds;
  329. break;
  330. case CAnnotationTypeActionButton:
  331. annotation = [[CPDFButtonWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document controlType:CPDFWidgetPushButtonControl];
  332. annotation.bounds = bounds;
  333. break;
  334. case CAnnotationTypeSignFalse:
  335. case CAnnotationTypeSignTure:
  336. case CAnnotationTypeSignCircle:
  337. case CAnnotationTypeSignLine:
  338. case CAnnotationTypeSignDot:
  339. annotation = [[CSelfSignAnnotation alloc]initPDFListViewNoteWithDocument:self.document type:self.annotationType];
  340. annotation.bounds = bounds;
  341. [(CSelfSignAnnotation *)annotation updateAppearanceStream];
  342. break;
  343. case CAnnotationTypeSignText:
  344. case CAnnotationTypeSignConfig:
  345. case CAnnotationTypeSignDate:
  346. annotation = [[CSelfSignAnnotationFreeText alloc]initPDFListViewNoteWithDocument:self.document subType:self.annotationType string:@"" bounds:bounds];
  347. break;
  348. case CAnnotationTypeRedact:
  349. annotation = [self addRedactPDFSelection:selection];
  350. break;
  351. default:
  352. break;
  353. }
  354. }
  355. if (annotation) {
  356. if (annotationType != CAnnotationTypeLine && annotationType != CAnnotationTypeArrow && annotationType != CAnnotationTypeInk && [text length] > 0)
  357. [annotation setString:text];
  358. [self addAnnotation:annotation toPage:page];
  359. }
  360. return annotation;
  361. }
  362. - (void)drawAnnotation:(CPDFAnnotation *)annotation toContext:(CGContextRef)context {
  363. [super drawAnnotation:annotation toContext:context];
  364. if ([self.pdfListViewDelegate respondsToSelector:@selector(PDFListView:needDrawAnnotation:)]) {
  365. if ([self.pdfListViewDelegate PDFListView:self needDrawAnnotation:annotation] == false) {
  366. return;
  367. }
  368. }
  369. if (CFormToolMode == _toolMode &&
  370. [annotation isKindOfClass:[CPDFWidgetAnnotation class]] && self.showFormFieldName && !annotation.isHidden) {
  371. CPDFWidgetAnnotation *annotationWidget = (CPDFWidgetAnnotation*)annotation;
  372. NSString *fieldName = annotationWidget.fieldName;
  373. if(self.showHopnumber) {
  374. NSInteger hopNumen = annotationWidget.widgetHopNumber;
  375. if(fieldName) {
  376. fieldName = [NSString stringWithFormat:@"%ld %@",hopNumen,fieldName];
  377. } else {
  378. fieldName = [NSString stringWithFormat:@"%ld",hopNumen];
  379. }
  380. }
  381. if (fieldName.length > 0) {
  382. CGContextSaveGState(context);
  383. NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
  384. paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
  385. paragraphStyle.alignment = NSTextAlignmentLeft;
  386. NSColor *backgroundColor = [NSColor blackColor];
  387. if ([self.activeAnnotations containsObject:annotation]) {
  388. backgroundColor = [NSColor colorWithRed:82.0/255.0 green:102.0/255.0 blue:204.0/255.0 alpha:1.0];
  389. }
  390. if([CPDFKitConfig sharedInstance].isShowFormRequiredFlagColor)
  391. backgroundColor = [CPDFKitConfig sharedInstance].formRequiredFlagColor?:[NSColor redColor];
  392. NSDictionary *attributes = @{NSFontAttributeName : [NSFont systemFontOfSize:12],
  393. NSForegroundColorAttributeName : [NSColor whiteColor],
  394. NSBackgroundColorAttributeName : backgroundColor,
  395. NSParagraphStyleAttributeName : paragraphStyle};
  396. NSString* drawString = fieldName;
  397. drawString = [drawString getMaxStringWithBounds:annotationWidget.bounds attributes:attributes];
  398. CGSize size = [drawString sizeWithAttributes:attributes];
  399. CGRect drawRect = annotationWidget.bounds;
  400. drawRect.origin.y += (drawRect.size.height - size.height)/2.0;
  401. drawRect.origin.x += (drawRect.size.width - size.width)/2.0;
  402. drawRect.size.height = size.height;
  403. drawRect.size.width = size.width;
  404. [NSGraphicsContext saveGraphicsState];
  405. [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]];
  406. [drawString drawInRect:drawRect withAttributes:attributes];
  407. [NSGraphicsContext restoreGraphicsState];
  408. CGContextRestoreGState(context);
  409. NSRect rect = [self integralRect:[annotationWidget bounds] onPage:[annotationWidget page]];
  410. CGFloat lineWidth = [self unitWidthOnPage:[annotationWidget page]];
  411. CGContextSaveGState(context);
  412. CGColorRef color = [NSColor blackColor].CGColor;
  413. CGContextSetStrokeColorWithColor(context, color);
  414. CGContextStrokeRectWithWidth(context, CGRectInset(NSRectToCGRect(rect), 0,0), lineWidth);
  415. }
  416. } else if(CNoteToolMode== _toolMode && _annotationType == CAnnotationTypeLink && [annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
  417. CGContextSaveGState(context);
  418. NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
  419. paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
  420. paragraphStyle.alignment = NSTextAlignmentLeft;
  421. NSColor *backgroundColor = [NSColor clearColor];
  422. NSDictionary *attributes = @{NSFontAttributeName : [NSFont systemFontOfSize:12],
  423. NSForegroundColorAttributeName : [NSColor whiteColor],
  424. NSBackgroundColorAttributeName : backgroundColor,
  425. NSParagraphStyleAttributeName : paragraphStyle};
  426. NSString *drawString = NSLocalizedString(@"No Destination", nil);
  427. CPDFDestination *destination = [(CPDFLinkAnnotation *)annotation destination];
  428. if(destination) {
  429. CPDFPage *page = destination.page;
  430. NSString * url = [(CPDFLinkAnnotation *)annotation URL];
  431. if(destination.page) {
  432. NSUInteger index = [self.document indexForPage:page] + 1;
  433. drawString = [NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"to Page",nil),@(index)];
  434. } else if(url.length > 0) {
  435. drawString = url;
  436. }
  437. }
  438. drawString = [drawString getMaxStringWithBounds:annotation.bounds attributes:attributes];
  439. CGSize size = [drawString sizeWithAttributes:attributes];
  440. CGRect drawRect = annotation.bounds;
  441. drawRect.origin.y += (drawRect.size.height - size.height)/2.0;
  442. drawRect.origin.x += (drawRect.size.width - size.width)/2.0;
  443. drawRect.size.height = size.height;
  444. drawRect.size.width = size.width;
  445. CGContextSaveGState(context);
  446. CGContextSetFillColorWithColor(context, [NSColor colorWithRed:23.0/255.0 green:112.0/255.0 blue:244.0/255.0 alpha:0.5].CGColor);
  447. CGContextFillRect(context,annotation.bounds);
  448. CGContextRestoreGState(context);
  449. [NSGraphicsContext saveGraphicsState];
  450. CGRect frameRect=CGRectMake(annotation.bounds.origin.x, annotation.bounds.origin.y + (annotation.bounds.size.height - 20)/2, annotation.bounds.size.width, 20);
  451. float fontSize = 9;
  452. NSColor *textColor = [NSColor whiteColor];
  453. CTTextAlignment alignMent = kCTTextAlignmentCenter;
  454. CFStringRef stringRef = (__bridge CFStringRef)drawString;
  455. NSMutableAttributedString* attString = [[NSMutableAttributedString alloc] initWithString:drawString];
  456. NSInteger _stringLength=[drawString length];
  457. CTTextAlignment theAlignment = alignMent;
  458. CTParagraphStyleSetting theSettings[1] =
  459. {
  460. { kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment),&theAlignment
  461. }
  462. };
  463. CTParagraphStyleRef paragraphStyleRef = CTParagraphStyleCreate(theSettings, 1);
  464. [attString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)paragraphStyleRef range:NSMakeRange(0, _stringLength)];
  465. [attString addAttribute:(id)kCTForegroundColorAttributeName value:(id)textColor.CGColor range:NSMakeRange(0, _stringLength)];
  466. [attString addAttribute:(id)kCTFontNameAttribute value:@"Courier" range:NSMakeRange(0, _stringLength)];
  467. CFAttributedStringRef attrString =(__bridge CFAttributedStringRef) attString;
  468. CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
  469. CGMutablePathRef framePath = CGPathCreateMutable();
  470. CGPathAddRect(framePath, NULL, frameRect);
  471. CFRange currentRange = CFRangeMake(0, 0);
  472. CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
  473. CGPathRelease(framePath);
  474. CTFrameDraw(frameRef, context);
  475. CFRelease(frameRef);
  476. CFRelease(framesetter);
  477. [NSGraphicsContext restoreGraphicsState];
  478. CGContextRestoreGState(context);
  479. }
  480. }
  481. - (void)setAddStampObject:(CStampSignatureObject *)stampObject keepToolModel:(BOOL)isToolModel {
  482. if (CAnnotationTypeStamp == self.annotationType ||
  483. CAnnotationTypeSignSignature == self.annotationType) {
  484. self.stampObject = stampObject;
  485. self.isStampModel = isToolModel;
  486. [NSCursor clearStampCursor];
  487. }
  488. }
  489. - (void)addAnnotationWithImage:(NSImage *)image page:(CPDFPage *)page point:(NSPoint)point {
  490. NSRect bounds = NSZeroRect;
  491. CPDFSelection *selection = [self currentSelection];
  492. CGFloat defaultWidth = 360;
  493. CGFloat defaultHeight = 90;
  494. NSSize defaultSize = ([page rotation] % 180 == 0) ? NSMakeSize(defaultWidth, defaultHeight) : NSMakeSize(defaultHeight, defaultWidth);
  495. bounds = CPDFListViewRectFromCenterAndSize(point, defaultSize);
  496. // Make sure it fits in the page
  497. bounds = CPDFListViewConstrainRect(bounds, [page boundsForBox:[self displayBox]],[CPDFListViewConfig defaultManager].annotationBorderOffset.floatValue);
  498. // bounds = CPDFListViewConstrainRect(bounds, [page boundsForBox:[self displayBox]]);
  499. if (page != nil) {
  500. BOOL isInitial = NSEqualSizes(bounds.size, NSZeroSize) && selection == nil;
  501. // new note added by note tool mode, don't add actual zero sized notes
  502. if (isInitial)
  503. bounds = CPDFListViewRectFromCenterAndSquareSize(bounds.origin, 8.0);
  504. CGFloat borderDefaultWidth = image.size.width;
  505. CGFloat borderScale = borderDefaultWidth/image.size.height;
  506. if (bounds.size.width/bounds.size.height > borderScale) {
  507. bounds.size.height = bounds.size.height;
  508. bounds.size.width = bounds.size.height*borderScale;
  509. } else {
  510. bounds.size.width = bounds.size.width;
  511. bounds.size.height = bounds.size.width/borderScale;
  512. }
  513. CPDFListStampAnnotation *newAnnotation = [[CPDFListStampAnnotation alloc] initWithDocument:self.document image:image];
  514. newAnnotation.bounds = bounds;
  515. [newAnnotation setBorderBoundsWithImage:image];
  516. [self addAnnotation:newAnnotation toPage:page];
  517. [[self undoManager] setActionName:NSLocalizedString(@"Add Note", @"Undo action name")];
  518. [self updateActiveAnnotations:@[newAnnotation]];
  519. [self setNeedsDisplayAnnotation:newAnnotation];
  520. }
  521. else NSBeep();
  522. }
  523. - (void)updateIsRightActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations {
  524. [self refreshActiveAnnotations:activeAnnotations isRight:YES];
  525. }
  526. - (void)updateActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations {
  527. [self refreshActiveAnnotations:activeAnnotations isRight:NO];
  528. }
  529. -(void)refreshActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations isRight:(BOOL)isRight {
  530. // for (CPDFAnnotation *annotation in self.activeAnnotations) {
  531. // if([annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
  532. // CPDFLinkAnnotation *linkAnnotation = (CPDFLinkAnnotation *)annotation;
  533. // if((linkAnnotation.destination || linkAnnotation.URL.length > 0)) {
  534. // } else {
  535. // [self removeAnnotation:linkAnnotation];
  536. // }
  537. // }
  538. // }
  539. if(activeAnnotations) {
  540. NSArray *selectTure = [activeAnnotations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF in %@", self.activeAnnotations]];
  541. BOOL isSame = NO;
  542. if(self.activeAnnotations.count == activeAnnotations.count && self.activeAnnotations.count == selectTure.count) isSame = YES;
  543. if(!isSame) {
  544. self.activeAnnotations = [NSMutableArray arrayWithArray:activeAnnotations];
  545. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
  546. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:isRight];
  547. dispatch_async(dispatch_get_main_queue(), ^{
  548. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  549. });
  550. }
  551. } else if (!activeAnnotations && self.activeAnnotations.count > 0) {
  552. [self.activeAnnotations removeAllObjects];
  553. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
  554. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:isRight];
  555. dispatch_async(dispatch_get_main_queue(), ^{
  556. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  557. });
  558. }
  559. }
  560. - (void)addActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations {
  561. BOOL isContains = YES;
  562. for (CPDFAnnotation *annotation in activeAnnotations) {
  563. if(![self.activeAnnotations containsObject:annotation]) {
  564. [self.activeAnnotations addObject:annotation];
  565. isContains = NO;
  566. }
  567. }
  568. if(!isContains) {
  569. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
  570. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:NO];
  571. dispatch_async(dispatch_get_main_queue(), ^{
  572. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  573. });
  574. }
  575. }
  576. - (void)removeActiveAnnotations:(NSArray<CPDFAnnotation *> *)removeAnnotations {
  577. BOOL isContains = NO;
  578. NSMutableArray *tarr = [NSMutableArray arrayWithArray:self.activeAnnotations];
  579. [tarr removeObjectsInArray:removeAnnotations];
  580. self.activeAnnotations = tarr;
  581. isContains = YES;
  582. if(isContains) {
  583. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
  584. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:NO];
  585. dispatch_async(dispatch_get_main_queue(), ^{
  586. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  587. });
  588. }
  589. }
  590. - (void)editAnnotation:(CPDFAnnotation *)annotation {
  591. if ([annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  592. [self editAnnotationFreeText:(CPDFFreeTextAnnotation *)annotation];
  593. [[self window] makeFirstResponder:self];
  594. [self setNeedsDisplayAnnotation:annotation];
  595. } else {
  596. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewEditAnnotation:forAnnotation:)]) {
  597. [self.pdfListViewDelegate PDFListViewEditAnnotation:self forAnnotation:annotation];
  598. } else {
  599. [CPDFListView cancelPreviousPerformRequestsWithTarget:self selector:@selector(showHUDHint:) object:self.hoverAnnotation];
  600. if (!self.popOver) {
  601. _popOver = [[NSPopover alloc] init];
  602. }
  603. if([annotation isKindOfClass:[CPDFTextAnnotation class]]) {
  604. __block typeof(self) blockSelf = self;
  605. _popOver.delegate = self;
  606. CPDFListEditAnnotationViewController *vc = [[CPDFListEditAnnotationViewController alloc] initWithNibName:@"CPDFListEditAnnotationViewController" bundle:[NSBundle mainBundle] annotation:annotation];
  607. vc.pdflistView = self;
  608. _popOver.contentViewController = vc;
  609. vc.closeCallBack = ^{
  610. [blockSelf.popOver close];
  611. };
  612. NSBox *box = (NSBox *)vc.view;
  613. NSView *popoverView = [[[self.popOver contentViewController] view] superview];
  614. [popoverView setWantsLayer:YES];
  615. vc.changeColorCallBack = ^{
  616. NSColor *borColor = annotation.color?:[NSColor whiteColor];
  617. [[popoverView layer] setBackgroundColor:borColor.CGColor];
  618. [blockSelf setNeedsDisplayAnnotationViewForPage:annotation.page];
  619. };
  620. NSColor *borColor = annotation.color?:[NSColor whiteColor];
  621. [[popoverView layer] setBackgroundColor:borColor.CGColor];
  622. _popOver.animates = YES;
  623. _popOver.behavior = NSPopoverBehaviorTransient;
  624. [_popOver showRelativeToRect:[self convertRect:annotation.bounds fromPage:annotation.page] ofView:self preferredEdge:NSMaxXEdge];
  625. } else if(![annotation isKindOfClass:[CPDFLinkAnnotation class]]){
  626. CPDFListAnnotationNoteWindowController *wc = [CPDFListAnnotationNoteWindowController sharedInstance];
  627. CGRect rect = wc.window.frame;
  628. CGRect anBounds = [self convertRect:annotation.bounds fromPage:annotation.page];
  629. anBounds = [self convertRectToScreen:anBounds];
  630. rect.origin.x = anBounds.origin.x;
  631. rect.origin.y = anBounds.origin.y;
  632. if (@available(macOS 10.13, *))
  633. [wc.window makeKeyAndOrderFront:nil];
  634. else
  635. [wc showWindow:nil];
  636. [wc updateAnnotation:annotation];
  637. [wc.window setFrame:rect display:YES animate:NO];
  638. wc.window.title = @"";
  639. }
  640. }
  641. }
  642. }
  643. - (void)rotateStampAnnotation:(CPDFListStampAnnotation *)annotation rotateAngle:(NSInteger)angle {
  644. if([annotation isKindOfClass:[CPDFListStampAnnotation class]]) {
  645. CPDFListStampAnnotation *stampAnnotation = (CPDFListStampAnnotation *)annotation;
  646. NSInteger rotation = stampAnnotation.rotation;
  647. rotation = rotation - angle;
  648. if (rotation < 0) {
  649. rotation += 360;
  650. } else if (rotation >= 360) {
  651. rotation-=360;
  652. }
  653. [annotation setRotation:rotation];
  654. rotation = [annotation appearanceStreamRotation];
  655. [self setNeedsDisplayAnnotationViewForPage:annotation.page];
  656. [[[self undoManager] prepareWithInvocationTarget:self] rotateStampAnnotation:annotation rotateAngle:-angle];
  657. }
  658. }
  659. - (void)rotateSignatureAnnotation:(CPDFListSignatureAnnotation *)annotation rotateAngle:(NSInteger)angle {
  660. if([annotation isKindOfClass:[CPDFListSignatureAnnotation class]]) {
  661. CPDFListSignatureAnnotation *signatureAnnotation = (CPDFListSignatureAnnotation *)annotation;
  662. NSInteger rotation = signatureAnnotation.rotation;
  663. rotation = rotation - angle;
  664. if (rotation < 0) {
  665. rotation += 360;
  666. } else if (rotation >= 360) {
  667. rotation-=360;
  668. }
  669. [signatureAnnotation setRotation:rotation];
  670. rotation = [signatureAnnotation appearanceStreamRotation];
  671. [self setNeedsDisplayAnnotationViewForPage:annotation.page];
  672. [[[self undoManager] prepareWithInvocationTarget:self] rotateSignatureAnnotation:annotation rotateAngle:-angle];
  673. }
  674. }
  675. - (void)addAnnotation:(CPDFAnnotation *)annotation toPage:(CPDFPage *)page {
  676. [[[self undoManager] prepareWithInvocationTarget:self] removeAnnotation:annotation];
  677. [annotation setModificationDate:[NSDate date]];
  678. //widget设置作者与fieldName冲突
  679. if (![annotation isKindOfClass:[CPDFWidgetAnnotation class]]) {
  680. [annotation setUserName:CPDFKitShareConfig.annotationAuthor?:NSFullUserName()];
  681. }
  682. [page addAnnotation:annotation];
  683. [self setNeedsDisplayAnnotation:annotation];
  684. [[self undoManager] setActionName:NSLocalizedString(@"Add Note", @"Undo action name")];
  685. dispatch_async(dispatch_get_main_queue(), ^{
  686. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewDidAddAnnotationNotification object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:page, CPDFListViewPageKey, annotation, CPDFListViewAnnotationKey, nil]];
  687. });
  688. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewAddAnnotations:forAddAnnotations:inPage:)])
  689. [self.pdfListViewDelegate PDFListViewAddAnnotations:self forAddAnnotations:@[annotation] inPage:page];
  690. }
  691. - (void)removeAnnotation:(CPDFAnnotation *)annotation {
  692. if ([annotation isKindOfClass:[CPDFTextAnnotation class]] && self.popOver.isShown) {
  693. // if (annotation.contents.length <= 0) {
  694. CPDFAnnotation *wasAnnotation = annotation;
  695. CPDFPage *page = wasAnnotation.page;
  696. [[[self undoManager] prepareWithInvocationTarget:self] addAnnotation:wasAnnotation toPage:page];
  697. [page removeAnnotation:wasAnnotation];
  698. [self annotationsChangedOnPage:page];
  699. [self setNeedsDisplayAnnotation:wasAnnotation];
  700. dispatch_async(dispatch_get_main_queue(), ^{
  701. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewDidRemoveAnnotationNotification object:self
  702. userInfo:[NSDictionary dictionaryWithObjectsAndKeys:wasAnnotation, CPDFListViewAnnotationKey, page, CPDFListViewPageKey, nil]];
  703. });
  704. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewRemoveAnnotations:forRemoveAnnotations:inPage:)])
  705. [self.pdfListViewDelegate PDFListViewRemoveAnnotations:self forRemoveAnnotations:@[annotation] inPage:page];
  706. // }
  707. } else {
  708. CPDFAnnotation *wasAnnotation = annotation;
  709. CPDFPage *page = wasAnnotation.page;
  710. [[[self undoManager] prepareWithInvocationTarget:self] addAnnotation:wasAnnotation toPage:page];
  711. [page removeAnnotation:wasAnnotation];
  712. [self annotationsChangedOnPage:page];
  713. [self setNeedsDisplayAnnotation:wasAnnotation];
  714. dispatch_async(dispatch_get_main_queue(), ^{
  715. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewDidRemoveAnnotationNotification object:self
  716. userInfo:[NSDictionary dictionaryWithObjectsAndKeys:wasAnnotation, CPDFListViewAnnotationKey, page, CPDFListViewPageKey, nil]];
  717. });
  718. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewRemoveAnnotations:forRemoveAnnotations:inPage:)])
  719. [self.pdfListViewDelegate PDFListViewRemoveAnnotations:self forRemoveAnnotations:@[annotation] inPage:page];
  720. }
  721. }
  722. - (void)drawPage:(CPDFPage *)pdfPage toContext:(CGContextRef)context {
  723. [super drawPage:pdfPage toContext:context];
  724. [self drawSelectionForPage:pdfPage inContext:context];
  725. BOOL isIncludText = NO;
  726. NSMutableArray *currentActiveAnnotations = [NSMutableArray array];
  727. NSMutableArray *currentPageAnnotations = [NSMutableArray array];
  728. for (CPDFAnnotation *an in self.activeAnnotations) {
  729. if([an.page isEqual:pdfPage]) {
  730. if([an isKindOfClass:[CPDFFreeTextAnnotation class]])
  731. isIncludText = YES;
  732. //多选注释框去除mark注释与便签注释
  733. if(!([an isKindOfClass:[CPDFMarkupAnnotation class]] || [an isKindOfClass:[CPDFTextAnnotation class]])) {
  734. [currentActiveAnnotations addObject:an];
  735. }
  736. [currentPageAnnotations addObject:an];
  737. }
  738. }
  739. if(currentActiveAnnotations.count > 1) {
  740. NSRect rect = [self selectionMultipleBoundsWithAnnotations:currentActiveAnnotations];
  741. CGFloat lineWidth = [self unitWidthOnPage:pdfPage];
  742. CGContextSaveGState(context);
  743. CGColorRef color = [CPDFListViewConfig defaultManager].annotationBorderColor.CGColor;
  744. CGContextSetStrokeColorWithColor(context, color);
  745. CGContextStrokeRectWithWidth(context, CGRectInset(NSRectToCGRect(rect), 0,0), lineWidth);
  746. CGContextRestoreGState(context);
  747. CGContextSaveGState(context);
  748. if(isIncludText) {
  749. CPDFListViewDrawFreeTextResizeHandle(context, rect, 4.0 * lineWidth, YES);
  750. } else {
  751. CPDFListViewDrawResizeHandles(context, rect, 4.0 * lineWidth, YES);
  752. }
  753. CGContextRestoreGState(context);
  754. }
  755. if(self.multiplAnnotationObject &&
  756. !CGRectIsEmpty(self.multiplAnnotationObject.drawRect) &&
  757. self.multiplAnnotationObject.page == pdfPage) {
  758. CGRect selectAnnotationBounds = self.multiplAnnotationObject.drawRect;
  759. NSRect rect = selectAnnotationBounds;
  760. CGFloat lineWidth = [self unitWidthOnPage:pdfPage];
  761. CGContextSaveGState(context);
  762. CGColorRef color = [CPDFListViewConfig defaultManager].annotationBorderColor.CGColor;
  763. CGContextSetStrokeColorWithColor(context, color);
  764. CGContextStrokeRectWithWidth(context, CGRectInset(NSRectToCGRect(rect), 0,0), lineWidth);
  765. CGContextRestoreGState(context);
  766. }
  767. [currentPageAnnotations enumerateObjectsUsingBlock:^(CPDFAnnotation *annotation, NSUInteger idx, BOOL * _Nonnull stop) {
  768. if (annotation.page && [annotation.page isEqual:pdfPage]) {
  769. [annotation drawSelectionHighlightForView:self inContext:context isHover:NO];
  770. }
  771. }];
  772. [self.selectAnnotations enumerateObjectsUsingBlock:^(CPDFAnnotation *annotation, NSUInteger idx, BOOL * _Nonnull stop) {
  773. if (annotation.page && [annotation.page isEqual:pdfPage]) {
  774. [annotation drawSelectionHighlightForView:self inContext:context isHover:NO];
  775. }
  776. }];
  777. if (self.hoverAnnotation.page && [self.hoverAnnotation.page isEqual:pdfPage] && ![self.activeAnnotations containsObject:self.hoverAnnotation]) {
  778. [self.hoverAnnotation drawSelectionHighlightForView:self inContext:context isHover:YES];
  779. }
  780. if(!CGRectEqualToRect(self.multiplSelectBounds, CGRectZero) && [self.multiplSelectPage isEqual:pdfPage]) {
  781. @synchronized (self) {
  782. CGContextSetLineCap(context, kCGLineCapRound);
  783. CGContextSetLineWidth(context, 1.0);
  784. CGContextSetStrokeColorWithColor(context, [CPDFListViewConfig defaultManager].annotationBorderColor.CGColor);
  785. CGContextBeginPath(context);
  786. CGContextMoveToPoint(context, self.multiplSelectBounds.origin.x, self.multiplSelectBounds.origin.y);
  787. CGContextAddLineToPoint(context, self.multiplSelectBounds.origin.x+ self.multiplSelectBounds.size.width,self.multiplSelectBounds.origin.y);
  788. CGContextAddLineToPoint(context, self.multiplSelectBounds.origin.x + self.multiplSelectBounds.size.width,self.multiplSelectBounds.origin.y + + self.multiplSelectBounds.size.height);
  789. CGContextAddLineToPoint(context, self.multiplSelectBounds.origin.x,self.multiplSelectBounds.origin.y + + self.multiplSelectBounds.size.height);
  790. CGContextClosePath(context);
  791. CGFloat lengths[] = {5,5};
  792. CGContextSetLineDash(context, 0, lengths,2);
  793. CGContextStrokePath(context);
  794. }
  795. }
  796. }
  797. - (void)drawSelectionForPage:(CPDFPage *)pdfPage inContext:(CGContextRef)context {
  798. NSRect rect;
  799. NSUInteger pageIndex;
  800. @synchronized (self) {
  801. pageIndex = self.selectionPageIndex;
  802. rect = self.selectionRect;
  803. }
  804. if (pageIndex != NSNotFound) {
  805. BOOL isWidget = NO;
  806. for (CPDFAnnotation * annotation in self.activeAnnotations) {
  807. if ([annotation isForm]) {
  808. isWidget = YES;
  809. break;
  810. }
  811. }
  812. if (isWidget &&
  813. _toolMode != CFormToolMode) {
  814. return;
  815. }
  816. NSRect bounds = [pdfPage boundsForBox:[self displayBox]];
  817. CGFloat radius = HANDLE_SIZE * [self unitWidthOnPage:pdfPage];
  818. CGColorRef color = CGColorCreateGenericGray(0.0, 0.6);
  819. CGContextSetFillColorWithColor(context, color);
  820. CGColorRelease(color);
  821. CGContextBeginPath(context);
  822. CGContextAddRect(context, NSRectToCGRect(bounds));
  823. CGContextAddRect(context, NSRectToCGRect(rect));
  824. CGContextEOFillPath(context);
  825. if ([pdfPage pageIndex] != pageIndex) {
  826. color = CGColorCreateGenericGray(0.0, 0.3);
  827. CGContextSetFillColorWithColor(context, color);
  828. CGColorRelease(color);
  829. CGContextFillRect(context, NSRectToCGRect(rect));
  830. }
  831. CPDFListViewDrawResizeHandles(context, rect, radius, YES);
  832. }
  833. }
  834. - (void)removeShapeLayer {
  835. if (![self.shapeLayerTopH isEqual:nil] && ![self.shapeLayerBottomH isEqual:nil] && ![self.shapeLayerLeftV isEqual:nil] && ![self.shapeLayerRightV isEqual:nil]) {
  836. [self.shapeLayerTopH removeFromSuperlayer];
  837. [self.shapeLayerBottomH removeFromSuperlayer];
  838. [self.shapeLayerLeftV removeFromSuperlayer];
  839. [self.shapeLayerRightV removeFromSuperlayer];
  840. self.shapeLayerTopH = nil;
  841. self.shapeLayerBottomH = nil;
  842. self.shapeLayerLeftV = nil;
  843. self.shapeLayerRightV = nil;
  844. }
  845. }
  846. - (BOOL)consistentTypeWithAnnotation:(CPDFAnnotation *)annotation {
  847. if (self.annotationType == CAnnotationTypeSignSignature) {
  848. if (![annotation isKindOfClass:[CPDFSignatureAnnotation class]])
  849. return NO;
  850. } else if (self.annotationType == CAnnotationTypeStamp) {
  851. if (![annotation isKindOfClass:[CPDFStampAnnotation class]])
  852. return NO;
  853. } else if (self.annotationType == CAnnotationTypeAnchored) {
  854. if (![annotation isKindOfClass:[CPDFTextAnnotation class]])
  855. return NO;
  856. } else if (self.annotationType == CAnnotationTypeLink) {
  857. if (![annotation isKindOfClass:[CPDFLinkAnnotation class]])
  858. return NO;
  859. } else if (self.annotationType == CAnnotationTypeFreeText) {
  860. if (![annotation isKindOfClass:[CPDFFreeTextAnnotation class]])
  861. return NO;
  862. } else if (self.annotationType == CAnnotationTypeLine || self.annotationType == CAnnotationTypeArrow || self.annotationType == CAnnotationTypeSquare || self.annotationType == CAnnotationTypeCircle) {
  863. if (![annotation isKindOfClass:[CPDFLineAnnotation class]] && ![annotation isKindOfClass:[CPDFSquareAnnotation class]] && ![annotation isKindOfClass:[CPDFCircleAnnotation class]])
  864. return NO;
  865. } else if (self.annotationType == CAnnotationTypeHighlight || self.annotationType == CAnnotationTypeUnderline || self.annotationType == CAnnotationTypeStrikeOut) {
  866. if (![annotation isKindOfClass:[CPDFMarkupAnnotation class]])
  867. return NO;
  868. } else if (self.annotationType == CAnnotationTypeInk) {
  869. if (![annotation isKindOfClass:[CPDFInkAnnotation class]])
  870. return NO;
  871. } else if (self.annotationType == CAnnotationTypeRadioButton) {
  872. } else if (self.annotationType == CAnnotationTypeCheckBox) {
  873. } else if (self.annotationType == CAnnotationTypeTextField) {
  874. } else if (self.annotationType == CAnnotationTypeComboBox) {
  875. } else if (self.annotationType == CAnnotationTypeListMenu) {
  876. } else if (self.annotationType == CAnnotationTypeActionButton) {
  877. } else if (self.annotationType == CAnnotationTypeSignature) {
  878. } else if (self.annotationType == CAnnotationTypeSignText) {
  879. } else if (self.annotationType == CAnnotationTypeSignFalse) {
  880. } else if (self.annotationType == CAnnotationTypeSignTure) {
  881. } else if (self.annotationType == CAnnotationTypeSignCircle) {
  882. } else if (self.annotationType == CAnnotationTypeSignLine) {
  883. } else if (self.annotationType == CAnnotationTypeSignDot) {
  884. } else if (self.annotationType == CAnnotationTypeSignConfig) {
  885. } else if (self.annotationType == CAnnotationTypeSignDate) {
  886. }
  887. return YES;
  888. }
  889. #pragma mark Zooming
  890. - (void)zoomToRect:(NSRect)rect onPage:(CPDFPage *)page {
  891. if (NSIsEmptyRect(rect) == NO) {
  892. BOOL isLegacy = [NSScroller respondsToSelector:@selector(preferredScrollerStyle)] == NO || [NSScroller preferredScrollerStyle] == NSScrollerStyleLegacy;
  893. NSRect bounds = [self bounds];
  894. CGFloat scale = 1.0;
  895. if (isLegacy) {
  896. bounds.size.width -= [NSScroller scrollerWidth];
  897. bounds.size.height -= [NSScroller scrollerWidth];
  898. }
  899. if (NSWidth(bounds) * NSHeight(rect) > NSWidth(rect) * NSHeight(bounds))
  900. scale = NSHeight(bounds) / NSHeight(rect);
  901. else
  902. scale = NSWidth(bounds) / NSWidth(rect);
  903. [self setScaleFactor:scale];
  904. NSScrollView *scrollView = [self scrollView];
  905. if (isLegacy && ([scrollView hasHorizontalScroller] == NO || [scrollView hasVerticalScroller] == NO)) {
  906. if ([scrollView hasVerticalScroller])
  907. bounds.size.width -= [NSScroller scrollerWidth];
  908. if ([scrollView hasHorizontalScroller])
  909. bounds.size.height -= [NSScroller scrollerWidth];
  910. if (NSWidth(bounds) * NSHeight(rect) > NSWidth(rect) * NSHeight(bounds))
  911. scale = NSHeight(bounds) / NSHeight(rect);
  912. else
  913. scale = NSWidth(bounds) / NSWidth(rect);
  914. [self setScaleFactor:scale];
  915. }
  916. [self goToRect:rect onPage:page];
  917. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  918. CGPoint tPagePoint = CGPointMake(rect.origin.x, rect.origin.y+rect.size.height);
  919. [self goToTargetPoint:tPagePoint onPage:page atScrollPosition:CScrollPositionTop];
  920. });
  921. }
  922. }
  923. #pragma mark - NSPopoverDelegate
  924. - (void)popoverWillClose:(NSNotification *)notification {
  925. NSPopover *popover = notification.object;
  926. if([popover.contentViewController isKindOfClass:[CPDFListEditAnnotationViewController class]]) {
  927. CPDFListEditAnnotationViewController *listEditAnnotationViewController = (CPDFListEditAnnotationViewController *)popover.contentViewController;
  928. CPDFAnnotation *annotation = listEditAnnotationViewController.annotation;
  929. NSString *contensString = listEditAnnotationViewController.contentString;
  930. if([annotation isKindOfClass:[CPDFTextAnnotation class]]) {
  931. if(contensString && contensString.length > 0) {
  932. annotation.contents = listEditAnnotationViewController.contentString;
  933. [self setNeedsDisplayAnnotation:annotation];
  934. } else {
  935. if([self.activeAnnotations containsObject:annotation]) {
  936. [self.activeAnnotations removeObject:annotation];
  937. }
  938. [self removeAnnotation:annotation];
  939. }
  940. } else {
  941. annotation.contents = listEditAnnotationViewController.contentString;
  942. [self setNeedsDisplayAnnotation:annotation];
  943. }
  944. }
  945. self.popOver = nil;
  946. }
  947. - (void)handlePageChangedNotification:(NSNotification *)notification {
  948. self.copyCount = 0;
  949. }
  950. @end