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"
29 static cairo_test_draw_function_t draw
;
33 "Tests calls to path_data functions: cairo_copy_path, cairo_copy_path_flat, and cairo_append_path",
39 scale_by_two (double *x
, double *y
)
45 typedef void (*munge_func_t
) (double *x
, double *y
);
48 munge_and_set_path (cairo_t
*cr
,
54 double x1
, y1
, x2
, y2
, x3
, y3
;
56 for (i
=0; i
< path
->num_data
; i
+= path
->data
[i
].header
.length
) {
58 switch (p
->header
.type
) {
59 case CAIRO_PATH_MOVE_TO
:
60 x1
= p
[1].point
.x
; y1
= p
[1].point
.y
;
62 cairo_move_to (cr
, x1
, y1
);
64 case CAIRO_PATH_LINE_TO
:
65 x1
= p
[1].point
.x
; y1
= p
[1].point
.y
;
67 cairo_line_to (cr
, x1
, y1
);
69 case CAIRO_PATH_CURVE_TO
:
70 x1
= p
[1].point
.x
; y1
= p
[1].point
.y
;
71 x2
= p
[2].point
.x
; y2
= p
[2].point
.y
;
72 x3
= p
[3].point
.x
; y3
= p
[3].point
.y
;
81 case CAIRO_PATH_CLOSE_PATH
:
82 cairo_close_path (cr
);
89 make_path (cairo_t
*cr
)
91 cairo_rectangle (cr
, 0, 0, 5, 5);
92 cairo_move_to (cr
, 15, 2.5);
93 cairo_arc (cr
, 12.5, 2.5, 2.5, 0, 2 * M_PI
);
96 static cairo_test_status_t
97 draw (cairo_t
*cr
, int width
, int height
)
102 /* Ensure that calling cairo_copy_path on an in-error cairo_t will
103 * propagate the error. */
104 cr_error
= cairo_create (NULL
);
105 path
= cairo_copy_path (cr_error
);
106 if (path
->status
== CAIRO_STATUS_SUCCESS
||
107 path
->status
!= cairo_status (cr_error
)) {
108 cairo_test_log ("Error: cairo_copy_path returned status of %s rather than propagating %s\n",
109 cairo_status_to_string (path
->status
),
110 cairo_status_to_string (cairo_status (cr_error
)));
111 cairo_path_destroy (path
);
112 return CAIRO_TEST_FAILURE
;
114 cairo_path_destroy (path
);
116 path
= cairo_copy_path_flat (cr_error
);
117 if (path
->status
== CAIRO_STATUS_SUCCESS
||
118 path
->status
!= cairo_status (cr_error
)) {
119 cairo_test_log ("Error: cairo_copy_path_flat returned status of %s rather than propagating %s\n",
120 cairo_status_to_string (path
->status
),
121 cairo_status_to_string (cairo_status (cr_error
)));
122 cairo_path_destroy (path
);
123 return CAIRO_TEST_FAILURE
;
125 cairo_path_destroy (path
);
127 cairo_destroy (cr_error
);
129 /* first check that we can copy an empty path */
131 path
= cairo_copy_path (cr
);
132 if (path
->status
!= CAIRO_STATUS_SUCCESS
) {
133 cairo_test_log ("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) {
139 cairo_test_log ("Error: cairo_copy_path did not copy an empty path, returned path contains %d elements\n",
141 cairo_path_destroy (path
);
142 return CAIRO_TEST_FAILURE
;
144 cairo_append_path (cr
, path
);
145 cairo_path_destroy (path
);
146 if (cairo_status (cr
) != CAIRO_STATUS_SUCCESS
) {
147 cairo_test_log ("Error: cairo_append_path failed with a copy of an empty path, returned status of %s\n",
148 cairo_status_to_string (cairo_status (cr
)));
149 return CAIRO_TEST_FAILURE
;
152 /* We draw in the default black, so paint white first. */
154 cairo_set_source_rgb (cr
, 1.0, 1.0, 1.0); /* white */
158 /* copy path, munge, and fill */
159 cairo_translate (cr
, 5, 5);
161 path
= cairo_copy_path (cr
);
164 munge_and_set_path (cr
, path
, scale_by_two
);
165 cairo_path_destroy (path
);
168 /* copy flattened path, munge, and fill */
169 cairo_translate (cr
, 0, 15);
171 path
= cairo_copy_path_flat (cr
);
174 munge_and_set_path (cr
, path
, scale_by_two
);
175 cairo_path_destroy (path
);
178 /* append two copies of path, and fill */
179 cairo_translate (cr
, 0, 15);
180 cairo_scale (cr
, 2.0, 2.0);
182 path
= cairo_copy_path (cr
);
185 cairo_append_path (cr
, path
);
186 cairo_translate (cr
, 2.5, 2.5);
187 cairo_append_path (cr
, path
);
189 cairo_set_fill_rule (cr
, CAIRO_FILL_RULE_EVEN_ODD
);
192 cairo_path_destroy (path
);
194 return CAIRO_TEST_SUCCESS
;
201 cairo_path_data_t data
;
203 cairo_surface_t
*surface
;
205 surface
= cairo_image_surface_create (CAIRO_FORMAT_ARGB32
, 1, 1);
207 /* Test a few error cases for cairo_append_path_data */
208 cr
= cairo_create (surface
);
209 cairo_append_path (cr
, NULL
);
210 if (cairo_status (cr
) != CAIRO_STATUS_NULL_POINTER
)
214 cr
= cairo_create (surface
);
216 cairo_append_path (cr
, &path
);
217 if (cairo_status (cr
) != CAIRO_STATUS_INVALID_STATUS
)
221 cr
= cairo_create (surface
);
222 path
.status
= CAIRO_STATUS_NO_MEMORY
;
223 cairo_append_path (cr
, &path
);
224 if (cairo_status (cr
) != CAIRO_STATUS_NO_MEMORY
)
228 cr
= cairo_create (surface
);
231 path
.status
= CAIRO_STATUS_SUCCESS
;
232 cairo_append_path (cr
, &path
);
233 if (cairo_status (cr
) != CAIRO_STATUS_SUCCESS
)
237 cr
= cairo_create (surface
);
240 path
.status
= CAIRO_STATUS_SUCCESS
;
241 cairo_append_path (cr
, &path
);
242 if (cairo_status (cr
) != CAIRO_STATUS_NULL_POINTER
)
246 cr
= cairo_create (surface
);
247 /* Intentionally insert bogus header.length value (otherwise would be 2) */
248 data
.header
.type
= CAIRO_PATH_MOVE_TO
;
249 data
.header
.length
= 1;
252 cairo_append_path (cr
, &path
);
253 if (cairo_status (cr
) != CAIRO_STATUS_INVALID_PATH_DATA
)
257 /* And test the degnerate case */
258 cr
= cairo_create (surface
);
260 cairo_append_path (cr
, &path
);
261 if (cairo_status (cr
) != CAIRO_STATUS_SUCCESS
)
265 cairo_surface_destroy (surface
);
267 return cairo_test (&test
);