CPDFListView.m 53 KB

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