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
#define TEXT_SIZE 12
32
#define SIZE 60 /* needs to be big to check large area effects (dithering) */
33
#define PAD 2
34

            
35
#define TT_SIZE 100
36
#define TT_PAD 5
37
#define TT_FONT_SIZE 32.0
38

            
39
#define GENERATE_REF 0
40

            
41
static uint32_t data[16] = {
42
    0xffffffff, 0xffffffff,		0xffff0000, 0xffff0000,
43
    0xffffffff, 0xffffffff,		0xffff0000, 0xffff0000,
44

            
45
    0xff00ff00, 0xff00ff00,		0xff0000ff, 0xff0000ff,
46
    0xff00ff00, 0xff00ff00,		0xff0000ff, 0xff0000ff
47
};
48

            
49
static const char *png_filename = "romedalen.png";
50

            
51
static cairo_t *
52
3
paint (cairo_t *cr)
53
{
54
3
    cairo_set_source_rgb (cr, 0, 0, 1);
55
3
    cairo_paint (cr);
56

            
57
3
    cairo_translate (cr, 2, 2);
58
3
    cairo_scale (cr, 0.5, 0.5);
59

            
60
3
    cairo_set_source_rgb (cr, 1, 0, 0);
61
3
    cairo_paint (cr);
62

            
63
3
    return cr;
64
}
65

            
66
static cairo_t *
67
3
paint_alpha (cairo_t *cr)
68
{
69
    cairo_surface_t *surface;
70

            
71
3
    surface = cairo_image_surface_create_for_data ((unsigned char *) data,
72
						   CAIRO_FORMAT_RGB24, 4, 4, 16);
73

            
74
3
    cairo_test_paint_checkered (cr);
75

            
76
3
    cairo_scale (cr, 4, 4);
77

            
78
3
    cairo_set_source_surface (cr, surface, 2 , 2);
79
3
    cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
80
3
    cairo_paint_with_alpha (cr, 0.5);
81

            
82
3
    cairo_surface_finish (surface); /* data will go out of scope */
83
3
    cairo_surface_destroy (surface);
84

            
85
3
    return cr;
86
}
87

            
88
static cairo_t *
89
3
paint_alpha_solid_clip (cairo_t *cr)
90
{
91
3
    cairo_test_paint_checkered (cr);
92

            
93
3
    cairo_rectangle (cr, 2.5, 2.5, 27, 27);
94
3
    cairo_clip (cr);
95

            
96
3
    cairo_set_source_rgb (cr, 1., 0.,0.);
97
3
    cairo_paint_with_alpha (cr, 0.5);
98

            
99
3
    return cr;
100
}
101

            
102
static cairo_t *
103
3
paint_alpha_clip (cairo_t *cr)
104
{
105
    cairo_surface_t *surface;
106

            
107
3
    surface = cairo_image_surface_create_for_data ((unsigned char *) data,
108
						   CAIRO_FORMAT_RGB24, 4, 4, 16);
109

            
110
3
    cairo_test_paint_checkered (cr);
111

            
112
3
    cairo_rectangle (cr, 10.5, 10.5, 11, 11);
113
3
    cairo_clip (cr);
114

            
115
3
    cairo_scale (cr, 4, 4);
116

            
117
3
    cairo_set_source_surface (cr, surface, 2 , 2);
118
3
    cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
119
3
    cairo_paint_with_alpha (cr, 0.5);
120

            
121
3
    cairo_surface_finish (surface); /* data will go out of scope */
122
3
    cairo_surface_destroy (surface);
123

            
124
3
    return cr;
125
}
126

            
127
static cairo_t *
128
3
paint_alpha_clip_mask (cairo_t *cr)
129
{
130
    cairo_surface_t *surface;
131

            
132
3
    surface = cairo_image_surface_create_for_data ((unsigned char *) data,
133
						   CAIRO_FORMAT_RGB24, 4, 4, 16);
134

            
135
3
    cairo_test_paint_checkered (cr);
136

            
137
3
    cairo_move_to (cr, 16, 5);
138
3
    cairo_line_to (cr, 5, 16);
139
3
    cairo_line_to (cr, 16, 27);
140
3
    cairo_line_to (cr, 27, 16);
141
3
    cairo_clip (cr);
142

            
143
3
    cairo_scale (cr, 4, 4);
144

            
145
3
    cairo_set_source_surface (cr, surface, 2 , 2);
146
3
    cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
147
3
    cairo_paint_with_alpha (cr, 0.5);
148

            
149
3
    cairo_surface_finish (surface); /* data will go out of scope */
150
3
    cairo_surface_destroy (surface);
151

            
152
3
    return cr;
153
}
154

            
155
static cairo_t *
156
3
select_font_face (cairo_t *cr)
157
{
158
    /* We draw in the default black, so paint white first. */
159
3
    cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
160
3
    cairo_paint (cr);
161

            
162
3
    cairo_set_source_rgb (cr, 0, 0, 0); /* black */
163

            
164
3
    cairo_set_font_size (cr, TEXT_SIZE);
165
3
    cairo_move_to (cr, 0, TEXT_SIZE);
166

            
167
3
    cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Serif",
168
			    CAIRO_FONT_SLANT_NORMAL,
169
			    CAIRO_FONT_WEIGHT_NORMAL);
170
3
    cairo_show_text (cr, "i-am-serif");
171

            
172
3
    cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
173
			    CAIRO_FONT_SLANT_NORMAL,
174
			    CAIRO_FONT_WEIGHT_NORMAL);
175
3
    cairo_show_text (cr, " i-am-sans");
176

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

            
182
3
    return cr;
183
}
184

            
185
static cairo_t *
186
3
fill_alpha (cairo_t *cr)
187
{
188
3
    const double alpha = 1./3;
189
    int n;
190

            
191
    /* flatten to white */
192
3
    cairo_set_source_rgb (cr, 1, 1, 1);
193
3
    cairo_paint (cr);
194

            
195
    /* square */
196
3
    cairo_rectangle (cr, PAD, PAD, SIZE, SIZE);
197
3
    cairo_set_source_rgba (cr, 1, 0, 0, alpha);
198
3
    cairo_fill (cr);
199

            
200
    /* circle */
201
3
    cairo_translate (cr, SIZE + 2 * PAD, 0);
202
3
    cairo_arc (cr, PAD + SIZE / 2., PAD + SIZE / 2., SIZE / 2., 0, 2 * M_PI);
203
3
    cairo_set_source_rgba (cr, 0, 1, 0, alpha);
204
3
    cairo_fill (cr);
205

            
206
    /* triangle */
207
3
    cairo_translate (cr, 0, SIZE + 2 * PAD);
208
3
    cairo_move_to (cr, PAD + SIZE / 2, PAD);
209
3
    cairo_line_to (cr, PAD + SIZE, PAD + SIZE);
210
3
    cairo_line_to (cr, PAD, PAD + SIZE);
211
3
    cairo_set_source_rgba (cr, 0, 0, 1, alpha);
212
3
    cairo_fill (cr);
213

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

            
221
15
	cairo_line_to (cr,
222
15
		       SIZE/4 * cos ((2*n+1)*2*M_PI / 10),
223
15
		       SIZE/4 * sin ((2*n+1)*2*M_PI / 10));
224
    }
225
3
    cairo_set_source_rgba (cr, 0, 0, 0, alpha);
226
3
    cairo_fill (cr);
227

            
228
3
    return cr;
229
}
230

            
231
static cairo_t *
232
3
self_intersecting (cairo_t *cr)
233
{
234
3
    cairo_set_source_rgb (cr, 1, 1, 1);
235
3
    cairo_paint (cr);
236

            
237
3
    cairo_translate (cr, 1.0, 1.0);
238

            
239
3
    cairo_set_source_rgb (cr, 1, 0, 0); /* red */
240

            
241
    /* First draw the desired shape with a fill */
242
3
    cairo_rectangle (cr, 0.5, 0.5,  4.0, 4.0);
243
3
    cairo_rectangle (cr, 3.5, 3.5,  4.0, 4.0);
244
3
    cairo_rectangle (cr, 3.5, 1.5, -2.0, 2.0);
245
3
    cairo_rectangle (cr, 6.5, 4.5, -2.0, 2.0);
246

            
247
3
    cairo_fill (cr);
248

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

            
259
3
    cairo_set_line_width (cr, 1.0);
260
3
    cairo_stroke (cr);
261

            
262
3
    return cr;
263
}
264

            
265
static void
266
6
draw_text_transform (cairo_t *cr)
267
{
268
    cairo_matrix_t tm;
269

            
270
    /* skew */
271
6
    cairo_matrix_init (&tm, 1, 0,
272
                       -0.25, 1,
273
                       0, 0);
274
6
    cairo_matrix_scale (&tm, TT_FONT_SIZE, TT_FONT_SIZE);
275
6
    cairo_set_font_matrix (cr, &tm);
276

            
277
6
    cairo_new_path (cr);
278
6
    cairo_move_to (cr, 50, TT_SIZE-TT_PAD);
279
6
    cairo_show_text (cr, "A");
280

            
281
    /* rotate and scale */
282
6
    cairo_matrix_init_rotate (&tm, M_PI / 2);
283
6
    cairo_matrix_scale (&tm, TT_FONT_SIZE, TT_FONT_SIZE * 2.0);
284
6
    cairo_set_font_matrix (cr, &tm);
285

            
286
6
    cairo_new_path (cr);
287
6
    cairo_move_to (cr, TT_PAD, TT_PAD + 25);
288
6
    cairo_show_text (cr, "A");
289

            
290
6
    cairo_matrix_init_rotate (&tm, M_PI / 2);
291
6
    cairo_matrix_scale (&tm, TT_FONT_SIZE * 2.0, TT_FONT_SIZE);
292
6
    cairo_set_font_matrix (cr, &tm);
293

            
294
6
    cairo_new_path (cr);
295
6
    cairo_move_to (cr, TT_PAD, TT_PAD + 50);
296
6
    cairo_show_text (cr, "A");
297
6
}
298

            
299
static cairo_t *
300
3
text_transform (cairo_t *cr)
301
{
302
3
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
303
    cairo_pattern_t *pattern;
304

            
305
3
    cairo_set_source_rgb (cr, 1., 1., 1.);
306
3
    cairo_paint (cr);
307

            
308
3
    cairo_set_source_rgb (cr, 0., 0., 0.);
309

            
310
3
    cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
311
			    CAIRO_FONT_SLANT_NORMAL,
312
			    CAIRO_FONT_WEIGHT_NORMAL);
313

            
314
3
    draw_text_transform (cr);
315

            
316
3
    cairo_translate (cr, TT_SIZE, TT_SIZE);
317
3
    cairo_rotate (cr, M_PI);
318

            
319
3
    pattern = cairo_test_create_pattern_from_png (ctx, png_filename);
320
3
    cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
321
3
    cairo_set_source (cr, pattern);
322
3
    cairo_pattern_destroy (pattern);
323

            
324
3
    draw_text_transform (cr);
325

            
326
3
    return cr;
327
}
328

            
329
/* And here begins the recording and replaying... */
330

            
331
static cairo_t *
332
27
record_create (cairo_t *target)
333
{
334
    cairo_surface_t *surface;
335
    cairo_t *cr;
336

            
337
27
    surface = cairo_recording_surface_create (cairo_surface_get_content (cairo_get_target (target)), NULL);
338
27
    cr = cairo_test_create (surface, cairo_test_get_context (target));
339
27
    cairo_surface_destroy (surface);
340

            
341
27
    return cr;
342
}
343

            
344
static cairo_surface_t *
345
27
record_get (cairo_t *target)
346
{
347
    cairo_surface_t *surface;
348

            
349
27
    surface = cairo_surface_reference (cairo_get_target (target));
350
27
    cairo_destroy (target);
351

            
352
27
    return surface;
353
}
354

            
355
static cairo_test_status_t
356
27
record_replay (cairo_t *cr, cairo_t *(*func)(cairo_t *), int width, int height)
357
{
358
    cairo_surface_t *surface;
359
    int x, y;
360

            
361
#if GENERATE_REF
362
    cairo_scale (cr, 2, 2);
363
    func(cr);
364
#else
365
27
    surface = record_get (func (record_create (cr)));
366

            
367
27
    cairo_scale (cr, 2, 2);
368
27
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
369
27
    cairo_set_source_surface (cr, surface, 0, 0);
370
27
    cairo_surface_destroy (surface);
371
27
    cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_NONE);
372

            
373
1227
    for (y = 0; y < height; y += 2) {
374
102648
	for (x = 0; x < width; x += 2) {
375
101448
	    cairo_rectangle (cr, x, y, 2, 2);
376
101448
	    cairo_clip (cr);
377
101448
	    cairo_paint (cr);
378
101448
	    cairo_reset_clip (cr);
379
	}
380
    }
381
#endif
382

            
383
27
    return CAIRO_TEST_SUCCESS;
384
}
385

            
386
static cairo_test_status_t
387
3
record_paint (cairo_t *cr, int width, int height)
388
{
389
3
    return record_replay (cr, paint, width, height);
390
}
391

            
392
static cairo_test_status_t
393
3
record_paint_alpha (cairo_t *cr, int width, int height)
394
{
395
3
    return record_replay (cr, paint_alpha, width, height);
396
}
397

            
398
static cairo_test_status_t
399
3
record_paint_alpha_solid_clip (cairo_t *cr, int width, int height)
400
{
401
3
    return record_replay (cr, paint_alpha_solid_clip, width, height);
402
}
403

            
404
static cairo_test_status_t
405
3
record_paint_alpha_clip (cairo_t *cr, int width, int height)
406
{
407
3
    return record_replay (cr, paint_alpha_clip, width, height);
408
}
409

            
410
static cairo_test_status_t
411
3
record_paint_alpha_clip_mask (cairo_t *cr, int width, int height)
412
{
413
3
    return record_replay (cr, paint_alpha_clip_mask, width, height);
414
}
415

            
416
static cairo_test_status_t
417
3
record_fill_alpha (cairo_t *cr, int width, int height)
418
{
419
3
    return record_replay (cr, fill_alpha, width, height);
420
}
421

            
422
static cairo_test_status_t
423
3
record_self_intersecting (cairo_t *cr, int width, int height)
424
{
425
3
    return record_replay (cr, self_intersecting, width, height);
426
}
427

            
428
static cairo_test_status_t
429
3
record_select_font_face (cairo_t *cr, int width, int height)
430
{
431
3
    return record_replay (cr, select_font_face, width, height);
432
}
433

            
434
static cairo_test_status_t
435
3
record_text_transform (cairo_t *cr, int width, int height)
436
{
437
3
    return record_replay (cr, text_transform, width, height);
438
}
439

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