CPDFListView.m 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  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. }
  630. return annotation;
  631. }
  632. - (void)drawAnnotation:(CPDFAnnotation *)annotation toContext:(CGContextRef)context {
  633. [super drawAnnotation:annotation toContext:context];
  634. if ([self.pdfListViewDelegate respondsToSelector:@selector(PDFListView:needDrawAnnotation:)]) {
  635. if ([self.pdfListViewDelegate PDFListView:self needDrawAnnotation:annotation] == false) {
  636. return;
  637. }
  638. }
  639. if (CFormToolMode == _toolMode &&
  640. [annotation isKindOfClass:[CPDFWidgetAnnotation class]] && self.showFormFieldName && !annotation.isHidden) {
  641. CPDFWidgetAnnotation *annotationWidget = (CPDFWidgetAnnotation*)annotation;
  642. NSString *fieldName = annotationWidget.fieldName;
  643. if(self.showHopnumber) {
  644. NSInteger hopNumen = annotationWidget.widgetHopNumber;
  645. if(fieldName) {
  646. fieldName = [NSString stringWithFormat:@"%ld %@",hopNumen,fieldName];
  647. } else {
  648. fieldName = [NSString stringWithFormat:@"%ld",hopNumen];
  649. }
  650. }
  651. if (fieldName.length > 0) {
  652. CGContextSaveGState(context);
  653. NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
  654. paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
  655. paragraphStyle.alignment = NSTextAlignmentLeft;
  656. NSColor *backgroundColor = [NSColor blackColor];
  657. if ([self.activeAnnotations containsObject:annotation]) {
  658. backgroundColor = [NSColor colorWithRed:82.0/255.0 green:102.0/255.0 blue:204.0/255.0 alpha:1.0];
  659. }
  660. if([CPDFKitConfig sharedInstance].isShowFormRequiredFlagColor)
  661. backgroundColor = [CPDFKitConfig sharedInstance].formRequiredFlagColor?:[NSColor redColor];
  662. NSDictionary *attributes = @{NSFontAttributeName : [NSFont systemFontOfSize:12],
  663. NSForegroundColorAttributeName : [NSColor whiteColor],
  664. NSBackgroundColorAttributeName : backgroundColor,
  665. NSParagraphStyleAttributeName : paragraphStyle};
  666. NSString* drawString = fieldName;
  667. drawString = [drawString getMaxStringWithBounds:annotationWidget.bounds attributes:attributes];
  668. CGSize size = [drawString sizeWithAttributes:attributes];
  669. CGRect drawRect = annotationWidget.bounds;
  670. drawRect.origin.y += (drawRect.size.height - size.height)/2.0;
  671. drawRect.origin.x += (drawRect.size.width - size.width)/2.0;
  672. drawRect.size.height = size.height;
  673. drawRect.size.width = size.width;
  674. [NSGraphicsContext saveGraphicsState];
  675. [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]];
  676. [drawString drawInRect:drawRect withAttributes:attributes];
  677. [NSGraphicsContext restoreGraphicsState];
  678. CGContextRestoreGState(context);
  679. NSRect rect = [self integralRect:[annotationWidget bounds] onPage:[annotationWidget page]];
  680. CGFloat lineWidth = [self unitWidthOnPage:[annotationWidget page]];
  681. CGContextSaveGState(context);
  682. CGColorRef color = [NSColor blackColor].CGColor;
  683. CGContextSetStrokeColorWithColor(context, color);
  684. CGContextStrokeRectWithWidth(context, CGRectInset(NSRectToCGRect(rect), 0,0), lineWidth);
  685. }
  686. } else if(CNoteToolMode== _toolMode && _annotationType == CAnnotationTypeLink && [annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
  687. CGContextSaveGState(context);
  688. NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
  689. paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
  690. paragraphStyle.alignment = NSTextAlignmentLeft;
  691. NSColor *backgroundColor = [NSColor clearColor];
  692. NSDictionary *attributes = @{NSFontAttributeName : [NSFont systemFontOfSize:12],
  693. NSForegroundColorAttributeName : [NSColor whiteColor],
  694. NSBackgroundColorAttributeName : backgroundColor,
  695. NSParagraphStyleAttributeName : paragraphStyle};
  696. NSString *drawString = NSLocalizedString(@"No Destination", nil);
  697. CPDFDestination *destination = [(CPDFLinkAnnotation *)annotation destination];
  698. if(destination) {
  699. CPDFPage *page = destination.page;
  700. NSString * url = [(CPDFLinkAnnotation *)annotation URL];
  701. if(destination.page) {
  702. NSUInteger index = [self.document indexForPage:page] + 1;
  703. drawString = [NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"to Page",nil),@(index)];
  704. } else if(url.length > 0) {
  705. drawString = url;
  706. }
  707. }else{
  708. NSString * url = [(CPDFLinkAnnotation *)annotation URL];
  709. if(url.length > 0) {
  710. drawString = url;
  711. }
  712. }
  713. drawString = [drawString getMaxStringWithBounds:annotation.bounds attributes:attributes];
  714. CGSize size = [drawString sizeWithAttributes:attributes];
  715. CGRect drawRect = annotation.bounds;
  716. drawRect.origin.y += (drawRect.size.height - size.height)/2.0;
  717. drawRect.origin.x += (drawRect.size.width - size.width)/2.0;
  718. drawRect.size.height = size.height;
  719. drawRect.size.width = size.width;
  720. CGContextSaveGState(context);
  721. CGContextSetFillColorWithColor(context, [NSColor colorWithRed:23.0/255.0 green:112.0/255.0 blue:244.0/255.0 alpha:0.5].CGColor);
  722. CGContextFillRect(context,annotation.bounds);
  723. CGContextRestoreGState(context);
  724. [NSGraphicsContext saveGraphicsState];
  725. CGRect frameRect=CGRectMake(annotation.bounds.origin.x, annotation.bounds.origin.y + (annotation.bounds.size.height - 20)/2, annotation.bounds.size.width, 20);
  726. float fontSize = 9;
  727. NSColor *textColor = [NSColor whiteColor];
  728. CTTextAlignment alignMent = kCTTextAlignmentCenter;
  729. CFStringRef stringRef = (__bridge CFStringRef)drawString;
  730. NSMutableAttributedString* attString = [[NSMutableAttributedString alloc] initWithString:drawString];
  731. NSInteger _stringLength=[drawString length];
  732. CTTextAlignment theAlignment = alignMent;
  733. CTParagraphStyleSetting theSettings[1] =
  734. {
  735. { kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment),&theAlignment
  736. }
  737. };
  738. CTParagraphStyleRef paragraphStyleRef = CTParagraphStyleCreate(theSettings, 1);
  739. [attString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)paragraphStyleRef range:NSMakeRange(0, _stringLength)];
  740. [attString addAttribute:(id)kCTForegroundColorAttributeName value:(id)textColor.CGColor range:NSMakeRange(0, _stringLength)];
  741. [attString addAttribute:(id)kCTFontNameAttribute value:@"Courier" range:NSMakeRange(0, _stringLength)];
  742. CFAttributedStringRef attrString =(__bridge CFAttributedStringRef) attString;
  743. CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
  744. CGMutablePathRef framePath = CGPathCreateMutable();
  745. CGPathAddRect(framePath, NULL, frameRect);
  746. CFRange currentRange = CFRangeMake(0, 0);
  747. CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
  748. CGPathRelease(framePath);
  749. CTFrameDraw(frameRef, context);
  750. CFRelease(frameRef);
  751. CFRelease(framesetter);
  752. [NSGraphicsContext restoreGraphicsState];
  753. CGContextRestoreGState(context);
  754. }
  755. }
  756. - (void)setAddStampObject:(CStampSignatureObject *)stampObject keepToolModel:(BOOL)isToolModel {
  757. if (CAnnotationTypeStamp == self.annotationType ||
  758. CAnnotationTypeSignSignature == self.annotationType) {
  759. self.stampObject = stampObject;
  760. self.isStampModel = isToolModel;
  761. [NSCursor clearStampCursor];
  762. }
  763. }
  764. - (void)addAnnotationWithImage:(NSImage *)image page:(CPDFPage *)page point:(NSPoint)point {
  765. NSRect bounds = NSZeroRect;
  766. CPDFSelection *selection = [self currentSelection];
  767. CGFloat defaultWidth = 360;
  768. CGFloat defaultHeight = 90;
  769. NSSize defaultSize = ([page rotation] % 180 == 0) ? NSMakeSize(defaultWidth, defaultHeight) : NSMakeSize(defaultHeight, defaultWidth);
  770. bounds = CPDFListViewRectFromCenterAndSize(point, defaultSize);
  771. // Make sure it fits in the page
  772. bounds = CPDFListViewConstrainRect(bounds, [page boundsForBox:[self displayBox]],[CPDFListViewConfig defaultManager].annotationBorderOffset.floatValue);
  773. // bounds = CPDFListViewConstrainRect(bounds, [page boundsForBox:[self displayBox]]);
  774. if (page != nil) {
  775. BOOL isInitial = NSEqualSizes(bounds.size, NSZeroSize) && selection == nil;
  776. // new note added by note tool mode, don't add actual zero sized notes
  777. if (isInitial)
  778. bounds = CPDFListViewRectFromCenterAndSquareSize(bounds.origin, 8.0);
  779. CGFloat borderDefaultWidth = image.size.width;
  780. CGFloat borderScale = borderDefaultWidth/image.size.height;
  781. if (bounds.size.width/bounds.size.height > borderScale) {
  782. bounds.size.height = bounds.size.height;
  783. bounds.size.width = bounds.size.height*borderScale;
  784. } else {
  785. bounds.size.width = bounds.size.width;
  786. bounds.size.height = bounds.size.width/borderScale;
  787. }
  788. CPDFListStampAnnotation *newAnnotation = [[CPDFListStampAnnotation alloc] initWithDocument:self.document image:image];
  789. newAnnotation.bounds = bounds;
  790. [newAnnotation setBorderBoundsWithImage:image];
  791. [self addAnnotation:newAnnotation toPage:page];
  792. [[self undoManager] setActionName:NSLocalizedString(@"Add Note", @"Undo action name")];
  793. [self updateActiveAnnotations:@[newAnnotation]];
  794. [self setNeedsDisplayAnnotation:newAnnotation];
  795. }
  796. else NSBeep();
  797. }
  798. - (void)updateIsRightActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations {
  799. [self refreshActiveAnnotations:activeAnnotations isRight:YES];
  800. }
  801. - (void)updateActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations {
  802. [self refreshActiveAnnotations:activeAnnotations isRight:NO];
  803. }
  804. -(void)refreshActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations isRight:(BOOL)isRight {
  805. // for (CPDFAnnotation *annotation in self.activeAnnotations) {
  806. // if([annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
  807. // CPDFLinkAnnotation *linkAnnotation = (CPDFLinkAnnotation *)annotation;
  808. // if((linkAnnotation.destination || linkAnnotation.URL.length > 0)) {
  809. // } else {
  810. // [self removeAnnotation:linkAnnotation];
  811. // }
  812. // }
  813. // }
  814. if(activeAnnotations) {
  815. NSArray *selectTure = [activeAnnotations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF in %@", self.activeAnnotations]];
  816. BOOL isSame = NO;
  817. if(self.activeAnnotations.count == activeAnnotations.count && self.activeAnnotations.count == selectTure.count) isSame = YES;
  818. if(!isSame) {
  819. self.activeAnnotations = [NSMutableArray arrayWithArray:activeAnnotations];
  820. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
  821. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:isRight];
  822. dispatch_async(dispatch_get_main_queue(), ^{
  823. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  824. });
  825. }
  826. } else if (!activeAnnotations && self.activeAnnotations.count > 0) {
  827. [self.activeAnnotations removeAllObjects];
  828. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
  829. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:isRight];
  830. dispatch_async(dispatch_get_main_queue(), ^{
  831. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  832. });
  833. }
  834. }
  835. - (void)addActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations {
  836. BOOL isContains = YES;
  837. for (CPDFAnnotation *annotation in activeAnnotations) {
  838. if(![self.activeAnnotations containsObject:annotation]) {
  839. [self.activeAnnotations addObject:annotation];
  840. isContains = NO;
  841. }
  842. }
  843. if(!isContains) {
  844. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
  845. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:NO];
  846. dispatch_async(dispatch_get_main_queue(), ^{
  847. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  848. });
  849. }
  850. }
  851. - (void)removeActiveAnnotations:(NSArray<CPDFAnnotation *> *)removeAnnotations {
  852. BOOL isContains = NO;
  853. NSMutableArray *tarr = [NSMutableArray arrayWithArray:self.activeAnnotations];
  854. [tarr removeObjectsInArray:removeAnnotations];
  855. self.activeAnnotations = tarr;
  856. isContains = YES;
  857. if(isContains) {
  858. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
  859. [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:NO];
  860. dispatch_async(dispatch_get_main_queue(), ^{
  861. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
  862. });
  863. }
  864. }
  865. - (void)editAnnotation:(CPDFAnnotation *)annotation {
  866. if ([annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  867. [self editAnnotationFreeText:(CPDFFreeTextAnnotation *)annotation];
  868. [[self window] makeFirstResponder:self];
  869. [self setNeedsDisplayAnnotation:annotation];
  870. } else if ([annotation isKindOfClass:[CPDFButtonWidgetAnnotation class]] || [annotation isKindOfClass:[CPDFChoiceWidgetAnnotation class]]) {
  871. [self editFormAnnotation:self.activeAnnotation];
  872. } else {
  873. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewEditAnnotation:forAnnotation:)]) {
  874. [self.pdfListViewDelegate PDFListViewEditAnnotation:self forAnnotation:annotation];
  875. } else {
  876. [CPDFListView cancelPreviousPerformRequestsWithTarget:self selector:@selector(showHUDHint:) object:self.hoverAnnotation];
  877. if (!self.popOver) {
  878. _popOver = [[NSPopover alloc] init];
  879. }
  880. if([annotation isKindOfClass:[CPDFTextAnnotation class]]) {
  881. __block typeof(self) blockSelf = self;
  882. _popOver.delegate = self;
  883. CPDFListEditAnnotationViewController *vc = [[CPDFListEditAnnotationViewController alloc] initWithNibName:@"CPDFListEditAnnotationViewController" bundle:[NSBundle mainBundle] annotation:annotation];
  884. vc.pdflistView = self;
  885. _popOver.contentViewController = vc;
  886. vc.closeCallBack = ^{
  887. [blockSelf.popOver close];
  888. };
  889. NSBox *box = (NSBox *)vc.view;
  890. NSView *popoverView = [[[self.popOver contentViewController] view] superview];
  891. [popoverView setWantsLayer:YES];
  892. vc.changeColorCallBack = ^{
  893. NSColor *borColor = annotation.color?:[NSColor whiteColor];
  894. [[popoverView layer] setBackgroundColor:borColor.CGColor];
  895. [blockSelf setNeedsDisplayAnnotationViewForPage:annotation.page];
  896. };
  897. NSColor *borColor = annotation.color?:[NSColor whiteColor];
  898. [[popoverView layer] setBackgroundColor:borColor.CGColor];
  899. _popOver.animates = YES;
  900. _popOver.behavior = NSPopoverBehaviorTransient;
  901. [_popOver showRelativeToRect:[self convertRect:annotation.bounds fromPage:annotation.page] ofView:self preferredEdge:NSMaxXEdge];
  902. } else if(![annotation isKindOfClass:[CPDFLinkAnnotation class]]){
  903. CPDFListAnnotationNoteWindowController *wc = [CPDFListAnnotationNoteWindowController sharedInstance];
  904. CGRect rect = wc.window.frame;
  905. CGRect anBounds = [self convertRect:annotation.bounds fromPage:annotation.page];
  906. anBounds = [self convertRectToScreen:anBounds];
  907. rect.origin.x = anBounds.origin.x;
  908. rect.origin.y = anBounds.origin.y;
  909. if (@available(macOS 10.13, *))
  910. [wc.window makeKeyAndOrderFront:nil];
  911. else
  912. [wc showWindow:nil];
  913. [wc updateAnnotation:annotation];
  914. [wc.window setFrame:rect display:YES animate:NO];
  915. wc.window.title = @"";
  916. }
  917. }
  918. }
  919. }
  920. - (void)rotateStampAnnotation:(CPDFListStampAnnotation *)annotation rotateAngle:(NSInteger)angle {
  921. if([annotation isKindOfClass:[CPDFListStampAnnotation class]]) {
  922. CPDFListStampAnnotation *stampAnnotation = (CPDFListStampAnnotation *)annotation;
  923. NSInteger rotation = stampAnnotation.rotation;
  924. rotation = rotation - angle;
  925. if (rotation < 0) {
  926. rotation += 360;
  927. } else if (rotation >= 360) {
  928. rotation-=360;
  929. }
  930. [annotation setRotation:rotation];
  931. rotation = [annotation appearanceStreamRotation];
  932. [self setNeedsDisplayAnnotationViewForPage:annotation.page];
  933. [[[self undoManager] prepareWithInvocationTarget:self] rotateStampAnnotation:annotation rotateAngle:-angle];
  934. }
  935. }
  936. - (void)rotateSignatureAnnotation:(CPDFListSignatureAnnotation *)annotation rotateAngle:(NSInteger)angle {
  937. if([annotation isKindOfClass:[CPDFListSignatureAnnotation class]]) {
  938. CPDFListSignatureAnnotation *signatureAnnotation = (CPDFListSignatureAnnotation *)annotation;
  939. NSInteger rotation = signatureAnnotation.rotation;
  940. rotation = rotation - angle;
  941. if (rotation < 0) {
  942. rotation += 360;
  943. } else if (rotation >= 360) {
  944. rotation-=360;
  945. }
  946. [signatureAnnotation setRotation:rotation];
  947. rotation = [signatureAnnotation appearanceStreamRotation];
  948. [self setNeedsDisplayAnnotationViewForPage:annotation.page];
  949. [[[self undoManager] prepareWithInvocationTarget:self] rotateSignatureAnnotation:annotation rotateAngle:-angle];
  950. }
  951. }
  952. - (void)addAnnotationWithAnnotation:(CPDFAnnotation *)annotation toPage:(CPDFPage *)page {
  953. [self addAnnotation:annotation toPage:page];
  954. }
  955. - (void)addAnnotation:(CPDFAnnotation *)annotation toPage:(CPDFPage *)page {
  956. [[[self undoManager] prepareWithInvocationTarget:self] removeAnnotation:annotation];
  957. [annotation setModificationDate:[NSDate date]];
  958. //widget设置作者与fieldName冲突
  959. if (![annotation isKindOfClass:[CPDFWidgetAnnotation class]]) {
  960. [annotation setUserName:CPDFKitShareConfig.annotationAuthor?:NSFullUserName()];
  961. }
  962. [page addAnnotation:annotation];
  963. [self setNeedsDisplayAnnotation:annotation];
  964. [[self undoManager] setActionName:NSLocalizedString(@"Add Note", @"Undo action name")];
  965. dispatch_async(dispatch_get_main_queue(), ^{
  966. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewDidAddAnnotationNotification object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:page, CPDFListViewPageKey, annotation, CPDFListViewAnnotationKey, nil]];
  967. });
  968. [self resetPDFToolTipRects];
  969. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewAddAnnotations:forAddAnnotations:inPage:)])
  970. [self.pdfListViewDelegate PDFListViewAddAnnotations:self forAddAnnotations:@[annotation] inPage:page];
  971. }
  972. - (void)removeAnnotation:(CPDFAnnotation *)annotation {
  973. if ([annotation isKindOfClass:[CPDFTextAnnotation class]] && self.popOver.isShown) {
  974. // if (annotation.contents.length <= 0) {
  975. CPDFAnnotation *wasAnnotation = annotation;
  976. CPDFPage *page = wasAnnotation.page;
  977. [[[self undoManager] prepareWithInvocationTarget:self] addAnnotation:wasAnnotation toPage:page];
  978. [page removeAnnotation:wasAnnotation];
  979. [self annotationsChangedOnPage:page];
  980. [self setNeedsDisplayAnnotation:wasAnnotation];
  981. dispatch_async(dispatch_get_main_queue(), ^{
  982. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewDidRemoveAnnotationNotification object:self
  983. userInfo:[NSDictionary dictionaryWithObjectsAndKeys:wasAnnotation, CPDFListViewAnnotationKey, page, CPDFListViewPageKey, nil]];
  984. });
  985. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewRemoveAnnotations:forRemoveAnnotations:inPage:)])
  986. [self.pdfListViewDelegate PDFListViewRemoveAnnotations:self forRemoveAnnotations:@[annotation] inPage:page];
  987. // }
  988. } else {
  989. CPDFAnnotation *wasAnnotation = annotation;
  990. CPDFPage *page = wasAnnotation.page;
  991. [[[self undoManager] prepareWithInvocationTarget:self] addAnnotation:wasAnnotation toPage:page];
  992. if ([wasAnnotation isNote]) {
  993. [self resetPDFToolTipRects];
  994. }
  995. [page removeAnnotation:wasAnnotation];
  996. [self annotationsChangedOnPage:page];
  997. [self setNeedsDisplayAnnotation:wasAnnotation];
  998. dispatch_async(dispatch_get_main_queue(), ^{
  999. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewDidRemoveAnnotationNotification object:self
  1000. userInfo:[NSDictionary dictionaryWithObjectsAndKeys:wasAnnotation, CPDFListViewAnnotationKey, page, CPDFListViewPageKey, nil]];
  1001. });
  1002. if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewRemoveAnnotations:forRemoveAnnotations:inPage:)])
  1003. [self.pdfListViewDelegate PDFListViewRemoveAnnotations:self forRemoveAnnotations:@[annotation] inPage:page];
  1004. }
  1005. }
  1006. - (void)drawPage:(CPDFPage *)pdfPage toContext:(CGContextRef)context {
  1007. [super drawPage:pdfPage toContext:context];
  1008. [self drawSelectionForPage:pdfPage inContext:context];
  1009. BOOL isIncludText = NO;
  1010. NSMutableArray *currentActiveAnnotations = [NSMutableArray array];
  1011. NSMutableArray *currentPageAnnotations = [NSMutableArray array];
  1012. for (CPDFAnnotation *an in self.activeAnnotations) {
  1013. if([an.page isEqual:pdfPage]) {
  1014. if([an isKindOfClass:[CPDFFreeTextAnnotation class]])
  1015. isIncludText = YES;
  1016. //多选注释框去除mark注释与便签注释
  1017. if(!([an isKindOfClass:[CPDFMarkupAnnotation class]] || [an isKindOfClass:[CPDFTextAnnotation class]])) {
  1018. [currentActiveAnnotations addObject:an];
  1019. }
  1020. [currentPageAnnotations addObject:an];
  1021. }
  1022. }
  1023. if(currentActiveAnnotations.count > 1) {
  1024. NSRect rect = [self selectionMultipleBoundsWithAnnotations:currentActiveAnnotations];
  1025. CGFloat lineWidth = [self unitWidthOnPage:pdfPage];
  1026. CGContextSaveGState(context);
  1027. CGColorRef color = [CPDFListViewConfig defaultManager].annotationBorderColor.CGColor;
  1028. CGContextSetStrokeColorWithColor(context, color);
  1029. CGContextStrokeRectWithWidth(context, CGRectInset(NSRectToCGRect(rect), 0,0), lineWidth);
  1030. CGContextRestoreGState(context);
  1031. CGContextSaveGState(context);
  1032. if(isIncludText) {
  1033. [CPDFListView DrawFreeTextResizeHandle:context rect:rect radius: 4.0 * lineWidth active:true];
  1034. } else {
  1035. [CPDFListView DrawResizeHandles:context rect:rect radius:4.0 * lineWidth active:true];
  1036. }
  1037. CGContextRestoreGState(context);
  1038. }
  1039. if(self.multiplAnnotationObject &&
  1040. !CGRectIsEmpty(self.multiplAnnotationObject.drawRect) &&
  1041. self.multiplAnnotationObject.page == pdfPage) {
  1042. CGRect selectAnnotationBounds = self.multiplAnnotationObject.drawRect;
  1043. NSRect rect = selectAnnotationBounds;
  1044. CGFloat lineWidth = [self unitWidthOnPage:pdfPage];
  1045. CGContextSaveGState(context);
  1046. CGColorRef color = [CPDFListViewConfig defaultManager].annotationBorderColor.CGColor;
  1047. CGContextSetStrokeColorWithColor(context, color);
  1048. CGContextStrokeRectWithWidth(context, CGRectInset(NSRectToCGRect(rect), 0,0), lineWidth);
  1049. CGContextRestoreGState(context);
  1050. }
  1051. [currentPageAnnotations enumerateObjectsUsingBlock:^(CPDFAnnotation *annotation, NSUInteger idx, BOOL * _Nonnull stop) {
  1052. if (annotation.page && [annotation.page isEqual:pdfPage]) {
  1053. [annotation drawSelectionHighlightForView:self inContext:context isHover:NO];
  1054. }
  1055. }];
  1056. [self.selectAnnotations enumerateObjectsUsingBlock:^(CPDFAnnotation *annotation, NSUInteger idx, BOOL * _Nonnull stop) {
  1057. if (annotation.page && [annotation.page isEqual:pdfPage]) {
  1058. [annotation drawSelectionHighlightForView:self inContext:context isHover:NO];
  1059. }
  1060. }];
  1061. if (self.hoverAnnotation.page && [self.hoverAnnotation.page isEqual:pdfPage] && ![self.activeAnnotations containsObject:self.hoverAnnotation]) {
  1062. [self.hoverAnnotation drawSelectionHighlightForView:self inContext:context isHover:YES];
  1063. }
  1064. if(!CGRectEqualToRect(self.multiplSelectBounds, CGRectZero) && [self.multiplSelectPage isEqual:pdfPage]) {
  1065. @synchronized (self) {
  1066. CGContextSetLineCap(context, kCGLineCapRound);
  1067. CGContextSetLineWidth(context, 1.0);
  1068. CGContextSetStrokeColorWithColor(context, [CPDFListViewConfig defaultManager].annotationBorderColor.CGColor);
  1069. CGContextBeginPath(context);
  1070. CGContextMoveToPoint(context, self.multiplSelectBounds.origin.x, self.multiplSelectBounds.origin.y);
  1071. CGContextAddLineToPoint(context, self.multiplSelectBounds.origin.x+ self.multiplSelectBounds.size.width,self.multiplSelectBounds.origin.y);
  1072. CGContextAddLineToPoint(context, self.multiplSelectBounds.origin.x + self.multiplSelectBounds.size.width,self.multiplSelectBounds.origin.y + + self.multiplSelectBounds.size.height);
  1073. CGContextAddLineToPoint(context, self.multiplSelectBounds.origin.x,self.multiplSelectBounds.origin.y + + self.multiplSelectBounds.size.height);
  1074. CGContextClosePath(context);
  1075. CGFloat lengths[] = {5,5};
  1076. CGContextSetLineDash(context, 0, lengths,2);
  1077. CGContextStrokePath(context);
  1078. }
  1079. }
  1080. }
  1081. - (void)drawSelectionForPage:(CPDFPage *)pdfPage inContext:(CGContextRef)context {
  1082. NSRect rect;
  1083. NSUInteger pageIndex;
  1084. @synchronized (self) {
  1085. pageIndex = self.selectionPageIndex;
  1086. rect = self.selectionRect;
  1087. }
  1088. if (pageIndex != NSNotFound) {
  1089. BOOL isWidget = NO;
  1090. for (CPDFAnnotation * annotation in self.activeAnnotations) {
  1091. if ([annotation isForm]) {
  1092. isWidget = YES;
  1093. break;
  1094. }
  1095. }
  1096. if (isWidget &&
  1097. _toolMode != CFormToolMode) {
  1098. return;
  1099. }
  1100. NSRect bounds = [pdfPage boundsForBox:[self displayBox]];
  1101. CGFloat radius = HANDLE_SIZE * [self unitWidthOnPage:pdfPage];
  1102. CGColorRef color = CGColorCreateGenericGray(0.0, 0.6);
  1103. CGContextSetFillColorWithColor(context, color);
  1104. CGColorRelease(color);
  1105. CGContextBeginPath(context);
  1106. CGContextAddRect(context, NSRectToCGRect(bounds));
  1107. CGContextAddRect(context, NSRectToCGRect(rect));
  1108. CGContextEOFillPath(context);
  1109. if ([pdfPage pageIndex] != pageIndex) {
  1110. color = CGColorCreateGenericGray(0.0, 0.3);
  1111. CGContextSetFillColorWithColor(context, color);
  1112. CGColorRelease(color);
  1113. CGContextFillRect(context, NSRectToCGRect(rect));
  1114. }
  1115. [CPDFListView DrawResizeHandles:context rect:rect radius:radius active:true];
  1116. }
  1117. }
  1118. - (void)removeShapeLayer {
  1119. if (![self.shapeLayerTopH isEqual:nil] && ![self.shapeLayerBottomH isEqual:nil] && ![self.shapeLayerLeftV isEqual:nil] && ![self.shapeLayerRightV isEqual:nil]) {
  1120. [self.shapeLayerTopH removeFromSuperlayer];
  1121. [self.shapeLayerBottomH removeFromSuperlayer];
  1122. [self.shapeLayerLeftV removeFromSuperlayer];
  1123. [self.shapeLayerRightV removeFromSuperlayer];
  1124. self.shapeLayerTopH = nil;
  1125. self.shapeLayerBottomH = nil;
  1126. self.shapeLayerLeftV = nil;
  1127. self.shapeLayerRightV = nil;
  1128. }
  1129. }
  1130. - (BOOL)consistentTypeWithAnnotation:(CPDFAnnotation *)annotation {
  1131. if (self.annotationType == CAnnotationTypeSignSignature) {
  1132. if (![annotation isKindOfClass:[CPDFSignatureAnnotation class]])
  1133. return NO;
  1134. } else if (self.annotationType == CAnnotationTypeStamp) {
  1135. if (![annotation isKindOfClass:[CPDFStampAnnotation class]])
  1136. return NO;
  1137. } else if (self.annotationType == CAnnotationTypeAnchored) {
  1138. if (![annotation isKindOfClass:[CPDFTextAnnotation class]])
  1139. return NO;
  1140. } else if (self.annotationType == CAnnotationTypeLink) {
  1141. if (![annotation isKindOfClass:[CPDFLinkAnnotation class]])
  1142. return NO;
  1143. } else if (self.annotationType == CAnnotationTypeFreeText) {
  1144. if (![annotation isKindOfClass:[CPDFFreeTextAnnotation class]])
  1145. return NO;
  1146. } else if (self.annotationType == CAnnotationTypeLine || self.annotationType == CAnnotationTypeArrow || self.annotationType == CAnnotationTypeSquare || self.annotationType == CAnnotationTypeCircle) {
  1147. if (![annotation isKindOfClass:[CPDFLineAnnotation class]] && ![annotation isKindOfClass:[CPDFSquareAnnotation class]] && ![annotation isKindOfClass:[CPDFCircleAnnotation class]])
  1148. return NO;
  1149. } else if (self.annotationType == CAnnotationTypeHighlight || self.annotationType == CAnnotationTypeUnderline || self.annotationType == CAnnotationTypeStrikeOut) {
  1150. if (![annotation isKindOfClass:[CPDFMarkupAnnotation class]])
  1151. return NO;
  1152. } else if (self.annotationType == CAnnotationTypeInk) {
  1153. if (![annotation isKindOfClass:[CPDFInkAnnotation class]])
  1154. return NO;
  1155. } else if (self.annotationType == CAnnotationTypeRadioButton) {
  1156. } else if (self.annotationType == CAnnotationTypeCheckBox) {
  1157. } else if (self.annotationType == CAnnotationTypeTextField) {
  1158. } else if (self.annotationType == CAnnotationTypeComboBox) {
  1159. } else if (self.annotationType == CAnnotationTypeListMenu) {
  1160. } else if (self.annotationType == CAnnotationTypeActionButton) {
  1161. } else if (self.annotationType == CAnnotationTypeSignature) {
  1162. } else if (self.annotationType == CAnnotationTypeSignText) {
  1163. } else if (self.annotationType == CAnnotationTypeSignFalse) {
  1164. } else if (self.annotationType == CAnnotationTypeSignTure) {
  1165. } else if (self.annotationType == CAnnotationTypeSignCircle) {
  1166. } else if (self.annotationType == CAnnotationTypeSignLine) {
  1167. } else if (self.annotationType == CAnnotationTypeSignDot) {
  1168. } else if (self.annotationType == CAnnotationTypeSignConfig) {
  1169. } else if (self.annotationType == CAnnotationTypeSignDate) {
  1170. }
  1171. return YES;
  1172. }
  1173. #pragma mark Zooming
  1174. - (void)zoomToRect:(NSRect)rect onPage:(CPDFPage *)page {
  1175. if (NSIsEmptyRect(rect) == NO) {
  1176. BOOL isLegacy = [NSScroller respondsToSelector:@selector(preferredScrollerStyle)] == NO || [NSScroller preferredScrollerStyle] == NSScrollerStyleLegacy;
  1177. NSRect bounds = [self bounds];
  1178. CGFloat scale = 1.0;
  1179. if (isLegacy) {
  1180. bounds.size.width -= [NSScroller scrollerWidth];
  1181. bounds.size.height -= [NSScroller scrollerWidth];
  1182. }
  1183. if (NSWidth(bounds) * NSHeight(rect) > NSWidth(rect) * NSHeight(bounds))
  1184. scale = NSHeight(bounds) / NSHeight(rect);
  1185. else
  1186. scale = NSWidth(bounds) / NSWidth(rect);
  1187. [self setScaleFactor:scale];
  1188. NSScrollView *scrollView = [self scrollView];
  1189. if (isLegacy && ([scrollView hasHorizontalScroller] == NO || [scrollView hasVerticalScroller] == NO)) {
  1190. if ([scrollView hasVerticalScroller])
  1191. bounds.size.width -= [NSScroller scrollerWidth];
  1192. if ([scrollView hasHorizontalScroller])
  1193. bounds.size.height -= [NSScroller scrollerWidth];
  1194. if (NSWidth(bounds) * NSHeight(rect) > NSWidth(rect) * NSHeight(bounds))
  1195. scale = NSHeight(bounds) / NSHeight(rect);
  1196. else
  1197. scale = NSWidth(bounds) / NSWidth(rect);
  1198. [self setScaleFactor:scale];
  1199. }
  1200. [self goToRect:rect onPage:page];
  1201. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1202. CGPoint tPagePoint = CGPointMake(rect.origin.x, rect.origin.y+rect.size.height);
  1203. [self goToTargetPoint:tPagePoint onPage:page atScrollPosition:CScrollPositionTop];
  1204. });
  1205. }
  1206. }
  1207. #pragma mark - NSPopoverDelegate
  1208. - (void)popoverWillClose:(NSNotification *)notification {
  1209. NSPopover *popover = notification.object;
  1210. if([popover.contentViewController isKindOfClass:[CPDFListEditAnnotationViewController class]]) {
  1211. CPDFListEditAnnotationViewController *listEditAnnotationViewController = (CPDFListEditAnnotationViewController *)popover.contentViewController;
  1212. CPDFAnnotation *annotation = listEditAnnotationViewController.annotation;
  1213. NSString *contensString = listEditAnnotationViewController.contentString;
  1214. if([annotation isKindOfClass:[CPDFTextAnnotation class]]) {
  1215. if(contensString && contensString.length > 0) {
  1216. annotation.contents = listEditAnnotationViewController.contentString;
  1217. [self setNeedsDisplayAnnotation:annotation];
  1218. } else {
  1219. if([self.activeAnnotations containsObject:annotation]) {
  1220. [self.activeAnnotations removeObject:annotation];
  1221. }
  1222. [self removeAnnotation:annotation];
  1223. }
  1224. } else {
  1225. annotation.contents = listEditAnnotationViewController.contentString;
  1226. [self setNeedsDisplayAnnotation:annotation];
  1227. }
  1228. }
  1229. self.popOver = nil;
  1230. }
  1231. - (void)handlePageChangedNotification:(NSNotification *)notification {
  1232. if ([self displayMode] == kPDFDisplaySinglePage || [self displayMode] == kPDFDisplayTwoUp) {
  1233. [self resetPDFToolTipRects];
  1234. }
  1235. self.copyCount = 0;
  1236. }
  1237. #pragma mark - Form Alignment
  1238. - (KMPDFViewActiveFormsType)formAnnotionsType:(NSArray *)annotationArray {
  1239. NSSet *set = [NSSet setWithArray:[annotationArray valueForKey:@"type"]];
  1240. NSArray *array = [set allObjects];
  1241. if (array.count > 1 || ![array.firstObject isEqualToString:@"Widget"]) {
  1242. return KMPDFViewActiveFormsType_NotAllform;
  1243. }
  1244. PDFAnnotation *referenceAnnotation = annotationArray.firstObject;
  1245. Class referenceAnnotationClass = [referenceAnnotation class];
  1246. if ([referenceAnnotationClass isEqual:[CPDFButtonWidgetAnnotation class]]) {
  1247. referenceAnnotationClass = [CPDFButtonWidgetAnnotation superclass];
  1248. }
  1249. if ([referenceAnnotationClass isEqual:[CPDFTextWidgetAnnotation class]]) {
  1250. referenceAnnotationClass = [CPDFTextWidgetAnnotation superclass];
  1251. }
  1252. if ([referenceAnnotationClass isEqual:[CPDFChoiceWidgetAnnotation class]]) {
  1253. referenceAnnotationClass = [CPDFChoiceWidgetAnnotation superclass];
  1254. }
  1255. if (annotationArray.count == 1) {
  1256. if ([referenceAnnotation isKindOfClass:[CPDFTextWidgetAnnotation class]]) {
  1257. return KMPDFViewActiveFormsType_TextSingle;
  1258. } else {
  1259. return KMPDFViewActiveFormsType_PureSingle;
  1260. }
  1261. }
  1262. for (NSInteger i = 0; i < annotationArray.count; i++) {
  1263. PDFAnnotation *annotation = [annotationArray objectAtIndex:i];
  1264. if (![annotation isKindOfClass:[CPDFButtonWidgetAnnotation class]] && ![annotation isKindOfClass:[CPDFTextWidgetAnnotation class]] && ![annotation isKindOfClass:[CPDFChoiceWidgetAnnotation class]]) {
  1265. return KMPDFViewActiveFormsType_Mix;
  1266. }
  1267. }
  1268. if ([referenceAnnotation isKindOfClass:[CPDFTextWidgetAnnotation class]]){
  1269. return KMPDFViewActiveFormsType_Text;
  1270. }
  1271. return KMPDFViewActiveFormsType_Pure;
  1272. }
  1273. - (void)formsAnnotationAlign:(KMPDFActiveFormsAlignType)type {
  1274. CGRect zeroRect = CGRectNull;
  1275. CGRect highestRect = CGRectZero;
  1276. CGRect widthestRect = CGRectZero;
  1277. CPDFAnnotation *firstAnnotation = self.activeAnnotations.firstObject;
  1278. CGRect leftestRect = firstAnnotation.bounds;
  1279. CGRect rightestRect = firstAnnotation.bounds;
  1280. CGRect topestRect = firstAnnotation.bounds;
  1281. CGRect bottomestRect = firstAnnotation.bounds;
  1282. CPDFAnnotation *leftestAnnotation = firstAnnotation;
  1283. CPDFAnnotation *rightestAnnotation = firstAnnotation;
  1284. CPDFAnnotation *topestAnnotation = firstAnnotation;
  1285. CPDFAnnotation *bottomestAnnotation = firstAnnotation;
  1286. CGFloat totalWidth = 0.;
  1287. CGFloat totalHeight = 0.;
  1288. for (CPDFAnnotation *annotation in self.activeAnnotations) {
  1289. zeroRect = CGRectUnion(zeroRect, annotation.bounds);
  1290. totalWidth += CGRectGetWidth(annotation.bounds);
  1291. totalHeight += CGRectGetHeight(annotation.bounds);
  1292. if (CGRectGetHeight(annotation.bounds) > CGRectGetHeight(highestRect)) {
  1293. highestRect = annotation.bounds;
  1294. }
  1295. if (CGRectGetWidth(annotation.bounds) > CGRectGetWidth(widthestRect)) {
  1296. widthestRect = annotation.bounds;
  1297. }
  1298. if (CGRectGetMinX(leftestRect) > CGRectGetMinX(annotation.bounds)) {
  1299. leftestRect = annotation.bounds;
  1300. leftestAnnotation = annotation;
  1301. }
  1302. if (CGRectGetMaxX(annotation.bounds) > CGRectGetMaxX(rightestRect)) {
  1303. rightestRect = annotation.bounds;
  1304. rightestAnnotation = annotation;
  1305. }
  1306. if (CGRectGetMaxY(annotation.bounds) > CGRectGetMaxY(topestRect)) {
  1307. topestRect = annotation.bounds;
  1308. topestAnnotation = annotation;
  1309. }
  1310. if (CGRectGetMinY(bottomestRect) > CGRectGetMinY(annotation.bounds)) {
  1311. bottomestRect = annotation.bounds;
  1312. bottomestAnnotation = annotation;
  1313. }
  1314. }
  1315. if (type == KMPDFActiveFormsAlignType_Left ) {
  1316. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1317. for (NSInteger i = 0; i< self.activeAnnotations.count; i++) {
  1318. CPDFAnnotation *annotation = [self.activeAnnotations objectAtIndex:i];
  1319. CGRect bounds = annotation.bounds;
  1320. bounds.origin.x = zeroRect.origin.x;
  1321. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1322. }
  1323. [self changeAnnotationsAlign:self.activeAnnotations newBounds:newBoundsArray];
  1324. } else if (type == KMPDFActiveFormsAlignType_Right) {
  1325. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1326. for (NSInteger i = 0; i< self.activeAnnotations.count; i++) {
  1327. CPDFAnnotation *annotation = [self.activeAnnotations objectAtIndex:i];
  1328. CGRect bounds = annotation.bounds;
  1329. bounds.origin.x = CGRectGetMaxX(zeroRect) - bounds.size.width;
  1330. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1331. }
  1332. [self changeAnnotationsAlign:self.activeAnnotations newBounds:newBoundsArray];
  1333. } else if (type == KMPDFActiveFormsAlignType_Top) {
  1334. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1335. for (NSInteger i = 0; i< self.activeAnnotations.count; i++) {
  1336. CPDFAnnotation *annotation = [self.activeAnnotations objectAtIndex:i];
  1337. CGRect bounds = annotation.bounds;
  1338. bounds.origin.y = CGRectGetMaxY(zeroRect) - bounds.size.height;
  1339. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1340. }
  1341. [self changeAnnotationsAlign:self.activeAnnotations newBounds:newBoundsArray];
  1342. } else if (type == KMPDFActiveFormsAlignType_Bottom) {
  1343. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1344. for (NSInteger i = 0; i< self.activeAnnotations.count; i++) {
  1345. CPDFAnnotation *annotation = [self.activeAnnotations objectAtIndex:i];
  1346. CGRect bounds = annotation.bounds;
  1347. bounds.origin.y = CGRectGetMinY(zeroRect);
  1348. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1349. }
  1350. [self changeAnnotationsAlign:self.activeAnnotations newBounds:newBoundsArray];
  1351. } else if (type == KMPDFActiveFormsAlignType_Horizontally) {
  1352. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1353. for (NSInteger i = 0; i< self.activeAnnotations.count; i++) {
  1354. CPDFAnnotation *annotation = [self.activeAnnotations objectAtIndex:i];
  1355. CGRect bounds = annotation.bounds;
  1356. bounds.origin.y = CGRectGetMidY(highestRect) - CGRectGetHeight(bounds)/2;
  1357. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1358. }
  1359. [self changeAnnotationsAlign:self.activeAnnotations newBounds:newBoundsArray];
  1360. } else if (type == KMPDFActiveFormsAlignType_Vertical) {
  1361. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1362. for (NSInteger i = 0; i< self.activeAnnotations.count; i++) {
  1363. CPDFAnnotation *annotation = [self.activeAnnotations objectAtIndex:i];
  1364. CGRect bounds = annotation.bounds;
  1365. bounds.origin.x = CGRectGetMidX(widthestRect) - CGRectGetWidth(bounds)/2;
  1366. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1367. }
  1368. [self changeAnnotationsAlign:self.activeAnnotations newBounds:newBoundsArray];
  1369. } else if (type == KMPDFActiveFormsAlignType_DisHorizontally) {
  1370. CGFloat middleGap = CGRectGetWidth(zeroRect) - CGRectGetWidth(leftestRect) - CGRectGetWidth(rightestRect);
  1371. CGFloat otherAnnotationsTotalWidth = totalWidth - CGRectGetWidth(leftestRect) - CGRectGetWidth(rightestRect);
  1372. CGFloat gap = (middleGap - otherAnnotationsTotalWidth)/(self.activeAnnotations.count - 1);
  1373. NSMutableArray *annotationsCopyArray = [self.activeAnnotations mutableCopy];
  1374. [annotationsCopyArray sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
  1375. CPDFAnnotation *annotation1 = (CPDFAnnotation *)obj1;
  1376. CPDFAnnotation *annotation2 = (CPDFAnnotation *)obj2;
  1377. if (annotation1.bounds.origin.x < annotation2.bounds.origin.x) {
  1378. return NSOrderedAscending;
  1379. } else {
  1380. return NSOrderedDescending;
  1381. }
  1382. }];
  1383. [annotationsCopyArray removeObject:leftestAnnotation];
  1384. [annotationsCopyArray removeObject:rightestAnnotation];
  1385. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1386. CGFloat leftStartX = CGRectGetMaxX(leftestRect) + gap;
  1387. for (NSInteger i = 0; i < annotationsCopyArray.count; i++) {
  1388. CPDFAnnotation *annotation = [annotationsCopyArray objectAtIndex:i];
  1389. CGRect bounds = annotation.bounds;
  1390. bounds.origin.x = leftStartX;
  1391. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1392. leftStartX += CGRectGetWidth(bounds) + gap;
  1393. }
  1394. [self changeAnnotationsAlign:annotationsCopyArray newBounds:newBoundsArray];
  1395. } else if (type == KMPDFActiveFormsAlignType_DisVertical) {
  1396. CGFloat middleGap = CGRectGetHeight(zeroRect) - CGRectGetHeight(topestRect) - CGRectGetHeight(bottomestRect);
  1397. CGFloat otherAnnotationsTotalHeight = totalHeight - CGRectGetHeight(topestRect) - CGRectGetHeight(bottomestRect);
  1398. CGFloat gap = (middleGap - otherAnnotationsTotalHeight)/(self.activeAnnotations.count - 1);
  1399. NSMutableArray *annotationsCopyArray = [self.activeAnnotations mutableCopy];
  1400. [annotationsCopyArray sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
  1401. CPDFAnnotation *annotation1 = (CPDFAnnotation *)obj1;
  1402. CPDFAnnotation *annotation2 = (CPDFAnnotation *)obj2;
  1403. if (annotation1.bounds.origin.y < annotation2.bounds.origin.y) {
  1404. return NSOrderedAscending;
  1405. } else {
  1406. return NSOrderedDescending;
  1407. }
  1408. }];
  1409. [annotationsCopyArray removeObject:topestAnnotation];
  1410. [annotationsCopyArray removeObject:bottomestAnnotation];
  1411. NSMutableArray *newBoundsArray = [NSMutableArray array];
  1412. CGFloat bottomStartY = CGRectGetMaxY(bottomestRect) + gap;
  1413. for (NSInteger i = 0; i < annotationsCopyArray.count; i++) {
  1414. PDFAnnotation *annotation = [annotationsCopyArray objectAtIndex:i];
  1415. CGRect bounds = annotation.bounds;
  1416. bounds.origin.y = bottomStartY;
  1417. [newBoundsArray addObject:NSStringFromRect(bounds)];
  1418. bottomStartY += CGRectGetHeight(bounds) + gap;
  1419. }
  1420. [self changeAnnotationsAlign:annotationsCopyArray newBounds:newBoundsArray];
  1421. }
  1422. }
  1423. - (void)changeAnnotationsAlign:(NSArray *)annotationArray newBounds:(NSArray *)newBoundsArray {
  1424. NSMutableArray * oldBoundsArray = [NSMutableArray array];
  1425. for (NSInteger i = 0; i < annotationArray.count; i++) {
  1426. CPDFAnnotation *annotation = [annotationArray objectAtIndex:i];
  1427. [oldBoundsArray addObject:NSStringFromRect(annotation.bounds)];
  1428. annotation.bounds = NSRectFromString([newBoundsArray objectAtIndex:i]);
  1429. }
  1430. [[[self undoManager] prepareWithInvocationTarget:self] changeAnnotationsAlign:annotationArray newBounds:oldBoundsArray];
  1431. [self setNeedsDisplay:YES];
  1432. }
  1433. #pragma mark Services
  1434. - (BOOL)writeSelectionToPasteboard:(NSPasteboard *)pboard types:(NSArray *)types {
  1435. if ([self toolMode] == CSelectToolMode && NSIsEmptyRect(_selectionRect) == NO && _selectionPageIndex != NSNotFound) {
  1436. NSMutableArray *writeTypes = [NSMutableArray array];
  1437. NSString *pdfType = nil;
  1438. NSData *pdfData = nil;
  1439. NSString *tiffType = nil;
  1440. NSData *tiffData = nil;
  1441. NSRect selRect = NSIntegralRect(_selectionRect);
  1442. // 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
  1443. if ([[self document] allowsPrinting] && [[self document] isLocked] == NO) {
  1444. if ([types containsObject:NSPasteboardTypePDF])
  1445. pdfType = NSPasteboardTypePDF;
  1446. else if ([types containsObject:NSPasteboardTypePDF])
  1447. pdfType = NSPasteboardTypePDF;
  1448. if (pdfType && (pdfData = [[self currentSelectionPage] pdfDataFor:selRect]))
  1449. [writeTypes addObject:pdfType];
  1450. }
  1451. if ([types containsObject:NSPasteboardTypeTIFF])
  1452. tiffType = NSPasteboardTypeTIFF;
  1453. else if ([types containsObject:NSPasteboardTypeTIFF])
  1454. tiffType = NSPasteboardTypeTIFF;
  1455. if (tiffType && (tiffData = [[self currentSelectionPage] PDFListViewTIFFDataFor:selRect]))
  1456. [writeTypes addObject:tiffType];
  1457. if ([writeTypes count] > 0) {
  1458. [pboard declareTypes:writeTypes owner:nil];
  1459. if (pdfData)
  1460. [pboard setData:pdfData forType:pdfType];
  1461. if (tiffData)
  1462. [pboard setData:tiffData forType:tiffType];
  1463. return YES;
  1464. }
  1465. }
  1466. if ([[self currentSelection] hasCharacters]) {
  1467. if ([types containsObject:NSPasteboardTypeRTF] || [types containsObject:NSPasteboardTypeRTF]) {
  1468. [pboard clearContents];
  1469. [pboard writeObjects:[NSArray arrayWithObjects:[[self currentSelection] attributedString], nil]];
  1470. return YES;
  1471. } else if ([types containsObject:NSPasteboardTypeString] || [types containsObject:NSPasteboardTypeString]) {
  1472. [pboard clearContents];
  1473. [pboard writeObjects:[NSArray arrayWithObjects:[[self currentSelection] string], nil]];
  1474. return YES;
  1475. }
  1476. }
  1477. return [[CPDFListView superclass] instancesRespondToSelector:_cmd];
  1478. }
  1479. - (id)validRequestorForSendType:(NSString *)sendType returnType:(NSString *)returnType {
  1480. if ([self toolMode] == CSelectToolMode && NSIsEmptyRect(_selectionRect) == NO && _selectionPageIndex != NSNotFound && returnType == nil &&
  1481. (([[self document] allowsPrinting] && [[self document] isLocked] == NO && [sendType isEqualToString:NSPasteboardTypePDF]) || [sendType isEqualToString:NSPasteboardTypeTIFF])) {
  1482. return self;
  1483. }
  1484. if ([[self currentSelection] hasCharacters] && returnType == nil && ([sendType isEqualToString:NSPasteboardTypeString] || [sendType isEqualToString:NSPasteboardTypeRTF])) {
  1485. return self;
  1486. }
  1487. return [super validRequestorForSendType:sendType returnType:returnType];
  1488. }
  1489. @end