123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161 |
- //
- // CPDFListView.m
- // ComPDFKit
- //
- // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
- //
- // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
- // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
- // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
- // This notice may not be removed from this file.
- //
- #import "CPDFListView.h"
- #import "CPDFAnnotationModel.h"
- #import "CStampSignatureObject.h"
- #import "CSelfSignAnnotation.h"
- #import "CSelfSignAnnotationFreeText.h"
- #import "CPDFListViewConfig.h"
- #import "CPDFListEditAnnotationViewController.h"
- #import "CPDFListAnnotationNoteWindowController.h"
- #import "CPDFListView+Private.h"
- #import "CPDFListView+Event.h"
- #import "CPDFListView+Tool.h"
- #import "CPDFAnnotation+PDFListView.h"
- #import "CPDFSelection+PDFListView.h"
- #import "CPDFPage+PDFListView.h"
- #import "CPDFListView+Extension.h"
- #import "CPDFListView+UndoManager.h"
- #import "NSString+PDFListView.h"
- #import "NSImage+PDFListView.h"
- #import "NSCursor+PDFListView.h"
- #import "CPDFListStampAnnotation+Private.h"
- #import "CPDFListSignatureAnnotation+Private.h"
- #import "CPDFMarkupAnnotation+PDFListView.h"
- #import "CPDFButtonWidgetAnnotation+PDFListView.h"
- #import "CPDFTextWidgetAnnotation+PDFListView.h"
- #import "CPDFChoiceWidgetAnnotation+PDFListView.h"
- #import "CPDFChoiceWidgetAnnotation+PDFListView.h"
- #import "CPDFLineAnnotation+PDFListView.h"
- #import "NSPopover+PDFListView.h"
- #import "NSGeometry_SKExtensions.h"
- NSNotificationName const CPDFListViewSelectionChangedNotification = @"CPDFListViewSelectionChangedNotification";
- NSNotificationName const CPDFListViewToolModeChangeNotification = @"CPDFListViewToolModeChangeNotification";
- NSNotificationName const CPDFListViewAnnotationTypeChangeNotification = @"CPDFListViewAnnotationTypeChangeNotification";
- NSNotificationName const CPDFListViewMagnificationChangedNotification = @"CPDFListViewMagnificationChangedNotification";
- NSNotificationName const CPDFListViewDidAddAnnotationNotification = @"CPDFListViewDidAddAnnotationNotification";
- NSNotificationName const CPDFListViewDidRemoveAnnotationNotification = @"CPDFListViewDidRemoveAnnotationNotification";
- NSNotificationName const CPDFListViewActiveAnnotationsChangeNotification = @"CPDFListViewActiveAnnotationsChangeNotification";
- NSNotificationName const CPDFListViewAnnotationsAttributeHasChangeNotification = @"CPDFListViewAnnotationsAttributeHasChangeNotification";
- @implementation CPDFListView
- #pragma mark - Init
- - (id)init {
- self = [super init];
- if (self) {
- [self commonInitialization];
- }
- return self;
- }
- - (id)initWithFrame:(NSRect)frameRect {
- self = [super initWithFrame:frameRect];
- if (self) {
- [self commonInitialization];
- }
- return self;
- }
- - (id)initWithCoder:(NSCoder *)decoder {
- self = [super initWithCoder:decoder];
- if (self) {
- [self commonInitialization];
- }
- return self;
- }
- - (void)updateLayer {
- [super updateLayer];
- if (@available(macOS 10.14, *)) {
- if ([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewBackgroundColor)]) {
- NSColor *color = [self.pdfListViewDelegate PDFListViewBackgroundColor];
- self.backgroundColor = color;
- }
- }
- }
- - (void)commonInitialization {
- self.selectionPageIndex = NSNotFound;
- self.clickLineAnnotation = nil;
- self.isClickDoubleCreatLine = NO;
-
- self.activeAnnotations = [NSMutableArray array];
- self.selectAnnotations = [NSMutableArray array];
- self.dragHoverPoints = [NSMutableArray array];
- self.aCopyAnnotations = [NSMutableArray array];
-
- [self registerAsObserver];
-
- [self addTrackingArea];
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlePageChangedNotification:)
- name:CPDFViewPageChangedNotification object:self];
- }
- - (void)addTrackingArea {
- NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect | NSTrackingActiveInKeyWindow | NSTrackingMouseMoved owner:self userInfo:nil];
- [self addTrackingArea:trackingArea];
- }
- - (void)goToPageIndex:(NSInteger)pageIndex animated:(BOOL)animated {
- if (self.currentPageIndex != pageIndex) {
- [self recordForwardBackWithPageIndex:self.currentPageIndex];
- }
-
- [super goToPageIndex:pageIndex animated:animated];
- }
- - (void)goToSelection:(CPDFSelection *)selection animated:(BOOL)animated {
- if (self.currentPageIndex != selection.page.pageIndex) {
- [self recordForwardBackWithPageIndex:self.currentPageIndex];
- }
-
- [super goToSelection:selection animated:animated];
- }
- - (void)goToDestination:(CPDFDestination *)destination {
- [super goToDestination:destination];
- }
- - (void)goToRect:(CGRect)rect onPage:(CPDFPage *)page animated:(BOOL)animated {
- if (self.currentPageIndex != page.pageIndex) {
- [self recordForwardBackWithPageIndex:self.currentPageIndex];
- }
-
- [super goToRect:rect onPage:page animated:animated];
- }
- - (void)goToTargetPoint:(NSPoint)point onPage:(CPDFPage *)page atScrollPosition:(CScrollPosition)scrollPosition {
- if (self.currentPageIndex != page.pageIndex) {
- [self recordForwardBackWithPageIndex:self.currentPageIndex];
- }
-
- [super goToTargetPoint:point onPage:page atScrollPosition:scrollPosition];
- }
- #pragma mark - Setter & Get
- - (void)setDocument:(CPDFDocument *)document {
- @synchronized (self) {
- self.selectionRect = NSZeroRect;
- self.selectionPageIndex = NSNotFound;
- }
- // self.toolMode = CFormToolMode;
- // self.annotationType = CAnnotationTypeRadioButton;
- // CStampSignatureObject *objc = [[CStampSignatureObject alloc] initImageStampWithFilePath:[[NSBundle mainBundle] pathForResource:@"Quick Start Guide" ofType:@"pdf"]];
- // [self setAddStampObject:objc keepToolModel:YES];
- //
- [super setDocument:document];
-
- [self.window makeFirstResponder:self];
-
- }
- -(void)setToolMode:(CToolMode)newToolMode {
- if(newToolMode != _toolMode) {
- BOOL isDisplayAnnotationView = NO;
-
- if(self.isSetLinkDestinationArea)
- self.isSetLinkDestinationArea = NO;
-
- if ((CTextToolMode == _toolMode || CNoteToolMode == _toolMode) && CTextToolMode != newToolMode) {
- if (newToolMode != CNoteToolMode && self.activeAnnotation) {
- if([self.activeAnnotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
- CPDFFreeTextAnnotation *freeTextAn = (CPDFFreeTextAnnotation *)self.activeAnnotation;
- if ([self isEditWithCurrentFreeText:freeTextAn]) {
- [self commitEditAnnotationFreeText:freeTextAn];
- }
- }
- [self updateActiveAnnotations:@[]];
- }
- if ([self currentSelection])
- [self setCurrentSelection:nil];
- } else if (_toolMode == CSelectToolMode && NSEqualRects(self.selectionRect, NSZeroRect) == NO) {
- self.selectionRect = NSZeroRect;
- self.selectionPageIndex = NSNotFound;
- dispatch_async(dispatch_get_main_queue(), ^{
- [[NSNotificationCenter defaultCenter] postNotificationName:CPDFViewSelectionChangedNotification object:self];
- });
- if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangedSelectionOrMagnification:)])
- [self.pdfListViewDelegate PDFListViewChangedSelectionOrMagnification:self];
-
- isDisplayAnnotationView = YES;
- }
-
- BOOL highlightFormFiled = [[NSUserDefaults standardUserDefaults] boolForKey:@"kPDFViewHighlightFormFiledKey"];
- if (CFormToolMode == _toolMode && !highlightFormFiled) {
- [[CPDFKitConfig sharedInstance] setEnableFormFieldHighlight:highlightFormFiled];
- isDisplayAnnotationView = YES;
- } else if (CFormToolMode == newToolMode && !highlightFormFiled) {
- [[CPDFKitConfig sharedInstance] setEnableFormFieldHighlight:YES];
- isDisplayAnnotationView = YES;
- }
-
- if(CEditPDFToolMode == newToolMode)
- [self beginEditingLoadType:CEditingLoadTypeText|CEditingLoadTypeImage];
-
- if(_toolMode == CEditPDFToolMode) {
- if(self.isEdited) {
- [self commitEditing];
- }
- [self endOfEditing];
- }
-
- _toolMode = newToolMode;
-
- self.selectZoomToolModeZoomOut = NO;
- if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangedToolMode:forToolMode:)])
- [self.pdfListViewDelegate PDFListViewChangedToolMode:self forToolMode:newToolMode];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewToolModeChangeNotification object:self];
- });
- [self resetPDFViewAnnotation];
-
- if(isDisplayAnnotationView)
- [self setNeedsDisplayAnnotationViewForVisiblePages];
- }
- }
- -(void)setShowHopnumber:(BOOL)showHopnumber {
- _showHopnumber = showHopnumber;
- [self setNeedsDisplayAnnotationViewForVisiblePages];
- }
- - (void)setAnnotationType:(CAnnotationType)annotationType {
- if (_annotationType != annotationType) {
- if(CAnnotationTypeLink == annotationType || CAnnotationTypeLink == _annotationType)
- [self setNeedsDisplayAnnotationViewForVisiblePages];
- _annotationType = annotationType;
-
- if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangedAnnotationType:forAnnotationType:)])
- [self.pdfListViewDelegate PDFListViewChangedAnnotationType:self forAnnotationType:annotationType];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewAnnotationTypeChangeNotification object:self];
- });
- }
- if(self.activeAnnotations.count > 0) {
- if([self.activeAnnotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
- CPDFFreeTextAnnotation *freeTextAn = (CPDFFreeTextAnnotation *)self.activeAnnotation;
- if ([self isEditWithCurrentFreeText:freeTextAn]) {
- [self commitEditAnnotationFreeText:freeTextAn];
- }
- }
- [self updateActiveAnnotations:@[]];
- [self setNeedsDisplayAnnotationViewForVisiblePages];
- }
-
- if(self.isSetLinkDestinationArea)
- self.isSetLinkDestinationArea = NO;
- [self resetPDFViewAnnotation];
- }
- - (NSRect)currentSelectionRect {
- if (CSelectToolMode == self.toolMode)
- return self.selectionRect;
- return NSZeroRect;
- }
- - (CPDFAnnotation *)activeAnnotation {
- return self.activeAnnotations.lastObject;
- }
- - (void)setHideNotes:(BOOL)hideNotes {
- _hideNotes = hideNotes;
-
- for(NSInteger i = 0;i<self.document.pageCount;i++) {
- CPDFPage *page = [self.document pageAtIndex:i];
- for (CPDFAnnotation *annotation in page.annotations) {
- [annotation setAnnotationShouldDisplay:!self.hideNotes];
- [annotation setHidden: self.hideNotes];
- }
- }
- [self setNeedsDisplayAnnotationViewForVisiblePages];
- }
- -(void)setIsSetLinkDestinationArea:(BOOL)isSetLinkDestinationArea {
- _isSetLinkDestinationArea = isSetLinkDestinationArea;
- if (_isSetLinkDestinationArea) {
- if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewLinkDestinationStart:withActiveAnnotation:)]) {
- [self.pdfListViewDelegate PDFListViewLinkDestinationStart:self withActiveAnnotation:self.activeAnnotation];
- }
- } else {
- if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewLinkDestinationEnd:withActiveAnnotation:)]) {
- [self.pdfListViewDelegate PDFListViewLinkDestinationEnd:self withActiveAnnotation:self.activeAnnotation];
- }
- }
- }
- #pragma mark - Public
- - (CPDFAnnotation *)addAnnotationWithType:(CAnnotationType)annotationType selection:(CPDFSelection *)selection page:(CPDFPage *)page bounds:(NSRect)bounds {
- CPDFAnnotation *annotation = nil;
-
- NSString *text = [selection PDFListViewCleanedString];
-
- if([CPDFListView isMarkupAnnotationType:annotationType]) {
- if(selection){
- annotation = [self addPDFSelection:selection annotationTyoe:annotationType];
- [annotation setContents:text?:@""];
- } else NSBeep();
- } else {
- BOOL isInitial = NO;
- if(selection) {
- CGRect noteRect = selection.bounds;
- isInitial = NSEqualSizes(noteRect.size, NSZeroSize);
- } else {
- isInitial = NSEqualSizes(bounds.size, NSZeroSize);
- }
- CPDFAnnotationModel *annotationModel = [[CPDFAnnotationModel alloc]initWithAnnotationType:annotationType];
- CGSize size = CGSizeMake(annotationModel.noteWidth, annotationModel.noteHeight);
- if(annotationType == CAnnotationTypeAnchored)
- bounds = CGRectMake(bounds.origin.x, bounds.origin.y, size.width, size.height);
- if (annotationType == CAnnotationTypeCircle || annotationType == CAnnotationTypeSquare || annotationType == CAnnotationTypeLine || annotationType == CAnnotationTypeArrow || annotationType == CAnnotationTypeAnchored) {
- bounds = KMConstrainRect(bounds, [page boundsForBox:[self displayBox]]);
- }
-
- if (isInitial)
- bounds = annotationType == CAnnotationTypeAnchored ? CPDFListViewRectFromCenterAndSize(bounds.origin, size) : CPDFListViewRectFromCenterAndSquareSize(bounds.origin, MIN_NOTE_SIZE);
- switch (annotationType) {
- case CAnnotationTypeFreeText:
- annotation = [[CPDFFreeTextAnnotation alloc]initPDFListViewNoteWithDocument:self.document];
- annotation.bounds = bounds;
- break;
- case CAnnotationTypeAnchored:
- annotation = [[CPDFTextAnnotation alloc]initPDFListViewNoteWithDocument:self.document];
- annotation.bounds = bounds;
- break;
- case CAnnotationTypeCircle:
- annotation = [[CPDFCircleAnnotation alloc]initPDFListViewNoteWithDocument:self.document];
- annotation.bounds = bounds;
- break;
- case CAnnotationTypeSquare:
- annotation = [[CPDFSquareAnnotation alloc]initPDFListViewNoteWithDocument:self.document];
- annotation.bounds = bounds;
- break;
- case CAnnotationTypeLine:
- annotation = [[CPDFLineAnnotation alloc]initPDFListViewNoteWithDocument:self.document annotationType:CAnnotationTypeLine];
- [(CPDFLineAnnotation *)annotation setObservedStartPoint:CGPointMake(bounds.origin.x, bounds.origin.y)];
- [(CPDFLineAnnotation *)annotation setObservedEndPoint:CGPointMake(bounds.origin.x + bounds.size.width,bounds.origin.y+ bounds.size.height)];
- [(CPDFLineAnnotation *)annotation setStartLineStyle:CPDFLineStyleNone];
- [(CPDFLineAnnotation *)annotation setEndLineStyle:CPDFLineStyleNone];
- break;
- case CAnnotationTypeArrow: {
- annotation = [[CPDFLineAnnotation alloc]initPDFListViewNoteWithDocument:self.document annotationType:CAnnotationTypeArrow];
- [(CPDFLineAnnotation *)annotation setObservedStartPoint:CGPointMake(bounds.origin.x, bounds.origin.y)];
- [(CPDFLineAnnotation *)annotation setObservedEndPoint:CGPointMake(bounds.origin.x + bounds.size.width,bounds.origin.y+ bounds.size.height)];
- }
- break;
- case CAnnotationTypeLink:
- annotation = [[CPDFLinkAnnotation alloc] initPDFListViewNoteWithDocument:self.document];
- annotation.bounds = bounds;
- break;
- case CAnnotationTypeTextField:
- annotation = [[CPDFTextWidgetAnnotation alloc] initPDFListViewNoteWithDocument:self.document];
- annotation.bounds = bounds;
- break;
- case CAnnotationTypeCheckBox:
- annotation = [[CPDFButtonWidgetAnnotation alloc] initPDFListViewNoteWithDocument:self.document controlType:CPDFWidgetCheckBoxControl];
- annotation.bounds = bounds;
- break;
- case CAnnotationTypeRadioButton:
- annotation = [[CPDFButtonWidgetAnnotation alloc] initPDFListViewNoteWithDocument:self.document controlType:CPDFWidgetRadioButtonControl];
- annotation.bounds = bounds;
- break;
- case CAnnotationTypeListMenu:
- annotation = [[CPDFChoiceWidgetAnnotation alloc] initPDFListViewNoteWithDocument:self.document listChoice:YES];
- annotation.bounds = bounds;
- break;
- case CAnnotationTypeComboBox:
- annotation = [[CPDFChoiceWidgetAnnotation alloc] initPDFListViewNoteWithDocument:self.document listChoice:NO];
- annotation.bounds = bounds;
- break;
- case CAnnotationTypeSignature:
- annotation = [[CPDFSignatureWidgetAnnotation alloc] initPDFListViewNoteWithDocument:self.document];
- annotation.bounds = bounds;
- break;
- case CAnnotationTypeActionButton:
- annotation = [[CPDFButtonWidgetAnnotation alloc] initPDFListViewNoteWithDocument:self.document controlType:CPDFWidgetPushButtonControl];
- annotation.bounds = bounds;
- break;
- case CAnnotationTypeSignFalse:
- case CAnnotationTypeSignTure:
- case CAnnotationTypeSignCircle:
- case CAnnotationTypeSignLine:
- case CAnnotationTypeSignDot:
- annotation = [[CSelfSignAnnotation alloc]initPDFListViewNoteWithDocument:self.document type:self.annotationType];
- annotation.bounds = bounds;
- [(CSelfSignAnnotation *)annotation updateAppearanceStream];
- break;
- case CAnnotationTypeSignText:
- case CAnnotationTypeSignConfig:
- case CAnnotationTypeSignDate:
- annotation = [[CSelfSignAnnotationFreeText alloc]initPDFListViewNoteWithDocument:self.document subType:self.annotationType string:@"" bounds:bounds];
- break;
- case CAnnotationTypeRedact:
- annotation = [self addRedactPDFSelection:selection];
- break;
- default:
- break;
- }
- }
-
- if (annotation) {
- if (annotationType != CAnnotationTypeLine && annotationType != CAnnotationTypeArrow && annotationType != CAnnotationTypeInk && [text length] > 0)
- [annotation setString:text];
-
- [self addAnnotation:annotation toPage:page];
- }
-
- return annotation;
- }
- - (void)drawAnnotation:(CPDFAnnotation *)annotation toContext:(CGContextRef)context {
- [super drawAnnotation:annotation toContext:context];
-
- if ([self.pdfListViewDelegate respondsToSelector:@selector(PDFListView:needDrawAnnotation:)]) {
- if ([self.pdfListViewDelegate PDFListView:self needDrawAnnotation:annotation] == false) {
- return;
- }
- }
-
- if (CFormToolMode == _toolMode &&
- [annotation isKindOfClass:[CPDFWidgetAnnotation class]] && self.showFormFieldName && !annotation.isHidden) {
-
- CPDFWidgetAnnotation *annotationWidget = (CPDFWidgetAnnotation*)annotation;
- NSString *fieldName = annotationWidget.fieldName;
- if(self.showHopnumber) {
- NSInteger hopNumen = annotationWidget.widgetHopNumber;
- if(fieldName) {
- fieldName = [NSString stringWithFormat:@"%ld %@",hopNumen,fieldName];
- } else {
- fieldName = [NSString stringWithFormat:@"%ld",hopNumen];
- }
-
- }
- if (fieldName.length > 0) {
- CGContextSaveGState(context);
- NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
- paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
- paragraphStyle.alignment = NSTextAlignmentLeft;
- NSColor *backgroundColor = [NSColor blackColor];
- if ([self.activeAnnotations containsObject:annotation]) {
- backgroundColor = [NSColor colorWithRed:82.0/255.0 green:102.0/255.0 blue:204.0/255.0 alpha:1.0];
- }
-
- if([CPDFKitConfig sharedInstance].isShowFormRequiredFlagColor)
- backgroundColor = [CPDFKitConfig sharedInstance].formRequiredFlagColor?:[NSColor redColor];
-
- NSDictionary *attributes = @{NSFontAttributeName : [NSFont systemFontOfSize:12],
- NSForegroundColorAttributeName : [NSColor whiteColor],
- NSBackgroundColorAttributeName : backgroundColor,
- NSParagraphStyleAttributeName : paragraphStyle};
- NSString* drawString = fieldName;
- drawString = [drawString getMaxStringWithBounds:annotationWidget.bounds attributes:attributes];
-
- CGSize size = [drawString sizeWithAttributes:attributes];
- CGRect drawRect = annotationWidget.bounds;
- drawRect.origin.y += (drawRect.size.height - size.height)/2.0;
- drawRect.origin.x += (drawRect.size.width - size.width)/2.0;
- drawRect.size.height = size.height;
- drawRect.size.width = size.width;
-
- [NSGraphicsContext saveGraphicsState];
- [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]];
- [drawString drawInRect:drawRect withAttributes:attributes];
- [NSGraphicsContext restoreGraphicsState];
- CGContextRestoreGState(context);
-
- NSRect rect = [self integralRect:[annotationWidget bounds] onPage:[annotationWidget page]];
- CGFloat lineWidth = [self unitWidthOnPage:[annotationWidget page]];
- CGContextSaveGState(context);
- CGColorRef color = [NSColor blackColor].CGColor;
- CGContextSetStrokeColorWithColor(context, color);
- CGContextStrokeRectWithWidth(context, CGRectInset(NSRectToCGRect(rect), 0,0), lineWidth);
- }
- } else if(CNoteToolMode== _toolMode && _annotationType == CAnnotationTypeLink && [annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
- CGContextSaveGState(context);
- NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
- paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
- paragraphStyle.alignment = NSTextAlignmentLeft;
- NSColor *backgroundColor = [NSColor clearColor];
-
- NSDictionary *attributes = @{NSFontAttributeName : [NSFont systemFontOfSize:12],
- NSForegroundColorAttributeName : [NSColor whiteColor],
- NSBackgroundColorAttributeName : backgroundColor,
- NSParagraphStyleAttributeName : paragraphStyle};
-
- NSString *drawString = NSLocalizedString(@"No Destination", nil);
- CPDFDestination *destination = [(CPDFLinkAnnotation *)annotation destination];
- if(destination) {
- CPDFPage *page = destination.page;
- NSString * url = [(CPDFLinkAnnotation *)annotation URL];
-
- if(destination.page) {
- NSUInteger index = [self.document indexForPage:page] + 1;
- drawString = [NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"to Page",nil),@(index)];
- } else if(url.length > 0) {
- drawString = url;
- }
- }
- drawString = [drawString getMaxStringWithBounds:annotation.bounds attributes:attributes];
-
- CGSize size = [drawString sizeWithAttributes:attributes];
- CGRect drawRect = annotation.bounds;
- drawRect.origin.y += (drawRect.size.height - size.height)/2.0;
- drawRect.origin.x += (drawRect.size.width - size.width)/2.0;
- drawRect.size.height = size.height;
- drawRect.size.width = size.width;
-
- CGContextSaveGState(context);
- CGContextSetFillColorWithColor(context, [NSColor colorWithRed:23.0/255.0 green:112.0/255.0 blue:244.0/255.0 alpha:0.5].CGColor);
- CGContextFillRect(context,annotation.bounds);
- CGContextRestoreGState(context);
-
- [NSGraphicsContext saveGraphicsState];
- CGRect frameRect=CGRectMake(annotation.bounds.origin.x, annotation.bounds.origin.y + (annotation.bounds.size.height - 20)/2, annotation.bounds.size.width, 20);
- float fontSize = 9;
- NSColor *textColor = [NSColor whiteColor];
- CTTextAlignment alignMent = kCTTextAlignmentCenter;
- CFStringRef stringRef = (__bridge CFStringRef)drawString;
- NSMutableAttributedString* attString = [[NSMutableAttributedString alloc] initWithString:drawString];
- NSInteger _stringLength=[drawString length];
- CTTextAlignment theAlignment = alignMent;
- CTParagraphStyleSetting theSettings[1] =
- {
- { kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment),&theAlignment
- }
-
- };
-
- CTParagraphStyleRef paragraphStyleRef = CTParagraphStyleCreate(theSettings, 1);
- [attString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)paragraphStyleRef range:NSMakeRange(0, _stringLength)];
- [attString addAttribute:(id)kCTForegroundColorAttributeName value:(id)textColor.CGColor range:NSMakeRange(0, _stringLength)];
- [attString addAttribute:(id)kCTFontNameAttribute value:@"Courier" range:NSMakeRange(0, _stringLength)];
- CFAttributedStringRef attrString =(__bridge CFAttributedStringRef) attString;
- CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
- CGMutablePathRef framePath = CGPathCreateMutable();
- CGPathAddRect(framePath, NULL, frameRect);
- CFRange currentRange = CFRangeMake(0, 0);
- CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
- CGPathRelease(framePath);
- CTFrameDraw(frameRef, context);
- CFRelease(frameRef);
- CFRelease(framesetter);
- [NSGraphicsContext restoreGraphicsState];
- CGContextRestoreGState(context);
- }
- }
- - (void)setAddStampObject:(CStampSignatureObject *)stampObject keepToolModel:(BOOL)isToolModel {
- if (CAnnotationTypeStamp == self.annotationType ||
- CAnnotationTypeSignSignature == self.annotationType) {
- self.stampObject = stampObject;
- self.isStampModel = isToolModel;
- [NSCursor clearStampCursor];
- }
- }
- - (void)addAnnotationWithImage:(NSImage *)image page:(CPDFPage *)page point:(NSPoint)point {
- NSRect bounds = NSZeroRect;
- CPDFSelection *selection = [self currentSelection];
-
- CGFloat defaultWidth = 360;
- CGFloat defaultHeight = 90;
- NSSize defaultSize = ([page rotation] % 180 == 0) ? NSMakeSize(defaultWidth, defaultHeight) : NSMakeSize(defaultHeight, defaultWidth);
- bounds = CPDFListViewRectFromCenterAndSize(point, defaultSize);
-
- // Make sure it fits in the page
- bounds = CPDFListViewConstrainRect(bounds, [page boundsForBox:[self displayBox]],[CPDFListViewConfig defaultManager].annotationBorderOffset.floatValue);
- // bounds = CPDFListViewConstrainRect(bounds, [page boundsForBox:[self displayBox]]);
- if (page != nil) {
- BOOL isInitial = NSEqualSizes(bounds.size, NSZeroSize) && selection == nil;
- // new note added by note tool mode, don't add actual zero sized notes
- if (isInitial)
- bounds = CPDFListViewRectFromCenterAndSquareSize(bounds.origin, 8.0);
-
- CGFloat borderDefaultWidth = image.size.width;
- CGFloat borderScale = borderDefaultWidth/image.size.height;
- if (bounds.size.width/bounds.size.height > borderScale) {
- bounds.size.height = bounds.size.height;
- bounds.size.width = bounds.size.height*borderScale;
- } else {
- bounds.size.width = bounds.size.width;
- bounds.size.height = bounds.size.width/borderScale;
- }
-
- CPDFListStampAnnotation *newAnnotation = [[CPDFListStampAnnotation alloc] initWithDocument:self.document image:image];
- newAnnotation.bounds = bounds;
- [newAnnotation setBorderBoundsWithPImage:image];
- [self addAnnotation:newAnnotation toPage:page];
- [[self undoManager] setActionName:NSLocalizedString(@"Add Note", @"Undo action name")];
-
- [self updateActiveAnnotations:@[newAnnotation]];
- [self setNeedsDisplayAnnotation:newAnnotation];
- }
- else NSBeep();
- }
- - (void)updateIsRightActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations {
- [self refreshActiveAnnotations:activeAnnotations isRight:YES];
- }
- - (void)updateActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations {
- [self refreshActiveAnnotations:activeAnnotations isRight:NO];
- }
- -(void)refreshActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations isRight:(BOOL)isRight {
- // for (CPDFAnnotation *annotation in self.activeAnnotations) {
- // if([annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
- // CPDFLinkAnnotation *linkAnnotation = (CPDFLinkAnnotation *)annotation;
- // if((linkAnnotation.destination || linkAnnotation.URL.length > 0)) {
- // } else {
- // [self removeAnnotation:linkAnnotation];
- // }
- // }
- // }
-
- if(activeAnnotations) {
- NSArray *selectTure = [activeAnnotations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF in %@", self.activeAnnotations]];
-
- BOOL isSame = NO;
- if(self.activeAnnotations.count == activeAnnotations.count && self.activeAnnotations.count == selectTure.count) isSame = YES;
-
- if(!isSame) {
- self.activeAnnotations = [NSMutableArray arrayWithArray:activeAnnotations];
-
- if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
- [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:isRight];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
- });
- }
- } else if (!activeAnnotations && self.activeAnnotations.count > 0) {
- [self.activeAnnotations removeAllObjects];
- if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
- [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:isRight];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
- });
- }
- }
- - (void)addActiveAnnotations:(NSArray<CPDFAnnotation *> *)activeAnnotations {
- BOOL isContains = YES;
- for (CPDFAnnotation *annotation in activeAnnotations) {
- if(![self.activeAnnotations containsObject:annotation]) {
- [self.activeAnnotations addObject:annotation];
- isContains = NO;
- }
- }
-
- if(!isContains) {
- if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
- [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:NO];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
- });
- }
- }
- - (void)removeActiveAnnotations:(NSArray<CPDFAnnotation *> *)removeAnnotations {
- BOOL isContains = NO;
- NSMutableArray *tarr = [NSMutableArray arrayWithArray:self.activeAnnotations];
- [tarr removeObjectsInArray:removeAnnotations];
- self.activeAnnotations = tarr;
- isContains = YES;
- if(isContains) {
- if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:isRightMenu:)])
- [self.pdfListViewDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations isRightMenu:NO];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
- });
- }
- }
- - (void)editAnnotation:(CPDFAnnotation *)annotation {
- if ([annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
- [self editAnnotationFreeText:(CPDFFreeTextAnnotation *)annotation];
- [[self window] makeFirstResponder:self];
-
- [self setNeedsDisplayAnnotation:annotation];
- } else {
- if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewEditAnnotation:forAnnotation:)]) {
- [self.pdfListViewDelegate PDFListViewEditAnnotation:self forAnnotation:annotation];
- } else {
- [CPDFListView cancelPreviousPerformRequestsWithTarget:self selector:@selector(showHUDHint:) object:self.hoverAnnotation];
- if (!self.popOver) {
- _popOver = [[NSPopover alloc] init];
- }
-
- if([annotation isKindOfClass:[CPDFTextAnnotation class]]) {
- __block typeof(self) blockSelf = self;
- _popOver.delegate = self;
- CPDFListEditAnnotationViewController *vc = [[CPDFListEditAnnotationViewController alloc] initWithNibName:@"CPDFListEditAnnotationViewController" bundle:[NSBundle mainBundle] annotation:annotation];
- vc.pdflistView = self;
- _popOver.contentViewController = vc;
- vc.closeCallBack = ^{
- [blockSelf.popOver close];
- };
- NSBox *box = (NSBox *)vc.view;
- NSView *popoverView = [[[self.popOver contentViewController] view] superview];
- [popoverView setWantsLayer:YES];
-
- vc.changeColorCallBack = ^{
- NSColor *borColor = annotation.color?:[NSColor whiteColor];
- [[popoverView layer] setBackgroundColor:borColor.CGColor];
- [blockSelf setNeedsDisplayAnnotationViewForPage:annotation.page];
- };
- NSColor *borColor = annotation.color?:[NSColor whiteColor];
- [[popoverView layer] setBackgroundColor:borColor.CGColor];
- _popOver.animates = YES;
- _popOver.behavior = NSPopoverBehaviorTransient;
- [_popOver showRelativeToRect:[self convertRect:annotation.bounds fromPage:annotation.page] ofView:self preferredEdge:NSMaxXEdge];
- } else if(![annotation isKindOfClass:[CPDFLinkAnnotation class]]){
- CPDFListAnnotationNoteWindowController *wc = [CPDFListAnnotationNoteWindowController sharedInstance];
- CGRect rect = wc.window.frame;
- CGRect anBounds = [self convertRect:annotation.bounds fromPage:annotation.page];
-
- anBounds = [self convertRectToScreen:anBounds];
- rect.origin.x = anBounds.origin.x;
- rect.origin.y = anBounds.origin.y;
- if (@available(macOS 10.13, *))
- [wc.window makeKeyAndOrderFront:nil];
- else
- [wc showWindow:nil];
- [wc updateAnnotation:annotation];
- [wc.window setFrame:rect display:YES animate:NO];
- wc.window.title = @"";
- }
-
- }
- }
- }
- - (void)rotateStampAnnotation:(CPDFListStampAnnotation *)annotation rotateAngle:(NSInteger)angle {
- if([annotation isKindOfClass:[CPDFListStampAnnotation class]]) {
- CPDFListStampAnnotation *stampAnnotation = (CPDFListStampAnnotation *)annotation;
- NSInteger rotation = stampAnnotation.rotation;
- rotation = rotation - angle;
-
- if (rotation < 0) {
- rotation += 360;
- } else if (rotation >= 360) {
- rotation-=360;
- }
- [annotation setRotation:rotation];
- rotation = [annotation appearanceStreamRotation];
-
- [self setNeedsDisplayAnnotationViewForPage:annotation.page];
-
- [[[self undoManager] prepareWithInvocationTarget:self] rotateStampAnnotation:annotation rotateAngle:-angle];
- }
- }
- - (void)rotateSignatureAnnotation:(CPDFListSignatureAnnotation *)annotation rotateAngle:(NSInteger)angle {
- if([annotation isKindOfClass:[CPDFListSignatureAnnotation class]]) {
- CPDFListSignatureAnnotation *signatureAnnotation = (CPDFListSignatureAnnotation *)annotation;
- NSInteger rotation = signatureAnnotation.rotation;
- rotation = rotation - angle;
-
- if (rotation < 0) {
- rotation += 360;
- } else if (rotation >= 360) {
- rotation-=360;
- }
- [signatureAnnotation setRotation:rotation];
- rotation = [signatureAnnotation appearanceStreamRotation];
-
- [self setNeedsDisplayAnnotationViewForPage:annotation.page];
-
- [[[self undoManager] prepareWithInvocationTarget:self] rotateSignatureAnnotation:annotation rotateAngle:-angle];
- }
- }
- - (void)addAnnotation:(CPDFAnnotation *)annotation toPage:(CPDFPage *)page {
- [[[self undoManager] prepareWithInvocationTarget:self] removeAnnotation:annotation];
-
- [annotation setModificationDate:[NSDate date]];
- //widget设置作者与fieldName冲突
- if (![annotation isKindOfClass:[CPDFWidgetAnnotation class]]) {
- [annotation setUserName:CPDFKitShareConfig.annotationAuthor?:NSFullUserName()];
- }
- [page addAnnotation:annotation];
- [self setNeedsDisplayAnnotation:annotation];
- [[self undoManager] setActionName:NSLocalizedString(@"Add Note", @"Undo action name")];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewDidAddAnnotationNotification object:self userInfo:[NSDictionary dictionaryWithObjectsAndKeys:page, CPDFListViewPageKey, annotation, CPDFListViewAnnotationKey, nil]];
- });
-
-
- if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewAddAnnotations:forAddAnnotations:inPage:)])
- [self.pdfListViewDelegate PDFListViewAddAnnotations:self forAddAnnotations:@[annotation] inPage:page];
- }
- - (void)removeAnnotation:(CPDFAnnotation *)annotation {
- if ([annotation isKindOfClass:[CPDFTextAnnotation class]] && self.popOver.isShown) {
- // if (annotation.contents.length <= 0) {
- CPDFAnnotation *wasAnnotation = annotation;
- CPDFPage *page = wasAnnotation.page;
-
- [[[self undoManager] prepareWithInvocationTarget:self] addAnnotation:wasAnnotation toPage:page];
-
- [page removeAnnotation:wasAnnotation];
- [self annotationsChangedOnPage:page];
- [self setNeedsDisplayAnnotation:wasAnnotation];
- dispatch_async(dispatch_get_main_queue(), ^{
- [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewDidRemoveAnnotationNotification object:self
- userInfo:[NSDictionary dictionaryWithObjectsAndKeys:wasAnnotation, CPDFListViewAnnotationKey, page, CPDFListViewPageKey, nil]];
- });
-
- if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewRemoveAnnotations:forRemoveAnnotations:inPage:)])
- [self.pdfListViewDelegate PDFListViewRemoveAnnotations:self forRemoveAnnotations:@[annotation] inPage:page];
- // }
- } else {
- CPDFAnnotation *wasAnnotation = annotation;
- CPDFPage *page = wasAnnotation.page;
-
- [[[self undoManager] prepareWithInvocationTarget:self] addAnnotation:wasAnnotation toPage:page];
-
- [page removeAnnotation:wasAnnotation];
- [self annotationsChangedOnPage:page];
- [self setNeedsDisplayAnnotation:wasAnnotation];
- dispatch_async(dispatch_get_main_queue(), ^{
- [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewDidRemoveAnnotationNotification object:self
- userInfo:[NSDictionary dictionaryWithObjectsAndKeys:wasAnnotation, CPDFListViewAnnotationKey, page, CPDFListViewPageKey, nil]];
- });
-
- if([self.pdfListViewDelegate respondsToSelector:@selector(PDFListViewRemoveAnnotations:forRemoveAnnotations:inPage:)])
- [self.pdfListViewDelegate PDFListViewRemoveAnnotations:self forRemoveAnnotations:@[annotation] inPage:page];
- }
- }
- - (void)drawPage:(CPDFPage *)pdfPage toContext:(CGContextRef)context {
-
- [super drawPage:pdfPage toContext:context];
-
- [self drawSelectionForPage:pdfPage inContext:context];
-
- BOOL isIncludText = NO;
- NSMutableArray *currentActiveAnnotations = [NSMutableArray array];
- NSMutableArray *currentPageAnnotations = [NSMutableArray array];
- for (CPDFAnnotation *an in self.activeAnnotations) {
- if([an.page isEqual:pdfPage]) {
- if([an isKindOfClass:[CPDFFreeTextAnnotation class]])
- isIncludText = YES;
-
- //多选注释框去除mark注释与便签注释
- if(!([an isKindOfClass:[CPDFMarkupAnnotation class]] || [an isKindOfClass:[CPDFTextAnnotation class]])) {
- [currentActiveAnnotations addObject:an];
- }
-
- [currentPageAnnotations addObject:an];
- }
- }
-
- if(currentActiveAnnotations.count > 1) {
- NSRect rect = [self selectionMultipleBoundsWithAnnotations:currentActiveAnnotations];
-
- CGFloat lineWidth = [self unitWidthOnPage:pdfPage];
- CGContextSaveGState(context);
- CGColorRef color = [CPDFListViewConfig defaultManager].annotationBorderColor.CGColor;
- CGContextSetStrokeColorWithColor(context, color);
- CGContextStrokeRectWithWidth(context, CGRectInset(NSRectToCGRect(rect), 0,0), lineWidth);
- CGContextRestoreGState(context);
- CGContextSaveGState(context);
- if(isIncludText) {
- CPDFListViewDrawFreeTextResizeHandle(context, rect, 4.0 * lineWidth, YES);
- } else {
- CPDFListViewDrawResizeHandles(context, rect, 4.0 * lineWidth, YES);
- }
- CGContextRestoreGState(context);
- }
-
- if(self.multiplAnnotationObject &&
- !CGRectIsEmpty(self.multiplAnnotationObject.drawRect) &&
- self.multiplAnnotationObject.page == pdfPage) {
- CGRect selectAnnotationBounds = self.multiplAnnotationObject.drawRect;
-
- NSRect rect = selectAnnotationBounds;
- CGFloat lineWidth = [self unitWidthOnPage:pdfPage];
- CGContextSaveGState(context);
- CGColorRef color = [CPDFListViewConfig defaultManager].annotationBorderColor.CGColor;
- CGContextSetStrokeColorWithColor(context, color);
- CGContextStrokeRectWithWidth(context, CGRectInset(NSRectToCGRect(rect), 0,0), lineWidth);
- CGContextRestoreGState(context);
- }
-
- [currentPageAnnotations enumerateObjectsUsingBlock:^(CPDFAnnotation *annotation, NSUInteger idx, BOOL * _Nonnull stop) {
- if (annotation.page && [annotation.page isEqual:pdfPage]) {
- [annotation drawSelectionHighlightForView:self inContext:context isHover:NO];
- }
- }];
-
- [self.selectAnnotations enumerateObjectsUsingBlock:^(CPDFAnnotation *annotation, NSUInteger idx, BOOL * _Nonnull stop) {
- if (annotation.page && [annotation.page isEqual:pdfPage]) {
- [annotation drawSelectionHighlightForView:self inContext:context isHover:NO];
- }
- }];
-
- if (self.hoverAnnotation.page && [self.hoverAnnotation.page isEqual:pdfPage] && ![self.activeAnnotations containsObject:self.hoverAnnotation]) {
- [self.hoverAnnotation drawSelectionHighlightForView:self inContext:context isHover:YES];
- }
-
- if(!CGRectEqualToRect(self.multiplSelectBounds, CGRectZero) && [self.multiplSelectPage isEqual:pdfPage]) {
- @synchronized (self) {
- CGContextSetLineCap(context, kCGLineCapRound);
- CGContextSetLineWidth(context, 1.0);
- CGContextSetStrokeColorWithColor(context, [CPDFListViewConfig defaultManager].annotationBorderColor.CGColor);
- CGContextBeginPath(context);
- CGContextMoveToPoint(context, self.multiplSelectBounds.origin.x, self.multiplSelectBounds.origin.y);
- CGContextAddLineToPoint(context, self.multiplSelectBounds.origin.x+ self.multiplSelectBounds.size.width,self.multiplSelectBounds.origin.y);
- CGContextAddLineToPoint(context, self.multiplSelectBounds.origin.x + self.multiplSelectBounds.size.width,self.multiplSelectBounds.origin.y + + self.multiplSelectBounds.size.height);
- CGContextAddLineToPoint(context, self.multiplSelectBounds.origin.x,self.multiplSelectBounds.origin.y + + self.multiplSelectBounds.size.height);
- CGContextClosePath(context);
- CGFloat lengths[] = {5,5};
- CGContextSetLineDash(context, 0, lengths,2);
- CGContextStrokePath(context);
- }
- }
-
- }
- - (void)drawSelectionForPage:(CPDFPage *)pdfPage inContext:(CGContextRef)context {
- NSRect rect;
- NSUInteger pageIndex;
- @synchronized (self) {
- pageIndex = self.selectionPageIndex;
- rect = self.selectionRect;
- }
- if (pageIndex != NSNotFound) {
- BOOL isWidget = NO;
- for (CPDFAnnotation * annotation in self.activeAnnotations) {
- if ([annotation isForm]) {
- isWidget = YES;
- break;
- }
- }
-
- if (isWidget &&
- _toolMode != CFormToolMode) {
- return;
- }
-
- NSRect bounds = [pdfPage boundsForBox:[self displayBox]];
- CGFloat radius = HANDLE_SIZE * [self unitWidthOnPage:pdfPage];
- CGColorRef color = CGColorCreateGenericGray(0.0, 0.6);
- CGContextSetFillColorWithColor(context, color);
- CGColorRelease(color);
- CGContextBeginPath(context);
- CGContextAddRect(context, NSRectToCGRect(bounds));
- CGContextAddRect(context, NSRectToCGRect(rect));
- CGContextEOFillPath(context);
- if ([pdfPage pageIndex] != pageIndex) {
- color = CGColorCreateGenericGray(0.0, 0.3);
- CGContextSetFillColorWithColor(context, color);
- CGColorRelease(color);
- CGContextFillRect(context, NSRectToCGRect(rect));
- }
- CPDFListViewDrawResizeHandles(context, rect, radius, YES);
- }
- }
- - (void)removeShapeLayer {
- if (![self.shapeLayerTopH isEqual:nil] && ![self.shapeLayerBottomH isEqual:nil] && ![self.shapeLayerLeftV isEqual:nil] && ![self.shapeLayerRightV isEqual:nil]) {
- [self.shapeLayerTopH removeFromSuperlayer];
- [self.shapeLayerBottomH removeFromSuperlayer];
- [self.shapeLayerLeftV removeFromSuperlayer];
- [self.shapeLayerRightV removeFromSuperlayer];
- self.shapeLayerTopH = nil;
- self.shapeLayerBottomH = nil;
- self.shapeLayerLeftV = nil;
- self.shapeLayerRightV = nil;
- }
- }
- - (BOOL)consistentTypeWithAnnotation:(CPDFAnnotation *)annotation {
- if (self.annotationType == CAnnotationTypeSignSignature) {
- if (![annotation isKindOfClass:[CPDFSignatureAnnotation class]])
- return NO;
- } else if (self.annotationType == CAnnotationTypeStamp) {
- if (![annotation isKindOfClass:[CPDFStampAnnotation class]])
- return NO;
- } else if (self.annotationType == CAnnotationTypeAnchored) {
- if (![annotation isKindOfClass:[CPDFTextAnnotation class]])
- return NO;
- } else if (self.annotationType == CAnnotationTypeLink) {
- if (![annotation isKindOfClass:[CPDFLinkAnnotation class]])
- return NO;
- } else if (self.annotationType == CAnnotationTypeFreeText) {
- if (![annotation isKindOfClass:[CPDFFreeTextAnnotation class]])
- return NO;
- } else if (self.annotationType == CAnnotationTypeLine || self.annotationType == CAnnotationTypeArrow || self.annotationType == CAnnotationTypeSquare || self.annotationType == CAnnotationTypeCircle) {
- if (![annotation isKindOfClass:[CPDFLineAnnotation class]] && ![annotation isKindOfClass:[CPDFSquareAnnotation class]] && ![annotation isKindOfClass:[CPDFCircleAnnotation class]])
- return NO;
- } else if (self.annotationType == CAnnotationTypeHighlight || self.annotationType == CAnnotationTypeUnderline || self.annotationType == CAnnotationTypeStrikeOut) {
- if (![annotation isKindOfClass:[CPDFMarkupAnnotation class]])
- return NO;
- } else if (self.annotationType == CAnnotationTypeInk) {
- if (![annotation isKindOfClass:[CPDFInkAnnotation class]])
- return NO;
- } else if (self.annotationType == CAnnotationTypeRadioButton) {
-
- } else if (self.annotationType == CAnnotationTypeCheckBox) {
-
- } else if (self.annotationType == CAnnotationTypeTextField) {
-
- } else if (self.annotationType == CAnnotationTypeComboBox) {
-
- } else if (self.annotationType == CAnnotationTypeListMenu) {
-
- } else if (self.annotationType == CAnnotationTypeActionButton) {
-
- } else if (self.annotationType == CAnnotationTypeSignature) {
-
- } else if (self.annotationType == CAnnotationTypeSignText) {
-
- } else if (self.annotationType == CAnnotationTypeSignFalse) {
-
- } else if (self.annotationType == CAnnotationTypeSignTure) {
-
- } else if (self.annotationType == CAnnotationTypeSignCircle) {
-
- } else if (self.annotationType == CAnnotationTypeSignLine) {
-
- } else if (self.annotationType == CAnnotationTypeSignDot) {
-
- } else if (self.annotationType == CAnnotationTypeSignConfig) {
-
- } else if (self.annotationType == CAnnotationTypeSignDate) {
-
- }
- return YES;
- }
- #pragma mark Zooming
- - (void)zoomToRect:(NSRect)rect onPage:(CPDFPage *)page {
- if (NSIsEmptyRect(rect) == NO) {
- BOOL isLegacy = [NSScroller respondsToSelector:@selector(preferredScrollerStyle)] == NO || [NSScroller preferredScrollerStyle] == NSScrollerStyleLegacy;
- NSRect bounds = [self bounds];
- CGFloat scale = 1.0;
- if (isLegacy) {
- bounds.size.width -= [NSScroller scrollerWidth];
- bounds.size.height -= [NSScroller scrollerWidth];
- }
- if (NSWidth(bounds) * NSHeight(rect) > NSWidth(rect) * NSHeight(bounds))
- scale = NSHeight(bounds) / NSHeight(rect);
- else
- scale = NSWidth(bounds) / NSWidth(rect);
- [self setScaleFactor:scale];
- NSScrollView *scrollView = [self scrollView];
- if (isLegacy && ([scrollView hasHorizontalScroller] == NO || [scrollView hasVerticalScroller] == NO)) {
- if ([scrollView hasVerticalScroller])
- bounds.size.width -= [NSScroller scrollerWidth];
- if ([scrollView hasHorizontalScroller])
- bounds.size.height -= [NSScroller scrollerWidth];
- if (NSWidth(bounds) * NSHeight(rect) > NSWidth(rect) * NSHeight(bounds))
- scale = NSHeight(bounds) / NSHeight(rect);
- else
- scale = NSWidth(bounds) / NSWidth(rect);
- [self setScaleFactor:scale];
- }
- [self goToRect:rect onPage:page];
-
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- CGPoint tPagePoint = CGPointMake(rect.origin.x, rect.origin.y+rect.size.height);
- [self goToTargetPoint:tPagePoint onPage:page atScrollPosition:CScrollPositionTop];
- });
- }
- }
- #pragma mark - NSPopoverDelegate
- - (void)popoverWillClose:(NSNotification *)notification {
- NSPopover *popover = notification.object;
-
- if([popover.contentViewController isKindOfClass:[CPDFListEditAnnotationViewController class]]) {
- CPDFListEditAnnotationViewController *listEditAnnotationViewController = (CPDFListEditAnnotationViewController *)popover.contentViewController;
- CPDFAnnotation *annotation = listEditAnnotationViewController.annotation;
- NSString *contensString = listEditAnnotationViewController.contentString;
- if([annotation isKindOfClass:[CPDFTextAnnotation class]]) {
- if(contensString && contensString.length > 0) {
- annotation.contents = listEditAnnotationViewController.contentString;
- [self setNeedsDisplayAnnotation:annotation];
- } else {
- if([self.activeAnnotations containsObject:annotation]) {
- [self.activeAnnotations removeObject:annotation];
- }
- [self removeAnnotation:annotation];
- }
- } else {
- annotation.contents = listEditAnnotationViewController.contentString;
- [self setNeedsDisplayAnnotation:annotation];
- }
- }
- self.popOver = nil;
- }
- - (void)handlePageChangedNotification:(NSNotification *)notification {
- self.copyCount = 0;
- }
- @end
|