]> www.fi.muni.cz Git - evince.git/blob - backend/pdf/ev-poppler.cc
Use g_type_module_add_interface instead of g_type_add_interface_static.
[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                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_SECURITY,
130                                                                  pdf_document_security_iface_init);
131                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
132                                                                  pdf_document_document_thumbnails_iface_init);
133                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
134                                                                  pdf_document_document_links_iface_init);
135                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_IMAGES,
136                                                                  pdf_document_document_images_iface_init);
137                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FORMS,
138                                                                  pdf_document_document_forms_iface_init);
139                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FONTS,
140                                                                  pdf_document_document_fonts_iface_init);
141                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND,
142                                                                  pdf_document_find_iface_init);
143                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
144                                                                  pdf_document_file_exporter_iface_init);
145                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION,
146                                                                  pdf_selection_iface_init);
147                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_TRANSITION,
148                                                                  pdf_document_page_transition_iface_init);
149                          });
150
151 static void
152 set_rc_data (PdfDocument     *pdf_document,
153              EvRenderContext *rc)
154 {
155         if (rc->data == NULL) {
156                 rc->data = poppler_document_get_page (pdf_document->document,
157                                                       rc->page);
158                 rc->destroy = g_object_unref;
159         } else {
160                 g_assert (rc->page == poppler_page_get_index (POPPLER_PAGE (rc->data)));
161         }
162 }
163
164 static void
165 pdf_document_search_free (PdfDocumentSearch   *search)
166 {
167         PdfDocument *pdf_document = search->document;
168         int n_pages;
169         int i;
170
171         if (search->idle != 0)
172                 g_source_remove (search->idle);
173
174         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
175         for (i = 0; i < n_pages; i++) {
176                 g_list_foreach (search->pages[i], (GFunc) g_free, NULL);
177                 g_list_free (search->pages[i]);
178         }
179         g_free (search->pages);
180         
181         g_free (search->text);
182         g_free (search);
183 }
184
185 static void
186 pdf_document_dispose (GObject *object)
187 {
188         PdfDocument *pdf_document = PDF_DOCUMENT(object);
189
190         if (pdf_document->print_ctx) {
191                 pdf_print_context_free (pdf_document->print_ctx);
192                 pdf_document->print_ctx = NULL;
193         }
194         
195         if (pdf_document->search) {
196                 pdf_document_search_free (pdf_document->search);
197                 pdf_document->search = NULL;
198         }
199
200         if (pdf_document->document) {
201                 g_object_unref (pdf_document->document);
202         }
203
204         if (pdf_document->font_info) { 
205                 poppler_font_info_free (pdf_document->font_info);
206         }
207
208         if (pdf_document->fonts_iter) {
209                 poppler_fonts_iter_free (pdf_document->fonts_iter);
210         }
211
212         G_OBJECT_CLASS (pdf_document_parent_class)->dispose (object);
213 }
214
215 static void
216 pdf_document_class_init (PdfDocumentClass *klass)
217 {
218         GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
219
220         g_object_class->dispose = pdf_document_dispose;
221 }
222
223 static void
224 pdf_document_init (PdfDocument *pdf_document)
225 {
226         pdf_document->password = NULL;
227 }
228
229 static void
230 convert_error (GError  *poppler_error,
231                GError **error)
232 {
233         if (poppler_error == NULL)
234                 return;
235
236         if (poppler_error->domain == POPPLER_ERROR) {
237                 /* convert poppler errors into EvDocument errors */
238                 gint code = EV_DOCUMENT_ERROR_INVALID;
239                 if (poppler_error->code == POPPLER_ERROR_INVALID)
240                         code = EV_DOCUMENT_ERROR_INVALID;
241                 else if (poppler_error->code == POPPLER_ERROR_ENCRYPTED)
242                         code = EV_DOCUMENT_ERROR_ENCRYPTED;
243                         
244
245                 g_set_error (error,
246                              EV_DOCUMENT_ERROR,
247                              code,
248                              poppler_error->message,
249                              NULL);
250         } else {
251                 g_propagate_error (error, poppler_error);
252         }
253 }
254
255
256 /* EvDocument */
257 static gboolean
258 pdf_document_save (EvDocument  *document,
259                    const char  *uri,
260                    GError     **error)
261 {
262         gboolean retval;
263         GError *poppler_error = NULL;
264
265         retval = poppler_document_save (PDF_DOCUMENT (document)->document,
266                                         uri,
267                                         &poppler_error);
268         if (! retval)
269                 convert_error (poppler_error, error);
270
271         return retval;
272 }
273
274 static gboolean
275 pdf_document_load (EvDocument   *document,
276                    const char   *uri,
277                    GError      **error)
278 {
279         GError *poppler_error = NULL;
280         PdfDocument *pdf_document = PDF_DOCUMENT (document);
281
282         pdf_document->document =
283                 poppler_document_new_from_file (uri, pdf_document->password, &poppler_error);
284
285         if (pdf_document->document == NULL) {
286                 convert_error (poppler_error, error);
287                 return FALSE;
288         }
289
290         return TRUE;
291 }
292
293 static int
294 pdf_document_get_n_pages (EvDocument *document)
295 {
296         return poppler_document_get_n_pages (PDF_DOCUMENT (document)->document);
297 }
298
299 static void
300 pdf_document_get_page_size (EvDocument   *document,
301                             int           page,
302                             double       *width,
303                             double       *height)
304 {
305         PdfDocument *pdf_document = PDF_DOCUMENT (document);
306         PopplerPage *poppler_page;
307
308         poppler_page = poppler_document_get_page (pdf_document->document, page);
309         poppler_page_get_size (poppler_page, width, height);
310         g_object_unref (poppler_page);
311 }
312
313 static char *
314 pdf_document_get_page_label (EvDocument *document,
315                              int         page)
316 {
317         PopplerPage *poppler_page;
318         char *label = NULL;
319
320         poppler_page = poppler_document_get_page (PDF_DOCUMENT (document)->document,
321                                                   page);
322
323         g_object_get (G_OBJECT (poppler_page),
324                       "label", &label,
325                       NULL);
326         g_object_unref (poppler_page);
327
328         return label;
329 }
330
331 static gboolean
332 pdf_document_has_attachments (EvDocument *document)
333 {
334         PdfDocument *pdf_document;
335
336         pdf_document = PDF_DOCUMENT (document);
337
338         return poppler_document_has_attachments (pdf_document->document);
339 }
340
341 struct SaveToBufferData {
342         gchar *buffer;
343         gsize len, max;
344 };
345
346 static gboolean
347 attachment_save_to_buffer_callback (const gchar  *buf,
348                                     gsize         count,
349                                     gpointer      user_data,
350                                     GError      **error)
351 {
352         struct SaveToBufferData *sdata = (SaveToBufferData *)user_data;
353         gchar *new_buffer;
354         gsize new_max;
355
356         if (sdata->len + count > sdata->max) {
357                 new_max = MAX (sdata->max * 2, sdata->len + count);
358                 new_buffer = (gchar *)g_realloc (sdata->buffer, new_max);
359
360                 sdata->buffer = new_buffer;
361                 sdata->max = new_max;
362         }
363         
364         memcpy (sdata->buffer + sdata->len, buf, count);
365         sdata->len += count;
366         
367         return TRUE;
368 }
369
370 static gboolean
371 attachment_save_to_buffer (PopplerAttachment  *attachment,
372                            gchar             **buffer,
373                            gsize              *buffer_size,
374                            GError            **error)
375 {
376         static const gint initial_max = 1024;
377         struct SaveToBufferData sdata;
378
379         *buffer = NULL;
380         *buffer_size = 0;
381
382         sdata.buffer = (gchar *) g_malloc (initial_max);
383         sdata.max = initial_max;
384         sdata.len = 0;
385
386         if (! poppler_attachment_save_to_callback (attachment,
387                                                    attachment_save_to_buffer_callback,
388                                                    &sdata,
389                                                    error)) {
390                 g_free (sdata.buffer);
391                 return FALSE;
392         }
393
394         *buffer = sdata.buffer;
395         *buffer_size = sdata.len;
396         
397         return TRUE;
398 }
399
400 static GList *
401 pdf_document_get_attachments (EvDocument *document)
402 {
403         PdfDocument *pdf_document;
404         GList *attachments;
405         GList *list;
406         GList *retval = NULL;
407
408         pdf_document = PDF_DOCUMENT (document);
409
410         if (!pdf_document_has_attachments (document))
411                 return NULL;
412
413         attachments = poppler_document_get_attachments (pdf_document->document);
414         
415         for (list = attachments; list; list = list->next) {
416                 PopplerAttachment *attachment;
417                 EvAttachment *ev_attachment;
418                 gchar *data = NULL;
419                 gsize size;
420                 GError *error = NULL;
421
422                 attachment = (PopplerAttachment *) list->data;
423
424                 if (attachment_save_to_buffer (attachment, &data, &size, &error)) {
425                         ev_attachment = ev_attachment_new (attachment->name,
426                                                            attachment->description,
427                                                            attachment->mtime,
428                                                            attachment->ctime,
429                                                            size, data);
430                         
431                         retval = g_list_prepend (retval, ev_attachment);
432                 } else {
433                         if (error) {
434                                 g_warning ("%s", error->message);
435                                 g_error_free (error);
436
437                                 g_free (data);
438                         }
439                 }
440
441                 g_object_unref (attachment);
442         }
443
444         return g_list_reverse (retval);
445 }
446
447 static cairo_surface_t *
448 pdf_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_image_mapping (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 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1180                 ev_image_mapping->image = ev_image_new (page, image_mapping->image_id);
1181 #else
1182                 ev_image_mapping->image = ev_image_new_from_pixbuf (image_mapping->image);
1183 #endif
1184                 ev_image_mapping->x1 = image_mapping->area.x1;
1185                 ev_image_mapping->x2 = image_mapping->area.x2;
1186                 ev_image_mapping->y1 = image_mapping->area.y1;
1187                 ev_image_mapping->y2 = image_mapping->area.y2;
1188
1189                 retval = g_list_prepend (retval, ev_image_mapping);
1190         }
1191
1192         poppler_page_free_image_mapping (mapping_list);
1193         g_object_unref (poppler_page);
1194
1195         return g_list_reverse (retval);
1196 }
1197
1198 GdkPixbuf *
1199 pdf_document_images_get_image (EvDocumentImages *document_images,
1200                                EvImage          *image)
1201 {
1202 #ifdef HAVE_POPPLER_PAGE_GET_IMAGE
1203         PdfDocument     *pdf_document;
1204         PopplerPage     *poppler_page;
1205         cairo_surface_t *surface;
1206         GdkPixbuf       *retval = NULL;
1207
1208         pdf_document = PDF_DOCUMENT (document_images);
1209         poppler_page = poppler_document_get_page (pdf_document->document,
1210                                                   ev_image_get_page (image));
1211
1212         surface = poppler_page_get_image (poppler_page, ev_image_get_id (image));
1213         if (surface) {
1214                 retval = ev_document_misc_pixbuf_from_surface (surface);
1215                 cairo_surface_destroy (surface);
1216         }
1217
1218         g_object_unref (poppler_page);
1219
1220         return retval;
1221 #else
1222         return GDK_PIXBUF (g_object_ref (ev_image_get_pixbuf (image)));
1223 #endif /* HAVE_POPPLER_PAGE_GET_IMAGE */
1224 }
1225
1226 static void
1227 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1228 {
1229         iface->get_image_mapping = pdf_document_images_get_image_mapping;
1230         iface->get_image = pdf_document_images_get_image;
1231 }
1232
1233 static GdkPixbuf *
1234 make_thumbnail_for_page (PdfDocument     *pdf_document,
1235                          PopplerPage     *poppler_page, 
1236                          EvRenderContext *rc)
1237 {
1238         GdkPixbuf *pixbuf;
1239         int width, height;
1240
1241         pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1242                                                 rc, &width, &height);
1243
1244         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1245                                  width, height);
1246         gdk_pixbuf_fill (pixbuf, 0xffffffff);
1247
1248         ev_document_fc_mutex_lock ();
1249         poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1250                                        width, height,
1251                                        rc->scale, rc->rotation, pixbuf);
1252         ev_document_fc_mutex_unlock ();
1253
1254         return pixbuf;
1255 }
1256
1257 static GdkPixbuf *
1258 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1259                                        EvRenderContext      *rc, 
1260                                        gboolean              border)
1261 {
1262         PdfDocument *pdf_document;
1263         PopplerPage *poppler_page;
1264         GdkPixbuf *pixbuf;
1265         GdkPixbuf *border_pixbuf;
1266
1267         pdf_document = PDF_DOCUMENT (document_thumbnails);
1268
1269         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1270         g_return_val_if_fail (poppler_page != NULL, NULL);
1271
1272         pixbuf = poppler_page_get_thumbnail (poppler_page);
1273         if (pixbuf) {
1274                 /* Rotate provided thumbnail if needed */
1275                 GdkPixbuf *rotated_pixbuf;
1276
1277                 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1278                                                            (GdkPixbufRotation) (360 - rc->rotation));
1279                 g_object_unref (pixbuf);
1280                 pixbuf = rotated_pixbuf;
1281         } else {
1282                 /* There is no provided thumbnail.  We need to make one. */
1283                 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1284         }
1285
1286         if (border) {           
1287                 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1288                 g_object_unref (pixbuf);
1289                 pixbuf = border_pixbuf;
1290         }               
1291
1292         g_object_unref (poppler_page);
1293         
1294         return pixbuf;
1295 }
1296
1297 static void
1298 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1299                                         EvRenderContext      *rc,
1300                                         gint                 *width,
1301                                         gint                 *height)
1302 {
1303         PdfDocument *pdf_document;
1304         PopplerPage *poppler_page;
1305         gint has_thumb;
1306         
1307         pdf_document = PDF_DOCUMENT (document_thumbnails);
1308         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1309
1310         g_return_if_fail (poppler_page != NULL);
1311
1312         has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1313
1314         if (!has_thumb) {
1315                 double page_width, page_height;
1316
1317                 poppler_page_get_size (poppler_page, &page_width, &page_height);
1318
1319                 *width = (gint) MAX (page_width * rc->scale, 1);
1320                 *height = (gint) MAX (page_height * rc->scale, 1);
1321         }
1322         
1323         if (rc->rotation == 90 || rc->rotation == 270) {
1324                 gint  temp;
1325
1326                 temp = *width;
1327                 *width = *height;
1328                 *height = temp;
1329         }
1330         
1331         g_object_unref (poppler_page);
1332 }
1333
1334 static void
1335 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1336 {
1337         iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1338         iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1339 }
1340
1341
1342 static gboolean
1343 pdf_document_search_idle_callback (void *data)
1344 {
1345         PdfDocumentSearch *search = (PdfDocumentSearch*) data;
1346         PdfDocument *pdf_document = search->document;
1347         int n_pages;
1348         GList *matches;
1349         PopplerPage *page;
1350
1351         page = poppler_document_get_page (search->document->document,
1352                                           search->search_page);
1353
1354         ev_document_doc_mutex_lock ();
1355         matches = poppler_page_find_text (page, search->text);
1356         ev_document_doc_mutex_unlock ();
1357
1358         g_object_unref (page);
1359
1360         search->pages[search->search_page] = matches;
1361         ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
1362                                   search->search_page);
1363
1364         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
1365         search->search_page += 1;
1366         if (search->search_page == n_pages) {
1367                 /* wrap around */
1368                 search->search_page = 0;
1369         }
1370
1371         if (search->search_page != search->start_page) {
1372                 return TRUE;
1373         }
1374
1375         /* We're done. */
1376         search->idle = 0; /* will return FALSE to remove */
1377         return FALSE;
1378 }
1379
1380
1381 static PdfDocumentSearch *
1382 pdf_document_search_new (PdfDocument *pdf_document,
1383                          int          start_page,
1384                          const char  *text)
1385 {
1386         PdfDocumentSearch *search;
1387         int n_pages;
1388         int i;
1389
1390         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
1391
1392         search = g_new0 (PdfDocumentSearch, 1);
1393
1394         search->text = g_strdup (text);
1395         search->pages = g_new0 (GList *, n_pages);
1396         search->document = pdf_document;
1397
1398         /* We add at low priority so the progress bar repaints */
1399         search->idle = g_idle_add_full (G_PRIORITY_LOW,
1400                                         pdf_document_search_idle_callback,
1401                                         search,
1402                                         NULL);
1403
1404         search->start_page = start_page;
1405         search->search_page = start_page;
1406
1407         return search;
1408 }
1409
1410 static void
1411 pdf_document_find_begin (EvDocumentFind   *document,
1412                          int               page,
1413                          const char       *search_string,
1414                          gboolean          case_sensitive)
1415 {
1416         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1417
1418         /* FIXME handle case_sensitive (right now XPDF
1419          * code is always case insensitive for ASCII
1420          * and case sensitive for all other languaages)
1421          */
1422
1423         if (pdf_document->search &&
1424             strcmp (search_string, pdf_document->search->text) == 0)
1425                 return;
1426
1427         if (pdf_document->search)
1428                 pdf_document_search_free (pdf_document->search);
1429
1430         pdf_document->search = pdf_document_search_new (pdf_document,
1431                                                         page,
1432                                                         search_string);
1433 }
1434
1435 static int
1436 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1437 {
1438         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1439
1440         if (search) {
1441                 return g_list_length (search->pages[page]);
1442         } else {
1443                 return 0;
1444         }
1445 }
1446
1447 static gboolean
1448 pdf_document_find_get_result (EvDocumentFind *document_find,
1449                               int             page,
1450                               int             n_result,
1451                               EvRectangle    *rectangle)
1452 {
1453         PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1454         PdfDocumentSearch *search = pdf_document->search;
1455         PopplerPage *poppler_page;
1456         PopplerRectangle *r;
1457         double height;
1458
1459         if (search == NULL)
1460                 return FALSE;
1461
1462         r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1463                                                   n_result);
1464         if (r == NULL)
1465                 return FALSE;
1466
1467         poppler_page = poppler_document_get_page (pdf_document->document, page);
1468         poppler_page_get_size (poppler_page, NULL, &height);
1469         rectangle->x1 = r->x1;
1470         rectangle->y1 = height - r->y2;
1471         rectangle->x2 = r->x2;
1472         rectangle->y2 = height - r->y1;
1473         g_object_unref (poppler_page);
1474                 
1475         return TRUE;
1476 }
1477
1478 static int
1479 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1480                                     int             page)
1481 {
1482         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1483
1484         return search && search->pages[page] != NULL;
1485 }
1486
1487 static double
1488 pdf_document_find_get_progress (EvDocumentFind *document_find)
1489 {
1490         PdfDocumentSearch *search;
1491         int n_pages, pages_done;
1492
1493         search = PDF_DOCUMENT (document_find)->search;
1494
1495         if (search == NULL) {
1496                 return 0;
1497         }
1498
1499         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1500         if (search->search_page > search->start_page) {
1501                 pages_done = search->search_page - search->start_page + 1;
1502         } else if (search->search_page == search->start_page) {
1503                 pages_done = n_pages;
1504         } else {
1505                 pages_done = n_pages - search->start_page + search->search_page;
1506         }
1507
1508         return pages_done / (double) n_pages;
1509 }
1510
1511 static void
1512 pdf_document_find_cancel (EvDocumentFind *document)
1513 {
1514         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1515
1516         if (pdf_document->search) {
1517                 pdf_document_search_free (pdf_document->search);
1518                 pdf_document->search = NULL;
1519         }
1520 }
1521
1522 static void
1523 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1524 {
1525         iface->begin = pdf_document_find_begin;
1526         iface->get_n_results = pdf_document_find_get_n_results;
1527         iface->get_result = pdf_document_find_get_result;
1528         iface->page_has_results = pdf_document_find_page_has_results;
1529         iface->get_progress = pdf_document_find_get_progress;
1530         iface->cancel = pdf_document_find_cancel;
1531 }
1532
1533 static void
1534 pdf_print_context_free (PdfPrintContext *ctx)
1535 {
1536         if (!ctx)
1537                 return;
1538
1539 #ifdef HAVE_CAIRO_PRINT
1540         if (ctx->cr) {
1541                 cairo_destroy (ctx->cr);
1542                 ctx->cr = NULL;
1543         }
1544 #else
1545         if (ctx->ps_file) {
1546                 poppler_ps_file_free (ctx->ps_file);
1547                 ctx->ps_file = NULL;
1548         }
1549 #endif
1550         g_free (ctx);
1551 }
1552
1553 static void
1554 pdf_document_file_exporter_begin (EvFileExporter        *exporter,
1555                                   EvFileExporterContext *fc)
1556 {
1557         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1558         PdfPrintContext *ctx;
1559 #ifdef HAVE_CAIRO_PRINT
1560         gdouble width, height;
1561         cairo_surface_t *surface = NULL;
1562 #endif
1563         
1564         if (pdf_document->print_ctx)
1565                 pdf_print_context_free (pdf_document->print_ctx);
1566         pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1567         ctx = pdf_document->print_ctx;
1568         ctx->format = fc->format;
1569         
1570 #ifdef HAVE_CAIRO_PRINT
1571         ctx->pages_per_sheet = CLAMP (fc->pages_per_sheet, 1, 16);
1572
1573         ctx->paper_width = fc->paper_width;
1574         ctx->paper_height = fc->paper_height;
1575         
1576         switch (fc->pages_per_sheet) {
1577                 default:
1578                 case 1:
1579                         ctx->pages_x = 1;
1580                         ctx->pages_y = 1;
1581                         break;
1582                 case 2:
1583                         ctx->pages_x = 1;
1584                         ctx->pages_y = 2;
1585                         break;
1586                 case 4:
1587                         ctx->pages_x = 2;
1588                         ctx->pages_y = 2;
1589                         break;
1590                 case 6:
1591                         ctx->pages_x = 2;
1592                         ctx->pages_y = 3;
1593                         break;
1594                 case 9:
1595                         ctx->pages_x = 3;
1596                         ctx->pages_y = 3;
1597                         break;
1598                 case 16:
1599                         ctx->pages_x = 4;
1600                         ctx->pages_y = 4;
1601                         break;
1602         }
1603
1604         ctx->pages_printed = 0;
1605         
1606         switch (fc->format) {
1607                 case EV_FILE_FORMAT_PS:
1608 #ifdef HAVE_CAIRO_PS
1609                         surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1610 #endif
1611                         break;
1612                 case EV_FILE_FORMAT_PDF:
1613 #ifdef HAVE_CAIRO_PDF
1614                         surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1615 #endif
1616                         break;
1617                 default:
1618                         g_assert_not_reached ();
1619         }
1620
1621         ctx->cr = cairo_create (surface);
1622         cairo_surface_destroy (surface);
1623
1624 #else /* HAVE_CAIRO_PRINT */
1625         if (ctx->format == EV_FILE_FORMAT_PS) {
1626                 ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1627                                                     fc->filename, fc->first_page,
1628                                                     fc->last_page - fc->first_page + 1);
1629                 poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1630                 poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1631         }
1632 #endif /* HAVE_CAIRO_PRINT */
1633 }
1634
1635 static void
1636 pdf_document_file_exporter_begin_page (EvFileExporter *exporter)
1637 {
1638         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1639         PdfPrintContext *ctx = pdf_document->print_ctx;
1640         
1641         g_return_if_fail (pdf_document->print_ctx != NULL);
1642
1643         ctx->pages_printed = 0;
1644         
1645 #ifdef HAVE_CAIRO_PRINT
1646         if (ctx->paper_width > ctx->paper_height) {
1647                 if (ctx->format == EV_FILE_FORMAT_PS) {
1648                         cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1649                                                    ctx->paper_height,
1650                                                    ctx->paper_width);
1651                 } else if (ctx->format == EV_FILE_FORMAT_PDF) {
1652                         cairo_pdf_surface_set_size (cairo_get_target (ctx->cr),
1653                                                     ctx->paper_height,
1654                                                     ctx->paper_width);
1655                 }
1656         }
1657 #endif /* HAVE_CAIRO_PRINT */
1658 }
1659
1660 static void
1661 pdf_document_file_exporter_do_page (EvFileExporter  *exporter,
1662                                     EvRenderContext *rc)
1663 {
1664         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1665         PdfPrintContext *ctx = pdf_document->print_ctx;
1666         PopplerPage *poppler_page;
1667 #ifdef HAVE_CAIRO_PRINT
1668         gdouble  page_width, page_height;
1669         gint     x, y;
1670         gboolean rotate, landscape;
1671         gdouble  width, height;
1672         gdouble  pwidth, pheight;
1673         gdouble  xscale, yscale;
1674 #endif
1675
1676         g_return_if_fail (pdf_document->print_ctx != NULL);
1677
1678         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1679         
1680 #ifdef HAVE_CAIRO_PRINT
1681         x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1682         y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1683         poppler_page_get_size (poppler_page, &page_width, &page_height);
1684
1685         if (page_width > page_height && page_width > ctx->paper_width) {
1686                 rotate = TRUE;
1687         } else {
1688                 rotate = FALSE;
1689         }
1690
1691         landscape = (ctx->paper_width > ctx->paper_height);
1692
1693         /* Use always portrait mode and rotate when necessary */
1694         if (ctx->paper_width > ctx->paper_height) {
1695                 width = ctx->paper_height;
1696                 height = ctx->paper_width;
1697                 rotate = !rotate;
1698         } else {
1699                 width = ctx->paper_width;
1700                 height = ctx->paper_height;
1701         }
1702
1703         if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1704                 rotate = !rotate;
1705         }       
1706
1707         if (rotate) {
1708                 gint tmp1;
1709                 gdouble tmp2;
1710
1711                 tmp1 = x;
1712                 x = y;
1713                 y = tmp1;
1714
1715                 tmp2 = page_width;
1716                 page_width = page_height;
1717                 page_height = tmp2;
1718         }
1719
1720         pwidth = width / ctx->pages_x;
1721         pheight = height / ctx->pages_y;
1722
1723         if ((page_width > pwidth || page_height > pheight) ||
1724             (page_width < pwidth && page_height < pheight)) {
1725                 xscale = pwidth / page_width;
1726                 yscale = pheight / page_height;
1727                 
1728                 if (yscale < xscale) {
1729                         xscale = yscale;
1730                 } else {
1731                         yscale = xscale;
1732                 }
1733                 
1734         } else {        
1735                 xscale = yscale = 1;
1736         }
1737
1738         /* TODO: center */
1739
1740         cairo_save (ctx->cr);
1741         if (rotate) {
1742                 cairo_matrix_t matrix;
1743                 
1744                 cairo_translate (ctx->cr, width, 0);
1745                 cairo_matrix_init (&matrix,
1746                                    0,  1,
1747                                    -1,  0,
1748                                    0,  0);
1749                 cairo_transform (ctx->cr, &matrix);
1750         }
1751         
1752         cairo_translate (ctx->cr,
1753                          x * (rotate ? pheight : pwidth),
1754                          y * (rotate ? pwidth : pheight));
1755         cairo_scale (ctx->cr, xscale, yscale);
1756
1757 #ifdef HAVE_POPPLER_PAGE_RENDER_FOR_PRINTING
1758         poppler_page_render_for_printing (poppler_page, ctx->cr);
1759 #else
1760 #ifdef HAVE_POPPLER_PAGE_RENDER
1761         poppler_page_render (poppler_page, ctx->cr);
1762 #endif
1763 #endif
1764
1765         ctx->pages_printed++;
1766                         
1767         cairo_restore (ctx->cr);
1768 #else /* HAVE_CAIRO_PRINT */
1769         if (ctx->format == EV_FILE_FORMAT_PS)
1770                 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1771 #endif /* HAVE_CAIRO_PRINT */
1772         
1773         g_object_unref (poppler_page);
1774 }
1775
1776 static void
1777 pdf_document_file_exporter_end_page (EvFileExporter *exporter)
1778 {
1779         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1780         PdfPrintContext *ctx = pdf_document->print_ctx;
1781         
1782         g_return_if_fail (pdf_document->print_ctx != NULL);
1783
1784 #ifdef HAVE_CAIRO_PRINT
1785         cairo_show_page (ctx->cr);
1786 #endif
1787 }
1788
1789 static void
1790 pdf_document_file_exporter_end (EvFileExporter *exporter)
1791 {
1792         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1793
1794         pdf_print_context_free (pdf_document->print_ctx);
1795         pdf_document->print_ctx = NULL;
1796 }
1797
1798 static EvFileExporterCapabilities
1799 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1800 {
1801         return  (EvFileExporterCapabilities) (
1802                 EV_FILE_EXPORTER_CAN_PAGE_SET |
1803                 EV_FILE_EXPORTER_CAN_COPIES |
1804                 EV_FILE_EXPORTER_CAN_COLLATE |
1805                 EV_FILE_EXPORTER_CAN_REVERSE |
1806                 EV_FILE_EXPORTER_CAN_SCALE |
1807 #ifdef HAVE_CAIRO_PRINT
1808 #ifdef HAVE_POPPLER_PAGE_RENDER
1809 #if GTK_CHECK_VERSION (2, 11, 1)
1810                 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1811 #endif
1812 #endif
1813 #endif
1814                 
1815 #ifdef HAVE_CAIRO_PDF
1816 #ifdef HAVE_POPPLER_PAGE_RENDER
1817                 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1818 #endif
1819 #endif
1820                 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1821 }
1822
1823 static void
1824 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1825 {
1826         iface->begin = pdf_document_file_exporter_begin;
1827         iface->begin_page = pdf_document_file_exporter_begin_page;
1828         iface->do_page = pdf_document_file_exporter_do_page;
1829         iface->end_page = pdf_document_file_exporter_end_page;
1830         iface->end = pdf_document_file_exporter_end;
1831         iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1832 }
1833
1834 static void
1835 pdf_selection_render_selection (EvSelection      *selection,
1836                                 EvRenderContext  *rc,
1837                                 cairo_surface_t **surface,
1838                                 EvRectangle      *points,
1839                                 EvRectangle      *old_points,
1840                                 EvSelectionStyle  style,
1841                                 GdkColor         *text,
1842                                 GdkColor         *base)
1843 {
1844         PdfDocument *pdf_document;
1845         double width_points, height_points;
1846         gint width, height;
1847
1848         pdf_document = PDF_DOCUMENT (selection);
1849         set_rc_data (pdf_document, rc);
1850
1851         poppler_page_get_size (POPPLER_PAGE (rc->data),
1852                                &width_points, &height_points);
1853         width = (int) ((width_points * rc->scale) + 0.5);
1854         height = (int) ((height_points * rc->scale) + 0.5);
1855
1856 #ifdef HAVE_POPPLER_PAGE_RENDER
1857         cairo_t *cr;
1858
1859         if (*surface == NULL) {
1860                 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1861                                                        width, height);
1862                 
1863         }
1864
1865         cr = cairo_create (*surface);
1866         cairo_scale (cr, rc->scale, rc->scale);
1867         cairo_surface_set_device_offset (*surface, 0, 0);
1868         memset (cairo_image_surface_get_data (*surface), 0x00,
1869                 cairo_image_surface_get_height (*surface) *
1870                 cairo_image_surface_get_stride (*surface));
1871         poppler_page_render_selection (POPPLER_PAGE (rc->data),
1872                                        cr,
1873                                        (PopplerRectangle *)points,
1874                                        (PopplerRectangle *)old_points,
1875                                        (PopplerSelectionStyle)style,
1876                                        text,
1877                                        base);
1878         cairo_destroy (cr);
1879 #else /* HAVE_POPPLER_PAGE_RENDER */
1880         GdkPixbuf *pixbuf;
1881         
1882         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1883                                  TRUE, 8,
1884                                  width, height);
1885
1886         poppler_page_render_selection_to_pixbuf (POPPLER_PAGE (rc->data),
1887                                                  rc->scale, rc->rotation, pixbuf,
1888                                                  (PopplerRectangle *)points,
1889                                                  (PopplerRectangle *)old_points,
1890                                                  (PopplerSelectionStyle)style,
1891                                                  text,
1892                                                  base);
1893         if (*surface)
1894                 cairo_surface_destroy (*surface);
1895         *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1896         g_object_unref (pixbuf);
1897 #endif /* HAVE_POPPLER_PAGE_RENDER */
1898 }
1899
1900 static gchar *
1901 pdf_selection_get_selected_text (EvSelection     *selection,
1902                                  EvRenderContext *rc,
1903                                  EvSelectionStyle style,
1904                                  EvRectangle     *points)
1905 {
1906         PdfDocument *pdf_document = PDF_DOCUMENT (selection);
1907         PopplerPage *poppler_page;
1908         PopplerRectangle r;
1909         double height;
1910         char *retval;
1911         
1912         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1913         g_return_val_if_fail (poppler_page != NULL, NULL);
1914
1915         poppler_page_get_size (poppler_page, NULL, &height);
1916         r.x1 = points->x1;
1917         r.y1 = height - points->y2;
1918         r.x2 = points->x2;
1919         r.y2 = height - points->y1;
1920
1921         retval = poppler_page_get_text (poppler_page,
1922                                         (PopplerSelectionStyle)style,
1923                                         &r);
1924
1925         g_object_unref (poppler_page);
1926
1927         return retval;
1928 }
1929
1930 static GdkRegion *
1931 pdf_selection_get_selection_region (EvSelection     *selection,
1932                                     EvRenderContext *rc,
1933                                     EvSelectionStyle style,
1934                                     EvRectangle     *points)
1935 {
1936         PdfDocument *pdf_document;
1937         GdkRegion *retval;
1938
1939         pdf_document = PDF_DOCUMENT (selection);
1940
1941         set_rc_data (pdf_document, rc);
1942
1943         retval = poppler_page_get_selection_region ((PopplerPage *)rc->data,
1944                                                     rc->scale,
1945                                                     (PopplerSelectionStyle)style,
1946                                                     (PopplerRectangle *) points);
1947         return retval;
1948 }
1949
1950 static GdkRegion *
1951 pdf_selection_get_selection_map (EvSelection     *selection,
1952                                  EvRenderContext *rc)
1953 {
1954         PdfDocument *pdf_document;
1955         PopplerPage *poppler_page;
1956         PopplerRectangle points;
1957         GdkRegion *retval;
1958
1959         pdf_document = PDF_DOCUMENT (selection);
1960         poppler_page = poppler_document_get_page (pdf_document->document,
1961                                                   rc->page);
1962
1963         points.x1 = 0.0;
1964         points.y1 = 0.0;
1965         poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1966         retval = poppler_page_get_selection_region (poppler_page, 1.0,
1967                                                     POPPLER_SELECTION_GLYPH,
1968                                                     &points);
1969         g_object_unref (poppler_page);
1970
1971         return retval;
1972 }
1973
1974 static void
1975 pdf_selection_iface_init (EvSelectionIface *iface)
1976 {
1977         iface->render_selection = pdf_selection_render_selection;
1978         iface->get_selected_text = pdf_selection_get_selected_text;
1979         iface->get_selection_region = pdf_selection_get_selection_region;
1980         iface->get_selection_map = pdf_selection_get_selection_map;
1981 }
1982
1983 /* Page Transitions */
1984 static gdouble
1985 pdf_document_get_page_duration (EvDocumentTransition *trans,
1986                                 gint                  page)
1987 {
1988         PdfDocument *pdf_document;
1989         PopplerPage *poppler_page;
1990         gdouble      duration = -1;
1991
1992         pdf_document = PDF_DOCUMENT (trans);
1993         poppler_page = poppler_document_get_page (pdf_document->document, page);
1994         if (!poppler_page)
1995                 return -1;
1996
1997         duration = poppler_page_get_duration (poppler_page);
1998         g_object_unref (poppler_page);
1999
2000         return duration;
2001 }
2002
2003 static EvTransitionEffect *
2004 pdf_document_get_effect (EvDocumentTransition *trans,
2005                          gint                  page)
2006 {
2007         PdfDocument            *pdf_document;
2008         PopplerPage            *poppler_page;
2009         PopplerPageTransition  *page_transition;
2010         EvTransitionEffect     *effect;
2011
2012         pdf_document = PDF_DOCUMENT (trans);
2013         poppler_page = poppler_document_get_page (pdf_document->document, page);
2014
2015         if (!poppler_page)
2016                 return NULL;
2017
2018         page_transition = poppler_page_get_transition (poppler_page);
2019
2020         if (!page_transition) {
2021                 g_object_unref (poppler_page);
2022                 return NULL;
2023         }
2024
2025         /* enums in PopplerPageTransition match the EvTransitionEffect ones */
2026         effect = ev_transition_effect_new ((EvTransitionEffectType) page_transition->type,
2027                                            "alignment", page_transition->alignment,
2028                                            "direction", page_transition->direction,
2029                                            "duration", page_transition->duration,
2030                                            "angle", page_transition->angle,
2031                                            "scale", page_transition->scale,
2032                                            "rectangular", page_transition->rectangular,
2033                                            NULL);
2034
2035         poppler_page_transition_free (page_transition);
2036         g_object_unref (poppler_page);
2037
2038         return effect;
2039 }
2040
2041 static void
2042 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
2043 {
2044         iface->get_page_duration = pdf_document_get_page_duration;
2045         iface->get_effect = pdf_document_get_effect;
2046 }
2047
2048 /* Forms */
2049 static void
2050 pdf_document_get_crop_box (EvDocument  *document, 
2051                            int          page, 
2052                            EvRectangle *rect)
2053 {
2054         PdfDocument *pdf_document;
2055         PopplerPage *poppler_page;
2056         PopplerRectangle poppler_rect;
2057
2058         pdf_document = PDF_DOCUMENT (document);
2059         poppler_page = poppler_document_get_page (pdf_document->document, page);
2060         poppler_page_get_crop_box (poppler_page, &poppler_rect);
2061         rect->x1 = poppler_rect.x1;
2062         rect->x2 = poppler_rect.x2;
2063         rect->y1 = poppler_rect.y1;
2064         rect->y2 = poppler_rect.y2;
2065 }
2066
2067 static EvFormField *
2068 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
2069 {
2070         EvFormField *ev_field = NULL;
2071         gint         id;
2072         gdouble      font_size;
2073         gboolean     is_read_only;
2074
2075         id = poppler_form_field_get_id (poppler_field);
2076         font_size = poppler_form_field_get_font_size (poppler_field);
2077         is_read_only = poppler_form_field_is_read_only (poppler_field);
2078
2079         switch (poppler_form_field_get_field_type (poppler_field)) {
2080                 case POPPLER_FORM_FIELD_TEXT: {
2081                         EvFormFieldText    *field_text;
2082                         EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2083
2084                         switch (poppler_form_field_text_get_text_type (poppler_field)) {
2085                                 case POPPLER_FORM_TEXT_NORMAL:
2086                                         ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2087                                         break;
2088                                 case POPPLER_FORM_TEXT_MULTILINE:
2089                                         ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2090                                         break;
2091                                 case POPPLER_FORM_TEXT_FILE_SELECT:
2092                                         ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2093                                         break;
2094                         }
2095                         
2096                         ev_field = ev_form_field_text_new (id, ev_text_type);
2097                         field_text = EV_FORM_FIELD_TEXT (ev_field);
2098
2099                         field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2100                         field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2101                         field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2102                         field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2103                         field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2104                         field_text->text = poppler_form_field_text_get_text (poppler_field);
2105
2106                 }
2107                         break;
2108                 case POPPLER_FORM_FIELD_BUTTON: {
2109                         EvFormFieldButton    *field_button;
2110                         EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2111
2112                         switch (poppler_form_field_button_get_button_type (poppler_field)) {
2113                                 case POPPLER_FORM_BUTTON_PUSH:
2114                                         ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2115                                         break;
2116                                 case POPPLER_FORM_BUTTON_CHECK:
2117                                         ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2118                                         break;
2119                                 case POPPLER_FORM_BUTTON_RADIO:
2120                                         ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2121                                         break;
2122                         }
2123
2124                         ev_field = ev_form_field_button_new (id, ev_button_type);
2125                         field_button = EV_FORM_FIELD_BUTTON (ev_field);
2126                         
2127                         field_button->state = poppler_form_field_button_get_state (poppler_field);
2128                 }
2129                         break;
2130                 case POPPLER_FORM_FIELD_CHOICE: {
2131                         EvFormFieldChoice    *field_choice;
2132                         EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2133
2134                         switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2135                                 case POPPLER_FORM_CHOICE_COMBO:
2136                                         ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2137                                         break;
2138                                 case EV_FORM_FIELD_CHOICE_LIST:
2139                                         ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2140                                         break;
2141                         }
2142
2143                         ev_field = ev_form_field_choice_new (id, ev_choice_type);
2144                         field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2145
2146                         field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2147                         field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2148                         field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2149                         field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2150
2151                         /* TODO: we need poppler_form_field_choice_get_selected_items in poppler 
2152                         field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2153                         if (field_choice->is_editable)
2154                                 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2155                 }
2156                         break;
2157                 case POPPLER_FORM_FIELD_SIGNATURE:
2158                         /* TODO */
2159                         ev_field = ev_form_field_signature_new (id);
2160                         break;
2161                 case POPPLER_FORM_FIELD_UNKNOWN:
2162                         return NULL;
2163         }
2164
2165         ev_field->font_size = font_size;
2166         ev_field->is_read_only = is_read_only;
2167
2168         return ev_field;
2169 }
2170
2171 static GList *
2172 pdf_document_forms_get_form_fields (EvDocumentForms *document, 
2173                                     gint             page)
2174 {
2175         PdfDocument *pdf_document;
2176         PopplerPage *poppler_page;
2177         GList *retval = NULL;
2178         GList *fields;
2179         GList *list;
2180         double height;
2181
2182         pdf_document = PDF_DOCUMENT (document);
2183         poppler_page = poppler_document_get_page (pdf_document->document, page);
2184         fields = poppler_page_get_form_field_mapping (poppler_page);
2185         poppler_page_get_size (poppler_page, NULL, &height);
2186
2187         for (list = fields; list; list = list->next) {
2188                 PopplerFormFieldMapping *mapping;
2189                 EvFormFieldMapping *field_mapping;
2190                 EvFormField *ev_field;
2191
2192                 mapping = (PopplerFormFieldMapping *)list->data;
2193
2194                 ev_field = ev_form_field_from_poppler_field (mapping->field);
2195                 if (!ev_field)
2196                         continue;
2197
2198                 field_mapping = g_new0 (EvFormFieldMapping, 1);
2199                 field_mapping->x1 = mapping->area.x1;
2200                 field_mapping->x2 = mapping->area.x2;
2201                 field_mapping->y1 = height - mapping->area.y2;
2202                 field_mapping->y2 = height - mapping->area.y1;
2203                 field_mapping->field = ev_field;
2204                 field_mapping->field->page = page;
2205                 
2206                 retval = g_list_prepend (retval, field_mapping);
2207         }
2208         
2209         poppler_page_free_form_field_mapping (fields);
2210         g_object_unref (poppler_page);
2211
2212         return g_list_reverse (retval);
2213 }
2214
2215 static gchar *
2216 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2217                                              EvFormField     *field)
2218         
2219 {
2220         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2221         PopplerFormField *poppler_field;
2222         gchar *text;
2223
2224         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2225         if (!poppler_field)
2226                 return NULL;
2227         
2228         text = poppler_form_field_text_get_text (poppler_field);
2229         g_object_unref (poppler_field);
2230
2231         return text;
2232 }
2233
2234 static void
2235 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document, 
2236                                              EvFormField     *field,
2237                                              const gchar     *text)
2238 {
2239         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2240         PopplerFormField *poppler_field;
2241
2242         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2243         if (!poppler_field)
2244                 return;
2245         poppler_form_field_text_set_text (poppler_field, text);
2246         g_object_unref (poppler_field);
2247 }
2248
2249 static void
2250 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document, 
2251                                                 EvFormField     *field,
2252                                                 gboolean         state)
2253 {
2254         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2255         PopplerFormField *poppler_field;
2256
2257         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2258         if (!poppler_field)
2259                 return;
2260         
2261         poppler_form_field_button_set_state (poppler_field, state);
2262         g_object_unref (poppler_field);
2263 }
2264
2265 static gboolean
2266 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document, 
2267                                                 EvFormField     *field)
2268 {
2269         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2270         PopplerFormField *poppler_field;
2271         gboolean state;
2272
2273         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2274         if (!poppler_field)
2275                 return FALSE;
2276
2277         state = poppler_form_field_button_get_state (poppler_field);
2278         g_object_unref (poppler_field);
2279
2280         return state;
2281 }
2282
2283 static gchar *
2284 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document, 
2285                                                EvFormField     *field,
2286                                                gint             index)
2287 {
2288         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2289         PopplerFormField *poppler_field;
2290         gchar *text;
2291
2292         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2293         if (!poppler_field)
2294                 return NULL;
2295
2296         text = poppler_form_field_choice_get_item (poppler_field, index);
2297         g_object_unref (poppler_field);
2298
2299         return text;
2300 }
2301
2302 static int
2303 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document, 
2304                                                   EvFormField     *field)
2305 {
2306         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2307         PopplerFormField *poppler_field;
2308         gint n_items;
2309
2310         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2311         if (!poppler_field)
2312                 return -1;
2313         
2314         n_items = poppler_form_field_choice_get_n_items (poppler_field);
2315         g_object_unref (poppler_field);
2316
2317         return n_items;
2318 }
2319
2320 static gboolean
2321 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document, 
2322                                                        EvFormField     *field,
2323                                                        gint             index)
2324 {
2325         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2326         PopplerFormField *poppler_field;
2327         gboolean selected;
2328
2329         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2330         if (!poppler_field)
2331                 return FALSE;
2332
2333         selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2334         g_object_unref (poppler_field);
2335
2336         return selected;
2337 }
2338
2339 static void
2340 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document, 
2341                                                   EvFormField     *field,
2342                                                   gint             index)
2343 {
2344         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2345         PopplerFormField *poppler_field;
2346
2347         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2348         if (!poppler_field)
2349                 return;
2350
2351         poppler_form_field_choice_select_item (poppler_field, index);
2352         g_object_unref (poppler_field);
2353 }
2354
2355 static void
2356 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document, 
2357                                                   EvFormField     *field,
2358                                                   gint             index)
2359 {
2360         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2361         PopplerFormField *poppler_field;
2362
2363         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2364         if (!poppler_field)
2365                 return;
2366
2367         poppler_form_field_choice_toggle_item (poppler_field, index);
2368         g_object_unref (poppler_field);
2369 }
2370
2371 static void
2372 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document, 
2373                                                    EvFormField     *field)
2374 {
2375         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2376         PopplerFormField *poppler_field;
2377
2378         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2379         if (!poppler_field)
2380                 return;
2381         
2382         poppler_form_field_choice_unselect_all (poppler_field);
2383         g_object_unref (poppler_field);
2384 }
2385
2386 static void
2387 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2388                                                EvFormField     *field,
2389                                                const gchar     *text)
2390 {
2391         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2392         PopplerFormField *poppler_field;
2393
2394         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2395         if (!poppler_field)
2396                 return;
2397         
2398         poppler_form_field_choice_set_text (poppler_field, text);
2399         g_object_unref (poppler_field);
2400 }
2401
2402 static gchar *
2403 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2404                                                EvFormField     *field)
2405 {
2406         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2407         PopplerFormField *poppler_field;
2408         gchar *text;
2409
2410         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2411         if (!poppler_field)
2412                 return NULL;
2413
2414         text = poppler_form_field_choice_get_text (poppler_field);
2415         g_object_unref (poppler_field);
2416
2417         return text;
2418 }
2419
2420 static void
2421 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2422 {
2423         iface->get_form_fields = pdf_document_forms_get_form_fields;
2424         iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2425         iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2426         iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2427         iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2428         iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2429         iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2430         iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2431         iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2432         iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2433         iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2434         iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2435         iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2436 }
2437