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