CPDFListView.m 48 KB

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