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: Owen Taylor <otaylor@redhat.com>
24
 *          Kristian Høgsberg <krh@redhat.com>
25
 */
26

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

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

            
35
static const char *png_filename = "romedalen.png";
36
static cairo_surface_t *image;
37

            
38
static void
39
27
set_solid_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
40
{
41
27
    cairo_set_source_rgb (cr, 0, 0, 0.6);
42
27
}
43

            
44
static void
45
27
set_translucent_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
46
{
47
27
    cairo_set_source_rgba (cr, 0, 0, 0.6, 0.5);
48
27
}
49

            
50
static void
51
27
set_gradient_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
52
{
53
    cairo_pattern_t *pattern;
54

            
55
    pattern =
56
27
	cairo_pattern_create_linear (x, y, x + WIDTH, y + HEIGHT);
57
27
    cairo_pattern_add_color_stop_rgba (pattern, 0, 1, 1, 1, 1);
58
27
    cairo_pattern_add_color_stop_rgba (pattern, 1, 0, 0, 0.4, 1);
59
27
    cairo_set_source (cr, pattern);
60
27
    cairo_pattern_destroy (pattern);
61
27
}
62

            
63
static void
64
27
set_image_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
65
{
66
    cairo_pattern_t *pattern;
67

            
68
27
    if (image == NULL || cairo_surface_status (image)) {
69
3
	cairo_surface_destroy (image);
70
3
	image = cairo_test_create_surface_from_png (ctx, png_filename);
71
    }
72

            
73
27
    pattern = cairo_pattern_create_for_surface (image);
74
27
    cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
75
27
    cairo_set_source (cr, pattern);
76
27
    cairo_pattern_destroy (pattern);
77
27
}
78

            
79
static void
80
36
mask_polygon (cairo_t *cr, int x, int y)
81
{
82
    cairo_surface_t *mask_surface;
83
    cairo_t *cr2;
84

            
85
36
    mask_surface = cairo_surface_create_similar (cairo_get_group_target (cr),
86
						 CAIRO_CONTENT_ALPHA,
87
						 WIDTH, HEIGHT);
88
36
    cr2 = cairo_create (mask_surface);
89
36
    cairo_surface_destroy (mask_surface);
90

            
91
36
    cairo_save (cr2);
92
36
    cairo_set_operator (cr2, CAIRO_OPERATOR_CLEAR);
93
36
    cairo_paint (cr2);
94
36
    cairo_restore (cr2);
95

            
96
36
    cairo_set_source_rgb (cr2, 1, 1, 1); /* white */
97

            
98
36
    cairo_new_path (cr2);
99
36
    cairo_move_to (cr2, 0, 0);
100
36
    cairo_line_to (cr2, 0, HEIGHT);
101
36
    cairo_line_to (cr2, WIDTH / 2, 3 * HEIGHT / 4);
102
36
    cairo_line_to (cr2, WIDTH, HEIGHT);
103
36
    cairo_line_to (cr2, WIDTH, 0);
104
36
    cairo_line_to (cr2, WIDTH / 2, HEIGHT / 4);
105
36
    cairo_close_path (cr2);
106
36
    cairo_fill (cr2);
107

            
108
36
    cairo_mask_surface (cr, cairo_get_target (cr2), x, y);
109
36
    cairo_destroy (cr2);
110
36
}
111

            
112
static void
113
36
mask_alpha (cairo_t *cr, int x, int y)
114
{
115
36
    cairo_paint_with_alpha (cr, 0.75);
116
36
}
117

            
118
static void
119
36
mask_gradient (cairo_t *cr, int x, int y)
120
{
121
    cairo_pattern_t *pattern;
122

            
123
36
    pattern = cairo_pattern_create_linear (x, y,
124
36
					   x + WIDTH, y + HEIGHT);
125

            
126
36
    cairo_pattern_add_color_stop_rgba (pattern,
127
				       0,
128
				       1, 1, 1, 1);
129
36
    cairo_pattern_add_color_stop_rgba (pattern,
130
				       1,
131
				       1, 1, 1, 0);
132

            
133
36
    cairo_mask (cr, pattern);
134

            
135
36
    cairo_pattern_destroy (pattern);
136
36
}
137

            
138
static void
139
36
clip_none (cairo_t *cr, int x, int y)
140
{
141
36
}
142

            
143
static void
144
36
clip_rects (cairo_t *cr, int x, int y)
145
{
146
36
    int height = HEIGHT / 3;
147

            
148
36
    cairo_new_path (cr);
149
36
    cairo_rectangle (cr, x, y, WIDTH, height);
150
36
    cairo_rectangle (cr, x, y + 2 * height, WIDTH, height);
151
36
    cairo_clip (cr);
152
36
}
153

            
154
static void
155
36
clip_circle (cairo_t *cr, int x, int y)
156
{
157
36
    cairo_new_path (cr);
158
36
    cairo_arc (cr, x + WIDTH / 2, y + HEIGHT / 2,
159
	       WIDTH / 2, 0, 2 * M_PI);
160
36
    cairo_clip (cr);
161
36
    cairo_new_path (cr);
162
36
}
163

            
164
static void (* const pattern_funcs[])(const cairo_test_context_t *ctx, cairo_t *cr, int x, int y) = {
165
    set_solid_pattern,
166
    set_translucent_pattern,
167
    set_gradient_pattern,
168
    set_image_pattern,
169
};
170

            
171
static void (* const mask_funcs[])(cairo_t *cr, int x, int y) = {
172
    mask_alpha,
173
    mask_gradient,
174
    mask_polygon,
175
};
176

            
177
static void (* const clip_funcs[])(cairo_t *cr, int x, int y) = {
178
    clip_none,
179
    clip_rects,
180
    clip_circle,
181
};
182

            
183
#define IMAGE_WIDTH (ARRAY_LENGTH (pattern_funcs) * (WIDTH + PAD) + PAD)
184
#define IMAGE_HEIGHT (ARRAY_LENGTH (mask_funcs) * ARRAY_LENGTH (clip_funcs) * (HEIGHT + PAD) + PAD)
185

            
186
static cairo_test_status_t
187
3
draw (cairo_t *cr, int width, int height)
188
{
189
3
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
190
    cairo_surface_t *tmp_surface;
191
    size_t i, j, k;
192
    cairo_t *cr2;
193

            
194
    /* Some of our drawing is unbounded, so we draw each test to
195
     * a temporary surface and copy over.
196
     */
197
3
    tmp_surface = cairo_surface_create_similar (cairo_get_group_target (cr),
198
						CAIRO_CONTENT_COLOR_ALPHA,
199
						IMAGE_WIDTH, IMAGE_HEIGHT);
200
3
    cr2 = cairo_create (tmp_surface);
201
3
    cairo_surface_destroy (tmp_surface);
202

            
203
12
    for (k = 0; k < ARRAY_LENGTH (clip_funcs); k++) {
204
36
	for (j = 0; j < ARRAY_LENGTH (mask_funcs); j++) {
205
135
	    for (i = 0; i < ARRAY_LENGTH (pattern_funcs); i++) {
206
108
		int x = i * (WIDTH + PAD) + PAD;
207
108
		int y = (ARRAY_LENGTH (mask_funcs) * k + j) * (HEIGHT + PAD) + PAD;
208

            
209
		/* Clear intermediate surface we are going to be drawing onto */
210
108
		cairo_save (cr2);
211
108
		cairo_set_operator (cr2, CAIRO_OPERATOR_CLEAR);
212
108
		cairo_paint (cr2);
213
108
		cairo_restore (cr2);
214

            
215
		/* draw */
216
108
		cairo_save (cr2);
217

            
218
108
		clip_funcs[k] (cr2, x, y);
219
108
		pattern_funcs[i] (ctx, cr2, x, y);
220
108
		mask_funcs[j] (cr2, x, y);
221

            
222
108
		cairo_restore (cr2);
223

            
224
		/* Copy back to the main pixmap */
225
108
		cairo_set_source_surface (cr, cairo_get_target (cr2), 0, 0);
226
108
		cairo_rectangle (cr, x, y, WIDTH, HEIGHT);
227
108
		cairo_fill (cr);
228
	    }
229
	}
230
    }
231

            
232
3
    cairo_destroy (cr2);
233

            
234
3
    cairo_surface_destroy (image);
235
3
    image = NULL;
236

            
237
3
    return CAIRO_TEST_SUCCESS;
238
}
239

            
240
1
CAIRO_TEST (mask,
241
	    "Tests of cairo_mask",
242
	    "mask", /* keywords */
243
	    NULL, /* requirements */
244
	    IMAGE_WIDTH, IMAGE_HEIGHT,
245
	    NULL, draw)
246