print.c 7.96 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>
Sebastian Ramacher's avatar
Sebastian Ramacher committed
11
#include <glib/gi18n.h>
12

13
static void cb_print_draw_page(GtkPrintOperation* print_operation,
14
15
                               GtkPrintContext* context, gint page_number,
                               zathura_t* zathura);
16
static void cb_print_end(GtkPrintOperation* print_operation, GtkPrintContext*
Moritz Lipp's avatar
Moritz Lipp committed
17
                         context, zathura_t* zathura);
18
static void cb_print_request_page_setup(GtkPrintOperation* print_operation,
19
20
21
22
23
                                        GtkPrintContext* context,
                                        gint page_number, GtkPageSetup* setup,
                                        zathura_t* zathura);
static void cb_print_done(GtkPrintOperation* operation,
                          GtkPrintOperationResult result, zathura_t* zathura);
24

25
26
27
28
29
30
31
32
33
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 */
34
35
36
37
38
39
  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);

40
  if (zathura->print.settings != NULL) {
Sebastian Ramacher's avatar
CS    
Sebastian Ramacher committed
41
42
    gtk_print_operation_set_print_settings(print_operation,
                                           zathura->print.settings);
43
44
45
  }

  if (zathura->print.page_setup != NULL) {
Sebastian Ramacher's avatar
CS    
Sebastian Ramacher committed
46
47
    gtk_print_operation_set_default_page_setup(print_operation,
                                               zathura->print.page_setup);
48
  }
49
  gtk_print_operation_set_embed_page_setup(print_operation, TRUE);
50
51

  /* print operation signals */
52
53
  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);
54
  g_signal_connect(print_operation, "request-page-setup", G_CALLBACK(cb_print_request_page_setup), zathura);
55
  g_signal_connect(print_operation, "done",               G_CALLBACK(cb_print_done),               zathura);
56
57

  /* print */
Sebastian Ramacher's avatar
Sebastian Ramacher committed
58
  GError* error = NULL;
59
  GtkPrintOperationResult result = gtk_print_operation_run(print_operation,
Sebastian Ramacher's avatar
Sebastian Ramacher committed
60
61
                                   GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
                                   NULL, &error);
62

63
  if (result == GTK_PRINT_OPERATION_RESULT_ERROR) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
64
65
66
    girara_notify(zathura->ui.session, GIRARA_ERROR, _("Printing failed: %s"),
                  error->message);
    g_error_free(error);
67
68
69
70
71
  }

  g_object_unref(print_operation);
}

72
static void
73
cb_print_end(GtkPrintOperation* UNUSED(print_operation), GtkPrintContext*
Moritz Lipp's avatar
Moritz Lipp committed
74
             UNUSED(context), zathura_t* zathura)
75
{
Sebastian Ramacher's avatar
CS    
Sebastian Ramacher committed
76
77
  if (zathura == NULL || zathura->ui.session == NULL ||
      zathura->document == NULL) {
78
79
    return;
  }
80

Lukas K.'s avatar
Lukas K. committed
81
82
83
84
  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);
85
86
}

87
static void
88
cb_print_draw_page(GtkPrintOperation* print_operation, GtkPrintContext*
Moritz Lipp's avatar
Moritz Lipp committed
89
                   context, gint page_number, zathura_t* zathura)
90
{
91
  if (context == NULL || zathura == NULL || zathura->document == NULL ||
92
      zathura->ui.session == NULL || zathura->ui.statusbar.file == NULL) {
93
    gtk_print_operation_cancel(print_operation);
94
95
    return;
  }
96

Sebastian Ramacher's avatar
Sebastian Ramacher committed
97
  /* Update statusbar. */
98
99
  char* tmp = g_strdup_printf("Printing %d...", page_number);
  girara_statusbar_item_set_text(zathura->ui.session,
Moritz Lipp's avatar
Moritz Lipp committed
100
                                 zathura->ui.statusbar.file, tmp);
101
102
  g_free(tmp);

Sebastian Ramacher's avatar
Sebastian Ramacher committed
103
  /* Get the page and cairo handle.  */
104
105
  cairo_t* cairo       = gtk_print_context_get_cairo_context(context);
  zathura_page_t* page = zathura_document_get_page(zathura->document, page_number);
106
  if (cairo == NULL || page == NULL) {
107
    gtk_print_operation_cancel(print_operation);
108
109
    return;
  }
110

Sebastian Ramacher's avatar
Sebastian Ramacher committed
111
112
  /* Try to render the page without a temporary surface. This only works with
   * plugins that support rendering to any surface.  */
113
  girara_debug("printing page %d ...", page_number);
114
  zathura_renderer_lock(zathura->sync.render_thread);
115
  int err = zathura_page_render(page, cairo, true);
116
  zathura_renderer_unlock(zathura->sync.render_thread);
117
118
119
120
  if (err == ZATHURA_ERROR_OK) {
    return;
  }

Sebastian Ramacher's avatar
Sebastian Ramacher committed
121
  /* Try to render the page on a temporary image surface. */
Sebastian Ramacher's avatar
Sebastian Ramacher committed
122
123
124
  const gdouble width = gtk_print_context_get_width(context);
  const gdouble height = gtk_print_context_get_height(context);

125
126
127
  /* 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;
128
  cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, page_width, page_height);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
129
  if (surface == NULL) {
130
    gtk_print_operation_cancel(print_operation);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
131
132
133
134
135
    return;
  }

  cairo_t* temp_cairo = cairo_create(surface);
  if (cairo == NULL) {
136
    gtk_print_operation_cancel(print_operation);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
137
138
139
140
    cairo_surface_destroy(surface);
    return;
  }

Sebastian Ramacher's avatar
Sebastian Ramacher committed
141
  /* Draw a white background. */
142
143
  cairo_save(temp_cairo);
  cairo_set_source_rgb(temp_cairo, 1, 1, 1);
144
  cairo_rectangle(temp_cairo, 0, 0, page_width, page_height);
145
146
  cairo_fill(temp_cairo);
  cairo_restore(temp_cairo);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
147

Sebastian Ramacher's avatar
Sebastian Ramacher committed
148
  /* Render the page to the temporary surface */
149
  girara_debug("printing page %d (fallback) ...", page_number);
150
  zathura_renderer_lock(zathura->sync.render_thread);
151
  err = zathura_page_render(page, temp_cairo, true);
152
  zathura_renderer_unlock(zathura->sync.render_thread);
153
154
155
156
157
158
  if (err != ZATHURA_ERROR_OK) {
    cairo_destroy(temp_cairo);
    cairo_surface_destroy(surface);
    gtk_print_operation_cancel(print_operation);
    return;
  }
Sebastian Ramacher's avatar
Sebastian Ramacher committed
159

Sebastian Ramacher's avatar
Sebastian Ramacher committed
160
  /* Rescale the page and keep the aspect ratio */
161
162
163
  const gdouble scale = MIN(width / page_width, height / page_height);
  cairo_scale(cairo, scale, scale);

Sebastian Ramacher's avatar
Sebastian Ramacher committed
164
  /* Blit temporary surface to original cairo object. */
Sebastian Ramacher's avatar
Sebastian Ramacher committed
165
166
167
168
  cairo_set_source_surface(cairo, surface, 0.0, 0.0);
  cairo_paint(cairo);
  cairo_destroy(temp_cairo);
  cairo_surface_destroy(surface);
169
}
170
171
172

static void
cb_print_request_page_setup(GtkPrintOperation* UNUSED(print_operation),
173
174
                            GtkPrintContext* UNUSED(context), gint page_number,
                            GtkPageSetup* setup, zathura_t* zathura)
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
{
  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);
  }
}
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208

static void
cb_print_done(GtkPrintOperation* operation, GtkPrintOperationResult result,
              zathura_t* zathura)
{
  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);
    }

    /* save previous settings */
    zathura->print.settings   = g_object_ref(gtk_print_operation_get_print_settings(operation));
    zathura->print.page_setup = g_object_ref(gtk_print_operation_get_default_page_setup(operation));
  } else if (result == GTK_PRINT_OPERATION_RESULT_ERROR) {
    GError* error = NULL;
    gtk_print_operation_get_error(operation, &error);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
209
    girara_notify(zathura->ui.session, GIRARA_ERROR, _("Printing failed: %s"),
210
211
212
213
214
                  error->message);
    g_error_free(error);
  }
}