1bc5bca9b0361d041a6048f46ec5759fdb307c2e
[platform/core/graphics/cairo.git] / test / push-group-color.c
1 /*
2  * Copyright © 2005 Mozilla Corporation
3  * Copyright © 2009 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  * Mozilla Corporation not be used in advertising or publicity pertaining to
11  * distribution of the software without specific, written prior
12  * permission. Mozilla Corporation 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  * MOZILLA CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18  * FITNESS, IN NO EVENT SHALL MOZILLA CORPORATION 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  * Author: Vladimir Vukicevic <vladimir@pobox.com>
25  *         Chris Wilson <chris@chris-wilson.co.uk>
26  */
27
28 #include "cairo-test.h"
29
30 #define UNIT_SIZE 100
31 #define PAD 5
32 #define INNER_PAD 10
33
34 #define WIDTH (UNIT_SIZE + PAD) + PAD
35 #define HEIGHT (UNIT_SIZE + PAD) + PAD
36
37 static cairo_pattern_t *
38 argb32_source (void)
39 {
40     cairo_surface_t *surface;
41     cairo_pattern_t *pattern;
42     cairo_t *cr;
43
44     surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 16, 16);
45     cr = cairo_create (surface);
46     cairo_surface_destroy (surface);
47
48     cairo_set_source_rgb (cr, 1, 0, 0);
49     cairo_rectangle (cr, 8, 0, 8, 8);
50     cairo_fill (cr);
51
52     cairo_set_source_rgb (cr, 0, 1, 0);
53     cairo_rectangle (cr, 0, 8, 8, 8);
54     cairo_fill (cr);
55
56     cairo_set_source_rgb (cr, 0, 0, 1);
57     cairo_rectangle (cr, 8, 8, 8, 8);
58     cairo_fill (cr);
59
60     pattern = cairo_pattern_create_for_surface (cairo_get_target (cr));
61     cairo_destroy (cr);
62
63     return pattern;
64 }
65
66 static cairo_test_status_t
67 draw (cairo_t *cr, int width, int height)
68 {
69     cairo_pattern_t *gradient, *image;
70
71     cairo_set_source_rgb (cr, 1,1,1);
72     cairo_paint (cr);
73
74     cairo_translate (cr, PAD, PAD);
75
76     /* clip to the unit size */
77     cairo_rectangle (cr, 0, 0,
78                      UNIT_SIZE, UNIT_SIZE);
79     cairo_clip (cr);
80
81     cairo_rectangle (cr, 0, 0,
82                      UNIT_SIZE, UNIT_SIZE);
83     cairo_set_source_rgba (cr, 0, 0, 0, 1);
84     cairo_set_line_width (cr, 2);
85     cairo_stroke (cr);
86
87     /* start a group */
88     cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR);
89
90     /* draw a gradient background */
91     cairo_save (cr);
92     cairo_translate (cr, INNER_PAD, INNER_PAD);
93     cairo_new_path (cr);
94     cairo_rectangle (cr, 0, 0,
95                      UNIT_SIZE - (INNER_PAD*2), UNIT_SIZE - (INNER_PAD*2));
96     gradient = cairo_pattern_create_linear (UNIT_SIZE - (INNER_PAD*2), 0,
97                                             UNIT_SIZE - (INNER_PAD*2), UNIT_SIZE - (INNER_PAD*2));
98     cairo_pattern_add_color_stop_rgba (gradient, 0.0, 0.3, 0.3, 0.3, 1.0);
99     cairo_pattern_add_color_stop_rgba (gradient, 1.0, 1.0, 1.0, 1.0, 1.0);
100     cairo_set_source (cr, gradient);
101     cairo_pattern_destroy (gradient);
102     cairo_fill (cr);
103     cairo_restore (cr);
104
105     /* draw diamond */
106     cairo_move_to (cr, UNIT_SIZE / 2, 0);
107     cairo_line_to (cr, UNIT_SIZE    , UNIT_SIZE / 2);
108     cairo_line_to (cr, UNIT_SIZE / 2, UNIT_SIZE);
109     cairo_line_to (cr, 0            , UNIT_SIZE / 2);
110     cairo_close_path (cr);
111     cairo_set_source_rgba (cr, 0, 0, 1, 1);
112     cairo_fill (cr);
113
114     /* draw circle */
115     cairo_arc (cr,
116                UNIT_SIZE / 2, UNIT_SIZE / 2,
117                UNIT_SIZE / 3.5,
118                0, M_PI * 2);
119     cairo_set_source_rgba (cr, 1, 0, 0, 1);
120     cairo_fill (cr);
121
122     /* and put the image on top */
123     cairo_translate (cr, UNIT_SIZE/2 - 8, UNIT_SIZE/2 - 8);
124     image = argb32_source ();
125     cairo_set_source (cr, image);
126     cairo_pattern_destroy (image);
127     cairo_paint (cr);
128
129     cairo_pop_group_to_source (cr);
130     cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
131     cairo_paint (cr);
132
133     return CAIRO_TEST_SUCCESS;
134 }
135
136 CAIRO_TEST (push_group_color,
137             "Verify that cairo_push_group_with_content works.",
138             "group", /* keywords */
139             NULL, /* requirements */
140             WIDTH, HEIGHT,
141             NULL, draw)