summaryrefslogtreecommitdiff
path: root/test/get-clip.c
blob: 9296966ae18d4a0ac74c9acf771e9fe7fb527fbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Copyright © 2006 Novell, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * Novell, Inc. not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. Novell, Inc. makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * NOVELL, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: Robert O'Callahan <rocallahan@novell.com>
 */

#include "cairo-test.h"
#include <stddef.h>

static cairo_bool_t
check_count (const cairo_test_context_t *ctx,
	     const char *message, cairo_bool_t uses_clip_rects,
             cairo_rectangle_list_t *list, int expected)
{
    if (!uses_clip_rects) {
        if (expected == 0 && list->num_rectangles == 0)
            return 1;
	if (list->num_rectangles == expected)
	    return 1;
	if (list->status == CAIRO_STATUS_CLIP_NOT_REPRESENTABLE)
	    return 1;
        cairo_test_log (ctx, "Error: %s; cairo_copy_clip_rectangle_list unexpectedly got %d rectangles\n",
                        message, list->num_rectangles);
        return 0;
    }

    if (list->status != CAIRO_STATUS_SUCCESS) {
        cairo_test_log (ctx, "Error: %s; cairo_copy_clip_rectangle_list failed with \"%s\"\n",
                        message, cairo_status_to_string(list->status));
        return 0;
    }

    if (list->num_rectangles == expected)
        return 1;
    cairo_test_log (ctx, "Error: %s; expected %d rectangles, got %d\n", message,
                    expected, list->num_rectangles);
    return 0;
}

static cairo_bool_t
check_unrepresentable (const cairo_test_context_t *ctx, const char *message, cairo_rectangle_list_t *list)
{
    if (list->status != CAIRO_STATUS_CLIP_NOT_REPRESENTABLE) {
        cairo_test_log (ctx, "Error: %s; cairo_copy_clip_rectangle_list got unexpected result \"%s\"\n"
                        " (we expected CAIRO_STATUS_CLIP_NOT_REPRESENTABLE)",
                        message, cairo_status_to_string(list->status));
        return 0;
    }
    return 1;
}

static cairo_bool_t
check_rectangles_contain (const cairo_test_context_t *ctx,
			  const char *message, cairo_bool_t uses_clip_rects,
                          cairo_rectangle_list_t *list,
                          double x, double y, double width, double height)
{
    int i;

    if (!uses_clip_rects)
        return 1;

    for (i = 0; i < list->num_rectangles; ++i) {
        if (list->rectangles[i].x == x && list->rectangles[i].y == y &&
            list->rectangles[i].width == width && list->rectangles[i].height == height)
            return 1;
    }
    cairo_test_log (ctx, "Error: %s; rectangle list does not contain rectangle %f,%f,%f,%f\n",
                    message, x, y, width, height);
    return 0;
}

static cairo_bool_t
check_clip_extents (const cairo_test_context_t *ctx,
		    const char *message, cairo_t *cr,
                    double x, double y, double width, double height)
{
    double ext_x1, ext_y1, ext_x2, ext_y2;
    cairo_clip_extents (cr, &ext_x1, &ext_y1, &ext_x2, &ext_y2);
    if (ext_x1 == x && ext_y1 == y && ext_x2 == x + width && ext_y2 == y + height)
        return 1;
    cairo_test_log (ctx, "Error: %s; clip extents %f,%f,%f,%f should be %f,%f,%f,%f\n",
                    message, ext_x1, ext_y1, ext_x2 - ext_x1, ext_y2 - ext_y1,
                    x, y, width, height);
    return 0;
}

static cairo_test_status_t
draw (cairo_t *cr, int width, int height)
{
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
    cairo_surface_t        *surface;
    cairo_t                *cr2;
    cairo_rectangle_list_t *rectangle_list;
    const char             *phase;
    cairo_bool_t            uses_clip_rects;
    cairo_status_t          status;

    surface = cairo_surface_create_similar (cairo_get_group_target (cr),
                                            CAIRO_CONTENT_COLOR, 100, 100);
    /* Check the surface type so we ignore cairo_copy_clip_rectangle_list failures
     * on surface types that don't use rectangle lists for clipping.
     * Default to FALSE for the internal surface types, (meta, test-fallback, etc.)
     */
    switch (cairo_surface_get_type (surface)) {
    case CAIRO_SURFACE_TYPE_IMAGE:
    case CAIRO_SURFACE_TYPE_XLIB:
    case CAIRO_SURFACE_TYPE_XCB:
    case CAIRO_SURFACE_TYPE_GLITZ:
    case CAIRO_SURFACE_TYPE_WIN32:
    case CAIRO_SURFACE_TYPE_BEOS:
    case CAIRO_SURFACE_TYPE_DIRECTFB:
    case CAIRO_SURFACE_TYPE_GL:
    case CAIRO_SURFACE_TYPE_DRM:
        uses_clip_rects = TRUE;
	break;
    case CAIRO_SURFACE_TYPE_QUARTZ:
    case CAIRO_SURFACE_TYPE_QUARTZ_IMAGE:
    case CAIRO_SURFACE_TYPE_PDF:
    case CAIRO_SURFACE_TYPE_PS:
    case CAIRO_SURFACE_TYPE_SVG:
    case CAIRO_SURFACE_TYPE_OS2:
    case CAIRO_SURFACE_TYPE_WIN32_PRINTING:
    case CAIRO_SURFACE_TYPE_SCRIPT:
    case CAIRO_SURFACE_TYPE_QT:
    case CAIRO_SURFACE_TYPE_META:
    case CAIRO_SURFACE_TYPE_VG:
    default:
        uses_clip_rects = FALSE;
        break;
    }

    /* don't use cr accidentally */
    cr = NULL;
    cr2 = cairo_create (surface);
    cairo_surface_destroy (surface);


    /* first, test basic stuff. This should not be clipped, it should
       return the surface rectangle. */
    phase = "No clip set";
    rectangle_list = cairo_copy_clip_rectangle_list (cr2);
    if (! check_count (ctx, phase, uses_clip_rects, rectangle_list, 1) ||
        ! check_clip_extents (ctx, phase, cr2, 0, 0, 100, 100) ||
        ! check_rectangles_contain (ctx, phase, uses_clip_rects, rectangle_list, 0, 0, 100, 100))
    {
	goto FAIL;
    }
    cairo_rectangle_list_destroy (rectangle_list);

    /* Test simple clip rect. */
    phase = "Simple clip rect";
    cairo_save (cr2);
    cairo_rectangle (cr2, 10, 10, 80, 80);
    cairo_clip (cr2);
    rectangle_list = cairo_copy_clip_rectangle_list (cr2);
    if (! check_count (ctx, phase, uses_clip_rects, rectangle_list, 1) ||
        ! check_clip_extents (ctx, phase, cr2, 10, 10, 80, 80) ||
        ! check_rectangles_contain (ctx, phase, uses_clip_rects, rectangle_list, 10, 10, 80, 80))
    {
	goto FAIL;
    }
    cairo_rectangle_list_destroy (rectangle_list);
    cairo_restore (cr2);

    /* Test everything clipped out. */
    phase = "All clipped out";
    cairo_save (cr2);
    cairo_clip (cr2);
    rectangle_list = cairo_copy_clip_rectangle_list (cr2);
    if (! check_count (ctx, phase, uses_clip_rects, rectangle_list, 0))
    {
	goto FAIL;
    }
    cairo_rectangle_list_destroy (rectangle_list);
    cairo_restore (cr2);

    /* test two clip rects */
    phase = "Two clip rects";
    cairo_save (cr2);
    cairo_rectangle (cr2, 10, 10, 10, 10);
    cairo_rectangle (cr2, 20, 20, 10, 10);
    cairo_clip (cr2);
    cairo_rectangle (cr2, 15, 15, 10, 10);
    cairo_clip (cr2);
    rectangle_list = cairo_copy_clip_rectangle_list (cr2);
    if (! check_count (ctx, phase, uses_clip_rects, rectangle_list, 2) ||
        ! check_clip_extents (ctx, phase, cr2, 15, 15, 10, 10) ||
        ! check_rectangles_contain (ctx, phase, uses_clip_rects, rectangle_list, 15, 15, 5, 5) ||
        ! check_rectangles_contain (ctx, phase, uses_clip_rects, rectangle_list, 20, 20, 5, 5))
    {
	goto FAIL;
    }
    cairo_rectangle_list_destroy (rectangle_list);
    cairo_restore (cr2);

    /* test non-rectangular clip */
    phase = "Nonrectangular clip";
    cairo_save (cr2);
    cairo_move_to (cr2, 0, 0);
    cairo_line_to (cr2, 100, 100);
    cairo_line_to (cr2, 100, 0);
    cairo_close_path (cr2);
    cairo_clip (cr2);
    rectangle_list = cairo_copy_clip_rectangle_list (cr2);
     /* can't get this in one tight user-space rectangle */
    if (! check_unrepresentable (ctx, phase, rectangle_list) ||
        ! check_clip_extents (ctx, phase, cr2, 0, 0, 100, 100))
    {
	goto FAIL;
    }
    cairo_rectangle_list_destroy (rectangle_list);
    cairo_restore (cr2);

    phase = "User space, simple scale, getting clip with same transform";
    cairo_save (cr2);
    cairo_scale (cr2, 2, 2);
    cairo_rectangle (cr2, 5, 5, 40, 40);
    cairo_clip (cr2);
    rectangle_list = cairo_copy_clip_rectangle_list (cr2);
    if (! check_count (ctx, phase, uses_clip_rects, rectangle_list, 1) ||
        ! check_clip_extents (ctx, phase, cr2, 5, 5, 40, 40) ||
        ! check_rectangles_contain (ctx, phase, uses_clip_rects, rectangle_list, 5, 5, 40, 40))
    {
	goto FAIL;
    }
    cairo_rectangle_list_destroy (rectangle_list);
    cairo_restore (cr2);

    phase = "User space, simple scale, getting clip with no transform";
    cairo_save (cr2);
    cairo_save (cr2);
    cairo_scale (cr2, 2, 2);
    cairo_rectangle (cr2, 5, 5, 40, 40);
    cairo_restore (cr2);
    cairo_clip (cr2);
    rectangle_list = cairo_copy_clip_rectangle_list (cr2);
    if (! check_count (ctx, phase, uses_clip_rects, rectangle_list, 1) ||
        ! check_clip_extents (ctx, phase, cr2, 10, 10, 80, 80) ||
        ! check_rectangles_contain (ctx, phase, uses_clip_rects, rectangle_list, 10, 10, 80, 80))
    {
	goto FAIL;
    }
    cairo_rectangle_list_destroy (rectangle_list);
    cairo_restore (cr2);

    phase = "User space, rotation, getting clip with no transform";
    cairo_save (cr2);
    cairo_save (cr2);
    cairo_rotate (cr2, 12);
    cairo_rectangle (cr2, 5, 5, 40, 40);
    cairo_restore (cr2);
    cairo_clip (cr2);
    rectangle_list = cairo_copy_clip_rectangle_list (cr2);
    if (! check_unrepresentable (ctx, phase, rectangle_list))
	goto FAIL;

FAIL:
    cairo_rectangle_list_destroy (rectangle_list);
    status = cairo_status (cr2);
    cairo_destroy (cr2);

    return cairo_test_status_from_status (ctx, status);
}

CAIRO_TEST (get_clip,
	    "Test cairo_copy_clip_rectangle_list and cairo_clip_extents",
	    "clip, extents", /* keywords */
	    NULL, /* requirements */
	    0, 0,
	    NULL, draw)