#include "stdio.h" #include "cairo.h" cairo_status_t write_func(void * closure, const unsigned char *data, unsigned length) { if (closure==NULL) return CAIRO_STATUS_SUCCESS; if (length == fwrite(data, 1, length, (FILE*)closure)) return CAIRO_STATUS_SUCCESS; else return CAIRO_STATUS_WRITE_ERROR; } int main() { /* Vary these input parameters */ const int offset = 20000; /* How much do we offset our elements in Y direction. */ const double scale = 1.6; /* How much scaling do we want to apply */ /* Additional preparation */ const int viewport_x = 200, viewport_y= 200; /* The size of our viewport in pixels.*/ char scale_text[1000]; char fname[1000]; sprintf(scale_text, "scale: %g, offset: %i", scale, offset); sprintf(fname, "test_%i_%i.png", (int)(scale*100), offset); /* Record a simple line and text at y position of 'offset'. */ /* Note that the recorded surface does not depend on scale or * offset other than the text output */ cairo_surface_t *rec = cairo_recording_surface_create(CAIRO_CONTENT_COLOR_ALPHA, NULL); cairo_t *cr = cairo_create(rec); cairo_move_to(cr, 0, offset); cairo_line_to(cr, 100, offset); cairo_stroke(cr); cairo_move_to(cr, 0, offset); cairo_show_text(cr, scale_text); cairo_destroy(cr); /* Replay the recorded surface to an image scaled by 'scale'. * (We only play back a small area of it around our elements to reduce * output image size to 'viewport'.)*/ cairo_surface_t *surf = cairo_image_surface_create(CAIRO_FORMAT_RGB24, viewport_x, viewport_y); cr = cairo_create(surf); cairo_set_source_rgb(cr, 1, 1, 1); cairo_rectangle(cr, 0, 0, viewport_x, viewport_y); cairo_fill(cr); cairo_scale(cr, scale, scale); cairo_set_source_surface(cr, rec, 0, -offset+viewport_y/2/scale); cairo_paint(cr); cairo_destroy(cr); FILE *outFile = fopen(fname, "wb"); cairo_surface_write_to_png_stream(surf, write_func, outFile); fclose(outFile); cairo_surface_destroy(surf); }