Opensource Compliance Issue.
[platform/core/graphics/cairo.git] / test / record1414x.c
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 #ifndef M_SQRT2
32 #define M_SQRT2 1.41421345623730951
33 #endif
34
35 #define TEXT_SIZE 12
36 #define SIZE 60 /* needs to be big to check large area effects (dithering) */
37 #define PAD 2
38
39 #define TT_SIZE 100
40 #define TT_PAD 5
41 #define TT_FONT_SIZE 32.0
42
43 #define GENERATE_REF 0
44
45 static uint32_t data[16] = {
46     0xffffffff, 0xffffffff,             0xffff0000, 0xffff0000,
47     0xffffffff, 0xffffffff,             0xffff0000, 0xffff0000,
48
49     0xff00ff00, 0xff00ff00,             0xff0000ff, 0xff0000ff,
50     0xff00ff00, 0xff00ff00,             0xff0000ff, 0xff0000ff
51 };
52
53 static const char *png_filename = "romedalen.png";
54
55 static cairo_t *
56 paint (cairo_t *cr)
57 {
58     cairo_set_source_rgb (cr, 0, 0, 1);
59     cairo_paint (cr);
60
61     cairo_translate (cr, 2, 2);
62     cairo_scale (cr, 0.5, 0.5);
63
64     cairo_set_source_rgb (cr, 1, 0, 0);
65     cairo_paint (cr);
66
67     return cr;
68 }
69
70 static cairo_t *
71 paint_alpha (cairo_t *cr)
72 {
73     cairo_surface_t *surface;
74
75     surface = cairo_image_surface_create_for_data ((unsigned char *) data,
76                                                    CAIRO_FORMAT_RGB24, 4, 4, 16);
77
78     cairo_test_paint_checkered (cr);
79
80     cairo_scale (cr, 4, 4);
81
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);
85
86     cairo_surface_finish (surface); /* data will go out of scope */
87     cairo_surface_destroy (surface);
88
89     return cr;
90 }
91
92 static cairo_t *
93 paint_alpha_solid_clip (cairo_t *cr)
94 {
95     cairo_test_paint_checkered (cr);
96
97     cairo_rectangle (cr, 2.5, 2.5, 27, 27);
98     cairo_clip (cr);
99
100     cairo_set_source_rgb (cr, 1., 0.,0.);
101     cairo_paint_with_alpha (cr, 0.5);
102
103     return cr;
104 }
105
106 static cairo_t *
107 paint_alpha_clip (cairo_t *cr)
108 {
109     cairo_surface_t *surface;
110
111     surface = cairo_image_surface_create_for_data ((unsigned char *) data,
112                                                    CAIRO_FORMAT_RGB24, 4, 4, 16);
113
114     cairo_test_paint_checkered (cr);
115
116     cairo_rectangle (cr, 10.5, 10.5, 11, 11);
117     cairo_clip (cr);
118
119     cairo_scale (cr, 4, 4);
120
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);
124
125     cairo_surface_finish (surface); /* data will go out of scope */
126     cairo_surface_destroy (surface);
127
128     return cr;
129 }
130
131 static cairo_t *
132 paint_alpha_clip_mask (cairo_t *cr)
133 {
134     cairo_surface_t *surface;
135
136     surface = cairo_image_surface_create_for_data ((unsigned char *) data,
137                                                    CAIRO_FORMAT_RGB24, 4, 4, 16);
138
139     cairo_test_paint_checkered (cr);
140
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);
145     cairo_clip (cr);
146
147     cairo_scale (cr, 4, 4);
148
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);
152
153     cairo_surface_finish (surface); /* data will go out of scope */
154     cairo_surface_destroy (surface);
155
156     return cr;
157 }
158
159 static cairo_t *
160 select_font_face (cairo_t *cr)
161 {
162     /* We draw in the default black, so paint white first. */
163     cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
164     cairo_paint (cr);
165
166     cairo_set_source_rgb (cr, 0, 0, 0); /* black */
167
168     cairo_set_font_size (cr, TEXT_SIZE);
169     cairo_move_to (cr, 0, TEXT_SIZE);
170
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");
175
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");
180
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");
185
186     return cr;
187 }
188
189 static cairo_t *
190 fill_alpha (cairo_t *cr)
191 {
192     const double alpha = 1./3;
193     int n;
194
195     /* flatten to white */
196     cairo_set_source_rgb (cr, 1, 1, 1);
197     cairo_paint (cr);
198
199     /* square */
200     cairo_rectangle (cr, PAD, PAD, SIZE, SIZE);
201     cairo_set_source_rgba (cr, 1, 0, 0, alpha);
202     cairo_fill (cr);
203
204     /* circle */
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);
208     cairo_fill (cr);
209
210     /* triangle */
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);
216     cairo_fill (cr);
217
218     /* star */
219     cairo_translate (cr, -(SIZE + 2 * PAD) + SIZE/2., SIZE/2.);
220     for (n = 0; n < 5; n++) {
221         cairo_line_to (cr,
222                        SIZE/2 * cos (2*n * 2*M_PI / 10),
223                        SIZE/2 * sin (2*n * 2*M_PI / 10));
224
225         cairo_line_to (cr,
226                        SIZE/4 * cos ((2*n+1)*2*M_PI / 10),
227                        SIZE/4 * sin ((2*n+1)*2*M_PI / 10));
228     }
229     cairo_set_source_rgba (cr, 0, 0, 0, alpha);
230     cairo_fill (cr);
231
232     return cr;
233 }
234
235 static cairo_t *
236 self_intersecting (cairo_t *cr)
237 {
238     cairo_set_source_rgb (cr, 1, 1, 1);
239     cairo_paint (cr);
240
241     cairo_translate (cr, 1.0, 1.0);
242
243     cairo_set_source_rgb (cr, 1, 0, 0); /* red */
244
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);
250
251     cairo_fill (cr);
252
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);
262
263     cairo_set_line_width (cr, 1.0);
264     cairo_stroke (cr);
265
266     return cr;
267 }
268
269 static void
270 draw_text_transform (cairo_t *cr)
271 {
272     cairo_matrix_t tm;
273
274     /* skew */
275     cairo_matrix_init (&tm, 1, 0,
276                        -0.25, 1,
277                        0, 0);
278     cairo_matrix_scale (&tm, TT_FONT_SIZE, TT_FONT_SIZE);
279     cairo_set_font_matrix (cr, &tm);
280
281     cairo_new_path (cr);
282     cairo_move_to (cr, 50, TT_SIZE-TT_PAD);
283     cairo_show_text (cr, "A");
284
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);
289
290     cairo_new_path (cr);
291     cairo_move_to (cr, TT_PAD, TT_PAD + 25);
292     cairo_show_text (cr, "A");
293
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);
297
298     cairo_new_path (cr);
299     cairo_move_to (cr, TT_PAD, TT_PAD + 50);
300     cairo_show_text (cr, "A");
301 }
302
303 static cairo_t *
304 text_transform (cairo_t *cr)
305 {
306     const cairo_test_context_t *ctx = cairo_test_get_context (cr);
307     cairo_pattern_t *pattern;
308
309     cairo_set_source_rgb (cr, 1., 1., 1.);
310     cairo_paint (cr);
311
312     cairo_set_source_rgb (cr, 0., 0., 0.);
313
314     cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
315                             CAIRO_FONT_SLANT_NORMAL,
316                             CAIRO_FONT_WEIGHT_NORMAL);
317
318     draw_text_transform (cr);
319
320     cairo_translate (cr, TT_SIZE, TT_SIZE);
321     cairo_rotate (cr, M_PI);
322
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);
327
328     draw_text_transform (cr);
329
330     return cr;
331 }
332
333 /* And here begins the recording and replaying... */
334
335 static cairo_t *
336 record_create (cairo_t *target)
337 {
338     cairo_surface_t *surface;
339     cairo_t *cr;
340
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);
344
345     return cr;
346 }
347
348 static cairo_surface_t *
349 record_get (cairo_t *target)
350 {
351     cairo_surface_t *surface;
352
353     surface = cairo_surface_reference (cairo_get_target (target));
354     cairo_destroy (target);
355
356     return surface;
357 }
358
359 static cairo_test_status_t
360 record_replay (cairo_t *cr, cairo_t *(*func)(cairo_t *), int width, int height)
361 {
362     cairo_surface_t *surface;
363     int x, y;
364
365 #if GENERATE_REF
366     cairo_scale (cr, M_SQRT2, M_SQRT2);
367     func (cr);
368 #else
369     surface = record_get (func (record_create (cr)));
370
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);
376
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);
381             cairo_clip (cr);
382             cairo_paint (cr);
383             cairo_reset_clip (cr);
384         }
385     }
386 #endif
387
388     return CAIRO_TEST_SUCCESS;
389 }
390
391 static cairo_test_status_t
392 record_paint (cairo_t *cr, int width, int height)
393 {
394     return record_replay (cr, paint, width, height);
395 }
396
397 static cairo_test_status_t
398 record_paint_alpha (cairo_t *cr, int width, int height)
399 {
400     return record_replay (cr, paint_alpha, width, height);
401 }
402
403 static cairo_test_status_t
404 record_paint_alpha_solid_clip (cairo_t *cr, int width, int height)
405 {
406     return record_replay (cr, paint_alpha_solid_clip, width, height);
407 }
408
409 static cairo_test_status_t
410 record_paint_alpha_clip (cairo_t *cr, int width, int height)
411 {
412     return record_replay (cr, paint_alpha_clip, width, height);
413 }
414
415 static cairo_test_status_t
416 record_paint_alpha_clip_mask (cairo_t *cr, int width, int height)
417 {
418     return record_replay (cr, paint_alpha_clip_mask, width, height);
419 }
420
421 static cairo_test_status_t
422 record_fill_alpha (cairo_t *cr, int width, int height)
423 {
424     return record_replay (cr, fill_alpha, width, height);
425 }
426
427 static cairo_test_status_t
428 record_self_intersecting (cairo_t *cr, int width, int height)
429 {
430     return record_replay (cr, self_intersecting, width, height);
431 }
432
433 static cairo_test_status_t
434 record_select_font_face (cairo_t *cr, int width, int height)
435 {
436     return record_replay (cr, select_font_face, width, height);
437 }
438
439 static cairo_test_status_t
440 record_text_transform (cairo_t *cr, int width, int height)
441 {
442     return record_replay (cr, text_transform, width, height);
443 }
444
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,
450             NULL, record_paint)
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)