document.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. #ifndef MUPDF_FITZ_DOCUMENT_H
  2. #define MUPDF_FITZ_DOCUMENT_H
  3. #include "mupdf/fitz/system.h"
  4. #include "mupdf/fitz/context.h"
  5. #include "mupdf/fitz/geometry.h"
  6. #include "mupdf/fitz/device.h"
  7. #include "mupdf/fitz/transition.h"
  8. #include "mupdf/fitz/link.h"
  9. #include "mupdf/fitz/outline.h"
  10. /*
  11. Document interface
  12. */
  13. typedef struct fz_document_s fz_document;
  14. typedef struct fz_document_handler_s fz_document_handler;
  15. typedef struct fz_page_s fz_page;
  16. typedef struct fz_annot_s fz_annot;
  17. typedef intptr_t fz_bookmark;
  18. typedef enum
  19. {
  20. FZ_PERMISSION_PRINT = 'p',
  21. FZ_PERMISSION_COPY = 'c',
  22. FZ_PERMISSION_EDIT = 'e',
  23. FZ_PERMISSION_ANNOTATE = 'n',
  24. }
  25. fz_permission;
  26. /*
  27. fz_document_drop_fn: Type for a function to be called when
  28. the reference count for the fz_document drops to 0. The
  29. implementation should release any resources held by the
  30. document. The actual document pointer will be freed by the
  31. caller.
  32. */
  33. typedef void (fz_document_drop_fn)(fz_context *ctx, fz_document *doc);
  34. /*
  35. fz_document_needs_password_fn: Type for a function to be
  36. called to enquire whether the document needs a password
  37. or not. See fz_needs_password for more information.
  38. */
  39. typedef int (fz_document_needs_password_fn)(fz_context *ctx, fz_document *doc);
  40. /*
  41. fz_document_authenticate_password_fn: Type for a function to be
  42. called to attempt to authenticate a password. See
  43. fz_authenticate_password for more information.
  44. */
  45. typedef int (fz_document_authenticate_password_fn)(fz_context *ctx, fz_document *doc, const char *password);
  46. /*
  47. fz_document_has_permission_fn: Type for a function to be
  48. called to see if a document grants a certain permission. See
  49. fz_document_has_permission for more information.
  50. */
  51. typedef int (fz_document_has_permission_fn)(fz_context *ctx, fz_document *doc, fz_permission permission);
  52. /*
  53. fz_document_load_outline_fn: Type for a function to be called to
  54. load the outlines for a document. See fz_document_load_outline
  55. for more information.
  56. */
  57. typedef fz_outline *(fz_document_load_outline_fn)(fz_context *ctx, fz_document *doc);
  58. /*
  59. fz_document_layout_fn: Type for a function to be called to lay
  60. out a document. See fz_layout_document for more information.
  61. */
  62. typedef void (fz_document_layout_fn)(fz_context *ctx, fz_document *doc, float w, float h, float em);
  63. /*
  64. fz_document_resolve_link_fn: Type for a function to be called to
  65. resolve an internal link to a page number. See fz_resolve_link
  66. for more information.
  67. */
  68. typedef int (fz_document_resolve_link_fn)(fz_context *ctx, fz_document *doc, const char *uri, float *xp, float *yp);
  69. /*
  70. fz_document_count_pages_fn: Type for a function to be called to
  71. count the number of pages in a document. See fz_count_pages for
  72. more information.
  73. */
  74. typedef int (fz_document_count_pages_fn)(fz_context *ctx, fz_document *doc);
  75. /*
  76. fz_document_load_page_fn: Type for a function to load a given
  77. page from a document. See fz_load_page for more information.
  78. */
  79. typedef fz_page *(fz_document_load_page_fn)(fz_context *ctx, fz_document *doc, int number);
  80. /*
  81. fz_document_lookup_metadata_fn: Type for a function to query
  82. a documents metadata. See fz_lookup_metadata for more
  83. information.
  84. */
  85. typedef int (fz_document_lookup_metadata_fn)(fz_context *ctx, fz_document *doc, const char *key, char *buf, int size);
  86. /*
  87. fz_document_make_bookmark_fn: Type for a function to make
  88. a bookmark. See fz_make_bookmark for more information.
  89. */
  90. typedef fz_bookmark (fz_document_make_bookmark_fn)(fz_context *ctx, fz_document *doc, int page);
  91. /*
  92. fz_document_lookup_bookmark_fn: Type for a function to lookup
  93. a bookmark. See fz_lookup_bookmark for more information.
  94. */
  95. typedef int (fz_document_lookup_bookmark_fn)(fz_context *ctx, fz_document *doc, fz_bookmark mark);
  96. /*
  97. fz_page_drop_page_fn: Type for a function to release all the
  98. resources held by a page. Called automatically when the
  99. reference count for that page reaches zero.
  100. */
  101. typedef void (fz_page_drop_page_fn)(fz_context *ctx, fz_page *page);
  102. /*
  103. fz_page_bound_page_fn: Type for a function to return the
  104. bounding box of a page. See fz_bound_page for more
  105. information.
  106. */
  107. typedef fz_rect *(fz_page_bound_page_fn)(fz_context *ctx, fz_page *page, fz_rect *);
  108. /*
  109. fz_page_run_page_contents_fn: Type for a function to run the
  110. contents of a page. See fz_run_page_contents for more
  111. information.
  112. */
  113. typedef void (fz_page_run_page_contents_fn)(fz_context *ctx, fz_page *page, fz_device *dev, const fz_matrix *transform, fz_cookie *cookie);
  114. /*
  115. fz_page_load_links_fn: Type for a function to load the links
  116. from a page. See fz_load_links for more information.
  117. */
  118. typedef fz_link *(fz_page_load_links_fn)(fz_context *ctx, fz_page *page);
  119. /*
  120. fz_page_first_annot_fn: Type for a function to load the
  121. annotations from a page. See fz_first_annot for more
  122. information.
  123. */
  124. typedef fz_annot *(fz_page_first_annot_fn)(fz_context *ctx, fz_page *page);
  125. /*
  126. fz_page_page_presentation_fn: Type for a function to
  127. obtain the details of how this page should be presented when
  128. in presentation mode. See fz_page_presentation for more
  129. information.
  130. */
  131. typedef fz_transition *(fz_page_page_presentation_fn)(fz_context *ctx, fz_page *page, fz_transition *transition, float *duration);
  132. /*
  133. fz_page_control_separation: Type for a function to enable/
  134. disable separations on a page. See fz_control_separation for
  135. more information.
  136. */
  137. typedef void (fz_page_control_separation_fn)(fz_context *ctx, fz_page *page, int separation, int disable);
  138. /*
  139. fz_page_separation_disabled_fn: Type for a function to detect
  140. whether a given separation is enabled or disabled on a page.
  141. See fz_separation_disabled for more information.
  142. */
  143. typedef int (fz_page_separation_disabled_fn)(fz_context *ctx, fz_page *page, int separation);
  144. /*
  145. fz_page_count_separations_fn: Type for a function to count
  146. the number of separations on a page. See fz_count_separations
  147. for more information.
  148. */
  149. typedef int (fz_page_count_separations_fn)(fz_context *ctx, fz_page *page);
  150. /*
  151. fz_page_get_separation_fn: Type for a function to retrieve
  152. details of a separation on a page. See fz_get_separation
  153. for more information.
  154. */
  155. typedef const char *(fz_page_get_separation_fn)(fz_context *ctx, fz_page *page, int separation, uint32_t *rgb, uint32_t *cmyk);
  156. typedef void (fz_annot_drop_fn)(fz_context *ctx, fz_annot *annot);
  157. typedef fz_annot *(fz_annot_next_fn)(fz_context *ctx, fz_annot *annot);
  158. typedef fz_rect *(fz_annot_bound_fn)(fz_context *ctx, fz_annot *annot, fz_rect *rect);
  159. typedef void (fz_annot_run_fn)(fz_context *ctx, fz_annot *annot, fz_device *dev, const fz_matrix *transform, fz_cookie *cookie);
  160. /*
  161. Structure definition is public so other classes can
  162. derive from it. Do not access the members directly.
  163. */
  164. struct fz_annot_s
  165. {
  166. int refs;
  167. fz_annot_drop_fn *drop_annot;
  168. fz_annot_bound_fn *bound_annot;
  169. fz_annot_run_fn *run_annot;
  170. fz_annot_next_fn *next_annot;
  171. };
  172. /*
  173. Structure definition is public so other classes can
  174. derive from it. Do not access the members directly.
  175. */
  176. struct fz_page_s
  177. {
  178. int refs;
  179. fz_page_drop_page_fn *drop_page;
  180. fz_page_bound_page_fn *bound_page;
  181. fz_page_run_page_contents_fn *run_page_contents;
  182. fz_page_load_links_fn *load_links;
  183. fz_page_first_annot_fn *first_annot;
  184. fz_page_page_presentation_fn *page_presentation;
  185. fz_page_control_separation_fn *control_separation;
  186. fz_page_separation_disabled_fn *separation_disabled;
  187. fz_page_count_separations_fn *count_separations;
  188. fz_page_get_separation_fn *get_separation;
  189. };
  190. /*
  191. Structure definition is public so other classes can
  192. derive from it. Callers shoud not access the members
  193. directly, though implementations will need initialize
  194. functions directly.
  195. */
  196. struct fz_document_s
  197. {
  198. int refs;
  199. fz_document_drop_fn *drop_document;
  200. fz_document_needs_password_fn *needs_password;
  201. fz_document_authenticate_password_fn *authenticate_password;
  202. fz_document_has_permission_fn *has_permission;
  203. fz_document_load_outline_fn *load_outline;
  204. fz_document_layout_fn *layout;
  205. fz_document_make_bookmark_fn *make_bookmark;
  206. fz_document_lookup_bookmark_fn *lookup_bookmark;
  207. fz_document_resolve_link_fn *resolve_link;
  208. fz_document_count_pages_fn *count_pages;
  209. fz_document_load_page_fn *load_page;
  210. fz_document_lookup_metadata_fn *lookup_metadata;
  211. int did_layout;
  212. int is_reflowable;
  213. };
  214. /*
  215. fz_document_open_fn: Function type to open a document from a
  216. file.
  217. filename: file to open
  218. Pointer to opened document. Throws exception in case of error.
  219. */
  220. typedef fz_document *(fz_document_open_fn)(fz_context *ctx, const char *filename);
  221. /*
  222. fz_document_open_with_stream_fn: Function type to open a
  223. document from a file.
  224. stream: fz_stream to read document data from. Must be
  225. seekable for formats that require it.
  226. Pointer to opened document. Throws exception in case of error.
  227. */
  228. typedef fz_document *(fz_document_open_with_stream_fn)(fz_context *ctx, fz_stream *stream);
  229. /*
  230. fz_document_recognize_fn: Recognize a document type from
  231. a magic string.
  232. magic: string to recognise - typically a filename or mime
  233. type.
  234. Returns a number between 0 (not recognized) and 100
  235. (fully recognized) based on how certain the recognizer
  236. is that this is of the required type.
  237. */
  238. typedef int (fz_document_recognize_fn)(fz_context *ctx, const char *magic);
  239. struct fz_document_handler_s
  240. {
  241. fz_document_recognize_fn *recognize;
  242. fz_document_open_fn *open;
  243. fz_document_open_with_stream_fn *open_with_stream;
  244. };
  245. /*
  246. fz_register_document_handler: Register a handler
  247. for a document type.
  248. handler: The handler to register.
  249. */
  250. void fz_register_document_handler(fz_context *ctx, const fz_document_handler *handler);
  251. /*
  252. fz_register_document_handler: Register handlers
  253. for all the standard document types supported in
  254. this build.
  255. */
  256. void fz_register_document_handlers(fz_context *ctx);
  257. /*
  258. fz_open_document: Open a PDF, XPS or CBZ document.
  259. Open a document file and read its basic structure so pages and
  260. objects can be located. MuPDF will try to repair broken
  261. documents (without actually changing the file contents).
  262. The returned fz_document is used when calling most other
  263. document related functions.
  264. filename: a path to a file as it would be given to open(2).
  265. */
  266. fz_document *fz_open_document(fz_context *ctx, const char *filename);
  267. /*
  268. fz_open_document_with_stream: Open a PDF, XPS or CBZ document.
  269. Open a document using the specified stream object rather than
  270. opening a file on disk.
  271. magic: a string used to detect document type; either a file name or mime-type.
  272. */
  273. fz_document *fz_open_document_with_stream(fz_context *ctx, const char *magic, fz_stream *stream);
  274. /*
  275. fz_new_document: Create and initialize a document struct.
  276. */
  277. void *fz_new_document_of_size(fz_context *ctx, int size);
  278. #define fz_new_derived_document(C,M) ((M*)Memento_label(fz_new_document_of_size(C, sizeof(M)), #M))
  279. /*
  280. fz_keep_document: Keep a reference to an open document.
  281. Does not throw exceptions.
  282. */
  283. fz_document *fz_keep_document(fz_context *ctx, fz_document *doc);
  284. /*
  285. fz_drop_document: Release an open document.
  286. The resource store in the context associated with fz_document
  287. is emptied, and any allocations for the document are freed when
  288. the last reference is dropped.
  289. Does not throw exceptions.
  290. */
  291. void fz_drop_document(fz_context *ctx, fz_document *doc);
  292. /*
  293. fz_needs_password: Check if a document is encrypted with a
  294. non-blank password.
  295. Does not throw exceptions.
  296. */
  297. int fz_needs_password(fz_context *ctx, fz_document *doc);
  298. /*
  299. fz_authenticate_password: Test if the given password can
  300. decrypt the document.
  301. password: The password string to be checked. Some document
  302. specifications do not specify any particular text encoding, so
  303. neither do we.
  304. Returns 0 for failure to authenticate, non-zero for success.
  305. For PDF documents, further information can be given by examining
  306. the bits in the return code.
  307. Bit 0 => No password required
  308. Bit 1 => User password authenticated
  309. Bit 2 => Owner password authenticated
  310. Does not throw exceptions.
  311. */
  312. int fz_authenticate_password(fz_context *ctx, fz_document *doc, const char *password);
  313. /*
  314. fz_load_outline: Load the hierarchical document outline.
  315. Should be freed by fz_drop_outline.
  316. */
  317. fz_outline *fz_load_outline(fz_context *ctx, fz_document *doc);
  318. /*
  319. fz_is_document_reflowable: Is the document reflowable.
  320. Returns 1 to indicate reflowable documents, otherwise 0.
  321. */
  322. int fz_is_document_reflowable(fz_context *ctx, fz_document *doc);
  323. /*
  324. fz_layout_document: Layout reflowable document types.
  325. w, h: Page size in points.
  326. em: Default font size in points.
  327. */
  328. void fz_layout_document(fz_context *ctx, fz_document *doc, float w, float h, float em);
  329. /*
  330. Create a bookmark for the given page, which can be used to find the
  331. same location after the document has been laid out with different
  332. parameters.
  333. */
  334. fz_bookmark fz_make_bookmark(fz_context *ctx, fz_document *doc, int page);
  335. /*
  336. Find a bookmark and return its page number.
  337. */
  338. int fz_lookup_bookmark(fz_context *ctx, fz_document *doc, fz_bookmark mark);
  339. /*
  340. fz_count_pages: Return the number of pages in document
  341. May return 0 for documents with no pages.
  342. */
  343. int fz_count_pages(fz_context *ctx, fz_document *doc);
  344. /*
  345. fz_resolve_link: Resolve an internal link to a page number.
  346. xp, yp: Pointer to store coordinate of destination on the page.
  347. Returns -1 if the URI cannot be resolved.
  348. */
  349. int fz_resolve_link(fz_context *ctx, fz_document *doc, const char *uri, float *xp, float *yp);
  350. /*
  351. fz_load_page: Load a page.
  352. After fz_load_page is it possible to retrieve the size of the
  353. page using fz_bound_page, or to render the page using
  354. fz_run_page_*. Free the page by calling fz_drop_page.
  355. number: page number, 0 is the first page of the document.
  356. */
  357. fz_page *fz_load_page(fz_context *ctx, fz_document *doc, int number);
  358. /*
  359. fz_load_links: Load the list of links for a page.
  360. Returns a linked list of all the links on the page, each with
  361. its clickable region and link destination. Each link is
  362. reference counted so drop and free the list of links by
  363. calling fz_drop_link on the pointer return from fz_load_links.
  364. page: Page obtained from fz_load_page.
  365. */
  366. fz_link *fz_load_links(fz_context *ctx, fz_page *page);
  367. /*
  368. fz_new_page_of_size: Create and initialize a page struct.
  369. */
  370. fz_page *fz_new_page_of_size(fz_context *ctx, int size);
  371. #define fz_new_derived_page(CTX,TYPE) \
  372. ((TYPE *)Memento_label(fz_new_page_of_size(CTX,sizeof(TYPE)),#TYPE))
  373. /*
  374. fz_bound_page: Determine the size of a page at 72 dpi.
  375. Does not throw exceptions.
  376. */
  377. fz_rect *fz_bound_page(fz_context *ctx, fz_page *page, fz_rect *rect);
  378. /*
  379. fz_run_page: Run a page through a device.
  380. page: Page obtained from fz_load_page.
  381. dev: Device obtained from fz_new_*_device.
  382. transform: Transform to apply to page. May include for example
  383. scaling and rotation, see fz_scale, fz_rotate and fz_concat.
  384. Set to fz_identity if no transformation is desired.
  385. cookie: Communication mechanism between caller and library
  386. rendering the page. Intended for multi-threaded applications,
  387. while single-threaded applications set cookie to NULL. The
  388. caller may abort an ongoing rendering of a page. Cookie also
  389. communicates progress information back to the caller. The
  390. fields inside cookie are continually updated while the page is
  391. rendering.
  392. */
  393. void fz_run_page(fz_context *ctx, fz_page *page, fz_device *dev, const fz_matrix *transform, fz_cookie *cookie);
  394. /*
  395. fz_run_page_contents: Run a page through a device. Just the main
  396. page content, without the annotations, if any.
  397. page: Page obtained from fz_load_page.
  398. dev: Device obtained from fz_new_*_device.
  399. transform: Transform to apply to page. May include for example
  400. scaling and rotation, see fz_scale, fz_rotate and fz_concat.
  401. Set to fz_identity if no transformation is desired.
  402. cookie: Communication mechanism between caller and library
  403. rendering the page. Intended for multi-threaded applications,
  404. while single-threaded applications set cookie to NULL. The
  405. caller may abort an ongoing rendering of a page. Cookie also
  406. communicates progress information back to the caller. The
  407. fields inside cookie are continually updated while the page is
  408. rendering.
  409. */
  410. void fz_run_page_contents(fz_context *ctx, fz_page *page, fz_device *dev, const fz_matrix *transform, fz_cookie *cookie);
  411. /*
  412. fz_run_annot: Run an annotation through a device.
  413. page: Page obtained from fz_load_page.
  414. annot: an annotation.
  415. dev: Device obtained from fz_new_*_device.
  416. transform: Transform to apply to page. May include for example
  417. scaling and rotation, see fz_scale, fz_rotate and fz_concat.
  418. Set to fz_identity if no transformation is desired.
  419. cookie: Communication mechanism between caller and library
  420. rendering the page. Intended for multi-threaded applications,
  421. while single-threaded applications set cookie to NULL. The
  422. caller may abort an ongoing rendering of a page. Cookie also
  423. communicates progress information back to the caller. The
  424. fields inside cookie are continually updated while the page is
  425. rendering.
  426. */
  427. void fz_run_annot(fz_context *ctx, fz_annot *annot, fz_device *dev, const fz_matrix *transform, fz_cookie *cookie);
  428. /*
  429. fz_keep_page: Keep a reference to a loaded page.
  430. Does not throw exceptions.
  431. */
  432. fz_page *fz_keep_page(fz_context *ctx, fz_page *page);
  433. /*
  434. fz_drop_page: Free a loaded page.
  435. Does not throw exceptions.
  436. */
  437. void fz_drop_page(fz_context *ctx, fz_page *page);
  438. /*
  439. fz_page_presentation: Get the presentation details for a given page.
  440. transition: A pointer to a transition struct to fill out.
  441. duration: A pointer to a place to set the page duration in seconds.
  442. Will be set to 0 if no transition is specified for the page.
  443. Returns: a pointer to the transition structure, or NULL if there is no
  444. transition specified for the page.
  445. */
  446. fz_transition *fz_page_presentation(fz_context *ctx, fz_page *page, fz_transition *transition, float *duration);
  447. /*
  448. fz_has_permission: Check permission flags on document.
  449. */
  450. int fz_has_permission(fz_context *ctx, fz_document *doc, fz_permission p);
  451. /*
  452. fz_lookup_metadata: Retrieve document meta data strings.
  453. doc: The document to query.
  454. key: Which meta data key to retrieve...
  455. Basic information:
  456. 'format' -- Document format and version.
  457. 'encryption' -- Description of the encryption used.
  458. From the document information dictionary:
  459. 'info:Title'
  460. 'info:Author'
  461. 'info:Subject'
  462. 'info:Keywords'
  463. 'info:Creator'
  464. 'info:Producer'
  465. 'info:CreationDate'
  466. 'info:ModDate'
  467. buf: The buffer to hold the results (a nul-terminated UTF-8 string).
  468. size: Size of 'buf'.
  469. Returns the size of the output string (may be larger than 'size' if
  470. the output was truncated), or -1 if the key is not recognized or found.
  471. */
  472. int fz_lookup_metadata(fz_context *ctx, fz_document *doc, const char *key, char *buf, int size);
  473. #define FZ_META_FORMAT "format"
  474. #define FZ_META_ENCRYPTION "encryption"
  475. #define FZ_META_INFO_AUTHOR "info:Author"
  476. #define FZ_META_INFO_TITLE "info:Title"
  477. /*
  478. Get the number of separations on a page (including CMYK). This will
  479. be 0, unless the format specifically supports separations (such as
  480. gproof files).
  481. */
  482. int fz_count_separations_on_page(fz_context *ctx, fz_page *page);
  483. /*
  484. Enable/Disable a given separation on a given page. This will only
  485. affect future renderings of pages from a format that supports
  486. separations (such as gproof files).
  487. */
  488. void fz_control_separation_on_page(fz_context *ctx, fz_page *page, int sep, int disable);
  489. /*
  490. Returns whether a given separation on a given page is disabled. This will only
  491. work from a format that supports separations (such as gproof files).
  492. */
  493. int fz_separation_disabled_on_page (fz_context *ctx, fz_page *, int sep);
  494. /*
  495. Get the name and equivalent RGBA, CMYK colors of a given separation
  496. on a given page. This will only work for formats that support
  497. gproof files.
  498. */
  499. const char *fz_get_separation_on_page(fz_context *ctx, fz_page *page, int sep, uint32_t *rgba, uint32_t *cmyk);
  500. /*
  501. fz_save_gproof: Given a currently open document, create a
  502. gproof skeleton file from that document.
  503. doc_filename: The name of the currently opened document file.
  504. doc: The currently opened document.
  505. filename: The filename of the desired gproof file.
  506. res: The resolution at which proofing should be done.
  507. print_profile: The filename of the ICC profile for the printer we are proofing
  508. display_profile: The filename of the ICC profile for our display device
  509. */
  510. void fz_save_gproof(fz_context *ctx, const char *doc_filename, fz_document *doc, const char *filename, int res,
  511. const char *print_profile, const char *display_profile);
  512. #endif