#include <cairo.h>
#include <cairo-pdf.h>
#include <cairo-ps.h>

static void box(cairo_t* cairo) {
    cairo_save(cairo); // makes no difference with regard to the bug
    cairo_move_to(cairo, 0, 0); // does not fix the problem
    cairo_rectangle(cairo, 10, 10, 52, 52);
    cairo_pattern_t* grey = cairo_pattern_create_rgb(.5, .5, .5);
    cairo_set_source(cairo, grey);
    cairo_fill(cairo);
    cairo_pattern_destroy(grey);
    cairo_restore(cairo);
}

static void text(cairo_t* cairo) {
    cairo_select_font_face(cairo, "Times", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
    cairo_move_to(cairo, 10, 10);
    cairo_show_text(cairo, "Hello World!");
}

static void done(cairo_t* cairo) {
    cairo_show_page(cairo);
    cairo_destroy(cairo);
}

int main(int argc, char** argv) {
    // pdf, without resizing
    cairo_surface_t* pdf_surface = cairo_pdf_surface_create("out.pdf", 72, 72);
    cairo_t* pdf_cairo = cairo_create(pdf_surface);
    cairo_surface_destroy(pdf_surface);
    // pdf, with resizing
    cairo_surface_t* pdf_surface_resized = cairo_pdf_surface_create("out_resized.pdf", 36, 36);
    cairo_t* pdf_cairo_resized = cairo_create(pdf_surface_resized);
    cairo_pdf_surface_set_size(pdf_surface_resized, 72, 72);
    cairo_surface_destroy(pdf_surface_resized);
    // ps, without resizing
    cairo_surface_t* ps_surface = cairo_ps_surface_create("out.ps", 72, 72);
    cairo_t* ps_cairo = cairo_create(ps_surface);
    cairo_surface_destroy(ps_surface);
    // ps, with resizing
    cairo_surface_t* ps_surface_resized = cairo_ps_surface_create("out_resized.ps", 36, 36);
    cairo_t* ps_cairo_resized = cairo_create(ps_surface_resized);
    cairo_ps_surface_set_size(ps_surface_resized, 72, 72);
    cairo_surface_destroy(ps_surface_resized);
    // draw stuff
    box(pdf_cairo);
    text(pdf_cairo);
    done(pdf_cairo);
    box(pdf_cairo_resized);
    text(pdf_cairo_resized);
    done(pdf_cairo_resized);
    box(ps_cairo);
    text(ps_cairo);
    done(ps_cairo);
    box(ps_cairo_resized);
    text(ps_cairo_resized);
    done(ps_cairo_resized);
}
