document.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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. #include "mupdf/fitz.h"
  23. #include "context-imp.h"
  24. #include <string.h>
  25. #ifndef _WIN32
  26. #include <unistd.h> /* For unlink */
  27. #endif
  28. #include <errno.h>
  29. static void fz_reap_dead_pages(fz_context *ctx, fz_document *doc);
  30. enum
  31. {
  32. FZ_DOCUMENT_HANDLER_MAX = 32
  33. };
  34. #define DEFW (450)
  35. #define DEFH (600)
  36. #define DEFEM (12)
  37. static fz_output *
  38. fz_new_output_to_tempfile(fz_context *ctx, char **namep)
  39. {
  40. fz_output *out = NULL;
  41. #ifdef _WIN32
  42. char namebuf[L_tmpnam];
  43. int attempts = 0;
  44. #else
  45. char namebuf[] = "/tmp/fztmpXXXXXX";
  46. #endif
  47. fz_var(out);
  48. #ifdef _WIN32
  49. /* Windows has no mkstemp command, so we have to use the old-style
  50. * tmpnam based system, and retry in the case of races. */
  51. do
  52. {
  53. if (tmpnam(namebuf) == NULL)
  54. fz_throw(ctx, FZ_ERROR_SYSTEM, "tmpnam failed");
  55. fz_try(ctx)
  56. out = fz_new_output_with_path(ctx, namebuf, 0);
  57. fz_catch(ctx)
  58. {
  59. /* We might hit a race condition and not be able to
  60. * open the file because someone beats us to it. We'd
  61. * be unbearably unlucky to hit this 10 times in a row. */
  62. attempts++;
  63. if (attempts >= 10)
  64. fz_rethrow(ctx);
  65. else
  66. fz_ignore_error(ctx);
  67. }
  68. }
  69. while (out == NULL);
  70. #else
  71. {
  72. FILE *file;
  73. int fd = mkstemp(namebuf);
  74. if (fd == -1)
  75. fz_throw(ctx, FZ_ERROR_SYSTEM, "Cannot mkstemp: %s", strerror(errno));
  76. file = fdopen(fd, "w");
  77. if (file == NULL)
  78. fz_throw(ctx, FZ_ERROR_SYSTEM, "Failed to open temporary file");
  79. out = fz_new_output_with_file_ptr(ctx, file);
  80. }
  81. #endif
  82. if (namep)
  83. {
  84. fz_try(ctx)
  85. *namep = fz_strdup(ctx, namebuf);
  86. fz_catch(ctx)
  87. {
  88. fz_drop_output(ctx, out);
  89. unlink(namebuf);
  90. fz_rethrow(ctx);
  91. }
  92. }
  93. return out;
  94. }
  95. static char *
  96. fz_new_tmpfile_from_stream(fz_context *ctx, fz_stream *stm)
  97. {
  98. char *name;
  99. fz_output *out = fz_new_output_to_tempfile(ctx, &name);
  100. fz_try(ctx)
  101. {
  102. fz_write_stream(ctx, out, stm);
  103. fz_close_output(ctx, out);
  104. }
  105. fz_always(ctx)
  106. fz_drop_output(ctx, out);
  107. fz_catch(ctx)
  108. {
  109. fz_free(ctx, name);
  110. fz_rethrow(ctx);
  111. }
  112. return name;
  113. }
  114. static fz_stream *
  115. fz_file_backed_stream(fz_context *ctx, fz_stream *stream)
  116. {
  117. const char *oname = fz_stream_filename(ctx, stream);
  118. char *name;
  119. /* If the file has a name, it's already a file-backed stream.*/
  120. if (oname)
  121. return stream;
  122. /* Otherwise we need to make it one. */
  123. name = fz_new_tmpfile_from_stream(ctx, stream);
  124. fz_try(ctx)
  125. stream = fz_open_file_autodelete(ctx, name);
  126. fz_always(ctx)
  127. fz_free(ctx, name);
  128. fz_catch(ctx)
  129. fz_rethrow(ctx);
  130. return stream;
  131. }
  132. struct fz_document_handler_context
  133. {
  134. int refs;
  135. int count;
  136. const fz_document_handler *handler[FZ_DOCUMENT_HANDLER_MAX];
  137. };
  138. void fz_new_document_handler_context(fz_context *ctx)
  139. {
  140. ctx->handler = fz_malloc_struct(ctx, fz_document_handler_context);
  141. ctx->handler->refs = 1;
  142. }
  143. fz_document_handler_context *fz_keep_document_handler_context(fz_context *ctx)
  144. {
  145. if (!ctx || !ctx->handler)
  146. return NULL;
  147. return fz_keep_imp(ctx, ctx->handler, &ctx->handler->refs);
  148. }
  149. void fz_drop_document_handler_context(fz_context *ctx)
  150. {
  151. int i;
  152. if (!ctx || !ctx->handler)
  153. return;
  154. for (i = 0; i < ctx->handler->count; i++)
  155. {
  156. if (ctx->handler->handler[i]->fin)
  157. {
  158. fz_try(ctx)
  159. ctx->handler->handler[i]->fin(ctx, ctx->handler->handler[i]);
  160. fz_catch(ctx)
  161. fz_ignore_error(ctx);
  162. }
  163. }
  164. if (fz_drop_imp(ctx, ctx->handler, &ctx->handler->refs))
  165. {
  166. fz_free(ctx, ctx->handler);
  167. ctx->handler = NULL;
  168. }
  169. }
  170. void fz_register_document_handler(fz_context *ctx, const fz_document_handler *handler)
  171. {
  172. fz_document_handler_context *dc;
  173. int i;
  174. if (!handler)
  175. return;
  176. dc = ctx->handler;
  177. if (dc == NULL)
  178. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Document handler list not found");
  179. for (i = 0; i < dc->count; i++)
  180. if (dc->handler[i] == handler)
  181. return;
  182. if (dc->count >= FZ_DOCUMENT_HANDLER_MAX)
  183. fz_throw(ctx, FZ_ERROR_LIMIT, "Too many document handlers");
  184. dc->handler[dc->count++] = handler;
  185. }
  186. const fz_document_handler *
  187. fz_recognize_document_stream_content(fz_context *ctx, fz_stream *stream, const char *magic)
  188. {
  189. return fz_recognize_document_stream_and_dir_content(ctx, stream, NULL, magic);
  190. }
  191. const fz_document_handler *
  192. do_recognize_document_stream_and_dir_content(fz_context *ctx, fz_stream **streamp, fz_archive *dir, const char *magic, void **handler_state, fz_document_recognize_state_free_fn **handler_free_state)
  193. {
  194. fz_document_handler_context *dc;
  195. int i, best_score, best_i;
  196. void *best_state = NULL;
  197. fz_document_recognize_state_free_fn *best_free_state = NULL;
  198. const char *ext;
  199. int drop_stream = 0;
  200. fz_stream *stream = *streamp;
  201. if (handler_state)
  202. *handler_state = NULL;
  203. if (handler_free_state)
  204. *handler_free_state = NULL;
  205. dc = ctx->handler;
  206. if (dc->count == 0)
  207. fz_throw(ctx, FZ_ERROR_ARGUMENT, "No document handlers registered");
  208. if (magic == NULL)
  209. magic = "";
  210. ext = strrchr(magic, '.');
  211. if (ext)
  212. ext = ext + 1;
  213. else
  214. ext = magic;
  215. best_score = 0;
  216. best_i = -1;
  217. /* If we're handed a stream, check to see if any of our document handlers
  218. * need a file. If so, change the stream to be a file-backed one. */
  219. if (stream)
  220. {
  221. int wants_file = 0;
  222. for (i = 0; i < dc->count; i++)
  223. wants_file |= dc->handler[i]->wants_file;
  224. /* Convert the stream into a file_backed stream. */
  225. if (wants_file)
  226. {
  227. fz_stream *stream2 = fz_file_backed_stream(ctx, stream);
  228. if (stream2 != stream)
  229. {
  230. /* Either we need to pass this back to our caller, or we
  231. * need to drop it. */
  232. drop_stream = 1;
  233. stream = stream2;
  234. }
  235. }
  236. }
  237. fz_try(ctx)
  238. {
  239. int can_recognize_stream = ((stream && stream->seek != NULL) || (stream == NULL && dir != NULL));
  240. for (i = 0; i < dc->count; i++)
  241. {
  242. void *state = NULL;
  243. fz_document_recognize_state_free_fn *free_state = NULL;
  244. int score = 0;
  245. int magic_score = 0;
  246. const char **entry;
  247. /* Get a score from recognizing the stream */
  248. if (dc->handler[i]->recognize_content && can_recognize_stream)
  249. {
  250. if (stream)
  251. fz_seek(ctx, stream, 0, SEEK_SET);
  252. fz_try(ctx)
  253. {
  254. score = dc->handler[i]->recognize_content(ctx, dc->handler[i], stream, dir, &state, &free_state);
  255. }
  256. fz_catch(ctx)
  257. {
  258. /* in case of zip errors when recognizing EPUB/XPS/DOCX files */
  259. fz_rethrow_unless(ctx, FZ_ERROR_FORMAT);
  260. (void)fz_convert_error(ctx, NULL); /* ugly hack to silence the error message */
  261. score = 0;
  262. }
  263. }
  264. /* Now get a score from recognizing the magic */
  265. if (dc->handler[i]->recognize)
  266. magic_score = dc->handler[i]->recognize(ctx, dc->handler[i], magic);
  267. for (entry = &dc->handler[i]->mimetypes[0]; *entry; entry++)
  268. if (!fz_strcasecmp(magic, *entry) && score < 100)
  269. {
  270. magic_score = 100;
  271. break;
  272. }
  273. if (ext)
  274. {
  275. for (entry = &dc->handler[i]->extensions[0]; *entry; entry++)
  276. if (!fz_strcasecmp(ext, *entry) && score < 100)
  277. {
  278. magic_score = 100;
  279. break;
  280. }
  281. }
  282. /* If we recognized the format (at least partially), and the magic_score matches, then that's
  283. * definitely the one we want to use. */
  284. if (score > 0 && magic_score > 0)
  285. score = 1000;
  286. /* Otherwise, if we didn't recognize the format, we'll weakly believe in the magic, but
  287. * we won't let it override anything that actually will cope. */
  288. else if (magic_score > 0)
  289. score = 1;
  290. if (best_score < score)
  291. {
  292. best_score = score;
  293. best_i = i;
  294. if (best_free_state)
  295. best_free_state(ctx, best_state);
  296. best_free_state = free_state;
  297. best_state = state;
  298. }
  299. else if (free_state)
  300. free_state(ctx, state);
  301. }
  302. if (stream)
  303. fz_seek(ctx, stream, 0, SEEK_SET);
  304. }
  305. fz_catch(ctx)
  306. {
  307. if (best_free_state)
  308. best_free_state(ctx, best_state);
  309. if (drop_stream)
  310. fz_drop_stream(ctx, stream);
  311. fz_rethrow(ctx);
  312. }
  313. if (best_i < 0)
  314. {
  315. if (drop_stream)
  316. fz_drop_stream(ctx, stream);
  317. return NULL;
  318. }
  319. /* Only if we found a handler, do we make our modified stream available to the
  320. * caller. */
  321. *streamp = stream;
  322. if (handler_state && handler_free_state)
  323. {
  324. *handler_state = best_state;
  325. *handler_free_state = best_free_state;
  326. }
  327. else if (best_free_state)
  328. best_free_state(ctx, best_state);
  329. return dc->handler[best_i];
  330. }
  331. const fz_document_handler *
  332. fz_recognize_document_stream_and_dir_content(fz_context *ctx, fz_stream *stream, fz_archive *dir, const char *magic)
  333. {
  334. fz_stream *stm = stream;
  335. const fz_document_handler *res;
  336. res = do_recognize_document_stream_and_dir_content(ctx, &stm, dir, magic, NULL, NULL);
  337. if (stm != stream)
  338. fz_drop_stream(ctx, stm);
  339. return res;
  340. }
  341. static const fz_document_handler *do_recognize_document_content(fz_context *ctx, const char *filename, void **handler_state, fz_document_recognize_state_free_fn **handler_free_state)
  342. {
  343. fz_stream *stream = NULL;
  344. const fz_document_handler *handler = NULL;
  345. fz_archive *zip = NULL;
  346. fz_stream *stm;
  347. if (fz_is_directory(ctx, filename))
  348. zip = fz_open_directory(ctx, filename);
  349. else
  350. stream = fz_open_file(ctx, filename);
  351. stm = stream;
  352. fz_try(ctx)
  353. handler = do_recognize_document_stream_and_dir_content(ctx, &stm, zip, filename, handler_state, handler_free_state);
  354. fz_always(ctx)
  355. {
  356. if (stm != stream)
  357. fz_drop_stream(ctx, stm);
  358. fz_drop_stream(ctx, stream);
  359. fz_drop_archive(ctx, zip);
  360. }
  361. fz_catch(ctx)
  362. fz_rethrow(ctx);
  363. return handler;
  364. }
  365. const fz_document_handler *fz_recognize_document_content(fz_context* ctx, const char* filename)
  366. {
  367. return do_recognize_document_content(ctx, filename, NULL, NULL);
  368. }
  369. const fz_document_handler *
  370. fz_recognize_document(fz_context *ctx, const char *magic)
  371. {
  372. return fz_recognize_document_stream_and_dir_content(ctx, NULL, NULL, magic);
  373. }
  374. #if FZ_ENABLE_PDF
  375. extern fz_document_handler pdf_document_handler;
  376. #endif
  377. fz_document *
  378. fz_open_accelerated_document_with_stream_and_dir(fz_context *ctx, const char *magic, fz_stream *stream, fz_stream *accel, fz_archive *dir)
  379. {
  380. const fz_document_handler *handler;
  381. fz_stream *wrapped_stream = stream;
  382. fz_document *ret;
  383. void *state = NULL;
  384. fz_document_recognize_state_free_fn *free_state = NULL;
  385. if (stream == NULL && dir == NULL)
  386. fz_throw(ctx, FZ_ERROR_ARGUMENT, "no document to open");
  387. if (magic == NULL)
  388. fz_throw(ctx, FZ_ERROR_ARGUMENT, "missing file type");
  389. /* If this finds a handler, then this might wrap stream. If it does, we reuse the wrapped one in
  390. * the open call (hence avoiding us having to 'file-back' a stream twice), but we must free it. */
  391. handler = do_recognize_document_stream_and_dir_content(ctx, &wrapped_stream, dir, magic, &state, &free_state);
  392. if (!handler)
  393. fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "cannot find document handler for file type: '%s'", magic);
  394. fz_try(ctx)
  395. ret = handler->open(ctx, handler, wrapped_stream, accel, dir, state);
  396. fz_always(ctx)
  397. {
  398. if (wrapped_stream != stream)
  399. fz_drop_stream(ctx, wrapped_stream);
  400. if (free_state && state)
  401. free_state(ctx, state);
  402. }
  403. fz_catch(ctx)
  404. fz_rethrow(ctx);
  405. return ret;
  406. }
  407. fz_document *
  408. fz_open_accelerated_document_with_stream(fz_context *ctx, const char *magic, fz_stream *stream, fz_stream *accel)
  409. {
  410. return fz_open_accelerated_document_with_stream_and_dir(ctx, magic, stream, accel, NULL);
  411. }
  412. fz_document *
  413. fz_open_document_with_stream(fz_context *ctx, const char *magic, fz_stream *stream)
  414. {
  415. return fz_open_accelerated_document_with_stream(ctx, magic, stream, NULL);
  416. }
  417. fz_document *
  418. fz_open_document_with_stream_and_dir(fz_context *ctx, const char *magic, fz_stream *stream, fz_archive *dir)
  419. {
  420. return fz_open_accelerated_document_with_stream_and_dir(ctx, magic, stream, NULL, dir);
  421. }
  422. fz_document *
  423. fz_open_document_with_buffer(fz_context *ctx, const char *magic, fz_buffer *buffer)
  424. {
  425. fz_document *doc;
  426. fz_stream *stream = fz_open_buffer(ctx, buffer);
  427. fz_try(ctx)
  428. doc = fz_open_document_with_stream(ctx, magic, stream);
  429. fz_always(ctx)
  430. fz_drop_stream(ctx, stream);
  431. fz_catch(ctx)
  432. fz_rethrow(ctx);
  433. return doc;
  434. }
  435. fz_document *
  436. fz_open_accelerated_document(fz_context *ctx, const char *filename, const char *accel)
  437. {
  438. const fz_document_handler *handler;
  439. fz_stream *file = NULL;
  440. fz_stream *afile = NULL;
  441. fz_document *doc = NULL;
  442. fz_archive *dir = NULL;
  443. char dirname[PATH_MAX];
  444. void *state = NULL;
  445. fz_document_recognize_state_free_fn *free_state = NULL;
  446. if (filename == NULL)
  447. fz_throw(ctx, FZ_ERROR_ARGUMENT, "no document to open");
  448. if (fz_is_directory(ctx, filename))
  449. {
  450. /* Cannot accelerate directories, currently. */
  451. dir = fz_open_directory(ctx, filename);
  452. fz_try(ctx)
  453. doc = fz_open_accelerated_document_with_stream_and_dir(ctx, filename, NULL, NULL, dir);
  454. fz_always(ctx)
  455. fz_drop_archive(ctx, dir);
  456. fz_catch(ctx)
  457. fz_rethrow(ctx);
  458. return doc;
  459. }
  460. handler = do_recognize_document_content(ctx, filename, &state, &free_state);
  461. if (!handler)
  462. fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "cannot find document handler for file: %s", filename);
  463. fz_var(afile);
  464. fz_var(file);
  465. fz_try(ctx)
  466. {
  467. file = fz_open_file(ctx, filename);
  468. if (accel)
  469. afile = fz_open_file(ctx, accel);
  470. if (handler->wants_dir)
  471. {
  472. fz_dirname(dirname, filename, sizeof dirname);
  473. dir = fz_open_directory(ctx, dirname);
  474. }
  475. doc = handler->open(ctx, handler, file, afile, dir, state);
  476. }
  477. fz_always(ctx)
  478. {
  479. if (free_state)
  480. free_state(ctx, state);
  481. fz_drop_archive(ctx, dir);
  482. fz_drop_stream(ctx, afile);
  483. fz_drop_stream(ctx, file);
  484. }
  485. fz_catch(ctx)
  486. fz_rethrow(ctx);
  487. return doc;
  488. }
  489. fz_document *
  490. fz_open_document(fz_context *ctx, const char *filename)
  491. {
  492. return fz_open_accelerated_document(ctx, filename, NULL);
  493. }
  494. void fz_save_accelerator(fz_context *ctx, fz_document *doc, const char *accel)
  495. {
  496. if (doc == NULL)
  497. return;
  498. if (doc->output_accelerator == NULL)
  499. return;
  500. fz_output_accelerator(ctx, doc, fz_new_output_with_path(ctx, accel, 0));
  501. }
  502. void fz_output_accelerator(fz_context *ctx, fz_document *doc, fz_output *accel)
  503. {
  504. if (doc == NULL || accel == NULL)
  505. return;
  506. if (doc->output_accelerator == NULL)
  507. {
  508. fz_drop_output(ctx, accel);
  509. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Document does not support writing an accelerator");
  510. }
  511. doc->output_accelerator(ctx, doc, accel);
  512. }
  513. int fz_document_supports_accelerator(fz_context *ctx, fz_document *doc)
  514. {
  515. if (doc == NULL)
  516. return 0;
  517. return (doc->output_accelerator) != NULL;
  518. }
  519. void *
  520. fz_new_document_of_size(fz_context *ctx, int size)
  521. {
  522. fz_document *doc = fz_calloc(ctx, 1, size);
  523. doc->refs = 1;
  524. fz_log_activity(ctx, FZ_ACTIVITY_NEW_DOC, NULL);
  525. return doc;
  526. }
  527. fz_document *
  528. fz_keep_document(fz_context *ctx, fz_document *doc)
  529. {
  530. return fz_keep_imp(ctx, doc, &doc->refs);
  531. }
  532. void
  533. fz_drop_document(fz_context *ctx, fz_document *doc)
  534. {
  535. if (fz_drop_imp(ctx, doc, &doc->refs))
  536. {
  537. fz_reap_dead_pages(ctx, doc);
  538. if (doc->open)
  539. fz_warn(ctx, "There are still open pages in the document!");
  540. if (doc->drop_document)
  541. doc->drop_document(ctx, doc);
  542. fz_free(ctx, doc);
  543. }
  544. }
  545. static void
  546. fz_ensure_layout(fz_context *ctx, fz_document *doc)
  547. {
  548. if (doc && doc->layout && !doc->did_layout)
  549. {
  550. doc->layout(ctx, doc, DEFW, DEFH, DEFEM);
  551. doc->did_layout = 1;
  552. }
  553. }
  554. int
  555. fz_is_document_reflowable(fz_context *ctx, fz_document *doc)
  556. {
  557. return doc ? doc->is_reflowable : 0;
  558. }
  559. fz_bookmark fz_make_bookmark(fz_context *ctx, fz_document *doc, fz_location loc)
  560. {
  561. if (doc && doc->make_bookmark)
  562. return doc->make_bookmark(ctx, doc, loc);
  563. return (loc.chapter<<16) + loc.page;
  564. }
  565. fz_location fz_lookup_bookmark(fz_context *ctx, fz_document *doc, fz_bookmark mark)
  566. {
  567. if (doc && doc->lookup_bookmark)
  568. return doc->lookup_bookmark(ctx, doc, mark);
  569. return fz_make_location((mark>>16) & 0xffff, mark & 0xffff);
  570. }
  571. int
  572. fz_needs_password(fz_context *ctx, fz_document *doc)
  573. {
  574. if (doc && doc->needs_password)
  575. return doc->needs_password(ctx, doc);
  576. return 0;
  577. }
  578. int
  579. fz_authenticate_password(fz_context *ctx, fz_document *doc, const char *password)
  580. {
  581. if (doc && doc->authenticate_password)
  582. return doc->authenticate_password(ctx, doc, password);
  583. return 1;
  584. }
  585. int
  586. fz_has_permission(fz_context *ctx, fz_document *doc, fz_permission p)
  587. {
  588. if (doc && doc->has_permission)
  589. return doc->has_permission(ctx, doc, p);
  590. return 1;
  591. }
  592. fz_outline *
  593. fz_load_outline(fz_context *ctx, fz_document *doc)
  594. {
  595. if (doc == NULL)
  596. return NULL;
  597. fz_ensure_layout(ctx, doc);
  598. if (doc->load_outline)
  599. return doc->load_outline(ctx, doc);
  600. if (doc->outline_iterator == NULL)
  601. return NULL;
  602. return fz_load_outline_from_iterator(ctx, doc->outline_iterator(ctx, doc));
  603. }
  604. fz_outline_iterator *
  605. fz_new_outline_iterator(fz_context *ctx, fz_document *doc)
  606. {
  607. if (doc == NULL)
  608. return NULL;
  609. if (doc->outline_iterator)
  610. return doc->outline_iterator(ctx, doc);
  611. if (doc->load_outline == NULL)
  612. return NULL;
  613. return fz_outline_iterator_from_outline(ctx, fz_load_outline(ctx, doc));
  614. }
  615. fz_link_dest
  616. fz_resolve_link_dest(fz_context *ctx, fz_document *doc, const char *uri)
  617. {
  618. fz_ensure_layout(ctx, doc);
  619. if (doc && doc->resolve_link_dest)
  620. return doc->resolve_link_dest(ctx, doc, uri);
  621. return fz_make_link_dest_none();
  622. }
  623. char *
  624. fz_format_link_uri(fz_context *ctx, fz_document *doc, fz_link_dest dest)
  625. {
  626. if (doc && doc->format_link_uri)
  627. return doc->format_link_uri(ctx, doc, dest);
  628. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot create internal links for this document type");
  629. }
  630. fz_location
  631. fz_resolve_link(fz_context *ctx, fz_document *doc, const char *uri, float *xp, float *yp)
  632. {
  633. fz_link_dest dest = fz_resolve_link_dest(ctx, doc, uri);
  634. if (xp) *xp = dest.x;
  635. if (yp) *yp = dest.y;
  636. return dest.loc;
  637. }
  638. void
  639. fz_layout_document(fz_context *ctx, fz_document *doc, float w, float h, float em)
  640. {
  641. if (doc && doc->layout)
  642. {
  643. doc->layout(ctx, doc, w, h, em);
  644. doc->did_layout = 1;
  645. }
  646. }
  647. int
  648. fz_count_chapters(fz_context *ctx, fz_document *doc)
  649. {
  650. fz_ensure_layout(ctx, doc);
  651. if (doc && doc->count_chapters)
  652. return doc->count_chapters(ctx, doc);
  653. return 1;
  654. }
  655. int
  656. fz_count_chapter_pages(fz_context *ctx, fz_document *doc, int chapter)
  657. {
  658. fz_ensure_layout(ctx, doc);
  659. if (doc && doc->count_pages)
  660. return doc->count_pages(ctx, doc, chapter);
  661. return 0;
  662. }
  663. int
  664. fz_count_pages(fz_context *ctx, fz_document *doc)
  665. {
  666. int i, c, n = 0;
  667. c = fz_count_chapters(ctx, doc);
  668. for (i = 0; i < c; ++i)
  669. n += fz_count_chapter_pages(ctx, doc, i);
  670. return n;
  671. }
  672. fz_page *
  673. fz_load_page(fz_context *ctx, fz_document *doc, int number)
  674. {
  675. int i, n = fz_count_chapters(ctx, doc);
  676. int start = 0;
  677. for (i = 0; i < n; ++i)
  678. {
  679. int m = fz_count_chapter_pages(ctx, doc, i);
  680. if (number < start + m)
  681. return fz_load_chapter_page(ctx, doc, i, number - start);
  682. start += m;
  683. }
  684. fz_throw(ctx, FZ_ERROR_ARGUMENT, "invalid page number: %d", number+1);
  685. }
  686. fz_location fz_last_page(fz_context *ctx, fz_document *doc)
  687. {
  688. int nc = fz_count_chapters(ctx, doc);
  689. int np = fz_count_chapter_pages(ctx, doc, nc-1);
  690. return fz_make_location(nc-1, np-1);
  691. }
  692. fz_location fz_next_page(fz_context *ctx, fz_document *doc, fz_location loc)
  693. {
  694. int nc = fz_count_chapters(ctx, doc);
  695. int np = fz_count_chapter_pages(ctx, doc, loc.chapter);
  696. if (loc.page + 1 == np)
  697. {
  698. if (loc.chapter + 1 < nc)
  699. {
  700. return fz_make_location(loc.chapter + 1, 0);
  701. }
  702. }
  703. else
  704. {
  705. return fz_make_location(loc.chapter, loc.page + 1);
  706. }
  707. return loc;
  708. }
  709. fz_location fz_previous_page(fz_context *ctx, fz_document *doc, fz_location loc)
  710. {
  711. if (loc.page == 0)
  712. {
  713. if (loc.chapter > 0)
  714. {
  715. int np = fz_count_chapter_pages(ctx, doc, loc.chapter - 1);
  716. return fz_make_location(loc.chapter - 1, np - 1);
  717. }
  718. }
  719. else
  720. {
  721. return fz_make_location(loc.chapter, loc.page - 1);
  722. }
  723. return loc;
  724. }
  725. fz_location fz_clamp_location(fz_context *ctx, fz_document *doc, fz_location loc)
  726. {
  727. int nc = fz_count_chapters(ctx, doc);
  728. int np;
  729. if (loc.chapter < 0) loc.chapter = 0;
  730. if (loc.chapter >= nc) loc.chapter = nc - 1;
  731. np = fz_count_chapter_pages(ctx, doc, loc.chapter);
  732. if (loc.page < 0) loc.page = 0;
  733. if (loc.page >= np) loc.page = np - 1;
  734. return loc;
  735. }
  736. fz_location fz_location_from_page_number(fz_context *ctx, fz_document *doc, int number)
  737. {
  738. int i, m = 0, n = fz_count_chapters(ctx, doc);
  739. int start = 0;
  740. if (number < 0)
  741. number = 0;
  742. for (i = 0; i < n; ++i)
  743. {
  744. m = fz_count_chapter_pages(ctx, doc, i);
  745. if (number < start + m)
  746. return fz_make_location(i, number - start);
  747. start += m;
  748. }
  749. return fz_make_location(i-1, m-1);
  750. }
  751. int fz_page_number_from_location(fz_context *ctx, fz_document *doc, fz_location loc)
  752. {
  753. int i, n, start = 0;
  754. n = fz_count_chapters(ctx, doc);
  755. for (i = 0; i < n; ++i)
  756. {
  757. if (i == loc.chapter)
  758. return start + loc.page;
  759. start += fz_count_chapter_pages(ctx, doc, i);
  760. }
  761. return -1;
  762. }
  763. int
  764. fz_lookup_metadata(fz_context *ctx, fz_document *doc, const char *key, char *buf, size_t size)
  765. {
  766. if (buf && size > 0)
  767. buf[0] = 0;
  768. if (doc && doc->lookup_metadata)
  769. return doc->lookup_metadata(ctx, doc, key, buf, size);
  770. return -1;
  771. }
  772. void
  773. fz_set_metadata(fz_context *ctx, fz_document *doc, const char *key, const char *value)
  774. {
  775. if (doc && doc->set_metadata)
  776. doc->set_metadata(ctx, doc, key, value);
  777. }
  778. fz_colorspace *
  779. fz_document_output_intent(fz_context *ctx, fz_document *doc)
  780. {
  781. if (doc && doc->get_output_intent)
  782. return doc->get_output_intent(ctx, doc);
  783. return NULL;
  784. }
  785. static void
  786. fz_reap_dead_pages(fz_context *ctx, fz_document *doc)
  787. {
  788. fz_page *page;
  789. fz_page *next_page;
  790. for (page = doc->open; page; page = next_page)
  791. {
  792. next_page = page->next;
  793. if (!page->doc)
  794. {
  795. if (page->next != NULL)
  796. page->next->prev = page->prev;
  797. if (page->prev != NULL)
  798. *page->prev = page->next;
  799. fz_free(ctx, page);
  800. if (page == doc->open)
  801. doc->open = next_page;
  802. }
  803. }
  804. }
  805. fz_page *
  806. fz_load_chapter_page(fz_context *ctx, fz_document *doc, int chapter, int number)
  807. {
  808. fz_page *page;
  809. if (doc == NULL)
  810. return NULL;
  811. fz_ensure_layout(ctx, doc);
  812. // Trigger reaping dead pages when we load a new page.
  813. fz_reap_dead_pages(ctx, doc);
  814. /* Protect modifications to the page list to cope with
  815. * destruction of pages on other threads. */
  816. for (page = doc->open; page; page = page->next)
  817. {
  818. if (page->chapter == chapter && page->number == number)
  819. {
  820. fz_keep_page(ctx, page);
  821. return page;
  822. }
  823. }
  824. if (doc->load_page)
  825. {
  826. page = doc->load_page(ctx, doc, chapter, number);
  827. page->chapter = chapter;
  828. page->number = number;
  829. /* Insert new page at the head of the list of open pages. */
  830. if (!page->incomplete)
  831. {
  832. if ((page->next = doc->open) != NULL)
  833. doc->open->prev = &page->next;
  834. doc->open = page;
  835. page->prev = &doc->open;
  836. page->in_doc = 1;
  837. }
  838. return page;
  839. }
  840. return NULL;
  841. }
  842. fz_link *
  843. fz_load_links(fz_context *ctx, fz_page *page)
  844. {
  845. if (page && page->load_links)
  846. return page->load_links(ctx, page);
  847. return NULL;
  848. }
  849. fz_rect
  850. fz_bound_page(fz_context *ctx, fz_page *page)
  851. {
  852. if (page && page->bound_page)
  853. return page->bound_page(ctx, page, FZ_CROP_BOX);
  854. return fz_empty_rect;
  855. }
  856. fz_rect
  857. fz_bound_page_box(fz_context *ctx, fz_page *page, fz_box_type box)
  858. {
  859. if (page && page->bound_page)
  860. return page->bound_page(ctx, page, box);
  861. return fz_empty_rect;
  862. }
  863. void
  864. fz_run_document_structure(fz_context *ctx, fz_document *doc, fz_device *dev, fz_cookie *cookie)
  865. {
  866. if (doc && doc->run_structure)
  867. {
  868. fz_try(ctx)
  869. {
  870. doc->run_structure(ctx, doc, dev, cookie);
  871. }
  872. fz_catch(ctx)
  873. {
  874. dev->close_device = NULL; /* aborted run, don't warn about unclosed device */
  875. fz_rethrow_unless(ctx, FZ_ERROR_ABORT);
  876. fz_ignore_error(ctx);
  877. }
  878. }
  879. }
  880. void
  881. fz_run_page_contents(fz_context *ctx, fz_page *page, fz_device *dev, fz_matrix transform, fz_cookie *cookie)
  882. {
  883. if (page && page->run_page_contents)
  884. {
  885. fz_try(ctx)
  886. {
  887. page->run_page_contents(ctx, page, dev, transform, cookie);
  888. }
  889. fz_catch(ctx)
  890. {
  891. dev->close_device = NULL; /* aborted run, don't warn about unclosed device */
  892. fz_rethrow_unless(ctx, FZ_ERROR_ABORT);
  893. fz_ignore_error(ctx);
  894. }
  895. }
  896. }
  897. void
  898. fz_run_page_annots(fz_context *ctx, fz_page *page, fz_device *dev, fz_matrix transform, fz_cookie *cookie)
  899. {
  900. if (page && page->run_page_annots)
  901. {
  902. fz_try(ctx)
  903. {
  904. page->run_page_annots(ctx, page, dev, transform, cookie);
  905. }
  906. fz_catch(ctx)
  907. {
  908. dev->close_device = NULL; /* aborted run, don't warn about unclosed device */
  909. fz_rethrow_unless(ctx, FZ_ERROR_ABORT);
  910. fz_ignore_error(ctx);
  911. }
  912. }
  913. }
  914. void
  915. fz_run_page_widgets(fz_context *ctx, fz_page *page, fz_device *dev, fz_matrix transform, fz_cookie *cookie)
  916. {
  917. if (page && page->run_page_widgets)
  918. {
  919. fz_try(ctx)
  920. {
  921. page->run_page_widgets(ctx, page, dev, transform, cookie);
  922. }
  923. fz_catch(ctx)
  924. {
  925. dev->close_device = NULL; /* aborted run, don't warn about unclosed device */
  926. fz_rethrow_unless(ctx, FZ_ERROR_ABORT);
  927. fz_ignore_error(ctx);
  928. }
  929. }
  930. }
  931. void
  932. fz_run_page(fz_context *ctx, fz_page *page, fz_device *dev, fz_matrix transform, fz_cookie *cookie)
  933. {
  934. fz_run_page_contents(ctx, page, dev, transform, cookie);
  935. fz_run_page_annots(ctx, page, dev, transform, cookie);
  936. fz_run_page_widgets(ctx, page, dev, transform, cookie);
  937. }
  938. fz_page *
  939. fz_new_page_of_size(fz_context *ctx, int size, fz_document *doc)
  940. {
  941. fz_page *page = Memento_label(fz_calloc(ctx, 1, size), "fz_page");
  942. page->refs = 1;
  943. page->doc = fz_keep_document(ctx, doc);
  944. return page;
  945. }
  946. fz_page *
  947. fz_keep_page(fz_context *ctx, fz_page *page)
  948. {
  949. return fz_keep_imp(ctx, page, &page->refs);
  950. }
  951. void
  952. fz_drop_page(fz_context *ctx, fz_page *page)
  953. {
  954. if (fz_drop_imp(ctx, page, &page->refs))
  955. {
  956. fz_document *doc = page->doc;
  957. if (page->drop_page)
  958. page->drop_page(ctx, page);
  959. // Mark the page as dead so we can reap the struct allocation later.
  960. page->doc = NULL;
  961. page->chapter = -1;
  962. page->number = -1;
  963. // If page has never been added to the list of open pages in a document,
  964. // it will not get be reaped upon document freeing; instead free the page
  965. // immediately.
  966. if (!page->in_doc)
  967. fz_free(ctx, page);
  968. fz_drop_document(ctx, doc);
  969. }
  970. }
  971. fz_transition *
  972. fz_page_presentation(fz_context *ctx, fz_page *page, fz_transition *transition, float *duration)
  973. {
  974. float dummy;
  975. if (duration)
  976. *duration = 0;
  977. else
  978. duration = &dummy;
  979. if (page && page->page_presentation && page)
  980. return page->page_presentation(ctx, page, transition, duration);
  981. return NULL;
  982. }
  983. fz_separations *
  984. fz_page_separations(fz_context *ctx, fz_page *page)
  985. {
  986. if (page && page->separations)
  987. return page->separations(ctx, page);
  988. return NULL;
  989. }
  990. int fz_page_uses_overprint(fz_context *ctx, fz_page *page)
  991. {
  992. if (page && page->overprint)
  993. return page->overprint(ctx, page);
  994. return 0;
  995. }
  996. fz_link *fz_create_link(fz_context *ctx, fz_page *page, fz_rect bbox, const char *uri)
  997. {
  998. if (page == NULL || uri == NULL)
  999. return NULL;
  1000. if (page->create_link == NULL)
  1001. fz_throw(ctx, FZ_ERROR_ARGUMENT, "This format of document does not support creating links");
  1002. return page->create_link(ctx, page, bbox, uri);
  1003. }
  1004. void fz_delete_link(fz_context *ctx, fz_page *page, fz_link *link)
  1005. {
  1006. if (page == NULL || link == NULL)
  1007. return;
  1008. if (page->delete_link == NULL)
  1009. fz_throw(ctx, FZ_ERROR_ARGUMENT, "This format of document does not support deleting links");
  1010. page->delete_link(ctx, page, link);
  1011. }
  1012. void fz_set_link_rect(fz_context *ctx, fz_link *link, fz_rect rect)
  1013. {
  1014. if (link == NULL)
  1015. return;
  1016. if (link->set_rect_fn == NULL)
  1017. fz_throw(ctx, FZ_ERROR_ARGUMENT, "This format of document does not support updating link bounds");
  1018. link->set_rect_fn(ctx, link, rect);
  1019. }
  1020. void fz_set_link_uri(fz_context *ctx, fz_link *link, const char *uri)
  1021. {
  1022. if (link == NULL)
  1023. return;
  1024. if (link->set_uri_fn == NULL)
  1025. fz_throw(ctx, FZ_ERROR_ARGUMENT, "This format of document does not support updating link uri");
  1026. link->set_uri_fn(ctx, link, uri);
  1027. }
  1028. void *
  1029. fz_process_opened_pages(fz_context *ctx, fz_document *doc, fz_process_opened_page_fn *process_opened_page, void *state)
  1030. {
  1031. fz_page *page;
  1032. void *ret;
  1033. for (page = doc->open; page != NULL; page = page->next)
  1034. {
  1035. // Skip dead pages.
  1036. if (page->doc == NULL)
  1037. continue;
  1038. ret = process_opened_page(ctx, page, state);
  1039. if (ret)
  1040. return ret;
  1041. }
  1042. return NULL;
  1043. }
  1044. const char *
  1045. fz_page_label(fz_context *ctx, fz_page *page, char *buf, int size)
  1046. {
  1047. fz_document *doc = page->doc;
  1048. if (doc->page_label)
  1049. doc->page_label(ctx, page->doc, page->chapter, page->number, buf, size);
  1050. else if (fz_count_chapters(ctx, page->doc) > 1)
  1051. fz_snprintf(buf, size, "%d/%d", page->chapter + 1, page->number + 1);
  1052. else
  1053. fz_snprintf(buf, size, "%d", page->number + 1);
  1054. return buf;
  1055. }
  1056. fz_box_type fz_box_type_from_string(const char *name)
  1057. {
  1058. if (!fz_strcasecmp(name, "MediaBox"))
  1059. return FZ_MEDIA_BOX;
  1060. if (!fz_strcasecmp(name, "CropBox"))
  1061. return FZ_CROP_BOX;
  1062. if (!fz_strcasecmp(name, "BleedBox"))
  1063. return FZ_BLEED_BOX;
  1064. if (!fz_strcasecmp(name, "TrimBox"))
  1065. return FZ_TRIM_BOX;
  1066. if (!fz_strcasecmp(name, "ArtBox"))
  1067. return FZ_ART_BOX;
  1068. return FZ_UNKNOWN_BOX;
  1069. }
  1070. const char *fz_string_from_box_type(fz_box_type box)
  1071. {
  1072. switch (box)
  1073. {
  1074. case FZ_MEDIA_BOX: return "MediaBox";
  1075. case FZ_CROP_BOX: return "CropBox";
  1076. case FZ_BLEED_BOX: return "BleedBox";
  1077. case FZ_TRIM_BOX: return "TrimBox";
  1078. case FZ_ART_BOX: return "ArtBox";
  1079. default: return "UnknownBox";
  1080. }
  1081. }