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