]> www.fi.muni.cz Git - evince.git/blob - backend/pdf/ev-poppler.cc
Fix build with poppler <= 0.7.2
[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 named action: %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 with a testcase.",
998                            unimplemented_action);
999         }
1000         
1001         link = ev_link_new (action->any.title, ev_action);
1002         
1003         return link;    
1004 }
1005
1006 static void
1007 build_tree (PdfDocument      *pdf_document,
1008             GtkTreeModel     *model,
1009             GtkTreeIter      *parent,
1010             PopplerIndexIter *iter)
1011 {
1012         
1013         do {
1014                 GtkTreeIter tree_iter;
1015                 PopplerIndexIter *child;
1016                 PopplerAction *action;
1017                 EvLink *link = NULL;
1018                 gboolean expand;
1019                 char *title_markup;
1020                 
1021                 action = poppler_index_iter_get_action (iter);
1022                 expand = poppler_index_iter_is_open (iter);
1023
1024                 if (!action)
1025                         continue;
1026
1027                 switch (action->type) {
1028                         case POPPLER_ACTION_GOTO_DEST: {
1029                                 /* For bookmarks, solve named destinations */
1030                                 if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
1031                                         PopplerDest *dest;
1032                                         EvLinkDest *ev_dest = NULL;
1033                                         EvLinkAction *ev_action;
1034                                         
1035                                         dest = poppler_document_find_dest (pdf_document->document,
1036                                                                            action->goto_dest.dest->named_dest);
1037                                         if (!dest) {
1038                                                 link = ev_link_from_action (pdf_document, action);
1039                                                 break;
1040                                         }
1041                                         
1042                                         ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1043                                         poppler_dest_free (dest);
1044                                         
1045                                         ev_action = ev_link_action_new_dest (ev_dest);
1046                                         link = ev_link_new (action->any.title, ev_action);
1047                                 } else {
1048                                         link = ev_link_from_action (pdf_document, action);
1049                                 }
1050                         }
1051                                 break;
1052                         default:
1053                                 link = ev_link_from_action (pdf_document, action);
1054                                 break;
1055                 }
1056                 
1057                 if (!link || strlen (ev_link_get_title (link)) <= 0) {
1058                         poppler_action_free (action);
1059                         if (link)
1060                                 g_object_unref (link);
1061                         
1062                         continue;
1063                 }
1064
1065                 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1066                 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1067                 
1068                 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1069                                     EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1070                                     EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1071                                     EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1072                                     -1);
1073                 
1074                 g_free (title_markup);
1075                 g_object_unref (link);
1076                 
1077                 child = poppler_index_iter_get_child (iter);
1078                 if (child)
1079                         build_tree (pdf_document, model, &tree_iter, child);
1080                 poppler_index_iter_free (child);
1081                 poppler_action_free (action);
1082                 
1083         } while (poppler_index_iter_next (iter));
1084 }
1085
1086 static GtkTreeModel *
1087 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1088 {
1089         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1090         GtkTreeModel *model = NULL;
1091         PopplerIndexIter *iter;
1092         
1093         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1094
1095         iter = poppler_index_iter_new (pdf_document->document);
1096         /* Create the model if we have items*/
1097         if (iter != NULL) {
1098                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1099                                                              G_TYPE_STRING,
1100                                                              G_TYPE_OBJECT,
1101                                                              G_TYPE_BOOLEAN,
1102                                                              G_TYPE_STRING);
1103                 build_tree (pdf_document, model, NULL, iter);
1104                 poppler_index_iter_free (iter);
1105         }
1106         
1107         return model;
1108 }
1109
1110 static GList *
1111 pdf_document_links_get_links (EvDocumentLinks *document_links,
1112                               gint             page)
1113 {
1114         PdfDocument *pdf_document;
1115         PopplerPage *poppler_page;
1116         GList *retval = NULL;
1117         GList *mapping_list;
1118         GList *list;
1119         double height;
1120
1121         pdf_document = PDF_DOCUMENT (document_links);
1122         poppler_page = poppler_document_get_page (pdf_document->document,
1123                                                   page);
1124         mapping_list = poppler_page_get_link_mapping (poppler_page);
1125         poppler_page_get_size (poppler_page, NULL, &height);
1126
1127         for (list = mapping_list; list; list = list->next) {
1128                 PopplerLinkMapping *link_mapping;
1129                 EvLinkMapping *ev_link_mapping;
1130
1131                 link_mapping = (PopplerLinkMapping *)list->data;
1132                 ev_link_mapping = g_new (EvLinkMapping, 1);
1133                 ev_link_mapping->link = ev_link_from_action (pdf_document,
1134                                                              link_mapping->action);
1135                 ev_link_mapping->x1 = link_mapping->area.x1;
1136                 ev_link_mapping->x2 = link_mapping->area.x2;
1137                 /* Invert this for X-style coordinates */
1138                 ev_link_mapping->y1 = height - link_mapping->area.y2;
1139                 ev_link_mapping->y2 = height - link_mapping->area.y1;
1140
1141                 retval = g_list_prepend (retval, ev_link_mapping);
1142         }
1143
1144         poppler_page_free_link_mapping (mapping_list);
1145         g_object_unref (poppler_page);
1146
1147         return g_list_reverse (retval);
1148 }
1149
1150 static EvLinkDest *
1151 pdf_document_links_find_link_dest (EvDocumentLinks  *document_links,
1152                                    const gchar      *link_name)
1153 {
1154         PdfDocument *pdf_document;
1155         PopplerDest *dest;
1156         EvLinkDest *ev_dest = NULL;
1157
1158         pdf_document = PDF_DOCUMENT (document_links);
1159         dest = poppler_document_find_dest (pdf_document->document,
1160                                            link_name);
1161         if (dest) {
1162                 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1163                 poppler_dest_free (dest);
1164         }
1165
1166         return ev_dest;
1167 }
1168
1169 static void
1170 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1171 {
1172         iface->has_document_links = pdf_document_links_has_document_links;
1173         iface->get_links_model = pdf_document_links_get_links_model;
1174         iface->get_links = pdf_document_links_get_links;
1175         iface->find_link_dest = pdf_document_links_find_link_dest;
1176 }
1177
1178 static GList *
1179 pdf_document_images_get_image_mapping (EvDocumentImages *document_images,
1180                                        gint              page)
1181 {
1182         GList *retval = NULL;
1183         PdfDocument *pdf_document;
1184         PopplerPage *poppler_page;
1185         GList *mapping_list;
1186         GList *list;
1187
1188         pdf_document = PDF_DOCUMENT (document_images);
1189         poppler_page = poppler_document_get_page (pdf_document->document, page);
1190         mapping_list = poppler_page_get_image_mapping (poppler_page);
1191
1192         for (list = mapping_list; list; list = list->next) {
1193                 PopplerImageMapping *image_mapping;
1194                 EvImageMapping *ev_image_mapping;
1195
1196                 image_mapping = (PopplerImageMapping *)list->data;
1197
1198                 ev_image_mapping = g_new (EvImageMapping, 1);
1199 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1200                 ev_image_mapping->image = ev_image_new (page, image_mapping->image_id);
1201 #elif POPPLER_HAS_CAIRO
1202                 ev_image_mapping->image = ev_image_new_from_pixbuf (image_mapping->image);
1203 #endif
1204                 ev_image_mapping->x1 = image_mapping->area.x1;
1205                 ev_image_mapping->x2 = image_mapping->area.x2;
1206                 ev_image_mapping->y1 = image_mapping->area.y1;
1207                 ev_image_mapping->y2 = image_mapping->area.y2;
1208
1209                 retval = g_list_prepend (retval, ev_image_mapping);
1210         }
1211
1212         poppler_page_free_image_mapping (mapping_list);
1213         g_object_unref (poppler_page);
1214
1215         return g_list_reverse (retval);
1216 }
1217
1218 GdkPixbuf *
1219 pdf_document_images_get_image (EvDocumentImages *document_images,
1220                                EvImage          *image)
1221 {
1222 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1223         PdfDocument     *pdf_document;
1224         PopplerPage     *poppler_page;
1225         cairo_surface_t *surface;
1226         GdkPixbuf       *retval = NULL;
1227
1228         pdf_document = PDF_DOCUMENT (document_images);
1229         poppler_page = poppler_document_get_page (pdf_document->document,
1230                                                   ev_image_get_page (image));
1231
1232         surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1233         if (surface) {
1234                 retval = ev_document_misc_pixbuf_from_surface (surface);
1235                 cairo_surface_destroy (surface);
1236         }
1237
1238         g_object_unref (poppler_page);
1239
1240         return retval;
1241 #else
1242         return GDK_PIXBUF (g_object_ref (ev_image_get_pixbuf (image)));
1243 #endif /* HAVE_POPPLER_PAGE_GET_IMAGE */
1244 }
1245
1246 static void
1247 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1248 {
1249         iface->get_image_mapping = pdf_document_images_get_image_mapping;
1250         iface->get_image = pdf_document_images_get_image;
1251 }
1252
1253 static GdkPixbuf *
1254 make_thumbnail_for_page (PdfDocument     *pdf_document,
1255                          PopplerPage     *poppler_page, 
1256                          EvRenderContext *rc)
1257 {
1258         GdkPixbuf *pixbuf;
1259         int width, height;
1260
1261         pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1262                                                 rc, &width, &height);
1263 #ifdef POPPLER_HAS_GDK
1264         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1265                                  width, height);
1266         gdk_pixbuf_fill (pixbuf, 0xffffffff);
1267
1268         ev_document_fc_mutex_lock ();
1269         poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1270                                        width, height,
1271                                        rc->scale, rc->rotation, pixbuf);
1272         ev_document_fc_mutex_unlock ();
1273 #else
1274         cairo_surface_t *surface;
1275
1276         ev_document_fc_mutex_lock ();
1277         surface = pdf_page_render (poppler_page, width, height, rc);
1278         ev_document_fc_mutex_unlock ();
1279         
1280         pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1281         cairo_surface_destroy (surface);
1282 #endif /* POPPLER_HAS_GDK */
1283
1284         return pixbuf;
1285 }
1286
1287 static GdkPixbuf *
1288 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1289                                        EvRenderContext      *rc, 
1290                                        gboolean              border)
1291 {
1292         PdfDocument *pdf_document;
1293         PopplerPage *poppler_page;
1294         GdkPixbuf *pixbuf = NULL;
1295         GdkPixbuf *border_pixbuf;
1296
1297         pdf_document = PDF_DOCUMENT (document_thumbnails);
1298
1299         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1300         g_return_val_if_fail (poppler_page != NULL, NULL);
1301
1302 #if POPPLER_MAJOR_VERSION <= 6 || (POPPLER_MAJOR_VERSION == 7 && POPPLER_MINOR_VERSION < 2)
1303         pixbuf = poppler_page_get_thumbnail (poppler_page);
1304 #else
1305 #ifdef POPPLER_HAS_GDK
1306         pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1307 #else
1308         cairo_surface_t *surface;
1309         
1310         surface = poppler_page_get_thumbnail (poppler_page);
1311         if (surface) {
1312                 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1313                 cairo_surface_destroy (surface);
1314         }
1315 #endif
1316 #endif
1317                 
1318         if (pixbuf) {
1319                 /* Rotate provided thumbnail if needed */
1320                 GdkPixbuf *rotated_pixbuf;
1321
1322                 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1323                                                            (GdkPixbufRotation) (360 - rc->rotation));
1324                 g_object_unref (pixbuf);
1325                 pixbuf = rotated_pixbuf;
1326         } else {
1327                 /* There is no provided thumbnail.  We need to make one. */
1328                 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1329         }
1330
1331         if (border) {           
1332                 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1333                 g_object_unref (pixbuf);
1334                 pixbuf = border_pixbuf;
1335         }               
1336
1337         g_object_unref (poppler_page);
1338         
1339         return pixbuf;
1340 }
1341
1342 static void
1343 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1344                                         EvRenderContext      *rc,
1345                                         gint                 *width,
1346                                         gint                 *height)
1347 {
1348         PdfDocument *pdf_document;
1349         PopplerPage *poppler_page;
1350         gint has_thumb;
1351         
1352         pdf_document = PDF_DOCUMENT (document_thumbnails);
1353         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1354
1355         g_return_if_fail (poppler_page != NULL);
1356
1357         has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1358
1359         if (!has_thumb) {
1360                 double page_width, page_height;
1361
1362                 poppler_page_get_size (poppler_page, &page_width, &page_height);
1363
1364                 *width = (gint) MAX (page_width * rc->scale, 1);
1365                 *height = (gint) MAX (page_height * rc->scale, 1);
1366         }
1367         
1368         if (rc->rotation == 90 || rc->rotation == 270) {
1369                 gint  temp;
1370
1371                 temp = *width;
1372                 *width = *height;
1373                 *height = temp;
1374         }
1375         
1376         g_object_unref (poppler_page);
1377 }
1378
1379 static void
1380 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1381 {
1382         iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1383         iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1384 }
1385
1386
1387 static gboolean
1388 pdf_document_search_idle_callback (void *data)
1389 {
1390         PdfDocumentSearch *search = (PdfDocumentSearch*) data;
1391         PdfDocument *pdf_document = search->document;
1392         int n_pages;
1393         GList *matches;
1394         PopplerPage *page;
1395
1396         page = poppler_document_get_page (search->document->document,
1397                                           search->search_page);
1398
1399         ev_document_doc_mutex_lock ();
1400         matches = poppler_page_find_text (page, search->text);
1401         ev_document_doc_mutex_unlock ();
1402
1403         g_object_unref (page);
1404
1405         search->pages[search->search_page] = matches;
1406         ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
1407                                   search->search_page);
1408
1409         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
1410         search->search_page += 1;
1411         if (search->search_page == n_pages) {
1412                 /* wrap around */
1413                 search->search_page = 0;
1414         }
1415
1416         if (search->search_page != search->start_page) {
1417                 return TRUE;
1418         }
1419
1420         /* We're done. */
1421         search->idle = 0; /* will return FALSE to remove */
1422         return FALSE;
1423 }
1424
1425
1426 static PdfDocumentSearch *
1427 pdf_document_search_new (PdfDocument *pdf_document,
1428                          int          start_page,
1429                          const char  *text)
1430 {
1431         PdfDocumentSearch *search;
1432         int n_pages;
1433         int i;
1434
1435         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
1436
1437         search = g_new0 (PdfDocumentSearch, 1);
1438
1439         search->text = g_strdup (text);
1440         search->pages = g_new0 (GList *, n_pages);
1441         search->document = pdf_document;
1442
1443         /* We add at low priority so the progress bar repaints */
1444         search->idle = g_idle_add_full (G_PRIORITY_LOW,
1445                                         pdf_document_search_idle_callback,
1446                                         search,
1447                                         NULL);
1448
1449         search->start_page = start_page;
1450         search->search_page = start_page;
1451
1452         return search;
1453 }
1454
1455 static void
1456 pdf_document_find_begin (EvDocumentFind   *document,
1457                          int               page,
1458                          const char       *search_string,
1459                          gboolean          case_sensitive)
1460 {
1461         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1462
1463         /* FIXME handle case_sensitive (right now XPDF
1464          * code is always case insensitive for ASCII
1465          * and case sensitive for all other languaages)
1466          */
1467
1468         if (pdf_document->search &&
1469             strcmp (search_string, pdf_document->search->text) == 0)
1470                 return;
1471
1472         if (pdf_document->search)
1473                 pdf_document_search_free (pdf_document->search);
1474
1475         pdf_document->search = pdf_document_search_new (pdf_document,
1476                                                         page,
1477                                                         search_string);
1478 }
1479
1480 static int
1481 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1482 {
1483         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1484
1485         if (search) {
1486                 return g_list_length (search->pages[page]);
1487         } else {
1488                 return 0;
1489         }
1490 }
1491
1492 static gboolean
1493 pdf_document_find_get_result (EvDocumentFind *document_find,
1494                               int             page,
1495                               int             n_result,
1496                               EvRectangle    *rectangle)
1497 {
1498         PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1499         PdfDocumentSearch *search = pdf_document->search;
1500         PopplerPage *poppler_page;
1501         PopplerRectangle *r;
1502         double height;
1503
1504         if (search == NULL)
1505                 return FALSE;
1506
1507         r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1508                                                   n_result);
1509         if (r == NULL)
1510                 return FALSE;
1511
1512         poppler_page = poppler_document_get_page (pdf_document->document, page);
1513         poppler_page_get_size (poppler_page, NULL, &height);
1514         rectangle->x1 = r->x1;
1515         rectangle->y1 = height - r->y2;
1516         rectangle->x2 = r->x2;
1517         rectangle->y2 = height - r->y1;
1518         g_object_unref (poppler_page);
1519                 
1520         return TRUE;
1521 }
1522
1523 static int
1524 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1525                                     int             page)
1526 {
1527         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1528
1529         return search && search->pages[page] != NULL;
1530 }
1531
1532 static double
1533 pdf_document_find_get_progress (EvDocumentFind *document_find)
1534 {
1535         PdfDocumentSearch *search;
1536         int n_pages, pages_done;
1537
1538         search = PDF_DOCUMENT (document_find)->search;
1539
1540         if (search == NULL) {
1541                 return 0;
1542         }
1543
1544         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1545         if (search->search_page > search->start_page) {
1546                 pages_done = search->search_page - search->start_page + 1;
1547         } else if (search->search_page == search->start_page) {
1548                 pages_done = n_pages;
1549         } else {
1550                 pages_done = n_pages - search->start_page + search->search_page;
1551         }
1552
1553         return pages_done / (double) n_pages;
1554 }
1555
1556 static void
1557 pdf_document_find_cancel (EvDocumentFind *document)
1558 {
1559         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1560
1561         if (pdf_document->search) {
1562                 pdf_document_search_free (pdf_document->search);
1563                 pdf_document->search = NULL;
1564         }
1565 }
1566
1567 static void
1568 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1569 {
1570         iface->begin = pdf_document_find_begin;
1571         iface->get_n_results = pdf_document_find_get_n_results;
1572         iface->get_result = pdf_document_find_get_result;
1573         iface->page_has_results = pdf_document_find_page_has_results;
1574         iface->get_progress = pdf_document_find_get_progress;
1575         iface->cancel = pdf_document_find_cancel;
1576 }
1577
1578 static void
1579 pdf_print_context_free (PdfPrintContext *ctx)
1580 {
1581         if (!ctx)
1582                 return;
1583
1584 #ifdef HAVE_CAIRO_PRINT
1585         if (ctx->cr) {
1586                 cairo_destroy (ctx->cr);
1587                 ctx->cr = NULL;
1588         }
1589 #else
1590         if (ctx->ps_file) {
1591                 poppler_ps_file_free (ctx->ps_file);
1592                 ctx->ps_file = NULL;
1593         }
1594 #endif
1595         g_free (ctx);
1596 }
1597
1598 static void
1599 pdf_document_file_exporter_begin (EvFileExporter        *exporter,
1600                                   EvFileExporterContext *fc)
1601 {
1602         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1603         PdfPrintContext *ctx;
1604 #ifdef HAVE_CAIRO_PRINT
1605         gdouble width, height;
1606         cairo_surface_t *surface = NULL;
1607 #endif
1608         
1609         if (pdf_document->print_ctx)
1610                 pdf_print_context_free (pdf_document->print_ctx);
1611         pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1612         ctx = pdf_document->print_ctx;
1613         ctx->format = fc->format;
1614         
1615 #ifdef HAVE_CAIRO_PRINT
1616         ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1617
1618         ctx->paper_width = fc->paper_width;
1619         ctx->paper_height = fc->paper_height;
1620         
1621         switch (fc->pages_per_sheet) {
1622                 default:
1623                 case 1:
1624                         ctx->pages_x = 1;
1625                         ctx->pages_y = 1;
1626                         break;
1627                 case 2:
1628                         ctx->pages_x = 1;
1629                         ctx->pages_y = 2;
1630                         break;
1631                 case 4:
1632                         ctx->pages_x = 2;
1633                         ctx->pages_y = 2;
1634                         break;
1635                 case 6:
1636                         ctx->pages_x = 2;
1637                         ctx->pages_y = 3;
1638                         break;
1639                 case 9:
1640                         ctx->pages_x = 3;
1641                         ctx->pages_y = 3;
1642                         break;
1643                 case 16:
1644                         ctx->pages_x = 4;
1645                         ctx->pages_y = 4;
1646                         break;
1647         }
1648
1649         ctx->pages_printed = 0;
1650         
1651         switch (fc->format) {
1652                 case EV_FILE_FORMAT_PS:
1653 #ifdef HAVE_CAIRO_PS
1654                         surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1655 #endif
1656                         break;
1657                 case EV_FILE_FORMAT_PDF:
1658 #ifdef HAVE_CAIRO_PDF
1659                         surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1660 #endif
1661                         break;
1662                 default:
1663                         g_assert_not_reached ();
1664         }
1665
1666         ctx->cr = cairo_create (surface);
1667         cairo_surface_destroy (surface);
1668
1669 #else /* HAVE_CAIRO_PRINT */
1670         if (ctx->format == EV_FILE_FORMAT_PS) {
1671                 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1672                                                     fc->filename, fc->first_page,
1673                                                     fc->last_page - fc->first_page + 1);
1674                 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1675                 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1676         }
1677 #endif /* HAVE_CAIRO_PRINT */
1678 }
1679
1680 static void
1681 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1682 {
1683         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1684         PdfPrintContext *ctx = pdf_document->print_ctx;
1685         
1686         g_return_if_fail (pdf_document->print_ctx != NULL);
1687
1688         ctx->pages_printed = 0;
1689         
1690 #ifdef HAVE_CAIRO_PRINT
1691         if (ctx->paper_width > ctx->paper_height) {
1692                 if (ctx->format == EV_FILE_FORMAT_PS) {
1693                         cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1694                                                    ctx->paper_height,
1695                                                    ctx->paper_width);
1696                 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1697                         cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1698                                                     ctx->paper_height,
1699                                                     ctx->paper_width);
1700                 }
1701         }
1702 #endif /* HAVE_CAIRO_PRINT */
1703 }
1704
1705 static void
1706 pdf_document_file_exporter_do_page (EvFileExporter  *exporter,
1707                                     EvRenderContext *rc)
1708 {
1709         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1710         PdfPrintContext *ctx = pdf_document->print_ctx;
1711         PopplerPage *poppler_page;
1712 #ifdef HAVE_CAIRO_PRINT
1713         gdouble  page_width, page_height;
1714         gint     x, y;
1715         gboolean rotate, landscape;
1716         gdouble  width, height;
1717         gdouble  pwidth, pheight;
1718         gdouble  xscale, yscale;
1719 #endif
1720
1721         g_return_if_fail (pdf_document->print_ctx != NULL);
1722
1723         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1724         
1725 #ifdef HAVE_CAIRO_PRINT
1726         x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1727         y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1728         poppler_page_get_size (poppler_page, &page_width, &page_height);
1729
1730         if (page_width > page_height && page_width > ctx->paper_width) {
1731                 rotate = TRUE;
1732         } else {
1733                 rotate = FALSE;
1734         }
1735
1736         landscape = (ctx->paper_width > ctx->paper_height);
1737
1738         /* Use always portrait mode and rotate when necessary */
1739         if (ctx->paper_width > ctx->paper_height) {
1740                 width = ctx->paper_height;
1741                 height = ctx->paper_width;
1742                 rotate = !rotate;
1743         } else {
1744                 width = ctx->paper_width;
1745                 height = ctx->paper_height;
1746         }
1747
1748         if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1749                 rotate = !rotate;
1750         }       
1751
1752         if (rotate) {
1753                 gint tmp1;
1754                 gdouble tmp2;
1755
1756                 tmp1 = x;
1757                 x = y;
1758                 y = tmp1;
1759
1760                 tmp2 = page_width;
1761                 page_width = page_height;
1762                 page_height = tmp2;
1763         }
1764
1765         pwidth = width / ctx->pages_x;
1766         pheight = height / ctx->pages_y;
1767
1768         if ((page_width > pwidth || page_height > pheight) ||
1769             (page_width < pwidth && page_height < pheight)) {
1770                 xscale = pwidth / page_width;
1771                 yscale = pheight / page_height;
1772                 
1773                 if (yscale < xscale) {
1774                         xscale = yscale;
1775                 } else {
1776                         yscale = xscale;
1777                 }
1778                 
1779         } else {        
1780                 xscale = yscale = 1;
1781         }
1782
1783         /* TODO: center */
1784
1785         cairo_save (ctx->cr);
1786         if (rotate) {
1787                 cairo_matrix_t matrix;
1788                 
1789                 cairo_translate (ctx->cr, width, 0);
1790                 cairo_matrix_init (&matrix,
1791                                    0,  1,
1792                                    -1,  0,
1793                                    0,  0);
1794                 cairo_transform (ctx->cr, &matrix);
1795         }
1796         
1797         cairo_translate (ctx->cr,
1798                          x * (rotate ? pheight : pwidth),
1799                          y * (rotate ? pwidth : pheight));
1800         cairo_scale (ctx->cr, xscale, yscale);
1801
1802 #ifdef HAVE_POPPLER_PAGE_RENDER_FOR_PRINTING
1803         poppler_page_render_for_printing (poppler_page, ctx->cr);
1804 #else
1805 #ifdef HAVE_POPPLER_PAGE_RENDER
1806         poppler_page_render (poppler_page, ctx->cr);
1807 #endif
1808 #endif
1809
1810         ctx->pages_printed++;
1811                         
1812         cairo_restore (ctx->cr);
1813 #else /* HAVE_CAIRO_PRINT */
1814         if (ctx->format == EV_FILE_FORMAT_PS)
1815                 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1816 #endif /* HAVE_CAIRO_PRINT */
1817         
1818         g_object_unref (poppler_page);
1819 }
1820
1821 static void
1822 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1823 {
1824         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1825         PdfPrintContext *ctx = pdf_document->print_ctx;
1826         
1827         g_return_if_fail (pdf_document->print_ctx != NULL);
1828
1829 #ifdef HAVE_CAIRO_PRINT
1830         cairo_show_page (ctx->cr);
1831 #endif
1832 }
1833
1834 static void
1835 pdf_document_file_exporter_end (EvFileExporter *exporter)
1836 {
1837         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1838
1839         pdf_print_context_free (pdf_document->print_ctx);
1840         pdf_document->print_ctx = NULL;
1841 }
1842
1843 static EvFileExporterCapabilities
1844 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1845 {
1846         return  (EvFileExporterCapabilities) (
1847                 EV_FILE_EXPORTER_CAN_PAGE_SET |
1848                 EV_FILE_EXPORTER_CAN_COPIES |
1849                 EV_FILE_EXPORTER_CAN_COLLATE |
1850                 EV_FILE_EXPORTER_CAN_REVERSE |
1851                 EV_FILE_EXPORTER_CAN_SCALE |
1852 #ifdef HAVE_CAIRO_PRINT
1853 #ifdef HAVE_POPPLER_PAGE_RENDER
1854 #if GTK_CHECK_VERSION (2, 11, 1)
1855                 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1856 #endif
1857 #endif
1858 #endif
1859                 
1860 #ifdef HAVE_CAIRO_PDF
1861 #ifdef HAVE_POPPLER_PAGE_RENDER
1862                 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1863 #endif
1864 #endif
1865                 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1866 }
1867
1868 static void
1869 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1870 {
1871         iface->begin = pdf_document_file_exporter_begin;
1872         iface->begin_page = pdf_document_file_exporter_begin_page;
1873         iface->do_page = pdf_document_file_exporter_do_page;
1874         iface->end_page = pdf_document_file_exporter_end_page;
1875         iface->end = pdf_document_file_exporter_end;
1876         iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1877 }
1878
1879 static void
1880 pdf_selection_render_selection (EvSelection      *selection,
1881                                 EvRenderContext  *rc,
1882                                 cairo_surface_t **surface,
1883                                 EvRectangle      *points,
1884                                 EvRectangle      *old_points,
1885                                 EvSelectionStyle  style,
1886                                 GdkColor         *text,
1887                                 GdkColor         *base)
1888 {
1889         PdfDocument *pdf_document;
1890         double width_points, height_points;
1891         gint width, height;
1892
1893         pdf_document = PDF_DOCUMENT (selection);
1894         set_rc_data (pdf_document, rc);
1895
1896         poppler_page_get_size (POPPLER_PAGE (rc->data),
1897                                &width_points, &height_points);
1898         width = (int) ((width_points * rc->scale) + 0.5);
1899         height = (int) ((height_points * rc->scale) + 0.5);
1900
1901 #ifdef HAVE_POPPLER_PAGE_RENDER
1902         cairo_t *cr;
1903         
1904 #if POPPLER_MAJOR_VERSION <= 6 || (POPPLER_MAJOR_VERSION == 7 && POPPLER_MINOR_VERSION < 2)
1905         GdkColor text_color, base_color;
1906
1907         text_color = *text;
1908         base_color = *base;
1909 #else
1910         PopplerColor text_color, base_color;
1911         
1912         text_color.red = text->red;
1913         text_color.green = text->green;
1914         text_color.blue = text->blue;
1915
1916         base_color.red = base->red;
1917         base_color.green = base->green;
1918         base_color.blue = base->blue;
1919 #endif
1920
1921         if (*surface == NULL) {
1922                 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1923                                                        width, height);
1924                 
1925         }
1926
1927         cr = cairo_create (*surface);
1928         cairo_scale (cr, rc->scale, rc->scale);
1929         cairo_surface_set_device_offset (*surface, 0, 0);
1930         memset (cairo_image_surface_get_data (*surface), 0x00,
1931                 cairo_image_surface_get_height (*surface) *
1932                 cairo_image_surface_get_stride (*surface));
1933         poppler_page_render_selection (POPPLER_PAGE (rc->data),
1934                                        cr,
1935                                        (PopplerRectangle *)points,
1936                                        (PopplerRectangle *)old_points,
1937                                        (PopplerSelectionStyle)style,
1938                                        &text_color,
1939                                        &base_color);
1940         cairo_destroy (cr);
1941 #else /* HAVE_POPPLER_PAGE_RENDER */
1942         GdkPixbuf *pixbuf;
1943         
1944         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1945                                  TRUE, 8,
1946                                  width, height);
1947
1948         poppler_page_render_selection_to_pixbuf (POPPLER_PAGE (rc->data),
1949                                                  rc->scale, rc->rotation, pixbuf,
1950                                                  (PopplerRectangle *)points,
1951                                                  (PopplerRectangle *)old_points,
1952                                                  (PopplerSelectionStyle)style,
1953                                                  text,
1954                                                  base);
1955         if (*surface)
1956                 cairo_surface_destroy (*surface);
1957         *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1958         g_object_unref (pixbuf);
1959 #endif /* HAVE_POPPLER_PAGE_RENDER */
1960 }
1961
1962 static gchar *
1963 pdf_selection_get_selected_text (EvSelection     *selection,
1964                                  EvRenderContext *rc,
1965                                  EvSelectionStyle style,
1966                                  EvRectangle     *points)
1967 {
1968         PdfDocument *pdf_document = PDF_DOCUMENT (selection);
1969         PopplerPage *poppler_page;
1970         PopplerRectangle r;
1971         double height;
1972         char *retval;
1973         
1974         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1975         g_return_val_if_fail (poppler_page != NULL, NULL);
1976
1977         poppler_page_get_size (poppler_page, NULL, &height);
1978         r.x1 = points->x1;
1979         r.y1 = height - points->y2;
1980         r.x2 = points->x2;
1981         r.y2 = height - points->y1;
1982
1983         retval = poppler_page_get_text (poppler_page,
1984                                         (PopplerSelectionStyle)style,
1985                                         &r);
1986
1987         g_object_unref (poppler_page);
1988
1989         return retval;
1990 }
1991
1992 static GdkRegion *
1993 create_gdk_region_from_poppler_region (GList *region)
1994 {
1995         GList *l;
1996         GdkRegion *retval;
1997         
1998         retval = gdk_region_new ();
1999         
2000         for (l = region; l; l = g_list_next (l)) {
2001                 PopplerRectangle *rectangle;
2002                 GdkRectangle      rect;
2003                 
2004                 rectangle = (PopplerRectangle *)l->data;
2005                 
2006                 rect.x = (gint) rectangle->x1;
2007                 rect.y = (gint) rectangle->y1;
2008                 rect.width  = (gint) (rectangle->x2 - rectangle->x1);
2009                 rect.height = (gint) (rectangle->y2 - rectangle->y1);
2010                 gdk_region_union_with_rect (retval, &rect);
2011                 
2012                 poppler_rectangle_free (rectangle);
2013         }
2014
2015         return retval;
2016 }
2017
2018 static GdkRegion *
2019 pdf_selection_get_selection_region (EvSelection     *selection,
2020                                     EvRenderContext *rc,
2021                                     EvSelectionStyle style,
2022                                     EvRectangle     *points)
2023 {
2024         PdfDocument *pdf_document;
2025         GdkRegion   *retval;
2026
2027         pdf_document = PDF_DOCUMENT (selection);
2028
2029         set_rc_data (pdf_document, rc);
2030         
2031 #if POPPLER_MAJOR_VERSION <= 6 || (POPPLER_MAJOR_VERSION == 7 && POPPLER_MINOR_VERSION < 2)
2032         retval = poppler_page_get_selection_region (POPPLER_PAGE (rc->data),
2033                                                     rc->scale,
2034                                                     (PopplerSelectionStyle)style,
2035                                                     (PopplerRectangle *) points);
2036 #else
2037         GList *region;
2038         
2039         region = poppler_page_get_selection_region (POPPLER_PAGE (rc->data),
2040                                                     rc->scale,
2041                                                     (PopplerSelectionStyle)style,
2042                                                     (PopplerRectangle *) points);
2043         retval = create_gdk_region_from_poppler_region (region);
2044         g_list_free (region);
2045 #endif
2046         
2047         return retval;
2048 }
2049
2050 static GdkRegion *
2051 pdf_selection_get_selection_map (EvSelection     *selection,
2052                                  EvRenderContext *rc)
2053 {
2054         PdfDocument *pdf_document;
2055         PopplerPage *poppler_page;
2056         PopplerRectangle points;
2057         GList *region;
2058         GdkRegion *retval;
2059
2060         pdf_document = PDF_DOCUMENT (selection);
2061         poppler_page = poppler_document_get_page (pdf_document->document,
2062                                                   rc->page);
2063
2064         points.x1 = 0.0;
2065         points.y1 = 0.0;
2066         poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
2067         
2068 #if POPPLER_MAJOR_VERSION <= 6 || (POPPLER_MAJOR_VERSION == 7 && POPPLER_MINOR_VERSION < 2)
2069         retval = poppler_page_get_selection_region (poppler_page, 1.0,
2070                                                     POPPLER_SELECTION_GLYPH,
2071                                                     &points);
2072 #else
2073         region = poppler_page_get_selection_region (poppler_page, 1.0,
2074                                                     POPPLER_SELECTION_GLYPH,
2075                                                     &points);
2076         retval = create_gdk_region_from_poppler_region (region);
2077         g_list_free (region);
2078 #endif
2079         g_object_unref (poppler_page);
2080
2081         return retval;
2082 }
2083
2084 static void
2085 pdf_selection_iface_init (EvSelectionIface *iface)
2086 {
2087         iface->render_selection = pdf_selection_render_selection;
2088         iface->get_selected_text = pdf_selection_get_selected_text;
2089         iface->get_selection_region = pdf_selection_get_selection_region;
2090         iface->get_selection_map = pdf_selection_get_selection_map;
2091 }
2092
2093 /* Page Transitions */
2094 static gdouble
2095 pdf_document_get_page_duration (EvDocumentTransition *trans,
2096                                 gint                  page)
2097 {
2098         PdfDocument *pdf_document;
2099         PopplerPage *poppler_page;
2100         gdouble      duration = -1;
2101
2102         pdf_document = PDF_DOCUMENT (trans);
2103         poppler_page = poppler_document_get_page (pdf_document->document, page);
2104         if (!poppler_page)
2105                 return -1;
2106
2107         duration = poppler_page_get_duration (poppler_page);
2108         g_object_unref (poppler_page);
2109
2110         return duration;
2111 }
2112
2113 static EvTransitionEffect *
2114 pdf_document_get_effect (EvDocumentTransition *trans,
2115                          gint                  page)
2116 {
2117         PdfDocument            *pdf_document;
2118         PopplerPage            *poppler_page;
2119         PopplerPageTransition  *page_transition;
2120         EvTransitionEffect     *effect;
2121
2122         pdf_document = PDF_DOCUMENT (trans);
2123         poppler_page = poppler_document_get_page (pdf_document->document, page);
2124
2125         if (!poppler_page)
2126                 return NULL;
2127
2128         page_transition = poppler_page_get_transition (poppler_page);
2129
2130         if (!page_transition) {
2131                 g_object_unref (poppler_page);
2132                 return NULL;
2133         }
2134
2135         /* enums in PopplerPageTransition match the EvTransitionEffect ones */
2136         effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
2137                                            "alignment", page_transition->alignment,
2138                                            "direction", page_transition->direction,
2139                                            "duration", page_transition->duration,
2140                                            "angle", page_transition->angle,
2141                                            "scale", page_transition->scale,
2142                                            "rectangular", page_transition->rectangular,
2143                                            NULL);
2144
2145         poppler_page_transition_free (page_transition);
2146         g_object_unref (poppler_page);
2147
2148         return effect;
2149 }
2150
2151 static void
2152 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
2153 {
2154         iface->get_page_duration = pdf_document_get_page_duration;
2155         iface->get_effect = pdf_document_get_effect;
2156 }
2157
2158 /* Forms */
2159 static void
2160 pdf_document_get_crop_box (EvDocument  *document, 
2161                            int          page, 
2162                            EvRectangle *rect)
2163 {
2164         PdfDocument *pdf_document;
2165         PopplerPage *poppler_page;
2166         PopplerRectangle poppler_rect;
2167
2168         pdf_document = PDF_DOCUMENT (document);
2169         poppler_page = poppler_document_get_page (pdf_document->document, page);
2170         poppler_page_get_crop_box (poppler_page, &poppler_rect);
2171         rect->x1 = poppler_rect.x1;
2172         rect->x2 = poppler_rect.x2;
2173         rect->y1 = poppler_rect.y1;
2174         rect->y2 = poppler_rect.y2;
2175 }
2176
2177 static EvFormField *
2178 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
2179 {
2180         EvFormField *ev_field = NULL;
2181         gint         id;
2182         gdouble      font_size;
2183         gboolean     is_read_only;
2184
2185         id = poppler_form_field_get_id (poppler_field);
2186         font_size = poppler_form_field_get_font_size (poppler_field);
2187         is_read_only = poppler_form_field_is_read_only (poppler_field);
2188
2189         switch (poppler_form_field_get_field_type (poppler_field)) {
2190                 case POPPLER_FORM_FIELD_TEXT: {
2191                         EvFormFieldText    *field_text;
2192                         EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2193
2194                         switch (poppler_form_field_text_get_text_type (poppler_field)) {
2195                                 case POPPLER_FORM_TEXT_NORMAL:
2196                                         ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2197                                         break;
2198                                 case POPPLER_FORM_TEXT_MULTILINE:
2199                                         ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2200                                         break;
2201                                 case POPPLER_FORM_TEXT_FILE_SELECT:
2202                                         ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2203                                         break;
2204                         }
2205                         
2206                         ev_field = ev_form_field_text_new (id, ev_text_type);
2207                         field_text = EV_FORM_FIELD_TEXT (ev_field);
2208
2209                         field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2210                         field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2211                         field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2212                         field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2213                         field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2214                         field_text->text = poppler_form_field_text_get_text (poppler_field);
2215
2216                 }
2217                         break;
2218                 case POPPLER_FORM_FIELD_BUTTON: {
2219                         EvFormFieldButton    *field_button;
2220                         EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2221
2222                         switch (poppler_form_field_button_get_button_type (poppler_field)) {
2223                                 case POPPLER_FORM_BUTTON_PUSH:
2224                                         ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2225                                         break;
2226                                 case POPPLER_FORM_BUTTON_CHECK:
2227                                         ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2228                                         break;
2229                                 case POPPLER_FORM_BUTTON_RADIO:
2230                                         ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2231                                         break;
2232                         }
2233
2234                         ev_field = ev_form_field_button_new (id, ev_button_type);
2235                         field_button = EV_FORM_FIELD_BUTTON (ev_field);
2236                         
2237                         field_button->state = poppler_form_field_button_get_state (poppler_field);
2238                 }
2239                         break;
2240                 case POPPLER_FORM_FIELD_CHOICE: {
2241                         EvFormFieldChoice    *field_choice;
2242                         EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2243
2244                         switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2245                                 case POPPLER_FORM_CHOICE_COMBO:
2246                                         ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2247                                         break;
2248                                 case EV_FORM_FIELD_CHOICE_LIST:
2249                                         ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2250                                         break;
2251                         }
2252
2253                         ev_field = ev_form_field_choice_new (id, ev_choice_type);
2254                         field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2255
2256                         field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2257                         field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2258                         field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2259                         field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2260
2261                         /* TODO: we need poppler_form_field_choice_get_selected_items in poppler 
2262                         field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2263                         if (field_choice->is_editable)
2264                                 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2265                 }
2266                         break;
2267                 case POPPLER_FORM_FIELD_SIGNATURE:
2268                         /* TODO */
2269                         ev_field = ev_form_field_signature_new (id);
2270                         break;
2271                 case POPPLER_FORM_FIELD_UNKNOWN:
2272                         return NULL;
2273         }
2274
2275         ev_field->font_size = font_size;
2276         ev_field->is_read_only = is_read_only;
2277
2278         return ev_field;
2279 }
2280
2281 static GList *
2282 pdf_document_forms_get_form_fields (EvDocumentForms *document, 
2283                                     gint             page)
2284 {
2285         PdfDocument *pdf_document;
2286         PopplerPage *poppler_page;
2287         GList *retval = NULL;
2288         GList *fields;
2289         GList *list;
2290         double height;
2291
2292         pdf_document = PDF_DOCUMENT (document);
2293         poppler_page = poppler_document_get_page (pdf_document->document, page);
2294         fields = poppler_page_get_form_field_mapping (poppler_page);
2295         poppler_page_get_size (poppler_page, NULL, &height);
2296
2297         for (list = fields; list; list = list->next) {
2298                 PopplerFormFieldMapping *mapping;
2299                 EvFormFieldMapping *field_mapping;
2300                 EvFormField *ev_field;
2301
2302                 mapping = (PopplerFormFieldMapping *)list->data;
2303
2304                 ev_field = ev_form_field_from_poppler_field (mapping->field);
2305                 if (!ev_field)
2306                         continue;
2307
2308                 field_mapping = g_new0 (EvFormFieldMapping, 1);
2309                 field_mapping->x1 = mapping->area.x1;
2310                 field_mapping->x2 = mapping->area.x2;
2311                 field_mapping->y1 = height - mapping->area.y2;
2312                 field_mapping->y2 = height - mapping->area.y1;
2313                 field_mapping->field = ev_field;
2314                 field_mapping->field->page = page;
2315                 
2316                 retval = g_list_prepend (retval, field_mapping);
2317         }
2318         
2319         poppler_page_free_form_field_mapping (fields);
2320         g_object_unref (poppler_page);
2321
2322         return g_list_reverse (retval);
2323 }
2324
2325 static gchar *
2326 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2327                                              EvFormField     *field)
2328         
2329 {
2330         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2331         PopplerFormField *poppler_field;
2332         gchar *text;
2333
2334         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2335         if (!poppler_field)
2336                 return NULL;
2337         
2338         text = poppler_form_field_text_get_text (poppler_field);
2339         g_object_unref (poppler_field);
2340
2341         return text;
2342 }
2343
2344 static void
2345 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document, 
2346                                              EvFormField     *field,
2347                                              const gchar     *text)
2348 {
2349         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2350         PopplerFormField *poppler_field;
2351
2352         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2353         if (!poppler_field)
2354                 return;
2355         poppler_form_field_text_set_text (poppler_field, text);
2356         g_object_unref (poppler_field);
2357 }
2358
2359 static void
2360 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document, 
2361                                                 EvFormField     *field,
2362                                                 gboolean         state)
2363 {
2364         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2365         PopplerFormField *poppler_field;
2366
2367         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2368         if (!poppler_field)
2369                 return;
2370         
2371         poppler_form_field_button_set_state (poppler_field, state);
2372         g_object_unref (poppler_field);
2373 }
2374
2375 static gboolean
2376 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document, 
2377                                                 EvFormField     *field)
2378 {
2379         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2380         PopplerFormField *poppler_field;
2381         gboolean state;
2382
2383         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2384         if (!poppler_field)
2385                 return FALSE;
2386
2387         state = poppler_form_field_button_get_state (poppler_field);
2388         g_object_unref (poppler_field);
2389
2390         return state;
2391 }
2392
2393 static gchar *
2394 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document, 
2395                                                EvFormField     *field,
2396                                                gint             index)
2397 {
2398         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2399         PopplerFormField *poppler_field;
2400         gchar *text;
2401
2402         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2403         if (!poppler_field)
2404                 return NULL;
2405
2406         text = poppler_form_field_choice_get_item (poppler_field, index);
2407         g_object_unref (poppler_field);
2408
2409         return text;
2410 }
2411
2412 static int
2413 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document, 
2414                                                   EvFormField     *field)
2415 {
2416         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2417         PopplerFormField *poppler_field;
2418         gint n_items;
2419
2420         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2421         if (!poppler_field)
2422                 return -1;
2423         
2424         n_items = poppler_form_field_choice_get_n_items (poppler_field);
2425         g_object_unref (poppler_field);
2426
2427         return n_items;
2428 }
2429
2430 static gboolean
2431 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document, 
2432                                                        EvFormField     *field,
2433                                                        gint             index)
2434 {
2435         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2436         PopplerFormField *poppler_field;
2437         gboolean selected;
2438
2439         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2440         if (!poppler_field)
2441                 return FALSE;
2442
2443         selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2444         g_object_unref (poppler_field);
2445
2446         return selected;
2447 }
2448
2449 static void
2450 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document, 
2451                                                   EvFormField     *field,
2452                                                   gint             index)
2453 {
2454         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2455         PopplerFormField *poppler_field;
2456
2457         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2458         if (!poppler_field)
2459                 return;
2460
2461         poppler_form_field_choice_select_item (poppler_field, index);
2462         g_object_unref (poppler_field);
2463 }
2464
2465 static void
2466 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document, 
2467                                                   EvFormField     *field,
2468                                                   gint             index)
2469 {
2470         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2471         PopplerFormField *poppler_field;
2472
2473         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2474         if (!poppler_field)
2475                 return;
2476
2477         poppler_form_field_choice_toggle_item (poppler_field, index);
2478         g_object_unref (poppler_field);
2479 }
2480
2481 static void
2482 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document, 
2483                                                    EvFormField     *field)
2484 {
2485         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2486         PopplerFormField *poppler_field;
2487
2488         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2489         if (!poppler_field)
2490                 return;
2491         
2492         poppler_form_field_choice_unselect_all (poppler_field);
2493         g_object_unref (poppler_field);
2494 }
2495
2496 static void
2497 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2498                                                EvFormField     *field,
2499                                                const gchar     *text)
2500 {
2501         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2502         PopplerFormField *poppler_field;
2503
2504         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2505         if (!poppler_field)
2506                 return;
2507         
2508         poppler_form_field_choice_set_text (poppler_field, text);
2509         g_object_unref (poppler_field);
2510 }
2511
2512 static gchar *
2513 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2514                                                EvFormField     *field)
2515 {
2516         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2517         PopplerFormField *poppler_field;
2518         gchar *text;
2519
2520         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2521         if (!poppler_field)
2522                 return NULL;
2523
2524         text = poppler_form_field_choice_get_text (poppler_field);
2525         g_object_unref (poppler_field);
2526
2527         return text;
2528 }
2529
2530 static void
2531 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2532 {
2533         iface->get_form_fields = pdf_document_forms_get_form_fields;
2534         iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2535         iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2536         iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2537         iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2538         iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2539         iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2540         iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2541         iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2542         iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2543         iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2544         iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2545         iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2546 }
2547