document.h 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. // Copyright (C) 2004-2025 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #ifndef MUPDF_FITZ_DOCUMENT_H
  23. #define MUPDF_FITZ_DOCUMENT_H
  24. #include "mupdf/fitz/system.h"
  25. #include "mupdf/fitz/types.h"
  26. #include "mupdf/fitz/context.h"
  27. #include "mupdf/fitz/geometry.h"
  28. #include "mupdf/fitz/device.h"
  29. #include "mupdf/fitz/transition.h"
  30. #include "mupdf/fitz/link.h"
  31. #include "mupdf/fitz/outline.h"
  32. #include "mupdf/fitz/separation.h"
  33. #include "mupdf/fitz/archive.h"
  34. typedef struct fz_document_handler fz_document_handler;
  35. typedef struct fz_page fz_page;
  36. typedef intptr_t fz_bookmark;
  37. typedef enum
  38. {
  39. FZ_MEDIA_BOX,
  40. FZ_CROP_BOX,
  41. FZ_BLEED_BOX,
  42. FZ_TRIM_BOX,
  43. FZ_ART_BOX,
  44. FZ_UNKNOWN_BOX
  45. } fz_box_type;
  46. fz_box_type fz_box_type_from_string(const char *name);
  47. const char *fz_string_from_box_type(fz_box_type box);
  48. /**
  49. Simple constructor for fz_locations.
  50. */
  51. static inline fz_location fz_make_location(int chapter, int page)
  52. {
  53. fz_location loc = { chapter, page };
  54. return loc;
  55. }
  56. enum
  57. {
  58. /* 6in at 4:3 */
  59. FZ_LAYOUT_KINDLE_W = 260,
  60. FZ_LAYOUT_KINDLE_H = 346,
  61. FZ_LAYOUT_KINDLE_EM = 9,
  62. /* 4.25 x 6.87 in */
  63. FZ_LAYOUT_US_POCKET_W = 306,
  64. FZ_LAYOUT_US_POCKET_H = 495,
  65. FZ_LAYOUT_US_POCKET_EM = 10,
  66. /* 5.5 x 8.5 in */
  67. FZ_LAYOUT_US_TRADE_W = 396,
  68. FZ_LAYOUT_US_TRADE_H = 612,
  69. FZ_LAYOUT_US_TRADE_EM = 11,
  70. /* 110 x 178 mm */
  71. FZ_LAYOUT_UK_A_FORMAT_W = 312,
  72. FZ_LAYOUT_UK_A_FORMAT_H = 504,
  73. FZ_LAYOUT_UK_A_FORMAT_EM = 10,
  74. /* 129 x 198 mm */
  75. FZ_LAYOUT_UK_B_FORMAT_W = 366,
  76. FZ_LAYOUT_UK_B_FORMAT_H = 561,
  77. FZ_LAYOUT_UK_B_FORMAT_EM = 10,
  78. /* 135 x 216 mm */
  79. FZ_LAYOUT_UK_C_FORMAT_W = 382,
  80. FZ_LAYOUT_UK_C_FORMAT_H = 612,
  81. FZ_LAYOUT_UK_C_FORMAT_EM = 11,
  82. /* 148 x 210 mm */
  83. FZ_LAYOUT_A5_W = 420,
  84. FZ_LAYOUT_A5_H = 595,
  85. FZ_LAYOUT_A5_EM = 11,
  86. /* Default to A5 */
  87. FZ_DEFAULT_LAYOUT_W = FZ_LAYOUT_A5_W,
  88. FZ_DEFAULT_LAYOUT_H = FZ_LAYOUT_A5_H,
  89. FZ_DEFAULT_LAYOUT_EM = FZ_LAYOUT_A5_EM,
  90. };
  91. typedef enum
  92. {
  93. FZ_PERMISSION_PRINT = 'p',
  94. FZ_PERMISSION_COPY = 'c',
  95. FZ_PERMISSION_EDIT = 'e',
  96. FZ_PERMISSION_ANNOTATE = 'n',
  97. FZ_PERMISSION_FORM = 'f',
  98. FZ_PERMISSION_ACCESSIBILITY = 'y',
  99. FZ_PERMISSION_ASSEMBLE = 'a',
  100. FZ_PERMISSION_PRINT_HQ = 'h',
  101. }
  102. fz_permission;
  103. /**
  104. Type for a function to be called when
  105. the reference count for the fz_document drops to 0. The
  106. implementation should release any resources held by the
  107. document. The actual document pointer will be freed by the
  108. caller.
  109. */
  110. typedef void (fz_document_drop_fn)(fz_context *ctx, fz_document *doc);
  111. /**
  112. Type for a function to be
  113. called to enquire whether the document needs a password
  114. or not. See fz_needs_password for more information.
  115. */
  116. typedef int (fz_document_needs_password_fn)(fz_context *ctx, fz_document *doc);
  117. /**
  118. Type for a function to be
  119. called to attempt to authenticate a password. See
  120. fz_authenticate_password for more information.
  121. */
  122. typedef int (fz_document_authenticate_password_fn)(fz_context *ctx, fz_document *doc, const char *password);
  123. /**
  124. Type for a function to be
  125. called to see if a document grants a certain permission. See
  126. fz_document_has_permission for more information.
  127. */
  128. typedef int (fz_document_has_permission_fn)(fz_context *ctx, fz_document *doc, fz_permission permission);
  129. /**
  130. Type for a function to be called to
  131. load the outlines for a document. See fz_document_load_outline
  132. for more information.
  133. */
  134. typedef fz_outline *(fz_document_load_outline_fn)(fz_context *ctx, fz_document *doc);
  135. /**
  136. Type for a function to be called to obtain an outline iterator
  137. for a document. See fz_document_outline_iterator for more information.
  138. */
  139. typedef fz_outline_iterator *(fz_document_outline_iterator_fn)(fz_context *ctx, fz_document *doc);
  140. /**
  141. Type for a function to be called to lay
  142. out a document. See fz_layout_document for more information.
  143. */
  144. typedef void (fz_document_layout_fn)(fz_context *ctx, fz_document *doc, float w, float h, float em);
  145. /**
  146. Type for a function to be called to
  147. resolve an internal link to a location (chapter/page number
  148. tuple). See fz_resolve_link_dest for more information.
  149. */
  150. typedef fz_link_dest (fz_document_resolve_link_dest_fn)(fz_context *ctx, fz_document *doc, const char *uri);
  151. /**
  152. Type for a function to be called to
  153. create an internal link to a destination (chapter/page/x/y/w/h/zoom/type
  154. tuple). See fz_resolve_link_dest for more information.
  155. */
  156. typedef char * (fz_document_format_link_uri_fn)(fz_context *ctx, fz_document *doc, fz_link_dest dest);
  157. /**
  158. Type for a function to be called to
  159. count the number of chapters in a document. See
  160. fz_count_chapters for more information.
  161. */
  162. typedef int (fz_document_count_chapters_fn)(fz_context *ctx, fz_document *doc);
  163. /**
  164. Type for a function to be called to
  165. count the number of pages in a document. See fz_count_pages for
  166. more information.
  167. */
  168. typedef int (fz_document_count_pages_fn)(fz_context *ctx, fz_document *doc, int chapter);
  169. /**
  170. Type for a function to load a given
  171. page from a document. See fz_load_page for more information.
  172. */
  173. typedef fz_page *(fz_document_load_page_fn)(fz_context *ctx, fz_document *doc, int chapter, int page);
  174. /**
  175. Type for a function to get the page label of a page in the document.
  176. See fz_page_label for more information.
  177. */
  178. typedef void (fz_document_page_label_fn)(fz_context *ctx, fz_document *doc, int chapter, int page, char *buf, size_t size);
  179. /**
  180. Type for a function to query
  181. a document's metadata. See fz_lookup_metadata for more
  182. information.
  183. */
  184. typedef int (fz_document_lookup_metadata_fn)(fz_context *ctx, fz_document *doc, const char *key, char *buf, size_t size);
  185. /**
  186. Type for a function to set
  187. a document's metadata. See fz_set_metadata for more
  188. information.
  189. */
  190. typedef void (fz_document_set_metadata_fn)(fz_context *ctx, fz_document *doc, const char *key, const char *value);
  191. /**
  192. Return output intent color space if it exists
  193. */
  194. typedef fz_colorspace *(fz_document_output_intent_fn)(fz_context *ctx, fz_document *doc);
  195. /**
  196. Write document accelerator data
  197. */
  198. typedef void (fz_document_output_accelerator_fn)(fz_context *ctx, fz_document *doc, fz_output *out);
  199. /**
  200. Send document structure to device
  201. */
  202. typedef void (fz_document_run_structure_fn)(fz_context *ctx, fz_document *doc, fz_device *dev, fz_cookie *cookie);
  203. /**
  204. Get a handle to this document as PDF.
  205. Returns a borrowed handle.
  206. */
  207. typedef fz_document *(fz_document_as_pdf_fn)(fz_context *ctx, fz_document *doc);
  208. /**
  209. Type for a function to make
  210. a bookmark. See fz_make_bookmark for more information.
  211. */
  212. typedef fz_bookmark (fz_document_make_bookmark_fn)(fz_context *ctx, fz_document *doc, fz_location loc);
  213. /**
  214. Type for a function to lookup a bookmark.
  215. See fz_lookup_bookmark for more information.
  216. */
  217. typedef fz_location (fz_document_lookup_bookmark_fn)(fz_context *ctx, fz_document *doc, fz_bookmark mark);
  218. /**
  219. Type for a function to release all the
  220. resources held by a page. Called automatically when the
  221. reference count for that page reaches zero.
  222. */
  223. typedef void (fz_page_drop_page_fn)(fz_context *ctx, fz_page *page);
  224. /**
  225. Type for a function to return the
  226. bounding box of a page. See fz_bound_page for more
  227. information.
  228. */
  229. typedef fz_rect (fz_page_bound_page_fn)(fz_context *ctx, fz_page *page, fz_box_type box);
  230. /**
  231. Type for a function to run the
  232. contents of a page. See fz_run_page_contents for more
  233. information.
  234. */
  235. typedef void (fz_page_run_page_fn)(fz_context *ctx, fz_page *page, fz_device *dev, fz_matrix transform, fz_cookie *cookie);
  236. /**
  237. Type for a function to load the links
  238. from a page. See fz_load_links for more information.
  239. */
  240. typedef fz_link *(fz_page_load_links_fn)(fz_context *ctx, fz_page *page);
  241. /**
  242. Type for a function to
  243. obtain the details of how this page should be presented when
  244. in presentation mode. See fz_page_presentation for more
  245. information.
  246. */
  247. typedef fz_transition *(fz_page_page_presentation_fn)(fz_context *ctx, fz_page *page, fz_transition *transition, float *duration);
  248. /**
  249. Type for a function to enable/
  250. disable separations on a page. See fz_control_separation for
  251. more information.
  252. */
  253. typedef void (fz_page_control_separation_fn)(fz_context *ctx, fz_page *page, int separation, int disable);
  254. /**
  255. Type for a function to detect
  256. whether a given separation is enabled or disabled on a page.
  257. See FZ_SEPARATION_DISABLED for more information.
  258. */
  259. typedef int (fz_page_separation_disabled_fn)(fz_context *ctx, fz_page *page, int separation);
  260. /**
  261. Type for a function to retrieve
  262. details of separations on a page. See fz_get_separations
  263. for more information.
  264. */
  265. typedef fz_separations *(fz_page_separations_fn)(fz_context *ctx, fz_page *page);
  266. /**
  267. Type for a function to retrieve
  268. whether or not a given page uses overprint.
  269. */
  270. typedef int (fz_page_uses_overprint_fn)(fz_context *ctx, fz_page *page);
  271. /**
  272. Type for a function to create a link on a page.
  273. */
  274. typedef fz_link *(fz_page_create_link_fn)(fz_context *ctx, fz_page *page, fz_rect bbox, const char *uri);
  275. /**
  276. Type for a function to delete a link on a page.
  277. */
  278. typedef void (fz_page_delete_link_fn)(fz_context *ctx, fz_page *page, fz_link *link);
  279. /**
  280. Function type to open a
  281. document from a file.
  282. handler: the document handler in use.
  283. stream: fz_stream to read document data from. Must be
  284. seekable for formats that require it.
  285. accel: fz_stream to read accelerator data from. May be
  286. NULL. May be ignored.
  287. dir: 'Directory context' in which the document is loaded;
  288. associated content from (like images for an html stream
  289. will be loaded from this). Maybe NULL. May be ignored.
  290. recognize_state: NULL, or a state pointer passed back from the call
  291. to recognise_content_fn. Ownership does not pass in. The
  292. caller remains responsible for freeing state.
  293. Pointer to opened document. Throws exception in case of error.
  294. */
  295. typedef fz_document *(fz_document_open_fn)(fz_context *ctx, const fz_document_handler *handler, fz_stream *stream, fz_stream *accel, fz_archive *dir, void *recognize_state);
  296. /**
  297. Recognize a document type from
  298. a magic string.
  299. handler: the handler in use.
  300. magic: string to recognise - typically a filename or mime
  301. type.
  302. Returns a number between 0 (not recognized) and 100
  303. (fully recognized) based on how certain the recognizer
  304. is that this is of the required type.
  305. */
  306. typedef int (fz_document_recognize_fn)(fz_context *ctx, const fz_document_handler *handler, const char *magic);
  307. typedef void (fz_document_recognize_state_free_fn)(fz_context *ctx, void *state);
  308. /**
  309. Recognize a document type from stream contents.
  310. handler: the handler in use.
  311. stream: stream contents to recognise (may be NULL if document is
  312. a directory).
  313. dir: directory context from which stream is loaded.
  314. recognize_state: pointer to retrieve opaque state that may be used
  315. by the open routine, or NULL.
  316. free_recognize_state: pointer to retrieve a function pointer to
  317. free the opaque state, or NULL.
  318. Note: state and free_state should either both be NULL or
  319. both be non-NULL!
  320. Returns a number between 0 (not recognized) and 100
  321. (fully recognized) based on how certain the recognizer
  322. is that this is of the required type.
  323. */
  324. typedef int (fz_document_recognize_content_fn)(fz_context *ctx, const fz_document_handler *handler, fz_stream *stream, fz_archive *dir, void **recognize_state, fz_document_recognize_state_free_fn **free_recognize_state);
  325. /**
  326. Finalise a document handler.
  327. This will be called on shutdown for a document handler to
  328. release resources. This should cope with being called with NULL.
  329. opaque: The value previously returned by the init call.
  330. */
  331. typedef void (fz_document_handler_fin_fn)(fz_context *ctx, const fz_document_handler *handler);
  332. /**
  333. Type for a function to be called when processing an already opened page.
  334. See fz_process_opened_pages.
  335. */
  336. typedef void *(fz_process_opened_page_fn)(fz_context *ctx, fz_page *page, void *state);
  337. /**
  338. Register a handler for a document type.
  339. handler: The handler to register. This must live on for the duration of the
  340. use of this handler. It will be passed back to the handler for calls so
  341. the caller can use it to retrieve state.
  342. */
  343. void fz_register_document_handler(fz_context *ctx, const fz_document_handler *handler);
  344. /**
  345. Register handlers for all the standard document types supported in
  346. this build.
  347. */
  348. void fz_register_document_handlers(fz_context *ctx);
  349. /**
  350. Given a magic find a document handler that can handle a
  351. document of this type.
  352. magic: Can be a filename extension (including initial period) or
  353. a mimetype.
  354. */
  355. const fz_document_handler *fz_recognize_document(fz_context *ctx, const char *magic);
  356. /**
  357. Given a filename find a document handler that can handle a
  358. document of this type.
  359. filename: The filename of the document. This will be opened and sampled
  360. to check data.
  361. */
  362. const fz_document_handler *fz_recognize_document_content(fz_context *ctx, const char *filename);
  363. /**
  364. Given a magic find a document handler that can handle a
  365. document of this type.
  366. stream: the file stream to sample. May be NULL if the document is
  367. a directory.
  368. magic: Can be a filename extension (including initial period) or
  369. a mimetype.
  370. */
  371. const fz_document_handler *fz_recognize_document_stream_content(fz_context *ctx, fz_stream *stream, const char *magic);
  372. /**
  373. Given a magic find a document handler that can handle a
  374. document of this type.
  375. stream: the file stream to sample. May be NULL if the document is
  376. a directory.
  377. dir: an fz_archive representing the directory from which the
  378. stream was opened (or NULL).
  379. magic: Can be a filename extension (including initial period) or
  380. a mimetype.
  381. */
  382. const fz_document_handler *fz_recognize_document_stream_and_dir_content(fz_context *ctx, fz_stream *stream, fz_archive *dir, const char *magic);
  383. /**
  384. Open a document file and read its basic structure so pages and
  385. objects can be located. MuPDF will try to repair broken
  386. documents (without actually changing the file contents).
  387. The returned fz_document is used when calling most other
  388. document related functions.
  389. filename: a path to a file as it would be given to open(2).
  390. */
  391. fz_document *fz_open_document(fz_context *ctx, const char *filename);
  392. /**
  393. Open a document file and read its basic structure so pages and
  394. objects can be located. MuPDF will try to repair broken
  395. documents (without actually changing the file contents).
  396. The returned fz_document is used when calling most other
  397. document related functions.
  398. filename: a path to a file as it would be given to open(2).
  399. */
  400. fz_document *fz_open_accelerated_document(fz_context *ctx, const char *filename, const char *accel);
  401. /**
  402. Open a document using the specified stream object rather than
  403. opening a file on disk.
  404. magic: a string used to detect document type; either a file name
  405. or mime-type.
  406. stream: a stream representing the contents of the document file.
  407. NOTE: The caller retains ownership of 'stream' - the document will take its
  408. own reference if required.
  409. */
  410. fz_document *fz_open_document_with_stream(fz_context *ctx, const char *magic, fz_stream *stream);
  411. /**
  412. Open a document using the specified stream object rather than
  413. opening a file on disk.
  414. magic: a string used to detect document type; either a file name
  415. or mime-type.
  416. stream: a stream representing the contents of the document file.
  417. dir: a 'directory context' for those filetypes that need it.
  418. NOTE: The caller retains ownership of 'stream' and 'dir' - the document will
  419. take its own references if required.
  420. */
  421. fz_document *fz_open_document_with_stream_and_dir(fz_context *ctx, const char *magic, fz_stream *stream, fz_archive *dir);
  422. /**
  423. Open a document using a buffer rather than opening a file on disk.
  424. */
  425. fz_document *fz_open_document_with_buffer(fz_context *ctx, const char *magic, fz_buffer *buffer);
  426. /**
  427. Open a document using the specified stream object rather than
  428. opening a file on disk.
  429. magic: a string used to detect document type; either a file name
  430. or mime-type.
  431. stream: a stream of the document contents.
  432. accel: NULL, or a stream of the 'accelerator' contents for this document.
  433. NOTE: The caller retains ownership of 'stream' and 'accel' - the document will
  434. take its own references if required.
  435. */
  436. fz_document *fz_open_accelerated_document_with_stream(fz_context *ctx, const char *magic, fz_stream *stream, fz_stream *accel);
  437. /**
  438. Open a document using the specified stream object rather than
  439. opening a file on disk.
  440. magic: a string used to detect document type; either a file name
  441. or mime-type.
  442. stream: a stream of the document contents.
  443. accel: NULL, or a stream of the 'accelerator' contents for this document.
  444. dir: NULL, or the 'directory context' for the stream contents.
  445. NOTE: The caller retains ownership of 'stream', 'accel' and 'dir' - the document will
  446. take its own references if required.
  447. */
  448. fz_document *fz_open_accelerated_document_with_stream_and_dir(fz_context *ctx, const char *magic, fz_stream *stream, fz_stream *accel, fz_archive *dir);
  449. /**
  450. Query if the document supports the saving of accelerator data.
  451. */
  452. int fz_document_supports_accelerator(fz_context *ctx, fz_document *doc);
  453. /**
  454. Save accelerator data for the document to a given file.
  455. */
  456. void fz_save_accelerator(fz_context *ctx, fz_document *doc, const char *accel);
  457. /**
  458. Output accelerator data for the document to a given output
  459. stream.
  460. */
  461. void fz_output_accelerator(fz_context *ctx, fz_document *doc, fz_output *accel);
  462. /**
  463. New documents are typically created by calls like
  464. foo_new_document(fz_context *ctx, ...). These work by
  465. deriving a new document type from fz_document, for instance:
  466. typedef struct { fz_document base; ...extras... } foo_document;
  467. These are allocated by calling
  468. fz_new_derived_document(ctx, foo_document)
  469. */
  470. void *fz_new_document_of_size(fz_context *ctx, int size);
  471. #define fz_new_derived_document(C,M) ((M*)Memento_label(fz_new_document_of_size(C, sizeof(M)), #M))
  472. /**
  473. Increment the document reference count. The same pointer is
  474. returned.
  475. Never throws exceptions.
  476. */
  477. fz_document *fz_keep_document(fz_context *ctx, fz_document *doc);
  478. /**
  479. Decrement the document reference count. When the reference
  480. count reaches 0, the document and all it's references are
  481. freed.
  482. Never throws exceptions.
  483. */
  484. void fz_drop_document(fz_context *ctx, fz_document *doc);
  485. /**
  486. Check if a document is encrypted with a
  487. non-blank password.
  488. */
  489. int fz_needs_password(fz_context *ctx, fz_document *doc);
  490. /**
  491. Test if the given password can decrypt the document.
  492. password: The password string to be checked. Some document
  493. specifications do not specify any particular text encoding, so
  494. neither do we.
  495. Returns 0 for failure to authenticate, non-zero for success.
  496. For PDF documents, further information can be given by examining
  497. the bits in the return code.
  498. Bit 0 => No password required
  499. Bit 1 => User password authenticated
  500. Bit 2 => Owner password authenticated
  501. */
  502. int fz_authenticate_password(fz_context *ctx, fz_document *doc, const char *password);
  503. /**
  504. Load the hierarchical document outline.
  505. Should be freed by fz_drop_outline.
  506. */
  507. fz_outline *fz_load_outline(fz_context *ctx, fz_document *doc);
  508. /**
  509. Get an iterator for the document outline.
  510. Should be freed by fz_drop_outline_iterator.
  511. */
  512. fz_outline_iterator *fz_new_outline_iterator(fz_context *ctx, fz_document *doc);
  513. /**
  514. Is the document reflowable.
  515. Returns 1 to indicate reflowable documents, otherwise 0.
  516. */
  517. int fz_is_document_reflowable(fz_context *ctx, fz_document *doc);
  518. /**
  519. Layout reflowable document types.
  520. w, h: Page size in points.
  521. em: Default font size in points.
  522. */
  523. void fz_layout_document(fz_context *ctx, fz_document *doc, float w, float h, float em);
  524. /**
  525. Create a bookmark for the given page, which can be used to find
  526. the same location after the document has been laid out with
  527. different parameters.
  528. */
  529. fz_bookmark fz_make_bookmark(fz_context *ctx, fz_document *doc, fz_location loc);
  530. /**
  531. Find a bookmark and return its page number.
  532. */
  533. fz_location fz_lookup_bookmark(fz_context *ctx, fz_document *doc, fz_bookmark mark);
  534. /**
  535. Return the number of pages in document
  536. May return 0 for documents with no pages.
  537. */
  538. int fz_count_pages(fz_context *ctx, fz_document *doc);
  539. /**
  540. Resolve an internal link to a page number, location, and possible viewing parameters.
  541. Returns location (-1,-1) if the URI cannot be resolved.
  542. */
  543. fz_link_dest fz_resolve_link_dest(fz_context *ctx, fz_document *doc, const char *uri);
  544. /**
  545. Format an internal link to a page number, location, and possible viewing parameters,
  546. suitable for use with fz_create_link.
  547. Returns a newly allocated string that the caller must free.
  548. */
  549. char *fz_format_link_uri(fz_context *ctx, fz_document *doc, fz_link_dest dest);
  550. /**
  551. Resolve an internal link to a page number.
  552. xp, yp: Pointer to store coordinate of destination on the page.
  553. Returns (-1,-1) if the URI cannot be resolved.
  554. */
  555. fz_location fz_resolve_link(fz_context *ctx, fz_document *doc, const char *uri, float *xp, float *yp);
  556. /**
  557. Run the document structure through a device.
  558. doc: Document in question.
  559. dev: Device obtained from fz_new_*_device.
  560. cookie: Communication mechanism between caller and library.
  561. Intended for multi-threaded applications, while
  562. single-threaded applications set cookie to NULL. The
  563. caller may abort an ongoing rendering of a page. Cookie also
  564. communicates progress information back to the caller. The
  565. fields inside cookie are continually updated while the page is
  566. rendering.
  567. */
  568. void fz_run_document_structure(fz_context *ctx, fz_document *doc, fz_device *dev, fz_cookie *cookie);
  569. /**
  570. Function to get the location for the last page in the document.
  571. Using this can be far more efficient in some cases than calling
  572. fz_count_pages and using the page number.
  573. */
  574. fz_location fz_last_page(fz_context *ctx, fz_document *doc);
  575. /**
  576. Function to get the location of the next page (allowing for the
  577. end of chapters etc). If at the end of the document, returns the
  578. current location.
  579. */
  580. fz_location fz_next_page(fz_context *ctx, fz_document *doc, fz_location loc);
  581. /**
  582. Function to get the location of the previous page (allowing for
  583. the end of chapters etc). If already at the start of the
  584. document, returns the current page.
  585. */
  586. fz_location fz_previous_page(fz_context *ctx, fz_document *doc, fz_location loc);
  587. /**
  588. Clamps a location into valid chapter/page range. (First clamps
  589. the chapter into range, then the page into range).
  590. */
  591. fz_location fz_clamp_location(fz_context *ctx, fz_document *doc, fz_location loc);
  592. /**
  593. Converts from page number to chapter+page. This may cause many
  594. chapters to be laid out in order to calculate the number of
  595. pages within those chapters.
  596. */
  597. fz_location fz_location_from_page_number(fz_context *ctx, fz_document *doc, int number);
  598. /**
  599. Converts from chapter+page to page number. This may cause many
  600. chapters to be laid out in order to calculate the number of
  601. pages within those chapters.
  602. */
  603. int fz_page_number_from_location(fz_context *ctx, fz_document *doc, fz_location loc);
  604. /**
  605. Load a given page number from a document. This may be much less
  606. efficient than loading by location (chapter+page) for some
  607. document types.
  608. */
  609. fz_page *fz_load_page(fz_context *ctx, fz_document *doc, int number);
  610. /**
  611. Return the number of chapters in the document.
  612. At least 1.
  613. */
  614. int fz_count_chapters(fz_context *ctx, fz_document *doc);
  615. /**
  616. Return the number of pages in a chapter.
  617. May return 0.
  618. */
  619. int fz_count_chapter_pages(fz_context *ctx, fz_document *doc, int chapter);
  620. /**
  621. Load a page.
  622. After fz_load_page is it possible to retrieve the size of the
  623. page using fz_bound_page, or to render the page using
  624. fz_run_page_*. Free the page by calling fz_drop_page.
  625. chapter: chapter number, 0 is the first chapter of the document.
  626. number: page number, 0 is the first page of the chapter.
  627. */
  628. fz_page *fz_load_chapter_page(fz_context *ctx, fz_document *doc, int chapter, int page);
  629. /**
  630. Load the list of links for a page.
  631. Returns a linked list of all the links on the page, each with
  632. its clickable region and link destination. Each link is
  633. reference counted so drop and free the list of links by
  634. calling fz_drop_link on the pointer return from fz_load_links.
  635. page: Page obtained from fz_load_page.
  636. */
  637. fz_link *fz_load_links(fz_context *ctx, fz_page *page);
  638. /**
  639. Different document types will be implemented by deriving from
  640. fz_page. This macro allocates such derived structures, and
  641. initialises the base sections.
  642. */
  643. fz_page *fz_new_page_of_size(fz_context *ctx, int size, fz_document *doc);
  644. #define fz_new_derived_page(CTX,TYPE,DOC) \
  645. ((TYPE *)Memento_label(fz_new_page_of_size(CTX,sizeof(TYPE),DOC),#TYPE))
  646. /**
  647. Determine the size of a page at 72 dpi.
  648. */
  649. fz_rect fz_bound_page(fz_context *ctx, fz_page *page);
  650. fz_rect fz_bound_page_box(fz_context *ctx, fz_page *page, fz_box_type box);
  651. /**
  652. Run a page through a device.
  653. page: Page obtained from fz_load_page.
  654. dev: Device obtained from fz_new_*_device.
  655. transform: Transform to apply to page. May include for example
  656. scaling and rotation, see fz_scale, fz_rotate and fz_concat.
  657. Set to fz_identity if no transformation is desired.
  658. cookie: Communication mechanism between caller and library
  659. rendering the page. Intended for multi-threaded applications,
  660. while single-threaded applications set cookie to NULL. The
  661. caller may abort an ongoing rendering of a page. Cookie also
  662. communicates progress information back to the caller. The
  663. fields inside cookie are continually updated while the page is
  664. rendering.
  665. */
  666. void fz_run_page(fz_context *ctx, fz_page *page, fz_device *dev, fz_matrix transform, fz_cookie *cookie);
  667. /**
  668. Run a page through a device. Just the main
  669. page content, without the annotations, if any.
  670. page: Page obtained from fz_load_page.
  671. dev: Device obtained from fz_new_*_device.
  672. transform: Transform to apply to page. May include for example
  673. scaling and rotation, see fz_scale, fz_rotate and fz_concat.
  674. Set to fz_identity if no transformation is desired.
  675. cookie: Communication mechanism between caller and library
  676. rendering the page. Intended for multi-threaded applications,
  677. while single-threaded applications set cookie to NULL. The
  678. caller may abort an ongoing rendering of a page. Cookie also
  679. communicates progress information back to the caller. The
  680. fields inside cookie are continually updated while the page is
  681. rendering.
  682. */
  683. void fz_run_page_contents(fz_context *ctx, fz_page *page, fz_device *dev, fz_matrix transform, fz_cookie *cookie);
  684. /**
  685. Run the annotations on a page through a device.
  686. */
  687. void fz_run_page_annots(fz_context *ctx, fz_page *page, fz_device *dev, fz_matrix transform, fz_cookie *cookie);
  688. /**
  689. Run the widgets on a page through a device.
  690. */
  691. void fz_run_page_widgets(fz_context *ctx, fz_page *page, fz_device *dev, fz_matrix transform, fz_cookie *cookie);
  692. /**
  693. Increment the reference count for the page. Returns the same
  694. pointer.
  695. Never throws exceptions.
  696. */
  697. fz_page *fz_keep_page(fz_context *ctx, fz_page *page);
  698. /**
  699. Decrements the reference count for the page. When the reference
  700. count hits 0, the page and its references are freed.
  701. Never throws exceptions.
  702. */
  703. void fz_drop_page(fz_context *ctx, fz_page *page);
  704. /**
  705. Get the presentation details for a given page.
  706. transition: A pointer to a transition struct to fill out.
  707. duration: A pointer to a place to set the page duration in
  708. seconds. Will be set to 0 if no transition is specified for the
  709. page.
  710. Returns: a pointer to the transition structure, or NULL if there
  711. is no transition specified for the page.
  712. */
  713. fz_transition *fz_page_presentation(fz_context *ctx, fz_page *page, fz_transition *transition, float *duration);
  714. /**
  715. Get page label for a given page.
  716. */
  717. const char *fz_page_label(fz_context *ctx, fz_page *page, char *buf, int size);
  718. /**
  719. Check permission flags on document.
  720. */
  721. int fz_has_permission(fz_context *ctx, fz_document *doc, fz_permission p);
  722. /**
  723. Retrieve document meta data strings.
  724. doc: The document to query.
  725. key: Which meta data key to retrieve...
  726. Basic information:
  727. 'format' -- Document format and version.
  728. 'encryption' -- Description of the encryption used.
  729. From the document information dictionary:
  730. 'info:Title'
  731. 'info:Author'
  732. 'info:Subject'
  733. 'info:Keywords'
  734. 'info:Creator'
  735. 'info:Producer'
  736. 'info:CreationDate'
  737. 'info:ModDate'
  738. buf: The buffer to hold the results (a nul-terminated UTF-8
  739. string).
  740. size: Size of 'buf'.
  741. Returns the number of bytes need to store the string plus terminator
  742. (will be larger than 'size' if the output was truncated), or -1 if the
  743. key is not recognized or found.
  744. */
  745. int fz_lookup_metadata(fz_context *ctx, fz_document *doc, const char *key, char *buf, size_t size);
  746. #define FZ_META_FORMAT "format"
  747. #define FZ_META_ENCRYPTION "encryption"
  748. #define FZ_META_INFO "info:"
  749. #define FZ_META_INFO_TITLE "info:Title"
  750. #define FZ_META_INFO_AUTHOR "info:Author"
  751. #define FZ_META_INFO_SUBJECT "info:Subject"
  752. #define FZ_META_INFO_KEYWORDS "info:Keywords"
  753. #define FZ_META_INFO_CREATOR "info:Creator"
  754. #define FZ_META_INFO_PRODUCER "info:Producer"
  755. #define FZ_META_INFO_CREATIONDATE "info:CreationDate"
  756. #define FZ_META_INFO_MODIFICATIONDATE "info:ModDate"
  757. void fz_set_metadata(fz_context *ctx, fz_document *doc, const char *key, const char *value);
  758. /**
  759. Find the output intent colorspace if the document has defined
  760. one.
  761. Returns a borrowed reference that should not be dropped, unless
  762. it is kept first.
  763. */
  764. fz_colorspace *fz_document_output_intent(fz_context *ctx, fz_document *doc);
  765. /**
  766. Get the separations details for a page.
  767. This will be NULL, unless the format specifically supports
  768. separations (such as PDF files). May be NULL even
  769. so, if there are no separations on a page.
  770. Returns a reference that must be dropped.
  771. */
  772. fz_separations *fz_page_separations(fz_context *ctx, fz_page *page);
  773. /**
  774. Query if a given page requires overprint.
  775. */
  776. int fz_page_uses_overprint(fz_context *ctx, fz_page *page);
  777. /**
  778. Create a new link on a page.
  779. */
  780. fz_link *fz_create_link(fz_context *ctx, fz_page *page, fz_rect bbox, const char *uri);
  781. /**
  782. Delete an existing link on a page.
  783. */
  784. void fz_delete_link(fz_context *ctx, fz_page *page, fz_link *link);
  785. /**
  786. Iterates over all opened pages of the document, calling the
  787. provided callback for each page for processing. If the callback
  788. returns non-NULL then the iteration stops and that value is returned
  789. to the called of fz_process_opened_pages().
  790. The state pointer provided to fz_process_opened_pages() is
  791. passed on to the callback but is owned by the caller.
  792. Returns the first non-NULL value returned by the callback,
  793. or NULL if the callback returned NULL for all opened pages.
  794. */
  795. void *fz_process_opened_pages(fz_context *ctx, fz_document *doc, fz_process_opened_page_fn *process_openend_page, void *state);
  796. /* Implementation details: subject to change. */
  797. /**
  798. Structure definition is public so other classes can
  799. derive from it. Do not access the members directly.
  800. */
  801. struct fz_page
  802. {
  803. int refs;
  804. fz_document *doc; /* kept reference to parent document. Guaranteed non-NULL. */
  805. int chapter; /* chapter number */
  806. int number; /* page number in chapter */
  807. int incomplete; /* incomplete from progressive loading; don't cache! */
  808. int in_doc; /* page has been placed into the document */
  809. fz_page_drop_page_fn *drop_page;
  810. fz_page_bound_page_fn *bound_page;
  811. fz_page_run_page_fn *run_page_contents;
  812. fz_page_run_page_fn *run_page_annots;
  813. fz_page_run_page_fn *run_page_widgets;
  814. fz_page_load_links_fn *load_links;
  815. fz_page_page_presentation_fn *page_presentation;
  816. fz_page_control_separation_fn *control_separation;
  817. fz_page_separation_disabled_fn *separation_disabled;
  818. fz_page_separations_fn *separations;
  819. fz_page_uses_overprint_fn *overprint;
  820. fz_page_create_link_fn *create_link;
  821. fz_page_delete_link_fn *delete_link;
  822. /* linked list of currently open pages. This list is maintained
  823. * by fz_load_chapter_page and fz_drop_page. All pages hold a
  824. * kept reference to the document, so the document cannot disappear
  825. * while pages exist. 'Incomplete' pages are NOT kept in this
  826. * list. */
  827. fz_page **prev, *next;
  828. };
  829. /**
  830. Structure definition is public so other classes can
  831. derive from it. Callers should not access the members
  832. directly, though implementations will need initialize
  833. functions directly.
  834. */
  835. struct fz_document
  836. {
  837. int refs;
  838. fz_document_drop_fn *drop_document;
  839. fz_document_needs_password_fn *needs_password;
  840. fz_document_authenticate_password_fn *authenticate_password;
  841. fz_document_has_permission_fn *has_permission;
  842. fz_document_load_outline_fn *load_outline;
  843. fz_document_outline_iterator_fn *outline_iterator;
  844. fz_document_layout_fn *layout;
  845. fz_document_make_bookmark_fn *make_bookmark;
  846. fz_document_lookup_bookmark_fn *lookup_bookmark;
  847. fz_document_resolve_link_dest_fn *resolve_link_dest;
  848. fz_document_format_link_uri_fn *format_link_uri;
  849. fz_document_count_chapters_fn *count_chapters;
  850. fz_document_count_pages_fn *count_pages;
  851. fz_document_load_page_fn *load_page;
  852. fz_document_page_label_fn *page_label;
  853. fz_document_lookup_metadata_fn *lookup_metadata;
  854. fz_document_set_metadata_fn *set_metadata;
  855. fz_document_output_intent_fn *get_output_intent;
  856. fz_document_output_accelerator_fn *output_accelerator;
  857. fz_document_run_structure_fn *run_structure;
  858. fz_document_as_pdf_fn *as_pdf;
  859. int did_layout;
  860. int is_reflowable;
  861. int id;
  862. /* Linked list of currently open pages. These are not
  863. * references, but just a linked list of open pages,
  864. * maintained by fz_load_chapter_page, and fz_drop_page.
  865. * Every page holds a kept reference to the document, so
  866. * the document cannot be destroyed while a page exists.
  867. * Incomplete pages are NOT inserted into this list, but
  868. * do still hold a real document reference. */
  869. fz_page *open;
  870. };
  871. struct fz_document_handler
  872. {
  873. /* These fields are initialised by the handler when it is registered. */
  874. fz_document_recognize_fn *recognize;
  875. fz_document_open_fn *open;
  876. const char **extensions;
  877. const char **mimetypes;
  878. fz_document_recognize_content_fn *recognize_content;
  879. int wants_dir;
  880. int wants_file;
  881. fz_document_handler_fin_fn *fin;
  882. };
  883. #endif