create-for-stream.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Copyright © 2006 Red Hat, Inc.
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software
  5. * and its documentation for any purpose is hereby granted without
  6. * fee, provided that the above copyright notice appear in all copies
  7. * and that both that copyright notice and this permission notice
  8. * appear in supporting documentation, and that the name of
  9. * Red Hat, Inc. not be used in advertising or publicity pertaining to
  10. * distribution of the software without specific, written prior
  11. * permission. Red Hat, Inc. makes no representations about the
  12. * suitability of this software for any purpose. It is provided "as
  13. * is" without express or implied warranty.
  14. *
  15. * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
  18. * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  19. * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  21. * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22. *
  23. * Author: Kristian Høgsberg <krh@redhat.com>
  24. */
  25. #include "cairo-test.h"
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <errno.h>
  29. #if CAIRO_HAS_PS_SURFACE
  30. #include <cairo-ps.h>
  31. #endif
  32. #if CAIRO_HAS_PDF_SURFACE
  33. #include <cairo-pdf.h>
  34. #endif
  35. #if CAIRO_HAS_SVG_SURFACE
  36. #include <cairo-svg.h>
  37. #endif
  38. #include "cairo-test.h"
  39. /* The main test suite doesn't test the *_create_for_stream
  40. * constructors for the PDF, PS and SVG surface, so we do that here.
  41. * We draw to an in-memory buffer using the stream constructor and
  42. * compare the output to the contents of a file written using the
  43. * file constructor.
  44. */
  45. #define MAX_OUTPUT_SIZE 4096
  46. #define WIDTH_IN_INCHES 3
  47. #define HEIGHT_IN_INCHES 3
  48. #define WIDTH_IN_POINTS (WIDTH_IN_INCHES * 72.0)
  49. #define HEIGHT_IN_POINTS (HEIGHT_IN_INCHES * 72.0)
  50. #define BASENAME "create-for-stream.out"
  51. static cairo_test_status_t
  52. draw (cairo_t *cr, int width, int height)
  53. {
  54. /* Just draw a rectangle. */
  55. cairo_rectangle (cr, width / 10., height /10.,
  56. width - 2 * width / 10.,
  57. height - 2 * height /10.);
  58. cairo_fill (cr);
  59. cairo_show_page (cr);
  60. return CAIRO_TEST_SUCCESS;
  61. }
  62. static void
  63. draw_to (cairo_surface_t *surface)
  64. {
  65. cairo_t *cr;
  66. cr = cairo_create (surface);
  67. draw (cr, WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
  68. cairo_destroy (cr);
  69. }
  70. typedef struct _write_closure {
  71. const cairo_test_context_t *ctx;
  72. char buffer[MAX_OUTPUT_SIZE];
  73. size_t index;
  74. cairo_test_status_t status;
  75. } write_closure_t;
  76. static cairo_status_t
  77. bad_write (void *closure,
  78. const unsigned char *data,
  79. unsigned int length)
  80. {
  81. return CAIRO_STATUS_WRITE_ERROR;
  82. }
  83. static cairo_status_t
  84. test_write (void *closure,
  85. const unsigned char *data,
  86. unsigned int length)
  87. {
  88. write_closure_t *wc = closure;
  89. if (wc->index + length >= sizeof wc->buffer) {
  90. cairo_test_log (wc->ctx, "Error: out of bounds in write callback\n");
  91. wc->status = CAIRO_TEST_FAILURE;
  92. return CAIRO_STATUS_SUCCESS;
  93. }
  94. memcpy (&wc->buffer[wc->index], data, length);
  95. wc->index += length;
  96. return CAIRO_STATUS_SUCCESS;
  97. }
  98. typedef cairo_surface_t *
  99. (*file_constructor_t) (const char *filename,
  100. double width_in_points,
  101. double height_in_points);
  102. typedef cairo_surface_t *
  103. (*stream_constructor_t) (cairo_write_func_t write_func,
  104. void *closure,
  105. double width_in_points,
  106. double height_in_points);
  107. static cairo_test_status_t
  108. test_surface (const cairo_test_context_t *ctx,
  109. const char *backend,
  110. const char *filename,
  111. file_constructor_t file_constructor,
  112. stream_constructor_t stream_constructor)
  113. {
  114. cairo_surface_t *surface;
  115. write_closure_t wc;
  116. char file_contents[MAX_OUTPUT_SIZE];
  117. cairo_status_t status;
  118. FILE *fp;
  119. /* test propagation of user errors */
  120. surface = stream_constructor (bad_write, &wc,
  121. WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
  122. status = cairo_surface_status (surface);
  123. if (status) {
  124. cairo_test_log (ctx,
  125. "%s: Failed to create surface for stream.\n",
  126. backend);
  127. return CAIRO_TEST_FAILURE;
  128. }
  129. draw_to (surface);
  130. cairo_surface_finish (surface);
  131. status = cairo_surface_status (surface);
  132. cairo_surface_destroy (surface);
  133. if (status != CAIRO_STATUS_WRITE_ERROR) {
  134. cairo_test_log (ctx,
  135. "%s: Error: expected \"write error\", but received \"%s\".\n",
  136. backend, cairo_status_to_string (status));
  137. return CAIRO_TEST_FAILURE;
  138. }
  139. /* construct the real surface */
  140. wc.ctx = ctx;
  141. wc.status = CAIRO_TEST_SUCCESS;
  142. wc.index = 0;
  143. surface = stream_constructor (test_write, &wc,
  144. WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
  145. status = cairo_surface_status (surface);
  146. if (status) {
  147. cairo_test_log (ctx,
  148. "%s: Failed to create surface for stream.\n", backend);
  149. return CAIRO_TEST_FAILURE;
  150. }
  151. draw_to (surface);
  152. cairo_surface_destroy (surface);
  153. if (wc.status != CAIRO_TEST_SUCCESS) {
  154. /* Error already reported. */
  155. return wc.status;
  156. }
  157. surface = file_constructor (filename,
  158. WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
  159. status = cairo_surface_status (surface);
  160. if (status) {
  161. cairo_test_log (ctx, "%s: Failed to create surface for file %s: %s.\n",
  162. backend, filename, cairo_status_to_string (status));
  163. return CAIRO_TEST_FAILURE;
  164. }
  165. draw_to (surface);
  166. cairo_surface_destroy (surface);
  167. fp = fopen (filename, "r");
  168. if (fp == NULL) {
  169. cairo_test_log (ctx, "%s: Failed to open %s for reading: %s.\n",
  170. backend, filename, strerror (errno));
  171. return CAIRO_TEST_FAILURE;
  172. }
  173. if (fread (file_contents, 1, wc.index, fp) != wc.index) {
  174. cairo_test_log (ctx, "%s: Failed to read %s: %s.\n",
  175. backend, filename, strerror (errno));
  176. fclose (fp);
  177. return CAIRO_TEST_FAILURE;
  178. }
  179. if (memcmp (file_contents, wc.buffer, wc.index) != 0) {
  180. cairo_test_log (ctx, "%s: Stream based output differ from file output for %s.\n",
  181. backend, filename);
  182. fclose (fp);
  183. return CAIRO_TEST_FAILURE;
  184. }
  185. fclose (fp);
  186. return CAIRO_TEST_SUCCESS;
  187. }
  188. static cairo_test_status_t
  189. preamble (cairo_test_context_t *ctx)
  190. {
  191. cairo_test_status_t status = CAIRO_TEST_UNTESTED;
  192. cairo_test_status_t test_status;
  193. char *filename;
  194. const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
  195. #if CAIRO_HAS_PS_SURFACE
  196. if (cairo_test_is_target_enabled (ctx, "ps2") ||
  197. cairo_test_is_target_enabled (ctx, "ps3"))
  198. {
  199. if (status == CAIRO_TEST_UNTESTED)
  200. status = CAIRO_TEST_SUCCESS;
  201. xasprintf (&filename, "%s/%s", path, BASENAME ".ps");
  202. test_status = test_surface (ctx, "ps", filename,
  203. cairo_ps_surface_create,
  204. cairo_ps_surface_create_for_stream);
  205. cairo_test_log (ctx, "TEST: %s TARGET: %s RESULT: %s\n",
  206. ctx->test->name, "ps",
  207. test_status ? "FAIL" : "PASS");
  208. if (status == CAIRO_TEST_SUCCESS)
  209. status = test_status;
  210. free (filename);
  211. }
  212. #endif
  213. #if CAIRO_HAS_PDF_SURFACE
  214. if (cairo_test_is_target_enabled (ctx, "pdf")) {
  215. if (status == CAIRO_TEST_UNTESTED)
  216. status = CAIRO_TEST_SUCCESS;
  217. xasprintf (&filename, "%s/%s", path, BASENAME ".pdf");
  218. test_status = test_surface (ctx, "pdf", filename,
  219. cairo_pdf_surface_create,
  220. cairo_pdf_surface_create_for_stream);
  221. cairo_test_log (ctx, "TEST: %s TARGET: %s RESULT: %s\n",
  222. ctx->test->name, "pdf",
  223. test_status ? "FAIL" : "PASS");
  224. if (status == CAIRO_TEST_SUCCESS)
  225. status = test_status;
  226. free (filename);
  227. }
  228. #endif
  229. #if CAIRO_HAS_SVG_SURFACE
  230. if (cairo_test_is_target_enabled (ctx, "svg11") ||
  231. cairo_test_is_target_enabled (ctx, "svg12"))
  232. {
  233. if (status == CAIRO_TEST_UNTESTED)
  234. status = CAIRO_TEST_SUCCESS;
  235. xasprintf (&filename, "%s/%s", path, BASENAME ".svg");
  236. test_status = test_surface (ctx, "svg", filename,
  237. cairo_svg_surface_create,
  238. cairo_svg_surface_create_for_stream);
  239. cairo_test_log (ctx, "TEST: %s TARGET: %s RESULT: %s\n",
  240. ctx->test->name, "svg",
  241. test_status ? "FAIL" : "PASS");
  242. if (status == CAIRO_TEST_SUCCESS)
  243. status = test_status;
  244. free (filename);
  245. }
  246. #endif
  247. return status;
  248. }
  249. CAIRO_TEST (create_for_stream,
  250. "Checks creating vector surfaces with user defined I/O\n",
  251. "stream", /* keywords */
  252. "target=vector", /* requirements */
  253. 0, 0,
  254. preamble, NULL)