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