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