CPDFListView.m 52 KB

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