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 "cairo-test.h"
27
#include <math.h>
28
#include <stdio.h>
29

            
30
#define WIDTH 16
31
#define HEIGHT 16
32
#define PAD 2
33

            
34
static void
35
42
draw_mask (cairo_t *cr, int x, int y)
36
{
37
    cairo_surface_t *mask_surface;
38
    cairo_t *cr2;
39

            
40
42
    double width = (int)(0.9 * WIDTH);
41
42
    double height = (int)(0.9 * HEIGHT);
42
42
    x += 0.05 * WIDTH;
43
42
    y += 0.05 * HEIGHT;
44

            
45
42
    mask_surface = cairo_surface_create_similar (cairo_get_group_target (cr),
46
						 CAIRO_CONTENT_ALPHA,
47
						 width, height);
48
42
    cr2 = cairo_create (mask_surface);
49
42
    cairo_surface_destroy (mask_surface);
50

            
51
42
    cairo_save (cr2);
52
42
    cairo_set_source_rgba (cr2, 0, 0, 0, 0); /* transparent */
53
42
    cairo_set_operator (cr2, CAIRO_OPERATOR_SOURCE);
54
42
    cairo_paint (cr2);
55
42
    cairo_restore (cr2);
56

            
57
42
    cairo_set_source_rgb (cr2, 1, 1, 1); /* white */
58

            
59
42
    cairo_arc (cr2, 0.5 * width, 0.5 * height, 0.45 * height, 0, 2 * M_PI);
60
42
    cairo_fill (cr2);
61

            
62
42
    cairo_mask_surface (cr, cairo_get_target (cr2), x, y);
63
42
    cairo_destroy (cr2);
64
42
}
65

            
66
static void
67
42
draw_glyphs (cairo_t *cr, int x, int y)
68
{
69
    cairo_text_extents_t extents;
70

            
71
42
    cairo_set_font_size (cr, 0.8 * HEIGHT);
72

            
73
42
    cairo_text_extents (cr, "FG", &extents);
74
42
    cairo_move_to (cr,
75
42
		   x + floor ((WIDTH - extents.width) / 2 + 0.5) - extents.x_bearing,
76
42
		   y + floor ((HEIGHT - extents.height) / 2 + 0.5) - extents.y_bearing);
77
42
    cairo_show_text (cr, "FG");
78
42
}
79

            
80
static void
81
42
draw_polygon (cairo_t *cr, int x, int y)
82
{
83
42
    double width = (int)(0.9 * WIDTH);
84
42
    double height = (int)(0.9 * HEIGHT);
85
42
    x += 0.05 * WIDTH;
86
42
    y += 0.05 * HEIGHT;
87

            
88
42
    cairo_new_path (cr);
89
42
    cairo_move_to (cr, x, y);
90
42
    cairo_line_to (cr, x, y + height);
91
42
    cairo_line_to (cr, x + width / 2, y + 3 * height / 4);
92
42
    cairo_line_to (cr, x + width, y + height);
93
42
    cairo_line_to (cr, x + width, y);
94
42
    cairo_line_to (cr, x + width / 2, y + height / 4);
95
42
    cairo_close_path (cr);
96
42
    cairo_fill (cr);
97
42
}
98

            
99
static void
100
42
draw_rects (cairo_t *cr, int x, int y)
101
{
102
42
    double block_width = (int)(0.33 * WIDTH + 0.5);
103
42
    double block_height = (int)(0.33 * HEIGHT + 0.5);
104
    int i, j;
105

            
106
168
    for (i = 0; i < 3; i++)
107
504
	for (j = 0; j < 3; j++)
108
378
	    if ((i + j) % 2 == 0)
109
210
		cairo_rectangle (cr,
110
210
				 x + block_width * i, y + block_height * j,
111
				 block_width,         block_height);
112

            
113
42
    cairo_fill (cr);
114
42
}
115

            
116
static void (* const draw_funcs[])(cairo_t *cr, int x, int y) = {
117
    draw_mask,
118
    draw_glyphs,
119
    draw_polygon,
120
    draw_rects
121
};
122

            
123
#define N_OPERATORS (1 + CAIRO_OPERATOR_SATURATE - CAIRO_OPERATOR_CLEAR)
124

            
125
#define IMAGE_WIDTH (N_OPERATORS * (WIDTH + PAD) + PAD)
126
#define IMAGE_HEIGHT (ARRAY_LENGTH (draw_funcs) * (HEIGHT + PAD) + PAD)
127

            
128
static cairo_test_status_t
129
3
draw (cairo_t *cr, int width, int height)
130
{
131
3
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
132
    size_t j, x, y;
133
    cairo_operator_t op;
134
    cairo_pattern_t *pattern;
135

            
136
3
    cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
137
			    CAIRO_FONT_SLANT_NORMAL,
138
			    CAIRO_FONT_WEIGHT_NORMAL);
139
3
    cairo_set_font_size (cr, 0.9 * HEIGHT);
140

            
141
15
    for (j = 0; j < ARRAY_LENGTH (draw_funcs); j++) {
142
180
	for (op = CAIRO_OPERATOR_CLEAR; op < N_OPERATORS; op++) {
143
168
	    x = op * (WIDTH + PAD) + PAD;
144
168
	    y = j * (HEIGHT + PAD) + PAD;
145

            
146
168
	    cairo_save (cr);
147

            
148
168
	    pattern = cairo_pattern_create_linear (x + WIDTH, y,
149
168
						   x,         y + HEIGHT);
150
168
	    cairo_pattern_add_color_stop_rgba (pattern, 0.2,
151
					       0.0, 0.0, 1.0, 1.0); /* Solid blue */
152
168
	    cairo_pattern_add_color_stop_rgba (pattern, 0.8,
153
					       0.0, 0.0, 1.0, 0.0); /* Transparent blue */
154
168
	    cairo_set_source (cr, pattern);
155
168
	    cairo_pattern_destroy (pattern);
156

            
157
168
	    cairo_rectangle (cr, x, y, WIDTH, HEIGHT);
158
168
	    cairo_fill (cr);
159

            
160
168
	    cairo_set_operator (cr, op);
161
168
	    cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
162

            
163
168
	    cairo_move_to (cr, x, y);
164
168
	    cairo_line_to (cr, x + WIDTH, y);
165
168
	    cairo_line_to (cr, x, y + HEIGHT);
166
168
	    cairo_clip (cr);
167

            
168
168
	    draw_funcs[j] (cr, x, y);
169
168
	    if (cairo_status (cr))
170
		cairo_test_log (ctx, "%d %d HERE!\n", op, (int)j);
171

            
172
168
	    cairo_restore (cr);
173
	}
174
    }
175

            
176
3
    if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
177
	cairo_test_log (ctx, "%d %d .HERE!\n", op, (int)j);
178

            
179
3
    return CAIRO_TEST_SUCCESS;
180
}
181

            
182
1
CAIRO_TEST (clip_operator,
183
	    "Surface clipping with different operators",
184
	    "clip", /* keywords */
185
	    NULL, /* requirements */
186
	    IMAGE_WIDTH, IMAGE_HEIGHT,
187
	    NULL, draw)
188