context.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 <assert.h>
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include <time.h>
  28. struct fz_style_context
  29. {
  30. int refs;
  31. char *user_css;
  32. int use_document_css;
  33. };
  34. static void fz_new_style_context(fz_context *ctx)
  35. {
  36. if (ctx)
  37. {
  38. ctx->style = fz_malloc_struct(ctx, fz_style_context);
  39. ctx->style->refs = 1;
  40. ctx->style->user_css = NULL;
  41. ctx->style->use_document_css = 1;
  42. }
  43. }
  44. static fz_style_context *fz_keep_style_context(fz_context *ctx)
  45. {
  46. if (!ctx)
  47. return NULL;
  48. return fz_keep_imp(ctx, ctx->style, &ctx->style->refs);
  49. }
  50. static void fz_drop_style_context(fz_context *ctx)
  51. {
  52. if (!ctx)
  53. return;
  54. if (fz_drop_imp(ctx, ctx->style, &ctx->style->refs))
  55. {
  56. fz_free(ctx, ctx->style->user_css);
  57. fz_free(ctx, ctx->style);
  58. }
  59. }
  60. void fz_set_use_document_css(fz_context *ctx, int use)
  61. {
  62. ctx->style->use_document_css = use;
  63. }
  64. int fz_use_document_css(fz_context *ctx)
  65. {
  66. return ctx->style->use_document_css;
  67. }
  68. void fz_set_user_css(fz_context *ctx, const char *user_css)
  69. {
  70. fz_free(ctx, ctx->style->user_css);
  71. ctx->style->user_css = user_css ? fz_strdup(ctx, user_css) : NULL;
  72. }
  73. const char *fz_user_css(fz_context *ctx)
  74. {
  75. return ctx->style->user_css;
  76. }
  77. void fz_load_user_css(fz_context *ctx, const char *filename)
  78. {
  79. fz_buffer *buf = NULL;
  80. fz_var(buf);
  81. fz_try(ctx)
  82. {
  83. buf = fz_read_file(ctx, filename);
  84. fz_set_user_css(ctx, fz_string_from_buffer(ctx, buf));
  85. }
  86. fz_always(ctx)
  87. {
  88. fz_drop_buffer(ctx, buf);
  89. }
  90. fz_catch(ctx)
  91. {
  92. fz_report_error(ctx);
  93. fz_warn(ctx, "cannot read user css file");
  94. }
  95. }
  96. static void fz_new_tuning_context(fz_context *ctx)
  97. {
  98. if (ctx)
  99. {
  100. ctx->tuning = fz_malloc_struct(ctx, fz_tuning_context);
  101. ctx->tuning->refs = 1;
  102. ctx->tuning->image_decode = fz_default_image_decode;
  103. ctx->tuning->image_scale = fz_default_image_scale;
  104. }
  105. }
  106. static fz_tuning_context *fz_keep_tuning_context(fz_context *ctx)
  107. {
  108. if (!ctx)
  109. return NULL;
  110. return fz_keep_imp(ctx, ctx->tuning, &ctx->tuning->refs);
  111. }
  112. static void fz_drop_tuning_context(fz_context *ctx)
  113. {
  114. if (!ctx)
  115. return;
  116. if (fz_drop_imp(ctx, ctx->tuning, &ctx->tuning->refs))
  117. {
  118. fz_free(ctx, ctx->tuning);
  119. }
  120. }
  121. void fz_tune_image_decode(fz_context *ctx, fz_tune_image_decode_fn *image_decode, void *arg)
  122. {
  123. ctx->tuning->image_decode = image_decode ? image_decode : fz_default_image_decode;
  124. ctx->tuning->image_decode_arg = arg;
  125. }
  126. void fz_tune_image_scale(fz_context *ctx, fz_tune_image_scale_fn *image_scale, void *arg)
  127. {
  128. ctx->tuning->image_scale = image_scale ? image_scale : fz_default_image_scale;
  129. ctx->tuning->image_scale_arg = arg;
  130. }
  131. static void fz_init_random_context(fz_context *ctx)
  132. {
  133. if (!ctx)
  134. return;
  135. ctx->seed48[0] = 0;
  136. ctx->seed48[1] = 0;
  137. ctx->seed48[2] = 0;
  138. ctx->seed48[3] = 0xe66d;
  139. ctx->seed48[4] = 0xdeec;
  140. ctx->seed48[5] = 0x5;
  141. ctx->seed48[6] = 0xb;
  142. fz_srand48(ctx, (uint32_t)time(NULL));
  143. }
  144. void
  145. fz_drop_context(fz_context *ctx)
  146. {
  147. int free_master = 0;
  148. int call_log = 0;
  149. if (!ctx)
  150. return;
  151. if (ctx->error.errcode)
  152. {
  153. fz_flush_warnings(ctx);
  154. fz_warn(ctx, "UNHANDLED EXCEPTION!");
  155. fz_report_error(ctx);
  156. #ifdef CLUSTER
  157. abort();
  158. #endif
  159. }
  160. assert(ctx->master);
  161. fz_lock(ctx, FZ_LOCK_ALLOC);
  162. ctx->master->context_count--;
  163. if (ctx->master->context_count == 0)
  164. {
  165. call_log = 1;
  166. if (ctx->master != ctx)
  167. free_master = 1;
  168. }
  169. fz_unlock(ctx, FZ_LOCK_ALLOC);
  170. /* We call the log with ctx intact, apart from the master
  171. * pointer having had the context_count reduced. The
  172. * only possible problem here is if fz_log_activity
  173. * clones the context, but it really shouldn't be doing
  174. * that! */
  175. if (call_log)
  176. fz_log_activity(ctx, FZ_ACTIVITY_SHUTDOWN, NULL);
  177. if (free_master)
  178. ctx->alloc.free(ctx->alloc.user, ctx->master);
  179. /* Other finalisation calls go here (in reverse order) */
  180. fz_drop_document_handler_context(ctx);
  181. fz_drop_archive_handler_context(ctx);
  182. fz_drop_glyph_cache_context(ctx);
  183. fz_drop_store_context(ctx);
  184. fz_drop_style_context(ctx);
  185. fz_drop_tuning_context(ctx);
  186. fz_drop_colorspace_context(ctx);
  187. fz_drop_font_context(ctx);
  188. fz_flush_warnings(ctx);
  189. assert(ctx->error.top == ctx->error.stack_base);
  190. /* Free the context itself */
  191. if (ctx->master == ctx && ctx->context_count != 0)
  192. {
  193. /* Need to delay our freeing until all our children have died. */
  194. ctx->master = NULL;
  195. }
  196. else
  197. {
  198. ctx->alloc.free(ctx->alloc.user, ctx);
  199. }
  200. }
  201. static void
  202. fz_init_error_context(fz_context *ctx)
  203. {
  204. #define ALIGN(addr, align) ((((intptr_t)(addr)) + (align-1)) & ~(align-1))
  205. ctx->error.stack_base = (fz_error_stack_slot *)ALIGN(ctx->error.stack, FZ_JMPBUF_ALIGN);
  206. ctx->error.top = ctx->error.stack_base;
  207. ctx->error.errcode = FZ_ERROR_NONE;
  208. ctx->error.message[0] = 0;
  209. ctx->warn.message[0] = 0;
  210. ctx->warn.count = 0;
  211. }
  212. fz_context *
  213. fz_new_context_imp(const fz_alloc_context *alloc, const fz_locks_context *locks, size_t max_store, const char *version)
  214. {
  215. fz_context *ctx;
  216. if (strcmp(version, FZ_VERSION))
  217. {
  218. fprintf(stderr, "cannot create context: incompatible header (%s) and library (%s) versions\n", version, FZ_VERSION);
  219. return NULL;
  220. }
  221. if (!alloc)
  222. alloc = &fz_alloc_default;
  223. if (!locks)
  224. locks = &fz_locks_default;
  225. ctx = Memento_label(alloc->malloc(alloc->user, sizeof(fz_context)), "fz_context");
  226. if (!ctx)
  227. {
  228. fprintf(stderr, "cannot create context (phase 1)\n");
  229. return NULL;
  230. }
  231. memset(ctx, 0, sizeof *ctx);
  232. ctx->user = NULL;
  233. ctx->alloc = *alloc;
  234. ctx->locks = *locks;
  235. /* We are our own master! */
  236. ctx->master = ctx;
  237. ctx->context_count = 1;
  238. ctx->error.print = fz_default_error_callback;
  239. ctx->warn.print = fz_default_warning_callback;
  240. fz_init_error_context(ctx);
  241. fz_init_aa_context(ctx);
  242. fz_init_random_context(ctx);
  243. /* Now initialise sections that are shared */
  244. fz_try(ctx)
  245. {
  246. fz_new_store_context(ctx, max_store);
  247. fz_new_glyph_cache_context(ctx);
  248. fz_new_colorspace_context(ctx);
  249. fz_new_font_context(ctx);
  250. fz_new_document_handler_context(ctx);
  251. fz_new_archive_handler_context(ctx);
  252. fz_new_style_context(ctx);
  253. fz_new_tuning_context(ctx);
  254. }
  255. fz_catch(ctx)
  256. {
  257. fz_report_error(ctx);
  258. fprintf(stderr, "cannot create context (phase 2)\n");
  259. fz_drop_context(ctx);
  260. return NULL;
  261. }
  262. return ctx;
  263. }
  264. fz_context *
  265. fz_clone_context(fz_context *ctx)
  266. {
  267. fz_context *new_ctx;
  268. /* We cannot safely clone the context without having locking/
  269. * unlocking functions. */
  270. if (ctx == NULL || (ctx->locks.lock == fz_locks_default.lock && ctx->locks.unlock == fz_locks_default.unlock))
  271. return NULL;
  272. new_ctx = ctx->alloc.malloc(ctx->alloc.user, sizeof(fz_context));
  273. if (!new_ctx)
  274. return NULL;
  275. fz_lock(ctx, FZ_LOCK_ALLOC);
  276. ctx->master->context_count++;
  277. fz_unlock(ctx, FZ_LOCK_ALLOC);
  278. new_ctx->master = ctx->master;
  279. /* First copy old context, including pointers to shared contexts */
  280. memcpy(new_ctx, ctx, sizeof (fz_context));
  281. /* Reset error context to initial state. */
  282. fz_init_error_context(new_ctx);
  283. /* Then keep lock checking happy by keeping shared contexts with new context */
  284. fz_keep_document_handler_context(new_ctx);
  285. fz_keep_archive_handler_context(new_ctx);
  286. fz_keep_style_context(new_ctx);
  287. fz_keep_tuning_context(new_ctx);
  288. fz_keep_font_context(new_ctx);
  289. fz_keep_colorspace_context(new_ctx);
  290. fz_keep_store_context(new_ctx);
  291. fz_keep_glyph_cache(new_ctx);
  292. return new_ctx;
  293. }
  294. void fz_set_user_context(fz_context *ctx, void *user)
  295. {
  296. if (ctx != NULL)
  297. ctx->user = user;
  298. }
  299. void *fz_user_context(fz_context *ctx)
  300. {
  301. if (ctx == NULL)
  302. return NULL;
  303. return ctx->user;
  304. }
  305. void fz_register_activity_logger(fz_context *ctx, fz_activity_fn *activity, void *opaque)
  306. {
  307. if (ctx == NULL)
  308. return;
  309. ctx->activity.activity = activity;
  310. ctx->activity.opaque = opaque;
  311. }
  312. void fz_log_activity(fz_context *ctx, fz_activity_reason reason, void *arg)
  313. {
  314. if (ctx == NULL || ctx->activity.activity == NULL)
  315. return;
  316. ctx->activity.activity(ctx, ctx->activity.opaque, reason, arg);
  317. }