]> www.fi.muni.cz Git - evince.git/blob - backend/pdf/ev-poppler.cc
[pdf] Remove unneeded #ifdefs
[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         PdfDocument     *pdf_document;
1240         PopplerPage     *poppler_page;
1241         cairo_surface_t *surface;
1242         GdkPixbuf       *retval = NULL;
1243
1244         pdf_document = PDF_DOCUMENT (document_images);
1245         poppler_page = poppler_document_get_page (pdf_document->document,
1246                                                   ev_image_get_page (image));
1247
1248         surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1249         if (surface) {
1250                 retval = ev_document_misc_pixbuf_from_surface (surface);
1251                 cairo_surface_destroy (surface);
1252         }
1253
1254         g_object_unref (poppler_page);
1255
1256         return retval;
1257 }
1258
1259 static void
1260 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1261 {
1262         iface->get_image_mapping = pdf_document_images_get_image_mapping;
1263         iface->get_image = pdf_document_images_get_image;
1264 }
1265
1266 static GdkPixbuf *
1267 make_thumbnail_for_page (PopplerPage     *poppler_page,
1268                          EvRenderContext *rc,
1269                          gint             width,
1270                          gint             height)
1271 {
1272         GdkPixbuf *pixbuf;
1273
1274 #ifdef POPPLER_WITH_GDK
1275         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1276                                  width, height);
1277         gdk_pixbuf_fill (pixbuf, 0xffffffff);
1278
1279         ev_document_fc_mutex_lock ();
1280         poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1281                                        width, height,
1282                                        rc->scale, rc->rotation, pixbuf);
1283         ev_document_fc_mutex_unlock ();
1284 #else
1285         cairo_surface_t *surface;
1286
1287         ev_document_fc_mutex_lock ();
1288         surface = pdf_page_render (poppler_page, width, height, rc);
1289         ev_document_fc_mutex_unlock ();
1290         
1291         pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1292         cairo_surface_destroy (surface);
1293 #endif /* POPPLER_WITH_GDK */
1294
1295         return pixbuf;
1296 }
1297
1298 static GdkPixbuf *
1299 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1300                                        EvRenderContext      *rc, 
1301                                        gboolean              border)
1302 {
1303         PdfDocument *pdf_document = PDF_DOCUMENT (document_thumbnails);
1304         PopplerPage *poppler_page;
1305         GdkPixbuf *pixbuf = NULL;
1306         GdkPixbuf *border_pixbuf;
1307         gint width, height;
1308
1309         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1310
1311         pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1312                                                 rc, &width, &height);
1313         
1314 #ifdef POPPLER_WITH_GDK
1315         pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1316 #else
1317         cairo_surface_t *surface;
1318         
1319         surface = poppler_page_get_thumbnail (poppler_page);
1320         if (surface) {
1321                 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1322                 cairo_surface_destroy (surface);
1323         }
1324 #endif /* POPPLER_WITH_GDK */
1325
1326         if (pixbuf != NULL) {
1327                 int thumb_width = (rc->rotation == 90 || rc->rotation == 270) ?
1328                         gdk_pixbuf_get_height (pixbuf) :
1329                         gdk_pixbuf_get_width (pixbuf);
1330
1331                 if (thumb_width == width) {
1332                         GdkPixbuf *rotated_pixbuf;
1333
1334                         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1335                                                                    (GdkPixbufRotation) (360 - rc->rotation));
1336                         g_object_unref (pixbuf);
1337                         pixbuf = rotated_pixbuf;
1338                 } else {
1339                         /* The provided thumbnail has a different size */
1340                         g_object_unref (pixbuf);
1341                         pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1342                 }
1343         } else {
1344                 /* There is no provided thumbnail. We need to make one. */
1345                 pixbuf = make_thumbnail_for_page (poppler_page, rc, width, height);
1346         }
1347
1348         if (border && pixbuf) {
1349                 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1350                 g_object_unref (pixbuf);
1351                 pixbuf = border_pixbuf;
1352         }               
1353
1354         return pixbuf;
1355 }
1356
1357 static void
1358 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1359                                         EvRenderContext      *rc,
1360                                         gint                 *width,
1361                                         gint                 *height)
1362 {
1363         double page_width, page_height;
1364         
1365         poppler_page_get_size (POPPLER_PAGE (rc->page->backend_page),
1366                                &page_width, &page_height);
1367         
1368         *width = (gint) MAX (page_width * rc->scale, 1);
1369         *height = (gint) MAX (page_height * rc->scale, 1);
1370         
1371         if (rc->rotation == 90 || rc->rotation == 270) {
1372                 gint  temp;
1373
1374                 temp = *width;
1375                 *width = *height;
1376                 *height = temp;
1377         }
1378 }
1379
1380 static void
1381 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1382 {
1383         iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1384         iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1385 }
1386
1387
1388 static GList *
1389 pdf_document_find_find_text (EvDocumentFind *document_find,
1390                              EvPage         *page,
1391                              const gchar    *text,
1392                              gboolean        case_sensitive)
1393 {
1394         GList *matches, *l;
1395         PopplerPage *poppler_page;
1396         gdouble height;
1397         
1398         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
1399         g_return_val_if_fail (text != NULL, NULL);
1400
1401         poppler_page = POPPLER_PAGE (page->backend_page);
1402         
1403         matches = poppler_page_find_text (poppler_page, text);
1404         if (!matches)
1405                 return NULL;
1406
1407         poppler_page_get_size (poppler_page, NULL, &height);
1408         for (l = matches; l && l->data; l = g_list_next (l)) {
1409                 PopplerRectangle *rect = (PopplerRectangle *)l->data;
1410                 gdouble           tmp;
1411
1412                 tmp = rect->y1;
1413                 rect->y1 = height - rect->y2;
1414                 rect->y2 = height - tmp;
1415         }
1416         
1417         return matches;
1418 }
1419
1420 static void
1421 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1422 {
1423         iface->find_text = pdf_document_find_find_text;
1424 }
1425
1426 static void
1427 pdf_print_context_free (PdfPrintContext *ctx)
1428 {
1429         if (!ctx)
1430                 return;
1431
1432 #ifdef HAVE_CAIRO_PRINT
1433         if (ctx->cr) {
1434                 cairo_destroy (ctx->cr);
1435                 ctx->cr = NULL;
1436         }
1437 #else
1438         if (ctx->ps_file) {
1439                 poppler_ps_file_free (ctx->ps_file);
1440                 ctx->ps_file = NULL;
1441         }
1442 #endif
1443         g_free (ctx);
1444 }
1445
1446 static void
1447 pdf_document_file_exporter_begin (EvFileExporter        *exporter,
1448                                   EvFileExporterContext *fc)
1449 {
1450         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1451         PdfPrintContext *ctx;
1452 #ifdef HAVE_CAIRO_PRINT
1453         gdouble width, height;
1454         cairo_surface_t *surface = NULL;
1455 #endif
1456         
1457         if (pdf_document->print_ctx)
1458                 pdf_print_context_free (pdf_document->print_ctx);
1459         pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1460         ctx = pdf_document->print_ctx;
1461         ctx->format = fc->format;
1462         
1463 #ifdef HAVE_CAIRO_PRINT
1464         ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1465
1466         ctx->paper_width = fc->paper_width;
1467         ctx->paper_height = fc->paper_height;
1468         
1469         switch (fc->pages_per_sheet) {
1470                 default:
1471                 case 1:
1472                         ctx->pages_x = 1;
1473                         ctx->pages_y = 1;
1474                         break;
1475                 case 2:
1476                         ctx->pages_x = 1;
1477                         ctx->pages_y = 2;
1478                         break;
1479                 case 4:
1480                         ctx->pages_x = 2;
1481                         ctx->pages_y = 2;
1482                         break;
1483                 case 6:
1484                         ctx->pages_x = 2;
1485                         ctx->pages_y = 3;
1486                         break;
1487                 case 9:
1488                         ctx->pages_x = 3;
1489                         ctx->pages_y = 3;
1490                         break;
1491                 case 16:
1492                         ctx->pages_x = 4;
1493                         ctx->pages_y = 4;
1494                         break;
1495         }
1496
1497         ctx->pages_printed = 0;
1498         
1499         switch (fc->format) {
1500                 case EV_FILE_FORMAT_PS:
1501 #ifdef HAVE_CAIRO_PS
1502                         surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1503 #endif
1504                         break;
1505                 case EV_FILE_FORMAT_PDF:
1506 #ifdef HAVE_CAIRO_PDF
1507                         surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1508 #endif
1509                         break;
1510                 default:
1511                         g_assert_not_reached ();
1512         }
1513
1514         ctx->cr = cairo_create (surface);
1515         cairo_surface_destroy (surface);
1516
1517 #else /* HAVE_CAIRO_PRINT */
1518         if (ctx->format == EV_FILE_FORMAT_PS) {
1519                 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1520                                                     fc->filename, fc->first_page,
1521                                                     fc->last_page - fc->first_page + 1);
1522                 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1523                 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1524         }
1525 #endif /* HAVE_CAIRO_PRINT */
1526 }
1527
1528 static void
1529 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1530 {
1531         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1532         PdfPrintContext *ctx = pdf_document->print_ctx;
1533         
1534         g_return_if_fail (pdf_document->print_ctx != NULL);
1535
1536         ctx->pages_printed = 0;
1537         
1538 #ifdef HAVE_CAIRO_PRINT
1539         if (ctx->paper_width > ctx->paper_height) {
1540                 if (ctx->format == EV_FILE_FORMAT_PS) {
1541                         cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1542                                                    ctx->paper_height,
1543                                                    ctx->paper_width);
1544                 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1545                         cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1546                                                     ctx->paper_height,
1547                                                     ctx->paper_width);
1548                 }
1549         }
1550 #endif /* HAVE_CAIRO_PRINT */
1551 }
1552
1553 static void
1554 pdf_document_file_exporter_do_page (EvFileExporter  *exporter,
1555                                     EvRenderContext *rc)
1556 {
1557         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1558         PdfPrintContext *ctx = pdf_document->print_ctx;
1559         PopplerPage *poppler_page;
1560 #ifdef HAVE_CAIRO_PRINT
1561         gdouble  page_width, page_height;
1562         gint     x, y;
1563         gboolean rotate;
1564         gdouble  width, height;
1565         gdouble  pwidth, pheight;
1566         gdouble  xscale, yscale;
1567 #endif
1568
1569         g_return_if_fail (pdf_document->print_ctx != NULL);
1570
1571         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1572         
1573 #ifdef HAVE_CAIRO_PRINT
1574         x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1575         y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1576         poppler_page_get_size (poppler_page, &page_width, &page_height);
1577
1578         if (page_width > page_height && page_width > ctx->paper_width) {
1579                 rotate = TRUE;
1580         } else {
1581                 rotate = FALSE;
1582         }
1583
1584         /* Use always portrait mode and rotate when necessary */
1585         if (ctx->paper_width > ctx->paper_height) {
1586                 width = ctx->paper_height;
1587                 height = ctx->paper_width;
1588                 rotate = !rotate;
1589         } else {
1590                 width = ctx->paper_width;
1591                 height = ctx->paper_height;
1592         }
1593
1594         if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1595                 rotate = !rotate;
1596         }       
1597
1598         if (rotate) {
1599                 gint tmp1;
1600                 gdouble tmp2;
1601
1602                 tmp1 = x;
1603                 x = y;
1604                 y = tmp1;
1605
1606                 tmp2 = page_width;
1607                 page_width = page_height;
1608                 page_height = tmp2;
1609         }
1610
1611         pwidth = width / ctx->pages_x;
1612         pheight = height / ctx->pages_y;
1613
1614         if ((page_width > pwidth || page_height > pheight) ||
1615             (page_width < pwidth && page_height < pheight)) {
1616                 xscale = pwidth / page_width;
1617                 yscale = pheight / page_height;
1618                 
1619                 if (yscale < xscale) {
1620                         xscale = yscale;
1621                 } else {
1622                         yscale = xscale;
1623                 }
1624                 
1625         } else {        
1626                 xscale = yscale = 1;
1627         }
1628
1629         /* TODO: center */
1630
1631         cairo_save (ctx->cr);
1632         if (rotate) {
1633                 cairo_matrix_t matrix;
1634                 
1635                 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1636                 cairo_matrix_init (&matrix,
1637                                    0,  1,
1638                                    -1,  0,
1639                                    0,  0);
1640                 cairo_transform (ctx->cr, &matrix);
1641         }
1642         
1643         cairo_translate (ctx->cr,
1644                          x * (rotate ? pheight : pwidth),
1645                          y * (rotate ? pwidth : pheight));
1646         cairo_scale (ctx->cr, xscale, yscale);
1647
1648         poppler_page_render_for_printing (poppler_page, ctx->cr);
1649
1650         ctx->pages_printed++;
1651                         
1652         cairo_restore (ctx->cr);
1653 #else /* HAVE_CAIRO_PRINT */
1654         if (ctx->format == EV_FILE_FORMAT_PS)
1655                 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1656 #endif /* HAVE_CAIRO_PRINT */
1657 }
1658
1659 static void
1660 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1661 {
1662         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1663         PdfPrintContext *ctx = pdf_document->print_ctx;
1664         
1665         g_return_if_fail (pdf_document->print_ctx != NULL);
1666
1667 #ifdef HAVE_CAIRO_PRINT
1668         cairo_show_page (ctx->cr);
1669 #endif
1670 }
1671
1672 static void
1673 pdf_document_file_exporter_end (EvFileExporter *exporter)
1674 {
1675         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1676
1677         pdf_print_context_free (pdf_document->print_ctx);
1678         pdf_document->print_ctx = NULL;
1679 }
1680
1681 static EvFileExporterCapabilities
1682 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1683 {
1684         return  (EvFileExporterCapabilities) (
1685                 EV_FILE_EXPORTER_CAN_PAGE_SET |
1686                 EV_FILE_EXPORTER_CAN_COPIES |
1687                 EV_FILE_EXPORTER_CAN_COLLATE |
1688                 EV_FILE_EXPORTER_CAN_REVERSE |
1689                 EV_FILE_EXPORTER_CAN_SCALE |
1690 #ifdef HAVE_CAIRO_PRINT
1691 #ifdef HAVE_POPPLER_PAGE_RENDER
1692                 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1693 #endif
1694 #endif
1695                 
1696 #ifdef HAVE_CAIRO_PDF
1697 #ifdef HAVE_POPPLER_PAGE_RENDER
1698                 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1699 #endif
1700 #endif
1701                 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1702 }
1703
1704 static void
1705 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1706 {
1707         iface->begin = pdf_document_file_exporter_begin;
1708         iface->begin_page = pdf_document_file_exporter_begin_page;
1709         iface->do_page = pdf_document_file_exporter_do_page;
1710         iface->end_page = pdf_document_file_exporter_end_page;
1711         iface->end = pdf_document_file_exporter_end;
1712         iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1713 }
1714
1715 static void
1716 pdf_selection_render_selection (EvSelection      *selection,
1717                                 EvRenderContext  *rc,
1718                                 cairo_surface_t **surface,
1719                                 EvRectangle      *points,
1720                                 EvRectangle      *old_points,
1721                                 EvSelectionStyle  style,
1722                                 GdkColor         *text,
1723                                 GdkColor         *base)
1724 {
1725         PopplerPage *poppler_page;
1726         double width_points, height_points;
1727         gint width, height;
1728
1729         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1730
1731         poppler_page_get_size (poppler_page,
1732                                &width_points, &height_points);
1733         width = (int) ((width_points * rc->scale) + 0.5);
1734         height = (int) ((height_points * rc->scale) + 0.5);
1735
1736 #ifdef HAVE_POPPLER_PAGE_RENDER
1737         cairo_t *cr;
1738         PopplerColor text_color, base_color;
1739         
1740         text_color.red = text->red;
1741         text_color.green = text->green;
1742         text_color.blue = text->blue;
1743
1744         base_color.red = base->red;
1745         base_color.green = base->green;
1746         base_color.blue = base->blue;
1747
1748         if (*surface == NULL) {
1749                 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1750                                                        width, height);
1751                 
1752         }
1753
1754         cr = cairo_create (*surface);
1755         cairo_scale (cr, rc->scale, rc->scale);
1756         cairo_surface_set_device_offset (*surface, 0, 0);
1757         memset (cairo_image_surface_get_data (*surface), 0x00,
1758                 cairo_image_surface_get_height (*surface) *
1759                 cairo_image_surface_get_stride (*surface));
1760         poppler_page_render_selection (poppler_page,
1761                                        cr,
1762                                        (PopplerRectangle *)points,
1763                                        (PopplerRectangle *)old_points,
1764                                        (PopplerSelectionStyle)style,
1765                                        &text_color,
1766                                        &base_color);
1767         cairo_destroy (cr);
1768 #else /* HAVE_POPPLER_PAGE_RENDER */
1769         GdkPixbuf *pixbuf;
1770         
1771         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1772                                  TRUE, 8,
1773                                  width, height);
1774
1775         poppler_page_render_selection_to_pixbuf (poppler_page,
1776                                                  rc->scale, rc->rotation, pixbuf,
1777                                                  (PopplerRectangle *)points,
1778                                                  (PopplerRectangle *)old_points,
1779                                                  (PopplerSelectionStyle)style,
1780                                                  text,
1781                                                  base);
1782         if (*surface)
1783                 cairo_surface_destroy (*surface);
1784         *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1785         g_object_unref (pixbuf);
1786 #endif /* HAVE_POPPLER_PAGE_RENDER */
1787 }
1788
1789 static gchar *
1790 pdf_selection_get_selected_text (EvSelection     *selection,
1791                                  EvRenderContext *rc,
1792                                  EvSelectionStyle style,
1793                                  EvRectangle     *points)
1794 {
1795         PopplerPage *poppler_page;
1796         PopplerRectangle r;
1797         double height;
1798         char *retval;
1799         
1800         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1801
1802         poppler_page_get_size (poppler_page, NULL, &height);
1803         r.x1 = points->x1;
1804         r.y1 = height - points->y2;
1805         r.x2 = points->x2;
1806         r.y2 = height - points->y1;
1807
1808         retval = poppler_page_get_text (poppler_page,
1809                                         (PopplerSelectionStyle)style,
1810                                         &r);
1811
1812         return retval;
1813 }
1814
1815 static GdkRegion *
1816 create_gdk_region_from_poppler_region (GList *region)
1817 {
1818         GList *l;
1819         GdkRegion *retval;
1820         
1821         retval = gdk_region_new ();
1822         
1823         for (l = region; l; l = g_list_next (l)) {
1824                 PopplerRectangle *rectangle;
1825                 GdkRectangle      rect;
1826                 
1827                 rectangle = (PopplerRectangle *)l->data;
1828                 
1829                 rect.x = (gint) rectangle->x1;
1830                 rect.y = (gint) rectangle->y1;
1831                 rect.width  = (gint) (rectangle->x2 - rectangle->x1);
1832                 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1833                 gdk_region_union_with_rect (retval, &rect);
1834                 
1835                 poppler_rectangle_free (rectangle);
1836         }
1837
1838         return retval;
1839 }
1840
1841 static GdkRegion *
1842 pdf_selection_get_selection_region (EvSelection     *selection,
1843                                     EvRenderContext *rc,
1844                                     EvSelectionStyle style,
1845                                     EvRectangle     *points)
1846 {
1847         PopplerPage *poppler_page;
1848         GdkRegion   *retval;
1849         GList       *region;
1850
1851         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1852         
1853         region = poppler_page_get_selection_region (poppler_page,
1854                                                     rc->scale,
1855                                                     (PopplerSelectionStyle)style,
1856                                                     (PopplerRectangle *) points);
1857         retval = create_gdk_region_from_poppler_region (region);
1858         g_list_free (region);
1859         
1860         return retval;
1861 }
1862
1863 static GdkRegion *
1864 pdf_selection_get_selection_map (EvSelection     *selection,
1865                                  EvRenderContext *rc)
1866 {
1867         PopplerPage *poppler_page;
1868         PopplerRectangle points;
1869         GList *region;
1870         GdkRegion *retval;
1871
1872         poppler_page = POPPLER_PAGE (rc->page->backend_page);
1873
1874         points.x1 = 0.0;
1875         points.y1 = 0.0;
1876         poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1877         
1878         region = poppler_page_get_selection_region (poppler_page, 1.0,
1879                                                     POPPLER_SELECTION_GLYPH,
1880                                                     &points);
1881         retval = create_gdk_region_from_poppler_region (region);
1882         g_list_free (region);
1883
1884         return retval;
1885 }
1886
1887 static void
1888 pdf_selection_iface_init (EvSelectionIface *iface)
1889 {
1890         iface->render_selection = pdf_selection_render_selection;
1891         iface->get_selected_text = pdf_selection_get_selected_text;
1892         iface->get_selection_region = pdf_selection_get_selection_region;
1893         iface->get_selection_map = pdf_selection_get_selection_map;
1894 }
1895
1896 /* Page Transitions */
1897 static gdouble
1898 pdf_document_get_page_duration (EvDocumentTransition *trans,
1899                                 gint                  page)
1900 {
1901         PdfDocument *pdf_document;
1902         PopplerPage *poppler_page;
1903         gdouble      duration = -1;
1904
1905         pdf_document = PDF_DOCUMENT (trans);
1906         poppler_page = poppler_document_get_page (pdf_document->document, page);
1907         if (!poppler_page)
1908                 return -1;
1909
1910         duration = poppler_page_get_duration (poppler_page);
1911         g_object_unref (poppler_page);
1912
1913         return duration;
1914 }
1915
1916 static EvTransitionEffect *
1917 pdf_document_get_effect (EvDocumentTransition *trans,
1918                          gint                  page)
1919 {
1920         PdfDocument            *pdf_document;
1921         PopplerPage            *poppler_page;
1922         PopplerPageTransition  *page_transition;
1923         EvTransitionEffect     *effect;
1924
1925         pdf_document = PDF_DOCUMENT (trans);
1926         poppler_page = poppler_document_get_page (pdf_document->document, page);
1927
1928         if (!poppler_page)
1929                 return NULL;
1930
1931         page_transition = poppler_page_get_transition (poppler_page);
1932
1933         if (!page_transition) {
1934                 g_object_unref (poppler_page);
1935                 return NULL;
1936         }
1937
1938         /* enums in PopplerPageTransition match the EvTransitionEffect ones */
1939         effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
1940                                            "alignment", page_transition->alignment,
1941                                            "direction", page_transition->direction,
1942                                            "duration", page_transition->duration,
1943                                            "angle", page_transition->angle,
1944                                            "scale", page_transition->scale,
1945                                            "rectangular", page_transition->rectangular,
1946                                            NULL);
1947
1948         poppler_page_transition_free (page_transition);
1949         g_object_unref (poppler_page);
1950
1951         return effect;
1952 }
1953
1954 static void
1955 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1956 {
1957         iface->get_page_duration = pdf_document_get_page_duration;
1958         iface->get_effect = pdf_document_get_effect;
1959 }
1960
1961 /* Forms */
1962 static void
1963 pdf_document_get_crop_box (EvDocument  *document, 
1964                            int          page, 
1965                            EvRectangle *rect)
1966 {
1967         PdfDocument *pdf_document;
1968         PopplerPage *poppler_page;
1969         PopplerRectangle poppler_rect;
1970
1971         pdf_document = PDF_DOCUMENT (document);
1972         poppler_page = poppler_document_get_page (pdf_document->document, page);
1973         poppler_page_get_crop_box (poppler_page, &poppler_rect);
1974         rect->x1 = poppler_rect.x1;
1975         rect->x2 = poppler_rect.x2;
1976         rect->y1 = poppler_rect.y1;
1977         rect->y2 = poppler_rect.y2;
1978 }
1979
1980 static EvFormField *
1981 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
1982 {
1983         EvFormField *ev_field = NULL;
1984         gint         id;
1985         gdouble      font_size;
1986         gboolean     is_read_only;
1987
1988         id = poppler_form_field_get_id (poppler_field);
1989         font_size = poppler_form_field_get_font_size (poppler_field);
1990         is_read_only = poppler_form_field_is_read_only (poppler_field);
1991
1992         switch (poppler_form_field_get_field_type (poppler_field)) {
1993                 case POPPLER_FORM_FIELD_TEXT: {
1994                         EvFormFieldText    *field_text;
1995                         EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
1996
1997                         switch (poppler_form_field_text_get_text_type (poppler_field)) {
1998                                 case POPPLER_FORM_TEXT_NORMAL:
1999                                         ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2000                                         break;
2001                                 case POPPLER_FORM_TEXT_MULTILINE:
2002                                         ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2003                                         break;
2004                                 case POPPLER_FORM_TEXT_FILE_SELECT:
2005                                         ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2006                                         break;
2007                         }
2008                         
2009                         ev_field = ev_form_field_text_new (id, ev_text_type);
2010                         field_text = EV_FORM_FIELD_TEXT (ev_field);
2011
2012                         field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2013                         field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2014                         field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2015                         field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2016                         field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2017                         field_text->text = poppler_form_field_text_get_text (poppler_field);
2018
2019                 }
2020                         break;
2021                 case POPPLER_FORM_FIELD_BUTTON: {
2022                         EvFormFieldButton    *field_button;
2023                         EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2024
2025                         switch (poppler_form_field_button_get_button_type (poppler_field)) {
2026                                 case POPPLER_FORM_BUTTON_PUSH:
2027                                         ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2028                                         break;
2029                                 case POPPLER_FORM_BUTTON_CHECK:
2030                                         ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2031                                         break;
2032                                 case POPPLER_FORM_BUTTON_RADIO:
2033                                         ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2034                                         break;
2035                         }
2036
2037                         ev_field = ev_form_field_button_new (id, ev_button_type);
2038                         field_button = EV_FORM_FIELD_BUTTON (ev_field);
2039                         
2040                         field_button->state = poppler_form_field_button_get_state (poppler_field);
2041                 }
2042                         break;
2043                 case POPPLER_FORM_FIELD_CHOICE: {
2044                         EvFormFieldChoice    *field_choice;
2045                         EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2046
2047                         switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2048                                 case POPPLER_FORM_CHOICE_COMBO:
2049                                         ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2050                                         break;
2051                                 case EV_FORM_FIELD_CHOICE_LIST:
2052                                         ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2053                                         break;
2054                         }
2055
2056                         ev_field = ev_form_field_choice_new (id, ev_choice_type);
2057                         field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2058
2059                         field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2060                         field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2061                         field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2062                         field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2063
2064                         /* TODO: we need poppler_form_field_choice_get_selected_items in poppler 
2065                         field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2066                         if (field_choice->is_editable)
2067                                 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2068                 }
2069                         break;
2070                 case POPPLER_FORM_FIELD_SIGNATURE:
2071                         /* TODO */
2072                         ev_field = ev_form_field_signature_new (id);
2073                         break;
2074                 case POPPLER_FORM_FIELD_UNKNOWN:
2075                         return NULL;
2076         }
2077
2078         ev_field->font_size = font_size;
2079         ev_field->is_read_only = is_read_only;
2080
2081         return ev_field;
2082 }
2083
2084 static GList *
2085 pdf_document_forms_get_form_fields (EvDocumentForms *document, 
2086                                     EvPage          *page)
2087 {
2088         PopplerPage *poppler_page;
2089         GList *retval = NULL;
2090         GList *fields;
2091         GList *list;
2092         double height;
2093
2094         g_return_val_if_fail (POPPLER_IS_PAGE (page->backend_page), NULL);
2095         
2096         poppler_page = POPPLER_PAGE (page->backend_page);
2097         fields = poppler_page_get_form_field_mapping (poppler_page);
2098         poppler_page_get_size (poppler_page, NULL, &height);
2099
2100         for (list = fields; list; list = list->next) {
2101                 PopplerFormFieldMapping *mapping;
2102                 EvFormFieldMapping *field_mapping;
2103                 EvFormField *ev_field;
2104
2105                 mapping = (PopplerFormFieldMapping *)list->data;
2106
2107                 ev_field = ev_form_field_from_poppler_field (mapping->field);
2108                 if (!ev_field)
2109                         continue;
2110
2111                 field_mapping = g_new0 (EvFormFieldMapping, 1);
2112                 field_mapping->x1 = mapping->area.x1;
2113                 field_mapping->x2 = mapping->area.x2;
2114                 field_mapping->y1 = height - mapping->area.y2;
2115                 field_mapping->y2 = height - mapping->area.y1;
2116                 field_mapping->field = ev_field;
2117                 field_mapping->field->page = EV_PAGE (g_object_ref (page));
2118
2119                 g_object_set_data_full (G_OBJECT (ev_field),
2120                                         "poppler-field",
2121                                         g_object_ref (mapping->field),
2122                                         (GDestroyNotify) g_object_unref);
2123                 
2124                 retval = g_list_prepend (retval, field_mapping);
2125         }
2126         
2127         poppler_page_free_form_field_mapping (fields);
2128
2129         return g_list_reverse (retval);
2130 }
2131
2132 static gchar *
2133 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2134                                              EvFormField     *field)
2135         
2136 {
2137         PopplerFormField *poppler_field;
2138         gchar *text;
2139
2140         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2141         if (!poppler_field)
2142                 return NULL;
2143         
2144         text = poppler_form_field_text_get_text (poppler_field);
2145
2146         return text;
2147 }
2148
2149 static void
2150 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document, 
2151                                              EvFormField     *field,
2152                                              const gchar     *text)
2153 {
2154         PopplerFormField *poppler_field;
2155
2156         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2157         if (!poppler_field)
2158                 return;
2159         
2160         poppler_form_field_text_set_text (poppler_field, text);
2161         PDF_DOCUMENT (document)->modified = TRUE;
2162 }
2163
2164 static void
2165 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document, 
2166                                                 EvFormField     *field,
2167                                                 gboolean         state)
2168 {
2169         PopplerFormField *poppler_field;
2170
2171         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2172         if (!poppler_field)
2173                 return;
2174         
2175         poppler_form_field_button_set_state (poppler_field, state);
2176         PDF_DOCUMENT (document)->modified = TRUE;
2177 }
2178
2179 static gboolean
2180 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document, 
2181                                                 EvFormField     *field)
2182 {
2183         PopplerFormField *poppler_field;
2184         gboolean state;
2185
2186         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2187         if (!poppler_field)
2188                 return FALSE;
2189
2190         state = poppler_form_field_button_get_state (poppler_field);
2191
2192         return state;
2193 }
2194
2195 static gchar *
2196 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document, 
2197                                                EvFormField     *field,
2198                                                gint             index)
2199 {
2200         PopplerFormField *poppler_field;
2201         gchar *text;
2202
2203         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2204         if (!poppler_field)
2205                 return NULL;
2206
2207         text = poppler_form_field_choice_get_item (poppler_field, index);
2208
2209         return text;
2210 }
2211
2212 static int
2213 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document, 
2214                                                   EvFormField     *field)
2215 {
2216         PopplerFormField *poppler_field;
2217         gint n_items;
2218
2219         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2220         if (!poppler_field)
2221                 return -1;
2222         
2223         n_items = poppler_form_field_choice_get_n_items (poppler_field);
2224
2225         return n_items;
2226 }
2227
2228 static gboolean
2229 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document, 
2230                                                        EvFormField     *field,
2231                                                        gint             index)
2232 {
2233         PopplerFormField *poppler_field;
2234         gboolean selected;
2235
2236         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2237         if (!poppler_field)
2238                 return FALSE;
2239
2240         selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2241
2242         return selected;
2243 }
2244
2245 static void
2246 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document, 
2247                                                   EvFormField     *field,
2248                                                   gint             index)
2249 {
2250         PopplerFormField *poppler_field;
2251
2252         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2253         if (!poppler_field)
2254                 return;
2255
2256         poppler_form_field_choice_select_item (poppler_field, index);
2257         PDF_DOCUMENT (document)->modified = TRUE;
2258 }
2259
2260 static void
2261 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document, 
2262                                                   EvFormField     *field,
2263                                                   gint             index)
2264 {
2265         PopplerFormField *poppler_field;
2266
2267         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2268         if (!poppler_field)
2269                 return;
2270
2271         poppler_form_field_choice_toggle_item (poppler_field, index);
2272         PDF_DOCUMENT (document)->modified = TRUE;
2273 }
2274
2275 static void
2276 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document, 
2277                                                    EvFormField     *field)
2278 {
2279         PopplerFormField *poppler_field;
2280
2281         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2282         if (!poppler_field)
2283                 return;
2284         
2285         poppler_form_field_choice_unselect_all (poppler_field);
2286         PDF_DOCUMENT (document)->modified = TRUE;
2287 }
2288
2289 static void
2290 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2291                                                EvFormField     *field,
2292                                                const gchar     *text)
2293 {
2294         PopplerFormField *poppler_field;
2295
2296         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2297         if (!poppler_field)
2298                 return;
2299         
2300         poppler_form_field_choice_set_text (poppler_field, text);
2301         PDF_DOCUMENT (document)->modified = TRUE;
2302 }
2303
2304 static gchar *
2305 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2306                                                EvFormField     *field)
2307 {
2308         PopplerFormField *poppler_field;
2309         gchar *text;
2310
2311         poppler_field = POPPLER_FORM_FIELD (g_object_get_data (G_OBJECT (field), "poppler-field"));
2312         if (!poppler_field)
2313                 return NULL;
2314
2315         text = poppler_form_field_choice_get_text (poppler_field);
2316
2317         return text;
2318 }
2319
2320 static void
2321 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2322 {
2323         iface->get_form_fields = pdf_document_forms_get_form_fields;
2324         iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2325         iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2326         iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2327         iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2328         iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2329         iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2330         iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2331         iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2332         iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2333         iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2334         iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2335         iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2336 }
2337
2338 /* Annotations */
2339 static void
2340 poppler_annot_color_to_gdk_color (PopplerAnnot *poppler_annot,
2341                                   GdkColor     *color)
2342 {
2343         PopplerColor *poppler_color;
2344
2345         poppler_color = poppler_annot_get_color (poppler_annot);
2346         if (poppler_color) {
2347                 color->red = poppler_color->red;
2348                 color->green = poppler_color->green;
2349                 color->blue = poppler_color->blue;
2350
2351                 g_free (poppler_color);
2352         } /* TODO: else use a default color */
2353 }
2354
2355 static EvAnnotation *
2356 ev_annot_from_poppler_annot (PopplerAnnot *poppler_annot,
2357                              EvPage       *page)
2358 {
2359         EvAnnotation *ev_annot = NULL;
2360         const gchar  *unimplemented_annot = NULL;
2361
2362         switch (poppler_annot_get_annot_type (poppler_annot)) {
2363                 case POPPLER_ANNOT_TEXT:
2364                         PopplerAnnotText *poppler_text;
2365                         EvAnnotationText *ev_annot_text;
2366
2367                         poppler_text = POPPLER_ANNOT_TEXT (poppler_annot);
2368
2369                         ev_annot = ev_annotation_text_new (page);
2370
2371                         ev_annot_text = EV_ANNOTATION_TEXT (ev_annot);
2372                         ev_annot_text->is_open = poppler_annot_text_get_is_open (poppler_text);
2373
2374                         break;
2375                 case POPPLER_ANNOT_LINK:
2376                 case POPPLER_ANNOT_WIDGET:
2377                         /* Ignore link and widgets annots since they are already handled */
2378                         break;
2379                 default: {
2380                         GEnumValue *enum_value;
2381
2382                         enum_value = g_enum_get_value ((GEnumClass *) g_type_class_ref (POPPLER_TYPE_ANNOT_TYPE),
2383                                                        poppler_annot_get_annot_type (poppler_annot));
2384                         unimplemented_annot = enum_value ? enum_value->value_name : "Unknown annotation";
2385                 }
2386         }
2387
2388         if (unimplemented_annot) {
2389                 g_warning ("Unimplemented annotation: %s, please post a "
2390                            "bug report in Evince bugzilla "
2391                            "(http://bugzilla.gnome.org) with a testcase.",
2392                            unimplemented_annot);
2393         }
2394
2395         if (ev_annot) {
2396                 ev_annot->contents = poppler_annot_get_contents (poppler_annot);
2397                 ev_annot->name = poppler_annot_get_name (poppler_annot);
2398                 ev_annot->modified = poppler_annot_get_modified (poppler_annot);
2399                 poppler_annot_color_to_gdk_color (poppler_annot, &ev_annot->color);
2400
2401                 if (POPPLER_IS_ANNOT_MARKUP (poppler_annot)) {
2402                         PopplerAnnotMarkup *markup;
2403                         gchar *label;
2404                         gdouble opacity;
2405                         gboolean is_open;
2406                         PopplerRectangle poppler_rect;
2407
2408                         markup = POPPLER_ANNOT_MARKUP (poppler_annot);
2409
2410                         if (poppler_annot_markup_get_popup_rectangle (markup, &poppler_rect)) {
2411                                 EvRectangle ev_rect;
2412                                 gdouble height;
2413
2414                                 poppler_page_get_size (POPPLER_PAGE (page->backend_page),
2415                                                        NULL, &height);
2416                                 ev_rect.x1 = poppler_rect.x1;
2417                                 ev_rect.x2 = poppler_rect.x2;
2418                                 ev_rect.y1 = height - poppler_rect.y2;
2419                                 ev_rect.y2 = height - poppler_rect.y1;
2420
2421                                 g_object_set (ev_annot, "rectangle", &ev_rect, NULL);
2422                         }
2423
2424                         label = poppler_annot_markup_get_label (markup);
2425                         opacity = poppler_annot_markup_get_opacity (markup);
2426                         is_open = poppler_annot_markup_get_popup_is_open (markup);
2427
2428                         g_object_set (ev_annot,
2429                                       "label", label,
2430                                       "opacity", opacity,
2431                                       "is_open", is_open,
2432                                       NULL);
2433
2434                         g_free (label);
2435                 }
2436         }
2437
2438         return ev_annot;
2439 }
2440
2441 static GList *
2442 pdf_document_annotations_get_annotations (EvDocumentAnnotations *document_annotations,
2443                                           EvPage                *page)
2444 {
2445         GList *retval = NULL;
2446         PdfDocument *pdf_document;
2447         PopplerPage *poppler_page;
2448         GList *annots;
2449         GList *list;
2450         gdouble height;
2451         gint i = 0;
2452
2453         pdf_document = PDF_DOCUMENT (document_annotations);
2454         poppler_page = POPPLER_PAGE (page->backend_page);
2455         annots = poppler_page_get_annot_mapping (poppler_page);
2456         poppler_page_get_size (poppler_page, NULL, &height);
2457
2458         for (list = annots; list; list = list->next) {
2459                 PopplerAnnotMapping *mapping;
2460                 EvAnnotationMapping *annot_mapping;
2461                 EvAnnotation        *ev_annot;
2462
2463                 mapping = (PopplerAnnotMapping *)list->data;
2464
2465                 ev_annot = ev_annot_from_poppler_annot (mapping->annot, page);
2466                 if (!ev_annot)
2467                         continue;
2468
2469                 i++;
2470
2471                 /* Make sure annot has a unique name */
2472                 if (!ev_annot->name)
2473                         ev_annot->name = g_strdup_printf ("annot-%d-%d", page->index, i);
2474
2475                 annot_mapping = g_new0 (EvAnnotationMapping, 1);
2476                 annot_mapping->x1 = mapping->area.x1;
2477                 annot_mapping->x2 = mapping->area.x2;
2478                 annot_mapping->y1 = height - mapping->area.y2;
2479                 annot_mapping->y2 = height - mapping->area.y1;
2480                 annot_mapping->annotation = ev_annot;
2481
2482                 g_object_set_data_full (G_OBJECT (ev_annot),
2483                                         "poppler-annot",
2484                                         g_object_ref (mapping->annot),
2485                                         (GDestroyNotify) g_object_unref);
2486
2487                 retval = g_list_prepend (retval, annot_mapping);
2488         }
2489
2490         poppler_page_free_annot_mapping (annots);
2491
2492         return g_list_reverse (retval);
2493 }
2494
2495 static void
2496 pdf_document_annotations_annotation_set_contents (EvDocumentAnnotations *document,
2497                                                   EvAnnotation          *annot,
2498                                                   const gchar           *contents)
2499 {
2500         PopplerAnnot *poppler_annot;
2501
2502         poppler_annot = POPPLER_ANNOT (g_object_get_data (G_OBJECT (annot), "poppler-annot"));
2503         if (!poppler_annot)
2504                 return;
2505
2506         poppler_annot_set_contents (poppler_annot, contents);
2507         PDF_DOCUMENT (document)->modified = TRUE;
2508 }
2509
2510 static void
2511 pdf_document_document_annotations_iface_init (EvDocumentAnnotationsIface *iface)
2512 {
2513         iface->get_annotations = pdf_document_annotations_get_annotations;
2514         iface->annotation_set_contents = pdf_document_annotations_annotation_set_contents;
2515 }
2516
2517 /* Layers */
2518 static gboolean
2519 pdf_document_layers_has_layers (EvDocumentLayers *document)
2520 {
2521         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2522         PopplerLayersIter *iter;
2523
2524         iter = poppler_layers_iter_new (pdf_document->document);
2525         if (!iter)
2526                 return FALSE;
2527         poppler_layers_iter_free (iter);
2528
2529         return TRUE;
2530 }
2531
2532 static void
2533 build_layers_tree (PdfDocument       *pdf_document,
2534                    GtkTreeModel      *model,
2535                    GtkTreeIter       *parent,
2536                    PopplerLayersIter *iter)
2537 {
2538         do {
2539                 GtkTreeIter        tree_iter;
2540                 PopplerLayersIter *child;
2541                 PopplerLayer      *layer;
2542                 EvLayer           *ev_layer = NULL;
2543                 gboolean           visible;
2544                 gchar             *markup;
2545                 gint               rb_group = 0;
2546
2547                 layer = poppler_layers_iter_get_layer (iter);
2548                 if (layer) {
2549                         markup = g_markup_escape_text (poppler_layer_get_title (layer), -1);
2550                         visible = poppler_layer_is_visible (layer);
2551                         rb_group = poppler_layer_get_radio_button_group_id (layer);
2552                         pdf_document->layers = g_list_append (pdf_document->layers,
2553                                                               g_object_ref (layer));
2554                         ev_layer = ev_layer_new (g_list_length (pdf_document->layers) - 1,
2555                                                  poppler_layer_is_parent (layer),
2556                                                  rb_group);
2557                 } else {
2558                         gchar *title;
2559
2560                         title = poppler_layers_iter_get_title (iter);
2561                         markup = g_markup_escape_text (title, -1);
2562                         g_free (title);
2563
2564                         visible = FALSE;
2565                         layer = NULL;
2566                 }
2567
2568                 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
2569                 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
2570                                     EV_DOCUMENT_LAYERS_COLUMN_TITLE, markup,
2571                                     EV_DOCUMENT_LAYERS_COLUMN_VISIBLE, visible,
2572                                     EV_DOCUMENT_LAYERS_COLUMN_ENABLED, TRUE, /* FIXME */
2573                                     EV_DOCUMENT_LAYERS_COLUMN_SHOWTOGGLE, (layer != NULL),
2574                                     EV_DOCUMENT_LAYERS_COLUMN_RBGROUP, rb_group,
2575                                     EV_DOCUMENT_LAYERS_COLUMN_LAYER, ev_layer,
2576                                     -1);
2577                 if (ev_layer)
2578                         g_object_unref (ev_layer);
2579                 g_free (markup);
2580
2581                 child = poppler_layers_iter_get_child (iter);
2582                 if (child)
2583                         build_layers_tree (pdf_document, model, &tree_iter, child);
2584                 poppler_layers_iter_free (child);
2585         } while (poppler_layers_iter_next (iter));
2586 }
2587
2588 static GtkTreeModel *
2589 pdf_document_layers_get_layers (EvDocumentLayers *document)
2590 {
2591         GtkTreeModel *model = NULL;
2592         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2593         PopplerLayersIter *iter;
2594
2595         iter = poppler_layers_iter_new (pdf_document->document);
2596         if (iter) {
2597                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LAYERS_N_COLUMNS,
2598                                                              G_TYPE_STRING,  /* TITLE */
2599                                                              G_TYPE_OBJECT,  /* LAYER */
2600                                                              G_TYPE_BOOLEAN, /* VISIBLE */
2601                                                              G_TYPE_BOOLEAN, /* ENABLED */
2602                                                              G_TYPE_BOOLEAN, /* SHOWTOGGLE */
2603                                                              G_TYPE_INT);    /* RBGROUP */
2604                 build_layers_tree (pdf_document, model, NULL, iter);
2605                 poppler_layers_iter_free (iter);
2606         }
2607         return model;
2608 }
2609
2610 static void
2611 pdf_document_layers_show_layer (EvDocumentLayers *document,
2612                                 EvLayer          *layer)
2613 {
2614         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2615         guint        layer_id = ev_layer_get_id (layer);
2616
2617         poppler_layer_show (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2618 }
2619
2620 static void
2621 pdf_document_layers_hide_layer (EvDocumentLayers *document,
2622                                 EvLayer          *layer)
2623 {
2624         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2625         guint        layer_id = ev_layer_get_id (layer);
2626
2627         poppler_layer_hide (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2628 }
2629
2630 static gboolean
2631 pdf_document_layers_layer_is_visible (EvDocumentLayers *document,
2632                                       EvLayer          *layer)
2633 {
2634         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2635         guint        layer_id = ev_layer_get_id (layer);
2636
2637         return poppler_layer_is_visible (POPPLER_LAYER (g_list_nth_data (pdf_document->layers, layer_id)));
2638 }
2639
2640 static void
2641 pdf_document_document_layers_iface_init (EvDocumentLayersIface *iface)
2642 {
2643         iface->has_layers = pdf_document_layers_has_layers;
2644         iface->get_layers = pdf_document_layers_get_layers;
2645         iface->show_layer = pdf_document_layers_show_layer;
2646         iface->hide_layer = pdf_document_layers_hide_layer;
2647         iface->layer_is_visible = pdf_document_layers_layer_is_visible;
2648 }