[cairo] Copy a drawable to a cairo surface

Carl Worth cworth at cworth.org
Tue Jul 31 09:55:59 PDT 2007


On Tue, 31 Jul 2007 09:16:07 +0200, Tobias Ceglarek wrote:
> I know that I can create a pangocairo.CairoContext with
> gtk.gdk.Drawable.cairo_create(). With it I can draw with cairo on the
> gtk.gdk.Drawable. But how can I change the surface and copy the contents
> of the old surface to it?

You can't change the surface that a cairo context is targeting, but
it's quite simple to just create your own cairo surface and cairo
context with something like:

	cairo_surface_t *image_surface = cairo_image_surface_create (...);
	cairo_t *cr = cairo_create (image_surface);

Then the only thing you need is a cairo surface for the GDK
Drawable. As far as I know, GDK hasn't yet added a convenience method
for creating this, (which is a nuisance, since it's really useful here
*and* it's already a necessary part of what happens as part of
gdk_cairo_create anyway).

But at least you can take advantage of the fact that GDK must create a
surface as part of implementing its cairo_create, so you can extract
the target surface from the context:

	cairo_t *tmp = gdk_cairo_create (drawable);
	drawable_surface = cairo_get_target (tmp);

And now that you have that surface you can copy it with something
simple like:

	cairo_set_source_surface (cr, drawable_surface, 0, 0);
	cairo_paint (cr);

Of course, I've given examples in C, but hopefully it's simple enough
to translate them to your language of chose, (for example,
gdk_cairo_create(drawable) probably becomes drawable.cairo_create() or
whatever).

And see the following for more hints on copying from one surface to
another:

	http://cairographics.org/FAQ/#paint_from_a_surface

Have fun with cairo!

-Carl

PS. The above should work, but may not be exactly what you want:

> My aim is to export the text created with a TextView to a PNG, PS, SVG
> or what else.

The steps I described above will simply copy the rasterized contents
of any drawable to any cairo surface. So you can get the contents of a
TextView to a PostScript file that way, but the contents will be an
image inside the PostScript file and not text. To get something with
less rasterization would require something like a way to redirect the
drawing of a widget to a cairo context that you could provide. And I
don't think GTK+ provides anything like that, (nor does the nature of
the GTK+/cairo integration easily allow for it to be extended that
way).

A future toolkit built with cairo in mind from the beginning could
definitely do a better job of allowing for things like all-vector "PDF
screenshots" from arbitrary widgets.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.cairographics.org/archives/cairo/attachments/20070731/54362d39/attachment.pgp 


More information about the cairo mailing list