1
/*
2
 * Copyright © 2016 Adrian Johnson
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
 * Authors:
25
 *	Adrian Johnson <ajohnson@redneon.com>
26
 */
27

            
28
#include "cairo-test.h"
29
#include <stdio.h>
30
#include <math.h>
31

            
32
#define PAT_SIZE  64
33
#define PAD (PAT_SIZE/8)
34
#define WIDTH (PAT_SIZE*4 + PAD*5)
35
#define HEIGHT (PAT_SIZE + PAD*2)
36

            
37
/* Test case based on bug 89232 - painting a recording surface to a pdf/ps surface
38
 * omits objects on the recording surface with negative coordinates even though
39
 * the pattern matrix has transformed the objects to within the page extents.
40
 * The bug is a result of pdf/ps assuming the surface extents are always
41
 * (0,0) to (page_width, page_height).
42
 *
43
 * Each test has four cases of painting a recording pattern where:
44
 * 1) recording surface origin is transformed to the center of the pattern
45
 * 2) same as 1) but also scaled up 10x
46
 * 3) same as 1) but also scaled down 10x
47
 * 4) same as 1) but also rotated 45 deg
48
 */
49

            
50

            
51
static void
52
12
transform_extents(cairo_rectangle_t *extents, cairo_matrix_t *mat)
53
{
54
    double x1, y1, x2, y2, x, y;
55

            
56
#define UPDATE_BBOX \
57
    x1 = x < x1 ? x : x1; \
58
    y1 = y < y1 ? y : y1; \
59
    x2 = x > x2 ? x : x2; \
60
    y2 = y > y2 ? y : y2;
61

            
62
12
    x = extents->x;
63
12
    y = extents->y;
64
12
    cairo_matrix_transform_point (mat, &x, &y);
65
12
    x1 = x2 = x;
66
12
    y1 = y2 = y;
67

            
68
12
    x = extents->x + extents->width;
69
12
    y = extents->y;
70
12
    cairo_matrix_transform_point (mat, &x, &y);
71
12
    UPDATE_BBOX;
72

            
73
12
    x = extents->x;
74
12
    y = extents->y + extents->height;
75
12
    cairo_matrix_transform_point (mat, &x, &y);
76
12
    UPDATE_BBOX;
77

            
78
12
    x = extents->x + extents->width;
79
12
    y = extents->y + extents->height;
80
12
    cairo_matrix_transform_point (mat, &x, &y);
81
12
    UPDATE_BBOX;
82

            
83
12
    extents->x = x1;
84
12
    extents->y = y1;
85
12
    extents->width = x2 - extents->x;
86
12
    extents->height = y2 - extents->y;
87

            
88
#undef UPDATE_BBOX
89
12
}
90

            
91
static cairo_pattern_t *
92
24
create_pattern (cairo_matrix_t *mat, cairo_bool_t bounded)
93
{
94
    cairo_surface_t *surf;
95
    cairo_pattern_t *pat;
96
    cairo_t *cr;
97
    int border;
98
    int square;
99

            
100
24
    if (bounded) {
101
12
	cairo_rectangle_t extents = { 0, 0, PAT_SIZE, PAT_SIZE };
102
12
	transform_extents (&extents, mat);
103
12
	surf = cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, &extents);
104
    } else {
105
12
	surf = cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, NULL);
106
    }
107

            
108
24
    cr = cairo_create (surf);
109
24
    cairo_transform (cr, mat);
110

            
111
24
    border  = PAT_SIZE/8;
112
24
    square = (PAT_SIZE - 2*border)/2;
113

            
114
24
    cairo_rectangle (cr, 0, 0, PAT_SIZE, PAT_SIZE);
115
24
    cairo_clip (cr);
116
24
    cairo_set_source_rgb (cr, 1, 0, 0);
117
24
    cairo_paint (cr);
118

            
119
24
    cairo_translate (cr, border, border);
120
24
    cairo_rectangle (cr, 0, 0, square, square);
121
24
    cairo_set_source_rgb (cr, 0, 1, 0);
122
24
    cairo_fill (cr);
123

            
124
24
    cairo_translate (cr, square, 0);
125
24
    cairo_rectangle (cr, 0, 0, square, square);
126
24
    cairo_set_source_rgb (cr, 0, 0, 1);
127
24
    cairo_fill (cr);
128

            
129
24
    cairo_translate (cr, 0, square);
130
24
    cairo_rectangle (cr, 0, 0, square, square);
131
24
    cairo_set_source_rgb (cr, 0, 1, 1);
132
24
    cairo_fill (cr);
133

            
134
24
    cairo_translate (cr, -square, 0);
135
24
    cairo_rectangle (cr, 0, 0, square, square);
136
24
    cairo_set_source_rgb (cr, 1, 1, 0);
137
24
    cairo_fill (cr);
138

            
139
24
    cairo_destroy (cr);
140

            
141
24
    pat = cairo_pattern_create_for_surface (surf);
142
24
    cairo_surface_destroy (surf);
143
24
    cairo_pattern_set_matrix (pat, mat);
144

            
145
24
    return pat;
146
}
147

            
148
static cairo_test_status_t
149
6
record_extents (cairo_t *cr, int width, int height, cairo_bool_t bounded)
150
{
151
    cairo_pattern_t *pat;
152
    cairo_matrix_t mat;
153

            
154
    /* record surface extents (-PAT_SIZE/2, -PAT_SIZE/2) to (PAT_SIZE/2, PAT_SIZE/2) */
155
6
    cairo_translate (cr, PAD, PAD);
156
6
    cairo_matrix_init_translate (&mat, -PAT_SIZE/2, -PAT_SIZE/2);
157
6
    pat = create_pattern (&mat, bounded);
158
6
    cairo_set_source (cr, pat);
159
6
    cairo_pattern_destroy (pat);
160
6
    cairo_paint (cr);
161

            
162
    /* record surface extents (-10*PAT_SIZE/2, -10*PAT_SIZE/2) to (10*PAT_SIZE/2, 10*PAT_SIZE/2) */
163
6
    cairo_translate (cr, PAT_SIZE + PAD, 0);
164
6
    cairo_matrix_init_translate (&mat, -10.0*PAT_SIZE/2, -10.0*PAT_SIZE/2);
165
6
    cairo_matrix_scale (&mat, 10, 10);
166
6
    pat = create_pattern (&mat, bounded);
167
6
    cairo_set_source (cr, pat);
168
6
    cairo_pattern_destroy (pat);
169
6
    cairo_paint (cr);
170

            
171
    /* record surface extents (-0.1*PAT_SIZE/2, -0.1*PAT_SIZE/2) to (0.1*PAT_SIZE/2, 0.1*PAT_SIZE/2) */
172
6
    cairo_translate (cr, PAT_SIZE + PAD, 0);
173
6
    cairo_matrix_init_translate (&mat, -0.1*PAT_SIZE/2, -0.1*PAT_SIZE/2);
174
6
    cairo_matrix_scale (&mat, 0.1, 0.1);
175
6
    pat = create_pattern (&mat, bounded);
176
6
    cairo_set_source (cr, pat);
177
6
    cairo_pattern_destroy (pat);
178
6
    cairo_paint (cr);
179

            
180
    /* record surface centered on (0,0) and rotated 45 deg */
181
6
    cairo_translate (cr, PAT_SIZE + PAD, 0);
182
6
    cairo_matrix_init_translate (&mat, -PAT_SIZE/sqrt(2), -PAT_SIZE/sqrt(2));
183
6
    cairo_matrix_rotate (&mat, M_PI/4.0);
184
6
    cairo_matrix_translate (&mat, PAT_SIZE/2, -PAT_SIZE/2);
185
6
    pat = create_pattern (&mat, bounded);
186
6
    cairo_set_source (cr, pat);
187
6
    cairo_pattern_destroy (pat);
188
6
    cairo_paint (cr);
189

            
190
6
    return CAIRO_TEST_SUCCESS;
191
}
192

            
193
static cairo_test_status_t
194
3
record_neg_extents_bounded (cairo_t *cr, int width, int height)
195
{
196
3
    return record_extents(cr, width, height, TRUE);
197
}
198

            
199
static cairo_test_status_t
200
3
record_neg_extents_unbounded (cairo_t *cr, int width, int height)
201
{
202
3
    return record_extents(cr, width, height, FALSE);
203
}
204

            
205

            
206
1
CAIRO_TEST (record_neg_extents_unbounded,
207
	    "Paint unbounded recording pattern with untransformed extents outside of target extents",
208
	    "record,transform,pattern", /* keywords */
209
	    NULL, /* requirements */
210
	    WIDTH, HEIGHT,
211
	    NULL, record_neg_extents_unbounded)
212
1
CAIRO_TEST (record_neg_extents_bounded,
213
	    "Paint bounded recording pattern with untransformed extents outside of target extents",
214
	    "record,transform,pattern", /* keywords */
215
	    NULL, /* requirements */
216
	    WIDTH, HEIGHT,
217
	    NULL, record_neg_extents_bounded)