print.c 7.18 KB
Newer Older
1 2
/* See LICENSE file for license and copyright information */

3 4
#include "print.h"
#include "document.h"
5
#include "render.h"
Moritz Lipp's avatar
Moritz Lipp committed
6
#include "page.h"
7

8 9
#include <girara/utils.h>
#include <girara/statusbar.h>
10
#include <girara/session.h>
11
#include <glib/gi18n.h>
12

13
static void
14
cb_print_end(GtkPrintOperation* UNUSED(print_operation), GtkPrintContext*
Moritz Lipp's avatar
Moritz Lipp committed
15
             UNUSED(context), zathura_t* zathura)
16
{
Sebastian Ramacher's avatar
CS  
Sebastian Ramacher committed
17 18
  if (zathura == NULL || zathura->ui.session == NULL ||
      zathura->document == NULL) {
19 20
    return;
  }
21

Lukas K.'s avatar
Lukas K. committed
22 23 24 25
  char* file_path = get_formatted_filename(zathura, true);
  girara_statusbar_item_set_text(zathura->ui.session,
                                 zathura->ui.statusbar.file, file_path);
  g_free(file_path);
26 27
}

28 29
static bool
draw_page_cairo(cairo_t* cairo, zathura_t* zathura, zathura_page_t* page)
30
{
Sebastian Ramacher's avatar
Sebastian Ramacher committed
31 32
  /* Try to render the page without a temporary surface. This only works with
   * plugins that support rendering to any surface.  */
33
  zathura_renderer_lock(zathura->sync.render_thread);
34
  const int err = zathura_page_render(page, cairo, true);
35
  zathura_renderer_unlock(zathura->sync.render_thread);
36

37 38 39 40 41 42 43
  return err == ZATHURA_ERROR_OK;
}

static bool
draw_page_image(cairo_t* cairo, GtkPrintContext* context, zathura_t* zathura,
                zathura_page_t* page)
{
Sebastian Ramacher's avatar
Sebastian Ramacher committed
44
  /* Try to render the page on a temporary image surface. */
45 46 47 48 49
  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;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
50

51
  /* Render to a surface that is 5 times larger to workaround quality issues. */
52 53
  const double page_height = zathura_page_get_height(page) * scale_height;
  const double page_width  = zathura_page_get_width(page) * scale_width;
54
  cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, page_width, page_height);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
55
  if (surface == NULL) {
56
    return false;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
57
  }
58 59
  if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
    cairo_surface_destroy(surface);
60
    return false;
61
  }
Sebastian Ramacher's avatar
Sebastian Ramacher committed
62 63 64 65

  cairo_t* temp_cairo = cairo_create(surface);
  if (cairo == NULL) {
    cairo_surface_destroy(surface);
66
    return false;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
67 68
  }

Sebastian Ramacher's avatar
Sebastian Ramacher committed
69
  /* Draw a white background. */
70 71
  cairo_save(temp_cairo);
  cairo_set_source_rgb(temp_cairo, 1, 1, 1);
72
  cairo_rectangle(temp_cairo, 0, 0, page_width, page_height);
73 74
  cairo_fill(temp_cairo);
  cairo_restore(temp_cairo);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
75

Sebastian Ramacher's avatar
Sebastian Ramacher committed
76
  /* Render the page to the temporary surface */
77
  zathura_renderer_lock(zathura->sync.render_thread);
78
  const int err = zathura_page_render(page, temp_cairo, true);
79
  zathura_renderer_unlock(zathura->sync.render_thread);
80 81 82
  if (err != ZATHURA_ERROR_OK) {
    cairo_destroy(temp_cairo);
    cairo_surface_destroy(surface);
83
    return false;
84
  }
Sebastian Ramacher's avatar
Sebastian Ramacher committed
85

Sebastian Ramacher's avatar
Sebastian Ramacher committed
86
  /* Rescale the page and keep the aspect ratio */
87 88 89
  const gdouble scale = MIN(width / page_width, height / page_height);
  cairo_scale(cairo, scale, scale);

Sebastian Ramacher's avatar
Sebastian Ramacher committed
90
  /* Blit temporary surface to original cairo object. */
Sebastian Ramacher's avatar
Sebastian Ramacher committed
91 92 93 94
  cairo_set_source_surface(cairo, surface, 0.0, 0.0);
  cairo_paint(cairo);
  cairo_destroy(temp_cairo);
  cairo_surface_destroy(surface);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

  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. */
Sebastian Ramacher's avatar
Sebastian Ramacher committed
110
  char* tmp = g_strdup_printf(_("Printing page %d ..."), page_number);
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
  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);
  }
132
}
133 134 135

static void
cb_print_request_page_setup(GtkPrintOperation* UNUSED(print_operation),
136 137
                            GtkPrintContext* UNUSED(context), gint page_number,
                            GtkPageSetup* setup, zathura_t* zathura)
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
{
  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);
  }
}
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

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 */
  gtk_print_operation_set_job_name(print_operation, zathura_document_get_path(zathura->document));
  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));
  gtk_print_operation_set_use_full_page(print_operation, TRUE);

  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);
  }
  gtk_print_operation_set_embed_page_setup(print_operation, TRUE);

  /* 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);
  g_signal_connect(print_operation, "request-page-setup", G_CALLBACK(cb_print_request_page_setup), zathura);

  /* print */
  GError* error = NULL;
  GtkPrintOperationResult result = gtk_print_operation_run(print_operation,
                                   GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
                                   GTK_WINDOW(zathura->ui.session->gtk.window), &error);

  if (result == GTK_PRINT_OPERATION_RESULT_ERROR) {
    girara_notify(zathura->ui.session, GIRARA_ERROR, _("Printing failed: %s"),
                  error->message);
    g_error_free(error);
  } else if (result == GTK_PRINT_OPERATION_RESULT_APPLY) {
    g_clear_object(&zathura->print.settings);
    g_clear_object(&zathura->print.page_setup);

    /* 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));
  }

  g_object_unref(print_operation);
}