2 * Copyright © 2005, 2007 Red Hat, Inc.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 * Author: Carl D. Worth <cworth@cworth.org>
27 #include "cairo-test.h"
29 #define NUM_GRADIENTS 7
32 #define WIDTH (SIZE * NUM_GRADIENTS)
33 #define HEIGHT (SIZE * NUM_EXTEND)
35 typedef void (*composite_t)(cairo_t *cr, cairo_pattern_t *pattern);
36 typedef void (*add_stops_t)(cairo_pattern_t *pattern);
39 * We want to test all the possible relative positions of the start
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.
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).
51 * - If the two circles have the same size, neither can be inside or
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
58 * - Small start circle completely inside the end circle
59 * 0.25 -> 1.75; dr = 1.5 > 0; a = 1 - 1.50^2 < 0
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
64 * - Small start circle outside of the end circle
65 * 0.50 -> 1.00; dr = 0.5 > 0; a = 1 - 0.50^2 > 0
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
70 * - Small end circle outside of the start circle
71 * 1.00 -> 0.50; dr = -0.5 > 0; a = 1 - 0.50^2 > 0
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
76 * - Small end circle completely inside the start circle
77 * 1.75 -> 0.25; dr = -1.5 > 0; a = 1 - 1.50^2 < 0
81 static const double radiuses[NUM_GRADIENTS] = {
91 static cairo_pattern_t *
92 create_pattern (int index)
94 double x0, x1, radius0, radius1, left, right, center;
98 radius0 = radiuses[index];
99 radius1 = radiuses[NUM_GRADIENTS - index - 1];
101 /* center the gradient */
102 left = MIN (x0 - radius0, x1 - radius1);
103 right = MAX (x0 + radius0, x1 + radius1);
104 center = (left + right) * 0.5;
108 /* scale to make it fit within a 1x1 rect centered in (0,0) */
114 return cairo_pattern_create_radial (x0, 0, radius0, x1, 0, radius1);
118 pattern_add_stops (cairo_pattern_t *pattern)
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);
126 pattern_add_single_stop (cairo_pattern_t *pattern)
128 cairo_pattern_add_color_stop_rgba (pattern, 0.25, 1, 0, 0, 1);
132 static cairo_test_status_t
133 draw (cairo_t *cr, add_stops_t add_stops, composite_t composite)
136 cairo_extend_t extend[NUM_EXTEND] = {
139 CAIRO_EXTEND_REFLECT,
143 cairo_scale (cr, SIZE, SIZE);
144 cairo_translate (cr, 0.5, 0.5);
146 for (j = 0; j < NUM_EXTEND; j++) {
148 for (i = 0; i < NUM_GRADIENTS; i++) {
149 cairo_pattern_t *pattern;
151 pattern = create_pattern (i);
153 cairo_pattern_set_extend (pattern, extend[j]);
156 cairo_rectangle (cr, -0.5, -0.5, 1, 1);
158 composite (cr, pattern);
160 cairo_pattern_destroy (pattern);
162 cairo_translate (cr, 1, 0);
165 cairo_translate (cr, 0, 1);
168 return CAIRO_TEST_SUCCESS;
173 composite_simple (cairo_t *cr, cairo_pattern_t *pattern)
175 cairo_set_source (cr, pattern);
180 composite_mask (cairo_t *cr, cairo_pattern_t *pattern)
182 cairo_set_source_rgb (cr, 1, 0, 1);
183 cairo_mask (cr, pattern);
187 static cairo_test_status_t
188 draw_simple (cairo_t *cr, int width, int height)
190 cairo_test_paint_checkered (cr);
191 return draw (cr, pattern_add_stops, composite_simple);
194 static cairo_test_status_t
195 draw_mask (cairo_t *cr, int width, int height)
197 cairo_test_paint_checkered (cr);
198 return draw (cr, pattern_add_stops, composite_mask);
201 static cairo_test_status_t
202 draw_source (cairo_t *cr, int width, int height)
204 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
205 return draw (cr, pattern_add_stops, composite_simple);
209 static cairo_test_status_t
210 draw_mask_source (cairo_t *cr, int width, int height)
212 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
213 return draw (cr, pattern_add_stops, composite_mask);
216 static cairo_test_status_t
217 draw_one_stop (cairo_t *cr, int width, int height)
219 cairo_test_paint_checkered (cr);
220 return draw (cr, pattern_add_single_stop, composite_simple);
223 CAIRO_TEST (radial_gradient,
224 "Simple test of radial gradients",
225 "gradient", /* keywords */
226 NULL, /* requirements */
230 CAIRO_TEST (radial_gradient_mask,
231 "Simple test of radial gradients using a MASK",
232 "gradient,mask", /* keywords */
233 NULL, /* requirements */
237 CAIRO_TEST (radial_gradient_source,
238 "Simple test of radial gradients using the SOURCE operator",
239 "gradient,source", /* keywords */
240 NULL, /* requirements */
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 */
249 NULL, draw_mask_source)
251 CAIRO_TEST (radial_gradient_one_stop,
252 "Tests radial gradients with a single stop",
253 "gradient,radial", /* keywords */
254 NULL, /* requirements */