From 23649bc0bc5a975669c700298bfa846538e6e8c5 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Thu, 6 Dec 2012 21:43:21 +0100 Subject: [PATCH] Fix printing The plugins expect an image surface but the target of the print context might not be one. --- print.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/print.c b/print.c index 0f0fe4b..133a852 100644 --- a/print.c +++ b/print.c @@ -103,10 +103,40 @@ cb_print_draw_page(GtkPrintOperation* UNUSED(print_operation), GtkPrintContext* return; } + /* we need a temporary cairo object since the cairo object from the print + * context doesn't have a width or height */ + const gdouble width = gtk_print_context_get_width(context); + const gdouble height = gtk_print_context_get_height(context); + + cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height); + if (surface == NULL) { + return; + } + + cairo_t* temp_cairo = cairo_create(surface); + if (cairo == NULL) { + cairo_surface_destroy(surface); + return; + } + + /* white background */ + cairo_save(cairo); + cairo_set_source_rgb(cairo, 1, 1, 1); + cairo_rectangle(cairo, 0, 0, width, height); + cairo_fill(cairo); + cairo_restore(cairo); + + /* render the page to the temporary surface */ girara_debug("printing page %d ...", page_number); render_lock(zathura->sync.render_thread); - zathura_page_render(page, cairo, true); + zathura_page_render(page, temp_cairo, true); render_unlock(zathura->sync.render_thread); + + /* copy temporary surface */ + cairo_set_source_surface(cairo, surface, 0.0, 0.0); + cairo_paint(cairo); + cairo_destroy(temp_cairo); + cairo_surface_destroy(surface); } static void