cairopangocairo

Access cairo_path_t when generated using a pattern


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 ?


Solution

  • 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:

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