CPDFListView.m 52 KB

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