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
30
96
scale_by_two (double *x, double *y)
31
{
32
96
    *x = *x * 2.0;
33
96
    *y = *y * 2.0;
34
96
}
35

            
36
typedef void (*munge_func_t) (double *x, double *y);
37

            
38
static void
39
6
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
6
    if (path->status) {
48
	cairo_append_path (cr, path);
49
	return;
50
    }
51

            
52
96
    for (i=0; i < path->num_data; i += path->data[i].header.length) {
53
90
	p = &path->data[i];
54
90
	switch (p->header.type) {
55
12
	case CAIRO_PATH_MOVE_TO:
56
12
	    x1 = p[1].point.x; y1 = p[1].point.y;
57
12
	    (munge) (&x1, &y1);
58
12
	    cairo_move_to (cr, x1, y1);
59
12
	    break;
60
66
	case CAIRO_PATH_LINE_TO:
61
66
	    x1 = p[1].point.x; y1 = p[1].point.y;
62
66
	    (munge) (&x1, &y1);
63
66
	    cairo_line_to (cr, x1, y1);
64
66
	    break;
65
6
	case CAIRO_PATH_CURVE_TO:
66
6
	    x1 = p[1].point.x; y1 = p[1].point.y;
67
6
	    x2 = p[2].point.x; y2 = p[2].point.y;
68
6
	    x3 = p[3].point.x; y3 = p[3].point.y;
69
6
	    (munge) (&x1, &y1);
70
6
	    (munge) (&x2, &y2);
71
6
	    (munge) (&x3, &y3);
72
6
	    cairo_curve_to (cr,
73
			    x1, y1,
74
			    x2, y2,
75
			    x3, y3);
76
6
	    break;
77
6
	case CAIRO_PATH_CLOSE_PATH:
78
6
	    cairo_close_path (cr);
79
6
	    break;
80
	}
81
    }
82
}
83

            
84
static void
85
9
make_path (cairo_t *cr)
86
{
87
9
    cairo_rectangle (cr, 0, 0, 5, 5);
88
9
    cairo_move_to (cr, 15, 2.5);
89
9
    cairo_arc (cr, 12.5, 2.5, 2.5, 0, 2 * M_PI);
90
9
}
91

            
92
static cairo_test_status_t
93
3
draw (cairo_t *cr, int width, int height)
94
{
95
3
    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
3
    cr_error = cairo_create (NULL);
102
3
    path = cairo_copy_path (cr_error);
103
3
    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
3
    cairo_path_destroy (path);
113

            
114
3
    path = cairo_copy_path_flat (cr_error);
115
3
    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
3
    cairo_path_destroy (path);
125

            
126
3
    cairo_destroy (cr_error);
127

            
128
    /* first check that we can copy an empty path */
129
3
    cairo_new_path (cr);
130
3
    path = cairo_copy_path (cr);
131
3
    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
3
    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
3
    cairo_append_path (cr, path);
147
3
    cairo_path_destroy (path);
148
3
    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
3
    cairo_save (cr);
157
3
    cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
158
3
    cairo_paint (cr);
159
3
    cairo_restore (cr);
160

            
161
    /* copy path, munge, and fill */
162
3
    cairo_translate (cr, 5, 5);
163
3
    make_path (cr);
164
3
    path = cairo_copy_path (cr);
165

            
166
3
    cairo_new_path (cr);
167
3
    munge_and_set_path (cr, path, scale_by_two);
168
3
    cairo_path_destroy (path);
169
3
    cairo_fill (cr);
170

            
171
    /* copy flattened path, munge, and fill */
172
3
    cairo_translate (cr, 0, 15);
173
3
    make_path (cr);
174
3
    path = cairo_copy_path_flat (cr);
175

            
176
3
    cairo_new_path (cr);
177
3
    munge_and_set_path (cr, path, scale_by_two);
178
3
    cairo_path_destroy (path);
179
3
    cairo_fill (cr);
180

            
181
    /* append two copies of path, and fill */
182
3
    cairo_translate (cr, 0, 15);
183
3
    cairo_scale (cr, 2.0, 2.0);
184
3
    make_path (cr);
185
3
    path = cairo_copy_path (cr);
186

            
187
3
    cairo_new_path (cr);
188
3
    cairo_append_path (cr, path);
189
3
    cairo_translate (cr, 2.5, 2.5);
190
3
    cairo_append_path (cr, path);
191

            
192
3
    cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
193
3
    cairo_fill (cr);
194

            
195
3
    cairo_path_destroy (path);
196

            
197
3
    return CAIRO_TEST_SUCCESS;
198
}
199

            
200
static cairo_test_status_t
201
1
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
1
    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
210
1
    status = cairo_surface_status (surface);
211
1
    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
1
    CAIRO_CREATE ();
227
1
    cairo_append_path (cr, NULL);
228
1
    status = cairo_status (cr);
229
1
    cairo_destroy (cr);
230
1
    if (status != CAIRO_STATUS_NULL_POINTER) {
231
	cairo_surface_destroy (surface);
232
	return cairo_test_status_from_status (ctx, status);
233
    }
234

            
235
1
    CAIRO_CREATE ();
236
1
    path.status = -1;
237
1
    cairo_append_path (cr, &path);
238
1
    status = cairo_status (cr);
239
1
    cairo_destroy (cr);
240
1
    if (status != CAIRO_STATUS_INVALID_STATUS) {
241
	cairo_surface_destroy (surface);
242
	return cairo_test_status_from_status (ctx, status);
243
    }
244

            
245
1
    CAIRO_CREATE ();
246
1
    path.status = CAIRO_STATUS_NO_MEMORY;
247
1
    cairo_append_path (cr, &path);
248
1
    status = cairo_status (cr);
249
1
    cairo_destroy (cr);
250
1
    if (status != CAIRO_STATUS_NO_MEMORY) {
251
	cairo_surface_destroy (surface);
252
	return cairo_test_status_from_status (ctx, status);
253
    }
254

            
255
1
    CAIRO_CREATE ();
256
1
    path.data = NULL;
257
1
    path.num_data = 0;
258
1
    path.status = CAIRO_STATUS_SUCCESS;
259
1
    cairo_append_path (cr, &path);
260
1
    status = cairo_status (cr);
261
1
    cairo_destroy (cr);
262
1
    if (status != CAIRO_STATUS_SUCCESS) {
263
	cairo_surface_destroy (surface);
264
	return cairo_test_status_from_status (ctx, status);
265
    }
266

            
267
1
    CAIRO_CREATE ();
268
1
    path.data = NULL;
269
1
    path.num_data = 1;
270
1
    path.status = CAIRO_STATUS_SUCCESS;
271
1
    cairo_append_path (cr, &path);
272
1
    status = cairo_status (cr);
273
1
    cairo_destroy (cr);
274
1
    if (status != CAIRO_STATUS_NULL_POINTER) {
275
	cairo_surface_destroy (surface);
276
	return cairo_test_status_from_status (ctx, status);
277
    }
278

            
279
1
    CAIRO_CREATE ();
280
    /* Intentionally insert bogus header.length value (otherwise would be 2) */
281
1
    data.header.type = CAIRO_PATH_MOVE_TO;
282
1
    data.header.length = 1;
283
1
    path.data = &data;
284
1
    path.num_data = 1;
285
1
    cairo_append_path (cr, &path);
286
1
    status = cairo_status (cr);
287
1
    cairo_destroy (cr);
288
1
    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
1
    CAIRO_CREATE ();
295
1
    path.num_data = 0;
296
1
    cairo_append_path (cr, &path);
297
1
    status = cairo_status (cr);
298
1
    cairo_destroy (cr);
299
1
    if (status != CAIRO_STATUS_SUCCESS) {
300
	cairo_surface_destroy (surface);
301
	return cairo_test_status_from_status (ctx, status);
302
    }
303

            
304
1
    cairo_surface_destroy (surface);
305

            
306
1
    return CAIRO_TEST_SUCCESS;
307
}
308

            
309
1
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)