context.h 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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_CONTEXT_H
  23. #define MUPDF_FITZ_CONTEXT_H
  24. #include "mupdf/fitz/version.h"
  25. #include "mupdf/fitz/system.h"
  26. #include "mupdf/fitz/geometry.h"
  27. #ifndef FZ_VERBOSE_EXCEPTIONS
  28. #define FZ_VERBOSE_EXCEPTIONS 0
  29. #endif
  30. typedef struct fz_font_context fz_font_context;
  31. typedef struct fz_colorspace_context fz_colorspace_context;
  32. typedef struct fz_style_context fz_style_context;
  33. typedef struct fz_tuning_context fz_tuning_context;
  34. typedef struct fz_store fz_store;
  35. typedef struct fz_glyph_cache fz_glyph_cache;
  36. typedef struct fz_document_handler_context fz_document_handler_context;
  37. typedef struct fz_archive_handler_context fz_archive_handler_context;
  38. typedef struct fz_output fz_output;
  39. typedef struct fz_context fz_context;
  40. /**
  41. Allocator structure; holds callbacks and private data pointer.
  42. */
  43. typedef struct
  44. {
  45. void *user;
  46. void *(*malloc)(void *, size_t);
  47. void *(*realloc)(void *, void *, size_t);
  48. void (*free)(void *, void *);
  49. } fz_alloc_context;
  50. /**
  51. Exception macro definitions. Just treat these as a black box -
  52. pay no attention to the man behind the curtain.
  53. */
  54. #define fz_var(var) fz_var_imp((void *)&(var))
  55. #define fz_try(ctx) if (!fz_setjmp(*fz_push_try(ctx))) if (fz_do_try(ctx)) do
  56. #define fz_always(ctx) while (0); if (fz_do_always(ctx)) do
  57. #define fz_catch(ctx) while (0); if (fz_do_catch(ctx))
  58. /**
  59. These macros provide a simple exception handling system. Use them as
  60. follows:
  61. fz_try(ctx)
  62. ...
  63. fz_catch(ctx)
  64. ...
  65. or as:
  66. fz_try(ctx)
  67. ...
  68. fz_always(ctx)
  69. ...
  70. fz_catch(ctx)
  71. ...
  72. Code within the fz_try() section can then throw exceptions using fz_throw()
  73. (or fz_vthrow()).
  74. They are implemented with setjmp/longjmp, which can have unfortunate
  75. consequences for 'losing' local variable values on a throw. To avoid this
  76. we recommend calling 'fz_var(variable)' before the fz_try() for any
  77. local variable whose value may change within the fz_try() block and whose
  78. value will be required afterwards.
  79. Do not call anything in the fz_always() section that can throw.
  80. Any exception can be rethrown from the fz_catch() section using fz_rethrow()
  81. as long as there has been no intervening use of fz_try/fz_catch.
  82. */
  83. /**
  84. Throw an exception.
  85. This assumes an enclosing fz_try() block within the callstack.
  86. */
  87. FZ_NORETURN void fz_vthrow(fz_context *ctx, int errcode, const char *, va_list ap);
  88. FZ_NORETURN void fz_throw(fz_context *ctx, int errcode, const char *, ...) FZ_PRINTFLIKE(3,4);
  89. FZ_NORETURN void fz_rethrow(fz_context *ctx);
  90. /**
  91. Called within a catch block this modifies the current
  92. exception's code. If it's of type 'fromcode' it is
  93. modified to 'tocode'. Typically used for 'downgrading'
  94. exception severity.
  95. */
  96. void fz_morph_error(fz_context *ctx, int fromcode, int tocode);
  97. /**
  98. Log a warning.
  99. This goes to the registered warning stream (stderr by
  100. default).
  101. */
  102. void fz_vwarn(fz_context *ctx, const char *fmt, va_list ap);
  103. void fz_warn(fz_context *ctx, const char *fmt, ...) FZ_PRINTFLIKE(2,3);
  104. /**
  105. Within an fz_catch() block, retrieve the formatted message
  106. string for the current exception.
  107. This assumes no intervening use of fz_try/fz_catch.
  108. */
  109. const char *fz_caught_message(fz_context *ctx);
  110. /**
  111. Within an fz_catch() block, retrieve the error code for
  112. the current exception.
  113. This assumes no intervening use of fz_try/fz_catch.
  114. */
  115. int fz_caught(fz_context *ctx);
  116. /*
  117. Within an fz_catch() block, retrieve the errno code for
  118. the current SYSTEM exception.
  119. Is undefined for non-SYSTEM errors.
  120. */
  121. int fz_caught_errno(fz_context *ctx);
  122. /**
  123. Within an fz_catch() block, rethrow the current exception
  124. if the errcode of the current exception matches.
  125. This assumes no intervening use of fz_try/fz_catch.
  126. */
  127. void fz_rethrow_if(fz_context *ctx, int errcode);
  128. void fz_rethrow_unless(fz_context *ctx, int errcode);
  129. /**
  130. Format an error message, and log it to the registered
  131. error stream (stderr by default).
  132. */
  133. void fz_log_error_printf(fz_context *ctx, const char *fmt, ...) FZ_PRINTFLIKE(2,3);
  134. void fz_vlog_error_printf(fz_context *ctx, const char *fmt, va_list ap);
  135. /**
  136. Log a (preformatted) string to the registered
  137. error stream (stderr by default).
  138. */
  139. void fz_log_error(fz_context *ctx, const char *str);
  140. void fz_start_throw_on_repair(fz_context *ctx);
  141. void fz_end_throw_on_repair(fz_context *ctx);
  142. /**
  143. Now, a debugging feature. If FZ_VERBOSE_EXCEPTIONS is 1 then
  144. some of the above functions are replaced by versions that print
  145. FILE and LINE information.
  146. */
  147. #if FZ_VERBOSE_EXCEPTIONS
  148. #define fz_vthrow(CTX, ERRCODE, FMT, VA) fz_vthrowFL(CTX, __FILE__, __LINE__, ERRCODE, FMT, VA)
  149. #define fz_throw(CTX, ERRCODE, ...) fz_throwFL(CTX, __FILE__, __LINE__, ERRCODE, __VA_ARGS__)
  150. #define fz_rethrow(CTX) fz_rethrowFL(CTX, __FILE__, __LINE__)
  151. #define fz_morph_error(CTX, FROM, TO) fz_morph_errorFL(CTX, __FILE__, __LINE__, FROM, TO)
  152. #define fz_vwarn(CTX, FMT, VA) fz_vwarnFL(CTX, __FILE__, __LINE__, FMT, VA)
  153. #define fz_warn(CTX, ...) fz_warnFL(CTX, __FILE__, __LINE__, __VA_ARGS__)
  154. #define fz_rethrow_if(CTX, ERRCODE) fz_rethrow_ifFL(CTX, __FILE__, __LINE__, ERRCODE)
  155. #define fz_rethrow_unless(CTX, ERRCODE) fz_rethrow_unlessFL(CTX, __FILE__, __LINE__, ERRCODE)
  156. #define fz_log_error_printf(CTX, ...) fz_log_error_printfFL(CTX, __FILE__, __LINE__, __VA_ARGS__)
  157. #define fz_vlog_error_printf(CTX, FMT, VA) fz_log_error_printfFL(CTX, __FILE__, __LINE__, FMT, VA)
  158. #define fz_log_error(CTX, STR) fz_log_error_printfFL(CTX, __FILE__, __LINE__, STR)
  159. #define fz_do_catch(CTX) fz_do_catchFL(CTX, __FILE__, __LINE__)
  160. FZ_NORETURN void fz_vthrowFL(fz_context *ctx, const char *file, int line, int errcode, const char *fmt, va_list ap);
  161. FZ_NORETURN void fz_throwFL(fz_context *ctx, const char *file, int line, int errcode, const char *fmt, ...) FZ_PRINTFLIKE(5,6);
  162. FZ_NORETURN void fz_rethrowFL(fz_context *ctx, const char *file, int line);
  163. void fz_morph_errorFL(fz_context *ctx, const char *file, int line, int fromcode, int tocode);
  164. void fz_vwarnFL(fz_context *ctx, const char *file, int line, const char *fmt, va_list ap);
  165. void fz_warnFL(fz_context *ctx, const char *file, int line, const char *fmt, ...) FZ_PRINTFLIKE(4,5);
  166. void fz_rethrow_ifFL(fz_context *ctx, const char *file, int line, int errcode);
  167. void fz_rethrow_unlessFL(fz_context *ctx, const char *file, int line, int errcode);
  168. void fz_log_error_printfFL(fz_context *ctx, const char *file, int line, const char *fmt, ...) FZ_PRINTFLIKE(4,5);
  169. void fz_vlog_error_printfFL(fz_context *ctx, const char *file, int line, const char *fmt, va_list ap);
  170. void fz_log_errorFL(fz_context *ctx, const char *file, int line, const char *str);
  171. int fz_do_catchFL(fz_context *ctx, const char *file, int line);
  172. #endif
  173. /* Report an error to the registered error callback. */
  174. void fz_report_error(fz_context *ctx);
  175. /*
  176. * Swallow an error and ignore it completely.
  177. * This should only be called to signal that you've handled a TRYLATER or ABORT error,
  178. */
  179. void fz_ignore_error(fz_context *ctx);
  180. /* Convert an error into another runtime exception.
  181. * For use when converting an exception from Fitz to a language binding exception.
  182. */
  183. const char *fz_convert_error(fz_context *ctx, int *code);
  184. enum fz_error_type
  185. {
  186. FZ_ERROR_NONE,
  187. FZ_ERROR_GENERIC,
  188. FZ_ERROR_SYSTEM, // fatal out of memory or syscall error
  189. FZ_ERROR_LIBRARY, // unclassified error from third-party library
  190. FZ_ERROR_ARGUMENT, // invalid or out-of-range arguments to functions
  191. FZ_ERROR_LIMIT, // failed because of resource or other hard limits
  192. FZ_ERROR_UNSUPPORTED, // tried to use an unsupported feature
  193. FZ_ERROR_FORMAT, // syntax or format errors that are unrecoverable
  194. FZ_ERROR_SYNTAX, // syntax errors that should be diagnosed and ignored
  195. // for internal use only
  196. FZ_ERROR_TRYLATER, // try-later progressive loading signal
  197. FZ_ERROR_ABORT, // user requested abort signal
  198. FZ_ERROR_REPAIRED, // internal flag used when repairing a PDF to avoid cycles
  199. };
  200. /**
  201. Flush any repeated warnings.
  202. Repeated warnings are buffered, counted and eventually printed
  203. along with the number of repetitions. Call fz_flush_warnings
  204. to force printing of the latest buffered warning and the
  205. number of repetitions, for example to make sure that all
  206. warnings are printed before exiting an application.
  207. */
  208. void fz_flush_warnings(fz_context *ctx);
  209. /**
  210. Locking functions
  211. MuPDF is kept deliberately free of any knowledge of particular
  212. threading systems. As such, in order for safe multi-threaded
  213. operation, we rely on callbacks to client provided functions.
  214. A client is expected to provide FZ_LOCK_MAX number of mutexes,
  215. and a function to lock/unlock each of them. These may be
  216. recursive mutexes, but do not have to be.
  217. If a client does not intend to use multiple threads, then it
  218. may pass NULL instead of a lock structure.
  219. In order to avoid deadlocks, we have one simple rule
  220. internally as to how we use locks: We can never take lock n
  221. when we already hold any lock i, where 0 <= i <= n. In order
  222. to verify this, we have some debugging code, that can be
  223. enabled by defining FITZ_DEBUG_LOCKING.
  224. */
  225. typedef struct
  226. {
  227. void *user;
  228. void (*lock)(void *user, int lock);
  229. void (*unlock)(void *user, int lock);
  230. } fz_locks_context;
  231. enum {
  232. FZ_LOCK_ALLOC = 0,
  233. FZ_LOCK_FREETYPE,
  234. FZ_LOCK_GLYPHCACHE,
  235. FZ_LOCK_MAX
  236. };
  237. #if defined(MEMENTO) || !defined(NDEBUG)
  238. #define FITZ_DEBUG_LOCKING
  239. #endif
  240. #ifdef FITZ_DEBUG_LOCKING
  241. void fz_assert_lock_held(fz_context *ctx, int lock);
  242. void fz_assert_lock_not_held(fz_context *ctx, int lock);
  243. void fz_lock_debug_lock(fz_context *ctx, int lock);
  244. void fz_lock_debug_unlock(fz_context *ctx, int lock);
  245. #else
  246. #define fz_assert_lock_held(A,B) do { } while (0)
  247. #define fz_assert_lock_not_held(A,B) do { } while (0)
  248. #define fz_lock_debug_lock(A,B) do { } while (0)
  249. #define fz_lock_debug_unlock(A,B) do { } while (0)
  250. #endif /* !FITZ_DEBUG_LOCKING */
  251. /**
  252. Specifies the maximum size in bytes of the resource store in
  253. fz_context. Given as argument to fz_new_context.
  254. FZ_STORE_UNLIMITED: Let resource store grow unbounded.
  255. FZ_STORE_DEFAULT: A reasonable upper bound on the size, for
  256. devices that are not memory constrained.
  257. */
  258. enum {
  259. FZ_STORE_UNLIMITED = 0,
  260. FZ_STORE_DEFAULT = 256 << 20,
  261. };
  262. /**
  263. Allocate context containing global state.
  264. The global state contains an exception stack, resource store,
  265. etc. Most functions in MuPDF take a context argument to be
  266. able to reference the global state. See fz_drop_context for
  267. freeing an allocated context.
  268. alloc: Supply a custom memory allocator through a set of
  269. function pointers. Set to NULL for the standard library
  270. allocator. The context will keep the allocator pointer, so the
  271. data it points to must not be modified or freed during the
  272. lifetime of the context.
  273. locks: Supply a set of locks and functions to lock/unlock
  274. them, intended for multi-threaded applications. Set to NULL
  275. when using MuPDF in a single-threaded applications. The
  276. context will keep the locks pointer, so the data it points to
  277. must not be modified or freed during the lifetime of the
  278. context.
  279. max_store: Maximum size in bytes of the resource store, before
  280. it will start evicting cached resources such as fonts and
  281. images. FZ_STORE_UNLIMITED can be used if a hard limit is not
  282. desired. Use FZ_STORE_DEFAULT to get a reasonable size.
  283. May return NULL.
  284. */
  285. #define fz_new_context(alloc, locks, max_store) fz_new_context_imp(alloc, locks, max_store, FZ_VERSION)
  286. /**
  287. Make a clone of an existing context.
  288. This function is meant to be used in multi-threaded
  289. applications where each thread requires its own context, yet
  290. parts of the global state, for example caching, are shared.
  291. ctx: Context obtained from fz_new_context to make a copy of.
  292. ctx must have had locks and lock/functions setup when created.
  293. The two contexts will share the memory allocator, resource
  294. store, locks and lock/unlock functions. They will each have
  295. their own exception stacks though.
  296. May return NULL.
  297. */
  298. fz_context *fz_clone_context(fz_context *ctx);
  299. /**
  300. Free a context and its global state.
  301. The context and all of its global state is freed, and any
  302. buffered warnings are flushed (see fz_flush_warnings). If NULL
  303. is passed in nothing will happen.
  304. Must not be called for a context that is being used in an active
  305. fz_try(), fz_always() or fz_catch() block.
  306. */
  307. void fz_drop_context(fz_context *ctx);
  308. /**
  309. Set the user field in the context.
  310. NULL initially, this field can be set to any opaque value
  311. required by the user. It is copied on clones.
  312. */
  313. void fz_set_user_context(fz_context *ctx, void *user);
  314. /**
  315. Read the user field from the context.
  316. */
  317. void *fz_user_context(fz_context *ctx);
  318. /**
  319. FIXME: Better not to expose fz_default_error_callback, and
  320. fz_default_warning callback and to allow 'NULL' to be used
  321. int fz_set_xxxx_callback to mean "defaults".
  322. FIXME: Do we need/want functions like
  323. fz_error_callback(ctx, message) to allow callers to inject
  324. stuff into the error/warning streams?
  325. */
  326. /**
  327. The default error callback. Declared publicly just so that the
  328. error callback can be set back to this after it has been
  329. overridden.
  330. */
  331. void fz_default_error_callback(void *user, const char *message);
  332. /**
  333. The default warning callback. Declared publicly just so that
  334. the warning callback can be set back to this after it has been
  335. overridden.
  336. */
  337. void fz_default_warning_callback(void *user, const char *message);
  338. /**
  339. A callback called whenever an error message is generated.
  340. The user pointer passed to fz_set_error_callback() is passed
  341. along with the error message.
  342. */
  343. typedef void (fz_error_cb)(void *user, const char *message);
  344. /**
  345. A callback called whenever a warning message is generated.
  346. The user pointer passed to fz_set_warning_callback() is
  347. passed along with the warning message.
  348. */
  349. typedef void (fz_warning_cb)(void *user, const char *message);
  350. /**
  351. Set the error callback. This will be called as part of the
  352. exception handling.
  353. The callback must not throw exceptions!
  354. */
  355. void fz_set_error_callback(fz_context *ctx, fz_error_cb *error_cb, void *user);
  356. /**
  357. Retrieve the currently set error callback, or NULL if none
  358. has been set. Optionally, if user is non-NULL, the user pointer
  359. given when the warning callback was set is also passed back to
  360. the caller.
  361. */
  362. fz_error_cb *fz_error_callback(fz_context *ctx, void **user);
  363. /**
  364. Set the warning callback. This will be called as part of the
  365. exception handling.
  366. The callback must not throw exceptions!
  367. */
  368. void fz_set_warning_callback(fz_context *ctx, fz_warning_cb *warning_cb, void *user);
  369. /**
  370. Retrieve the currently set warning callback, or NULL if none
  371. has been set. Optionally, if user is non-NULL, the user pointer
  372. given when the warning callback was set is also passed back to
  373. the caller.
  374. */
  375. fz_warning_cb *fz_warning_callback(fz_context *ctx, void **user);
  376. /**
  377. In order to tune MuPDF's behaviour, certain functions can
  378. (optionally) be provided by callers.
  379. */
  380. /**
  381. Given the width and height of an image,
  382. the subsample factor, and the subarea of the image actually
  383. required, the caller can decide whether to decode the whole
  384. image or just a subarea.
  385. arg: The caller supplied opaque argument.
  386. w, h: The width/height of the complete image.
  387. l2factor: The log2 factor for subsampling (i.e. image will be
  388. decoded to (w>>l2factor, h>>l2factor)).
  389. subarea: The actual subarea required for the current operation.
  390. The tuning function is allowed to increase this in size if
  391. required.
  392. */
  393. typedef void (fz_tune_image_decode_fn)(void *arg, int w, int h, int l2factor, fz_irect *subarea);
  394. /**
  395. Given the source width and height of
  396. image, together with the actual required width and height,
  397. decide whether we should use mitchell scaling.
  398. arg: The caller supplied opaque argument.
  399. dst_w, dst_h: The actual width/height required on the target
  400. device.
  401. src_w, src_h: The source width/height of the image.
  402. Return 0 not to use the Mitchell scaler, 1 to use the Mitchell
  403. scaler. All other values reserved.
  404. */
  405. typedef int (fz_tune_image_scale_fn)(void *arg, int dst_w, int dst_h, int src_w, int src_h);
  406. /**
  407. Set the tuning function to use for
  408. image decode.
  409. image_decode: Function to use.
  410. arg: Opaque argument to be passed to tuning function.
  411. */
  412. void fz_tune_image_decode(fz_context *ctx, fz_tune_image_decode_fn *image_decode, void *arg);
  413. /**
  414. Set the tuning function to use for
  415. image scaling.
  416. image_scale: Function to use.
  417. arg: Opaque argument to be passed to tuning function.
  418. */
  419. void fz_tune_image_scale(fz_context *ctx, fz_tune_image_scale_fn *image_scale, void *arg);
  420. /**
  421. Get the number of bits of antialiasing we are
  422. using (for graphics). Between 0 and 8.
  423. */
  424. int fz_aa_level(fz_context *ctx);
  425. /**
  426. Set the number of bits of antialiasing we should
  427. use (for both text and graphics).
  428. bits: The number of bits of antialiasing to use (values are
  429. clamped to within the 0 to 8 range).
  430. */
  431. void fz_set_aa_level(fz_context *ctx, int bits);
  432. /**
  433. Get the number of bits of antialiasing we are
  434. using for text. Between 0 and 8.
  435. */
  436. int fz_text_aa_level(fz_context *ctx);
  437. /**
  438. Set the number of bits of antialiasing we
  439. should use for text.
  440. bits: The number of bits of antialiasing to use (values are
  441. clamped to within the 0 to 8 range).
  442. */
  443. void fz_set_text_aa_level(fz_context *ctx, int bits);
  444. /**
  445. Get the number of bits of antialiasing we are
  446. using for graphics. Between 0 and 8.
  447. */
  448. int fz_graphics_aa_level(fz_context *ctx);
  449. /**
  450. Set the number of bits of antialiasing we
  451. should use for graphics.
  452. bits: The number of bits of antialiasing to use (values are
  453. clamped to within the 0 to 8 range).
  454. */
  455. void fz_set_graphics_aa_level(fz_context *ctx, int bits);
  456. /**
  457. Get the minimum line width to be
  458. used for stroked lines.
  459. min_line_width: The minimum line width to use (in pixels).
  460. */
  461. float fz_graphics_min_line_width(fz_context *ctx);
  462. /**
  463. Set the minimum line width to be
  464. used for stroked lines.
  465. min_line_width: The minimum line width to use (in pixels).
  466. */
  467. void fz_set_graphics_min_line_width(fz_context *ctx, float min_line_width);
  468. /**
  469. Get the user stylesheet source text.
  470. */
  471. const char *fz_user_css(fz_context *ctx);
  472. /**
  473. Set the user stylesheet source text for use with HTML and EPUB.
  474. */
  475. void fz_set_user_css(fz_context *ctx, const char *text);
  476. /**
  477. Set the user stylesheet by loading the source from a file.
  478. If the file is missing, do nothing.
  479. */
  480. void fz_load_user_css(fz_context *ctx, const char *filename);
  481. /**
  482. Return whether to respect document styles in HTML and EPUB.
  483. */
  484. int fz_use_document_css(fz_context *ctx);
  485. /**
  486. Toggle whether to respect document styles in HTML and EPUB.
  487. */
  488. void fz_set_use_document_css(fz_context *ctx, int use);
  489. /**
  490. Enable icc profile based operation.
  491. */
  492. void fz_enable_icc(fz_context *ctx);
  493. /**
  494. Disable icc profile based operation.
  495. */
  496. void fz_disable_icc(fz_context *ctx);
  497. /**
  498. Memory Allocation and Scavenging:
  499. All calls to MuPDF's allocator functions pass through to the
  500. underlying allocators passed in when the initial context is
  501. created, after locks are taken (using the supplied locking
  502. function) to ensure that only one thread at a time calls
  503. through.
  504. If the underlying allocator fails, MuPDF attempts to make room
  505. for the allocation by evicting elements from the store, then
  506. retrying.
  507. Any call to allocate may then result in several calls to the
  508. underlying allocator, and result in elements that are only
  509. referred to by the store being freed.
  510. */
  511. /**
  512. Allocate memory for a structure, clear it, and tag the pointer
  513. for Memento.
  514. Throws exception in the event of failure to allocate.
  515. */
  516. #define fz_malloc_struct(CTX, TYPE) \
  517. ((TYPE*)Memento_label(fz_calloc(CTX, 1, sizeof(TYPE)), #TYPE))
  518. /**
  519. Allocate memory for an array of structures, clear it, and tag
  520. the pointer for Memento.
  521. Throws exception in the event of failure to allocate.
  522. */
  523. #define fz_malloc_struct_array(CTX, N, TYPE) \
  524. ((TYPE*)Memento_label(fz_calloc(CTX, N, sizeof(TYPE)), #TYPE "[]"))
  525. /**
  526. Allocate uninitialized memory for an array of structures, and
  527. tag the pointer for Memento. Does NOT clear the memory!
  528. Throws exception in the event of failure to allocate.
  529. */
  530. #define fz_malloc_array(CTX, COUNT, TYPE) \
  531. ((TYPE*)Memento_label(fz_malloc(CTX, (COUNT) * sizeof(TYPE)), #TYPE "[]"))
  532. #define fz_realloc_array(CTX, OLD, COUNT, TYPE) \
  533. ((TYPE*)Memento_label(fz_realloc(CTX, OLD, (COUNT) * sizeof(TYPE)), #TYPE "[]"))
  534. /**
  535. Allocate uninitialized memory of a given size.
  536. Does NOT clear the memory!
  537. May return NULL for size = 0.
  538. Throws exception in the event of failure to allocate.
  539. */
  540. void *fz_malloc(fz_context *ctx, size_t size);
  541. /**
  542. Allocate array of memory of count entries of size bytes.
  543. Clears the memory to zero.
  544. Throws exception in the event of failure to allocate.
  545. */
  546. void *fz_calloc(fz_context *ctx, size_t count, size_t size);
  547. /**
  548. Reallocates a block of memory to given size. Existing contents
  549. up to min(old_size,new_size) are maintained. The rest of the
  550. block is uninitialised.
  551. fz_realloc(ctx, NULL, size) behaves like fz_malloc(ctx, size).
  552. fz_realloc(ctx, p, 0); behaves like fz_free(ctx, p).
  553. Throws exception in the event of failure to allocate.
  554. */
  555. void *fz_realloc(fz_context *ctx, void *p, size_t size);
  556. /**
  557. Free a previously allocated block of memory.
  558. fz_free(ctx, NULL) does nothing.
  559. Never throws exceptions.
  560. */
  561. void fz_free(fz_context *ctx, void *p);
  562. /**
  563. Flexible array member allocation helpers.
  564. */
  565. #define fz_malloc_flexible(ctx, T, M, count) \
  566. Memento_label(fz_calloc(ctx, 1, offsetof(T, M) + sizeof(*((T*)0)->M) * (count)), #T)
  567. #define fz_realloc_flexible(ctx, p, T, M, count) \
  568. Memento_label(fz_realloc(ctx, p, offsetof(T, M) + sizeof(*((T*)0)->M) * (count)), #T)
  569. #define fz_pool_alloc_flexible(ctx, pool, T, M, count) \
  570. fz_pool_alloc(ctx, pool, offsetof(T, M) + sizeof(*((T*)0)->M) * (count))
  571. /**
  572. fz_malloc equivalent that returns NULL rather than throwing
  573. exceptions.
  574. */
  575. void *fz_malloc_no_throw(fz_context *ctx, size_t size);
  576. /**
  577. fz_calloc equivalent that returns NULL rather than throwing
  578. exceptions.
  579. */
  580. void *fz_calloc_no_throw(fz_context *ctx, size_t count, size_t size);
  581. /**
  582. fz_realloc equivalent that returns NULL rather than throwing
  583. exceptions.
  584. */
  585. void *fz_realloc_no_throw(fz_context *ctx, void *p, size_t size);
  586. /**
  587. fz_malloc equivalent, except that the block is guaranteed aligned.
  588. Block must be freed later using fz_free_aligned.
  589. */
  590. void *fz_malloc_aligned(fz_context *ctx, size_t size, int align);
  591. /**
  592. fz_free equivalent, for blocks allocated via fz_malloc_aligned.
  593. */
  594. void fz_free_aligned(fz_context *ctx, void *p);
  595. /**
  596. Portable strdup implementation, using fz allocators.
  597. */
  598. char *fz_strdup(fz_context *ctx, const char *s);
  599. /**
  600. Fill block with len bytes of pseudo-randomness.
  601. */
  602. void fz_memrnd(fz_context *ctx, uint8_t *block, int len);
  603. /*
  604. Reference counted malloced C strings.
  605. */
  606. typedef struct
  607. {
  608. int refs;
  609. char str[FZ_FLEXIBLE_ARRAY];
  610. } fz_string;
  611. /*
  612. Allocate a new string to hold a copy of str.
  613. Returns with a refcount of 1.
  614. */
  615. fz_string *fz_new_string(fz_context *ctx, const char *str);
  616. /*
  617. Take another reference to a string.
  618. */
  619. fz_string *fz_keep_string(fz_context *ctx, fz_string *str);
  620. /*
  621. Drop a reference to a string, freeing if the refcount
  622. reaches 0.
  623. */
  624. void fz_drop_string(fz_context *ctx, fz_string *str);
  625. #define fz_cstring_from_string(A) ((A) == NULL ? NULL : (A)->str)
  626. /* Implementation details: subject to change. */
  627. /* Implementations exposed for speed, but considered private. */
  628. void fz_var_imp(void *);
  629. fz_jmp_buf *fz_push_try(fz_context *ctx);
  630. int fz_do_try(fz_context *ctx);
  631. int fz_do_always(fz_context *ctx);
  632. int (fz_do_catch)(fz_context *ctx);
  633. #ifndef FZ_JMPBUF_ALIGN
  634. #define FZ_JMPBUF_ALIGN 32
  635. #endif
  636. typedef struct
  637. {
  638. fz_jmp_buf buffer;
  639. int state, code;
  640. char padding[FZ_JMPBUF_ALIGN-sizeof(int)*2];
  641. } fz_error_stack_slot;
  642. typedef struct
  643. {
  644. fz_error_stack_slot *top;
  645. fz_error_stack_slot stack[256];
  646. fz_error_stack_slot padding;
  647. fz_error_stack_slot *stack_base;
  648. int errcode;
  649. int errnum; /* errno for SYSTEM class errors */
  650. void *print_user;
  651. void (*print)(void *user, const char *message);
  652. char message[256];
  653. } fz_error_context;
  654. typedef struct
  655. {
  656. void *print_user;
  657. void (*print)(void *user, const char *message);
  658. int count;
  659. char message[256];
  660. } fz_warn_context;
  661. typedef struct
  662. {
  663. int hscale;
  664. int vscale;
  665. int scale;
  666. int bits;
  667. int text_bits;
  668. float min_line_width;
  669. } fz_aa_context;
  670. typedef enum
  671. {
  672. FZ_ACTIVITY_NEW_DOC = 0,
  673. FZ_ACTIVITY_SHUTDOWN = 1
  674. } fz_activity_reason;
  675. typedef void (fz_activity_fn)(fz_context *ctx, void *opaque, fz_activity_reason reason, void *reason_arg);
  676. typedef struct
  677. {
  678. void *opaque;
  679. fz_activity_fn *activity;
  680. } fz_activity_context;
  681. void fz_register_activity_logger(fz_context *ctx, fz_activity_fn *activity, void *opaque);
  682. struct fz_context
  683. {
  684. void *user;
  685. /* If master points to itself, then we are the master context.
  686. * If master is NULL, then we are the master context, but we have
  687. * been destroyed. We exist just so the count of clones can live
  688. * on. Otherwise master points to the master context from which
  689. * we were cloned. */
  690. fz_context *master;
  691. /* The number of contexts in this family. 1 for this one, plus
  692. * 1 for every context cloned (directly or indirectly) from it. */
  693. int context_count;
  694. /* Only the master version of this is used! */
  695. int next_document_id;
  696. fz_alloc_context alloc;
  697. fz_locks_context locks;
  698. fz_error_context error;
  699. fz_warn_context warn;
  700. fz_activity_context activity;
  701. /* unshared contexts */
  702. fz_aa_context aa;
  703. uint16_t seed48[7];
  704. #if FZ_ENABLE_ICC
  705. int icc_enabled;
  706. #endif
  707. int throw_on_repair;
  708. /* TODO: should these be unshared? */
  709. fz_document_handler_context *handler;
  710. fz_archive_handler_context *archive;
  711. fz_style_context *style;
  712. fz_tuning_context *tuning;
  713. /* shared contexts */
  714. fz_output *stddbg;
  715. fz_font_context *font;
  716. fz_colorspace_context *colorspace;
  717. fz_store *store;
  718. fz_glyph_cache *glyph_cache;
  719. };
  720. fz_context *fz_new_context_imp(const fz_alloc_context *alloc, const fz_locks_context *locks, size_t max_store, const char *version);
  721. /**
  722. Lock one of the user supplied mutexes.
  723. */
  724. static inline void
  725. fz_lock(fz_context *ctx, int lock)
  726. {
  727. fz_lock_debug_lock(ctx, lock);
  728. ctx->locks.lock(ctx->locks.user, lock);
  729. }
  730. /**
  731. Unlock one of the user supplied mutexes.
  732. */
  733. static inline void
  734. fz_unlock(fz_context *ctx, int lock)
  735. {
  736. fz_lock_debug_unlock(ctx, lock);
  737. ctx->locks.unlock(ctx->locks.user, lock);
  738. }
  739. /* Lock-safe reference counting functions */
  740. static inline void *
  741. fz_keep_imp(fz_context *ctx, void *p, int *refs)
  742. {
  743. if (p)
  744. {
  745. (void)Memento_checkIntPointerOrNull(refs);
  746. fz_lock(ctx, FZ_LOCK_ALLOC);
  747. if (*refs > 0)
  748. {
  749. (void)Memento_takeRef(p);
  750. ++*refs;
  751. }
  752. fz_unlock(ctx, FZ_LOCK_ALLOC);
  753. }
  754. return p;
  755. }
  756. static inline void *
  757. fz_keep_imp_locked(fz_context *ctx FZ_UNUSED, void *p, int *refs)
  758. {
  759. if (p)
  760. {
  761. (void)Memento_checkIntPointerOrNull(refs);
  762. if (*refs > 0)
  763. {
  764. (void)Memento_takeRef(p);
  765. ++*refs;
  766. }
  767. }
  768. return p;
  769. }
  770. static inline void *
  771. fz_keep_imp8_locked(fz_context *ctx FZ_UNUSED, void *p, int8_t *refs)
  772. {
  773. if (p)
  774. {
  775. (void)Memento_checkIntPointerOrNull(refs);
  776. if (*refs > 0)
  777. {
  778. (void)Memento_takeRef(p);
  779. ++*refs;
  780. }
  781. }
  782. return p;
  783. }
  784. static inline void *
  785. fz_keep_imp8(fz_context *ctx, void *p, int8_t *refs)
  786. {
  787. if (p)
  788. {
  789. (void)Memento_checkBytePointerOrNull(refs);
  790. fz_lock(ctx, FZ_LOCK_ALLOC);
  791. if (*refs > 0)
  792. {
  793. (void)Memento_takeRef(p);
  794. ++*refs;
  795. }
  796. fz_unlock(ctx, FZ_LOCK_ALLOC);
  797. }
  798. return p;
  799. }
  800. static inline void *
  801. fz_keep_imp16(fz_context *ctx, void *p, int16_t *refs)
  802. {
  803. if (p)
  804. {
  805. (void)Memento_checkShortPointerOrNull(refs);
  806. fz_lock(ctx, FZ_LOCK_ALLOC);
  807. if (*refs > 0)
  808. {
  809. (void)Memento_takeRef(p);
  810. ++*refs;
  811. }
  812. fz_unlock(ctx, FZ_LOCK_ALLOC);
  813. }
  814. return p;
  815. }
  816. static inline int
  817. fz_drop_imp(fz_context *ctx, void *p, int *refs)
  818. {
  819. if (p)
  820. {
  821. int drop;
  822. (void)Memento_checkIntPointerOrNull(refs);
  823. fz_lock(ctx, FZ_LOCK_ALLOC);
  824. if (*refs > 0)
  825. {
  826. (void)Memento_dropIntRef(p);
  827. drop = --*refs == 0;
  828. }
  829. else
  830. drop = 0;
  831. fz_unlock(ctx, FZ_LOCK_ALLOC);
  832. return drop;
  833. }
  834. return 0;
  835. }
  836. static inline int
  837. fz_drop_imp8(fz_context *ctx, void *p, int8_t *refs)
  838. {
  839. if (p)
  840. {
  841. int drop;
  842. (void)Memento_checkBytePointerOrNull(refs);
  843. fz_lock(ctx, FZ_LOCK_ALLOC);
  844. if (*refs > 0)
  845. {
  846. (void)Memento_dropByteRef(p);
  847. drop = --*refs == 0;
  848. }
  849. else
  850. drop = 0;
  851. fz_unlock(ctx, FZ_LOCK_ALLOC);
  852. return drop;
  853. }
  854. return 0;
  855. }
  856. static inline int
  857. fz_drop_imp16(fz_context *ctx, void *p, int16_t *refs)
  858. {
  859. if (p)
  860. {
  861. int drop;
  862. (void)Memento_checkShortPointerOrNull(refs);
  863. fz_lock(ctx, FZ_LOCK_ALLOC);
  864. if (*refs > 0)
  865. {
  866. (void)Memento_dropShortRef(p);
  867. drop = --*refs == 0;
  868. }
  869. else
  870. drop = 0;
  871. fz_unlock(ctx, FZ_LOCK_ALLOC);
  872. return drop;
  873. }
  874. return 0;
  875. }
  876. #endif