123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595 |
- #import "ReaderConstants.h"
- #import "ReaderContentPage.h"
- #import "ReaderContentTile.h"
- #import "CGPDFDocument.h"
- @implementation ReaderContentPage
- {
- NSMutableArray *_links;
- CGPDFDocumentRef _PDFDocRef;
- CGPDFPageRef _PDFPageRef;
- NSInteger _pageAngle;
- CGFloat _pageWidth;
- CGFloat _pageHeight;
- CGFloat _pageOffsetX;
- CGFloat _pageOffsetY;
- }
- #pragma mark - ReaderContentPage class methods
- + (Class)layerClass
- {
- return [ReaderContentTile class];
- }
- #pragma mark - ReaderContentPage PDF link methods
- - (void)highlightPageLinks
- {
- if (_links.count > 0)
- {
- UIColor *hilite = [UIColor colorWithRed:0.0f green:0.0f blue:1.0f alpha:0.15f];
- for (ReaderDocumentLink *link in _links)
- {
- UIView *highlight = [[UIView alloc] initWithFrame:link.rect];
- highlight.autoresizesSubviews = NO;
- highlight.userInteractionEnabled = NO;
- highlight.contentMode = UIViewContentModeRedraw;
- highlight.autoresizingMask = UIViewAutoresizingNone;
- highlight.backgroundColor = hilite;
- [self addSubview:highlight];
- }
- }
- }
- - (ReaderDocumentLink *)linkFromAnnotation:(CGPDFDictionaryRef)annotationDictionary
- {
- ReaderDocumentLink *documentLink = nil;
- CGPDFArrayRef annotationRectArray = NULL;
- if (CGPDFDictionaryGetArray(annotationDictionary, "Rect", &annotationRectArray))
- {
- CGPDFReal ll_x = 0.0f; CGPDFReal ll_y = 0.0f;
- CGPDFReal ur_x = 0.0f; CGPDFReal ur_y = 0.0f;
- CGPDFArrayGetNumber(annotationRectArray, 0, &ll_x);
- CGPDFArrayGetNumber(annotationRectArray, 1, &ll_y);
- CGPDFArrayGetNumber(annotationRectArray, 2, &ur_x);
- CGPDFArrayGetNumber(annotationRectArray, 3, &ur_y);
- if (ll_x > ur_x) { CGPDFReal t = ll_x; ll_x = ur_x; ur_x = t; }
- if (ll_y > ur_y) { CGPDFReal t = ll_y; ll_y = ur_y; ur_y = t; }
- ll_x -= _pageOffsetX; ll_y -= _pageOffsetY;
- ur_x -= _pageOffsetX; ur_y -= _pageOffsetY;
- switch (_pageAngle)
- {
- case 90:
- {
- CGPDFReal swap;
- swap = ll_y; ll_y = ll_x; ll_x = swap;
- swap = ur_y; ur_y = ur_x; ur_x = swap;
- break;
- }
- case 270:
- {
- CGPDFReal swap;
- swap = ll_y; ll_y = ll_x; ll_x = swap;
- swap = ur_y; ur_y = ur_x; ur_x = swap;
- ll_x = ((0.0f - ll_x) + _pageWidth);
- ur_x = ((0.0f - ur_x) + _pageWidth);
- break;
- }
- case 0:
- {
- ll_y = ((0.0f - ll_y) + _pageHeight);
- ur_y = ((0.0f - ur_y) + _pageHeight);
- break;
- }
- }
- NSInteger vr_x = ll_x; NSInteger vr_w = (ur_x - ll_x);
- NSInteger vr_y = ll_y; NSInteger vr_h = (ur_y - ll_y);
- CGRect viewRect = CGRectMake(vr_x, vr_y, vr_w, vr_h);
- documentLink = [ReaderDocumentLink newWithRect:viewRect dictionary:annotationDictionary];
- }
- return documentLink;
- }
- - (void)buildAnnotationLinksList
- {
- _links = [NSMutableArray new];
- CGPDFArrayRef pageAnnotations = NULL;
- CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(_PDFPageRef);
- if (CGPDFDictionaryGetArray(pageDictionary, "Annots", &pageAnnotations) == true)
- {
- NSInteger count = CGPDFArrayGetCount(pageAnnotations);
- for (NSInteger index = 0; index < count; index++)
- {
- CGPDFDictionaryRef annotationDictionary = NULL;
- if (CGPDFArrayGetDictionary(pageAnnotations, index, &annotationDictionary) == true)
- {
- const char *annotationSubtype = NULL;
- if (CGPDFDictionaryGetName(annotationDictionary, "Subtype", &annotationSubtype) == true)
- {
- if (strcmp(annotationSubtype, "Link") == 0)
- {
- ReaderDocumentLink *documentLink = [self linkFromAnnotation:annotationDictionary];
- if (documentLink != nil) [_links insertObject:documentLink atIndex:0];
- }
- }
- }
- }
-
- }
- }
- - (CGPDFArrayRef)destinationWithName:(const char *)destinationName inDestsTree:(CGPDFDictionaryRef)node
- {
- CGPDFArrayRef destinationArray = NULL;
- CGPDFArrayRef limitsArray = NULL;
- if (CGPDFDictionaryGetArray(node, "Limits", &limitsArray) == true)
- {
- CGPDFStringRef lowerLimit = NULL; CGPDFStringRef upperLimit = NULL;
- if (CGPDFArrayGetString(limitsArray, 0, &lowerLimit) == true)
- {
- if (CGPDFArrayGetString(limitsArray, 1, &upperLimit) == true)
- {
- const char *ll = (const char *)CGPDFStringGetBytePtr(lowerLimit);
- const char *ul = (const char *)CGPDFStringGetBytePtr(upperLimit);
- if ((strcmp(destinationName, ll) < 0) || (strcmp(destinationName, ul) > 0))
- {
- return NULL;
- }
- }
- }
- }
- CGPDFArrayRef namesArray = NULL;
- if (CGPDFDictionaryGetArray(node, "Names", &namesArray) == true)
- {
- NSInteger namesCount = CGPDFArrayGetCount(namesArray);
- for (NSInteger index = 0; index < namesCount; index += 2)
- {
- CGPDFStringRef destName;
- if (CGPDFArrayGetString(namesArray, index, &destName) == true)
- {
- const char *dn = (const char *)CGPDFStringGetBytePtr(destName);
- if (strcmp(dn, destinationName) == 0)
- {
- if (CGPDFArrayGetArray(namesArray, (index + 1), &destinationArray) == false)
- {
- CGPDFDictionaryRef destinationDictionary = NULL;
- if (CGPDFArrayGetDictionary(namesArray, (index + 1), &destinationDictionary) == true)
- {
- CGPDFDictionaryGetArray(destinationDictionary, "D", &destinationArray);
- }
- }
- return destinationArray;
- }
- }
- }
- }
- CGPDFArrayRef kidsArray = NULL;
- if (CGPDFDictionaryGetArray(node, "Kids", &kidsArray) == true)
- {
- NSInteger kidsCount = CGPDFArrayGetCount(kidsArray);
- for (NSInteger index = 0; index < kidsCount; index++)
- {
- CGPDFDictionaryRef kidNode = NULL;
- if (CGPDFArrayGetDictionary(kidsArray, index, &kidNode) == true)
- {
- destinationArray = [self destinationWithName:destinationName inDestsTree:kidNode];
- if (destinationArray != NULL) return destinationArray;
- }
- }
- }
- return NULL;
- }
- - (id)annotationLinkTarget:(CGPDFDictionaryRef)annotationDictionary
- {
- id linkTarget = nil;
- CGPDFStringRef destName = NULL; const char *destString = NULL;
- CGPDFDictionaryRef actionDictionary = NULL; CGPDFArrayRef destArray = NULL;
- if (CGPDFDictionaryGetDictionary(annotationDictionary, "A", &actionDictionary) == true)
- {
- const char *actionType = NULL;
- if (CGPDFDictionaryGetName(actionDictionary, "S", &actionType) == true)
- {
- if (strcmp(actionType, "GoTo") == 0)
- {
- if (CGPDFDictionaryGetArray(actionDictionary, "D", &destArray) == false)
- {
- CGPDFDictionaryGetString(actionDictionary, "D", &destName);
- }
- }
- else
- {
- if (strcmp(actionType, "URI") == 0)
- {
- CGPDFStringRef uriString = NULL;
- if (CGPDFDictionaryGetString(actionDictionary, "URI", &uriString) == true)
- {
- const char *uri = (const char *)CGPDFStringGetBytePtr(uriString);
- NSString *target = [NSString stringWithCString:uri encoding:NSUTF8StringEncoding];
-
- linkTarget = [NSURL URLWithString:[target stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]];
-
- if (linkTarget == nil) NSLog(@"%s Bad URI '%@'", __FUNCTION__, target);
- }
- }
- }
- }
- }
- else
- {
- if (CGPDFDictionaryGetArray(annotationDictionary, "Dest", &destArray) == false)
- {
- if (CGPDFDictionaryGetString(annotationDictionary, "Dest", &destName) == false)
- {
- CGPDFDictionaryGetName(annotationDictionary, "Dest", &destString);
- }
- }
- }
- if (destName != NULL)
- {
- CGPDFDictionaryRef catalogDictionary = CGPDFDocumentGetCatalog(_PDFDocRef);
- CGPDFDictionaryRef namesDictionary = NULL;
- if (CGPDFDictionaryGetDictionary(catalogDictionary, "Names", &namesDictionary) == true)
- {
- CGPDFDictionaryRef destsDictionary = NULL;
- if (CGPDFDictionaryGetDictionary(namesDictionary, "Dests", &destsDictionary) == true)
- {
- const char *destinationName = (const char *)CGPDFStringGetBytePtr(destName);
- destArray = [self destinationWithName:destinationName inDestsTree:destsDictionary];
- }
- }
- }
- if (destString != NULL)
- {
- CGPDFDictionaryRef catalogDictionary = CGPDFDocumentGetCatalog(_PDFDocRef);
- CGPDFDictionaryRef destsDictionary = NULL;
- if (CGPDFDictionaryGetDictionary(catalogDictionary, "Dests", &destsDictionary) == true)
- {
- CGPDFDictionaryRef targetDictionary = NULL;
- if (CGPDFDictionaryGetDictionary(destsDictionary, destString, &targetDictionary) == true)
- {
- CGPDFDictionaryGetArray(targetDictionary, "D", &destArray);
- }
- }
- }
- if (destArray != NULL)
- {
- NSInteger targetPageNumber = 0;
- CGPDFDictionaryRef pageDictionaryFromDestArray = NULL;
- if (CGPDFArrayGetDictionary(destArray, 0, &pageDictionaryFromDestArray) == true)
- {
- NSInteger pageCount = CGPDFDocumentGetNumberOfPages(_PDFDocRef);
- for (NSInteger pageNumber = 1; pageNumber <= pageCount; pageNumber++)
- {
- CGPDFPageRef pageRef = CGPDFDocumentGetPage(_PDFDocRef, pageNumber);
- CGPDFDictionaryRef pageDictionaryFromPage = CGPDFPageGetDictionary(pageRef);
- if (pageDictionaryFromPage == pageDictionaryFromDestArray)
- {
- targetPageNumber = pageNumber; break;
- }
- }
- }
- else
- {
- CGPDFInteger pageNumber = 0;
- if (CGPDFArrayGetInteger(destArray, 0, &pageNumber) == true)
- {
- targetPageNumber = (pageNumber + 1);
- }
- }
- if (targetPageNumber > 0)
- {
- linkTarget = [NSNumber numberWithInteger:targetPageNumber];
- }
- }
- return linkTarget;
- }
- - (id)processSingleTap:(UITapGestureRecognizer *)recognizer
- {
- id result = nil;
- if (recognizer.state == UIGestureRecognizerStateRecognized)
- {
- if (_links.count > 0)
- {
- CGPoint point = [recognizer locationInView:self];
- for (ReaderDocumentLink *link in _links)
- {
- if (CGRectContainsPoint(link.rect, point) == true)
- {
- result = [self annotationLinkTarget:link.dictionary]; break;
- }
- }
- }
- }
- return result;
- }
- #pragma mark - ReaderContentPage instance methods
- - (instancetype)initWithFrame:(CGRect)frame
- {
- if ((self = [super initWithFrame:frame]))
- {
- self.autoresizesSubviews = NO;
- self.userInteractionEnabled = NO;
- self.contentMode = UIViewContentModeRedraw;
- self.autoresizingMask = UIViewAutoresizingNone;
- self.backgroundColor = [UIColor clearColor];
- }
- return self;
- }
- - (instancetype)initWithURL:(NSURL *)fileURL page:(NSInteger)page password:(NSString *)phrase
- {
- CGRect viewRect = CGRectZero;
- if (fileURL != nil)
- {
- _PDFDocRef = CGPDFDocumentCreateUsingUrl((__bridge CFURLRef)fileURL, phrase);
- if (_PDFDocRef != NULL)
- {
- if (page < 1) page = 1;
- NSInteger pages = CGPDFDocumentGetNumberOfPages(_PDFDocRef);
- if (page > pages) page = pages;
- _PDFPageRef = CGPDFDocumentGetPage(_PDFDocRef, page);
- if (_PDFPageRef != NULL)
- {
- CGPDFPageRetain(_PDFPageRef);
- CGRect cropBoxRect = CGPDFPageGetBoxRect(_PDFPageRef, kCGPDFCropBox);
- CGRect mediaBoxRect = CGPDFPageGetBoxRect(_PDFPageRef, kCGPDFMediaBox);
- CGRect effectiveRect = CGRectIntersection(cropBoxRect, mediaBoxRect);
- _pageAngle = CGPDFPageGetRotationAngle(_PDFPageRef);
- switch (_pageAngle)
- {
- default:
- case 0: case 180:
- {
- _pageWidth = effectiveRect.size.width;
- _pageHeight = effectiveRect.size.height;
- _pageOffsetX = effectiveRect.origin.x;
- _pageOffsetY = effectiveRect.origin.y;
- break;
- }
- case 90: case 270:
- {
- _pageWidth = effectiveRect.size.height;
- _pageHeight = effectiveRect.size.width;
- _pageOffsetX = effectiveRect.origin.y;
- _pageOffsetY = effectiveRect.origin.x;
- break;
- }
- }
- NSInteger page_w = _pageWidth;
- NSInteger page_h = _pageHeight;
- if (page_w % 2) page_w--; if (page_h % 2) page_h--;
- viewRect.size = CGSizeMake(page_w, page_h);
- }
- else
- {
- (void)(CGPDFDocumentRelease(_PDFDocRef)), _PDFDocRef = NULL;
- NSAssert(NO, @"CGPDFPageRef == NULL");
- }
- }
- else
- {
- NSAssert(NO, @"CGPDFDocumentRef == NULL");
- }
- }
- else
- {
- NSAssert(NO, @"fileURL == nil");
- }
- ReaderContentPage *view = [self initWithFrame:viewRect];
- if (view != nil) [self buildAnnotationLinksList];
- return view;
- }
- - (void)removeFromSuperview
- {
- self.layer.delegate = nil;
-
- [super removeFromSuperview];
- }
- - (void)dealloc
- {
- (void)(CGPDFPageRelease(_PDFPageRef)), _PDFPageRef = NULL;
- (void)(CGPDFDocumentRelease(_PDFDocRef)), _PDFDocRef = NULL;
- }
- #if (READER_DISABLE_RETINA == TRUE)
- - (void)didMoveToWindow
- {
- self.contentScaleFactor = 1.0f;
- }
- #endif
- #pragma mark - CATiledLayer delegate methods
- - (void)drawLayer:(CATiledLayer *)layer inContext:(CGContextRef)context
- {
- ReaderContentPage *readerContentPage = self;
- CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f);
- CGContextFillRect(context, CGContextGetClipBoundingBox(context));
-
- CGContextTranslateCTM(context, 0.0f, self.bounds.size.height); CGContextScaleCTM(context, 1.0f, -1.0f);
- CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(_PDFPageRef, kCGPDFCropBox, self.bounds, 0, true));
-
- CGContextDrawPDFPage(context, _PDFPageRef);
- if (readerContentPage != nil) readerContentPage = nil;
- }
- @end
- #pragma mark -
- @implementation ReaderDocumentLink
- {
- CGPDFDictionaryRef _dictionary;
- CGRect _rect;
- }
- #pragma mark - Properties
- @synthesize rect = _rect;
- @synthesize dictionary = _dictionary;
- #pragma mark - ReaderDocumentLink class methods
- + (instancetype)newWithRect:(CGRect)linkRect dictionary:(CGPDFDictionaryRef)linkDictionary
- {
- return [[ReaderDocumentLink alloc] initWithRect:linkRect dictionary:linkDictionary];
- }
- #pragma mark - ReaderDocumentLink instance methods
- - (instancetype)initWithRect:(CGRect)linkRect dictionary:(CGPDFDictionaryRef)linkDictionary
- {
- if ((self = [super init]))
- {
- _dictionary = linkDictionary;
- _rect = linkRect;
- }
- return self;
- }
- @end
|