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 *
92
420
create_pattern (int index)
93
{
94
    double x0, x1, radius0, radius1, left, right, center;
95

            
96
420
    x0 = 0;
97
420
    x1 = 1;
98
420
    radius0 = radiuses[index];
99
420
    radius1 = radiuses[NUM_GRADIENTS - index - 1];
100

            
101
    /* center the gradient */
102
420
    left = MIN (x0 - radius0, x1 - radius1);
103
420
    right = MAX (x0 + radius0, x1 + radius1);
104
420
    center = (left + right) * 0.5;
105
420
    x0 -= center;
106
420
    x1 -= center;
107

            
108
    /* scale to make it fit within a 1x1 rect centered in (0,0) */
109
420
    x0 *= 0.25;
110
420
    x1 *= 0.25;
111
420
    radius0 *= 0.25;
112
420
    radius1 *= 0.25;
113

            
114
420
    return cairo_pattern_create_radial (x0, 0, radius0, x1, 0, radius1);
115
}
116

            
117
static void
118
336
pattern_add_stops (cairo_pattern_t *pattern)
119
{
120
336
    cairo_pattern_add_color_stop_rgba (pattern, 0.0,        1, 0, 0, 0.75);
121
336
    cairo_pattern_add_color_stop_rgba (pattern, sqrt (0.5), 0, 1, 0, 0);
122
336
    cairo_pattern_add_color_stop_rgba (pattern, 1.0,        0, 0, 1, 1);
123
336
}
124

            
125
static void
126
84
pattern_add_single_stop (cairo_pattern_t *pattern)
127
{
128
84
    cairo_pattern_add_color_stop_rgba (pattern, 0.25, 1, 0, 0, 1);
129
84
}
130

            
131

            
132
static cairo_test_status_t
133
15
draw (cairo_t *cr, add_stops_t add_stops, composite_t composite)
134
{
135
    int i, j;
136
15
    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
15
    cairo_scale (cr, SIZE, SIZE);
144
15
    cairo_translate (cr, 0.5, 0.5);
145

            
146
75
    for (j = 0; j < NUM_EXTEND; j++) {
147
60
	cairo_save (cr);
148
480
	for (i = 0; i < NUM_GRADIENTS; i++) {
149
	    cairo_pattern_t *pattern;
150

            
151
420
	    pattern = create_pattern (i);
152
420
	    add_stops (pattern);
153
420
	    cairo_pattern_set_extend (pattern, extend[j]);
154

            
155
420
	    cairo_save (cr);
156
420
	    cairo_rectangle (cr, -0.5, -0.5, 1, 1);
157
420
	    cairo_clip (cr);
158
420
	    composite (cr, pattern);
159
420
	    cairo_restore (cr);
160
420
	    cairo_pattern_destroy (pattern);
161

            
162
420
	    cairo_translate (cr, 1, 0);
163
	}
164
60
	cairo_restore (cr);
165
60
	cairo_translate (cr, 0, 1);
166
    }
167

            
168
15
    return CAIRO_TEST_SUCCESS;
169
}
170

            
171

            
172
static void
173
252
composite_simple (cairo_t *cr, cairo_pattern_t *pattern)
174
{
175
252
    cairo_set_source (cr, pattern);
176
252
    cairo_paint (cr);
177
252
}
178

            
179
static void
180
168
composite_mask (cairo_t *cr, cairo_pattern_t *pattern)
181
{
182
168
    cairo_set_source_rgb (cr, 1, 0, 1);
183
168
    cairo_mask (cr, pattern);
184
168
}
185

            
186

            
187
static cairo_test_status_t
188
3
draw_simple (cairo_t *cr, int width, int height)
189
{
190
3
    cairo_test_paint_checkered (cr);
191
3
    return draw (cr, pattern_add_stops, composite_simple);
192
}
193

            
194
static cairo_test_status_t
195
3
draw_mask (cairo_t *cr, int width, int height)
196
{
197
3
    cairo_test_paint_checkered (cr);
198
3
    return draw (cr, pattern_add_stops, composite_mask);
199
}
200

            
201
static cairo_test_status_t
202
3
draw_source (cairo_t *cr, int width, int height)
203
{
204
3
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
205
3
    return draw (cr, pattern_add_stops, composite_simple);
206
}
207

            
208

            
209
static cairo_test_status_t
210
3
draw_mask_source (cairo_t *cr, int width, int height)
211
{
212
3
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
213
3
    return draw (cr, pattern_add_stops, composite_mask);
214
}
215

            
216
static cairo_test_status_t
217
3
draw_one_stop (cairo_t *cr, int width, int height)
218
{
219
3
    cairo_test_paint_checkered (cr);
220
3
    return draw (cr, pattern_add_single_stop, composite_simple);
221
}
222

            
223
1
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
1
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
1
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
1
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
1
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)