[cairo] Pretty-please, help with cropping

Carl Worth cworth at cworth.org
Tue Mar 6 13:07:02 PST 2007

On Tue, 6 Mar 2007 15:28:30 -0500, Marty Sherrill wrote:
> I think I posted my question right in the heat of your big release
> yesterday, so I hope you'll forgive me for re-posting it today.

Yeah, sorry about that. The reposting is not a problem at all.

> Would one of you experts be willing to give me just a quick hint at how to
> size (or resize) an image surface *after* I've drawn the content, in order
> to output a .PNG that is right-sized for the drawing?  I've spent hours
> fumbling with clipping functions without success. . .

So, if I understand your question correctly, you've now got
big_surface that's big but has useful content only within a rectangle
(x,y,width,height), and you've got small_surface that's just the right
size (width,height).

So, copying the sub-piece of big_surface of interest would be as
simple as this:

	cairo_t *cr = cairo_create (small_surface);
	cairo_set_source_surface (cr, big_surface, -x, -y);
	cairo_rectangle (cr, 0, 0, width, height);
	cairo_fill (cr);

The interesting thing to note here, (and what will hopefully be an
"Ah-ha!" moment for some readers), is that cairo does not have any
drawing operation that is "paint an image". Instead, cairo has a much
more general notion where you can use an image, (or other more general
patterns, such as gradients), as the source pattern. And with that,
you can draw any shape you want, and the appropriate pieces of the
source surface will appear within that shape.

Does that make sense? Give the above sequence a try and let us know
how it goes.

> Congratulations on Cairo version 1.4.0.  It's really a super package.

Thanks! I hope you have lots of fun with it.

> p.s. I'm using rcairo on Red Hat, if that matters.

I'm not up on ruby syntax, but hopefully it is extremely
straightforward for you to translate the above from C to ruby.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.freedesktop.org/archives/cairo/attachments/20070306/92fee980/attachment-0001.pgp

More information about the cairo mailing list