1
/*
2
 * Copyright © 2005 Red Hat, Inc.
3
 *
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
 * Red Hat, Inc. not be used in advertising or publicity pertaining to
10
 * distribution of the software without specific, written prior
11
 * permission. Red Hat, 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.
14
 *
15
 * RED HAT, 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.
22
 *
23
 * Authors: Kristian Høgsberg <krh@redhat.com>
24
 *          Owen Taylor <otaylor@redhat.com>
25
 *          Uli Schlachter <psychon@znc.in>
26
 */
27

            
28
#include "cairo-test.h"
29
#include <math.h>
30
#include <stdio.h>
31

            
32
#define WIDTH 16
33
#define HEIGHT 16
34
#define PAD 2
35

            
36
static void
37
15
set_solid_pattern (cairo_t *cr, int x, int y)
38
{
39
15
    cairo_set_source_rgb (cr, 1.0, 0, 0.0);
40
15
}
41

            
42
static void
43
15
set_translucent_pattern (cairo_t *cr, int x, int y)
44
{
45
15
    cairo_set_source_rgba (cr, 1, 0, 0, 0.5);
46
15
}
47

            
48
static void
49
15
set_gradient_pattern (cairo_t *cr, int x, int y)
50
{
51
    cairo_pattern_t *pattern;
52

            
53
15
    pattern = cairo_pattern_create_linear (x, y, x + WIDTH, y + HEIGHT);
54
15
    cairo_pattern_add_color_stop_rgba (pattern, 0.2, 1, 0, 0, 1);
55
15
    cairo_pattern_add_color_stop_rgba (pattern, 0.8, 1, 0, 0, 0.0);
56
15
    cairo_set_source (cr, pattern);
57
15
    cairo_pattern_destroy (pattern);
58
15
}
59

            
60
static void
61
15
set_surface_pattern (cairo_t *cr, int x, int y)
62
{
63
    cairo_surface_t *source_surface;
64
    cairo_t *cr2;
65

            
66
15
    double width = (int)(0.6 * WIDTH);
67
15
    double height = (int)(0.6 * HEIGHT);
68
15
    x += 0.2 * WIDTH;
69
15
    y += 0.2 * HEIGHT;
70

            
71
15
    source_surface = cairo_surface_create_similar (cairo_get_group_target (cr),
72
						   CAIRO_CONTENT_COLOR_ALPHA,
73
						   width, height);
74
15
    cr2 = cairo_create (source_surface);
75
15
    cairo_surface_destroy (source_surface);
76

            
77
15
    cairo_set_source_rgb (cr2, 1, 0, 0); /* red */
78
15
    cairo_paint (cr2);
79

            
80
15
    cairo_set_source_rgb (cr2, 1, 1, 1); /* white */
81

            
82
15
    cairo_arc (cr2, 0.5 * width, 0.5 * height, 0.5 * height, 0, 2 * M_PI);
83
15
    cairo_fill (cr2);
84

            
85
15
    cairo_set_source_surface (cr, cairo_get_target (cr2), x, y);
86
15
    cairo_destroy (cr2);
87
15
}
88

            
89
static void
90
12
draw_mask (cairo_t *cr, int x, int y)
91
{
92
    cairo_surface_t *mask_surface;
93
    cairo_t *cr2;
94

            
95
12
    double width = (int)(0.9 * WIDTH);
96
12
    double height = (int)(0.9 * HEIGHT);
97
12
    x += 0.05 * WIDTH;
98
12
    y += 0.05 * HEIGHT;
99

            
100
12
    mask_surface = cairo_surface_create_similar (cairo_get_group_target (cr),
101
						 CAIRO_CONTENT_ALPHA,
102
						 width, height);
103
12
    cr2 = cairo_create (mask_surface);
104
12
    cairo_surface_destroy (mask_surface);
105

            
106
12
    cairo_set_source_rgb (cr2, 1, 1, 1); /* white */
107

            
108
12
    cairo_arc (cr2, 0.5 * width, 0.5 * height, 0.45 * height, 0, 2 * M_PI);
109
12
    cairo_fill (cr2);
110

            
111
12
    cairo_mask_surface (cr, cairo_get_target (cr2), x, y);
112
12
    cairo_destroy (cr2);
113
12
}
114

            
115
static void
116
12
draw_glyphs (cairo_t *cr, int x, int y)
117
{
118
    cairo_text_extents_t extents;
119

            
120
12
    cairo_set_font_size (cr, 0.8 * HEIGHT);
121

            
122
12
    cairo_text_extents (cr, "FG", &extents);
123
12
    cairo_move_to (cr,
124
12
		   x + floor ((WIDTH - extents.width) / 2 + 0.5) - extents.x_bearing,
125
12
		   y + floor ((HEIGHT - extents.height) / 2 + 0.5) - extents.y_bearing);
126
12
    cairo_show_text (cr, "FG");
127
12
}
128

            
129
static void
130
12
draw_polygon (cairo_t *cr, int x, int y)
131
{
132
12
    double width = (int)(0.9 * WIDTH);
133
12
    double height = (int)(0.9 * HEIGHT);
134
12
    x += 0.05 * WIDTH;
135
12
    y += 0.05 * HEIGHT;
136

            
137
12
    cairo_new_path (cr);
138
12
    cairo_move_to (cr, x, y);
139
12
    cairo_line_to (cr, x, y + height);
140
12
    cairo_line_to (cr, x + width / 2, y + 3 * height / 4);
141
12
    cairo_line_to (cr, x + width, y + height);
142
12
    cairo_line_to (cr, x + width, y);
143
12
    cairo_line_to (cr, x + width / 2, y + height / 4);
144
12
    cairo_close_path (cr);
145
12
    cairo_fill (cr);
146
12
}
147

            
148
static void
149
24
draw_rects (cairo_t *cr, int x, int y, double offset)
150
{
151
24
    double block_width = (int)(0.33 * WIDTH + 0.5) - offset/3;
152
24
    double block_height = (int)(0.33 * HEIGHT + 0.5) - offset/3;
153
    int i, j;
154

            
155
24
    x += offset/2;
156
24
    y += offset/2;
157

            
158
96
    for (i = 0; i < 3; i++)
159
288
	for (j = 0; j < 3; j++)
160
216
	    if ((i + j) % 2 == 0)
161
120
		cairo_rectangle (cr,
162
120
				 x + block_width * i, y + block_height * j,
163
				 block_width,         block_height);
164

            
165
24
    cairo_fill (cr);
166
24
}
167

            
168
static void
169
12
draw_aligned_rects (cairo_t *cr, int x, int y)
170
{
171
12
    draw_rects (cr, x, y, 0);
172
12
}
173

            
174
static void
175
12
draw_unaligned_rects (cairo_t *cr, int x, int y)
176
{
177
12
    draw_rects (cr, x, y, 2.1);
178
12
}
179

            
180
static void (* const pattern_funcs[])(cairo_t *cr, int x, int y) = {
181
    set_solid_pattern,
182
    set_translucent_pattern,
183
    set_gradient_pattern,
184
    set_surface_pattern,
185
};
186

            
187
static void (* const draw_funcs[])(cairo_t *cr, int x, int y) = {
188
    draw_mask,
189
    draw_glyphs,
190
    draw_polygon,
191
    draw_aligned_rects,
192
    draw_unaligned_rects
193
};
194

            
195
#define IMAGE_WIDTH (ARRAY_LENGTH (pattern_funcs) * (WIDTH + PAD) + PAD)
196
#define IMAGE_HEIGHT (ARRAY_LENGTH (draw_funcs) * (HEIGHT + PAD) + PAD)
197

            
198
static cairo_test_status_t
199
3
draw (cairo_t *cr, int width, int height)
200
{
201
3
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
202
    size_t i, j, x, y;
203
    cairo_pattern_t *pattern;
204

            
205
3
    cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
206
			    CAIRO_FONT_SLANT_NORMAL,
207
			    CAIRO_FONT_WEIGHT_NORMAL);
208

            
209
18
    for (j = 0; j < ARRAY_LENGTH (draw_funcs); j++) {
210
75
	for (i = 0; i < ARRAY_LENGTH (pattern_funcs); i++) {
211
60
	    x = i * (WIDTH + PAD) + PAD;
212
60
	    y = j * (HEIGHT + PAD) + PAD;
213

            
214
60
	    cairo_save (cr);
215

            
216
60
	    pattern = cairo_pattern_create_linear (x + WIDTH, y,
217
60
						   x,         y + HEIGHT);
218
60
	    cairo_pattern_add_color_stop_rgba (pattern, 0.2,
219
					       0.0, 0.0, 1.0, 1.0); /* Solid blue */
220
60
	    cairo_pattern_add_color_stop_rgba (pattern, 0.8,
221
					       0.0, 0.0, 1.0, 0.0); /* Transparent blue */
222
60
	    cairo_set_source (cr, pattern);
223
60
	    cairo_pattern_destroy (pattern);
224

            
225
60
	    cairo_rectangle (cr, x, y, WIDTH, HEIGHT);
226
60
	    cairo_fill_preserve (cr);
227
60
	    cairo_clip (cr);
228

            
229
60
	    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
230
60
	    pattern_funcs[i] (cr, x, y);
231
60
	    draw_funcs[j] (cr, x, y);
232
60
	    if (cairo_status (cr))
233
		cairo_test_log (ctx, "%d %d HERE!\n", (int)i, (int)j);
234

            
235
60
	    cairo_restore (cr);
236
	}
237
    }
238

            
239
3
    if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
240
	cairo_test_log (ctx, "%d %d .HERE!\n", (int)i, (int)j);
241

            
242
3
    return CAIRO_TEST_SUCCESS;
243
}
244

            
245
1
CAIRO_TEST (operator_source,
246
	    "Test of CAIRO_OPERATOR_SOURCE",
247
	    "operator", /* keywords */
248
	    NULL, /* requirements */
249
	    IMAGE_WIDTH, IMAGE_HEIGHT,
250
	    NULL, draw)