print.c 6.82 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
10
#include <girara/utils.h>
#include <girara/statusbar.h>

11
static void cb_print_draw_page(GtkPrintOperation* print_operation,
Moritz Lipp's avatar
Moritz Lipp committed
12
                               GtkPrintContext* context, gint page_number, zathura_t* zathura);
13
static void cb_print_end(GtkPrintOperation* print_operation, GtkPrintContext*
Moritz Lipp's avatar
Moritz Lipp committed
14
                         context, zathura_t* zathura);
15
static void cb_print_request_page_setup(GtkPrintOperation* print_operation,
Moritz Lipp's avatar
Moritz Lipp committed
16
17
                                        GtkPrintContext* context, gint page_number, GtkPageSetup* setup, zathura_t*
                                        zathura);
18

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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);
  }
35

36
  gtk_print_operation_set_allow_async(print_operation, TRUE);
37
38
  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));
Sebastian Ramacher's avatar
Sebastian Ramacher committed
39
  gtk_print_operation_set_use_full_page(print_operation, TRUE);
40
  gtk_print_operation_set_embed_page_setup(print_operation, TRUE);
41
42

  /* print operation signals */
43
44
  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);
45
  g_signal_connect(print_operation, "request-page-setup", G_CALLBACK(cb_print_request_page_setup), zathura);
46
47
48

  /* print */
  GtkPrintOperationResult result = gtk_print_operation_run(print_operation,
Moritz Lipp's avatar
Moritz Lipp committed
49
                                   GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, NULL, NULL);
50
51

  if (result == GTK_PRINT_OPERATION_RESULT_APPLY) {
52
53
54
55
56
57
58
    if (zathura->print.settings != NULL) {
      g_object_unref(zathura->print.settings);
    }
    if (zathura->print.page_setup != NULL) {
      g_object_unref(zathura->print.page_setup);
    }

59
    /* save previous settings */
60
61
    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));
62
63
64
65
66
67
68
  } else if (result == GTK_PRINT_OPERATION_RESULT_ERROR) {
    girara_error("Error occured while printing progress");
  }

  g_object_unref(print_operation);
}

69
static void
70
cb_print_end(GtkPrintOperation* UNUSED(print_operation), GtkPrintContext*
Moritz Lipp's avatar
Moritz Lipp committed
71
             UNUSED(context), zathura_t* zathura)
72
{
73
  if (zathura == NULL || zathura->ui.session == NULL || zathura->document == NULL) {
74
75
    return;
  }
76

77
78
79
80
  const char* file_path = zathura_document_get_path(zathura->document);

  if (file_path != NULL) {
    girara_statusbar_item_set_text(zathura->ui.session,
Moritz Lipp's avatar
Moritz Lipp committed
81
                                   zathura->ui.statusbar.file, file_path);
82
  }
83
84
}

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

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

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

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

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

123
124
125
  const double page_height = zathura_page_get_height(page);
  const double page_width  = zathura_page_get_width(page);
  cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, page_width, page_height);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
126
  if (surface == NULL) {
127
    gtk_print_operation_cancel(print_operation);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
128
129
130
131
132
    return;
  }

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

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

Sebastian Ramacher's avatar
Sebastian Ramacher committed
145
  /* Render the page to the temporary surface */
Sebastian Ramacher's avatar
Sebastian Ramacher committed
146
  girara_debug("printing page %d ...", page_number);
147
  zathura_renderer_lock(zathura->sync.render_thread);
148
  err = zathura_page_render(page, temp_cairo, true);
149
  zathura_renderer_unlock(zathura->sync.render_thread);
150
151
152
153
154
155
  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
156

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

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

static void
cb_print_request_page_setup(GtkPrintOperation* UNUSED(print_operation),
Moritz Lipp's avatar
Moritz Lipp committed
170
171
                            GtkPrintContext* UNUSED(context), gint page_number, GtkPageSetup* setup,
                            zathura_t* zathura)
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
{
  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);
  }
}