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