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
26 #include "cairo-test.h"
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <errno.h>
31
32 #if CAIRO_HAS_PS_SURFACE
33 #include <cairo-ps.h>
34 #endif
35
36 #if CAIRO_HAS_PDF_SURFACE
37 #include <cairo-pdf.h>
38 #endif
39
40 #if CAIRO_HAS_SVG_SURFACE
41 #include <cairo-svg.h>
42 #endif
43
44 #include "cairo-test.h"
45
46 /* The main test suite doesn't test the *_create_for_stream
47 * constructors for the PDF, PS and SVG surface, so we do that here.
48 * We draw to an in-memory buffer using the stream constructor and
49 * compare the output to the contents of a file written using the
50 * file constructor.
51 */
52
53 #define MAX_OUTPUT_SIZE 4096
54
55 #define WIDTH_IN_INCHES 3
56 #define HEIGHT_IN_INCHES 3
57 #define WIDTH_IN_POINTS (WIDTH_IN_INCHES * 72.0)
58 #define HEIGHT_IN_POINTS (HEIGHT_IN_INCHES * 72.0)
59
60 #define BASENAME "create-for-stream.out"
61
62 static cairo_test_status_t
draw(cairo_t * cr,int width,int height)63 draw (cairo_t *cr, int width, int height)
64 {
65 /* Just draw a rectangle. */
66
67 cairo_rectangle (cr, width / 10., height /10.,
68 width - 2 * width / 10.,
69 height - 2 * height /10.);
70 cairo_fill (cr);
71
72 cairo_show_page (cr);
73
74 return CAIRO_TEST_SUCCESS;
75 }
76
77 static void
draw_to(cairo_surface_t * surface)78 draw_to (cairo_surface_t *surface)
79 {
80 cairo_t *cr;
81
82 cr = cairo_create (surface);
83
84 draw (cr, WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
85
86 cairo_destroy (cr);
87 }
88
89 typedef struct _write_closure {
90 const cairo_test_context_t *ctx;
91 char buffer[MAX_OUTPUT_SIZE];
92 size_t index;
93 cairo_test_status_t status;
94 } write_closure_t;
95
96 static cairo_status_t
bad_write(void * closure,const unsigned char * data,unsigned int length)97 bad_write (void *closure,
98 const unsigned char *data,
99 unsigned int length)
100 {
101 return CAIRO_STATUS_WRITE_ERROR;
102 }
103
104 static cairo_status_t
test_write(void * closure,const unsigned char * data,unsigned int length)105 test_write (void *closure,
106 const unsigned char *data,
107 unsigned int length)
108 {
109 write_closure_t *wc = closure;
110
111 if (wc->index + length >= sizeof wc->buffer) {
112 cairo_test_log (wc->ctx, "Error: out of bounds in write callback\n");
113 wc->status = CAIRO_TEST_FAILURE;
114 return CAIRO_STATUS_SUCCESS;
115 }
116
117 memcpy (&wc->buffer[wc->index], data, length);
118 wc->index += length;
119
120 return CAIRO_STATUS_SUCCESS;
121 }
122
123
124 typedef cairo_surface_t *
125 (*file_constructor_t) (const char *filename,
126 double width_in_points,
127 double height_in_points);
128
129 typedef cairo_surface_t *
130 (*stream_constructor_t) (cairo_write_func_t write_func,
131 void *closure,
132 double width_in_points,
133 double height_in_points);
134
135 static cairo_test_status_t
test_surface(const cairo_test_context_t * ctx,const char * backend,const char * filename,file_constructor_t file_constructor,stream_constructor_t stream_constructor)136 test_surface (const cairo_test_context_t *ctx,
137 const char *backend,
138 const char *filename,
139 file_constructor_t file_constructor,
140 stream_constructor_t stream_constructor)
141 {
142 cairo_surface_t *surface;
143 write_closure_t wc;
144 char file_contents[MAX_OUTPUT_SIZE];
145 cairo_status_t status;
146 FILE *fp;
147
148 /* test propagation of user errors */
149 surface = stream_constructor (bad_write, &wc,
150 WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
151
152 status = cairo_surface_status (surface);
153 if (status) {
154 cairo_test_log (ctx,
155 "%s: Failed to create surface for stream.\n",
156 backend);
157 return CAIRO_TEST_FAILURE;
158 }
159
160 draw_to (surface);
161
162 cairo_surface_finish (surface);
163 status = cairo_surface_status (surface);
164 cairo_surface_destroy (surface);
165
166 if (status != CAIRO_STATUS_WRITE_ERROR) {
167 cairo_test_log (ctx,
168 "%s: Error: expected \"write error\", but received \"%s\".\n",
169 backend, cairo_status_to_string (status));
170 return CAIRO_TEST_FAILURE;
171 }
172
173 /* construct the real surface */
174 wc.ctx = ctx;
175 wc.status = CAIRO_TEST_SUCCESS;
176 wc.index = 0;
177
178 surface = stream_constructor (test_write, &wc,
179 WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
180
181 status = cairo_surface_status (surface);
182 if (status) {
183 cairo_test_log (ctx,
184 "%s: Failed to create surface for stream.\n", backend);
185 return CAIRO_TEST_FAILURE;
186 }
187
188 draw_to (surface);
189
190 cairo_surface_destroy (surface);
191
192 if (wc.status != CAIRO_TEST_SUCCESS) {
193 /* Error already reported. */
194 return wc.status;
195 }
196
197 surface = file_constructor (filename,
198 WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
199
200 status = cairo_surface_status (surface);
201 if (status) {
202 cairo_test_log (ctx, "%s: Failed to create surface for file %s: %s.\n",
203 backend, filename, cairo_status_to_string (status));
204 return CAIRO_TEST_FAILURE;
205 }
206
207 draw_to (surface);
208
209 cairo_surface_destroy (surface);
210
211 fp = fopen (filename, "r");
212 if (fp == NULL) {
213 cairo_test_log (ctx, "%s: Failed to open %s for reading: %s.\n",
214 backend, filename, strerror (errno));
215 return CAIRO_TEST_FAILURE;
216 }
217
218 if (fread (file_contents, 1, wc.index, fp) != wc.index) {
219 cairo_test_log (ctx, "%s: Failed to read %s: %s.\n",
220 backend, filename, strerror (errno));
221 fclose (fp);
222 return CAIRO_TEST_FAILURE;
223 }
224
225 if (memcmp (file_contents, wc.buffer, wc.index) != 0) {
226 cairo_test_log (ctx, "%s: Stream based output differ from file output for %s.\n",
227 backend, filename);
228 fclose (fp);
229 return CAIRO_TEST_FAILURE;
230 }
231
232 fclose (fp);
233
234 return CAIRO_TEST_SUCCESS;
235 }
236
237 static cairo_test_status_t
preamble(cairo_test_context_t * ctx)238 preamble (cairo_test_context_t *ctx)
239 {
240 cairo_test_status_t status = CAIRO_TEST_UNTESTED;
241 cairo_test_status_t test_status;
242 char *filename;
243 const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
244
245 #if CAIRO_HAS_PS_SURFACE
246 if (cairo_test_is_target_enabled (ctx, "ps2") ||
247 cairo_test_is_target_enabled (ctx, "ps3"))
248 {
249 if (status == CAIRO_TEST_UNTESTED)
250 status = CAIRO_TEST_SUCCESS;
251
252 xasprintf (&filename, "%s/%s", path, BASENAME ".ps");
253 test_status = test_surface (ctx, "ps", filename,
254 cairo_ps_surface_create,
255 cairo_ps_surface_create_for_stream);
256 cairo_test_log (ctx, "TEST: %s TARGET: %s RESULT: %s\n",
257 ctx->test->name, "ps",
258 test_status ? "FAIL" : "PASS");
259 if (status == CAIRO_TEST_SUCCESS)
260 status = test_status;
261 free (filename);
262 }
263 #endif
264
265 #if CAIRO_HAS_PDF_SURFACE
266 if (cairo_test_is_target_enabled (ctx, "pdf")) {
267 if (status == CAIRO_TEST_UNTESTED)
268 status = CAIRO_TEST_SUCCESS;
269
270 xasprintf (&filename, "%s/%s", path, BASENAME ".pdf");
271 test_status = test_surface (ctx, "pdf", filename,
272 cairo_pdf_surface_create,
273 cairo_pdf_surface_create_for_stream);
274 cairo_test_log (ctx, "TEST: %s TARGET: %s RESULT: %s\n",
275 ctx->test->name, "pdf",
276 test_status ? "FAIL" : "PASS");
277 if (status == CAIRO_TEST_SUCCESS)
278 status = test_status;
279 free (filename);
280 }
281 #endif
282
283 #if CAIRO_HAS_SVG_SURFACE
284 if (cairo_test_is_target_enabled (ctx, "svg11") ||
285 cairo_test_is_target_enabled (ctx, "svg12"))
286 {
287 if (status == CAIRO_TEST_UNTESTED)
288 status = CAIRO_TEST_SUCCESS;
289
290 xasprintf (&filename, "%s/%s", path, BASENAME ".svg");
291 test_status = test_surface (ctx, "svg", filename,
292 cairo_svg_surface_create,
293 cairo_svg_surface_create_for_stream);
294 cairo_test_log (ctx, "TEST: %s TARGET: %s RESULT: %s\n",
295 ctx->test->name, "svg",
296 test_status ? "FAIL" : "PASS");
297 if (status == CAIRO_TEST_SUCCESS)
298 status = test_status;
299 free (filename);
300 }
301 #endif
302
303 return status;
304 }
305
306 CAIRO_TEST (create_for_stream,
307 "Checks creating vector surfaces with user defined I/O\n",
308 "stream", /* keywords */
309 "target=vector", /* requirements */
310 0, 0,
311 preamble, NULL)
312