Tizen 2.0 Release
[framework/graphics/cairo.git] / test / clear-source.c
1 /*
2  * Copyright 2009 Benjamin Otte
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * Author: Benjamin Otte <otte@gnome.org>
25  */
26
27 #include "cairo-test.h"
28
29 typedef enum {
30   CLEAR,
31   CLEARED,
32   PAINTED
33 } surface_type_t;
34
35 #define SIZE 10
36 #define SPACE 5
37
38 static cairo_surface_t *
39 create_surface (cairo_t *target, cairo_content_t content, surface_type_t type)
40 {
41     cairo_surface_t *surface;
42     cairo_t *cr;
43
44     surface = cairo_surface_create_similar (cairo_get_target (target),
45                                             content,
46                                             SIZE, SIZE);
47
48     if (type == CLEAR)
49         return surface;
50
51     cr = cairo_create (surface);
52     cairo_surface_destroy (surface);
53
54     cairo_set_source_rgb (cr, 0.75, 0, 0);
55     cairo_paint (cr);
56
57     if (type == PAINTED)
58         goto DONE;
59
60     cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
61     cairo_paint (cr);
62
63 DONE:
64     surface = cairo_surface_reference (cairo_get_target (cr));
65     cairo_destroy (cr);
66
67     return surface;
68 }
69
70 static void
71 paint (cairo_t *cr, cairo_surface_t *surface)
72 {
73     cairo_set_source_surface (cr, surface, 0, 0);
74     cairo_paint (cr);
75 }
76
77 static void
78 fill (cairo_t *cr, cairo_surface_t *surface)
79 {
80     cairo_set_source_surface (cr, surface, 0, 0);
81     cairo_rectangle (cr, -SPACE, -SPACE, SIZE + 2 * SPACE, SIZE + 2 * SPACE);
82     cairo_fill (cr);
83 }
84
85 static void
86 stroke (cairo_t *cr, cairo_surface_t *surface)
87 {
88     cairo_set_source_surface (cr, surface, 0, 0);
89     cairo_set_line_width (cr, 2.0);
90     cairo_rectangle (cr, 1, 1, SIZE - 2, SIZE - 2);
91     cairo_stroke (cr);
92 }
93
94 static void
95 mask (cairo_t *cr, cairo_surface_t *surface)
96 {
97     cairo_set_source_rgb (cr, 0, 0, 0.75);
98     cairo_mask_surface (cr, surface, 0, 0);
99 }
100
101 static void
102 mask_self (cairo_t *cr, cairo_surface_t *surface)
103 {
104     cairo_set_source_surface (cr, surface, 0, 0);
105     cairo_mask_surface (cr, surface, 0, 0);
106 }
107
108 static void
109 glyphs (cairo_t *cr, cairo_surface_t *surface)
110 {
111     cairo_set_source_surface (cr, surface, 0, 0);
112     cairo_select_font_face (cr,
113                             "@cairo:",
114                             CAIRO_FONT_SLANT_NORMAL,
115                             CAIRO_FONT_WEIGHT_NORMAL);
116     cairo_set_font_size (cr, 16);
117     cairo_translate (cr, 0, SIZE);
118     cairo_show_text (cr, "C");
119 }
120
121 typedef void (* operation_t) (cairo_t *cr, cairo_surface_t *surface);
122 static operation_t operations[] = {
123     paint,
124     fill,
125     stroke,
126     mask,
127     mask_self,
128     glyphs
129 };
130
131 static cairo_test_status_t
132 draw (cairo_t *cr, int width, int height)
133 {
134     cairo_content_t contents[] = { CAIRO_CONTENT_COLOR_ALPHA, CAIRO_CONTENT_COLOR, CAIRO_CONTENT_ALPHA };
135     unsigned int content, type, ops;
136
137     cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
138     cairo_paint (cr);
139     cairo_translate (cr, SPACE, SPACE);
140
141     for (type = 0; type <= PAINTED; type++) {
142         for (content = 0; content < ARRAY_LENGTH (contents); content++) {
143             cairo_surface_t *surface;
144
145             surface = create_surface (cr, contents[content], type);
146
147             cairo_save (cr);
148             for (ops = 0; ops < ARRAY_LENGTH (operations); ops++) {
149                 cairo_save (cr);
150                 operations[ops] (cr, surface);
151                 cairo_restore (cr);
152                 cairo_translate (cr, 0, SIZE + SPACE);
153             }
154             cairo_restore (cr);
155             cairo_translate (cr, SIZE + SPACE, 0);
156
157             cairo_surface_destroy (surface);
158         }
159     }
160
161     return CAIRO_TEST_SUCCESS;
162 }
163
164 CAIRO_TEST (clear_source,
165             "Check painting with cleared surfaces works as expected",
166             NULL, /* keywords */
167             NULL, /* requirements */
168             (SIZE + SPACE) * 9 + SPACE, ARRAY_LENGTH (operations) * (SIZE + SPACE) + SPACE,
169             NULL, draw)