]> Pileus Git - ~andy/gtk/blob - tests/testcairo.c
Merge branch 'master' into broadway2
[~andy/gtk] / tests / testcairo.c
1 /* testimage.c
2  * Copyright (C) 2005  Red Hat, Inc.
3  * Based on cairo-demo/X11/cairo-knockout.c
4  *
5  * Author: Owen Taylor
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <math.h>
24
25 #include <gtk/gtk.h>
26
27 static void
28 oval_path (cairo_t *cr,
29            double xc, double yc,
30            double xr, double yr)
31 {
32   cairo_save (cr);
33
34   cairo_translate (cr, xc, yc);
35   cairo_scale (cr, 1.0, yr / xr);
36   cairo_move_to (cr, xr, 0.0);
37   cairo_arc (cr,
38              0, 0,
39              xr,
40              0, 2 * G_PI);
41   cairo_close_path (cr);
42
43   cairo_restore (cr);
44 }
45
46 /* Create a path that is a circular oval with radii xr, yr at xc,
47  * yc.
48  */
49 /* Fill the given area with checks in the standard style
50  * for showing compositing effects.
51  *
52  * It would make sense to do this as a repeating surface,
53  * but most implementations of RENDER currently have broken
54  * implementations of repeat + transform, even when the
55  * transform is a translation.
56  */
57 static void
58 fill_checks (cairo_t *cr,
59              int x,     int y,
60              int width, int height)
61 {
62   int i, j;
63   
64 #define CHECK_SIZE 32
65
66   cairo_rectangle (cr, x, y, width, height);
67   cairo_set_source_rgb (cr, 0.4, 0.4, 0.4);
68   cairo_fill (cr);
69
70   /* Only works for CHECK_SIZE a power of 2 */
71   j = x & (-CHECK_SIZE);
72   
73   for (; j < height; j += CHECK_SIZE)
74     {
75       i = y & (-CHECK_SIZE);
76       for (; i < width; i += CHECK_SIZE)
77         if ((i / CHECK_SIZE + j / CHECK_SIZE) % 2 == 0)
78           cairo_rectangle (cr, i, j, CHECK_SIZE, CHECK_SIZE);
79     }
80
81   cairo_set_source_rgb (cr, 0.7, 0.7, 0.7);
82   cairo_fill (cr);
83 }
84
85 /* Draw a red, green, and blue circle equally spaced inside
86  * the larger circle of radius r at (xc, yc)
87  */
88 static void
89 draw_3circles (cairo_t *cr,
90                double xc, double yc,
91                double radius,
92                double alpha)
93 {
94   double subradius = radius * (2 / 3. - 0.1);
95     
96   cairo_set_source_rgba (cr, 1., 0., 0., alpha);
97   oval_path (cr,
98              xc + radius / 3. * cos (G_PI * (0.5)),
99              yc - radius / 3. * sin (G_PI * (0.5)),
100              subradius, subradius);
101   cairo_fill (cr);
102     
103   cairo_set_source_rgba (cr, 0., 1., 0., alpha);
104   oval_path (cr,
105              xc + radius / 3. * cos (G_PI * (0.5 + 2/.3)),
106              yc - radius / 3. * sin (G_PI * (0.5 + 2/.3)),
107              subradius, subradius);
108   cairo_fill (cr);
109     
110   cairo_set_source_rgba (cr, 0., 0., 1., alpha);
111   oval_path (cr,
112              xc + radius / 3. * cos (G_PI * (0.5 + 4/.3)),
113              yc - radius / 3. * sin (G_PI * (0.5 + 4/.3)),
114              subradius, subradius);
115   cairo_fill (cr);
116 }
117
118 static gboolean
119 on_draw (GtkWidget *widget,
120          cairo_t   *cr)
121 {
122   cairo_surface_t *overlay, *punch, *circles;
123   cairo_t *overlay_cr, *punch_cr, *circles_cr;
124
125   /* Fill the background */
126   int width = gtk_widget_get_allocated_width (widget);
127   int height = gtk_widget_get_allocated_height (widget);
128   double radius = 0.5 * (width < height ? width : height) - 10;
129   double xc = width / 2.;
130   double yc = height / 2.;
131
132   overlay = cairo_surface_create_similar (cairo_get_target (cr),
133                                           CAIRO_CONTENT_COLOR_ALPHA,
134                                           width, height);
135
136   punch = cairo_surface_create_similar (cairo_get_target (cr),
137                                         CAIRO_CONTENT_ALPHA,
138                                         width, height);
139
140   circles = cairo_surface_create_similar (cairo_get_target (cr),
141                                           CAIRO_CONTENT_COLOR_ALPHA,
142                                           width, height);
143     
144   fill_checks (cr, 0, 0, width, height);
145
146   /* Draw a black circle on the overlay
147    */
148   overlay_cr = cairo_create (overlay);
149   cairo_set_source_rgb (overlay_cr, 0., 0., 0.);
150   oval_path (overlay_cr, xc, yc, radius, radius);
151   cairo_fill (overlay_cr);
152
153   /* Draw 3 circles to the punch surface, then cut
154    * that out of the main circle in the overlay
155    */
156   punch_cr = cairo_create (punch);
157   draw_3circles (punch_cr, xc, yc, radius, 1.0);
158   cairo_destroy (punch_cr);
159
160   cairo_set_operator (overlay_cr, CAIRO_OPERATOR_DEST_OUT);
161   cairo_set_source_surface (overlay_cr, punch, 0, 0);
162   cairo_paint (overlay_cr);
163
164   /* Now draw the 3 circles in a subgroup again
165    * at half intensity, and use OperatorAdd to join up
166    * without seams.
167    */
168   circles_cr = cairo_create (circles);
169   
170   cairo_set_operator (circles_cr, CAIRO_OPERATOR_OVER);
171   draw_3circles (circles_cr, xc, yc, radius, 0.5);
172   cairo_destroy (circles_cr);
173
174   cairo_set_operator (overlay_cr, CAIRO_OPERATOR_ADD);
175   cairo_set_source_surface (overlay_cr, circles, 0, 0);
176   cairo_paint (overlay_cr);
177
178   cairo_destroy (overlay_cr);
179
180   cairo_set_source_surface (cr, overlay, 0, 0);
181   cairo_paint (cr);
182
183   cairo_surface_destroy (overlay);
184   cairo_surface_destroy (punch);
185   cairo_surface_destroy (circles);
186
187   return FALSE;
188 }
189
190 int
191 main (int argc, char **argv)
192 {
193   GtkWidget *window, *darea;
194
195   gtk_init (&argc, &argv);
196
197   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
198   
199   gtk_window_set_default_size (GTK_WINDOW (window), 400, 400);
200   gtk_window_set_title (GTK_WINDOW (window), "cairo: Knockout Groups");
201
202   darea = gtk_drawing_area_new ();
203   gtk_container_add (GTK_CONTAINER (window), darea);
204
205   g_signal_connect (darea, "draw",
206                     G_CALLBACK (on_draw), NULL);
207   g_signal_connect (window, "destroy-event",
208                     G_CALLBACK (gtk_main_quit), NULL);
209
210   gtk_widget_show_all (window);
211   
212   gtk_main ();
213
214   return 0;
215 }