CPDFListView.m 52 KB

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