6e132fb2fcf72e20697b096373c5319adfb3cb16
[framework/graphics/cairo.git] / test / unbounded-operator.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  * Authors: Kristian Høgsberg <krh@redhat.com>
24  *          Owen Taylor <otaylor@redhat.com>
25  */
26
27 #include <math.h>
28 #include "cairo-test.h"
29 #include <stdio.h>
30
31 #define WIDTH 16
32 #define HEIGHT 16
33 #define PAD 2
34
35 static void
36 draw_mask (cairo_t *cr, int x, int y)
37 {
38     cairo_surface_t *mask_surface;
39     cairo_t *cr2;
40
41     double width = (int)(0.9 * WIDTH);
42     double height = (int)(0.9 * HEIGHT);
43     x += 0.05 * WIDTH;
44     y += 0.05 * HEIGHT;
45
46     mask_surface = cairo_surface_create_similar (cairo_get_group_target (cr),
47                                                  CAIRO_CONTENT_ALPHA,
48                                                  width, height);
49     cr2 = cairo_create (mask_surface);
50     cairo_surface_destroy (mask_surface);
51
52     cairo_save (cr2);
53     cairo_set_source_rgba (cr2, 0, 0, 0, 0); /* transparent */
54     cairo_set_operator (cr2, CAIRO_OPERATOR_SOURCE);
55     cairo_paint (cr2);
56     cairo_restore (cr2);
57
58     cairo_set_source_rgb (cr2, 1, 1, 1); /* white */
59
60     cairo_arc (cr2, 0.5 * width, 0.5 * height, 0.45 * height, 0, 2 * M_PI);
61     cairo_fill (cr2);
62
63     cairo_mask_surface (cr, cairo_get_target (cr2), x, y);
64     cairo_destroy (cr2);
65 }
66
67 static void
68 draw_glyphs (cairo_t *cr, int x, int y)
69 {
70     cairo_text_extents_t extents;
71
72     cairo_set_font_size (cr, 0.8 * HEIGHT);
73
74     cairo_text_extents (cr, "FG", &extents);
75     cairo_move_to (cr,
76                    x + floor ((WIDTH - extents.width) / 2 + 0.5) - extents.x_bearing,
77                    y + floor ((HEIGHT - extents.height) / 2 + 0.5) - extents.y_bearing);
78     cairo_show_text (cr, "FG");
79 }
80
81 static void
82 draw_polygon (cairo_t *cr, int x, int y)
83 {
84     double width = (int)(0.9 * WIDTH);
85     double height = (int)(0.9 * HEIGHT);
86     x += 0.05 * WIDTH;
87     y += 0.05 * HEIGHT;
88
89     cairo_new_path (cr);
90     cairo_move_to (cr, x, y);
91     cairo_line_to (cr, x, y + height);
92     cairo_line_to (cr, x + width / 2, y + 3 * height / 4);
93     cairo_line_to (cr, x + width, y + height);
94     cairo_line_to (cr, x + width, y);
95     cairo_line_to (cr, x + width / 2, y + height / 4);
96     cairo_close_path (cr);
97     cairo_fill (cr);
98 }
99
100 static void
101 draw_rects (cairo_t *cr, int x, int y)
102 {
103     double block_width = (int)(0.33 * WIDTH + 0.5);
104     double block_height = (int)(0.33 * HEIGHT + 0.5);
105     int i, j;
106
107     for (i = 0; i < 3; i++)
108         for (j = 0; j < 3; j++)
109             if ((i + j) % 2 == 0)
110                 cairo_rectangle (cr,
111                                  x + block_width * i, y + block_height * j,
112                                  block_width,         block_height);
113
114     cairo_fill (cr);
115 }
116
117 static void (*const draw_funcs[])(cairo_t *cr, int x, int y) = {
118     draw_mask,
119     draw_glyphs,
120     draw_polygon,
121     draw_rects
122 };
123
124 static cairo_operator_t operators[] = {
125     CAIRO_OPERATOR_IN, CAIRO_OPERATOR_OUT,
126     CAIRO_OPERATOR_DEST_IN, CAIRO_OPERATOR_DEST_ATOP
127 };
128
129 #define IMAGE_WIDTH (ARRAY_LENGTH (operators) * (WIDTH + PAD) + PAD)
130 #define IMAGE_HEIGHT (ARRAY_LENGTH (draw_funcs) * (HEIGHT + PAD) + PAD)
131
132 static cairo_test_status_t
133 draw (cairo_t *cr, int width, int height)
134 {
135     const cairo_test_context_t *ctx = cairo_test_get_context (cr);
136     size_t i, j, x, y;
137     cairo_pattern_t *pattern;
138
139     cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
140                             CAIRO_FONT_SLANT_NORMAL,
141                             CAIRO_FONT_WEIGHT_NORMAL);
142
143     for (j = 0; j < ARRAY_LENGTH (draw_funcs); j++) {
144         for (i = 0; i < ARRAY_LENGTH (operators); i++) {
145             x = i * (WIDTH + PAD) + PAD;
146             y = j * (HEIGHT + PAD) + PAD;
147
148             cairo_save (cr);
149
150             pattern = cairo_pattern_create_linear (x + WIDTH, y,
151                                                    x,         y + HEIGHT);
152             cairo_pattern_add_color_stop_rgba (pattern, 0.2,
153                                                0.0, 0.0, 1.0, 1.0); /* Solid blue */
154             cairo_pattern_add_color_stop_rgba (pattern, 0.8,
155                                                0.0, 0.0, 1.0, 0.0); /* Transparent blue */
156             cairo_set_source (cr, pattern);
157             cairo_pattern_destroy (pattern);
158
159             cairo_rectangle (cr, x, y, WIDTH, HEIGHT);
160             cairo_fill_preserve (cr);
161             cairo_clip (cr);
162
163             cairo_set_operator (cr, operators[i]);
164             cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
165
166             draw_funcs[j] (cr, x, y);
167             if (cairo_status (cr))
168                 cairo_test_log (ctx, "%d %d HERE!\n", (int)i, (int)j);
169
170             cairo_restore (cr);
171         }
172     }
173
174     if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
175         cairo_test_log (ctx, "%d %d .HERE!\n", (int)i, (int)j);
176
177     return CAIRO_TEST_SUCCESS;
178 }
179
180 CAIRO_TEST (unbounded_operator,
181             "Operators with an effect for transparent source/mask",
182             "operator", /* keywords */
183             NULL, /* requirements */
184             IMAGE_WIDTH, IMAGE_HEIGHT,
185             NULL, draw)