1
/*
2
 * Copyright © 2005 Red Hat, Inc.
3
 * Copyright © 2011 Intel Corporation
4
 *
5
 * Permission to use, copy, modify, distribute, and sell this software
6
 * and its documentation for any purpose is hereby granted without
7
 * fee, provided that the above copyright notice appear in all copies
8
 * and that both that copyright notice and this permission notice
9
 * appear in supporting documentation, and that the name of
10
 * Red Hat, Inc. not be used in advertising or publicity pertaining to
11
 * distribution of the software without specific, written prior
12
 * permission. Red Hat, Inc. makes no representations about the
13
 * suitability of this software for any purpose.  It is provided "as
14
 * is" without express or implied warranty.
15
 *
16
 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18
 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
19
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
22
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23
 *
24
 * Authors:
25
 *	Carl D. Worth <cworth@cworth.org>
26
 *	Chris Wilson <chris@chris-wilson.co.uk>
27
 */
28

            
29
#include "cairo-test.h"
30

            
31
#ifndef M_SQRT2
32
#define M_SQRT2 1.41421345623730951
33
#endif
34

            
35
#define TEXT_SIZE 12
36
#define SIZE 60 /* needs to be big to check large area effects (dithering) */
37
#define PAD 2
38

            
39
#define TT_SIZE 100
40
#define TT_PAD 5
41
#define TT_FONT_SIZE 32.0
42

            
43
#define GENERATE_REF 0
44

            
45
static uint32_t data[16] = {
46
    0xffffffff, 0xffffffff,		0xffff0000, 0xffff0000,
47
    0xffffffff, 0xffffffff,		0xffff0000, 0xffff0000,
48

            
49
    0xff00ff00, 0xff00ff00,		0xff0000ff, 0xff0000ff,
50
    0xff00ff00, 0xff00ff00,		0xff0000ff, 0xff0000ff
51
};
52

            
53
static const char *png_filename = "romedalen.png";
54

            
55
static cairo_t *
56
3
paint (cairo_t *cr)
57
{
58
3
    cairo_set_source_rgb (cr, 0, 0, 1);
59
3
    cairo_paint (cr);
60

            
61
3
    cairo_translate (cr, 2, 2);
62
3
    cairo_scale (cr, 0.5, 0.5);
63

            
64
3
    cairo_set_source_rgb (cr, 1, 0, 0);
65
3
    cairo_paint (cr);
66

            
67
3
    return cr;
68
}
69

            
70
static cairo_t *
71
3
paint_alpha (cairo_t *cr)
72
{
73
    cairo_surface_t *surface;
74

            
75
3
    surface = cairo_image_surface_create_for_data ((unsigned char *) data,
76
						   CAIRO_FORMAT_RGB24, 4, 4, 16);
77

            
78
3
    cairo_test_paint_checkered (cr);
79

            
80
3
    cairo_scale (cr, 4, 4);
81

            
82
3
    cairo_set_source_surface (cr, surface, 2 , 2);
83
3
    cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
84
3
    cairo_paint_with_alpha (cr, 0.5);
85

            
86
3
    cairo_surface_finish (surface); /* data will go out of scope */
87
3
    cairo_surface_destroy (surface);
88

            
89
3
    return cr;
90
}
91

            
92
static cairo_t *
93
3
paint_alpha_solid_clip (cairo_t *cr)
94
{
95
3
    cairo_test_paint_checkered (cr);
96

            
97
3
    cairo_rectangle (cr, 2.5, 2.5, 27, 27);
98
3
    cairo_clip (cr);
99

            
100
3
    cairo_set_source_rgb (cr, 1., 0.,0.);
101
3
    cairo_paint_with_alpha (cr, 0.5);
102

            
103
3
    return cr;
104
}
105

            
106
static cairo_t *
107
3
paint_alpha_clip (cairo_t *cr)
108
{
109
    cairo_surface_t *surface;
110

            
111
3
    surface = cairo_image_surface_create_for_data ((unsigned char *) data,
112
						   CAIRO_FORMAT_RGB24, 4, 4, 16);
113

            
114
3
    cairo_test_paint_checkered (cr);
115

            
116
3
    cairo_rectangle (cr, 10.5, 10.5, 11, 11);
117
3
    cairo_clip (cr);
118

            
119
3
    cairo_scale (cr, 4, 4);
120

            
121
3
    cairo_set_source_surface (cr, surface, 2 , 2);
122
3
    cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
123
3
    cairo_paint_with_alpha (cr, 0.5);
124

            
125
3
    cairo_surface_finish (surface); /* data will go out of scope */
126
3
    cairo_surface_destroy (surface);
127

            
128
3
    return cr;
129
}
130

            
131
static cairo_t *
132
3
paint_alpha_clip_mask (cairo_t *cr)
133
{
134
    cairo_surface_t *surface;
135

            
136
3
    surface = cairo_image_surface_create_for_data ((unsigned char *) data,
137
						   CAIRO_FORMAT_RGB24, 4, 4, 16);
138

            
139
3
    cairo_test_paint_checkered (cr);
140

            
141
3
    cairo_move_to (cr, 16, 5);
142
3
    cairo_line_to (cr, 5, 16);
143
3
    cairo_line_to (cr, 16, 27);
144
3
    cairo_line_to (cr, 27, 16);
145
3
    cairo_clip (cr);
146

            
147
3
    cairo_scale (cr, 4, 4);
148

            
149
3
    cairo_set_source_surface (cr, surface, 2 , 2);
150
3
    cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
151
3
    cairo_paint_with_alpha (cr, 0.5);
152

            
153
3
    cairo_surface_finish (surface); /* data will go out of scope */
154
3
    cairo_surface_destroy (surface);
155

            
156
3
    return cr;
157
}
158

            
159
static cairo_t *
160
3
select_font_face (cairo_t *cr)
161
{
162
    /* We draw in the default black, so paint white first. */
163
3
    cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
164
3
    cairo_paint (cr);
165

            
166
3
    cairo_set_source_rgb (cr, 0, 0, 0); /* black */
167

            
168
3
    cairo_set_font_size (cr, TEXT_SIZE);
169
3
    cairo_move_to (cr, 0, TEXT_SIZE);
170

            
171
3
    cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Serif",
172
			    CAIRO_FONT_SLANT_NORMAL,
173
			    CAIRO_FONT_WEIGHT_NORMAL);
174
3
    cairo_show_text (cr, "i-am-serif");
175

            
176
3
    cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
177
			    CAIRO_FONT_SLANT_NORMAL,
178
			    CAIRO_FONT_WEIGHT_NORMAL);
179
3
    cairo_show_text (cr, " i-am-sans");
180

            
181
3
    cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans Mono",
182
			    CAIRO_FONT_SLANT_NORMAL,
183
			    CAIRO_FONT_WEIGHT_NORMAL);
184
3
    cairo_show_text (cr, " i-am-mono");
185

            
186
3
    return cr;
187
}
188

            
189
static cairo_t *
190
3
fill_alpha (cairo_t *cr)
191
{
192
3
    const double alpha = 1./3;
193
    int n;
194

            
195
    /* flatten to white */
196
3
    cairo_set_source_rgb (cr, 1, 1, 1);
197
3
    cairo_paint (cr);
198

            
199
    /* square */
200
3
    cairo_rectangle (cr, PAD, PAD, SIZE, SIZE);
201
3
    cairo_set_source_rgba (cr, 1, 0, 0, alpha);
202
3
    cairo_fill (cr);
203

            
204
    /* circle */
205
3
    cairo_translate (cr, SIZE + 2 * PAD, 0);
206
3
    cairo_arc (cr, PAD + SIZE / 2., PAD + SIZE / 2., SIZE / 2., 0, 2 * M_PI);
207
3
    cairo_set_source_rgba (cr, 0, 1, 0, alpha);
208
3
    cairo_fill (cr);
209

            
210
    /* triangle */
211
3
    cairo_translate (cr, 0, SIZE + 2 * PAD);
212
3
    cairo_move_to (cr, PAD + SIZE / 2, PAD);
213
3
    cairo_line_to (cr, PAD + SIZE, PAD + SIZE);
214
3
    cairo_line_to (cr, PAD, PAD + SIZE);
215
3
    cairo_set_source_rgba (cr, 0, 0, 1, alpha);
216
3
    cairo_fill (cr);
217

            
218
    /* star */
219
3
    cairo_translate (cr, -(SIZE + 2 * PAD) + SIZE/2., SIZE/2.);
220
18
    for (n = 0; n < 5; n++) {
221
15
	cairo_line_to (cr,
222
15
		       SIZE/2 * cos (2*n * 2*M_PI / 10),
223
15
		       SIZE/2 * sin (2*n * 2*M_PI / 10));
224

            
225
15
	cairo_line_to (cr,
226
15
		       SIZE/4 * cos ((2*n+1)*2*M_PI / 10),
227
15
		       SIZE/4 * sin ((2*n+1)*2*M_PI / 10));
228
    }
229
3
    cairo_set_source_rgba (cr, 0, 0, 0, alpha);
230
3
    cairo_fill (cr);
231

            
232
3
    return cr;
233
}
234

            
235
static cairo_t *
236
3
self_intersecting (cairo_t *cr)
237
{
238
3
    cairo_set_source_rgb (cr, 1, 1, 1);
239
3
    cairo_paint (cr);
240

            
241
3
    cairo_translate (cr, 1.0, 1.0);
242

            
243
3
    cairo_set_source_rgb (cr, 1, 0, 0); /* red */
244

            
245
    /* First draw the desired shape with a fill */
246
3
    cairo_rectangle (cr, 0.5, 0.5,  4.0, 4.0);
247
3
    cairo_rectangle (cr, 3.5, 3.5,  4.0, 4.0);
248
3
    cairo_rectangle (cr, 3.5, 1.5, -2.0, 2.0);
249
3
    cairo_rectangle (cr, 6.5, 4.5, -2.0, 2.0);
250

            
251
3
    cairo_fill (cr);
252

            
253
    /* Then try the same thing with a stroke */
254
3
    cairo_translate (cr, 0, 10);
255
3
    cairo_move_to (cr, 1.0, 1.0);
256
3
    cairo_rel_line_to (cr,  3.0,  0.0);
257
3
    cairo_rel_line_to (cr,  0.0,  6.0);
258
3
    cairo_rel_line_to (cr,  3.0,  0.0);
259
3
    cairo_rel_line_to (cr,  0.0, -3.0);
260
3
    cairo_rel_line_to (cr, -6.0,  0.0);
261
3
    cairo_close_path (cr);
262

            
263
3
    cairo_set_line_width (cr, 1.0);
264
3
    cairo_stroke (cr);
265

            
266
3
    return cr;
267
}
268

            
269
static void
270
6
draw_text_transform (cairo_t *cr)
271
{
272
    cairo_matrix_t tm;
273

            
274
    /* skew */
275
6
    cairo_matrix_init (&tm, 1, 0,
276
                       -0.25, 1,
277
                       0, 0);
278
6
    cairo_matrix_scale (&tm, TT_FONT_SIZE, TT_FONT_SIZE);
279
6
    cairo_set_font_matrix (cr, &tm);
280

            
281
6
    cairo_new_path (cr);
282
6
    cairo_move_to (cr, 50, TT_SIZE-TT_PAD);
283
6
    cairo_show_text (cr, "A");
284

            
285
    /* rotate and scale */
286
6
    cairo_matrix_init_rotate (&tm, M_PI / 2);
287
6
    cairo_matrix_scale (&tm, TT_FONT_SIZE, TT_FONT_SIZE * 2.0);
288
6
    cairo_set_font_matrix (cr, &tm);
289

            
290
6
    cairo_new_path (cr);
291
6
    cairo_move_to (cr, TT_PAD, TT_PAD + 25);
292
6
    cairo_show_text (cr, "A");
293

            
294
6
    cairo_matrix_init_rotate (&tm, M_PI / 2);
295
6
    cairo_matrix_scale (&tm, TT_FONT_SIZE * 2.0, TT_FONT_SIZE);
296
6
    cairo_set_font_matrix (cr, &tm);
297

            
298
6
    cairo_new_path (cr);
299
6
    cairo_move_to (cr, TT_PAD, TT_PAD + 50);
300
6
    cairo_show_text (cr, "A");
301
6
}
302

            
303
static cairo_t *
304
3
text_transform (cairo_t *cr)
305
{
306
3
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
307
    cairo_pattern_t *pattern;
308

            
309
3
    cairo_set_source_rgb (cr, 1., 1., 1.);
310
3
    cairo_paint (cr);
311

            
312
3
    cairo_set_source_rgb (cr, 0., 0., 0.);
313

            
314
3
    cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
315
			    CAIRO_FONT_SLANT_NORMAL,
316
			    CAIRO_FONT_WEIGHT_NORMAL);
317

            
318
3
    draw_text_transform (cr);
319

            
320
3
    cairo_translate (cr, TT_SIZE, TT_SIZE);
321
3
    cairo_rotate (cr, M_PI);
322

            
323
3
    pattern = cairo_test_create_pattern_from_png (ctx, png_filename);
324
3
    cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
325
3
    cairo_set_source (cr, pattern);
326
3
    cairo_pattern_destroy (pattern);
327

            
328
3
    draw_text_transform (cr);
329

            
330
3
    return cr;
331
}
332

            
333
/* And here begins the recording and replaying... */
334

            
335
static cairo_t *
336
27
record_create (cairo_t *target)
337
{
338
    cairo_surface_t *surface;
339
    cairo_t *cr;
340

            
341
27
    surface = cairo_recording_surface_create (cairo_surface_get_content (cairo_get_target (target)), NULL);
342
27
    cr = cairo_test_create (surface, cairo_test_get_context (target));
343
27
    cairo_surface_destroy (surface);
344

            
345
27
    return cr;
346
}
347

            
348
static cairo_surface_t *
349
27
record_get (cairo_t *target)
350
{
351
    cairo_surface_t *surface;
352

            
353
27
    surface = cairo_surface_reference (cairo_get_target (target));
354
27
    cairo_destroy (target);
355

            
356
27
    return surface;
357
}
358

            
359
static cairo_test_status_t
360
27
record_replay (cairo_t *cr, cairo_t *(*func)(cairo_t *), int width, int height)
361
{
362
    cairo_surface_t *surface;
363
    int x, y;
364

            
365
#if GENERATE_REF
366
    cairo_scale (cr, M_SQRT2, M_SQRT2);
367
    func (cr);
368
#else
369
27
    surface = record_get (func (record_create (cr)));
370

            
371
27
    cairo_scale (cr, M_SQRT2, M_SQRT2);
372
27
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
373
27
    cairo_set_source_surface (cr, surface, 0, 0);
374
27
    cairo_surface_destroy (surface);
375
27
    cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_NONE);
376

            
377
27
    cairo_identity_matrix (cr); /* make sure the clip is pixel-aligned */
378
882
    for (y = 0; y < height; y += 2) {
379
52059
	for (x = 0; x < width; x += 2) {
380
51204
	    cairo_rectangle (cr, x, y, 2, 2);
381
51204
	    cairo_clip (cr);
382
51204
	    cairo_paint (cr);
383
51204
	    cairo_reset_clip (cr);
384
	}
385
    }
386
#endif
387

            
388
27
    return CAIRO_TEST_SUCCESS;
389
}
390

            
391
static cairo_test_status_t
392
3
record_paint (cairo_t *cr, int width, int height)
393
{
394
3
    return record_replay (cr, paint, width, height);
395
}
396

            
397
static cairo_test_status_t
398
3
record_paint_alpha (cairo_t *cr, int width, int height)
399
{
400
3
    return record_replay (cr, paint_alpha, width, height);
401
}
402

            
403
static cairo_test_status_t
404
3
record_paint_alpha_solid_clip (cairo_t *cr, int width, int height)
405
{
406
3
    return record_replay (cr, paint_alpha_solid_clip, width, height);
407
}
408

            
409
static cairo_test_status_t
410
3
record_paint_alpha_clip (cairo_t *cr, int width, int height)
411
{
412
3
    return record_replay (cr, paint_alpha_clip, width, height);
413
}
414

            
415
static cairo_test_status_t
416
3
record_paint_alpha_clip_mask (cairo_t *cr, int width, int height)
417
{
418
3
    return record_replay (cr, paint_alpha_clip_mask, width, height);
419
}
420

            
421
static cairo_test_status_t
422
3
record_fill_alpha (cairo_t *cr, int width, int height)
423
{
424
3
    return record_replay (cr, fill_alpha, width, height);
425
}
426

            
427
static cairo_test_status_t
428
3
record_self_intersecting (cairo_t *cr, int width, int height)
429
{
430
3
    return record_replay (cr, self_intersecting, width, height);
431
}
432

            
433
static cairo_test_status_t
434
3
record_select_font_face (cairo_t *cr, int width, int height)
435
{
436
3
    return record_replay (cr, select_font_face, width, height);
437
}
438

            
439
static cairo_test_status_t
440
3
record_text_transform (cairo_t *cr, int width, int height)
441
{
442
3
    return record_replay (cr, text_transform, width, height);
443
}
444

            
445
1
CAIRO_TEST (record1414x_paint,
446
	    "Test replayed calls to cairo_paint",
447
	    "paint, record", /* keywords */
448
	    NULL, /* requirements */
449
	    M_SQRT2*8, M_SQRT2*8,
450
	    NULL, record_paint)
451
1
CAIRO_TEST (record1414x_paint_alpha,
452
	    "Simple test of cairo_paint_with_alpha",
453
	    "record, paint, alpha", /* keywords */
454
	    NULL, /* requirements */
455
	    M_SQRT2*32, M_SQRT2*32,
456
	    NULL, record_paint_alpha)
457
1
CAIRO_TEST (record1414x_paint_alpha_solid_clip,
458
	    "Simple test of cairo_paint_with_alpha+unaligned clip",
459
	    "record, paint, alpha, clip", /* keywords */
460
	    NULL, /* requirements */
461
	    M_SQRT2*32, M_SQRT2*32,
462
	    NULL, record_paint_alpha_solid_clip)
463
1
CAIRO_TEST (record1414x_paint_alpha_clip,
464
	    "Simple test of cairo_paint_with_alpha+unaligned clip",
465
	    "record, paint, alpha, clip", /* keywords */
466
	    NULL, /* requirements */
467
	    M_SQRT2*32, M_SQRT2*32,
468
	    NULL, record_paint_alpha_clip)
469
1
CAIRO_TEST (record1414x_paint_alpha_clip_mask,
470
	    "Simple test of cairo_paint_with_alpha+triangular clip",
471
	    "record, paint, alpha, clip", /* keywords */
472
	    NULL, /* requirements */
473
	    M_SQRT2*32, M_SQRT2*32,
474
	    NULL, record_paint_alpha_clip_mask)
475
1
CAIRO_TEST (record1414x_fill_alpha,
476
	    "Tests using set_rgba();fill()",
477
	    "record, fill, alpha", /* keywords */
478
	    NULL, /* requirements */
479
	    M_SQRT2*(2*SIZE + 4*PAD), M_SQRT2*(2*SIZE + 4*PAD),
480
	    NULL, record_fill_alpha)
481
1
CAIRO_TEST (record1414x_select_font_face,
482
	    "Tests using cairo_select_font_face to draw text in different faces",
483
	    "record, font", /* keywords */
484
	    NULL, /* requirements */
485
	    M_SQRT2*192, M_SQRT2*(TEXT_SIZE + 4),
486
	    NULL, record_select_font_face)
487
1
CAIRO_TEST (record1414x_self_intersecting,
488
	    "Test strokes of self-intersecting paths",
489
	    "record, stroke, trap", /* keywords */
490
	    NULL, /* requirements */
491
	    M_SQRT2*10, M_SQRT2*20,
492
	    NULL, record_self_intersecting)
493
1
CAIRO_TEST (record1414x_text_transform,
494
	    "Test various applications of the font matrix",
495
	    "record, text, transform", /* keywords */
496
	    NULL, /* requirements */
497
	    M_SQRT2*TT_SIZE, M_SQRT2*TT_SIZE,
498
	    NULL, record_text_transform)