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