1 /*
2 * Copyright © 2005 Red Hat, Inc.
3 *
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.
14 *
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.
22 *
23 * Author: Carl D. Worth <cworth@cworth.org>
24 */
25
26 #include "cairo-test.h"
27 #include <stdlib.h>
28
29 static void
scale_by_two(double * x,double * y)30 scale_by_two (double *x, double *y)
31 {
32 *x = *x * 2.0;
33 *y = *y * 2.0;
34 }
35
36 typedef void (*munge_func_t) (double *x, double *y);
37
38 static void
munge_and_set_path(cairo_t * cr,cairo_path_t * path,munge_func_t munge)39 munge_and_set_path (cairo_t *cr,
40 cairo_path_t *path,
41 munge_func_t munge)
42 {
43 int i;
44 cairo_path_data_t *p;
45 double x1, y1, x2, y2, x3, y3;
46
47 if (path->status) {
48 cairo_append_path (cr, path);
49 return;
50 }
51
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;
80 }
81 }
82 }
83
84 static void
make_path(cairo_t * cr)85 make_path (cairo_t *cr)
86 {
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);
90 }
91
92 static cairo_test_status_t
draw(cairo_t * cr,int width,int height)93 draw (cairo_t *cr, int width, int height)
94 {
95 const cairo_test_context_t *ctx = cairo_test_get_context (cr);
96 cairo_path_t *path;
97 cairo_t *cr_error;
98
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) {
104 cairo_test_log (ctx,
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;
111 }
112 cairo_path_destroy (path);
113
114 path = cairo_copy_path_flat (cr_error);
115 if (path->status != CAIRO_STATUS_NULL_POINTER) {
116 cairo_test_log (ctx,
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;
123 }
124 cairo_path_destroy (path);
125
126 cairo_destroy (cr_error);
127
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_status_t status = path->status;
133 cairo_test_log (ctx,
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);
138 }
139 if (path->num_data != 0) {
140 cairo_test_log (ctx,
141 "Error: cairo_copy_path did not copy an empty path, returned path contains %d elements\n",
142 path->num_data);
143 cairo_path_destroy (path);
144 return CAIRO_TEST_FAILURE;
145 }
146 cairo_append_path (cr, path);
147 cairo_path_destroy (path);
148 if (cairo_status (cr) != CAIRO_STATUS_SUCCESS) {
149 cairo_test_log (ctx,
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));
153 }
154
155 /* We draw in the default black, so paint white first. */
156 cairo_save (cr);
157 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
158 cairo_paint (cr);
159 cairo_restore (cr);
160
161 /* copy path, munge, and fill */
162 cairo_translate (cr, 5, 5);
163 make_path (cr);
164 path = cairo_copy_path (cr);
165
166 cairo_new_path (cr);
167 munge_and_set_path (cr, path, scale_by_two);
168 cairo_path_destroy (path);
169 cairo_fill (cr);
170
171 /* copy flattened path, munge, and fill */
172 cairo_translate (cr, 0, 15);
173 make_path (cr);
174 path = cairo_copy_path_flat (cr);
175
176 cairo_new_path (cr);
177 munge_and_set_path (cr, path, scale_by_two);
178 cairo_path_destroy (path);
179 cairo_fill (cr);
180
181 /* append two copies of path, and fill */
182 cairo_translate (cr, 0, 15);
183 cairo_scale (cr, 2.0, 2.0);
184 make_path (cr);
185 path = cairo_copy_path (cr);
186
187 cairo_new_path (cr);
188 cairo_append_path (cr, path);
189 cairo_translate (cr, 2.5, 2.5);
190 cairo_append_path (cr, path);
191
192 cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
193 cairo_fill (cr);
194
195 cairo_path_destroy (path);
196
197 return CAIRO_TEST_SUCCESS;
198 }
199
200 static cairo_test_status_t
preamble(cairo_test_context_t * ctx)201 preamble (cairo_test_context_t *ctx)
202 {
203 cairo_t *cr;
204 cairo_path_data_t data;
205 cairo_path_t path;
206 cairo_surface_t *surface;
207 cairo_status_t status;
208
209 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
210 status = cairo_surface_status (surface);
211 if (status) {
212 cairo_surface_destroy (surface);
213 return cairo_test_status_from_status (ctx, status);
214 }
215
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); \
220 if (status) { \
221 cairo_destroy (cr); \
222 cairo_surface_destroy (surface); \
223 return cairo_test_status_from_status (ctx, status); \
224 } \
225 } while (0)
226 CAIRO_CREATE ();
227 cairo_append_path (cr, NULL);
228 status = cairo_status (cr);
229 cairo_destroy (cr);
230 if (status != CAIRO_STATUS_NULL_POINTER) {
231 cairo_surface_destroy (surface);
232 return cairo_test_status_from_status (ctx, status);
233 }
234
235 CAIRO_CREATE ();
236 path.status = -1;
237 cairo_append_path (cr, &path);
238 status = cairo_status (cr);
239 cairo_destroy (cr);
240 if (status != CAIRO_STATUS_INVALID_STATUS) {
241 cairo_surface_destroy (surface);
242 return cairo_test_status_from_status (ctx, status);
243 }
244
245 CAIRO_CREATE ();
246 path.status = CAIRO_STATUS_NO_MEMORY;
247 cairo_append_path (cr, &path);
248 status = cairo_status (cr);
249 cairo_destroy (cr);
250 if (status != CAIRO_STATUS_NO_MEMORY) {
251 cairo_surface_destroy (surface);
252 return cairo_test_status_from_status (ctx, status);
253 }
254
255 CAIRO_CREATE ();
256 path.data = NULL;
257 path.num_data = 0;
258 path.status = CAIRO_STATUS_SUCCESS;
259 cairo_append_path (cr, &path);
260 status = cairo_status (cr);
261 cairo_destroy (cr);
262 if (status != CAIRO_STATUS_SUCCESS) {
263 cairo_surface_destroy (surface);
264 return cairo_test_status_from_status (ctx, status);
265 }
266
267 CAIRO_CREATE ();
268 path.data = NULL;
269 path.num_data = 1;
270 path.status = CAIRO_STATUS_SUCCESS;
271 cairo_append_path (cr, &path);
272 status = cairo_status (cr);
273 cairo_destroy (cr);
274 if (status != CAIRO_STATUS_NULL_POINTER) {
275 cairo_surface_destroy (surface);
276 return cairo_test_status_from_status (ctx, status);
277 }
278
279 CAIRO_CREATE ();
280 /* Intentionally insert bogus header.length value (otherwise would be 2) */
281 data.header.type = CAIRO_PATH_MOVE_TO;
282 data.header.length = 1;
283 path.data = &data;
284 path.num_data = 1;
285 cairo_append_path (cr, &path);
286 status = cairo_status (cr);
287 cairo_destroy (cr);
288 if (status != CAIRO_STATUS_INVALID_PATH_DATA) {
289 cairo_surface_destroy (surface);
290 return cairo_test_status_from_status (ctx, status);
291 }
292
293 /* And test the degenerate case */
294 CAIRO_CREATE ();
295 path.num_data = 0;
296 cairo_append_path (cr, &path);
297 status = cairo_status (cr);
298 cairo_destroy (cr);
299 if (status != CAIRO_STATUS_SUCCESS) {
300 cairo_surface_destroy (surface);
301 return cairo_test_status_from_status (ctx, status);
302 }
303
304 cairo_surface_destroy (surface);
305
306 return CAIRO_TEST_SUCCESS;
307 }
308
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 */
313 45, 53,
314 preamble, draw)
315