CPDFListView.m 53 KB

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