1
/*
2
 * Copyright 2010 Red Hat Inc.
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
#define TARGET_SIZE 10
30

            
31
#define SUB_SIZE 15
32
#define SUB_OFFSET -5
33

            
34
#define PAINT_OFFSET SUB_SIZE
35
#define PAINT_SIZE (3 * SUB_SIZE)
36

            
37
static cairo_content_t contents[] = { CAIRO_CONTENT_ALPHA,
38
                                      CAIRO_CONTENT_COLOR,
39
                                      CAIRO_CONTENT_COLOR_ALPHA };
40

            
41
#define N_CONTENTS ARRAY_LENGTH (contents)
42
#define N_PADS (CAIRO_EXTEND_PAD + 1)
43

            
44

            
45
static cairo_surface_t *
46
18
create_target (cairo_surface_t *similar_to,
47
               cairo_content_t content)
48
{
49
    cairo_surface_t *surface;
50
    cairo_t *cr;
51

            
52
18
    surface = cairo_surface_create_similar (similar_to,
53
                                            content,
54
                                            TARGET_SIZE, TARGET_SIZE);
55
    
56
18
    cr = cairo_create (surface);
57
18
    cairo_test_paint_checkered (cr);
58
18
    cairo_destroy (cr);
59

            
60
18
    return surface;
61
}
62

            
63
static cairo_test_status_t
64
18
check_surface_extents (const cairo_test_context_t *ctx,
65
                       cairo_surface_t *           surface,
66
                       double                      x,
67
                       double                      y,
68
                       double                      width,
69
                       double                      height)
70
{
71
    double x1, y1, x2, y2;
72
    cairo_t *cr;
73

            
74
18
    cr = cairo_create (surface);
75
18
    cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
76
18
    cairo_destroy (cr);
77

            
78
18
    if (x != x1 ||
79
18
        y != y1 ||
80
18
        width != x2 - x1 ||
81
18
        height != y2 - y1) {
82
        cairo_test_log (ctx,
83
                        "surface extents should be (%g, %g, %g, %g), but are (%g, %g, %g, %g)\n",
84
                        x, y, width, height,
85
                        x1, y1, x2 - x1, y2 - y1);
86
        return CAIRO_TEST_FAILURE;
87
    }
88

            
89
18
    return CAIRO_TEST_SUCCESS;
90
}
91

            
92
static cairo_test_status_t
93
6
draw_for_size (cairo_t *cr,
94
               double   x,
95
               double   y)
96
{
97
    cairo_surface_t *target, *subsurface;
98
    cairo_extend_t extend;
99
6
    cairo_test_status_t check, result = CAIRO_TEST_SUCCESS;
100
    unsigned int content;
101

            
102
24
    for (content = 0; content < N_CONTENTS; content++) {
103
18
        cairo_save (cr);
104

            
105
        /* create a target surface for our subsurface */
106
18
        target = create_target (cairo_get_target (cr),
107
                                contents[content]);
108

            
109
        /* create a subsurface that extends the target surface */
110
18
        subsurface = cairo_surface_create_for_rectangle (target, 
111
                                                         x, y,
112
                                                         SUB_SIZE, SUB_SIZE);
113

            
114
        /* ensure the extents are ok */
115
18
        check = check_surface_extents (cairo_test_get_context (cr),
116
                                       subsurface,
117
                                       0, 0,
118
                                       SUB_SIZE, SUB_SIZE);
119
18
        if (result == CAIRO_TEST_SUCCESS)
120
18
          result = check;
121

            
122
        /* paint this surface with all extend modes. */
123
90
        for (extend = 0; extend < N_PADS; extend++) {
124
72
            cairo_save (cr);
125

            
126
72
            cairo_rectangle (cr, 0, 0, PAINT_SIZE, PAINT_SIZE);
127
72
            cairo_clip (cr);
128

            
129
72
            cairo_set_source_surface (cr, subsurface, PAINT_OFFSET, PAINT_OFFSET);
130
72
            cairo_pattern_set_extend (cairo_get_source (cr), extend);
131
72
            cairo_paint (cr);
132

            
133
72
            cairo_restore (cr);
134

            
135
72
            cairo_translate (cr, PAINT_SIZE + TARGET_SIZE, 0);
136
        }
137

            
138
18
        cairo_surface_destroy (subsurface);
139
18
        cairo_surface_destroy (target);
140

            
141
18
        cairo_restore (cr);
142

            
143
18
        cairo_translate (cr, 0, PAINT_SIZE + TARGET_SIZE);
144
    }
145

            
146
6
    return result;
147
}
148

            
149
static cairo_test_status_t
150
3
draw (cairo_t *cr, int width, int height)
151
{
152
3
    cairo_test_status_t check, result = CAIRO_TEST_SUCCESS;
153

            
154
    /* paint background in nice gray */
155
3
    cairo_set_source_rgb (cr, 0.51613, 0.55555, 0.51613);
156
3
    cairo_paint (cr);
157

            
158
    /* Use CAIRO_OPERATOR_SOURCE in the tests so we get the actual
159
     * contents of the subsurface */
160
3
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
161

            
162
3
    result = draw_for_size (cr, SUB_OFFSET, SUB_OFFSET);
163

            
164
3
    check = draw_for_size (cr, 0, 0);
165
3
    if (result == CAIRO_TEST_SUCCESS)
166
3
      result = check;
167

            
168
3
    return result;
169
}
170

            
171
1
CAIRO_TEST (subsurface_outside_target,
172
	    "Tests contents of subsurfaces outside target area",
173
	    "subsurface, pad", /* keywords */
174
	    "target=raster", /* FIXME! recursion bug in subsurface/snapshot (with pdf backend) */ /* requirements */
175
	    (PAINT_SIZE + TARGET_SIZE) * N_PADS         - TARGET_SIZE,
176
            (PAINT_SIZE + TARGET_SIZE) * N_CONTENTS * 2 - TARGET_SIZE,
177
	    NULL, draw)