1
/*
2
 * Copyright 2009 Benjamin Otte
3
 *
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use, copy,
8
 * modify, merge, publish, distribute, sublicense, and/or sell copies
9
 * of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 *
24
 * Author: Benjamin Otte <otte@gnome.org>
25
 */
26

            
27
#include "cairo-test.h"
28

            
29
typedef enum {
30
  CLEAR,
31
  CLEARED,
32
  PAINTED
33
} surface_type_t;
34

            
35
#define SIZE 10
36
#define SPACE 5
37

            
38
static cairo_surface_t *
39
27
create_surface (cairo_t *target, cairo_content_t content, surface_type_t type)
40
{
41
    cairo_surface_t *surface;
42
    cairo_t *cr;
43

            
44
27
    surface = cairo_surface_create_similar (cairo_get_target (target),
45
					    content,
46
					    SIZE, SIZE);
47

            
48
27
    if (type == CLEAR)
49
9
	return surface;
50

            
51
18
    cr = cairo_create (surface);
52
18
    cairo_surface_destroy (surface);
53

            
54
18
    cairo_set_source_rgb (cr, 0.75, 0, 0);
55
18
    cairo_paint (cr);
56

            
57
18
    if (type == PAINTED)
58
9
	goto DONE;
59

            
60
9
    cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
61
9
    cairo_paint (cr);
62

            
63
18
DONE:
64
18
    surface = cairo_surface_reference (cairo_get_target (cr));
65
18
    cairo_destroy (cr);
66

            
67
18
    return surface;
68
}
69

            
70
static void
71
27
paint (cairo_t *cr, cairo_surface_t *surface)
72
{
73
27
    cairo_set_source_surface (cr, surface, 0, 0);
74
27
    cairo_paint (cr);
75
27
}
76

            
77
static void
78
27
fill (cairo_t *cr, cairo_surface_t *surface)
79
{
80
27
    cairo_set_source_surface (cr, surface, 0, 0);
81
27
    cairo_rectangle (cr, -SPACE, -SPACE, SIZE + 2 * SPACE, SIZE + 2 * SPACE);
82
27
    cairo_fill (cr);
83
27
}
84

            
85
static void
86
27
stroke (cairo_t *cr, cairo_surface_t *surface)
87
{
88
27
    cairo_set_source_surface (cr, surface, 0, 0);
89
27
    cairo_set_line_width (cr, 2.0);
90
27
    cairo_rectangle (cr, 1, 1, SIZE - 2, SIZE - 2);
91
27
    cairo_stroke (cr);
92
27
}
93

            
94
static void
95
27
mask (cairo_t *cr, cairo_surface_t *surface)
96
{
97
27
    cairo_set_source_rgb (cr, 0, 0, 0.75);
98
27
    cairo_mask_surface (cr, surface, 0, 0);
99
27
}
100

            
101
static void
102
27
mask_self (cairo_t *cr, cairo_surface_t *surface)
103
{
104
27
    cairo_set_source_surface (cr, surface, 0, 0);
105
27
    cairo_mask_surface (cr, surface, 0, 0);
106
27
}
107

            
108
static void
109
27
glyphs (cairo_t *cr, cairo_surface_t *surface)
110
{
111
27
    cairo_set_source_surface (cr, surface, 0, 0);
112
27
    cairo_select_font_face (cr,
113
			    "@cairo:",
114
			    CAIRO_FONT_SLANT_NORMAL,
115
			    CAIRO_FONT_WEIGHT_NORMAL);
116
27
    cairo_set_font_size (cr, 16);
117
27
    cairo_translate (cr, 0, SIZE);
118
27
    cairo_show_text (cr, "C");
119
27
}
120

            
121
typedef void (* operation_t) (cairo_t *cr, cairo_surface_t *surface);
122
static operation_t operations[] = {
123
    paint,
124
    fill,
125
    stroke,
126
    mask,
127
    mask_self,
128
    glyphs
129
};
130

            
131
static cairo_test_status_t
132
3
draw (cairo_t *cr, int width, int height)
133
{
134
3
    cairo_content_t contents[] = { CAIRO_CONTENT_COLOR_ALPHA, CAIRO_CONTENT_COLOR, CAIRO_CONTENT_ALPHA };
135
    unsigned int content, type, ops;
136

            
137
3
    cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
138
3
    cairo_paint (cr);
139
3
    cairo_translate (cr, SPACE, SPACE);
140

            
141
12
    for (type = 0; type <= PAINTED; type++) {
142
36
	for (content = 0; content < ARRAY_LENGTH (contents); content++) {
143
	    cairo_surface_t *surface;
144

            
145
27
	    surface = create_surface (cr, contents[content], type);
146

            
147
27
            cairo_save (cr);
148
189
            for (ops = 0; ops < ARRAY_LENGTH (operations); ops++) {
149
162
                cairo_save (cr);
150
162
                operations[ops] (cr, surface);
151
162
                cairo_restore (cr);
152
162
                cairo_translate (cr, 0, SIZE + SPACE);
153
            }
154
27
            cairo_restore (cr);
155
27
            cairo_translate (cr, SIZE + SPACE, 0);
156

            
157
27
	    cairo_surface_destroy (surface);
158
        }
159
    }
160

            
161
3
    return CAIRO_TEST_SUCCESS;
162
}
163

            
164
1
CAIRO_TEST (clear_source,
165
	    "Check painting with cleared surfaces works as expected",
166
	    NULL, /* keywords */
167
	    NULL, /* requirements */
168
	    (SIZE + SPACE) * 9 + SPACE, ARRAY_LENGTH (operations) * (SIZE + SPACE) + SPACE,
169
	    NULL, draw)