1 /*
2  * Copyright © 2005, 2007 Red Hat, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * Author: Carl D. Worth <cworth@cworth.org>
25  */
26 
27 #include "cairo-test.h"
28 
29 #define NUM_GRADIENTS 7
30 #define NUM_EXTEND 4
31 #define SIZE 120
32 #define WIDTH (SIZE * NUM_GRADIENTS)
33 #define HEIGHT (SIZE * NUM_EXTEND)
34 
35 typedef void (*composite_t)(cairo_t *cr, cairo_pattern_t *pattern);
36 typedef void (*add_stops_t)(cairo_pattern_t *pattern);
37 
38 /*
39  * We want to test all the possible relative positions of the start
40  * and end circle:
41  *
42  *  - The start circle can be smaller/equal/bigger than the end
43  *    circle. A radial gradient can be classified in one of these
44  *    three cases depending on the sign of dr.
45  *
46  *  - The smaller circle can be completely inside/internally
47  *    tangent/outside (at least in part) of the bigger circle. This
48  *    classification is the same as the one which can be computed by
49  *    examining the sign of a = (dx^2 + dy^2 - dr^2).
50  *
51  *  - If the two circles have the same size, neither can be inside or
52  *    internally tangent
53  *
54  * This test draws radial gradients whose circles always have the same
55  * centers (0, 0) and (1, 0), but with different radiuses. From left
56  * to right:
57  *
58  * - Small start circle completely inside the end circle
59  *     0.25 -> 1.75; dr =  1.5 > 0; a = 1 - 1.50^2 < 0
60  *
61  * - Small start circle internally tangent to the end circle
62  *     0.50 -> 1.50; dr =  1.0 > 0; a = 1 - 1.00^2 = 0
63  *
64  * - Small start circle outside of the end circle
65  *     0.50 -> 1.00; dr =  0.5 > 0; a = 1 - 0.50^2 > 0
66  *
67  * - Start circle with the same size as the end circle
68  *     1.00 -> 1.00; dr =  0.0 = 0; a = 1 - 0.00^2 > 0
69  *
70  * - Small end circle outside of the start circle
71  *     1.00 -> 0.50; dr = -0.5 > 0; a = 1 - 0.50^2 > 0
72  *
73  * - Small end circle internally tangent to the start circle
74  *     1.50 -> 0.50; dr = -1.0 > 0; a = 1 - 1.00^2 = 0
75  *
76  * - Small end circle completely inside the start circle
77  *     1.75 -> 0.25; dr = -1.5 > 0; a = 1 - 1.50^2 < 0
78  *
79  */
80 
81 static const double radiuses[NUM_GRADIENTS] = {
82     0.25,
83     0.50,
84     0.50,
85     1.00,
86     1.00,
87     1.50,
88     1.75
89 };
90 
91 static cairo_pattern_t *
create_pattern(int index)92 create_pattern (int index)
93 {
94     double x0, x1, radius0, radius1, left, right, center;
95 
96     x0 = 0;
97     x1 = 1;
98     radius0 = radiuses[index];
99     radius1 = radiuses[NUM_GRADIENTS - index - 1];
100 
101     /* center the gradient */
102     left = MIN (x0 - radius0, x1 - radius1);
103     right = MAX (x0 + radius0, x1 + radius1);
104     center = (left + right) * 0.5;
105     x0 -= center;
106     x1 -= center;
107 
108     /* scale to make it fit within a 1x1 rect centered in (0,0) */
109     x0 *= 0.25;
110     x1 *= 0.25;
111     radius0 *= 0.25;
112     radius1 *= 0.25;
113 
114     return cairo_pattern_create_radial (x0, 0, radius0, x1, 0, radius1);
115 }
116 
117 static void
pattern_add_stops(cairo_pattern_t * pattern)118 pattern_add_stops (cairo_pattern_t *pattern)
119 {
120     cairo_pattern_add_color_stop_rgba (pattern, 0.0,        1, 0, 0, 0.75);
121     cairo_pattern_add_color_stop_rgba (pattern, sqrt (0.5), 0, 1, 0, 0);
122     cairo_pattern_add_color_stop_rgba (pattern, 1.0,        0, 0, 1, 1);
123 }
124 
125 static void
pattern_add_single_stop(cairo_pattern_t * pattern)126 pattern_add_single_stop (cairo_pattern_t *pattern)
127 {
128     cairo_pattern_add_color_stop_rgba (pattern, 0.25, 1, 0, 0, 1);
129 }
130 
131 
132 static cairo_test_status_t
draw(cairo_t * cr,add_stops_t add_stops,composite_t composite)133 draw (cairo_t *cr, add_stops_t add_stops, composite_t composite)
134 {
135     int i, j;
136     cairo_extend_t extend[NUM_EXTEND] = {
137 	CAIRO_EXTEND_NONE,
138 	CAIRO_EXTEND_REPEAT,
139 	CAIRO_EXTEND_REFLECT,
140 	CAIRO_EXTEND_PAD
141     };
142 
143     cairo_scale (cr, SIZE, SIZE);
144     cairo_translate (cr, 0.5, 0.5);
145 
146     for (j = 0; j < NUM_EXTEND; j++) {
147 	cairo_save (cr);
148 	for (i = 0; i < NUM_GRADIENTS; i++) {
149 	    cairo_pattern_t *pattern;
150 
151 	    pattern = create_pattern (i);
152 	    add_stops (pattern);
153 	    cairo_pattern_set_extend (pattern, extend[j]);
154 
155 	    cairo_save (cr);
156 	    cairo_rectangle (cr, -0.5, -0.5, 1, 1);
157 	    cairo_clip (cr);
158 	    composite (cr, pattern);
159 	    cairo_restore (cr);
160 	    cairo_pattern_destroy (pattern);
161 
162 	    cairo_translate (cr, 1, 0);
163 	}
164 	cairo_restore (cr);
165 	cairo_translate (cr, 0, 1);
166     }
167 
168     return CAIRO_TEST_SUCCESS;
169 }
170 
171 
172 static void
composite_simple(cairo_t * cr,cairo_pattern_t * pattern)173 composite_simple (cairo_t *cr, cairo_pattern_t *pattern)
174 {
175     cairo_set_source (cr, pattern);
176     cairo_paint (cr);
177 }
178 
179 static void
composite_mask(cairo_t * cr,cairo_pattern_t * pattern)180 composite_mask (cairo_t *cr, cairo_pattern_t *pattern)
181 {
182     cairo_set_source_rgb (cr, 1, 0, 1);
183     cairo_mask (cr, pattern);
184 }
185 
186 
187 static cairo_test_status_t
draw_simple(cairo_t * cr,int width,int height)188 draw_simple (cairo_t *cr, int width, int height)
189 {
190     cairo_test_paint_checkered (cr);
191     return draw (cr, pattern_add_stops, composite_simple);
192 }
193 
194 static cairo_test_status_t
draw_mask(cairo_t * cr,int width,int height)195 draw_mask (cairo_t *cr, int width, int height)
196 {
197     cairo_test_paint_checkered (cr);
198     return draw (cr, pattern_add_stops, composite_mask);
199 }
200 
201 static cairo_test_status_t
draw_source(cairo_t * cr,int width,int height)202 draw_source (cairo_t *cr, int width, int height)
203 {
204     cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
205     return draw (cr, pattern_add_stops, composite_simple);
206 }
207 
208 
209 static cairo_test_status_t
draw_mask_source(cairo_t * cr,int width,int height)210 draw_mask_source (cairo_t *cr, int width, int height)
211 {
212     cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
213     return draw (cr, pattern_add_stops, composite_mask);
214 }
215 
216 static cairo_test_status_t
draw_one_stop(cairo_t * cr,int width,int height)217 draw_one_stop (cairo_t *cr, int width, int height)
218 {
219     cairo_test_paint_checkered (cr);
220     return draw (cr, pattern_add_single_stop, composite_simple);
221 }
222 
223 CAIRO_TEST (radial_gradient,
224 	    "Simple test of radial gradients",
225 	    "gradient", /* keywords */
226 	    NULL, /* requirements */
227 	    WIDTH, HEIGHT,
228 	    NULL, draw_simple)
229 
230 CAIRO_TEST (radial_gradient_mask,
231 	    "Simple test of radial gradients using a MASK",
232 	    "gradient,mask", /* keywords */
233 	    NULL, /* requirements */
234 	    WIDTH, HEIGHT,
235 	    NULL, draw_mask)
236 
237 CAIRO_TEST (radial_gradient_source,
238 	    "Simple test of radial gradients using the SOURCE operator",
239 	    "gradient,source", /* keywords */
240 	    NULL, /* requirements */
241 	    WIDTH, HEIGHT,
242 	    NULL, draw_source)
243 
244 CAIRO_TEST (radial_gradient_mask_source,
245 	    "Simple test of radial gradients using a MASK with a SOURCE operator",
246 	    "gradient,mask,source", /* keywords */
247 	    NULL, /* requirements */
248 	    WIDTH, HEIGHT,
249 	    NULL, draw_mask_source)
250 
251 CAIRO_TEST (radial_gradient_one_stop,
252 	    "Tests radial gradients with a single stop",
253 	    "gradient,radial", /* keywords */
254 	    NULL, /* requirements */
255 	    WIDTH, HEIGHT,
256 	    NULL, draw_one_stop)
257