summaryrefslogtreecommitdiff
path: root/test/record-neg-extents.c
blob: 29a2aa1e610dc577558065e78a1db705a46d46d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * Copyright © 2016 Adrian Johnson
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors:
 *	Adrian Johnson <ajohnson@redneon.com>
 */

#include "cairo-test.h"
#include <stdio.h>
#include <math.h>

#define PAT_SIZE  64
#define PAD (PAT_SIZE/8)
#define WIDTH (PAT_SIZE*4 + PAD*5)
#define HEIGHT (PAT_SIZE + PAD*2)

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


static void
transform_extents(cairo_rectangle_t *extents, cairo_matrix_t *mat)
{
    double x1, y1, x2, y2, x, y;

#define UPDATE_BBOX \
    x1 = x < x1 ? x : x1; \
    y1 = y < y1 ? y : y1; \
    x2 = x > x2 ? x : x2; \
    y2 = y > y2 ? y : y2;

    x = extents->x;
    y = extents->y;
    cairo_matrix_transform_point (mat, &x, &y);
    x1 = x2 = x;
    y1 = y2 = y;

    x = extents->x + extents->width;
    y = extents->y;
    cairo_matrix_transform_point (mat, &x, &y);
    UPDATE_BBOX;

    x = extents->x;
    y = extents->y + extents->height;
    cairo_matrix_transform_point (mat, &x, &y);
    UPDATE_BBOX;

    x = extents->x + extents->width;
    y = extents->y + extents->height;
    cairo_matrix_transform_point (mat, &x, &y);
    UPDATE_BBOX;

    extents->x = x1;
    extents->y = y1;
    extents->width = x2 - extents->x;
    extents->height = y2 - extents->y;

#undef UPDATE_BBOX
}

static cairo_pattern_t *
create_pattern (cairo_matrix_t *mat, cairo_bool_t bounded)
{
    cairo_surface_t *surf;
    cairo_pattern_t *pat;
    cairo_t *cr;
    int border;
    int square;

    if (bounded) {
	cairo_rectangle_t extents = { 0, 0, PAT_SIZE, PAT_SIZE };
	transform_extents (&extents, mat);
	surf = cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, &extents);
    } else {
	surf = cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, NULL);
    }

    cr = cairo_create (surf);
    cairo_transform (cr, mat);

    border  = PAT_SIZE/8;
    square = (PAT_SIZE - 2*border)/2;

    cairo_rectangle (cr, 0, 0, PAT_SIZE, PAT_SIZE);
    cairo_clip (cr);
    cairo_set_source_rgb (cr, 1, 0, 0);
    cairo_paint (cr);

    cairo_translate (cr, border, border);
    cairo_rectangle (cr, 0, 0, square, square);
    cairo_set_source_rgb (cr, 0, 1, 0);
    cairo_fill (cr);

    cairo_translate (cr, square, 0);
    cairo_rectangle (cr, 0, 0, square, square);
    cairo_set_source_rgb (cr, 0, 0, 1);
    cairo_fill (cr);

    cairo_translate (cr, 0, square);
    cairo_rectangle (cr, 0, 0, square, square);
    cairo_set_source_rgb (cr, 0, 1, 1);
    cairo_fill (cr);

    cairo_translate (cr, -square, 0);
    cairo_rectangle (cr, 0, 0, square, square);
    cairo_set_source_rgb (cr, 1, 1, 0);
    cairo_fill (cr);

    cairo_destroy (cr);

    pat = cairo_pattern_create_for_surface (surf);
    cairo_surface_destroy (surf);
    cairo_pattern_set_matrix (pat, mat);

    return pat;
}

static cairo_test_status_t
record_extents (cairo_t *cr, int width, int height, cairo_bool_t bounded)
{
    cairo_pattern_t *pat;
    cairo_matrix_t mat;

    /* record surface extents (-PAT_SIZE/2, -PAT_SIZE/2) to (PAT_SIZE/2, PAT_SIZE/2) */
    cairo_translate (cr, PAD, PAD);
    cairo_matrix_init_translate (&mat, -PAT_SIZE/2, -PAT_SIZE/2);
    pat = create_pattern (&mat, bounded);
    cairo_set_source (cr, pat);
    cairo_pattern_destroy (pat);
    cairo_paint (cr);

    /* record surface extents (-10*PAT_SIZE/2, -10*PAT_SIZE/2) to (10*PAT_SIZE/2, 10*PAT_SIZE/2) */
    cairo_translate (cr, PAT_SIZE + PAD, 0);
    cairo_matrix_init_translate (&mat, -10.0*PAT_SIZE/2, -10.0*PAT_SIZE/2);
    cairo_matrix_scale (&mat, 10, 10);
    pat = create_pattern (&mat, bounded);
    cairo_set_source (cr, pat);
    cairo_pattern_destroy (pat);
    cairo_paint (cr);

    /* record surface extents (-0.1*PAT_SIZE/2, -0.1*PAT_SIZE/2) to (0.1*PAT_SIZE/2, 0.1*PAT_SIZE/2) */
    cairo_translate (cr, PAT_SIZE + PAD, 0);
    cairo_matrix_init_translate (&mat, -0.1*PAT_SIZE/2, -0.1*PAT_SIZE/2);
    cairo_matrix_scale (&mat, 0.1, 0.1);
    pat = create_pattern (&mat, bounded);
    cairo_set_source (cr, pat);
    cairo_pattern_destroy (pat);
    cairo_paint (cr);

    /* record surface centered on (0,0) and rotated 45 deg */
    cairo_translate (cr, PAT_SIZE + PAD, 0);
    cairo_matrix_init_translate (&mat, -PAT_SIZE/sqrt(2), -PAT_SIZE/sqrt(2));
    cairo_matrix_rotate (&mat, M_PI/4.0);
    cairo_matrix_translate (&mat, PAT_SIZE/2, -PAT_SIZE/2);
    pat = create_pattern (&mat, bounded);
    cairo_set_source (cr, pat);
    cairo_pattern_destroy (pat);
    cairo_paint (cr);

    return CAIRO_TEST_SUCCESS;
}

static cairo_test_status_t
record_neg_extents_bounded (cairo_t *cr, int width, int height)
{
    return record_extents(cr, width, height, TRUE);
}

static cairo_test_status_t
record_neg_extents_unbounded (cairo_t *cr, int width, int height)
{
    return record_extents(cr, width, height, FALSE);
}


CAIRO_TEST (record_neg_extents_unbounded,
	    "Paint unbounded recording pattern with untransformed extents outside of target extents",
	    "record,transform,pattern", /* keywords */
	    NULL, /* requirements */
	    WIDTH, HEIGHT,
	    NULL, record_neg_extents_unbounded)
CAIRO_TEST (record_neg_extents_bounded,
	    "Paint bounded recording pattern with untransformed extents outside of target extents",
	    "record,transform,pattern", /* keywords */
	    NULL, /* requirements */
	    WIDTH, HEIGHT,
	    NULL, record_neg_extents_bounded)