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>
27 #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_SUCCESS
||
104 path
->status
!= cairo_status (cr_error
)) {
106 "Error: cairo_copy_path returned status of %s rather than propagating %s\n",
107 cairo_status_to_string (path
->status
),
108 cairo_status_to_string (cairo_status (cr_error
)));
109 cairo_path_destroy (path
);
110 return CAIRO_TEST_FAILURE
;
112 cairo_path_destroy (path
);
114 path
= cairo_copy_path_flat (cr_error
);
115 if (path
->status
== CAIRO_STATUS_SUCCESS
||
116 path
->status
!= cairo_status (cr_error
)) {
118 "Error: cairo_copy_path_flat returned status of %s rather than propagating %s\n",
119 cairo_status_to_string (path
->status
),
120 cairo_status_to_string (cairo_status (cr_error
)));
121 cairo_path_destroy (path
);
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
) {
133 "Error: cairo_copy_path returned status of %s\n",
134 cairo_status_to_string (path
->status
));
135 cairo_path_destroy (path
);
136 return CAIRO_TEST_FAILURE
;
138 if (path
->num_data
!= 0) {
140 "Error: cairo_copy_path did not copy an empty path, returned path contains %d elements\n",
142 cairo_path_destroy (path
);
143 return CAIRO_TEST_FAILURE
;
145 cairo_append_path (cr
, path
);
146 cairo_path_destroy (path
);
147 if (cairo_status (cr
) != CAIRO_STATUS_SUCCESS
) {
149 "Error: cairo_append_path failed with a copy of an empty path, returned status of %s\n",
150 cairo_status_to_string (cairo_status (cr
)));
151 return CAIRO_TEST_FAILURE
;
154 /* We draw in the default black, so paint white first. */
156 cairo_set_source_rgb (cr
, 1.0, 1.0, 1.0); /* white */
160 /* copy path, munge, and fill */
161 cairo_translate (cr
, 5, 5);
163 path
= cairo_copy_path (cr
);
166 munge_and_set_path (cr
, path
, scale_by_two
);
167 cairo_path_destroy (path
);
170 /* copy flattened path, munge, and fill */
171 cairo_translate (cr
, 0, 15);
173 path
= cairo_copy_path_flat (cr
);
176 munge_and_set_path (cr
, path
, scale_by_two
);
177 cairo_path_destroy (path
);
180 /* append two copies of path, and fill */
181 cairo_translate (cr
, 0, 15);
182 cairo_scale (cr
, 2.0, 2.0);
184 path
= cairo_copy_path (cr
);
187 cairo_append_path (cr
, path
);
188 cairo_translate (cr
, 2.5, 2.5);
189 cairo_append_path (cr
, path
);
191 cairo_set_fill_rule (cr
, CAIRO_FILL_RULE_EVEN_ODD
);
194 cairo_path_destroy (path
);
196 return CAIRO_TEST_SUCCESS
;
199 static cairo_test_status_t
200 preamble (cairo_test_context_t
*ctx
)
203 cairo_path_data_t data
;
205 cairo_surface_t
*surface
;
206 cairo_status_t status
;
208 surface
= cairo_image_surface_create (CAIRO_FORMAT_ARGB32
, 1, 1);
210 /* Test a few error cases for cairo_append_path_data */
211 cr
= cairo_create (surface
);
212 cairo_append_path (cr
, NULL
);
213 status
= cairo_status (cr
);
215 if (status
!= CAIRO_STATUS_NULL_POINTER
) {
216 cairo_surface_destroy (surface
);
217 return CAIRO_TEST_FAILURE
;
220 cr
= cairo_create (surface
);
222 cairo_append_path (cr
, &path
);
223 status
= cairo_status (cr
);
225 if (status
!= CAIRO_STATUS_INVALID_STATUS
) {
226 cairo_surface_destroy (surface
);
227 return CAIRO_TEST_FAILURE
;
230 cr
= cairo_create (surface
);
231 path
.status
= CAIRO_STATUS_NO_MEMORY
;
232 cairo_append_path (cr
, &path
);
233 status
= cairo_status (cr
);
235 if (status
!= CAIRO_STATUS_NO_MEMORY
) {
236 cairo_surface_destroy (surface
);
237 return CAIRO_TEST_FAILURE
;
240 cr
= cairo_create (surface
);
243 path
.status
= CAIRO_STATUS_SUCCESS
;
244 cairo_append_path (cr
, &path
);
245 status
= cairo_status (cr
);
247 if (status
!= CAIRO_STATUS_SUCCESS
) {
248 cairo_surface_destroy (surface
);
249 return CAIRO_TEST_FAILURE
;
252 cr
= cairo_create (surface
);
255 path
.status
= CAIRO_STATUS_SUCCESS
;
256 cairo_append_path (cr
, &path
);
257 status
= cairo_status (cr
);
259 if (status
!= CAIRO_STATUS_NULL_POINTER
) {
260 cairo_surface_destroy (surface
);
261 return CAIRO_TEST_FAILURE
;
264 cr
= cairo_create (surface
);
265 /* Intentionally insert bogus header.length value (otherwise would be 2) */
266 data
.header
.type
= CAIRO_PATH_MOVE_TO
;
267 data
.header
.length
= 1;
270 cairo_append_path (cr
, &path
);
271 status
= cairo_status (cr
);
273 if (status
!= CAIRO_STATUS_INVALID_PATH_DATA
) {
274 cairo_surface_destroy (surface
);
275 return CAIRO_TEST_FAILURE
;
278 /* And test the degnerate case */
279 cr
= cairo_create (surface
);
281 cairo_append_path (cr
, &path
);
282 status
= cairo_status (cr
);
284 if (status
!= CAIRO_STATUS_SUCCESS
) {
285 cairo_surface_destroy (surface
);
286 return CAIRO_TEST_FAILURE
;
289 cairo_surface_destroy (surface
);
291 return CAIRO_TEST_SUCCESS
;
294 CAIRO_TEST (copy_path
,
295 "Tests calls to path_data functions: cairo_copy_path, cairo_copy_path_flat, and cairo_append_path",
296 "path", /* keywords */
297 NULL
, /* requirements */