]> www.fi.muni.cz Git - evince.git/blob - backend/pdf/ev-poppler.cc
01c9a698f8cf96ace3c47f4f66ae12815e8f7ccc
[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 set_rc_data (PdfDocument     *pdf_document,
153              EvRenderContext *rc)
154 {
155         if (rc->data == NULL) {
156                 rc->data = poppler_document_get_page (pdf_document->document,
157                                                       rc->page);
158                 rc->destroy = g_object_unref;
159         } else {
160                 g_assert (rc->page == poppler_page_get_index (POPPLER_PAGE (rc->data)));
161         }
162 }
163
164 static void
165 pdf_document_search_free (PdfDocumentSearch   *search)
166 {
167         PdfDocument *pdf_document = search->document;
168         int n_pages;
169         int i;
170
171         if (search->idle != 0)
172                 g_source_remove (search->idle);
173
174         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
175         for (i = 0; i < n_pages; i++) {
176                 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
177                 g_list_free (search->pages[i]);
178         }
179         g_free (search->pages);
180         
181         g_free (search->text);
182         g_free (search);
183 }
184
185 static void
186 pdf_document_dispose (GObject *object)
187 {
188         PdfDocument *pdf_document = PDF_DOCUMENT(object);
189
190         if (pdf_document->print_ctx) {
191                 pdf_print_context_free (pdf_document->print_ctx);
192                 pdf_document->print_ctx = NULL;
193         }
194         
195         if (pdf_document->search) {
196                 pdf_document_search_free (pdf_document->search);
197                 pdf_document->search = NULL;
198         }
199
200         if (pdf_document->document) {
201                 g_object_unref (pdf_document->document);
202         }
203
204         if (pdf_document->font_info) { 
205                 poppler_font_info_free (pdf_document->font_info);
206         }
207
208         if (pdf_document->fonts_iter) {
209                 poppler_fonts_iter_free (pdf_document->fonts_iter);
210         }
211
212         G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
213 }
214
215 static void
216 pdf_document_class_init (PdfDocumentClass *klass)
217 {
218         GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
219
220         g_object_class->dispose = pdf_document_dispose;
221 }
222
223 static void
224 pdf_document_init (PdfDocument *pdf_document)
225 {
226         pdf_document->password = NULL;
227 }
228
229 static void
230 convert_error (GError  *poppler_error,
231                GError **error)
232 {
233         if (poppler_error == NULL)
234                 return;
235
236         if (poppler_error->domain == POPPLER_ERROR) {
237                 /* convert poppler errors into EvDocument errors */
238                 gint code = EV_DOCUMENT_ERROR_INVALID;
239                 if (poppler_error->code == POPPLER_ERROR_INVALID)
240                         code = EV_DOCUMENT_ERROR_INVALID;
241                 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
242                         code = EV_DOCUMENT_ERROR_ENCRYPTED;
243                         
244
245                 g_set_error (error,
246                              EV_DOCUMENT_ERROR,
247                              code,
248                              poppler_error->message,
249                              NULL);
250         } else {
251                 g_propagate_error (error, poppler_error);
252         }
253 }
254
255
256 /* EvDocument */
257 static gboolean
258 pdf_document_save (EvDocument  *document,
259                    const char  *uri,
260                    GError     **error)
261 {
262         gboolean retval;
263         GError *poppler_error = NULL;
264
265         retval = poppler_document_save (PDF_DOCUMENT (document)->document,
266                                         uri,
267                                         &poppler_error);
268         if (! retval)
269                 convert_error (poppler_error, error);
270
271         return retval;
272 }
273
274 static gboolean
275 pdf_document_load (EvDocument   *document,
276                    const char   *uri,
277                    GError      **error)
278 {
279         GError *poppler_error = NULL;
280         PdfDocument *pdf_document = PDF_DOCUMENT (document);
281
282         pdf_document->document =
283                 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
284
285         if (pdf_document->document == NULL) {
286                 convert_error (poppler_error, error);
287                 return FALSE;
288         }
289
290         return TRUE;
291 }
292
293 static int
294 pdf_document_get_n_pages (EvDocument *document)
295 {
296         return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
297 }
298
299 static void
300 pdf_document_get_page_size (EvDocument   *document,
301                             int           page,
302                             double       *width,
303                             double       *height)
304 {
305         PdfDocument *pdf_document = PDF_DOCUMENT (document);
306         PopplerPage *poppler_page;
307
308         poppler_page = poppler_document_get_page (pdf_document->document, page);
309         poppler_page_get_size (poppler_page, width, height);
310         g_object_unref (poppler_page);
311 }
312
313 static char *
314 pdf_document_get_page_label (EvDocument *document,
315                              int         page)
316 {
317         PopplerPage *poppler_page;
318         char *label = NULL;
319
320         poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
321                                                   page);
322
323         g_object_get (G_OBJECT (poppler_page),
324                       "label", &label,
325                       NULL);
326         g_object_unref (poppler_page);
327
328         return label;
329 }
330
331 static gboolean
332 pdf_document_has_attachments (EvDocument *document)
333 {
334         PdfDocument *pdf_document;
335
336         pdf_document = PDF_DOCUMENT (document);
337
338         return poppler_document_has_attachments (pdf_document->document);
339 }
340
341 struct SaveToBufferData {
342         gchar *buffer;
343         gsize len, max;
344 };
345
346 static gboolean
347 attachment_save_to_buffer_callback (const gchar  *buf,
348                                     gsize         count,
349                                     gpointer      user_data,
350                                     GError      **error)
351 {
352         struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
353         gchar *new_buffer;
354         gsize new_max;
355
356         if (sdata->len + count > sdata->max) {
357                 new_max = MAX (sdata->max * 2, sdata->len + count);
358                 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
359
360                 sdata->buffer = new_buffer;
361                 sdata->max = new_max;
362         }
363         
364         memcpy (sdata->buffer + sdata->len, buf, count);
365         sdata->len += count;
366         
367         return TRUE;
368 }
369
370 static gboolean
371 attachment_save_to_buffer (PopplerAttachment  *attachment,
372                            gchar             **buffer,
373                            gsize              *buffer_size,
374                            GError            **error)
375 {
376         static const gint initial_max = 1024;
377         struct SaveToBufferData sdata;
378
379         *buffer = NULL;
380         *buffer_size = 0;
381
382         sdata.buffer = (gchar *) g_malloc (initial_max);
383         sdata.max = initial_max;
384         sdata.len = 0;
385
386         if (! poppler_attachment_save_to_callback (attachment,
387                                                    attachment_save_to_buffer_callback,
388                                                    &sdata,
389                                                    error)) {
390                 g_free (sdata.buffer);
391                 return FALSE;
392         }
393
394         *buffer = sdata.buffer;
395         *buffer_size = sdata.len;
396         
397         return TRUE;
398 }
399
400 static GList *
401 pdf_document_get_attachments (EvDocument *document)
402 {
403         PdfDocument *pdf_document;
404         GList *attachments;
405         GList *list;
406         GList *retval = NULL;
407
408         pdf_document = PDF_DOCUMENT (document);
409
410         if (!pdf_document_has_attachments (document))
411                 return NULL;
412
413         attachments = poppler_document_get_attachments (pdf_document->document);
414         
415         for (list = attachments; list; list = list->next) {
416                 PopplerAttachment *attachment;
417                 EvAttachment *ev_attachment;
418                 gchar *data = NULL;
419                 gsize size;
420                 GError *error = NULL;
421
422                 attachment = (PopplerAttachment *) list->data;
423
424                 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
425                         ev_attachment = ev_attachment_new (attachment->name,
426                                                            attachment->description,
427                                                            attachment->mtime,
428                                                            attachment->ctime,
429                                                            size, data);
430                         
431                         retval = g_list_prepend (retval, ev_attachment);
432                 } else {
433                         if (error) {
434                                 g_warning ("%s", error->message);
435                                 g_error_free (error);
436
437                                 g_free (data);
438                         }
439                 }
440
441                 g_object_unref (attachment);
442         }
443
444         return g_list_reverse (retval);
445 }
446
447 static cairo_surface_t *
448 pdf_page_render (PopplerPage     *page,
449                  gint             width,
450                  gint             height,
451                  EvRenderContext *rc)
452 {
453         cairo_surface_t *surface;
454
455 #ifdef HAVE_POPPLER_PAGE_RENDER
456         cairo_t *cr;
457         
458         surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
459                                               width, height);
460         memset (cairo_image_surface_get_data (surface), 0xff,
461                 cairo_image_surface_get_height (surface) *
462                 cairo_image_surface_get_stride (surface));
463         
464         cr = cairo_create (surface);
465         switch (rc->rotation) {
466                 case 90:
467                         cairo_translate (cr, width, 0);
468                         break;
469                 case 180:
470                         cairo_translate (cr, width, height);
471                         break;
472                 case 270:
473                         cairo_translate (cr, 0, height);
474                         break;
475                 default:
476                         cairo_translate (cr, 0, 0);
477         }
478         cairo_scale (cr, rc->scale, rc->scale);
479         cairo_rotate (cr, rc->rotation * G_PI / 180.0);
480         poppler_page_render (page, cr);
481         cairo_destroy (cr);
482 #else /* HAVE_POPPLER_PAGE_RENDER */
483         GdkPixbuf *pixbuf;
484         
485         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
486                                  FALSE, 8,
487                                  width, height);
488
489         poppler_page_render_to_pixbuf (page,
490                                        0, 0,
491                                        width, height,
492                                        rc->scale,
493                                        rc->rotation,
494                                        pixbuf);
495         surface = ev_document_misc_surface_from_pixbuf (pixbuf);
496         g_object_unref (pixbuf);
497 #endif /* HAVE_POPPLER_PAGE_RENDER */
498
499         return surface; 
500 }
501
502 static cairo_surface_t *
503 pdf_document_render (EvDocument      *document,
504                      EvRenderContext *rc)
505 {
506         PdfDocument *pdf_document;
507         double width_points, height_points;
508         gint width, height;
509
510         pdf_document = PDF_DOCUMENT (document);
511
512         set_rc_data (pdf_document, rc);
513
514         poppler_page_get_size (POPPLER_PAGE (rc->data),
515                                &width_points, &height_points);
516         
517         if (rc->rotation == 90 || rc->rotation == 270) {
518                 width = (int) ((height_points * rc->scale) + 0.5);
519                 height = (int) ((width_points * rc->scale) + 0.5);
520         } else {
521                 width = (int) ((width_points * rc->scale) + 0.5);
522                 height = (int) ((height_points * rc->scale) + 0.5);
523         }
524         
525         return pdf_page_render (POPPLER_PAGE (rc->data),
526                                 width, height, rc);
527 }
528
529 /* EvDocumentSecurity */
530
531 static gboolean
532 pdf_document_has_document_security (EvDocumentSecurity *document_security)
533 {
534         /* FIXME: do we really need to have this? */
535         return FALSE;
536 }
537
538 static void
539 pdf_document_set_password (EvDocumentSecurity *document_security,
540                            const char         *password)
541 {
542         PdfDocument *document = PDF_DOCUMENT (document_security);
543
544         if (document->password)
545                 g_free (document->password);
546
547         document->password = g_strdup (password);
548 }
549
550 static EvDocumentInfo *
551 pdf_document_get_info (EvDocument *document)
552 {
553         EvDocumentInfo *info;
554         PopplerPageLayout layout;
555         PopplerPageMode mode;
556         PopplerViewerPreferences view_prefs;
557         PopplerPermissions permissions;
558
559         info = g_new0 (EvDocumentInfo, 1);
560
561         info->fields_mask = EV_DOCUMENT_INFO_TITLE |
562                             EV_DOCUMENT_INFO_FORMAT |
563                             EV_DOCUMENT_INFO_AUTHOR |
564                             EV_DOCUMENT_INFO_SUBJECT |
565                             EV_DOCUMENT_INFO_KEYWORDS |
566                             EV_DOCUMENT_INFO_LAYOUT |
567                             EV_DOCUMENT_INFO_START_MODE |
568                             EV_DOCUMENT_INFO_PERMISSIONS |
569                             EV_DOCUMENT_INFO_UI_HINTS |
570                             EV_DOCUMENT_INFO_CREATOR |
571                             EV_DOCUMENT_INFO_PRODUCER |
572                             EV_DOCUMENT_INFO_CREATION_DATE |
573                             EV_DOCUMENT_INFO_MOD_DATE |
574                             EV_DOCUMENT_INFO_LINEARIZED |
575                             EV_DOCUMENT_INFO_N_PAGES |
576                             EV_DOCUMENT_INFO_SECURITY | 
577                             EV_DOCUMENT_INFO_PAPER_SIZE;
578
579         g_object_get (PDF_DOCUMENT (document)->document,
580                       "title", &(info->title),
581                       "format", &(info->format),
582                       "author", &(info->author),
583                       "subject", &(info->subject),
584                       "keywords", &(info->keywords),
585                       "page-mode", &mode,
586                       "page-layout", &layout,
587                       "viewer-preferences", &view_prefs,
588                       "permissions", &permissions,
589                       "creator", &(info->creator),
590                       "producer", &(info->producer),
591                       "creation-date", &(info->creation_date),
592                       "mod-date", &(info->modified_date),
593                       "linearized", &(info->linearized),
594                       NULL);
595
596         pdf_document_get_page_size(document, 0,
597                                    &(info->paper_width),
598                                    &(info->paper_height));
599
600         // Convert to mm.
601         info->paper_width = info->paper_width / 72.0f * 25.4f;
602         info->paper_height = info->paper_height / 72.0f * 25.4f;
603
604         switch (layout) {
605                 case POPPLER_PAGE_LAYOUT_SINGLE_PAGE:
606                         info->layout = EV_DOCUMENT_LAYOUT_SINGLE_PAGE;
607                         break;
608                 case POPPLER_PAGE_LAYOUT_ONE_COLUMN:
609                         info->layout = EV_DOCUMENT_LAYOUT_ONE_COLUMN;
610                         break;
611                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_LEFT:
612                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_LEFT;
613                         break;
614                 case POPPLER_PAGE_LAYOUT_TWO_COLUMN_RIGHT:
615                         info->layout = EV_DOCUMENT_LAYOUT_TWO_COLUMN_RIGHT;
616                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_LEFT:
617                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_LEFT;
618                         break;
619                 case POPPLER_PAGE_LAYOUT_TWO_PAGE_RIGHT:
620                         info->layout = EV_DOCUMENT_LAYOUT_TWO_PAGE_RIGHT;
621                         break;
622                 default:
623                         break;
624         }
625
626         switch (mode) {
627                 case POPPLER_PAGE_MODE_NONE:
628                         info->mode = EV_DOCUMENT_MODE_NONE;
629                         break;
630                 case POPPLER_PAGE_MODE_USE_THUMBS:
631                         info->mode = EV_DOCUMENT_MODE_USE_THUMBS;
632                         break;
633                 case POPPLER_PAGE_MODE_USE_OC:
634                         info->mode = EV_DOCUMENT_MODE_USE_OC;
635                         break;
636                 case POPPLER_PAGE_MODE_FULL_SCREEN:
637                         info->mode = EV_DOCUMENT_MODE_FULL_SCREEN;
638                         break;
639                 case POPPLER_PAGE_MODE_USE_ATTACHMENTS:
640                         info->mode = EV_DOCUMENT_MODE_USE_ATTACHMENTS;
641                 default:
642                         break;
643         }
644
645         info->ui_hints = 0;
646         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_TOOLBAR) {
647                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_TOOLBAR;
648         }
649         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_MENUBAR) {
650                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_MENUBAR;
651         }
652         if (view_prefs & POPPLER_VIEWER_PREFERENCES_HIDE_WINDOWUI) {
653                 info->ui_hints |= EV_DOCUMENT_UI_HINT_HIDE_WINDOWUI;
654         }
655         if (view_prefs & POPPLER_VIEWER_PREFERENCES_FIT_WINDOW) {
656                 info->ui_hints |= EV_DOCUMENT_UI_HINT_FIT_WINDOW;
657         }
658         if (view_prefs & POPPLER_VIEWER_PREFERENCES_CENTER_WINDOW) {
659                 info->ui_hints |= EV_DOCUMENT_UI_HINT_CENTER_WINDOW;
660         }
661         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DISPLAY_DOC_TITLE) {
662                 info->ui_hints |= EV_DOCUMENT_UI_HINT_DISPLAY_DOC_TITLE;
663         }
664         if (view_prefs & POPPLER_VIEWER_PREFERENCES_DIRECTION_RTL) {
665                 info->ui_hints |=  EV_DOCUMENT_UI_HINT_DIRECTION_RTL;
666         }
667
668         info->permissions = 0;
669         if (permissions & POPPLER_PERMISSIONS_OK_TO_PRINT) {
670                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT;
671         }
672         if (permissions & POPPLER_PERMISSIONS_OK_TO_MODIFY) {
673                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_MODIFY;
674         }
675         if (permissions & POPPLER_PERMISSIONS_OK_TO_COPY) {
676                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_COPY;
677         }
678         if (permissions & POPPLER_PERMISSIONS_OK_TO_ADD_NOTES) {
679                 info->permissions |= EV_DOCUMENT_PERMISSIONS_OK_TO_ADD_NOTES;
680         }
681
682         info->n_pages = ev_document_get_n_pages (document);
683
684         if (ev_document_security_has_document_security (EV_DOCUMENT_SECURITY (document))) {
685                 /* translators: this is the document security state */
686                 info->security = g_strdup (_("Yes"));
687         } else {
688                 /* translators: this is the document security state */
689                 info->security = g_strdup (_("No"));
690         }
691
692         return info;
693 }
694
695 static void
696 pdf_document_document_iface_init (EvDocumentIface *iface)
697 {
698         iface->save = pdf_document_save;
699         iface->load = pdf_document_load;
700         iface->get_n_pages = pdf_document_get_n_pages;
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;
1285         PopplerPage *poppler_page;
1286         GdkPixbuf *pixbuf = NULL;
1287         GdkPixbuf *border_pixbuf;
1288
1289         pdf_document = PDF_DOCUMENT (document_thumbnails);
1290
1291         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1292         g_return_val_if_fail (poppler_page != NULL, NULL);
1293
1294 #ifdef POPPLER_WITH_GDK
1295         pixbuf = poppler_page_get_thumbnail_pixbuf (poppler_page);
1296 #else
1297         cairo_surface_t *surface;
1298         
1299         surface = poppler_page_get_thumbnail (poppler_page);
1300         if (surface) {
1301                 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
1302                 cairo_surface_destroy (surface);
1303         }
1304 #endif /* POPPLER_WITH_GDK */
1305
1306                 
1307         if (pixbuf) {
1308                 /* Rotate provided thumbnail if needed */
1309                 GdkPixbuf *rotated_pixbuf;
1310
1311                 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1312                                                            (GdkPixbufRotation) (360 - rc->rotation));
1313                 g_object_unref (pixbuf);
1314                 pixbuf = rotated_pixbuf;
1315         } else {
1316                 /* There is no provided thumbnail.  We need to make one. */
1317                 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1318         }
1319
1320         if (border) {           
1321                 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1322                 g_object_unref (pixbuf);
1323                 pixbuf = border_pixbuf;
1324         }               
1325
1326         g_object_unref (poppler_page);
1327         
1328         return pixbuf;
1329 }
1330
1331 static void
1332 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1333                                         EvRenderContext      *rc,
1334                                         gint                 *width,
1335                                         gint                 *height)
1336 {
1337         PdfDocument *pdf_document;
1338         PopplerPage *poppler_page;
1339         gint has_thumb;
1340         
1341         pdf_document = PDF_DOCUMENT (document_thumbnails);
1342         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1343
1344         g_return_if_fail (poppler_page != NULL);
1345
1346         has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1347
1348         if (!has_thumb) {
1349                 double page_width, page_height;
1350
1351                 poppler_page_get_size (poppler_page, &page_width, &page_height);
1352
1353                 *width = (gint) MAX (page_width * rc->scale, 1);
1354                 *height = (gint) MAX (page_height * rc->scale, 1);
1355         }
1356         
1357         if (rc->rotation == 90 || rc->rotation == 270) {
1358                 gint  temp;
1359
1360                 temp = *width;
1361                 *width = *height;
1362                 *height = temp;
1363         }
1364         
1365         g_object_unref (poppler_page);
1366 }
1367
1368 static void
1369 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1370 {
1371         iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1372         iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1373 }
1374
1375
1376 static gboolean
1377 pdf_document_search_idle_callback (void *data)
1378 {
1379         PdfDocumentSearch *search = (PdfDocumentSearch*) data;
1380         PdfDocument *pdf_document = search->document;
1381         int n_pages;
1382         GList *matches;
1383         PopplerPage *page;
1384
1385         page = poppler_document_get_page (search->document->document,
1386                                           search->search_page);
1387
1388         ev_document_doc_mutex_lock ();
1389         matches = poppler_page_find_text (page, search->text);
1390         ev_document_doc_mutex_unlock ();
1391
1392         g_object_unref (page);
1393
1394         search->pages[search->search_page] = matches;
1395         ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
1396                                   search->search_page);
1397
1398         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
1399         search->search_page += 1;
1400         if (search->search_page == n_pages) {
1401                 /* wrap around */
1402                 search->search_page = 0;
1403         }
1404
1405         if (search->search_page != search->start_page) {
1406                 return TRUE;
1407         }
1408
1409         /* We're done. */
1410         search->idle = 0; /* will return FALSE to remove */
1411         return FALSE;
1412 }
1413
1414
1415 static PdfDocumentSearch *
1416 pdf_document_search_new (PdfDocument *pdf_document,
1417                          int          start_page,
1418                          const char  *text)
1419 {
1420         PdfDocumentSearch *search;
1421         int n_pages;
1422         int i;
1423
1424         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
1425
1426         search = g_new0 (PdfDocumentSearch, 1);
1427
1428         search->text = g_strdup (text);
1429         search->pages = g_new0 (GList *, n_pages);
1430         search->document = pdf_document;
1431
1432         /* We add at low priority so the progress bar repaints */
1433         search->idle = g_idle_add_full (G_PRIORITY_LOW,
1434                                         pdf_document_search_idle_callback,
1435                                         search,
1436                                         NULL);
1437
1438         search->start_page = start_page;
1439         search->search_page = start_page;
1440
1441         return search;
1442 }
1443
1444 static void
1445 pdf_document_find_begin (EvDocumentFind   *document,
1446                          int               page,
1447                          const char       *search_string,
1448                          gboolean          case_sensitive)
1449 {
1450         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1451
1452         /* FIXME handle case_sensitive (right now XPDF
1453          * code is always case insensitive for ASCII
1454          * and case sensitive for all other languaages)
1455          */
1456
1457         if (pdf_document->search &&
1458             strcmp (search_string, pdf_document->search->text) == 0)
1459                 return;
1460
1461         if (pdf_document->search)
1462                 pdf_document_search_free (pdf_document->search);
1463
1464         pdf_document->search = pdf_document_search_new (pdf_document,
1465                                                         page,
1466                                                         search_string);
1467 }
1468
1469 static int
1470 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1471 {
1472         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1473
1474         if (search) {
1475                 return g_list_length (search->pages[page]);
1476         } else {
1477                 return 0;
1478         }
1479 }
1480
1481 static gboolean
1482 pdf_document_find_get_result (EvDocumentFind *document_find,
1483                               int             page,
1484                               int             n_result,
1485                               EvRectangle    *rectangle)
1486 {
1487         PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1488         PdfDocumentSearch *search = pdf_document->search;
1489         PopplerPage *poppler_page;
1490         PopplerRectangle *r;
1491         double height;
1492
1493         if (search == NULL)
1494                 return FALSE;
1495
1496         r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1497                                                   n_result);
1498         if (r == NULL)
1499                 return FALSE;
1500
1501         poppler_page = poppler_document_get_page (pdf_document->document, page);
1502         poppler_page_get_size (poppler_page, NULL, &height);
1503         rectangle->x1 = r->x1;
1504         rectangle->y1 = height - r->y2;
1505         rectangle->x2 = r->x2;
1506         rectangle->y2 = height - r->y1;
1507         g_object_unref (poppler_page);
1508                 
1509         return TRUE;
1510 }
1511
1512 static int
1513 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1514                                     int             page)
1515 {
1516         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1517
1518         return search && search->pages[page] != NULL;
1519 }
1520
1521 static double
1522 pdf_document_find_get_progress (EvDocumentFind *document_find)
1523 {
1524         PdfDocumentSearch *search;
1525         int n_pages, pages_done;
1526
1527         search = PDF_DOCUMENT (document_find)->search;
1528
1529         if (search == NULL) {
1530                 return 0;
1531         }
1532
1533         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1534         if (search->search_page > search->start_page) {
1535                 pages_done = search->search_page - search->start_page + 1;
1536         } else if (search->search_page == search->start_page) {
1537                 pages_done = n_pages;
1538         } else {
1539                 pages_done = n_pages - search->start_page + search->search_page;
1540         }
1541
1542         return pages_done / (double) n_pages;
1543 }
1544
1545 static void
1546 pdf_document_find_cancel (EvDocumentFind *document)
1547 {
1548         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1549
1550         if (pdf_document->search) {
1551                 pdf_document_search_free (pdf_document->search);
1552                 pdf_document->search = NULL;
1553         }
1554 }
1555
1556 static void
1557 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1558 {
1559         iface->begin = pdf_document_find_begin;
1560         iface->get_n_results = pdf_document_find_get_n_results;
1561         iface->get_result = pdf_document_find_get_result;
1562         iface->page_has_results = pdf_document_find_page_has_results;
1563         iface->get_progress = pdf_document_find_get_progress;
1564         iface->cancel = pdf_document_find_cancel;
1565 }
1566
1567 static void
1568 pdf_print_context_free (PdfPrintContext *ctx)
1569 {
1570         if (!ctx)
1571                 return;
1572
1573 #ifdef HAVE_CAIRO_PRINT
1574         if (ctx->cr) {
1575                 cairo_destroy (ctx->cr);
1576                 ctx->cr = NULL;
1577         }
1578 #else
1579         if (ctx->ps_file) {
1580                 poppler_ps_file_free (ctx->ps_file);
1581                 ctx->ps_file = NULL;
1582         }
1583 #endif
1584         g_free (ctx);
1585 }
1586
1587 static void
1588 pdf_document_file_exporter_begin (EvFileExporter        *exporter,
1589                                   EvFileExporterContext *fc)
1590 {
1591         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1592         PdfPrintContext *ctx;
1593 #ifdef HAVE_CAIRO_PRINT
1594         gdouble width, height;
1595         cairo_surface_t *surface = NULL;
1596 #endif
1597         
1598         if (pdf_document->print_ctx)
1599                 pdf_print_context_free (pdf_document->print_ctx);
1600         pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1601         ctx = pdf_document->print_ctx;
1602         ctx->format = fc->format;
1603         
1604 #ifdef HAVE_CAIRO_PRINT
1605         ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1606
1607         ctx->paper_width = fc->paper_width;
1608         ctx->paper_height = fc->paper_height;
1609         
1610         switch (fc->pages_per_sheet) {
1611                 default:
1612                 case 1:
1613                         ctx->pages_x = 1;
1614                         ctx->pages_y = 1;
1615                         break;
1616                 case 2:
1617                         ctx->pages_x = 1;
1618                         ctx->pages_y = 2;
1619                         break;
1620                 case 4:
1621                         ctx->pages_x = 2;
1622                         ctx->pages_y = 2;
1623                         break;
1624                 case 6:
1625                         ctx->pages_x = 2;
1626                         ctx->pages_y = 3;
1627                         break;
1628                 case 9:
1629                         ctx->pages_x = 3;
1630                         ctx->pages_y = 3;
1631                         break;
1632                 case 16:
1633                         ctx->pages_x = 4;
1634                         ctx->pages_y = 4;
1635                         break;
1636         }
1637
1638         ctx->pages_printed = 0;
1639         
1640         switch (fc->format) {
1641                 case EV_FILE_FORMAT_PS:
1642 #ifdef HAVE_CAIRO_PS
1643                         surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1644 #endif
1645                         break;
1646                 case EV_FILE_FORMAT_PDF:
1647 #ifdef HAVE_CAIRO_PDF
1648                         surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1649 #endif
1650                         break;
1651                 default:
1652                         g_assert_not_reached ();
1653         }
1654
1655         ctx->cr = cairo_create (surface);
1656         cairo_surface_destroy (surface);
1657
1658 #else /* HAVE_CAIRO_PRINT */
1659         if (ctx->format == EV_FILE_FORMAT_PS) {
1660                 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1661                                                     fc->filename, fc->first_page,
1662                                                     fc->last_page - fc->first_page + 1);
1663                 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1664                 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1665         }
1666 #endif /* HAVE_CAIRO_PRINT */
1667 }
1668
1669 static void
1670 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1671 {
1672         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1673         PdfPrintContext *ctx = pdf_document->print_ctx;
1674         
1675         g_return_if_fail (pdf_document->print_ctx != NULL);
1676
1677         ctx->pages_printed = 0;
1678         
1679 #ifdef HAVE_CAIRO_PRINT
1680         if (ctx->paper_width > ctx->paper_height) {
1681                 if (ctx->format == EV_FILE_FORMAT_PS) {
1682                         cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1683                                                    ctx->paper_height,
1684                                                    ctx->paper_width);
1685                 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1686                         cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1687                                                     ctx->paper_height,
1688                                                     ctx->paper_width);
1689                 }
1690         }
1691 #endif /* HAVE_CAIRO_PRINT */
1692 }
1693
1694 static void
1695 pdf_document_file_exporter_do_page (EvFileExporter  *exporter,
1696                                     EvRenderContext *rc)
1697 {
1698         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1699         PdfPrintContext *ctx = pdf_document->print_ctx;
1700         PopplerPage *poppler_page;
1701 #ifdef HAVE_CAIRO_PRINT
1702         gdouble  page_width, page_height;
1703         gint     x, y;
1704         gboolean rotate;
1705         gdouble  width, height;
1706         gdouble  pwidth, pheight;
1707         gdouble  xscale, yscale;
1708 #endif
1709
1710         g_return_if_fail (pdf_document->print_ctx != NULL);
1711
1712         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1713         
1714 #ifdef HAVE_CAIRO_PRINT
1715         x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1716         y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1717         poppler_page_get_size (poppler_page, &page_width, &page_height);
1718
1719         if (page_width > page_height && page_width > ctx->paper_width) {
1720                 rotate = TRUE;
1721         } else {
1722                 rotate = FALSE;
1723         }
1724
1725         /* Use always portrait mode and rotate when necessary */
1726         if (ctx->paper_width > ctx->paper_height) {
1727                 width = ctx->paper_height;
1728                 height = ctx->paper_width;
1729                 rotate = !rotate;
1730         } else {
1731                 width = ctx->paper_width;
1732                 height = ctx->paper_height;
1733         }
1734
1735         if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1736                 rotate = !rotate;
1737         }       
1738
1739         if (rotate) {
1740                 gint tmp1;
1741                 gdouble tmp2;
1742
1743                 tmp1 = x;
1744                 x = y;
1745                 y = tmp1;
1746
1747                 tmp2 = page_width;
1748                 page_width = page_height;
1749                 page_height = tmp2;
1750         }
1751
1752         pwidth = width / ctx->pages_x;
1753         pheight = height / ctx->pages_y;
1754
1755         if ((page_width > pwidth || page_height > pheight) ||
1756             (page_width < pwidth && page_height < pheight)) {
1757                 xscale = pwidth / page_width;
1758                 yscale = pheight / page_height;
1759                 
1760                 if (yscale < xscale) {
1761                         xscale = yscale;
1762                 } else {
1763                         yscale = xscale;
1764                 }
1765                 
1766         } else {        
1767                 xscale = yscale = 1;
1768         }
1769
1770         /* TODO: center */
1771
1772         cairo_save (ctx->cr);
1773         if (rotate) {
1774                 cairo_matrix_t matrix;
1775                 
1776                 cairo_translate (ctx->cr, (2 * y + 1) * pwidth, 0);
1777                 cairo_matrix_init (&matrix,
1778                                    0,  1,
1779                                    -1,  0,
1780                                    0,  0);
1781                 cairo_transform (ctx->cr, &matrix);
1782         }
1783         
1784         cairo_translate (ctx->cr,
1785                          x * (rotate ? pheight : pwidth),
1786                          y * (rotate ? pwidth : pheight));
1787         cairo_scale (ctx->cr, xscale, yscale);
1788
1789         poppler_page_render_for_printing (poppler_page, ctx->cr);
1790
1791         ctx->pages_printed++;
1792                         
1793         cairo_restore (ctx->cr);
1794 #else /* HAVE_CAIRO_PRINT */
1795         if (ctx->format == EV_FILE_FORMAT_PS)
1796                 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1797 #endif /* HAVE_CAIRO_PRINT */
1798         
1799         g_object_unref (poppler_page);
1800 }
1801
1802 static void
1803 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1804 {
1805         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1806         PdfPrintContext *ctx = pdf_document->print_ctx;
1807         
1808         g_return_if_fail (pdf_document->print_ctx != NULL);
1809
1810 #ifdef HAVE_CAIRO_PRINT
1811         cairo_show_page (ctx->cr);
1812 #endif
1813 }
1814
1815 static void
1816 pdf_document_file_exporter_end (EvFileExporter *exporter)
1817 {
1818         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1819
1820         pdf_print_context_free (pdf_document->print_ctx);
1821         pdf_document->print_ctx = NULL;
1822 }
1823
1824 static EvFileExporterCapabilities
1825 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1826 {
1827         return  (EvFileExporterCapabilities) (
1828                 EV_FILE_EXPORTER_CAN_PAGE_SET |
1829                 EV_FILE_EXPORTER_CAN_COPIES |
1830                 EV_FILE_EXPORTER_CAN_COLLATE |
1831                 EV_FILE_EXPORTER_CAN_REVERSE |
1832                 EV_FILE_EXPORTER_CAN_SCALE |
1833 #ifdef HAVE_CAIRO_PRINT
1834 #ifdef HAVE_POPPLER_PAGE_RENDER
1835 #if GTK_CHECK_VERSION (2, 11, 1)
1836                 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1837 #endif
1838 #endif
1839 #endif
1840                 
1841 #ifdef HAVE_CAIRO_PDF
1842 #ifdef HAVE_POPPLER_PAGE_RENDER
1843                 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1844 #endif
1845 #endif
1846                 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1847 }
1848
1849 static void
1850 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1851 {
1852         iface->begin = pdf_document_file_exporter_begin;
1853         iface->begin_page = pdf_document_file_exporter_begin_page;
1854         iface->do_page = pdf_document_file_exporter_do_page;
1855         iface->end_page = pdf_document_file_exporter_end_page;
1856         iface->end = pdf_document_file_exporter_end;
1857         iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1858 }
1859
1860 static void
1861 pdf_selection_render_selection (EvSelection      *selection,
1862                                 EvRenderContext  *rc,
1863                                 cairo_surface_t **surface,
1864                                 EvRectangle      *points,
1865                                 EvRectangle      *old_points,
1866                                 EvSelectionStyle  style,
1867                                 GdkColor         *text,
1868                                 GdkColor         *base)
1869 {
1870         PdfDocument *pdf_document;
1871         double width_points, height_points;
1872         gint width, height;
1873
1874         pdf_document = PDF_DOCUMENT (selection);
1875         set_rc_data (pdf_document, rc);
1876
1877         poppler_page_get_size (POPPLER_PAGE (rc->data),
1878                                &width_points, &height_points);
1879         width = (int) ((width_points * rc->scale) + 0.5);
1880         height = (int) ((height_points * rc->scale) + 0.5);
1881
1882 #ifdef HAVE_POPPLER_PAGE_RENDER
1883         cairo_t *cr;
1884         PopplerColor text_color, base_color;
1885         
1886         text_color.red = text->red;
1887         text_color.green = text->green;
1888         text_color.blue = text->blue;
1889
1890         base_color.red = base->red;
1891         base_color.green = base->green;
1892         base_color.blue = base->blue;
1893
1894         if (*surface == NULL) {
1895                 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1896                                                        width, height);
1897                 
1898         }
1899
1900         cr = cairo_create (*surface);
1901         cairo_scale (cr, rc->scale, rc->scale);
1902         cairo_surface_set_device_offset (*surface, 0, 0);
1903         memset (cairo_image_surface_get_data (*surface), 0x00,
1904                 cairo_image_surface_get_height (*surface) *
1905                 cairo_image_surface_get_stride (*surface));
1906         poppler_page_render_selection (POPPLER_PAGE (rc->data),
1907                                        cr,
1908                                        (PopplerRectangle *)points,
1909                                        (PopplerRectangle *)old_points,
1910                                        (PopplerSelectionStyle)style,
1911                                        &text_color,
1912                                        &base_color);
1913         cairo_destroy (cr);
1914 #else /* HAVE_POPPLER_PAGE_RENDER */
1915         GdkPixbuf *pixbuf;
1916         
1917         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1918                                  TRUE, 8,
1919                                  width, height);
1920
1921         poppler_page_render_selection_to_pixbuf (POPPLER_PAGE (rc->data),
1922                                                  rc->scale, rc->rotation, pixbuf,
1923                                                  (PopplerRectangle *)points,
1924                                                  (PopplerRectangle *)old_points,
1925                                                  (PopplerSelectionStyle)style,
1926                                                  text,
1927                                                  base);
1928         if (*surface)
1929                 cairo_surface_destroy (*surface);
1930         *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1931         g_object_unref (pixbuf);
1932 #endif /* HAVE_POPPLER_PAGE_RENDER */
1933 }
1934
1935 static gchar *
1936 pdf_selection_get_selected_text (EvSelection     *selection,
1937                                  EvRenderContext *rc,
1938                                  EvSelectionStyle style,
1939                                  EvRectangle     *points)
1940 {
1941         PdfDocument *pdf_document = PDF_DOCUMENT (selection);
1942         PopplerPage *poppler_page;
1943         PopplerRectangle r;
1944         double height;
1945         char *retval;
1946         
1947         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1948         g_return_val_if_fail (poppler_page != NULL, NULL);
1949
1950         poppler_page_get_size (poppler_page, NULL, &height);
1951         r.x1 = points->x1;
1952         r.y1 = height - points->y2;
1953         r.x2 = points->x2;
1954         r.y2 = height - points->y1;
1955
1956         retval = poppler_page_get_text (poppler_page,
1957                                         (PopplerSelectionStyle)style,
1958                                         &r);
1959
1960         g_object_unref (poppler_page);
1961
1962         return retval;
1963 }
1964
1965 static GdkRegion *
1966 create_gdk_region_from_poppler_region (GList *region)
1967 {
1968         GList *l;
1969         GdkRegion *retval;
1970         
1971         retval = gdk_region_new ();
1972         
1973         for (l = region; l; l = g_list_next (l)) {
1974                 PopplerRectangle *rectangle;
1975                 GdkRectangle      rect;
1976                 
1977                 rectangle = (PopplerRectangle *)l->data;
1978                 
1979                 rect.x = (gint) rectangle->x1;
1980                 rect.y = (gint) rectangle->y1;
1981                 rect.width  = (gint) (rectangle->x2 - rectangle->x1);
1982                 rect.height = (gint) (rectangle->y2 - rectangle->y1);
1983                 gdk_region_union_with_rect (retval, &rect);
1984                 
1985                 poppler_rectangle_free (rectangle);
1986         }
1987
1988         return retval;
1989 }
1990
1991 static GdkRegion *
1992 pdf_selection_get_selection_region (EvSelection     *selection,
1993                                     EvRenderContext *rc,
1994                                     EvSelectionStyle style,
1995                                     EvRectangle     *points)
1996 {
1997         PdfDocument *pdf_document;
1998         GdkRegion   *retval;
1999         GList *region;
2000
2001         pdf_document = PDF_DOCUMENT (selection);
2002
2003         set_rc_data (pdf_document, rc);
2004         
2005         region = poppler_page_get_selection_region (POPPLER_PAGE (rc->data),
2006                                                     rc->scale,
2007                                                     (PopplerSelectionStyle)style,
2008                                                     (PopplerRectangle *) points);
2009         retval = create_gdk_region_from_poppler_region (region);
2010         g_list_free (region);
2011         
2012         return retval;
2013 }
2014
2015 static GdkRegion *
2016 pdf_selection_get_selection_map (EvSelection     *selection,
2017                                  EvRenderContext *rc)
2018 {
2019         PdfDocument *pdf_document;
2020         PopplerPage *poppler_page;
2021         PopplerRectangle points;
2022         GList *region;
2023         GdkRegion *retval;
2024
2025         pdf_document = PDF_DOCUMENT (selection);
2026         poppler_page = poppler_document_get_page (pdf_document->document,
2027                                                   rc->page);
2028
2029         points.x1 = 0.0;
2030         points.y1 = 0.0;
2031         poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
2032         
2033         region = poppler_page_get_selection_region (poppler_page, 1.0,
2034                                                     POPPLER_SELECTION_GLYPH,
2035                                                     &points);
2036         retval = create_gdk_region_from_poppler_region (region);
2037         g_list_free (region);
2038         g_object_unref (poppler_page);
2039
2040         return retval;
2041 }
2042
2043 static void
2044 pdf_selection_iface_init (EvSelectionIface *iface)
2045 {
2046         iface->render_selection = pdf_selection_render_selection;
2047         iface->get_selected_text = pdf_selection_get_selected_text;
2048         iface->get_selection_region = pdf_selection_get_selection_region;
2049         iface->get_selection_map = pdf_selection_get_selection_map;
2050 }
2051
2052 /* Page Transitions */
2053 static gdouble
2054 pdf_document_get_page_duration (EvDocumentTransition *trans,
2055                                 gint                  page)
2056 {
2057         PdfDocument *pdf_document;
2058         PopplerPage *poppler_page;
2059         gdouble      duration = -1;
2060
2061         pdf_document = PDF_DOCUMENT (trans);
2062         poppler_page = poppler_document_get_page (pdf_document->document, page);
2063         if (!poppler_page)
2064                 return -1;
2065
2066         duration = poppler_page_get_duration (poppler_page);
2067         g_object_unref (poppler_page);
2068
2069         return duration;
2070 }
2071
2072 static EvTransitionEffect *
2073 pdf_document_get_effect (EvDocumentTransition *trans,
2074                          gint                  page)
2075 {
2076         PdfDocument            *pdf_document;
2077         PopplerPage            *poppler_page;
2078         PopplerPageTransition  *page_transition;
2079         EvTransitionEffect     *effect;
2080
2081         pdf_document = PDF_DOCUMENT (trans);
2082         poppler_page = poppler_document_get_page (pdf_document->document, page);
2083
2084         if (!poppler_page)
2085                 return NULL;
2086
2087         page_transition = poppler_page_get_transition (poppler_page);
2088
2089         if (!page_transition) {
2090                 g_object_unref (poppler_page);
2091                 return NULL;
2092         }
2093
2094         /* enums in PopplerPageTransition match the EvTransitionEffect ones */
2095         effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
2096                                            "alignment", page_transition->alignment,
2097                                            "direction", page_transition->direction,
2098                                            "duration", page_transition->duration,
2099                                            "angle", page_transition->angle,
2100                                            "scale", page_transition->scale,
2101                                            "rectangular", page_transition->rectangular,
2102                                            NULL);
2103
2104         poppler_page_transition_free (page_transition);
2105         g_object_unref (poppler_page);
2106
2107         return effect;
2108 }
2109
2110 static void
2111 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
2112 {
2113         iface->get_page_duration = pdf_document_get_page_duration;
2114         iface->get_effect = pdf_document_get_effect;
2115 }
2116
2117 /* Forms */
2118 static void
2119 pdf_document_get_crop_box (EvDocument  *document, 
2120                            int          page, 
2121                            EvRectangle *rect)
2122 {
2123         PdfDocument *pdf_document;
2124         PopplerPage *poppler_page;
2125         PopplerRectangle poppler_rect;
2126
2127         pdf_document = PDF_DOCUMENT (document);
2128         poppler_page = poppler_document_get_page (pdf_document->document, page);
2129         poppler_page_get_crop_box (poppler_page, &poppler_rect);
2130         rect->x1 = poppler_rect.x1;
2131         rect->x2 = poppler_rect.x2;
2132         rect->y1 = poppler_rect.y1;
2133         rect->y2 = poppler_rect.y2;
2134 }
2135
2136 static EvFormField *
2137 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
2138 {
2139         EvFormField *ev_field = NULL;
2140         gint         id;
2141         gdouble      font_size;
2142         gboolean     is_read_only;
2143
2144         id = poppler_form_field_get_id (poppler_field);
2145         font_size = poppler_form_field_get_font_size (poppler_field);
2146         is_read_only = poppler_form_field_is_read_only (poppler_field);
2147
2148         switch (poppler_form_field_get_field_type (poppler_field)) {
2149                 case POPPLER_FORM_FIELD_TEXT: {
2150                         EvFormFieldText    *field_text;
2151                         EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2152
2153                         switch (poppler_form_field_text_get_text_type (poppler_field)) {
2154                                 case POPPLER_FORM_TEXT_NORMAL:
2155                                         ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2156                                         break;
2157                                 case POPPLER_FORM_TEXT_MULTILINE:
2158                                         ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2159                                         break;
2160                                 case POPPLER_FORM_TEXT_FILE_SELECT:
2161                                         ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2162                                         break;
2163                         }
2164                         
2165                         ev_field = ev_form_field_text_new (id, ev_text_type);
2166                         field_text = EV_FORM_FIELD_TEXT (ev_field);
2167
2168                         field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2169                         field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2170                         field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2171                         field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2172                         field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2173                         field_text->text = poppler_form_field_text_get_text (poppler_field);
2174
2175                 }
2176                         break;
2177                 case POPPLER_FORM_FIELD_BUTTON: {
2178                         EvFormFieldButton    *field_button;
2179                         EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2180
2181                         switch (poppler_form_field_button_get_button_type (poppler_field)) {
2182                                 case POPPLER_FORM_BUTTON_PUSH:
2183                                         ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2184                                         break;
2185                                 case POPPLER_FORM_BUTTON_CHECK:
2186                                         ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2187                                         break;
2188                                 case POPPLER_FORM_BUTTON_RADIO:
2189                                         ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2190                                         break;
2191                         }
2192
2193                         ev_field = ev_form_field_button_new (id, ev_button_type);
2194                         field_button = EV_FORM_FIELD_BUTTON (ev_field);
2195                         
2196                         field_button->state = poppler_form_field_button_get_state (poppler_field);
2197                 }
2198                         break;
2199                 case POPPLER_FORM_FIELD_CHOICE: {
2200                         EvFormFieldChoice    *field_choice;
2201                         EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2202
2203                         switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2204                                 case POPPLER_FORM_CHOICE_COMBO:
2205                                         ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2206                                         break;
2207                                 case EV_FORM_FIELD_CHOICE_LIST:
2208                                         ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2209                                         break;
2210                         }
2211
2212                         ev_field = ev_form_field_choice_new (id, ev_choice_type);
2213                         field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2214
2215                         field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2216                         field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2217                         field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2218                         field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2219
2220                         /* TODO: we need poppler_form_field_choice_get_selected_items in poppler 
2221                         field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2222                         if (field_choice->is_editable)
2223                                 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2224                 }
2225                         break;
2226                 case POPPLER_FORM_FIELD_SIGNATURE:
2227                         /* TODO */
2228                         ev_field = ev_form_field_signature_new (id);
2229                         break;
2230                 case POPPLER_FORM_FIELD_UNKNOWN:
2231                         return NULL;
2232         }
2233
2234         ev_field->font_size = font_size;
2235         ev_field->is_read_only = is_read_only;
2236
2237         return ev_field;
2238 }
2239
2240 static GList *
2241 pdf_document_forms_get_form_fields (EvDocumentForms *document, 
2242                                     gint             page)
2243 {
2244         PdfDocument *pdf_document;
2245         PopplerPage *poppler_page;
2246         GList *retval = NULL;
2247         GList *fields;
2248         GList *list;
2249         double height;
2250
2251         pdf_document = PDF_DOCUMENT (document);
2252         poppler_page = poppler_document_get_page (pdf_document->document, page);
2253         fields = poppler_page_get_form_field_mapping (poppler_page);
2254         poppler_page_get_size (poppler_page, NULL, &height);
2255
2256         for (list = fields; list; list = list->next) {
2257                 PopplerFormFieldMapping *mapping;
2258                 EvFormFieldMapping *field_mapping;
2259                 EvFormField *ev_field;
2260
2261                 mapping = (PopplerFormFieldMapping *)list->data;
2262
2263                 ev_field = ev_form_field_from_poppler_field (mapping->field);
2264                 if (!ev_field)
2265                         continue;
2266
2267                 field_mapping = g_new0 (EvFormFieldMapping, 1);
2268                 field_mapping->x1 = mapping->area.x1;
2269                 field_mapping->x2 = mapping->area.x2;
2270                 field_mapping->y1 = height - mapping->area.y2;
2271                 field_mapping->y2 = height - mapping->area.y1;
2272                 field_mapping->field = ev_field;
2273                 field_mapping->field->page = page;
2274                 
2275                 retval = g_list_prepend (retval, field_mapping);
2276         }
2277         
2278         poppler_page_free_form_field_mapping (fields);
2279         g_object_unref (poppler_page);
2280
2281         return g_list_reverse (retval);
2282 }
2283
2284 static gchar *
2285 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2286                                              EvFormField     *field)
2287         
2288 {
2289         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2290         PopplerFormField *poppler_field;
2291         gchar *text;
2292
2293         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2294         if (!poppler_field)
2295                 return NULL;
2296         
2297         text = poppler_form_field_text_get_text (poppler_field);
2298         g_object_unref (poppler_field);
2299
2300         return text;
2301 }
2302
2303 static void
2304 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document, 
2305                                              EvFormField     *field,
2306                                              const gchar     *text)
2307 {
2308         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2309         PopplerFormField *poppler_field;
2310
2311         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2312         if (!poppler_field)
2313                 return;
2314         poppler_form_field_text_set_text (poppler_field, text);
2315         g_object_unref (poppler_field);
2316 }
2317
2318 static void
2319 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document, 
2320                                                 EvFormField     *field,
2321                                                 gboolean         state)
2322 {
2323         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2324         PopplerFormField *poppler_field;
2325
2326         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2327         if (!poppler_field)
2328                 return;
2329         
2330         poppler_form_field_button_set_state (poppler_field, state);
2331         g_object_unref (poppler_field);
2332 }
2333
2334 static gboolean
2335 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document, 
2336                                                 EvFormField     *field)
2337 {
2338         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2339         PopplerFormField *poppler_field;
2340         gboolean state;
2341
2342         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2343         if (!poppler_field)
2344                 return FALSE;
2345
2346         state = poppler_form_field_button_get_state (poppler_field);
2347         g_object_unref (poppler_field);
2348
2349         return state;
2350 }
2351
2352 static gchar *
2353 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document, 
2354                                                EvFormField     *field,
2355                                                gint             index)
2356 {
2357         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2358         PopplerFormField *poppler_field;
2359         gchar *text;
2360
2361         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2362         if (!poppler_field)
2363                 return NULL;
2364
2365         text = poppler_form_field_choice_get_item (poppler_field, index);
2366         g_object_unref (poppler_field);
2367
2368         return text;
2369 }
2370
2371 static int
2372 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document, 
2373                                                   EvFormField     *field)
2374 {
2375         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2376         PopplerFormField *poppler_field;
2377         gint n_items;
2378
2379         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2380         if (!poppler_field)
2381                 return -1;
2382         
2383         n_items = poppler_form_field_choice_get_n_items (poppler_field);
2384         g_object_unref (poppler_field);
2385
2386         return n_items;
2387 }
2388
2389 static gboolean
2390 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document, 
2391                                                        EvFormField     *field,
2392                                                        gint             index)
2393 {
2394         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2395         PopplerFormField *poppler_field;
2396         gboolean selected;
2397
2398         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2399         if (!poppler_field)
2400                 return FALSE;
2401
2402         selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2403         g_object_unref (poppler_field);
2404
2405         return selected;
2406 }
2407
2408 static void
2409 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document, 
2410                                                   EvFormField     *field,
2411                                                   gint             index)
2412 {
2413         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2414         PopplerFormField *poppler_field;
2415
2416         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2417         if (!poppler_field)
2418                 return;
2419
2420         poppler_form_field_choice_select_item (poppler_field, index);
2421         g_object_unref (poppler_field);
2422 }
2423
2424 static void
2425 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document, 
2426                                                   EvFormField     *field,
2427                                                   gint             index)
2428 {
2429         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2430         PopplerFormField *poppler_field;
2431
2432         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2433         if (!poppler_field)
2434                 return;
2435
2436         poppler_form_field_choice_toggle_item (poppler_field, index);
2437         g_object_unref (poppler_field);
2438 }
2439
2440 static void
2441 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document, 
2442                                                    EvFormField     *field)
2443 {
2444         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2445         PopplerFormField *poppler_field;
2446
2447         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2448         if (!poppler_field)
2449                 return;
2450         
2451         poppler_form_field_choice_unselect_all (poppler_field);
2452         g_object_unref (poppler_field);
2453 }
2454
2455 static void
2456 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2457                                                EvFormField     *field,
2458                                                const gchar     *text)
2459 {
2460         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2461         PopplerFormField *poppler_field;
2462
2463         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2464         if (!poppler_field)
2465                 return;
2466         
2467         poppler_form_field_choice_set_text (poppler_field, text);
2468         g_object_unref (poppler_field);
2469 }
2470
2471 static gchar *
2472 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2473                                                EvFormField     *field)
2474 {
2475         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2476         PopplerFormField *poppler_field;
2477         gchar *text;
2478
2479         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2480         if (!poppler_field)
2481                 return NULL;
2482
2483         text = poppler_form_field_choice_get_text (poppler_field);
2484         g_object_unref (poppler_field);
2485
2486         return text;
2487 }
2488
2489 static void
2490 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2491 {
2492         iface->get_form_fields = pdf_document_forms_get_form_fields;
2493         iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2494         iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2495         iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2496         iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2497         iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2498         iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2499         iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2500         iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2501         iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2502         iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2503         iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2504         iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2505 }
2506