CPDFListView.m 53 KB

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