2 * Copyright © 2006 Red Hat, Inc.
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.
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.
23 * Author: Kristian Høgsberg <krh@redhat.com>
32 #if CAIRO_HAS_PS_SURFACE
36 #if CAIRO_HAS_PDF_SURFACE
37 #include <cairo-pdf.h>
40 #if CAIRO_HAS_SVG_SURFACE
41 #include <cairo-svg.h>
44 #include "cairo-test.h"
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 writting using the
53 #define MAX_OUTPUT_SIZE 4096
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)
60 static cairo_test_status_t
61 draw (cairo_t
*cr
, int width
, int height
)
63 /* Just draw a rectangle. */
65 cairo_rectangle (cr
, width
/ 10., height
/10.,
66 width
- 2 * width
/ 10.,
67 height
- 2 * height
/10.);
72 return CAIRO_TEST_SUCCESS
;
76 draw_to (cairo_surface_t
*surface
)
80 cr
= cairo_create (surface
);
82 draw (cr
, WIDTH_IN_POINTS
, HEIGHT_IN_POINTS
);
87 typedef struct _write_closure
{
88 char buffer
[MAX_OUTPUT_SIZE
];
90 cairo_test_status_t status
;
94 bad_write (void *closure
,
95 const unsigned char *data
,
98 return CAIRO_STATUS_WRITE_ERROR
;
101 static cairo_status_t
102 test_write (void *closure
,
103 const unsigned char *data
,
106 write_closure_t
*wc
= closure
;
108 if (wc
->index
+ length
>= sizeof wc
->buffer
) {
109 cairo_test_log ("Error: out of bounds in write callback\n");
110 wc
->status
= CAIRO_TEST_FAILURE
;
111 return CAIRO_STATUS_SUCCESS
;
114 memcpy (&wc
->buffer
[wc
->index
], data
, length
);
117 return CAIRO_STATUS_SUCCESS
;
121 typedef cairo_surface_t
*
122 (*file_constructor_t
) (const char *filename
,
123 double width_in_points
,
124 double height_in_points
);
126 typedef cairo_surface_t
*
127 (*stream_constructor_t
) (cairo_write_func_t write_func
,
129 double width_in_points
,
130 double height_in_points
);
132 static cairo_test_status_t
133 test_surface (const char *backend
,
134 const char *filename
,
135 file_constructor_t file_constructor
,
136 stream_constructor_t stream_constructor
)
138 cairo_surface_t
*surface
;
140 char file_contents
[MAX_OUTPUT_SIZE
];
141 cairo_status_t status
;
144 /* test propagation of user errors */
145 surface
= stream_constructor (bad_write
, &wc
,
146 WIDTH_IN_POINTS
, HEIGHT_IN_POINTS
);
148 status
= cairo_surface_status (surface
);
150 cairo_test_log ("%s: Failed to create surface for stream.\n", backend
);
151 return CAIRO_TEST_FAILURE
;
156 cairo_surface_finish (surface
);
157 status
= cairo_surface_status (surface
);
158 cairo_surface_destroy (surface
);
160 if (status
!= CAIRO_STATUS_WRITE_ERROR
) {
161 cairo_test_log ("%s: Error: expected \"write error\", but received \"%s\".\n",
162 backend
, cairo_status_to_string (status
));
163 return CAIRO_TEST_FAILURE
;
166 /* construct the real surface */
167 wc
.status
= CAIRO_TEST_SUCCESS
;
170 surface
= stream_constructor (test_write
, &wc
,
171 WIDTH_IN_POINTS
, HEIGHT_IN_POINTS
);
173 status
= cairo_surface_status (surface
);
175 cairo_test_log ("%s: Failed to create surface for stream.\n", backend
);
176 return CAIRO_TEST_FAILURE
;
181 cairo_surface_destroy (surface
);
183 if (wc
.status
!= CAIRO_TEST_SUCCESS
) {
184 /* Error already reported. */
188 surface
= file_constructor (filename
,
189 WIDTH_IN_POINTS
, HEIGHT_IN_POINTS
);
191 status
= cairo_surface_status (surface
);
193 cairo_test_log ("%s: Failed to create surface for file %s: %s.\n",
194 backend
, filename
, cairo_status_to_string (status
));
195 return CAIRO_TEST_FAILURE
;
200 cairo_surface_destroy (surface
);
202 fp
= fopen (filename
, "r");
204 cairo_test_log ("%s: Failed to open %s for reading: %s.\n",
205 backend
, filename
, strerror (errno
));
206 return CAIRO_TEST_FAILURE
;
209 if (fread (file_contents
, 1, wc
.index
, fp
) != wc
.index
) {
210 cairo_test_log ("%s: Failed to read %s: %s.\n",
211 backend
, filename
, strerror (errno
));
213 return CAIRO_TEST_FAILURE
;
216 if (memcmp (file_contents
, wc
.buffer
, wc
.index
) != 0) {
217 cairo_test_log ("%s: Stream based output differ from file output for %s.\n",
220 return CAIRO_TEST_FAILURE
;
225 return CAIRO_TEST_SUCCESS
;
231 cairo_test_status_t status
= CAIRO_TEST_SUCCESS
;
232 cairo_test_status_t test_status
;
233 const char test_name
[] = "create-for-stream";
235 cairo_test_init (test_name
);
237 #if CAIRO_HAS_PS_SURFACE
238 test_status
= test_surface ("ps", "create-for-stream.ps",
239 cairo_ps_surface_create
,
240 cairo_ps_surface_create_for_stream
);
241 cairo_test_log ("TEST: %s TARGET: %s RESULT: %s\n",
243 test_status
? "FAIL" : "PASS");
244 if (status
== CAIRO_TEST_SUCCESS
)
245 status
= test_status
;
248 #if CAIRO_HAS_PDF_SURFACE
249 test_status
= test_surface ("pdf", "create-for-stream.pdf",
250 cairo_pdf_surface_create
,
251 cairo_pdf_surface_create_for_stream
);
252 cairo_test_log ("TEST: %s TARGET: %s RESULT: %s\n",
254 test_status
? "FAIL" : "PASS");
255 if (status
== CAIRO_TEST_SUCCESS
)
256 status
= test_status
;
259 #if CAIRO_HAS_SVG_SURFACE
260 test_status
= test_surface ("svg", "create-for-stream.svg",
261 cairo_svg_surface_create
,
262 cairo_svg_surface_create_for_stream
);
263 cairo_test_log ("TEST: %s TARGET: %s RESULT: %s\n",
265 test_status
? "FAIL" : "PASS");
266 if (status
== CAIRO_TEST_SUCCESS
)
267 status
= test_status
;