CPDFListView.m 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  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 "CPDFListView+Private.h"
  17. #import "CPDFListView+Event.h"
  18. #import "CPDFListView+Tool.h"
  19. #import "CPDFListView+Extension.h"
  20. #import "CPDFListView+UndoManager.h"
  21. #import "NSImage+PDFListView.h"
  22. #import "NSCursor+PDFListView.h"
  23. #import <PDF_Reader_Pro-Swift.h>
  24. NSNotificationName const CPDFListViewSelectionChangedNotification = @"CPDFListViewSelectionChangedNotification";
  25. NSNotificationName const CPDFListViewToolModeChangeNotification = @"CPDFListViewToolModeChangeNotification";
  26. NSNotificationName const CPDFListViewAnnotationTypeChangeNotification = @"CPDFListViewAnnotationTypeChangeNotification";
  27. NSNotificationName const CPDFListViewMagnificationChangedNotification = @"CPDFListViewMagnificationChangedNotification";
  28. NSNotificationName const CPDFListViewDidAddAnnotationNotification = @"CPDFListViewDidAddAnnotationNotification";
  29. NSNotificationName const CPDFListViewDidRemoveAnnotationNotification = @"CPDFListViewDidRemoveAnnotationNotification";
  30. NSNotificationName const CPDFListViewActiveAnnotationsChangeNotification = @"CPDFListViewActiveAnnotationsChangeNotification";
  31. NSNotificationName const CPDFListViewAnnotationsAttributeHasChangeNotification = @"CPDFListViewAnnotationsAttributeHasChangeNotification";
  32. NSNotificationName const KMPDFViewTableAnnotationDidChangeNotification = @"KMPDFViewTableAnnotationDidChangeNotification";
  33. @implementation CPDFListView
  34. #pragma mark - Init
  35. - (id)init {
  36. self = [super init];
  37. if (self) {
  38. [self commonInitialization];
  39. }
  40. return self;
  41. }
  42. - (id)initWithFrame:(NSRect)frameRect {
  43. self = [super initWithFrame:frameRect];
  44. if (self) {
  45. [self commonInitialization];
  46. }
  47. return self;
  48. }
  49. - (id)initWithCoder:(NSCoder *)decoder {
  50. self = [super initWithCoder:decoder];
  51. if (self) {
  52. [self commonInitialization];
  53. }
  54. return self;
  55. }
  56. - (void)updateLayer {
  57. [super updateLayer];
  58. if (@available(macOS 10.14, *)) {
  59. if ([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewBackgroundColor)]) {
  60. NSColor *color = [self.pdfListViewDelegate PDFListViewBackgroundColor];
  61. self.backgroundColor = color;
  62. }
  63. }
  64. }
  65. - (void)commonInitialization {
  66. self.selectionPageIndex = NSNotFound;
  67. self.clickLineAnnotation = nil;
  68. self.isClickDoubleCreatLine = NO;
  69. NSArray *sendTypes = [NSArray arrayWithObjects:NSPasteboardTypePDF, NSPasteboardTypeTIFF, nil];
  70. [NSApp registerServicesMenuSendTypes:sendTypes returnTypes:[NSArray array]];
  71. KMPDFViewMode viewMode = KMPDFViewModeNormal;
  72. if ([[NSUserDefaults standardUserDefaults] objectForKey:@"kPDFViewModeKey"]) {
  73. viewMode = (KMPDFViewMode)[[[NSUserDefaults standardUserDefaults] objectForKey:@"kPDFViewModeKey"] integerValue];
  74. }
  75. NSData * data = nil;
  76. if ([[NSUserDefaults standardUserDefaults] objectForKey:@"kKMPDFViewModeThemesArray"]) {
  77. data = [[NSUserDefaults standardUserDefaults] objectForKey:@"kKMPDFViewModeThemesArray"];
  78. }
  79. NSArray * appArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  80. NSMutableArray * mutableArray = [NSMutableArray arrayWithArray:appArray];
  81. if (mutableArray.count == 0) {
  82. NSColor *normalColor = [NSColor whiteColor];
  83. NSColor *softColor = [NSColor colorWithDeviceRed:238.0/255.0 green:232.0/255.0 blue:216.0/255.0 alpha:1.0];
  84. NSColor *nightColor = [NSColor blackColor];
  85. NSColor *greenColor = [NSColor colorWithDeviceRed:153.0/255.0 green:207.0/255.0 blue:161.0/255.0 alpha:1.0];
  86. mutableArray = [NSMutableArray arrayWithArray:@[normalColor, softColor,nightColor, greenColor]];
  87. }
  88. NSColor *pageBackgroundColor = mutableArray[viewMode];
  89. if (viewMode == KMPDFViewModeOther &&
  90. [[NSUserDefaults standardUserDefaults] objectForKey:@"kPDFViewModeColorKey"]) {
  91. CGFloat red,green,blue,alpha;
  92. NSArray *colors = [[NSUserDefaults standardUserDefaults] objectForKey:@"kPDFViewModeColorKey"];
  93. red = colors.count>0 ? [colors[0] floatValue] : 0;
  94. green = colors.count>1 ? [colors[1] floatValue] : 0;
  95. blue = colors.count>2 ? [colors[2] floatValue] : 0;
  96. alpha = colors.count>3 ? [colors[3] floatValue] : 0;
  97. pageBackgroundColor = [NSColor colorWithDeviceRed:red green:green blue:blue alpha:alpha];
  98. }
  99. self.viewMode = viewMode;
  100. [self setPageBackgroundColorWithColor:pageBackgroundColor viewMode:viewMode];
  101. self.activeAnnotations = [NSMutableArray array];
  102. self.selectAnnotations = [NSMutableArray array];
  103. self.dragHoverPoints = [NSMutableArray array];
  104. self.aCopyAnnotations = [NSMutableArray array];
  105. [self registerAsObserver];
  106. [self addTrackingArea];
  107. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlePageChangedNotification:)
  108. name:CPDFViewPageChangedNotification object:self];
  109. }
  110. - (void)addTrackingArea {
  111. self.trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect | NSTrackingActiveInKeyWindow | NSTrackingMouseMoved owner:self userInfo:nil];
  112. [self addTrackingArea:self.trackingArea];
  113. }
  114. - (void)resetPDFToolTipRects {
  115. [self removePDFToolTipRects];
  116. if ([self document] && [self window]) {
  117. NSRect visibleRect = [self visibleContentRect];
  118. NSView *docView = [self documentView];
  119. BOOL hasLinkToolTips = (self.toolMode == CTextToolMode || self.toolMode == CMoveToolMode || self.toolMode == CNoteToolMode);
  120. for (CPDFPage *page in [self visiblePages]) {
  121. for (CPDFAnnotation *annotation in [page annotations]) {
  122. if ([annotation isNote] || (hasLinkToolTips && [annotation linkDestination])) {
  123. // NSLog(@"%@",NSStringFromRect([annotation bounds]));
  124. NSRect rect = NSIntersectionRect([self convertRect:[annotation bounds] fromPage:page], visibleRect);
  125. if (NSIsEmptyRect(rect) == NO) {
  126. rect = [self convertRect:rect toView:docView];
  127. // NSLog(@"%@",NSStringFromRect(rect));
  128. NSDictionary *userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:annotation, SKAnnotationKey, nil];
  129. NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:rect options:NSTrackingMouseEnteredAndExited | NSTrackingActiveInActiveApp owner:self userInfo:userInfo];
  130. [docView addTrackingArea:area];
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. - (void)removePDFToolTipRects {
  138. NSView *docView = [self documentView];
  139. NSArray *trackingAreas = [[docView trackingAreas] copy];
  140. for (NSTrackingArea *area in trackingAreas) {
  141. if ([area owner] == self && [[area userInfo] objectForKey:SKAnnotationKey])
  142. [docView removeTrackingArea:area];
  143. }
  144. }
  145. - (void)goToPageIndex:(NSInteger)pageIndex animated:(BOOL)animated {
  146. if (self.currentPageIndex != pageIndex) {
  147. [self recordForwardBackWithPageIndex:self.currentPageIndex];
  148. }
  149. [super goToPageIndex:pageIndex animated:animated];
  150. }
  151. - (void)goToSelection:(CPDFSelection *)selection animated:(BOOL)animated {
  152. if (self.currentPageIndex != selection.page.pageIndex) {
  153. [self recordForwardBackWithPageIndex:self.currentPageIndex];
  154. }
  155. [super goToSelection:selection animated:animated];
  156. }
  157. - (void)goToDestination:(CPDFDestination *)destination {
  158. [super goToDestination:destination];
  159. }
  160. - (void)goToRect:(CGRect)rect onPage:(CPDFPage *)page animated:(BOOL)animated {
  161. if (self.currentPageIndex != page.pageIndex) {
  162. [self recordForwardBackWithPageIndex:self.currentPageIndex];
  163. }
  164. [super goToRect:rect onPage:page animated:animated];
  165. }
  166. - (void)goToTargetPoint:(NSPoint)point onPage:(CPDFPage *)page atScrollPosition:(CScrollPosition)scrollPosition {
  167. if (self.currentPageIndex != page.pageIndex) {
  168. [self recordForwardBackWithPageIndex:self.currentPageIndex];
  169. }
  170. [super goToTargetPoint:point onPage:page atScrollPosition:scrollPosition];
  171. }
  172. #pragma mark - Setter & Get
  173. - (void)setDocument:(CPDFDocument *)document {
  174. @synchronized (self) {
  175. self.selectionRect = NSZeroRect;
  176. self.selectionPageIndex = NSNotFound;
  177. }
  178. // self.toolMode = CFormToolMode;
  179. // self.annotationType = CAnnotationTypeRadioButton;
  180. // CStampSignatureObject *objc = [[CStampSignatureObject alloc] initImageStampWithFilePath:[[NSBundle mainBundle] pathForResource:@"Quick Start Guide" ofType:@"pdf"]];
  181. // [self setAddStampObject:objc keepToolModel:YES];
  182. //
  183. [super setDocument:document];
  184. [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(resetPDFToolTipRects) object:nil];
  185. [self performSelector:@selector(resetPDFToolTipRects) withObject:nil afterDelay:2];
  186. [self.window makeFirstResponder:self];
  187. }
  188. -(void)setToolMode:(CToolMode)newToolMode {
  189. if(newToolMode != _toolMode) {
  190. BOOL isDisplayAnnotationView = NO;
  191. if(self.isSetLinkDestinationArea)
  192. self.isSetLinkDestinationArea = NO;
  193. if ((CTextToolMode == _toolMode || CNoteToolMode == _toolMode) && CTextToolMode != newToolMode) {
  194. if (newToolMode != CNoteToolMode && self.activeAnnotation) {
  195. if([self.activeAnnotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  196. CPDFFreeTextAnnotation *freeTextAn = (CPDFFreeTextAnnotation *)self.activeAnnotation;
  197. if ([self isEditWithCurrentFreeText:freeTextAn]) {
  198. [self commitEditAnnotationFreeText:freeTextAn];
  199. }
  200. }
  201. [self updateActiveAnnotations:@[]];
  202. }
  203. if ([self currentSelection])
  204. [self setCurrentSelection:nil];
  205. } else if (_toolMode == CSelectToolMode && NSEqualRects(self.selectionRect, NSZeroRect) == NO) {
  206. self.selectionRect = NSZeroRect;
  207. self.selectionPageIndex = NSNotFound;
  208. dispatch_async(dispatch_get_main_queue(), ^{
  209. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFViewSelectionChangedNotification object:self];
  210. });
  211. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangedSelectionOrMagnification:)])
  212. [self.pdfListViewDelegate PDFListViewChangedSelectionOrMagnification:self];
  213. isDisplayAnnotationView = YES;
  214. }
  215. BOOL highlightFormFiled = [[NSUserDefaults standardUserDefaults] boolForKey:@"kPDFViewHighlightFormFiledKey"];
  216. if (CFormToolMode == _toolMode && !highlightFormFiled) {
  217. [[CPDFKitConfig sharedInstance] setEnableFormFieldHighlight:highlightFormFiled];
  218. isDisplayAnnotationView = YES;
  219. } else if (CFormToolMode == newToolMode && !highlightFormFiled) {
  220. [[CPDFKitConfig sharedInstance] setEnableFormFieldHighlight:YES];
  221. isDisplayAnnotationView = YES;
  222. }
  223. if(CEditPDFToolMode == newToolMode)
  224. [self beginEditingLoadType:CEditingLoadTypeText|CEditingLoadTypeImage];
  225. if(_toolMode == CEditPDFToolMode) {
  226. if(self.isEdited) {
  227. [self commitEditing];
  228. }
  229. [self endOfEditing];
  230. }
  231. _toolMode = newToolMode;
  232. self.selectZoomToolModeZoomOut = NO;
  233. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangedToolMode:forToolMode:)])
  234. [self.pdfListViewDelegate PDFListViewChangedToolMode:self forToolMode:newToolMode];
  235. dispatch_async(dispatch_get_main_queue(), ^{
  236. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewToolModeChangeNotification object:self];
  237. });
  238. [self resetPDFViewAnnotation];
  239. [self resetPDFToolTipRects];
  240. if(isDisplayAnnotationView)
  241. [self setNeedsDisplayAnnotationViewForVisiblePages];
  242. }
  243. }
  244. -(void)setShowHopnumber:(BOOL)showHopnumber {
  245. _showHopnumber = showHopnumber;
  246. [self setNeedsDisplayAnnotationViewForVisiblePages];
  247. }
  248. - (void)setAnnotationType:(CAnnotationType)annotationType {
  249. if (_annotationType != annotationType) {
  250. if(CAnnotationTypeLink == annotationType || CAnnotationTypeLink == _annotationType)
  251. [self setNeedsDisplayAnnotationViewForVisiblePages];
  252. _annotationType = annotationType;
  253. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangedAnnotationType:forAnnotationType:)])
  254. [self.pdfListViewDelegate PDFListViewChangedAnnotationType:self forAnnotationType:annotationType];
  255. dispatch_async(dispatch_get_main_queue(), ^{
  256. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewAnnotationTypeChangeNotification object:self];
  257. });
  258. }
  259. if(self.activeAnnotations.count > 0) {
  260. if([self.activeAnnotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  261. CPDFFreeTextAnnotation *freeTextAn = (CPDFFreeTextAnnotation *)self.activeAnnotation;
  262. if ([self isEditWithCurrentFreeText:freeTextAn]) {
  263. [self commitEditAnnotationFreeText:freeTextAn];
  264. }
  265. }
  266. [self updateActiveAnnotations:@[]];
  267. [self setNeedsDisplayAnnotationViewForVisiblePages];
  268. }
  269. if(self.isSetLinkDestinationArea)
  270. self.isSetLinkDestinationArea = NO;
  271. [self resetPDFViewAnnotation];
  272. }
  273. - (NSRect)currentSelectionRect {
  274. if (CSelectToolMode == self.toolMode)
  275. return self.selectionRect;
  276. return NSZeroRect;
  277. }
  278. - (CPDFAnnotation *)activeAnnotation {
  279. return self.activeAnnotations.lastObject;
  280. }
  281. - (void)setHideNotes:(BOOL)hideNotes {
  282. _hideNotes = hideNotes;
  283. for(NSInteger i = 0;i<self.document.pageCount;i++) {
  284. CPDFPage *page = [self.document pageAtIndex:i];
  285. for (CPDFAnnotation *annotation in page.annotations) {
  286. [annotation setAnnotationShouldDisplay:!self.hideNotes];
  287. [annotation setHidden: self.hideNotes];
  288. }
  289. }
  290. [self setNeedsDisplayAnnotationViewForVisiblePages];
  291. }
  292. -(void)setIsSetLinkDestinationArea:(BOOL)isSetLinkDestinationArea {
  293. _isSetLinkDestinationArea = isSetLinkDestinationArea;
  294. if (_isSetLinkDestinationArea) {
  295. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewLinkDestinationStart:withActiveAnnotation:)]) {
  296. [self.pdfListViewDelegate PDFListViewLinkDestinationStart:self withActiveAnnotation:self.activeAnnotation];
  297. }
  298. } else {
  299. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewLinkDestinationEnd:withActiveAnnotation:)]) {
  300. [self.pdfListViewDelegate PDFListViewLinkDestinationEnd:self withActiveAnnotation:self.activeAnnotation];
  301. }
  302. }
  303. }
  304. - (void)setPageBackgroundColorWithColor:(NSColor *)color viewMode:(KMPDFViewMode)viewMode {
  305. if (self.activeAnnotation != nil) {
  306. return;
  307. }
  308. _viewMode = viewMode;
  309. if (viewMode == KMPDFViewModeNormal) {
  310. self.displayMode = CPDFDisplayModeNormal;
  311. } else if (viewMode == KMPDFViewModeSoft) {
  312. self.displayMode = CPDFDisplayModeSoft;
  313. } else if (viewMode == KMPDFViewModeNight) {
  314. self.displayMode = CPDFDisplayModeNight;
  315. } else if (viewMode == KMPDFViewModeGreen) {
  316. self.displayMode = CPDFDisplayModeGreen;
  317. } else {
  318. self.displayMode = CPDFDisplayModeCustom;
  319. self.displayModeCustomColor = color;
  320. }
  321. [self layoutDocumentView];
  322. [self setNeedsDisplayForVisiblePages];
  323. [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInteger:viewMode] forKey:@"kPDFViewModeKey"];
  324. [[NSUserDefaults standardUserDefaults] synchronize];
  325. }
  326. - (void)updateDocumentViewForBackgroundColor{
  327. [self layoutDocumentView];
  328. // [self setNeedsDisplayForVisiblePages];
  329. [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInteger:_viewMode] forKey:@"kPDFViewModeKey"];
  330. [[NSUserDefaults standardUserDefaults] synchronize];
  331. }
  332. - (void)setViewSplitMode:(KMPDFViewSplitMode)viewSplitMode {
  333. if (_viewSplitMode == viewSplitMode) {
  334. return;
  335. }
  336. _viewSplitMode = viewSplitMode;
  337. [self setNeedsDisplay:YES];
  338. }
  339. - (void)setCurrentSelection:(CPDFSelection *)currentSelection {
  340. if (self.toolMode == CNoteToolMode && self.annotationType == CAnnotationTypeHighlight) {
  341. [currentSelection setColor:[[NSUserDefaults standardUserDefaults] colorForKey:CHighlightNoteColorKey]];
  342. }
  343. if (currentSelection == nil) {
  344. // selection = [[[PDFSelection alloc] initWithDocument:[self document]] autorelease];
  345. }
  346. [super setCurrentSelection:currentSelection];
  347. }
  348. #pragma mark - menu add image
  349. - (void)addImageAnnotation:(NSImage *)image center:(NSPoint)center isRemoveBGColor:(BOOL)isRemoveBGColor{
  350. [self setToolMode:CTextToolMode]; //不支持重复添加,重置状态
  351. if (isRemoveBGColor) {
  352. NSData *imageData = [image TIFFRepresentation];
  353. CGImageRef imageRef;
  354. if (imageData) {
  355. CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
  356. imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
  357. } else {
  358. return NSBeep();
  359. }
  360. const int imageWidth = image.size.width;
  361. const int imageHeight = image.size.height;
  362. size_t bytesPerRow = imageWidth * 4;
  363. uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);
  364. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  365. CGContextRef context = CGBitmapContextCreate(rgbImageBuf,
  366. imageWidth,
  367. imageHeight,
  368. 8,
  369. bytesPerRow,
  370. colorSpace,
  371. kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
  372. CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), imageRef);
  373. int pixelNum = imageWidth * imageHeight;
  374. uint32_t* pCurPtr = rgbImageBuf;
  375. for (int i = 0; i < pixelNum; i++, pCurPtr++) {
  376. uint8_t* ptr = (uint8_t*)pCurPtr;
  377. if (ptr[1] > 240 && ptr[2] > 240 && ptr[3] > 240) {
  378. ptr[0] = 0;
  379. }
  380. }
  381. CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, nil);
  382. imageRef = CGImageCreate(imageWidth,
  383. imageHeight,
  384. 8,
  385. 32,
  386. bytesPerRow,
  387. colorSpace,
  388. kCGImageAlphaLast |kCGBitmapByteOrder32Little,
  389. dataProvider,
  390. NULL,
  391. true,
  392. kCGRenderingIntentDefault);
  393. CGDataProviderRelease(dataProvider);
  394. NSImage *newImage = nil;
  395. if (imageRef) {
  396. NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
  397. CGContextRef imageContext = nil;
  398. // Get the image dimensions.
  399. imageRect.size.height = CGImageGetHeight(imageRef);
  400. imageRect.size.width = CGImageGetWidth(imageRef);
  401. // Create a new image to receive the Quartz image data.
  402. newImage = [[NSImage alloc] initWithSize:imageRect.size];
  403. [newImage lockFocus];
  404. // Get the Quartz context and draw.
  405. imageContext = (CGContextRef)[[NSGraphicsContext currentContext] CGContext];
  406. CGContextDrawImage(imageContext, *(CGRect*)&imageRect, imageRef);
  407. [newImage unlockFocus];
  408. CGImageRelease(imageRef);
  409. }
  410. if (newImage) {
  411. [self addAnnotationWithImage:newImage center:center];
  412. } else {
  413. [self addAnnotationWithImage:image center:center];
  414. }
  415. } else {
  416. [self addAnnotationWithImage:image center:center];
  417. }
  418. }
  419. - (void)addAnnotationWithImage:(NSImage *)image center:(NSPoint)center {
  420. CPDFPage *page = nil;
  421. NSRect bounds = NSZeroRect;
  422. CPDFSelection *selection = [self currentSelection];
  423. page = [selection safeFirstPage];
  424. // if the mouse was in the toolbar and there is a page below the toolbar, we get a point outside of the visible rect
  425. page = NSMouseInRect(center, [self visibleContentRect], [self isFlipped]) ? [self pageForPoint:center nearest:NO] : nil;
  426. if (page == nil) {
  427. // Get center of the PDFView.
  428. // NSRect viewFrame = [self frame];
  429. // center = [self KMCpdfCenterPoint:viewFrame];//KMCpdfCenterPoint(viewFrame);
  430. page = [self pageForPoint: center nearest: YES];
  431. if (page == nil) {
  432. // Get center of the current page
  433. page = [self currentPage];
  434. NSPoint point = [self KMCpdfCenterPoint:[page boundsForBox:[self displayBox]]];
  435. center = [self convertPoint:point fromPage:page];
  436. }
  437. }
  438. CGFloat defaultWidth = 360;
  439. CGFloat defaultHeight = 90;
  440. CGFloat scaleS = MIN(defaultWidth/image.size.width, defaultHeight/image.size.height);
  441. NSSize defaultSize = CGSizeMake(image.size.width * scaleS, image.size.height * scaleS);
  442. // Convert to "page space".
  443. // center = [self KMCpdfIntegralPoint:[self convertPoint:center toPage:page]];
  444. // bounds = [self KMCpdfRectFromCenterAndSize:center size:defaultSize];
  445. // Make sure it fits in the page
  446. // bounds = [self KMCpdfConstrainRect:bounds boundary:[page boundsForBox:[self displayBox]]];
  447. if (page != nil) {
  448. BOOL isInitial = NSEqualSizes(bounds.size, NSZeroSize) && selection == nil;
  449. NSRect pageBounds = [page boundsForBox:CPDFDisplayMediaBox];
  450. // new note added by note tool mode, don't add actual zero sized notes
  451. if (isInitial)
  452. bounds = [self KMCRectFromCenterAndSquareSizeWith:bounds.origin size:8.0];
  453. if(!CGPointEqualToPoint(self.lastAddAnnotationPoint, CGPointZero)) {
  454. bounds.origin.x = self.lastAddAnnotationPoint.x + 3.0;
  455. bounds.origin.y = self.lastAddAnnotationPoint.y - 3.0;
  456. CGFloat pageWidth = pageBounds.size.width;
  457. if (bounds.origin.x + bounds.size.width >pageWidth) {
  458. bounds.origin.x = pageWidth - bounds.size.width;
  459. }
  460. if (bounds.origin.y - bounds.size.height < 0) {
  461. bounds.origin.y = bounds.size.height;
  462. }
  463. }
  464. self.lastAddAnnotationPoint = bounds.origin;
  465. CPDFStampAnnotation *newAnnotation = [[CPDFStampAnnotation alloc]initWithDocument:self.document image:image];
  466. newAnnotation.bounds = CGRectMake(center.x, center.y, defaultSize.width, defaultSize.height);
  467. [newAnnotation registerUserName];
  468. [self addAnnotation:newAnnotation toPage:page];
  469. [[self undoManager] setActionName:NSLocalizedString(@"Add Note", @"Undo action name")];
  470. [self.activeAnnotations removeAllObjects];
  471. [self.activeAnnotations addObject:newAnnotation];
  472. }
  473. else NSBeep();
  474. }
  475. - (NSRect)KMCRectFromCenterAndSquareSizeWith: (NSPoint)center size:(CGFloat)size{
  476. NSRect rect = NSZeroRect;
  477. rect.origin.x = center.x - 0.5 * size;
  478. rect.origin.y = center.y - 0.5 * size;
  479. rect.size.width = size;
  480. rect.size.height = size;
  481. return rect;
  482. }
  483. - (NSRect)KMCpdfRectFromCenterAndSize: (NSPoint)center size: (NSSize)size{
  484. NSRect rect = NSZeroRect;
  485. rect.origin.x = center.x - 0.5 * size.width;
  486. rect.origin.y = center.y - 0.5 * size.height;
  487. rect.size = size;
  488. return rect;
  489. }
  490. - (NSPoint)KMCpdfCenterPoint:(NSRect)rect{
  491. return NSMakePoint(NSMidX(rect), NSMidY(rect));
  492. }
  493. - (NSPoint)KMCpdfIntegralPoint:(NSPoint)point{
  494. return NSMakePoint(round(point.x), round(point.y));
  495. }
  496. - (NSRect)KMCpdfConstrainRect:(NSRect)rect boundary: (NSRect)boundary{
  497. NSRect newRect = rect;
  498. if (NSWidth(rect) > NSWidth(boundary)){
  499. newRect.size.width = NSWidth(boundary);
  500. }
  501. if (NSHeight(rect) > NSHeight(boundary)){
  502. newRect.size.height = NSHeight(boundary);
  503. }
  504. if (NSMinX(rect) < NSMinX(boundary)){
  505. newRect.origin.x = NSMinX(boundary);
  506. }else if (NSMaxX(rect) > NSMaxX(boundary)){
  507. newRect.origin.x = NSMaxX(boundary) - NSWidth(rect);
  508. }
  509. if (NSMinY(rect) < NSMinY(boundary)){
  510. newRect.origin.y = NSMinY(boundary);
  511. }else if (NSMaxY(rect) > NSMaxY(boundary)){
  512. newRect.origin.y = NSMaxY(boundary) - NSHeight(rect);
  513. }
  514. return newRect;
  515. }
  516. #pragma mark - Public
  517. - (CPDFAnnotation *)addAnnotationWithType:(CAnnotationType)annotationType selection:(CPDFSelection *)selection page:(CPDFPage *)page bounds:(NSRect)bounds {
  518. CPDFAnnotation *annotation = nil;
  519. NSString *text = [selection PDFListViewCleanedString];
  520. if([CPDFListView isMarkupAnnotationType:annotationType]) {
  521. if(selection){
  522. annotation = [self addPDFSelection:selection annotationTyoe:annotationType];
  523. [annotation setContents:text?:@""];
  524. } else NSBeep();
  525. } else {
  526. BOOL isInitial = NO;
  527. if(selection) {
  528. CGRect noteRect = selection.bounds;
  529. isInitial = NSEqualSizes(noteRect.size, NSZeroSize);
  530. } else {
  531. isInitial = NSEqualSizes(bounds.size, NSZeroSize);
  532. }
  533. CPDFAnnotationModel *annotationModel = [[CPDFAnnotationModel alloc]initWithAnnotationType:annotationType];
  534. CGSize size = CGSizeMake(annotationModel.noteWidth, annotationModel.noteHeight);
  535. if(annotationType == CAnnotationTypeAnchored)
  536. bounds = CGRectMake(bounds.origin.x, bounds.origin.y, size.width, size.height);
  537. if (annotationType == CAnnotationTypeCircle || annotationType == CAnnotationTypeSquare || annotationType == CAnnotationTypeLine || annotationType == CAnnotationTypeArrow || annotationType == CAnnotationTypeAnchored) {
  538. bounds = KMConstrainRect(bounds, [page boundsForBox:[self displayBox]]);
  539. }
  540. if (isInitial)
  541. bounds = annotationType == CAnnotationTypeAnchored ? CPDFListViewRectFromCenterAndSize(bounds.origin, size) : CPDFListViewRectFromCenterAndSquareSize(bounds.origin, MIN_NOTE_SIZE);
  542. switch (annotationType) {
  543. case CAnnotationTypeFreeText:
  544. annotation = [[CPDFFreeTextAnnotation alloc]initWithPDFListViewNoteWith:self.document];
  545. annotation.bounds = bounds;
  546. break;
  547. case CAnnotationTypeAnchored:
  548. annotation = [[CPDFTextAnnotation alloc]initWithPDFListViewNoteWith:self.document];
  549. annotation.bounds = bounds;
  550. break;
  551. case CAnnotationTypeCircle:
  552. annotation = [[CPDFCircleAnnotation alloc]initWithPDFListViewNoteWith:self.document];
  553. annotation.bounds = bounds;
  554. break;
  555. case CAnnotationTypeSquare:
  556. annotation = [[CPDFSquareAnnotation alloc]initWithPDFListViewNoteWith:self.document];
  557. annotation.bounds = bounds;
  558. break;
  559. case CAnnotationTypeLine:
  560. annotation = [[CPDFLineAnnotation alloc]initWithPDFListViewNoteWith:self.document annotationType:CAnnotationTypeLine];
  561. [(CPDFLineAnnotation *)annotation setObservedStartPoint:CGPointMake(bounds.origin.x, bounds.origin.y)];
  562. [(CPDFLineAnnotation *)annotation setObservedEndPoint:CGPointMake(bounds.origin.x + bounds.size.width,bounds.origin.y+ bounds.size.height)];
  563. [(CPDFLineAnnotation *)annotation setStartLineStyle:CPDFLineStyleNone];
  564. [(CPDFLineAnnotation *)annotation setEndLineStyle:CPDFLineStyleNone];
  565. break;
  566. case CAnnotationTypeArrow: {
  567. annotation = [[CPDFLineAnnotation alloc]initWithPDFListViewNoteWith:self.document annotationType:CAnnotationTypeArrow];
  568. [(CPDFLineAnnotation *)annotation setObservedStartPoint:CGPointMake(bounds.origin.x, bounds.origin.y)];
  569. [(CPDFLineAnnotation *)annotation setObservedEndPoint:CGPointMake(bounds.origin.x + bounds.size.width,bounds.origin.y+ bounds.size.height)];
  570. }
  571. break;
  572. case CAnnotationTypeLink:
  573. annotation = [[CPDFLinkAnnotation alloc] initWithPDFListViewNoteWith:self.document];
  574. annotation.bounds = bounds;
  575. break;
  576. case CAnnotationTypeTextField:
  577. annotation = [[CPDFTextWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document];
  578. annotation.bounds = bounds;
  579. break;
  580. case CAnnotationTypeCheckBox:
  581. annotation = [[CPDFButtonWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document controlType:CPDFWidgetCheckBoxControl];
  582. annotation.bounds = bounds;
  583. break;
  584. case CAnnotationTypeRadioButton:
  585. annotation = [[CPDFButtonWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document controlType:CPDFWidgetRadioButtonControl];
  586. annotation.bounds = bounds;
  587. break;
  588. case CAnnotationTypeListMenu:
  589. annotation = [[CPDFChoiceWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document listChoice:YES];
  590. annotation.bounds = bounds;
  591. break;
  592. case CAnnotationTypeComboBox:
  593. annotation = [[CPDFChoiceWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document listChoice:NO];
  594. annotation.bounds = bounds;
  595. break;
  596. case CAnnotationTypeSignature:
  597. annotation = [[CPDFSignatureWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document];
  598. annotation.bounds = bounds;
  599. break;
  600. case CAnnotationTypeActionButton:
  601. annotation = [[CPDFButtonWidgetAnnotation alloc] initWithPDFListViewNoteWith:self.document controlType:CPDFWidgetPushButtonControl];
  602. annotation.bounds = bounds;
  603. break;
  604. case CAnnotationTypeSignFalse:
  605. case CAnnotationTypeSignTure:
  606. case CAnnotationTypeSignCircle:
  607. case CAnnotationTypeSignLine:
  608. case CAnnotationTypeSignDot:
  609. annotation = [[CSelfSignAnnotation alloc]initPDFListViewNoteWithDocument:self.document type:self.annotationType];
  610. annotation.bounds = bounds;
  611. [(CSelfSignAnnotation *)annotation updateAppearanceStream];
  612. break;
  613. case CAnnotationTypeSignText:
  614. case CAnnotationTypeSignConfig:
  615. case CAnnotationTypeSignDate:
  616. annotation = [[CSelfSignAnnotationFreeText alloc]initPDFListViewNoteWithDocument:self.document subType:self.annotationType string:@"" bounds:bounds];
  617. break;
  618. case CAnnotationTypeRedact:
  619. annotation = [self addRedactPDFSelection:selection];
  620. break;
  621. default:
  622. break;
  623. }
  624. }
  625. if (annotation) {
  626. if (annotationType != CAnnotationTypeLine && annotationType != CAnnotationTypeArrow && annotationType != CAnnotationTypeInk && [text length] > 0)
  627. [annotation setString:text];
  628. [self addAnnotation:annotation toPage:page];
  629. if ([annotation isKindOfClass:[CPDFMarkupAnnotation class]]) {
  630. [self updateActiveAnnotations:@[annotation]];
  631. }
  632. }
  633. return annotation;
  634. }
  635. - (void)drawAnnotation:(CPDFAnnotation *)annotation toContext:(CGContextRef)context {
  636. [super drawAnnotation:annotation toContext:context];
  637. if ([self.pdfListViewDelegate respondsToSelector:@selector(PDFListView:needDrawAnnotation:)]) {
  638. if ([self.pdfListViewDelegate PDFListView:self needDrawAnnotation:annotation] == false) {
  639. return;
  640. }
  641. }
  642. if (CFormToolMode == _toolMode &&
  643. [annotation isKindOfClass:[CPDFWidgetAnnotation class]] && self.showFormFieldName && !annotation.isHidden) {
  644. CPDFWidgetAnnotation *annotationWidget = (CPDFWidgetAnnotation*)annotation;
  645. NSString *fieldName = annotationWidget.fieldName;
  646. if(self.showHopnumber) {
  647. NSInteger hopNumen = annotationWidget.widgetHopNumber;
  648. if(fieldName) {
  649. fieldName = [NSString stringWithFormat:@"%ld %@",hopNumen,fieldName];
  650. } else {
  651. fieldName = [NSString stringWithFormat:@"%ld",hopNumen];
  652. }
  653. }
  654. if (fieldName.length > 0) {
  655. CGContextSaveGState(context);
  656. NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
  657. paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
  658. paragraphStyle.alignment = NSTextAlignmentLeft;
  659. NSColor *backgroundColor = [NSColor blackColor];
  660. if ([self.activeAnnotations containsObject:annotation]) {
  661. backgroundColor = [NSColor colorWithRed:82.0/255.0 green:102.0/255.0 blue:204.0/255.0 alpha:1.0];
  662. }
  663. if([CPDFKitConfig sharedInstance].isShowFormRequiredFlagColor)
  664. backgroundColor = [CPDFKitConfig sharedInstance].formRequiredFlagColor?:[NSColor redColor];
  665. NSDictionary *attributes = @{NSFontAttributeName : [NSFont systemFontOfSize:12],
  666. NSForegroundColorAttributeName : [NSColor whiteColor],
  667. NSBackgroundColorAttributeName : backgroundColor,
  668. NSParagraphStyleAttributeName : paragraphStyle};
  669. NSString* drawString = fieldName;
  670. drawString = [drawString getMaxStringWithBounds:annotationWidget.bounds attributes:attributes];
  671. CGSize size = [drawString sizeWithAttributes:attributes];
  672. CGRect drawRect = annotationWidget.bounds;
  673. drawRect.origin.y += (drawRect.size.height - size.height)/2.0;
  674. drawRect.origin.x += (drawRect.size.width - size.width)/2.0;
  675. drawRect.size.height = size.height;
  676. drawRect.size.width = size.width;
  677. [NSGraphicsContext saveGraphicsState];
  678. [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]];
  679. [drawString drawInRect:drawRect withAttributes:attributes];
  680. [NSGraphicsContext restoreGraphicsState];
  681. CGContextRestoreGState(context);
  682. NSRect rect = [self integralRect:[annotationWidget bounds] onPage:[annotationWidget page]];
  683. CGFloat lineWidth = [self unitWidthOnPage:[annotationWidget page]];
  684. CGContextSaveGState(context);
  685. CGColorRef color = [NSColor blackColor].CGColor;
  686. CGContextSetStrokeColorWithColor(context, color);
  687. CGContextStrokeRectWithWidth(context, CGRectInset(NSRectToCGRect(rect), 0,0), lineWidth);
  688. }
  689. } else if(CNoteToolMode== _toolMode && _annotationType == CAnnotationTypeLink && [annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
  690. CGContextSaveGState(context);
  691. NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
  692. paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
  693. paragraphStyle.alignment = NSTextAlignmentLeft;
  694. NSColor *backgroundColor = [NSColor clearColor];
  695. NSDictionary *attributes = @{NSFontAttributeName : [NSFont systemFontOfSize:12],
  696. NSForegroundColorAttributeName : [NSColor whiteColor],
  697. NSBackgroundColorAttributeName : backgroundColor,
  698. NSParagraphStyleAttributeName : paragraphStyle};
  699. NSString *drawString = NSLocalizedString(@"No Destination", nil);
  700. CPDFDestination *destination = [(CPDFLinkAnnotation *)annotation destination];
  701. if(destination) {
  702. CPDFPage *page = destination.page;
  703. NSString * url = [(CPDFLinkAnnotation *)annotation URL];
  704. if(destination.page) {
  705. NSUInteger index = [self.document indexForPage:page] + 1;
  706. drawString = [NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"to Page",nil),@(index)];
  707. } else if(url.length > 0) {
  708. drawString = url;
  709. }
  710. }else{
  711. NSString * url = [(CPDFLinkAnnotation *)annotation URL];
  712. if(url.length > 0) {
  713. drawString = url;
  714. }
  715. }
  716. drawString = [drawString getMaxStringWithBounds:annotation.bounds attributes:attributes];
  717. drawString = @"";
  718. CGSize size = [drawString sizeWithAttributes:attributes];
  719. CGRect drawRect = annotation.bounds;
  720. drawRect.origin.y += (drawRect.size.height - size.height)/2.0;
  721. drawRect.origin.x += (drawRect.size.width - size.width)/2.0;
  722. drawRect.size.height = size.height;
  723. drawRect.size.width = size.width;
  724. CGContextSaveGState(context);
  725. CGContextSetFillColorWithColor(context, [NSColor colorWithRed:23.0/255.0 green:112.0/255.0 blue:244.0/255.0 alpha:0.5].CGColor);
  726. CGContextFillRect(context,annotation.bounds);
  727. CGContextRestoreGState(context);
  728. [NSGraphicsContext saveGraphicsState];
  729. CGRect frameRect=CGRectMake(annotation.bounds.origin.x, annotation.bounds.origin.y + (annotation.bounds.size.height - 20)/2, annotation.bounds.size.width, 20);
  730. float fontSize = 9;
  731. NSColor *textColor = [NSColor whiteColor];
  732. CTTextAlignment alignMent = kCTTextAlignmentCenter;
  733. CFStringRef stringRef = (__bridge CFStringRef)drawString;
  734. NSMutableAttributedString* attString = [[NSMutableAttributedString alloc] initWithString:drawString];
  735. NSInteger _stringLength=[drawString length];
  736. CTTextAlignment theAlignment = alignMent;
  737. CTParagraphStyleSetting theSettings[1] =
  738. {
  739. { kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment),&theAlignment
  740. }
  741. };
  742. CTParagraphStyleRef paragraphStyleRef = CTParagraphStyleCreate(theSettings, 1);
  743. [attString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)paragraphStyleRef range:NSMakeRange(0, _stringLength)];
  744. [attString addAttribute:(id)kCTForegroundColorAttributeName value:(id)textColor.CGColor range:NSMakeRange(0, _stringLength)];
  745. [attString addAttribute:(id)kCTFontNameAttribute value:@"Courier" range:NSMakeRange(0, _stringLength)];
  746. CFAttributedStringRef attrString =(__bridge CFAttributedStringRef) attString;
  747. CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
  748. CGMutablePathRef framePath = CGPathCreateMutable();
  749. CGPathAddRect(framePath, NULL, frameRect);
  750. CFRange currentRange = CFRangeMake(0, 0);
  751. CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
  752. CGPathRelease(framePath);
  753. CTFrameDraw(frameRef, context);
  754. CFRelease(frameRef);
  755. CFRelease(framesetter);
  756. [NSGraphicsContext restoreGraphicsState];
  757. CGContextRestoreGState(context);
  758. }
  759. }
  760. - (void)setAddStampObject:(CStampSignatureObject *)stampObject keepToolModel:(BOOL)isToolModel {
  761. if (CAnnotationTypeStamp == self.annotationType ||
  762. CAnnotationTypeSignSignature == self.annotationType) {
  763. self.stampObject = stampObject;
  764. self.isStampModel = isToolModel;
  765. [NSCursor clearStampCursor];
  766. }
  767. }
  768. - (void)addAnnotationWithImage:(NSImage *)image page:(CPDFPage *)page point:(NSPoint)point {
  769. NSRect bounds = NSZeroRect;
  770. CPDFSelection *selection = [self currentSelection];
  771. CGFloat defaultWidth = 360;
  772. CGFloat defaultHeight = 90;
  773. NSSize defaultSize = ([page rotation] % 180 == 0) ? NSMakeSize(defaultWidth, defaultHeight) : NSMakeSize(defaultHeight, defaultWidth);
  774. bounds = CPDFListViewRectFromCenterAndSize(point, defaultSize);
  775. // Make sure it fits in the page
  776. bounds = CPDFListViewConstrainRect(bounds, [page boundsForBox:[self displayBox]],[CPDFListViewConfig defaultManager].annotationBorderOffset.floatValue);
  777. // bounds = CPDFListViewConstrainRect(bounds, [page boundsForBox:[self displayBox]]);
  778. if (page != nil) {
  779. BOOL isInitial = NSEqualSizes(bounds.size, NSZeroSize) && selection == nil;
  780. // new note added by note tool mode, don't add actual zero sized notes
  781. if (isInitial)
  782. bounds = CPDFListViewRectFromCenterAndSquareSize(bounds.origin, 8.0);
  783. CGFloat borderDefaultWidth = image.size.width;
  784. CGFloat borderScale = borderDefaultWidth/image.size.height;
  785. if (bounds.size.width/bounds.size.height > borderScale) {
  786. bounds.size.height = bounds.size.height;
  787. bounds.size.width = bounds.size.height*borderScale;
  788. } else {
  789. bounds.size.width = bounds.size.width;
  790. bounds.size.height = bounds.size.width/borderScale;
  791. }
  792. CPDFListStampAnnotation *newAnnotation = [[CPDFListStampAnnotation alloc] initWithDocument:self.document image:image];
  793. newAnnotation.bounds = bounds;
  794. [newAnnotation setBorderBoundsWithImage:image];
  795. [self addAnnotation:newAnnotation toPage:page];
  796. [[self undoManager] setActionName:NSLocalizedString(@"Add Note", @"Undo action name")];
  797. [self updateActiveAnnotations:@[newAnnotation]];
  798. [self setNeedsDisplayAnnotation:newAnnotation];
  799. }
  800. else NSBeep();
  801. }
  802. - (void)updateIsRightActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations {
  803. [self refreshActiveAnnotations:activeAnnotations isRight:YES];
  804. }
  805. - (void)updateActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations {
  806. [self refreshActiveAnnotations:activeAnnotations isRight:NO];
  807. }
  808. -(void)refreshActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations isRight:(BOOL)isRight {
  809. // for (CPDFAnnotation *annotation in self.activeAnnotations) {
  810. // if([annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
  811. // CPDFLinkAnnotation *linkAnnotation = (CPDFLinkAnnotation *)annotation;
  812. // if((linkAnnotation.destination || linkAnnotation.URL.length > 0)) {
  813. // } else {
  814. // [self removeAnnotation:linkAnnotation];
  815. // }
  816. // }
  817. // }
  818. if(activeAnnotations) {
  819. NSArray *selectTure = [activeAnnotations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF in %@", self.activeAnnotations]];
  820. BOOL isSame = NO;
  821. if(self.activeAnnotations.count == activeAnnotations.count && self.activeAnnotations.count == selectTure.count) isSame = YES;
  822. if(!isSame) {
  823. self.activeAnnotations = [NSMutableArray arrayWithArray:activeAnnotations];
  824. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
  825. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:isRight];
  826. dispatch_async(dispatch_get_main_queue(), ^{
  827. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  828. });
  829. }
  830. } else if (!activeAnnotations && self.activeAnnotations.count > 0) {
  831. [self.activeAnnotations removeAllObjects];
  832. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
  833. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:isRight];
  834. dispatch_async(dispatch_get_main_queue(), ^{
  835. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  836. });
  837. }
  838. }
  839. - (void)addActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations {
  840. BOOL isContains = YES;
  841. for (CPDFAnnotation *annotation in activeAnnotations) {
  842. if(![self.activeAnnotations containsObject:annotation]) {
  843. [self.activeAnnotations addObject:annotation];
  844. isContains = NO;
  845. }
  846. }
  847. if(!isContains) {
  848. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
  849. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:NO];
  850. dispatch_async(dispatch_get_main_queue(), ^{
  851. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  852. });
  853. }
  854. }
  855. - (void)removeActiveAnnotations:(NSArray<CPDFAnnotation *> *)removeAnnotations {
  856. BOOL isContains = NO;
  857. NSMutableArray *tarr = [NSMutableArray arrayWithArray:self.activeAnnotations];
  858. [tarr removeObjectsInArray:removeAnnotations];
  859. self.activeAnnotations = tarr;
  860. isContains = YES;
  861. if(isContains) {
  862. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
  863. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:NO];
  864. dispatch_async(dispatch_get_main_queue(), ^{
  865. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  866. });
  867. }
  868. }
  869. - (void)editAnnotation:(CPDFAnnotation *)annotation {
  870. if ([annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  871. [self editAnnotationFreeText:(CPDFFreeTextAnnotation *)annotation];
  872. [[self window] makeFirstResponder:self];
  873. [self setNeedsDisplayAnnotation:annotation];
  874. } else if ([annotation isKindOfClass:[CPDFButtonWidgetAnnotation class]] || [annotation isKindOfClass:[CPDFChoiceWidgetAnnotation class]]) {
  875. [self editFormAnnotation:self.activeAnnotation];
  876. } else {
  877. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewEditAnnotation:forAnnotation:)]) {
  878. [self.pdfListViewDelegate PDFListViewEditAnnotation:self forAnnotation:annotation];
  879. } else {
  880. [CPDFListView cancelPreviousPerformRequestsWithTarget:self selector:@selector(showHUDHint:) object:self.hoverAnnotation];
  881. if (!self.popOver) {
  882. _popOver = [[NSPopover alloc] init];
  883. }
  884. if([annotation isKindOfClass:[CPDFTextAnnotation class]]) {
  885. __block typeof(self) blockSelf = self;
  886. _popOver.delegate = self;
  887. CPDFListEditAnnotationViewController *vc = [[CPDFListEditAnnotationViewController alloc] initWithNibName:@"CPDFListEditAnnotationViewController" bundle:[NSBundle mainBundle] annotation:annotation];
  888. vc.pdflistView = self;
  889. _popOver.contentViewController = vc;
  890. vc.closeCallBack = ^{
  891. [blockSelf.popOver close];
  892. };
  893. NSBox *box = (NSBox *)vc.view;
  894. NSView *popoverView = [[[self.popOver contentViewController] view] superview];
  895. [popoverView setWantsLayer:YES];
  896. vc.changeColorCallBack = ^{
  897. NSColor *borColor = annotation.color?:[NSColor whiteColor];
  898. [[popoverView layer] setBackgroundColor:borColor.CGColor];
  899. [blockSelf setNeedsDisplayAnnotationViewForPage:annotation.page];
  900. };
  901. NSColor *borColor = annotation.color?:[NSColor whiteColor];
  902. [[popoverView layer] setBackgroundColor:borColor.CGColor];
  903. _popOver.animates = YES;
  904. _popOver.behavior = NSPopoverBehaviorTransient;
  905. [_popOver showRelativeToRect:[self convertRect:annotation.bounds fromPage:annotation.page] ofView:self preferredEdge:NSMaxXEdge];
  906. } else if(![annotation isKindOfClass:[CPDFLinkAnnotation class]]){
  907. CPDFListAnnotationNoteWindowController *wc = [CPDFListAnnotationNoteWindowController sharedInstance];
  908. CGRect rect = wc.window.frame;
  909. CGRect anBounds = [self convertRect:annotation.bounds fromPage:annotation.page];
  910. anBounds = [self convertRectToScreen:anBounds];
  911. rect.origin.x = anBounds.origin.x;
  912. rect.origin.y = anBounds.origin.y;
  913. if (@available(macOS 10.13, *))
  914. [wc.window makeKeyAndOrderFront:nil];
  915. else
  916. [wc showWindow:nil];
  917. [wc updateAnnotation:annotation];
  918. [wc.window setFrame:rect display:YES animate:NO];
  919. wc.window.title = @"";
  920. }
  921. }
  922. }
  923. }
  924. - (void)rotateStampAnnotation:(CPDFListStampAnnotation *)annotation rotateAngle:(NSInteger)angle {
  925. if([annotation isKindOfClass:[CPDFListStampAnnotation class]]) {
  926. CPDFListStampAnnotation *stampAnnotation = (CPDFListStampAnnotation *)annotation;
  927. NSInteger rotation = stampAnnotation.rotation;
  928. rotation = rotation - angle;
  929. if (rotation < 0) {
  930. rotation += 360;
  931. } else if (rotation >= 360) {
  932. rotation-=360;
  933. }
  934. [annotation setRotation:rotation];
  935. rotation = [annotation appearanceStreamRotation];
  936. [self setNeedsDisplayAnnotationViewForPage:annotation.page];
  937. [[[self undoManager] prepareWithInvocationTarget:self] rotateStampAnnotation:annotation rotateAngle:-angle];
  938. }
  939. }
  940. - (void)rotateSignatureAnnotation:(CPDFListSignatureAnnotation *)annotation rotateAngle:(NSInteger)angle {
  941. if([annotation isKindOfClass:[CPDFListSignatureAnnotation class]]) {
  942. CPDFListSignatureAnnotation *signatureAnnotation = (CPDFListSignatureAnnotation *)annotation;
  943. NSInteger rotation = signatureAnnotation.rotation;
  944. rotation = rotation - angle;
  945. if (rotation < 0) {
  946. rotation += 360;
  947. } else if (rotation >= 360) {
  948. rotation-=360;
  949. }
  950. [signatureAnnotation setRotation:rotation];
  951. rotation = [signatureAnnotation appearanceStreamRotation];
  952. [self setNeedsDisplayAnnotationViewForPage:annotation.page];
  953. [[[self undoManager] prepareWithInvocationTarget:self] rotateSignatureAnnotation:annotation rotateAngle:-angle];
  954. }
  955. }
  956. - (void)addAnnotationWithAnnotation:(CPDFAnnotation *)annotation toPage:(CPDFPage *)page {
  957. [self addAnnotation:annotation toPage:page];
  958. }
  959. - (void)addAnnotation:(CPDFAnnotation *)annotation toPage:(CPDFPage *)page {
  960. [[[self undoManager] prepareWithInvocationTarget:self] removeAnnotation:annotation];
  961. [annotation setModificationDate:[NSDate date]];
  962. //widget设置作者与fieldName冲突
  963. if (![annotation isKindOfClass:[CPDFWidgetAnnotation class]]) {
  964. [annotation setUserName:CPDFKitShareConfig.annotationAuthor?:NSFullUserName()];
  965. }
  966. [page addAnnotation:annotation];
  967. [self setNeedsDisplayAnnotation:annotation];
  968. [[self undoManager] setActionName:NSLocalizedString(@"Add Note", @"Undo action name")];
  969. dispatch_async(dispatch_get_main_queue(), ^{
  970. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewDidAddAnnotationNotification object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:page, CPDFListViewPageKey, annotation, CPDFListViewAnnotationKey, nil]];
  971. });
  972. [self resetPDFToolTipRects];
  973. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewAddAnnotations:forAddAnnotations:inPage:)])
  974. [self.pdfListViewDelegate PDFListViewAddAnnotations:self forAddAnnotations:@[annotation] inPage:page];
  975. }
  976. - (void)removeAnnotation:(CPDFAnnotation *)annotation {
  977. if ([annotation isKindOfClass:[CPDFTextAnnotation class]] && self.popOver.isShown) {
  978. // if (annotation.contents.length <= 0) {
  979. CPDFAnnotation *wasAnnotation = annotation;
  980. CPDFPage *page = wasAnnotation.page;
  981. [[[self undoManager] prepareWithInvocationTarget:self] addAnnotation:wasAnnotation toPage:page];
  982. [page removeAnnotation:wasAnnotation];
  983. [self annotationsChangedOnPage:page];
  984. [self setNeedsDisplayAnnotation:wasAnnotation];
  985. dispatch_async(dispatch_get_main_queue(), ^{
  986. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewDidRemoveAnnotationNotification object:self
  987. userInfo:[NSDictionary dictionaryWithObjectsAndKeys:wasAnnotation, CPDFListViewAnnotationKey, page, CPDFListViewPageKey, nil]];
  988. });
  989. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewRemoveAnnotations:forRemoveAnnotations:inPage:)])
  990. [self.pdfListViewDelegate PDFListViewRemoveAnnotations:self forRemoveAnnotations:@[annotation] inPage:page];
  991. // }
  992. } else {
  993. CPDFAnnotation *wasAnnotation = annotation;
  994. CPDFPage *page = wasAnnotation.page;
  995. [[[self undoManager] prepareWithInvocationTarget:self] addAnnotation:wasAnnotation toPage:page];
  996. if ([wasAnnotation isNote]) {
  997. [self resetPDFToolTipRects];
  998. }
  999. [page removeAnnotation:wasAnnotation];
  1000. [self annotationsChangedOnPage:page];
  1001. [self setNeedsDisplayAnnotation:wasAnnotation];
  1002. dispatch_async(dispatch_get_main_queue(), ^{
  1003. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewDidRemoveAnnotationNotification object:self
  1004. userInfo:[NSDictionary dictionaryWithObjectsAndKeys:wasAnnotation, CPDFListViewAnnotationKey, page, CPDFListViewPageKey, nil]];
  1005. });
  1006. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewRemoveAnnotations:forRemoveAnnotations:inPage:)])
  1007. [self.pdfListViewDelegate PDFListViewRemoveAnnotations:self forRemoveAnnotations:@[annotation] inPage:page];
  1008. }
  1009. }
  1010. - (void)drawPage:(CPDFPage *)pdfPage toContext:(CGContextRef)context {
  1011. [super drawPage:pdfPage toContext:context];
  1012. [self drawSelectionForPage:pdfPage inContext:context];
  1013. BOOL isIncludText = NO;
  1014. NSMutableArray *currentActiveAnnotations = [NSMutableArray array];
  1015. NSMutableArray *currentPageAnnotations = [NSMutableArray array];
  1016. for (CPDFAnnotation *an in self.activeAnnotations) {
  1017. if([an.page isEqual:pdfPage]) {
  1018. if([an isKindOfClass:[CPDFFreeTextAnnotation class]])
  1019. isIncludText = YES;
  1020. //多选注释框去除mark注释与便签注释
  1021. if(!([an isKindOfClass:[CPDFMarkupAnnotation class]] || [an isKindOfClass:[CPDFTextAnnotation class]])) {
  1022. [currentActiveAnnotations addObject:an];
  1023. }
  1024. [currentPageAnnotations addObject:an];
  1025. }
  1026. }
  1027. if(currentActiveAnnotations.count > 1) {
  1028. NSRect rect = [self selectionMultipleBoundsWithAnnotations:currentActiveAnnotations];
  1029. CGFloat lineWidth = [self unitWidthOnPage:pdfPage];
  1030. CGContextSaveGState(context);
  1031. CGColorRef color = [CPDFListViewConfig defaultManager].annotationBorderColor.CGColor;
  1032. CGContextSetStrokeColorWithColor(context, color);
  1033. CGContextStrokeRectWithWidth(context, CGRectInset(NSRectToCGRect(rect), 0,0), lineWidth);
  1034. CGContextRestoreGState(context);
  1035. CGContextSaveGState(context);
  1036. if(isIncludText) {
  1037. [CPDFListView DrawFreeTextResizeHandle:context rect:rect radius: 4.0 * lineWidth active:true];
  1038. } else {
  1039. [CPDFListView DrawResizeHandles:context rect:rect radius:4.0 * lineWidth active:true];
  1040. }
  1041. CGContextRestoreGState(context);
  1042. }
  1043. if(self.multiplAnnotationObject &&
  1044. !CGRectIsEmpty(self.multiplAnnotationObject.drawRect) &&
  1045. self.multiplAnnotationObject.page == pdfPage) {
  1046. CGRect selectAnnotationBounds = self.multiplAnnotationObject.drawRect;
  1047. NSRect rect = selectAnnotationBounds;
  1048. CGFloat lineWidth = [self unitWidthOnPage:pdfPage];
  1049. CGContextSaveGState(context);
  1050. CGColorRef color = [CPDFListViewConfig defaultManager].annotationBorderColor.CGColor;
  1051. CGContextSetStrokeColorWithColor(context, color);
  1052. CGContextStrokeRectWithWidth(context, CGRectInset(NSRectToCGRect(rect), 0,0), lineWidth);
  1053. CGContextRestoreGState(context);
  1054. }
  1055. [currentPageAnnotations enumerateObjectsUsingBlock:^(CPDFAnnotation *annotation, NSUInteger idx, BOOL * _Nonnull stop) {
  1056. if (annotation.page && [annotation.page isEqual:pdfPage]) {
  1057. [annotation drawSelectionHighlightForView:self inContext:context isHover:NO];
  1058. }
  1059. }];
  1060. [self.selectAnnotations enumerateObjectsUsingBlock:^(CPDFAnnotation *annotation, NSUInteger idx, BOOL * _Nonnull stop) {
  1061. if (annotation.page && [annotation.page isEqual:pdfPage]) {
  1062. [annotation drawSelectionHighlightForView:self inContext:context isHover:NO];
  1063. }
  1064. }];
  1065. if (self.hoverAnnotation.page && [self.hoverAnnotation.page isEqual:pdfPage] && ![self.activeAnnotations containsObject:self.hoverAnnotation]) {
  1066. [self.hoverAnnotation drawSelectionHighlightForView:self inContext:context isHover:YES];
  1067. }
  1068. if(!CGRectEqualToRect(self.multiplSelectBounds, CGRectZero) && [self.multiplSelectPage isEqual:pdfPage]) {
  1069. @synchronized (self) {
  1070. CGContextSetLineCap(context, kCGLineCapRound);
  1071. CGContextSetLineWidth(context, 1.0);
  1072. CGContextSetStrokeColorWithColor(context, [CPDFListViewConfig defaultManager].annotationBorderColor.CGColor);
  1073. CGContextBeginPath(context);
  1074. CGContextMoveToPoint(context, self.multiplSelectBounds.origin.x, self.multiplSelectBounds.origin.y);
  1075. CGContextAddLineToPoint(context, self.multiplSelectBounds.origin.x+ self.multiplSelectBounds.size.width,self.multiplSelectBounds.origin.y);
  1076. CGContextAddLineToPoint(context, self.multiplSelectBounds.origin.x + self.multiplSelectBounds.size.width,self.multiplSelectBounds.origin.y + + self.multiplSelectBounds.size.height);
  1077. CGContextAddLineToPoint(context, self.multiplSelectBounds.origin.x,self.multiplSelectBounds.origin.y + + self.multiplSelectBounds.size.height);
  1078. CGContextClosePath(context);
  1079. CGFloat lengths[] = {5,5};
  1080. CGContextSetLineDash(context, 0, lengths,2);
  1081. CGContextStrokePath(context);
  1082. }
  1083. }
  1084. }
  1085. - (void)drawSelectionForPage:(CPDFPage *)pdfPage inContext:(CGContextRef)context {
  1086. NSRect rect;
  1087. NSUInteger pageIndex;
  1088. @synchronized (self) {
  1089. pageIndex = self.selectionPageIndex;
  1090. rect = self.selectionRect;
  1091. }
  1092. if (pageIndex != NSNotFound) {
  1093. BOOL isWidget = NO;
  1094. for (CPDFAnnotation * annotation in self.activeAnnotations) {
  1095. if ([annotation isForm]) {
  1096. isWidget = YES;
  1097. break;
  1098. }
  1099. }
  1100. if (isWidget &&
  1101. _toolMode != CFormToolMode) {
  1102. return;
  1103. }
  1104. NSRect bounds = [pdfPage boundsForBox:[self displayBox]];
  1105. CGFloat radius = HANDLE_SIZE * [self unitWidthOnPage:pdfPage];
  1106. CGColorRef color = CGColorCreateGenericGray(0.0, 0.6);
  1107. CGContextSetFillColorWithColor(context, color);
  1108. CGColorRelease(color);
  1109. CGContextBeginPath(context);
  1110. CGContextAddRect(context, NSRectToCGRect(bounds));
  1111. CGContextAddRect(context, NSRectToCGRect(rect));
  1112. CGContextEOFillPath(context);
  1113. if ([pdfPage pageIndex] != pageIndex) {
  1114. color = CGColorCreateGenericGray(0.0, 0.3);
  1115. CGContextSetFillColorWithColor(context, color);
  1116. CGColorRelease(color);
  1117. CGContextFillRect(context, NSRectToCGRect(rect));
  1118. }
  1119. [CPDFListView DrawResizeHandles:context rect:rect radius:radius active:true];
  1120. }
  1121. }
  1122. - (void)removeShapeLayer {
  1123. if (![self.shapeLayerTopH isEqual:nil] && ![self.shapeLayerBottomH isEqual:nil] && ![self.shapeLayerLeftV isEqual:nil] && ![self.shapeLayerRightV isEqual:nil]) {
  1124. [self.shapeLayerTopH removeFromSuperlayer];
  1125. [self.shapeLayerBottomH removeFromSuperlayer];
  1126. [self.shapeLayerLeftV removeFromSuperlayer];
  1127. [self.shapeLayerRightV removeFromSuperlayer];
  1128. self.shapeLayerTopH = nil;
  1129. self.shapeLayerBottomH = nil;
  1130. self.shapeLayerLeftV = nil;
  1131. self.shapeLayerRightV = nil;
  1132. }
  1133. }
  1134. - (BOOL)consistentTypeWithAnnotation:(CPDFAnnotation *)annotation {
  1135. if (self.annotationType == CAnnotationTypeSignSignature) {
  1136. if (![annotation isKindOfClass:[CPDFSignatureAnnotation class]])
  1137. return NO;
  1138. } else if (self.annotationType == CAnnotationTypeStamp) {
  1139. if (![annotation isKindOfClass:[CPDFStampAnnotation class]])
  1140. return NO;
  1141. } else if (self.annotationType == CAnnotationTypeAnchored) {
  1142. if (![annotation isKindOfClass:[CPDFTextAnnotation class]])
  1143. return NO;
  1144. } else if (self.annotationType == CAnnotationTypeLink) {
  1145. if (![annotation isKindOfClass:[CPDFLinkAnnotation class]])
  1146. return NO;
  1147. } else if (self.annotationType == CAnnotationTypeFreeText) {
  1148. if (![annotation isKindOfClass:[CPDFFreeTextAnnotation class]])
  1149. return NO;
  1150. } else if (self.annotationType == CAnnotationTypeLine || self.annotationType == CAnnotationTypeArrow || self.annotationType == CAnnotationTypeSquare || self.annotationType == CAnnotationTypeCircle) {
  1151. if (![annotation isKindOfClass:[CPDFLineAnnotation class]] && ![annotation isKindOfClass:[CPDFSquareAnnotation class]] && ![annotation isKindOfClass:[CPDFCircleAnnotation class]])
  1152. return NO;
  1153. } else if (self.annotationType == CAnnotationTypeHighlight || self.annotationType == CAnnotationTypeUnderline || self.annotationType == CAnnotationTypeStrikeOut) {
  1154. if (![annotation isKindOfClass:[CPDFMarkupAnnotation class]])
  1155. return NO;
  1156. } else if (self.annotationType == CAnnotationTypeInk) {
  1157. if (![annotation isKindOfClass:[CPDFInkAnnotation class]])
  1158. return NO;
  1159. } else if (self.annotationType == CAnnotationTypeRadioButton) {
  1160. } else if (self.annotationType == CAnnotationTypeCheckBox) {
  1161. } else if (self.annotationType == CAnnotationTypeTextField) {
  1162. } else if (self.annotationType == CAnnotationTypeComboBox) {
  1163. } else if (self.annotationType == CAnnotationTypeListMenu) {
  1164. } else if (self.annotationType == CAnnotationTypeActionButton) {
  1165. } else if (self.annotationType == CAnnotationTypeSignature) {
  1166. } else if (self.annotationType == CAnnotationTypeSignText) {
  1167. } else if (self.annotationType == CAnnotationTypeSignFalse) {
  1168. } else if (self.annotationType == CAnnotationTypeSignTure) {
  1169. } else if (self.annotationType == CAnnotationTypeSignCircle) {
  1170. } else if (self.annotationType == CAnnotationTypeSignLine) {
  1171. } else if (self.annotationType == CAnnotationTypeSignDot) {
  1172. } else if (self.annotationType == CAnnotationTypeSignConfig) {
  1173. } else if (self.annotationType == CAnnotationTypeSignDate) {
  1174. }
  1175. return YES;
  1176. }
  1177. #pragma mark Zooming
  1178. - (void)zoomToRect:(NSRect)rect onPage:(CPDFPage *)page {
  1179. if (NSIsEmptyRect(rect) == NO) {
  1180. BOOL isLegacy = [NSScroller respondsToSelector:@selector(preferredScrollerStyle)] == NO || [NSScroller preferredScrollerStyle] == NSScrollerStyleLegacy;
  1181. NSRect bounds = [self bounds];
  1182. CGFloat scale = 1.0;
  1183. if (isLegacy) {
  1184. bounds.size.width -= [NSScroller scrollerWidth];
  1185. bounds.size.height -= [NSScroller scrollerWidth];
  1186. }
  1187. if (NSWidth(bounds) * NSHeight(rect) > NSWidth(rect) * NSHeight(bounds))
  1188. scale = NSHeight(bounds) / NSHeight(rect);
  1189. else
  1190. scale = NSWidth(bounds) / NSWidth(rect);
  1191. [self setScaleFactor:scale];
  1192. NSScrollView *scrollView = [self scrollView];
  1193. if (isLegacy && ([scrollView hasHorizontalScroller] == NO || [scrollView hasVerticalScroller] == NO)) {
  1194. if ([scrollView hasVerticalScroller])
  1195. bounds.size.width -= [NSScroller scrollerWidth];
  1196. if ([scrollView hasHorizontalScroller])
  1197. bounds.size.height -= [NSScroller scrollerWidth];
  1198. if (NSWidth(bounds) * NSHeight(rect) > NSWidth(rect) * NSHeight(bounds))
  1199. scale = NSHeight(bounds) / NSHeight(rect);
  1200. else
  1201. scale = NSWidth(bounds) / NSWidth(rect);
  1202. [self setScaleFactor:scale];
  1203. }
  1204. [self goToRect:rect onPage:page];
  1205. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1206. CGPoint tPagePoint = CGPointMake(rect.origin.x, rect.origin.y+rect.size.height);
  1207. [self goToTargetPoint:tPagePoint onPage:page atScrollPosition:CScrollPositionTop];
  1208. });
  1209. }
  1210. }
  1211. #pragma mark - NSPopoverDelegate
  1212. - (void)popoverWillClose:(NSNotification *)notification {
  1213. NSPopover *popover = notification.object;
  1214. if([popover.contentViewController isKindOfClass:[CPDFListEditAnnotationViewController class]]) {
  1215. CPDFListEditAnnotationViewController *listEditAnnotationViewController = (CPDFListEditAnnotationViewController *)popover.contentViewController;
  1216. CPDFAnnotation *annotation = listEditAnnotationViewController.annotation;
  1217. NSString *contensString = listEditAnnotationViewController.contentString;
  1218. if([annotation isKindOfClass:[CPDFTextAnnotation class]]) {
  1219. if(contensString && contensString.length > 0) {
  1220. annotation.contents = listEditAnnotationViewController.contentString;
  1221. [self setNeedsDisplayAnnotation:annotation];
  1222. } else {
  1223. if([self.activeAnnotations containsObject:annotation]) {
  1224. [self.activeAnnotations removeObject:annotation];
  1225. }
  1226. [self removeAnnotation:annotation];
  1227. }
  1228. } else {
  1229. annotation.contents = listEditAnnotationViewController.contentString;
  1230. [self setNeedsDisplayAnnotation:annotation];
  1231. }
  1232. }
  1233. self.popOver = nil;
  1234. }
  1235. - (void)handlePageChangedNotification:(NSNotification *)notification {
  1236. if ([self displayMode] == kPDFDisplaySinglePage || [self displayMode] == kPDFDisplayTwoUp) {
  1237. [self resetPDFToolTipRects];
  1238. }
  1239. self.copyCount = 0;
  1240. }
  1241. #pragma mark - Form Alignment
  1242. - (KMPDFViewActiveFormsType)formAnnotionsType:(NSArray *)annotationArray {
  1243. NSSet *set = [NSSet setWithArray:[annotationArray valueForKey:@"type"]];
  1244. NSArray *array = [set allObjects];
  1245. if (array.count > 1 || ![array.firstObject isEqualToString:@"Widget"]) {
  1246. return KMPDFViewActiveFormsType_NotAllform;
  1247. }
  1248. PDFAnnotation *referenceAnnotation = annotationArray.firstObject;
  1249. Class referenceAnnotationClass = [referenceAnnotation class];
  1250. if ([referenceAnnotationClass isEqual:[CPDFButtonWidgetAnnotation class]]) {
  1251. referenceAnnotationClass = [CPDFButtonWidgetAnnotation superclass];
  1252. }
  1253. if ([referenceAnnotationClass isEqual:[CPDFTextWidgetAnnotation class]]) {
  1254. referenceAnnotationClass = [CPDFTextWidgetAnnotation superclass];
  1255. }
  1256. if ([referenceAnnotationClass isEqual:[CPDFChoiceWidgetAnnotation class]]) {
  1257. referenceAnnotationClass = [CPDFChoiceWidgetAnnotation superclass];
  1258. }
  1259. if (annotationArray.count == 1) {
  1260. if ([referenceAnnotation isKindOfClass:[CPDFTextWidgetAnnotation class]]) {
  1261. return KMPDFViewActiveFormsType_TextSingle;
  1262. } else {
  1263. return KMPDFViewActiveFormsType_PureSingle;
  1264. }
  1265. }
  1266. for (NSInteger i = 0; i < annotationArray.count; i++) {
  1267. PDFAnnotation *annotation = [annotationArray objectAtIndex:i];
  1268. if (![annotation isKindOfClass:[CPDFButtonWidgetAnnotation class]] && ![annotation isKindOfClass:[CPDFTextWidgetAnnotation class]] && ![annotation isKindOfClass:[CPDFChoiceWidgetAnnotation class]]) {
  1269. return KMPDFViewActiveFormsType_Mix;
  1270. }
  1271. }
  1272. if ([referenceAnnotation isKindOfClass:[CPDFTextWidgetAnnotation class]]){
  1273. return KMPDFViewActiveFormsType_Text;
  1274. }
  1275. return KMPDFViewActiveFormsType_Pure;
  1276. }
  1277. - (void)formsAnnotationAlign:(KMPDFActiveFormsAlignType)type {
  1278. CGRect zeroRect = CGRectNull;
  1279. CGRect highestRect = CGRectZero;
  1280. CGRect widthestRect = CGRectZero;
  1281. CPDFAnnotation *firstAnnotation = self.activeAnnotations.firstObject;
  1282. CGRect leftestRect = firstAnnotation.bounds;
  1283. CGRect rightestRect = firstAnnotation.bounds;
  1284. CGRect topestRect = firstAnnotation.bounds;
  1285. CGRect bottomestRect = firstAnnotation.bounds;
  1286. CPDFAnnotation *leftestAnnotation = firstAnnotation;
  1287. CPDFAnnotation *rightestAnnotation = firstAnnotation;
  1288. CPDFAnnotation *topestAnnotation = firstAnnotation;
  1289. CPDFAnnotation *bottomestAnnotation = firstAnnotation;
  1290. CGFloat totalWidth = 0.;
  1291. CGFloat totalHeight = 0.;
  1292. for (CPDFAnnotation *annotation in self.activeAnnotations) {
  1293. zeroRect = CGRectUnion(zeroRect, annotation.bounds);
  1294. totalWidth += CGRectGetWidth(annotation.bounds);
  1295. totalHeight += CGRectGetHeight(annotation.bounds);
  1296. if (CGRectGetHeight(annotation.bounds) > CGRectGetHeight(highestRect)) {
  1297. highestRect = annotation.bounds;
  1298. }
  1299. if (CGRectGetWidth(annotation.bounds) > CGRectGetWidth(widthestRect)) {
  1300. widthestRect = annotation.bounds;
  1301. }
  1302. if (CGRectGetMinX(leftestRect) > CGRectGetMinX(annotation.bounds)) {
  1303. leftestRect = annotation.bounds;
  1304. leftestAnnotation = annotation;
  1305. }
  1306. if (CGRectGetMaxX(annotation.bounds) > CGRectGetMaxX(rightestRect)) {
  1307. rightestRect = annotation.bounds;
  1308. rightestAnnotation = annotation;
  1309. }
  1310. if (CGRectGetMaxY(annotation.bounds) > CGRectGetMaxY(topestRect)) {
  1311. topestRect = annotation.bounds;
  1312. topestAnnotation = annotation;
  1313. }
  1314. if (CGRectGetMinY(bottomestRect) > CGRectGetMinY(annotation.bounds)) {
  1315. bottomestRect = annotation.bounds;
  1316. bottomestAnnotation = annotation;
  1317. }
  1318. }
  1319. if (type == KMPDFActiveFormsAlignType_Left ) {
  1320. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1321. for (NSInteger i = 0; i< self.activeAnnotations.count; i++) {
  1322. CPDFAnnotation *annotation = [self.activeAnnotations objectAtIndex:i];
  1323. CGRect bounds = annotation.bounds;
  1324. bounds.origin.x = zeroRect.origin.x;
  1325. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1326. }
  1327. [self changeAnnotationsAlign:self.activeAnnotations newBounds:newBoundsArray];
  1328. } else if (type == KMPDFActiveFormsAlignType_Right) {
  1329. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1330. for (NSInteger i = 0; i< self.activeAnnotations.count; i++) {
  1331. CPDFAnnotation *annotation = [self.activeAnnotations objectAtIndex:i];
  1332. CGRect bounds = annotation.bounds;
  1333. bounds.origin.x = CGRectGetMaxX(zeroRect) - bounds.size.width;
  1334. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1335. }
  1336. [self changeAnnotationsAlign:self.activeAnnotations newBounds:newBoundsArray];
  1337. } else if (type == KMPDFActiveFormsAlignType_Top) {
  1338. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1339. for (NSInteger i = 0; i< self.activeAnnotations.count; i++) {
  1340. CPDFAnnotation *annotation = [self.activeAnnotations objectAtIndex:i];
  1341. CGRect bounds = annotation.bounds;
  1342. bounds.origin.y = CGRectGetMaxY(zeroRect) - bounds.size.height;
  1343. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1344. }
  1345. [self changeAnnotationsAlign:self.activeAnnotations newBounds:newBoundsArray];
  1346. } else if (type == KMPDFActiveFormsAlignType_Bottom) {
  1347. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1348. for (NSInteger i = 0; i< self.activeAnnotations.count; i++) {
  1349. CPDFAnnotation *annotation = [self.activeAnnotations objectAtIndex:i];
  1350. CGRect bounds = annotation.bounds;
  1351. bounds.origin.y = CGRectGetMinY(zeroRect);
  1352. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1353. }
  1354. [self changeAnnotationsAlign:self.activeAnnotations newBounds:newBoundsArray];
  1355. } else if (type == KMPDFActiveFormsAlignType_Horizontally) {
  1356. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1357. for (NSInteger i = 0; i< self.activeAnnotations.count; i++) {
  1358. CPDFAnnotation *annotation = [self.activeAnnotations objectAtIndex:i];
  1359. CGRect bounds = annotation.bounds;
  1360. bounds.origin.y = CGRectGetMidY(highestRect) - CGRectGetHeight(bounds)/2;
  1361. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1362. }
  1363. [self changeAnnotationsAlign:self.activeAnnotations newBounds:newBoundsArray];
  1364. } else if (type == KMPDFActiveFormsAlignType_Vertical) {
  1365. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1366. for (NSInteger i = 0; i< self.activeAnnotations.count; i++) {
  1367. CPDFAnnotation *annotation = [self.activeAnnotations objectAtIndex:i];
  1368. CGRect bounds = annotation.bounds;
  1369. bounds.origin.x = CGRectGetMidX(widthestRect) - CGRectGetWidth(bounds)/2;
  1370. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1371. }
  1372. [self changeAnnotationsAlign:self.activeAnnotations newBounds:newBoundsArray];
  1373. } else if (type == KMPDFActiveFormsAlignType_DisHorizontally) {
  1374. CGFloat middleGap = CGRectGetWidth(zeroRect) - CGRectGetWidth(leftestRect) - CGRectGetWidth(rightestRect);
  1375. CGFloat otherAnnotationsTotalWidth = totalWidth - CGRectGetWidth(leftestRect) - CGRectGetWidth(rightestRect);
  1376. CGFloat gap = (middleGap - otherAnnotationsTotalWidth)/(self.activeAnnotations.count - 1);
  1377. NSMutableArray *annotationsCopyArray = [self.activeAnnotations mutableCopy];
  1378. [annotationsCopyArray sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
  1379. CPDFAnnotation *annotation1 = (CPDFAnnotation *)obj1;
  1380. CPDFAnnotation *annotation2 = (CPDFAnnotation *)obj2;
  1381. if (annotation1.bounds.origin.x < annotation2.bounds.origin.x) {
  1382. return NSOrderedAscending;
  1383. } else {
  1384. return NSOrderedDescending;
  1385. }
  1386. }];
  1387. [annotationsCopyArray removeObject:leftestAnnotation];
  1388. [annotationsCopyArray removeObject:rightestAnnotation];
  1389. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1390. CGFloat leftStartX = CGRectGetMaxX(leftestRect) + gap;
  1391. for (NSInteger i = 0; i < annotationsCopyArray.count; i++) {
  1392. CPDFAnnotation *annotation = [annotationsCopyArray objectAtIndex:i];
  1393. CGRect bounds = annotation.bounds;
  1394. bounds.origin.x = leftStartX;
  1395. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1396. leftStartX += CGRectGetWidth(bounds) + gap;
  1397. }
  1398. [self changeAnnotationsAlign:annotationsCopyArray newBounds:newBoundsArray];
  1399. } else if (type == KMPDFActiveFormsAlignType_DisVertical) {
  1400. CGFloat middleGap = CGRectGetHeight(zeroRect) - CGRectGetHeight(topestRect) - CGRectGetHeight(bottomestRect);
  1401. CGFloat otherAnnotationsTotalHeight = totalHeight - CGRectGetHeight(topestRect) - CGRectGetHeight(bottomestRect);
  1402. CGFloat gap = (middleGap - otherAnnotationsTotalHeight)/(self.activeAnnotations.count - 1);
  1403. NSMutableArray *annotationsCopyArray = [self.activeAnnotations mutableCopy];
  1404. [annotationsCopyArray sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
  1405. CPDFAnnotation *annotation1 = (CPDFAnnotation *)obj1;
  1406. CPDFAnnotation *annotation2 = (CPDFAnnotation *)obj2;
  1407. if (annotation1.bounds.origin.y < annotation2.bounds.origin.y) {
  1408. return NSOrderedAscending;
  1409. } else {
  1410. return NSOrderedDescending;
  1411. }
  1412. }];
  1413. [annotationsCopyArray removeObject:topestAnnotation];
  1414. [annotationsCopyArray removeObject:bottomestAnnotation];
  1415. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1416. CGFloat bottomStartY = CGRectGetMaxY(bottomestRect) + gap;
  1417. for (NSInteger i = 0; i < annotationsCopyArray.count; i++) {
  1418. PDFAnnotation *annotation = [annotationsCopyArray objectAtIndex:i];
  1419. CGRect bounds = annotation.bounds;
  1420. bounds.origin.y = bottomStartY;
  1421. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1422. bottomStartY += CGRectGetHeight(bounds) + gap;
  1423. }
  1424. [self changeAnnotationsAlign:annotationsCopyArray newBounds:newBoundsArray];
  1425. }
  1426. }
  1427. - (void)changeAnnotationsAlign:(NSArray *)annotationArray newBounds:(NSArray *)newBoundsArray {
  1428. NSMutableArray * oldBoundsArray = [NSMutableArray array];
  1429. for (NSInteger i = 0; i < annotationArray.count; i++) {
  1430. CPDFAnnotation *annotation = [annotationArray objectAtIndex:i];
  1431. [oldBoundsArray addObject:NSStringFromRect(annotation.bounds)];
  1432. annotation.bounds = NSRectFromString([newBoundsArray objectAtIndex:i]);
  1433. }
  1434. [[[self undoManager] prepareWithInvocationTarget:self] changeAnnotationsAlign:annotationArray newBounds:oldBoundsArray];
  1435. [self setNeedsDisplay:YES];
  1436. }
  1437. #pragma mark Services
  1438. - (BOOL)writeSelectionToPasteboard:(NSPasteboard *)pboard types:(NSArray *)types {
  1439. if ([self toolMode] == CSelectToolMode && NSIsEmptyRect(_selectionRect) == NO && _selectionPageIndex != NSNotFound) {
  1440. NSMutableArray *writeTypes = [NSMutableArray array];
  1441. NSString *pdfType = nil;
  1442. NSData *pdfData = nil;
  1443. NSString *tiffType = nil;
  1444. NSData *tiffData = nil;
  1445. NSRect selRect = NSIntegralRect(_selectionRect);
  1446. // Unfortunately only old PboardTypes are requested rather than preferred UTIs, even if we only validate and the Service only requests UTIs, so we need to support both
  1447. if ([[self document] allowsPrinting] && [[self document] isLocked] == NO) {
  1448. if ([types containsObject:NSPasteboardTypePDF])
  1449. pdfType = NSPasteboardTypePDF;
  1450. else if ([types containsObject:NSPasteboardTypePDF])
  1451. pdfType = NSPasteboardTypePDF;
  1452. if (pdfType && (pdfData = [[self currentSelectionPage] pdfDataFor:selRect]))
  1453. [writeTypes addObject:pdfType];
  1454. }
  1455. if ([types containsObject:NSPasteboardTypeTIFF])
  1456. tiffType = NSPasteboardTypeTIFF;
  1457. else if ([types containsObject:NSPasteboardTypeTIFF])
  1458. tiffType = NSPasteboardTypeTIFF;
  1459. if (tiffType && (tiffData = [[self currentSelectionPage] PDFListViewTIFFDataFor:selRect]))
  1460. [writeTypes addObject:tiffType];
  1461. if ([writeTypes count] > 0) {
  1462. [pboard declareTypes:writeTypes owner:nil];
  1463. if (pdfData)
  1464. [pboard setData:pdfData forType:pdfType];
  1465. if (tiffData)
  1466. [pboard setData:tiffData forType:tiffType];
  1467. return YES;
  1468. }
  1469. }
  1470. if ([[self currentSelection] hasCharacters]) {
  1471. if ([types containsObject:NSPasteboardTypeRTF] || [types containsObject:NSPasteboardTypeRTF]) {
  1472. [pboard clearContents];
  1473. [pboard writeObjects:[NSArray arrayWithObjects:[[self currentSelection] attributedString], nil]];
  1474. return YES;
  1475. } else if ([types containsObject:NSPasteboardTypeString] || [types containsObject:NSPasteboardTypeString]) {
  1476. [pboard clearContents];
  1477. [pboard writeObjects:[NSArray arrayWithObjects:[[self currentSelection] string], nil]];
  1478. return YES;
  1479. }
  1480. }
  1481. return [[CPDFListView superclass] instancesRespondToSelector:_cmd];
  1482. }
  1483. - (id)validRequestorForSendType:(NSString *)sendType returnType:(NSString *)returnType {
  1484. if ([self toolMode] == CSelectToolMode && NSIsEmptyRect(_selectionRect) == NO && _selectionPageIndex != NSNotFound && returnType == nil &&
  1485. (([[self document] allowsPrinting] && [[self document] isLocked] == NO && [sendType isEqualToString:NSPasteboardTypePDF]) || [sendType isEqualToString:NSPasteboardTypeTIFF])) {
  1486. return self;
  1487. }
  1488. if ([[self currentSelection] hasCharacters] && returnType == nil && ([sendType isEqualToString:NSPasteboardTypeString] || [sendType isEqualToString:NSPasteboardTypeRTF])) {
  1489. return self;
  1490. }
  1491. return [super validRequestorForSendType:sendType returnType:returnType];
  1492. }
  1493. @end