zathura/print.c

188 lines
6.9 KiB
C
Raw Permalink Normal View History

2011-09-29 18:05:15 +02:00
/* See LICENSE file for license and copyright information */
2011-04-29 00:28:19 +02:00
#include "print.h"
#include "document.h"
2011-09-29 18:05:15 +02:00
#include "render.h"
2012-03-26 14:44:56 +02:00
#include "page.h"
2011-04-29 00:28:19 +02:00
#include <girara/utils.h>
#include <girara/statusbar.h>
static void cb_print_draw_page(GtkPrintOperation* print_operation,
2012-10-09 01:12:18 +02:00
GtkPrintContext* context, gint page_number, zathura_t* zathura);
static void cb_print_end(GtkPrintOperation* print_operation, GtkPrintContext*
2012-10-09 01:12:18 +02:00
context, zathura_t* zathura);
static void cb_print_request_page_setup(GtkPrintOperation* print_operation,
2012-10-09 01:12:18 +02:00
GtkPrintContext* context, gint page_number, GtkPageSetup* setup, zathura_t*
zathura);
2011-04-29 00:28:19 +02:00
void
print(zathura_t* zathura)
{
g_return_if_fail(zathura != NULL);
g_return_if_fail(zathura->document != NULL);
GtkPrintOperation* print_operation = gtk_print_operation_new();
/* print operation settings */
if (zathura->print.settings != NULL) {
gtk_print_operation_set_print_settings(print_operation, zathura->print.settings);
}
if (zathura->print.page_setup != NULL) {
gtk_print_operation_set_default_page_setup(print_operation, zathura->print.page_setup);
}
2011-09-29 15:23:13 +02:00
gtk_print_operation_set_allow_async(print_operation, TRUE);
gtk_print_operation_set_n_pages(print_operation, zathura_document_get_number_of_pages(zathura->document));
gtk_print_operation_set_current_page(print_operation, zathura_document_get_current_page_number(zathura->document));
2012-06-03 22:07:42 +02:00
gtk_print_operation_set_use_full_page(print_operation, TRUE);
2012-12-07 14:24:14 +01:00
gtk_print_operation_set_embed_page_setup(print_operation, TRUE);
2011-04-29 00:28:19 +02:00
/* print operation signals */
g_signal_connect(print_operation, "draw-page", G_CALLBACK(cb_print_draw_page), zathura);
g_signal_connect(print_operation, "end-print", G_CALLBACK(cb_print_end), zathura);
2012-06-11 23:04:21 +02:00
g_signal_connect(print_operation, "request-page-setup", G_CALLBACK(cb_print_request_page_setup), zathura);
2011-04-29 00:28:19 +02:00
/* print */
GtkPrintOperationResult result = gtk_print_operation_run(print_operation,
2012-10-09 01:12:18 +02:00
GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, NULL, NULL);
2011-04-29 00:28:19 +02:00
if (result == GTK_PRINT_OPERATION_RESULT_APPLY) {
if (zathura->print.settings != NULL) {
g_object_unref(zathura->print.settings);
}
if (zathura->print.page_setup != NULL) {
g_object_unref(zathura->print.page_setup);
}
2011-04-29 00:28:19 +02:00
/* save previous settings */
zathura->print.settings = g_object_ref(gtk_print_operation_get_print_settings(print_operation));
zathura->print.page_setup = g_object_ref(gtk_print_operation_get_default_page_setup(print_operation));
2011-04-29 00:28:19 +02:00
} else if (result == GTK_PRINT_OPERATION_RESULT_ERROR) {
girara_error("Error pccurred while printing progress");
2011-04-29 00:28:19 +02:00
}
g_object_unref(print_operation);
}
static void
2011-09-29 18:05:15 +02:00
cb_print_end(GtkPrintOperation* UNUSED(print_operation), GtkPrintContext*
2012-10-09 01:12:18 +02:00
UNUSED(context), zathura_t* zathura)
{
if (zathura == NULL || zathura->ui.session == NULL || zathura->document == NULL) {
2011-09-29 18:05:15 +02:00
return;
}
const char* file_path = zathura_document_get_path(zathura->document);
if (file_path != NULL) {
girara_statusbar_item_set_text(zathura->ui.session,
2012-10-09 01:12:18 +02:00
zathura->ui.statusbar.file, file_path);
}
}
static void
cb_print_draw_page(GtkPrintOperation* print_operation, GtkPrintContext*
2012-10-09 01:12:18 +02:00
context, gint page_number, zathura_t* zathura)
2011-04-29 00:28:19 +02:00
{
if (context == NULL || zathura == NULL || zathura->document == NULL ||
2011-09-29 18:05:15 +02:00
zathura->ui.session == NULL || zathura->ui.statusbar.file == NULL) {
gtk_print_operation_cancel(print_operation);
2011-09-29 18:05:15 +02:00
return;
}
2012-12-07 15:24:57 +01:00
/* Update statusbar. */
2011-09-29 18:05:15 +02:00
char* tmp = g_strdup_printf("Printing %d...", page_number);
girara_statusbar_item_set_text(zathura->ui.session,
2012-10-09 01:12:18 +02:00
zathura->ui.statusbar.file, tmp);
2011-09-29 18:05:15 +02:00
g_free(tmp);
2012-12-07 15:24:57 +01:00
/* Get the page and cairo handle. */
cairo_t* cairo = gtk_print_context_get_cairo_context(context);
zathura_page_t* page = zathura_document_get_page(zathura->document, page_number);
2011-09-29 18:05:15 +02:00
if (cairo == NULL || page == NULL) {
gtk_print_operation_cancel(print_operation);
2011-09-29 18:05:15 +02:00
return;
}
2012-12-07 15:24:57 +01:00
/* Try to render the page without a temporary surface. This only works with
* plugins that support rendering to any surface. */
2012-12-07 15:11:57 +01:00
girara_debug("printing page %d ...", page_number);
zathura_renderer_lock(zathura->sync.render_thread);
2012-12-07 15:11:57 +01:00
int err = zathura_page_render(page, cairo, true);
zathura_renderer_unlock(zathura->sync.render_thread);
2012-12-07 15:11:57 +01:00
if (err == ZATHURA_ERROR_OK) {
return;
}
2012-12-07 15:24:57 +01:00
/* Try to render the page on a temporary image surface. */
const gdouble width = gtk_print_context_get_width(context);
const gdouble height = gtk_print_context_get_height(context);
/* Render to a surface that is 5 times larger to workaround quality issues. */
const double page_height = zathura_page_get_height(page) * 5;
const double page_width = zathura_page_get_width(page) * 5;
2012-12-07 14:53:09 +01:00
cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, page_width, page_height);
if (surface == NULL) {
gtk_print_operation_cancel(print_operation);
return;
}
cairo_t* temp_cairo = cairo_create(surface);
if (cairo == NULL) {
gtk_print_operation_cancel(print_operation);
cairo_surface_destroy(surface);
return;
}
2012-12-07 15:24:57 +01:00
/* Draw a white background. */
cairo_save(temp_cairo);
cairo_set_source_rgb(temp_cairo, 1, 1, 1);
2012-12-07 14:53:09 +01:00
cairo_rectangle(temp_cairo, 0, 0, page_width, page_height);
cairo_fill(temp_cairo);
cairo_restore(temp_cairo);
2012-12-07 15:24:57 +01:00
/* Render the page to the temporary surface */
girara_debug("printing page %d (fallback) ...", page_number);
zathura_renderer_lock(zathura->sync.render_thread);
2012-12-07 15:11:57 +01:00
err = zathura_page_render(page, temp_cairo, true);
zathura_renderer_unlock(zathura->sync.render_thread);
2012-12-07 15:11:57 +01:00
if (err != ZATHURA_ERROR_OK) {
cairo_destroy(temp_cairo);
cairo_surface_destroy(surface);
gtk_print_operation_cancel(print_operation);
return;
}
2012-12-07 15:24:57 +01:00
/* Rescale the page and keep the aspect ratio */
2012-12-07 14:53:09 +01:00
const gdouble scale = MIN(width / page_width, height / page_height);
cairo_scale(cairo, scale, scale);
2012-12-07 15:24:57 +01:00
/* Blit temporary surface to original cairo object. */
cairo_set_source_surface(cairo, surface, 0.0, 0.0);
cairo_paint(cairo);
cairo_destroy(temp_cairo);
cairo_surface_destroy(surface);
2011-04-29 00:28:19 +02:00
}
static void
cb_print_request_page_setup(GtkPrintOperation* UNUSED(print_operation),
2014-01-06 16:02:58 +01:00
GtkPrintContext* UNUSED(context), gint page_number,
GtkPageSetup* setup, zathura_t* zathura)
{
if (zathura == NULL || zathura->document == NULL) {
return;
}
zathura_page_t* page = zathura_document_get_page(zathura->document, page_number);
double width = zathura_page_get_width(page);
double height = zathura_page_get_height(page);
if (width > height) {
gtk_page_setup_set_orientation(setup, GTK_PAGE_ORIENTATION_LANDSCAPE);
} else {
gtk_page_setup_set_orientation(setup, GTK_PAGE_ORIENTATION_PORTRAIT);
}
}