2 * Copyright © 2005 Red Hat, Inc.
3 * Copyright © 2011 Intel Corporation
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.
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.
25 * Carl D. Worth <cworth@cworth.org>
26 * Chris Wilson <chris@chris-wilson.co.uk>
29 #include "cairo-test.h"
32 #define SIZE 60 /* needs to be big to check large area effects (dithering) */
37 #define TT_FONT_SIZE 32.0
39 #define GENERATE_REF 0
41 static uint32_t data[16] = {
42 0xffffffff, 0xffffffff, 0xffff0000, 0xffff0000,
43 0xffffffff, 0xffffffff, 0xffff0000, 0xffff0000,
45 0xff00ff00, 0xff00ff00, 0xff0000ff, 0xff0000ff,
46 0xff00ff00, 0xff00ff00, 0xff0000ff, 0xff0000ff
49 static const char *png_filename = "romedalen.png";
54 cairo_set_source_rgb (cr, 0, 0, 1);
57 cairo_translate (cr, 2, 2);
58 cairo_scale (cr, 0.5, 0.5);
60 cairo_set_source_rgb (cr, 1, 0, 0);
67 paint_alpha (cairo_t *cr)
69 cairo_surface_t *surface;
71 surface = cairo_image_surface_create_for_data ((unsigned char *) data,
72 CAIRO_FORMAT_RGB24, 4, 4, 16);
74 cairo_test_paint_checkered (cr);
76 cairo_scale (cr, 4, 4);
78 cairo_set_source_surface (cr, surface, 2 , 2);
79 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
80 cairo_paint_with_alpha (cr, 0.5);
82 cairo_surface_finish (surface); /* data will go out of scope */
83 cairo_surface_destroy (surface);
89 paint_alpha_solid_clip (cairo_t *cr)
91 cairo_test_paint_checkered (cr);
93 cairo_rectangle (cr, 2.5, 2.5, 27, 27);
96 cairo_set_source_rgb (cr, 1., 0.,0.);
97 cairo_paint_with_alpha (cr, 0.5);
103 paint_alpha_clip (cairo_t *cr)
105 cairo_surface_t *surface;
107 surface = cairo_image_surface_create_for_data ((unsigned char *) data,
108 CAIRO_FORMAT_RGB24, 4, 4, 16);
110 cairo_test_paint_checkered (cr);
112 cairo_rectangle (cr, 10.5, 10.5, 11, 11);
115 cairo_scale (cr, 4, 4);
117 cairo_set_source_surface (cr, surface, 2 , 2);
118 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
119 cairo_paint_with_alpha (cr, 0.5);
121 cairo_surface_finish (surface); /* data will go out of scope */
122 cairo_surface_destroy (surface);
128 paint_alpha_clip_mask (cairo_t *cr)
130 cairo_surface_t *surface;
132 surface = cairo_image_surface_create_for_data ((unsigned char *) data,
133 CAIRO_FORMAT_RGB24, 4, 4, 16);
135 cairo_test_paint_checkered (cr);
137 cairo_move_to (cr, 16, 5);
138 cairo_line_to (cr, 5, 16);
139 cairo_line_to (cr, 16, 27);
140 cairo_line_to (cr, 27, 16);
143 cairo_scale (cr, 4, 4);
145 cairo_set_source_surface (cr, surface, 2 , 2);
146 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
147 cairo_paint_with_alpha (cr, 0.5);
149 cairo_surface_finish (surface); /* data will go out of scope */
150 cairo_surface_destroy (surface);
156 select_font_face (cairo_t *cr)
158 /* We draw in the default black, so paint white first. */
159 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
162 cairo_set_source_rgb (cr, 0, 0, 0); /* black */
164 cairo_set_font_size (cr, TEXT_SIZE);
165 cairo_move_to (cr, 0, TEXT_SIZE);
167 cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Serif",
168 CAIRO_FONT_SLANT_NORMAL,
169 CAIRO_FONT_WEIGHT_NORMAL);
170 cairo_show_text (cr, "i-am-serif");
172 cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
173 CAIRO_FONT_SLANT_NORMAL,
174 CAIRO_FONT_WEIGHT_NORMAL);
175 cairo_show_text (cr, " i-am-sans");
177 cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans Mono",
178 CAIRO_FONT_SLANT_NORMAL,
179 CAIRO_FONT_WEIGHT_NORMAL);
180 cairo_show_text (cr, " i-am-mono");
186 fill_alpha (cairo_t *cr)
188 const double alpha = 1./3;
191 /* flatten to white */
192 cairo_set_source_rgb (cr, 1, 1, 1);
196 cairo_rectangle (cr, PAD, PAD, SIZE, SIZE);
197 cairo_set_source_rgba (cr, 1, 0, 0, alpha);
201 cairo_translate (cr, SIZE + 2 * PAD, 0);
202 cairo_arc (cr, PAD + SIZE / 2., PAD + SIZE / 2., SIZE / 2., 0, 2 * M_PI);
203 cairo_set_source_rgba (cr, 0, 1, 0, alpha);
207 cairo_translate (cr, 0, SIZE + 2 * PAD);
208 cairo_move_to (cr, PAD + SIZE / 2, PAD);
209 cairo_line_to (cr, PAD + SIZE, PAD + SIZE);
210 cairo_line_to (cr, PAD, PAD + SIZE);
211 cairo_set_source_rgba (cr, 0, 0, 1, alpha);
215 cairo_translate (cr, -(SIZE + 2 * PAD) + SIZE/2., SIZE/2.);
216 for (n = 0; n < 5; n++) {
218 SIZE/2 * cos (2*n * 2*M_PI / 10),
219 SIZE/2 * sin (2*n * 2*M_PI / 10));
222 SIZE/4 * cos ((2*n+1)*2*M_PI / 10),
223 SIZE/4 * sin ((2*n+1)*2*M_PI / 10));
225 cairo_set_source_rgba (cr, 0, 0, 0, alpha);
232 self_intersecting (cairo_t *cr)
234 cairo_set_source_rgb (cr, 1, 1, 1);
237 cairo_translate (cr, 1.0, 1.0);
239 cairo_set_source_rgb (cr, 1, 0, 0); /* red */
241 /* First draw the desired shape with a fill */
242 cairo_rectangle (cr, 0.5, 0.5, 4.0, 4.0);
243 cairo_rectangle (cr, 3.5, 3.5, 4.0, 4.0);
244 cairo_rectangle (cr, 3.5, 1.5, -2.0, 2.0);
245 cairo_rectangle (cr, 6.5, 4.5, -2.0, 2.0);
249 /* Then try the same thing with a stroke */
250 cairo_translate (cr, 0, 10);
251 cairo_move_to (cr, 1.0, 1.0);
252 cairo_rel_line_to (cr, 3.0, 0.0);
253 cairo_rel_line_to (cr, 0.0, 6.0);
254 cairo_rel_line_to (cr, 3.0, 0.0);
255 cairo_rel_line_to (cr, 0.0, -3.0);
256 cairo_rel_line_to (cr, -6.0, 0.0);
257 cairo_close_path (cr);
259 cairo_set_line_width (cr, 1.0);
266 draw_text_transform (cairo_t *cr)
271 cairo_matrix_init (&tm, 1, 0,
274 cairo_matrix_scale (&tm, TT_FONT_SIZE, TT_FONT_SIZE);
275 cairo_set_font_matrix (cr, &tm);
278 cairo_move_to (cr, 50, TT_SIZE-TT_PAD);
279 cairo_show_text (cr, "A");
281 /* rotate and scale */
282 cairo_matrix_init_rotate (&tm, M_PI / 2);
283 cairo_matrix_scale (&tm, TT_FONT_SIZE, TT_FONT_SIZE * 2.0);
284 cairo_set_font_matrix (cr, &tm);
287 cairo_move_to (cr, TT_PAD, TT_PAD + 25);
288 cairo_show_text (cr, "A");
290 cairo_matrix_init_rotate (&tm, M_PI / 2);
291 cairo_matrix_scale (&tm, TT_FONT_SIZE * 2.0, TT_FONT_SIZE);
292 cairo_set_font_matrix (cr, &tm);
295 cairo_move_to (cr, TT_PAD, TT_PAD + 50);
296 cairo_show_text (cr, "A");
300 text_transform (cairo_t *cr)
302 const cairo_test_context_t *ctx = cairo_test_get_context (cr);
303 cairo_pattern_t *pattern;
305 cairo_set_source_rgb (cr, 1., 1., 1.);
308 cairo_set_source_rgb (cr, 0., 0., 0.);
310 cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
311 CAIRO_FONT_SLANT_NORMAL,
312 CAIRO_FONT_WEIGHT_NORMAL);
314 draw_text_transform (cr);
316 cairo_translate (cr, TT_SIZE, TT_SIZE);
317 cairo_rotate (cr, M_PI);
319 pattern = cairo_test_create_pattern_from_png (ctx, png_filename);
320 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
321 cairo_set_source (cr, pattern);
322 cairo_pattern_destroy (pattern);
324 draw_text_transform (cr);
329 /* And here begins the recording and replaying... */
332 record_create (cairo_t *target)
334 cairo_surface_t *surface;
337 surface = cairo_recording_surface_create (cairo_surface_get_content (cairo_get_target (target)), NULL);
338 cr = cairo_create (surface);
339 cairo_surface_destroy (surface);
344 static cairo_surface_t *
345 record_get (cairo_t *target)
347 cairo_surface_t *surface;
349 surface = cairo_surface_reference (cairo_get_target (target));
350 cairo_destroy (target);
355 static cairo_test_status_t
356 record_replay (cairo_t *cr, cairo_t *(*func)(cairo_t *), int width, int height)
358 cairo_surface_t *surface;
362 cairo_scale (cr, 2, 2);
365 surface = record_get (func (record_create (cr)));
367 cairo_scale (cr, 2, 2);
368 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
369 cairo_set_source_surface (cr, surface, 0, 0);
370 cairo_surface_destroy (surface);
371 cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_NONE);
373 for (y = 0; y < height; y += 2) {
374 for (x = 0; x < width; x += 2) {
375 cairo_rectangle (cr, x, y, 2, 2);
378 cairo_reset_clip (cr);
383 return CAIRO_TEST_SUCCESS;
386 static cairo_test_status_t
387 record_paint (cairo_t *cr, int width, int height)
389 return record_replay (cr, paint, width, height);
392 static cairo_test_status_t
393 record_paint_alpha (cairo_t *cr, int width, int height)
395 return record_replay (cr, paint_alpha, width, height);
398 static cairo_test_status_t
399 record_paint_alpha_solid_clip (cairo_t *cr, int width, int height)
401 return record_replay (cr, paint_alpha_solid_clip, width, height);
404 static cairo_test_status_t
405 record_paint_alpha_clip (cairo_t *cr, int width, int height)
407 return record_replay (cr, paint_alpha_clip, width, height);
410 static cairo_test_status_t
411 record_paint_alpha_clip_mask (cairo_t *cr, int width, int height)
413 return record_replay (cr, paint_alpha_clip_mask, width, height);
416 static cairo_test_status_t
417 record_fill_alpha (cairo_t *cr, int width, int height)
419 return record_replay (cr, fill_alpha, width, height);
422 static cairo_test_status_t
423 record_self_intersecting (cairo_t *cr, int width, int height)
425 return record_replay (cr, self_intersecting, width, height);
428 static cairo_test_status_t
429 record_select_font_face (cairo_t *cr, int width, int height)
431 return record_replay (cr, select_font_face, width, height);
434 static cairo_test_status_t
435 record_text_transform (cairo_t *cr, int width, int height)
437 return record_replay (cr, text_transform, width, height);
440 CAIRO_TEST (record2x_paint,
441 "Test replayed calls to cairo_paint",
442 "paint,record", /* keywords */
443 NULL, /* requirements */
446 CAIRO_TEST (record2x_paint_alpha,
447 "Simple test of cairo_paint_with_alpha",
448 "record, paint, alpha", /* keywords */
449 NULL, /* requirements */
451 NULL, record_paint_alpha)
452 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 */
457 NULL, record_paint_alpha_solid_clip)
458 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 */
463 NULL, record_paint_alpha_clip)
464 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 */
469 NULL, record_paint_alpha_clip_mask)
470 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 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 CAIRO_TEST (record2x_self_intersecting,
483 "Test strokes of self-intersecting paths",
484 "record, stroke, trap", /* keywords */
485 NULL, /* requirements */
487 NULL, record_self_intersecting)
488 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)