Dooly
Dooly

Reputation: 1

How to access Cairo drawn path?

I'm using cairomm to draw object.

#include "cairo/cairo.h"


int main()
{
    cairo_surface_t *surface;
    cairo_t *cr1;

    double width = 3840;
    double height = 2160;

    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
    cr1 = cairo_create (surface);

    cairo_move_to(cr1, 0, 0);
    cairo_set_source_rgb(cr1, 1, 1, 1);
    cairo_set_line_width(cr1, 50.0);
    cairo_move_to(cr1, 0, 0)
    cairo_line_to(cr1, width, height)
    cairo_stroke();


    return 0;
}

Above code makes a line. I want to manipulate(to move, to extend, etc..) the line after the line is created. Are there any solutions?

Upvotes: 0

Views: 276

Answers (1)

ile
ile

Reputation: 11

Your code is creating a path containing one line. You have to copy the current path from drawing context to the end of a cairo_path_t pointer before calling cairo_stroke (or anything else that clears the current path) to be able to manipulate that path.

In my example I extend that copied path a little bit and then stroke it again with different colour. I also used non-transparent image format and wrote that created image to png file to see the results.

#include <cairo/cairo.h>

int main() {
    double width = 3840;
    double height = 2160;

    cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height);
    cairo_t* cr = cairo_create(surface);

    cairo_move_to(cr, 0, 0);
    cairo_line_to(cr, width, height);

    cairo_path_t* path = cairo_copy_path(cr);

    // Draw original path
    cairo_set_source_rgb(cr, 1, 1, 1);
    cairo_set_line_width(cr, 50.0);
    cairo_stroke(cr);

    cairo_move_to(cr, 0, 0);
    cairo_append_path(cr, path);

    // Extend that path with some additional line
    cairo_rel_line_to(cr, -1 * width / 2, -1 * height / 3);

    // Draw extended path with different colour
    cairo_set_source_rgb(cr, 1, 0, 0);
    cairo_set_line_width(cr, 25.0);
    cairo_stroke(cr);

    cairo_surface_write_to_png(surface, "output.png");

    cairo_path_destroy(path);
    cairo_destroy(cr);
    cairo_surface_destroy(surface);
}

Sidenote: You are not using cairomm wrapper but the original cairo API.

Upvotes: 1

Related Questions