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