doc_document.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include "fitz-internal.h"
  2. /* Yuck! Promiscuous we are. */
  3. extern struct pdf_document *pdf_open_document(fz_context *ctx, const char *filename);
  4. extern struct xps_document *xps_open_document(fz_context *ctx, const char *filename);
  5. extern struct cbz_document *cbz_open_document(fz_context *ctx, const char *filename);
  6. extern struct pdf_document *pdf_open_document_with_stream(fz_context *ctx, fz_stream *file);
  7. extern struct xps_document *xps_open_document_with_stream(fz_context *ctx, fz_stream *file);
  8. extern struct cbz_document *cbz_open_document_with_stream(fz_context *ctx, fz_stream *file);
  9. extern int pdf_js_supported(void);
  10. static inline int fz_tolower(int c)
  11. {
  12. if (c >= 'A' && c <= 'Z')
  13. return c + 32;
  14. return c;
  15. }
  16. static inline int fz_strcasecmp(const char *a, const char *b)
  17. {
  18. while (fz_tolower(*a) == fz_tolower(*b))
  19. {
  20. if (*a++ == 0)
  21. return 0;
  22. b++;
  23. }
  24. return fz_tolower(*a) - fz_tolower(*b);
  25. }
  26. fz_document *
  27. fz_open_document_with_stream(fz_context *ctx, const char *magic, fz_stream *stream)
  28. {
  29. char *ext = strrchr(magic, '.');
  30. if (ext)
  31. {
  32. if (!fz_strcasecmp(ext, ".xps") || !fz_strcasecmp(ext, ".rels"))
  33. return (fz_document*) xps_open_document_with_stream(ctx, stream);
  34. if (!fz_strcasecmp(ext, ".cbz") || !fz_strcasecmp(ext, ".zip"))
  35. return (fz_document*) cbz_open_document_with_stream(ctx, stream);
  36. if (!fz_strcasecmp(ext, ".pdf"))
  37. return (fz_document*) pdf_open_document_with_stream(ctx, stream);
  38. }
  39. if (!strcmp(magic, "cbz") || !strcmp(magic, "application/x-cbz"))
  40. return (fz_document*) cbz_open_document_with_stream(ctx, stream);
  41. if (!strcmp(magic, "xps") || !strcmp(magic, "application/vnd.ms-xpsdocument"))
  42. return (fz_document*) xps_open_document_with_stream(ctx, stream);
  43. if (!strcmp(magic, "pdf") || !strcmp(magic, "application/pdf"))
  44. return (fz_document*) pdf_open_document_with_stream(ctx, stream);
  45. /* last guess: pdf */
  46. return (fz_document*) pdf_open_document_with_stream(ctx, stream);
  47. }
  48. fz_document *
  49. fz_open_document(fz_context *ctx, const char *filename)
  50. {
  51. char *ext = strrchr(filename, '.');
  52. if (ext)
  53. {
  54. if (!fz_strcasecmp(ext, ".xps") || !fz_strcasecmp(ext, ".rels") || !fz_strcasecmp(ext, ".oxps"))
  55. return (fz_document*) xps_open_document(ctx, filename);
  56. if (!fz_strcasecmp(ext, ".cbz") || !fz_strcasecmp(ext, ".zip"))
  57. return (fz_document*) cbz_open_document(ctx, filename);
  58. if (!fz_strcasecmp(ext, ".pdf"))
  59. return (fz_document*) pdf_open_document(ctx, filename);
  60. }
  61. /* last guess: pdf */
  62. return (fz_document*) pdf_open_document(ctx, filename);
  63. }
  64. void
  65. fz_close_document(fz_document *doc)
  66. {
  67. if (doc && doc->close)
  68. doc->close(doc);
  69. }
  70. int
  71. fz_needs_password(fz_document *doc)
  72. {
  73. if (doc && doc->needs_password)
  74. return doc->needs_password(doc);
  75. return 0;
  76. }
  77. int
  78. fz_authenticate_password(fz_document *doc, char *password)
  79. {
  80. if (doc && doc->authenticate_password)
  81. return doc->authenticate_password(doc, password);
  82. return 1;
  83. }
  84. fz_outline *
  85. fz_load_outline(fz_document *doc)
  86. {
  87. if (doc && doc->load_outline)
  88. return doc->load_outline(doc);
  89. return NULL;
  90. }
  91. int
  92. fz_count_pages(fz_document *doc)
  93. {
  94. if (doc && doc->count_pages)
  95. return doc->count_pages(doc);
  96. return 0;
  97. }
  98. fz_page *
  99. fz_load_page(fz_document *doc, int number)
  100. {
  101. if (doc && doc->load_page)
  102. return doc->load_page(doc, number);
  103. return NULL;
  104. }
  105. fz_link *
  106. fz_load_links(fz_document *doc, fz_page *page)
  107. {
  108. if (doc && doc->load_links && page)
  109. return doc->load_links(doc, page);
  110. return NULL;
  111. }
  112. fz_rect *
  113. fz_bound_page(fz_document *doc, fz_page *page, fz_rect *r)
  114. {
  115. if (doc && doc->bound_page && page && r)
  116. return doc->bound_page(doc, page, r);
  117. if (r)
  118. *r = fz_empty_rect;
  119. return r;
  120. }
  121. fz_annot *
  122. fz_first_annot(fz_document *doc, fz_page *page)
  123. {
  124. if (doc && doc->first_annot && page)
  125. return doc->first_annot(doc, page);
  126. return NULL;
  127. }
  128. fz_annot *
  129. fz_next_annot(fz_document *doc, fz_annot *annot)
  130. {
  131. if (doc && doc->next_annot && annot)
  132. return doc->next_annot(doc, annot);
  133. return NULL;
  134. }
  135. fz_rect *
  136. fz_bound_annot(fz_document *doc, fz_annot *annot, fz_rect *rect)
  137. {
  138. if (doc && doc->bound_annot && annot && rect)
  139. return doc->bound_annot(doc, annot, rect);
  140. if (rect)
  141. *rect = fz_empty_rect;
  142. return rect;
  143. }
  144. void
  145. fz_run_page_contents(fz_document *doc, fz_page *page, fz_device *dev, const fz_matrix *transform, fz_cookie *cookie)
  146. {
  147. if (doc && doc->run_page_contents && page)
  148. doc->run_page_contents(doc, page, dev, transform, cookie);
  149. }
  150. void
  151. fz_run_annot(fz_document *doc, fz_page *page, fz_annot *annot, fz_device *dev, const fz_matrix *transform, fz_cookie *cookie)
  152. {
  153. if (doc && doc->run_annot && page && annot)
  154. doc->run_annot(doc, page, annot, dev, transform, cookie);
  155. }
  156. void
  157. fz_run_page(fz_document *doc, fz_page *page, fz_device *dev, const fz_matrix *transform, fz_cookie *cookie)
  158. {
  159. fz_annot *annot;
  160. fz_run_page_contents(doc, page, dev, transform, cookie);
  161. if (cookie && cookie->progress_max != -1)
  162. {
  163. int count = 1;
  164. for (annot = fz_first_annot(doc, page); annot; annot = fz_next_annot(doc, annot))
  165. count++;
  166. cookie->progress_max += count;
  167. }
  168. for (annot = fz_first_annot(doc, page); annot; annot = fz_next_annot(doc, annot))
  169. {
  170. /* Check the cookie for aborting */
  171. if (cookie)
  172. {
  173. if (cookie->abort)
  174. break;
  175. cookie->progress++;
  176. }
  177. fz_run_annot(doc, page, annot, dev, transform, cookie);
  178. }
  179. }
  180. void
  181. fz_free_page(fz_document *doc, fz_page *page)
  182. {
  183. if (doc && doc->free_page && page)
  184. doc->free_page(doc, page);
  185. }
  186. int
  187. fz_meta(fz_document *doc, int key, void *ptr, int size)
  188. {
  189. if (doc && doc->meta)
  190. return doc->meta(doc, key, ptr, size);
  191. return FZ_META_UNKNOWN_KEY;
  192. }
  193. fz_transition *
  194. fz_page_presentation(fz_document *doc, fz_page *page, float *duration)
  195. {
  196. float dummy;
  197. if (duration)
  198. *duration = 0;
  199. else
  200. duration = &dummy;
  201. if (doc && doc->page_presentation && page)
  202. return doc->page_presentation(doc, page, duration);
  203. return NULL;
  204. }
  205. fz_interactive *fz_interact(fz_document *doc)
  206. {
  207. if (doc && doc->interact)
  208. return doc->interact(doc);
  209. return NULL;
  210. }
  211. int fz_javascript_supported()
  212. {
  213. return pdf_js_supported();
  214. }
  215. void
  216. fz_write_document(fz_document *doc, char *filename, fz_write_options *opts)
  217. {
  218. if (doc && doc->write)
  219. doc->write(doc, filename, opts);
  220. }