CPDFListView.m 53 KB

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