0

I am interesting in drawing various paths in cairo. Then these paths are accessed via cairo_path_t and cairo_path_data_t for pango usage. This becomes a problem when I use patterns. For example the following code works great.

   cairo_surface_t* pat_surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 2 * 500, 1000);
        cairo_t* pat;
    
        if (pat_surf)
            pat = cairo_create(pat_surf);
    
    cairo_translate(pat, amp, line_width);
    cairo_move_to(pat, 0, 0);
    cairo_line_to(pat, 500, 500);
    cairo_translate(pat, -500, 0);

I can access the paths using pat. However, if I use this as a repeating pattern. The paths are lost.

cr = cairo_create(result_surface);
cairo_pattern_t* pattern1 = cairo_pattern_create_for_surface(pat_surf);
cairo_pattern_set_extend(pattern1, CAIRO_EXTEND_REPEAT);
cairo_translate(cr, 200, 400);
cairo_set_source(cr, pattern1);
cairo_translate(cr, -200, -400);

I cannot access any paths using cr. As a test, if I add a rectangle to define the window, only the window path is visible. The repeating pattern disappears.

cairo_rectangle(cr, 0, 200, 1000, 400);

Any idea what I am doing wrong ?

moi
  • 467
  • 4
  • 19

1 Answers1

0

cairo_path_t is for representing a path. However, your code contains this line:

cairo_set_source(cr, pattern1);

The source is distinct from the path. Thus, of course changing the source does not change the path.

A bit more different:

  • The source defines the "color" to use for drawing, where "color" is in quotes because it can also be a gradient or even a whole surface.
  • The path defines which parts of the source to use for drawing.

See https://www.cairographics.org/tutorial/#L3source and https://www.cairographics.org/tutorial/#L3path (and perhaps the whole tutorial since the parts that I link to are a bit dense).

Uli Schlachter
  • 9,337
  • 1
  • 23
  • 39
  • 1
    Got it. Thats a good way to put it. So all point movements i.e. line, arc etc are on a path. And they can be accessed until you do a stroke or a fill. But a pattern is the background, so there is no path data. – moi Jul 04 '21 at 14:02