2 * Copyright © 2006 M Joonas Pihlaja
3 * Copyright © 2011 Chris Wilson
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use, copy,
9 * modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * M Joonas Pihlaja <jpihlaja@cc.helsinki.fi>
27 * Chris Wilson <chris@chris-wilson.co.uk>
29 #include "cairo-test.h"
33 #define NUM_SEGMENTS 128
35 static uint32_t state;
38 uniform_random (double minval, double maxval)
40 static uint32_t const poly = 0x9a795537U;
43 state = 2*state < state ? (2*state ^ poly) : 2*state;
44 return minval + state * (maxval - minval) / 4294967296.0;
47 static void nz_path (cairo_t *cr)
53 cairo_move_to (cr, 0, 0);
54 for (i = 0; i < NUM_SEGMENTS; i++) {
55 double x = uniform_random (0, SIZE);
56 double y = uniform_random (0, SIZE);
57 cairo_line_to (cr, x, y);
59 cairo_close_path (cr);
62 static void region_path (cairo_t *cr)
68 for (i = 0; i < NUM_SEGMENTS; i++) {
69 int x = uniform_random (0, SIZE);
70 int y = uniform_random (0, SIZE);
71 int w = uniform_random (0, 40);
72 int h = uniform_random (0, 40);
73 cairo_rectangle (cr, x, y, w, h);
77 static void rectangle_path (cairo_t *cr)
83 for (i = 0; i < NUM_SEGMENTS; i++) {
84 double x = uniform_random (0, SIZE);
85 double y = uniform_random (0, SIZE);
86 double w = uniform_random (0, 40);
87 double h = uniform_random (0, 40);
88 cairo_rectangle (cr, x, y, w, h);
92 static void arc_path (cairo_t *cr)
98 for (i = 0; i < NUM_SEGMENTS; i++) {
99 double x = uniform_random (0, SIZE);
100 double y = uniform_random (0, SIZE);
101 double r = uniform_random (0, 20);
102 cairo_new_sub_path (cr);
103 cairo_arc (cr, x, y, r, 0, 2*M_PI);
108 static void nz_fill_stroke (cairo_t *cr)
112 cairo_set_source_rgb (cr, 1, 0, 0);
113 cairo_fill_preserve (cr);
114 cairo_set_source_rgb (cr, 0, 1, 0);
115 cairo_set_line_width (cr, 1.0);
119 static void clip_to_quadrant (cairo_t *cr)
121 cairo_rectangle (cr, 0, 0, SIZE, SIZE);
125 static cairo_test_status_t
126 draw (cairo_t *cr, int width, int height)
128 cairo_set_source_rgb (cr, 0, 0, 0);
131 cairo_set_fill_rule (cr, CAIRO_FILL_RULE_WINDING);
134 cairo_translate (cr, 1, 1);
138 clip_to_quadrant (cr);
141 } cairo_restore (cr);
143 cairo_translate (cr, STEP, 0);
145 /* random clipping */
147 clip_to_quadrant (cr);
154 cairo_set_source_rgba (cr, 1, 1, 1, 0.5);
156 } cairo_restore (cr);
158 cairo_translate (cr, STEP, 0);
160 /* regional clipping */
162 clip_to_quadrant (cr);
169 cairo_set_source_rgba (cr, 1, 1, 1, 0.5);
171 } cairo_restore (cr);
173 cairo_translate (cr, -2*STEP, STEP);
175 /* rectangular clipping */
177 clip_to_quadrant (cr);
184 cairo_set_source_rgba (cr, 1, 1, 1, 0.5);
186 } cairo_restore (cr);
188 cairo_translate (cr, STEP, 0);
190 /* circular clipping */
192 clip_to_quadrant (cr);
199 cairo_set_source_rgba (cr, 1, 1, 1, 0.5);
201 } cairo_restore (cr);
203 cairo_translate (cr, STEP, 0);
205 /* all-of-the-above clipping */
207 clip_to_quadrant (cr);
220 cairo_set_source_rgba (cr, 1, 1, 1, 0.5);
222 } cairo_restore (cr);
224 return CAIRO_TEST_SUCCESS;
227 CAIRO_TEST (random_clip,
228 "Tests the clip generation and intersection computation",
229 "trap, clip", /* keywords */
230 NULL, /* requirements */