Viewport clipping now works with both GL and Gallium.
[cairo/gpu.git] / test / copy-path.c
blob0ea5b4b39a7ae730d39a70fb66ab54bb0cb98d92
1 /*
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 <stdlib.h>
27 #include "cairo-test.h"
29 static void
30 scale_by_two (double *x, double *y)
32 *x = *x * 2.0;
33 *y = *y * 2.0;
36 typedef void (*munge_func_t) (double *x, double *y);
38 static void
39 munge_and_set_path (cairo_t *cr,
40 cairo_path_t *path,
41 munge_func_t munge)
43 int i;
44 cairo_path_data_t *p;
45 double x1, y1, x2, y2, x3, y3;
47 if (path->status) {
48 cairo_append_path (cr, path);
49 return;
52 for (i=0; i < path->num_data; i += path->data[i].header.length) {
53 p = &path->data[i];
54 switch (p->header.type) {
55 case CAIRO_PATH_MOVE_TO:
56 x1 = p[1].point.x; y1 = p[1].point.y;
57 (munge) (&x1, &y1);
58 cairo_move_to (cr, x1, y1);
59 break;
60 case CAIRO_PATH_LINE_TO:
61 x1 = p[1].point.x; y1 = p[1].point.y;
62 (munge) (&x1, &y1);
63 cairo_line_to (cr, x1, y1);
64 break;
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;
69 (munge) (&x1, &y1);
70 (munge) (&x2, &y2);
71 (munge) (&x3, &y3);
72 cairo_curve_to (cr,
73 x1, y1,
74 x2, y2,
75 x3, y3);
76 break;
77 case CAIRO_PATH_CLOSE_PATH:
78 cairo_close_path (cr);
79 break;
84 static void
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);
96 cairo_path_t *path;
97 cairo_t *cr_error;
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)) {
105 cairo_test_log (ctx,
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)) {
117 cairo_test_log (ctx,
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 */
129 cairo_new_path (cr);
130 path = cairo_copy_path (cr);
131 if (path->status != CAIRO_STATUS_SUCCESS) {
132 cairo_test_log (ctx,
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) {
139 cairo_test_log (ctx,
140 "Error: cairo_copy_path did not copy an empty path, returned path contains %d elements\n",
141 path->num_data);
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) {
148 cairo_test_log (ctx,
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. */
155 cairo_save (cr);
156 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
157 cairo_paint (cr);
158 cairo_restore (cr);
160 /* copy path, munge, and fill */
161 cairo_translate (cr, 5, 5);
162 make_path (cr);
163 path = cairo_copy_path (cr);
165 cairo_new_path (cr);
166 munge_and_set_path (cr, path, scale_by_two);
167 cairo_path_destroy (path);
168 cairo_fill (cr);
170 /* copy flattened path, munge, and fill */
171 cairo_translate (cr, 0, 15);
172 make_path (cr);
173 path = cairo_copy_path_flat (cr);
175 cairo_new_path (cr);
176 munge_and_set_path (cr, path, scale_by_two);
177 cairo_path_destroy (path);
178 cairo_fill (cr);
180 /* append two copies of path, and fill */
181 cairo_translate (cr, 0, 15);
182 cairo_scale (cr, 2.0, 2.0);
183 make_path (cr);
184 path = cairo_copy_path (cr);
186 cairo_new_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);
192 cairo_fill (cr);
194 cairo_path_destroy (path);
196 return CAIRO_TEST_SUCCESS;
199 static cairo_test_status_t
200 preamble (cairo_test_context_t *ctx)
202 cairo_t *cr;
203 cairo_path_data_t data;
204 cairo_path_t path;
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);
214 cairo_destroy (cr);
215 if (status != CAIRO_STATUS_NULL_POINTER) {
216 cairo_surface_destroy (surface);
217 return CAIRO_TEST_FAILURE;
220 cr = cairo_create (surface);
221 path.status = -1;
222 cairo_append_path (cr, &path);
223 status = cairo_status (cr);
224 cairo_destroy (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);
234 cairo_destroy (cr);
235 if (status != CAIRO_STATUS_NO_MEMORY) {
236 cairo_surface_destroy (surface);
237 return CAIRO_TEST_FAILURE;
240 cr = cairo_create (surface);
241 path.data = NULL;
242 path.num_data = 0;
243 path.status = CAIRO_STATUS_SUCCESS;
244 cairo_append_path (cr, &path);
245 status = cairo_status (cr);
246 cairo_destroy (cr);
247 if (status != CAIRO_STATUS_SUCCESS) {
248 cairo_surface_destroy (surface);
249 return CAIRO_TEST_FAILURE;
252 cr = cairo_create (surface);
253 path.data = NULL;
254 path.num_data = 1;
255 path.status = CAIRO_STATUS_SUCCESS;
256 cairo_append_path (cr, &path);
257 status = cairo_status (cr);
258 cairo_destroy (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;
268 path.data = &data;
269 path.num_data = 1;
270 cairo_append_path (cr, &path);
271 status = cairo_status (cr);
272 cairo_destroy (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);
280 path.num_data = 0;
281 cairo_append_path (cr, &path);
282 status = cairo_status (cr);
283 cairo_destroy (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 */
298 45, 53,
299 preamble, draw)