testcairo.c 6.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/* testimage.c
 * Copyright (C) 2005  Red Hat, Inc.
 * Based on cairo-demo/X11/cairo-knockout.c
 *
 * Author: Owen Taylor
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <math.h>

#include <gtk/gtk.h>

static void
oval_path (cairo_t *cr,
           double xc, double yc,
           double xr, double yr)
{
  cairo_matrix_t *matrix;

  matrix = cairo_matrix_create ();
  cairo_current_matrix (cr, matrix);

  cairo_translate (cr, xc, yc);
  cairo_scale (cr, 1.0, yr / xr);
  cairo_move_to (cr, xr, 0.0);
  cairo_arc (cr,
	     0, 0,
	     xr,
	     0, 2 * G_PI);
  cairo_close_path (cr);

  cairo_set_matrix (cr, matrix);
  cairo_matrix_destroy (matrix);
}

/* Create a path that is a circular oval with radii xr, yr at xc,
 * yc.
 */
/* Fill the given area with checks in the standard style
 * for showing compositing effects.
 *
 * It would make sense to do this as a repeating surface,
 * but most implementations of RENDER currently have broken
 * implementations of repeat + transform, even when the
 * transform is a translation.
 */
static void
fill_checks (cairo_t *cr,
             int x,     int y,
             int width, int height)
{
  int i, j;
  
#define CHECK_SIZE 32

  cairo_rectangle (cr, x, y, width, height);
71
  cairo_set_source_rgb (cr, 0.4, 0.4, 0.4);
72 73 74 75 76 77 78 79 80 81 82 83 84
  cairo_fill (cr);

  /* Only works for CHECK_SIZE a power of 2 */
  j = x & (-CHECK_SIZE);
  
  for (; j < height; j += CHECK_SIZE)
    {
      i = y & (-CHECK_SIZE);
      for (; i < width; i += CHECK_SIZE)
	if ((i / CHECK_SIZE + j / CHECK_SIZE) % 2 == 0)
	  cairo_rectangle (cr, i, j, CHECK_SIZE, CHECK_SIZE);
    }

85
  cairo_set_source_rgb (cr, 0.7, 0.7, 0.7);
86 87 88 89 90 91 92 93 94
  cairo_fill (cr);
}

/* Draw a red, green, and blue circle equally spaced inside
 * the larger circle of radius r at (xc, yc)
 */
static void
draw_3circles (cairo_t *cr,
               double xc, double yc,
95 96
               double radius,
	       double alpha)
97 98 99
{
  double subradius = radius * (2 / 3. - 0.1);
    
100
  cairo_set_source_rgba (cr, 1., 0., 0., alpha);
101 102 103 104 105 106
  oval_path (cr,
	     xc + radius / 3. * cos (G_PI * (0.5)),
	     yc - radius / 3. * sin (G_PI * (0.5)),
	     subradius, subradius);
  cairo_fill (cr);
    
107
  cairo_set_source_rgba (cr, 0., 1., 0., alpha);
108 109 110 111 112 113
  oval_path (cr,
	     xc + radius / 3. * cos (G_PI * (0.5 + 2/.3)),
	     yc - radius / 3. * sin (G_PI * (0.5 + 2/.3)),
	     subradius, subradius);
  cairo_fill (cr);
    
114
  cairo_set_source_rgba (cr, 0., 0., 1., alpha);
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  oval_path (cr,
	     xc + radius / 3. * cos (G_PI * (0.5 + 4/.3)),
	     yc - radius / 3. * sin (G_PI * (0.5 + 4/.3)),
	     subradius, subradius);
  cairo_fill (cr);
}

static void
draw (cairo_t *cr,
      int      width,
      int      height)
{
  cairo_surface_t *overlay, *punch, *circles;

  /* Fill the background */
  double radius = 0.5 * (width < height ? width : height) - 10;
  double xc = width / 2.;
  double yc = height / 2.;

  overlay = cairo_surface_create_similar (cairo_current_target_surface (cr),
					  CAIRO_FORMAT_ARGB32,
					  width, height);
  if (overlay == NULL)
    return;

  punch = cairo_surface_create_similar (cairo_current_target_surface (cr),
					CAIRO_FORMAT_A8,
					width, height);
  if (punch == NULL)
    return;

  circles = cairo_surface_create_similar (cairo_current_target_surface (cr),
					  CAIRO_FORMAT_ARGB32,
					  width, height);
  if (circles == NULL)
    return;
    
  fill_checks (cr, 0, 0, width, height);

  cairo_save (cr);
  cairo_set_target_surface (cr, overlay);
  cairo_identity_matrix (cr);

  /* Draw a black circle on the overlay
   */
160
  cairo_set_source_rgb (cr, 0., 0., 0.);
161 162 163 164 165 166 167 168 169
  oval_path (cr, xc, yc, radius, radius);
  cairo_fill (cr);

  cairo_save (cr);
  cairo_set_target_surface (cr, punch);

  /* Draw 3 circles to the punch surface, then cut
   * that out of the main circle in the overlay
   */
170
  draw_3circles (cr, xc, yc, radius, 1.0);
171 172 173 174 175 176 177 178 179 180 181 182 183 184

  cairo_restore (cr);

  cairo_set_operator (cr, CAIRO_OPERATOR_OUT_REVERSE);
  cairo_show_surface (cr, punch, width, height);

  /* Now draw the 3 circles in a subgroup again
   * at half intensity, and use OperatorAdd to join up
   * without seams.
   */
  cairo_save (cr);
  cairo_set_target_surface (cr, circles);

  cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
185
  draw_3circles (cr, xc, yc, radius, 0.5);
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

  cairo_restore (cr);

  cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
  cairo_show_surface (cr, circles, width, height);

  cairo_restore (cr);

  cairo_show_surface (cr, overlay, width, height);

  cairo_surface_destroy (overlay);
  cairo_surface_destroy (punch);
  cairo_surface_destroy (circles);
}

static gboolean
on_expose_event (GtkWidget      *widget,
		 GdkEventExpose *event,
		 gpointer        data)
{
  cairo_t *cr;

208
  cr = gdk_drawable_create_cairo_context (widget->window);
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

  draw (cr, widget->allocation.width, widget->allocation.height);

  cairo_destroy (cr);

  return FALSE;
}

int
main (int argc, char **argv)
{
  GtkWidget *window, *darea;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  
  gtk_window_set_default_size (GTK_WINDOW (window), 400, 400);
  gtk_window_set_title (GTK_WINDOW (window), "cairo: Knockout Groups");

  darea = gtk_drawing_area_new ();
  gtk_container_add (GTK_CONTAINER (window), darea);

  g_signal_connect (darea, "expose-event",
		    G_CALLBACK (on_expose_event), NULL);
  g_signal_connect (window, "destroy-event",
		    G_CALLBACK (gtk_main_quit), NULL);

  gtk_widget_show_all (window);
  
  gtk_main ();

  return 0;
}