ReaderContentPage.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. //
  2. // ReaderContentPage.m
  3. // Reader v2.8.6
  4. //
  5. // Created by Julius Oklamcak on 2011-07-01.
  6. // Copyright © 2011-2015 Julius Oklamcak. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights to
  11. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  12. // of the Software, and to permit persons to whom the Software is furnished to
  13. // do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in all
  16. // copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. //
  25. #import "ReaderConstants.h"
  26. #import "ReaderContentPage.h"
  27. #import "ReaderContentTile.h"
  28. #import "CGPDFDocument.h"
  29. @implementation ReaderContentPage
  30. {
  31. NSMutableArray *_links;
  32. CGPDFDocumentRef _PDFDocRef;
  33. CGPDFPageRef _PDFPageRef;
  34. NSInteger _pageAngle;
  35. CGFloat _pageWidth;
  36. CGFloat _pageHeight;
  37. CGFloat _pageOffsetX;
  38. CGFloat _pageOffsetY;
  39. }
  40. #pragma mark - ReaderContentPage class methods
  41. + (Class)layerClass
  42. {
  43. return [ReaderContentTile class];
  44. }
  45. #pragma mark - ReaderContentPage PDF link methods
  46. - (void)highlightPageLinks
  47. {
  48. if (_links.count > 0) // Add highlight views over all links
  49. {
  50. UIColor *hilite = [UIColor colorWithRed:0.0f green:0.0f blue:1.0f alpha:0.15f];
  51. for (ReaderDocumentLink *link in _links) // Enumerate the links array
  52. {
  53. UIView *highlight = [[UIView alloc] initWithFrame:link.rect];
  54. highlight.autoresizesSubviews = NO;
  55. highlight.userInteractionEnabled = NO;
  56. highlight.contentMode = UIViewContentModeRedraw;
  57. highlight.autoresizingMask = UIViewAutoresizingNone;
  58. highlight.backgroundColor = hilite; // Color
  59. [self addSubview:highlight];
  60. }
  61. }
  62. }
  63. - (ReaderDocumentLink *)linkFromAnnotation:(CGPDFDictionaryRef)annotationDictionary
  64. {
  65. ReaderDocumentLink *documentLink = nil; // Document link object
  66. CGPDFArrayRef annotationRectArray = NULL; // Annotation co-ordinates array
  67. if (CGPDFDictionaryGetArray(annotationDictionary, "Rect", &annotationRectArray))
  68. {
  69. CGPDFReal ll_x = 0.0f; CGPDFReal ll_y = 0.0f; // PDFRect lower-left X and Y
  70. CGPDFReal ur_x = 0.0f; CGPDFReal ur_y = 0.0f; // PDFRect upper-right X and Y
  71. CGPDFArrayGetNumber(annotationRectArray, 0, &ll_x); // Lower-left X co-ordinate
  72. CGPDFArrayGetNumber(annotationRectArray, 1, &ll_y); // Lower-left Y co-ordinate
  73. CGPDFArrayGetNumber(annotationRectArray, 2, &ur_x); // Upper-right X co-ordinate
  74. CGPDFArrayGetNumber(annotationRectArray, 3, &ur_y); // Upper-right Y co-ordinate
  75. if (ll_x > ur_x) { CGPDFReal t = ll_x; ll_x = ur_x; ur_x = t; } // Normalize Xs
  76. if (ll_y > ur_y) { CGPDFReal t = ll_y; ll_y = ur_y; ur_y = t; } // Normalize Ys
  77. ll_x -= _pageOffsetX; ll_y -= _pageOffsetY; // Offset lower-left co-ordinate
  78. ur_x -= _pageOffsetX; ur_y -= _pageOffsetY; // Offset upper-right co-ordinate
  79. switch (_pageAngle) // Page rotation angle (in degrees)
  80. {
  81. case 90: // 90 degree page rotation
  82. {
  83. CGPDFReal swap;
  84. swap = ll_y; ll_y = ll_x; ll_x = swap;
  85. swap = ur_y; ur_y = ur_x; ur_x = swap;
  86. break;
  87. }
  88. case 270: // 270 degree page rotation
  89. {
  90. CGPDFReal swap;
  91. swap = ll_y; ll_y = ll_x; ll_x = swap;
  92. swap = ur_y; ur_y = ur_x; ur_x = swap;
  93. ll_x = ((0.0f - ll_x) + _pageWidth);
  94. ur_x = ((0.0f - ur_x) + _pageWidth);
  95. break;
  96. }
  97. case 0: // 0 degree page rotation
  98. {
  99. ll_y = ((0.0f - ll_y) + _pageHeight);
  100. ur_y = ((0.0f - ur_y) + _pageHeight);
  101. break;
  102. }
  103. }
  104. NSInteger vr_x = ll_x; NSInteger vr_w = (ur_x - ll_x); // Integer X and width
  105. NSInteger vr_y = ll_y; NSInteger vr_h = (ur_y - ll_y); // Integer Y and height
  106. CGRect viewRect = CGRectMake(vr_x, vr_y, vr_w, vr_h); // View CGRect from PDFRect
  107. documentLink = [ReaderDocumentLink newWithRect:viewRect dictionary:annotationDictionary];
  108. }
  109. return documentLink;
  110. }
  111. - (void)buildAnnotationLinksList
  112. {
  113. _links = [NSMutableArray new]; // Links list array
  114. CGPDFArrayRef pageAnnotations = NULL; // Page annotations array
  115. CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(_PDFPageRef);
  116. if (CGPDFDictionaryGetArray(pageDictionary, "Annots", &pageAnnotations) == true)
  117. {
  118. NSInteger count = CGPDFArrayGetCount(pageAnnotations); // Number of annotations
  119. for (NSInteger index = 0; index < count; index++) // Iterate through all annotations
  120. {
  121. CGPDFDictionaryRef annotationDictionary = NULL; // PDF annotation dictionary
  122. if (CGPDFArrayGetDictionary(pageAnnotations, index, &annotationDictionary) == true)
  123. {
  124. const char *annotationSubtype = NULL; // PDF annotation subtype string
  125. if (CGPDFDictionaryGetName(annotationDictionary, "Subtype", &annotationSubtype) == true)
  126. {
  127. if (strcmp(annotationSubtype, "Link") == 0) // Found annotation subtype of 'Link'
  128. {
  129. ReaderDocumentLink *documentLink = [self linkFromAnnotation:annotationDictionary];
  130. if (documentLink != nil) [_links insertObject:documentLink atIndex:0]; // Add link
  131. }
  132. }
  133. }
  134. }
  135. //[self highlightPageLinks]; // Link support debugging
  136. }
  137. }
  138. - (CGPDFArrayRef)destinationWithName:(const char *)destinationName inDestsTree:(CGPDFDictionaryRef)node
  139. {
  140. CGPDFArrayRef destinationArray = NULL;
  141. CGPDFArrayRef limitsArray = NULL; // Limits array
  142. if (CGPDFDictionaryGetArray(node, "Limits", &limitsArray) == true)
  143. {
  144. CGPDFStringRef lowerLimit = NULL; CGPDFStringRef upperLimit = NULL;
  145. if (CGPDFArrayGetString(limitsArray, 0, &lowerLimit) == true) // Lower limit
  146. {
  147. if (CGPDFArrayGetString(limitsArray, 1, &upperLimit) == true) // Upper limit
  148. {
  149. const char *ll = (const char *)CGPDFStringGetBytePtr(lowerLimit); // Lower string
  150. const char *ul = (const char *)CGPDFStringGetBytePtr(upperLimit); // Upper string
  151. if ((strcmp(destinationName, ll) < 0) || (strcmp(destinationName, ul) > 0))
  152. {
  153. return NULL; // Destination name is outside this node's limits
  154. }
  155. }
  156. }
  157. }
  158. CGPDFArrayRef namesArray = NULL; // Names array
  159. if (CGPDFDictionaryGetArray(node, "Names", &namesArray) == true)
  160. {
  161. NSInteger namesCount = CGPDFArrayGetCount(namesArray);
  162. for (NSInteger index = 0; index < namesCount; index += 2)
  163. {
  164. CGPDFStringRef destName; // Destination name string
  165. if (CGPDFArrayGetString(namesArray, index, &destName) == true)
  166. {
  167. const char *dn = (const char *)CGPDFStringGetBytePtr(destName);
  168. if (strcmp(dn, destinationName) == 0) // Found the destination name
  169. {
  170. if (CGPDFArrayGetArray(namesArray, (index + 1), &destinationArray) == false)
  171. {
  172. CGPDFDictionaryRef destinationDictionary = NULL; // Destination dictionary
  173. if (CGPDFArrayGetDictionary(namesArray, (index + 1), &destinationDictionary) == true)
  174. {
  175. CGPDFDictionaryGetArray(destinationDictionary, "D", &destinationArray);
  176. }
  177. }
  178. return destinationArray; // Return the destination array
  179. }
  180. }
  181. }
  182. }
  183. CGPDFArrayRef kidsArray = NULL; // Kids array
  184. if (CGPDFDictionaryGetArray(node, "Kids", &kidsArray) == true)
  185. {
  186. NSInteger kidsCount = CGPDFArrayGetCount(kidsArray);
  187. for (NSInteger index = 0; index < kidsCount; index++)
  188. {
  189. CGPDFDictionaryRef kidNode = NULL; // Kid node dictionary
  190. if (CGPDFArrayGetDictionary(kidsArray, index, &kidNode) == true) // Recurse into node
  191. {
  192. destinationArray = [self destinationWithName:destinationName inDestsTree:kidNode];
  193. if (destinationArray != NULL) return destinationArray; // Return destination array
  194. }
  195. }
  196. }
  197. return NULL;
  198. }
  199. - (id)annotationLinkTarget:(CGPDFDictionaryRef)annotationDictionary
  200. {
  201. id linkTarget = nil; // Link target object
  202. CGPDFStringRef destName = NULL; const char *destString = NULL;
  203. CGPDFDictionaryRef actionDictionary = NULL; CGPDFArrayRef destArray = NULL;
  204. if (CGPDFDictionaryGetDictionary(annotationDictionary, "A", &actionDictionary) == true)
  205. {
  206. const char *actionType = NULL; // Annotation action type string
  207. if (CGPDFDictionaryGetName(actionDictionary, "S", &actionType) == true)
  208. {
  209. if (strcmp(actionType, "GoTo") == 0) // GoTo action type
  210. {
  211. if (CGPDFDictionaryGetArray(actionDictionary, "D", &destArray) == false)
  212. {
  213. CGPDFDictionaryGetString(actionDictionary, "D", &destName);
  214. }
  215. }
  216. else // Handle other link action type possibility
  217. {
  218. if (strcmp(actionType, "URI") == 0) // URI action type
  219. {
  220. CGPDFStringRef uriString = NULL; // Action's URI string
  221. if (CGPDFDictionaryGetString(actionDictionary, "URI", &uriString) == true)
  222. {
  223. const char *uri = (const char *)CGPDFStringGetBytePtr(uriString); // Destination URI string
  224. NSString *target = [NSString stringWithCString:uri encoding:NSUTF8StringEncoding]; // NSString - UTF8
  225. linkTarget = [NSURL URLWithString:[target stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  226. if (linkTarget == nil) NSLog(@"%s Bad URI '%@'", __FUNCTION__, target);
  227. }
  228. }
  229. }
  230. }
  231. }
  232. else // Handle other link target possibilities
  233. {
  234. if (CGPDFDictionaryGetArray(annotationDictionary, "Dest", &destArray) == false)
  235. {
  236. if (CGPDFDictionaryGetString(annotationDictionary, "Dest", &destName) == false)
  237. {
  238. CGPDFDictionaryGetName(annotationDictionary, "Dest", &destString);
  239. }
  240. }
  241. }
  242. if (destName != NULL) // Handle a destination name
  243. {
  244. CGPDFDictionaryRef catalogDictionary = CGPDFDocumentGetCatalog(_PDFDocRef);
  245. CGPDFDictionaryRef namesDictionary = NULL; // Destination names in the document
  246. if (CGPDFDictionaryGetDictionary(catalogDictionary, "Names", &namesDictionary) == true)
  247. {
  248. CGPDFDictionaryRef destsDictionary = NULL; // Document destinations dictionary
  249. if (CGPDFDictionaryGetDictionary(namesDictionary, "Dests", &destsDictionary) == true)
  250. {
  251. const char *destinationName = (const char *)CGPDFStringGetBytePtr(destName); // Name
  252. destArray = [self destinationWithName:destinationName inDestsTree:destsDictionary];
  253. }
  254. }
  255. }
  256. if (destString != NULL) // Handle a destination string
  257. {
  258. CGPDFDictionaryRef catalogDictionary = CGPDFDocumentGetCatalog(_PDFDocRef);
  259. CGPDFDictionaryRef destsDictionary = NULL; // Document destinations dictionary
  260. if (CGPDFDictionaryGetDictionary(catalogDictionary, "Dests", &destsDictionary) == true)
  261. {
  262. CGPDFDictionaryRef targetDictionary = NULL; // Destination target dictionary
  263. if (CGPDFDictionaryGetDictionary(destsDictionary, destString, &targetDictionary) == true)
  264. {
  265. CGPDFDictionaryGetArray(targetDictionary, "D", &destArray);
  266. }
  267. }
  268. }
  269. if (destArray != NULL) // Handle a destination array
  270. {
  271. NSInteger targetPageNumber = 0; // The target page number
  272. CGPDFDictionaryRef pageDictionaryFromDestArray = NULL; // Target reference
  273. if (CGPDFArrayGetDictionary(destArray, 0, &pageDictionaryFromDestArray) == true)
  274. {
  275. NSInteger pageCount = CGPDFDocumentGetNumberOfPages(_PDFDocRef); // Pages
  276. for (NSInteger pageNumber = 1; pageNumber <= pageCount; pageNumber++)
  277. {
  278. CGPDFPageRef pageRef = CGPDFDocumentGetPage(_PDFDocRef, pageNumber);
  279. CGPDFDictionaryRef pageDictionaryFromPage = CGPDFPageGetDictionary(pageRef);
  280. if (pageDictionaryFromPage == pageDictionaryFromDestArray) // Found it
  281. {
  282. targetPageNumber = pageNumber; break;
  283. }
  284. }
  285. }
  286. else // Try page number from array possibility
  287. {
  288. CGPDFInteger pageNumber = 0; // Page number in array
  289. if (CGPDFArrayGetInteger(destArray, 0, &pageNumber) == true)
  290. {
  291. targetPageNumber = (pageNumber + 1); // 1-based
  292. }
  293. }
  294. if (targetPageNumber > 0) // We have a target page number
  295. {
  296. linkTarget = [NSNumber numberWithInteger:targetPageNumber];
  297. }
  298. }
  299. return linkTarget;
  300. }
  301. - (id)processSingleTap:(UITapGestureRecognizer *)recognizer
  302. {
  303. id result = nil; // Tap result object
  304. if (recognizer.state == UIGestureRecognizerStateRecognized)
  305. {
  306. if (_links.count > 0) // Process the single tap
  307. {
  308. CGPoint point = [recognizer locationInView:self];
  309. for (ReaderDocumentLink *link in _links) // Enumerate links
  310. {
  311. if (CGRectContainsPoint(link.rect, point) == true) // Found it
  312. {
  313. result = [self annotationLinkTarget:link.dictionary]; break;
  314. }
  315. }
  316. }
  317. }
  318. return result;
  319. }
  320. #pragma mark - ReaderContentPage instance methods
  321. - (instancetype)initWithFrame:(CGRect)frame
  322. {
  323. if ((self = [super initWithFrame:frame]))
  324. {
  325. self.autoresizesSubviews = NO;
  326. self.userInteractionEnabled = NO;
  327. self.contentMode = UIViewContentModeRedraw;
  328. self.autoresizingMask = UIViewAutoresizingNone;
  329. self.backgroundColor = [UIColor clearColor];
  330. }
  331. return self;
  332. }
  333. - (instancetype)initWithURL:(NSURL *)fileURL page:(NSInteger)page password:(NSString *)phrase
  334. {
  335. CGRect viewRect = CGRectZero; // View rect
  336. if (fileURL != nil) // Check for non-nil file URL
  337. {
  338. _PDFDocRef = CGPDFDocumentCreateUsingUrl((__bridge CFURLRef)fileURL, phrase);
  339. if (_PDFDocRef != NULL) // Check for non-NULL CGPDFDocumentRef
  340. {
  341. if (page < 1) page = 1; // Check the lower page bounds
  342. NSInteger pages = CGPDFDocumentGetNumberOfPages(_PDFDocRef);
  343. if (page > pages) page = pages; // Check the upper page bounds
  344. _PDFPageRef = CGPDFDocumentGetPage(_PDFDocRef, page); // Get page
  345. if (_PDFPageRef != NULL) // Check for non-NULL CGPDFPageRef
  346. {
  347. CGPDFPageRetain(_PDFPageRef); // Retain the PDF page
  348. CGRect cropBoxRect = CGPDFPageGetBoxRect(_PDFPageRef, kCGPDFCropBox);
  349. CGRect mediaBoxRect = CGPDFPageGetBoxRect(_PDFPageRef, kCGPDFMediaBox);
  350. CGRect effectiveRect = CGRectIntersection(cropBoxRect, mediaBoxRect);
  351. _pageAngle = CGPDFPageGetRotationAngle(_PDFPageRef); // Angle
  352. switch (_pageAngle) // Page rotation angle (in degrees)
  353. {
  354. default: // Default case
  355. case 0: case 180: // 0 and 180 degrees
  356. {
  357. _pageWidth = effectiveRect.size.width;
  358. _pageHeight = effectiveRect.size.height;
  359. _pageOffsetX = effectiveRect.origin.x;
  360. _pageOffsetY = effectiveRect.origin.y;
  361. break;
  362. }
  363. case 90: case 270: // 90 and 270 degrees
  364. {
  365. _pageWidth = effectiveRect.size.height;
  366. _pageHeight = effectiveRect.size.width;
  367. _pageOffsetX = effectiveRect.origin.y;
  368. _pageOffsetY = effectiveRect.origin.x;
  369. break;
  370. }
  371. }
  372. NSInteger page_w = _pageWidth; // Integer width
  373. NSInteger page_h = _pageHeight; // Integer height
  374. if (page_w % 2) page_w--; if (page_h % 2) page_h--; // Even
  375. viewRect.size = CGSizeMake(page_w, page_h); // View size
  376. }
  377. else // Error out with a diagnostic
  378. {
  379. CGPDFDocumentRelease(_PDFDocRef), _PDFDocRef = NULL;
  380. NSAssert(NO, @"CGPDFPageRef == NULL");
  381. }
  382. }
  383. else // Error out with a diagnostic
  384. {
  385. NSAssert(NO, @"CGPDFDocumentRef == NULL");
  386. }
  387. }
  388. else // Error out with a diagnostic
  389. {
  390. NSAssert(NO, @"fileURL == nil");
  391. }
  392. ReaderContentPage *view = [self initWithFrame:viewRect];
  393. if (view != nil) [self buildAnnotationLinksList];
  394. return view;
  395. }
  396. - (void)removeFromSuperview
  397. {
  398. self.layer.delegate = nil;
  399. //self.layer.contents = nil;
  400. [super removeFromSuperview];
  401. }
  402. - (void)dealloc
  403. {
  404. CGPDFPageRelease(_PDFPageRef), _PDFPageRef = NULL;
  405. CGPDFDocumentRelease(_PDFDocRef), _PDFDocRef = NULL;
  406. }
  407. #if (READER_DISABLE_RETINA == TRUE) // Option
  408. - (void)didMoveToWindow
  409. {
  410. self.contentScaleFactor = 1.0f; // Override scale factor
  411. }
  412. #endif // end of READER_DISABLE_RETINA Option
  413. #pragma mark - CATiledLayer delegate methods
  414. - (void)drawLayer:(CATiledLayer *)layer inContext:(CGContextRef)context
  415. {
  416. ReaderContentPage *readerContentPage = self; // Retain self
  417. CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f); // White
  418. CGContextFillRect(context, CGContextGetClipBoundingBox(context)); // Fill
  419. //NSLog(@"%s %@", __FUNCTION__, NSStringFromCGRect(CGContextGetClipBoundingBox(context)));
  420. CGContextTranslateCTM(context, 0.0f, self.bounds.size.height); CGContextScaleCTM(context, 1.0f, -1.0f);
  421. CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(_PDFPageRef, kCGPDFCropBox, self.bounds, 0, true));
  422. //CGContextSetRenderingIntent(context, kCGRenderingIntentDefault); CGContextSetInterpolationQuality(context, kCGInterpolationDefault);
  423. CGContextDrawPDFPage(context, _PDFPageRef); // Render the PDF page into the context
  424. if (readerContentPage != nil) readerContentPage = nil; // Release self
  425. }
  426. @end
  427. #pragma mark -
  428. //
  429. // ReaderDocumentLink class implementation
  430. //
  431. @implementation ReaderDocumentLink
  432. {
  433. CGPDFDictionaryRef _dictionary;
  434. CGRect _rect;
  435. }
  436. #pragma mark - Properties
  437. @synthesize rect = _rect;
  438. @synthesize dictionary = _dictionary;
  439. #pragma mark - ReaderDocumentLink class methods
  440. + (instancetype)newWithRect:(CGRect)linkRect dictionary:(CGPDFDictionaryRef)linkDictionary
  441. {
  442. return [[ReaderDocumentLink alloc] initWithRect:linkRect dictionary:linkDictionary];
  443. }
  444. #pragma mark - ReaderDocumentLink instance methods
  445. - (instancetype)initWithRect:(CGRect)linkRect dictionary:(CGPDFDictionaryRef)linkDictionary
  446. {
  447. if ((self = [super init]))
  448. {
  449. _dictionary = linkDictionary;
  450. _rect = linkRect;
  451. }
  452. return self;
  453. }
  454. @end