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