[cairo] Polygon fill styles?

Ian Britten britten at caris.com
Wed Aug 20 09:47:59 PDT 2008


Kalle Vahlman wrote:
>> Hi all,
>> [ Disclaimer - I'm fearing that most Cairo-purists will cringe
>> at this question, but I've got to ask...  :( ]
> 
> Heh, all that disclaimer is going to do is *lower* your change of
> getting a constructive answer (who want's to be called a "purist"?).
> Fortunately for you, this place is pretty tolerant to such remarks :)

Oops!  Sorry, I didn't mean that in a negative way.  My apologies
if anyone took offence!

It's just after my foray into line weights the other day, I'm
beginning to realize that one of Cairo's main concerns is output
accuracy, and this doesn't always jive with some device-specific
operations that I'm trying to shoehorn into cairo (Like fill
styles!)

>> Failing any native cairo support, does anyone have any suggestions
>> (Or maybe example code! :) ) for doing stuff like this?  I'm
>> suspect it'll be something to do with a cairo_pattern_t...  (But
>> that's just one of many cairo things I haven't learned yet...)
> 
> You are right on the money, patterns are exactly what is used for this.
> 
> The idea is that you first create a small surface (preferrably with
> create_similar() from the surface you want to draw to) and draw
> whatever it is you want to repeat to it. 

Ok, I think this is my mental stumbling block.  Are you suggesting
that in order to fill with a grid pattern (for example), I'll need
to dynamically draw a couple of lines to a temporary surface (at
runtime), to generate the pattern?

I guess I can do that (and cache it, etc), but is there no way to
help work with precanned stuff?  For example, GTK has a
gdk_bitmap_create_from_data() function, letting you just load (Or
#include) an externally created XPM file.  I didn't see anything
like that in cairo.

Obviously the first approach will result in nicer vector output,
is more accurate, more flexible, etc.  I'm just trying to keep
my eyes open for simple approaches, eliminating unnecessary
steps, etc.

> Then you use
> cairo_pattern_create_for_surface() to create a pattern of the surface.
> To get it repeated, you simply set the extend mode for the pattern
> with cairo_pattern_set_extend() to CAIRO_EXTEND_REPEAT.
> 
> Then you draw your polygon, but instead of setting the source to a
> color like normal solid fills, you set the previously created pattern
> as the source with cairo_set_source().
> 
> Easy as pie, no?

Sound like it, once I get the patterns made...  I may have more
questions at that time though.

Hmmm... I just got wondering if anyone knows how patterns get
mapped to PS/PDF output?  Do all the individual pattern lines get
written out to the file when the pattern is applied to each polygon,
or is the pattern somehow refactored out?  For our current PS
output, we write out some custom PS procedures to do the fills, to
keep the file size from bloating.  However, maybe file size isn't
as much of a concern as it used to be...

Thank for the info!
Ian



More information about the cairo mailing list