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 M_SQRT2 1.41421345623730951
36 #define SIZE 60 /* needs to be big to check large area effects (dithering) */
41 #define TT_FONT_SIZE 32.0
43 #define GENERATE_REF 0
45 static uint32_t data[16] = {
46 0xffffffff, 0xffffffff, 0xffff0000, 0xffff0000,
47 0xffffffff, 0xffffffff, 0xffff0000, 0xffff0000,
49 0xff00ff00, 0xff00ff00, 0xff0000ff, 0xff0000ff,
50 0xff00ff00, 0xff00ff00, 0xff0000ff, 0xff0000ff
53 static const char *png_filename = "romedalen.png";
58 cairo_set_source_rgb (cr, 0, 0, 1);
61 cairo_translate (cr, 2, 2);
62 cairo_scale (cr, 0.5, 0.5);
64 cairo_set_source_rgb (cr, 1, 0, 0);
71 paint_alpha (cairo_t *cr)
73 cairo_surface_t *surface;
75 surface = cairo_image_surface_create_for_data ((unsigned char *) data,
76 CAIRO_FORMAT_RGB24, 4, 4, 16);
78 cairo_test_paint_checkered (cr);
80 cairo_scale (cr, 4, 4);
82 cairo_set_source_surface (cr, surface, 2 , 2);
83 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
84 cairo_paint_with_alpha (cr, 0.5);
86 cairo_surface_finish (surface); /* data will go out of scope */
87 cairo_surface_destroy (surface);
93 paint_alpha_solid_clip (cairo_t *cr)
95 cairo_test_paint_checkered (cr);
97 cairo_rectangle (cr, 2.5, 2.5, 27, 27);
100 cairo_set_source_rgb (cr, 1., 0.,0.);
101 cairo_paint_with_alpha (cr, 0.5);
107 paint_alpha_clip (cairo_t *cr)
109 cairo_surface_t *surface;
111 surface = cairo_image_surface_create_for_data ((unsigned char *) data,
112 CAIRO_FORMAT_RGB24, 4, 4, 16);
114 cairo_test_paint_checkered (cr);
116 cairo_rectangle (cr, 10.5, 10.5, 11, 11);
119 cairo_scale (cr, 4, 4);
121 cairo_set_source_surface (cr, surface, 2 , 2);
122 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
123 cairo_paint_with_alpha (cr, 0.5);
125 cairo_surface_finish (surface); /* data will go out of scope */
126 cairo_surface_destroy (surface);
132 paint_alpha_clip_mask (cairo_t *cr)
134 cairo_surface_t *surface;
136 surface = cairo_image_surface_create_for_data ((unsigned char *) data,
137 CAIRO_FORMAT_RGB24, 4, 4, 16);
139 cairo_test_paint_checkered (cr);
141 cairo_move_to (cr, 16, 5);
142 cairo_line_to (cr, 5, 16);
143 cairo_line_to (cr, 16, 27);
144 cairo_line_to (cr, 27, 16);
147 cairo_scale (cr, 4, 4);
149 cairo_set_source_surface (cr, surface, 2 , 2);
150 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
151 cairo_paint_with_alpha (cr, 0.5);
153 cairo_surface_finish (surface); /* data will go out of scope */
154 cairo_surface_destroy (surface);
160 select_font_face (cairo_t *cr)
162 /* We draw in the default black, so paint white first. */
163 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
166 cairo_set_source_rgb (cr, 0, 0, 0); /* black */
168 cairo_set_font_size (cr, TEXT_SIZE);
169 cairo_move_to (cr, 0, TEXT_SIZE);
171 cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Serif",
172 CAIRO_FONT_SLANT_NORMAL,
173 CAIRO_FONT_WEIGHT_NORMAL);
174 cairo_show_text (cr, "i-am-serif");
176 cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
177 CAIRO_FONT_SLANT_NORMAL,
178 CAIRO_FONT_WEIGHT_NORMAL);
179 cairo_show_text (cr, " i-am-sans");
181 cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans Mono",
182 CAIRO_FONT_SLANT_NORMAL,
183 CAIRO_FONT_WEIGHT_NORMAL);
184 cairo_show_text (cr, " i-am-mono");
190 fill_alpha (cairo_t *cr)
192 const double alpha = 1./3;
195 /* flatten to white */
196 cairo_set_source_rgb (cr, 1, 1, 1);
200 cairo_rectangle (cr, PAD, PAD, SIZE, SIZE);
201 cairo_set_source_rgba (cr, 1, 0, 0, alpha);
205 cairo_translate (cr, SIZE + 2 * PAD, 0);
206 cairo_arc (cr, PAD + SIZE / 2., PAD + SIZE / 2., SIZE / 2., 0, 2 * M_PI);
207 cairo_set_source_rgba (cr, 0, 1, 0, alpha);
211 cairo_translate (cr, 0, SIZE + 2 * PAD);
212 cairo_move_to (cr, PAD + SIZE / 2, PAD);
213 cairo_line_to (cr, PAD + SIZE, PAD + SIZE);
214 cairo_line_to (cr, PAD, PAD + SIZE);
215 cairo_set_source_rgba (cr, 0, 0, 1, alpha);
219 cairo_translate (cr, -(SIZE + 2 * PAD) + SIZE/2., SIZE/2.);
220 for (n = 0; n < 5; n++) {
222 SIZE/2 * cos (2*n * 2*M_PI / 10),
223 SIZE/2 * sin (2*n * 2*M_PI / 10));
226 SIZE/4 * cos ((2*n+1)*2*M_PI / 10),
227 SIZE/4 * sin ((2*n+1)*2*M_PI / 10));
229 cairo_set_source_rgba (cr, 0, 0, 0, alpha);
236 self_intersecting (cairo_t *cr)
238 cairo_set_source_rgb (cr, 1, 1, 1);
241 cairo_translate (cr, 1.0, 1.0);
243 cairo_set_source_rgb (cr, 1, 0, 0); /* red */
245 /* First draw the desired shape with a fill */
246 cairo_rectangle (cr, 0.5, 0.5, 4.0, 4.0);
247 cairo_rectangle (cr, 3.5, 3.5, 4.0, 4.0);
248 cairo_rectangle (cr, 3.5, 1.5, -2.0, 2.0);
249 cairo_rectangle (cr, 6.5, 4.5, -2.0, 2.0);
253 /* Then try the same thing with a stroke */
254 cairo_translate (cr, 0, 10);
255 cairo_move_to (cr, 1.0, 1.0);
256 cairo_rel_line_to (cr, 3.0, 0.0);
257 cairo_rel_line_to (cr, 0.0, 6.0);
258 cairo_rel_line_to (cr, 3.0, 0.0);
259 cairo_rel_line_to (cr, 0.0, -3.0);
260 cairo_rel_line_to (cr, -6.0, 0.0);
261 cairo_close_path (cr);
263 cairo_set_line_width (cr, 1.0);
270 draw_text_transform (cairo_t *cr)
275 cairo_matrix_init (&tm, 1, 0,
278 cairo_matrix_scale (&tm, TT_FONT_SIZE, TT_FONT_SIZE);
279 cairo_set_font_matrix (cr, &tm);
282 cairo_move_to (cr, 50, TT_SIZE-TT_PAD);
283 cairo_show_text (cr, "A");
285 /* rotate and scale */
286 cairo_matrix_init_rotate (&tm, M_PI / 2);
287 cairo_matrix_scale (&tm, TT_FONT_SIZE, TT_FONT_SIZE * 2.0);
288 cairo_set_font_matrix (cr, &tm);
291 cairo_move_to (cr, TT_PAD, TT_PAD + 25);
292 cairo_show_text (cr, "A");
294 cairo_matrix_init_rotate (&tm, M_PI / 2);
295 cairo_matrix_scale (&tm, TT_FONT_SIZE * 2.0, TT_FONT_SIZE);
296 cairo_set_font_matrix (cr, &tm);
299 cairo_move_to (cr, TT_PAD, TT_PAD + 50);
300 cairo_show_text (cr, "A");
304 text_transform (cairo_t *cr)
306 const cairo_test_context_t *ctx = cairo_test_get_context (cr);
307 cairo_pattern_t *pattern;
309 cairo_set_source_rgb (cr, 1., 1., 1.);
312 cairo_set_source_rgb (cr, 0., 0., 0.);
314 cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
315 CAIRO_FONT_SLANT_NORMAL,
316 CAIRO_FONT_WEIGHT_NORMAL);
318 draw_text_transform (cr);
320 cairo_translate (cr, TT_SIZE, TT_SIZE);
321 cairo_rotate (cr, M_PI);
323 pattern = cairo_test_create_pattern_from_png (ctx, png_filename);
324 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
325 cairo_set_source (cr, pattern);
326 cairo_pattern_destroy (pattern);
328 draw_text_transform (cr);
333 /* And here begins the recording and replaying... */
336 record_create (cairo_t *target)
338 cairo_surface_t *surface;
341 surface = cairo_recording_surface_create (cairo_surface_get_content (cairo_get_target (target)), NULL);
342 cr = cairo_create (surface);
343 cairo_surface_destroy (surface);
348 static cairo_surface_t *
349 record_get (cairo_t *target)
351 cairo_surface_t *surface;
353 surface = cairo_surface_reference (cairo_get_target (target));
354 cairo_destroy (target);
359 static cairo_test_status_t
360 record_replay (cairo_t *cr, cairo_t *(*func)(cairo_t *), int width, int height)
362 cairo_surface_t *surface;
366 cairo_scale (cr, M_SQRT2, M_SQRT2);
369 surface = record_get (func (record_create (cr)));
371 cairo_scale (cr, M_SQRT2, M_SQRT2);
372 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
373 cairo_set_source_surface (cr, surface, 0, 0);
374 cairo_surface_destroy (surface);
375 cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_NONE);
377 cairo_identity_matrix (cr); /* make sure the clip is pixel-aligned */
378 for (y = 0; y < height; y += 2) {
379 for (x = 0; x < width; x += 2) {
380 cairo_rectangle (cr, x, y, 2, 2);
383 cairo_reset_clip (cr);
388 return CAIRO_TEST_SUCCESS;
391 static cairo_test_status_t
392 record_paint (cairo_t *cr, int width, int height)
394 return record_replay (cr, paint, width, height);
397 static cairo_test_status_t
398 record_paint_alpha (cairo_t *cr, int width, int height)
400 return record_replay (cr, paint_alpha, width, height);
403 static cairo_test_status_t
404 record_paint_alpha_solid_clip (cairo_t *cr, int width, int height)
406 return record_replay (cr, paint_alpha_solid_clip, width, height);
409 static cairo_test_status_t
410 record_paint_alpha_clip (cairo_t *cr, int width, int height)
412 return record_replay (cr, paint_alpha_clip, width, height);
415 static cairo_test_status_t
416 record_paint_alpha_clip_mask (cairo_t *cr, int width, int height)
418 return record_replay (cr, paint_alpha_clip_mask, width, height);
421 static cairo_test_status_t
422 record_fill_alpha (cairo_t *cr, int width, int height)
424 return record_replay (cr, fill_alpha, width, height);
427 static cairo_test_status_t
428 record_self_intersecting (cairo_t *cr, int width, int height)
430 return record_replay (cr, self_intersecting, width, height);
433 static cairo_test_status_t
434 record_select_font_face (cairo_t *cr, int width, int height)
436 return record_replay (cr, select_font_face, width, height);
439 static cairo_test_status_t
440 record_text_transform (cairo_t *cr, int width, int height)
442 return record_replay (cr, text_transform, width, height);
445 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,
451 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 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 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 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 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 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 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 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)