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