]> www.fi.muni.cz Git - evince.git/blob - backend/pdf/ev-poppler.cc
Ignore outline items without a title. Fixes bug #453913.
[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 || strlen (ev_link_get_title (link)) <= 0) {
1064                         poppler_action_free (action);
1065                         if (link)
1066                                 g_object_unref (link);
1067                         
1068                         continue;
1069                 }
1070
1071                 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
1072                 title_markup = g_markup_escape_text (ev_link_get_title (link), -1);
1073                 
1074                 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
1075                                     EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
1076                                     EV_DOCUMENT_LINKS_COLUMN_LINK, link,
1077                                     EV_DOCUMENT_LINKS_COLUMN_EXPAND, expand,
1078                                     -1);
1079                 
1080                 g_free (title_markup);
1081                 g_object_unref (link);
1082                 
1083                 child = poppler_index_iter_get_child (iter);
1084                 if (child)
1085                         build_tree (pdf_document, model, &tree_iter, child);
1086                 poppler_index_iter_free (child);
1087                 poppler_action_free (action);
1088                 
1089         } while (poppler_index_iter_next (iter));
1090 }
1091
1092 static GtkTreeModel *
1093 pdf_document_links_get_links_model (EvDocumentLinks *document_links)
1094 {
1095         PdfDocument *pdf_document = PDF_DOCUMENT (document_links);
1096         GtkTreeModel *model = NULL;
1097         PopplerIndexIter *iter;
1098         
1099         g_return_val_if_fail (PDF_IS_DOCUMENT (document_links), NULL);
1100
1101         iter = poppler_index_iter_new (pdf_document->document);
1102         /* Create the model if we have items*/
1103         if (iter != NULL) {
1104                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
1105                                                              G_TYPE_STRING,
1106                                                              G_TYPE_OBJECT,
1107                                                              G_TYPE_BOOLEAN,
1108                                                              G_TYPE_STRING);
1109                 build_tree (pdf_document, model, NULL, iter);
1110                 poppler_index_iter_free (iter);
1111         }
1112         
1113         return model;
1114 }
1115
1116 static GList *
1117 pdf_document_links_get_links (EvDocumentLinks *document_links,
1118                               gint             page)
1119 {
1120         PdfDocument *pdf_document;
1121         PopplerPage *poppler_page;
1122         GList *retval = NULL;
1123         GList *mapping_list;
1124         GList *list;
1125         double height;
1126
1127         pdf_document = PDF_DOCUMENT (document_links);
1128         poppler_page = poppler_document_get_page (pdf_document->document,
1129                                                   page);
1130         mapping_list = poppler_page_get_link_mapping (poppler_page);
1131         poppler_page_get_size (poppler_page, NULL, &height);
1132
1133         for (list = mapping_list; list; list = list->next) {
1134                 PopplerLinkMapping *link_mapping;
1135                 EvLinkMapping *ev_link_mapping;
1136
1137                 link_mapping = (PopplerLinkMapping *)list->data;
1138                 ev_link_mapping = g_new (EvLinkMapping, 1);
1139                 ev_link_mapping->link = ev_link_from_action (pdf_document,
1140                                                              link_mapping->action);
1141                 ev_link_mapping->x1 = link_mapping->area.x1;
1142                 ev_link_mapping->x2 = link_mapping->area.x2;
1143                 /* Invert this for X-style coordinates */
1144                 ev_link_mapping->y1 = height - link_mapping->area.y2;
1145                 ev_link_mapping->y2 = height - link_mapping->area.y1;
1146
1147                 retval = g_list_prepend (retval, ev_link_mapping);
1148         }
1149
1150         poppler_page_free_link_mapping (mapping_list);
1151         g_object_unref (poppler_page);
1152
1153         return g_list_reverse (retval);
1154 }
1155
1156 static EvLinkDest *
1157 pdf_document_links_find_link_dest (EvDocumentLinks  *document_links,
1158                                    const gchar      *link_name)
1159 {
1160         PdfDocument *pdf_document;
1161         PopplerDest *dest;
1162         EvLinkDest *ev_dest = NULL;
1163
1164         pdf_document = PDF_DOCUMENT (document_links);
1165         dest = poppler_document_find_dest (pdf_document->document,
1166                                            link_name);
1167         if (dest) {
1168                 ev_dest = ev_link_dest_from_dest (pdf_document, dest);
1169                 poppler_dest_free (dest);
1170         }
1171
1172         return ev_dest;
1173 }
1174
1175 static void
1176 pdf_document_document_links_iface_init (EvDocumentLinksIface *iface)
1177 {
1178         iface->has_document_links = pdf_document_links_has_document_links;
1179         iface->get_links_model = pdf_document_links_get_links_model;
1180         iface->get_links = pdf_document_links_get_links;
1181         iface->find_link_dest = pdf_document_links_find_link_dest;
1182 }
1183
1184 static GList *
1185 pdf_document_images_get_images (EvDocumentImages *document_images,
1186                                 gint              page)
1187 {
1188         GList *retval = NULL;
1189         PdfDocument *pdf_document;
1190         PopplerPage *poppler_page;
1191         GList *mapping_list;
1192         GList *list;
1193
1194         pdf_document = PDF_DOCUMENT (document_images);
1195         poppler_page = poppler_document_get_page (pdf_document->document, page);
1196         mapping_list = poppler_page_get_image_mapping (poppler_page);
1197
1198         for (list = mapping_list; list; list = list->next) {
1199                 PopplerImageMapping *image_mapping;
1200                 EvImageMapping *ev_image_mapping;
1201
1202                 image_mapping = (PopplerImageMapping *)list->data;
1203
1204                 ev_image_mapping = g_new (EvImageMapping, 1);
1205                 
1206                 ev_image_mapping->image = ev_image_new_from_pixbuf (image_mapping->image);
1207                 ev_image_mapping->x1 = image_mapping->area.x1;
1208                 ev_image_mapping->x2 = image_mapping->area.x2;
1209                 ev_image_mapping->y1 = image_mapping->area.y1;
1210                 ev_image_mapping->y2 = image_mapping->area.y2;
1211
1212                 retval = g_list_prepend (retval, ev_image_mapping);
1213         }
1214
1215         poppler_page_free_image_mapping (mapping_list);
1216         g_object_unref (poppler_page);
1217
1218         return retval;
1219 }
1220
1221 static void
1222 pdf_document_document_images_iface_init (EvDocumentImagesIface *iface)
1223 {
1224         iface->get_images = pdf_document_images_get_images;
1225 }
1226
1227 static GdkPixbuf *
1228 make_thumbnail_for_page (PdfDocument     *pdf_document,
1229                          PopplerPage     *poppler_page, 
1230                          EvRenderContext *rc)
1231 {
1232         GdkPixbuf *pixbuf;
1233         int width, height;
1234
1235         pdf_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (pdf_document),
1236                                                 rc, &width, &height);
1237
1238         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1239                                  width, height);
1240         gdk_pixbuf_fill (pixbuf, 0xffffffff);
1241
1242         ev_document_fc_mutex_lock ();
1243         poppler_page_render_to_pixbuf (poppler_page, 0, 0,
1244                                        width, height,
1245                                        rc->scale, rc->rotation, pixbuf);
1246         ev_document_fc_mutex_unlock ();
1247
1248         return pixbuf;
1249 }
1250
1251 static GdkPixbuf *
1252 pdf_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
1253                                        EvRenderContext      *rc, 
1254                                        gboolean              border)
1255 {
1256         PdfDocument *pdf_document;
1257         PopplerPage *poppler_page;
1258         GdkPixbuf *pixbuf;
1259         GdkPixbuf *border_pixbuf;
1260
1261         pdf_document = PDF_DOCUMENT (document_thumbnails);
1262
1263         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1264         g_return_val_if_fail (poppler_page != NULL, NULL);
1265
1266         pixbuf = poppler_page_get_thumbnail (poppler_page);
1267         if (pixbuf) {
1268                 /* Rotate provided thumbnail if needed */
1269                 GdkPixbuf *rotated_pixbuf;
1270
1271                 rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf,
1272                                                            (GdkPixbufRotation) (360 - rc->rotation));
1273                 g_object_unref (pixbuf);
1274                 pixbuf = rotated_pixbuf;
1275         } else {
1276                 /* There is no provided thumbnail.  We need to make one. */
1277                 pixbuf = make_thumbnail_for_page (pdf_document, poppler_page, rc);
1278         }
1279
1280         if (border) {           
1281                 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
1282                 g_object_unref (pixbuf);
1283                 pixbuf = border_pixbuf;
1284         }               
1285
1286         g_object_unref (poppler_page);
1287         
1288         return pixbuf;
1289 }
1290
1291 static void
1292 pdf_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
1293                                         EvRenderContext      *rc,
1294                                         gint                 *width,
1295                                         gint                 *height)
1296 {
1297         PdfDocument *pdf_document;
1298         PopplerPage *poppler_page;
1299         gint has_thumb;
1300         
1301         pdf_document = PDF_DOCUMENT (document_thumbnails);
1302         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1303
1304         g_return_if_fail (poppler_page != NULL);
1305
1306         has_thumb = poppler_page_get_thumbnail_size (poppler_page, width, height);
1307
1308         if (!has_thumb) {
1309                 double page_width, page_height;
1310
1311                 poppler_page_get_size (poppler_page, &page_width, &page_height);
1312
1313                 *width = (gint) MAX (page_width * rc->scale, 1);
1314                 *height = (gint) MAX (page_height * rc->scale, 1);
1315         }
1316         
1317         if (rc->rotation == 90 || rc->rotation == 270) {
1318                 gint  temp;
1319
1320                 temp = *width;
1321                 *width = *height;
1322                 *height = temp;
1323         }
1324         
1325         g_object_unref (poppler_page);
1326 }
1327
1328 static void
1329 pdf_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
1330 {
1331         iface->get_thumbnail = pdf_document_thumbnails_get_thumbnail;
1332         iface->get_dimensions = pdf_document_thumbnails_get_dimensions;
1333 }
1334
1335
1336 static gboolean
1337 pdf_document_search_idle_callback (void *data)
1338 {
1339         PdfDocumentSearch *search = (PdfDocumentSearch*) data;
1340         PdfDocument *pdf_document = search->document;
1341         int n_pages;
1342         GList *matches;
1343         PopplerPage *page;
1344
1345         page = poppler_document_get_page (search->document->document,
1346                                           search->search_page);
1347
1348         ev_document_doc_mutex_lock ();
1349         matches = poppler_page_find_text (page, search->text);
1350         ev_document_doc_mutex_unlock ();
1351
1352         g_object_unref (page);
1353
1354         search->pages[search->search_page] = matches;
1355         ev_document_find_changed (EV_DOCUMENT_FIND (pdf_document),
1356                                   search->search_page);
1357
1358         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (search->document));
1359         search->search_page += 1;
1360         if (search->search_page == n_pages) {
1361                 /* wrap around */
1362                 search->search_page = 0;
1363         }
1364
1365         if (search->search_page != search->start_page) {
1366                 return TRUE;
1367         }
1368
1369         /* We're done. */
1370         search->idle = 0; /* will return FALSE to remove */
1371         return FALSE;
1372 }
1373
1374
1375 static PdfDocumentSearch *
1376 pdf_document_search_new (PdfDocument *pdf_document,
1377                          int          start_page,
1378                          const char  *text)
1379 {
1380         PdfDocumentSearch *search;
1381         int n_pages;
1382         int i;
1383
1384         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (pdf_document));
1385
1386         search = g_new0 (PdfDocumentSearch, 1);
1387
1388         search->text = g_strdup (text);
1389         search->pages = g_new0 (GList *, n_pages);
1390         search->document = pdf_document;
1391
1392         /* We add at low priority so the progress bar repaints */
1393         search->idle = g_idle_add_full (G_PRIORITY_LOW,
1394                                         pdf_document_search_idle_callback,
1395                                         search,
1396                                         NULL);
1397
1398         search->start_page = start_page;
1399         search->search_page = start_page;
1400
1401         return search;
1402 }
1403
1404 static void
1405 pdf_document_find_begin (EvDocumentFind   *document,
1406                          int               page,
1407                          const char       *search_string,
1408                          gboolean          case_sensitive)
1409 {
1410         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1411
1412         /* FIXME handle case_sensitive (right now XPDF
1413          * code is always case insensitive for ASCII
1414          * and case sensitive for all other languaages)
1415          */
1416
1417         if (pdf_document->search &&
1418             strcmp (search_string, pdf_document->search->text) == 0)
1419                 return;
1420
1421         if (pdf_document->search)
1422                 pdf_document_search_free (pdf_document->search);
1423
1424         pdf_document->search = pdf_document_search_new (pdf_document,
1425                                                         page,
1426                                                         search_string);
1427 }
1428
1429 static int
1430 pdf_document_find_get_n_results (EvDocumentFind *document_find, int page)
1431 {
1432         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1433
1434         if (search) {
1435                 return g_list_length (search->pages[page]);
1436         } else {
1437                 return 0;
1438         }
1439 }
1440
1441 static gboolean
1442 pdf_document_find_get_result (EvDocumentFind *document_find,
1443                               int             page,
1444                               int             n_result,
1445                               EvRectangle    *rectangle)
1446 {
1447         PdfDocument *pdf_document = PDF_DOCUMENT (document_find);
1448         PdfDocumentSearch *search = pdf_document->search;
1449         PopplerPage *poppler_page;
1450         PopplerRectangle *r;
1451         double height;
1452
1453         if (search == NULL)
1454                 return FALSE;
1455
1456         r = (PopplerRectangle *) g_list_nth_data (search->pages[page],
1457                                                   n_result);
1458         if (r == NULL)
1459                 return FALSE;
1460
1461         poppler_page = poppler_document_get_page (pdf_document->document, page);
1462         poppler_page_get_size (poppler_page, NULL, &height);
1463         rectangle->x1 = r->x1;
1464         rectangle->y1 = height - r->y2;
1465         rectangle->x2 = r->x2;
1466         rectangle->y2 = height - r->y1;
1467         g_object_unref (poppler_page);
1468                 
1469         return TRUE;
1470 }
1471
1472 static int
1473 pdf_document_find_page_has_results (EvDocumentFind *document_find,
1474                                     int             page)
1475 {
1476         PdfDocumentSearch *search = PDF_DOCUMENT (document_find)->search;
1477
1478         return search && search->pages[page] != NULL;
1479 }
1480
1481 static double
1482 pdf_document_find_get_progress (EvDocumentFind *document_find)
1483 {
1484         PdfDocumentSearch *search;
1485         int n_pages, pages_done;
1486
1487         search = PDF_DOCUMENT (document_find)->search;
1488
1489         if (search == NULL) {
1490                 return 0;
1491         }
1492
1493         n_pages = pdf_document_get_n_pages (EV_DOCUMENT (document_find));
1494         if (search->search_page > search->start_page) {
1495                 pages_done = search->search_page - search->start_page + 1;
1496         } else if (search->search_page == search->start_page) {
1497                 pages_done = n_pages;
1498         } else {
1499                 pages_done = n_pages - search->start_page + search->search_page;
1500         }
1501
1502         return pages_done / (double) n_pages;
1503 }
1504
1505 static void
1506 pdf_document_find_cancel (EvDocumentFind *document)
1507 {
1508         PdfDocument *pdf_document = PDF_DOCUMENT (document);
1509
1510         if (pdf_document->search) {
1511                 pdf_document_search_free (pdf_document->search);
1512                 pdf_document->search = NULL;
1513         }
1514 }
1515
1516 static void
1517 pdf_document_find_iface_init (EvDocumentFindIface *iface)
1518 {
1519         iface->begin = pdf_document_find_begin;
1520         iface->get_n_results = pdf_document_find_get_n_results;
1521         iface->get_result = pdf_document_find_get_result;
1522         iface->page_has_results = pdf_document_find_page_has_results;
1523         iface->get_progress = pdf_document_find_get_progress;
1524         iface->cancel = pdf_document_find_cancel;
1525 }
1526
1527 static void
1528 pdf_print_context_free (PdfPrintContext *ctx)
1529 {
1530         if (!ctx)
1531                 return;
1532
1533 #ifdef HAVE_CAIRO_PRINT
1534         if (ctx->cr) {
1535                 cairo_destroy (ctx->cr);
1536                 ctx->cr = NULL;
1537         }
1538 #else
1539         if (ctx->ps_file) {
1540                 poppler_ps_file_free (ctx->ps_file);
1541                 ctx->ps_file = NULL;
1542         }
1543 #endif
1544         g_free (ctx);
1545 }
1546
1547 static void
1548 pdf_document_file_exporter_begin (EvFileExporter        *exporter,
1549                                   EvFileExporterContext *fc)
1550 {
1551         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1552         PdfPrintContext *ctx;
1553         gdouble width, height;
1554 #ifdef HAVE_CAIRO_PRINT
1555         cairo_surface_t *surface = NULL;
1556 #endif
1557         
1558         if (pdf_document->print_ctx)
1559                 pdf_print_context_free (pdf_document->print_ctx);
1560         pdf_document->print_ctx = g_new0 (PdfPrintContext, 1);
1561         ctx = pdf_document->print_ctx;
1562         ctx->format = fc->format;
1563         ctx->pages_per_sheet = fc->pages_per_sheet;
1564
1565         ctx->paper_width = fc->paper_width;
1566         ctx->paper_height = fc->paper_height;
1567         
1568         switch (fc->pages_per_sheet) {
1569                 default:
1570                 case 1:
1571                         ctx->pages_x = 1;
1572                         ctx->pages_y = 1;
1573                         break;
1574                 case 2:
1575                         ctx->pages_x = 1;
1576                         ctx->pages_y = 2;
1577                         break;
1578                 case 4:
1579                         ctx->pages_x = 2;
1580                         ctx->pages_y = 2;
1581                         break;
1582                 case 6:
1583                         ctx->pages_x = 2;
1584                         ctx->pages_y = 3;
1585                         break;
1586                 case 9:
1587                         ctx->pages_x = 3;
1588                         ctx->pages_y = 3;
1589                         break;
1590                 case 16:
1591                         ctx->pages_x = 4;
1592                         ctx->pages_y = 4;
1593                         break;
1594         }
1595
1596         ctx->pages_printed = 0;
1597
1598         switch (fc->format) {
1599                 case EV_FILE_FORMAT_PS:
1600 #ifdef HAVE_CAIRO_PS
1601                         surface = cairo_ps_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1602 #else
1603                         ctx->ps_file = poppler_ps_file_new (pdf_document->document,
1604                                                             fc->filename, fc->first_page,
1605                                                             fc->last_page - fc->first_page + 1);
1606                         poppler_ps_file_set_paper_size (ctx->ps_file, fc->paper_width, fc->paper_height);
1607                         poppler_ps_file_set_duplex (ctx->ps_file, fc->duplex);
1608 #endif /* HAVE_CAIRO_PS */
1609                         break;
1610                 case EV_FILE_FORMAT_PDF:
1611 #ifdef HAVE_CAIRO_PDF
1612                         surface = cairo_pdf_surface_create (fc->filename, fc->paper_width, fc->paper_height);
1613 #endif
1614                         break;
1615                 default:
1616                         g_assert_not_reached ();
1617         }
1618
1619 #ifdef HAVE_CAIRO_PRINT
1620         ctx->cr = cairo_create (surface);
1621         cairo_surface_destroy (surface);
1622 #endif
1623 }
1624
1625 static void
1626 pdf_document_file_exporter_do_page (EvFileExporter  *exporter,
1627                                     EvRenderContext *rc)
1628 {
1629         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1630         PdfPrintContext *ctx = pdf_document->print_ctx;
1631         PopplerPage *poppler_page;
1632 #ifdef HAVE_CAIRO_PRINT
1633         gdouble  page_width, page_height;
1634         gint     x, y;
1635         gboolean rotate, landscape;
1636         gdouble  width, height;
1637         gdouble  pwidth, pheight;
1638         gdouble  xscale, yscale;
1639 #endif
1640
1641         g_return_if_fail (pdf_document->print_ctx != NULL);
1642
1643         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1644         
1645 #ifdef HAVE_CAIRO_PRINT
1646         x = (ctx->pages_printed % ctx->pages_per_sheet) % ctx->pages_x;
1647         y = (ctx->pages_printed % ctx->pages_per_sheet) / ctx->pages_x;
1648         poppler_page_get_size (poppler_page, &page_width, &page_height);
1649
1650         if (page_width > page_height && page_width > ctx->paper_width) {
1651                 rotate = TRUE;
1652         } else {
1653                 rotate = FALSE;
1654         }
1655
1656         landscape = (ctx->paper_width > ctx->paper_height);
1657
1658         /* Use always portrait mode and rotate when necessary */
1659         if (ctx->paper_width > ctx->paper_height) {
1660                 width = ctx->paper_height;
1661                 height = ctx->paper_width;
1662                 rotate = !rotate;
1663
1664                 cairo_ps_surface_set_size (cairo_get_target (ctx->cr),
1665                                            width, height);
1666         } else {
1667                 width = ctx->paper_width;
1668                 height = ctx->paper_height;
1669         }
1670
1671         if (ctx->pages_per_sheet == 2 || ctx->pages_per_sheet == 6) {
1672                 rotate = !rotate;
1673         }       
1674
1675         if (rotate) {
1676                 gint tmp1;
1677                 gdouble tmp2;
1678
1679                 tmp1 = x;
1680                 x = y;
1681                 y = tmp1;
1682
1683                 tmp2 = page_width;
1684                 page_width = page_height;
1685                 page_height = tmp2;
1686
1687         }
1688
1689         pwidth = width / ctx->pages_x;
1690         pheight = height / ctx->pages_y;
1691
1692         if ((page_width > pwidth || page_height > pheight) ||
1693             (page_width < pwidth && page_height < pheight)) {
1694                 xscale = pwidth / page_width;
1695                 yscale = pheight / page_height;
1696                 
1697                 if (yscale < xscale) {
1698                         xscale = yscale;
1699                 } else {
1700                         yscale = xscale;
1701                 }
1702                 
1703         } else {        
1704                 xscale = yscale = 1;
1705         }
1706
1707         /* TODO: center */
1708
1709         cairo_save (ctx->cr);
1710         if (rotate) {
1711                 cairo_matrix_t matrix;
1712                 
1713                 cairo_translate (ctx->cr, width, 0);
1714                 cairo_matrix_init (&matrix,
1715                                    0,  1,
1716                                    -1,  0,
1717                                    0,  0);
1718                 cairo_transform (ctx->cr, &matrix);
1719         }
1720         
1721         cairo_translate (ctx->cr,
1722                          x * (rotate ? pheight : pwidth),
1723                          y * (rotate ? pwidth : pheight));
1724         cairo_scale (ctx->cr, xscale, yscale);
1725         
1726 #ifdef HAVE_POPPLER_PAGE_RENDER
1727         poppler_page_render (poppler_page, ctx->cr);
1728 #endif
1729         ctx->pages_printed++;
1730                         
1731         if (ctx->pages_printed % ctx->pages_per_sheet == 0) {
1732                 cairo_show_page (ctx->cr);
1733         }
1734         cairo_restore (ctx->cr);
1735 #else /* HAVE_CAIRO_PRINT */
1736         if (ctx->format == EV_FILE_FORMAT_PS)
1737                 poppler_page_render_to_ps (poppler_page, ctx->ps_file);
1738 #endif /* HAVE_CAIRO_PRINT */
1739         
1740         g_object_unref (poppler_page);
1741 }
1742
1743 static void
1744 pdf_document_file_exporter_end (EvFileExporter *exporter)
1745 {
1746         PdfDocument *pdf_document = PDF_DOCUMENT (exporter);
1747
1748         pdf_print_context_free (pdf_document->print_ctx);
1749         pdf_document->print_ctx = NULL;
1750 }
1751
1752 static EvFileExporterCapabilities
1753 pdf_document_file_exporter_get_capabilities (EvFileExporter *exporter)
1754 {
1755         return  (EvFileExporterCapabilities) (
1756                 EV_FILE_EXPORTER_CAN_PAGE_SET |
1757                 EV_FILE_EXPORTER_CAN_COPIES |
1758                 EV_FILE_EXPORTER_CAN_COLLATE |
1759                 EV_FILE_EXPORTER_CAN_REVERSE |
1760                 EV_FILE_EXPORTER_CAN_SCALE |
1761 #ifdef HAVE_CAIRO_PRINT
1762 #ifdef HAVE_POPPLER_PAGE_RENDER
1763 #if GTK_CHECK_VERSION (2, 11, 1)
1764                 EV_FILE_EXPORTER_CAN_NUMBER_UP |
1765 #endif
1766 #endif
1767 #endif
1768                 
1769 #ifdef HAVE_CAIRO_PDF
1770 #ifdef HAVE_POPPLER_PAGE_RENDER
1771                 EV_FILE_EXPORTER_CAN_GENERATE_PDF |
1772 #endif
1773 #endif
1774                 EV_FILE_EXPORTER_CAN_GENERATE_PS);
1775 }
1776
1777 static void
1778 pdf_document_file_exporter_iface_init (EvFileExporterIface *iface)
1779 {
1780         iface->begin = pdf_document_file_exporter_begin;
1781         iface->do_page = pdf_document_file_exporter_do_page;
1782         iface->end = pdf_document_file_exporter_end;
1783         iface->get_capabilities = pdf_document_file_exporter_get_capabilities;
1784 }
1785
1786 static void
1787 pdf_selection_render_selection (EvSelection      *selection,
1788                                 EvRenderContext  *rc,
1789                                 cairo_surface_t **surface,
1790                                 EvRectangle      *points,
1791                                 EvRectangle      *old_points,
1792                                 EvSelectionStyle  style,
1793                                 GdkColor         *text,
1794                                 GdkColor         *base)
1795 {
1796         PdfDocument *pdf_document;
1797         double width_points, height_points;
1798         gint width, height;
1799
1800         pdf_document = PDF_DOCUMENT (selection);
1801         set_rc_data (pdf_document, rc);
1802
1803         poppler_page_get_size (POPPLER_PAGE (rc->data),
1804                                &width_points, &height_points);
1805         width = (int) ((width_points * rc->scale) + 0.5);
1806         height = (int) ((height_points * rc->scale) + 0.5);
1807
1808 #ifdef HAVE_POPPLER_PAGE_RENDER
1809         cairo_t *cr;
1810
1811         if (*surface == NULL) {
1812                 *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1813                                                        width, height);
1814                 
1815         }
1816
1817         cr = cairo_create (*surface);
1818         cairo_scale (cr, rc->scale, rc->scale);
1819         cairo_surface_set_device_offset (*surface, 0, 0);
1820         memset (cairo_image_surface_get_data (*surface), 0x00,
1821                 cairo_image_surface_get_height (*surface) *
1822                 cairo_image_surface_get_stride (*surface));
1823         poppler_page_render_selection (POPPLER_PAGE (rc->data),
1824                                        cr,
1825                                        (PopplerRectangle *)points,
1826                                        (PopplerRectangle *)old_points,
1827                                        (PopplerSelectionStyle)style,
1828                                        text,
1829                                        base);
1830         cairo_destroy (cr);
1831 #else /* HAVE_POPPLER_PAGE_RENDER */
1832         GdkPixbuf *pixbuf;
1833         
1834         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1835                                  TRUE, 8,
1836                                  width, height);
1837
1838         poppler_page_render_selection_to_pixbuf (POPPLER_PAGE (rc->data),
1839                                                  rc->scale, rc->rotation, pixbuf,
1840                                                  (PopplerRectangle *)points,
1841                                                  (PopplerRectangle *)old_points,
1842                                                  (PopplerSelectionStyle)style,
1843                                                  text,
1844                                                  base);
1845         if (*surface)
1846                 cairo_surface_destroy (*surface);
1847         *surface = ev_document_misc_surface_from_pixbuf (pixbuf);
1848         g_object_unref (pixbuf);
1849 #endif /* HAVE_POPPLER_PAGE_RENDER */
1850 }
1851
1852 static gchar *
1853 pdf_selection_get_selected_text (EvSelection     *selection,
1854                                  EvRenderContext *rc,
1855                                  EvSelectionStyle style,
1856                                  EvRectangle     *points)
1857 {
1858         PdfDocument *pdf_document = PDF_DOCUMENT (selection);
1859         PopplerPage *poppler_page;
1860         PopplerRectangle r;
1861         double height;
1862         char *retval;
1863         
1864         poppler_page = poppler_document_get_page (pdf_document->document, rc->page);
1865         g_return_val_if_fail (poppler_page != NULL, NULL);
1866
1867         poppler_page_get_size (poppler_page, NULL, &height);
1868         r.x1 = points->x1;
1869         r.y1 = height - points->y2;
1870         r.x2 = points->x2;
1871         r.y2 = height - points->y1;
1872
1873         retval = poppler_page_get_text (poppler_page,
1874 #if 0
1875                                         (PopplerSelectionStyle)style,
1876 #endif
1877                                         &r);
1878
1879         g_object_unref (poppler_page);
1880
1881         return retval;
1882 }
1883
1884 static GdkRegion *
1885 pdf_selection_get_selection_region (EvSelection     *selection,
1886                                     EvRenderContext *rc,
1887                                     EvSelectionStyle style,
1888                                     EvRectangle     *points)
1889 {
1890         PdfDocument *pdf_document;
1891         GdkRegion *retval;
1892
1893         pdf_document = PDF_DOCUMENT (selection);
1894
1895         set_rc_data (pdf_document, rc);
1896
1897         retval = poppler_page_get_selection_region ((PopplerPage *)rc->data,
1898                                                     rc->scale,
1899 #if 0
1900                                                     (PopplerSelectionStyle)style,
1901 #endif
1902                                                     (PopplerRectangle *) points);
1903         return retval;
1904 }
1905
1906 static GdkRegion *
1907 pdf_selection_get_selection_map (EvSelection     *selection,
1908                                  EvRenderContext *rc)
1909 {
1910         PdfDocument *pdf_document;
1911         PopplerPage *poppler_page;
1912         PopplerRectangle points;
1913         GdkRegion *retval;
1914
1915         pdf_document = PDF_DOCUMENT (selection);
1916         poppler_page = poppler_document_get_page (pdf_document->document,
1917                                                   rc->page);
1918
1919         points.x1 = 0.0;
1920         points.y1 = 0.0;
1921         poppler_page_get_size (poppler_page, &(points.x2), &(points.y2));
1922         retval = poppler_page_get_selection_region (poppler_page, 1.0,
1923 #if 0
1924                                                     POPPLER_SELECTION_GLYPH,
1925 #endif
1926                                                     &points);
1927         g_object_unref (poppler_page);
1928
1929         return retval;
1930 }
1931
1932 static void
1933 pdf_selection_iface_init (EvSelectionIface *iface)
1934 {
1935         iface->render_selection = pdf_selection_render_selection;
1936         iface->get_selected_text = pdf_selection_get_selected_text;
1937         iface->get_selection_region = pdf_selection_get_selection_region;
1938         iface->get_selection_map = pdf_selection_get_selection_map;
1939 }
1940
1941 /* Page Transitions */
1942 static gdouble
1943 pdf_document_get_page_duration (EvDocumentTransition *trans,
1944                                 gint                  page)
1945 {
1946         PdfDocument *pdf_document;
1947         PopplerPage *poppler_page;
1948         gdouble      duration = -1;
1949
1950         pdf_document = PDF_DOCUMENT (trans);
1951         poppler_page = poppler_document_get_page (pdf_document->document, page);
1952         if (!poppler_page)
1953                 return -1;
1954
1955         duration = poppler_page_get_duration (poppler_page);
1956         g_object_unref (poppler_page);
1957
1958         return duration;
1959 }
1960
1961 static void
1962 pdf_document_page_transition_iface_init (EvDocumentTransitionIface *iface)
1963 {
1964         iface->get_page_duration = pdf_document_get_page_duration;
1965 }
1966
1967 PdfDocument *
1968 pdf_document_new (void)
1969 {
1970         return PDF_DOCUMENT (g_object_new (PDF_TYPE_DOCUMENT, NULL));
1971 }
1972
1973 /* Forms */
1974 static void
1975 pdf_document_get_crop_box (EvDocument  *document, 
1976                            int          page, 
1977                            EvRectangle *rect)
1978 {
1979         PdfDocument *pdf_document;
1980         PopplerPage *poppler_page;
1981         PopplerRectangle poppler_rect;
1982
1983         pdf_document = PDF_DOCUMENT (document);
1984         poppler_page = poppler_document_get_page (pdf_document->document, page);
1985         poppler_page_get_crop_box (poppler_page, &poppler_rect);
1986         rect->x1 = poppler_rect.x1;
1987         rect->x2 = poppler_rect.x2;
1988         rect->y1 = poppler_rect.y1;
1989         rect->y2 = poppler_rect.y2;
1990 }
1991
1992 #ifdef HAVE_FORMS
1993 static EvFormField *
1994 ev_form_field_from_poppler_field (PopplerFormField *poppler_field)
1995 {
1996         EvFormField *ev_field = NULL;
1997         gint         id;
1998         gdouble      font_size;
1999         gboolean     is_read_only;
2000
2001         id = poppler_form_field_get_id (poppler_field);
2002         font_size = poppler_form_field_get_font_size (poppler_field);
2003         is_read_only = poppler_form_field_is_read_only (poppler_field);
2004
2005         switch (poppler_form_field_get_field_type (poppler_field)) {
2006                 case POPPLER_FORM_FIELD_TEXT: {
2007                         EvFormFieldText    *field_text;
2008                         EvFormFieldTextType ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2009
2010                         switch (poppler_form_field_text_get_text_type (poppler_field)) {
2011                                 case POPPLER_FORM_TEXT_NORMAL:
2012                                         ev_text_type = EV_FORM_FIELD_TEXT_NORMAL;
2013                                         break;
2014                                 case POPPLER_FORM_TEXT_MULTILINE:
2015                                         ev_text_type = EV_FORM_FIELD_TEXT_MULTILINE;
2016                                         break;
2017                                 case POPPLER_FORM_TEXT_FILE_SELECT:
2018                                         ev_text_type = EV_FORM_FIELD_TEXT_FILE_SELECT;
2019                                         break;
2020                         }
2021                         
2022                         ev_field = ev_form_field_text_new (id, ev_text_type);
2023                         field_text = EV_FORM_FIELD_TEXT (ev_field);
2024
2025                         field_text->do_spell_check = poppler_form_field_text_do_spell_check (poppler_field);
2026                         field_text->do_scroll = poppler_form_field_text_do_scroll (poppler_field);
2027                         field_text->is_rich_text = poppler_form_field_text_is_rich_text (poppler_field);
2028                         field_text->is_password = poppler_form_field_text_is_password (poppler_field);
2029                         
2030 #ifdef HAVE_POPPLER_FORM_FIELD_TEXT_GET_MAX_LEN
2031                         field_text->max_len = poppler_form_field_text_get_max_len (poppler_field);
2032 #endif
2033                         field_text->text = poppler_form_field_text_get_text (poppler_field);
2034
2035                 }
2036                         break;
2037                 case POPPLER_FORM_FIELD_BUTTON: {
2038                         EvFormFieldButton    *field_button;
2039                         EvFormFieldButtonType ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2040
2041                         switch (poppler_form_field_button_get_button_type (poppler_field)) {
2042                                 case POPPLER_FORM_BUTTON_PUSH:
2043                                         ev_button_type = EV_FORM_FIELD_BUTTON_PUSH;
2044                                         break;
2045                                 case POPPLER_FORM_BUTTON_CHECK:
2046                                         ev_button_type = EV_FORM_FIELD_BUTTON_CHECK;
2047                                         break;
2048                                 case POPPLER_FORM_BUTTON_RADIO:
2049                                         ev_button_type = EV_FORM_FIELD_BUTTON_RADIO;
2050                                         break;
2051                         }
2052
2053                         ev_field = ev_form_field_button_new (id, ev_button_type);
2054                         field_button = EV_FORM_FIELD_BUTTON (ev_field);
2055                         
2056                         field_button->state = poppler_form_field_button_get_state (poppler_field);
2057                 }
2058                         break;
2059                 case POPPLER_FORM_FIELD_CHOICE: {
2060                         EvFormFieldChoice    *field_choice;
2061                         EvFormFieldChoiceType ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2062
2063                         switch (poppler_form_field_choice_get_choice_type (poppler_field)) {
2064                                 case POPPLER_FORM_CHOICE_COMBO:
2065                                         ev_choice_type = EV_FORM_FIELD_CHOICE_COMBO;
2066                                         break;
2067                                 case EV_FORM_FIELD_CHOICE_LIST:
2068                                         ev_choice_type = EV_FORM_FIELD_CHOICE_LIST;
2069                                         break;
2070                         }
2071
2072                         ev_field = ev_form_field_choice_new (id, ev_choice_type);
2073                         field_choice = EV_FORM_FIELD_CHOICE (ev_field);
2074
2075                         field_choice->is_editable = poppler_form_field_choice_is_editable (poppler_field);
2076                         field_choice->multi_select = poppler_form_field_choice_can_select_multiple (poppler_field);
2077                         field_choice->do_spell_check = poppler_form_field_choice_do_spell_check (poppler_field);
2078                         field_choice->commit_on_sel_change = poppler_form_field_choice_commit_on_change (poppler_field);
2079
2080                         /* TODO: we need poppler_form_field_choice_get_selected_items in poppler 
2081                         field_choice->selected_items = poppler_form_field_choice_get_selected_items (poppler_field);*/
2082                         if (field_choice->is_editable)
2083                                 field_choice->text = poppler_form_field_choice_get_text (poppler_field);
2084                 }
2085                         break;
2086                 case POPPLER_FORM_FIELD_SIGNATURE:
2087                         /* TODO */
2088                         ev_field = ev_form_field_signature_new (id);
2089                         break;
2090                 case POPPLER_FORM_FIELD_UNKNOWN:
2091                         break;
2092         }
2093
2094         ev_field->font_size = font_size;
2095         ev_field->is_read_only = is_read_only;
2096
2097         return ev_field;
2098 }
2099
2100 static GList *
2101 pdf_document_forms_get_form_fields (EvDocumentForms *document, 
2102                                     gint             page)
2103 {
2104         PdfDocument *pdf_document;
2105         PopplerPage *poppler_page;
2106         GList *retval = NULL;
2107         GList *fields;
2108         GList *list;
2109         double height;
2110         
2111
2112         pdf_document = PDF_DOCUMENT (document);
2113         poppler_page = poppler_document_get_page (pdf_document->document, page);
2114         fields = poppler_page_get_form_field_mapping (poppler_page);
2115         poppler_page_get_size (poppler_page, NULL, &height);
2116
2117         for (list = fields; list; list = list->next) {
2118                 PopplerFormFieldMapping *mapping;
2119                 EvFormFieldMapping *field_mapping;
2120                 
2121                 mapping = (PopplerFormFieldMapping *)list->data;
2122
2123                 field_mapping = g_new0 (EvFormFieldMapping, 1);
2124                 field_mapping->x1 = mapping->area.x1;
2125                 field_mapping->x2 = mapping->area.x2;
2126                 field_mapping->y1 = height - mapping->area.y2;
2127                 field_mapping->y2 = height - mapping->area.y1;
2128                 field_mapping->field = ev_form_field_from_poppler_field (mapping->field);
2129                 field_mapping->field->page = page;
2130                 
2131                 retval = g_list_prepend (retval, field_mapping);
2132         }
2133         poppler_page_free_form_field_mapping (fields);
2134         g_object_unref (poppler_page);
2135
2136         return g_list_reverse (retval);
2137 }
2138
2139 static gchar *
2140 pdf_document_forms_form_field_text_get_text (EvDocumentForms *document,
2141                                              EvFormField     *field)
2142         
2143 {
2144         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2145         PopplerFormField *poppler_field;
2146         gchar *text;
2147
2148         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2149         if (!poppler_field)
2150                 return NULL;
2151         
2152         text = poppler_form_field_text_get_text (poppler_field);
2153         g_object_unref (poppler_field);
2154
2155         return text;
2156 }
2157
2158 static void
2159 pdf_document_forms_form_field_text_set_text (EvDocumentForms *document, 
2160                                              EvFormField     *field,
2161                                              const gchar     *text)
2162 {
2163         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2164         PopplerFormField *poppler_field;
2165
2166         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2167         if (!poppler_field)
2168                 return;
2169         poppler_form_field_text_set_text (poppler_field, text);
2170         g_object_unref (poppler_field);
2171 }
2172
2173 static void
2174 pdf_document_forms_form_field_button_set_state (EvDocumentForms *document, 
2175                                                 EvFormField     *field,
2176                                                 gboolean         state)
2177 {
2178         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2179         PopplerFormField *poppler_field;
2180
2181         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2182         if (!poppler_field)
2183                 return;
2184         
2185         poppler_form_field_button_set_state (poppler_field, state);
2186         g_object_unref (poppler_field);
2187 }
2188
2189 static gboolean
2190 pdf_document_forms_form_field_button_get_state (EvDocumentForms *document, 
2191                                                 EvFormField     *field)
2192 {
2193         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2194         PopplerFormField *poppler_field;
2195         gboolean state;
2196
2197         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2198         if (!poppler_field)
2199                 return FALSE;
2200
2201         state = poppler_form_field_button_get_state (poppler_field);
2202         g_object_unref (poppler_field);
2203
2204         return state;
2205 }
2206
2207 static gchar *
2208 pdf_document_forms_form_field_choice_get_item (EvDocumentForms *document, 
2209                                                EvFormField     *field,
2210                                                gint             index)
2211 {
2212         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2213         PopplerFormField *poppler_field;
2214         gchar *text;
2215
2216         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2217         if (!poppler_field)
2218                 return NULL;
2219
2220         text = poppler_form_field_choice_get_item (poppler_field, index);
2221         g_object_unref (poppler_field);
2222
2223         return text;
2224 }
2225
2226 static int
2227 pdf_document_forms_form_field_choice_get_n_items (EvDocumentForms *document, 
2228                                                   EvFormField     *field)
2229 {
2230         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2231         PopplerFormField *poppler_field;
2232         gint n_items;
2233
2234         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2235         if (!poppler_field)
2236                 return -1;
2237         
2238         n_items = poppler_form_field_choice_get_n_items (poppler_field);
2239         g_object_unref (poppler_field);
2240
2241         return n_items;
2242 }
2243
2244 static gboolean
2245 pdf_document_forms_form_field_choice_is_item_selected (EvDocumentForms *document, 
2246                                                        EvFormField     *field,
2247                                                        gint             index)
2248 {
2249         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2250         PopplerFormField *poppler_field;
2251         gboolean selected;
2252
2253         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2254         if (!poppler_field)
2255                 return FALSE;
2256
2257         selected = poppler_form_field_choice_is_item_selected (poppler_field, index);
2258         g_object_unref (poppler_field);
2259
2260         return selected;
2261 }
2262
2263 static void
2264 pdf_document_forms_form_field_choice_select_item (EvDocumentForms *document, 
2265                                                   EvFormField     *field,
2266                                                   gint             index)
2267 {
2268         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2269         PopplerFormField *poppler_field;
2270
2271         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2272         if (!poppler_field)
2273                 return;
2274
2275         poppler_form_field_choice_select_item (poppler_field, index);
2276         g_object_unref (poppler_field);
2277 }
2278
2279 static void
2280 pdf_document_forms_form_field_choice_toggle_item (EvDocumentForms *document, 
2281                                                   EvFormField     *field,
2282                                                   gint             index)
2283 {
2284         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2285         PopplerFormField *poppler_field;
2286
2287         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2288         if (!poppler_field)
2289                 return;
2290
2291         poppler_form_field_choice_toggle_item (poppler_field, index);
2292         g_object_unref (poppler_field);
2293 }
2294
2295 static void
2296 pdf_document_forms_form_field_choice_unselect_all (EvDocumentForms *document, 
2297                                                    EvFormField     *field)
2298 {
2299         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2300         PopplerFormField *poppler_field;
2301
2302         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2303         if (!poppler_field)
2304                 return;
2305         
2306         poppler_form_field_choice_unselect_all (poppler_field);
2307         g_object_unref (poppler_field);
2308 }
2309
2310 static void
2311 pdf_document_forms_form_field_choice_set_text (EvDocumentForms *document,
2312                                                EvFormField     *field,
2313                                                const gchar     *text)
2314 {
2315         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2316         PopplerFormField *poppler_field;
2317
2318         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2319         if (!poppler_field)
2320                 return;
2321         
2322         poppler_form_field_choice_set_text (poppler_field, text);
2323         g_object_unref (poppler_field);
2324 }
2325
2326 static gchar *
2327 pdf_document_forms_form_field_choice_get_text (EvDocumentForms *document,
2328                                                EvFormField     *field)
2329 {
2330         PdfDocument *pdf_document = PDF_DOCUMENT (document);
2331         PopplerFormField *poppler_field;
2332         gchar *text;
2333
2334         poppler_field = poppler_document_get_form_field (pdf_document->document, field->id);
2335         if (!poppler_field)
2336                 return NULL;
2337
2338         text = poppler_form_field_choice_get_text (poppler_field);
2339         g_object_unref (poppler_field);
2340
2341         return text;
2342 }
2343
2344 static void
2345 pdf_document_document_forms_iface_init (EvDocumentFormsIface *iface)
2346 {
2347         iface->get_form_fields = pdf_document_forms_get_form_fields;
2348         iface->form_field_text_get_text = pdf_document_forms_form_field_text_get_text;
2349         iface->form_field_text_set_text = pdf_document_forms_form_field_text_set_text;
2350         iface->form_field_button_set_state = pdf_document_forms_form_field_button_set_state;
2351         iface->form_field_button_get_state = pdf_document_forms_form_field_button_get_state;
2352         iface->form_field_choice_get_item = pdf_document_forms_form_field_choice_get_item;
2353         iface->form_field_choice_get_n_items = pdf_document_forms_form_field_choice_get_n_items;
2354         iface->form_field_choice_is_item_selected = pdf_document_forms_form_field_choice_is_item_selected;
2355         iface->form_field_choice_select_item = pdf_document_forms_form_field_choice_select_item;
2356         iface->form_field_choice_toggle_item = pdf_document_forms_form_field_choice_toggle_item;
2357         iface->form_field_choice_unselect_all = pdf_document_forms_form_field_choice_unselect_all;
2358         iface->form_field_choice_set_text = pdf_document_forms_form_field_choice_set_text;
2359         iface->form_field_choice_get_text = pdf_document_forms_form_field_choice_get_text;
2360 }
2361 #endif /* HAVE_FORMS */