Commit 601fb840 authored by Sebastian Ramacher's avatar Sebastian Ramacher

Split cb_print_draw_page

Signed-off-by: Sebastian Ramacher's avatarSebastian Ramacher <sebastian+dev@ramacher.at>
parent 6f6e8ea9
......@@ -84,63 +84,45 @@ cb_print_end(GtkPrintOperation* UNUSED(print_operation), GtkPrintContext*
g_free(file_path);
}
static void
cb_print_draw_page(GtkPrintOperation* print_operation, GtkPrintContext*
context, gint page_number, zathura_t* zathura)
static bool
draw_page_cairo(cairo_t* cairo, zathura_t* zathura, zathura_page_t* page)
{
if (context == NULL || zathura == NULL || zathura->document == NULL ||
zathura->ui.session == NULL || zathura->ui.statusbar.file == NULL) {
gtk_print_operation_cancel(print_operation);
return;
}
/* Update statusbar. */
char* tmp = g_strdup_printf("Printing %d...", page_number);
girara_statusbar_item_set_text(zathura->ui.session,
zathura->ui.statusbar.file, tmp);
g_free(tmp);
/* 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);
if (cairo == NULL || page == NULL) {
gtk_print_operation_cancel(print_operation);
return;
}
/* Try to render the page without a temporary surface. This only works with
* plugins that support rendering to any surface. */
girara_debug("printing page %d ...", page_number);
zathura_renderer_lock(zathura->sync.render_thread);
int err = zathura_page_render(page, cairo, true);
const int err = zathura_page_render(page, cairo, true);
zathura_renderer_unlock(zathura->sync.render_thread);
if (err == ZATHURA_ERROR_OK) {
return;
}
return err == ZATHURA_ERROR_OK;
}
static bool
draw_page_image(cairo_t* cairo, GtkPrintContext* context, zathura_t* zathura,
zathura_page_t* page)
{
/* 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);
const double width = gtk_print_context_get_width(context);
const double height = gtk_print_context_get_height(context);
const double scale_height = 5;
const double scale_width = 5;
/* 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;
const double page_height = zathura_page_get_height(page) * scale_height;
const double page_width = zathura_page_get_width(page) * scale_width;
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;
return false;
}
if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
gtk_print_operation_cancel(print_operation);
cairo_surface_destroy(surface);
return;
return false;
}
cairo_t* temp_cairo = cairo_create(surface);
if (cairo == NULL) {
gtk_print_operation_cancel(print_operation);
cairo_surface_destroy(surface);
return;
return false;
}
/* Draw a white background. */
......@@ -151,15 +133,13 @@ cb_print_draw_page(GtkPrintOperation* print_operation, GtkPrintContext*
cairo_restore(temp_cairo);
/* Render the page to the temporary surface */
girara_debug("printing page %d (fallback) ...", page_number);
zathura_renderer_lock(zathura->sync.render_thread);
err = zathura_page_render(page, temp_cairo, true);
const int err = zathura_page_render(page, temp_cairo, true);
zathura_renderer_unlock(zathura->sync.render_thread);
if (err != ZATHURA_ERROR_OK) {
cairo_destroy(temp_cairo);
cairo_surface_destroy(surface);
gtk_print_operation_cancel(print_operation);
return;
return false;
}
/* Rescale the page and keep the aspect ratio */
......@@ -171,6 +151,43 @@ cb_print_draw_page(GtkPrintOperation* print_operation, GtkPrintContext*
cairo_paint(cairo);
cairo_destroy(temp_cairo);
cairo_surface_destroy(surface);
return true;
}
static void
cb_print_draw_page(GtkPrintOperation* print_operation, GtkPrintContext*
context, gint page_number, zathura_t* zathura)
{
if (context == NULL || zathura == NULL || zathura->document == NULL ||
zathura->ui.session == NULL || zathura->ui.statusbar.file == NULL) {
gtk_print_operation_cancel(print_operation);
return;
}
/* Update statusbar. */
char* tmp = g_strdup_printf("Printing %d...", page_number);
girara_statusbar_item_set_text(zathura->ui.session,
zathura->ui.statusbar.file, tmp);
g_free(tmp);
/* 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);
if (cairo == NULL || page == NULL) {
gtk_print_operation_cancel(print_operation);
return;
}
girara_debug("printing page %d ...", page_number);
if (draw_page_cairo(cairo, zathura, page) == true) {
return;
}
girara_debug("printing page %d (fallback) ...", page_number);
if (draw_page_image(cairo, context, zathura, page) == false) {
gtk_print_operation_cancel(print_operation);
}
}
static void
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment