2 * Copyright © 2006 Novell, 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 * Novell, Inc. not be used in advertising or publicity pertaining to
10 * distribution of the software without specific, written prior
11 * permission. Novell, 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 * NOVELL, 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: Robert O'Callahan <rocallahan@novell.com>
26 #include "cairo-test.h"
30 check_count (const cairo_test_context_t *ctx,
32 cairo_rectangle_list_t *list, int expected)
34 if (list->status != CAIRO_STATUS_SUCCESS) {
35 cairo_test_log (ctx, "Error: %s; cairo_copy_clip_rectangle_list failed with \"%s\"\n",
36 message, cairo_status_to_string(list->status));
40 if (list->num_rectangles == expected)
42 cairo_test_log (ctx, "Error: %s; expected %d rectangles, got %d\n", message,
43 expected, list->num_rectangles);
48 check_unrepresentable (const cairo_test_context_t *ctx, const char *message, cairo_rectangle_list_t *list)
50 if (list->status != CAIRO_STATUS_CLIP_NOT_REPRESENTABLE) {
51 cairo_test_log (ctx, "Error: %s; cairo_copy_clip_rectangle_list got unexpected result \"%s\"\n"
52 " (we expected CAIRO_STATUS_CLIP_NOT_REPRESENTABLE)",
53 message, cairo_status_to_string(list->status));
60 check_rectangles_contain (const cairo_test_context_t *ctx,
62 cairo_rectangle_list_t *list,
63 double x, double y, double width, double height)
67 for (i = 0; i < list->num_rectangles; ++i) {
68 if (list->rectangles[i].x == x && list->rectangles[i].y == y &&
69 list->rectangles[i].width == width && list->rectangles[i].height == height)
72 cairo_test_log (ctx, "Error: %s; rectangle list does not contain rectangle %f,%f,%f,%f\n",
73 message, x, y, width, height);
78 check_clip_extents (const cairo_test_context_t *ctx,
79 const char *message, cairo_t *cr,
80 double x, double y, double width, double height)
82 double ext_x1, ext_y1, ext_x2, ext_y2;
83 cairo_clip_extents (cr, &ext_x1, &ext_y1, &ext_x2, &ext_y2);
84 if (ext_x1 == x && ext_y1 == y && ext_x2 == x + width && ext_y2 == y + height)
86 if (width == 0.0 && height == 0.0 && ext_x1 == ext_x2 && ext_y1 == ext_y2)
88 cairo_test_log (ctx, "Error: %s; clip extents %f,%f,%f,%f should be %f,%f,%f,%f\n",
89 message, ext_x1, ext_y1, ext_x2 - ext_x1, ext_y2 - ext_y1,
96 static cairo_test_status_t
97 preamble (cairo_test_context_t *ctx)
99 cairo_surface_t *surface;
101 cairo_rectangle_list_t *rectangle_list;
103 cairo_bool_t completed = 0;
104 cairo_status_t status;
106 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, SIZE, SIZE);
107 cr = cairo_create (surface);
108 cairo_surface_destroy (surface);
111 /* first, test basic stuff. This should not be clipped, it should
112 return the surface rectangle. */
113 phase = "No clip set";
114 rectangle_list = cairo_copy_clip_rectangle_list (cr);
115 if (! check_count (ctx, phase, rectangle_list, 1) ||
116 ! check_clip_extents (ctx, phase, cr, 0, 0, SIZE, SIZE) ||
117 ! check_rectangles_contain (ctx, phase, rectangle_list, 0, 0, SIZE, SIZE))
121 cairo_rectangle_list_destroy (rectangle_list);
123 /* We should get the same results after applying a clip that contains the
125 phase = "Clip beyond surface extents";
127 cairo_rectangle (cr, -10, -10, SIZE + 20 , SIZE + 20);
129 rectangle_list = cairo_copy_clip_rectangle_list (cr);
130 if (! check_count (ctx, phase, rectangle_list, 1) ||
131 ! check_clip_extents (ctx, phase, cr, 0, 0, SIZE, SIZE) ||
132 ! check_rectangles_contain (ctx, phase, rectangle_list, 0, 0, SIZE, SIZE))
136 cairo_rectangle_list_destroy (rectangle_list);
139 /* Test simple clip rect. */
140 phase = "Simple clip rect";
142 cairo_rectangle (cr, 10, 10, 80, 80);
144 rectangle_list = cairo_copy_clip_rectangle_list (cr);
145 if (! check_count (ctx, phase, rectangle_list, 1) ||
146 ! check_clip_extents (ctx, phase, cr, 10, 10, 80, 80) ||
147 ! check_rectangles_contain (ctx, phase, rectangle_list, 10, 10, 80, 80))
151 cairo_rectangle_list_destroy (rectangle_list);
154 /* Test everything clipped out. */
155 phase = "All clipped out";
158 rectangle_list = cairo_copy_clip_rectangle_list (cr);
159 if (! check_count (ctx, phase, rectangle_list, 0) ||
160 ! check_clip_extents (ctx, phase, cr, 0, 0, 0, 0))
164 cairo_rectangle_list_destroy (rectangle_list);
167 /* test two clip rects */
168 phase = "Two clip rects";
170 cairo_rectangle (cr, 10, 10, 10, 10);
171 cairo_rectangle (cr, 20, 20, 10, 10);
173 cairo_rectangle (cr, 15, 15, 10, 10);
175 rectangle_list = cairo_copy_clip_rectangle_list (cr);
176 if (! check_count (ctx, phase, rectangle_list, 2) ||
177 ! check_clip_extents (ctx, phase, cr, 15, 15, 10, 10) ||
178 ! check_rectangles_contain (ctx, phase, rectangle_list, 15, 15, 5, 5) ||
179 ! check_rectangles_contain (ctx, phase, rectangle_list, 20, 20, 5, 5))
183 cairo_rectangle_list_destroy (rectangle_list);
186 /* test non-rectangular clip */
187 phase = "Nonrectangular clip";
189 cairo_move_to (cr, 0, 0);
190 cairo_line_to (cr, 100, 100);
191 cairo_line_to (cr, 100, 0);
192 cairo_close_path (cr);
194 rectangle_list = cairo_copy_clip_rectangle_list (cr);
195 /* can't get this in one tight user-space rectangle */
196 if (! check_unrepresentable (ctx, phase, rectangle_list) ||
197 ! check_clip_extents (ctx, phase, cr, 0, 0, 100, 100))
201 cairo_rectangle_list_destroy (rectangle_list);
204 phase = "User space, simple scale, getting clip with same transform";
206 cairo_scale (cr, 2, 2);
207 cairo_rectangle (cr, 5, 5, 40, 40);
209 rectangle_list = cairo_copy_clip_rectangle_list (cr);
210 if (! check_count (ctx, phase, rectangle_list, 1) ||
211 ! check_clip_extents (ctx, phase, cr, 5, 5, 40, 40) ||
212 ! check_rectangles_contain (ctx, phase, rectangle_list, 5, 5, 40, 40))
216 cairo_rectangle_list_destroy (rectangle_list);
219 phase = "User space, simple scale, getting clip with no transform";
222 cairo_scale (cr, 2, 2);
223 cairo_rectangle (cr, 5, 5, 40, 40);
226 rectangle_list = cairo_copy_clip_rectangle_list (cr);
227 if (! check_count (ctx, phase, rectangle_list, 1) ||
228 ! check_clip_extents (ctx, phase, cr, 10, 10, 80, 80) ||
229 ! check_rectangles_contain (ctx, phase, rectangle_list, 10, 10, 80, 80))
233 cairo_rectangle_list_destroy (rectangle_list);
236 phase = "User space, rotation, getting clip with no transform";
239 cairo_rotate (cr, 12);
240 cairo_rectangle (cr, 5, 5, 40, 40);
243 rectangle_list = cairo_copy_clip_rectangle_list (cr);
244 if (! check_unrepresentable (ctx, phase, rectangle_list))
249 cairo_rectangle_list_destroy (rectangle_list);
250 status = cairo_status (cr);
254 return CAIRO_TEST_FAILURE;
256 return cairo_test_status_from_status (ctx, status);
259 CAIRO_TEST (get_clip,
260 "Test cairo_copy_clip_rectangle_list and cairo_clip_extents",
261 "clip, extents", /* keywords */
262 NULL, /* requirements */