2 * Copyright © 2005 Red Hat, Inc.
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.
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.
23 * Authors: Owen Taylor <otaylor@redhat.com>
24 * Kristian Høgsberg <krh@redhat.com>
27 #include "cairo-test.h"
35 static const char *png_filename = "romedalen.png";
36 static cairo_surface_t *image;
39 set_solid_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
41 cairo_set_source_rgb (cr, 0, 0, 0.6);
45 set_translucent_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
47 cairo_set_source_rgba (cr, 0, 0, 0.6, 0.5);
51 set_gradient_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
53 cairo_pattern_t *pattern;
56 cairo_pattern_create_linear (x, y, x + WIDTH, y + HEIGHT);
57 cairo_pattern_add_color_stop_rgba (pattern, 0, 1, 1, 1, 1);
58 cairo_pattern_add_color_stop_rgba (pattern, 1, 0, 0, 0.4, 1);
59 cairo_set_source (cr, pattern);
60 cairo_pattern_destroy (pattern);
64 set_image_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
66 cairo_pattern_t *pattern;
68 if (image == NULL || cairo_surface_status (image)) {
69 cairo_surface_destroy (image);
70 image = cairo_test_create_surface_from_png (ctx, png_filename);
73 pattern = cairo_pattern_create_for_surface (image);
74 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
75 cairo_set_source (cr, pattern);
76 cairo_pattern_destroy (pattern);
80 mask_polygon (cairo_t *cr, int x, int y)
82 cairo_surface_t *mask_surface;
85 mask_surface = cairo_surface_create_similar (cairo_get_group_target (cr),
88 cr2 = cairo_create (mask_surface);
89 cairo_surface_destroy (mask_surface);
92 cairo_set_operator (cr2, CAIRO_OPERATOR_CLEAR);
96 cairo_set_source_rgb (cr2, 1, 1, 1); /* white */
99 cairo_move_to (cr2, 0, 0);
100 cairo_line_to (cr2, 0, HEIGHT);
101 cairo_line_to (cr2, WIDTH / 2, 3 * HEIGHT / 4);
102 cairo_line_to (cr2, WIDTH, HEIGHT);
103 cairo_line_to (cr2, WIDTH, 0);
104 cairo_line_to (cr2, WIDTH / 2, HEIGHT / 4);
105 cairo_close_path (cr2);
108 cairo_mask_surface (cr, cairo_get_target (cr2), x, y);
113 mask_alpha (cairo_t *cr, int x, int y)
115 cairo_paint_with_alpha (cr, 0.75);
119 mask_gradient (cairo_t *cr, int x, int y)
121 cairo_pattern_t *pattern;
123 pattern = cairo_pattern_create_linear (x, y,
124 x + WIDTH, y + HEIGHT);
126 cairo_pattern_add_color_stop_rgba (pattern,
129 cairo_pattern_add_color_stop_rgba (pattern,
133 cairo_mask (cr, pattern);
135 cairo_pattern_destroy (pattern);
139 clip_none (cairo_t *cr, int x, int y)
144 clip_rects (cairo_t *cr, int x, int y)
146 int height = HEIGHT / 3;
149 cairo_rectangle (cr, x, y, WIDTH, height);
150 cairo_rectangle (cr, x, y + 2 * height, WIDTH, height);
155 clip_circle (cairo_t *cr, int x, int y)
158 cairo_arc (cr, x + WIDTH / 2, y + HEIGHT / 2,
159 WIDTH / 2, 0, 2 * M_PI);
164 static void (* const pattern_funcs[])(const cairo_test_context_t *ctx, cairo_t *cr, int x, int y) = {
166 set_translucent_pattern,
167 set_gradient_pattern,
171 static void (* const mask_funcs[])(cairo_t *cr, int x, int y) = {
177 static void (* const clip_funcs[])(cairo_t *cr, int x, int y) = {
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)
186 static cairo_test_status_t
187 draw (cairo_t *cr, int width, int height)
189 const cairo_test_context_t *ctx = cairo_test_get_context (cr);
190 cairo_surface_t *tmp_surface;
194 /* Some of our drawing is unbounded, so we draw each test to
195 * a temporary surface and copy over.
197 tmp_surface = cairo_surface_create_similar (cairo_get_group_target (cr),
198 CAIRO_CONTENT_COLOR_ALPHA,
199 IMAGE_WIDTH, IMAGE_HEIGHT);
200 cr2 = cairo_create (tmp_surface);
201 cairo_surface_destroy (tmp_surface);
203 for (k = 0; k < ARRAY_LENGTH (clip_funcs); k++) {
204 for (j = 0; j < ARRAY_LENGTH (mask_funcs); j++) {
205 for (i = 0; i < ARRAY_LENGTH (pattern_funcs); i++) {
206 int x = i * (WIDTH + PAD) + PAD;
207 int y = (ARRAY_LENGTH (mask_funcs) * k + j) * (HEIGHT + PAD) + PAD;
209 /* Clear intermediate surface we are going to be drawing onto */
211 cairo_set_operator (cr2, CAIRO_OPERATOR_CLEAR);
218 clip_funcs[k] (cr2, x, y);
219 pattern_funcs[i] (ctx, cr2, x, y);
220 mask_funcs[j] (cr2, x, y);
224 /* Copy back to the main pixmap */
225 cairo_set_source_surface (cr, cairo_get_target (cr2), 0, 0);
226 cairo_rectangle (cr, x, y, WIDTH, HEIGHT);
234 cairo_surface_destroy (image);
237 return CAIRO_TEST_SUCCESS;
241 "Tests of cairo_mask",
242 "mask", /* keywords */
243 NULL, /* requirements */
244 IMAGE_WIDTH, IMAGE_HEIGHT,