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_translate(cr, width, 0);
363
    cairo_rotate (cr, M_PI/2);
364
    func(cr);
365
#else
366
27
    surface = record_get (func (record_create (cr)));
367

            
368
27
    cairo_translate(cr, width, 0);
369
27
    cairo_rotate (cr, M_PI/2);
370
27
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
371
27
    cairo_set_source_surface (cr, surface, 0, 0);
372
27
    cairo_surface_destroy (surface);
373
27
    cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_NONE);
374

            
375
582
    for (y = 0; y < width; y += 2) {
376
25197
	for (x = 0; x < height; x += 2) {
377
24642
	    cairo_rectangle (cr, x, y, 2, 2);
378
24642
	    cairo_clip (cr);
379
24642
	    cairo_paint (cr);
380
24642
	    cairo_reset_clip (cr);
381
	}
382
    }
383
#endif
384

            
385
27
    return CAIRO_TEST_SUCCESS;
386
}
387

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

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

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

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

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

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

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

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

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

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