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 * Author: Kristian Høgsberg <krh@redhat.com>
27 #include "cairo-test.h"
34 static const char *png_filename = "romedalen.png";
35 static cairo_surface_t *image;
38 set_solid_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
40 cairo_set_source_rgb (cr, 0, 0, 0.6);
44 set_translucent_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
46 cairo_set_source_rgba (cr, 0, 0, 0.6, 0.5);
50 set_gradient_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
52 cairo_pattern_t *pattern;
55 cairo_pattern_create_linear (x, y, x + WIDTH, y + HEIGHT);
56 cairo_pattern_add_color_stop_rgba (pattern, 0, 1, 1, 1, 1);
57 cairo_pattern_add_color_stop_rgba (pattern, 1, 0, 0, 0.4, 1);
58 cairo_set_source (cr, pattern);
59 cairo_pattern_destroy (pattern);
63 set_image_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
65 cairo_pattern_t *pattern;
67 if (image == NULL || cairo_surface_status (image)) {
68 cairo_surface_destroy (image);
69 image = cairo_test_create_surface_from_png (ctx, png_filename);
72 pattern = cairo_pattern_create_for_surface (image);
73 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
74 cairo_set_source (cr, pattern);
75 cairo_pattern_destroy (pattern);
79 draw_rect (cairo_t *cr, int x, int y)
82 cairo_rectangle (cr, x, y, WIDTH, HEIGHT);
87 draw_rects (cairo_t *cr, int x, int y)
89 int width = WIDTH / 3;
90 int height = HEIGHT / 2;
93 cairo_rectangle (cr, x, y, width, height);
94 cairo_rectangle (cr, x + width, y + height, width, height);
95 cairo_rectangle (cr, x + 2 * width, y, width, height);
100 draw_polygon (cairo_t *cr, int x, int y)
103 cairo_move_to (cr, x, y);
104 cairo_line_to (cr, x, y + HEIGHT);
105 cairo_line_to (cr, x + WIDTH / 2, y + 3 * HEIGHT / 4);
106 cairo_line_to (cr, x + WIDTH, y + HEIGHT);
107 cairo_line_to (cr, x + WIDTH, y);
108 cairo_line_to (cr, x + WIDTH / 2, y + HEIGHT / 4);
109 cairo_close_path (cr);
114 clip_none (cairo_t *cr, int x, int y)
119 clip_rect (cairo_t *cr, int x, int y)
122 cairo_rectangle (cr, x + (int)WIDTH / 6, y + (int)HEIGHT / 6,
123 4 * ((int)WIDTH / 6), 4 * ((int)WIDTH / 6));
129 clip_rects (cairo_t *cr, int x, int y)
131 int height = HEIGHT / 3;
134 cairo_rectangle (cr, x, y, WIDTH, height);
135 cairo_rectangle (cr, x, y + 2 * height, WIDTH, height);
141 clip_circle (cairo_t *cr, int x, int y)
144 cairo_arc (cr, x + WIDTH / 2, y + HEIGHT / 2,
145 WIDTH / 3, 0, 2 * M_PI);
150 static void (* const pattern_funcs[])(const cairo_test_context_t *ctx, cairo_t *cr, int x, int y) = {
152 set_translucent_pattern,
153 set_gradient_pattern,
157 static void (* const draw_funcs[])(cairo_t *cr, int x, int y) = {
163 static void (* const clip_funcs[])(cairo_t *cr, int x, int y) = {
170 #define IMAGE_WIDTH (ARRAY_LENGTH (pattern_funcs) * (WIDTH + PAD) + PAD)
171 #define IMAGE_HEIGHT (ARRAY_LENGTH (draw_funcs) * ARRAY_LENGTH (clip_funcs) * (HEIGHT + PAD) + PAD)
173 static cairo_test_status_t
174 draw (cairo_t *cr, int width, int height)
176 const cairo_test_context_t *ctx = cairo_test_get_context (cr);
177 size_t i, j, k, x, y;
179 for (k = 0; k < ARRAY_LENGTH (clip_funcs); k++) {
180 for (j = 0; j < ARRAY_LENGTH (draw_funcs); j++) {
181 for (i = 0; i < ARRAY_LENGTH (pattern_funcs); i++) {
182 x = i * (WIDTH + PAD) + PAD;
183 y = (ARRAY_LENGTH (draw_funcs) * k + j) * (HEIGHT + PAD) + PAD;
187 cairo_move_to (cr, x, y);
188 clip_funcs[k] (cr, x, y);
189 pattern_funcs[i] (ctx, cr, x, y);
190 draw_funcs[j] (cr, x, y);
191 if (cairo_status (cr))
192 cairo_test_log (ctx, "%d %d HERE!\n", (int)i, (int)j);
199 if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
200 cairo_test_log (ctx, "%d %d .HERE!\n", (int)i, (int)j);
202 cairo_surface_destroy (image);
205 return CAIRO_TEST_SUCCESS;
208 CAIRO_TEST (trap_clip,
209 "Trapezoid clipping",
210 "clip, trap", /* keywords */
211 NULL, /* requirements */
212 IMAGE_WIDTH, IMAGE_HEIGHT,