CPDFListView.m 52 KB

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