output.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #ifndef MUPDF_FITZ_OUTPUT_H
  2. #define MUPDF_FITZ_OUTPUT_H
  3. #include "mupdf/fitz/system.h"
  4. #include "mupdf/fitz/context.h"
  5. #include "mupdf/fitz/buffer.h"
  6. #include "mupdf/fitz/string-util.h"
  7. /*
  8. Generic output streams - generalise between outputting to a file,
  9. a buffer, etc.
  10. */
  11. typedef struct fz_output_s fz_output;
  12. /*
  13. fz_output_write_fn: A function type for use when implementing
  14. fz_outputs. The supplied function of this type is called
  15. whenever data is written to the output.
  16. state: The state for the output stream.
  17. data: a pointer to a buffer of data to write.
  18. n: The number of bytes of data to write.
  19. */
  20. typedef void (fz_output_write_fn)(fz_context *ctx, void *state, const void *data, size_t n);
  21. /*
  22. fz_output_seek_fn: A function type for use when implementing
  23. fz_outputs. The supplied function of this type is called when
  24. fz_seek_output is requested.
  25. state: The output stream state to seek within.
  26. offset, whence: as defined for fs_seek_output.
  27. */
  28. typedef void (fz_output_seek_fn)(fz_context *ctx, void *state, fz_off_t offset, int whence);
  29. /*
  30. fz_output_tell_fn: A function type for use when implementing
  31. fz_outputs. The supplied function of this type is called when
  32. fz_tell_output is requested.
  33. state: The output stream state to report on.
  34. Returns the offset within the output stream.
  35. */
  36. typedef fz_off_t (fz_output_tell_fn)(fz_context *ctx, void *state);
  37. /*
  38. fz_output_close_fn: A function type for use when implementing
  39. fz_outputs. The supplied function of this type is called
  40. when the output stream is closed, to release the stream specific
  41. state information.
  42. state: The output stream state to release.
  43. */
  44. typedef void (fz_output_close_fn)(fz_context *ctx, void *state);
  45. struct fz_output_s
  46. {
  47. void *state;
  48. fz_output_write_fn *write;
  49. fz_output_seek_fn *seek;
  50. fz_output_tell_fn *tell;
  51. fz_output_close_fn *close;
  52. };
  53. /*
  54. fz_new_output: Create a new output object with the given
  55. internal state and function pointers.
  56. state: Internal state (opaque to everything but implementation).
  57. write: Function to output a given buffer.
  58. close: Cleanup function to destroy state when output closed.
  59. May permissibly be null.
  60. */
  61. fz_output *fz_new_output(fz_context *ctx, void *state, fz_output_write_fn *write, fz_output_close_fn *close);
  62. /*
  63. fz_new_output_with_file: Open an output stream that writes to a
  64. FILE *.
  65. file: The file to write to.
  66. close: non-zero if we should close the file when the fz_output
  67. is closed.
  68. */
  69. fz_output *fz_new_output_with_file_ptr(fz_context *ctx, FILE *file, int close);
  70. /*
  71. fz_new_output_with_path: Open an output stream that writes to a
  72. given path.
  73. filename: The filename to write to (specified in UTF-8).
  74. append: non-zero if we should append to the file, rather than
  75. overwriting it.
  76. */
  77. fz_output *fz_new_output_with_path(fz_context *, const char *filename, int append);
  78. /*
  79. fz_new_output_with_buffer: Open an output stream that appends
  80. to a buffer.
  81. buf: The buffer to append to.
  82. */
  83. fz_output *fz_new_output_with_buffer(fz_context *ctx, fz_buffer *buf);
  84. /*
  85. fz_stdout: The standard out output stream. By default
  86. this stream writes to stdout. This may be overridden
  87. using fz_set_stdout.
  88. */
  89. fz_output *fz_stdout(fz_context *ctx);
  90. /*
  91. fz_stderr: The standard error output stream. By default
  92. this stream writes to stderr. This may be overridden
  93. using fz_set_stderr.
  94. */
  95. fz_output *fz_stderr(fz_context *ctx);
  96. /*
  97. fz_set_stdout: Replace default standard output stream
  98. with a given stream.
  99. out: The new stream to use.
  100. */
  101. void fz_set_stdout(fz_context *ctx, fz_output *out);
  102. /*
  103. fz_set_stderr: Replace default standard error stream
  104. with a given stream.
  105. err: The new stream to use.
  106. */
  107. void fz_set_stderr(fz_context *ctx, fz_output *err);
  108. /*
  109. fz_write_printf: Format and write data to an output stream.
  110. See fz_vsnprintf for formatting details.
  111. */
  112. void fz_write_printf(fz_context *ctx, fz_output *out, const char *fmt, ...);
  113. /*
  114. fz_write_vprintf: va_list version of fz_write_printf.
  115. */
  116. void fz_write_vprintf(fz_context *ctx, fz_output *out, const char *fmt, va_list ap);
  117. /*
  118. fz_seek_output: Seek to the specified file position.
  119. See fseek for arguments.
  120. Throw an error on unseekable outputs.
  121. */
  122. void fz_seek_output(fz_context *ctx, fz_output *out, fz_off_t off, int whence);
  123. /*
  124. fz_tell_output: Return the current file position.
  125. Throw an error on untellable outputs.
  126. */
  127. fz_off_t fz_tell_output(fz_context *ctx, fz_output *out);
  128. /*
  129. fz_drop_output: Close and free an output stream.
  130. */
  131. void fz_drop_output(fz_context *, fz_output *);
  132. /*
  133. fz_write_data: Write data to output.
  134. data: Pointer to data to write.
  135. size: Size of data to write in bytes.
  136. */
  137. static inline void fz_write_data(fz_context *ctx, fz_output *out, const void *data, size_t size)
  138. {
  139. if (out)
  140. out->write(ctx, out->state, data, size);
  141. }
  142. /*
  143. fz_write_string: Write a string. Does not write zero terminator.
  144. */
  145. static inline void fz_write_string(fz_context *ctx, fz_output *out, const char *s)
  146. {
  147. if (out)
  148. out->write(ctx, out->state, s, strlen(s));
  149. }
  150. /*
  151. fz_write_int32_be: Write a big-endian 32-bit binary integer.
  152. */
  153. static inline void fz_write_int32_be(fz_context *ctx, fz_output *out, int x)
  154. {
  155. char data[4];
  156. data[0] = x>>24;
  157. data[1] = x>>16;
  158. data[2] = x>>8;
  159. data[3] = x;
  160. if (out)
  161. out->write(ctx, out->state, data, 4);
  162. }
  163. /*
  164. fz_write_int32_le: Write a little-endian 32-bit binary integer.
  165. */
  166. static inline void fz_write_int32_le(fz_context *ctx, fz_output *out, int x)
  167. {
  168. char data[4];
  169. data[0] = x;
  170. data[1] = x>>8;
  171. data[2] = x>>16;
  172. data[3] = x>>24;
  173. if (out)
  174. out->write(ctx, out->state, data, 4);
  175. }
  176. /*
  177. fz_write_int16_be: Write a big-endian 16-bit binary integer.
  178. */
  179. static inline void fz_write_int16_be(fz_context *ctx, fz_output *out, int x)
  180. {
  181. char data[2];
  182. data[0] = x>>8;
  183. data[1] = x;
  184. if (out)
  185. out->write(ctx, out->state, data, 2);
  186. }
  187. /*
  188. fz_write_int16_le: Write a little-endian 16-bit binary integer.
  189. */
  190. static inline void fz_write_int16_le(fz_context *ctx, fz_output *out, int x)
  191. {
  192. char data[2];
  193. data[0] = x;
  194. data[1] = x>>8;
  195. if (out)
  196. out->write(ctx, out->state, data, 2);
  197. }
  198. /*
  199. fz_write_byte: Write a single byte.
  200. */
  201. static inline void fz_write_byte(fz_context *ctx, fz_output *out, unsigned char x)
  202. {
  203. if (out)
  204. out->write(ctx, out->state, &x, 1);
  205. }
  206. /*
  207. fz_write_rune: Write a UTF-8 encoded unicode character.
  208. */
  209. static inline void fz_write_rune(fz_context *ctx, fz_output *out, int rune)
  210. {
  211. char data[10];
  212. if (out)
  213. out->write(ctx, out->state, data, fz_runetochar(data, rune));
  214. }
  215. /*
  216. fz_format_string: Our customised 'printf'-like string formatter.
  217. Takes %c, %d, %o, %s, %u, %x, as usual.
  218. Modifiers are not supported except for zero-padding ints (e.g. %02d, %03o, %04x, etc).
  219. %g output in "as short as possible hopefully lossless non-exponent" form,
  220. see fz_ftoa for specifics.
  221. %f and %e output as usual.
  222. %C outputs a utf8 encoded int.
  223. %M outputs a fz_matrix*. %R outputs a fz_rect*. %P outputs a fz_point*.
  224. %q and %( output escaped strings in C/PDF syntax.
  225. %ll{d,u,x} indicates that the values are 64bit.
  226. %z{d,u,x} indicates that the value is a size_t.
  227. %Z{d,u,x} indicates that the value is a fz_off_t.
  228. user: An opaque pointer that is passed to the emit function.
  229. emit: A function pointer called to emit output bytes as the string is being formatted.
  230. */
  231. void
  232. fz_format_string(fz_context *ctx, void *user, void (*emit)(fz_context *ctx, void *user, int c), const char *fmt, va_list args);
  233. /*
  234. fz_vsnprintf: A vsnprintf work-alike, using our custom formatter.
  235. */
  236. size_t fz_vsnprintf(char *buffer, size_t space, const char *fmt, va_list args);
  237. /*
  238. fz_snprintf: The non va_list equivalent of fz_vsnprintf.
  239. */
  240. size_t fz_snprintf(char *buffer, size_t space, const char *fmt, ...);
  241. /*
  242. fz_tempfilename: Get a temporary filename based upon 'base'.
  243. 'hint' is the path of a file (normally the existing document file)
  244. supplied to give the function an idea of what directory to use. This
  245. may or may not be used depending on the implementation's whim.
  246. The returned path must be freed.
  247. */
  248. char *fz_tempfilename(fz_context *ctx, const char *base, const char *hint);
  249. /*
  250. fz_save_buffer: Save contents of a buffer to file.
  251. */
  252. void fz_save_buffer(fz_context *ctx, fz_buffer *buf, const char *filename);
  253. #endif