2 * Copyright © 2005 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: Carl D. Worth <cworth@cworth.org>
26 #include "cairo-test.h"
30 scale_by_two (double *x, double *y)
36 typedef void (*munge_func_t) (double *x, double *y);
39 munge_and_set_path (cairo_t *cr,
45 double x1, y1, x2, y2, x3, y3;
48 cairo_append_path (cr, path);
52 for (i=0; i < path->num_data; i += path->data[i].header.length) {
54 switch (p->header.type) {
55 case CAIRO_PATH_MOVE_TO:
56 x1 = p[1].point.x; y1 = p[1].point.y;
58 cairo_move_to (cr, x1, y1);
60 case CAIRO_PATH_LINE_TO:
61 x1 = p[1].point.x; y1 = p[1].point.y;
63 cairo_line_to (cr, x1, y1);
65 case CAIRO_PATH_CURVE_TO:
66 x1 = p[1].point.x; y1 = p[1].point.y;
67 x2 = p[2].point.x; y2 = p[2].point.y;
68 x3 = p[3].point.x; y3 = p[3].point.y;
77 case CAIRO_PATH_CLOSE_PATH:
78 cairo_close_path (cr);
85 make_path (cairo_t *cr)
87 cairo_rectangle (cr, 0, 0, 5, 5);
88 cairo_move_to (cr, 15, 2.5);
89 cairo_arc (cr, 12.5, 2.5, 2.5, 0, 2 * M_PI);
92 static cairo_test_status_t
93 draw (cairo_t *cr, int width, int height)
95 const cairo_test_context_t *ctx = cairo_test_get_context (cr);
99 /* Ensure that calling cairo_copy_path on an in-error cairo_t will
100 * propagate the error. */
101 cr_error = cairo_create (NULL);
102 path = cairo_copy_path (cr_error);
103 if (path->status != CAIRO_STATUS_NULL_POINTER) {
105 "Error: cairo_copy_path returned status of %s rather than propagating %s\n",
106 cairo_status_to_string (path->status),
107 cairo_status_to_string (CAIRO_STATUS_NULL_POINTER));
108 cairo_path_destroy (path);
109 cairo_destroy (cr_error);
110 return CAIRO_TEST_FAILURE;
112 cairo_path_destroy (path);
114 path = cairo_copy_path_flat (cr_error);
115 if (path->status != CAIRO_STATUS_NULL_POINTER) {
117 "Error: cairo_copy_path_flat returned status of %s rather than propagating %s\n",
118 cairo_status_to_string (path->status),
119 cairo_status_to_string (CAIRO_STATUS_NULL_POINTER));
120 cairo_path_destroy (path);
121 cairo_destroy (cr_error);
122 return CAIRO_TEST_FAILURE;
124 cairo_path_destroy (path);
126 cairo_destroy (cr_error);
128 /* first check that we can copy an empty path */
130 path = cairo_copy_path (cr);
131 if (path->status != CAIRO_STATUS_SUCCESS) {
132 cairo_status_t status = path->status;
134 "Error: cairo_copy_path returned status of %s\n",
135 cairo_status_to_string (status));
136 cairo_path_destroy (path);
137 return cairo_test_status_from_status (ctx, status);
139 if (path->num_data != 0) {
141 "Error: cairo_copy_path did not copy an empty path, returned path contains %d elements\n",
143 cairo_path_destroy (path);
144 return CAIRO_TEST_FAILURE;
146 cairo_append_path (cr, path);
147 cairo_path_destroy (path);
148 if (cairo_status (cr) != CAIRO_STATUS_SUCCESS) {
150 "Error: cairo_append_path failed with a copy of an empty path, returned status of %s\n",
151 cairo_status_to_string (cairo_status (cr)));
152 return cairo_test_status_from_status (ctx, cairo_status (cr));
155 /* We draw in the default black, so paint white first. */
157 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
161 /* copy path, munge, and fill */
162 cairo_translate (cr, 5, 5);
164 path = cairo_copy_path (cr);
167 munge_and_set_path (cr, path, scale_by_two);
168 cairo_path_destroy (path);
171 /* copy flattened path, munge, and fill */
172 cairo_translate (cr, 0, 15);
174 path = cairo_copy_path_flat (cr);
177 munge_and_set_path (cr, path, scale_by_two);
178 cairo_path_destroy (path);
181 /* append two copies of path, and fill */
182 cairo_translate (cr, 0, 15);
183 cairo_scale (cr, 2.0, 2.0);
185 path = cairo_copy_path (cr);
188 cairo_append_path (cr, path);
189 cairo_translate (cr, 2.5, 2.5);
190 cairo_append_path (cr, path);
192 cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
195 cairo_path_destroy (path);
197 return CAIRO_TEST_SUCCESS;
200 static cairo_test_status_t
201 preamble (cairo_test_context_t *ctx)
204 cairo_path_data_t data;
206 cairo_surface_t *surface;
207 cairo_status_t status;
209 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
210 status = cairo_surface_status (surface);
212 cairo_surface_destroy (surface);
213 return cairo_test_status_from_status (ctx, status);
216 /* Test a few error cases for cairo_append_path_data */
217 #define CAIRO_CREATE() do {\
218 cr = cairo_create (surface); \
219 status = cairo_status (cr); \
221 cairo_destroy (cr); \
222 cairo_surface_destroy (surface); \
223 return cairo_test_status_from_status (ctx, status); \
227 cairo_append_path (cr, NULL);
228 status = cairo_status (cr);
230 if (status != CAIRO_STATUS_NULL_POINTER) {
231 cairo_surface_destroy (surface);
232 return cairo_test_status_from_status (ctx, status);
237 cairo_append_path (cr, &path);
238 status = cairo_status (cr);
240 if (status != CAIRO_STATUS_INVALID_STATUS) {
241 cairo_surface_destroy (surface);
242 return cairo_test_status_from_status (ctx, status);
246 path.status = CAIRO_STATUS_NO_MEMORY;
247 cairo_append_path (cr, &path);
248 status = cairo_status (cr);
250 if (status != CAIRO_STATUS_NO_MEMORY) {
251 cairo_surface_destroy (surface);
252 return cairo_test_status_from_status (ctx, status);
258 path.status = CAIRO_STATUS_SUCCESS;
259 cairo_append_path (cr, &path);
260 status = cairo_status (cr);
262 if (status != CAIRO_STATUS_SUCCESS) {
263 cairo_surface_destroy (surface);
264 return cairo_test_status_from_status (ctx, status);
270 path.status = CAIRO_STATUS_SUCCESS;
271 cairo_append_path (cr, &path);
272 status = cairo_status (cr);
274 if (status != CAIRO_STATUS_NULL_POINTER) {
275 cairo_surface_destroy (surface);
276 return cairo_test_status_from_status (ctx, status);
280 /* Intentionally insert bogus header.length value (otherwise would be 2) */
281 data.header.type = CAIRO_PATH_MOVE_TO;
282 data.header.length = 1;
285 cairo_append_path (cr, &path);
286 status = cairo_status (cr);
288 if (status != CAIRO_STATUS_INVALID_PATH_DATA) {
289 cairo_surface_destroy (surface);
290 return cairo_test_status_from_status (ctx, status);
293 /* And test the degnerate case */
296 cairo_append_path (cr, &path);
297 status = cairo_status (cr);
299 if (status != CAIRO_STATUS_SUCCESS) {
300 cairo_surface_destroy (surface);
301 return cairo_test_status_from_status (ctx, status);
304 cairo_surface_destroy (surface);
306 return CAIRO_TEST_SUCCESS;
309 CAIRO_TEST (copy_path,
310 "Tests calls to path_data functions: cairo_copy_path, cairo_copy_path_flat, and cairo_append_path",
311 "path", /* keywords */
312 NULL, /* requirements */