ReaderDocumentOutline.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. //
  2. // ReaderDocumentOutline.m
  3. // Reader v2.8.6
  4. //
  5. // Created by Julius Oklamcak on 2012-09-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 "ReaderDocumentOutline.h"
  26. #import "CGPDFDocument.h"
  27. @implementation ReaderDocumentOutline
  28. #pragma mark - Build option flags
  29. #define HIERARCHICAL_OUTLINE TRUE
  30. #pragma mark - ReaderDocumentOutline functions
  31. void logDictionaryEntry(const char *key, CGPDFObjectRef object, void *info)
  32. {
  33. //CGPDFDictionaryApplyFunction(dictionary, logDictionaryEntry, NULL);
  34. NSString *kind = nil; // CGPDFObject type
  35. CGPDFObjectType type = CGPDFObjectGetType(object);
  36. switch (type) // CGPDFObjectTypes
  37. {
  38. case kCGPDFObjectTypeNull:
  39. kind = @"CGPDFObjectTypeNull";
  40. NSLog(@"%s %@", key, kind);
  41. break;
  42. case kCGPDFObjectTypeBoolean:
  43. kind = @"CGPDFObjectTypeBoolean";
  44. NSLog(@"%s %@", key, kind);
  45. break;
  46. case kCGPDFObjectTypeInteger:
  47. kind = @"CGPDFObjectTypeInteger";
  48. NSLog(@"%s %@", key, kind);
  49. break;
  50. case kCGPDFObjectTypeReal:
  51. kind = @"CGPDFObjectTypeReal";
  52. NSLog(@"%s %@", key, kind);
  53. break;
  54. case kCGPDFObjectTypeName:
  55. {
  56. kind = @"CGPDFObjectTypeName"; const char *pdfName = NULL;
  57. if (CGPDFObjectGetValue(object, kCGPDFObjectTypeName, &pdfName))
  58. {
  59. if (pdfName != NULL) NSLog(@"%s %@ %s", key, kind, pdfName);
  60. }
  61. break;
  62. }
  63. case kCGPDFObjectTypeString:
  64. {
  65. kind = @"CGPDFObjectTypeString"; CGPDFStringRef pdfString = NULL;
  66. if (CGPDFObjectGetValue(object, kCGPDFObjectTypeString, &pdfString))
  67. {
  68. const unsigned char *string = CGPDFStringGetBytePtr(pdfString);
  69. if (string != NULL) NSLog(@"%s %@ %s", key, kind, string);
  70. }
  71. break;
  72. }
  73. case kCGPDFObjectTypeArray:
  74. kind = @"CGPDFObjectTypeArray";
  75. NSLog(@"%s %@", key, kind);
  76. break;
  77. case kCGPDFObjectTypeDictionary:
  78. kind = @"CGPDFObjectTypeDictionary";
  79. NSLog(@"%s %@", key, kind);
  80. break;
  81. case kCGPDFObjectTypeStream:
  82. kind = @"CGPDFObjectTypeStream";
  83. NSLog(@"%s %@", key, kind);
  84. break;
  85. }
  86. }
  87. #pragma mark - ReaderDocumentOutline class methods
  88. + (void)logDocumentOutlineArray:(NSArray *)array
  89. {
  90. for (DocumentOutlineEntry *item in array) // Enumerate array entries
  91. {
  92. NSInteger indent = (item.level * 2); // Indent amount for NSLog output
  93. NSLog(@"%@%@", [@"" stringByPaddingToLength:indent withString:@" " startingAtIndex:0], item);
  94. [self logDocumentOutlineArray:item.children]; // Log any child entries
  95. }
  96. }
  97. + (CGPDFArrayRef)destinationWithName:(const char *)destinationName inDestsTree:(CGPDFDictionaryRef)node
  98. {
  99. CGPDFArrayRef destinationArray = NULL;
  100. CGPDFArrayRef limitsArray = NULL; // Limits array
  101. if (CGPDFDictionaryGetArray(node, "Limits", &limitsArray) == true)
  102. {
  103. CGPDFStringRef lowerLimit = NULL; CGPDFStringRef upperLimit = NULL;
  104. if (CGPDFArrayGetString(limitsArray, 0, &lowerLimit) == true) // Lower limit
  105. {
  106. if (CGPDFArrayGetString(limitsArray, 1, &upperLimit) == true) // Upper limit
  107. {
  108. const char *ll = (const char *)CGPDFStringGetBytePtr(lowerLimit); // Lower string
  109. const char *ul = (const char *)CGPDFStringGetBytePtr(upperLimit); // Upper string
  110. if ((strcmp(destinationName, ll) < 0) || (strcmp(destinationName, ul) > 0))
  111. {
  112. return NULL; // Destination name is outside this node's limits
  113. }
  114. }
  115. }
  116. }
  117. CGPDFArrayRef namesArray = NULL; // Names array
  118. if (CGPDFDictionaryGetArray(node, "Names", &namesArray) == true)
  119. {
  120. NSInteger namesCount = CGPDFArrayGetCount(namesArray);
  121. for (NSInteger index = 0; index < namesCount; index += 2)
  122. {
  123. CGPDFStringRef destName; // Destination name string
  124. if (CGPDFArrayGetString(namesArray, index, &destName) == true)
  125. {
  126. const char *dn = (const char *)CGPDFStringGetBytePtr(destName);
  127. if (strcmp(dn, destinationName) == 0) // Found the destination name
  128. {
  129. if (CGPDFArrayGetArray(namesArray, (index + 1), &destinationArray) == false)
  130. {
  131. CGPDFDictionaryRef destinationDictionary = NULL; // Destination dictionary
  132. if (CGPDFArrayGetDictionary(namesArray, (index + 1), &destinationDictionary) == true)
  133. {
  134. CGPDFDictionaryGetArray(destinationDictionary, "D", &destinationArray);
  135. }
  136. }
  137. return destinationArray; // Return the destination array
  138. }
  139. }
  140. }
  141. }
  142. CGPDFArrayRef kidsArray = NULL; // Kids array
  143. if (CGPDFDictionaryGetArray(node, "Kids", &kidsArray) == true)
  144. {
  145. NSInteger kidsCount = CGPDFArrayGetCount(kidsArray);
  146. for (NSInteger index = 0; index < kidsCount; index++)
  147. {
  148. CGPDFDictionaryRef kidNode = NULL; // Kid node dictionary
  149. if (CGPDFArrayGetDictionary(kidsArray, index, &kidNode) == true) // Recurse into node
  150. {
  151. destinationArray = [self destinationWithName:destinationName inDestsTree:kidNode];
  152. if (destinationArray != NULL) return destinationArray; // Return destination array
  153. }
  154. }
  155. }
  156. return NULL;
  157. }
  158. + (id)outlineEntryTarget:(CGPDFDictionaryRef)outlineDictionary document:(CGPDFDocumentRef)document
  159. {
  160. id entryTarget = nil; // Entry target object
  161. CGPDFStringRef destName = NULL; const char *destString = NULL;
  162. CGPDFDictionaryRef actionDictionary = NULL; CGPDFArrayRef destArray = NULL;
  163. if (CGPDFDictionaryGetDictionary(outlineDictionary, "A", &actionDictionary) == true)
  164. {
  165. const char *actionType = NULL; // Outline entry action type string
  166. if (CGPDFDictionaryGetName(actionDictionary, "S", &actionType) == true)
  167. {
  168. if (strcmp(actionType, "GoTo") == 0) // GoTo action type
  169. {
  170. if (CGPDFDictionaryGetArray(actionDictionary, "D", &destArray) == false)
  171. {
  172. CGPDFDictionaryGetString(actionDictionary, "D", &destName);
  173. }
  174. }
  175. else // Handle other entry action type possibility
  176. {
  177. if (strcmp(actionType, "URI") == 0) // URI action type
  178. {
  179. CGPDFStringRef uriString = NULL; // Action's URI string
  180. if (CGPDFDictionaryGetString(actionDictionary, "URI", &uriString) == true)
  181. {
  182. const char *uri = (const char *)CGPDFStringGetBytePtr(uriString); // Destination URI string
  183. entryTarget = [NSURL URLWithString:[NSString stringWithCString:uri encoding:NSASCIIStringEncoding]];
  184. }
  185. }
  186. }
  187. }
  188. }
  189. else // Handle other entry target possibilities
  190. {
  191. if (CGPDFDictionaryGetArray(outlineDictionary, "Dest", &destArray) == false)
  192. {
  193. if (CGPDFDictionaryGetString(outlineDictionary, "Dest", &destName) == false)
  194. {
  195. CGPDFDictionaryGetName(outlineDictionary, "Dest", &destString);
  196. }
  197. }
  198. }
  199. if (destName != NULL) // Handle a destination name
  200. {
  201. CGPDFDictionaryRef catalogDictionary = CGPDFDocumentGetCatalog(document);
  202. CGPDFDictionaryRef namesDictionary = NULL; // Destination names in the document
  203. if (CGPDFDictionaryGetDictionary(catalogDictionary, "Names", &namesDictionary) == true)
  204. {
  205. CGPDFDictionaryRef destsDictionary = NULL; // Document destinations dictionary
  206. if (CGPDFDictionaryGetDictionary(namesDictionary, "Dests", &destsDictionary) == true)
  207. {
  208. const char *destinationName = (const char *)CGPDFStringGetBytePtr(destName); // Name
  209. destArray = [self destinationWithName:destinationName inDestsTree:destsDictionary];
  210. }
  211. }
  212. }
  213. if (destString != NULL) // Handle a destination string
  214. {
  215. CGPDFDictionaryRef catalogDictionary = CGPDFDocumentGetCatalog(document);
  216. CGPDFDictionaryRef destsDictionary = NULL; // Document destinations dictionary
  217. if (CGPDFDictionaryGetDictionary(catalogDictionary, "Dests", &destsDictionary) == true)
  218. {
  219. CGPDFDictionaryRef targetDictionary = NULL; // Destination target dictionary
  220. if (CGPDFDictionaryGetDictionary(destsDictionary, destString, &targetDictionary) == true)
  221. {
  222. CGPDFDictionaryGetArray(targetDictionary, "D", &destArray);
  223. }
  224. }
  225. }
  226. if (destArray != NULL) // Handle a destination array
  227. {
  228. NSInteger targetPageNumber = 0; // The target page number
  229. CGPDFDictionaryRef pageDictionaryFromDestArray = NULL; // Target reference
  230. if (CGPDFArrayGetDictionary(destArray, 0, &pageDictionaryFromDestArray) == true)
  231. {
  232. NSInteger pageCount = CGPDFDocumentGetNumberOfPages(document); // Pages
  233. for (NSInteger pageNumber = 1; pageNumber <= pageCount; pageNumber++)
  234. {
  235. CGPDFPageRef pageRef = CGPDFDocumentGetPage(document, pageNumber);
  236. CGPDFDictionaryRef pageDictionaryFromPage = CGPDFPageGetDictionary(pageRef);
  237. if (pageDictionaryFromPage == pageDictionaryFromDestArray) // Found it
  238. {
  239. targetPageNumber = pageNumber; break;
  240. }
  241. }
  242. }
  243. else // Try page number from array possibility
  244. {
  245. CGPDFInteger pageNumber = 0; // Page number in array
  246. if (CGPDFArrayGetInteger(destArray, 0, &pageNumber) == true)
  247. {
  248. targetPageNumber = (pageNumber + 1); // 1-based
  249. }
  250. }
  251. if (targetPageNumber > 0) // We have a target page number
  252. {
  253. entryTarget = [NSNumber numberWithInteger:targetPageNumber];
  254. }
  255. }
  256. return entryTarget;
  257. }
  258. + (void)outlineItems:(CGPDFDictionaryRef)outlineDictionary document:(CGPDFDocumentRef)document array:(NSMutableArray *)array level:(NSInteger)level
  259. {
  260. do // Loop through current level outline entries
  261. {
  262. DocumentOutlineEntry *outlineEntry = nil; // An entry
  263. CGPDFStringRef string = NULL; // Outline entry title string
  264. if (CGPDFDictionaryGetString(outlineDictionary, "Title", &string) == true)
  265. {
  266. CFStringRef title = NULL; // Actual outline title (CFObject) string
  267. if ((title = CGPDFStringCopyTextString(string)) != NULL) // Copy of CFObject string
  268. {
  269. NSString *titleString = (__bridge NSString *)title; // CFString to NSString toll-free bridge cast
  270. id entryTarget = [self outlineEntryTarget:outlineDictionary document:document]; // Get target object
  271. NSString *trimmed = [titleString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  272. outlineEntry = [DocumentOutlineEntry newWithTitle:trimmed target:entryTarget level:level]; // New entry
  273. [array addObject:outlineEntry]; CFRelease(title); // Add new entry and cleanup
  274. }
  275. }
  276. if (outlineEntry != nil) // Must have a current outline entry
  277. {
  278. CGPDFDictionaryRef childItem = NULL; // First child outline item
  279. if (CGPDFDictionaryGetDictionary(outlineDictionary, "First", &childItem) == true)
  280. {
  281. #if (HIERARCHICAL_OUTLINE == TRUE) // Option
  282. NSMutableArray *childArray = [NSMutableArray array]; outlineEntry.children = childArray;
  283. [self outlineItems:childItem document:document array:childArray level:(level + 1)];
  284. #else // Flat
  285. [self outlineItems:childItem document:document array:array level:(level + 1)];
  286. #endif // end of HIERARCHICAL_OUTLINE Option
  287. }
  288. }
  289. } while (CGPDFDictionaryGetDictionary(outlineDictionary, "Next", &outlineDictionary) == true);
  290. }
  291. + (NSArray *)outlineFromFileURL:(NSURL *)fileURL password:(NSString *)phrase
  292. {
  293. NSMutableArray *outlineArray = nil; // Mutable outline array
  294. if ((fileURL != nil) && [fileURL isFileURL]) // Check for valid file URL
  295. {
  296. CGPDFDocumentRef document = CGPDFDocumentCreateUsingUrl((__bridge CFURLRef)fileURL, phrase);
  297. if (document != NULL) // Check for non-NULL CGPDFDocumentRef
  298. {
  299. CGPDFDictionaryRef outlines = NULL; // Document's outlines
  300. CGPDFDictionaryRef catalog = CGPDFDocumentGetCatalog(document);
  301. if (CGPDFDictionaryGetDictionary(catalog, "Outlines", &outlines) == true)
  302. {
  303. CGPDFDictionaryRef firstItem = NULL; // First outline item entry
  304. if (CGPDFDictionaryGetDictionary(outlines, "First", &firstItem) == true)
  305. {
  306. outlineArray = [NSMutableArray array]; // Top level outline entries array
  307. [self outlineItems:firstItem document:document array:outlineArray level:0];
  308. }
  309. }
  310. CGPDFDocumentRelease(document); // Cleanup
  311. }
  312. }
  313. //[self logDocumentOutlineArray:outlineArray]; // Log it
  314. return [outlineArray copy]; // NSArray
  315. }
  316. @end
  317. #pragma mark -
  318. //
  319. // DocumentOutlineEntry class implementation
  320. //
  321. @interface DocumentOutlineEntry ()
  322. @property (nonatomic, assign, readwrite) NSInteger level;
  323. @property (nonatomic, strong, readwrite) NSString *title;
  324. @property (nonatomic, strong, readwrite) id target;
  325. @end
  326. @implementation DocumentOutlineEntry
  327. {
  328. NSInteger _level;
  329. NSMutableArray *_children;
  330. NSString *_title;
  331. id _target;
  332. }
  333. #pragma mark - Properties
  334. @synthesize level = _level;
  335. @synthesize children = _children;
  336. @synthesize target = _target;
  337. @synthesize title = _title;
  338. #pragma mark - DocumentOutlineEntry class methods
  339. + (instancetype)newWithTitle:(NSString *)title target:(id)target level:(NSInteger)level
  340. {
  341. return [[DocumentOutlineEntry alloc] initWithTitle:title target:target level:level];
  342. }
  343. #pragma mark - DocumentOutlineEntry instance methods
  344. - (instancetype)initWithTitle:(NSString *)title target:(id)target level:(NSInteger)level
  345. {
  346. if ((self = [super init]))
  347. {
  348. self.title = title; self.target = target; self.level = level;
  349. }
  350. return self;
  351. }
  352. - (NSString *)description
  353. {
  354. NSString *format = @"%@ Title = '%@', Target = '%@', Level = (%i)";
  355. return [[NSString alloc] initWithFormat:format, [super description], _title, _target, _level];
  356. }
  357. @end