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