]> www.fi.muni.cz Git - evince.git/blob - backend/pdf/ev-poppler.cc
Fix printing in documents with rotated pages. Patch by Eugen Dedu. Fixes
[evince.git] / backend / pdf / ev-poppler.cc
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* pdfdocument.h: Implementation of EvDocument for PDF
3  * Copyright (C) 2004, Red Hat, Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21
22 #include <math.h>
23 #include <string.h>
24 #include <gtk/gtk.h>
25 #include <poppler.h>
26 #include <poppler-document.h>
27 #include <poppler-page.h>
28 #ifdef HAVE_CAIRO_PDF
29 #include <cairo-pdf.h>
30 #endif
31 #ifdef HAVE_CAIRO_PS
32 #include <cairo-ps.h>
33 #endif
34 #include <glib/gi18n.h>
35
36 #include "ev-poppler.h"
37 #include "ev-file-exporter.h"
38 #include "ev-document-find.h"
39 #include "ev-document-misc.h"
40 #include "ev-document-links.h"
41 #include "ev-document-images.h"
42 #include "ev-document-fonts.h"
43 #include "ev-document-security.h"
44 #include "ev-document-thumbnails.h"
45 #include "ev-document-transition.h"
46 #include "ev-document-forms.h"
47 #include "ev-selection.h"
48 #include "ev-transition-effect.h"
49 #include "ev-attachment.h"
50 #include "ev-image.h"
51
52 #if (defined (HAVE_POPPLER_PAGE_RENDER) || defined (HAVE_POPPLER_PAGE_RENDER_FOR_PRINTING)) && (defined (HAVE_CAIRO_PDF) || defined (HAVE_CAIRO_PS))
53 #define HAVE_CAIRO_PRINT
54 #endif
55
56 #if POPPLER_MAJOR_VERSION <= 6 || (POPPLER_MAJOR_VERSION == 7 && POPPLER_MINOR_VERSION < 2)
57 #define POPPLER_HAS_GDK
58 #else
59 #ifdef POPPLER_WITH_GDK
60 #define POPPLER_HAS_GDK
61 #endif
62 #endif
63
64 typedef struct {
65         PdfDocument *document;
66         char *text;
67         GList **pages;
68         guint idle;
69         int start_page;
70         int search_page;
71 } PdfDocumentSearch;
72
73 typedef struct {
74         EvFileExporterFormat format;
75
76         /* Pages per sheet */
77         gint pages_per_sheet;
78         gint pages_printed;
79         gint pages_x;
80         gint pages_y;
81         gdouble paper_width;
82         gdouble paper_height;
83         
84 #ifdef HAVE_CAIRO_PRINT
85         cairo_t *cr;
86 #else
87         PopplerPSFile *ps_file;
88 #endif
89 } PdfPrintContext;
90
91 struct _PdfDocumentClass
92 {
93         GObjectClass parent_class;
94 };
95
96 struct _PdfDocument
97 {
98         GObject parent_instance;
99
100         PopplerDocument *document;
101         gchar *password;
102
103         PopplerFontInfo *font_info;
104         PopplerFontsIter *fonts_iter;
105         int fonts_scanned_pages;
106
107         PdfDocumentSearch *search;
108         PdfPrintContext *print_ctx;
109 };
110
111 static void pdf_document_document_iface_init            (EvDocumentIface           *iface);
112 static void pdf_document_security_iface_init            (EvDocumentSecurityIface   *iface);
113 static void pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
114 static void pdf_document_document_links_iface_init      (EvDocumentLinksIface      *iface);
115 static void pdf_document_document_images_iface_init     (EvDocumentImagesIface     *iface);
116 static void pdf_document_document_forms_iface_init      (EvDocumentFormsIface      *iface);
117 static void pdf_document_document_fonts_iface_init      (EvDocumentFontsIface      *iface);
118 static void pdf_document_find_iface_init                (EvDocumentFindIface       *iface);
119 static void pdf_document_file_exporter_iface_init       (EvFileExporterIface       *iface);
120 static void pdf_selection_iface_init                    (EvSelectionIface          *iface);
121 static void pdf_document_page_transition_iface_init     (EvDocumentTransitionIface *iface);
122 static void pdf_document_thumbnails_get_dimensions      (EvDocumentThumbnails      *document_thumbnails,
123                                                          EvRenderContext           *rc,
124                                                          gint                      *width,
125                                                          gint                      *height);
126 static int  pdf_document_get_n_pages                    (EvDocument                *document);
127
128 static EvLinkDest *ev_link_dest_from_dest   (PdfDocument       *pdf_document,
129                                              PopplerDest       *dest);
130 static EvLink     *ev_link_from_action      (PdfDocument       *pdf_document,
131                                              PopplerAction     *action);
132 static void        pdf_document_search_free (PdfDocumentSearch *search);
133 static void        pdf_print_context_free   (PdfPrintContext   *ctx);
134
135 EV_BACKEND_REGISTER_WITH_CODE (PdfDocument, pdf_document,
136                          {
137                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
138                                                                  pdf_document_security_iface_init);
139                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
140                                                                  pdf_document_document_thumbnails_iface_init);
141                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
142                                                                  pdf_document_document_links_iface_init);
143                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
144                                                                  pdf_document_document_images_iface_init);
145                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
146                                                                  pdf_document_document_forms_iface_init);
147                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
148                                                                  pdf_document_document_fonts_iface_init);
149                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
150                                                                  pdf_document_find_iface_init);
151                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
152                                                                  pdf_document_file_exporter_iface_init);
153                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
154                                                                  pdf_selection_iface_init);
155                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
156                                                                  pdf_document_page_transition_iface_init);
157                          });
158
159 static void
160 set_rc_data (PdfDocument     *pdf_document,
161              EvRenderContext *rc)
162 {
163         if (rc->data == NULL) {
164                 rc->data = poppler_document_get_page (pdf_document->document,
165                                                       rc->page);
166                 rc->destroy = g_object_unref;
167         } else {
168                 g_assert (rc->page == poppler_page_get_index (POPPLER_PAGE (rc->data)));
169         }
170 }
171
172 static void
173 pdf_document_search_free (PdfDocumentSearch   *search)
174 {
175         PdfDocument *pdf_document = search->document;
176         int n_pages;
177         int i;
178
179         if (search->idle != 0)
180                 g_source_remove (search->idle);
181
182         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
183         for (i = 0; i < n_pages; i++) {
184                 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
185                 g_list_free (search->pages[i]);
186         }
187         g_free (search->pages);
188         
189         g_free (search->text);
190         g_free (search);
191 }
192
193 static void
194 pdf_document_dispose (GObject *object)
195 {
196         PdfDocument *pdf_document = PDF_DOCUMENT(object);
197
198         if (pdf_document->print_ctx) {
199                 pdf_print_context_free (pdf_document->print_ctx);
200                 pdf_document->print_ctx = NULL;
201         }
202         
203         if (pdf_document->search) {
204                 pdf_document_search_free (pdf_document->search);
205                 pdf_document->search = NULL;
206         }
207
208         if (pdf_document->document) {
209                 g_object_unref (pdf_document->document);
210         }
211
212         if (pdf_document->font_info) { 
213                 poppler_font_info_free (pdf_document->font_info);
214         }
215
216         if (pdf_document->fonts_iter) {
217                 poppler_fonts_iter_free (pdf_document->fonts_iter);
218         }
219
220         G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
221 }
222
223 static void
224 pdf_document_class_init (PdfDocumentClass *klass)
225 {
226         GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
227
228         g_object_class->dispose = pdf_document_dispose;
229 }
230
231 static void
232 pdf_document_init (PdfDocument *pdf_document)
233 {
234         pdf_document->password = NULL;
235 }
236
237 static void
238 convert_error (GError  *poppler_error,
239                GError **error)
240 {
241         if (poppler_error == NULL)
242                 return;
243
244         if (poppler_error->domain == POPPLER_ERROR) {
245                 /* convert poppler errors into EvDocument errors */
246                 gint code = EV_DOCUMENT_ERROR_INVALID;
247                 if (poppler_error->code == POPPLER_ERROR_INVALID)
248                         code = EV_DOCUMENT_ERROR_INVALID;
249                 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
250                         code = EV_DOCUMENT_ERROR_ENCRYPTED;
251                         
252
253                 g_set_error (error,
254                              EV_DOCUMENT_ERROR,
255                              code,
256                              poppler_error->message,
257                              NULL);
258         } else {
259                 g_propagate_error (error, poppler_error);
260         }
261 }
262
263
264 /* EvDocument */
265 static gboolean
266 pdf_document_save (EvDocument  *document,
267                    const char  *uri,
268                    GError     **error)
269 {
270         gboolean retval;
271         GError *poppler_error = NULL;
272
273         retval = poppler_document_save (PDF_DOCUMENT (document)->document,
274                                         uri,
275                                         &poppler_error);
276         if (! retval)
277                 convert_error (poppler_error, error);
278
279         return retval;
280 }
281
282 static gboolean
283 pdf_document_load (EvDocument   *document,
284                    const char   *uri,
285                    GError      **error)
286 {
287         GError *poppler_error = NULL;
288         PdfDocument *pdf_document = PDF_DOCUMENT (document);
289
290         pdf_document->document =
291                 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
292
293         if (pdf_document->document == NULL) {
294                 convert_error (poppler_error, error);
295                 return FALSE;
296         }
297
298         return TRUE;
299 }
300
301 static int
302 pdf_document_get_n_pages (EvDocument *document)
303 {
304         return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
305 }
306
307 static void
308 pdf_document_get_page_size (EvDocument   *document,
309                             int           page,
310                             double       *width,
311                             double       *height)
312 {
313         PdfDocument *pdf_document = PDF_DOCUMENT (document);
314         PopplerPage *poppler_page;
315
316         poppler_page = poppler_document_get_page (pdf_document->document, page);
317         poppler_page_get_size (poppler_page, width, height);
318         g_object_unref (poppler_page);
319 }
320
321 static char *
322 pdf_document_get_page_label (EvDocument *document,
323                              int         page)
324 {
325         PopplerPage *poppler_page;
326         char *label = NULL;
327
328         poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
329                                                   page);
330
331         g_object_get (G_OBJECT (poppler_page),
332                       "label", &label,
333                       NULL);
334         g_object_unref (poppler_page);
335
336         return label;
337 }
338
339 static gboolean
340 pdf_document_has_attachments (EvDocument *document)
341 {
342         PdfDocument *pdf_document;
343
344         pdf_document = PDF_DOCUMENT (document);
345
346         return poppler_document_has_attachments (pdf_document->document);
347 }
348
349 struct SaveToBufferData {
350         gchar *buffer;
351         gsize len, max;
352 };
353
354 static gboolean
355 attachment_save_to_buffer_callback (const gchar  *buf,
356                                     gsize         count,
357                                     gpointer      user_data,
358                                     GError      **error)
359 {
360         struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
361         gchar *new_buffer;
362         gsize new_max;
363
364         if (sdata->len + count > sdata->max) {
365                 new_max = MAX (sdata->max * 2, sdata->len + count);
366                 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
367
368                 sdata->buffer = new_buffer;
369                 sdata->max = new_max;
370         }
371         
372         memcpy (sdata->buffer + sdata->len, buf, count);
373         sdata->len += count;
374         
375         return TRUE;
376 }
377
378 static gboolean
379 attachment_save_to_buffer (PopplerAttachment  *attachment,
380                            gchar             **buffer,
381                            gsize              *buffer_size,
382                            GError            **error)
383 {
384         static const gint initial_max = 1024;
385         struct SaveToBufferData sdata;
386
387         *buffer = NULL;
388         *buffer_size = 0;
389
390         sdata.buffer = (gchar *) g_malloc (initial_max);
391         sdata.max = initial_max;
392         sdata.len = 0;
393
394         if (! poppler_attachment_save_to_callback (attachment,
395                                                    attachment_save_to_buffer_callback,
396                                                    &sdata,
397                                                    error)) {
398                 g_free (sdata.buffer);
399                 return FALSE;
400         }
401
402         *buffer = sdata.buffer;
403         *buffer_size = sdata.len;
404         
405         return TRUE;
406 }
407
408 static GList *
409 pdf_document_get_attachments (EvDocument *document)
410 {
411         PdfDocument *pdf_document;
412         GList *attachments;
413         GList *list;
414         GList *retval = NULL;
415
416         pdf_document = PDF_DOCUMENT (document);
417
418         if (!pdf_document_has_attachments (document))
419                 return NULL;
420
421         attachments = poppler_document_get_attachments (pdf_document->document);
422         
423         for (list = attachments; list; list = list->next) {
424                 PopplerAttachment *attachment;
425                 EvAttachment *ev_attachment;
426                 gchar *data = NULL;
427                 gsize size;
428                 GError *error = NULL;
429
430                 attachment = (PopplerAttachment *) list->data;
431
432                 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
433                         ev_attachment = ev_attachment_new (attachment->name,
434                                                            attachment->description,
435                                                            attachment->mtime,
436                                                            attachment->ctime,
437                                                            size, data);
438                         
439                         retval = g_list_prepend (retval, ev_attachment);
440                 } else {
441                         if (error) {
442                                 g_warning ("%s", error->message);
443                                 g_error_free (error);
444
445                                 g_free (data);
446                         }
447                 }
448
449                 g_object_unref (attachment);
450         }
451
452         return g_list_reverse (retval);
453 }
454
455 static cairo_surface_t *
456 pdf_page_render (PopplerPage     *page,
457                  gint             width,
458                  gint             height,
459                  EvRenderContext *rc)
460 {
461         cairo_surface_t *surface;
462
463 #ifdef HAVE_POPPLER_PAGE_RENDER
464         cairo_t *cr;
465         
466         surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
467                                               width, height);
468         memset (cairo_image_surface_get_data (surface), 0xff,
469                 cairo_image_surface_get_height (surface) *
470                 cairo_image_surface_get_stride (surface));
471         
472         cr = cairo_create (surface);
473         switch (rc->rotation) {
474                 case 90:
475                         cairo_translate (cr, width, 0);
476                         break;
477                 case 180:
478                         cairo_translate (cr, width, height);
479                         break;
480                 case 270:
481                         cairo_translate (cr, 0, height);
482                         break;
483                 default:
484                         cairo_translate (cr, 0, 0);
485         }
486         cairo_scale (cr, rc->scale, rc->scale);
487         cairo_rotate (cr, rc->rotation * G_PI / 180.0);
488         poppler_page_render (page, cr);
489         cairo_destroy (cr);
490 #else /* HAVE_POPPLER_PAGE_RENDER */
491         GdkPixbuf *pixbuf;
492         
493         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
494                                  FALSE, 8,
495                                  width, height);
496
497         poppler_page_render_to_pixbuf (page,
498                                        0, 0,
499                                        width, height,
500                                        rc->scale,
501                                        rc->rotation,
502                                        pixbuf);
503         surface = ev_document_misc_surface_from_pixbuf (pixbuf);
504         g_object_unref (pixbuf);
505 #endif /* HAVE_POPPLER_PAGE_RENDER */
506
507         return surface; 
508 }
509
510 static cairo_surface_t *
511 pdf_document_render (EvDocument      *document,
512                      EvRenderContext *rc)
513 {
514         PdfDocument *pdf_document;
515         double width_points, height_points;
516         gint width, height;
517
518         pdf_document = PDF_DOCUMENT (document);
519
520         set_rc_data (pdf_document, rc);
521
522         poppler_page_get_size (POPPLER_PAGE (rc->data),
523                                &width_points, &height_points);
524         
525         if (rc->rotation == 90 || rc->rotation == 270) {
526                 width = (int) ((height_points * rc->scale) + 0.5);
527                 height = (int) ((width_points * rc->scale) + 0.5);
528         } else {
529                 width = (int) ((width_points * rc->scale) + 0.5);
530                 height = (int) ((height_points * rc->scale) + 0.5);
531         }
532         
533         return pdf_page_render (POPPLER_PAGE (rc->data),
534                                 width, height, rc);
535 }
536
537 /* EvDocumentSecurity */
538
539 static gboolean
540 pdf_document_has_document_security (EvDocumentSecurity *document_security)
541 {
542         /* FIXME: do we really need to have this? */
543         return FALSE;
544 }
545
546 static void
547 pdf_document_set_password (EvDocumentSecurity *document_security,
548                            const char         *password)
549 {
550         PdfDocument *document = PDF_DOCUMENT (document_security);
551
552         if (document->password)
553                 g_free (document->password);
554
555         document->password = g_strdup (password);
556 }
557
558 static EvDocumentInfo *
559 pdf_document_get_info (EvDocument *document)
560 {
561         EvDocumentInfo *info;
562         PopplerPageLayout layout;
563         PopplerPageMode mode;
564         PopplerViewerPreferences view_prefs;
565         PopplerPermissions permissions;
566
567         info = g_new0 (EvDocumentInfo, 1);
568
569         info->fields_mask = EV_DOCUMENT_INFO_TITLE |
570                             EV_DOCUMENT_INFO_FORMAT |
571                             EV_DOCUMENT_INFO_AUTHOR |
572                             EV_DOCUMENT_INFO_SUBJECT |
573                             EV_DOCUMENT_INFO_KEYWORDS |
574                             EV_DOCUMENT_INFO_LAYOUT |
575                             EV_DOCUMENT_INFO_START_MODE |
576                             EV_DOCUMENT_INFO_PERMISSIONS |
577                             EV_DOCUMENT_INFO_UI_HINTS |
578                             EV_DOCUMENT_INFO_CREATOR |
579                             EV_DOCUMENT_INFO_PRODUCER |
580                             EV_DOCUMENT_INFO_CREATION_DATE |
581                             EV_DOCUMENT_INFO_MOD_DATE |
582                             EV_DOCUMENT_INFO_LINEARIZED |
583                             EV_DOCUMENT_INFO_N_PAGES |
584                             EV_DOCUMENT_INFO_SECURITY | 
585                             EV_DOCUMENT_INFO_PAPER_SIZE;
586
587         g_object_get (PDF_DOCUMENT (document)->document,
588                       "title", &(info->title),
589                       "format", &(info->format),
590                       "author", &(info->author),
591                       "subject", &(info->subject),
592                       "keywords", &(info->keywords),
593                       "page-mode", &mode,
594                       "page-layout", &layout,
595                       "viewer-preferences", &view_prefs,
596                       "permissions", &permissions,
597                       "creator", &(info->creator),
598                       "producer", &(info->producer),
599                       "creation-date", &(info->creation_date),
600                       "mod-date", &(info->modified_date),
601                       "linearized", &(info->linearized),
602                       NULL);
603
604         pdf_document_get_page_size(document, 0,
605                                    &(info->paper_width),
606                                    &(info->paper_height));
607
608         // Convert to mm.
609         info->paper_width = info->paper_width / 72.0f * 25.4f;
610         info->paper_height = info->paper_height / 72.0f * 25.4f;
611
612         switch (layout) {
613                 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
614                         info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
615                         break;
616                 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
617                         info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
618                         break;
619                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
620                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
621                         break;
622                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
623                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
624                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
625                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
626                         break;
627                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
628                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
629                         break;
630                 default:
631                         break;
632         }
633
634         switch (mode) {
635                 case POPPLER_PAGE_MODE_NONE:
636                         info->mode = EV_DOCUMENT_MODE_NONE;
637                         break;
638                 case POPPLER_PAGE_MODE_USE_THUMBS:
639                         info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
640                         break;
641                 case POPPLER_PAGE_MODE_USE_OC:
642                         info->mode = EV_DOCUMENT_MODE_USE_OC;
643                         break;
644                 case POPPLER_PAGE_MODE_FULL_SCREEN:
645                         info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
646                         break;
647                 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
648                         info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
649                 default:
650                         break;
651         }
652
653         info->ui_hints = 0;
654         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
655                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
656         }
657         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
658                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
659         }
660         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
661                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
662         }
663         if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
664                 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
665         }
666         if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
667                 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
668         }
669         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
670                 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
671         }
672         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
673                 info->ui_hints |=  EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
674         }
675
676         info->permissions = 0;
677         if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
678                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
679         }
680         if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
681                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
682         }
683         if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
684                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
685         }
686         if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
687                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
688         }
689
690         info->n_pages = ev_document_get_n_pages (document);
691
692         if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
693                 /* translators: this is the document security state */
694                 info->security = g_strdup (_("Yes"));
695         } else {
696                 /* translators: this is the document security state */
697                 info->security = g_strdup (_("No"));
698         }
699
700         return info;
701 }
702
703 static void
704 pdf_document_document_iface_init (EvDocumentIface *iface)
705 {
706         iface->save = pdf_document_save;
707         iface->load = pdf_document_load;
708         iface->get_n_pages = pdf_document_get_n_pages;
709         iface->get_page_size = pdf_document_get_page_size;
710         iface->get_page_label = pdf_document_get_page_label;
711         iface->has_attachments = pdf_document_has_attachments;
712         iface->get_attachments = pdf_document_get_attachments;
713         iface->render = pdf_document_render;
714         iface->get_info = pdf_document_get_info;
715 };
716
717 static void
718 pdf_document_security_iface_init (EvDocumentSecurityIface *iface)
719 {
720         iface->has_document_security = pdf_document_has_document_security;
721         iface->set_password = pdf_document_set_password;
722 }
723
724 static gdouble
725 pdf_document_fonts_get_progress (EvDocumentFonts *document_fonts)
726 {
727         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
728         int n_pages;
729
730         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
731
732         return (double)pdf_document->fonts_scanned_pages / (double)n_pages;
733 }
734
735 static gboolean
736 pdf_document_fonts_scan (EvDocumentFonts *document_fonts,
737                          int              n_pages)
738 {
739         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
740         gboolean result;
741
742         g_return_val_if_fail (PDF_IS_DOCUMENT (document_fonts), FALSE);
743
744         if (pdf_document->font_info == NULL) { 
745                 pdf_document->font_info = poppler_font_info_new (pdf_document->document);
746         }
747
748         if (pdf_document->fonts_iter) {
749                 poppler_fonts_iter_free (pdf_document->fonts_iter);
750         }
751
752         pdf_document->fonts_scanned_pages += n_pages;
753
754         result = poppler_font_info_scan (pdf_document->font_info, n_pages,
755                                          &pdf_document->fonts_iter);
756         if (!result) {
757                 pdf_document->fonts_scanned_pages = 0;
758                 poppler_font_info_free (pdf_document->font_info);
759                 pdf_document->font_info = NULL; 
760         }
761
762         return result;
763 }
764
765 static const char *
766 font_type_to_string (PopplerFontType type)
767 {
768         switch (type) {
769                 case POPPLER_FONT_TYPE_TYPE1:
770                         return _("Type 1");
771                 case POPPLER_FONT_TYPE_TYPE1C:
772                         return _("Type 1C");
773                 case POPPLER_FONT_TYPE_TYPE3:
774                         return _("Type 3");
775                 case POPPLER_FONT_TYPE_TRUETYPE:
776                         return _("TrueType");
777                 case POPPLER_FONT_TYPE_CID_TYPE0:
778                         return _("Type 1 (CID)");
779                 case POPPLER_FONT_TYPE_CID_TYPE0C:
780                         return _("Type 1C (CID)");
781                 case POPPLER_FONT_TYPE_CID_TYPE2:
782                         return _("TrueType (CID)");
783                 default:
784                         return _("Unknown font type");
785         }
786 }
787
788 static void
789 pdf_document_fonts_fill_model (EvDocumentFonts *document_fonts,
790                                GtkTreeModel    *model)
791 {
792         PdfDocument *pdf_document = PDF_DOCUMENT (document_fonts);
793         PopplerFontsIter *iter = pdf_document->fonts_iter;
794
795         g_return_if_fail (PDF_IS_DOCUMENT (document_fonts));
796
797         if (!iter)
798                 return;
799
800         do {
801                 GtkTreeIter list_iter;
802                 const char *name;
803                 const char *type;
804                 const char *embedded;
805                 char *details;
806                 
807                 name = poppler_fonts_iter_get_name (iter);
808
809                 if (name == NULL) {
810                         name = _("No name");
811                 }
812
813                 type = font_type_to_string (
814                         poppler_fonts_iter_get_font_type (iter));
815
816                 if (poppler_fonts_iter_is_embedded (iter)) {
817                         if (poppler_fonts_iter_is_subset (iter))
818                                 embedded = _("Embedded subset");
819                         else
820                                 embedded = _("Embedded");
821                 } else {
822                         embedded = _("Not embedded");
823                 }
824
825                 details = g_markup_printf_escaped ("%s\n%s", type, embedded);
826
827                 gtk_list_store_append (GTK_LIST_STORE (model), &list_iter);
828                 gtk_list_store_set (GTK_LIST_STORE (model), &list_iter,
829                                     EV_DOCUMENT_FONTS_COLUMN_NAME, name,
830                                     EV_DOCUMENT_FONTS_COLUMN_DETAILS, details,
831                                     -1);
832
833                 g_free (details);
834         } while (poppler_fonts_iter_next (iter));
835 }
836
837 static void
838 pdf_document_document_fonts_iface_init (EvDocumentFontsIface *iface)
839 {
840         iface->fill_model = pdf_document_fonts_fill_model;
841         iface->scan = pdf_document_fonts_scan;
842         iface->get_progress = pdf_document_fonts_get_progress;
843 }
844
845 static gboolean
846 pdf_document_links_has_document_links (EvDocumentLinks *document_links)
847 {
848         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
849         PopplerIndexIter *iter;
850
851         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), FALSE);
852
853         iter = poppler_index_iter_new (pdf_document->document);
854         if (iter == NULL)
855                 return FALSE;
856         poppler_index_iter_free (iter);
857
858         return TRUE;
859 }
860
861 static EvLinkDest *
862 ev_link_dest_from_dest (PdfDocument *pdf_document,
863                         PopplerDest *dest)
864 {
865         EvLinkDest *ev_dest = NULL;
866         const char *unimplemented_dest = NULL;
867
868         g_assert (dest != NULL);
869
870         switch (dest->type) {
871                 case POPPLER_DEST_XYZ: {
872                         PopplerPage *poppler_page;
873                         double height;
874
875                         poppler_page = poppler_document_get_page (pdf_document->document,
876                                                                   MAX (0, dest->page_num - 1));
877                         poppler_page_get_size (poppler_page, NULL, &height);
878                         ev_dest = ev_link_dest_new_xyz (dest->page_num - 1,
879                                                         dest->left,
880                                                         height - dest->top,
881                                                         dest->zoom,
882                                                         dest->change_left,
883                                                         dest->change_top,
884                                                         dest->change_zoom);
885                         g_object_unref (poppler_page);
886                 }
887                         break;
888                 case POPPLER_DEST_FIT:
889                         ev_dest = ev_link_dest_new_fit (dest->page_num - 1);
890                         break;
891                 case POPPLER_DEST_FITH: {
892                         PopplerPage *poppler_page;
893                         double height;
894
895                         poppler_page = poppler_document_get_page (pdf_document->document,
896                                                                   MAX (0, dest->page_num - 1));
897                         poppler_page_get_size (poppler_page, NULL, &height);
898                         ev_dest = ev_link_dest_new_fith (dest->page_num - 1,
899                                                          height - dest->top,
900                                                          dest->change_top);
901                         g_object_unref (poppler_page);
902                 }
903                         break;
904                 case POPPLER_DEST_FITV:
905                         ev_dest = ev_link_dest_new_fitv (dest->page_num - 1,
906                                                          dest->left,
907                                                          dest->change_left);
908                         break;
909                 case POPPLER_DEST_FITR: {
910                         PopplerPage *poppler_page;
911                         double height;
912
913                         poppler_page = poppler_document_get_page (pdf_document->document,
914                                                                   MAX (0, dest->page_num - 1));
915                         poppler_page_get_size (poppler_page, NULL, &height);
916                         ev_dest = ev_link_dest_new_fitr (dest->page_num - 1,
917                                                          dest->left,
918                                                          height - dest->bottom,
919                                                          dest->right,
920                                                          height - dest->top);
921                         g_object_unref (poppler_page);
922                 }
923                         break;
924                 case POPPLER_DEST_FITB:
925                         unimplemented_dest = "POPPLER_DEST_FITB";
926                         break;
927                 case POPPLER_DEST_FITBH:
928                         unimplemented_dest = "POPPLER_DEST_FITBH";
929                         break;
930                 case POPPLER_DEST_FITBV:
931                         unimplemented_dest = "POPPLER_DEST_FITBV";
932                         break;
933                 case POPPLER_DEST_NAMED:
934                         ev_dest = ev_link_dest_new_named (dest->named_dest);
935                         break;
936                 case POPPLER_DEST_UNKNOWN:
937                         unimplemented_dest = "POPPLER_DEST_UNKNOWN";
938                         break;
939         }
940
941         if (unimplemented_dest) {
942                 g_warning ("Unimplemented destination: %s, please post a "
943                            "bug report in Evince bugzilla "
944                            "(http://bugzilla.gnome.org) with a testcase.",
945                            unimplemented_dest);
946         }
947
948         if (!ev_dest)
949                 ev_dest = ev_link_dest_new_page (dest->page_num - 1);
950         
951         return ev_dest;
952 }
953
954 static EvLink *
955 ev_link_from_action (PdfDocument   *pdf_document,
956                      PopplerAction *action)
957 {
958         EvLink       *link = NULL;
959         EvLinkAction *ev_action = NULL;
960         const char   *unimplemented_action = NULL;
961
962         switch (action->type) {
963                 case POPPLER_ACTION_GOTO_DEST: {
964                         EvLinkDest *dest;
965                         
966                         dest = ev_link_dest_from_dest (pdf_document, action->goto_dest.dest);
967                         ev_action = ev_link_action_new_dest (dest);
968                 }
969                         break;
970                 case POPPLER_ACTION_GOTO_REMOTE: {
971                         EvLinkDest *dest;
972                         
973                         dest = ev_link_dest_from_dest (pdf_document, action->goto_remote.dest);
974                         ev_action = ev_link_action_new_remote (dest, 
975                                                                action->goto_remote.file_name);
976                         
977                 }
978                         break;
979                 case POPPLER_ACTION_LAUNCH:
980                         ev_action = ev_link_action_new_launch (action->launch.file_name,
981                                                                action->launch.params);
982                         break;
983                 case POPPLER_ACTION_URI:
984                         ev_action = ev_link_action_new_external_uri (action->uri.uri);
985                         break;
986                 case POPPLER_ACTION_NAMED:
987                         ev_action = ev_link_action_new_named (action->named.named_dest);
988                         break;
989                 case POPPLER_ACTION_MOVIE:
990                         unimplemented_action = "POPPLER_ACTION_MOVIE";
991                         break;
992                 case POPPLER_ACTION_UNKNOWN:
993                         unimplemented_action = "POPPLER_ACTION_UNKNOWN";
994         }
995         
996         if (unimplemented_action) {
997                 g_warning ("Unimplemented action: %s, please post a bug report "
998                            "in Evince bugzilla (http://bugzilla.gnome.org) "
999                            "with a testcase.", unimplemented_action);
1000         }
1001         
1002         link = ev_link_new (action->any.title, ev_action);
1003         
1004         return link;    
1005 }
1006
1007 static void
1008 build_tree (PdfDocument      *pdf_document,
1009             GtkTreeModel     *model,
1010             GtkTreeIter      *parent,
1011             PopplerIndexIter *iter)
1012 {
1013         
1014         do {
1015                 GtkTreeIter tree_iter;
1016                 PopplerIndexIter *child;
1017                 PopplerAction *action;
1018                 EvLink *link = NULL;
1019                 gboolean expand;
1020                 char *title_markup;
1021                 
1022                 action = poppler_index_iter_get_action (iter);
1023                 expand = poppler_index_iter_is_open (iter);
1024
1025                 if (!action)
1026                         continue;
1027
1028                 switch (action->type) {
1029                         case POPPLER_ACTION_GOTO_DEST: {
1030                                 /* For bookmarks, solve named destinations */
1031                                 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1032                                         PopplerDest *dest;
1033                                         EvLinkDest *ev_dest = NULL;
1034                                         EvLinkAction *ev_action;
1035                                         
1036                                         dest = poppler_document_find_dest (pdf_document->document,
1037                                                                            action->goto_dest.dest->named_dest);
1038                                         if (!dest) {
1039                                                 link = ev_link_from_action (pdf_document, action);
1040                                                 break;
1041                                         }
1042                                         
1043                                         ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1044                                         poppler_dest_free (dest);
1045                                         
1046                                         ev_action = ev_link_action_new_dest (ev_dest);
1047                                         link = ev_link_new (action->any.title, ev_action);
1048                                 } else {
1049                                         link = ev_link_from_action (pdf_document, action);
1050                                 }
1051                         }
1052                                 break;
1053                         default:
1054                                 link = ev_link_from_action (pdf_document, action);
1055                                 break;
1056                 }
1057                 
1058                 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1059                         poppler_action_free (action);
1060                         if (link)
1061                                 g_object_unref (link);
1062                         
1063                         continue;
1064                 }
1065
1066                 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1067                 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1068                 
1069                 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1070                                     EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1071                                     EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1072                                     EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1073                                     -1);
1074                 
1075                 g_free (title_markup);
1076                 g_object_unref (link);
1077                 
1078                 child = poppler_index_iter_get_child (iter);
1079                 if (child)
1080                         build_tree (pdf_document, model, &tree_iter, child);
1081                 poppler_index_iter_free (child);
1082                 poppler_action_free (action);
1083                 
1084         } while (poppler_index_iter_next (iter));
1085 }
1086
1087 static GtkTreeModel *
1088 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1089 {
1090         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1091         GtkTreeModel *model = NULL;
1092         PopplerIndexIter *iter;
1093         
1094         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1095
1096         iter = poppler_index_iter_new (pdf_document->document);
1097         /* Create the model if we have items*/
1098         if (iter != NULL) {
1099                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1100                                                              G_TYPE_STRING,
1101                                                              G_TYPE_OBJECT,
1102                                                              G_TYPE_BOOLEAN,
1103                                                              G_TYPE_STRING);
1104                 build_tree (pdf_document, model, NULL, iter);
1105                 poppler_index_iter_free (iter);
1106         }
1107         
1108         return model;
1109 }
1110
1111 static GList *
1112 pdf_document_links_get_links (EvDocumentLinks *document_links,
1113                               gint             page)
1114 {
1115         PdfDocument *pdf_document;
1116         PopplerPage *poppler_page;
1117         GList *retval = NULL;
1118         GList *mapping_list;
1119         GList *list;
1120         double height;
1121
1122         pdf_document = PDF_DOCUMENT (document_links);
1123         poppler_page = poppler_document_get_page (pdf_document->document,
1124                                                   page);
1125         mapping_list = poppler_page_get_link_mapping (poppler_page);
1126         poppler_page_get_size (poppler_page, NULL, &height);
1127
1128         for (list = mapping_list; list; list = list->next) {
1129                 PopplerLinkMapping *link_mapping;
1130                 EvLinkMapping *ev_link_mapping;
1131
1132                 link_mapping = (PopplerLinkMapping *)list->data;
1133                 ev_link_mapping = g_new (EvLinkMapping, 1);
1134                 ev_link_mapping->link = ev_link_from_action (pdf_document,
1135                                                              link_mapping->action);
1136                 ev_link_mapping->x1 = link_mapping->area.x1;
1137                 ev_link_mapping->x2 = link_mapping->area.x2;
1138                 /* Invert this for X-style coordinates */
1139                 ev_link_mapping->y1 = height - link_mapping->area.y2;
1140                 ev_link_mapping->y2 = height - link_mapping->area.y1;
1141
1142                 retval = g_list_prepend (retval, ev_link_mapping);
1143         }
1144
1145         poppler_page_free_link_mapping (mapping_list);
1146         g_object_unref (poppler_page);
1147
1148         return g_list_reverse (retval);
1149 }
1150
1151 static EvLinkDest *
1152 pdf_document_links_find_link_dest (EvDocumentLinks  *document_links,
1153                                    const gchar      *link_name)
1154 {
1155         PdfDocument *pdf_document;
1156         PopplerDest *dest;
1157         EvLinkDest *ev_dest = NULL;
1158
1159         pdf_document = PDF_DOCUMENT (document_links);
1160         dest = poppler_document_find_dest (pdf_document->document,
1161                                            link_name);
1162         if (dest) {
1163                 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1164                 poppler_dest_free (dest);
1165         }
1166
1167         return ev_dest;
1168 }
1169
1170 static void
1171 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1172 {
1173         iface->has_document_links = pdf_document_links_has_document_links;
1174         iface->get_links_model = pdf_document_links_get_links_model;
1175         iface->get_links = pdf_document_links_get_links;
1176         iface->find_link_dest = pdf_document_links_find_link_dest;
1177 }
1178
1179 static GList *
1180 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1181                                        gint              page)
1182 {
1183         GList *retval = NULL;
1184         PdfDocument *pdf_document;
1185         PopplerPage *poppler_page;
1186         GList *mapping_list;
1187         GList *list;
1188
1189         pdf_document = PDF_DOCUMENT (document_images);
1190         poppler_page = poppler_document_get_page (pdf_document->document, page);
1191         mapping_list = poppler_page_get_image_mapping (poppler_page);
1192
1193         for (list = mapping_list; list; list = list->next) {
1194                 PopplerImageMapping *image_mapping;
1195                 EvImageMapping *ev_image_mapping;
1196
1197                 image_mapping = (PopplerImageMapping *)list->data;
1198
1199                 ev_image_mapping = g_new (EvImageMapping, 1);
1200 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1201                 ev_image_mapping->image = ev_image_new (page, image_mapping->image_id);
1202 #elif POPPLER_HAS_CAIRO
1203                 ev_image_mapping->image = ev_image_new_from_pixbuf (image_mapping->image);
1204 #endif
1205                 ev_image_mapping->x1 = image_mapping->area.x1;
1206                 ev_image_mapping->x2 = image_mapping->area.x2;
1207                 ev_image_mapping->y1 = image_mapping->area.y1;
1208                 ev_image_mapping->y2 = image_mapping->area.y2;
1209
1210                 retval = g_list_prepend (retval, ev_image_mapping);
1211         }
1212
1213         poppler_page_free_image_mapping (mapping_list);
1214         g_object_unref (poppler_page);
1215
1216         return g_list_reverse (retval);
1217 }
1218
1219 GdkPixbuf *
1220 pdf_document_images_get_image (EvDocumentImages *document_images,
1221                                EvImage          *image)
1222 {
1223 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1224         PdfDocument     *pdf_document;
1225         PopplerPage     *poppler_page;
1226         cairo_surface_t *surface;
1227         GdkPixbuf       *retval = NULL;
1228
1229         pdf_document = PDF_DOCUMENT (document_images);
1230         poppler_page = poppler_document_get_page (pdf_document->document,
1231                                                   ev_image_get_page (image));
1232
1233         surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1234         if (surface) {
1235                 retval = ev_document_misc_pixbuf_from_surface (surface);
1236                 cairo_surface_destroy (surface);
1237         }
1238
1239         g_object_unref (poppler_page);
1240
1241         return retval;
1242 #else
1243         return GDK_PIXBUF (g_object_ref (ev_image_get_pixbuf (image)));
1244 #endif /* HAVE_POPPLER_PAGE_GET_IMAGE */
1245 }
1246
1247 static void
1248 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1249 {
1250         iface->get_image_mapping = pdf_document_images_get_image_mapping;
1251         iface->get_image = pdf_document_images_get_image;
1252 }
1253
1254 static GdkPixbuf *
1255 make_thumbnail_for_page (PdfDocument     *pdf_document,
1256                          PopplerPage     *poppler_page, 
1257                          EvRenderContext *rc)
1258 {
1259         GdkPixbuf *pixbuf;
1260         int width, height;
1261
1262         pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1263                                                 rc, &width, &height);
1264 #ifdef POPPLER_HAS_GDK
1265         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1266                                  width, height);
1267         gdk_pixbuf_fill (pixbuf, 0xffffffff);
1268
1269         ev_document_fc_mutex_lock ();
1270         poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1271                                        width, height,
1272                                        rc->scale, rc->rotation, pixbuf);
1273         ev_document_fc_mutex_unlock ();
1274 #else
1275         cairo_surface_t *surface;
1276
1277         ev_document_fc_mutex_lock ();
1278         surface = pdf_page_render (poppler_page, width, height, rc);
1279         ev_document_fc_mutex_unlock ();
1280         
1281         pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1282         cairo_surface_destroy (surface);
1283 #endif /* POPPLER_HAS_GDK */
1284
1285         return pixbuf;
1286 }
1287
1288 static GdkPixbuf *
1289 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1290                                        EvRenderContext      *rc, 
1291                                        gboolean              border)
1292 {
1293         PdfDocument *pdf_document;
1294         PopplerPage *poppler_page;
1295         GdkPixbuf *pixbuf = NULL;
1296         GdkPixbuf *border_pixbuf;
1297
1298         pdf_document = PDF_DOCUMENT (document_thumbnails);
1299
1300         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1301         g_return_val_if_fail (poppler_page != NULL, NULL);
1302
1303 #if POPPLER_MAJOR_VERSION <= 6 || (POPPLER_MAJOR_VERSION == 7 && POPPLER_MINOR_VERSION < 2)
1304         pixbuf = poppler_page_get_thumbnail (poppler_page);
1305 #else
1306 #ifdef POPPLER_HAS_GDK
1307         pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1308 #else
1309         cairo_surface_t *surface;
1310         
1311         surface = poppler_page_get_thumbnail (poppler_page);
1312         if (surface) {
1313                 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1314                 cairo_surface_destroy (surface);
1315         }
1316 #endif
1317 #endif
1318                 
1319         if (pixbuf) {
1320                 /* Rotate provided thumbnail if needed */
1321                 GdkPixbuf *rotated_pixbuf;
1322
1323                 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1324                                                            (GdkPixbufRotation) (360 - rc->rotation));
1325                 g_object_unref (pixbuf);
1326                 pixbuf = rotated_pixbuf;
1327         } else {
1328                 /* There is no provided thumbnail.  We need to make one. */
1329                 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1330         }
1331
1332         if (border) {           
1333                 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1334                 g_object_unref (pixbuf);
1335                 pixbuf = border_pixbuf;
1336         }               
1337
1338         g_object_unref (poppler_page);
1339         
1340         return pixbuf;
1341 }
1342
1343 static void
1344 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1345                                         EvRenderContext      *rc,
1346                                         gint                 *width,
1347                                         gint                 *height)
1348 {
1349         PdfDocument *pdf_document;
1350         PopplerPage *poppler_page;
1351         gint has_thumb;
1352         
1353         pdf_document = PDF_DOCUMENT (document_thumbnails);
1354         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1355
1356         g_return_if_fail (poppler_page != NULL);
1357
1358         has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1359
1360         if (!has_thumb) {
1361                 double page_width, page_height;
1362
1363                 poppler_page_get_size (poppler_page, &page_width, &page_height);
1364
1365                 *width = (gint) MAX (page_width * rc->scale, 1);
1366                 *height = (gint) MAX (page_height * rc->scale, 1);
1367         }
1368         
1369         if (rc->rotation == 90 || rc->rotation == 270) {
1370                 gint  temp;
1371
1372                 temp = *width;
1373                 *width = *height;
1374                 *height = temp;
1375         }
1376         
1377         g_object_unref (poppler_page);
1378 }
1379
1380 static void
1381 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1382 {
1383         iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1384         iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1385 }
1386
1387
1388 static gboolean
1389 pdf_document_search_idle_callback (void *data)
1390 {
1391         PdfDocumentSearch *search = (PdfDocumentSearch*) data;
1392         PdfDocument *pdf_document = search->document;
1393         int n_pages;
1394         GList *matches;
1395         PopplerPage *page;
1396
1397         page = poppler_document_get_page (search->document->document,
1398                                           search->search_page);
1399
1400         ev_document_doc_mutex_lock ();
1401         matches = poppler_page_find_text (page, search->text);
1402         ev_document_doc_mutex_unlock ();
1403
1404         g_object_unref (page);
1405
1406         search->pages[search->search_page] = matches;
1407         ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
1408                                   search->search_page);
1409
1410         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
1411         search->search_page += 1;
1412         if (search->search_page == n_pages) {
1413                 /* wrap around */
1414                 search->search_page = 0;
1415         }
1416
1417         if (search->search_page != search->start_page) {
1418                 return TRUE;
1419         }
1420
1421         /* We're done. */
1422         search->idle = 0; /* will return FALSE to remove */
1423         return FALSE;
1424 }
1425
1426
1427 static PdfDocumentSearch *
1428 pdf_document_search_new (PdfDocument *pdf_document,
1429                          int          start_page,
1430                          const char  *text)
1431 {
1432         PdfDocumentSearch *search;
1433         int n_pages;
1434         int i;
1435
1436         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
1437
1438         search = g_new0 (PdfDocumentSearch, 1);
1439
1440         search->text = g_strdup (text);
1441         search->pages = g_new0 (GList *, n_pages);
1442         search->document = pdf_document;
1443
1444         /* We add at low priority so the progress bar repaints */
1445         search->idle = g_idle_add_full (G_PRIORITY_LOW,
1446                                         pdf_document_search_idle_callback,
1447                                         search,
1448                                         NULL);
1449
1450         search->start_page = start_page;
1451         search->search_page = start_page;
1452
1453         return search;
1454 }
1455
1456 static void
1457 pdf_document_find_begin (EvDocumentFind   *document,
1458                          int               page,
1459                          const char       *search_string,
1460                          gboolean          case_sensitive)
1461 {
1462         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1463
1464         /* FIXME handle case_sensitive (right now XPDF
1465          * code is always case insensitive for ASCII
1466          * and case sensitive for all other languaages)
1467          */
1468
1469         if (pdf_document->search &&
1470             strcmp (search_string, pdf_document->search->text) == 0)
1471                 return;
1472
1473         if (pdf_document->search)
1474                 pdf_document_search_free (pdf_document->search);
1475
1476         pdf_document->search = pdf_document_search_new (pdf_document,
1477                                                         page,
1478                                                         search_string);
1479 }
1480
1481 static int
1482 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1483 {
1484         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1485
1486         if (search) {
1487                 return g_list_length (search->pages[page]);
1488         } else {
1489                 return 0;
1490         }
1491 }
1492
1493 static gboolean
1494 pdf_document_find_get_result (EvDocumentFind *document_find,
1495                               int             page,
1496                               int             n_result,
1497                               EvRectangle    *rectangle)
1498 {
1499         PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1500         PdfDocumentSearch *search = pdf_document->search;
1501         PopplerPage *poppler_page;
1502         PopplerRectangle *r;
1503         double height;
1504
1505         if (search == NULL)
1506                 return FALSE;
1507
1508         r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1509                                                   n_result);
1510         if (r == NULL)
1511                 return FALSE;
1512
1513         poppler_page = poppler_document_get_page (pdf_document->document, page);
1514         poppler_page_get_size (poppler_page, NULL, &height);
1515         rectangle->x1 = r->x1;
1516         rectangle->y1 = height - r->y2;
1517         rectangle->x2 = r->x2;
1518         rectangle->y2 = height - r->y1;
1519         g_object_unref (poppler_page);
1520                 
1521         return TRUE;
1522 }
1523
1524 static int
1525 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1526                                     int             page)
1527 {
1528         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1529
1530         return search && search->pages[page] != NULL;
1531 }
1532
1533 static double
1534 pdf_document_find_get_progress (EvDocumentFind *document_find)
1535 {
1536         PdfDocumentSearch *search;
1537         int n_pages, pages_done;
1538
1539         search = PDF_DOCUMENT (document_find)->search;
1540
1541         if (search == NULL) {
1542                 return 0;
1543         }
1544
1545         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1546         if (search->search_page > search->start_page) {
1547                 pages_done = search->search_page - search->start_page + 1;
1548         } else if (search->search_page == search->start_page) {
1549                 pages_done = n_pages;
1550         } else {
1551                 pages_done = n_pages - search->start_page + search->search_page;
1552         }
1553
1554         return pages_done / (double) n_pages;
1555 }
1556
1557 static void
1558 pdf_document_find_cancel (EvDocumentFind *document)
1559 {
1560         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1561
1562         if (pdf_document->search) {
1563                 pdf_document_search_free (pdf_document->search);
1564                 pdf_document->search = NULL;
1565         }
1566 }
1567
1568 static void
1569 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1570 {
1571         iface->begin = pdf_document_find_begin;
1572         iface->get_n_results = pdf_document_find_get_n_results;
1573         iface->get_result = pdf_document_find_get_result;
1574         iface->page_has_results = pdf_document_find_page_has_results;
1575         iface->get_progress = pdf_document_find_get_progress;
1576         iface->cancel = pdf_document_find_cancel;
1577 }
1578
1579 static void
1580 pdf_print_context_free (PdfPrintContext *ctx)
1581 {
1582         if (!ctx)
1583                 return;
1584
1585 #ifdef HAVE_CAIRO_PRINT
1586         if (ctx->cr) {
1587                 cairo_destroy (ctx->cr);
1588                 ctx->cr = NULL;
1589         }
1590 #else
1591         if (ctx->ps_file) {
1592                 poppler_ps_file_free (ctx->ps_file);
1593                 ctx->ps_file = NULL;
1594         }
1595 #endif
1596         g_free (ctx);
1597 }
1598
1599 static void
1600 pdf_document_file_exporter_begin (EvFileExporter        *exporter,
1601                                   EvFileExporterContext *fc)
1602 {
1603         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1604         PdfPrintContext *ctx;
1605 #ifdef HAVE_CAIRO_PRINT
1606         gdouble width, height;
1607         cairo_surface_t *surface = NULL;
1608 #endif
1609         
1610         if (pdf_document->print_ctx)
1611                 pdf_print_context_free (pdf_document->print_ctx);
1612         pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1613         ctx = pdf_document->print_ctx;
1614         ctx->format = fc->format;
1615         
1616 #ifdef HAVE_CAIRO_PRINT
1617         ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1618
1619         ctx->paper_width = fc->paper_width;
1620         ctx->paper_height = fc->paper_height;
1621         
1622         switch (fc->pages_per_sheet) {
1623                 default:
1624                 case 1:
1625                         ctx->pages_x = 1;
1626                         ctx->pages_y = 1;
1627                         break;
1628                 case 2:
1629                         ctx->pages_x = 1;
1630                         ctx->pages_y = 2;
1631                         break;
1632                 case 4:
1633                         ctx->pages_x = 2;
1634                         ctx->pages_y = 2;
1635                         break;
1636                 case 6:
1637                         ctx->pages_x = 2;
1638                         ctx->pages_y = 3;
1639                         break;
1640                 case 9:
1641                         ctx->pages_x = 3;
1642                         ctx->pages_y = 3;
1643                         break;
1644                 case 16:
1645                         ctx->pages_x = 4;
1646                         ctx->pages_y = 4;
1647                         break;
1648         }
1649
1650         ctx->pages_printed = 0;
1651         
1652         switch (fc->format) {
1653                 case EV_FILE_FORMAT_PS:
1654 #ifdef HAVE_CAIRO_PS
1655                         surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1656 #endif
1657                         break;
1658                 case EV_FILE_FORMAT_PDF:
1659 #ifdef HAVE_CAIRO_PDF
1660                         surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1661 #endif
1662                         break;
1663                 default:
1664                         g_assert_not_reached ();
1665         }
1666
1667         ctx->cr = cairo_create (surface);
1668         cairo_surface_destroy (surface);
1669
1670 #else /* HAVE_CAIRO_PRINT */
1671         if (ctx->format == EV_FILE_FORMAT_PS) {
1672                 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1673                                                     fc->filename, fc->first_page,
1674                                                     fc->last_page - fc->first_page + 1);
1675                 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1676                 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1677         }
1678 #endif /* HAVE_CAIRO_PRINT */
1679 }
1680
1681 static void
1682 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1683 {
1684         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1685         PdfPrintContext *ctx = pdf_document->print_ctx;
1686         
1687         g_return_if_fail (pdf_document->print_ctx != NULL);
1688
1689         ctx->pages_printed = 0;
1690         
1691 #ifdef HAVE_CAIRO_PRINT
1692         if (ctx->paper_width > ctx->paper_height) {
1693                 if (ctx->format == EV_FILE_FORMAT_PS) {
1694                         cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1695                                                    ctx->paper_height,
1696                                                    ctx->paper_width);
1697                 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1698                         cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1699                                                     ctx->paper_height,
1700                                                     ctx->paper_width);
1701                 }
1702         }
1703 #endif /* HAVE_CAIRO_PRINT */
1704 }
1705
1706 static void
1707 pdf_document_file_exporter_do_page (EvFileExporter  *exporter,
1708                                     EvRenderContext *rc)
1709 {
1710         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1711         PdfPrintContext *ctx = pdf_document->print_ctx;
1712         PopplerPage *poppler_page;
1713 #ifdef HAVE_CAIRO_PRINT
1714         gdouble  page_width, page_height;
1715         gint     x, y;
1716         gboolean rotate;
1717         gdouble  width, height;
1718         gdouble  pwidth, pheight;
1719         gdouble  xscale, yscale;
1720 #endif
1721
1722         g_return_if_fail (pdf_document->print_ctx != NULL);
1723
1724         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1725         
1726 #ifdef HAVE_CAIRO_PRINT
1727         x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1728         y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1729         poppler_page_get_size (poppler_page, &page_width, &page_height);
1730
1731         if (page_width > page_height && page_width > ctx->paper_width) {
1732                 rotate = TRUE;
1733         } else {
1734                 rotate = FALSE;
1735         }
1736
1737         /* Use always portrait mode and rotate when necessary */
1738         if (ctx->paper_width > ctx->paper_height) {
1739                 width = ctx->paper_height;
1740                 height = ctx->paper_width;
1741                 rotate = !rotate;
1742         } else {
1743                 width = ctx->paper_width;
1744                 height = ctx->paper_height;
1745         }
1746
1747         if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1748                 rotate = !rotate;
1749         }       
1750
1751         if (rotate) {
1752                 gint tmp1;
1753                 gdouble tmp2;
1754
1755                 tmp1 = x;
1756                 x = y;
1757                 y = tmp1;
1758
1759                 tmp2 = page_width;
1760                 page_width = page_height;
1761                 page_height = tmp2;
1762         }
1763
1764         pwidth = width / ctx->pages_x;
1765         pheight = height / ctx->pages_y;
1766
1767         if ((page_width > pwidth || page_height > pheight) ||
1768             (page_width < pwidth && page_height < pheight)) {
1769                 xscale = pwidth / page_width;
1770                 yscale = pheight / page_height;
1771                 
1772                 if (yscale < xscale) {
1773                         xscale = yscale;
1774                 } else {
1775                         yscale = xscale;
1776                 }
1777                 
1778         } else {        
1779                 xscale = yscale = 1;
1780         }
1781
1782         /* TODO: center */
1783
1784         cairo_save (ctx->cr);
1785         if (rotate) {
1786                 cairo_matrix_t matrix;
1787                 
1788                 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1789                 cairo_matrix_init (&matrix,
1790                                    0,  1,
1791                                    -1,  0,
1792                                    0,  0);
1793                 cairo_transform (ctx->cr, &matrix);
1794         }
1795         
1796         cairo_translate (ctx->cr,
1797                          x * (rotate ? pheight : pwidth),
1798                          y * (rotate ? pwidth : pheight));
1799         cairo_scale (ctx->cr, xscale, yscale);
1800
1801 #ifdef HAVE_POPPLER_PAGE_RENDER_FOR_PRINTING
1802         poppler_page_render_for_printing (poppler_page, ctx->cr);
1803 #else
1804 #ifdef HAVE_POPPLER_PAGE_RENDER
1805         poppler_page_render (poppler_page, ctx->cr);
1806 #endif
1807 #endif
1808
1809         ctx->pages_printed++;
1810                         
1811         cairo_restore (ctx->cr);
1812 #else /* HAVE_CAIRO_PRINT */
1813         if (ctx->format == EV_FILE_FORMAT_PS)
1814                 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1815 #endif /* HAVE_CAIRO_PRINT */
1816         
1817         g_object_unref (poppler_page);
1818 }
1819
1820 static void
1821 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1822 {
1823         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1824         PdfPrintContext *ctx = pdf_document->print_ctx;
1825         
1826         g_return_if_fail (pdf_document->print_ctx != NULL);
1827
1828 #ifdef HAVE_CAIRO_PRINT
1829         cairo_show_page (ctx->cr);
1830 #endif
1831 }
1832
1833 static void
1834 pdf_document_file_exporter_end (EvFileExporter *exporter)
1835 {
1836         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1837
1838         pdf_print_context_free (pdf_document->print_ctx);
1839         pdf_document->print_ctx = NULL;
1840 }
1841
1842 static EvFileExporterCapabilities
1843 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1844 {
1845         return  (EvFileExporterCapabilities) (
1846                 EV_FILE_EXPORTER_CAN_PAGE_SET |
1847                 EV_FILE_EXPORTER_CAN_COPIES |
1848                 EV_FILE_EXPORTER_CAN_COLLATE |
1849                 EV_FILE_EXPORTER_CAN_REVERSE |
1850                 EV_FILE_EXPORTER_CAN_SCALE |
1851 #ifdef HAVE_CAIRO_PRINT
1852 #ifdef HAVE_POPPLER_PAGE_RENDER
1853 #if GTK_CHECK_VERSION (2, 11, 1)
1854                 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1855 #endif
1856 #endif
1857 #endif
1858                 
1859 #ifdef HAVE_CAIRO_PDF
1860 #ifdef HAVE_POPPLER_PAGE_RENDER
1861                 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1862 #endif
1863 #endif
1864                 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1865 }
1866
1867 static void
1868 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1869 {
1870         iface->begin = pdf_document_file_exporter_begin;
1871         iface->begin_page = pdf_document_file_exporter_begin_page;
1872         iface->do_page = pdf_document_file_exporter_do_page;
1873         iface->end_page = pdf_document_file_exporter_end_page;
1874         iface->end = pdf_document_file_exporter_end;
1875         iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1876 }
1877
1878 static void
1879 pdf_selection_render_selection (EvSelection      *selection,
1880                                 EvRenderContext  *rc,
1881                                 cairo_surface_t **surface,
1882                                 EvRectangle      *points,
1883                                 EvRectangle      *old_points,
1884                                 EvSelectionStyle  style,
1885                                 GdkColor         *text,
1886                                 GdkColor         *base)
1887 {
1888         PdfDocument *pdf_document;
1889         double width_points, height_points;
1890         gint width, height;
1891
1892         pdf_document = PDF_DOCUMENT (selection);
1893         set_rc_data (pdf_document, rc);
1894
1895         poppler_page_get_size (POPPLER_PAGE (rc->data),
1896                                &width_points, &height_points);
1897         width = (int) ((width_points * rc->scale) + 0.5);
1898         height = (int) ((height_points * rc->scale) + 0.5);
1899
1900 #ifdef HAVE_POPPLER_PAGE_RENDER
1901         cairo_t *cr;
1902         
1903 #if POPPLER_MAJOR_VERSION <= 6 || (POPPLER_MAJOR_VERSION == 7 && POPPLER_MINOR_VERSION < 2)
1904         GdkColor text_color, base_color;
1905
1906         text_color = *text;
1907         base_color = *base;
1908 #else
1909         PopplerColor text_color, base_color;
1910         
1911         text_color.red = text->red;
1912         text_color.green = text->green;
1913         text_color.blue = text->blue;
1914
1915         base_color.red = base->red;
1916         base_color.green = base->green;
1917         base_color.blue = base->blue;
1918 #endif
1919
1920         if (*surface == NULL) {
1921                 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1922                                                        width, height);
1923                 
1924         }
1925
1926         cr = cairo_create (*surface);
1927         cairo_scale (cr, rc->scale, rc->scale);
1928         cairo_surface_set_device_offset (*surface, 0, 0);
1929         memset (cairo_image_surface_get_data (*surface), 0x00,
1930                 cairo_image_surface_get_height (*surface) *
1931                 cairo_image_surface_get_stride (*surface));
1932         poppler_page_render_selection (POPPLER_PAGE (rc->data),
1933                                        cr,
1934                                        (PopplerRectangle *)points,
1935                                        (PopplerRectangle *)old_points,
1936                                        (PopplerSelectionStyle)style,
1937                                        &text_color,
1938                                        &base_color);
1939         cairo_destroy (cr);
1940 #else /* HAVE_POPPLER_PAGE_RENDER */
1941         GdkPixbuf *pixbuf;
1942         
1943         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1944                                  TRUE, 8,
1945                                  width, height);
1946
1947         poppler_page_render_selection_to_pixbuf (POPPLER_PAGE (rc->data),
1948                                                  rc->scale, rc->rotation, pixbuf,
1949                                                  (PopplerRectangle *)points,
1950                                                  (PopplerRectangle *)old_points,
1951                                                  (PopplerSelectionStyle)style,
1952                                                  text,
1953                                                  base);
1954         if (*surface)
1955                 cairo_surface_destroy (*surface);
1956         *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1957         g_object_unref (pixbuf);
1958 #endif /* HAVE_POPPLER_PAGE_RENDER */
1959 }
1960
1961 static gchar *
1962 pdf_selection_get_selected_text (EvSelection     *selection,
1963                                  EvRenderContext *rc,
1964                                  EvSelectionStyle style,
1965                                  EvRectangle     *points)
1966 {
1967         PdfDocument *pdf_document = PDF_DOCUMENT (selection);
1968         PopplerPage *poppler_page;
1969         PopplerRectangle r;
1970         double height;
1971         char *retval;
1972         
1973         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1974         g_return_val_if_fail (poppler_page != NULL, NULL);
1975
1976         poppler_page_get_size (poppler_page, NULL, &height);
1977         r.x1 = points->x1;
1978         r.y1 = height - points->y2;
1979         r.x2 = points->x2;
1980         r.y2 = height - points->y1;
1981
1982         retval = poppler_page_get_text (poppler_page,
1983                                         (PopplerSelectionStyle)style,
1984                                         &r);
1985
1986         g_object_unref (poppler_page);
1987
1988         return retval;
1989 }
1990
1991 static GdkRegion *
1992 create_gdk_region_from_poppler_region (GList *region)
1993 {
1994         GList *l;
1995         GdkRegion *retval;
1996         
1997         retval = gdk_region_new ();
1998         
1999         for (l = region; l; l = g_list_next (l)) {
2000                 PopplerRectangle *rectangle;
2001                 GdkRectangle      rect;
2002                 
2003                 rectangle = (PopplerRectangle *)l->data;
2004                 
2005                 rect.x = (gint) rectangle->x1;
2006                 rect.y = (gint) rectangle->y1;
2007                 rect.width  = (gint) (rectangle->x2 - rectangle->x1);
2008                 rect.height = (gint) (rectangle->y2 - rectangle->y1);
2009                 gdk_region_union_with_rect (retval, &rect);
2010                 
2011                 poppler_rectangle_free (rectangle);
2012         }
2013
2014         return retval;
2015 }
2016
2017 static GdkRegion *
2018 pdf_selection_get_selection_region (EvSelection     *selection,
2019                                     EvRenderContext *rc,
2020                                     EvSelectionStyle style,
2021                                     EvRectangle     *points)
2022 {
2023         PdfDocument *pdf_document;
2024         GdkRegion   *retval;
2025
2026         pdf_document = PDF_DOCUMENT (selection);
2027
2028         set_rc_data (pdf_document, rc);
2029         
2030 #if POPPLER_MAJOR_VERSION <= 6 || (POPPLER_MAJOR_VERSION == 7 && POPPLER_MINOR_VERSION < 2)
2031         retval = poppler_page_get_selection_region (POPPLER_PAGE (rc->data),
2032                                                     rc->scale,
2033                                                     (PopplerSelectionStyle)style,
2034                                                     (PopplerRectangle *) points);
2035 #else
2036         GList *region;
2037         
2038         region = poppler_page_get_selection_region (POPPLER_PAGE (rc->data),
2039                                                     rc->scale,
2040                                                     (PopplerSelectionStyle)style,
2041                                                     (PopplerRectangle *) points);
2042         retval = create_gdk_region_from_poppler_region (region);
2043         g_list_free (region);
2044 #endif
2045         
2046         return retval;
2047 }
2048
2049 static GdkRegion *
2050 pdf_selection_get_selection_map (EvSelection     *selection,
2051                                  EvRenderContext *rc)
2052 {
2053         PdfDocument *pdf_document;
2054         PopplerPage *poppler_page;
2055         PopplerRectangle points;
2056         GList *region;
2057         GdkRegion *retval;
2058
2059         pdf_document = PDF_DOCUMENT (selection);
2060         poppler_page = poppler_document_get_page (pdf_document->document,
2061                                                   rc->page);
2062
2063         points.x1 = 0.0;
2064         points.y1 = 0.0;
2065         poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
2066         
2067 #if POPPLER_MAJOR_VERSION <= 6 || (POPPLER_MAJOR_VERSION == 7 && POPPLER_MINOR_VERSION < 2)
2068         retval = poppler_page_get_selection_region (poppler_page, 1.0,
2069                                                     POPPLER_SELECTION_GLYPH,
2070                                                     &points);
2071 #else
2072         region = poppler_page_get_selection_region (poppler_page, 1.0,
2073                                                     POPPLER_SELECTION_GLYPH,
2074                                                     &points);
2075         retval = create_gdk_region_from_poppler_region (region);
2076         g_list_free (region);
2077 #endif
2078         g_object_unref (poppler_page);
2079
2080         return retval;
2081 }
2082
2083 static void
2084 pdf_selection_iface_init (EvSelectionIface *iface)
2085 {
2086         iface->render_selection = pdf_selection_render_selection;
2087         iface->get_selected_text = pdf_selection_get_selected_text;
2088         iface->get_selection_region = pdf_selection_get_selection_region;
2089         iface->get_selection_map = pdf_selection_get_selection_map;
2090 }
2091
2092 /* Page Transitions */
2093 static gdouble
2094 pdf_document_get_page_duration (EvDocumentTransition *trans,
2095                                 gint                  page)
2096 {
2097         PdfDocument *pdf_document;
2098         PopplerPage *poppler_page;
2099         gdouble      duration = -1;
2100
2101         pdf_document = PDF_DOCUMENT (trans);
2102         poppler_page = poppler_document_get_page (pdf_document->document, page);
2103         if (!poppler_page)
2104                 return -1;
2105
2106         duration = poppler_page_get_duration (poppler_page);
2107         g_object_unref (poppler_page);
2108
2109         return duration;
2110 }
2111
2112 static EvTransitionEffect *
2113 pdf_document_get_effect (EvDocumentTransition *trans,
2114                          gint                  page)
2115 {
2116         PdfDocument            *pdf_document;
2117         PopplerPage            *poppler_page;
2118         PopplerPageTransition  *page_transition;
2119         EvTransitionEffect     *effect;
2120
2121         pdf_document = PDF_DOCUMENT (trans);
2122         poppler_page = poppler_document_get_page (pdf_document->document, page);
2123
2124         if (!poppler_page)
2125                 return NULL;
2126
2127         page_transition = poppler_page_get_transition (poppler_page);
2128
2129         if (!page_transition) {
2130                 g_object_unref (poppler_page);
2131                 return NULL;
2132         }
2133
2134         /* enums in PopplerPageTransition match the EvTransitionEffect ones */
2135         effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
2136                                            "alignment", page_transition->alignment,
2137                                            "direction", page_transition->direction,
2138                                            "duration", page_transition->duration,
2139                                            "angle", page_transition->angle,
2140                                            "scale", page_transition->scale,
2141                                            "rectangular", page_transition->rectangular,
2142                                            NULL);
2143
2144         poppler_page_transition_free (page_transition);
2145         g_object_unref (poppler_page);
2146
2147         return effect;
2148 }
2149
2150 static void
2151 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
2152 {
2153         iface->get_page_duration = pdf_document_get_page_duration;
2154         iface->get_effect = pdf_document_get_effect;
2155 }
2156
2157 /* Forms */
2158 static void
2159 pdf_document_get_crop_box (EvDocument  *document, 
2160                            int          page, 
2161                            EvRectangle *rect)
2162 {
2163         PdfDocument *pdf_document;
2164         PopplerPage *poppler_page;
2165         PopplerRectangle poppler_rect;
2166
2167         pdf_document = PDF_DOCUMENT (document);
2168         poppler_page = poppler_document_get_page (pdf_document->document, page);
2169         poppler_page_get_crop_box (poppler_page, &poppler_rect);
2170         rect->x1 = poppler_rect.x1;
2171         rect->x2 = poppler_rect.x2;
2172         rect->y1 = poppler_rect.y1;
2173         rect->y2 = poppler_rect.y2;
2174 }
2175
2176 static EvFormField *
2177 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
2178 {
2179         EvFormField *ev_field = NULL;
2180         gint         id;
2181         gdouble      font_size;
2182         gboolean     is_read_only;
2183
2184         id = poppler_form_field_get_id (poppler_field);
2185         font_size = poppler_form_field_get_font_size (poppler_field);
2186         is_read_only = poppler_form_field_is_read_only (poppler_field);
2187
2188         switch (poppler_form_field_get_field_type (poppler_field)) {
2189                 case POPPLER_FORM_FIELD_TEXT: {
2190                         EvFormFieldText    *field_text;
2191                         EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2192
2193                         switch (poppler_form_field_text_get_text_type (poppler_field)) {
2194                                 case POPPLER_FORM_TEXT_NORMAL:
2195                                         ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2196                                         break;
2197                                 case POPPLER_FORM_TEXT_MULTILINE:
2198                                         ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2199                                         break;
2200                                 case POPPLER_FORM_TEXT_FILE_SELECT:
2201                                         ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2202                                         break;
2203                         }
2204                         
2205                         ev_field = ev_form_field_text_new (id, ev_text_type);
2206                         field_text = EV_FORM_FIELD_TEXT (ev_field);
2207
2208                         field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2209                         field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2210                         field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2211                         field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2212                         field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2213                         field_text->text = poppler_form_field_text_get_text (poppler_field);
2214
2215                 }
2216                         break;
2217                 case POPPLER_FORM_FIELD_BUTTON: {
2218                         EvFormFieldButton    *field_button;
2219                         EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2220
2221                         switch (poppler_form_field_button_get_button_type (poppler_field)) {
2222                                 case POPPLER_FORM_BUTTON_PUSH:
2223                                         ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2224                                         break;
2225                                 case POPPLER_FORM_BUTTON_CHECK:
2226                                         ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2227                                         break;
2228                                 case POPPLER_FORM_BUTTON_RADIO:
2229                                         ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2230                                         break;
2231                         }
2232
2233                         ev_field = ev_form_field_button_new (id, ev_button_type);
2234                         field_button = EV_FORM_FIELD_BUTTON (ev_field);
2235                         
2236                         field_button->state = poppler_form_field_button_get_state (poppler_field);
2237                 }
2238                         break;
2239                 case POPPLER_FORM_FIELD_CHOICE: {
2240                         EvFormFieldChoice    *field_choice;
2241                         EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2242
2243                         switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2244                                 case POPPLER_FORM_CHOICE_COMBO:
2245                                         ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2246                                         break;
2247                                 case EV_FORM_FIELD_CHOICE_LIST:
2248                                         ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2249                                         break;
2250                         }
2251
2252                         ev_field = ev_form_field_choice_new (id, ev_choice_type);
2253                         field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2254
2255                         field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2256                         field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2257                         field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2258                         field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2259
2260                         /* TODO: we need poppler_form_field_choice_get_selected_items in poppler 
2261                         field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2262                         if (field_choice->is_editable)
2263                                 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2264                 }
2265                         break;
2266                 case POPPLER_FORM_FIELD_SIGNATURE:
2267                         /* TODO */
2268                         ev_field = ev_form_field_signature_new (id);
2269                         break;
2270                 case POPPLER_FORM_FIELD_UNKNOWN:
2271                         return NULL;
2272         }
2273
2274         ev_field->font_size = font_size;
2275         ev_field->is_read_only = is_read_only;
2276
2277         return ev_field;
2278 }
2279
2280 static GList *
2281 pdf_document_forms_get_form_fields (EvDocumentForms *document, 
2282                                     gint             page)
2283 {
2284         PdfDocument *pdf_document;
2285         PopplerPage *poppler_page;
2286         GList *retval = NULL;
2287         GList *fields;
2288         GList *list;
2289         double height;
2290
2291         pdf_document = PDF_DOCUMENT (document);
2292         poppler_page = poppler_document_get_page (pdf_document->document, page);
2293         fields = poppler_page_get_form_field_mapping (poppler_page);
2294         poppler_page_get_size (poppler_page, NULL, &height);
2295
2296         for (list = fields; list; list = list->next) {
2297                 PopplerFormFieldMapping *mapping;
2298                 EvFormFieldMapping *field_mapping;
2299                 EvFormField *ev_field;
2300
2301                 mapping = (PopplerFormFieldMapping *)list->data;
2302
2303                 ev_field = ev_form_field_from_poppler_field (mapping->field);
2304                 if (!ev_field)
2305                         continue;
2306
2307                 field_mapping = g_new0 (EvFormFieldMapping, 1);
2308                 field_mapping->x1 = mapping->area.x1;
2309                 field_mapping->x2 = mapping->area.x2;
2310                 field_mapping->y1 = height - mapping->area.y2;
2311                 field_mapping->y2 = height - mapping->area.y1;
2312                 field_mapping->field = ev_field;
2313                 field_mapping->field->page = page;
2314                 
2315                 retval = g_list_prepend (retval, field_mapping);
2316         }
2317         
2318         poppler_page_free_form_field_mapping (fields);
2319         g_object_unref (poppler_page);
2320
2321         return g_list_reverse (retval);
2322 }
2323
2324 static gchar *
2325 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2326                                              EvFormField     *field)
2327         
2328 {
2329         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2330         PopplerFormField *poppler_field;
2331         gchar *text;
2332
2333         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2334         if (!poppler_field)
2335                 return NULL;
2336         
2337         text = poppler_form_field_text_get_text (poppler_field);
2338         g_object_unref (poppler_field);
2339
2340         return text;
2341 }
2342
2343 static void
2344 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document, 
2345                                              EvFormField     *field,
2346                                              const gchar     *text)
2347 {
2348         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2349         PopplerFormField *poppler_field;
2350
2351         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2352         if (!poppler_field)
2353                 return;
2354         poppler_form_field_text_set_text (poppler_field, text);
2355         g_object_unref (poppler_field);
2356 }
2357
2358 static void
2359 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document, 
2360                                                 EvFormField     *field,
2361                                                 gboolean         state)
2362 {
2363         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2364         PopplerFormField *poppler_field;
2365
2366         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2367         if (!poppler_field)
2368                 return;
2369         
2370         poppler_form_field_button_set_state (poppler_field, state);
2371         g_object_unref (poppler_field);
2372 }
2373
2374 static gboolean
2375 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document, 
2376                                                 EvFormField     *field)
2377 {
2378         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2379         PopplerFormField *poppler_field;
2380         gboolean state;
2381
2382         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2383         if (!poppler_field)
2384                 return FALSE;
2385
2386         state = poppler_form_field_button_get_state (poppler_field);
2387         g_object_unref (poppler_field);
2388
2389         return state;
2390 }
2391
2392 static gchar *
2393 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document, 
2394                                                EvFormField     *field,
2395                                                gint             index)
2396 {
2397         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2398         PopplerFormField *poppler_field;
2399         gchar *text;
2400
2401         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2402         if (!poppler_field)
2403                 return NULL;
2404
2405         text = poppler_form_field_choice_get_item (poppler_field, index);
2406         g_object_unref (poppler_field);
2407
2408         return text;
2409 }
2410
2411 static int
2412 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document, 
2413                                                   EvFormField     *field)
2414 {
2415         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2416         PopplerFormField *poppler_field;
2417         gint n_items;
2418
2419         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2420         if (!poppler_field)
2421                 return -1;
2422         
2423         n_items = poppler_form_field_choice_get_n_items (poppler_field);
2424         g_object_unref (poppler_field);
2425
2426         return n_items;
2427 }
2428
2429 static gboolean
2430 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document, 
2431                                                        EvFormField     *field,
2432                                                        gint             index)
2433 {
2434         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2435         PopplerFormField *poppler_field;
2436         gboolean selected;
2437
2438         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2439         if (!poppler_field)
2440                 return FALSE;
2441
2442         selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2443         g_object_unref (poppler_field);
2444
2445         return selected;
2446 }
2447
2448 static void
2449 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document, 
2450                                                   EvFormField     *field,
2451                                                   gint             index)
2452 {
2453         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2454         PopplerFormField *poppler_field;
2455
2456         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2457         if (!poppler_field)
2458                 return;
2459
2460         poppler_form_field_choice_select_item (poppler_field, index);
2461         g_object_unref (poppler_field);
2462 }
2463
2464 static void
2465 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document, 
2466                                                   EvFormField     *field,
2467                                                   gint             index)
2468 {
2469         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2470         PopplerFormField *poppler_field;
2471
2472         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2473         if (!poppler_field)
2474                 return;
2475
2476         poppler_form_field_choice_toggle_item (poppler_field, index);
2477         g_object_unref (poppler_field);
2478 }
2479
2480 static void
2481 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document, 
2482                                                    EvFormField     *field)
2483 {
2484         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2485         PopplerFormField *poppler_field;
2486
2487         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2488         if (!poppler_field)
2489                 return;
2490         
2491         poppler_form_field_choice_unselect_all (poppler_field);
2492         g_object_unref (poppler_field);
2493 }
2494
2495 static void
2496 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2497                                                EvFormField     *field,
2498                                                const gchar     *text)
2499 {
2500         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2501         PopplerFormField *poppler_field;
2502
2503         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2504         if (!poppler_field)
2505                 return;
2506         
2507         poppler_form_field_choice_set_text (poppler_field, text);
2508         g_object_unref (poppler_field);
2509 }
2510
2511 static gchar *
2512 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2513                                                EvFormField     *field)
2514 {
2515         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2516         PopplerFormField *poppler_field;
2517         gchar *text;
2518
2519         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2520         if (!poppler_field)
2521                 return NULL;
2522
2523         text = poppler_form_field_choice_get_text (poppler_field);
2524         g_object_unref (poppler_field);
2525
2526         return text;
2527 }
2528
2529 static void
2530 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2531 {
2532         iface->get_form_fields = pdf_document_forms_get_form_fields;
2533         iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2534         iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2535         iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2536         iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2537         iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2538         iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2539         iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2540         iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2541         iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2542         iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2543         iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2544         iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2545 }
2546