[cairo] Polygon fill styles?

Travis Griggs tgriggs at cincom.com
Wed Aug 20 10:04:49 PDT 2008

On Aug 20, 2008, at 9:47 AM, Ian Britten wrote:

> 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.

Cairo has support of similiar ilk for Png format files. See the  
ImageSurface creation functions.


> 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?

An alternate way for doing this, at least for something like a grid  
pattern, would be to change the operations slightly.

Rather than
	build repeating image of grid
	set as source
	build path

You could:
	build path
	set it as the clip
	now just draw the grid (path the vertical and horizontal lines, then  
stroke them)

Travis Griggs
10 2 letter words: "If it is to be, it is up to me"

More information about the cairo mailing list