[cairo] fill pre-existing png polygons? (tried formatting better)

Daniel Goldman dagoldman at yahoo.com
Mon Aug 9 21:04:49 PDT 2010

On Mon, 9 Aug 2010, Daniel Goldman wrote:

> What might be some example cairo functions that cache surfaces? I think what 
> are saying is maybe I can just draw once, and then copy to the png and pdf 
> surfaces.

Yeah, almost exactly that.  In the upcoming 1.10 release exactly this 
is possible and you'll be able to use a "recording surface" instead of 
a "similar surface" to capture drawing commands for later replay.  
This doesn't give you any speed benefits like using a similar surface 
might, however.

For the moment, however, you'd need to create an image surface for 
caching the drawing when you're making a png map and a pdf surface for 
caching when making a pdf map.  You can create the right kind of 
surface using the function cairo_surface_create_similar(): this takes 
a surface and produces a new empty surface of the same kind (this is 
what the API means by "similar") as the one you gave it.  Then you'd 
draw onto your polygons onto the similar surface normally once, and 
when you want to reuse your drawing later, you'd use 
cairo_set_source_surface() and cairo_paint() (or perhaps cairo_fill()) 
on your target context to copy the previous rendering.

> I have the png polygon coordinates as integers, and the pdf 
> coordinates as doubles. Maybe I might draw with the doubles, and then copy the 

> surface to the png map, possibly resizing if a bigger or smaller map is 

I'm not entirely sure what you mean here.  Cairo always takes polygon 
coordinates as doubles.



********* new reply below

Thanks for the further explanation about the surfaces. I don't "get it" yet, but 
I'm sure I will once I do some experiments. cairo is really great.

What I meant was that png output really only takes ints, since it's a raster 
file. So at some point in generating the png map, I currently generate ints. For 
example, there is no point in having png pixel 31.1, pixel 31.2, etc., since 
they round off to 31. Also, some of the png polygons get simplified (leave off 
some coordinates). I will try using the double pdf coordinates, have cairo 
generate the png, and see how it goes. Of course, the pdf has much better 
printing resolution for these maps, and the user can zoom, as you pointed out. 
But the png fits inside a web page, which is nice.


More information about the cairo mailing list