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_translate(cr, width, 0);
363 cairo_rotate (cr, M_PI/2);
366 surface = record_get (func (record_create (cr)));
368 cairo_translate(cr, width, 0);
369 cairo_rotate (cr, M_PI/2);
370 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
371 cairo_set_source_surface (cr, surface, 0, 0);
372 cairo_surface_destroy (surface);
373 cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_NONE);
375 for (y = 0; y < width; y += 2) {
376 for (x = 0; x < height; x += 2) {
377 cairo_rectangle (cr, x, y, 2, 2);
380 cairo_reset_clip (cr);
385 return CAIRO_TEST_SUCCESS;
388 static cairo_test_status_t
389 record_paint (cairo_t *cr, int width, int height)
391 return record_replay (cr, paint, width, height);
394 static cairo_test_status_t
395 record_paint_alpha (cairo_t *cr, int width, int height)
397 return record_replay (cr, paint_alpha, width, height);
400 static cairo_test_status_t
401 record_paint_alpha_solid_clip (cairo_t *cr, int width, int height)
403 return record_replay (cr, paint_alpha_solid_clip, width, height);
406 static cairo_test_status_t
407 record_paint_alpha_clip (cairo_t *cr, int width, int height)
409 return record_replay (cr, paint_alpha_clip, width, height);
412 static cairo_test_status_t
413 record_paint_alpha_clip_mask (cairo_t *cr, int width, int height)
415 return record_replay (cr, paint_alpha_clip_mask, width, height);
418 static cairo_test_status_t
419 record_fill_alpha (cairo_t *cr, int width, int height)
421 return record_replay (cr, fill_alpha, width, height);
424 static cairo_test_status_t
425 record_self_intersecting (cairo_t *cr, int width, int height)
427 return record_replay (cr, self_intersecting, width, height);
430 static cairo_test_status_t
431 record_select_font_face (cairo_t *cr, int width, int height)
433 return record_replay (cr, select_font_face, width, height);
436 static cairo_test_status_t
437 record_text_transform (cairo_t *cr, int width, int height)
439 return record_replay (cr, text_transform, width, height);
442 CAIRO_TEST (record90_paint,
443 "Test replayed calls to cairo_paint",
444 "paint,record", /* keywords */
445 NULL, /* requirements */
448 CAIRO_TEST (record90_paint_alpha,
449 "Simple test of cairo_paint_with_alpha",
450 "record, paint, alpha", /* keywords */
451 NULL, /* requirements */
453 NULL, record_paint_alpha)
454 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 */
459 NULL, record_paint_alpha_solid_clip)
460 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 */
465 NULL, record_paint_alpha_clip)
466 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 */
471 NULL, record_paint_alpha_clip_mask)
472 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 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 */
483 NULL, record_select_font_face)
484 CAIRO_TEST (record90_self_intersecting,
485 "Test strokes of self-intersecting paths",
486 "record, stroke, trap", /* keywords */
487 NULL, /* requirements */
489 NULL, record_self_intersecting)
490 CAIRO_TEST (record90_text_transform,
491 "Test various applications of the font matrix",
492 "record, text, transform", /* keywords */
493 NULL, /* requirements */
495 NULL, record_text_transform)