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 *unique_id = "data";
50

            
51
static const char *png_filename = "romedalen.png";
52

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

            
59
3
    cairo_translate (cr, 2, 2);
60
3
    cairo_scale (cr, 0.5, 0.5);
61

            
62
3
    cairo_set_source_rgb (cr, 1, 0, 0);
63
3
    cairo_paint (cr);
64

            
65
3
    return cr;
66
}
67

            
68
static cairo_t *
69
3
paint_alpha (cairo_t *cr)
70
{
71
    cairo_surface_t *surface;
72

            
73
3
    surface = cairo_image_surface_create_for_data ((unsigned char *) data,
74
						   CAIRO_FORMAT_RGB24, 4, 4, 16);
75
3
    cairo_surface_set_mime_data (surface, CAIRO_MIME_TYPE_UNIQUE_ID,
76
				 (unsigned char *)unique_id, strlen(unique_id),
77
				 NULL, NULL);
78

            
79
3
    cairo_test_paint_checkered (cr);
80

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

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

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

            
90
3
    return cr;
91
}
92

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

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

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

            
104
3
    return cr;
105
}
106

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

            
112
3
    surface = cairo_image_surface_create_for_data ((unsigned char *) data,
113
						   CAIRO_FORMAT_RGB24, 4, 4, 16);
114
3
    cairo_surface_set_mime_data (surface, CAIRO_MIME_TYPE_UNIQUE_ID,
115
				 (unsigned char *)unique_id, strlen(unique_id),
116
				 NULL, NULL);
117

            
118
3
    cairo_test_paint_checkered (cr);
119

            
120
3
    cairo_rectangle (cr, 10.5, 10.5, 11, 11);
121
3
    cairo_clip (cr);
122

            
123
3
    cairo_scale (cr, 4, 4);
124

            
125
3
    cairo_set_source_surface (cr, surface, 2 , 2);
126
3
    cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
127
3
    cairo_paint_with_alpha (cr, 0.5);
128

            
129
3
    cairo_surface_finish (surface); /* data will go out of scope */
130
3
    cairo_surface_destroy (surface);
131

            
132
3
    return cr;
133
}
134

            
135
static cairo_t *
136
3
paint_alpha_clip_mask (cairo_t *cr)
137
{
138
    cairo_surface_t *surface;
139

            
140
3
    surface = cairo_image_surface_create_for_data ((unsigned char *) data,
141
						   CAIRO_FORMAT_RGB24, 4, 4, 16);
142
3
    cairo_surface_set_mime_data (surface, CAIRO_MIME_TYPE_UNIQUE_ID,
143
				 (unsigned char *)unique_id, strlen(unique_id),
144
				 NULL, NULL);
145

            
146
3
    cairo_test_paint_checkered (cr);
147

            
148
3
    cairo_move_to (cr, 16, 5);
149
3
    cairo_line_to (cr, 5, 16);
150
3
    cairo_line_to (cr, 16, 27);
151
3
    cairo_line_to (cr, 27, 16);
152
3
    cairo_clip (cr);
153

            
154
3
    cairo_scale (cr, 4, 4);
155

            
156
3
    cairo_set_source_surface (cr, surface, 2 , 2);
157
3
    cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
158
3
    cairo_paint_with_alpha (cr, 0.5);
159

            
160
3
    cairo_surface_finish (surface); /* data will go out of scope */
161
3
    cairo_surface_destroy (surface);
162

            
163
3
    return cr;
164
}
165

            
166
static cairo_t *
167
3
select_font_face (cairo_t *cr)
168
{
169
    /* We draw in the default black, so paint white first. */
170
3
    cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
171
3
    cairo_paint (cr);
172

            
173
3
    cairo_set_source_rgb (cr, 0, 0, 0); /* black */
174

            
175
3
    cairo_set_font_size (cr, TEXT_SIZE);
176
3
    cairo_move_to (cr, 0, TEXT_SIZE);
177

            
178
3
    cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Serif",
179
			    CAIRO_FONT_SLANT_NORMAL,
180
			    CAIRO_FONT_WEIGHT_NORMAL);
181
3
    cairo_show_text (cr, "i-am-serif");
182

            
183
3
    cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
184
			    CAIRO_FONT_SLANT_NORMAL,
185
			    CAIRO_FONT_WEIGHT_NORMAL);
186
3
    cairo_show_text (cr, " i-am-sans");
187

            
188
3
    cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans Mono",
189
			    CAIRO_FONT_SLANT_NORMAL,
190
			    CAIRO_FONT_WEIGHT_NORMAL);
191
3
    cairo_show_text (cr, " i-am-mono");
192

            
193
3
    return cr;
194
}
195

            
196
static cairo_t *
197
3
fill_alpha (cairo_t *cr)
198
{
199
3
    const double alpha = 1./3;
200
    int n;
201

            
202
    /* flatten to white */
203
3
    cairo_set_source_rgb (cr, 1, 1, 1);
204
3
    cairo_paint (cr);
205

            
206
    /* square */
207
3
    cairo_rectangle (cr, PAD, PAD, SIZE, SIZE);
208
3
    cairo_set_source_rgba (cr, 1, 0, 0, alpha);
209
3
    cairo_fill (cr);
210

            
211
    /* circle */
212
3
    cairo_translate (cr, SIZE + 2 * PAD, 0);
213
3
    cairo_arc (cr, PAD + SIZE / 2., PAD + SIZE / 2., SIZE / 2., 0, 2 * M_PI);
214
3
    cairo_set_source_rgba (cr, 0, 1, 0, alpha);
215
3
    cairo_fill (cr);
216

            
217
    /* triangle */
218
3
    cairo_translate (cr, 0, SIZE + 2 * PAD);
219
3
    cairo_move_to (cr, PAD + SIZE / 2, PAD);
220
3
    cairo_line_to (cr, PAD + SIZE, PAD + SIZE);
221
3
    cairo_line_to (cr, PAD, PAD + SIZE);
222
3
    cairo_set_source_rgba (cr, 0, 0, 1, alpha);
223
3
    cairo_fill (cr);
224

            
225
    /* star */
226
3
    cairo_translate (cr, -(SIZE + 2 * PAD) + SIZE/2., SIZE/2.);
227
18
    for (n = 0; n < 5; n++) {
228
15
	cairo_line_to (cr,
229
15
		       SIZE/2 * cos (2*n * 2*M_PI / 10),
230
15
		       SIZE/2 * sin (2*n * 2*M_PI / 10));
231

            
232
15
	cairo_line_to (cr,
233
15
		       SIZE/4 * cos ((2*n+1)*2*M_PI / 10),
234
15
		       SIZE/4 * sin ((2*n+1)*2*M_PI / 10));
235
    }
236
3
    cairo_set_source_rgba (cr, 0, 0, 0, alpha);
237
3
    cairo_fill (cr);
238

            
239
3
    return cr;
240
}
241

            
242
static cairo_t *
243
3
self_intersecting (cairo_t *cr)
244
{
245
3
    cairo_set_source_rgb (cr, 1, 1, 1);
246
3
    cairo_paint (cr);
247

            
248
3
    cairo_translate (cr, 1.0, 1.0);
249

            
250
3
    cairo_set_source_rgb (cr, 1, 0, 0); /* red */
251

            
252
    /* First draw the desired shape with a fill */
253
3
    cairo_rectangle (cr, 0.5, 0.5,  4.0, 4.0);
254
3
    cairo_rectangle (cr, 3.5, 3.5,  4.0, 4.0);
255
3
    cairo_rectangle (cr, 3.5, 1.5, -2.0, 2.0);
256
3
    cairo_rectangle (cr, 6.5, 4.5, -2.0, 2.0);
257

            
258
3
    cairo_fill (cr);
259

            
260
    /* Then try the same thing with a stroke */
261
3
    cairo_translate (cr, 0, 10);
262
3
    cairo_move_to (cr, 1.0, 1.0);
263
3
    cairo_rel_line_to (cr,  3.0,  0.0);
264
3
    cairo_rel_line_to (cr,  0.0,  6.0);
265
3
    cairo_rel_line_to (cr,  3.0,  0.0);
266
3
    cairo_rel_line_to (cr,  0.0, -3.0);
267
3
    cairo_rel_line_to (cr, -6.0,  0.0);
268
3
    cairo_close_path (cr);
269

            
270
3
    cairo_set_line_width (cr, 1.0);
271
3
    cairo_stroke (cr);
272

            
273
3
    return cr;
274
}
275

            
276
static void
277
6
draw_text_transform (cairo_t *cr)
278
{
279
    cairo_matrix_t tm;
280

            
281
    /* skew */
282
6
    cairo_matrix_init (&tm, 1, 0,
283
                       -0.25, 1,
284
                       0, 0);
285
6
    cairo_matrix_scale (&tm, TT_FONT_SIZE, TT_FONT_SIZE);
286
6
    cairo_set_font_matrix (cr, &tm);
287

            
288
6
    cairo_new_path (cr);
289
6
    cairo_move_to (cr, 50, TT_SIZE-TT_PAD);
290
6
    cairo_show_text (cr, "A");
291

            
292
    /* rotate and scale */
293
6
    cairo_matrix_init_rotate (&tm, M_PI / 2);
294
6
    cairo_matrix_scale (&tm, TT_FONT_SIZE, TT_FONT_SIZE * 2.0);
295
6
    cairo_set_font_matrix (cr, &tm);
296

            
297
6
    cairo_new_path (cr);
298
6
    cairo_move_to (cr, TT_PAD, TT_PAD + 25);
299
6
    cairo_show_text (cr, "A");
300

            
301
6
    cairo_matrix_init_rotate (&tm, M_PI / 2);
302
6
    cairo_matrix_scale (&tm, TT_FONT_SIZE * 2.0, TT_FONT_SIZE);
303
6
    cairo_set_font_matrix (cr, &tm);
304

            
305
6
    cairo_new_path (cr);
306
6
    cairo_move_to (cr, TT_PAD, TT_PAD + 50);
307
6
    cairo_show_text (cr, "A");
308
6
}
309

            
310
static cairo_t *
311
3
text_transform (cairo_t *cr)
312
{
313
3
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
314
    cairo_pattern_t *pattern;
315

            
316
3
    cairo_set_source_rgb (cr, 1., 1., 1.);
317
3
    cairo_paint (cr);
318

            
319
3
    cairo_set_source_rgb (cr, 0., 0., 0.);
320

            
321
3
    cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
322
			    CAIRO_FONT_SLANT_NORMAL,
323
			    CAIRO_FONT_WEIGHT_NORMAL);
324

            
325
3
    draw_text_transform (cr);
326

            
327
3
    cairo_translate (cr, TT_SIZE, TT_SIZE);
328
3
    cairo_rotate (cr, M_PI);
329

            
330
3
    pattern = cairo_test_create_pattern_from_png (ctx, png_filename);
331
3
    cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
332
3
    cairo_set_source (cr, pattern);
333
3
    cairo_pattern_destroy (pattern);
334

            
335
3
    draw_text_transform (cr);
336

            
337
3
    return cr;
338
}
339

            
340
/* And here begins the recording and replaying... */
341

            
342
static cairo_t *
343
27
record_create (cairo_t *target)
344
{
345
    cairo_surface_t *surface;
346
    cairo_t *cr;
347

            
348
27
    surface = cairo_recording_surface_create (cairo_surface_get_content (cairo_get_target (target)), NULL);
349
27
    cr = cairo_test_create (surface, cairo_test_get_context (target));
350
27
    cairo_surface_destroy (surface);
351

            
352
27
    return cr;
353
}
354

            
355
static cairo_surface_t *
356
27
record_get (cairo_t *target)
357
{
358
    cairo_surface_t *surface;
359

            
360
27
    surface = cairo_surface_reference (cairo_get_target (target));
361
27
    cairo_destroy (target);
362

            
363
27
    return surface;
364
}
365

            
366
static cairo_test_status_t
367
27
record_replay (cairo_t *cr, cairo_t *(*func)(cairo_t *), int width, int height)
368
{
369
    cairo_surface_t *surface;
370
    int x, y;
371

            
372
#if GENERATE_REF
373
    func(cr);
374
#else
375
27
    surface = record_get (func (record_create (cr)));
376

            
377
27
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
378
27
    cairo_set_source_surface (cr, surface, 0, 0);
379
27
    cairo_surface_destroy (surface);
380
27
    cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_NONE);
381

            
382
627
    for (y = 0; y < height; y += 2) {
383
25962
	for (x = 0; x < width; x += 2) {
384
25362
	    cairo_rectangle (cr, x, y, 2, 2);
385
25362
	    cairo_clip (cr);
386
25362
	    cairo_paint (cr);
387
25362
	    cairo_reset_clip (cr);
388
	}
389
    }
390
#endif
391

            
392
27
    return CAIRO_TEST_SUCCESS;
393
}
394

            
395
static cairo_test_status_t
396
3
record_paint (cairo_t *cr, int width, int height)
397
{
398
3
    return record_replay (cr, paint, width, height);
399
}
400

            
401
static cairo_test_status_t
402
3
record_paint_alpha (cairo_t *cr, int width, int height)
403
{
404
3
    return record_replay (cr, paint_alpha, width, height);
405
}
406

            
407
static cairo_test_status_t
408
3
record_paint_alpha_solid_clip (cairo_t *cr, int width, int height)
409
{
410
3
    return record_replay (cr, paint_alpha_solid_clip, width, height);
411
}
412

            
413
static cairo_test_status_t
414
3
record_paint_alpha_clip (cairo_t *cr, int width, int height)
415
{
416
3
    return record_replay (cr, paint_alpha_clip, width, height);
417
}
418

            
419
static cairo_test_status_t
420
3
record_paint_alpha_clip_mask (cairo_t *cr, int width, int height)
421
{
422
3
    return record_replay (cr, paint_alpha_clip_mask, width, height);
423
}
424

            
425
static cairo_test_status_t
426
3
record_fill_alpha (cairo_t *cr, int width, int height)
427
{
428
3
    return record_replay (cr, fill_alpha, width, height);
429
}
430

            
431
static cairo_test_status_t
432
3
record_self_intersecting (cairo_t *cr, int width, int height)
433
{
434
3
    return record_replay (cr, self_intersecting, width, height);
435
}
436

            
437
static cairo_test_status_t
438
3
record_select_font_face (cairo_t *cr, int width, int height)
439
{
440
3
    return record_replay (cr, select_font_face, width, height);
441
}
442

            
443
static cairo_test_status_t
444
3
record_text_transform (cairo_t *cr, int width, int height)
445
{
446
3
    return record_replay (cr, text_transform, width, height);
447
}
448

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