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