123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981 |
- //
- // CPDFListView.m
- // compdfkit-tools
- //
- // Copyright © 2014-2023 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 "CPDFColorUtils.h"
- #import "CPDFSlider.h"
- #import "CPDFPageIndicatorView.h"
- typedef NS_ENUM(NSInteger, CPDFAnnotationDraggingType) {
- CPDFAnnotationDraggingNone = 0,
- CPDFAnnotationDraggingCenter,
- CPDFAnnotationDraggingTopLeft,
- CPDFAnnotationDraggingTopRight,
- CPDFAnnotationDraggingBottomLeft,
- CPDFAnnotationDraggingBottomRight,
- CPDFAnnotationDraggingStart,
- CPDFAnnotationDraggingEnd
- };
- @interface CPDFListView()
- @property (nonatomic, strong) CPDFPageIndicatorView * pageIndicatorView;
- @property (nonatomic, assign) CPDFAnnotationDraggingType draggingType;
- @property (nonatomic, assign) CGPoint draggingPoint;
- @property (nonatomic, assign) CGRect topLeftRect;
- @property (nonatomic, assign) CGRect bottomLeftRect;
- @property (nonatomic, assign) CGRect topRightRect;
- @property (nonatomic, assign) CGRect bottomRightRect;
- @property (nonatomic, assign) CGRect startPointRect;
- @property (nonatomic, assign) CGRect endPointRect;
- @property (nonatomic, assign) CGPoint addLinkPoint;
- @property (nonatomic, assign) CGRect addLinkRect;
- @property (nonatomic, strong) CPDFAnnotation *menuAnnotation;
- @property (nonatomic, assign) CGPoint menuPoint;
- @property (nonatomic, assign) CPDFPage *menuPage;
- @end
- @implementation CPDFListView
- #pragma mark - Initializers
- - (instancetype)initWithFrame:(CGRect)frame {
- if (self = [super initWithFrame:frame]) {
- self.backgroundColor = [UIColor lightGrayColor];
-
- [self commomInit];
-
- [self addNotification];
-
- }
- return self;
- }
- - (id)initWithCoder:(NSCoder *)decoder {
- self = [super initWithCoder:decoder];
- if (self) {
- self.backgroundColor = [UIColor lightGrayColor];
- [self commomInit];
- [self addNotification];
- }
- return self;
- }
- - (void)setAnnotationMode:(CPDFViewAnnotationMode)annotationMode {
- _annotationMode = annotationMode;
-
- if (CPDFViewAnnotationModeHighlight == annotationMode ||
- CPDFViewAnnotationModeUnderline == annotationMode ||
- CPDFViewAnnotationModeStrikeout == annotationMode ||
- CPDFViewAnnotationModeSquiggly == annotationMode) {
- self.textSelectionMode = YES;
- } else {
- self.textSelectionMode = NO;
- }
-
- if (CPDFViewAnnotationModeLink == annotationMode) {
- self.scrollEnabled = NO;
- [self endDrawing];
- } else if (CPDFViewAnnotationModeInk == annotationMode) {
- self.scrollEnabled = NO;
- [self beginDrawing];
- } else {
- if (self.activeAnnotation) {
- self.scrollEnabled = NO;
- } else {
- self.scrollEnabled = YES;
- }
- [self endDrawing];
- [self becomeFirstResponder];
- }
-
- if (CPDFViewAnnotationModeNone != annotationMode) {
- CPDFPage *page = self.activeAnnotation.page;
- self.activeAnnotation = nil;
- [self setNeedsDisplayForPage:page];
- }
- }
- - (void)updateScrollEnabled {
- if (self.activeAnnotation) {
- self.scrollEnabled = NO;
- } else {
- if (CPDFViewAnnotationModeLink == self.annotationMode) {
- self.scrollEnabled = NO;
- } else {
- self.scrollEnabled = YES;
- }
- }
- }
- - (NSString *)annotationUserName {
- NSString *annotationUserName = CPDFKitShareConfig.annotationAuthor;
- if (!annotationUserName || [annotationUserName length] <= 0) {
- annotationUserName = [[UIDevice currentDevice] name];
- }
- return annotationUserName ? : @"";
- }
- - (UIImage *)compressImage:(UIImage *)image size:(CGSize)size {
- CGFloat imageScale = 1.0;
- if (image.size.width > size.width || image.size.height > size.height) {
- imageScale = MIN(size.width / image.size.width, size.height / image.size.height);
- }
- CGSize newSize = CGSizeMake(image.size.width * imageScale, image.size.height * imageScale);
- UIGraphicsBeginImageContext(newSize);
- [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
- UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return newImage;
- }
- #pragma mark - Touch
- - (void)touchBeganAtPoint:(CGPoint)point forPage:(CPDFPage *)page {
- if (CToolModelAnnotation == self.toolModel) {
-
- } else if (CToolModelForm == self.toolModel) {
-
- } else if (CToolModelEdit == self.toolModel) {
-
- } else {
- }
-
- if (self.textSelectionMode) {
- } else {
- self.addLinkPoint = point;
- self.addLinkRect = CGRectZero;
-
- self.draggingType = CPDFAnnotationDraggingNone;
- if (!self.activeAnnotation || self.activeAnnotation.page != page) {
- return;
- }
- CGRect topLeftRect = CGRectInset(self.topLeftRect, -5, -5);
- CGRect bottomLeftRect = CGRectInset(self.bottomLeftRect, -5, -5);
- CGRect topRightRect = CGRectInset(self.topRightRect, -5, -5);
- CGRect bottomRightRect = CGRectInset(self.bottomRightRect, -5, -5);
- CGRect startPointRect = CGRectInset(self.startPointRect, -5, -5);
- CGRect endPointRect = CGRectInset(self.endPointRect, -5, -5);
- if (CGRectContainsPoint(topLeftRect, point)) {
- self.draggingType = CPDFAnnotationDraggingBottomLeft;
- } else if (CGRectContainsPoint(topRightRect, point)) {
- self.draggingType = CPDFAnnotationDraggingTopRight;
- } else if (CGRectContainsPoint(bottomRightRect, point)) {
- self.draggingType = CPDFAnnotationDraggingBottomRight;
- } else if (CGRectContainsPoint(startPointRect, point)) {
- self.draggingType = CPDFAnnotationDraggingStart;
- } else if (CGRectContainsPoint(endPointRect, point)) {
- self.draggingType = CPDFAnnotationDraggingEnd;
- } else if ([page annotation:self.activeAnnotation atPoint:point]) {
- self.draggingType = CPDFAnnotationDraggingCenter;
- }
- self.draggingPoint = point;
-
- if (CPDFAnnotationDraggingCenter == self.draggingType) {
- if ([self.activeAnnotation isKindOfClass:[CPDFFreeTextAnnotation class]] ||
- [self.activeAnnotation isKindOfClass:[CPDFSignatureAnnotation class]]) {
- // [self addMagnifierAtPoint:point forPage:page];
- }
- }
- }
- }
- - (void)touchMovedAtPoint:(CGPoint)point forPage:(CPDFPage *)page {
- if (CToolModelAnnotation == self.toolModel) {
-
- } else if (CToolModelForm == self.toolModel) {
-
- } else if (CToolModelEdit == self.toolModel) {
-
- } else {
- }
-
- if (self.textSelectionMode) {
-
- } else if (CPDFAnnotationDraggingNone != self.draggingType) {
- [self moveAnnotation:self.activeAnnotation fromPoint:self.draggingPoint toPoint:point forType:self.draggingType];
- [self setNeedsDisplayForPage:page];
- self.draggingPoint = point;
-
- if (CPDFAnnotationDraggingCenter == self.draggingType) {
- if ([self.activeAnnotation isKindOfClass:[CPDFFreeTextAnnotation class]] ||
- [self.activeAnnotation isKindOfClass:[CPDFSignatureAnnotation class]]) {
- // [self addMagnifierAtPoint:point forPage:page];
- }
- }
- } else if (CPDFViewAnnotationModeLink == self.annotationMode) {
- CGRect rect = CGRectZero;
- if (point.x > self.addLinkPoint.x) {
- rect.origin.x = self.addLinkPoint.x;
- rect.size.width = point.x-self.addLinkPoint.x;
- } else {
- rect.origin.x = point.x;
- rect.size.width = self.addLinkPoint.x-point.x;
- }
- if (point.y > self.addLinkPoint.y) {
- rect.origin.y = self.addLinkPoint.y;
- rect.size.height = point.y-self.addLinkPoint.y;
- } else {
- rect.origin.y = point.y;
- rect.size.height = self.addLinkPoint.y-point.y;
- }
- self.addLinkRect = rect;
- [self setNeedsDisplayForPage:page];
- }
- }
- - (void)touchEndedAtPoint:(CGPoint)point forPage:(CPDFPage *)page {
- if (CToolModelAnnotation == self.toolModel) {
-
- } else if (CToolModelForm == self.toolModel) {
-
- } else if (CToolModelEdit == self.toolModel) {
-
- } else {
- }
- if (self.textSelectionMode) {
- if (self.currentSelection) {
- [self addAnnotation:self.annotationMode atPoint:point forPage:page];
- } else {
- CPDFAnnotation *annotation = [page annotationAtPoint:point];
- if ([annotation isKindOfClass:[CPDFMarkupAnnotation class]]) {
- // [self showMenuForAnnotation:annotation];
- }
- }
- } else if (CPDFAnnotationDraggingNone == self.draggingType) {
- if (self.activeAnnotation) {
- CPDFPage *previousPage = self.activeAnnotation.page;
- self.activeAnnotation = nil;
- [self setNeedsDisplayForPage:previousPage];
- [self updateScrollEnabled];
- } else {
- if (CPDFViewAnnotationModeNone == self.annotationMode) {
- CPDFAnnotation *annotation = [page annotationAtPoint:point];
- if ([annotation isKindOfClass:[CPDFTextAnnotation class]]) {
- if ([self.performDelegate respondsToSelector:@selector(PDFViewPerformOpenNote:forAnnotation:)]) {
- [self.performDelegate PDFViewPerformOpenNote:self forAnnotation:annotation];
- }
- } else if ([annotation isKindOfClass:[CPDFMarkupAnnotation class]]) {
- // if (CGRectContainsPoint(annotation.bounds, point)) {
- // [self showMenuForAnnotation:annotation];
- // } else {
- // if ([self.performDelegate respondsToSelector:@selector(PDFViewPerformPopup:forAnnotation:)]) {
- // [self.performDelegate PDFViewPerformPopup:self forAnnotation:(CPDFMarkupAnnotation *)annotation];
- // }
- // }
- } else if ([annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
- [super touchEndedAtPoint:point forPage:page];
- } else if ([annotation isKindOfClass:[CPDFMovieAnnotation class]]) {
- [super touchEndedAtPoint:point forPage:page];
- } else if ([annotation isKindOfClass:[CPDFWidgetAnnotation class]]) {
- if ([annotation isKindOfClass:[CPDFSignatureWidgetAnnotation class]]) {
- // if ([(CPDFSignatureWidgetAnnotation *)annotation isSigned]) {
- // [self showMenuForAnnotation:annotation];
- // } else {
- // if ([self.performDelegate respondsToSelector:@selector(PDFViewPerformSignatureWidget:forAnnotation:)]) {
- // [self.performDelegate PDFViewPerformSignatureWidget:self forAnnotation:(CPDFSignatureWidgetAnnotation *)annotation];
- // }
- // }
- } else {
- [super touchEndedAtPoint:point forPage:page];
- }
- } else {
- self.activeAnnotation = annotation;
- [self setNeedsDisplayForPage:page];
- [self updateScrollEnabled];
-
- // [self showMenuForAnnotation:annotation];
- //
- if (!self.activeAnnotation) {
- if ([self.performDelegate respondsToSelector:@selector(PDFViewPerformTouchEnded:)]) {
- [self.performDelegate PDFViewPerformTouchEnded:self];
- }
- }
- }
- } else if (CPDFViewAnnotationModeLink == self.annotationMode) {
- // if (CGRectIsEmpty(self.addLinkRect)) {
- // CPDFAnnotation *annotation = [page annotationAtPoint:point];
- // if ([annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
- // self.activeAnnotation = annotation;
- // [self setNeedsDisplayForPage:page];
- // [self updateScrollEnabled];
- //
- // [self showMenuForAnnotation:annotation];
- // }
- // } else {
- // [self addAnnotationLinkAtPoint:point forPage:page];
- // }
- } else if (CPDFViewAnnotationModeFreeText == self.annotationMode) {
- // [self addAnnotationFreeTextAtPoint:point forPage:page];
- } else if (CPDFViewAnnotationModeStamp == self.annotationMode) {
- // [self addAnnotationAtPoint:point forPage:page];
- if ([self.performDelegate respondsToSelector:@selector(PDFViewPerformTouchEnded:)]) {
- [self.performDelegate PDFViewPerformTouchEnded:self];
- }
- } else if (CPDFViewAnnotationModeImage == self.annotationMode) {
- // [self addAnnotationAtPoint:point forPage:page];
- if ([self.performDelegate respondsToSelector:@selector(PDFViewPerformTouchEnded:)]) {
- [self.performDelegate PDFViewPerformTouchEnded:self];
- }
- } else {
- [self addAnnotation:self.annotationMode atPoint:point forPage:page];
- }
- }
- } else {
- if (CPDFAnnotationDraggingCenter != self.draggingType) {
- if ([self.activeAnnotation isKindOfClass:[CPDFFreeTextAnnotation class]] ||
- [self.activeAnnotation isKindOfClass:[CPDFStampAnnotation class]] ||
- [self.activeAnnotation isKindOfClass:[CPDFSignatureAnnotation class]]) {
- [self.activeAnnotation updateAppearanceStream];
- [self setNeedsDisplayForPage:page];
- }
- }
-
- self.draggingType = CPDFAnnotationDraggingNone;
-
- // [self showMenuForAnnotation:self.activeAnnotation];
- }
- // [self removeMagnifier];
- }
- - (void)touchCancelledAtPoint:(CGPoint)point forPage:(CPDFPage *)page {
- if (CToolModelAnnotation == self.toolModel) {
-
- } else if (CToolModelForm == self.toolModel) {
-
- } else if (CToolModelEdit == self.toolModel) {
-
- } else {
- }
-
- self.draggingType = CPDFAnnotationDraggingNone;
- }
- - (NSArray<UIMenuItem *> *)menuItemsAtPoint:(CGPoint)point forPage:(CPDFPage *)page {
- if([self.performDelegate respondsToSelector:@selector(PDFListView:customizeMenuForPage:forPagePoint:)])
- return [self.performDelegate PDFListView:self customizeMenuForPage:page forPagePoint:point];
- return [super menuItemsAtPoint:point forPage:page];
- }
- #pragma mark - Private method
- - (void)commomInit {
- _pageSliderView = [[CPDFSlider alloc] initWithPDFView:self];
- _pageSliderView.frame = CGRectMake(self.bounds.size.width-22, 0, 22, self.bounds.size.height);
- _pageSliderView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin;
- [self addSubview:self.pageSliderView];
-
- _pageIndicatorView = [[CPDFPageIndicatorView alloc] init];
-
- __weak typeof(self) weakSelf = self;
- _pageIndicatorView.touchCallBack = ^{
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"Go to page", nil) preferredStyle:UIAlertControllerStyleAlert];
-
- [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- }]];
-
- [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Done", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- UITextField *pageTextField = alertController.textFields.firstObject;
- NSInteger pageIndex = [pageTextField.text integerValue] - 1;
-
- if (pageIndex > weakSelf.document.pageCount){
- pageIndex = weakSelf.document.pageCount;
- } else if(pageIndex<0){
- pageIndex = 0;
- } else if(pageTextField.text.length == 0){
- pageIndex = (int)weakSelf.currentPageIndex;
- }
-
- [weakSelf goToPageIndex:pageIndex animated:YES];
- }]];
-
- [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
-
- textField.placeholder = NSLocalizedString(@"Enter a page number", nil);
- textField.keyboardType = UIKeyboardTypeNumberPad;
-
- }];
-
- UIViewController *tRootViewControl = [UIApplication sharedApplication].keyWindow.rootViewController;
- if ([tRootViewControl presentedViewController]) {
- tRootViewControl = [tRootViewControl presentedViewController];
- }
-
- [tRootViewControl presentViewController:alertController animated:true completion:nil];
- };
- }
- - (void)addNotification {
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(documentChangedNotification:) name:CPDFViewDocumentChangedNotification object:nil];
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pageChangedNotification:) name:CPDFViewPageChangedNotification object:nil];
- }
- - (void)showPageNumIndicator {
- __weak typeof(self) weakSelf = self;
- if(![self.pageIndicatorView superview])
- [self.pageIndicatorView showInView:weakSelf position:CPDFPageIndicatorViewPositionLeftBottom];
- [self.pageIndicatorView updatePageCount:weakSelf.document.pageCount currentPageIndex:self.currentPageIndex + 1];
- }
- #pragma mark - NotificationCenter
- - (void)documentChangedNotification:(NSNotification *)notification {
- CPDFView *pdfview = notification.object;
- if (pdfview.document == self.document) {
- [self showPageNumIndicator];
- [self.pageSliderView reloadData];
- }
- }
- - (void)pageChangedNotification:(NSNotification *)notification {
- CPDFView *pdfview = notification.object;
- if (pdfview.document == self.document) {
- [self showPageNumIndicator];
- [self.pageSliderView reloadData];
- }
- }
- #pragma mark - Rendering
- - (void)drawPage:(CPDFPage *)page toContext:(CGContextRef)context {
- if (CPDFViewAnnotationModeLink == self.annotationMode) {
- CGContextSetLineWidth(context, 1.0);
- CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:255.0/255.f green:255.0/255.f blue:255.0/255.f alpha:0.8].CGColor);
- CGContextSetFillColorWithColor(context, [UIColor colorWithRed:100.0/255.f green:149.0/255.f blue:237.0/255.f alpha:0.4].CGColor);
- CGContextAddRect(context, self.addLinkRect);
- CGContextDrawPath(context, kCGPathFillStroke);
- }
-
- if (self.activeAnnotation.page != page) {
- return;
- }
- CGSize dragDotSize = CGSizeMake(30, 30);
- CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:72.0/255.0 green:183.0/255.0 blue:247.0/255.0 alpha:1.0].CGColor);
- if ([self.activeAnnotation isKindOfClass:[CPDFLineAnnotation class]]) {
- CPDFLineAnnotation *line = (CPDFLineAnnotation *)self.activeAnnotation;
- CGPoint startPoint = line.startPoint;
- CGPoint endPoint = line.endPoint;
-
- CGPoint tStartPoint = startPoint;
- CGPoint tEndPoint = endPoint;
-
- float final = 40;
- if (fabs(tStartPoint.x - tEndPoint.x) < 0.00001) {
- if (tStartPoint.y > tEndPoint.y) {
- tStartPoint.y += final;
- tEndPoint.y -= final;
- } else {
- tStartPoint.y -= final;
- tEndPoint.y += final;
- }
- } else if (fabs(tStartPoint.y - tEndPoint.y) < 0.00001) {
- if (tStartPoint.x > tEndPoint.x) {
- tStartPoint.x += final;
- tEndPoint.x -= final;
- } else {
- tStartPoint.x -= final;
- tEndPoint.x += final;
- }
- } else {
- double k = (tEndPoint.y - tStartPoint.y)/(tEndPoint.x - tStartPoint.x);
- double atank = atan(k);
- if (endPoint.x > startPoint.x) {
- tEndPoint.x += cos(atank) * final;
- tEndPoint.y += sin(atank) * final;
- tStartPoint.x -= cos(atank) * final;
- tStartPoint.y -= sin(atank) * final;
- } else {
- tEndPoint.x -= cos(atank) * final;
- tEndPoint.y -= sin(atank) * final;
- tStartPoint.x += cos(atank) * final;
- tStartPoint.y += sin(atank) * final;
- }
- }
-
- CGContextSetLineWidth(context, 1.0);
- CGFloat dashArray[] = {3,3};
- CGContextSetLineDash(context, 0, dashArray, 2);
- CGContextMoveToPoint(context, tStartPoint.x, tStartPoint.y);
- CGContextAddLineToPoint(context, startPoint.x, startPoint.y);
- CGContextStrokePath(context);
- CGContextMoveToPoint(context, tEndPoint.x, tEndPoint.y);
- CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
- CGContextStrokePath(context);
-
- CGRect startPointRect = CGRectMake(tStartPoint.x - dragDotSize.width/2.0,
- tStartPoint.y - dragDotSize.height/2.0,
- dragDotSize.width, dragDotSize.height);
- CGRect endPointRect = CGRectMake(tEndPoint.x - dragDotSize.width/2.0,
- tEndPoint.y - dragDotSize.height/2.0,
- dragDotSize.width, dragDotSize.height);
-
- UIImage *image = [UIImage imageNamed:@"annotation_drag_dot.png" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil];
- CGImageRef dragDotImage = image.CGImage;
-
- CGContextDrawImage(context, startPointRect, dragDotImage);
- CGContextDrawImage(context, endPointRect, dragDotImage);
-
- self.startPointRect = startPointRect;
- self.endPointRect = endPointRect;
- } else if ([self.activeAnnotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
- CGRect rect = CGRectInset(self.activeAnnotation.bounds, -dragDotSize.width/2.0, -dragDotSize.height/2.0);
- CGContextSetLineWidth(context, 1.0);
- CGFloat lengths[] = {6, 6};
- CGContextSetLineDash(context, 0, lengths, 2);
- CGContextStrokeRect(context, rect);
- CGContextStrokePath(context);
-
- CGAffineTransform transform = [page transform];
- if (CPDFKitShareConfig.enableAnnotationNoRotate) {
- rect = CGRectApplyAffineTransform(rect, transform);
- }
- CGRect leftCenterRect = CGRectMake(CGRectGetMinX(rect)-dragDotSize.width/2.0,
- CGRectGetMidY(rect)-dragDotSize.height/2.0,
- dragDotSize.width, dragDotSize.height);
- CGRect rightCenterRect = CGRectMake(CGRectGetMaxX(rect)-dragDotSize.width/2.0,
- CGRectGetMidY(rect)-dragDotSize.height/2.0,
- dragDotSize.width, dragDotSize.height);
- if (CPDFKitShareConfig.enableAnnotationNoRotate) {
- leftCenterRect = CGRectApplyAffineTransform(leftCenterRect, CGAffineTransformInvert(transform));
- rightCenterRect = CGRectApplyAffineTransform(rightCenterRect, CGAffineTransformInvert(transform));
- }
-
- UIImage *image = [UIImage imageNamed:@"annotation_drag_dot.png" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil];
- CGImageRef dragDotImage = image.CGImage;
-
- CGContextDrawImage(context, leftCenterRect, dragDotImage);
- CGContextDrawImage(context, rightCenterRect, dragDotImage);
-
- self.startPointRect = leftCenterRect;
- self.endPointRect = rightCenterRect;
- } else {
- CGRect rect = CGRectInset(self.activeAnnotation.bounds, -dragDotSize.width/2.0, -dragDotSize.height/2.0);
- CGContextSetLineWidth(context, 1.0);
- CGFloat lengths[] = {6, 6};
- CGContextSetLineDash(context, 0, lengths, 2);
- CGContextStrokeRect(context, rect);
- CGContextStrokePath(context);
-
- if ([self.activeAnnotation isKindOfClass:[CPDFSoundAnnotation class]] ||
- [self.activeAnnotation isKindOfClass:[CPDFMovieAnnotation class]]) {
- return;
- }
-
- CGRect topLeftRect = CGRectMake(CGRectGetMinX(rect)-dragDotSize.width/2.0,
- CGRectGetMaxY(rect)-dragDotSize.height/2.0,
- dragDotSize.width, dragDotSize.height);
- CGRect bottomLeftRect = CGRectMake(CGRectGetMinX(rect)-dragDotSize.width/2.0,
- CGRectGetMinY(rect)-dragDotSize.height/2.0,
- dragDotSize.width, dragDotSize.height);
- CGRect topRightRect = CGRectMake(CGRectGetMaxX(rect)-dragDotSize.width/2.0,
- CGRectGetMaxY(rect)-dragDotSize.height/2.0,
- dragDotSize.width, dragDotSize.height);
- CGRect bottomRightRect = CGRectMake(CGRectGetMaxX(rect)-dragDotSize.width/2.0,
- CGRectGetMinY(rect)-dragDotSize.height/2.0,
- dragDotSize.width, dragDotSize.height);
-
- UIImage *image = [UIImage imageNamed:@"annotation_drag_dot.png" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil];
- CGImageRef dragDotImage = image.CGImage;
-
- CGContextDrawImage(context, topLeftRect, dragDotImage);
- CGContextDrawImage(context, bottomLeftRect, dragDotImage);
- CGContextDrawImage(context, topRightRect, dragDotImage);
- CGContextDrawImage(context, bottomRightRect, dragDotImage);
-
- self.topLeftRect = topLeftRect;
- self.bottomLeftRect = bottomLeftRect;
- self.topRightRect = topRightRect;
- self.bottomRightRect = bottomRightRect;
- }
- }
- #pragma mark - Annotation
- - (void)moveAnnotation:(CPDFAnnotation *)annotation fromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint forType:(CPDFAnnotationDraggingType)draggingType {
- CGRect bounds = annotation.bounds;
- CGPoint offsetPoint = CGPointMake(toPoint.x - fromPoint.x, toPoint.y - fromPoint.y);
- CGFloat scale = bounds.size.height/bounds.size.width;
- if ([annotation isKindOfClass:[CPDFLineAnnotation class]]) {
- CPDFLineAnnotation *line = (CPDFLineAnnotation *)annotation;
- CGPoint startPoint = line.startPoint;
- CGPoint endPoint = line.endPoint;
- switch (draggingType) {
- case CPDFAnnotationDraggingCenter:
- {
- startPoint.x += offsetPoint.x;
- startPoint.y += offsetPoint.y;
- endPoint.x += offsetPoint.x;
- endPoint.y += offsetPoint.y;
- }
- break;
- case CPDFAnnotationDraggingStart:
- {
- startPoint.x += offsetPoint.x;
- startPoint.y += offsetPoint.y;
- }
- break;
- case CPDFAnnotationDraggingEnd:
- {
- endPoint.x += offsetPoint.x;
- endPoint.y += offsetPoint.y;
- }
- break;
- default:
- break;
- }
- line.startPoint = startPoint;
- line.endPoint = endPoint;
- bounds = line.bounds;
- } else if ([annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
- CGAffineTransform transform = [annotation.page transform];
- if (CPDFKitShareConfig.enableAnnotationNoRotate) {
- bounds = CGRectApplyAffineTransform(bounds, transform);
- toPoint = CGPointApplyAffineTransform(toPoint, transform);
- fromPoint = CGPointApplyAffineTransform(fromPoint, transform);
- offsetPoint = CGPointMake(toPoint.x - fromPoint.x, toPoint.y - fromPoint.y);
- }
- CPDFFreeTextAnnotation *freeText = (CPDFFreeTextAnnotation *)annotation;
- NSDictionary *attributes = @{NSFontAttributeName : freeText.font};
- switch (draggingType) {
- case CPDFAnnotationDraggingCenter:
- {
- bounds.origin.x += offsetPoint.x;
- bounds.origin.y += offsetPoint.y;
- }
- break;
- case CPDFAnnotationDraggingStart:
- {
- CGFloat x = CGRectGetMaxX(bounds);
- bounds.size.width -= offsetPoint.x;
- bounds.size.width = MAX(bounds.size.width, 5.0);
- bounds.origin.x = x - bounds.size.width;
-
- CGRect rect = [freeText.contents boundingRectWithSize:CGSizeMake(bounds.size.width, CGFLOAT_MAX)
- options:NSStringDrawingUsesLineFragmentOrigin
- attributes:attributes
- context:nil];
- bounds.origin.y = CGRectGetMaxY(bounds) - rect.size.height;
- bounds.size.height = rect.size.height;
- }
- break;
- case CPDFAnnotationDraggingEnd:
- {
- bounds.size.width += offsetPoint.x;
- bounds.size.width = MAX(bounds.size.width, 5.0);
-
- CGRect rect = [freeText.contents boundingRectWithSize:CGSizeMake(bounds.size.width, CGFLOAT_MAX)
- options:NSStringDrawingUsesLineFragmentOrigin
- attributes:attributes
- context:nil];
- bounds.origin.y = CGRectGetMaxY(bounds) - rect.size.height;
- bounds.size.height = rect.size.height;
- }
- break;
- default:
- break;
- }
- if (CPDFKitShareConfig.enableAnnotationNoRotate) {
- bounds = CGRectApplyAffineTransform(bounds, CGAffineTransformInvert(transform));
- }
- } else {
- switch (draggingType) {
- case CPDFAnnotationDraggingCenter:
- {
- bounds.origin.x += offsetPoint.x;
- bounds.origin.y += offsetPoint.y;
- }
- break;
- case CPDFAnnotationDraggingTopLeft:
- {
- CGFloat x = CGRectGetMaxX(bounds);
- bounds.size.width -= offsetPoint.x;
- bounds.size.height += offsetPoint.y;
-
- if ([annotation isKindOfClass:[CPDFStampAnnotation class]] ||
- [annotation isKindOfClass:[CPDFSignatureAnnotation class]] ||
- [annotation isKindOfClass:[CPDFInkAnnotation class]]) {
- bounds.size.height = bounds.size.width*scale;
- bounds.size.width = MAX(bounds.size.width, 5.0);
- bounds.size.height = MAX(bounds.size.height, 5.0*scale);
- } else {
- bounds.size.width = MAX(bounds.size.width, 5.0);
- bounds.size.height = MAX(bounds.size.height, 5.0);
- }
-
- bounds.origin.x = x - bounds.size.width;
- }
- break;
- case CPDFAnnotationDraggingBottomLeft:
- {
- CGFloat x = CGRectGetMaxX(bounds);
- CGFloat y = CGRectGetMaxY(bounds);
- bounds.size.width -= offsetPoint.x;
- bounds.size.height -= offsetPoint.y;
-
- if ([annotation isKindOfClass:[CPDFStampAnnotation class]] ||
- [annotation isKindOfClass:[CPDFSignatureAnnotation class]] ||
- [annotation isKindOfClass:[CPDFInkAnnotation class]]) {
- bounds.size.height = bounds.size.width*scale;
- bounds.size.width = MAX(bounds.size.width, 5.0);
- bounds.size.height = MAX(bounds.size.height, 5.0*scale);
- } else {
- bounds.size.width = MAX(bounds.size.width, 5.0);
- bounds.size.height = MAX(bounds.size.height, 5.0);
- }
-
- bounds.origin.x = x - bounds.size.width;
- bounds.origin.y = y - bounds.size.height;
- }
- break;
- case CPDFAnnotationDraggingTopRight:
- {
- bounds.size.width += offsetPoint.x;
- bounds.size.height += offsetPoint.y;
-
- if ([annotation isKindOfClass:[CPDFStampAnnotation class]] ||
- [annotation isKindOfClass:[CPDFSignatureAnnotation class]] ||
- [annotation isKindOfClass:[CPDFInkAnnotation class]]) {
- bounds.size.height = bounds.size.width*scale;
- bounds.size.width = MAX(bounds.size.width, 5.0);
- bounds.size.height = MAX(bounds.size.height, 5.0*scale);
- } else {
- bounds.size.width = MAX(bounds.size.width, 5.0);
- bounds.size.height = MAX(bounds.size.height, 5.0);
- }
- }
- break;
- case CPDFAnnotationDraggingBottomRight:
- {
- CGFloat y = CGRectGetMaxY(bounds);
- bounds.size.width += offsetPoint.x;
- bounds.size.height -= offsetPoint.y;
-
- if ([annotation isKindOfClass:[CPDFStampAnnotation class]] ||
- [annotation isKindOfClass:[CPDFSignatureAnnotation class]] ||
- [annotation isKindOfClass:[CPDFInkAnnotation class]]) {
- bounds.size.height = bounds.size.width*scale;
- bounds.size.width = MAX(bounds.size.width, 5.0);
- bounds.size.height = MAX(bounds.size.height, 5.0*scale);
- } else {
- bounds.size.width = MAX(bounds.size.width, 5.0);
- bounds.size.height = MAX(bounds.size.height, 5.0);
- }
-
- bounds.origin.y = y - bounds.size.height;
- }
- break;
- default:
- break;
- }
-
- if (CGRectGetMinX(bounds) < 0) {
- bounds.origin.x = 0;
- }
- if (CGRectGetMaxX(bounds) > CGRectGetWidth(annotation.page.bounds)) {
- bounds.origin.x = CGRectGetWidth(annotation.page.bounds) - CGRectGetWidth(bounds);
- }
- if (CGRectGetMinY(bounds) < 0) {
- bounds.origin.y = 0;
- }
- if (CGRectGetMaxY(bounds) > CGRectGetHeight(annotation.page.bounds)) {
- bounds.origin.y = CGRectGetHeight(annotation.page.bounds) - CGRectGetHeight(bounds);
- }
- }
- annotation.bounds = bounds;
- }
- - (void)addAnnotation:(CPDFViewAnnotationMode)mode atPoint:(CGPoint)point forPage:(CPDFPage *)page {
- CPDFAnnotation *annotation = nil;
- switch (mode) {
- case CPDFViewAnnotationModeNote:
- {
- CGFloat width = 57.0/1.5;
- annotation = [[CPDFTextAnnotation alloc] initWithDocument:self.document];
- annotation.bounds = CGRectMake(point.x-width/2.0, point.y-width/2.0, width, width);
- }
- break;
- case CPDFViewAnnotationModeHighlight:
- {
- if (!self.currentSelection) {
- return;
- }
- NSMutableArray *quadrilateralPoints = [NSMutableArray array];
- annotation = [[CPDFMarkupAnnotation alloc] initWithDocument:self.document markupType:CPDFMarkupTypeHighlight];
- for (CPDFSelection *selection in self.currentSelection.selectionsByLine) {
- CGRect bounds = selection.bounds;
- [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(bounds), CGRectGetMaxY(bounds))]];
- [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))]];
- [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(bounds), CGRectGetMinY(bounds))]];
- [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMinY(bounds))]];
- }
- [(CPDFMarkupAnnotation *)annotation setQuadrilateralPoints:quadrilateralPoints];
- [(CPDFMarkupAnnotation *)annotation setMarkupText:self.currentSelection.string];
- [self clearSelection];
- }
- break;
- case CPDFViewAnnotationModeUnderline:
- {
- if (!self.currentSelection) {
- return;
- }
- NSMutableArray *quadrilateralPoints = [NSMutableArray array];
- annotation = [[CPDFMarkupAnnotation alloc] initWithDocument:self.document markupType:CPDFMarkupTypeUnderline];
- for (CPDFSelection *selection in self.currentSelection.selectionsByLine) {
- CGRect bounds = selection.bounds;
- [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(bounds), CGRectGetMaxY(bounds))]];
- [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))]];
- [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(bounds), CGRectGetMinY(bounds))]];
- [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMinY(bounds))]];
- }
- [(CPDFMarkupAnnotation *)annotation setQuadrilateralPoints:quadrilateralPoints];
- [(CPDFMarkupAnnotation *)annotation setMarkupText:self.currentSelection.string];
- [self clearSelection];
- }
- break;
- case CPDFViewAnnotationModeStrikeout:
- {
- if (!self.currentSelection) {
- return;
- }
- NSMutableArray *quadrilateralPoints = [NSMutableArray array];
- annotation = [[CPDFMarkupAnnotation alloc] initWithDocument:self.document markupType:CPDFMarkupTypeStrikeOut];
- for (CPDFSelection *selection in self.currentSelection.selectionsByLine) {
- CGRect bounds = selection.bounds;
- [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(bounds), CGRectGetMaxY(bounds))]];
- [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))]];
- [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(bounds), CGRectGetMinY(bounds))]];
- [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMinY(bounds))]];
- }
- [(CPDFMarkupAnnotation *)annotation setQuadrilateralPoints:quadrilateralPoints];
- [(CPDFMarkupAnnotation *)annotation setMarkupText:self.currentSelection.string];
- [self clearSelection];
- }
- break;
- case CPDFViewAnnotationModeSquiggly:
- {
- if (!self.currentSelection) {
- return;
- }
- NSMutableArray *quadrilateralPoints = [NSMutableArray array];
- annotation = [[CPDFMarkupAnnotation alloc] initWithDocument:self.document markupType:CPDFMarkupTypeSquiggly];
- for (CPDFSelection *selection in self.currentSelection.selectionsByLine) {
- CGRect bounds = selection.bounds;
- [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(bounds), CGRectGetMaxY(bounds))]];
- [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))]];
- [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(bounds), CGRectGetMinY(bounds))]];
- [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMinY(bounds))]];
- }
- [(CPDFMarkupAnnotation *)annotation setQuadrilateralPoints:quadrilateralPoints];
- [(CPDFMarkupAnnotation *)annotation setMarkupText:self.currentSelection.string];
- [self clearSelection];
- }
- break;
- case CPDFViewAnnotationModeCircle:
- {
- annotation = [[CPDFCircleAnnotation alloc] initWithDocument:self.document];
- annotation.bounds = CGRectMake(point.x-50, point.y-50, 100, 100);
- }
- break;
- case CPDFViewAnnotationModeSquare:
- {
- annotation = [[CPDFSquareAnnotation alloc] initWithDocument:self.document];
- annotation.bounds = CGRectMake(point.x-50, point.y-50, 100, 100);
- }
- break;
- case CPDFViewAnnotationModeArrow:
- {
- annotation = [[CPDFLineAnnotation alloc] initWithDocument:self.document];
- [(CPDFLineAnnotation *)annotation setStartPoint:CGPointMake(point.x-50, point.y)];
- [(CPDFLineAnnotation *)annotation setEndPoint:CGPointMake(point.x+50, point.y)];
- [(CPDFLineAnnotation *)annotation setEndLineStyle:CPDFLineStyleClosedArrow];
- }
- break;
- case CPDFViewAnnotationModeLine:
- {
- annotation = [[CPDFLineAnnotation alloc] initWithDocument:self.document];
- [(CPDFLineAnnotation *)annotation setStartPoint:CGPointMake(point.x-50, point.y)];
- [(CPDFLineAnnotation *)annotation setEndPoint:CGPointMake(point.x+50, point.y)];
- }
- break;
- default:
- break;
- }
-
- if (!annotation) {
- return;
- }
- [annotation setModificationDate:[NSDate date]];
- [annotation setUserName:[self annotationUserName]];
- [page addAnnotation:annotation];
-
- if ([annotation isKindOfClass:[CPDFTextAnnotation class]]) {
- [self setNeedsDisplayForPage:page];
- // if ([self.performDelegate respondsToSelector:@selector(PDFViewPerformOpenNote:forAnnotation:)]) {
- // [self.performDelegate PDFViewPerformOpenNote:self forAnnotation:annotation];
- // }
- } else if ([annotation isKindOfClass:[CPDFMarkupAnnotation class]]) {
- [self setNeedsDisplayForPage:page];
- } else {
- self.activeAnnotation = annotation;
- [self setNeedsDisplayForPage:page];
- [self updateScrollEnabled];
-
- // [self showMenuForAnnotation:annotation];
- }
- }
- - (void)addAnnotation:(CPDFAnnotation *)annotation forPage:(CPDFPage *)page {
- if (!annotation || !page) {
- return;
- }
- [annotation setModificationDate:[NSDate date]];
- [annotation setUserName:[self annotationUserName]];
- [page addAnnotation:annotation];
-
- self.activeAnnotation = annotation;
- [self setNeedsDisplayForPage:page];
- [self updateScrollEnabled];
-
- // [self showMenuForAnnotation:annotation];
- }
- - (void)addAnnotation:(CPDFAnnotation *)annotation {
- CPDFPage *page = [self.document pageAtIndex:self.currentPageIndex];
- CGPoint center = [self convertPoint:self.center toPage:page];
- if (CGPointEqualToPoint(center, CGPointZero)) {
- return;
- }
- CGRect bounds = annotation.bounds;
- bounds.origin.x = center.x-bounds.size.width/2.0;
- bounds.origin.y = center.y-bounds.size.height/2.0;
- bounds.origin.y = MIN(MAX(0, bounds.origin.y), page.bounds.size.height-bounds.size.height);
- annotation.bounds = bounds;
- [self addAnnotation:annotation forPage:page];
- }
- @end
|