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