CPDFListView.m 48 KB

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