[cairo] I don't see the pattern

Bertram Felgenhauer bertram.felgenhauer at googlemail.com
Thu Dec 6 05:54:53 PST 2007


Donn wrote:
> Hi all,
>  I have been trying to figure out (what and) how patterns work. This is the 
> sort of code I've been using:
> 
>         cr.push_group()
>         # Create two rects
>         cr.set_source_rgb(1,0,0)
>         cr.rectangle(0,0,50,50)
>         cr.fill()
>         cr.set_source_rgb(1,1,0)
>         cr.rectangle(0,50,50,50)
>         cr.fill()
>         #  I want them to be the 'source' for later
>         cr.pop_group_to_source()

Use
        pattern = cr.pop_group()
here. (See below for an explanation)

>         
>         #cr.translate(100,100) # tried to move the paint ()

This is actually a good idea, but it does not work this way.

>         # Attempt to draw a path and fill it from the 'source' above
>         cr.move_to ( 128.0, 25.6)
>         cr.line_to ( 230.4, 230.4)
>         cr.rel_line_to ( -102.4, 0.0)
>         cr.curve_to ( 51.2, 230.4, 51.2, 128.0, 128.0, 128.0)
>         cr.close_path ()

To set the translated pattern as a source, use
        cr.translate (100,100) # set up transformation for pattern
        cr.set_source (pattern)

>         #cr.fill() # to see what I just drew

This should work then.

>         #cr.paint() # paints the two recs in top left: unexpected.

pop_group_to_source() is a combination of pop_group() and set_source().

Once a pattern is set as a source, it is locked in; changing the
transformation afterwards will have no effect on the pattern.

To translate the pattern, you need to change the transformation *before*
setting the pattern as the source, so you should do the two steps of
pop_group_to_source() separately.

HTH,

Bertram


More information about the cairo mailing list