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