#include #include int main() { // Create surface and draw cairo_surface_t *surface = cairo_recording_surface_create(CAIRO_CONTENT_COLOR_ALPHA,0); cairo_t *ctx = cairo_create(surface); //cairo_translate(ctx,1000,1000); cairo_rectangle(ctx, -300, -300, 600, 600); cairo_set_source_rgb(ctx, 0.5, 0.2, 0.8); cairo_fill(ctx); cairo_rectangle(ctx, 0.0, 0.0, 300, 300); cairo_set_source_rgb(ctx, 0.3, 0.6, 0.8); cairo_fill(ctx); // This rectangle doesn't show up in pdf output cairo_rectangle(ctx, -300, -300, 300, 300); cairo_set_source_rgb(ctx, 0.6, 0.9, 0.2); cairo_fill(ctx); cairo_destroy(ctx); // Get surface extents double x,y,w,h; cairo_recording_surface_ink_extents(surface,&x,&y,&w,&h); // PDF output cairo_surface_t *output = cairo_pdf_surface_create("block.pdf",w,h); ctx = cairo_create(output); cairo_set_source_surface(ctx,surface,-x,-y); cairo_paint(ctx); cairo_show_page(ctx); cairo_destroy(ctx); cairo_surface_flush(output); cairo_surface_destroy(output); // PNG output output = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,w,h); ctx = cairo_create(output); cairo_set_source_surface(ctx,surface,-x,-y); cairo_paint(ctx); cairo_surface_write_to_png(output,"block.png"); cairo_destroy(ctx); cairo_surface_flush(output); cairo_surface_destroy(output); cairo_surface_destroy(surface); return 0; }