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