Upload Tizen2.0 source
[framework/graphics/cairo.git] / test / trap-clip.c
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  * Author: Kristian Høgsberg <krh@redhat.com>
24  */
25
26 #include <math.h>
27 #include "cairo-test.h"
28 #include <stdio.h>
29
30 #define WIDTH 16
31 #define HEIGHT 16
32 #define PAD 2
33
34 static const char *png_filename = "romedalen.png";
35 static cairo_surface_t *image;
36
37 static void
38 set_solid_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
39 {
40     cairo_set_source_rgb (cr, 0, 0, 0.6);
41 }
42
43 static void
44 set_translucent_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
45 {
46     cairo_set_source_rgba (cr, 0, 0, 0.6, 0.5);
47 }
48
49 static void
50 set_gradient_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
51 {
52     cairo_pattern_t *pattern;
53
54     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);
60 }
61
62 static void
63 set_image_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
64 {
65     cairo_pattern_t *pattern;
66
67     if (image == NULL || cairo_surface_status (image)) {
68         cairo_surface_destroy (image);
69         image = cairo_test_create_surface_from_png (ctx, png_filename);
70     }
71
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);
76 }
77
78 static void
79 draw_rect (cairo_t *cr, int x, int y)
80 {
81     cairo_new_path (cr);
82     cairo_rectangle (cr, x, y, WIDTH, HEIGHT);
83     cairo_fill (cr);
84 }
85
86 static void
87 draw_rects (cairo_t *cr, int x, int y)
88 {
89     int width = WIDTH / 3;
90     int height = HEIGHT / 2;
91
92     cairo_new_path (cr);
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);
96     cairo_fill (cr);
97 }
98
99 static void
100 draw_polygon (cairo_t *cr, int x, int y)
101 {
102     cairo_new_path (cr);
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);
110     cairo_fill (cr);
111 }
112
113 static void
114 clip_none (cairo_t *cr, int x, int y)
115 {
116 }
117
118 static void
119 clip_rect (cairo_t *cr, int x, int y)
120 {
121     cairo_new_path (cr);
122     cairo_rectangle (cr, x + (int)WIDTH / 6, y + (int)HEIGHT / 6,
123                      4 * ((int)WIDTH  / 6), 4 * ((int)WIDTH / 6));
124     cairo_clip (cr);
125     cairo_new_path (cr);
126 }
127
128 static void
129 clip_rects (cairo_t *cr, int x, int y)
130 {
131     int height = HEIGHT / 3;
132
133     cairo_new_path (cr);
134     cairo_rectangle (cr, x, y, WIDTH, height);
135     cairo_rectangle (cr, x, y + 2 * height, WIDTH, height);
136     cairo_clip (cr);
137     cairo_new_path (cr);
138 }
139
140 static void
141 clip_circle (cairo_t *cr, int x, int y)
142 {
143     cairo_new_path (cr);
144     cairo_arc (cr, x + WIDTH / 2, y + HEIGHT / 2,
145                WIDTH / 3, 0, 2 * M_PI);
146     cairo_clip (cr);
147     cairo_new_path (cr);
148 }
149
150 static void (* const pattern_funcs[])(const cairo_test_context_t *ctx, cairo_t *cr, int x, int y) = {
151     set_solid_pattern,
152     set_translucent_pattern,
153     set_gradient_pattern,
154     set_image_pattern,
155 };
156
157 static void (* const draw_funcs[])(cairo_t *cr, int x, int y) = {
158     draw_rect,
159     draw_rects,
160     draw_polygon,
161 };
162
163 static void (* const clip_funcs[])(cairo_t *cr, int x, int y) = {
164     clip_none,
165     clip_rect,
166     clip_rects,
167     clip_circle,
168 };
169
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)
172
173 static cairo_test_status_t
174 draw (cairo_t *cr, int width, int height)
175 {
176     const cairo_test_context_t *ctx = cairo_test_get_context (cr);
177     size_t i, j, k, x, y;
178
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;
184
185                 cairo_save (cr);
186
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);
193
194                 cairo_restore (cr);
195             }
196         }
197     }
198
199     if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
200         cairo_test_log (ctx, "%d %d .HERE!\n", (int)i, (int)j);
201
202     cairo_surface_destroy (image);
203     image = NULL;
204
205     return CAIRO_TEST_SUCCESS;
206 }
207
208 CAIRO_TEST (trap_clip,
209             "Trapezoid clipping",
210             "clip, trap", /* keywords */
211             NULL, /* requirements */
212             IMAGE_WIDTH, IMAGE_HEIGHT,
213             NULL, draw)