1
/*
2
 * Copyright (c) 2010 M Joonas Pihlaja
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: M Joonas Pihlaja <jpihlaja@cc.helsinki.fi>
25
 */
26
#include "cairo-test.h"
27

            
28
/* Tests specific clipping fast paths and their interaction with
29
 * groups:  It shouldn't matter if the clip is set before or after
30
 * pushing a group.
31
 *
32
 * There's some overlap with the following tests, but they test for
33
 * different things:
34
 *
35
 *  group-clip.c (tests preserving paths), clipped-group.c (tests
36
 *  clipping the same thing different ways), clip-push-group (tests
37
 *  for a specific bug).
38
 */
39

            
40
#define GENERATE_REF 0
41

            
42
/* For determining whether we establish the clip path before or after
43
 * pushing a group. */
44
enum {
45
    CLIP_OUTSIDE_GROUP,
46
    CLIP_INSIDE_GROUP
47
};
48

            
49
typedef void (*clipper_t)(cairo_t *cr, int w, int h);
50

            
51
static cairo_test_status_t
52
18
clip_and_paint (cairo_t *cr,
53
                int w, int h,
54
                clipper_t do_clip,
55
                int clip_where)
56
{
57
18
    cairo_save (cr); {
58
        if (GENERATE_REF) {
59
            do_clip (cr, w, h);
60
            cairo_paint (cr);
61
        } else {
62
18
            if (clip_where == CLIP_OUTSIDE_GROUP)
63
9
                do_clip (cr, w, h);
64
18
            cairo_push_group (cr); {
65
18
                if (clip_where == CLIP_INSIDE_GROUP)
66
9
                    do_clip (cr, w, h);
67
18
                cairo_paint (cr);
68
            }
69
18
            cairo_pop_group_to_source (cr);
70
18
            if (clip_where == CLIP_OUTSIDE_GROUP)
71
9
		cairo_reset_clip (cr);
72
18
            cairo_paint (cr);
73
        }
74
    }
75
18
    cairo_restore (cr);
76
18
    return CAIRO_TEST_SUCCESS;
77
}
78

            
79
static cairo_test_status_t
80
9
run_clip_test (cairo_t *cr, int w, int h, clipper_t do_clip)
81
{
82
9
    cairo_set_source_rgb (cr, 1,1,1);
83
9
    cairo_paint (cr);
84
9
    cairo_set_source_rgb (cr, 1,0,0);
85

            
86
    /* Left. */
87
9
    clip_and_paint (cr, w/2, h, do_clip, CLIP_OUTSIDE_GROUP);
88

            
89
    /* Right */
90
9
    cairo_translate(cr, w/2, 0);
91
9
    clip_and_paint (cr, w/2, h, do_clip, CLIP_INSIDE_GROUP);
92

            
93
9
    return CAIRO_TEST_SUCCESS;
94
}
95

            
96
static void
97
6
clip_aligned_rectangles (cairo_t *cr, int w, int h)
98
{
99
6
    int x1 = 0.2 * w;
100
6
    int y1 = 0.2 * h;
101
6
    int x2 = 0.8 * w;
102
6
    int y2 = 0.8 * h;
103

            
104
6
    cairo_rectangle (cr, x1, y1, w, h);
105
6
    cairo_clip (cr);
106

            
107
6
    cairo_rectangle (cr, x2, y2, -w, -h);
108
6
    cairo_clip (cr);
109
6
}
110

            
111
static void
112
6
clip_unaligned_rectangles (cairo_t *cr, int w, int h)
113
{
114
    /* This clip stresses the antialiased edges produced by an
115
     * unaligned rectangular clip. The edges should be produced by
116
     * compositing red on white with alpha = 0.5 on the sides, and with
117
     * alpha = 0.25 in the corners. */
118
6
    int x1 = 0.2 * w;
119
6
    int y1 = 0.2 * h;
120
6
    int x2 = 0.8 * w;
121
6
    int y2 = 0.8 * h;
122

            
123
6
    cairo_rectangle (cr, x1+0.5, y1+0.5, w, h);
124
6
    cairo_clip (cr);
125

            
126
6
    cairo_rectangle (cr, x2+0.5, y2+0.5, -w, -h);
127
6
    w = x2 - x1;
128
6
    h = y2 - y1;
129
6
    cairo_rectangle (cr, x2, y1+1, -w+1, h-1);
130
6
    cairo_clip (cr);
131
6
}
132

            
133
static void
134
6
clip_circles (cairo_t *cr, int w, int h)
135
{
136
6
    int x1 = 0.5 * w;
137
6
    int y1 = 0.5 * h;
138
6
    int x2 = 0.75 * w;
139
6
    int y2 = 0.75 * h;
140
6
    int r = 0.4*MIN(w,h);
141

            
142
6
    cairo_arc (cr, x1, y1, r, 0, 6.28);
143
6
    cairo_close_path (cr);
144
6
    cairo_clip (cr);
145

            
146
6
    cairo_arc (cr, x2, y2, r, 0, 6.28);
147
6
    cairo_close_path (cr);
148
6
    cairo_clip (cr);
149
6
}
150

            
151
static cairo_test_status_t
152
3
draw_aligned_rectangles (cairo_t *cr, int width, int height)
153
{
154
3
    return run_clip_test (cr, width, height, clip_aligned_rectangles);
155
}
156

            
157
static cairo_test_status_t
158
3
draw_unaligned_rectangles (cairo_t *cr, int width, int height)
159
{
160
3
    return run_clip_test (cr, width, height, clip_unaligned_rectangles);
161
}
162

            
163
static cairo_test_status_t
164
3
draw_circles (cairo_t *cr, int width, int height)
165
{
166
3
    return run_clip_test (cr, width, height, clip_circles);
167
}
168

            
169
1
CAIRO_TEST (clip_group_shapes_aligned_rectangles,
170
	    "Test clip and group interaction with aligned rectangle clips",
171
	    "clip", /* keywords */
172
	    NULL, /* requirements */
173
	    200, 100,
174
	    NULL, draw_aligned_rectangles)
175

            
176
1
CAIRO_TEST (clip_group_shapes_unaligned_rectangles,
177
	    "Test clip and group interaction with unaligned rectangle clips",
178
	    "clip", /* keywords */
179
	    "target=raster", /* requirements */
180
	    200, 100,
181
	    NULL, draw_unaligned_rectangles)
182

            
183
1
CAIRO_TEST (clip_group_shapes_circles,
184
	    "Test clip and group interaction with circular clips",
185
	    "clip", /* keywords */
186
	    NULL, /* requirements */
187
	    200, 100,
188
	    NULL, draw_circles)