CPDFListView.m 53 KB

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