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 "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 void
set_solid_pattern(cairo_t * cr,int x,int y)36 set_solid_pattern (cairo_t *cr, int x, int y)
37 {
38 cairo_set_source_rgb (cr, 1.0, 0, 0.0);
39 }
40
41 static void
set_gradient_pattern(cairo_t * cr,int x,int y)42 set_gradient_pattern (cairo_t *cr, int x, int y)
43 {
44 cairo_pattern_t *pattern;
45
46 pattern = cairo_pattern_create_linear (x, y, x + WIDTH, y + HEIGHT);
47 cairo_pattern_add_color_stop_rgba (pattern, 0.2, 1, 0, 0, 1);
48 cairo_pattern_add_color_stop_rgba (pattern, 0.8, 1, 0, 0, 0.0);
49 cairo_set_source (cr, pattern);
50 cairo_pattern_destroy (pattern);
51 }
52
53 static void
draw_mask(cairo_t * cr,int x,int y)54 draw_mask (cairo_t *cr, int x, int y)
55 {
56 cairo_surface_t *mask_surface;
57 cairo_t *cr2;
58
59 double width = (int)(0.9 * WIDTH);
60 double height = (int)(0.9 * HEIGHT);
61 x += 0.05 * WIDTH;
62 y += 0.05 * HEIGHT;
63
64 mask_surface = cairo_surface_create_similar (cairo_get_group_target (cr),
65 CAIRO_CONTENT_ALPHA,
66 width, height);
67 cr2 = cairo_create (mask_surface);
68 cairo_surface_destroy (mask_surface);
69
70 cairo_set_source_rgb (cr2, 1, 1, 1); /* white */
71
72 cairo_arc (cr2, 0.5 * width, 0.5 * height, 0.45 * height, 0, 2 * M_PI);
73 cairo_fill (cr2);
74
75 cairo_mask_surface (cr, cairo_get_target (cr2), x, y);
76 cairo_destroy (cr2);
77 }
78
79 static void
draw_glyphs(cairo_t * cr,int x,int y)80 draw_glyphs (cairo_t *cr, int x, int y)
81 {
82 cairo_text_extents_t extents;
83
84 cairo_set_font_size (cr, 0.8 * HEIGHT);
85
86 cairo_text_extents (cr, "FG", &extents);
87 cairo_move_to (cr,
88 x + floor ((WIDTH - extents.width) / 2 + 0.5) - extents.x_bearing,
89 y + floor ((HEIGHT - extents.height) / 2 + 0.5) - extents.y_bearing);
90 cairo_show_text (cr, "FG");
91 }
92
93 static void
draw_polygon(cairo_t * cr,int x,int y)94 draw_polygon (cairo_t *cr, int x, int y)
95 {
96 double width = (int)(0.9 * WIDTH);
97 double height = (int)(0.9 * HEIGHT);
98 x += 0.05 * WIDTH;
99 y += 0.05 * HEIGHT;
100
101 cairo_new_path (cr);
102 cairo_move_to (cr, x, y);
103 cairo_line_to (cr, x, y + height);
104 cairo_line_to (cr, x + width / 2, y + 3 * height / 4);
105 cairo_line_to (cr, x + width, y + height);
106 cairo_line_to (cr, x + width, y);
107 cairo_line_to (cr, x + width / 2, y + height / 4);
108 cairo_close_path (cr);
109 cairo_fill (cr);
110 }
111
112 static void
draw_rects(cairo_t * cr,int x,int y)113 draw_rects (cairo_t *cr, int x, int y)
114 {
115 double block_width = (int)(0.33 * WIDTH + 0.5);
116 double block_height = (int)(0.33 * HEIGHT + 0.5);
117 int i, j;
118
119 for (i = 0; i < 3; i++)
120 for (j = 0; j < 3; j++)
121 if ((i + j) % 2 == 0)
122 cairo_rectangle (cr,
123 x + block_width * i, y + block_height * j,
124 block_width, block_height);
125
126 cairo_fill (cr);
127 }
128
129 static void (* const pattern_funcs[])(cairo_t *cr, int x, int y) = {
130 set_solid_pattern,
131 set_gradient_pattern,
132 };
133
134 static void (* const draw_funcs[])(cairo_t *cr, int x, int y) = {
135 draw_mask,
136 draw_glyphs,
137 draw_polygon,
138 draw_rects
139 };
140
141 #define IMAGE_WIDTH (ARRAY_LENGTH (pattern_funcs) * (WIDTH + PAD) + PAD)
142 #define IMAGE_HEIGHT (ARRAY_LENGTH (draw_funcs) * (HEIGHT + PAD) + PAD)
143
144 static cairo_test_status_t
draw(cairo_t * cr,int width,int height)145 draw (cairo_t *cr, int width, int height)
146 {
147 const cairo_test_context_t *ctx = cairo_test_get_context (cr);
148 size_t i, j, x, y;
149 cairo_pattern_t *pattern;
150
151 cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
152 CAIRO_FONT_SLANT_NORMAL,
153 CAIRO_FONT_WEIGHT_NORMAL);
154
155 for (j = 0; j < ARRAY_LENGTH (draw_funcs); j++) {
156 for (i = 0; i < ARRAY_LENGTH (pattern_funcs); i++) {
157 x = i * (WIDTH + PAD) + PAD;
158 y = j * (HEIGHT + PAD) + PAD;
159
160 cairo_save (cr);
161
162 pattern = cairo_pattern_create_linear (x + WIDTH, y,
163 x, y + HEIGHT);
164 cairo_pattern_add_color_stop_rgba (pattern, 0.2,
165 0.0, 0.0, 1.0, 1.0); /* Solid blue */
166 cairo_pattern_add_color_stop_rgba (pattern, 0.8,
167 0.0, 0.0, 1.0, 0.0); /* Transparent blue */
168 cairo_set_source (cr, pattern);
169 cairo_pattern_destroy (pattern);
170
171 cairo_rectangle (cr, x, y, WIDTH, HEIGHT);
172 cairo_fill_preserve (cr);
173 cairo_clip (cr);
174
175 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
176 pattern_funcs[i] (cr, x, y);
177 draw_funcs[j] (cr, x, y);
178 if (cairo_status (cr))
179 cairo_test_log (ctx, "%d %d HERE!\n", (int)i, (int)j);
180
181 cairo_restore (cr);
182 }
183 }
184
185 if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
186 cairo_test_log (ctx, "%d %d .HERE!\n", (int)i, (int)j);
187
188 return CAIRO_TEST_SUCCESS;
189 }
190
191 CAIRO_TEST (operator_clear,
192 "Test of CAIRO_OPERATOR_CLEAR",
193 "operator", /* keywords */
194 NULL, /* requirements */
195 IMAGE_WIDTH, IMAGE_HEIGHT,
196 NULL, draw)
197