[cairo] Non-transparent composition of transparent objects

LRN lrn1986 at gmail.com
Wed Nov 13 00:09:53 PST 2013


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The [1] shows the output various composition operators, but the effect
i'd like to achieve is not obvious from that documentation.

What i want to do is to draw two transparent objects in sequence, and
second object should be painted only in places where first object
isn't (at all).

Now, AFAICS, this can be achieved by just using CAIRO_OPERATOR_SOURCE
and drawing object in reverse order: second object is drawn, then
first object is drawn on top of it, resulting in a composition of
both, where second object can be seen only in places where first isn't.

Doesn't seem to be working though, because i'm drawing with surface
patterns.

Here's the source:

gboolean
drawing_area_expose_event_cb (GtkWidget *widget, GdkEventExpose *ee,
gpointer user_data)
{
  draw_all (widget->window, 0, 0, ee->area.width, ee->area.height);
  return FALSE;
}

int
draw_all (GdkWindow *gdk_window, gint x, gint y, gint w, gint h)
{
  cairo_t *cr;
  cairo_surface_t *s;
  cairo_t *cr2;
  cairo_pattern_t *p;

  # Fill gdk window with white
  cr = gdk_cairo_create (gdk_window);
  cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
  cairo_rectangle (cr, 0, 0, w, h);
  cairo_fill (cr);

  # For clarity, use an image surface instead of a similar surface
  s = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 100, 100);
  cr2 = cairo_create (s);

  # Draw a cyan line on the second surface
  cairo_set_source_rgba (cr2, 0, 1, 1, 1);
  cairo_move_to (cr2, 0, 0);
  cairo_line_to (cr2, 100, 100);
  cairo_stroke (cr2);
  cairo_destroy (cr2);

  # Make a pattern out of it
  p = cairo_pattern_create_for_surface (s);

  # Paint that
  cairo_set_source (cr, p);
  cairo_rectangle (cr, 0, 0, 100, 100);
  cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
  cairo_fill (cr);

  cairo_pattern_destroy (p);
  cairo_surface_destroy (s);
  cairo_destroy (cr);

  return 0;
}

Desired result is to have cyan line over white background.
Actual result is a black 100x100 rectangle with cyan line in it, drawn
over white background.

[1] http://cairographics.org/operators/

- -- 
O< ascii ribbon - stop html email! - www.asciiribbon.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (MingW32)

iQEcBAEBAgAGBQJSgzPPAAoJEOs4Jb6SI2CwKN8IALVgV5SHSRepWwti9ih++V+A
QUfzS6PlbWJPZH+Q7AZWz5ckbV6LEuUDXa7ZBLRpF4Aqp9kA0BvfnCMid8KX+FCh
3NJRxX4jR+B5hi2N8pk6gTNYZarAGBhyhZ1AARAYtedXxwLyYznMWSFY82igfN5j
1Vcb9ODvm2Fj/Lb+FXJUKZpHIjaZZLsK9FzG+l7BbmIsyUaKFwpxKB6ys3Rj+uKB
tG8rgugCF0AEHwN7QB9lyw/zjyWSxu47bOJ0kOZmk+mAg/aB0cU27D8eV9rmb00a
3uFZhAY+w9o6TU3i47ELx7SGabySGWeqcGHYRKVjiS1agLrjamICNRjZQDn39r0=
=nvsA
-----END PGP SIGNATURE-----


More information about the cairo mailing list